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" |
Will Dietz | 0b48c73 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
Rafael Espindola | 97de474 | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 17 | #include "llvm/MC/StringTableBuilder.h" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ELFObjectFile.h" |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 19 | #include "llvm/Object/ELFYAML.h" |
| 20 | #include "llvm/Support/ELF.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include "llvm/Support/YAMLTraits.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 27 | // This class is used to build up a contiguous binary blob while keeping |
| 28 | // track of an offset in the output (which notionally begins at |
| 29 | // `InitialOffset`). |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 30 | namespace { |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 31 | class ContiguousBlobAccumulator { |
| 32 | const uint64_t InitialOffset; |
Sean Silva | bd3bc69 | 2013-06-20 19:11:41 +0000 | [diff] [blame] | 33 | SmallVector<char, 128> Buf; |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 34 | raw_svector_ostream OS; |
| 35 | |
Sean Silva | d93323f | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 36 | /// \returns The new offset. |
| 37 | uint64_t padToAlignment(unsigned Align) { |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame^] | 38 | if (Align == 0) |
| 39 | Align = 1; |
Sean Silva | d93323f | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 40 | uint64_t CurrentOffset = InitialOffset + OS.tell(); |
| 41 | uint64_t AlignedOffset = RoundUpToAlignment(CurrentOffset, Align); |
| 42 | for (; CurrentOffset != AlignedOffset; ++CurrentOffset) |
| 43 | OS.write('\0'); |
| 44 | return AlignedOffset; // == CurrentOffset; |
| 45 | } |
| 46 | |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 47 | public: |
Sean Silva | bd3bc69 | 2013-06-20 19:11:41 +0000 | [diff] [blame] | 48 | ContiguousBlobAccumulator(uint64_t InitialOffset_) |
| 49 | : InitialOffset(InitialOffset_), Buf(), OS(Buf) {} |
Sean Silva | d93323f | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 50 | template <class Integer> |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame^] | 51 | raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) { |
Sean Silva | d93323f | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 52 | Offset = padToAlignment(Align); |
| 53 | return OS; |
| 54 | } |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 55 | void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); } |
| 56 | }; |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 57 | } // end anonymous namespace |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 58 | |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 59 | // Used to keep track of section and symbol names, so that in the YAML file |
| 60 | // sections and symbols can be referenced by name instead of by index. |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 61 | namespace { |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 62 | class NameToIdxMap { |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 63 | StringMap<int> Map; |
| 64 | public: |
| 65 | /// \returns true if name is already present in the map. |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 66 | bool addName(StringRef Name, unsigned i) { |
David Blaikie | 5106ce7 | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 67 | return !Map.insert(std::make_pair(Name, (int)i)).second; |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 68 | } |
| 69 | /// \returns true if name is not present in the map |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 70 | bool lookup(StringRef Name, unsigned &Idx) const { |
| 71 | StringMap<int>::const_iterator I = Map.find(Name); |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 72 | if (I == Map.end()) |
| 73 | return true; |
| 74 | Idx = I->getValue(); |
| 75 | return false; |
| 76 | } |
| 77 | }; |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 78 | } // end anonymous namespace |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 79 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 80 | template <class T> |
Will Dietz | 0b48c73 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 81 | static size_t arrayDataSize(ArrayRef<T> A) { |
| 82 | return A.size() * sizeof(T); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | template <class T> |
Will Dietz | 0b48c73 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 86 | static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) { |
| 87 | OS.write((const char *)A.data(), arrayDataSize(A)); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | template <class T> |
| 91 | static void zero(T &Obj) { |
| 92 | memset(&Obj, 0, sizeof(Obj)); |
| 93 | } |
| 94 | |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 95 | namespace { |
| 96 | /// \brief "Single point of truth" for the ELF file construction. |
| 97 | /// TODO: This class still has a ways to go before it is truly a "single |
| 98 | /// point of truth". |
| 99 | template <class ELFT> |
| 100 | class ELFState { |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 101 | typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr; |
| 102 | typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 103 | typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 104 | typedef typename object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 105 | typedef typename object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 106 | |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 107 | /// \brief The future ".strtab" section. |
| 108 | StringTableBuilder DotStrtab; |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 109 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 110 | /// \brief The future ".shstrtab" section. |
| 111 | StringTableBuilder DotShStrtab; |
| 112 | |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 113 | NameToIdxMap SN2I; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 114 | NameToIdxMap SymN2I; |
Simon Atanasyan | 220c54a | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 115 | const ELFYAML::Object &Doc; |
Sean Silva | c1c290b | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 116 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 117 | bool buildSectionIndex(); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 118 | bool buildSymbolIndex(std::size_t &StartIndex, |
| 119 | const std::vector<ELFYAML::Symbol> &Symbols); |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 120 | void initELFHeader(Elf_Ehdr &Header); |
| 121 | bool initSectionHeaders(std::vector<Elf_Shdr> &SHeaders, |
| 122 | ContiguousBlobAccumulator &CBA); |
| 123 | void initSymtabSectionHeader(Elf_Shdr &SHeader, |
| 124 | ContiguousBlobAccumulator &CBA); |
| 125 | void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, |
| 126 | StringTableBuilder &STB, |
| 127 | ContiguousBlobAccumulator &CBA); |
| 128 | void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, |
| 129 | std::vector<Elf_Sym> &Syms, unsigned SymbolBinding); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 130 | void writeSectionContent(Elf_Shdr &SHeader, |
| 131 | const ELFYAML::RawContentSection &Section, |
| 132 | ContiguousBlobAccumulator &CBA); |
| 133 | bool writeSectionContent(Elf_Shdr &SHeader, |
| 134 | const ELFYAML::RelocationSection &Section, |
| 135 | ContiguousBlobAccumulator &CBA); |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 136 | bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group, |
| 137 | ContiguousBlobAccumulator &CBA); |
Simon Atanasyan | 04d9e65 | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 138 | bool writeSectionContent(Elf_Shdr &SHeader, |
| 139 | const ELFYAML::MipsABIFlags &Section, |
| 140 | ContiguousBlobAccumulator &CBA); |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 141 | |
Simon Atanasyan | 220c54a | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 142 | // - SHT_NULL entry (placed first, i.e. 0'th entry) |
| 143 | // - symbol table (.symtab) (placed third to last) |
| 144 | // - string table (.strtab) (placed second to last) |
| 145 | // - section header string table (.shstrtab) (placed last) |
| 146 | unsigned getDotSymTabSecNo() const { return Doc.Sections.size() + 1; } |
| 147 | unsigned getDotStrTabSecNo() const { return Doc.Sections.size() + 2; } |
| 148 | unsigned getDotShStrTabSecNo() const { return Doc.Sections.size() + 3; } |
| 149 | unsigned getSectionCount() const { return Doc.Sections.size() + 4; } |
| 150 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 151 | ELFState(const ELFYAML::Object &D) : Doc(D) {} |
Simon Atanasyan | 220c54a | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 152 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 153 | public: |
| 154 | static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc); |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 155 | }; |
| 156 | } // end anonymous namespace |
| 157 | |
Sean Silva | 37e817c | 2013-06-21 00:33:01 +0000 | [diff] [blame] | 158 | template <class ELFT> |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 159 | void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) { |
| 160 | using namespace llvm::ELF; |
| 161 | zero(Header); |
| 162 | Header.e_ident[EI_MAG0] = 0x7f; |
| 163 | Header.e_ident[EI_MAG1] = 'E'; |
| 164 | Header.e_ident[EI_MAG2] = 'L'; |
| 165 | Header.e_ident[EI_MAG3] = 'F'; |
| 166 | Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; |
| 167 | bool IsLittleEndian = ELFT::TargetEndianness == support::little; |
| 168 | Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB; |
| 169 | Header.e_ident[EI_VERSION] = EV_CURRENT; |
| 170 | Header.e_ident[EI_OSABI] = Doc.Header.OSABI; |
| 171 | Header.e_ident[EI_ABIVERSION] = 0; |
| 172 | Header.e_type = Doc.Header.Type; |
| 173 | Header.e_machine = Doc.Header.Machine; |
| 174 | Header.e_version = EV_CURRENT; |
| 175 | Header.e_entry = Doc.Header.Entry; |
| 176 | Header.e_flags = Doc.Header.Flags; |
| 177 | Header.e_ehsize = sizeof(Elf_Ehdr); |
| 178 | Header.e_shentsize = sizeof(Elf_Shdr); |
| 179 | // Immediately following the ELF header. |
| 180 | Header.e_shoff = sizeof(Header); |
| 181 | Header.e_shnum = getSectionCount(); |
| 182 | Header.e_shstrndx = getDotShStrTabSecNo(); |
| 183 | } |
| 184 | |
| 185 | template <class ELFT> |
| 186 | bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders, |
| 187 | ContiguousBlobAccumulator &CBA) { |
| 188 | // Ensure SHN_UNDEF entry is present. An all-zero section header is a |
| 189 | // valid SHN_UNDEF entry since SHT_NULL == 0. |
| 190 | Elf_Shdr SHeader; |
| 191 | zero(SHeader); |
| 192 | SHeaders.push_back(SHeader); |
| 193 | |
Hans Wennborg | 59f0cba | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 194 | for (const auto &Sec : Doc.Sections) |
| 195 | DotShStrtab.add(Sec->Name); |
Hans Wennborg | f26bfc1 | 2014-09-29 22:43:20 +0000 | [diff] [blame] | 196 | DotShStrtab.finalize(StringTableBuilder::ELF); |
Hans Wennborg | 59f0cba | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 197 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 198 | for (const auto &Sec : Doc.Sections) { |
| 199 | zero(SHeader); |
Hans Wennborg | 59f0cba | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 200 | SHeader.sh_name = DotShStrtab.getOffset(Sec->Name); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 201 | SHeader.sh_type = Sec->Type; |
| 202 | SHeader.sh_flags = Sec->Flags; |
| 203 | SHeader.sh_addr = Sec->Address; |
| 204 | SHeader.sh_addralign = Sec->AddressAlign; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 205 | |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 206 | if (!Sec->Link.empty()) { |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 207 | unsigned Index; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 208 | if (SN2I.lookup(Sec->Link, Index)) { |
| 209 | errs() << "error: Unknown section referenced: '" << Sec->Link |
| 210 | << "' at YAML section '" << Sec->Name << "'.\n"; |
| 211 | return false; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 212 | } |
| 213 | SHeader.sh_link = Index; |
| 214 | } |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 215 | |
| 216 | if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec.get())) |
| 217 | writeSectionContent(SHeader, *S, CBA); |
| 218 | else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec.get())) { |
| 219 | if (S->Link.empty()) |
| 220 | // For relocation section set link to .symtab by default. |
| 221 | SHeader.sh_link = getDotSymTabSecNo(); |
| 222 | |
| 223 | unsigned Index; |
| 224 | if (SN2I.lookup(S->Info, Index)) { |
| 225 | errs() << "error: Unknown section referenced: '" << S->Info |
| 226 | << "' at YAML section '" << S->Name << "'.\n"; |
| 227 | return false; |
| 228 | } |
| 229 | SHeader.sh_info = Index; |
| 230 | |
| 231 | if (!writeSectionContent(SHeader, *S, CBA)) |
| 232 | return false; |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 233 | } else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) { |
| 234 | unsigned SymIdx; |
| 235 | if (SymN2I.lookup(S->Info, SymIdx)) { |
| 236 | errs() << "error: Unknown symbol referenced: '" << S->Info |
| 237 | << "' at YAML section '" << S->Name << "'.\n"; |
| 238 | return false; |
| 239 | } |
| 240 | SHeader.sh_info = SymIdx; |
| 241 | if (!writeSectionContent(SHeader, *S, CBA)) |
| 242 | return false; |
Simon Atanasyan | 04d9e65 | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 243 | } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) { |
| 244 | if (!writeSectionContent(SHeader, *S, CBA)) |
| 245 | return false; |
Simon Atanasyan | 5db0276 | 2015-07-03 23:00:54 +0000 | [diff] [blame] | 246 | } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec.get())) { |
| 247 | SHeader.sh_entsize = 0; |
| 248 | SHeader.sh_size = S->Size; |
| 249 | // SHT_NOBITS section does not have content |
| 250 | // so just to setup the section offset. |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame^] | 251 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 252 | } else |
| 253 | llvm_unreachable("Unknown section type"); |
| 254 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 255 | SHeaders.push_back(SHeader); |
| 256 | } |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | template <class ELFT> |
| 261 | void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader, |
| 262 | ContiguousBlobAccumulator &CBA) { |
| 263 | zero(SHeader); |
Hans Wennborg | 59f0cba | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 264 | SHeader.sh_name = DotShStrtab.getOffset(".symtab"); |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 265 | SHeader.sh_type = ELF::SHT_SYMTAB; |
| 266 | SHeader.sh_link = getDotStrTabSecNo(); |
| 267 | // One greater than symbol table index of the last local symbol. |
| 268 | SHeader.sh_info = Doc.Symbols.Local.size() + 1; |
| 269 | SHeader.sh_entsize = sizeof(Elf_Sym); |
| 270 | |
| 271 | std::vector<Elf_Sym> Syms; |
| 272 | { |
| 273 | // Ensure STN_UNDEF is present |
| 274 | Elf_Sym Sym; |
| 275 | zero(Sym); |
| 276 | Syms.push_back(Sym); |
| 277 | } |
Hans Wennborg | 59f0cba | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 278 | |
| 279 | // Add symbol names to .strtab. |
| 280 | for (const auto &Sym : Doc.Symbols.Local) |
| 281 | DotStrtab.add(Sym.Name); |
| 282 | for (const auto &Sym : Doc.Symbols.Global) |
| 283 | DotStrtab.add(Sym.Name); |
| 284 | for (const auto &Sym : Doc.Symbols.Weak) |
| 285 | DotStrtab.add(Sym.Name); |
Hans Wennborg | f26bfc1 | 2014-09-29 22:43:20 +0000 | [diff] [blame] | 286 | DotStrtab.finalize(StringTableBuilder::ELF); |
Hans Wennborg | 59f0cba | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 287 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 288 | addSymbols(Doc.Symbols.Local, Syms, ELF::STB_LOCAL); |
| 289 | addSymbols(Doc.Symbols.Global, Syms, ELF::STB_GLOBAL); |
| 290 | addSymbols(Doc.Symbols.Weak, Syms, ELF::STB_WEAK); |
| 291 | |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame^] | 292 | writeArrayData( |
| 293 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign), |
| 294 | makeArrayRef(Syms)); |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 295 | SHeader.sh_size = arrayDataSize(makeArrayRef(Syms)); |
| 296 | } |
| 297 | |
| 298 | template <class ELFT> |
| 299 | void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, |
| 300 | StringTableBuilder &STB, |
| 301 | ContiguousBlobAccumulator &CBA) { |
| 302 | zero(SHeader); |
Hans Wennborg | 59f0cba | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 303 | SHeader.sh_name = DotShStrtab.getOffset(Name); |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 304 | SHeader.sh_type = ELF::SHT_STRTAB; |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame^] | 305 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign) |
| 306 | << STB.data(); |
Hans Wennborg | 59f0cba | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 307 | SHeader.sh_size = STB.data().size(); |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 308 | SHeader.sh_addralign = 1; |
| 309 | } |
| 310 | |
| 311 | template <class ELFT> |
| 312 | void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, |
| 313 | std::vector<Elf_Sym> &Syms, |
| 314 | unsigned SymbolBinding) { |
Simon Atanasyan | 048baca | 2014-03-14 06:53:30 +0000 | [diff] [blame] | 315 | for (const auto &Sym : Symbols) { |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 316 | Elf_Sym Symbol; |
| 317 | zero(Symbol); |
| 318 | if (!Sym.Name.empty()) |
Hans Wennborg | 59f0cba | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 319 | Symbol.st_name = DotStrtab.getOffset(Sym.Name); |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 320 | Symbol.setBindingAndType(SymbolBinding, Sym.Type); |
Sean Silva | c4afa6d | 2013-06-21 01:11:48 +0000 | [diff] [blame] | 321 | if (!Sym.Section.empty()) { |
| 322 | unsigned Index; |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 323 | if (SN2I.lookup(Sym.Section, Index)) { |
Sean Silva | c4afa6d | 2013-06-21 01:11:48 +0000 | [diff] [blame] | 324 | errs() << "error: Unknown section referenced: '" << Sym.Section |
| 325 | << "' by YAML symbol " << Sym.Name << ".\n"; |
| 326 | exit(1); |
| 327 | } |
| 328 | Symbol.st_shndx = Index; |
| 329 | } // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier. |
Sean Silva | 05001b9 | 2013-06-20 20:59:47 +0000 | [diff] [blame] | 330 | Symbol.st_value = Sym.Value; |
Simon Atanasyan | 60e1a79 | 2014-11-06 22:46:24 +0000 | [diff] [blame] | 331 | Symbol.st_other = Sym.Other; |
Sean Silva | 05001b9 | 2013-06-20 20:59:47 +0000 | [diff] [blame] | 332 | Symbol.st_size = Sym.Size; |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 333 | Syms.push_back(Symbol); |
| 334 | } |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 337 | template <class ELFT> |
| 338 | void |
| 339 | ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
| 340 | const ELFYAML::RawContentSection &Section, |
| 341 | ContiguousBlobAccumulator &CBA) { |
Simon Atanasyan | b83f380 | 2014-05-16 16:01:00 +0000 | [diff] [blame] | 342 | assert(Section.Size >= Section.Content.binary_size() && |
| 343 | "Section size and section content are inconsistent"); |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame^] | 344 | raw_ostream &OS = |
| 345 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Simon Atanasyan | b83f380 | 2014-05-16 16:01:00 +0000 | [diff] [blame] | 346 | Section.Content.writeAsBinary(OS); |
| 347 | for (auto i = Section.Content.binary_size(); i < Section.Size; ++i) |
| 348 | OS.write(0); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 349 | SHeader.sh_entsize = 0; |
Simon Atanasyan | b83f380 | 2014-05-16 16:01:00 +0000 | [diff] [blame] | 350 | SHeader.sh_size = Section.Size; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Simon Atanasyan | 5d19c67 | 2015-01-25 13:29:25 +0000 | [diff] [blame] | 353 | static bool isMips64EL(const ELFYAML::Object &Doc) { |
| 354 | return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) && |
| 355 | Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) && |
| 356 | Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); |
| 357 | } |
| 358 | |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 359 | template <class ELFT> |
| 360 | bool |
| 361 | ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
| 362 | const ELFYAML::RelocationSection &Section, |
| 363 | ContiguousBlobAccumulator &CBA) { |
Simon Atanasyan | 8eb9d1b | 2015-05-08 07:05:04 +0000 | [diff] [blame] | 364 | assert((Section.Type == llvm::ELF::SHT_REL || |
| 365 | Section.Type == llvm::ELF::SHT_RELA) && |
| 366 | "Section type is not SHT_REL nor SHT_RELA"); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 367 | |
| 368 | bool IsRela = Section.Type == llvm::ELF::SHT_RELA; |
| 369 | SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 370 | SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size(); |
| 371 | |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame^] | 372 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 373 | |
| 374 | for (const auto &Rel : Section.Relocations) { |
Adhemerval Zanella | 9f3dbff | 2015-04-22 15:26:43 +0000 | [diff] [blame] | 375 | unsigned SymIdx = 0; |
| 376 | // Some special relocation, R_ARM_v4BX for instance, does not have |
| 377 | // an external reference. So it ignores the return value of lookup() |
| 378 | // here. |
| 379 | SymN2I.lookup(Rel.Symbol, SymIdx); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 380 | |
| 381 | if (IsRela) { |
| 382 | Elf_Rela REntry; |
| 383 | zero(REntry); |
| 384 | REntry.r_offset = Rel.Offset; |
| 385 | REntry.r_addend = Rel.Addend; |
Simon Atanasyan | 5d19c67 | 2015-01-25 13:29:25 +0000 | [diff] [blame] | 386 | REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc)); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 387 | OS.write((const char *)&REntry, sizeof(REntry)); |
| 388 | } else { |
| 389 | Elf_Rel REntry; |
| 390 | zero(REntry); |
| 391 | REntry.r_offset = Rel.Offset; |
Simon Atanasyan | 5d19c67 | 2015-01-25 13:29:25 +0000 | [diff] [blame] | 392 | REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc)); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 393 | OS.write((const char *)&REntry, sizeof(REntry)); |
| 394 | } |
| 395 | } |
| 396 | return true; |
| 397 | } |
| 398 | |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 399 | template <class ELFT> |
| 400 | bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
| 401 | const ELFYAML::Group &Section, |
| 402 | ContiguousBlobAccumulator &CBA) { |
| 403 | typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word; |
Simon Atanasyan | 8eb9d1b | 2015-05-08 07:05:04 +0000 | [diff] [blame] | 404 | assert(Section.Type == llvm::ELF::SHT_GROUP && |
| 405 | "Section type is not SHT_GROUP"); |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 406 | |
| 407 | SHeader.sh_entsize = sizeof(Elf_Word); |
| 408 | SHeader.sh_size = SHeader.sh_entsize * Section.Members.size(); |
| 409 | |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame^] | 410 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 411 | |
| 412 | for (auto member : Section.Members) { |
| 413 | Elf_Word SIdx; |
| 414 | unsigned int sectionIndex = 0; |
| 415 | if (member.sectionNameOrType == "GRP_COMDAT") |
| 416 | sectionIndex = llvm::ELF::GRP_COMDAT; |
| 417 | else if (SN2I.lookup(member.sectionNameOrType, sectionIndex)) { |
| 418 | errs() << "error: Unknown section referenced: '" |
| 419 | << member.sectionNameOrType << "' at YAML section' " |
| 420 | << Section.Name << "\n"; |
| 421 | return false; |
| 422 | } |
| 423 | SIdx = sectionIndex; |
| 424 | OS.write((const char *)&SIdx, sizeof(SIdx)); |
| 425 | } |
| 426 | return true; |
| 427 | } |
| 428 | |
Simon Atanasyan | 04d9e65 | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 429 | template <class ELFT> |
| 430 | bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
| 431 | const ELFYAML::MipsABIFlags &Section, |
| 432 | ContiguousBlobAccumulator &CBA) { |
Simon Atanasyan | 8eb9d1b | 2015-05-08 07:05:04 +0000 | [diff] [blame] | 433 | assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS && |
| 434 | "Section type is not SHT_MIPS_ABIFLAGS"); |
Simon Atanasyan | 04d9e65 | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 435 | |
| 436 | object::Elf_Mips_ABIFlags<ELFT> Flags; |
| 437 | zero(Flags); |
| 438 | SHeader.sh_entsize = sizeof(Flags); |
| 439 | SHeader.sh_size = SHeader.sh_entsize; |
| 440 | |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame^] | 441 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Simon Atanasyan | 04d9e65 | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 442 | Flags.version = Section.Version; |
| 443 | Flags.isa_level = Section.ISALevel; |
| 444 | Flags.isa_rev = Section.ISARevision; |
| 445 | Flags.gpr_size = Section.GPRSize; |
| 446 | Flags.cpr1_size = Section.CPR1Size; |
| 447 | Flags.cpr2_size = Section.CPR2Size; |
| 448 | Flags.fp_abi = Section.FpABI; |
| 449 | Flags.isa_ext = Section.ISAExtension; |
| 450 | Flags.ases = Section.ASEs; |
| 451 | Flags.flags1 = Section.Flags1; |
| 452 | Flags.flags2 = Section.Flags2; |
| 453 | OS.write((const char *)&Flags, sizeof(Flags)); |
| 454 | |
| 455 | return true; |
| 456 | } |
| 457 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 458 | template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() { |
| 459 | SN2I.addName(".symtab", getDotSymTabSecNo()); |
| 460 | SN2I.addName(".strtab", getDotStrTabSecNo()); |
| 461 | SN2I.addName(".shstrtab", getDotShStrTabSecNo()); |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 462 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 463 | for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) { |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 464 | StringRef Name = Doc.Sections[i]->Name; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 465 | if (Name.empty()) |
| 466 | continue; |
| 467 | // "+ 1" to take into account the SHT_NULL entry. |
| 468 | if (SN2I.addName(Name, i + 1)) { |
| 469 | errs() << "error: Repeated section name: '" << Name |
| 470 | << "' at YAML section number " << i << ".\n"; |
| 471 | return false; |
| 472 | } |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 473 | } |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 474 | return true; |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 477 | template <class ELFT> |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 478 | bool |
| 479 | ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex, |
| 480 | const std::vector<ELFYAML::Symbol> &Symbols) { |
| 481 | for (const auto &Sym : Symbols) { |
| 482 | ++StartIndex; |
| 483 | if (Sym.Name.empty()) |
| 484 | continue; |
| 485 | if (SymN2I.addName(Sym.Name, StartIndex)) { |
| 486 | errs() << "error: Repeated symbol name: '" << Sym.Name << "'.\n"; |
| 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | template <class ELFT> |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 494 | int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) { |
Simon Atanasyan | 220c54a | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 495 | ELFState<ELFT> State(Doc); |
| 496 | if (!State.buildSectionIndex()) |
| 497 | return 1; |
| 498 | |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 499 | std::size_t StartSymIndex = 0; |
| 500 | if (!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Local) || |
| 501 | !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Global) || |
| 502 | !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Weak)) |
| 503 | return 1; |
| 504 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 505 | Elf_Ehdr Header; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 506 | State.initELFHeader(Header); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 507 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 508 | // TODO: Flesh out section header support. |
| 509 | // TODO: Program headers. |
| 510 | |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 511 | // XXX: This offset is tightly coupled with the order that we write |
| 512 | // things to `OS`. |
| 513 | const size_t SectionContentBeginOffset = |
| 514 | Header.e_ehsize + Header.e_shentsize * Header.e_shnum; |
| 515 | ContiguousBlobAccumulator CBA(SectionContentBeginOffset); |
Sean Silva | c1c290b | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 516 | |
Hans Wennborg | 59f0cba | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 517 | // Doc might not contain .symtab, .strtab and .shstrtab sections, |
| 518 | // but we will emit them, so make sure to add them to ShStrTabSHeader. |
| 519 | State.DotShStrtab.add(".symtab"); |
| 520 | State.DotShStrtab.add(".strtab"); |
| 521 | State.DotShStrtab.add(".shstrtab"); |
| 522 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 523 | std::vector<Elf_Shdr> SHeaders; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 524 | if(!State.initSectionHeaders(SHeaders, CBA)) |
| 525 | return 1; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 526 | |
Sean Silva | 8217757 | 2013-06-22 01:38:00 +0000 | [diff] [blame] | 527 | // .symtab section. |
| 528 | Elf_Shdr SymtabSHeader; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 529 | State.initSymtabSectionHeader(SymtabSHeader, CBA); |
Sean Silva | 8217757 | 2013-06-22 01:38:00 +0000 | [diff] [blame] | 530 | SHeaders.push_back(SymtabSHeader); |
| 531 | |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 532 | // .strtab string table header. |
Sean Silva | c313192 | 2013-06-18 21:37:50 +0000 | [diff] [blame] | 533 | Elf_Shdr DotStrTabSHeader; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 534 | State.initStrtabSectionHeader(DotStrTabSHeader, ".strtab", State.DotStrtab, |
| 535 | CBA); |
Sean Silva | 7d61722 | 2013-06-22 01:06:12 +0000 | [diff] [blame] | 536 | SHeaders.push_back(DotStrTabSHeader); |
Sean Silva | c313192 | 2013-06-18 21:37:50 +0000 | [diff] [blame] | 537 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 538 | // .shstrtab string table header. |
| 539 | Elf_Shdr ShStrTabSHeader; |
| 540 | State.initStrtabSectionHeader(ShStrTabSHeader, ".shstrtab", State.DotShStrtab, |
| 541 | CBA); |
| 542 | SHeaders.push_back(ShStrTabSHeader); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 543 | |
| 544 | OS.write((const char *)&Header, sizeof(Header)); |
Will Dietz | 0b48c73 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 545 | writeArrayData(OS, makeArrayRef(SHeaders)); |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 546 | CBA.writeBlobToStream(OS); |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 547 | return 0; |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Sean Silva | 11caeba | 2013-06-22 01:03:35 +0000 | [diff] [blame] | 550 | static bool is64Bit(const ELFYAML::Object &Doc) { |
| 551 | return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64); |
| 552 | } |
| 553 | |
| 554 | static bool isLittleEndian(const ELFYAML::Object &Doc) { |
| 555 | return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); |
| 556 | } |
| 557 | |
Simon Atanasyan | f97af8a | 2014-05-31 04:51:07 +0000 | [diff] [blame] | 558 | int yaml2elf(yaml::Input &YIn, raw_ostream &Out) { |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 559 | ELFYAML::Object Doc; |
| 560 | YIn >> Doc; |
| 561 | if (YIn.error()) { |
| 562 | errs() << "yaml2obj: Failed to parse YAML file!\n"; |
| 563 | return 1; |
| 564 | } |
Sean Silva | 11caeba | 2013-06-22 01:03:35 +0000 | [diff] [blame] | 565 | using object::ELFType; |
Rafael Espindola | ac729b4 | 2015-06-02 12:05:27 +0000 | [diff] [blame] | 566 | typedef ELFType<support::little, true> LE64; |
| 567 | typedef ELFType<support::big, true> BE64; |
| 568 | typedef ELFType<support::little, false> LE32; |
| 569 | typedef ELFType<support::big, false> BE32; |
Sean Silva | 11caeba | 2013-06-22 01:03:35 +0000 | [diff] [blame] | 570 | if (is64Bit(Doc)) { |
| 571 | if (isLittleEndian(Doc)) |
Simon Atanasyan | 73e047e | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 572 | return ELFState<LE64>::writeELF(Out, Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 573 | else |
Simon Atanasyan | 73e047e | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 574 | return ELFState<BE64>::writeELF(Out, Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 575 | } else { |
Sean Silva | 11caeba | 2013-06-22 01:03:35 +0000 | [diff] [blame] | 576 | if (isLittleEndian(Doc)) |
Simon Atanasyan | 73e047e | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 577 | return ELFState<LE32>::writeELF(Out, Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 578 | else |
Simon Atanasyan | 73e047e | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 579 | return ELFState<BE32>::writeELF(Out, Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 580 | } |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 581 | } |