Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 1 | //===- yaml2elf - Convert YAML to a ELF object file -----------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// The ELF component of yaml2obj. |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Will Dietz | 0b48c73 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/ArrayRef.h" |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSet.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 16 | #include "llvm/BinaryFormat/ELF.h" |
Rafael Espindola | 97de474 | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 17 | #include "llvm/MC/StringTableBuilder.h" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ELFObjectFile.h" |
Rafael Espindola | ebd9193 | 2016-03-01 19:15:06 +0000 | [diff] [blame] | 19 | #include "llvm/ObjectYAML/ELFYAML.h" |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 20 | #include "llvm/ObjectYAML/yaml2obj.h" |
George Rimar | d063c7d | 2019-02-20 13:58:43 +0000 | [diff] [blame] | 21 | #include "llvm/Support/EndianStream.h" |
George Rimar | 1a219aa | 2019-09-24 14:22:37 +0000 | [diff] [blame] | 22 | #include "llvm/Support/LEB128.h" |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Jonas Devlieghere | 2cd41eb | 2018-04-21 21:11:59 +0000 | [diff] [blame] | 24 | #include "llvm/Support/WithColor.h" |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 25 | #include "llvm/Support/YAMLTraits.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 30 | // This class is used to build up a contiguous binary blob while keeping |
| 31 | // track of an offset in the output (which notionally begins at |
| 32 | // `InitialOffset`). |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 33 | namespace { |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 34 | class ContiguousBlobAccumulator { |
| 35 | const uint64_t InitialOffset; |
Sean Silva | bd3bc69 | 2013-06-20 19:11:41 +0000 | [diff] [blame] | 36 | SmallVector<char, 128> Buf; |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 37 | raw_svector_ostream OS; |
| 38 | |
Sean Silva | d93323f | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 39 | /// \returns The new offset. |
| 40 | uint64_t padToAlignment(unsigned Align) { |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 41 | if (Align == 0) |
| 42 | Align = 1; |
Sean Silva | d93323f | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 43 | uint64_t CurrentOffset = InitialOffset + OS.tell(); |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 44 | uint64_t AlignedOffset = alignTo(CurrentOffset, Align); |
George Rimar | bd8bfd3 | 2019-04-29 12:05:53 +0000 | [diff] [blame] | 45 | OS.write_zeros(AlignedOffset - CurrentOffset); |
Sean Silva | d93323f | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 46 | return AlignedOffset; // == CurrentOffset; |
| 47 | } |
| 48 | |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 49 | public: |
Sean Silva | bd3bc69 | 2013-06-20 19:11:41 +0000 | [diff] [blame] | 50 | ContiguousBlobAccumulator(uint64_t InitialOffset_) |
| 51 | : InitialOffset(InitialOffset_), Buf(), OS(Buf) {} |
Sean Silva | d93323f | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 52 | template <class Integer> |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 53 | raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) { |
Sean Silva | d93323f | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 54 | Offset = padToAlignment(Align); |
| 55 | return OS; |
| 56 | } |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 57 | void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); } |
| 58 | }; |
| 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 |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 61 | // sections and symbols can be referenced by name instead of by index. |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 62 | class NameToIdxMap { |
Puyan Lotfi | a10f016 | 2019-05-11 17:03:36 +0000 | [diff] [blame] | 63 | StringMap<unsigned> Map; |
| 64 | |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 65 | public: |
Puyan Lotfi | a10f016 | 2019-05-11 17:03:36 +0000 | [diff] [blame] | 66 | /// \Returns false if name is already present in the map. |
| 67 | bool addName(StringRef Name, unsigned Ndx) { |
| 68 | return Map.insert({Name, Ndx}).second; |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 69 | } |
Puyan Lotfi | a10f016 | 2019-05-11 17:03:36 +0000 | [diff] [blame] | 70 | /// \Returns false if name is not present in the map. |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 71 | bool lookup(StringRef Name, unsigned &Idx) const { |
Puyan Lotfi | a10f016 | 2019-05-11 17:03:36 +0000 | [diff] [blame] | 72 | auto I = Map.find(Name); |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 73 | if (I == Map.end()) |
Puyan Lotfi | a10f016 | 2019-05-11 17:03:36 +0000 | [diff] [blame] | 74 | return false; |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 75 | Idx = I->getValue(); |
Puyan Lotfi | a10f016 | 2019-05-11 17:03:36 +0000 | [diff] [blame] | 76 | return true; |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 77 | } |
Puyan Lotfi | a10f016 | 2019-05-11 17:03:36 +0000 | [diff] [blame] | 78 | /// Asserts if name is not present in the map. |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 79 | unsigned get(StringRef Name) const { |
Puyan Lotfi | a10f016 | 2019-05-11 17:03:36 +0000 | [diff] [blame] | 80 | unsigned Idx; |
| 81 | if (lookup(Name, Idx)) |
| 82 | return Idx; |
| 83 | assert(false && "Expected section not found in index"); |
| 84 | return 0; |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 85 | } |
| 86 | unsigned size() const { return Map.size(); } |
| 87 | }; |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 88 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 89 | /// "Single point of truth" for the ELF file construction. |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 90 | /// TODO: This class still has a ways to go before it is truly a "single |
| 91 | /// point of truth". |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 92 | template <class ELFT> class ELFState { |
Rui Ueyama | 478d635 | 2018-01-12 02:28:31 +0000 | [diff] [blame] | 93 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| 94 | typedef typename ELFT::Phdr Elf_Phdr; |
| 95 | typedef typename ELFT::Shdr Elf_Shdr; |
| 96 | typedef typename ELFT::Sym Elf_Sym; |
| 97 | typedef typename ELFT::Rel Elf_Rel; |
| 98 | typedef typename ELFT::Rela Elf_Rela; |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 99 | typedef typename ELFT::Relr Elf_Relr; |
Paul Semel | 1dbbfba | 2018-07-23 18:49:04 +0000 | [diff] [blame] | 100 | typedef typename ELFT::Dyn Elf_Dyn; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 101 | |
Dave Lee | 67b4966 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 102 | enum class SymtabType { Static, Dynamic }; |
| 103 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 104 | /// The future ".strtab" section. |
Rafael Espindola | 21956e4 | 2015-10-23 21:48:05 +0000 | [diff] [blame] | 105 | StringTableBuilder DotStrtab{StringTableBuilder::ELF}; |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 106 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 107 | /// The future ".shstrtab" section. |
Rafael Espindola | 21956e4 | 2015-10-23 21:48:05 +0000 | [diff] [blame] | 108 | StringTableBuilder DotShStrtab{StringTableBuilder::ELF}; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 109 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 110 | /// The future ".dynstr" section. |
Dave Lee | 67b4966 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 111 | StringTableBuilder DotDynstr{StringTableBuilder::ELF}; |
| 112 | |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 113 | NameToIdxMap SN2I; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 114 | NameToIdxMap SymN2I; |
George Rimar | 9120844 | 2019-08-22 12:39:56 +0000 | [diff] [blame] | 115 | NameToIdxMap DynSymN2I; |
George Rimar | 13a364e | 2019-07-22 12:01:52 +0000 | [diff] [blame] | 116 | ELFYAML::Object &Doc; |
Sean Silva | c1c290b | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 117 | |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 118 | bool HasError = false; |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 119 | yaml::ErrorHandler ErrHandler; |
| 120 | void reportError(const Twine &Msg); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 121 | |
| 122 | std::vector<Elf_Sym> toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols, |
| 123 | const StringTableBuilder &Strtab); |
| 124 | unsigned toSectionIndex(StringRef S, StringRef LocSec, StringRef LocSym = ""); |
| 125 | unsigned toSymbolIndex(StringRef S, StringRef LocSec, bool IsDynamic); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 126 | |
| 127 | void buildSectionIndex(); |
| 128 | void buildSymbolIndexes(); |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 129 | void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders); |
George Rimar | 33b1a0e | 2019-09-05 08:59:28 +0000 | [diff] [blame] | 130 | bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header, |
| 131 | StringRef SecName, ELFYAML::Section *YAMLSec); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 132 | void initSectionHeaders(std::vector<Elf_Shdr> &SHeaders, |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 133 | ContiguousBlobAccumulator &CBA); |
Dave Lee | 67b4966 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 134 | void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType, |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 135 | ContiguousBlobAccumulator &CBA, |
| 136 | ELFYAML::Section *YAMLSec); |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 137 | void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, |
| 138 | StringTableBuilder &STB, |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 139 | ContiguousBlobAccumulator &CBA, |
| 140 | ELFYAML::Section *YAMLSec); |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 141 | void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders, |
| 142 | std::vector<Elf_Shdr> &SHeaders); |
George Rimar | 33b1a0e | 2019-09-05 08:59:28 +0000 | [diff] [blame] | 143 | void finalizeStrings(); |
Fangrui Song | c3bc697 | 2019-09-05 14:25:57 +0000 | [diff] [blame] | 144 | void writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 145 | void writeSectionContent(Elf_Shdr &SHeader, |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 146 | const ELFYAML::RawContentSection &Section, |
| 147 | ContiguousBlobAccumulator &CBA); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 148 | void writeSectionContent(Elf_Shdr &SHeader, |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 149 | const ELFYAML::RelocationSection &Section, |
| 150 | ContiguousBlobAccumulator &CBA); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 151 | void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group, |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 152 | ContiguousBlobAccumulator &CBA); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 153 | void writeSectionContent(Elf_Shdr &SHeader, |
George Rimar | d396305 | 2019-08-08 09:49:05 +0000 | [diff] [blame] | 154 | const ELFYAML::SymtabShndxSection &Shndx, |
| 155 | ContiguousBlobAccumulator &CBA); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 156 | void writeSectionContent(Elf_Shdr &SHeader, |
George Rimar | 646af08 | 2019-02-19 15:29:07 +0000 | [diff] [blame] | 157 | const ELFYAML::SymverSection &Section, |
| 158 | ContiguousBlobAccumulator &CBA); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 159 | void writeSectionContent(Elf_Shdr &SHeader, |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 160 | const ELFYAML::VerneedSection &Section, |
| 161 | ContiguousBlobAccumulator &CBA); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 162 | void writeSectionContent(Elf_Shdr &SHeader, |
George Rimar | 623ae72 | 2019-02-21 12:21:43 +0000 | [diff] [blame] | 163 | const ELFYAML::VerdefSection &Section, |
| 164 | ContiguousBlobAccumulator &CBA); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 165 | void writeSectionContent(Elf_Shdr &SHeader, |
Simon Atanasyan | 04d9e65 | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 166 | const ELFYAML::MipsABIFlags &Section, |
| 167 | ContiguousBlobAccumulator &CBA); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 168 | void writeSectionContent(Elf_Shdr &SHeader, |
George Rimar | 0e7ed91 | 2019-02-09 11:34:28 +0000 | [diff] [blame] | 169 | const ELFYAML::DynamicSection &Section, |
| 170 | ContiguousBlobAccumulator &CBA); |
George Rimar | 1a219aa | 2019-09-24 14:22:37 +0000 | [diff] [blame] | 171 | void writeSectionContent(Elf_Shdr &SHeader, |
| 172 | const ELFYAML::StackSizesSection &Section, |
| 173 | ContiguousBlobAccumulator &CBA); |
George Rimar | e5163eb | 2019-10-01 09:45:59 +0000 | [diff] [blame] | 174 | void writeSectionContent(Elf_Shdr &SHeader, |
| 175 | const ELFYAML::HashSection &Section, |
| 176 | ContiguousBlobAccumulator &CBA); |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 177 | ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH); |
| 178 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 179 | public: |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 180 | static bool writeELF(raw_ostream &OS, ELFYAML::Object &Doc, |
| 181 | yaml::ErrorHandler EH); |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 182 | }; |
| 183 | } // end anonymous namespace |
| 184 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 185 | template <class T> static size_t arrayDataSize(ArrayRef<T> A) { |
| 186 | return A.size() * sizeof(T); |
| 187 | } |
| 188 | |
| 189 | template <class T> static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) { |
| 190 | OS.write((const char *)A.data(), arrayDataSize(A)); |
| 191 | } |
| 192 | |
| 193 | template <class T> static void zero(T &Obj) { memset(&Obj, 0, sizeof(Obj)); } |
| 194 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 195 | template <class ELFT> |
| 196 | ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH) |
| 197 | : Doc(D), ErrHandler(EH) { |
George Rimar | 13a364e | 2019-07-22 12:01:52 +0000 | [diff] [blame] | 198 | StringSet<> DocSections; |
| 199 | for (std::unique_ptr<ELFYAML::Section> &D : Doc.Sections) |
| 200 | if (!D->Name.empty()) |
| 201 | DocSections.insert(D->Name); |
| 202 | |
George Rimar | 1957d68 | 2019-07-23 11:03:37 +0000 | [diff] [blame] | 203 | // Insert SHT_NULL section implicitly when it is not defined in YAML. |
| 204 | if (Doc.Sections.empty() || Doc.Sections.front()->Type != ELF::SHT_NULL) |
| 205 | Doc.Sections.insert( |
| 206 | Doc.Sections.begin(), |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 207 | std::make_unique<ELFYAML::Section>( |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 208 | ELFYAML::Section::SectionKind::RawContent, /*IsImplicit=*/true)); |
George Rimar | ab658f4 | 2019-07-23 07:38:44 +0000 | [diff] [blame] | 209 | |
George Rimar | 13a364e | 2019-07-22 12:01:52 +0000 | [diff] [blame] | 210 | std::vector<StringRef> ImplicitSections = {".symtab", ".strtab", ".shstrtab"}; |
| 211 | if (!Doc.DynamicSymbols.empty()) |
| 212 | ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"}); |
| 213 | |
| 214 | // Insert placeholders for implicit sections that are not |
| 215 | // defined explicitly in YAML. |
| 216 | for (StringRef SecName : ImplicitSections) { |
| 217 | if (DocSections.count(SecName)) |
| 218 | continue; |
| 219 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 220 | std::unique_ptr<ELFYAML::Section> Sec = std::make_unique<ELFYAML::Section>( |
George Rimar | 13a364e | 2019-07-22 12:01:52 +0000 | [diff] [blame] | 221 | ELFYAML::Section::SectionKind::RawContent, true /*IsImplicit*/); |
| 222 | Sec->Name = SecName; |
| 223 | Doc.Sections.push_back(std::move(Sec)); |
| 224 | } |
| 225 | } |
| 226 | |
Fangrui Song | c3bc697 | 2019-09-05 14:25:57 +0000 | [diff] [blame] | 227 | template <class ELFT> |
| 228 | void ELFState<ELFT>::writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS) { |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 229 | using namespace llvm::ELF; |
Fangrui Song | c3bc697 | 2019-09-05 14:25:57 +0000 | [diff] [blame] | 230 | |
| 231 | Elf_Ehdr Header; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 232 | zero(Header); |
| 233 | Header.e_ident[EI_MAG0] = 0x7f; |
| 234 | Header.e_ident[EI_MAG1] = 'E'; |
| 235 | Header.e_ident[EI_MAG2] = 'L'; |
| 236 | Header.e_ident[EI_MAG3] = 'F'; |
| 237 | Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; |
George Rimar | a5a0a0f | 2019-03-07 12:09:19 +0000 | [diff] [blame] | 238 | Header.e_ident[EI_DATA] = Doc.Header.Data; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 239 | Header.e_ident[EI_VERSION] = EV_CURRENT; |
| 240 | Header.e_ident[EI_OSABI] = Doc.Header.OSABI; |
George Rimar | 6367d7a | 2018-12-20 10:43:49 +0000 | [diff] [blame] | 241 | Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 242 | Header.e_type = Doc.Header.Type; |
| 243 | Header.e_machine = Doc.Header.Machine; |
| 244 | Header.e_version = EV_CURRENT; |
| 245 | Header.e_entry = Doc.Header.Entry; |
Alex Brachet | 0b69c59 | 2019-09-06 02:27:55 +0000 | [diff] [blame] | 246 | Header.e_phoff = Doc.ProgramHeaders.size() ? sizeof(Header) : 0; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 247 | Header.e_flags = Doc.Header.Flags; |
| 248 | Header.e_ehsize = sizeof(Elf_Ehdr); |
Alex Brachet | 0b69c59 | 2019-09-06 02:27:55 +0000 | [diff] [blame] | 249 | Header.e_phentsize = Doc.ProgramHeaders.size() ? sizeof(Elf_Phdr) : 0; |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 250 | Header.e_phnum = Doc.ProgramHeaders.size(); |
George Rimar | 687d47c | 2019-06-27 11:08:42 +0000 | [diff] [blame] | 251 | |
| 252 | Header.e_shentsize = |
| 253 | Doc.Header.SHEntSize ? (uint16_t)*Doc.Header.SHEntSize : sizeof(Elf_Shdr); |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 254 | // Immediately following the ELF header and program headers. |
Fangrui Song | c3bc697 | 2019-09-05 14:25:57 +0000 | [diff] [blame] | 255 | // Align the start of the section header and write the ELF header. |
Fangrui Song | d20c41d | 2019-09-06 09:23:17 +0000 | [diff] [blame] | 256 | uint64_t SHOff; |
| 257 | CBA.getOSAndAlignedOffset(SHOff, sizeof(typename ELFT::uint)); |
| 258 | Header.e_shoff = |
| 259 | Doc.Header.SHOff ? typename ELFT::uint(*Doc.Header.SHOff) : SHOff; |
George Rimar | 687d47c | 2019-06-27 11:08:42 +0000 | [diff] [blame] | 260 | Header.e_shnum = |
George Rimar | 1480229 | 2019-07-25 10:19:23 +0000 | [diff] [blame] | 261 | Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : Doc.Sections.size(); |
George Rimar | 687d47c | 2019-06-27 11:08:42 +0000 | [diff] [blame] | 262 | Header.e_shstrndx = Doc.Header.SHStrNdx ? (uint16_t)*Doc.Header.SHStrNdx |
| 263 | : SN2I.get(".shstrtab"); |
Fangrui Song | c3bc697 | 2019-09-05 14:25:57 +0000 | [diff] [blame] | 264 | |
| 265 | OS.write((const char *)&Header, sizeof(Header)); |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | template <class ELFT> |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 269 | void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) { |
| 270 | for (const auto &YamlPhdr : Doc.ProgramHeaders) { |
| 271 | Elf_Phdr Phdr; |
| 272 | Phdr.p_type = YamlPhdr.Type; |
| 273 | Phdr.p_flags = YamlPhdr.Flags; |
| 274 | Phdr.p_vaddr = YamlPhdr.VAddr; |
| 275 | Phdr.p_paddr = YamlPhdr.PAddr; |
| 276 | PHeaders.push_back(Phdr); |
| 277 | } |
| 278 | } |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 279 | |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 280 | template <class ELFT> |
| 281 | unsigned ELFState<ELFT>::toSectionIndex(StringRef S, StringRef LocSec, |
| 282 | StringRef LocSym) { |
| 283 | unsigned Index; |
| 284 | if (SN2I.lookup(S, Index) || to_integer(S, Index)) |
| 285 | return Index; |
| 286 | |
| 287 | assert(LocSec.empty() || LocSym.empty()); |
| 288 | if (!LocSym.empty()) |
| 289 | reportError("unknown section referenced: '" + S + "' by YAML symbol '" + |
| 290 | LocSym + "'"); |
| 291 | else |
| 292 | reportError("unknown section referenced: '" + S + "' by YAML section '" + |
| 293 | LocSec + "'"); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | template <class ELFT> |
| 298 | unsigned ELFState<ELFT>::toSymbolIndex(StringRef S, StringRef LocSec, |
| 299 | bool IsDynamic) { |
| 300 | const NameToIdxMap &SymMap = IsDynamic ? DynSymN2I : SymN2I; |
| 301 | unsigned Index; |
| 302 | // Here we try to look up S in the symbol table. If it is not there, |
| 303 | // treat its value as a symbol index. |
| 304 | if (!SymMap.lookup(S, Index) && !to_integer(S, Index)) { |
| 305 | reportError("unknown symbol referenced: '" + S + "' by YAML section '" + |
| 306 | LocSec + "'"); |
| 307 | return 0; |
Xing GUO | 9822835 | 2018-12-04 14:27:51 +0000 | [diff] [blame] | 308 | } |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 309 | return Index; |
Xing GUO | 9822835 | 2018-12-04 14:27:51 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 312 | template <class ELFT> |
George Rimar | 33b1a0e | 2019-09-05 08:59:28 +0000 | [diff] [blame] | 313 | bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA, |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 314 | Elf_Shdr &Header, StringRef SecName, |
| 315 | ELFYAML::Section *YAMLSec) { |
| 316 | // Check if the header was already initialized. |
| 317 | if (Header.sh_offset) |
| 318 | return false; |
| 319 | |
| 320 | if (SecName == ".symtab") |
George Rimar | 33b1a0e | 2019-09-05 08:59:28 +0000 | [diff] [blame] | 321 | initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec); |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 322 | else if (SecName == ".strtab") |
George Rimar | 33b1a0e | 2019-09-05 08:59:28 +0000 | [diff] [blame] | 323 | initStrtabSectionHeader(Header, SecName, DotStrtab, CBA, YAMLSec); |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 324 | else if (SecName == ".shstrtab") |
George Rimar | 33b1a0e | 2019-09-05 08:59:28 +0000 | [diff] [blame] | 325 | initStrtabSectionHeader(Header, SecName, DotShStrtab, CBA, YAMLSec); |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 326 | else if (SecName == ".dynsym") |
George Rimar | 33b1a0e | 2019-09-05 08:59:28 +0000 | [diff] [blame] | 327 | initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec); |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 328 | else if (SecName == ".dynstr") |
George Rimar | 33b1a0e | 2019-09-05 08:59:28 +0000 | [diff] [blame] | 329 | initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec); |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 330 | else |
| 331 | return false; |
George Rimar | 9df825f | 2019-07-02 10:20:12 +0000 | [diff] [blame] | 332 | |
George Rimar | 86cc736 | 2019-09-02 09:47:17 +0000 | [diff] [blame] | 333 | // Override the fields if requested. |
George Rimar | eb41f7f | 2019-07-11 12:59:29 +0000 | [diff] [blame] | 334 | if (YAMLSec) { |
George Rimar | 86cc736 | 2019-09-02 09:47:17 +0000 | [diff] [blame] | 335 | if (YAMLSec->ShName) |
| 336 | Header.sh_name = *YAMLSec->ShName; |
George Rimar | eb41f7f | 2019-07-11 12:59:29 +0000 | [diff] [blame] | 337 | if (YAMLSec->ShOffset) |
| 338 | Header.sh_offset = *YAMLSec->ShOffset; |
| 339 | if (YAMLSec->ShSize) |
| 340 | Header.sh_size = *YAMLSec->ShSize; |
| 341 | } |
George Rimar | 9df825f | 2019-07-02 10:20:12 +0000 | [diff] [blame] | 342 | |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 343 | return true; |
| 344 | } |
| 345 | |
George Rimar | cfc2bcc | 2019-09-25 12:09:30 +0000 | [diff] [blame] | 346 | StringRef llvm::ELFYAML::dropUniqueSuffix(StringRef S) { |
George Rimar | 60dc5d4 | 2019-06-25 08:22:57 +0000 | [diff] [blame] | 347 | size_t SuffixPos = S.rfind(" ["); |
| 348 | if (SuffixPos == StringRef::npos) |
| 349 | return S; |
| 350 | return S.substr(0, SuffixPos); |
| 351 | } |
| 352 | |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 353 | template <class ELFT> |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 354 | void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders, |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 355 | ContiguousBlobAccumulator &CBA) { |
| 356 | // Ensure SHN_UNDEF entry is present. An all-zero section header is a |
| 357 | // valid SHN_UNDEF entry since SHT_NULL == 0. |
George Rimar | ab658f4 | 2019-07-23 07:38:44 +0000 | [diff] [blame] | 358 | SHeaders.resize(Doc.Sections.size()); |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 359 | |
George Rimar | 1957d68 | 2019-07-23 11:03:37 +0000 | [diff] [blame] | 360 | for (size_t I = 0; I < Doc.Sections.size(); ++I) { |
George Rimar | ab658f4 | 2019-07-23 07:38:44 +0000 | [diff] [blame] | 361 | ELFYAML::Section *Sec = Doc.Sections[I].get(); |
George Rimar | 1480229 | 2019-07-25 10:19:23 +0000 | [diff] [blame] | 362 | if (I == 0 && Sec->IsImplicit) |
George Rimar | 1957d68 | 2019-07-23 11:03:37 +0000 | [diff] [blame] | 363 | continue; |
George Rimar | 1957d68 | 2019-07-23 11:03:37 +0000 | [diff] [blame] | 364 | |
George Rimar | eb394e9 | 2019-06-07 08:31:36 +0000 | [diff] [blame] | 365 | // We have a few sections like string or symbol tables that are usually |
| 366 | // added implicitly to the end. However, if they are explicitly specified |
| 367 | // in the YAML, we need to write them here. This ensures the file offset |
| 368 | // remains correct. |
George Rimar | 1480229 | 2019-07-25 10:19:23 +0000 | [diff] [blame] | 369 | Elf_Shdr &SHeader = SHeaders[I]; |
George Rimar | 33b1a0e | 2019-09-05 08:59:28 +0000 | [diff] [blame] | 370 | if (initImplicitHeader(CBA, SHeader, Sec->Name, |
George Rimar | 13a364e | 2019-07-22 12:01:52 +0000 | [diff] [blame] | 371 | Sec->IsImplicit ? nullptr : Sec)) |
George Rimar | eb394e9 | 2019-06-07 08:31:36 +0000 | [diff] [blame] | 372 | continue; |
| 373 | |
| 374 | assert(Sec && "It can't be null unless it is an implicit section. But all " |
| 375 | "implicit sections should already have been handled above."); |
| 376 | |
George Rimar | cfc2bcc | 2019-09-25 12:09:30 +0000 | [diff] [blame] | 377 | SHeader.sh_name = |
| 378 | DotShStrtab.getOffset(ELFYAML::dropUniqueSuffix(Sec->Name)); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 379 | SHeader.sh_type = Sec->Type; |
George Rimar | cfa1a62 | 2019-06-14 11:01:14 +0000 | [diff] [blame] | 380 | if (Sec->Flags) |
| 381 | SHeader.sh_flags = *Sec->Flags; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 382 | SHeader.sh_addr = Sec->Address; |
| 383 | SHeader.sh_addralign = Sec->AddressAlign; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 384 | |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 385 | if (!Sec->Link.empty()) |
| 386 | SHeader.sh_link = toSectionIndex(Sec->Link, Sec->Name); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 387 | |
George Rimar | 1480229 | 2019-07-25 10:19:23 +0000 | [diff] [blame] | 388 | if (I == 0) { |
| 389 | if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) { |
| 390 | // We do not write any content for special SHN_UNDEF section. |
| 391 | if (RawSec->Size) |
| 392 | SHeader.sh_size = *RawSec->Size; |
| 393 | if (RawSec->Info) |
| 394 | SHeader.sh_info = *RawSec->Info; |
| 395 | } |
| 396 | if (Sec->EntSize) |
| 397 | SHeader.sh_entsize = *Sec->EntSize; |
| 398 | } else if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) { |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 399 | writeSectionContent(SHeader, *S, CBA); |
George Rimar | d396305 | 2019-08-08 09:49:05 +0000 | [diff] [blame] | 400 | } else if (auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) { |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 401 | writeSectionContent(SHeader, *S, CBA); |
George Rimar | eb394e9 | 2019-06-07 08:31:36 +0000 | [diff] [blame] | 402 | } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) { |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 403 | writeSectionContent(SHeader, *S, CBA); |
George Rimar | eb394e9 | 2019-06-07 08:31:36 +0000 | [diff] [blame] | 404 | } else if (auto S = dyn_cast<ELFYAML::Group>(Sec)) { |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 405 | writeSectionContent(SHeader, *S, CBA); |
George Rimar | eb394e9 | 2019-06-07 08:31:36 +0000 | [diff] [blame] | 406 | } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) { |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 407 | writeSectionContent(SHeader, *S, CBA); |
George Rimar | eb394e9 | 2019-06-07 08:31:36 +0000 | [diff] [blame] | 408 | } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) { |
Simon Atanasyan | 5db0276 | 2015-07-03 23:00:54 +0000 | [diff] [blame] | 409 | SHeader.sh_entsize = 0; |
| 410 | SHeader.sh_size = S->Size; |
| 411 | // SHT_NOBITS section does not have content |
| 412 | // so just to setup the section offset. |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 413 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
George Rimar | eb394e9 | 2019-06-07 08:31:36 +0000 | [diff] [blame] | 414 | } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) { |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 415 | writeSectionContent(SHeader, *S, CBA); |
George Rimar | eb394e9 | 2019-06-07 08:31:36 +0000 | [diff] [blame] | 416 | } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) { |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 417 | writeSectionContent(SHeader, *S, CBA); |
George Rimar | eb394e9 | 2019-06-07 08:31:36 +0000 | [diff] [blame] | 418 | } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) { |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 419 | writeSectionContent(SHeader, *S, CBA); |
George Rimar | eb394e9 | 2019-06-07 08:31:36 +0000 | [diff] [blame] | 420 | } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) { |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 421 | writeSectionContent(SHeader, *S, CBA); |
George Rimar | 1a219aa | 2019-09-24 14:22:37 +0000 | [diff] [blame] | 422 | } else if (auto S = dyn_cast<ELFYAML::StackSizesSection>(Sec)) { |
George Rimar | e5163eb | 2019-10-01 09:45:59 +0000 | [diff] [blame] | 423 | writeSectionContent(SHeader, *S, CBA); |
| 424 | } else if (auto S = dyn_cast<ELFYAML::HashSection>(Sec)) { |
| 425 | writeSectionContent(SHeader, *S, CBA); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 426 | } else { |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 427 | llvm_unreachable("Unknown section type"); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 428 | } |
George Rimar | 9df825f | 2019-07-02 10:20:12 +0000 | [diff] [blame] | 429 | |
George Rimar | 86cc736 | 2019-09-02 09:47:17 +0000 | [diff] [blame] | 430 | // Override the fields if requested. |
George Rimar | eb41f7f | 2019-07-11 12:59:29 +0000 | [diff] [blame] | 431 | if (Sec) { |
George Rimar | 86cc736 | 2019-09-02 09:47:17 +0000 | [diff] [blame] | 432 | if (Sec->ShName) |
| 433 | SHeader.sh_name = *Sec->ShName; |
George Rimar | eb41f7f | 2019-07-11 12:59:29 +0000 | [diff] [blame] | 434 | if (Sec->ShOffset) |
| 435 | SHeader.sh_offset = *Sec->ShOffset; |
| 436 | if (Sec->ShSize) |
| 437 | SHeader.sh_size = *Sec->ShSize; |
| 438 | } |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 439 | } |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 440 | } |
| 441 | |
George Rimar | 6da44ad | 2019-04-03 14:53:42 +0000 | [diff] [blame] | 442 | static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) { |
| 443 | for (size_t I = 0; I < Symbols.size(); ++I) |
| 444 | if (Symbols[I].Binding.value != ELF::STB_LOCAL) |
| 445 | return I; |
| 446 | return Symbols.size(); |
| 447 | } |
| 448 | |
George Rimar | f302436 | 2019-09-25 11:40:11 +0000 | [diff] [blame] | 449 | static uint64_t writeContent(raw_ostream &OS, |
| 450 | const Optional<yaml::BinaryRef> &Content, |
| 451 | const Optional<llvm::yaml::Hex64> &Size) { |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 452 | size_t ContentSize = 0; |
George Rimar | f302436 | 2019-09-25 11:40:11 +0000 | [diff] [blame] | 453 | if (Content) { |
| 454 | Content->writeAsBinary(OS); |
| 455 | ContentSize = Content->binary_size(); |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 456 | } |
| 457 | |
George Rimar | f302436 | 2019-09-25 11:40:11 +0000 | [diff] [blame] | 458 | if (!Size) |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 459 | return ContentSize; |
| 460 | |
George Rimar | f302436 | 2019-09-25 11:40:11 +0000 | [diff] [blame] | 461 | OS.write_zeros(*Size - ContentSize); |
| 462 | return *Size; |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 465 | template <class ELFT> |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 466 | std::vector<typename ELFT::Sym> |
| 467 | ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols, |
| 468 | const StringTableBuilder &Strtab) { |
George Rimar | 30ea0c4 | 2019-06-20 14:44:48 +0000 | [diff] [blame] | 469 | std::vector<Elf_Sym> Ret; |
| 470 | Ret.resize(Symbols.size() + 1); |
| 471 | |
| 472 | size_t I = 0; |
| 473 | for (const auto &Sym : Symbols) { |
| 474 | Elf_Sym &Symbol = Ret[++I]; |
| 475 | |
| 476 | // If NameIndex, which contains the name offset, is explicitly specified, we |
| 477 | // use it. This is useful for preparing broken objects. Otherwise, we add |
| 478 | // the specified Name to the string table builder to get its offset. |
| 479 | if (Sym.NameIndex) |
| 480 | Symbol.st_name = *Sym.NameIndex; |
| 481 | else if (!Sym.Name.empty()) |
George Rimar | cfc2bcc | 2019-09-25 12:09:30 +0000 | [diff] [blame] | 482 | Symbol.st_name = Strtab.getOffset(ELFYAML::dropUniqueSuffix(Sym.Name)); |
George Rimar | 30ea0c4 | 2019-06-20 14:44:48 +0000 | [diff] [blame] | 483 | |
| 484 | Symbol.setBindingAndType(Sym.Binding, Sym.Type); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 485 | if (!Sym.Section.empty()) |
| 486 | Symbol.st_shndx = toSectionIndex(Sym.Section, "", Sym.Name); |
| 487 | else if (Sym.Index) |
George Rimar | 30ea0c4 | 2019-06-20 14:44:48 +0000 | [diff] [blame] | 488 | Symbol.st_shndx = *Sym.Index; |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 489 | |
George Rimar | 30ea0c4 | 2019-06-20 14:44:48 +0000 | [diff] [blame] | 490 | Symbol.st_value = Sym.Value; |
George Rimar | 4e71702 | 2019-08-30 13:39:22 +0000 | [diff] [blame] | 491 | Symbol.st_other = Sym.Other ? *Sym.Other : 0; |
George Rimar | 30ea0c4 | 2019-06-20 14:44:48 +0000 | [diff] [blame] | 492 | Symbol.st_size = Sym.Size; |
| 493 | } |
| 494 | |
| 495 | return Ret; |
| 496 | } |
| 497 | |
| 498 | template <class ELFT> |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 499 | void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader, |
Dave Lee | 67b4966 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 500 | SymtabType STType, |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 501 | ContiguousBlobAccumulator &CBA, |
| 502 | ELFYAML::Section *YAMLSec) { |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 503 | |
Dave Lee | 67b4966 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 504 | bool IsStatic = STType == SymtabType::Static; |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 505 | const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols; |
| 506 | |
| 507 | ELFYAML::RawContentSection *RawSec = |
| 508 | dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec); |
| 509 | if (RawSec && !Symbols.empty() && (RawSec->Content || RawSec->Size)) { |
| 510 | if (RawSec->Content) |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 511 | reportError("cannot specify both `Content` and " + |
| 512 | (IsStatic ? Twine("`Symbols`") : Twine("`DynamicSymbols`")) + |
| 513 | " for symbol table section '" + RawSec->Name + "'"); |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 514 | if (RawSec->Size) |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 515 | reportError("cannot specify both `Size` and " + |
| 516 | (IsStatic ? Twine("`Symbols`") : Twine("`DynamicSymbols`")) + |
| 517 | " for symbol table section '" + RawSec->Name + "'"); |
| 518 | return; |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | zero(SHeader); |
Dave Lee | 67b4966 | 2017-11-16 18:10:15 +0000 | [diff] [blame] | 522 | SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym"); |
George Rimar | 0aecaba | 2019-06-14 14:25:34 +0000 | [diff] [blame] | 523 | |
| 524 | if (YAMLSec) |
| 525 | SHeader.sh_type = YAMLSec->Type; |
| 526 | else |
| 527 | SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM; |
George Rimar | 379aa18 | 2019-06-10 11:38:06 +0000 | [diff] [blame] | 528 | |
George Rimar | ffb3c72 | 2019-06-11 10:00:51 +0000 | [diff] [blame] | 529 | if (RawSec && !RawSec->Link.empty()) { |
| 530 | // If the Link field is explicitly defined in the document, |
| 531 | // we should use it. |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 532 | SHeader.sh_link = toSectionIndex(RawSec->Link, RawSec->Name); |
George Rimar | ffb3c72 | 2019-06-11 10:00:51 +0000 | [diff] [blame] | 533 | } else { |
| 534 | // When we describe the .dynsym section in the document explicitly, it is |
| 535 | // allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not |
| 536 | // added implicitly and we should be able to leave the Link zeroed if |
| 537 | // .dynstr is not defined. |
| 538 | unsigned Link = 0; |
| 539 | if (IsStatic) |
| 540 | Link = SN2I.get(".strtab"); |
| 541 | else |
| 542 | SN2I.lookup(".dynstr", Link); |
| 543 | SHeader.sh_link = Link; |
| 544 | } |
George Rimar | 379aa18 | 2019-06-10 11:38:06 +0000 | [diff] [blame] | 545 | |
George Rimar | cfa1a62 | 2019-06-14 11:01:14 +0000 | [diff] [blame] | 546 | if (YAMLSec && YAMLSec->Flags) |
| 547 | SHeader.sh_flags = *YAMLSec->Flags; |
| 548 | else if (!IsStatic) |
| 549 | SHeader.sh_flags = ELF::SHF_ALLOC; |
George Rimar | c1da149 | 2019-04-26 12:45:54 +0000 | [diff] [blame] | 550 | |
George Rimar | a7ba1a0 | 2019-03-01 10:18:16 +0000 | [diff] [blame] | 551 | // If the symbol table section is explicitly described in the YAML |
| 552 | // then we should set the fields requested. |
George Rimar | b6e2093 | 2019-06-19 08:57:38 +0000 | [diff] [blame] | 553 | SHeader.sh_info = (RawSec && RawSec->Info) ? (unsigned)(*RawSec->Info) |
| 554 | : findFirstNonGlobal(Symbols) + 1; |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 555 | SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize) |
| 556 | ? (uint64_t)(*YAMLSec->EntSize) |
| 557 | : sizeof(Elf_Sym); |
| 558 | SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8; |
| 559 | SHeader.sh_addr = YAMLSec ? (uint64_t)YAMLSec->Address : 0; |
| 560 | |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 561 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
| 562 | if (RawSec && (RawSec->Content || RawSec->Size)) { |
| 563 | assert(Symbols.empty()); |
George Rimar | f302436 | 2019-09-25 11:40:11 +0000 | [diff] [blame] | 564 | SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size); |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 565 | return; |
George Rimar | 6947acd | 2019-02-19 12:15:04 +0000 | [diff] [blame] | 566 | } |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 567 | |
George Rimar | 30ea0c4 | 2019-06-20 14:44:48 +0000 | [diff] [blame] | 568 | std::vector<Elf_Sym> Syms = |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 569 | toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr); |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 570 | writeArrayData(OS, makeArrayRef(Syms)); |
| 571 | SHeader.sh_size = arrayDataSize(makeArrayRef(Syms)); |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | template <class ELFT> |
| 575 | void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, |
| 576 | StringTableBuilder &STB, |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 577 | ContiguousBlobAccumulator &CBA, |
| 578 | ELFYAML::Section *YAMLSec) { |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 579 | zero(SHeader); |
Hans Wennborg | 59f0cba | 2014-04-30 19:38:09 +0000 | [diff] [blame] | 580 | SHeader.sh_name = DotShStrtab.getOffset(Name); |
George Rimar | 0aecaba | 2019-06-14 14:25:34 +0000 | [diff] [blame] | 581 | SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB; |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 582 | SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1; |
| 583 | |
| 584 | ELFYAML::RawContentSection *RawSec = |
| 585 | dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec); |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 586 | |
| 587 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
| 588 | if (RawSec && (RawSec->Content || RawSec->Size)) { |
George Rimar | f302436 | 2019-09-25 11:40:11 +0000 | [diff] [blame] | 589 | SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size); |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 590 | } else { |
George Rimar | 1e41007 | 2019-06-10 12:43:18 +0000 | [diff] [blame] | 591 | STB.write(OS); |
George Rimar | 66296dc | 2019-06-05 13:16:53 +0000 | [diff] [blame] | 592 | SHeader.sh_size = STB.getSize(); |
| 593 | } |
| 594 | |
| 595 | if (YAMLSec && YAMLSec->EntSize) |
| 596 | SHeader.sh_entsize = *YAMLSec->EntSize; |
George Rimar | 6947acd | 2019-02-19 12:15:04 +0000 | [diff] [blame] | 597 | |
George Rimar | b6e2093 | 2019-06-19 08:57:38 +0000 | [diff] [blame] | 598 | if (RawSec && RawSec->Info) |
| 599 | SHeader.sh_info = *RawSec->Info; |
| 600 | |
George Rimar | cfa1a62 | 2019-06-14 11:01:14 +0000 | [diff] [blame] | 601 | if (YAMLSec && YAMLSec->Flags) |
| 602 | SHeader.sh_flags = *YAMLSec->Flags; |
| 603 | else if (Name == ".dynstr") |
| 604 | SHeader.sh_flags = ELF::SHF_ALLOC; |
| 605 | |
George Rimar | 43f62ff | 2019-06-14 11:13:32 +0000 | [diff] [blame] | 606 | // If the section is explicitly described in the YAML |
George Rimar | 6947acd | 2019-02-19 12:15:04 +0000 | [diff] [blame] | 607 | // then we want to use its section address. |
George Rimar | 43f62ff | 2019-06-14 11:13:32 +0000 | [diff] [blame] | 608 | if (YAMLSec) |
George Rimar | cfa1a62 | 2019-06-14 11:01:14 +0000 | [diff] [blame] | 609 | SHeader.sh_addr = YAMLSec->Address; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 610 | } |
| 611 | |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 612 | template <class ELFT> void ELFState<ELFT>::reportError(const Twine &Msg) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 613 | ErrHandler(Msg); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 614 | HasError = true; |
| 615 | } |
| 616 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 617 | template <class ELFT> |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 618 | void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders, |
| 619 | std::vector<Elf_Shdr> &SHeaders) { |
| 620 | uint32_t PhdrIdx = 0; |
| 621 | for (auto &YamlPhdr : Doc.ProgramHeaders) { |
George Rimar | f5345a3 | 2019-05-01 09:45:55 +0000 | [diff] [blame] | 622 | Elf_Phdr &PHeader = PHeaders[PhdrIdx++]; |
| 623 | |
Puyan Lotfi | a10f016 | 2019-05-11 17:03:36 +0000 | [diff] [blame] | 624 | std::vector<Elf_Shdr *> Sections; |
| 625 | for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) { |
| 626 | unsigned Index; |
| 627 | if (!SN2I.lookup(SecName.Section, Index)) { |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 628 | reportError("unknown section referenced: '" + SecName.Section + |
| 629 | "' by program header"); |
| 630 | continue; |
George Rimar | f5345a3 | 2019-05-01 09:45:55 +0000 | [diff] [blame] | 631 | } |
| 632 | Sections.push_back(&SHeaders[Index]); |
| 633 | } |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 634 | |
James Henderson | b10f48b | 2019-03-15 10:35:27 +0000 | [diff] [blame] | 635 | if (YamlPhdr.Offset) { |
| 636 | PHeader.p_offset = *YamlPhdr.Offset; |
| 637 | } else { |
| 638 | if (YamlPhdr.Sections.size()) |
| 639 | PHeader.p_offset = UINT32_MAX; |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 640 | else |
James Henderson | b10f48b | 2019-03-15 10:35:27 +0000 | [diff] [blame] | 641 | PHeader.p_offset = 0; |
| 642 | |
| 643 | // Find the minimum offset for the program header. |
George Rimar | f5345a3 | 2019-05-01 09:45:55 +0000 | [diff] [blame] | 644 | for (Elf_Shdr *SHeader : Sections) |
| 645 | PHeader.p_offset = std::min(PHeader.p_offset, SHeader->sh_offset); |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Fangrui Song | c28f3e6 | 2019-09-09 16:45:17 +0000 | [diff] [blame] | 648 | // Find the maximum offset of the end of a section in order to set p_filesz |
| 649 | // and p_memsz. When setting p_filesz, trailing SHT_NOBITS sections are not |
| 650 | // counted. |
| 651 | uint64_t FileOffset = PHeader.p_offset, MemOffset = PHeader.p_offset; |
| 652 | for (Elf_Shdr *SHeader : Sections) { |
| 653 | uint64_t End = SHeader->sh_offset + SHeader->sh_size; |
| 654 | MemOffset = std::max(MemOffset, End); |
| 655 | |
| 656 | if (SHeader->sh_type != llvm::ELF::SHT_NOBITS) |
| 657 | FileOffset = std::max(FileOffset, End); |
James Henderson | b10f48b | 2019-03-15 10:35:27 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Fangrui Song | c28f3e6 | 2019-09-09 16:45:17 +0000 | [diff] [blame] | 660 | // Set the file size and the memory size if not set explicitly. |
| 661 | PHeader.p_filesz = YamlPhdr.FileSize ? uint64_t(*YamlPhdr.FileSize) |
| 662 | : FileOffset - PHeader.p_offset; |
| 663 | PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize) |
| 664 | : MemOffset - PHeader.p_offset; |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 665 | |
Jake Ehrlich | 03aeeb09 | 2017-11-01 23:14:48 +0000 | [diff] [blame] | 666 | if (YamlPhdr.Align) { |
| 667 | PHeader.p_align = *YamlPhdr.Align; |
| 668 | } else { |
Fangrui Song | 1da4f47 | 2019-09-10 09:16:34 +0000 | [diff] [blame] | 669 | // Set the alignment of the segment to be the maximum alignment of the |
| 670 | // sections so that by default the segment has a valid and sensible |
| 671 | // alignment. |
Jake Ehrlich | 03aeeb09 | 2017-11-01 23:14:48 +0000 | [diff] [blame] | 672 | PHeader.p_align = 1; |
George Rimar | f5345a3 | 2019-05-01 09:45:55 +0000 | [diff] [blame] | 673 | for (Elf_Shdr *SHeader : Sections) |
Fangrui Song | 1da4f47 | 2019-09-10 09:16:34 +0000 | [diff] [blame] | 674 | PHeader.p_align = std::max(PHeader.p_align, SHeader->sh_addralign); |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | template <class ELFT> |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 680 | void ELFState<ELFT>::writeSectionContent( |
George Rimar | b49e192 | 2019-04-24 13:02:15 +0000 | [diff] [blame] | 681 | Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, |
| 682 | ContiguousBlobAccumulator &CBA) { |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 683 | raw_ostream &OS = |
| 684 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
George Rimar | f302436 | 2019-09-25 11:40:11 +0000 | [diff] [blame] | 685 | SHeader.sh_size = writeContent(OS, Section.Content, Section.Size); |
George Rimar | b49e192 | 2019-04-24 13:02:15 +0000 | [diff] [blame] | 686 | |
George Rimar | 65a6828 | 2018-08-07 08:11:38 +0000 | [diff] [blame] | 687 | if (Section.EntSize) |
| 688 | SHeader.sh_entsize = *Section.EntSize; |
| 689 | else if (Section.Type == llvm::ELF::SHT_RELR) |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 690 | SHeader.sh_entsize = sizeof(Elf_Relr); |
| 691 | else |
| 692 | SHeader.sh_entsize = 0; |
George Rimar | b6e2093 | 2019-06-19 08:57:38 +0000 | [diff] [blame] | 693 | |
| 694 | if (Section.Info) |
| 695 | SHeader.sh_info = *Section.Info; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Simon Atanasyan | 5d19c67 | 2015-01-25 13:29:25 +0000 | [diff] [blame] | 698 | static bool isMips64EL(const ELFYAML::Object &Doc) { |
| 699 | return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) && |
| 700 | Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) && |
| 701 | Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); |
| 702 | } |
| 703 | |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 704 | template <class ELFT> |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 705 | void ELFState<ELFT>::writeSectionContent( |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 706 | Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section, |
| 707 | ContiguousBlobAccumulator &CBA) { |
Simon Atanasyan | 8eb9d1b | 2015-05-08 07:05:04 +0000 | [diff] [blame] | 708 | assert((Section.Type == llvm::ELF::SHT_REL || |
| 709 | Section.Type == llvm::ELF::SHT_RELA) && |
| 710 | "Section type is not SHT_REL nor SHT_RELA"); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 711 | |
| 712 | bool IsRela = Section.Type == llvm::ELF::SHT_RELA; |
| 713 | SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 714 | SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size(); |
| 715 | |
George Rimar | da1b3ab | 2019-04-26 12:15:32 +0000 | [diff] [blame] | 716 | // For relocation section set link to .symtab by default. |
| 717 | if (Section.Link.empty()) |
George Rimar | d71017b | 2019-06-10 09:57:29 +0000 | [diff] [blame] | 718 | SHeader.sh_link = SN2I.get(".symtab"); |
George Rimar | da1b3ab | 2019-04-26 12:15:32 +0000 | [diff] [blame] | 719 | |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 720 | if (!Section.RelocatableSec.empty()) |
| 721 | SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name); |
George Rimar | da1b3ab | 2019-04-26 12:15:32 +0000 | [diff] [blame] | 722 | |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 723 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 724 | for (const auto &Rel : Section.Relocations) { |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 725 | unsigned SymIdx = Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name, |
| 726 | Section.Link == ".dynsym") |
| 727 | : 0; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 728 | if (IsRela) { |
| 729 | Elf_Rela REntry; |
| 730 | zero(REntry); |
| 731 | REntry.r_offset = Rel.Offset; |
| 732 | REntry.r_addend = Rel.Addend; |
Simon Atanasyan | 5d19c67 | 2015-01-25 13:29:25 +0000 | [diff] [blame] | 733 | REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc)); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 734 | OS.write((const char *)&REntry, sizeof(REntry)); |
| 735 | } else { |
| 736 | Elf_Rel REntry; |
| 737 | zero(REntry); |
| 738 | REntry.r_offset = Rel.Offset; |
Simon Atanasyan | 5d19c67 | 2015-01-25 13:29:25 +0000 | [diff] [blame] | 739 | REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc)); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 740 | OS.write((const char *)&REntry, sizeof(REntry)); |
| 741 | } |
| 742 | } |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 745 | template <class ELFT> |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 746 | void ELFState<ELFT>::writeSectionContent( |
George Rimar | d396305 | 2019-08-08 09:49:05 +0000 | [diff] [blame] | 747 | Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx, |
| 748 | ContiguousBlobAccumulator &CBA) { |
| 749 | raw_ostream &OS = |
| 750 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
| 751 | |
| 752 | for (uint32_t E : Shndx.Entries) |
| 753 | support::endian::write<uint32_t>(OS, E, ELFT::TargetEndianness); |
| 754 | |
| 755 | SHeader.sh_entsize = Shndx.EntSize ? (uint64_t)*Shndx.EntSize : 4; |
| 756 | SHeader.sh_size = Shndx.Entries.size() * SHeader.sh_entsize; |
George Rimar | d396305 | 2019-08-08 09:49:05 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | template <class ELFT> |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 760 | void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 761 | const ELFYAML::Group &Section, |
| 762 | ContiguousBlobAccumulator &CBA) { |
Simon Atanasyan | 8eb9d1b | 2015-05-08 07:05:04 +0000 | [diff] [blame] | 763 | assert(Section.Type == llvm::ELF::SHT_GROUP && |
| 764 | "Section type is not SHT_GROUP"); |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 765 | |
George Rimar | d063c7d | 2019-02-20 13:58:43 +0000 | [diff] [blame] | 766 | SHeader.sh_entsize = 4; |
George Rimar | 0974688 | 2019-05-07 12:10:51 +0000 | [diff] [blame] | 767 | SHeader.sh_size = SHeader.sh_entsize * Section.Members.size(); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 768 | SHeader.sh_info = |
| 769 | toSymbolIndex(Section.Signature, Section.Name, /*IsDynamic=*/false); |
George Rimar | da1b3ab | 2019-04-26 12:15:32 +0000 | [diff] [blame] | 770 | |
George Rimar | d063c7d | 2019-02-20 13:58:43 +0000 | [diff] [blame] | 771 | raw_ostream &OS = |
| 772 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 773 | |
George Rimar | fb7780a | 2019-04-26 12:20:51 +0000 | [diff] [blame] | 774 | for (const ELFYAML::SectionOrType &Member : Section.Members) { |
| 775 | unsigned int SectionIndex = 0; |
| 776 | if (Member.sectionNameOrType == "GRP_COMDAT") |
| 777 | SectionIndex = llvm::ELF::GRP_COMDAT; |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 778 | else |
| 779 | SectionIndex = toSectionIndex(Member.sectionNameOrType, Section.Name); |
George Rimar | fb7780a | 2019-04-26 12:20:51 +0000 | [diff] [blame] | 780 | support::endian::write<uint32_t>(OS, SectionIndex, ELFT::TargetEndianness); |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 781 | } |
Shankar Easwaran | 6fbbe20 | 2015-02-21 04:28:26 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Simon Atanasyan | 04d9e65 | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 784 | template <class ELFT> |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 785 | void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
George Rimar | 646af08 | 2019-02-19 15:29:07 +0000 | [diff] [blame] | 786 | const ELFYAML::SymverSection &Section, |
| 787 | ContiguousBlobAccumulator &CBA) { |
George Rimar | 646af08 | 2019-02-19 15:29:07 +0000 | [diff] [blame] | 788 | raw_ostream &OS = |
| 789 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
George Rimar | dac37fb | 2019-02-20 14:01:02 +0000 | [diff] [blame] | 790 | for (uint16_t Version : Section.Entries) |
| 791 | support::endian::write<uint16_t>(OS, Version, ELFT::TargetEndianness); |
George Rimar | 646af08 | 2019-02-19 15:29:07 +0000 | [diff] [blame] | 792 | |
George Rimar | 149aa2f | 2019-08-05 13:54:35 +0000 | [diff] [blame] | 793 | SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2; |
George Rimar | d063c7d | 2019-02-20 13:58:43 +0000 | [diff] [blame] | 794 | SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize; |
George Rimar | 646af08 | 2019-02-19 15:29:07 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | template <class ELFT> |
George Rimar | 1a219aa | 2019-09-24 14:22:37 +0000 | [diff] [blame] | 798 | void ELFState<ELFT>::writeSectionContent( |
| 799 | Elf_Shdr &SHeader, const ELFYAML::StackSizesSection &Section, |
| 800 | ContiguousBlobAccumulator &CBA) { |
| 801 | using uintX_t = typename ELFT::uint; |
| 802 | raw_ostream &OS = |
| 803 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
| 804 | |
George Rimar | f302436 | 2019-09-25 11:40:11 +0000 | [diff] [blame] | 805 | if (Section.Content || Section.Size) { |
| 806 | SHeader.sh_size = writeContent(OS, Section.Content, Section.Size); |
George Rimar | 1a219aa | 2019-09-24 14:22:37 +0000 | [diff] [blame] | 807 | return; |
| 808 | } |
| 809 | |
| 810 | for (const ELFYAML::StackSizeEntry &E : *Section.Entries) { |
| 811 | support::endian::write<uintX_t>(OS, E.Address, ELFT::TargetEndianness); |
| 812 | SHeader.sh_size += sizeof(uintX_t) + encodeULEB128(E.Size, OS); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | template <class ELFT> |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 817 | void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
George Rimar | e5163eb | 2019-10-01 09:45:59 +0000 | [diff] [blame] | 818 | const ELFYAML::HashSection &Section, |
| 819 | ContiguousBlobAccumulator &CBA) { |
| 820 | raw_ostream &OS = |
| 821 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
| 822 | |
| 823 | unsigned Link = 0; |
George Rimar | 0210a1a | 2019-10-01 09:54:40 +0000 | [diff] [blame] | 824 | if (Section.Link.empty() && SN2I.lookup(".dynsym", Link)) |
George Rimar | e5163eb | 2019-10-01 09:45:59 +0000 | [diff] [blame] | 825 | SHeader.sh_link = Link; |
| 826 | |
George Rimar | 6fa696f | 2019-10-02 13:52:37 +0000 | [diff] [blame] | 827 | if (Section.Content || Section.Size) { |
| 828 | SHeader.sh_size = writeContent(OS, Section.Content, Section.Size); |
George Rimar | e5163eb | 2019-10-01 09:45:59 +0000 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | |
| 832 | support::endian::write<uint32_t>(OS, Section.Bucket->size(), |
| 833 | ELFT::TargetEndianness); |
| 834 | support::endian::write<uint32_t>(OS, Section.Chain->size(), |
| 835 | ELFT::TargetEndianness); |
| 836 | for (uint32_t Val : *Section.Bucket) |
| 837 | support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness); |
| 838 | for (uint32_t Val : *Section.Chain) |
| 839 | support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness); |
| 840 | |
| 841 | SHeader.sh_size = (2 + Section.Bucket->size() + Section.Chain->size()) * 4; |
| 842 | } |
| 843 | |
| 844 | template <class ELFT> |
| 845 | void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
George Rimar | 623ae72 | 2019-02-21 12:21:43 +0000 | [diff] [blame] | 846 | const ELFYAML::VerdefSection &Section, |
| 847 | ContiguousBlobAccumulator &CBA) { |
| 848 | typedef typename ELFT::Verdef Elf_Verdef; |
| 849 | typedef typename ELFT::Verdaux Elf_Verdaux; |
| 850 | raw_ostream &OS = |
| 851 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
| 852 | |
| 853 | uint64_t AuxCnt = 0; |
| 854 | for (size_t I = 0; I < Section.Entries.size(); ++I) { |
| 855 | const ELFYAML::VerdefEntry &E = Section.Entries[I]; |
| 856 | |
| 857 | Elf_Verdef VerDef; |
| 858 | VerDef.vd_version = E.Version; |
| 859 | VerDef.vd_flags = E.Flags; |
| 860 | VerDef.vd_ndx = E.VersionNdx; |
| 861 | VerDef.vd_hash = E.Hash; |
| 862 | VerDef.vd_aux = sizeof(Elf_Verdef); |
| 863 | VerDef.vd_cnt = E.VerNames.size(); |
| 864 | if (I == Section.Entries.size() - 1) |
| 865 | VerDef.vd_next = 0; |
| 866 | else |
| 867 | VerDef.vd_next = |
| 868 | sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux); |
| 869 | OS.write((const char *)&VerDef, sizeof(Elf_Verdef)); |
| 870 | |
| 871 | for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) { |
| 872 | Elf_Verdaux VernAux; |
| 873 | VernAux.vda_name = DotDynstr.getOffset(E.VerNames[J]); |
| 874 | if (J == E.VerNames.size() - 1) |
| 875 | VernAux.vda_next = 0; |
| 876 | else |
| 877 | VernAux.vda_next = sizeof(Elf_Verdaux); |
| 878 | OS.write((const char *)&VernAux, sizeof(Elf_Verdaux)); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | SHeader.sh_size = Section.Entries.size() * sizeof(Elf_Verdef) + |
| 883 | AuxCnt * sizeof(Elf_Verdaux); |
| 884 | SHeader.sh_info = Section.Info; |
George Rimar | 623ae72 | 2019-02-21 12:21:43 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | template <class ELFT> |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 888 | void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 889 | const ELFYAML::VerneedSection &Section, |
| 890 | ContiguousBlobAccumulator &CBA) { |
George Rimar | da1b3ab | 2019-04-26 12:15:32 +0000 | [diff] [blame] | 891 | typedef typename ELFT::Verneed Elf_Verneed; |
| 892 | typedef typename ELFT::Vernaux Elf_Vernaux; |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 893 | |
George Rimar | da1b3ab | 2019-04-26 12:15:32 +0000 | [diff] [blame] | 894 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 895 | |
George Rimar | da1b3ab | 2019-04-26 12:15:32 +0000 | [diff] [blame] | 896 | uint64_t AuxCnt = 0; |
| 897 | for (size_t I = 0; I < Section.VerneedV.size(); ++I) { |
| 898 | const ELFYAML::VerneedEntry &VE = Section.VerneedV[I]; |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 899 | |
George Rimar | da1b3ab | 2019-04-26 12:15:32 +0000 | [diff] [blame] | 900 | Elf_Verneed VerNeed; |
| 901 | VerNeed.vn_version = VE.Version; |
| 902 | VerNeed.vn_file = DotDynstr.getOffset(VE.File); |
| 903 | if (I == Section.VerneedV.size() - 1) |
| 904 | VerNeed.vn_next = 0; |
| 905 | else |
| 906 | VerNeed.vn_next = |
| 907 | sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux); |
| 908 | VerNeed.vn_cnt = VE.AuxV.size(); |
| 909 | VerNeed.vn_aux = sizeof(Elf_Verneed); |
| 910 | OS.write((const char *)&VerNeed, sizeof(Elf_Verneed)); |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 911 | |
George Rimar | da1b3ab | 2019-04-26 12:15:32 +0000 | [diff] [blame] | 912 | for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) { |
| 913 | const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J]; |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 914 | |
George Rimar | da1b3ab | 2019-04-26 12:15:32 +0000 | [diff] [blame] | 915 | Elf_Vernaux VernAux; |
| 916 | VernAux.vna_hash = VAuxE.Hash; |
| 917 | VernAux.vna_flags = VAuxE.Flags; |
| 918 | VernAux.vna_other = VAuxE.Other; |
| 919 | VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name); |
| 920 | if (J == VE.AuxV.size() - 1) |
| 921 | VernAux.vna_next = 0; |
| 922 | else |
| 923 | VernAux.vna_next = sizeof(Elf_Vernaux); |
| 924 | OS.write((const char *)&VernAux, sizeof(Elf_Vernaux)); |
| 925 | } |
| 926 | } |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 927 | |
George Rimar | da1b3ab | 2019-04-26 12:15:32 +0000 | [diff] [blame] | 928 | SHeader.sh_size = Section.VerneedV.size() * sizeof(Elf_Verneed) + |
| 929 | AuxCnt * sizeof(Elf_Vernaux); |
| 930 | SHeader.sh_info = Section.Info; |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | template <class ELFT> |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 934 | void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
Simon Atanasyan | 04d9e65 | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 935 | const ELFYAML::MipsABIFlags &Section, |
| 936 | ContiguousBlobAccumulator &CBA) { |
Simon Atanasyan | 8eb9d1b | 2015-05-08 07:05:04 +0000 | [diff] [blame] | 937 | assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS && |
| 938 | "Section type is not SHT_MIPS_ABIFLAGS"); |
Simon Atanasyan | 04d9e65 | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 939 | |
| 940 | object::Elf_Mips_ABIFlags<ELFT> Flags; |
| 941 | zero(Flags); |
| 942 | SHeader.sh_entsize = sizeof(Flags); |
| 943 | SHeader.sh_size = SHeader.sh_entsize; |
| 944 | |
Simon Atanasyan | 22c4c9e | 2015-07-08 10:12:40 +0000 | [diff] [blame] | 945 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
Simon Atanasyan | 04d9e65 | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 946 | Flags.version = Section.Version; |
| 947 | Flags.isa_level = Section.ISALevel; |
| 948 | Flags.isa_rev = Section.ISARevision; |
| 949 | Flags.gpr_size = Section.GPRSize; |
| 950 | Flags.cpr1_size = Section.CPR1Size; |
| 951 | Flags.cpr2_size = Section.CPR2Size; |
| 952 | Flags.fp_abi = Section.FpABI; |
| 953 | Flags.isa_ext = Section.ISAExtension; |
| 954 | Flags.ases = Section.ASEs; |
| 955 | Flags.flags1 = Section.Flags1; |
| 956 | Flags.flags2 = Section.Flags2; |
| 957 | OS.write((const char *)&Flags, sizeof(Flags)); |
Simon Atanasyan | 04d9e65 | 2015-05-07 15:40:48 +0000 | [diff] [blame] | 958 | } |
| 959 | |
George Rimar | 0e7ed91 | 2019-02-09 11:34:28 +0000 | [diff] [blame] | 960 | template <class ELFT> |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 961 | void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
George Rimar | 0e7ed91 | 2019-02-09 11:34:28 +0000 | [diff] [blame] | 962 | const ELFYAML::DynamicSection &Section, |
| 963 | ContiguousBlobAccumulator &CBA) { |
George Rimar | d063c7d | 2019-02-20 13:58:43 +0000 | [diff] [blame] | 964 | typedef typename ELFT::uint uintX_t; |
| 965 | |
George Rimar | 0e7ed91 | 2019-02-09 11:34:28 +0000 | [diff] [blame] | 966 | assert(Section.Type == llvm::ELF::SHT_DYNAMIC && |
| 967 | "Section type is not SHT_DYNAMIC"); |
| 968 | |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 969 | if (!Section.Entries.empty() && Section.Content) |
| 970 | reportError("cannot specify both raw content and explicit entries " |
| 971 | "for dynamic section '" + |
| 972 | Section.Name + "'"); |
James Henderson | fd99780 | 2019-02-25 11:02:24 +0000 | [diff] [blame] | 973 | |
| 974 | if (Section.Content) |
| 975 | SHeader.sh_size = Section.Content->binary_size(); |
| 976 | else |
| 977 | SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size(); |
George Rimar | 0e7ed91 | 2019-02-09 11:34:28 +0000 | [diff] [blame] | 978 | if (Section.EntSize) |
| 979 | SHeader.sh_entsize = *Section.EntSize; |
| 980 | else |
| 981 | SHeader.sh_entsize = sizeof(Elf_Dyn); |
| 982 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 983 | raw_ostream &OS = |
| 984 | CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); |
George Rimar | 0e7ed91 | 2019-02-09 11:34:28 +0000 | [diff] [blame] | 985 | for (const ELFYAML::DynamicEntry &DE : Section.Entries) { |
George Rimar | d063c7d | 2019-02-20 13:58:43 +0000 | [diff] [blame] | 986 | support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness); |
| 987 | support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness); |
George Rimar | 0e7ed91 | 2019-02-09 11:34:28 +0000 | [diff] [blame] | 988 | } |
James Henderson | fd99780 | 2019-02-25 11:02:24 +0000 | [diff] [blame] | 989 | if (Section.Content) |
| 990 | Section.Content->writeAsBinary(OS); |
George Rimar | 0e7ed91 | 2019-02-09 11:34:28 +0000 | [diff] [blame] | 991 | } |
| 992 | |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 993 | template <class ELFT> void ELFState<ELFT>::buildSectionIndex() { |
George Rimar | 1480229 | 2019-07-25 10:19:23 +0000 | [diff] [blame] | 994 | for (unsigned I = 0, E = Doc.Sections.size(); I != E; ++I) { |
George Rimar | ab658f4 | 2019-07-23 07:38:44 +0000 | [diff] [blame] | 995 | StringRef Name = Doc.Sections[I]->Name; |
George Rimar | 1480229 | 2019-07-25 10:19:23 +0000 | [diff] [blame] | 996 | if (Name.empty()) |
| 997 | continue; |
| 998 | |
George Rimar | cfc2bcc | 2019-09-25 12:09:30 +0000 | [diff] [blame] | 999 | DotShStrtab.add(ELFYAML::dropUniqueSuffix(Name)); |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 1000 | if (!SN2I.addName(Name, I)) |
| 1001 | reportError("repeated section name: '" + Name + |
| 1002 | "' at YAML section number " + Twine(I)); |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 1003 | } |
Dave Lee | 17307d9 | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 1004 | |
Dave Lee | 17307d9 | 2017-11-09 14:53:43 +0000 | [diff] [blame] | 1005 | DotShStrtab.finalize(); |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 1008 | template <class ELFT> void ELFState<ELFT>::buildSymbolIndexes() { |
| 1009 | auto Build = [this](ArrayRef<ELFYAML::Symbol> V, NameToIdxMap &Map) { |
| 1010 | for (size_t I = 0, S = V.size(); I < S; ++I) { |
| 1011 | const ELFYAML::Symbol &Sym = V[I]; |
| 1012 | if (!Sym.Name.empty() && !Map.addName(Sym.Name, I + 1)) |
| 1013 | reportError("repeated symbol name: '" + Sym.Name + "'"); |
| 1014 | } |
| 1015 | }; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 1016 | |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 1017 | Build(Doc.Symbols, SymN2I); |
| 1018 | Build(Doc.DynamicSymbols, DynSymN2I); |
George Rimar | 9120844 | 2019-08-22 12:39:56 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 1021 | template <class ELFT> void ELFState<ELFT>::finalizeStrings() { |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 1022 | // Add the regular symbol names to .strtab section. |
George Rimar | 6da44ad | 2019-04-03 14:53:42 +0000 | [diff] [blame] | 1023 | for (const ELFYAML::Symbol &Sym : Doc.Symbols) |
George Rimar | cfc2bcc | 2019-09-25 12:09:30 +0000 | [diff] [blame] | 1024 | DotStrtab.add(ELFYAML::dropUniqueSuffix(Sym.Name)); |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 1025 | DotStrtab.finalize(); |
| 1026 | |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 1027 | // Add the dynamic symbol names to .dynstr section. |
George Rimar | 6da44ad | 2019-04-03 14:53:42 +0000 | [diff] [blame] | 1028 | for (const ELFYAML::Symbol &Sym : Doc.DynamicSymbols) |
George Rimar | cfc2bcc | 2019-09-25 12:09:30 +0000 | [diff] [blame] | 1029 | DotDynstr.add(ELFYAML::dropUniqueSuffix(Sym.Name)); |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 1030 | |
George Rimar | 623ae72 | 2019-02-21 12:21:43 +0000 | [diff] [blame] | 1031 | // SHT_GNU_verdef and SHT_GNU_verneed sections might also |
| 1032 | // add strings to .dynstr section. |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 1033 | for (const std::unique_ptr<ELFYAML::Section> &Sec : Doc.Sections) { |
George Rimar | 623ae72 | 2019-02-21 12:21:43 +0000 | [diff] [blame] | 1034 | if (auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec.get())) { |
| 1035 | for (const ELFYAML::VerneedEntry &VE : VerNeed->VerneedV) { |
| 1036 | DotDynstr.add(VE.File); |
| 1037 | for (const ELFYAML::VernauxEntry &Aux : VE.AuxV) |
| 1038 | DotDynstr.add(Aux.Name); |
| 1039 | } |
| 1040 | } else if (auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec.get())) { |
| 1041 | for (const ELFYAML::VerdefEntry &E : VerDef->Entries) |
| 1042 | for (StringRef Name : E.VerNames) |
| 1043 | DotDynstr.add(Name); |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | DotDynstr.finalize(); |
| 1048 | } |
| 1049 | |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 1050 | template <class ELFT> |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 1051 | bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc, |
| 1052 | yaml::ErrorHandler EH) { |
| 1053 | ELFState<ELFT> State(Doc, EH); |
George Rimar | 0621b79 | 2019-02-19 14:53:48 +0000 | [diff] [blame] | 1054 | |
| 1055 | // Finalize .strtab and .dynstr sections. We do that early because want to |
| 1056 | // finalize the string table builders before writing the content of the |
| 1057 | // sections that might want to use them. |
| 1058 | State.finalizeStrings(); |
| 1059 | |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 1060 | State.buildSectionIndex(); |
| 1061 | State.buildSymbolIndexes(); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame] | 1062 | |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 1063 | std::vector<Elf_Phdr> PHeaders; |
| 1064 | State.initProgramHeaders(PHeaders); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 1065 | |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 1066 | // XXX: This offset is tightly coupled with the order that we write |
| 1067 | // things to `OS`. |
Fangrui Song | c3bc697 | 2019-09-05 14:25:57 +0000 | [diff] [blame] | 1068 | const size_t SectionContentBeginOffset = |
| 1069 | sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size(); |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 1070 | ContiguousBlobAccumulator CBA(SectionContentBeginOffset); |
Sean Silva | c1c290b | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 1071 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 1072 | std::vector<Elf_Shdr> SHeaders; |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 1073 | State.initSectionHeaders(SHeaders, CBA); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 1074 | |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 1075 | // Now we can decide segment offsets |
| 1076 | State.setProgramHeaderLayout(PHeaders, SHeaders); |
| 1077 | |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 1078 | if (State.HasError) |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 1079 | return false; |
George Rimar | 3212ecf | 2019-09-09 09:43:03 +0000 | [diff] [blame] | 1080 | |
Fangrui Song | c3bc697 | 2019-09-05 14:25:57 +0000 | [diff] [blame] | 1081 | State.writeELFHeader(CBA, OS); |
Petr Hosek | eb04da3 | 2017-07-19 20:38:46 +0000 | [diff] [blame] | 1082 | writeArrayData(OS, makeArrayRef(PHeaders)); |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 1083 | CBA.writeBlobToStream(OS); |
Fangrui Song | c3bc697 | 2019-09-05 14:25:57 +0000 | [diff] [blame] | 1084 | writeArrayData(OS, makeArrayRef(SHeaders)); |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 1085 | return true; |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 1088 | namespace llvm { |
| 1089 | namespace yaml { |
| 1090 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 1091 | bool yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH) { |
George Rimar | bc4d3c4 | 2019-04-29 12:25:01 +0000 | [diff] [blame] | 1092 | bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); |
| 1093 | bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64); |
| 1094 | if (Is64Bit) { |
| 1095 | if (IsLE) |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 1096 | return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH); |
| 1097 | return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 1098 | } |
George Rimar | bc4d3c4 | 2019-04-29 12:25:01 +0000 | [diff] [blame] | 1099 | if (IsLE) |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 1100 | return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH); |
| 1101 | return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 1102 | } |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 1103 | |
| 1104 | } // namespace yaml |
| 1105 | } // namespace llvm |