Sean Silva | 5918b7a | 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 |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// The ELF component of yaml2obj. |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "yaml2obj.h" |
Will Dietz | 6dd7893 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
Zachary Turner | 19ca2b0 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 17 | #include "llvm/BinaryFormat/ELF.h" |
Rafael Espindola | 7413fef | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 18 | #include "llvm/MC/StringTableBuilder.h" |
Michael J. Spencer | 081a194 | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 19 | #include "llvm/Object/ELFObjectFile.h" |
Rafael Espindola | 18903ff | 2016-03-01 19:15:06 +0000 | [diff] [blame] | 20 | #include "llvm/ObjectYAML/ELFYAML.h" |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Jonas Devlieghere | ae6875e | 2018-04-21 21:11:59 +0000 | [diff] [blame] | 22 | #include "llvm/Support/WithColor.h" |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 23 | #include "llvm/Support/YAMLTraits.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
Sean Silva | 2a7e79a | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 28 | // This class is used to build up a contiguous binary blob while keeping |
| 29 | // track of an offset in the output (which notionally begins at |
| 30 | // `InitialOffset`). |
Sean Silva | f3f3523 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 31 | namespace { |
Sean Silva | 2a7e79a | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 32 | class ContiguousBlobAccumulator { |
| 33 | const uint64_t InitialOffset; |
Sean Silva | b14f972 | 2013-06-20 19:11:41 +0000 | [diff] [blame] | 34 | SmallVector<char, 128> Buf; |
Sean Silva | 2a7e79a | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 35 | raw_svector_ostream OS; |
| 36 | |
Sean Silva | e766884 | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 37 | /// \returns The new offset. |
| 38 | uint64_t padToAlignment(unsigned Align) { |
Simon Atanasyan | 080d7a8 | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 39 | if (Align == 0) |
| 40 | Align = 1; |
Sean Silva | e766884 | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 41 | uint64_t CurrentOffset = InitialOffset + OS.tell(); |
Rui Ueyama | 3edb0ec | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 42 | uint64_t AlignedOffset = alignTo(CurrentOffset, Align); |
Sean Silva | e766884 | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 43 | for (; CurrentOffset != AlignedOffset; ++CurrentOffset) |
| 44 | OS.write('\0'); |
| 45 | return AlignedOffset; // == CurrentOffset; |
| 46 | } |
| 47 | |
Sean Silva | 2a7e79a | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 48 | public: |
Sean Silva | b14f972 | 2013-06-20 19:11:41 +0000 | [diff] [blame] | 49 | ContiguousBlobAccumulator(uint64_t InitialOffset_) |
| 50 | : InitialOffset(InitialOffset_), Buf(), OS(Buf) {} |
Sean Silva | e766884 | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 51 | template <class Integer> |
Simon Atanasyan | 080d7a8 | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 52 | raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) { |
Sean Silva | e766884 | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 53 | Offset = padToAlignment(Align); |
| 54 | return OS; |
| 55 | } |
Sean Silva | 2a7e79a | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 56 | void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); } |
| 57 | }; |
Sean Silva | f3f3523 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 58 | } // end anonymous namespace |
Sean Silva | 2a7e79a | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 59 | |
Simon Atanasyan | cde97ea | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 60 | // Used to keep track of section and symbol names, so that in the YAML file |
| 61 | // sections and symbols can be referenced by name instead of by index. |
Sean Silva | f3f3523 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 62 | namespace { |
Simon Atanasyan | cde97ea | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 63 | class NameToIdxMap { |
Sean Silva | fe57e34 | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 64 | StringMap<int> Map; |
| 65 | public: |
| 66 | /// \returns true if name is already present in the map. |
Simon Atanasyan | cde97ea | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 67 | bool addName(StringRef Name, unsigned i) { |
David Blaikie | 1d4f28c | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 68 | return !Map.insert(std::make_pair(Name, (int)i)).second; |
Sean Silva | fe57e34 | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 69 | } |
| 70 | /// \returns true if name is not present in the map |
Simon Atanasyan | cde97ea | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 71 | bool lookup(StringRef Name, unsigned &Idx) const { |
| 72 | StringMap<int>::const_iterator I = Map.find(Name); |
Sean Silva | fe57e34 | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 73 | if (I == Map.end()) |
| 74 | return true; |
| 75 | Idx = I->getValue(); |
| 76 | return false; |
| 77 | } |
Dave Lee | 9a5be1f | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 78 | /// asserts if name is not present in the map |
| 79 | unsigned get(StringRef Name) const { |
| 80 | unsigned Idx = 0; |
| 81 | auto missing = lookup(Name, Idx); |
| 82 | (void)missing; |
| 83 | assert(!missing && "Expected section not found in index"); |
| 84 | return Idx; |
| 85 | } |
| 86 | unsigned size() const { return Map.size(); } |
Sean Silva | fe57e34 | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 87 | }; |
Sean Silva | f3f3523 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 88 | } // end anonymous namespace |
Sean Silva | fe57e34 | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 89 | |
Sean Silva | 274264c | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 90 | template <class T> |
Will Dietz | 6dd7893 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 91 | static size_t arrayDataSize(ArrayRef<T> A) { |
| 92 | return A.size() * sizeof(T); |
Sean Silva | 274264c | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | template <class T> |
Will Dietz | 6dd7893 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 96 | static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) { |
| 97 | OS.write((const char *)A.data(), arrayDataSize(A)); |
Sean Silva | 274264c | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | template <class T> |
| 101 | static void zero(T &Obj) { |
| 102 | memset(&Obj, 0, sizeof(Obj)); |
| 103 | } |
| 104 | |
Sean Silva | 0382b30 | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 105 | namespace { |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 106 | /// "Single point of truth" for the ELF file construction. |
Sean Silva | 0382b30 | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 107 | /// TODO: This class still has a ways to go before it is truly a "single |
| 108 | /// point of truth". |
| 109 | template <class ELFT> |
| 110 | class ELFState { |
Rui Ueyama | 5a286a0 | 2018-01-12 02:28:31 +0000 | [diff] [blame] | 111 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| 112 | typedef typename ELFT::Phdr Elf_Phdr; |
| 113 | typedef typename ELFT::Shdr Elf_Shdr; |
| 114 | typedef typename ELFT::Sym Elf_Sym; |
| 115 | typedef typename ELFT::Rel Elf_Rel; |
| 116 | typedef typename ELFT::Rela Elf_Rela; |
Jake Ehrlich | eefdbb4 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 117 | typedef typename ELFT::Relr Elf_Relr; |
Paul Semel | f3f578d | 2018-07-23 18:49:04 +0000 | [diff] [blame] | 118 | typedef typename ELFT::Dyn Elf_Dyn; |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 119 | |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 120 | enum class SymtabType { Static, Dynamic }; |
| 121 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 122 | /// The future ".strtab" section. |
Rafael Espindola | 715bcca | 2015-10-23 21:48:05 +0000 | [diff] [blame] | 123 | StringTableBuilder DotStrtab{StringTableBuilder::ELF}; |
Sean Silva | 0382b30 | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 124 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 125 | /// The future ".shstrtab" section. |
Rafael Espindola | 715bcca | 2015-10-23 21:48:05 +0000 | [diff] [blame] | 126 | StringTableBuilder DotShStrtab{StringTableBuilder::ELF}; |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 127 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 128 | /// The future ".dynstr" section. |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 129 | StringTableBuilder DotDynstr{StringTableBuilder::ELF}; |
| 130 | |
Simon Atanasyan | cde97ea | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 131 | NameToIdxMap SN2I; |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 132 | NameToIdxMap SymN2I; |
Simon Atanasyan | 38ac43b1 | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 133 | const ELFYAML::Object &Doc; |
Sean Silva | c18f66e | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 134 | |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 135 | bool buildSectionIndex(); |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 136 | bool buildSymbolIndex(std::size_t &StartIndex, |
| 137 | const std::vector<ELFYAML::Symbol> &Symbols); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 138 | void initELFHeader(Elf_Ehdr &Header); |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 139 | void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 140 | bool initSectionHeaders(std::vector<Elf_Shdr> &SHeaders, |
| 141 | ContiguousBlobAccumulator &CBA); |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 142 | void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType, |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 143 | ContiguousBlobAccumulator &CBA); |
| 144 | void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, |
| 145 | StringTableBuilder &STB, |
| 146 | ContiguousBlobAccumulator &CBA); |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 147 | void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders, |
| 148 | std::vector<Elf_Shdr> &SHeaders); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 149 | void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 150 | std::vector<Elf_Sym> &Syms, unsigned SymbolBinding, |
| 151 | const StringTableBuilder &Strtab); |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 152 | void writeSectionContent(Elf_Shdr &SHeader, |
| 153 | const ELFYAML::RawContentSection &Section, |
| 154 | ContiguousBlobAccumulator &CBA); |
| 155 | bool writeSectionContent(Elf_Shdr &SHeader, |
| 156 | const ELFYAML::RelocationSection &Section, |
| 157 | ContiguousBlobAccumulator &CBA); |
Shankar Easwaran | 5872174 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 158 | bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group, |
| 159 | ContiguousBlobAccumulator &CBA); |
Simon Atanasyan | bd58bdb | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 160 | bool writeSectionContent(Elf_Shdr &SHeader, |
| 161 | const ELFYAML::MipsABIFlags &Section, |
| 162 | ContiguousBlobAccumulator &CBA); |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 163 | bool hasDynamicSymbols() const; |
| 164 | SmallVector<const char *, 5> implicitSectionNames() const; |
Sean Silva | 0382b30 | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 165 | |
Simon Atanasyan | 38ac43b1 | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 166 | // - SHT_NULL entry (placed first, i.e. 0'th entry) |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 167 | // - symbol table (.symtab) (defaults to after last yaml section) |
| 168 | // - string table (.strtab) (defaults to after .symtab) |
| 169 | // - section header string table (.shstrtab) (defaults to after .strtab) |
| 170 | // - dynamic symbol table (.dynsym) (defaults to after .shstrtab) |
| 171 | // - dynamic string table (.dynstr) (defaults to after .dynsym) |
Dave Lee | 9a5be1f | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 172 | unsigned getDotSymTabSecNo() const { return SN2I.get(".symtab"); } |
| 173 | unsigned getDotStrTabSecNo() const { return SN2I.get(".strtab"); } |
| 174 | unsigned getDotShStrTabSecNo() const { return SN2I.get(".shstrtab"); } |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 175 | unsigned getDotDynSymSecNo() const { return SN2I.get(".dynsym"); } |
| 176 | unsigned getDotDynStrSecNo() const { return SN2I.get(".dynstr"); } |
Dave Lee | 9a5be1f | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 177 | unsigned getSectionCount() const { return SN2I.size() + 1; } |
Simon Atanasyan | 38ac43b1 | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 178 | |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 179 | ELFState(const ELFYAML::Object &D) : Doc(D) {} |
Simon Atanasyan | 38ac43b1 | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 180 | |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 181 | public: |
| 182 | static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc); |
Sean Silva | 0382b30 | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 183 | }; |
| 184 | } // end anonymous namespace |
| 185 | |
Sean Silva | 552d7cd | 2013-06-21 00:33:01 +0000 | [diff] [blame] | 186 | template <class ELFT> |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 187 | void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) { |
| 188 | using namespace llvm::ELF; |
| 189 | zero(Header); |
| 190 | Header.e_ident[EI_MAG0] = 0x7f; |
| 191 | Header.e_ident[EI_MAG1] = 'E'; |
| 192 | Header.e_ident[EI_MAG2] = 'L'; |
| 193 | Header.e_ident[EI_MAG3] = 'F'; |
| 194 | Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; |
| 195 | bool IsLittleEndian = ELFT::TargetEndianness == support::little; |
| 196 | Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB; |
| 197 | Header.e_ident[EI_VERSION] = EV_CURRENT; |
| 198 | Header.e_ident[EI_OSABI] = Doc.Header.OSABI; |
George Rimar | 64002dd | 2018-12-20 10:43:49 +0000 | [diff] [blame] | 199 | Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion; |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 200 | Header.e_type = Doc.Header.Type; |
| 201 | Header.e_machine = Doc.Header.Machine; |
| 202 | Header.e_version = EV_CURRENT; |
| 203 | Header.e_entry = Doc.Header.Entry; |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 204 | Header.e_phoff = sizeof(Header); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 205 | Header.e_flags = Doc.Header.Flags; |
| 206 | Header.e_ehsize = sizeof(Elf_Ehdr); |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 207 | Header.e_phentsize = sizeof(Elf_Phdr); |
| 208 | Header.e_phnum = Doc.ProgramHeaders.size(); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 209 | Header.e_shentsize = sizeof(Elf_Shdr); |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 210 | // Immediately following the ELF header and program headers. |
| 211 | Header.e_shoff = |
| 212 | sizeof(Header) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size(); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 213 | Header.e_shnum = getSectionCount(); |
| 214 | Header.e_shstrndx = getDotShStrTabSecNo(); |
| 215 | } |
| 216 | |
| 217 | template <class ELFT> |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 218 | void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) { |
| 219 | for (const auto &YamlPhdr : Doc.ProgramHeaders) { |
| 220 | Elf_Phdr Phdr; |
| 221 | Phdr.p_type = YamlPhdr.Type; |
| 222 | Phdr.p_flags = YamlPhdr.Flags; |
| 223 | Phdr.p_vaddr = YamlPhdr.VAddr; |
| 224 | Phdr.p_paddr = YamlPhdr.PAddr; |
| 225 | PHeaders.push_back(Phdr); |
| 226 | } |
| 227 | } |
| 228 | |
Xing GUO | 53a9c2e | 2018-12-04 14:27:51 +0000 | [diff] [blame] | 229 | static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName, |
| 230 | StringRef IndexSrc, unsigned &IndexDest) { |
| 231 | if (SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) { |
| 232 | WithColor::error() << "Unknown section referenced: '" << IndexSrc |
| 233 | << "' at YAML section '" << SecName << "'.\n"; |
| 234 | return false; |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 239 | template <class ELFT> |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 240 | bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders, |
| 241 | ContiguousBlobAccumulator &CBA) { |
| 242 | // Ensure SHN_UNDEF entry is present. An all-zero section header is a |
| 243 | // valid SHN_UNDEF entry since SHT_NULL == 0. |
| 244 | Elf_Shdr SHeader; |
| 245 | zero(SHeader); |
| 246 | SHeaders.push_back(SHeader); |
| 247 | |
| 248 | for (const auto &Sec : Doc.Sections) { |
| 249 | zero(SHeader); |
Hans Wennborg | 14d1db9 | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 250 | SHeader.sh_name = DotShStrtab.getOffset(Sec->Name); |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 251 | SHeader.sh_type = Sec->Type; |
| 252 | SHeader.sh_flags = Sec->Flags; |
| 253 | SHeader.sh_addr = Sec->Address; |
| 254 | SHeader.sh_addralign = Sec->AddressAlign; |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 255 | |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 256 | if (!Sec->Link.empty()) { |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 257 | unsigned Index; |
Xing GUO | 53a9c2e | 2018-12-04 14:27:51 +0000 | [diff] [blame] | 258 | if (!convertSectionIndex(SN2I, Sec->Name, Sec->Link, Index)) |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 259 | return false; |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 260 | SHeader.sh_link = Index; |
| 261 | } |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 262 | |
| 263 | if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec.get())) |
| 264 | writeSectionContent(SHeader, *S, CBA); |
| 265 | else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec.get())) { |
| 266 | if (S->Link.empty()) |
| 267 | // For relocation section set link to .symtab by default. |
| 268 | SHeader.sh_link = getDotSymTabSecNo(); |
| 269 | |
| 270 | unsigned Index; |
Xing GUO | 53a9c2e | 2018-12-04 14:27:51 +0000 | [diff] [blame] | 271 | if (!convertSectionIndex(SN2I, S->Name, S->Info, Index)) |
George Rimar | d9d6711 | 2018-08-16 12:23:22 +0000 | [diff] [blame] | 272 | return false; |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 273 | SHeader.sh_info = Index; |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 274 | if (!writeSectionContent(SHeader, *S, CBA)) |
| 275 | return false; |
Shankar Easwaran | 5872174 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 276 | } else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) { |
| 277 | unsigned SymIdx; |
George Rimar | 312924a | 2018-08-15 13:55:22 +0000 | [diff] [blame] | 278 | if (SymN2I.lookup(S->Info, SymIdx) && !to_integer(S->Info, SymIdx)) { |
Jonas Devlieghere | ae6875e | 2018-04-21 21:11:59 +0000 | [diff] [blame] | 279 | WithColor::error() << "Unknown symbol referenced: '" << S->Info |
| 280 | << "' at YAML section '" << S->Name << "'.\n"; |
Shankar Easwaran | 5872174 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | SHeader.sh_info = SymIdx; |
| 284 | if (!writeSectionContent(SHeader, *S, CBA)) |
| 285 | return false; |
Simon Atanasyan | bd58bdb | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 286 | } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) { |
| 287 | if (!writeSectionContent(SHeader, *S, CBA)) |
| 288 | return false; |
Simon Atanasyan | afc0340 | 2015-07-03 23:00:54 +0000 | [diff] [blame] | 289 | } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec.get())) { |
| 290 | SHeader.sh_entsize = 0; |
| 291 | SHeader.sh_size = S->Size; |
| 292 | // SHT_NOBITS section does not have content |
| 293 | // so just to setup the section offset. |
Simon Atanasyan | 080d7a8 | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 294 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 295 | } else |
| 296 | llvm_unreachable("Unknown section type"); |
| 297 | |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 298 | SHeaders.push_back(SHeader); |
| 299 | } |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | template <class ELFT> |
| 304 | void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader, |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 305 | SymtabType STType, |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 306 | ContiguousBlobAccumulator &CBA) { |
| 307 | zero(SHeader); |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 308 | bool IsStatic = STType == SymtabType::Static; |
| 309 | SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym"); |
| 310 | SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM; |
| 311 | SHeader.sh_link = IsStatic ? getDotStrTabSecNo() : getDotDynStrSecNo(); |
| 312 | const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols; |
| 313 | auto &Strtab = IsStatic ? DotStrtab : DotDynstr; |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 314 | // One greater than symbol table index of the last local symbol. |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 315 | SHeader.sh_info = Symbols.Local.size() + 1; |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 316 | SHeader.sh_entsize = sizeof(Elf_Sym); |
Simon Atanasyan | ba8d948 | 2015-07-09 18:23:02 +0000 | [diff] [blame] | 317 | SHeader.sh_addralign = 8; |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 318 | |
| 319 | std::vector<Elf_Sym> Syms; |
| 320 | { |
| 321 | // Ensure STN_UNDEF is present |
| 322 | Elf_Sym Sym; |
| 323 | zero(Sym); |
| 324 | Syms.push_back(Sym); |
| 325 | } |
Hans Wennborg | 14d1db9 | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 326 | |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 327 | // Add symbol names to .strtab or .dynstr. |
| 328 | for (const auto &Sym : Symbols.Local) |
| 329 | Strtab.add(Sym.Name); |
| 330 | for (const auto &Sym : Symbols.Global) |
| 331 | Strtab.add(Sym.Name); |
| 332 | for (const auto &Sym : Symbols.Weak) |
| 333 | Strtab.add(Sym.Name); |
| 334 | Strtab.finalize(); |
Hans Wennborg | 14d1db9 | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 335 | |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 336 | addSymbols(Symbols.Local, Syms, ELF::STB_LOCAL, Strtab); |
| 337 | addSymbols(Symbols.Global, Syms, ELF::STB_GLOBAL, Strtab); |
| 338 | addSymbols(Symbols.Weak, Syms, ELF::STB_WEAK, Strtab); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 339 | |
Simon Atanasyan | 080d7a8 | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 340 | writeArrayData( |
| 341 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign), |
| 342 | makeArrayRef(Syms)); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 343 | SHeader.sh_size = arrayDataSize(makeArrayRef(Syms)); |
| 344 | } |
| 345 | |
| 346 | template <class ELFT> |
| 347 | void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, |
| 348 | StringTableBuilder &STB, |
| 349 | ContiguousBlobAccumulator &CBA) { |
| 350 | zero(SHeader); |
Hans Wennborg | 14d1db9 | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 351 | SHeader.sh_name = DotShStrtab.getOffset(Name); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 352 | SHeader.sh_type = ELF::SHT_STRTAB; |
Rafael Espindola | 2638e45 | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 353 | STB.write(CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign)); |
| 354 | SHeader.sh_size = STB.getSize(); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 355 | SHeader.sh_addralign = 1; |
| 356 | } |
| 357 | |
| 358 | template <class ELFT> |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 359 | void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders, |
| 360 | std::vector<Elf_Shdr> &SHeaders) { |
| 361 | uint32_t PhdrIdx = 0; |
| 362 | for (auto &YamlPhdr : Doc.ProgramHeaders) { |
| 363 | auto &PHeader = PHeaders[PhdrIdx++]; |
| 364 | |
| 365 | if (YamlPhdr.Sections.size()) |
| 366 | PHeader.p_offset = UINT32_MAX; |
| 367 | else |
| 368 | PHeader.p_offset = 0; |
| 369 | |
| 370 | // Find the minimum offset for the program header. |
| 371 | for (auto SecName : YamlPhdr.Sections) { |
| 372 | uint32_t Index = 0; |
| 373 | SN2I.lookup(SecName.Section, Index); |
| 374 | const auto &SHeader = SHeaders[Index]; |
| 375 | PHeader.p_offset = std::min(PHeader.p_offset, SHeader.sh_offset); |
| 376 | } |
| 377 | |
| 378 | // Find the maximum offset of the end of a section in order to set p_filesz. |
| 379 | PHeader.p_filesz = 0; |
| 380 | for (auto SecName : YamlPhdr.Sections) { |
| 381 | uint32_t Index = 0; |
| 382 | SN2I.lookup(SecName.Section, Index); |
| 383 | const auto &SHeader = SHeaders[Index]; |
| 384 | uint64_t EndOfSection; |
| 385 | if (SHeader.sh_type == llvm::ELF::SHT_NOBITS) |
| 386 | EndOfSection = SHeader.sh_offset; |
| 387 | else |
| 388 | EndOfSection = SHeader.sh_offset + SHeader.sh_size; |
| 389 | uint64_t EndOfSegment = PHeader.p_offset + PHeader.p_filesz; |
| 390 | EndOfSegment = std::max(EndOfSegment, EndOfSection); |
| 391 | PHeader.p_filesz = EndOfSegment - PHeader.p_offset; |
| 392 | } |
| 393 | |
| 394 | // Find the memory size by adding the size of sections at the end of the |
| 395 | // segment. These should be empty (size of zero) and NOBITS sections. |
| 396 | PHeader.p_memsz = PHeader.p_filesz; |
| 397 | for (auto SecName : YamlPhdr.Sections) { |
| 398 | uint32_t Index = 0; |
| 399 | SN2I.lookup(SecName.Section, Index); |
| 400 | const auto &SHeader = SHeaders[Index]; |
| 401 | if (SHeader.sh_offset == PHeader.p_offset + PHeader.p_filesz) |
| 402 | PHeader.p_memsz += SHeader.sh_size; |
| 403 | } |
| 404 | |
| 405 | // Set the alignment of the segment to be the same as the maximum alignment |
Jake Ehrlich | 9ae2da6 | 2017-11-01 23:14:48 +0000 | [diff] [blame] | 406 | // of the sections with the same offset so that by default the segment |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 407 | // has a valid and sensible alignment. |
Jake Ehrlich | 9ae2da6 | 2017-11-01 23:14:48 +0000 | [diff] [blame] | 408 | if (YamlPhdr.Align) { |
| 409 | PHeader.p_align = *YamlPhdr.Align; |
| 410 | } else { |
| 411 | PHeader.p_align = 1; |
| 412 | for (auto SecName : YamlPhdr.Sections) { |
| 413 | uint32_t Index = 0; |
| 414 | SN2I.lookup(SecName.Section, Index); |
| 415 | const auto &SHeader = SHeaders[Index]; |
| 416 | if (SHeader.sh_offset == PHeader.p_offset) |
| 417 | PHeader.p_align = std::max(PHeader.p_align, SHeader.sh_addralign); |
| 418 | } |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | template <class ELFT> |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 424 | void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, |
| 425 | std::vector<Elf_Sym> &Syms, |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 426 | unsigned SymbolBinding, |
| 427 | const StringTableBuilder &Strtab) { |
Simon Atanasyan | 74f42fd | 2014-03-14 06:53:30 +0000 | [diff] [blame] | 428 | for (const auto &Sym : Symbols) { |
Sean Silva | afcf60f | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 429 | Elf_Sym Symbol; |
| 430 | zero(Symbol); |
| 431 | if (!Sym.Name.empty()) |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 432 | Symbol.st_name = Strtab.getOffset(Sym.Name); |
Sean Silva | 4235ba3 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 433 | Symbol.setBindingAndType(SymbolBinding, Sym.Type); |
Sean Silva | 326c193 | 2013-06-21 01:11:48 +0000 | [diff] [blame] | 434 | if (!Sym.Section.empty()) { |
| 435 | unsigned Index; |
Simon Atanasyan | cde97ea | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 436 | if (SN2I.lookup(Sym.Section, Index)) { |
Jonas Devlieghere | ae6875e | 2018-04-21 21:11:59 +0000 | [diff] [blame] | 437 | WithColor::error() << "Unknown section referenced: '" << Sym.Section |
| 438 | << "' by YAML symbol " << Sym.Name << ".\n"; |
Sean Silva | 326c193 | 2013-06-21 01:11:48 +0000 | [diff] [blame] | 439 | exit(1); |
| 440 | } |
| 441 | Symbol.st_shndx = Index; |
Petr Hosek | 746c778 | 2017-09-07 20:44:16 +0000 | [diff] [blame] | 442 | } else if (Sym.Index) { |
| 443 | Symbol.st_shndx = *Sym.Index; |
| 444 | } |
| 445 | // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier. |
Sean Silva | e38f640 | 2013-06-20 20:59:47 +0000 | [diff] [blame] | 446 | Symbol.st_value = Sym.Value; |
Simon Atanasyan | 848edb1 | 2014-11-06 22:46:24 +0000 | [diff] [blame] | 447 | Symbol.st_other = Sym.Other; |
Sean Silva | e38f640 | 2013-06-20 20:59:47 +0000 | [diff] [blame] | 448 | Symbol.st_size = Sym.Size; |
Sean Silva | afcf60f | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 449 | Syms.push_back(Symbol); |
| 450 | } |
Sean Silva | 4235ba3 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 453 | template <class ELFT> |
| 454 | void |
| 455 | ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
| 456 | const ELFYAML::RawContentSection &Section, |
| 457 | ContiguousBlobAccumulator &CBA) { |
Simon Atanasyan | 19ab8f8 | 2014-05-16 16:01:00 +0000 | [diff] [blame] | 458 | assert(Section.Size >= Section.Content.binary_size() && |
| 459 | "Section size and section content are inconsistent"); |
Simon Atanasyan | 080d7a8 | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 460 | raw_ostream &OS = |
| 461 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Simon Atanasyan | 19ab8f8 | 2014-05-16 16:01:00 +0000 | [diff] [blame] | 462 | Section.Content.writeAsBinary(OS); |
| 463 | for (auto i = Section.Content.binary_size(); i < Section.Size; ++i) |
| 464 | OS.write(0); |
George Rimar | b40956fe | 2018-08-07 08:11:38 +0000 | [diff] [blame] | 465 | if (Section.EntSize) |
| 466 | SHeader.sh_entsize = *Section.EntSize; |
| 467 | else if (Section.Type == llvm::ELF::SHT_RELR) |
Jake Ehrlich | eefdbb4 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 468 | SHeader.sh_entsize = sizeof(Elf_Relr); |
Paul Semel | f3f578d | 2018-07-23 18:49:04 +0000 | [diff] [blame] | 469 | else if (Section.Type == llvm::ELF::SHT_DYNAMIC) |
| 470 | SHeader.sh_entsize = sizeof(Elf_Dyn); |
Jake Ehrlich | eefdbb4 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 471 | else |
| 472 | SHeader.sh_entsize = 0; |
Simon Atanasyan | 19ab8f8 | 2014-05-16 16:01:00 +0000 | [diff] [blame] | 473 | SHeader.sh_size = Section.Size; |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Simon Atanasyan | 09a4607 | 2015-01-25 13:29:25 +0000 | [diff] [blame] | 476 | static bool isMips64EL(const ELFYAML::Object &Doc) { |
| 477 | return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) && |
| 478 | Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) && |
| 479 | Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); |
| 480 | } |
| 481 | |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 482 | template <class ELFT> |
| 483 | bool |
| 484 | ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
| 485 | const ELFYAML::RelocationSection &Section, |
| 486 | ContiguousBlobAccumulator &CBA) { |
Simon Atanasyan | b1e7595 | 2015-05-08 07:05:04 +0000 | [diff] [blame] | 487 | assert((Section.Type == llvm::ELF::SHT_REL || |
| 488 | Section.Type == llvm::ELF::SHT_RELA) && |
| 489 | "Section type is not SHT_REL nor SHT_RELA"); |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 490 | |
| 491 | bool IsRela = Section.Type == llvm::ELF::SHT_RELA; |
| 492 | SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 493 | SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size(); |
| 494 | |
Simon Atanasyan | 080d7a8 | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 495 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 496 | |
| 497 | for (const auto &Rel : Section.Relocations) { |
Adhemerval Zanella | c728e85 | 2015-04-22 15:26:43 +0000 | [diff] [blame] | 498 | unsigned SymIdx = 0; |
| 499 | // Some special relocation, R_ARM_v4BX for instance, does not have |
| 500 | // an external reference. So it ignores the return value of lookup() |
| 501 | // here. |
Petr Hosek | 8231133 | 2017-08-30 23:13:31 +0000 | [diff] [blame] | 502 | if (Rel.Symbol) |
| 503 | SymN2I.lookup(*Rel.Symbol, SymIdx); |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 504 | |
| 505 | if (IsRela) { |
| 506 | Elf_Rela REntry; |
| 507 | zero(REntry); |
| 508 | REntry.r_offset = Rel.Offset; |
| 509 | REntry.r_addend = Rel.Addend; |
Simon Atanasyan | 09a4607 | 2015-01-25 13:29:25 +0000 | [diff] [blame] | 510 | REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc)); |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 511 | OS.write((const char *)&REntry, sizeof(REntry)); |
| 512 | } else { |
| 513 | Elf_Rel REntry; |
| 514 | zero(REntry); |
| 515 | REntry.r_offset = Rel.Offset; |
Simon Atanasyan | 09a4607 | 2015-01-25 13:29:25 +0000 | [diff] [blame] | 516 | REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc)); |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 517 | OS.write((const char *)&REntry, sizeof(REntry)); |
| 518 | } |
| 519 | } |
| 520 | return true; |
| 521 | } |
| 522 | |
Shankar Easwaran | 5872174 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 523 | template <class ELFT> |
| 524 | bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
| 525 | const ELFYAML::Group &Section, |
| 526 | ContiguousBlobAccumulator &CBA) { |
Rui Ueyama | 5a286a0 | 2018-01-12 02:28:31 +0000 | [diff] [blame] | 527 | typedef typename ELFT::Word Elf_Word; |
Simon Atanasyan | b1e7595 | 2015-05-08 07:05:04 +0000 | [diff] [blame] | 528 | assert(Section.Type == llvm::ELF::SHT_GROUP && |
| 529 | "Section type is not SHT_GROUP"); |
Shankar Easwaran | 5872174 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 530 | |
| 531 | SHeader.sh_entsize = sizeof(Elf_Word); |
| 532 | SHeader.sh_size = SHeader.sh_entsize * Section.Members.size(); |
| 533 | |
Simon Atanasyan | 080d7a8 | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 534 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Shankar Easwaran | 5872174 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 535 | |
| 536 | for (auto member : Section.Members) { |
| 537 | Elf_Word SIdx; |
| 538 | unsigned int sectionIndex = 0; |
| 539 | if (member.sectionNameOrType == "GRP_COMDAT") |
| 540 | sectionIndex = llvm::ELF::GRP_COMDAT; |
Xing GUO | 53a9c2e | 2018-12-04 14:27:51 +0000 | [diff] [blame] | 541 | else if (!convertSectionIndex(SN2I, Section.Name, member.sectionNameOrType, |
| 542 | sectionIndex)) |
Shankar Easwaran | 5872174 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 543 | return false; |
Shankar Easwaran | 5872174 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 544 | SIdx = sectionIndex; |
| 545 | OS.write((const char *)&SIdx, sizeof(SIdx)); |
| 546 | } |
| 547 | return true; |
| 548 | } |
| 549 | |
Simon Atanasyan | bd58bdb | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 550 | template <class ELFT> |
| 551 | bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
| 552 | const ELFYAML::MipsABIFlags &Section, |
| 553 | ContiguousBlobAccumulator &CBA) { |
Simon Atanasyan | b1e7595 | 2015-05-08 07:05:04 +0000 | [diff] [blame] | 554 | assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS && |
| 555 | "Section type is not SHT_MIPS_ABIFLAGS"); |
Simon Atanasyan | bd58bdb | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 556 | |
| 557 | object::Elf_Mips_ABIFlags<ELFT> Flags; |
| 558 | zero(Flags); |
| 559 | SHeader.sh_entsize = sizeof(Flags); |
| 560 | SHeader.sh_size = SHeader.sh_entsize; |
| 561 | |
Simon Atanasyan | 080d7a8 | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 562 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Simon Atanasyan | bd58bdb | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 563 | Flags.version = Section.Version; |
| 564 | Flags.isa_level = Section.ISALevel; |
| 565 | Flags.isa_rev = Section.ISARevision; |
| 566 | Flags.gpr_size = Section.GPRSize; |
| 567 | Flags.cpr1_size = Section.CPR1Size; |
| 568 | Flags.cpr2_size = Section.CPR2Size; |
| 569 | Flags.fp_abi = Section.FpABI; |
| 570 | Flags.isa_ext = Section.ISAExtension; |
| 571 | Flags.ases = Section.ASEs; |
| 572 | Flags.flags1 = Section.Flags1; |
| 573 | Flags.flags2 = Section.Flags2; |
| 574 | OS.write((const char *)&Flags, sizeof(Flags)); |
| 575 | |
| 576 | return true; |
| 577 | } |
| 578 | |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 579 | template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() { |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 580 | for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) { |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 581 | StringRef Name = Doc.Sections[i]->Name; |
Dave Lee | 9a5be1f | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 582 | DotShStrtab.add(Name); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 583 | // "+ 1" to take into account the SHT_NULL entry. |
| 584 | if (SN2I.addName(Name, i + 1)) { |
Jonas Devlieghere | ae6875e | 2018-04-21 21:11:59 +0000 | [diff] [blame] | 585 | WithColor::error() << "Repeated section name: '" << Name |
| 586 | << "' at YAML section number " << i << ".\n"; |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 587 | return false; |
| 588 | } |
Sean Silva | 4235ba3 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 589 | } |
Dave Lee | 9a5be1f | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 590 | |
| 591 | auto SecNo = 1 + Doc.Sections.size(); |
| 592 | // Add special sections after input sections, if necessary. |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 593 | for (const auto &Name : implicitSectionNames()) |
Dave Lee | 9a5be1f | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 594 | if (!SN2I.addName(Name, SecNo)) { |
| 595 | // Account for this section, since it wasn't in the Doc |
| 596 | ++SecNo; |
| 597 | DotShStrtab.add(Name); |
| 598 | } |
| 599 | |
| 600 | DotShStrtab.finalize(); |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 601 | return true; |
Sean Silva | afcf60f | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 604 | template <class ELFT> |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 605 | bool |
| 606 | ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex, |
| 607 | const std::vector<ELFYAML::Symbol> &Symbols) { |
| 608 | for (const auto &Sym : Symbols) { |
| 609 | ++StartIndex; |
| 610 | if (Sym.Name.empty()) |
| 611 | continue; |
| 612 | if (SymN2I.addName(Sym.Name, StartIndex)) { |
Jonas Devlieghere | ae6875e | 2018-04-21 21:11:59 +0000 | [diff] [blame] | 613 | WithColor::error() << "Repeated symbol name: '" << Sym.Name << "'.\n"; |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 614 | return false; |
| 615 | } |
| 616 | } |
| 617 | return true; |
| 618 | } |
| 619 | |
| 620 | template <class ELFT> |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 621 | int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) { |
Simon Atanasyan | 38ac43b1 | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 622 | ELFState<ELFT> State(Doc); |
| 623 | if (!State.buildSectionIndex()) |
| 624 | return 1; |
| 625 | |
Simon Atanasyan | eb0c909 | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 626 | std::size_t StartSymIndex = 0; |
| 627 | if (!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Local) || |
| 628 | !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Global) || |
| 629 | !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Weak)) |
| 630 | return 1; |
| 631 | |
Sean Silva | 274264c | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 632 | Elf_Ehdr Header; |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 633 | State.initELFHeader(Header); |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 634 | |
Sean Silva | 274264c | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 635 | // TODO: Flesh out section header support. |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 636 | |
| 637 | std::vector<Elf_Phdr> PHeaders; |
| 638 | State.initProgramHeaders(PHeaders); |
Sean Silva | 274264c | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 639 | |
Sean Silva | 0382b30 | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 640 | // XXX: This offset is tightly coupled with the order that we write |
| 641 | // things to `OS`. |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 642 | const size_t SectionContentBeginOffset = Header.e_ehsize + |
| 643 | Header.e_phentsize * Header.e_phnum + |
| 644 | Header.e_shentsize * Header.e_shnum; |
Sean Silva | 0382b30 | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 645 | ContiguousBlobAccumulator CBA(SectionContentBeginOffset); |
Sean Silva | c18f66e | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 646 | |
Sean Silva | 274264c | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 647 | std::vector<Elf_Shdr> SHeaders; |
Simon Atanasyan | be2ec9b | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 648 | if(!State.initSectionHeaders(SHeaders, CBA)) |
| 649 | return 1; |
Sean Silva | 274264c | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 650 | |
Dave Lee | 9a5be1f | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 651 | // Populate SHeaders with implicit sections not present in the Doc |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 652 | for (const auto &Name : State.implicitSectionNames()) |
Dave Lee | 9a5be1f | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 653 | if (State.SN2I.get(Name) >= SHeaders.size()) |
| 654 | SHeaders.push_back({}); |
Sean Silva | 068463b | 2013-06-22 01:38:00 +0000 | [diff] [blame] | 655 | |
Dave Lee | 9a5be1f | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 656 | // Initialize the implicit sections |
| 657 | auto Index = State.SN2I.get(".symtab"); |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 658 | State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Static, CBA); |
Dave Lee | 9a5be1f | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 659 | Index = State.SN2I.get(".strtab"); |
| 660 | State.initStrtabSectionHeader(SHeaders[Index], ".strtab", State.DotStrtab, CBA); |
| 661 | Index = State.SN2I.get(".shstrtab"); |
| 662 | State.initStrtabSectionHeader(SHeaders[Index], ".shstrtab", State.DotShStrtab, CBA); |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 663 | if (State.hasDynamicSymbols()) { |
| 664 | Index = State.SN2I.get(".dynsym"); |
| 665 | State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Dynamic, CBA); |
| 666 | SHeaders[Index].sh_flags |= ELF::SHF_ALLOC; |
| 667 | Index = State.SN2I.get(".dynstr"); |
| 668 | State.initStrtabSectionHeader(SHeaders[Index], ".dynstr", State.DotDynstr, CBA); |
| 669 | SHeaders[Index].sh_flags |= ELF::SHF_ALLOC; |
| 670 | } |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 671 | |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 672 | // Now we can decide segment offsets |
| 673 | State.setProgramHeaderLayout(PHeaders, SHeaders); |
| 674 | |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 675 | OS.write((const char *)&Header, sizeof(Header)); |
Petr Hosek | e7726ca | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 676 | writeArrayData(OS, makeArrayRef(PHeaders)); |
Will Dietz | 6dd7893 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 677 | writeArrayData(OS, makeArrayRef(SHeaders)); |
Sean Silva | 2a7e79a | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 678 | CBA.writeBlobToStream(OS); |
Sean Silva | 4b548ec | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 679 | return 0; |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 682 | template <class ELFT> bool ELFState<ELFT>::hasDynamicSymbols() const { |
| 683 | return Doc.DynamicSymbols.Global.size() > 0 || |
| 684 | Doc.DynamicSymbols.Weak.size() > 0 || |
| 685 | Doc.DynamicSymbols.Local.size() > 0; |
| 686 | } |
| 687 | |
Xing GUO | a4079af | 2018-12-07 11:04:22 +0000 | [diff] [blame] | 688 | template <class ELFT> |
| 689 | SmallVector<const char *, 5> ELFState<ELFT>::implicitSectionNames() const { |
Dave Lee | 9c5b799 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 690 | if (!hasDynamicSymbols()) |
| 691 | return {".symtab", ".strtab", ".shstrtab"}; |
| 692 | return {".symtab", ".strtab", ".shstrtab", ".dynsym", ".dynstr"}; |
| 693 | } |
| 694 | |
Sean Silva | efc7898 | 2013-06-22 01:03:35 +0000 | [diff] [blame] | 695 | static bool is64Bit(const ELFYAML::Object &Doc) { |
| 696 | return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64); |
| 697 | } |
| 698 | |
| 699 | static bool isLittleEndian(const ELFYAML::Object &Doc) { |
| 700 | return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); |
| 701 | } |
| 702 | |
Chris Bieneman | 9cf90e6 | 2016-06-27 19:53:53 +0000 | [diff] [blame] | 703 | int yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out) { |
Sean Silva | efc7898 | 2013-06-22 01:03:35 +0000 | [diff] [blame] | 704 | if (is64Bit(Doc)) { |
| 705 | if (isLittleEndian(Doc)) |
Rui Ueyama | 0a75940 | 2018-01-12 01:40:32 +0000 | [diff] [blame] | 706 | return ELFState<object::ELF64LE>::writeELF(Out, Doc); |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 707 | else |
Rui Ueyama | 0a75940 | 2018-01-12 01:40:32 +0000 | [diff] [blame] | 708 | return ELFState<object::ELF64BE>::writeELF(Out, Doc); |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 709 | } else { |
Sean Silva | efc7898 | 2013-06-22 01:03:35 +0000 | [diff] [blame] | 710 | if (isLittleEndian(Doc)) |
Rui Ueyama | 0a75940 | 2018-01-12 01:40:32 +0000 | [diff] [blame] | 711 | return ELFState<object::ELF32LE>::writeELF(Out, Doc); |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 712 | else |
Rui Ueyama | 0a75940 | 2018-01-12 01:40:32 +0000 | [diff] [blame] | 713 | return ELFState<object::ELF32BE>::writeELF(Out, Doc); |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 714 | } |
Sean Silva | 5918b7a | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 715 | } |