Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 1 | //===- yaml2elf - Convert YAML to a ELF object file -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief The ELF component of yaml2obj. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "yaml2obj.h" |
| 16 | #include "llvm/Object/ELF.h" |
| 17 | #include "llvm/Object/ELFYAML.h" |
| 18 | #include "llvm/Support/ELF.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #include "llvm/Support/YAMLTraits.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 25 | // There is similar code in yaml2coff, but with some slight COFF-specific |
| 26 | // variations like different initial state. Might be able to deduplicate |
| 27 | // some day, but also want to make sure that the Mach-O use case is served. |
| 28 | // |
| 29 | // This class has a deliberately small interface, since a lot of |
| 30 | // implementation variation is possible. |
| 31 | // |
| 32 | // TODO: Use an ordered container with a suffix-based comparison in order |
| 33 | // to deduplicate suffixes. std::map<> with a custom comparator is likely |
| 34 | // to be the simplest implementation, but a suffix trie could be more |
| 35 | // suitable for the job. |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 36 | namespace { |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 37 | class StringTableBuilder { |
| 38 | /// \brief Indices of strings currently present in `Buf`. |
| 39 | StringMap<unsigned> StringIndices; |
| 40 | /// \brief The contents of the string table as we build it. |
| 41 | std::string Buf; |
| 42 | public: |
| 43 | StringTableBuilder() { |
| 44 | Buf.push_back('\0'); |
| 45 | } |
| 46 | /// \returns Index of string in string table. |
| 47 | unsigned addString(StringRef S) { |
| 48 | StringMapEntry<unsigned> &Entry = StringIndices.GetOrCreateValue(S); |
| 49 | unsigned &I = Entry.getValue(); |
| 50 | if (I != 0) |
| 51 | return I; |
| 52 | I = Buf.size(); |
| 53 | Buf.append(S.begin(), S.end()); |
| 54 | Buf.push_back('\0'); |
| 55 | return I; |
| 56 | } |
| 57 | size_t size() const { |
| 58 | return Buf.size(); |
| 59 | } |
| 60 | void writeToStream(raw_ostream &OS) { |
| 61 | OS.write(Buf.data(), Buf.size()); |
| 62 | } |
| 63 | }; |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 64 | } // end anonymous namespace |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 65 | |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 66 | // This class is used to build up a contiguous binary blob while keeping |
| 67 | // track of an offset in the output (which notionally begins at |
| 68 | // `InitialOffset`). |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 69 | namespace { |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 70 | class ContiguousBlobAccumulator { |
| 71 | const uint64_t InitialOffset; |
Sean Silva | bd3bc69 | 2013-06-20 19:11:41 +0000 | [diff] [blame] | 72 | SmallVector<char, 128> Buf; |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 73 | raw_svector_ostream OS; |
| 74 | |
| 75 | public: |
Sean Silva | bd3bc69 | 2013-06-20 19:11:41 +0000 | [diff] [blame] | 76 | ContiguousBlobAccumulator(uint64_t InitialOffset_) |
| 77 | : InitialOffset(InitialOffset_), Buf(), OS(Buf) {} |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 78 | raw_ostream &getOS() { return OS; } |
| 79 | uint64_t currentOffset() const { return InitialOffset + OS.tell(); } |
| 80 | void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); } |
| 81 | }; |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 82 | } // end anonymous namespace |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 83 | |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 84 | // Used to keep track of section names, so that in the YAML file sections |
| 85 | // can be referenced by name instead of by index. |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 86 | namespace { |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 87 | class SectionNameToIdxMap { |
| 88 | StringMap<int> Map; |
| 89 | public: |
| 90 | /// \returns true if name is already present in the map. |
| 91 | bool addName(StringRef SecName, unsigned i) { |
| 92 | StringMapEntry<int> &Entry = Map.GetOrCreateValue(SecName, -1); |
| 93 | if (Entry.getValue() != -1) |
| 94 | return true; |
| 95 | Entry.setValue((int)i); |
| 96 | return false; |
| 97 | } |
| 98 | /// \returns true if name is not present in the map |
| 99 | bool lookupSection(StringRef SecName, unsigned &Idx) const { |
| 100 | StringMap<int>::const_iterator I = Map.find(SecName); |
| 101 | if (I == Map.end()) |
| 102 | return true; |
| 103 | Idx = I->getValue(); |
| 104 | return false; |
| 105 | } |
| 106 | }; |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 107 | } // end anonymous namespace |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 108 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 109 | template <class T> |
| 110 | static size_t vectorDataSize(const std::vector<T> &Vec) { |
| 111 | return Vec.size() * sizeof(T); |
| 112 | } |
| 113 | |
| 114 | template <class T> |
| 115 | static void writeVectorData(raw_ostream &OS, const std::vector<T> &Vec) { |
| 116 | OS.write((const char *)Vec.data(), vectorDataSize(Vec)); |
| 117 | } |
| 118 | |
| 119 | template <class T> |
| 120 | static void zero(T &Obj) { |
| 121 | memset(&Obj, 0, sizeof(Obj)); |
| 122 | } |
| 123 | |
Sean Silva | 85d3eeb | 2013-06-18 01:11:27 +0000 | [diff] [blame] | 124 | /// \brief Create a string table in `SHeader`, which we assume is already |
| 125 | /// zero'd. |
| 126 | template <class Elf_Shdr> |
| 127 | static void createStringTableSectionHeader(Elf_Shdr &SHeader, |
| 128 | StringTableBuilder &STB, |
| 129 | ContiguousBlobAccumulator &CBA) { |
| 130 | SHeader.sh_type = ELF::SHT_STRTAB; |
| 131 | SHeader.sh_offset = CBA.currentOffset(); |
| 132 | SHeader.sh_size = STB.size(); |
| 133 | STB.writeToStream(CBA.getOS()); |
| 134 | SHeader.sh_addralign = 1; |
| 135 | } |
| 136 | |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 137 | namespace { |
| 138 | /// \brief "Single point of truth" for the ELF file construction. |
| 139 | /// TODO: This class still has a ways to go before it is truly a "single |
| 140 | /// point of truth". |
| 141 | template <class ELFT> |
| 142 | class ELFState { |
| 143 | /// \brief The future ".strtab" section. |
| 144 | StringTableBuilder DotStrtab; |
| 145 | /// \brief The section number of the ".strtab" section. |
| 146 | unsigned DotStrtabSecNo; |
| 147 | /// \brief The accumulated contents of all sections so far. |
| 148 | ContiguousBlobAccumulator &SectionContentAccum; |
| 149 | typedef typename object::ELFObjectFile<ELFT>::Elf_Ehdr Elf_Ehdr; |
| 150 | /// \brief The ELF file header. |
| 151 | Elf_Ehdr &Header; |
| 152 | |
Sean Silva | c1c290b | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 153 | SectionNameToIdxMap &SN2I; |
| 154 | |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 155 | public: |
| 156 | |
| 157 | ELFState(Elf_Ehdr &Header_, ContiguousBlobAccumulator &Accum, |
Sean Silva | c1c290b | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 158 | unsigned DotStrtabSecNo_, SectionNameToIdxMap &SN2I_) |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 159 | : DotStrtab(), DotStrtabSecNo(DotStrtabSecNo_), |
Sean Silva | c1c290b | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 160 | SectionContentAccum(Accum), Header(Header_), SN2I(SN2I_) {} |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 161 | |
| 162 | unsigned getDotStrTabSecNo() const { return DotStrtabSecNo; } |
| 163 | StringTableBuilder &getStringTable() { return DotStrtab; } |
| 164 | ContiguousBlobAccumulator &getSectionContentAccum() { |
| 165 | return SectionContentAccum; |
| 166 | } |
Sean Silva | c1c290b | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 167 | SectionNameToIdxMap &getSN2I() { return SN2I; } |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 168 | }; |
| 169 | } // end anonymous namespace |
| 170 | |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame^] | 171 | // FIXME: At this point it is fairly clear that we need to refactor these |
| 172 | // static functions into methods of a class sharing some typedefs. These |
| 173 | // ELF type names are insane. |
| 174 | template <class ELFT, |
| 175 | class Elf_Sym = typename object::ELFObjectFile<ELFT>::Elf_Sym> |
| 176 | static void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, |
| 177 | ELFState<ELFT> &State, std::vector<Elf_Sym> &Syms, |
| 178 | unsigned SymbolBinding) { |
| 179 | for (unsigned i = 0, e = Symbols.size(); i != e; ++i) { |
| 180 | const ELFYAML::Symbol &Sym = Symbols[i]; |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 181 | Elf_Sym Symbol; |
| 182 | zero(Symbol); |
| 183 | if (!Sym.Name.empty()) |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 184 | Symbol.st_name = State.getStringTable().addString(Sym.Name); |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame^] | 185 | Symbol.setBindingAndType(SymbolBinding, Sym.Type); |
Sean Silva | 9818622 | 2013-06-20 20:59:41 +0000 | [diff] [blame] | 186 | unsigned Index; |
| 187 | if (State.getSN2I().lookupSection(Sym.Section, Index)) { |
| 188 | errs() << "error: Unknown section referenced: '" << Sym.Section |
| 189 | << "' by YAML symbol " << Sym.Name << ".\n"; |
| 190 | exit(1); |
| 191 | } |
| 192 | Symbol.st_shndx = Index; |
Sean Silva | 05001b9 | 2013-06-20 20:59:47 +0000 | [diff] [blame] | 193 | Symbol.st_value = Sym.Value; |
| 194 | Symbol.st_size = Sym.Size; |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 195 | Syms.push_back(Symbol); |
| 196 | } |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame^] | 197 | } |
| 198 | |
| 199 | template <class ELFT> |
| 200 | static void handleSymtabSectionHeader( |
| 201 | const ELFYAML::Section &Sec, ELFState<ELFT> &State, |
| 202 | typename object::ELFObjectFile<ELFT>::Elf_Shdr &SHeader) { |
| 203 | |
| 204 | typedef typename object::ELFObjectFile<ELFT>::Elf_Sym Elf_Sym; |
| 205 | // TODO: Ensure that a manually specified `Link` field is diagnosed as an |
| 206 | // error for SHT_SYMTAB. |
| 207 | SHeader.sh_link = State.getDotStrTabSecNo(); |
| 208 | // One greater than symbol table index of the last local symbol. |
| 209 | SHeader.sh_info = Sec.Symbols.Local.size() + 1; |
| 210 | SHeader.sh_entsize = sizeof(Elf_Sym); |
| 211 | |
| 212 | std::vector<Elf_Sym> Syms; |
| 213 | { |
| 214 | // Ensure STN_UNDEF is present |
| 215 | Elf_Sym Sym; |
| 216 | zero(Sym); |
| 217 | Syms.push_back(Sym); |
| 218 | } |
| 219 | addSymbols(Sec.Symbols.Local, State, Syms, ELF::STB_LOCAL); |
| 220 | addSymbols(Sec.Symbols.Global, State, Syms, ELF::STB_GLOBAL); |
| 221 | addSymbols(Sec.Symbols.Weak, State, Syms, ELF::STB_WEAK); |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 222 | |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 223 | ContiguousBlobAccumulator &CBA = State.getSectionContentAccum(); |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 224 | SHeader.sh_offset = CBA.currentOffset(); |
| 225 | SHeader.sh_size = vectorDataSize(Syms); |
| 226 | writeVectorData(CBA.getOS(), Syms); |
| 227 | } |
| 228 | |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 229 | template <class ELFT> |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 230 | static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) { |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 231 | using namespace llvm::ELF; |
Sean Silva | 67416d7 | 2013-06-19 01:13:28 +0000 | [diff] [blame] | 232 | typedef typename object::ELFObjectFile<ELFT>::Elf_Ehdr Elf_Ehdr; |
| 233 | typedef typename object::ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 234 | |
| 235 | const ELFYAML::FileHeader &Hdr = Doc.Header; |
| 236 | |
| 237 | Elf_Ehdr Header; |
| 238 | zero(Header); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 239 | Header.e_ident[EI_MAG0] = 0x7f; |
| 240 | Header.e_ident[EI_MAG1] = 'E'; |
| 241 | Header.e_ident[EI_MAG2] = 'L'; |
| 242 | Header.e_ident[EI_MAG3] = 'F'; |
| 243 | Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; |
| 244 | bool IsLittleEndian = ELFT::TargetEndianness == support::little; |
| 245 | Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB; |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 246 | Header.e_ident[EI_VERSION] = EV_CURRENT; |
Sean Silva | b3a013a | 2013-06-19 01:10:58 +0000 | [diff] [blame] | 247 | Header.e_ident[EI_OSABI] = Hdr.OSABI; |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 248 | Header.e_ident[EI_ABIVERSION] = 0; |
| 249 | Header.e_type = Hdr.Type; |
| 250 | Header.e_machine = Hdr.Machine; |
| 251 | Header.e_version = EV_CURRENT; |
| 252 | Header.e_entry = Hdr.Entry; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 253 | Header.e_ehsize = sizeof(Elf_Ehdr); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 254 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 255 | // TODO: Flesh out section header support. |
| 256 | // TODO: Program headers. |
| 257 | |
| 258 | Header.e_shentsize = sizeof(Elf_Shdr); |
| 259 | // Immediately following the ELF header. |
| 260 | Header.e_shoff = sizeof(Header); |
Sean Silva | bdf1986 | 2013-06-18 23:37:23 +0000 | [diff] [blame] | 261 | const std::vector<ELFYAML::Section> &Sections = Doc.Sections; |
| 262 | // "+ 3" for |
| 263 | // - SHT_NULL entry (placed first, i.e. 0'th entry) |
| 264 | // - string table (.strtab) (placed second to last) |
| 265 | // - section header string table. (placed last) |
| 266 | Header.e_shnum = Sections.size() + 3; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 267 | // Place section header string table last. |
Sean Silva | bdf1986 | 2013-06-18 23:37:23 +0000 | [diff] [blame] | 268 | Header.e_shstrndx = Header.e_shnum - 1; |
| 269 | const unsigned DotStrtabSecNo = Header.e_shnum - 2; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 270 | |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 271 | // XXX: This offset is tightly coupled with the order that we write |
| 272 | // things to `OS`. |
| 273 | const size_t SectionContentBeginOffset = |
| 274 | Header.e_ehsize + Header.e_shentsize * Header.e_shnum; |
| 275 | ContiguousBlobAccumulator CBA(SectionContentBeginOffset); |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 276 | SectionNameToIdxMap SN2I; |
| 277 | for (unsigned i = 0, e = Sections.size(); i != e; ++i) { |
| 278 | StringRef Name = Sections[i].Name; |
| 279 | if (Name.empty()) |
| 280 | continue; |
Sean Silva | bdf1986 | 2013-06-18 23:37:23 +0000 | [diff] [blame] | 281 | // "+ 1" to take into account the SHT_NULL entry. |
| 282 | if (SN2I.addName(Name, i + 1)) { |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 283 | errs() << "error: Repeated section name: '" << Name |
| 284 | << "' at YAML section number " << i << ".\n"; |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 285 | return 1; |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Sean Silva | c1c290b | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 289 | ELFState<ELFT> State(Header, CBA, DotStrtabSecNo, SN2I); |
| 290 | |
Sean Silva | fde4ab0 | 2013-06-18 01:11:24 +0000 | [diff] [blame] | 291 | StringTableBuilder SHStrTab; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 292 | std::vector<Elf_Shdr> SHeaders; |
Sean Silva | bdf1986 | 2013-06-18 23:37:23 +0000 | [diff] [blame] | 293 | { |
| 294 | // Ensure SHN_UNDEF entry is present. An all-zero section header is a |
| 295 | // valid SHN_UNDEF entry since SHT_NULL == 0. |
| 296 | Elf_Shdr SHdr; |
| 297 | zero(SHdr); |
| 298 | SHeaders.push_back(SHdr); |
| 299 | } |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 300 | for (unsigned i = 0, e = Sections.size(); i != e; ++i) { |
| 301 | const ELFYAML::Section &Sec = Sections[i]; |
| 302 | Elf_Shdr SHeader; |
| 303 | zero(SHeader); |
Sean Silva | fde4ab0 | 2013-06-18 01:11:24 +0000 | [diff] [blame] | 304 | SHeader.sh_name = SHStrTab.addString(Sec.Name); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 305 | SHeader.sh_type = Sec.Type; |
| 306 | SHeader.sh_flags = Sec.Flags; |
Sean Silva | f4bfced | 2013-06-13 22:19:54 +0000 | [diff] [blame] | 307 | SHeader.sh_addr = Sec.Address; |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 308 | |
| 309 | SHeader.sh_offset = CBA.currentOffset(); |
| 310 | SHeader.sh_size = Sec.Content.binary_size(); |
| 311 | Sec.Content.writeAsBinary(CBA.getOS()); |
| 312 | |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 313 | if (!Sec.Link.empty()) { |
| 314 | unsigned Index; |
| 315 | if (SN2I.lookupSection(Sec.Link, Index)) { |
| 316 | errs() << "error: Unknown section referenced: '" << Sec.Link |
| 317 | << "' at YAML section number " << i << ".\n"; |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 318 | return 1; |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 319 | } |
| 320 | SHeader.sh_link = Index; |
| 321 | } |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 322 | SHeader.sh_info = 0; |
Sean Silva | 0a409cf | 2013-06-14 00:38:02 +0000 | [diff] [blame] | 323 | SHeader.sh_addralign = Sec.AddressAlign; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 324 | SHeader.sh_entsize = 0; |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 325 | // XXX: Really ugly right now. Should not be writing to `CBA` above |
| 326 | // (and setting sh_offset and sh_size) when going through this branch |
| 327 | // here. |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 328 | if (Sec.Type == ELFYAML::ELF_SHT(SHT_SYMTAB)) |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 329 | handleSymtabSectionHeader<ELFT>(Sec, State, SHeader); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 330 | SHeaders.push_back(SHeader); |
| 331 | } |
| 332 | |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 333 | // .strtab string table header. |
Sean Silva | c313192 | 2013-06-18 21:37:50 +0000 | [diff] [blame] | 334 | Elf_Shdr DotStrTabSHeader; |
| 335 | zero(DotStrTabSHeader); |
| 336 | DotStrTabSHeader.sh_name = SHStrTab.addString(StringRef(".strtab")); |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 337 | createStringTableSectionHeader(DotStrTabSHeader, State.getStringTable(), CBA); |
Sean Silva | c313192 | 2013-06-18 21:37:50 +0000 | [diff] [blame] | 338 | |
Sean Silva | fde4ab0 | 2013-06-18 01:11:24 +0000 | [diff] [blame] | 339 | // Section header string table header. |
| 340 | Elf_Shdr SHStrTabSHeader; |
| 341 | zero(SHStrTabSHeader); |
Sean Silva | 85d3eeb | 2013-06-18 01:11:27 +0000 | [diff] [blame] | 342 | createStringTableSectionHeader(SHStrTabSHeader, SHStrTab, CBA); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 343 | |
| 344 | OS.write((const char *)&Header, sizeof(Header)); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 345 | writeVectorData(OS, SHeaders); |
Sean Silva | c313192 | 2013-06-18 21:37:50 +0000 | [diff] [blame] | 346 | OS.write((const char *)&DotStrTabSHeader, sizeof(DotStrTabSHeader)); |
Sean Silva | fde4ab0 | 2013-06-18 01:11:24 +0000 | [diff] [blame] | 347 | OS.write((const char *)&SHStrTabSHeader, sizeof(SHStrTabSHeader)); |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 348 | CBA.writeBlobToStream(OS); |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 349 | return 0; |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) { |
| 353 | yaml::Input YIn(Buf->getBuffer()); |
| 354 | ELFYAML::Object Doc; |
| 355 | YIn >> Doc; |
| 356 | if (YIn.error()) { |
| 357 | errs() << "yaml2obj: Failed to parse YAML file!\n"; |
| 358 | return 1; |
| 359 | } |
| 360 | if (Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) { |
| 361 | if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB)) |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 362 | return writeELF<object::ELFType<support::little, 8, true> >(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 363 | else |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 364 | return writeELF<object::ELFType<support::big, 8, true> >(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 365 | } else { |
| 366 | if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB)) |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 367 | return writeELF<object::ELFType<support::little, 4, false> >(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 368 | else |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 369 | return writeELF<object::ELFType<support::big, 4, false> >(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 370 | } |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 371 | } |