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