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