blob: 4584e3d73bd7610adb264b369003c60f8f67aef9 [file] [log] [blame]
Sean Silvaf99309c2013-06-10 23:44:15 +00001//===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Silvaf99309c2013-06-10 23:44:15 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// The ELF component of yaml2obj.
Sean Silvaf99309c2013-06-10 23:44:15 +000011///
12//===----------------------------------------------------------------------===//
13
Will Dietz0b48c732013-10-12 21:29:16 +000014#include "llvm/ADT/ArrayRef.h"
Alex Brachetc22d9662019-08-07 02:44:49 +000015#include "llvm/ADT/StringSet.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000016#include "llvm/BinaryFormat/ELF.h"
Rafael Espindola97de4742014-07-03 02:01:39 +000017#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000018#include "llvm/Object/ELFObjectFile.h"
Rafael Espindolaebd91932016-03-01 19:15:06 +000019#include "llvm/ObjectYAML/ELFYAML.h"
Alex Brachetc22d9662019-08-07 02:44:49 +000020#include "llvm/ObjectYAML/yaml2obj.h"
George Rimard063c7d2019-02-20 13:58:43 +000021#include "llvm/Support/EndianStream.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000022#include "llvm/Support/MemoryBuffer.h"
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +000023#include "llvm/Support/WithColor.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000024#include "llvm/Support/YAMLTraits.h"
25#include "llvm/Support/raw_ostream.h"
26
27using namespace llvm;
28
Sean Silva46dffff2013-06-13 22:20:01 +000029// 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 Silva2a74f702013-06-15 00:31:46 +000032namespace {
Sean Silva46dffff2013-06-13 22:20:01 +000033class ContiguousBlobAccumulator {
34 const uint64_t InitialOffset;
Sean Silvabd3bc692013-06-20 19:11:41 +000035 SmallVector<char, 128> Buf;
Sean Silva46dffff2013-06-13 22:20:01 +000036 raw_svector_ostream OS;
37
Sean Silvad93323f2013-06-22 00:47:43 +000038 /// \returns The new offset.
39 uint64_t padToAlignment(unsigned Align) {
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +000040 if (Align == 0)
41 Align = 1;
Sean Silvad93323f2013-06-22 00:47:43 +000042 uint64_t CurrentOffset = InitialOffset + OS.tell();
Rui Ueyamada00f2f2016-01-14 21:06:47 +000043 uint64_t AlignedOffset = alignTo(CurrentOffset, Align);
George Rimarbd8bfd32019-04-29 12:05:53 +000044 OS.write_zeros(AlignedOffset - CurrentOffset);
Sean Silvad93323f2013-06-22 00:47:43 +000045 return AlignedOffset; // == CurrentOffset;
46 }
47
Sean Silva46dffff2013-06-13 22:20:01 +000048public:
Sean Silvabd3bc692013-06-20 19:11:41 +000049 ContiguousBlobAccumulator(uint64_t InitialOffset_)
50 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
Sean Silvad93323f2013-06-22 00:47:43 +000051 template <class Integer>
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +000052 raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) {
Sean Silvad93323f2013-06-22 00:47:43 +000053 Offset = padToAlignment(Align);
54 return OS;
55 }
Sean Silva46dffff2013-06-13 22:20:01 +000056 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
57};
58
Simon Atanasyan35babf92014-04-06 09:02:55 +000059// Used to keep track of section and symbol names, so that in the YAML file
George Rimar09746882019-05-07 12:10:51 +000060// sections and symbols can be referenced by name instead of by index.
George Rimar09746882019-05-07 12:10:51 +000061class NameToIdxMap {
Puyan Lotfia10f0162019-05-11 17:03:36 +000062 StringMap<unsigned> Map;
63
George Rimar09746882019-05-07 12:10:51 +000064public:
Puyan Lotfia10f0162019-05-11 17:03:36 +000065 /// \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 Rimar09746882019-05-07 12:10:51 +000068 }
Puyan Lotfia10f0162019-05-11 17:03:36 +000069 /// \Returns false if name is not present in the map.
George Rimar09746882019-05-07 12:10:51 +000070 bool lookup(StringRef Name, unsigned &Idx) const {
Puyan Lotfia10f0162019-05-11 17:03:36 +000071 auto I = Map.find(Name);
George Rimar09746882019-05-07 12:10:51 +000072 if (I == Map.end())
Puyan Lotfia10f0162019-05-11 17:03:36 +000073 return false;
George Rimar09746882019-05-07 12:10:51 +000074 Idx = I->getValue();
Puyan Lotfia10f0162019-05-11 17:03:36 +000075 return true;
George Rimar09746882019-05-07 12:10:51 +000076 }
Puyan Lotfia10f0162019-05-11 17:03:36 +000077 /// Asserts if name is not present in the map.
George Rimar09746882019-05-07 12:10:51 +000078 unsigned get(StringRef Name) const {
Puyan Lotfia10f0162019-05-11 17:03:36 +000079 unsigned Idx;
80 if (lookup(Name, Idx))
81 return Idx;
82 assert(false && "Expected section not found in index");
83 return 0;
George Rimar09746882019-05-07 12:10:51 +000084 }
85 unsigned size() const { return Map.size(); }
86};
Sean Silvaa6423eb2013-06-15 00:25:26 +000087
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000088/// "Single point of truth" for the ELF file construction.
Sean Silva08a75ae2013-06-20 19:11:44 +000089/// TODO: This class still has a ways to go before it is truly a "single
90/// point of truth".
Alex Brachetc22d9662019-08-07 02:44:49 +000091template <class ELFT> class ELFState {
Rui Ueyama478d6352018-01-12 02:28:31 +000092 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 Ehrlich0f440d82018-06-28 21:07:34 +000098 typedef typename ELFT::Relr Elf_Relr;
Paul Semel1dbbfba2018-07-23 18:49:04 +000099 typedef typename ELFT::Dyn Elf_Dyn;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000100
Dave Lee67b49662017-11-16 18:10:15 +0000101 enum class SymtabType { Static, Dynamic };
102
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000103 /// The future ".strtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000104 StringTableBuilder DotStrtab{StringTableBuilder::ELF};
Sean Silva08a75ae2013-06-20 19:11:44 +0000105
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000106 /// The future ".shstrtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000107 StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000108
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000109 /// The future ".dynstr" section.
Dave Lee67b49662017-11-16 18:10:15 +0000110 StringTableBuilder DotDynstr{StringTableBuilder::ELF};
111
Simon Atanasyan35babf92014-04-06 09:02:55 +0000112 NameToIdxMap SN2I;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000113 NameToIdxMap SymN2I;
George Rimar91208442019-08-22 12:39:56 +0000114 NameToIdxMap DynSymN2I;
George Rimar13a364e2019-07-22 12:01:52 +0000115 ELFYAML::Object &Doc;
Sean Silvac1c290b2013-06-20 20:59:34 +0000116
George Rimar3212ecf2019-09-09 09:43:03 +0000117 bool HasError = false;
George Rimar85011022019-09-13 16:00:16 +0000118 yaml::ErrorHandler ErrHandler;
119 void reportError(const Twine &Msg);
George Rimar3212ecf2019-09-09 09:43:03 +0000120
121 std::vector<Elf_Sym> toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
122 const StringTableBuilder &Strtab);
123 unsigned toSectionIndex(StringRef S, StringRef LocSec, StringRef LocSym = "");
124 unsigned toSymbolIndex(StringRef S, StringRef LocSec, bool IsDynamic);
George Rimar3212ecf2019-09-09 09:43:03 +0000125
126 void buildSectionIndex();
127 void buildSymbolIndexes();
Petr Hosekeb04da32017-07-19 20:38:46 +0000128 void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
George Rimar33b1a0e2019-09-05 08:59:28 +0000129 bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header,
130 StringRef SecName, ELFYAML::Section *YAMLSec);
George Rimar3212ecf2019-09-09 09:43:03 +0000131 void initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000132 ContiguousBlobAccumulator &CBA);
Dave Lee67b49662017-11-16 18:10:15 +0000133 void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
George Rimar66296dc2019-06-05 13:16:53 +0000134 ContiguousBlobAccumulator &CBA,
135 ELFYAML::Section *YAMLSec);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000136 void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
137 StringTableBuilder &STB,
George Rimar66296dc2019-06-05 13:16:53 +0000138 ContiguousBlobAccumulator &CBA,
139 ELFYAML::Section *YAMLSec);
Petr Hosekeb04da32017-07-19 20:38:46 +0000140 void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
141 std::vector<Elf_Shdr> &SHeaders);
George Rimar33b1a0e2019-09-05 08:59:28 +0000142 void finalizeStrings();
Fangrui Songc3bc6972019-09-05 14:25:57 +0000143 void writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS);
George Rimar3212ecf2019-09-09 09:43:03 +0000144 void writeSectionContent(Elf_Shdr &SHeader,
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000145 const ELFYAML::RawContentSection &Section,
146 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000147 void writeSectionContent(Elf_Shdr &SHeader,
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000148 const ELFYAML::RelocationSection &Section,
149 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000150 void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000151 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000152 void writeSectionContent(Elf_Shdr &SHeader,
George Rimard3963052019-08-08 09:49:05 +0000153 const ELFYAML::SymtabShndxSection &Shndx,
154 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000155 void writeSectionContent(Elf_Shdr &SHeader,
George Rimar646af082019-02-19 15:29:07 +0000156 const ELFYAML::SymverSection &Section,
157 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000158 void writeSectionContent(Elf_Shdr &SHeader,
George Rimar0621b792019-02-19 14:53:48 +0000159 const ELFYAML::VerneedSection &Section,
160 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000161 void writeSectionContent(Elf_Shdr &SHeader,
George Rimar623ae722019-02-21 12:21:43 +0000162 const ELFYAML::VerdefSection &Section,
163 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000164 void writeSectionContent(Elf_Shdr &SHeader,
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000165 const ELFYAML::MipsABIFlags &Section,
166 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000167 void writeSectionContent(Elf_Shdr &SHeader,
George Rimar0e7ed912019-02-09 11:34:28 +0000168 const ELFYAML::DynamicSection &Section,
169 ContiguousBlobAccumulator &CBA);
George Rimar85011022019-09-13 16:00:16 +0000170 ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH);
171
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000172public:
George Rimar85011022019-09-13 16:00:16 +0000173 static bool writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
174 yaml::ErrorHandler EH);
Sean Silva08a75ae2013-06-20 19:11:44 +0000175};
176} // end anonymous namespace
177
Alex Brachetc22d9662019-08-07 02:44:49 +0000178template <class T> static size_t arrayDataSize(ArrayRef<T> A) {
179 return A.size() * sizeof(T);
180}
181
182template <class T> static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
183 OS.write((const char *)A.data(), arrayDataSize(A));
184}
185
186template <class T> static void zero(T &Obj) { memset(&Obj, 0, sizeof(Obj)); }
187
George Rimar85011022019-09-13 16:00:16 +0000188template <class ELFT>
189ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
190 : Doc(D), ErrHandler(EH) {
George Rimar13a364e2019-07-22 12:01:52 +0000191 StringSet<> DocSections;
192 for (std::unique_ptr<ELFYAML::Section> &D : Doc.Sections)
193 if (!D->Name.empty())
194 DocSections.insert(D->Name);
195
George Rimar1957d682019-07-23 11:03:37 +0000196 // Insert SHT_NULL section implicitly when it is not defined in YAML.
197 if (Doc.Sections.empty() || Doc.Sections.front()->Type != ELF::SHT_NULL)
198 Doc.Sections.insert(
199 Doc.Sections.begin(),
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000200 std::make_unique<ELFYAML::Section>(
Alex Brachetc22d9662019-08-07 02:44:49 +0000201 ELFYAML::Section::SectionKind::RawContent, /*IsImplicit=*/true));
George Rimarab658f42019-07-23 07:38:44 +0000202
George Rimar13a364e2019-07-22 12:01:52 +0000203 std::vector<StringRef> ImplicitSections = {".symtab", ".strtab", ".shstrtab"};
204 if (!Doc.DynamicSymbols.empty())
205 ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"});
206
207 // Insert placeholders for implicit sections that are not
208 // defined explicitly in YAML.
209 for (StringRef SecName : ImplicitSections) {
210 if (DocSections.count(SecName))
211 continue;
212
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000213 std::unique_ptr<ELFYAML::Section> Sec = std::make_unique<ELFYAML::Section>(
George Rimar13a364e2019-07-22 12:01:52 +0000214 ELFYAML::Section::SectionKind::RawContent, true /*IsImplicit*/);
215 Sec->Name = SecName;
216 Doc.Sections.push_back(std::move(Sec));
217 }
218}
219
Fangrui Songc3bc6972019-09-05 14:25:57 +0000220template <class ELFT>
221void ELFState<ELFT>::writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS) {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000222 using namespace llvm::ELF;
Fangrui Songc3bc6972019-09-05 14:25:57 +0000223
224 Elf_Ehdr Header;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000225 zero(Header);
226 Header.e_ident[EI_MAG0] = 0x7f;
227 Header.e_ident[EI_MAG1] = 'E';
228 Header.e_ident[EI_MAG2] = 'L';
229 Header.e_ident[EI_MAG3] = 'F';
230 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
George Rimara5a0a0f2019-03-07 12:09:19 +0000231 Header.e_ident[EI_DATA] = Doc.Header.Data;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000232 Header.e_ident[EI_VERSION] = EV_CURRENT;
233 Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
George Rimar6367d7a2018-12-20 10:43:49 +0000234 Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000235 Header.e_type = Doc.Header.Type;
236 Header.e_machine = Doc.Header.Machine;
237 Header.e_version = EV_CURRENT;
238 Header.e_entry = Doc.Header.Entry;
Alex Brachet0b69c592019-09-06 02:27:55 +0000239 Header.e_phoff = Doc.ProgramHeaders.size() ? sizeof(Header) : 0;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000240 Header.e_flags = Doc.Header.Flags;
241 Header.e_ehsize = sizeof(Elf_Ehdr);
Alex Brachet0b69c592019-09-06 02:27:55 +0000242 Header.e_phentsize = Doc.ProgramHeaders.size() ? sizeof(Elf_Phdr) : 0;
Petr Hosekeb04da32017-07-19 20:38:46 +0000243 Header.e_phnum = Doc.ProgramHeaders.size();
George Rimar687d47c2019-06-27 11:08:42 +0000244
245 Header.e_shentsize =
246 Doc.Header.SHEntSize ? (uint16_t)*Doc.Header.SHEntSize : sizeof(Elf_Shdr);
Petr Hosekeb04da32017-07-19 20:38:46 +0000247 // Immediately following the ELF header and program headers.
Fangrui Songc3bc6972019-09-05 14:25:57 +0000248 // Align the start of the section header and write the ELF header.
Fangrui Songd20c41d2019-09-06 09:23:17 +0000249 uint64_t SHOff;
250 CBA.getOSAndAlignedOffset(SHOff, sizeof(typename ELFT::uint));
251 Header.e_shoff =
252 Doc.Header.SHOff ? typename ELFT::uint(*Doc.Header.SHOff) : SHOff;
George Rimar687d47c2019-06-27 11:08:42 +0000253 Header.e_shnum =
George Rimar14802292019-07-25 10:19:23 +0000254 Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : Doc.Sections.size();
George Rimar687d47c2019-06-27 11:08:42 +0000255 Header.e_shstrndx = Doc.Header.SHStrNdx ? (uint16_t)*Doc.Header.SHStrNdx
256 : SN2I.get(".shstrtab");
Fangrui Songc3bc6972019-09-05 14:25:57 +0000257
258 OS.write((const char *)&Header, sizeof(Header));
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000259}
260
261template <class ELFT>
Petr Hosekeb04da32017-07-19 20:38:46 +0000262void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
263 for (const auto &YamlPhdr : Doc.ProgramHeaders) {
264 Elf_Phdr Phdr;
265 Phdr.p_type = YamlPhdr.Type;
266 Phdr.p_flags = YamlPhdr.Flags;
267 Phdr.p_vaddr = YamlPhdr.VAddr;
268 Phdr.p_paddr = YamlPhdr.PAddr;
269 PHeaders.push_back(Phdr);
270 }
271}
George Rimar09746882019-05-07 12:10:51 +0000272
George Rimar3212ecf2019-09-09 09:43:03 +0000273template <class ELFT>
274unsigned ELFState<ELFT>::toSectionIndex(StringRef S, StringRef LocSec,
275 StringRef LocSym) {
276 unsigned Index;
277 if (SN2I.lookup(S, Index) || to_integer(S, Index))
278 return Index;
279
280 assert(LocSec.empty() || LocSym.empty());
281 if (!LocSym.empty())
282 reportError("unknown section referenced: '" + S + "' by YAML symbol '" +
283 LocSym + "'");
284 else
285 reportError("unknown section referenced: '" + S + "' by YAML section '" +
286 LocSec + "'");
287 return 0;
288}
289
290template <class ELFT>
291unsigned ELFState<ELFT>::toSymbolIndex(StringRef S, StringRef LocSec,
292 bool IsDynamic) {
293 const NameToIdxMap &SymMap = IsDynamic ? DynSymN2I : SymN2I;
294 unsigned Index;
295 // Here we try to look up S in the symbol table. If it is not there,
296 // treat its value as a symbol index.
297 if (!SymMap.lookup(S, Index) && !to_integer(S, Index)) {
298 reportError("unknown symbol referenced: '" + S + "' by YAML section '" +
299 LocSec + "'");
300 return 0;
Xing GUO98228352018-12-04 14:27:51 +0000301 }
George Rimar3212ecf2019-09-09 09:43:03 +0000302 return Index;
Xing GUO98228352018-12-04 14:27:51 +0000303}
304
Petr Hosekeb04da32017-07-19 20:38:46 +0000305template <class ELFT>
George Rimar33b1a0e2019-09-05 08:59:28 +0000306bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
George Rimar66296dc2019-06-05 13:16:53 +0000307 Elf_Shdr &Header, StringRef SecName,
308 ELFYAML::Section *YAMLSec) {
309 // Check if the header was already initialized.
310 if (Header.sh_offset)
311 return false;
312
313 if (SecName == ".symtab")
George Rimar33b1a0e2019-09-05 08:59:28 +0000314 initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec);
George Rimar66296dc2019-06-05 13:16:53 +0000315 else if (SecName == ".strtab")
George Rimar33b1a0e2019-09-05 08:59:28 +0000316 initStrtabSectionHeader(Header, SecName, DotStrtab, CBA, YAMLSec);
George Rimar66296dc2019-06-05 13:16:53 +0000317 else if (SecName == ".shstrtab")
George Rimar33b1a0e2019-09-05 08:59:28 +0000318 initStrtabSectionHeader(Header, SecName, DotShStrtab, CBA, YAMLSec);
George Rimar66296dc2019-06-05 13:16:53 +0000319 else if (SecName == ".dynsym")
George Rimar33b1a0e2019-09-05 08:59:28 +0000320 initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
George Rimar66296dc2019-06-05 13:16:53 +0000321 else if (SecName == ".dynstr")
George Rimar33b1a0e2019-09-05 08:59:28 +0000322 initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec);
George Rimar66296dc2019-06-05 13:16:53 +0000323 else
324 return false;
George Rimar9df825f2019-07-02 10:20:12 +0000325
George Rimar86cc7362019-09-02 09:47:17 +0000326 // Override the fields if requested.
George Rimareb41f7f2019-07-11 12:59:29 +0000327 if (YAMLSec) {
George Rimar86cc7362019-09-02 09:47:17 +0000328 if (YAMLSec->ShName)
329 Header.sh_name = *YAMLSec->ShName;
George Rimareb41f7f2019-07-11 12:59:29 +0000330 if (YAMLSec->ShOffset)
331 Header.sh_offset = *YAMLSec->ShOffset;
332 if (YAMLSec->ShSize)
333 Header.sh_size = *YAMLSec->ShSize;
334 }
George Rimar9df825f2019-07-02 10:20:12 +0000335
George Rimar66296dc2019-06-05 13:16:53 +0000336 return true;
337}
338
George Rimar60dc5d42019-06-25 08:22:57 +0000339static StringRef dropUniqueSuffix(StringRef S) {
340 size_t SuffixPos = S.rfind(" [");
341 if (SuffixPos == StringRef::npos)
342 return S;
343 return S.substr(0, SuffixPos);
344}
345
George Rimar66296dc2019-06-05 13:16:53 +0000346template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000347void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000348 ContiguousBlobAccumulator &CBA) {
349 // Ensure SHN_UNDEF entry is present. An all-zero section header is a
350 // valid SHN_UNDEF entry since SHT_NULL == 0.
George Rimarab658f42019-07-23 07:38:44 +0000351 SHeaders.resize(Doc.Sections.size());
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000352
George Rimar1957d682019-07-23 11:03:37 +0000353 for (size_t I = 0; I < Doc.Sections.size(); ++I) {
George Rimarab658f42019-07-23 07:38:44 +0000354 ELFYAML::Section *Sec = Doc.Sections[I].get();
George Rimar14802292019-07-25 10:19:23 +0000355 if (I == 0 && Sec->IsImplicit)
George Rimar1957d682019-07-23 11:03:37 +0000356 continue;
George Rimar1957d682019-07-23 11:03:37 +0000357
George Rimareb394e92019-06-07 08:31:36 +0000358 // We have a few sections like string or symbol tables that are usually
359 // added implicitly to the end. However, if they are explicitly specified
360 // in the YAML, we need to write them here. This ensures the file offset
361 // remains correct.
George Rimar14802292019-07-25 10:19:23 +0000362 Elf_Shdr &SHeader = SHeaders[I];
George Rimar33b1a0e2019-09-05 08:59:28 +0000363 if (initImplicitHeader(CBA, SHeader, Sec->Name,
George Rimar13a364e2019-07-22 12:01:52 +0000364 Sec->IsImplicit ? nullptr : Sec))
George Rimareb394e92019-06-07 08:31:36 +0000365 continue;
366
367 assert(Sec && "It can't be null unless it is an implicit section. But all "
368 "implicit sections should already have been handled above.");
369
George Rimar13a364e2019-07-22 12:01:52 +0000370 SHeader.sh_name = DotShStrtab.getOffset(dropUniqueSuffix(Sec->Name));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000371 SHeader.sh_type = Sec->Type;
George Rimarcfa1a622019-06-14 11:01:14 +0000372 if (Sec->Flags)
373 SHeader.sh_flags = *Sec->Flags;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000374 SHeader.sh_addr = Sec->Address;
375 SHeader.sh_addralign = Sec->AddressAlign;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000376
George Rimar3212ecf2019-09-09 09:43:03 +0000377 if (!Sec->Link.empty())
378 SHeader.sh_link = toSectionIndex(Sec->Link, Sec->Name);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000379
George Rimar14802292019-07-25 10:19:23 +0000380 if (I == 0) {
381 if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
382 // We do not write any content for special SHN_UNDEF section.
383 if (RawSec->Size)
384 SHeader.sh_size = *RawSec->Size;
385 if (RawSec->Info)
386 SHeader.sh_info = *RawSec->Info;
387 }
388 if (Sec->EntSize)
389 SHeader.sh_entsize = *Sec->EntSize;
390 } else if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000391 writeSectionContent(SHeader, *S, CBA);
George Rimard3963052019-08-08 09:49:05 +0000392 } else if (auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000393 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000394 } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000395 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000396 } else if (auto S = dyn_cast<ELFYAML::Group>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000397 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000398 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000399 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000400 } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
Simon Atanasyan5db02762015-07-03 23:00:54 +0000401 SHeader.sh_entsize = 0;
402 SHeader.sh_size = S->Size;
403 // SHT_NOBITS section does not have content
404 // so just to setup the section offset.
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000405 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimareb394e92019-06-07 08:31:36 +0000406 } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000407 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000408 } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000409 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000410 } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000411 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000412 } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000413 writeSectionContent(SHeader, *S, CBA);
414 } else {
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000415 llvm_unreachable("Unknown section type");
George Rimar3212ecf2019-09-09 09:43:03 +0000416 }
George Rimar9df825f2019-07-02 10:20:12 +0000417
George Rimar86cc7362019-09-02 09:47:17 +0000418 // Override the fields if requested.
George Rimareb41f7f2019-07-11 12:59:29 +0000419 if (Sec) {
George Rimar86cc7362019-09-02 09:47:17 +0000420 if (Sec->ShName)
421 SHeader.sh_name = *Sec->ShName;
George Rimareb41f7f2019-07-11 12:59:29 +0000422 if (Sec->ShOffset)
423 SHeader.sh_offset = *Sec->ShOffset;
424 if (Sec->ShSize)
425 SHeader.sh_size = *Sec->ShSize;
426 }
George Rimar66296dc2019-06-05 13:16:53 +0000427 }
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000428}
429
George Rimar6da44ad2019-04-03 14:53:42 +0000430static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
431 for (size_t I = 0; I < Symbols.size(); ++I)
432 if (Symbols[I].Binding.value != ELF::STB_LOCAL)
433 return I;
434 return Symbols.size();
435}
436
George Rimar1e410072019-06-10 12:43:18 +0000437static uint64_t writeRawSectionData(raw_ostream &OS,
438 const ELFYAML::RawContentSection &RawSec) {
439 size_t ContentSize = 0;
440 if (RawSec.Content) {
441 RawSec.Content->writeAsBinary(OS);
442 ContentSize = RawSec.Content->binary_size();
443 }
444
445 if (!RawSec.Size)
446 return ContentSize;
447
448 OS.write_zeros(*RawSec.Size - ContentSize);
449 return *RawSec.Size;
450}
451
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000452template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000453std::vector<typename ELFT::Sym>
454ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
455 const StringTableBuilder &Strtab) {
George Rimar30ea0c42019-06-20 14:44:48 +0000456 std::vector<Elf_Sym> Ret;
457 Ret.resize(Symbols.size() + 1);
458
459 size_t I = 0;
460 for (const auto &Sym : Symbols) {
461 Elf_Sym &Symbol = Ret[++I];
462
463 // If NameIndex, which contains the name offset, is explicitly specified, we
464 // use it. This is useful for preparing broken objects. Otherwise, we add
465 // the specified Name to the string table builder to get its offset.
466 if (Sym.NameIndex)
467 Symbol.st_name = *Sym.NameIndex;
468 else if (!Sym.Name.empty())
George Rimar60dc5d42019-06-25 08:22:57 +0000469 Symbol.st_name = Strtab.getOffset(dropUniqueSuffix(Sym.Name));
George Rimar30ea0c42019-06-20 14:44:48 +0000470
471 Symbol.setBindingAndType(Sym.Binding, Sym.Type);
George Rimar3212ecf2019-09-09 09:43:03 +0000472 if (!Sym.Section.empty())
473 Symbol.st_shndx = toSectionIndex(Sym.Section, "", Sym.Name);
474 else if (Sym.Index)
George Rimar30ea0c42019-06-20 14:44:48 +0000475 Symbol.st_shndx = *Sym.Index;
George Rimar3212ecf2019-09-09 09:43:03 +0000476
George Rimar30ea0c42019-06-20 14:44:48 +0000477 Symbol.st_value = Sym.Value;
George Rimar4e717022019-08-30 13:39:22 +0000478 Symbol.st_other = Sym.Other ? *Sym.Other : 0;
George Rimar30ea0c42019-06-20 14:44:48 +0000479 Symbol.st_size = Sym.Size;
480 }
481
482 return Ret;
483}
484
485template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000486void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
Dave Lee67b49662017-11-16 18:10:15 +0000487 SymtabType STType,
George Rimar66296dc2019-06-05 13:16:53 +0000488 ContiguousBlobAccumulator &CBA,
489 ELFYAML::Section *YAMLSec) {
George Rimar1e410072019-06-10 12:43:18 +0000490
Dave Lee67b49662017-11-16 18:10:15 +0000491 bool IsStatic = STType == SymtabType::Static;
George Rimar1e410072019-06-10 12:43:18 +0000492 const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
493
494 ELFYAML::RawContentSection *RawSec =
495 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
496 if (RawSec && !Symbols.empty() && (RawSec->Content || RawSec->Size)) {
497 if (RawSec->Content)
George Rimar3212ecf2019-09-09 09:43:03 +0000498 reportError("cannot specify both `Content` and " +
499 (IsStatic ? Twine("`Symbols`") : Twine("`DynamicSymbols`")) +
500 " for symbol table section '" + RawSec->Name + "'");
George Rimar1e410072019-06-10 12:43:18 +0000501 if (RawSec->Size)
George Rimar3212ecf2019-09-09 09:43:03 +0000502 reportError("cannot specify both `Size` and " +
503 (IsStatic ? Twine("`Symbols`") : Twine("`DynamicSymbols`")) +
504 " for symbol table section '" + RawSec->Name + "'");
505 return;
George Rimar1e410072019-06-10 12:43:18 +0000506 }
507
508 zero(SHeader);
Dave Lee67b49662017-11-16 18:10:15 +0000509 SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
George Rimar0aecaba2019-06-14 14:25:34 +0000510
511 if (YAMLSec)
512 SHeader.sh_type = YAMLSec->Type;
513 else
514 SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
George Rimar379aa182019-06-10 11:38:06 +0000515
George Rimarffb3c722019-06-11 10:00:51 +0000516 if (RawSec && !RawSec->Link.empty()) {
517 // If the Link field is explicitly defined in the document,
518 // we should use it.
George Rimar3212ecf2019-09-09 09:43:03 +0000519 SHeader.sh_link = toSectionIndex(RawSec->Link, RawSec->Name);
George Rimarffb3c722019-06-11 10:00:51 +0000520 } else {
521 // When we describe the .dynsym section in the document explicitly, it is
522 // allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not
523 // added implicitly and we should be able to leave the Link zeroed if
524 // .dynstr is not defined.
525 unsigned Link = 0;
526 if (IsStatic)
527 Link = SN2I.get(".strtab");
528 else
529 SN2I.lookup(".dynstr", Link);
530 SHeader.sh_link = Link;
531 }
George Rimar379aa182019-06-10 11:38:06 +0000532
George Rimarcfa1a622019-06-14 11:01:14 +0000533 if (YAMLSec && YAMLSec->Flags)
534 SHeader.sh_flags = *YAMLSec->Flags;
535 else if (!IsStatic)
536 SHeader.sh_flags = ELF::SHF_ALLOC;
George Rimarc1da1492019-04-26 12:45:54 +0000537
George Rimara7ba1a02019-03-01 10:18:16 +0000538 // If the symbol table section is explicitly described in the YAML
539 // then we should set the fields requested.
George Rimarb6e20932019-06-19 08:57:38 +0000540 SHeader.sh_info = (RawSec && RawSec->Info) ? (unsigned)(*RawSec->Info)
541 : findFirstNonGlobal(Symbols) + 1;
George Rimar66296dc2019-06-05 13:16:53 +0000542 SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize)
543 ? (uint64_t)(*YAMLSec->EntSize)
544 : sizeof(Elf_Sym);
545 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8;
546 SHeader.sh_addr = YAMLSec ? (uint64_t)YAMLSec->Address : 0;
547
George Rimar1e410072019-06-10 12:43:18 +0000548 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
549 if (RawSec && (RawSec->Content || RawSec->Size)) {
550 assert(Symbols.empty());
551 SHeader.sh_size = writeRawSectionData(OS, *RawSec);
552 return;
George Rimar6947acd2019-02-19 12:15:04 +0000553 }
George Rimar1e410072019-06-10 12:43:18 +0000554
George Rimar30ea0c42019-06-20 14:44:48 +0000555 std::vector<Elf_Sym> Syms =
George Rimar3212ecf2019-09-09 09:43:03 +0000556 toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr);
George Rimar1e410072019-06-10 12:43:18 +0000557 writeArrayData(OS, makeArrayRef(Syms));
558 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000559}
560
561template <class ELFT>
562void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
563 StringTableBuilder &STB,
George Rimar66296dc2019-06-05 13:16:53 +0000564 ContiguousBlobAccumulator &CBA,
565 ELFYAML::Section *YAMLSec) {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000566 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000567 SHeader.sh_name = DotShStrtab.getOffset(Name);
George Rimar0aecaba2019-06-14 14:25:34 +0000568 SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
George Rimar66296dc2019-06-05 13:16:53 +0000569 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
570
571 ELFYAML::RawContentSection *RawSec =
572 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
George Rimar1e410072019-06-10 12:43:18 +0000573
574 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
575 if (RawSec && (RawSec->Content || RawSec->Size)) {
576 SHeader.sh_size = writeRawSectionData(OS, *RawSec);
George Rimar66296dc2019-06-05 13:16:53 +0000577 } else {
George Rimar1e410072019-06-10 12:43:18 +0000578 STB.write(OS);
George Rimar66296dc2019-06-05 13:16:53 +0000579 SHeader.sh_size = STB.getSize();
580 }
581
582 if (YAMLSec && YAMLSec->EntSize)
583 SHeader.sh_entsize = *YAMLSec->EntSize;
George Rimar6947acd2019-02-19 12:15:04 +0000584
George Rimarb6e20932019-06-19 08:57:38 +0000585 if (RawSec && RawSec->Info)
586 SHeader.sh_info = *RawSec->Info;
587
George Rimarcfa1a622019-06-14 11:01:14 +0000588 if (YAMLSec && YAMLSec->Flags)
589 SHeader.sh_flags = *YAMLSec->Flags;
590 else if (Name == ".dynstr")
591 SHeader.sh_flags = ELF::SHF_ALLOC;
592
George Rimar43f62ff2019-06-14 11:13:32 +0000593 // If the section is explicitly described in the YAML
George Rimar6947acd2019-02-19 12:15:04 +0000594 // then we want to use its section address.
George Rimar43f62ff2019-06-14 11:13:32 +0000595 if (YAMLSec)
George Rimarcfa1a622019-06-14 11:01:14 +0000596 SHeader.sh_addr = YAMLSec->Address;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000597}
598
George Rimar3212ecf2019-09-09 09:43:03 +0000599template <class ELFT> void ELFState<ELFT>::reportError(const Twine &Msg) {
George Rimar85011022019-09-13 16:00:16 +0000600 ErrHandler(Msg);
George Rimar3212ecf2019-09-09 09:43:03 +0000601 HasError = true;
602}
603
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000604template <class ELFT>
Petr Hosekeb04da32017-07-19 20:38:46 +0000605void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
606 std::vector<Elf_Shdr> &SHeaders) {
607 uint32_t PhdrIdx = 0;
608 for (auto &YamlPhdr : Doc.ProgramHeaders) {
George Rimarf5345a32019-05-01 09:45:55 +0000609 Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
610
Puyan Lotfia10f0162019-05-11 17:03:36 +0000611 std::vector<Elf_Shdr *> Sections;
612 for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
613 unsigned Index;
614 if (!SN2I.lookup(SecName.Section, Index)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000615 reportError("unknown section referenced: '" + SecName.Section +
616 "' by program header");
617 continue;
George Rimarf5345a32019-05-01 09:45:55 +0000618 }
619 Sections.push_back(&SHeaders[Index]);
620 }
Petr Hosekeb04da32017-07-19 20:38:46 +0000621
James Hendersonb10f48b2019-03-15 10:35:27 +0000622 if (YamlPhdr.Offset) {
623 PHeader.p_offset = *YamlPhdr.Offset;
624 } else {
625 if (YamlPhdr.Sections.size())
626 PHeader.p_offset = UINT32_MAX;
Petr Hosekeb04da32017-07-19 20:38:46 +0000627 else
James Hendersonb10f48b2019-03-15 10:35:27 +0000628 PHeader.p_offset = 0;
629
630 // Find the minimum offset for the program header.
George Rimarf5345a32019-05-01 09:45:55 +0000631 for (Elf_Shdr *SHeader : Sections)
632 PHeader.p_offset = std::min(PHeader.p_offset, SHeader->sh_offset);
Petr Hosekeb04da32017-07-19 20:38:46 +0000633 }
634
Fangrui Songc28f3e62019-09-09 16:45:17 +0000635 // Find the maximum offset of the end of a section in order to set p_filesz
636 // and p_memsz. When setting p_filesz, trailing SHT_NOBITS sections are not
637 // counted.
638 uint64_t FileOffset = PHeader.p_offset, MemOffset = PHeader.p_offset;
639 for (Elf_Shdr *SHeader : Sections) {
640 uint64_t End = SHeader->sh_offset + SHeader->sh_size;
641 MemOffset = std::max(MemOffset, End);
642
643 if (SHeader->sh_type != llvm::ELF::SHT_NOBITS)
644 FileOffset = std::max(FileOffset, End);
James Hendersonb10f48b2019-03-15 10:35:27 +0000645 }
646
Fangrui Songc28f3e62019-09-09 16:45:17 +0000647 // Set the file size and the memory size if not set explicitly.
648 PHeader.p_filesz = YamlPhdr.FileSize ? uint64_t(*YamlPhdr.FileSize)
649 : FileOffset - PHeader.p_offset;
650 PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize)
651 : MemOffset - PHeader.p_offset;
Petr Hosekeb04da32017-07-19 20:38:46 +0000652
Jake Ehrlich03aeeb092017-11-01 23:14:48 +0000653 if (YamlPhdr.Align) {
654 PHeader.p_align = *YamlPhdr.Align;
655 } else {
Fangrui Song1da4f472019-09-10 09:16:34 +0000656 // Set the alignment of the segment to be the maximum alignment of the
657 // sections so that by default the segment has a valid and sensible
658 // alignment.
Jake Ehrlich03aeeb092017-11-01 23:14:48 +0000659 PHeader.p_align = 1;
George Rimarf5345a32019-05-01 09:45:55 +0000660 for (Elf_Shdr *SHeader : Sections)
Fangrui Song1da4f472019-09-10 09:16:34 +0000661 PHeader.p_align = std::max(PHeader.p_align, SHeader->sh_addralign);
Petr Hosekeb04da32017-07-19 20:38:46 +0000662 }
663 }
664}
665
666template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000667void ELFState<ELFT>::writeSectionContent(
George Rimarb49e1922019-04-24 13:02:15 +0000668 Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
669 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000670 raw_ostream &OS =
671 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimar1e410072019-06-10 12:43:18 +0000672 SHeader.sh_size = writeRawSectionData(OS, Section);
George Rimarb49e1922019-04-24 13:02:15 +0000673
George Rimar65a68282018-08-07 08:11:38 +0000674 if (Section.EntSize)
675 SHeader.sh_entsize = *Section.EntSize;
676 else if (Section.Type == llvm::ELF::SHT_RELR)
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000677 SHeader.sh_entsize = sizeof(Elf_Relr);
678 else
679 SHeader.sh_entsize = 0;
George Rimarb6e20932019-06-19 08:57:38 +0000680
681 if (Section.Info)
682 SHeader.sh_info = *Section.Info;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000683}
684
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000685static bool isMips64EL(const ELFYAML::Object &Doc) {
686 return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
687 Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
688 Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
689}
690
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000691template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000692void ELFState<ELFT>::writeSectionContent(
Alex Brachetc22d9662019-08-07 02:44:49 +0000693 Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section,
694 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000695 assert((Section.Type == llvm::ELF::SHT_REL ||
696 Section.Type == llvm::ELF::SHT_RELA) &&
697 "Section type is not SHT_REL nor SHT_RELA");
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000698
699 bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
700 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
701 SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
702
George Rimarda1b3ab2019-04-26 12:15:32 +0000703 // For relocation section set link to .symtab by default.
704 if (Section.Link.empty())
George Rimard71017b2019-06-10 09:57:29 +0000705 SHeader.sh_link = SN2I.get(".symtab");
George Rimarda1b3ab2019-04-26 12:15:32 +0000706
George Rimar3212ecf2019-09-09 09:43:03 +0000707 if (!Section.RelocatableSec.empty())
708 SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name);
George Rimarda1b3ab2019-04-26 12:15:32 +0000709
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000710 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000711 for (const auto &Rel : Section.Relocations) {
George Rimar3212ecf2019-09-09 09:43:03 +0000712 unsigned SymIdx = Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name,
713 Section.Link == ".dynsym")
714 : 0;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000715 if (IsRela) {
716 Elf_Rela REntry;
717 zero(REntry);
718 REntry.r_offset = Rel.Offset;
719 REntry.r_addend = Rel.Addend;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000720 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000721 OS.write((const char *)&REntry, sizeof(REntry));
722 } else {
723 Elf_Rel REntry;
724 zero(REntry);
725 REntry.r_offset = Rel.Offset;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000726 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000727 OS.write((const char *)&REntry, sizeof(REntry));
728 }
729 }
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000730}
731
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000732template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000733void ELFState<ELFT>::writeSectionContent(
George Rimard3963052019-08-08 09:49:05 +0000734 Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx,
735 ContiguousBlobAccumulator &CBA) {
736 raw_ostream &OS =
737 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
738
739 for (uint32_t E : Shndx.Entries)
740 support::endian::write<uint32_t>(OS, E, ELFT::TargetEndianness);
741
742 SHeader.sh_entsize = Shndx.EntSize ? (uint64_t)*Shndx.EntSize : 4;
743 SHeader.sh_size = Shndx.Entries.size() * SHeader.sh_entsize;
George Rimard3963052019-08-08 09:49:05 +0000744}
745
746template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000747void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000748 const ELFYAML::Group &Section,
749 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000750 assert(Section.Type == llvm::ELF::SHT_GROUP &&
751 "Section type is not SHT_GROUP");
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000752
George Rimard063c7d2019-02-20 13:58:43 +0000753 SHeader.sh_entsize = 4;
George Rimar09746882019-05-07 12:10:51 +0000754 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
George Rimar3212ecf2019-09-09 09:43:03 +0000755 SHeader.sh_info =
756 toSymbolIndex(Section.Signature, Section.Name, /*IsDynamic=*/false);
George Rimarda1b3ab2019-04-26 12:15:32 +0000757
George Rimard063c7d2019-02-20 13:58:43 +0000758 raw_ostream &OS =
759 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000760
George Rimarfb7780a2019-04-26 12:20:51 +0000761 for (const ELFYAML::SectionOrType &Member : Section.Members) {
762 unsigned int SectionIndex = 0;
763 if (Member.sectionNameOrType == "GRP_COMDAT")
764 SectionIndex = llvm::ELF::GRP_COMDAT;
George Rimar3212ecf2019-09-09 09:43:03 +0000765 else
766 SectionIndex = toSectionIndex(Member.sectionNameOrType, Section.Name);
George Rimarfb7780a2019-04-26 12:20:51 +0000767 support::endian::write<uint32_t>(OS, SectionIndex, ELFT::TargetEndianness);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000768 }
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000769}
770
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000771template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000772void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar646af082019-02-19 15:29:07 +0000773 const ELFYAML::SymverSection &Section,
774 ContiguousBlobAccumulator &CBA) {
George Rimar646af082019-02-19 15:29:07 +0000775 raw_ostream &OS =
776 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimardac37fb2019-02-20 14:01:02 +0000777 for (uint16_t Version : Section.Entries)
778 support::endian::write<uint16_t>(OS, Version, ELFT::TargetEndianness);
George Rimar646af082019-02-19 15:29:07 +0000779
George Rimar149aa2f2019-08-05 13:54:35 +0000780 SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2;
George Rimard063c7d2019-02-20 13:58:43 +0000781 SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize;
George Rimar646af082019-02-19 15:29:07 +0000782}
783
784template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000785void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar623ae722019-02-21 12:21:43 +0000786 const ELFYAML::VerdefSection &Section,
787 ContiguousBlobAccumulator &CBA) {
788 typedef typename ELFT::Verdef Elf_Verdef;
789 typedef typename ELFT::Verdaux Elf_Verdaux;
790 raw_ostream &OS =
791 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
792
793 uint64_t AuxCnt = 0;
794 for (size_t I = 0; I < Section.Entries.size(); ++I) {
795 const ELFYAML::VerdefEntry &E = Section.Entries[I];
796
797 Elf_Verdef VerDef;
798 VerDef.vd_version = E.Version;
799 VerDef.vd_flags = E.Flags;
800 VerDef.vd_ndx = E.VersionNdx;
801 VerDef.vd_hash = E.Hash;
802 VerDef.vd_aux = sizeof(Elf_Verdef);
803 VerDef.vd_cnt = E.VerNames.size();
804 if (I == Section.Entries.size() - 1)
805 VerDef.vd_next = 0;
806 else
807 VerDef.vd_next =
808 sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux);
809 OS.write((const char *)&VerDef, sizeof(Elf_Verdef));
810
811 for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) {
812 Elf_Verdaux VernAux;
813 VernAux.vda_name = DotDynstr.getOffset(E.VerNames[J]);
814 if (J == E.VerNames.size() - 1)
815 VernAux.vda_next = 0;
816 else
817 VernAux.vda_next = sizeof(Elf_Verdaux);
818 OS.write((const char *)&VernAux, sizeof(Elf_Verdaux));
819 }
820 }
821
822 SHeader.sh_size = Section.Entries.size() * sizeof(Elf_Verdef) +
823 AuxCnt * sizeof(Elf_Verdaux);
824 SHeader.sh_info = Section.Info;
George Rimar623ae722019-02-21 12:21:43 +0000825}
826
827template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000828void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar0621b792019-02-19 14:53:48 +0000829 const ELFYAML::VerneedSection &Section,
830 ContiguousBlobAccumulator &CBA) {
George Rimarda1b3ab2019-04-26 12:15:32 +0000831 typedef typename ELFT::Verneed Elf_Verneed;
832 typedef typename ELFT::Vernaux Elf_Vernaux;
George Rimar0621b792019-02-19 14:53:48 +0000833
George Rimarda1b3ab2019-04-26 12:15:32 +0000834 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimar0621b792019-02-19 14:53:48 +0000835
George Rimarda1b3ab2019-04-26 12:15:32 +0000836 uint64_t AuxCnt = 0;
837 for (size_t I = 0; I < Section.VerneedV.size(); ++I) {
838 const ELFYAML::VerneedEntry &VE = Section.VerneedV[I];
George Rimar0621b792019-02-19 14:53:48 +0000839
George Rimarda1b3ab2019-04-26 12:15:32 +0000840 Elf_Verneed VerNeed;
841 VerNeed.vn_version = VE.Version;
842 VerNeed.vn_file = DotDynstr.getOffset(VE.File);
843 if (I == Section.VerneedV.size() - 1)
844 VerNeed.vn_next = 0;
845 else
846 VerNeed.vn_next =
847 sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux);
848 VerNeed.vn_cnt = VE.AuxV.size();
849 VerNeed.vn_aux = sizeof(Elf_Verneed);
850 OS.write((const char *)&VerNeed, sizeof(Elf_Verneed));
George Rimar0621b792019-02-19 14:53:48 +0000851
George Rimarda1b3ab2019-04-26 12:15:32 +0000852 for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) {
853 const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J];
George Rimar0621b792019-02-19 14:53:48 +0000854
George Rimarda1b3ab2019-04-26 12:15:32 +0000855 Elf_Vernaux VernAux;
856 VernAux.vna_hash = VAuxE.Hash;
857 VernAux.vna_flags = VAuxE.Flags;
858 VernAux.vna_other = VAuxE.Other;
859 VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name);
860 if (J == VE.AuxV.size() - 1)
861 VernAux.vna_next = 0;
862 else
863 VernAux.vna_next = sizeof(Elf_Vernaux);
864 OS.write((const char *)&VernAux, sizeof(Elf_Vernaux));
865 }
866 }
George Rimar0621b792019-02-19 14:53:48 +0000867
George Rimarda1b3ab2019-04-26 12:15:32 +0000868 SHeader.sh_size = Section.VerneedV.size() * sizeof(Elf_Verneed) +
869 AuxCnt * sizeof(Elf_Vernaux);
870 SHeader.sh_info = Section.Info;
George Rimar0621b792019-02-19 14:53:48 +0000871}
872
873template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000874void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000875 const ELFYAML::MipsABIFlags &Section,
876 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000877 assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
878 "Section type is not SHT_MIPS_ABIFLAGS");
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000879
880 object::Elf_Mips_ABIFlags<ELFT> Flags;
881 zero(Flags);
882 SHeader.sh_entsize = sizeof(Flags);
883 SHeader.sh_size = SHeader.sh_entsize;
884
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000885 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000886 Flags.version = Section.Version;
887 Flags.isa_level = Section.ISALevel;
888 Flags.isa_rev = Section.ISARevision;
889 Flags.gpr_size = Section.GPRSize;
890 Flags.cpr1_size = Section.CPR1Size;
891 Flags.cpr2_size = Section.CPR2Size;
892 Flags.fp_abi = Section.FpABI;
893 Flags.isa_ext = Section.ISAExtension;
894 Flags.ases = Section.ASEs;
895 Flags.flags1 = Section.Flags1;
896 Flags.flags2 = Section.Flags2;
897 OS.write((const char *)&Flags, sizeof(Flags));
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000898}
899
George Rimar0e7ed912019-02-09 11:34:28 +0000900template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000901void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar0e7ed912019-02-09 11:34:28 +0000902 const ELFYAML::DynamicSection &Section,
903 ContiguousBlobAccumulator &CBA) {
George Rimard063c7d2019-02-20 13:58:43 +0000904 typedef typename ELFT::uint uintX_t;
905
George Rimar0e7ed912019-02-09 11:34:28 +0000906 assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
907 "Section type is not SHT_DYNAMIC");
908
George Rimar3212ecf2019-09-09 09:43:03 +0000909 if (!Section.Entries.empty() && Section.Content)
910 reportError("cannot specify both raw content and explicit entries "
911 "for dynamic section '" +
912 Section.Name + "'");
James Hendersonfd997802019-02-25 11:02:24 +0000913
914 if (Section.Content)
915 SHeader.sh_size = Section.Content->binary_size();
916 else
917 SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
George Rimar0e7ed912019-02-09 11:34:28 +0000918 if (Section.EntSize)
919 SHeader.sh_entsize = *Section.EntSize;
920 else
921 SHeader.sh_entsize = sizeof(Elf_Dyn);
922
Alex Brachetc22d9662019-08-07 02:44:49 +0000923 raw_ostream &OS =
924 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimar0e7ed912019-02-09 11:34:28 +0000925 for (const ELFYAML::DynamicEntry &DE : Section.Entries) {
George Rimard063c7d2019-02-20 13:58:43 +0000926 support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness);
927 support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness);
George Rimar0e7ed912019-02-09 11:34:28 +0000928 }
James Hendersonfd997802019-02-25 11:02:24 +0000929 if (Section.Content)
930 Section.Content->writeAsBinary(OS);
George Rimar0e7ed912019-02-09 11:34:28 +0000931}
932
George Rimar3212ecf2019-09-09 09:43:03 +0000933template <class ELFT> void ELFState<ELFT>::buildSectionIndex() {
George Rimar14802292019-07-25 10:19:23 +0000934 for (unsigned I = 0, E = Doc.Sections.size(); I != E; ++I) {
George Rimarab658f42019-07-23 07:38:44 +0000935 StringRef Name = Doc.Sections[I]->Name;
George Rimar14802292019-07-25 10:19:23 +0000936 if (Name.empty())
937 continue;
938
George Rimar60dc5d42019-06-25 08:22:57 +0000939 DotShStrtab.add(dropUniqueSuffix(Name));
George Rimar3212ecf2019-09-09 09:43:03 +0000940 if (!SN2I.addName(Name, I))
941 reportError("repeated section name: '" + Name +
942 "' at YAML section number " + Twine(I));
Sean Silvaaff51252013-06-21 00:27:50 +0000943 }
Dave Lee17307d92017-11-09 14:53:43 +0000944
Dave Lee17307d92017-11-09 14:53:43 +0000945 DotShStrtab.finalize();
Sean Silva6b083882013-06-18 23:14:03 +0000946}
947
George Rimar3212ecf2019-09-09 09:43:03 +0000948template <class ELFT> void ELFState<ELFT>::buildSymbolIndexes() {
949 auto Build = [this](ArrayRef<ELFYAML::Symbol> V, NameToIdxMap &Map) {
950 for (size_t I = 0, S = V.size(); I < S; ++I) {
951 const ELFYAML::Symbol &Sym = V[I];
952 if (!Sym.Name.empty() && !Map.addName(Sym.Name, I + 1))
953 reportError("repeated symbol name: '" + Sym.Name + "'");
954 }
955 };
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000956
George Rimar3212ecf2019-09-09 09:43:03 +0000957 Build(Doc.Symbols, SymN2I);
958 Build(Doc.DynamicSymbols, DynSymN2I);
George Rimar91208442019-08-22 12:39:56 +0000959}
960
George Rimar0621b792019-02-19 14:53:48 +0000961template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
George Rimar0621b792019-02-19 14:53:48 +0000962 // Add the regular symbol names to .strtab section.
George Rimar6da44ad2019-04-03 14:53:42 +0000963 for (const ELFYAML::Symbol &Sym : Doc.Symbols)
George Rimar60dc5d42019-06-25 08:22:57 +0000964 DotStrtab.add(dropUniqueSuffix(Sym.Name));
George Rimar0621b792019-02-19 14:53:48 +0000965 DotStrtab.finalize();
966
George Rimar0621b792019-02-19 14:53:48 +0000967 // Add the dynamic symbol names to .dynstr section.
George Rimar6da44ad2019-04-03 14:53:42 +0000968 for (const ELFYAML::Symbol &Sym : Doc.DynamicSymbols)
George Rimar60dc5d42019-06-25 08:22:57 +0000969 DotDynstr.add(dropUniqueSuffix(Sym.Name));
George Rimar0621b792019-02-19 14:53:48 +0000970
George Rimar623ae722019-02-21 12:21:43 +0000971 // SHT_GNU_verdef and SHT_GNU_verneed sections might also
972 // add strings to .dynstr section.
George Rimar0621b792019-02-19 14:53:48 +0000973 for (const std::unique_ptr<ELFYAML::Section> &Sec : Doc.Sections) {
George Rimar623ae722019-02-21 12:21:43 +0000974 if (auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec.get())) {
975 for (const ELFYAML::VerneedEntry &VE : VerNeed->VerneedV) {
976 DotDynstr.add(VE.File);
977 for (const ELFYAML::VernauxEntry &Aux : VE.AuxV)
978 DotDynstr.add(Aux.Name);
979 }
980 } else if (auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec.get())) {
981 for (const ELFYAML::VerdefEntry &E : VerDef->Entries)
982 for (StringRef Name : E.VerNames)
983 DotDynstr.add(Name);
George Rimar0621b792019-02-19 14:53:48 +0000984 }
985 }
986
987 DotDynstr.finalize();
988}
989
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000990template <class ELFT>
George Rimar85011022019-09-13 16:00:16 +0000991bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
992 yaml::ErrorHandler EH) {
993 ELFState<ELFT> State(Doc, EH);
George Rimar0621b792019-02-19 14:53:48 +0000994
995 // Finalize .strtab and .dynstr sections. We do that early because want to
996 // finalize the string table builders before writing the content of the
997 // sections that might want to use them.
998 State.finalizeStrings();
999
George Rimar3212ecf2019-09-09 09:43:03 +00001000 State.buildSectionIndex();
1001 State.buildSymbolIndexes();
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +00001002
Petr Hosekeb04da32017-07-19 20:38:46 +00001003 std::vector<Elf_Phdr> PHeaders;
1004 State.initProgramHeaders(PHeaders);
Sean Silva38205932013-06-13 22:19:48 +00001005
Sean Silva08a75ae2013-06-20 19:11:44 +00001006 // XXX: This offset is tightly coupled with the order that we write
1007 // things to `OS`.
Fangrui Songc3bc6972019-09-05 14:25:57 +00001008 const size_t SectionContentBeginOffset =
1009 sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
Sean Silva08a75ae2013-06-20 19:11:44 +00001010 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
Sean Silvac1c290b2013-06-20 20:59:34 +00001011
Sean Silva38205932013-06-13 22:19:48 +00001012 std::vector<Elf_Shdr> SHeaders;
George Rimar3212ecf2019-09-09 09:43:03 +00001013 State.initSectionHeaders(SHeaders, CBA);
Sean Silva38205932013-06-13 22:19:48 +00001014
Petr Hosekeb04da32017-07-19 20:38:46 +00001015 // Now we can decide segment offsets
1016 State.setProgramHeaderLayout(PHeaders, SHeaders);
1017
George Rimar3212ecf2019-09-09 09:43:03 +00001018 if (State.HasError)
George Rimar7da559f2019-09-13 09:12:38 +00001019 return false;
George Rimar3212ecf2019-09-09 09:43:03 +00001020
Fangrui Songc3bc6972019-09-05 14:25:57 +00001021 State.writeELFHeader(CBA, OS);
Petr Hosekeb04da32017-07-19 20:38:46 +00001022 writeArrayData(OS, makeArrayRef(PHeaders));
Sean Silva46dffff2013-06-13 22:20:01 +00001023 CBA.writeBlobToStream(OS);
Fangrui Songc3bc6972019-09-05 14:25:57 +00001024 writeArrayData(OS, makeArrayRef(SHeaders));
George Rimar7da559f2019-09-13 09:12:38 +00001025 return true;
Sean Silvaf99309c2013-06-10 23:44:15 +00001026}
1027
Alex Brachetc22d9662019-08-07 02:44:49 +00001028namespace llvm {
1029namespace yaml {
1030
George Rimar85011022019-09-13 16:00:16 +00001031bool yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH) {
George Rimarbc4d3c42019-04-29 12:25:01 +00001032 bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1033 bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1034 if (Is64Bit) {
1035 if (IsLE)
George Rimar85011022019-09-13 16:00:16 +00001036 return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH);
1037 return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH);
Sean Silvaf99309c2013-06-10 23:44:15 +00001038 }
George Rimarbc4d3c42019-04-29 12:25:01 +00001039 if (IsLE)
George Rimar85011022019-09-13 16:00:16 +00001040 return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH);
1041 return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH);
Sean Silvaf99309c2013-06-10 23:44:15 +00001042}
Alex Brachetc22d9662019-08-07 02:44:49 +00001043
1044} // namespace yaml
1045} // namespace llvm