blob: c6be508a64ae9db6667cd6ac7eb84d4410cba218 [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
14#include "yaml2obj.h"
Will Dietz0b48c732013-10-12 21:29:16 +000015#include "llvm/ADT/ArrayRef.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"
George Rimar13a364e2019-07-22 12:01:52 +000020#include "llvm/ADT/StringSet.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};
Sean Silva2a74f702013-06-15 00:31:46 +000058} // end anonymous namespace
Sean Silva46dffff2013-06-13 22:20:01 +000059
Simon Atanasyan35babf92014-04-06 09:02:55 +000060// Used to keep track of section and symbol names, so that in the YAML file
George Rimar09746882019-05-07 12:10:51 +000061// sections and symbols can be referenced by name instead of by index.
62namespace {
63class NameToIdxMap {
Puyan Lotfia10f0162019-05-11 17:03:36 +000064 StringMap<unsigned> Map;
65
George Rimar09746882019-05-07 12:10:51 +000066public:
Puyan Lotfia10f0162019-05-11 17:03:36 +000067 /// \Returns false if name is already present in the map.
68 bool addName(StringRef Name, unsigned Ndx) {
69 return Map.insert({Name, Ndx}).second;
George Rimar09746882019-05-07 12:10:51 +000070 }
Puyan Lotfia10f0162019-05-11 17:03:36 +000071 /// \Returns false if name is not present in the map.
George Rimar09746882019-05-07 12:10:51 +000072 bool lookup(StringRef Name, unsigned &Idx) const {
Puyan Lotfia10f0162019-05-11 17:03:36 +000073 auto I = Map.find(Name);
George Rimar09746882019-05-07 12:10:51 +000074 if (I == Map.end())
Puyan Lotfia10f0162019-05-11 17:03:36 +000075 return false;
George Rimar09746882019-05-07 12:10:51 +000076 Idx = I->getValue();
Puyan Lotfia10f0162019-05-11 17:03:36 +000077 return true;
George Rimar09746882019-05-07 12:10:51 +000078 }
Puyan Lotfia10f0162019-05-11 17:03:36 +000079 /// Asserts if name is not present in the map.
George Rimar09746882019-05-07 12:10:51 +000080 unsigned get(StringRef Name) const {
Puyan Lotfia10f0162019-05-11 17:03:36 +000081 unsigned Idx;
82 if (lookup(Name, Idx))
83 return Idx;
84 assert(false && "Expected section not found in index");
85 return 0;
George Rimar09746882019-05-07 12:10:51 +000086 }
87 unsigned size() const { return Map.size(); }
88};
Sean Silva2a74f702013-06-15 00:31:46 +000089} // end anonymous namespace
Sean Silvaa6423eb2013-06-15 00:25:26 +000090
Sean Silva38205932013-06-13 22:19:48 +000091template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +000092static size_t arrayDataSize(ArrayRef<T> A) {
93 return A.size() * sizeof(T);
Sean Silva38205932013-06-13 22:19:48 +000094}
95
96template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +000097static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
98 OS.write((const char *)A.data(), arrayDataSize(A));
Sean Silva38205932013-06-13 22:19:48 +000099}
100
101template <class T>
102static void zero(T &Obj) {
103 memset(&Obj, 0, sizeof(Obj));
104}
105
Sean Silva08a75ae2013-06-20 19:11:44 +0000106namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000107/// "Single point of truth" for the ELF file construction.
Sean Silva08a75ae2013-06-20 19:11:44 +0000108/// TODO: This class still has a ways to go before it is truly a "single
109/// point of truth".
110template <class ELFT>
111class ELFState {
Rui Ueyama478d6352018-01-12 02:28:31 +0000112 typedef typename ELFT::Ehdr Elf_Ehdr;
113 typedef typename ELFT::Phdr Elf_Phdr;
114 typedef typename ELFT::Shdr Elf_Shdr;
115 typedef typename ELFT::Sym Elf_Sym;
116 typedef typename ELFT::Rel Elf_Rel;
117 typedef typename ELFT::Rela Elf_Rela;
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000118 typedef typename ELFT::Relr Elf_Relr;
Paul Semel1dbbfba2018-07-23 18:49:04 +0000119 typedef typename ELFT::Dyn Elf_Dyn;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000120
Dave Lee67b49662017-11-16 18:10:15 +0000121 enum class SymtabType { Static, Dynamic };
122
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000123 /// The future ".strtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000124 StringTableBuilder DotStrtab{StringTableBuilder::ELF};
Sean Silva08a75ae2013-06-20 19:11:44 +0000125
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000126 /// The future ".shstrtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000127 StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000128
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000129 /// The future ".dynstr" section.
Dave Lee67b49662017-11-16 18:10:15 +0000130 StringTableBuilder DotDynstr{StringTableBuilder::ELF};
131
Simon Atanasyan35babf92014-04-06 09:02:55 +0000132 NameToIdxMap SN2I;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000133 NameToIdxMap SymN2I;
George Rimar13a364e2019-07-22 12:01:52 +0000134 ELFYAML::Object &Doc;
Sean Silvac1c290b2013-06-20 20:59:34 +0000135
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000136 bool buildSectionIndex();
George Rimar6da44ad2019-04-03 14:53:42 +0000137 bool buildSymbolIndex(ArrayRef<ELFYAML::Symbol> Symbols);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000138 void initELFHeader(Elf_Ehdr &Header);
Petr Hosekeb04da32017-07-19 20:38:46 +0000139 void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
George Rimar66296dc2019-06-05 13:16:53 +0000140 bool initImplicitHeader(ELFState<ELFT> &State, ContiguousBlobAccumulator &CBA,
141 Elf_Shdr &Header, StringRef SecName,
142 ELFYAML::Section *YAMLSec);
143 bool initSectionHeaders(ELFState<ELFT> &State,
144 std::vector<Elf_Shdr> &SHeaders,
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000145 ContiguousBlobAccumulator &CBA);
Dave Lee67b49662017-11-16 18:10:15 +0000146 void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
George Rimar66296dc2019-06-05 13:16:53 +0000147 ContiguousBlobAccumulator &CBA,
148 ELFYAML::Section *YAMLSec);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000149 void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
150 StringTableBuilder &STB,
George Rimar66296dc2019-06-05 13:16:53 +0000151 ContiguousBlobAccumulator &CBA,
152 ELFYAML::Section *YAMLSec);
Petr Hosekeb04da32017-07-19 20:38:46 +0000153 void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
154 std::vector<Elf_Shdr> &SHeaders);
George Rimarda1b3ab2019-04-26 12:15:32 +0000155 bool writeSectionContent(Elf_Shdr &SHeader,
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000156 const ELFYAML::RawContentSection &Section,
157 ContiguousBlobAccumulator &CBA);
158 bool writeSectionContent(Elf_Shdr &SHeader,
159 const ELFYAML::RelocationSection &Section,
160 ContiguousBlobAccumulator &CBA);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000161 bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
162 ContiguousBlobAccumulator &CBA);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000163 bool writeSectionContent(Elf_Shdr &SHeader,
George Rimar646af082019-02-19 15:29:07 +0000164 const ELFYAML::SymverSection &Section,
165 ContiguousBlobAccumulator &CBA);
166 bool writeSectionContent(Elf_Shdr &SHeader,
George Rimar0621b792019-02-19 14:53:48 +0000167 const ELFYAML::VerneedSection &Section,
168 ContiguousBlobAccumulator &CBA);
169 bool writeSectionContent(Elf_Shdr &SHeader,
George Rimar623ae722019-02-21 12:21:43 +0000170 const ELFYAML::VerdefSection &Section,
171 ContiguousBlobAccumulator &CBA);
172 bool writeSectionContent(Elf_Shdr &SHeader,
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000173 const ELFYAML::MipsABIFlags &Section,
174 ContiguousBlobAccumulator &CBA);
James Hendersonfd997802019-02-25 11:02:24 +0000175 bool writeSectionContent(Elf_Shdr &SHeader,
George Rimar0e7ed912019-02-09 11:34:28 +0000176 const ELFYAML::DynamicSection &Section,
177 ContiguousBlobAccumulator &CBA);
George Rimar13a364e2019-07-22 12:01:52 +0000178 ELFState(ELFYAML::Object &D);
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000179
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000180public:
George Rimar13a364e2019-07-22 12:01:52 +0000181 static int writeELF(raw_ostream &OS, ELFYAML::Object &Doc);
George Rimar0621b792019-02-19 14:53:48 +0000182
183private:
184 void finalizeStrings();
Sean Silva08a75ae2013-06-20 19:11:44 +0000185};
186} // end anonymous namespace
187
Sean Silva37e817c2013-06-21 00:33:01 +0000188template <class ELFT>
George Rimar13a364e2019-07-22 12:01:52 +0000189ELFState<ELFT>::ELFState(ELFYAML::Object &D) : Doc(D) {
190 StringSet<> DocSections;
191 for (std::unique_ptr<ELFYAML::Section> &D : Doc.Sections)
192 if (!D->Name.empty())
193 DocSections.insert(D->Name);
194
George Rimar1957d682019-07-23 11:03:37 +0000195 // Insert SHT_NULL section implicitly when it is not defined in YAML.
196 if (Doc.Sections.empty() || Doc.Sections.front()->Type != ELF::SHT_NULL)
197 Doc.Sections.insert(
198 Doc.Sections.begin(),
199 llvm::make_unique<ELFYAML::Section>(
George Rimarab658f42019-07-23 07:38:44 +0000200 ELFYAML::Section::SectionKind::RawContent, /*IsImplicit=*/true));
201
George Rimar13a364e2019-07-22 12:01:52 +0000202 std::vector<StringRef> ImplicitSections = {".symtab", ".strtab", ".shstrtab"};
203 if (!Doc.DynamicSymbols.empty())
204 ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"});
205
206 // Insert placeholders for implicit sections that are not
207 // defined explicitly in YAML.
208 for (StringRef SecName : ImplicitSections) {
209 if (DocSections.count(SecName))
210 continue;
211
212 std::unique_ptr<ELFYAML::Section> Sec = llvm::make_unique<ELFYAML::Section>(
213 ELFYAML::Section::SectionKind::RawContent, true /*IsImplicit*/);
214 Sec->Name = SecName;
215 Doc.Sections.push_back(std::move(Sec));
216 }
217}
218
219template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000220void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
221 using namespace llvm::ELF;
222 zero(Header);
223 Header.e_ident[EI_MAG0] = 0x7f;
224 Header.e_ident[EI_MAG1] = 'E';
225 Header.e_ident[EI_MAG2] = 'L';
226 Header.e_ident[EI_MAG3] = 'F';
227 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
George Rimara5a0a0f2019-03-07 12:09:19 +0000228 Header.e_ident[EI_DATA] = Doc.Header.Data;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000229 Header.e_ident[EI_VERSION] = EV_CURRENT;
230 Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
George Rimar6367d7a2018-12-20 10:43:49 +0000231 Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000232 Header.e_type = Doc.Header.Type;
233 Header.e_machine = Doc.Header.Machine;
234 Header.e_version = EV_CURRENT;
235 Header.e_entry = Doc.Header.Entry;
Petr Hosekeb04da32017-07-19 20:38:46 +0000236 Header.e_phoff = sizeof(Header);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000237 Header.e_flags = Doc.Header.Flags;
238 Header.e_ehsize = sizeof(Elf_Ehdr);
Petr Hosekeb04da32017-07-19 20:38:46 +0000239 Header.e_phentsize = sizeof(Elf_Phdr);
240 Header.e_phnum = Doc.ProgramHeaders.size();
George Rimar687d47c2019-06-27 11:08:42 +0000241
242 Header.e_shentsize =
243 Doc.Header.SHEntSize ? (uint16_t)*Doc.Header.SHEntSize : sizeof(Elf_Shdr);
Petr Hosekeb04da32017-07-19 20:38:46 +0000244 // Immediately following the ELF header and program headers.
245 Header.e_shoff =
George Rimar687d47c2019-06-27 11:08:42 +0000246 Doc.Header.SHOffset
George Rimarec10d5c62019-07-24 11:24:37 +0000247 ? (typename ELFT::uint)(*Doc.Header.SHOffset)
George Rimar687d47c2019-06-27 11:08:42 +0000248 : sizeof(Header) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
249 Header.e_shnum =
JF Bastien65217a42019-07-24 18:29:33 +0000250 Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : SN2I.size() + 1;
George Rimar687d47c2019-06-27 11:08:42 +0000251 Header.e_shstrndx = Doc.Header.SHStrNdx ? (uint16_t)*Doc.Header.SHStrNdx
252 : SN2I.get(".shstrtab");
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000253}
254
255template <class ELFT>
Petr Hosekeb04da32017-07-19 20:38:46 +0000256void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
257 for (const auto &YamlPhdr : Doc.ProgramHeaders) {
258 Elf_Phdr Phdr;
259 Phdr.p_type = YamlPhdr.Type;
260 Phdr.p_flags = YamlPhdr.Flags;
261 Phdr.p_vaddr = YamlPhdr.VAddr;
262 Phdr.p_paddr = YamlPhdr.PAddr;
263 PHeaders.push_back(Phdr);
264 }
265}
George Rimar09746882019-05-07 12:10:51 +0000266
267static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName,
268 StringRef IndexSrc, unsigned &IndexDest) {
Puyan Lotfia10f0162019-05-11 17:03:36 +0000269 if (!SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) {
George Rimar09746882019-05-07 12:10:51 +0000270 WithColor::error() << "Unknown section referenced: '" << IndexSrc
271 << "' at YAML section '" << SecName << "'.\n";
272 return false;
Xing GUO98228352018-12-04 14:27:51 +0000273 }
274 return true;
275}
276
Petr Hosekeb04da32017-07-19 20:38:46 +0000277template <class ELFT>
George Rimar66296dc2019-06-05 13:16:53 +0000278bool ELFState<ELFT>::initImplicitHeader(ELFState<ELFT> &State,
279 ContiguousBlobAccumulator &CBA,
280 Elf_Shdr &Header, StringRef SecName,
281 ELFYAML::Section *YAMLSec) {
282 // Check if the header was already initialized.
283 if (Header.sh_offset)
284 return false;
285
286 if (SecName == ".symtab")
287 State.initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec);
288 else if (SecName == ".strtab")
289 State.initStrtabSectionHeader(Header, SecName, State.DotStrtab, CBA,
290 YAMLSec);
291 else if (SecName == ".shstrtab")
292 State.initStrtabSectionHeader(Header, SecName, State.DotShStrtab, CBA,
293 YAMLSec);
294
295 else if (SecName == ".dynsym")
296 State.initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
297 else if (SecName == ".dynstr")
298 State.initStrtabSectionHeader(Header, SecName, State.DotDynstr, CBA,
299 YAMLSec);
300 else
301 return false;
George Rimar9df825f2019-07-02 10:20:12 +0000302
George Rimareb41f7f2019-07-11 12:59:29 +0000303 // Override the sh_offset/sh_size fields if requested.
304 if (YAMLSec) {
305 if (YAMLSec->ShOffset)
306 Header.sh_offset = *YAMLSec->ShOffset;
307 if (YAMLSec->ShSize)
308 Header.sh_size = *YAMLSec->ShSize;
309 }
George Rimar9df825f2019-07-02 10:20:12 +0000310
George Rimar66296dc2019-06-05 13:16:53 +0000311 return true;
312}
313
George Rimar60dc5d42019-06-25 08:22:57 +0000314static StringRef dropUniqueSuffix(StringRef S) {
315 size_t SuffixPos = S.rfind(" [");
316 if (SuffixPos == StringRef::npos)
317 return S;
318 return S.substr(0, SuffixPos);
319}
320
George Rimar66296dc2019-06-05 13:16:53 +0000321template <class ELFT>
322bool ELFState<ELFT>::initSectionHeaders(ELFState<ELFT> &State,
323 std::vector<Elf_Shdr> &SHeaders,
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000324 ContiguousBlobAccumulator &CBA) {
325 // Ensure SHN_UNDEF entry is present. An all-zero section header is a
326 // valid SHN_UNDEF entry since SHT_NULL == 0.
George Rimarab658f42019-07-23 07:38:44 +0000327 SHeaders.resize(Doc.Sections.size());
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000328
George Rimar1957d682019-07-23 11:03:37 +0000329 for (size_t I = 0; I < Doc.Sections.size(); ++I) {
JF Bastien65217a42019-07-24 18:29:33 +0000330 Elf_Shdr &SHeader = SHeaders[I];
George Rimarab658f42019-07-23 07:38:44 +0000331 ELFYAML::Section *Sec = Doc.Sections[I].get();
JF Bastien65217a42019-07-24 18:29:33 +0000332
333 if (I == 0) {
334 if (Sec->IsImplicit)
335 continue;
336
337 if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec))
338 if (S->Size)
339 SHeader.sh_size = *S->Size;
340
341 if (!Sec->Link.empty()) {
342 unsigned Index;
343 if (!convertSectionIndex(SN2I, Sec->Name, Sec->Link, Index))
344 return false;
345 SHeader.sh_link = Index;
346 }
George Rimar1957d682019-07-23 11:03:37 +0000347 continue;
JF Bastien65217a42019-07-24 18:29:33 +0000348 }
George Rimar1957d682019-07-23 11:03:37 +0000349
George Rimareb394e92019-06-07 08:31:36 +0000350 // We have a few sections like string or symbol tables that are usually
351 // added implicitly to the end. However, if they are explicitly specified
352 // in the YAML, we need to write them here. This ensures the file offset
353 // remains correct.
George Rimar13a364e2019-07-22 12:01:52 +0000354 if (initImplicitHeader(State, CBA, SHeader, Sec->Name,
355 Sec->IsImplicit ? nullptr : Sec))
George Rimareb394e92019-06-07 08:31:36 +0000356 continue;
357
358 assert(Sec && "It can't be null unless it is an implicit section. But all "
359 "implicit sections should already have been handled above.");
360
George Rimar13a364e2019-07-22 12:01:52 +0000361 SHeader.sh_name = DotShStrtab.getOffset(dropUniqueSuffix(Sec->Name));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000362 SHeader.sh_type = Sec->Type;
George Rimarcfa1a622019-06-14 11:01:14 +0000363 if (Sec->Flags)
364 SHeader.sh_flags = *Sec->Flags;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000365 SHeader.sh_addr = Sec->Address;
366 SHeader.sh_addralign = Sec->AddressAlign;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000367
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000368 if (!Sec->Link.empty()) {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000369 unsigned Index;
Xing GUO98228352018-12-04 14:27:51 +0000370 if (!convertSectionIndex(SN2I, Sec->Name, Sec->Link, Index))
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000371 return false;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000372 SHeader.sh_link = Index;
373 }
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000374
JF Bastien65217a42019-07-24 18:29:33 +0000375 if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
George Rimarda1b3ab2019-04-26 12:15:32 +0000376 if (!writeSectionContent(SHeader, *S, CBA))
George Rimar7f2df7d2018-08-16 12:23:22 +0000377 return false;
George Rimareb394e92019-06-07 08:31:36 +0000378 } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000379 if (!writeSectionContent(SHeader, *S, CBA))
380 return false;
George Rimareb394e92019-06-07 08:31:36 +0000381 } else if (auto S = dyn_cast<ELFYAML::Group>(Sec)) {
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000382 if (!writeSectionContent(SHeader, *S, CBA))
383 return false;
George Rimareb394e92019-06-07 08:31:36 +0000384 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000385 if (!writeSectionContent(SHeader, *S, CBA))
386 return false;
George Rimareb394e92019-06-07 08:31:36 +0000387 } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
Simon Atanasyan5db02762015-07-03 23:00:54 +0000388 SHeader.sh_entsize = 0;
389 SHeader.sh_size = S->Size;
390 // SHT_NOBITS section does not have content
391 // so just to setup the section offset.
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000392 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimareb394e92019-06-07 08:31:36 +0000393 } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
James Hendersonfd997802019-02-25 11:02:24 +0000394 if (!writeSectionContent(SHeader, *S, CBA))
395 return false;
George Rimareb394e92019-06-07 08:31:36 +0000396 } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
George Rimarda1b3ab2019-04-26 12:15:32 +0000397 if (!writeSectionContent(SHeader, *S, CBA))
398 return false;
George Rimareb394e92019-06-07 08:31:36 +0000399 } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
George Rimarda1b3ab2019-04-26 12:15:32 +0000400 if (!writeSectionContent(SHeader, *S, CBA))
401 return false;
George Rimareb394e92019-06-07 08:31:36 +0000402 } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
George Rimarda1b3ab2019-04-26 12:15:32 +0000403 if (!writeSectionContent(SHeader, *S, CBA))
404 return false;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000405 } else
406 llvm_unreachable("Unknown section type");
George Rimar9df825f2019-07-02 10:20:12 +0000407
George Rimareb41f7f2019-07-11 12:59:29 +0000408 // Override the sh_offset/sh_size fields if requested.
409 if (Sec) {
410 if (Sec->ShOffset)
411 SHeader.sh_offset = *Sec->ShOffset;
412 if (Sec->ShSize)
413 SHeader.sh_size = *Sec->ShSize;
414 }
George Rimar66296dc2019-06-05 13:16:53 +0000415 }
416
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000417 return true;
418}
419
George Rimar6da44ad2019-04-03 14:53:42 +0000420static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
421 for (size_t I = 0; I < Symbols.size(); ++I)
422 if (Symbols[I].Binding.value != ELF::STB_LOCAL)
423 return I;
424 return Symbols.size();
425}
426
George Rimar1e410072019-06-10 12:43:18 +0000427static uint64_t writeRawSectionData(raw_ostream &OS,
428 const ELFYAML::RawContentSection &RawSec) {
429 size_t ContentSize = 0;
430 if (RawSec.Content) {
431 RawSec.Content->writeAsBinary(OS);
432 ContentSize = RawSec.Content->binary_size();
433 }
434
435 if (!RawSec.Size)
436 return ContentSize;
437
438 OS.write_zeros(*RawSec.Size - ContentSize);
439 return *RawSec.Size;
440}
441
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000442template <class ELFT>
George Rimar30ea0c42019-06-20 14:44:48 +0000443static std::vector<typename ELFT::Sym>
444toELFSymbols(NameToIdxMap &SN2I, ArrayRef<ELFYAML::Symbol> Symbols,
445 const StringTableBuilder &Strtab) {
446 using Elf_Sym = typename ELFT::Sym;
447
448 std::vector<Elf_Sym> Ret;
449 Ret.resize(Symbols.size() + 1);
450
451 size_t I = 0;
452 for (const auto &Sym : Symbols) {
453 Elf_Sym &Symbol = Ret[++I];
454
455 // If NameIndex, which contains the name offset, is explicitly specified, we
456 // use it. This is useful for preparing broken objects. Otherwise, we add
457 // the specified Name to the string table builder to get its offset.
458 if (Sym.NameIndex)
459 Symbol.st_name = *Sym.NameIndex;
460 else if (!Sym.Name.empty())
George Rimar60dc5d42019-06-25 08:22:57 +0000461 Symbol.st_name = Strtab.getOffset(dropUniqueSuffix(Sym.Name));
George Rimar30ea0c42019-06-20 14:44:48 +0000462
463 Symbol.setBindingAndType(Sym.Binding, Sym.Type);
464 if (!Sym.Section.empty()) {
465 unsigned Index;
466 if (!SN2I.lookup(Sym.Section, Index)) {
467 WithColor::error() << "Unknown section referenced: '" << Sym.Section
468 << "' by YAML symbol " << Sym.Name << ".\n";
469 exit(1);
470 }
471 Symbol.st_shndx = Index;
472 } else if (Sym.Index) {
473 Symbol.st_shndx = *Sym.Index;
474 }
475 // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
476 Symbol.st_value = Sym.Value;
477 Symbol.st_other = Sym.Other;
478 Symbol.st_size = Sym.Size;
479 }
480
481 return Ret;
482}
483
484template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000485void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
Dave Lee67b49662017-11-16 18:10:15 +0000486 SymtabType STType,
George Rimar66296dc2019-06-05 13:16:53 +0000487 ContiguousBlobAccumulator &CBA,
488 ELFYAML::Section *YAMLSec) {
George Rimar1e410072019-06-10 12:43:18 +0000489
Dave Lee67b49662017-11-16 18:10:15 +0000490 bool IsStatic = STType == SymtabType::Static;
George Rimar1e410072019-06-10 12:43:18 +0000491 const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
492
493 ELFYAML::RawContentSection *RawSec =
494 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
495 if (RawSec && !Symbols.empty() && (RawSec->Content || RawSec->Size)) {
496 if (RawSec->Content)
497 WithColor::error() << "Cannot specify both `Content` and " +
498 (IsStatic ? Twine("`Symbols`")
499 : Twine("`DynamicSymbols`")) +
500 " for symbol table section '"
501 << RawSec->Name << "'.\n";
502 if (RawSec->Size)
503 WithColor::error() << "Cannot specify both `Size` and " +
504 (IsStatic ? Twine("`Symbols`")
505 : Twine("`DynamicSymbols`")) +
506 " for symbol table section '"
507 << RawSec->Name << "'.\n";
508 exit(1);
509 }
510
511 zero(SHeader);
Dave Lee67b49662017-11-16 18:10:15 +0000512 SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
George Rimar0aecaba2019-06-14 14:25:34 +0000513
514 if (YAMLSec)
515 SHeader.sh_type = YAMLSec->Type;
516 else
517 SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
George Rimar379aa182019-06-10 11:38:06 +0000518
George Rimarffb3c722019-06-11 10:00:51 +0000519 if (RawSec && !RawSec->Link.empty()) {
520 // If the Link field is explicitly defined in the document,
521 // we should use it.
522 unsigned Index;
523 if (!convertSectionIndex(SN2I, RawSec->Name, RawSec->Link, Index))
524 return;
525 SHeader.sh_link = Index;
526 } else {
527 // When we describe the .dynsym section in the document explicitly, it is
528 // allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not
529 // added implicitly and we should be able to leave the Link zeroed if
530 // .dynstr is not defined.
531 unsigned Link = 0;
532 if (IsStatic)
533 Link = SN2I.get(".strtab");
534 else
535 SN2I.lookup(".dynstr", Link);
536 SHeader.sh_link = Link;
537 }
George Rimar379aa182019-06-10 11:38:06 +0000538
George Rimarcfa1a622019-06-14 11:01:14 +0000539 if (YAMLSec && YAMLSec->Flags)
540 SHeader.sh_flags = *YAMLSec->Flags;
541 else if (!IsStatic)
542 SHeader.sh_flags = ELF::SHF_ALLOC;
George Rimarc1da1492019-04-26 12:45:54 +0000543
George Rimara7ba1a02019-03-01 10:18:16 +0000544 // If the symbol table section is explicitly described in the YAML
545 // then we should set the fields requested.
George Rimarb6e20932019-06-19 08:57:38 +0000546 SHeader.sh_info = (RawSec && RawSec->Info) ? (unsigned)(*RawSec->Info)
547 : findFirstNonGlobal(Symbols) + 1;
George Rimar66296dc2019-06-05 13:16:53 +0000548 SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize)
549 ? (uint64_t)(*YAMLSec->EntSize)
550 : sizeof(Elf_Sym);
551 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8;
552 SHeader.sh_addr = YAMLSec ? (uint64_t)YAMLSec->Address : 0;
553
George Rimar1e410072019-06-10 12:43:18 +0000554 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
555 if (RawSec && (RawSec->Content || RawSec->Size)) {
556 assert(Symbols.empty());
557 SHeader.sh_size = writeRawSectionData(OS, *RawSec);
558 return;
George Rimar6947acd2019-02-19 12:15:04 +0000559 }
George Rimar1e410072019-06-10 12:43:18 +0000560
George Rimar30ea0c42019-06-20 14:44:48 +0000561 std::vector<Elf_Sym> Syms =
562 toELFSymbols<ELFT>(SN2I, Symbols, IsStatic ? DotStrtab : DotDynstr);
George Rimar1e410072019-06-10 12:43:18 +0000563 writeArrayData(OS, makeArrayRef(Syms));
564 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000565}
566
567template <class ELFT>
568void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
569 StringTableBuilder &STB,
George Rimar66296dc2019-06-05 13:16:53 +0000570 ContiguousBlobAccumulator &CBA,
571 ELFYAML::Section *YAMLSec) {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000572 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000573 SHeader.sh_name = DotShStrtab.getOffset(Name);
George Rimar0aecaba2019-06-14 14:25:34 +0000574 SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
George Rimar66296dc2019-06-05 13:16:53 +0000575 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
576
577 ELFYAML::RawContentSection *RawSec =
578 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
George Rimar1e410072019-06-10 12:43:18 +0000579
580 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
581 if (RawSec && (RawSec->Content || RawSec->Size)) {
582 SHeader.sh_size = writeRawSectionData(OS, *RawSec);
George Rimar66296dc2019-06-05 13:16:53 +0000583 } else {
George Rimar1e410072019-06-10 12:43:18 +0000584 STB.write(OS);
George Rimar66296dc2019-06-05 13:16:53 +0000585 SHeader.sh_size = STB.getSize();
586 }
587
588 if (YAMLSec && YAMLSec->EntSize)
589 SHeader.sh_entsize = *YAMLSec->EntSize;
George Rimar6947acd2019-02-19 12:15:04 +0000590
George Rimarb6e20932019-06-19 08:57:38 +0000591 if (RawSec && RawSec->Info)
592 SHeader.sh_info = *RawSec->Info;
593
George Rimarcfa1a622019-06-14 11:01:14 +0000594 if (YAMLSec && YAMLSec->Flags)
595 SHeader.sh_flags = *YAMLSec->Flags;
596 else if (Name == ".dynstr")
597 SHeader.sh_flags = ELF::SHF_ALLOC;
598
George Rimar43f62ff2019-06-14 11:13:32 +0000599 // If the section is explicitly described in the YAML
George Rimar6947acd2019-02-19 12:15:04 +0000600 // then we want to use its section address.
George Rimar43f62ff2019-06-14 11:13:32 +0000601 if (YAMLSec)
George Rimarcfa1a622019-06-14 11:01:14 +0000602 SHeader.sh_addr = YAMLSec->Address;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000603}
604
605template <class ELFT>
Petr Hosekeb04da32017-07-19 20:38:46 +0000606void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
607 std::vector<Elf_Shdr> &SHeaders) {
608 uint32_t PhdrIdx = 0;
609 for (auto &YamlPhdr : Doc.ProgramHeaders) {
George Rimarf5345a32019-05-01 09:45:55 +0000610 Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
611
Puyan Lotfia10f0162019-05-11 17:03:36 +0000612 std::vector<Elf_Shdr *> Sections;
613 for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
614 unsigned Index;
615 if (!SN2I.lookup(SecName.Section, Index)) {
616 WithColor::error() << "Unknown section referenced: '" << SecName.Section
617 << "' by program header.\n";
618 exit(1);
George Rimarf5345a32019-05-01 09:45:55 +0000619 }
620 Sections.push_back(&SHeaders[Index]);
621 }
Petr Hosekeb04da32017-07-19 20:38:46 +0000622
James Hendersonb10f48b2019-03-15 10:35:27 +0000623 if (YamlPhdr.Offset) {
624 PHeader.p_offset = *YamlPhdr.Offset;
625 } else {
626 if (YamlPhdr.Sections.size())
627 PHeader.p_offset = UINT32_MAX;
Petr Hosekeb04da32017-07-19 20:38:46 +0000628 else
James Hendersonb10f48b2019-03-15 10:35:27 +0000629 PHeader.p_offset = 0;
630
631 // Find the minimum offset for the program header.
George Rimarf5345a32019-05-01 09:45:55 +0000632 for (Elf_Shdr *SHeader : Sections)
633 PHeader.p_offset = std::min(PHeader.p_offset, SHeader->sh_offset);
Petr Hosekeb04da32017-07-19 20:38:46 +0000634 }
635
James Hendersonb10f48b2019-03-15 10:35:27 +0000636 // Find the maximum offset of the end of a section in order to set p_filesz,
637 // if not set explicitly.
638 if (YamlPhdr.FileSize) {
639 PHeader.p_filesz = *YamlPhdr.FileSize;
640 } else {
641 PHeader.p_filesz = 0;
George Rimarf5345a32019-05-01 09:45:55 +0000642 for (Elf_Shdr *SHeader : Sections) {
James Hendersonb10f48b2019-03-15 10:35:27 +0000643 uint64_t EndOfSection;
George Rimarf5345a32019-05-01 09:45:55 +0000644 if (SHeader->sh_type == llvm::ELF::SHT_NOBITS)
645 EndOfSection = SHeader->sh_offset;
James Hendersonb10f48b2019-03-15 10:35:27 +0000646 else
George Rimarf5345a32019-05-01 09:45:55 +0000647 EndOfSection = SHeader->sh_offset + SHeader->sh_size;
James Hendersonb10f48b2019-03-15 10:35:27 +0000648 uint64_t EndOfSegment = PHeader.p_offset + PHeader.p_filesz;
649 EndOfSegment = std::max(EndOfSegment, EndOfSection);
650 PHeader.p_filesz = EndOfSegment - PHeader.p_offset;
651 }
652 }
653
654 // If not set explicitly, find the memory size by adding the size of
655 // sections at the end of the segment. These should be empty (size of zero)
656 // and NOBITS sections.
657 if (YamlPhdr.MemSize) {
658 PHeader.p_memsz = *YamlPhdr.MemSize;
659 } else {
660 PHeader.p_memsz = PHeader.p_filesz;
George Rimarf5345a32019-05-01 09:45:55 +0000661 for (Elf_Shdr *SHeader : Sections)
662 if (SHeader->sh_offset == PHeader.p_offset + PHeader.p_filesz)
663 PHeader.p_memsz += SHeader->sh_size;
Petr Hosekeb04da32017-07-19 20:38:46 +0000664 }
665
666 // Set the alignment of the segment to be the same as the maximum alignment
Jake Ehrlich03aeeb092017-11-01 23:14:48 +0000667 // of the sections with the same offset so that by default the segment
Petr Hosekeb04da32017-07-19 20:38:46 +0000668 // has a valid and sensible alignment.
Jake Ehrlich03aeeb092017-11-01 23:14:48 +0000669 if (YamlPhdr.Align) {
670 PHeader.p_align = *YamlPhdr.Align;
671 } else {
672 PHeader.p_align = 1;
George Rimarf5345a32019-05-01 09:45:55 +0000673 for (Elf_Shdr *SHeader : Sections)
674 if (SHeader->sh_offset == PHeader.p_offset)
675 PHeader.p_align = std::max(PHeader.p_align, SHeader->sh_addralign);
Petr Hosekeb04da32017-07-19 20:38:46 +0000676 }
677 }
678}
679
680template <class ELFT>
George Rimarda1b3ab2019-04-26 12:15:32 +0000681bool ELFState<ELFT>::writeSectionContent(
George Rimarb49e1922019-04-24 13:02:15 +0000682 Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
683 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000684 raw_ostream &OS =
685 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimar1e410072019-06-10 12:43:18 +0000686 SHeader.sh_size = writeRawSectionData(OS, Section);
George Rimarb49e1922019-04-24 13:02:15 +0000687
George Rimar65a68282018-08-07 08:11:38 +0000688 if (Section.EntSize)
689 SHeader.sh_entsize = *Section.EntSize;
690 else if (Section.Type == llvm::ELF::SHT_RELR)
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000691 SHeader.sh_entsize = sizeof(Elf_Relr);
692 else
693 SHeader.sh_entsize = 0;
George Rimarb6e20932019-06-19 08:57:38 +0000694
695 if (Section.Info)
696 SHeader.sh_info = *Section.Info;
697
George Rimarda1b3ab2019-04-26 12:15:32 +0000698 return true;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000699}
700
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000701static bool isMips64EL(const ELFYAML::Object &Doc) {
702 return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
703 Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
704 Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
705}
706
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000707template <class ELFT>
708bool
709ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
710 const ELFYAML::RelocationSection &Section,
711 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000712 assert((Section.Type == llvm::ELF::SHT_REL ||
713 Section.Type == llvm::ELF::SHT_RELA) &&
714 "Section type is not SHT_REL nor SHT_RELA");
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000715
716 bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
717 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
718 SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
719
George Rimarda1b3ab2019-04-26 12:15:32 +0000720 // For relocation section set link to .symtab by default.
721 if (Section.Link.empty())
George Rimard71017b2019-06-10 09:57:29 +0000722 SHeader.sh_link = SN2I.get(".symtab");
George Rimarda1b3ab2019-04-26 12:15:32 +0000723
724 unsigned Index = 0;
725 if (!Section.RelocatableSec.empty() &&
726 !convertSectionIndex(SN2I, Section.Name, Section.RelocatableSec, Index))
727 return false;
728 SHeader.sh_info = Index;
729
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000730 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000731
732 for (const auto &Rel : Section.Relocations) {
George Rimar09746882019-05-07 12:10:51 +0000733 unsigned SymIdx = 0;
734 // If a relocation references a symbol, try to look one up in the symbol
735 // table. If it is not there, treat the value as a symbol index.
Puyan Lotfia10f0162019-05-11 17:03:36 +0000736 if (Rel.Symbol && !SymN2I.lookup(*Rel.Symbol, SymIdx) &&
George Rimar09746882019-05-07 12:10:51 +0000737 !to_integer(*Rel.Symbol, SymIdx)) {
738 WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol
739 << "' at YAML section '" << Section.Name << "'.\n";
James Henderson9bc817a2019-03-12 17:00:25 +0000740 return false;
741 }
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000742
743 if (IsRela) {
744 Elf_Rela REntry;
745 zero(REntry);
746 REntry.r_offset = Rel.Offset;
747 REntry.r_addend = Rel.Addend;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000748 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000749 OS.write((const char *)&REntry, sizeof(REntry));
750 } else {
751 Elf_Rel REntry;
752 zero(REntry);
753 REntry.r_offset = Rel.Offset;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000754 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000755 OS.write((const char *)&REntry, sizeof(REntry));
756 }
757 }
758 return true;
759}
760
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000761template <class ELFT>
762bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
763 const ELFYAML::Group &Section,
764 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000765 assert(Section.Type == llvm::ELF::SHT_GROUP &&
766 "Section type is not SHT_GROUP");
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000767
George Rimard063c7d2019-02-20 13:58:43 +0000768 SHeader.sh_entsize = 4;
George Rimar09746882019-05-07 12:10:51 +0000769 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
770
Puyan Lotfia10f0162019-05-11 17:03:36 +0000771 unsigned SymIdx;
772 if (!SymN2I.lookup(Section.Signature, SymIdx) &&
773 !to_integer(Section.Signature, SymIdx)) {
774 WithColor::error() << "Unknown symbol referenced: '" << Section.Signature
775 << "' at YAML section '" << Section.Name << "'.\n";
George Rimarda1b3ab2019-04-26 12:15:32 +0000776 return false;
777 }
778 SHeader.sh_info = SymIdx;
779
George Rimard063c7d2019-02-20 13:58:43 +0000780 raw_ostream &OS =
781 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000782
George Rimarfb7780a2019-04-26 12:20:51 +0000783 for (const ELFYAML::SectionOrType &Member : Section.Members) {
784 unsigned int SectionIndex = 0;
785 if (Member.sectionNameOrType == "GRP_COMDAT")
786 SectionIndex = llvm::ELF::GRP_COMDAT;
787 else if (!convertSectionIndex(SN2I, Section.Name, Member.sectionNameOrType,
788 SectionIndex))
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000789 return false;
George Rimarfb7780a2019-04-26 12:20:51 +0000790 support::endian::write<uint32_t>(OS, SectionIndex, ELFT::TargetEndianness);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000791 }
792 return true;
793}
794
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000795template <class ELFT>
796bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar646af082019-02-19 15:29:07 +0000797 const ELFYAML::SymverSection &Section,
798 ContiguousBlobAccumulator &CBA) {
George Rimar646af082019-02-19 15:29:07 +0000799 raw_ostream &OS =
800 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimardac37fb2019-02-20 14:01:02 +0000801 for (uint16_t Version : Section.Entries)
802 support::endian::write<uint16_t>(OS, Version, ELFT::TargetEndianness);
George Rimar646af082019-02-19 15:29:07 +0000803
George Rimard063c7d2019-02-20 13:58:43 +0000804 SHeader.sh_entsize = 2;
805 SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize;
George Rimar646af082019-02-19 15:29:07 +0000806 return true;
807}
808
809template <class ELFT>
810bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar623ae722019-02-21 12:21:43 +0000811 const ELFYAML::VerdefSection &Section,
812 ContiguousBlobAccumulator &CBA) {
813 typedef typename ELFT::Verdef Elf_Verdef;
814 typedef typename ELFT::Verdaux Elf_Verdaux;
815 raw_ostream &OS =
816 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
817
818 uint64_t AuxCnt = 0;
819 for (size_t I = 0; I < Section.Entries.size(); ++I) {
820 const ELFYAML::VerdefEntry &E = Section.Entries[I];
821
822 Elf_Verdef VerDef;
823 VerDef.vd_version = E.Version;
824 VerDef.vd_flags = E.Flags;
825 VerDef.vd_ndx = E.VersionNdx;
826 VerDef.vd_hash = E.Hash;
827 VerDef.vd_aux = sizeof(Elf_Verdef);
828 VerDef.vd_cnt = E.VerNames.size();
829 if (I == Section.Entries.size() - 1)
830 VerDef.vd_next = 0;
831 else
832 VerDef.vd_next =
833 sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux);
834 OS.write((const char *)&VerDef, sizeof(Elf_Verdef));
835
836 for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) {
837 Elf_Verdaux VernAux;
838 VernAux.vda_name = DotDynstr.getOffset(E.VerNames[J]);
839 if (J == E.VerNames.size() - 1)
840 VernAux.vda_next = 0;
841 else
842 VernAux.vda_next = sizeof(Elf_Verdaux);
843 OS.write((const char *)&VernAux, sizeof(Elf_Verdaux));
844 }
845 }
846
847 SHeader.sh_size = Section.Entries.size() * sizeof(Elf_Verdef) +
848 AuxCnt * sizeof(Elf_Verdaux);
849 SHeader.sh_info = Section.Info;
850
851 return true;
852}
853
854template <class ELFT>
855bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar0621b792019-02-19 14:53:48 +0000856 const ELFYAML::VerneedSection &Section,
857 ContiguousBlobAccumulator &CBA) {
George Rimarda1b3ab2019-04-26 12:15:32 +0000858 typedef typename ELFT::Verneed Elf_Verneed;
859 typedef typename ELFT::Vernaux Elf_Vernaux;
George Rimar0621b792019-02-19 14:53:48 +0000860
George Rimarda1b3ab2019-04-26 12:15:32 +0000861 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimar0621b792019-02-19 14:53:48 +0000862
George Rimarda1b3ab2019-04-26 12:15:32 +0000863 uint64_t AuxCnt = 0;
864 for (size_t I = 0; I < Section.VerneedV.size(); ++I) {
865 const ELFYAML::VerneedEntry &VE = Section.VerneedV[I];
George Rimar0621b792019-02-19 14:53:48 +0000866
George Rimarda1b3ab2019-04-26 12:15:32 +0000867 Elf_Verneed VerNeed;
868 VerNeed.vn_version = VE.Version;
869 VerNeed.vn_file = DotDynstr.getOffset(VE.File);
870 if (I == Section.VerneedV.size() - 1)
871 VerNeed.vn_next = 0;
872 else
873 VerNeed.vn_next =
874 sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux);
875 VerNeed.vn_cnt = VE.AuxV.size();
876 VerNeed.vn_aux = sizeof(Elf_Verneed);
877 OS.write((const char *)&VerNeed, sizeof(Elf_Verneed));
George Rimar0621b792019-02-19 14:53:48 +0000878
George Rimarda1b3ab2019-04-26 12:15:32 +0000879 for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) {
880 const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J];
George Rimar0621b792019-02-19 14:53:48 +0000881
George Rimarda1b3ab2019-04-26 12:15:32 +0000882 Elf_Vernaux VernAux;
883 VernAux.vna_hash = VAuxE.Hash;
884 VernAux.vna_flags = VAuxE.Flags;
885 VernAux.vna_other = VAuxE.Other;
886 VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name);
887 if (J == VE.AuxV.size() - 1)
888 VernAux.vna_next = 0;
889 else
890 VernAux.vna_next = sizeof(Elf_Vernaux);
891 OS.write((const char *)&VernAux, sizeof(Elf_Vernaux));
892 }
893 }
George Rimar0621b792019-02-19 14:53:48 +0000894
George Rimarda1b3ab2019-04-26 12:15:32 +0000895 SHeader.sh_size = Section.VerneedV.size() * sizeof(Elf_Verneed) +
896 AuxCnt * sizeof(Elf_Vernaux);
897 SHeader.sh_info = Section.Info;
George Rimar0621b792019-02-19 14:53:48 +0000898
George Rimarda1b3ab2019-04-26 12:15:32 +0000899 return true;
George Rimar0621b792019-02-19 14:53:48 +0000900}
901
902template <class ELFT>
903bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000904 const ELFYAML::MipsABIFlags &Section,
905 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000906 assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
907 "Section type is not SHT_MIPS_ABIFLAGS");
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000908
909 object::Elf_Mips_ABIFlags<ELFT> Flags;
910 zero(Flags);
911 SHeader.sh_entsize = sizeof(Flags);
912 SHeader.sh_size = SHeader.sh_entsize;
913
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000914 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000915 Flags.version = Section.Version;
916 Flags.isa_level = Section.ISALevel;
917 Flags.isa_rev = Section.ISARevision;
918 Flags.gpr_size = Section.GPRSize;
919 Flags.cpr1_size = Section.CPR1Size;
920 Flags.cpr2_size = Section.CPR2Size;
921 Flags.fp_abi = Section.FpABI;
922 Flags.isa_ext = Section.ISAExtension;
923 Flags.ases = Section.ASEs;
924 Flags.flags1 = Section.Flags1;
925 Flags.flags2 = Section.Flags2;
926 OS.write((const char *)&Flags, sizeof(Flags));
927
928 return true;
929}
930
George Rimar0e7ed912019-02-09 11:34:28 +0000931template <class ELFT>
James Hendersonfd997802019-02-25 11:02:24 +0000932bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar0e7ed912019-02-09 11:34:28 +0000933 const ELFYAML::DynamicSection &Section,
934 ContiguousBlobAccumulator &CBA) {
George Rimard063c7d2019-02-20 13:58:43 +0000935 typedef typename ELFT::uint uintX_t;
936
George Rimar0e7ed912019-02-09 11:34:28 +0000937 assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
938 "Section type is not SHT_DYNAMIC");
939
James Hendersonfd997802019-02-25 11:02:24 +0000940 if (!Section.Entries.empty() && Section.Content) {
941 WithColor::error()
942 << "Cannot specify both raw content and explicit entries "
943 "for dynamic section '"
944 << Section.Name << "'.\n";
945 return false;
946 }
947
948 if (Section.Content)
949 SHeader.sh_size = Section.Content->binary_size();
950 else
951 SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
George Rimar0e7ed912019-02-09 11:34:28 +0000952 if (Section.EntSize)
953 SHeader.sh_entsize = *Section.EntSize;
954 else
955 SHeader.sh_entsize = sizeof(Elf_Dyn);
956
George Rimard063c7d2019-02-20 13:58:43 +0000957 raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimar0e7ed912019-02-09 11:34:28 +0000958 for (const ELFYAML::DynamicEntry &DE : Section.Entries) {
George Rimard063c7d2019-02-20 13:58:43 +0000959 support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness);
960 support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness);
George Rimar0e7ed912019-02-09 11:34:28 +0000961 }
James Hendersonfd997802019-02-25 11:02:24 +0000962 if (Section.Content)
963 Section.Content->writeAsBinary(OS);
964
965 return true;
George Rimar0e7ed912019-02-09 11:34:28 +0000966}
967
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000968template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
JF Bastien65217a42019-07-24 18:29:33 +0000969 for (unsigned I = 1, E = Doc.Sections.size(); I != E; ++I) {
George Rimarab658f42019-07-23 07:38:44 +0000970 StringRef Name = Doc.Sections[I]->Name;
George Rimar60dc5d42019-06-25 08:22:57 +0000971 DotShStrtab.add(dropUniqueSuffix(Name));
George Rimarab658f42019-07-23 07:38:44 +0000972 if (!SN2I.addName(Name, I)) {
George Rimar09746882019-05-07 12:10:51 +0000973 WithColor::error() << "Repeated section name: '" << Name
George Rimarab658f42019-07-23 07:38:44 +0000974 << "' at YAML section number " << I << ".\n";
George Rimar09746882019-05-07 12:10:51 +0000975 return false;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000976 }
Sean Silvaaff51252013-06-21 00:27:50 +0000977 }
Dave Lee17307d92017-11-09 14:53:43 +0000978
Dave Lee17307d92017-11-09 14:53:43 +0000979 DotShStrtab.finalize();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000980 return true;
Sean Silva6b083882013-06-18 23:14:03 +0000981}
982
Sean Silvaf99309c2013-06-10 23:44:15 +0000983template <class ELFT>
George Rimar6da44ad2019-04-03 14:53:42 +0000984bool ELFState<ELFT>::buildSymbolIndex(ArrayRef<ELFYAML::Symbol> Symbols) {
985 bool GlobalSymbolSeen = false;
George Rimar33e498b2019-03-11 16:10:02 +0000986 std::size_t I = 0;
George Rimar6da44ad2019-04-03 14:53:42 +0000987 for (const auto &Sym : Symbols) {
988 ++I;
989
990 StringRef Name = Sym.Name;
991 if (Sym.Binding.value == ELF::STB_LOCAL && GlobalSymbolSeen) {
992 WithColor::error() << "Local symbol '" + Name +
993 "' after global in Symbols list.\n";
994 return false;
995 }
George Rimar09746882019-05-07 12:10:51 +0000996 if (Sym.Binding.value != ELF::STB_LOCAL)
997 GlobalSymbolSeen = true;
998
Puyan Lotfia10f0162019-05-11 17:03:36 +0000999 if (!Name.empty() && !SymN2I.addName(Name, I)) {
George Rimar09746882019-05-07 12:10:51 +00001000 WithColor::error() << "Repeated symbol name: '" << Name << "'.\n";
1001 return false;
1002 }
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +00001003 }
1004 return true;
1005}
1006
George Rimar0621b792019-02-19 14:53:48 +00001007template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
George Rimar0621b792019-02-19 14:53:48 +00001008 // Add the regular symbol names to .strtab section.
George Rimar6da44ad2019-04-03 14:53:42 +00001009 for (const ELFYAML::Symbol &Sym : Doc.Symbols)
George Rimar60dc5d42019-06-25 08:22:57 +00001010 DotStrtab.add(dropUniqueSuffix(Sym.Name));
George Rimar0621b792019-02-19 14:53:48 +00001011 DotStrtab.finalize();
1012
George Rimar0621b792019-02-19 14:53:48 +00001013 // Add the dynamic symbol names to .dynstr section.
George Rimar6da44ad2019-04-03 14:53:42 +00001014 for (const ELFYAML::Symbol &Sym : Doc.DynamicSymbols)
George Rimar60dc5d42019-06-25 08:22:57 +00001015 DotDynstr.add(dropUniqueSuffix(Sym.Name));
George Rimar0621b792019-02-19 14:53:48 +00001016
George Rimar623ae722019-02-21 12:21:43 +00001017 // SHT_GNU_verdef and SHT_GNU_verneed sections might also
1018 // add strings to .dynstr section.
George Rimar0621b792019-02-19 14:53:48 +00001019 for (const std::unique_ptr<ELFYAML::Section> &Sec : Doc.Sections) {
George Rimar623ae722019-02-21 12:21:43 +00001020 if (auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec.get())) {
1021 for (const ELFYAML::VerneedEntry &VE : VerNeed->VerneedV) {
1022 DotDynstr.add(VE.File);
1023 for (const ELFYAML::VernauxEntry &Aux : VE.AuxV)
1024 DotDynstr.add(Aux.Name);
1025 }
1026 } else if (auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec.get())) {
1027 for (const ELFYAML::VerdefEntry &E : VerDef->Entries)
1028 for (StringRef Name : E.VerNames)
1029 DotDynstr.add(Name);
George Rimar0621b792019-02-19 14:53:48 +00001030 }
1031 }
1032
1033 DotDynstr.finalize();
1034}
1035
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +00001036template <class ELFT>
George Rimar13a364e2019-07-22 12:01:52 +00001037int ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc) {
Simon Atanasyan220c54a2014-04-02 16:34:40 +00001038 ELFState<ELFT> State(Doc);
George Rimar0621b792019-02-19 14:53:48 +00001039
1040 // Finalize .strtab and .dynstr sections. We do that early because want to
1041 // finalize the string table builders before writing the content of the
1042 // sections that might want to use them.
1043 State.finalizeStrings();
1044
Simon Atanasyan220c54a2014-04-02 16:34:40 +00001045 if (!State.buildSectionIndex())
1046 return 1;
1047
George Rimar33e498b2019-03-11 16:10:02 +00001048 if (!State.buildSymbolIndex(Doc.Symbols))
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +00001049 return 1;
1050
Sean Silva38205932013-06-13 22:19:48 +00001051 Elf_Ehdr Header;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +00001052 State.initELFHeader(Header);
Sean Silvaf99309c2013-06-10 23:44:15 +00001053
Sean Silva38205932013-06-13 22:19:48 +00001054 // TODO: Flesh out section header support.
Petr Hosekeb04da32017-07-19 20:38:46 +00001055
1056 std::vector<Elf_Phdr> PHeaders;
1057 State.initProgramHeaders(PHeaders);
Sean Silva38205932013-06-13 22:19:48 +00001058
Sean Silva08a75ae2013-06-20 19:11:44 +00001059 // XXX: This offset is tightly coupled with the order that we write
1060 // things to `OS`.
Petr Hosekeb04da32017-07-19 20:38:46 +00001061 const size_t SectionContentBeginOffset = Header.e_ehsize +
1062 Header.e_phentsize * Header.e_phnum +
1063 Header.e_shentsize * Header.e_shnum;
Sean Silva08a75ae2013-06-20 19:11:44 +00001064 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
Sean Silvac1c290b2013-06-20 20:59:34 +00001065
Sean Silva38205932013-06-13 22:19:48 +00001066 std::vector<Elf_Shdr> SHeaders;
George Rimar66296dc2019-06-05 13:16:53 +00001067 if (!State.initSectionHeaders(State, SHeaders, CBA))
Simon Atanasyan3ee21b02014-04-02 16:34:54 +00001068 return 1;
Sean Silva38205932013-06-13 22:19:48 +00001069
Petr Hosekeb04da32017-07-19 20:38:46 +00001070 // Now we can decide segment offsets
1071 State.setProgramHeaderLayout(PHeaders, SHeaders);
1072
Sean Silvaf99309c2013-06-10 23:44:15 +00001073 OS.write((const char *)&Header, sizeof(Header));
Petr Hosekeb04da32017-07-19 20:38:46 +00001074 writeArrayData(OS, makeArrayRef(PHeaders));
Will Dietz0b48c732013-10-12 21:29:16 +00001075 writeArrayData(OS, makeArrayRef(SHeaders));
Sean Silva46dffff2013-06-13 22:20:01 +00001076 CBA.writeBlobToStream(OS);
Sean Silva415d93f2013-06-17 20:14:59 +00001077 return 0;
Sean Silvaf99309c2013-06-10 23:44:15 +00001078}
1079
Chris Bieneman8ff0c112016-06-27 19:53:53 +00001080int yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out) {
George Rimarbc4d3c42019-04-29 12:25:01 +00001081 bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1082 bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1083 if (Is64Bit) {
1084 if (IsLE)
Rui Ueyama1b31eb92018-01-12 01:40:32 +00001085 return ELFState<object::ELF64LE>::writeELF(Out, Doc);
George Rimarbc4d3c42019-04-29 12:25:01 +00001086 return ELFState<object::ELF64BE>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +00001087 }
George Rimarbc4d3c42019-04-29 12:25:01 +00001088 if (IsLE)
1089 return ELFState<object::ELF32LE>::writeELF(Out, Doc);
1090 return ELFState<object::ELF32BE>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +00001091}