1 //===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// The ELF component of yaml2obj.
11 ///
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/StringSet.h"
18 #include "llvm/BinaryFormat/ELF.h"
19 #include "llvm/MC/StringTableBuilder.h"
20 #include "llvm/Object/ELFObjectFile.h"
21 #include "llvm/ObjectYAML/DWARFEmitter.h"
22 #include "llvm/ObjectYAML/DWARFYAML.h"
23 #include "llvm/ObjectYAML/ELFYAML.h"
24 #include "llvm/ObjectYAML/yaml2obj.h"
25 #include "llvm/Support/EndianStream.h"
26 #include "llvm/Support/Errc.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/LEB128.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/WithColor.h"
31 #include "llvm/Support/YAMLTraits.h"
32 #include "llvm/Support/raw_ostream.h"
33
34 using namespace llvm;
35
36 // This class is used to build up a contiguous binary blob while keeping
37 // track of an offset in the output (which notionally begins at
38 // `InitialOffset`).
39 // The blob might be limited to an arbitrary size. All attempts to write data
40 // are ignored and the error condition is remembered once the limit is reached.
41 // Such an approach allows us to simplify the code by delaying error reporting
42 // and doing it at a convenient time.
43 namespace {
44 class ContiguousBlobAccumulator {
45 const uint64_t InitialOffset;
46 const uint64_t MaxSize;
47
48 SmallVector<char, 128> Buf;
49 raw_svector_ostream OS;
50 Error ReachedLimitErr = Error::success();
51
checkLimit(uint64_t Size)52 bool checkLimit(uint64_t Size) {
53 if (!ReachedLimitErr && getOffset() + Size <= MaxSize)
54 return true;
55 if (!ReachedLimitErr)
56 ReachedLimitErr = createStringError(errc::invalid_argument,
57 "reached the output size limit");
58 return false;
59 }
60
61 public:
ContiguousBlobAccumulator(uint64_t BaseOffset,uint64_t SizeLimit)62 ContiguousBlobAccumulator(uint64_t BaseOffset, uint64_t SizeLimit)
63 : InitialOffset(BaseOffset), MaxSize(SizeLimit), OS(Buf) {}
64
tell() const65 uint64_t tell() const { return OS.tell(); }
getOffset() const66 uint64_t getOffset() const { return InitialOffset + OS.tell(); }
writeBlobToStream(raw_ostream & Out) const67 void writeBlobToStream(raw_ostream &Out) const { Out << OS.str(); }
68
takeLimitError()69 Error takeLimitError() {
70 // Request to write 0 bytes to check we did not reach the limit.
71 checkLimit(0);
72 return std::move(ReachedLimitErr);
73 }
74
75 /// \returns The new offset.
padToAlignment(unsigned Align)76 uint64_t padToAlignment(unsigned Align) {
77 uint64_t CurrentOffset = getOffset();
78 if (ReachedLimitErr)
79 return CurrentOffset;
80
81 uint64_t AlignedOffset = alignTo(CurrentOffset, Align == 0 ? 1 : Align);
82 uint64_t PaddingSize = AlignedOffset - CurrentOffset;
83 if (!checkLimit(PaddingSize))
84 return CurrentOffset;
85
86 writeZeros(PaddingSize);
87 return AlignedOffset;
88 }
89
getRawOS(uint64_t Size)90 raw_ostream *getRawOS(uint64_t Size) {
91 if (checkLimit(Size))
92 return &OS;
93 return nullptr;
94 }
95
writeAsBinary(const yaml::BinaryRef & Bin,uint64_t N=UINT64_MAX)96 void writeAsBinary(const yaml::BinaryRef &Bin, uint64_t N = UINT64_MAX) {
97 if (!checkLimit(Bin.binary_size()))
98 return;
99 Bin.writeAsBinary(OS, N);
100 }
101
writeZeros(uint64_t Num)102 void writeZeros(uint64_t Num) {
103 if (checkLimit(Num))
104 OS.write_zeros(Num);
105 }
106
write(const char * Ptr,size_t Size)107 void write(const char *Ptr, size_t Size) {
108 if (checkLimit(Size))
109 OS.write(Ptr, Size);
110 }
111
write(unsigned char C)112 void write(unsigned char C) {
113 if (checkLimit(1))
114 OS.write(C);
115 }
116
writeULEB128(uint64_t Val)117 unsigned writeULEB128(uint64_t Val) {
118 if (!checkLimit(sizeof(uint64_t)))
119 return 0;
120 return encodeULEB128(Val, OS);
121 }
122
write(T Val,support::endianness E)123 template <typename T> void write(T Val, support::endianness E) {
124 if (checkLimit(sizeof(T)))
125 support::endian::write<T>(OS, Val, E);
126 }
127 };
128
129 // Used to keep track of section and symbol names, so that in the YAML file
130 // sections and symbols can be referenced by name instead of by index.
131 class NameToIdxMap {
132 StringMap<unsigned> Map;
133
134 public:
135 /// \Returns false if name is already present in the map.
addName(StringRef Name,unsigned Ndx)136 bool addName(StringRef Name, unsigned Ndx) {
137 return Map.insert({Name, Ndx}).second;
138 }
139 /// \Returns false if name is not present in the map.
lookup(StringRef Name,unsigned & Idx) const140 bool lookup(StringRef Name, unsigned &Idx) const {
141 auto I = Map.find(Name);
142 if (I == Map.end())
143 return false;
144 Idx = I->getValue();
145 return true;
146 }
147 /// Asserts if name is not present in the map.
get(StringRef Name) const148 unsigned get(StringRef Name) const {
149 unsigned Idx;
150 if (lookup(Name, Idx))
151 return Idx;
152 assert(false && "Expected section not found in index");
153 return 0;
154 }
size() const155 unsigned size() const { return Map.size(); }
156 };
157
158 namespace {
159 struct Fragment {
160 uint64_t Offset;
161 uint64_t Size;
162 uint32_t Type;
163 uint64_t AddrAlign;
164 };
165 } // namespace
166
167 /// "Single point of truth" for the ELF file construction.
168 /// TODO: This class still has a ways to go before it is truly a "single
169 /// point of truth".
170 template <class ELFT> class ELFState {
171 typedef typename ELFT::Ehdr Elf_Ehdr;
172 typedef typename ELFT::Phdr Elf_Phdr;
173 typedef typename ELFT::Shdr Elf_Shdr;
174 typedef typename ELFT::Sym Elf_Sym;
175 typedef typename ELFT::Rel Elf_Rel;
176 typedef typename ELFT::Rela Elf_Rela;
177 typedef typename ELFT::Relr Elf_Relr;
178 typedef typename ELFT::Dyn Elf_Dyn;
179 typedef typename ELFT::uint uintX_t;
180
181 enum class SymtabType { Static, Dynamic };
182
183 /// The future ".strtab" section.
184 StringTableBuilder DotStrtab{StringTableBuilder::ELF};
185
186 /// The future ".shstrtab" section.
187 StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
188
189 /// The future ".dynstr" section.
190 StringTableBuilder DotDynstr{StringTableBuilder::ELF};
191
192 NameToIdxMap SN2I;
193 NameToIdxMap SymN2I;
194 NameToIdxMap DynSymN2I;
195 ELFYAML::Object &Doc;
196
197 StringSet<> ExcludedSectionHeaders;
198
199 uint64_t LocationCounter = 0;
200 bool HasError = false;
201 yaml::ErrorHandler ErrHandler;
202 void reportError(const Twine &Msg);
203 void reportError(Error Err);
204
205 std::vector<Elf_Sym> toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
206 const StringTableBuilder &Strtab);
207 unsigned toSectionIndex(StringRef S, StringRef LocSec, StringRef LocSym = "");
208 unsigned toSymbolIndex(StringRef S, StringRef LocSec, bool IsDynamic);
209
210 void buildSectionIndex();
211 void buildSymbolIndexes();
212 void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
213 bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header,
214 StringRef SecName, ELFYAML::Section *YAMLSec);
215 void initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
216 ContiguousBlobAccumulator &CBA);
217 void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
218 ContiguousBlobAccumulator &CBA,
219 ELFYAML::Section *YAMLSec);
220 void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
221 StringTableBuilder &STB,
222 ContiguousBlobAccumulator &CBA,
223 ELFYAML::Section *YAMLSec);
224 void initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
225 ContiguousBlobAccumulator &CBA,
226 ELFYAML::Section *YAMLSec);
227 void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
228 std::vector<Elf_Shdr> &SHeaders);
229
230 std::vector<Fragment>
231 getPhdrFragments(const ELFYAML::ProgramHeader &Phdr,
232 ArrayRef<typename ELFT::Shdr> SHeaders);
233
234 void finalizeStrings();
235 void writeELFHeader(raw_ostream &OS, Optional<uint64_t> SHOff);
236 void writeSectionContent(Elf_Shdr &SHeader,
237 const ELFYAML::NoBitsSection &Section,
238 ContiguousBlobAccumulator &CBA);
239 void writeSectionContent(Elf_Shdr &SHeader,
240 const ELFYAML::RawContentSection &Section,
241 ContiguousBlobAccumulator &CBA);
242 void writeSectionContent(Elf_Shdr &SHeader,
243 const ELFYAML::RelocationSection &Section,
244 ContiguousBlobAccumulator &CBA);
245 void writeSectionContent(Elf_Shdr &SHeader,
246 const ELFYAML::RelrSection &Section,
247 ContiguousBlobAccumulator &CBA);
248 void writeSectionContent(Elf_Shdr &SHeader,
249 const ELFYAML::GroupSection &Group,
250 ContiguousBlobAccumulator &CBA);
251 void writeSectionContent(Elf_Shdr &SHeader,
252 const ELFYAML::SymtabShndxSection &Shndx,
253 ContiguousBlobAccumulator &CBA);
254 void writeSectionContent(Elf_Shdr &SHeader,
255 const ELFYAML::SymverSection &Section,
256 ContiguousBlobAccumulator &CBA);
257 void writeSectionContent(Elf_Shdr &SHeader,
258 const ELFYAML::VerneedSection &Section,
259 ContiguousBlobAccumulator &CBA);
260 void writeSectionContent(Elf_Shdr &SHeader,
261 const ELFYAML::VerdefSection &Section,
262 ContiguousBlobAccumulator &CBA);
263 void writeSectionContent(Elf_Shdr &SHeader,
264 const ELFYAML::ARMIndexTableSection &Section,
265 ContiguousBlobAccumulator &CBA);
266 void writeSectionContent(Elf_Shdr &SHeader,
267 const ELFYAML::MipsABIFlags &Section,
268 ContiguousBlobAccumulator &CBA);
269 void writeSectionContent(Elf_Shdr &SHeader,
270 const ELFYAML::DynamicSection &Section,
271 ContiguousBlobAccumulator &CBA);
272 void writeSectionContent(Elf_Shdr &SHeader,
273 const ELFYAML::StackSizesSection &Section,
274 ContiguousBlobAccumulator &CBA);
275 void writeSectionContent(Elf_Shdr &SHeader,
276 const ELFYAML::BBAddrMapSection &Section,
277 ContiguousBlobAccumulator &CBA);
278 void writeSectionContent(Elf_Shdr &SHeader,
279 const ELFYAML::HashSection &Section,
280 ContiguousBlobAccumulator &CBA);
281 void writeSectionContent(Elf_Shdr &SHeader,
282 const ELFYAML::AddrsigSection &Section,
283 ContiguousBlobAccumulator &CBA);
284 void writeSectionContent(Elf_Shdr &SHeader,
285 const ELFYAML::NoteSection &Section,
286 ContiguousBlobAccumulator &CBA);
287 void writeSectionContent(Elf_Shdr &SHeader,
288 const ELFYAML::GnuHashSection &Section,
289 ContiguousBlobAccumulator &CBA);
290 void writeSectionContent(Elf_Shdr &SHeader,
291 const ELFYAML::LinkerOptionsSection &Section,
292 ContiguousBlobAccumulator &CBA);
293 void writeSectionContent(Elf_Shdr &SHeader,
294 const ELFYAML::DependentLibrariesSection &Section,
295 ContiguousBlobAccumulator &CBA);
296 void writeSectionContent(Elf_Shdr &SHeader,
297 const ELFYAML::CallGraphProfileSection &Section,
298 ContiguousBlobAccumulator &CBA);
299
300 void writeFill(ELFYAML::Fill &Fill, ContiguousBlobAccumulator &CBA);
301
302 ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH);
303
304 void assignSectionAddress(Elf_Shdr &SHeader, ELFYAML::Section *YAMLSec);
305
306 DenseMap<StringRef, size_t> buildSectionHeaderReorderMap();
307
308 BumpPtrAllocator StringAlloc;
309 uint64_t alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align,
310 llvm::Optional<llvm::yaml::Hex64> Offset);
311
312 uint64_t getSectionNameOffset(StringRef Name);
313
314 public:
315 static bool writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
316 yaml::ErrorHandler EH, uint64_t MaxSize);
317 };
318 } // end anonymous namespace
319
arrayDataSize(ArrayRef<T> A)320 template <class T> static size_t arrayDataSize(ArrayRef<T> A) {
321 return A.size() * sizeof(T);
322 }
323
writeArrayData(raw_ostream & OS,ArrayRef<T> A)324 template <class T> static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
325 OS.write((const char *)A.data(), arrayDataSize(A));
326 }
327
zero(T & Obj)328 template <class T> static void zero(T &Obj) { memset(&Obj, 0, sizeof(Obj)); }
329
330 template <class ELFT>
ELFState(ELFYAML::Object & D,yaml::ErrorHandler EH)331 ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
332 : Doc(D), ErrHandler(EH) {
333 std::vector<ELFYAML::Section *> Sections = Doc.getSections();
334 // Insert SHT_NULL section implicitly when it is not defined in YAML.
335 if (Sections.empty() || Sections.front()->Type != ELF::SHT_NULL)
336 Doc.Chunks.insert(
337 Doc.Chunks.begin(),
338 std::make_unique<ELFYAML::Section>(
339 ELFYAML::Chunk::ChunkKind::RawContent, /*IsImplicit=*/true));
340
341 // We add a technical suffix for each unnamed section/fill. It does not affect
342 // the output, but allows us to map them by name in the code and report better
343 // error messages.
344 StringSet<> DocSections;
345 for (size_t I = 0; I < Doc.Chunks.size(); ++I) {
346 const std::unique_ptr<ELFYAML::Chunk> &C = Doc.Chunks[I];
347 if (C->Name.empty()) {
348 std::string NewName = ELFYAML::appendUniqueSuffix(
349 /*Name=*/"", "index " + Twine(I));
350 C->Name = StringRef(NewName).copy(StringAlloc);
351 assert(ELFYAML::dropUniqueSuffix(C->Name).empty());
352 }
353
354 if (!DocSections.insert(C->Name).second)
355 reportError("repeated section/fill name: '" + C->Name +
356 "' at YAML section/fill number " + Twine(I));
357 }
358
359 std::vector<StringRef> ImplicitSections;
360 if (Doc.DynamicSymbols)
361 ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"});
362 if (Doc.Symbols)
363 ImplicitSections.push_back(".symtab");
364 if (Doc.DWARF)
365 for (StringRef DebugSecName : Doc.DWARF->getNonEmptySectionNames()) {
366 std::string SecName = ("." + DebugSecName).str();
367 ImplicitSections.push_back(StringRef(SecName).copy(StringAlloc));
368 }
369 ImplicitSections.insert(ImplicitSections.end(), {".strtab"});
370 if (!Doc.SectionHeaders || !Doc.SectionHeaders->NoHeaders.getValueOr(false))
371 ImplicitSections.insert(ImplicitSections.end(), {".shstrtab"});
372
373 // Insert placeholders for implicit sections that are not
374 // defined explicitly in YAML.
375 for (StringRef SecName : ImplicitSections) {
376 if (DocSections.count(SecName))
377 continue;
378
379 std::unique_ptr<ELFYAML::Chunk> Sec = std::make_unique<ELFYAML::Section>(
380 ELFYAML::Chunk::ChunkKind::RawContent, true /*IsImplicit*/);
381 Sec->Name = SecName;
382 Doc.Chunks.push_back(std::move(Sec));
383 }
384 }
385
386 template <class ELFT>
writeELFHeader(raw_ostream & OS,Optional<uint64_t> SHOff)387 void ELFState<ELFT>::writeELFHeader(raw_ostream &OS, Optional<uint64_t> SHOff) {
388 using namespace llvm::ELF;
389
390 Elf_Ehdr Header;
391 zero(Header);
392 Header.e_ident[EI_MAG0] = 0x7f;
393 Header.e_ident[EI_MAG1] = 'E';
394 Header.e_ident[EI_MAG2] = 'L';
395 Header.e_ident[EI_MAG3] = 'F';
396 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
397 Header.e_ident[EI_DATA] = Doc.Header.Data;
398 Header.e_ident[EI_VERSION] = EV_CURRENT;
399 Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
400 Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion;
401 Header.e_type = Doc.Header.Type;
402
403 if (Doc.Header.Machine)
404 Header.e_machine = *Doc.Header.Machine;
405 else
406 Header.e_machine = EM_NONE;
407
408 Header.e_version = EV_CURRENT;
409 Header.e_entry = Doc.Header.Entry;
410 Header.e_flags = Doc.Header.Flags;
411 Header.e_ehsize = sizeof(Elf_Ehdr);
412
413 if (Doc.Header.EPhOff)
414 Header.e_phoff = *Doc.Header.EPhOff;
415 else if (!Doc.ProgramHeaders.empty())
416 Header.e_phoff = sizeof(Header);
417 else
418 Header.e_phoff = 0;
419
420 if (Doc.Header.EPhEntSize)
421 Header.e_phentsize = *Doc.Header.EPhEntSize;
422 else if (!Doc.ProgramHeaders.empty())
423 Header.e_phentsize = sizeof(Elf_Phdr);
424 else
425 Header.e_phentsize = 0;
426
427 if (Doc.Header.EPhNum)
428 Header.e_phnum = *Doc.Header.EPhNum;
429 else if (!Doc.ProgramHeaders.empty())
430 Header.e_phnum = Doc.ProgramHeaders.size();
431 else
432 Header.e_phnum = 0;
433
434 Header.e_shentsize = Doc.Header.EShEntSize ? (uint16_t)*Doc.Header.EShEntSize
435 : sizeof(Elf_Shdr);
436
437 if (Doc.Header.EShOff)
438 Header.e_shoff = *Doc.Header.EShOff;
439 else if (SHOff)
440 Header.e_shoff = *SHOff;
441 else
442 Header.e_shoff = 0;
443
444 if (Doc.Header.EShNum)
445 Header.e_shnum = *Doc.Header.EShNum;
446 else if (!Doc.SectionHeaders ||
447 (Doc.SectionHeaders->NoHeaders && !*Doc.SectionHeaders->NoHeaders))
448 Header.e_shnum = Doc.getSections().size();
449 else if (!SHOff)
450 Header.e_shnum = 0;
451 else
452 Header.e_shnum =
453 (Doc.SectionHeaders->Sections ? Doc.SectionHeaders->Sections->size()
454 : 0) +
455 /*Null section*/ 1;
456
457 if (Doc.Header.EShStrNdx)
458 Header.e_shstrndx = *Doc.Header.EShStrNdx;
459 else if (SHOff && !ExcludedSectionHeaders.count(".shstrtab"))
460 Header.e_shstrndx = SN2I.get(".shstrtab");
461 else
462 Header.e_shstrndx = 0;
463
464 OS.write((const char *)&Header, sizeof(Header));
465 }
466
467 template <class ELFT>
initProgramHeaders(std::vector<Elf_Phdr> & PHeaders)468 void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
469 DenseMap<StringRef, ELFYAML::Fill *> NameToFill;
470 DenseMap<StringRef, size_t> NameToIndex;
471 for (size_t I = 0, E = Doc.Chunks.size(); I != E; ++I) {
472 if (auto S = dyn_cast<ELFYAML::Fill>(Doc.Chunks[I].get()))
473 NameToFill[S->Name] = S;
474 NameToIndex[Doc.Chunks[I]->Name] = I + 1;
475 }
476
477 std::vector<ELFYAML::Section *> Sections = Doc.getSections();
478 for (size_t I = 0, E = Doc.ProgramHeaders.size(); I != E; ++I) {
479 ELFYAML::ProgramHeader &YamlPhdr = Doc.ProgramHeaders[I];
480 Elf_Phdr Phdr;
481 zero(Phdr);
482 Phdr.p_type = YamlPhdr.Type;
483 Phdr.p_flags = YamlPhdr.Flags;
484 Phdr.p_vaddr = YamlPhdr.VAddr;
485 Phdr.p_paddr = YamlPhdr.PAddr;
486 PHeaders.push_back(Phdr);
487
488 if (!YamlPhdr.FirstSec && !YamlPhdr.LastSec)
489 continue;
490
491 // Get the index of the section, or 0 in the case when the section doesn't exist.
492 size_t First = NameToIndex[*YamlPhdr.FirstSec];
493 if (!First)
494 reportError("unknown section or fill referenced: '" + *YamlPhdr.FirstSec +
495 "' by the 'FirstSec' key of the program header with index " +
496 Twine(I));
497 size_t Last = NameToIndex[*YamlPhdr.LastSec];
498 if (!Last)
499 reportError("unknown section or fill referenced: '" + *YamlPhdr.LastSec +
500 "' by the 'LastSec' key of the program header with index " +
501 Twine(I));
502 if (!First || !Last)
503 continue;
504
505 if (First > Last)
506 reportError("program header with index " + Twine(I) +
507 ": the section index of " + *YamlPhdr.FirstSec +
508 " is greater than the index of " + *YamlPhdr.LastSec);
509
510 for (size_t I = First; I <= Last; ++I)
511 YamlPhdr.Chunks.push_back(Doc.Chunks[I - 1].get());
512 }
513 }
514
515 template <class ELFT>
toSectionIndex(StringRef S,StringRef LocSec,StringRef LocSym)516 unsigned ELFState<ELFT>::toSectionIndex(StringRef S, StringRef LocSec,
517 StringRef LocSym) {
518 assert(LocSec.empty() || LocSym.empty());
519
520 unsigned Index;
521 if (!SN2I.lookup(S, Index) && !to_integer(S, Index)) {
522 if (!LocSym.empty())
523 reportError("unknown section referenced: '" + S + "' by YAML symbol '" +
524 LocSym + "'");
525 else
526 reportError("unknown section referenced: '" + S + "' by YAML section '" +
527 LocSec + "'");
528 return 0;
529 }
530
531 if (!Doc.SectionHeaders || (Doc.SectionHeaders->NoHeaders &&
532 !Doc.SectionHeaders->NoHeaders.getValue()))
533 return Index;
534
535 assert(!Doc.SectionHeaders->NoHeaders.getValueOr(false) ||
536 !Doc.SectionHeaders->Sections);
537 size_t FirstExcluded =
538 Doc.SectionHeaders->Sections ? Doc.SectionHeaders->Sections->size() : 0;
539 if (Index >= FirstExcluded) {
540 if (LocSym.empty())
541 reportError("unable to link '" + LocSec + "' to excluded section '" + S +
542 "'");
543 else
544 reportError("excluded section referenced: '" + S + "' by symbol '" +
545 LocSym + "'");
546 }
547 return Index;
548 }
549
550 template <class ELFT>
toSymbolIndex(StringRef S,StringRef LocSec,bool IsDynamic)551 unsigned ELFState<ELFT>::toSymbolIndex(StringRef S, StringRef LocSec,
552 bool IsDynamic) {
553 const NameToIdxMap &SymMap = IsDynamic ? DynSymN2I : SymN2I;
554 unsigned Index;
555 // Here we try to look up S in the symbol table. If it is not there,
556 // treat its value as a symbol index.
557 if (!SymMap.lookup(S, Index) && !to_integer(S, Index)) {
558 reportError("unknown symbol referenced: '" + S + "' by YAML section '" +
559 LocSec + "'");
560 return 0;
561 }
562 return Index;
563 }
564
565 template <class ELFT>
overrideFields(ELFYAML::Section * From,typename ELFT::Shdr & To)566 static void overrideFields(ELFYAML::Section *From, typename ELFT::Shdr &To) {
567 if (!From)
568 return;
569 if (From->ShAddrAlign)
570 To.sh_addralign = *From->ShAddrAlign;
571 if (From->ShFlags)
572 To.sh_flags = *From->ShFlags;
573 if (From->ShName)
574 To.sh_name = *From->ShName;
575 if (From->ShOffset)
576 To.sh_offset = *From->ShOffset;
577 if (From->ShSize)
578 To.sh_size = *From->ShSize;
579 if (From->ShType)
580 To.sh_type = *From->ShType;
581 }
582
583 template <class ELFT>
initImplicitHeader(ContiguousBlobAccumulator & CBA,Elf_Shdr & Header,StringRef SecName,ELFYAML::Section * YAMLSec)584 bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
585 Elf_Shdr &Header, StringRef SecName,
586 ELFYAML::Section *YAMLSec) {
587 // Check if the header was already initialized.
588 if (Header.sh_offset)
589 return false;
590
591 if (SecName == ".symtab")
592 initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec);
593 else if (SecName == ".strtab")
594 initStrtabSectionHeader(Header, SecName, DotStrtab, CBA, YAMLSec);
595 else if (SecName == ".shstrtab")
596 initStrtabSectionHeader(Header, SecName, DotShStrtab, CBA, YAMLSec);
597 else if (SecName == ".dynsym")
598 initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
599 else if (SecName == ".dynstr")
600 initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec);
601 else if (SecName.startswith(".debug_")) {
602 // If a ".debug_*" section's type is a preserved one, e.g., SHT_DYNAMIC, we
603 // will not treat it as a debug section.
604 if (YAMLSec && !isa<ELFYAML::RawContentSection>(YAMLSec))
605 return false;
606 initDWARFSectionHeader(Header, SecName, CBA, YAMLSec);
607 } else
608 return false;
609
610 LocationCounter += Header.sh_size;
611
612 // Override section fields if requested.
613 overrideFields<ELFT>(YAMLSec, Header);
614 return true;
615 }
616
617 constexpr char SuffixStart = '(';
618 constexpr char SuffixEnd = ')';
619
appendUniqueSuffix(StringRef Name,const Twine & Msg)620 std::string llvm::ELFYAML::appendUniqueSuffix(StringRef Name,
621 const Twine &Msg) {
622 // Do not add a space when a Name is empty.
623 std::string Ret = Name.empty() ? "" : Name.str() + ' ';
624 return Ret + (Twine(SuffixStart) + Msg + Twine(SuffixEnd)).str();
625 }
626
dropUniqueSuffix(StringRef S)627 StringRef llvm::ELFYAML::dropUniqueSuffix(StringRef S) {
628 if (S.empty() || S.back() != SuffixEnd)
629 return S;
630
631 // A special case for empty names. See appendUniqueSuffix() above.
632 size_t SuffixPos = S.rfind(SuffixStart);
633 if (SuffixPos == 0)
634 return "";
635
636 if (SuffixPos == StringRef::npos || S[SuffixPos - 1] != ' ')
637 return S;
638 return S.substr(0, SuffixPos - 1);
639 }
640
641 template <class ELFT>
getSectionNameOffset(StringRef Name)642 uint64_t ELFState<ELFT>::getSectionNameOffset(StringRef Name) {
643 // If a section is excluded from section headers, we do not save its name in
644 // the string table.
645 if (ExcludedSectionHeaders.count(Name))
646 return 0;
647 return DotShStrtab.getOffset(Name);
648 }
649
writeContent(ContiguousBlobAccumulator & CBA,const Optional<yaml::BinaryRef> & Content,const Optional<llvm::yaml::Hex64> & Size)650 static uint64_t writeContent(ContiguousBlobAccumulator &CBA,
651 const Optional<yaml::BinaryRef> &Content,
652 const Optional<llvm::yaml::Hex64> &Size) {
653 size_t ContentSize = 0;
654 if (Content) {
655 CBA.writeAsBinary(*Content);
656 ContentSize = Content->binary_size();
657 }
658
659 if (!Size)
660 return ContentSize;
661
662 CBA.writeZeros(*Size - ContentSize);
663 return *Size;
664 }
665
666 template <class ELFT>
initSectionHeaders(std::vector<Elf_Shdr> & SHeaders,ContiguousBlobAccumulator & CBA)667 void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
668 ContiguousBlobAccumulator &CBA) {
669 // Ensure SHN_UNDEF entry is present. An all-zero section header is a
670 // valid SHN_UNDEF entry since SHT_NULL == 0.
671 SHeaders.resize(Doc.getSections().size());
672
673 for (const std::unique_ptr<ELFYAML::Chunk> &D : Doc.Chunks) {
674 if (ELFYAML::Fill *S = dyn_cast<ELFYAML::Fill>(D.get())) {
675 S->Offset = alignToOffset(CBA, /*Align=*/1, S->Offset);
676 writeFill(*S, CBA);
677 LocationCounter += S->Size;
678 continue;
679 }
680
681 ELFYAML::Section *Sec = cast<ELFYAML::Section>(D.get());
682 bool IsFirstUndefSection = D == Doc.Chunks.front();
683 if (IsFirstUndefSection && Sec->IsImplicit)
684 continue;
685
686 // We have a few sections like string or symbol tables that are usually
687 // added implicitly to the end. However, if they are explicitly specified
688 // in the YAML, we need to write them here. This ensures the file offset
689 // remains correct.
690 Elf_Shdr &SHeader = SHeaders[SN2I.get(Sec->Name)];
691 if (initImplicitHeader(CBA, SHeader, Sec->Name,
692 Sec->IsImplicit ? nullptr : Sec))
693 continue;
694
695 assert(Sec && "It can't be null unless it is an implicit section. But all "
696 "implicit sections should already have been handled above.");
697
698 SHeader.sh_name =
699 getSectionNameOffset(ELFYAML::dropUniqueSuffix(Sec->Name));
700 SHeader.sh_type = Sec->Type;
701 if (Sec->Flags)
702 SHeader.sh_flags = *Sec->Flags;
703 SHeader.sh_addralign = Sec->AddressAlign;
704
705 // Set the offset for all sections, except the SHN_UNDEF section with index
706 // 0 when not explicitly requested.
707 if (!IsFirstUndefSection || Sec->Offset)
708 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, Sec->Offset);
709
710 assignSectionAddress(SHeader, Sec);
711
712 if (Sec->Link)
713 SHeader.sh_link = toSectionIndex(*Sec->Link, Sec->Name);
714
715 if (IsFirstUndefSection) {
716 if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
717 // We do not write any content for special SHN_UNDEF section.
718 if (RawSec->Size)
719 SHeader.sh_size = *RawSec->Size;
720 if (RawSec->Info)
721 SHeader.sh_info = *RawSec->Info;
722 }
723 if (Sec->EntSize)
724 SHeader.sh_entsize = *Sec->EntSize;
725
726 LocationCounter += SHeader.sh_size;
727 overrideFields<ELFT>(Sec, SHeader);
728 continue;
729 }
730
731 if (!isa<ELFYAML::NoBitsSection>(Sec) && (Sec->Content || Sec->Size))
732 SHeader.sh_size = writeContent(CBA, Sec->Content, Sec->Size);
733
734 if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
735 writeSectionContent(SHeader, *S, CBA);
736 } else if (auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) {
737 writeSectionContent(SHeader, *S, CBA);
738 } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
739 writeSectionContent(SHeader, *S, CBA);
740 } else if (auto S = dyn_cast<ELFYAML::RelrSection>(Sec)) {
741 writeSectionContent(SHeader, *S, CBA);
742 } else if (auto S = dyn_cast<ELFYAML::GroupSection>(Sec)) {
743 writeSectionContent(SHeader, *S, CBA);
744 } else if (auto S = dyn_cast<ELFYAML::ARMIndexTableSection>(Sec)) {
745 writeSectionContent(SHeader, *S, CBA);
746 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
747 writeSectionContent(SHeader, *S, CBA);
748 } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
749 writeSectionContent(SHeader, *S, CBA);
750 } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
751 writeSectionContent(SHeader, *S, CBA);
752 } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
753 writeSectionContent(SHeader, *S, CBA);
754 } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
755 writeSectionContent(SHeader, *S, CBA);
756 } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
757 writeSectionContent(SHeader, *S, CBA);
758 } else if (auto S = dyn_cast<ELFYAML::StackSizesSection>(Sec)) {
759 writeSectionContent(SHeader, *S, CBA);
760 } else if (auto S = dyn_cast<ELFYAML::HashSection>(Sec)) {
761 writeSectionContent(SHeader, *S, CBA);
762 } else if (auto S = dyn_cast<ELFYAML::AddrsigSection>(Sec)) {
763 writeSectionContent(SHeader, *S, CBA);
764 } else if (auto S = dyn_cast<ELFYAML::LinkerOptionsSection>(Sec)) {
765 writeSectionContent(SHeader, *S, CBA);
766 } else if (auto S = dyn_cast<ELFYAML::NoteSection>(Sec)) {
767 writeSectionContent(SHeader, *S, CBA);
768 } else if (auto S = dyn_cast<ELFYAML::GnuHashSection>(Sec)) {
769 writeSectionContent(SHeader, *S, CBA);
770 } else if (auto S = dyn_cast<ELFYAML::DependentLibrariesSection>(Sec)) {
771 writeSectionContent(SHeader, *S, CBA);
772 } else if (auto S = dyn_cast<ELFYAML::CallGraphProfileSection>(Sec)) {
773 writeSectionContent(SHeader, *S, CBA);
774 } else if (auto S = dyn_cast<ELFYAML::BBAddrMapSection>(Sec)) {
775 writeSectionContent(SHeader, *S, CBA);
776 } else {
777 llvm_unreachable("Unknown section type");
778 }
779
780 LocationCounter += SHeader.sh_size;
781
782 // Override section fields if requested.
783 overrideFields<ELFT>(Sec, SHeader);
784 }
785 }
786
787 template <class ELFT>
assignSectionAddress(Elf_Shdr & SHeader,ELFYAML::Section * YAMLSec)788 void ELFState<ELFT>::assignSectionAddress(Elf_Shdr &SHeader,
789 ELFYAML::Section *YAMLSec) {
790 if (YAMLSec && YAMLSec->Address) {
791 SHeader.sh_addr = *YAMLSec->Address;
792 LocationCounter = *YAMLSec->Address;
793 return;
794 }
795
796 // sh_addr represents the address in the memory image of a process. Sections
797 // in a relocatable object file or non-allocatable sections do not need
798 // sh_addr assignment.
799 if (Doc.Header.Type.value == ELF::ET_REL ||
800 !(SHeader.sh_flags & ELF::SHF_ALLOC))
801 return;
802
803 LocationCounter =
804 alignTo(LocationCounter, SHeader.sh_addralign ? SHeader.sh_addralign : 1);
805 SHeader.sh_addr = LocationCounter;
806 }
807
findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols)808 static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
809 for (size_t I = 0; I < Symbols.size(); ++I)
810 if (Symbols[I].Binding.value != ELF::STB_LOCAL)
811 return I;
812 return Symbols.size();
813 }
814
815 template <class ELFT>
816 std::vector<typename ELFT::Sym>
toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,const StringTableBuilder & Strtab)817 ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
818 const StringTableBuilder &Strtab) {
819 std::vector<Elf_Sym> Ret;
820 Ret.resize(Symbols.size() + 1);
821
822 size_t I = 0;
823 for (const ELFYAML::Symbol &Sym : Symbols) {
824 Elf_Sym &Symbol = Ret[++I];
825
826 // If NameIndex, which contains the name offset, is explicitly specified, we
827 // use it. This is useful for preparing broken objects. Otherwise, we add
828 // the specified Name to the string table builder to get its offset.
829 if (Sym.StName)
830 Symbol.st_name = *Sym.StName;
831 else if (!Sym.Name.empty())
832 Symbol.st_name = Strtab.getOffset(ELFYAML::dropUniqueSuffix(Sym.Name));
833
834 Symbol.setBindingAndType(Sym.Binding, Sym.Type);
835 if (Sym.Section)
836 Symbol.st_shndx = toSectionIndex(*Sym.Section, "", Sym.Name);
837 else if (Sym.Index)
838 Symbol.st_shndx = *Sym.Index;
839
840 Symbol.st_value = Sym.Value;
841 Symbol.st_other = Sym.Other ? *Sym.Other : 0;
842 Symbol.st_size = Sym.Size;
843 }
844
845 return Ret;
846 }
847
848 template <class ELFT>
initSymtabSectionHeader(Elf_Shdr & SHeader,SymtabType STType,ContiguousBlobAccumulator & CBA,ELFYAML::Section * YAMLSec)849 void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
850 SymtabType STType,
851 ContiguousBlobAccumulator &CBA,
852 ELFYAML::Section *YAMLSec) {
853
854 bool IsStatic = STType == SymtabType::Static;
855 ArrayRef<ELFYAML::Symbol> Symbols;
856 if (IsStatic && Doc.Symbols)
857 Symbols = *Doc.Symbols;
858 else if (!IsStatic && Doc.DynamicSymbols)
859 Symbols = *Doc.DynamicSymbols;
860
861 ELFYAML::RawContentSection *RawSec =
862 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
863 if (RawSec && (RawSec->Content || RawSec->Size)) {
864 bool HasSymbolsDescription =
865 (IsStatic && Doc.Symbols) || (!IsStatic && Doc.DynamicSymbols);
866 if (HasSymbolsDescription) {
867 StringRef Property = (IsStatic ? "`Symbols`" : "`DynamicSymbols`");
868 if (RawSec->Content)
869 reportError("cannot specify both `Content` and " + Property +
870 " for symbol table section '" + RawSec->Name + "'");
871 if (RawSec->Size)
872 reportError("cannot specify both `Size` and " + Property +
873 " for symbol table section '" + RawSec->Name + "'");
874 return;
875 }
876 }
877
878 zero(SHeader);
879 SHeader.sh_name = getSectionNameOffset(IsStatic ? ".symtab" : ".dynsym");
880
881 if (YAMLSec)
882 SHeader.sh_type = YAMLSec->Type;
883 else
884 SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
885
886 if (RawSec && RawSec->Link) {
887 // If the Link field is explicitly defined in the document,
888 // we should use it.
889 SHeader.sh_link = toSectionIndex(*RawSec->Link, RawSec->Name);
890 } else {
891 // When we describe the .dynsym section in the document explicitly, it is
892 // allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not
893 // added implicitly and we should be able to leave the Link zeroed if
894 // .dynstr is not defined.
895 unsigned Link = 0;
896 if (IsStatic) {
897 if (!ExcludedSectionHeaders.count(".strtab"))
898 Link = SN2I.get(".strtab");
899 } else {
900 if (!ExcludedSectionHeaders.count(".dynstr"))
901 SN2I.lookup(".dynstr", Link);
902 }
903 SHeader.sh_link = Link;
904 }
905
906 if (YAMLSec && YAMLSec->Flags)
907 SHeader.sh_flags = *YAMLSec->Flags;
908 else if (!IsStatic)
909 SHeader.sh_flags = ELF::SHF_ALLOC;
910
911 // If the symbol table section is explicitly described in the YAML
912 // then we should set the fields requested.
913 SHeader.sh_info = (RawSec && RawSec->Info) ? (unsigned)(*RawSec->Info)
914 : findFirstNonGlobal(Symbols) + 1;
915 SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize)
916 ? (uint64_t)(*YAMLSec->EntSize)
917 : sizeof(Elf_Sym);
918 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8;
919
920 assignSectionAddress(SHeader, YAMLSec);
921
922 SHeader.sh_offset =
923 alignToOffset(CBA, SHeader.sh_addralign, RawSec ? RawSec->Offset : None);
924
925 if (RawSec && (RawSec->Content || RawSec->Size)) {
926 assert(Symbols.empty());
927 SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
928 return;
929 }
930
931 std::vector<Elf_Sym> Syms =
932 toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr);
933 SHeader.sh_size = Syms.size() * sizeof(Elf_Sym);
934 CBA.write((const char *)Syms.data(), SHeader.sh_size);
935 }
936
937 template <class ELFT>
initStrtabSectionHeader(Elf_Shdr & SHeader,StringRef Name,StringTableBuilder & STB,ContiguousBlobAccumulator & CBA,ELFYAML::Section * YAMLSec)938 void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
939 StringTableBuilder &STB,
940 ContiguousBlobAccumulator &CBA,
941 ELFYAML::Section *YAMLSec) {
942 zero(SHeader);
943 SHeader.sh_name = getSectionNameOffset(Name);
944 SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
945 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
946
947 ELFYAML::RawContentSection *RawSec =
948 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
949
950 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
951 YAMLSec ? YAMLSec->Offset : None);
952
953 if (RawSec && (RawSec->Content || RawSec->Size)) {
954 SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
955 } else {
956 if (raw_ostream *OS = CBA.getRawOS(STB.getSize()))
957 STB.write(*OS);
958 SHeader.sh_size = STB.getSize();
959 }
960
961 if (YAMLSec && YAMLSec->EntSize)
962 SHeader.sh_entsize = *YAMLSec->EntSize;
963
964 if (RawSec && RawSec->Info)
965 SHeader.sh_info = *RawSec->Info;
966
967 if (YAMLSec && YAMLSec->Flags)
968 SHeader.sh_flags = *YAMLSec->Flags;
969 else if (Name == ".dynstr")
970 SHeader.sh_flags = ELF::SHF_ALLOC;
971
972 assignSectionAddress(SHeader, YAMLSec);
973 }
974
shouldEmitDWARF(DWARFYAML::Data & DWARF,StringRef Name)975 static bool shouldEmitDWARF(DWARFYAML::Data &DWARF, StringRef Name) {
976 SetVector<StringRef> DebugSecNames = DWARF.getNonEmptySectionNames();
977 return Name.consume_front(".") && DebugSecNames.count(Name);
978 }
979
980 template <class ELFT>
emitDWARF(typename ELFT::Shdr & SHeader,StringRef Name,const DWARFYAML::Data & DWARF,ContiguousBlobAccumulator & CBA)981 Expected<uint64_t> emitDWARF(typename ELFT::Shdr &SHeader, StringRef Name,
982 const DWARFYAML::Data &DWARF,
983 ContiguousBlobAccumulator &CBA) {
984 // We are unable to predict the size of debug data, so we request to write 0
985 // bytes. This should always return us an output stream unless CBA is already
986 // in an error state.
987 raw_ostream *OS = CBA.getRawOS(0);
988 if (!OS)
989 return 0;
990
991 uint64_t BeginOffset = CBA.tell();
992
993 auto EmitFunc = DWARFYAML::getDWARFEmitterByName(Name.substr(1));
994 if (Error Err = EmitFunc(*OS, DWARF))
995 return std::move(Err);
996
997 return CBA.tell() - BeginOffset;
998 }
999
1000 template <class ELFT>
initDWARFSectionHeader(Elf_Shdr & SHeader,StringRef Name,ContiguousBlobAccumulator & CBA,ELFYAML::Section * YAMLSec)1001 void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
1002 ContiguousBlobAccumulator &CBA,
1003 ELFYAML::Section *YAMLSec) {
1004 zero(SHeader);
1005 SHeader.sh_name = getSectionNameOffset(ELFYAML::dropUniqueSuffix(Name));
1006 SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_PROGBITS;
1007 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
1008 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1009 YAMLSec ? YAMLSec->Offset : None);
1010
1011 ELFYAML::RawContentSection *RawSec =
1012 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
1013 if (Doc.DWARF && shouldEmitDWARF(*Doc.DWARF, Name)) {
1014 if (RawSec && (RawSec->Content || RawSec->Size))
1015 reportError("cannot specify section '" + Name +
1016 "' contents in the 'DWARF' entry and the 'Content' "
1017 "or 'Size' in the 'Sections' entry at the same time");
1018 else {
1019 if (Expected<uint64_t> ShSizeOrErr =
1020 emitDWARF<ELFT>(SHeader, Name, *Doc.DWARF, CBA))
1021 SHeader.sh_size = *ShSizeOrErr;
1022 else
1023 reportError(ShSizeOrErr.takeError());
1024 }
1025 } else if (RawSec)
1026 SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
1027 else
1028 llvm_unreachable("debug sections can only be initialized via the 'DWARF' "
1029 "entry or a RawContentSection");
1030
1031 if (YAMLSec && YAMLSec->EntSize)
1032 SHeader.sh_entsize = *YAMLSec->EntSize;
1033 else if (Name == ".debug_str")
1034 SHeader.sh_entsize = 1;
1035
1036 if (RawSec && RawSec->Info)
1037 SHeader.sh_info = *RawSec->Info;
1038
1039 if (YAMLSec && YAMLSec->Flags)
1040 SHeader.sh_flags = *YAMLSec->Flags;
1041 else if (Name == ".debug_str")
1042 SHeader.sh_flags = ELF::SHF_MERGE | ELF::SHF_STRINGS;
1043
1044 if (YAMLSec && YAMLSec->Link)
1045 SHeader.sh_link = toSectionIndex(*YAMLSec->Link, Name);
1046
1047 assignSectionAddress(SHeader, YAMLSec);
1048 }
1049
reportError(const Twine & Msg)1050 template <class ELFT> void ELFState<ELFT>::reportError(const Twine &Msg) {
1051 ErrHandler(Msg);
1052 HasError = true;
1053 }
1054
reportError(Error Err)1055 template <class ELFT> void ELFState<ELFT>::reportError(Error Err) {
1056 handleAllErrors(std::move(Err), [&](const ErrorInfoBase &Err) {
1057 reportError(Err.message());
1058 });
1059 }
1060
1061 template <class ELFT>
1062 std::vector<Fragment>
getPhdrFragments(const ELFYAML::ProgramHeader & Phdr,ArrayRef<Elf_Shdr> SHeaders)1063 ELFState<ELFT>::getPhdrFragments(const ELFYAML::ProgramHeader &Phdr,
1064 ArrayRef<Elf_Shdr> SHeaders) {
1065 std::vector<Fragment> Ret;
1066 for (const ELFYAML::Chunk *C : Phdr.Chunks) {
1067 if (const ELFYAML::Fill *F = dyn_cast<ELFYAML::Fill>(C)) {
1068 Ret.push_back({*F->Offset, F->Size, llvm::ELF::SHT_PROGBITS,
1069 /*ShAddrAlign=*/1});
1070 continue;
1071 }
1072
1073 const ELFYAML::Section *S = cast<ELFYAML::Section>(C);
1074 const Elf_Shdr &H = SHeaders[SN2I.get(S->Name)];
1075 Ret.push_back({H.sh_offset, H.sh_size, H.sh_type, H.sh_addralign});
1076 }
1077 return Ret;
1078 }
1079
1080 template <class ELFT>
setProgramHeaderLayout(std::vector<Elf_Phdr> & PHeaders,std::vector<Elf_Shdr> & SHeaders)1081 void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
1082 std::vector<Elf_Shdr> &SHeaders) {
1083 uint32_t PhdrIdx = 0;
1084 for (auto &YamlPhdr : Doc.ProgramHeaders) {
1085 Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
1086 std::vector<Fragment> Fragments = getPhdrFragments(YamlPhdr, SHeaders);
1087 if (!llvm::is_sorted(Fragments, [](const Fragment &A, const Fragment &B) {
1088 return A.Offset < B.Offset;
1089 }))
1090 reportError("sections in the program header with index " +
1091 Twine(PhdrIdx) + " are not sorted by their file offset");
1092
1093 if (YamlPhdr.Offset) {
1094 if (!Fragments.empty() && *YamlPhdr.Offset > Fragments.front().Offset)
1095 reportError("'Offset' for segment with index " + Twine(PhdrIdx) +
1096 " must be less than or equal to the minimum file offset of "
1097 "all included sections (0x" +
1098 Twine::utohexstr(Fragments.front().Offset) + ")");
1099 PHeader.p_offset = *YamlPhdr.Offset;
1100 } else if (!Fragments.empty()) {
1101 PHeader.p_offset = Fragments.front().Offset;
1102 }
1103
1104 // Set the file size if not set explicitly.
1105 if (YamlPhdr.FileSize) {
1106 PHeader.p_filesz = *YamlPhdr.FileSize;
1107 } else if (!Fragments.empty()) {
1108 uint64_t FileSize = Fragments.back().Offset - PHeader.p_offset;
1109 // SHT_NOBITS sections occupy no physical space in a file, we should not
1110 // take their sizes into account when calculating the file size of a
1111 // segment.
1112 if (Fragments.back().Type != llvm::ELF::SHT_NOBITS)
1113 FileSize += Fragments.back().Size;
1114 PHeader.p_filesz = FileSize;
1115 }
1116
1117 // Find the maximum offset of the end of a section in order to set p_memsz.
1118 uint64_t MemOffset = PHeader.p_offset;
1119 for (const Fragment &F : Fragments)
1120 MemOffset = std::max(MemOffset, F.Offset + F.Size);
1121 // Set the memory size if not set explicitly.
1122 PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize)
1123 : MemOffset - PHeader.p_offset;
1124
1125 if (YamlPhdr.Align) {
1126 PHeader.p_align = *YamlPhdr.Align;
1127 } else {
1128 // Set the alignment of the segment to be the maximum alignment of the
1129 // sections so that by default the segment has a valid and sensible
1130 // alignment.
1131 PHeader.p_align = 1;
1132 for (const Fragment &F : Fragments)
1133 PHeader.p_align = std::max((uint64_t)PHeader.p_align, F.AddrAlign);
1134 }
1135 }
1136 }
1137
shouldAllocateFileSpace(ArrayRef<ELFYAML::ProgramHeader> Phdrs,const ELFYAML::NoBitsSection & S)1138 bool llvm::ELFYAML::shouldAllocateFileSpace(
1139 ArrayRef<ELFYAML::ProgramHeader> Phdrs, const ELFYAML::NoBitsSection &S) {
1140 for (const ELFYAML::ProgramHeader &PH : Phdrs) {
1141 auto It = llvm::find_if(
1142 PH.Chunks, [&](ELFYAML::Chunk *C) { return C->Name == S.Name; });
1143 if (std::any_of(It, PH.Chunks.end(), [](ELFYAML::Chunk *C) {
1144 return (isa<ELFYAML::Fill>(C) ||
1145 cast<ELFYAML::Section>(C)->Type != ELF::SHT_NOBITS);
1146 }))
1147 return true;
1148 }
1149 return false;
1150 }
1151
1152 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::NoBitsSection & S,ContiguousBlobAccumulator & CBA)1153 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1154 const ELFYAML::NoBitsSection &S,
1155 ContiguousBlobAccumulator &CBA) {
1156 if (!S.Size)
1157 return;
1158
1159 SHeader.sh_size = *S.Size;
1160
1161 // When a nobits section is followed by a non-nobits section or fill
1162 // in the same segment, we allocate the file space for it. This behavior
1163 // matches linkers.
1164 if (shouldAllocateFileSpace(Doc.ProgramHeaders, S))
1165 CBA.writeZeros(*S.Size);
1166 }
1167
1168 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::RawContentSection & Section,ContiguousBlobAccumulator & CBA)1169 void ELFState<ELFT>::writeSectionContent(
1170 Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
1171 ContiguousBlobAccumulator &CBA) {
1172 if (Section.EntSize)
1173 SHeader.sh_entsize = *Section.EntSize;
1174
1175 if (Section.Info)
1176 SHeader.sh_info = *Section.Info;
1177 }
1178
isMips64EL(const ELFYAML::Object & Obj)1179 static bool isMips64EL(const ELFYAML::Object &Obj) {
1180 return Obj.getMachine() == llvm::ELF::EM_MIPS &&
1181 Obj.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
1182 Obj.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1183 }
1184
1185 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::RelocationSection & Section,ContiguousBlobAccumulator & CBA)1186 void ELFState<ELFT>::writeSectionContent(
1187 Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section,
1188 ContiguousBlobAccumulator &CBA) {
1189 assert((Section.Type == llvm::ELF::SHT_REL ||
1190 Section.Type == llvm::ELF::SHT_RELA) &&
1191 "Section type is not SHT_REL nor SHT_RELA");
1192
1193 bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
1194 if (Section.EntSize)
1195 SHeader.sh_entsize = *Section.EntSize;
1196 else
1197 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1198
1199 // For relocation section set link to .symtab by default.
1200 unsigned Link = 0;
1201 if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
1202 SN2I.lookup(".symtab", Link))
1203 SHeader.sh_link = Link;
1204
1205 if (!Section.RelocatableSec.empty())
1206 SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name);
1207
1208 if (!Section.Relocations)
1209 return;
1210
1211 for (const ELFYAML::Relocation &Rel : *Section.Relocations) {
1212 const bool IsDynamic = Section.Link && (*Section.Link == ".dynsym");
1213 unsigned SymIdx =
1214 Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name, IsDynamic) : 0;
1215 if (IsRela) {
1216 Elf_Rela REntry;
1217 zero(REntry);
1218 REntry.r_offset = Rel.Offset;
1219 REntry.r_addend = Rel.Addend;
1220 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
1221 CBA.write((const char *)&REntry, sizeof(REntry));
1222 } else {
1223 Elf_Rel REntry;
1224 zero(REntry);
1225 REntry.r_offset = Rel.Offset;
1226 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
1227 CBA.write((const char *)&REntry, sizeof(REntry));
1228 }
1229 }
1230
1231 SHeader.sh_size = (IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)) *
1232 Section.Relocations->size();
1233 }
1234
1235 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::RelrSection & Section,ContiguousBlobAccumulator & CBA)1236 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1237 const ELFYAML::RelrSection &Section,
1238 ContiguousBlobAccumulator &CBA) {
1239 SHeader.sh_entsize =
1240 Section.EntSize ? uint64_t(*Section.EntSize) : sizeof(Elf_Relr);
1241
1242 if (!Section.Entries)
1243 return;
1244
1245 for (llvm::yaml::Hex64 E : *Section.Entries) {
1246 if (!ELFT::Is64Bits && E > UINT32_MAX)
1247 reportError(Section.Name + ": the value is too large for 32-bits: 0x" +
1248 Twine::utohexstr(E));
1249 CBA.write<uintX_t>(E, ELFT::TargetEndianness);
1250 }
1251
1252 SHeader.sh_size = sizeof(uintX_t) * Section.Entries->size();
1253 }
1254
1255 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::SymtabShndxSection & Shndx,ContiguousBlobAccumulator & CBA)1256 void ELFState<ELFT>::writeSectionContent(
1257 Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx,
1258 ContiguousBlobAccumulator &CBA) {
1259 SHeader.sh_entsize = Shndx.EntSize ? (uint64_t)*Shndx.EntSize : 4;
1260
1261 if (Shndx.Content || Shndx.Size) {
1262 SHeader.sh_size = writeContent(CBA, Shndx.Content, Shndx.Size);
1263 return;
1264 }
1265
1266 if (!Shndx.Entries)
1267 return;
1268
1269 for (uint32_t E : *Shndx.Entries)
1270 CBA.write<uint32_t>(E, ELFT::TargetEndianness);
1271 SHeader.sh_size = Shndx.Entries->size() * SHeader.sh_entsize;
1272 }
1273
1274 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::GroupSection & Section,ContiguousBlobAccumulator & CBA)1275 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1276 const ELFYAML::GroupSection &Section,
1277 ContiguousBlobAccumulator &CBA) {
1278 assert(Section.Type == llvm::ELF::SHT_GROUP &&
1279 "Section type is not SHT_GROUP");
1280
1281 unsigned Link = 0;
1282 if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
1283 SN2I.lookup(".symtab", Link))
1284 SHeader.sh_link = Link;
1285
1286 SHeader.sh_entsize = 4;
1287
1288 if (Section.Signature)
1289 SHeader.sh_info =
1290 toSymbolIndex(*Section.Signature, Section.Name, /*IsDynamic=*/false);
1291
1292 if (!Section.Members)
1293 return;
1294
1295 for (const ELFYAML::SectionOrType &Member : *Section.Members) {
1296 unsigned int SectionIndex = 0;
1297 if (Member.sectionNameOrType == "GRP_COMDAT")
1298 SectionIndex = llvm::ELF::GRP_COMDAT;
1299 else
1300 SectionIndex = toSectionIndex(Member.sectionNameOrType, Section.Name);
1301 CBA.write<uint32_t>(SectionIndex, ELFT::TargetEndianness);
1302 }
1303 SHeader.sh_size = SHeader.sh_entsize * Section.Members->size();
1304 }
1305
1306 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::SymverSection & Section,ContiguousBlobAccumulator & CBA)1307 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1308 const ELFYAML::SymverSection &Section,
1309 ContiguousBlobAccumulator &CBA) {
1310 SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2;
1311
1312 if (!Section.Entries)
1313 return;
1314
1315 for (uint16_t Version : *Section.Entries)
1316 CBA.write<uint16_t>(Version, ELFT::TargetEndianness);
1317 SHeader.sh_size = Section.Entries->size() * SHeader.sh_entsize;
1318 }
1319
1320 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::StackSizesSection & Section,ContiguousBlobAccumulator & CBA)1321 void ELFState<ELFT>::writeSectionContent(
1322 Elf_Shdr &SHeader, const ELFYAML::StackSizesSection &Section,
1323 ContiguousBlobAccumulator &CBA) {
1324 if (!Section.Entries)
1325 return;
1326
1327 if (!Section.Entries)
1328 return;
1329
1330 for (const ELFYAML::StackSizeEntry &E : *Section.Entries) {
1331 CBA.write<uintX_t>(E.Address, ELFT::TargetEndianness);
1332 SHeader.sh_size += sizeof(uintX_t) + CBA.writeULEB128(E.Size);
1333 }
1334 }
1335
1336 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::BBAddrMapSection & Section,ContiguousBlobAccumulator & CBA)1337 void ELFState<ELFT>::writeSectionContent(
1338 Elf_Shdr &SHeader, const ELFYAML::BBAddrMapSection &Section,
1339 ContiguousBlobAccumulator &CBA) {
1340 if (!Section.Entries)
1341 return;
1342
1343 for (const ELFYAML::BBAddrMapEntry &E : *Section.Entries) {
1344 // Write the address of the function.
1345 CBA.write<uintX_t>(E.Address, ELFT::TargetEndianness);
1346 // Write number of BBEntries (number of basic blocks in the function).
1347 size_t NumBlocks = E.BBEntries ? E.BBEntries->size() : 0;
1348 SHeader.sh_size += sizeof(uintX_t) + CBA.writeULEB128(NumBlocks);
1349 if (!NumBlocks)
1350 continue;
1351 // Write all BBEntries.
1352 for (const ELFYAML::BBAddrMapEntry::BBEntry &BBE : *E.BBEntries)
1353 SHeader.sh_size += CBA.writeULEB128(BBE.AddressOffset) +
1354 CBA.writeULEB128(BBE.Size) +
1355 CBA.writeULEB128(BBE.Metadata);
1356 }
1357 }
1358
1359 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::LinkerOptionsSection & Section,ContiguousBlobAccumulator & CBA)1360 void ELFState<ELFT>::writeSectionContent(
1361 Elf_Shdr &SHeader, const ELFYAML::LinkerOptionsSection &Section,
1362 ContiguousBlobAccumulator &CBA) {
1363 if (!Section.Options)
1364 return;
1365
1366 for (const ELFYAML::LinkerOption &LO : *Section.Options) {
1367 CBA.write(LO.Key.data(), LO.Key.size());
1368 CBA.write('\0');
1369 CBA.write(LO.Value.data(), LO.Value.size());
1370 CBA.write('\0');
1371 SHeader.sh_size += (LO.Key.size() + LO.Value.size() + 2);
1372 }
1373 }
1374
1375 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::DependentLibrariesSection & Section,ContiguousBlobAccumulator & CBA)1376 void ELFState<ELFT>::writeSectionContent(
1377 Elf_Shdr &SHeader, const ELFYAML::DependentLibrariesSection &Section,
1378 ContiguousBlobAccumulator &CBA) {
1379 if (!Section.Libs)
1380 return;
1381
1382 for (StringRef Lib : *Section.Libs) {
1383 CBA.write(Lib.data(), Lib.size());
1384 CBA.write('\0');
1385 SHeader.sh_size += Lib.size() + 1;
1386 }
1387 }
1388
1389 template <class ELFT>
1390 uint64_t
alignToOffset(ContiguousBlobAccumulator & CBA,uint64_t Align,llvm::Optional<llvm::yaml::Hex64> Offset)1391 ELFState<ELFT>::alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align,
1392 llvm::Optional<llvm::yaml::Hex64> Offset) {
1393 uint64_t CurrentOffset = CBA.getOffset();
1394 uint64_t AlignedOffset;
1395
1396 if (Offset) {
1397 if ((uint64_t)*Offset < CurrentOffset) {
1398 reportError("the 'Offset' value (0x" +
1399 Twine::utohexstr((uint64_t)*Offset) + ") goes backward");
1400 return CurrentOffset;
1401 }
1402
1403 // We ignore an alignment when an explicit offset has been requested.
1404 AlignedOffset = *Offset;
1405 } else {
1406 AlignedOffset = alignTo(CurrentOffset, std::max(Align, (uint64_t)1));
1407 }
1408
1409 CBA.writeZeros(AlignedOffset - CurrentOffset);
1410 return AlignedOffset;
1411 }
1412
1413 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::CallGraphProfileSection & Section,ContiguousBlobAccumulator & CBA)1414 void ELFState<ELFT>::writeSectionContent(
1415 Elf_Shdr &SHeader, const ELFYAML::CallGraphProfileSection &Section,
1416 ContiguousBlobAccumulator &CBA) {
1417 if (Section.EntSize)
1418 SHeader.sh_entsize = *Section.EntSize;
1419 else
1420 SHeader.sh_entsize = 16;
1421
1422 unsigned Link = 0;
1423 if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
1424 SN2I.lookup(".symtab", Link))
1425 SHeader.sh_link = Link;
1426
1427 if (!Section.Entries)
1428 return;
1429
1430 for (const ELFYAML::CallGraphEntry &E : *Section.Entries) {
1431 unsigned From = toSymbolIndex(E.From, Section.Name, /*IsDynamic=*/false);
1432 unsigned To = toSymbolIndex(E.To, Section.Name, /*IsDynamic=*/false);
1433
1434 CBA.write<uint32_t>(From, ELFT::TargetEndianness);
1435 CBA.write<uint32_t>(To, ELFT::TargetEndianness);
1436 CBA.write<uint64_t>(E.Weight, ELFT::TargetEndianness);
1437 SHeader.sh_size += 16;
1438 }
1439 }
1440
1441 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::HashSection & Section,ContiguousBlobAccumulator & CBA)1442 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1443 const ELFYAML::HashSection &Section,
1444 ContiguousBlobAccumulator &CBA) {
1445 unsigned Link = 0;
1446 if (!Section.Link && !ExcludedSectionHeaders.count(".dynsym") &&
1447 SN2I.lookup(".dynsym", Link))
1448 SHeader.sh_link = Link;
1449
1450 if (Section.EntSize)
1451 SHeader.sh_entsize = *Section.EntSize;
1452 else
1453 SHeader.sh_entsize = sizeof(typename ELFT::Word);
1454
1455 if (!Section.Bucket)
1456 return;
1457
1458 if (!Section.Bucket)
1459 return;
1460
1461 CBA.write<uint32_t>(
1462 Section.NBucket.getValueOr(llvm::yaml::Hex64(Section.Bucket->size())),
1463 ELFT::TargetEndianness);
1464 CBA.write<uint32_t>(
1465 Section.NChain.getValueOr(llvm::yaml::Hex64(Section.Chain->size())),
1466 ELFT::TargetEndianness);
1467
1468 for (uint32_t Val : *Section.Bucket)
1469 CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1470 for (uint32_t Val : *Section.Chain)
1471 CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1472
1473 SHeader.sh_size = (2 + Section.Bucket->size() + Section.Chain->size()) * 4;
1474 }
1475
1476 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::VerdefSection & Section,ContiguousBlobAccumulator & CBA)1477 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1478 const ELFYAML::VerdefSection &Section,
1479 ContiguousBlobAccumulator &CBA) {
1480 typedef typename ELFT::Verdef Elf_Verdef;
1481 typedef typename ELFT::Verdaux Elf_Verdaux;
1482
1483 SHeader.sh_info = Section.Info;
1484
1485 if (!Section.Entries)
1486 return;
1487
1488 uint64_t AuxCnt = 0;
1489 for (size_t I = 0; I < Section.Entries->size(); ++I) {
1490 const ELFYAML::VerdefEntry &E = (*Section.Entries)[I];
1491
1492 Elf_Verdef VerDef;
1493 VerDef.vd_version = E.Version;
1494 VerDef.vd_flags = E.Flags;
1495 VerDef.vd_ndx = E.VersionNdx;
1496 VerDef.vd_hash = E.Hash;
1497 VerDef.vd_aux = sizeof(Elf_Verdef);
1498 VerDef.vd_cnt = E.VerNames.size();
1499 if (I == Section.Entries->size() - 1)
1500 VerDef.vd_next = 0;
1501 else
1502 VerDef.vd_next =
1503 sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux);
1504 CBA.write((const char *)&VerDef, sizeof(Elf_Verdef));
1505
1506 for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) {
1507 Elf_Verdaux VernAux;
1508 VernAux.vda_name = DotDynstr.getOffset(E.VerNames[J]);
1509 if (J == E.VerNames.size() - 1)
1510 VernAux.vda_next = 0;
1511 else
1512 VernAux.vda_next = sizeof(Elf_Verdaux);
1513 CBA.write((const char *)&VernAux, sizeof(Elf_Verdaux));
1514 }
1515 }
1516
1517 SHeader.sh_size = Section.Entries->size() * sizeof(Elf_Verdef) +
1518 AuxCnt * sizeof(Elf_Verdaux);
1519 }
1520
1521 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::VerneedSection & Section,ContiguousBlobAccumulator & CBA)1522 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1523 const ELFYAML::VerneedSection &Section,
1524 ContiguousBlobAccumulator &CBA) {
1525 typedef typename ELFT::Verneed Elf_Verneed;
1526 typedef typename ELFT::Vernaux Elf_Vernaux;
1527
1528 SHeader.sh_info = Section.Info;
1529
1530 if (!Section.VerneedV)
1531 return;
1532
1533 uint64_t AuxCnt = 0;
1534 for (size_t I = 0; I < Section.VerneedV->size(); ++I) {
1535 const ELFYAML::VerneedEntry &VE = (*Section.VerneedV)[I];
1536
1537 Elf_Verneed VerNeed;
1538 VerNeed.vn_version = VE.Version;
1539 VerNeed.vn_file = DotDynstr.getOffset(VE.File);
1540 if (I == Section.VerneedV->size() - 1)
1541 VerNeed.vn_next = 0;
1542 else
1543 VerNeed.vn_next =
1544 sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux);
1545 VerNeed.vn_cnt = VE.AuxV.size();
1546 VerNeed.vn_aux = sizeof(Elf_Verneed);
1547 CBA.write((const char *)&VerNeed, sizeof(Elf_Verneed));
1548
1549 for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) {
1550 const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J];
1551
1552 Elf_Vernaux VernAux;
1553 VernAux.vna_hash = VAuxE.Hash;
1554 VernAux.vna_flags = VAuxE.Flags;
1555 VernAux.vna_other = VAuxE.Other;
1556 VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name);
1557 if (J == VE.AuxV.size() - 1)
1558 VernAux.vna_next = 0;
1559 else
1560 VernAux.vna_next = sizeof(Elf_Vernaux);
1561 CBA.write((const char *)&VernAux, sizeof(Elf_Vernaux));
1562 }
1563 }
1564
1565 SHeader.sh_size = Section.VerneedV->size() * sizeof(Elf_Verneed) +
1566 AuxCnt * sizeof(Elf_Vernaux);
1567 }
1568
1569 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::ARMIndexTableSection & Section,ContiguousBlobAccumulator & CBA)1570 void ELFState<ELFT>::writeSectionContent(
1571 Elf_Shdr &SHeader, const ELFYAML::ARMIndexTableSection &Section,
1572 ContiguousBlobAccumulator &CBA) {
1573 if (!Section.Entries)
1574 return;
1575
1576 for (const ELFYAML::ARMIndexTableEntry &E : *Section.Entries) {
1577 CBA.write<uint32_t>(E.Offset, ELFT::TargetEndianness);
1578 CBA.write<uint32_t>(E.Value, ELFT::TargetEndianness);
1579 }
1580 SHeader.sh_size = Section.Entries->size() * 8;
1581 }
1582
1583 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::MipsABIFlags & Section,ContiguousBlobAccumulator & CBA)1584 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1585 const ELFYAML::MipsABIFlags &Section,
1586 ContiguousBlobAccumulator &CBA) {
1587 assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
1588 "Section type is not SHT_MIPS_ABIFLAGS");
1589
1590 object::Elf_Mips_ABIFlags<ELFT> Flags;
1591 zero(Flags);
1592 SHeader.sh_entsize = sizeof(Flags);
1593 SHeader.sh_size = SHeader.sh_entsize;
1594
1595 Flags.version = Section.Version;
1596 Flags.isa_level = Section.ISALevel;
1597 Flags.isa_rev = Section.ISARevision;
1598 Flags.gpr_size = Section.GPRSize;
1599 Flags.cpr1_size = Section.CPR1Size;
1600 Flags.cpr2_size = Section.CPR2Size;
1601 Flags.fp_abi = Section.FpABI;
1602 Flags.isa_ext = Section.ISAExtension;
1603 Flags.ases = Section.ASEs;
1604 Flags.flags1 = Section.Flags1;
1605 Flags.flags2 = Section.Flags2;
1606 CBA.write((const char *)&Flags, sizeof(Flags));
1607 }
1608
1609 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::DynamicSection & Section,ContiguousBlobAccumulator & CBA)1610 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1611 const ELFYAML::DynamicSection &Section,
1612 ContiguousBlobAccumulator &CBA) {
1613 assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
1614 "Section type is not SHT_DYNAMIC");
1615
1616 if (Section.EntSize)
1617 SHeader.sh_entsize = *Section.EntSize;
1618 else
1619 SHeader.sh_entsize = 2 * sizeof(uintX_t);
1620
1621 if (!Section.Entries)
1622 return;
1623
1624 for (const ELFYAML::DynamicEntry &DE : *Section.Entries) {
1625 CBA.write<uintX_t>(DE.Tag, ELFT::TargetEndianness);
1626 CBA.write<uintX_t>(DE.Val, ELFT::TargetEndianness);
1627 }
1628 SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries->size();
1629 }
1630
1631 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::AddrsigSection & Section,ContiguousBlobAccumulator & CBA)1632 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1633 const ELFYAML::AddrsigSection &Section,
1634 ContiguousBlobAccumulator &CBA) {
1635 unsigned Link = 0;
1636 if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
1637 SN2I.lookup(".symtab", Link))
1638 SHeader.sh_link = Link;
1639
1640 if (!Section.Symbols)
1641 return;
1642
1643 if (!Section.Symbols)
1644 return;
1645
1646 for (StringRef Sym : *Section.Symbols)
1647 SHeader.sh_size +=
1648 CBA.writeULEB128(toSymbolIndex(Sym, Section.Name, /*IsDynamic=*/false));
1649 }
1650
1651 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::NoteSection & Section,ContiguousBlobAccumulator & CBA)1652 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1653 const ELFYAML::NoteSection &Section,
1654 ContiguousBlobAccumulator &CBA) {
1655 if (!Section.Notes)
1656 return;
1657
1658 uint64_t Offset = CBA.tell();
1659 for (const ELFYAML::NoteEntry &NE : *Section.Notes) {
1660 // Write name size.
1661 if (NE.Name.empty())
1662 CBA.write<uint32_t>(0, ELFT::TargetEndianness);
1663 else
1664 CBA.write<uint32_t>(NE.Name.size() + 1, ELFT::TargetEndianness);
1665
1666 // Write description size.
1667 if (NE.Desc.binary_size() == 0)
1668 CBA.write<uint32_t>(0, ELFT::TargetEndianness);
1669 else
1670 CBA.write<uint32_t>(NE.Desc.binary_size(), ELFT::TargetEndianness);
1671
1672 // Write type.
1673 CBA.write<uint32_t>(NE.Type, ELFT::TargetEndianness);
1674
1675 // Write name, null terminator and padding.
1676 if (!NE.Name.empty()) {
1677 CBA.write(NE.Name.data(), NE.Name.size());
1678 CBA.write('\0');
1679 CBA.padToAlignment(4);
1680 }
1681
1682 // Write description and padding.
1683 if (NE.Desc.binary_size() != 0) {
1684 CBA.writeAsBinary(NE.Desc);
1685 CBA.padToAlignment(4);
1686 }
1687 }
1688
1689 SHeader.sh_size = CBA.tell() - Offset;
1690 }
1691
1692 template <class ELFT>
writeSectionContent(Elf_Shdr & SHeader,const ELFYAML::GnuHashSection & Section,ContiguousBlobAccumulator & CBA)1693 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1694 const ELFYAML::GnuHashSection &Section,
1695 ContiguousBlobAccumulator &CBA) {
1696 unsigned Link = 0;
1697 if (!Section.Link && !ExcludedSectionHeaders.count(".dynsym") &&
1698 SN2I.lookup(".dynsym", Link))
1699 SHeader.sh_link = Link;
1700
1701 if (!Section.HashBuckets)
1702 return;
1703
1704 if (!Section.Header)
1705 return;
1706
1707 // We write the header first, starting with the hash buckets count. Normally
1708 // it is the number of entries in HashBuckets, but the "NBuckets" property can
1709 // be used to override this field, which is useful for producing broken
1710 // objects.
1711 if (Section.Header->NBuckets)
1712 CBA.write<uint32_t>(*Section.Header->NBuckets, ELFT::TargetEndianness);
1713 else
1714 CBA.write<uint32_t>(Section.HashBuckets->size(), ELFT::TargetEndianness);
1715
1716 // Write the index of the first symbol in the dynamic symbol table accessible
1717 // via the hash table.
1718 CBA.write<uint32_t>(Section.Header->SymNdx, ELFT::TargetEndianness);
1719
1720 // Write the number of words in the Bloom filter. As above, the "MaskWords"
1721 // property can be used to set this field to any value.
1722 if (Section.Header->MaskWords)
1723 CBA.write<uint32_t>(*Section.Header->MaskWords, ELFT::TargetEndianness);
1724 else
1725 CBA.write<uint32_t>(Section.BloomFilter->size(), ELFT::TargetEndianness);
1726
1727 // Write the shift constant used by the Bloom filter.
1728 CBA.write<uint32_t>(Section.Header->Shift2, ELFT::TargetEndianness);
1729
1730 // We've finished writing the header. Now write the Bloom filter.
1731 for (llvm::yaml::Hex64 Val : *Section.BloomFilter)
1732 CBA.write<uintX_t>(Val, ELFT::TargetEndianness);
1733
1734 // Write an array of hash buckets.
1735 for (llvm::yaml::Hex32 Val : *Section.HashBuckets)
1736 CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1737
1738 // Write an array of hash values.
1739 for (llvm::yaml::Hex32 Val : *Section.HashValues)
1740 CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1741
1742 SHeader.sh_size = 16 /*Header size*/ +
1743 Section.BloomFilter->size() * sizeof(typename ELFT::uint) +
1744 Section.HashBuckets->size() * 4 +
1745 Section.HashValues->size() * 4;
1746 }
1747
1748 template <class ELFT>
writeFill(ELFYAML::Fill & Fill,ContiguousBlobAccumulator & CBA)1749 void ELFState<ELFT>::writeFill(ELFYAML::Fill &Fill,
1750 ContiguousBlobAccumulator &CBA) {
1751 size_t PatternSize = Fill.Pattern ? Fill.Pattern->binary_size() : 0;
1752 if (!PatternSize) {
1753 CBA.writeZeros(Fill.Size);
1754 return;
1755 }
1756
1757 // Fill the content with the specified pattern.
1758 uint64_t Written = 0;
1759 for (; Written + PatternSize <= Fill.Size; Written += PatternSize)
1760 CBA.writeAsBinary(*Fill.Pattern);
1761 CBA.writeAsBinary(*Fill.Pattern, Fill.Size - Written);
1762 }
1763
1764 template <class ELFT>
buildSectionHeaderReorderMap()1765 DenseMap<StringRef, size_t> ELFState<ELFT>::buildSectionHeaderReorderMap() {
1766 if (!Doc.SectionHeaders || Doc.SectionHeaders->NoHeaders)
1767 return DenseMap<StringRef, size_t>();
1768
1769 DenseMap<StringRef, size_t> Ret;
1770 size_t SecNdx = 0;
1771 StringSet<> Seen;
1772
1773 auto AddSection = [&](const ELFYAML::SectionHeader &Hdr) {
1774 if (!Ret.try_emplace(Hdr.Name, ++SecNdx).second)
1775 reportError("repeated section name: '" + Hdr.Name +
1776 "' in the section header description");
1777 Seen.insert(Hdr.Name);
1778 };
1779
1780 if (Doc.SectionHeaders->Sections)
1781 for (const ELFYAML::SectionHeader &Hdr : *Doc.SectionHeaders->Sections)
1782 AddSection(Hdr);
1783
1784 if (Doc.SectionHeaders->Excluded)
1785 for (const ELFYAML::SectionHeader &Hdr : *Doc.SectionHeaders->Excluded)
1786 AddSection(Hdr);
1787
1788 for (const ELFYAML::Section *S : Doc.getSections()) {
1789 // Ignore special first SHT_NULL section.
1790 if (S == Doc.getSections().front())
1791 continue;
1792 if (!Seen.count(S->Name))
1793 reportError("section '" + S->Name +
1794 "' should be present in the 'Sections' or 'Excluded' lists");
1795 Seen.erase(S->Name);
1796 }
1797
1798 for (const auto &It : Seen)
1799 reportError("section header contains undefined section '" + It.getKey() +
1800 "'");
1801 return Ret;
1802 }
1803
buildSectionIndex()1804 template <class ELFT> void ELFState<ELFT>::buildSectionIndex() {
1805 // A YAML description can have an explicit section header declaration that
1806 // allows to change the order of section headers.
1807 DenseMap<StringRef, size_t> ReorderMap = buildSectionHeaderReorderMap();
1808
1809 if (HasError)
1810 return;
1811
1812 // Build excluded section headers map.
1813 std::vector<ELFYAML::Section *> Sections = Doc.getSections();
1814 if (Doc.SectionHeaders) {
1815 if (Doc.SectionHeaders->Excluded)
1816 for (const ELFYAML::SectionHeader &Hdr : *Doc.SectionHeaders->Excluded)
1817 if (!ExcludedSectionHeaders.insert(Hdr.Name).second)
1818 llvm_unreachable("buildSectionIndex() failed");
1819
1820 if (Doc.SectionHeaders->NoHeaders.getValueOr(false))
1821 for (const ELFYAML::Section *S : Sections)
1822 if (!ExcludedSectionHeaders.insert(S->Name).second)
1823 llvm_unreachable("buildSectionIndex() failed");
1824 }
1825
1826 size_t SecNdx = -1;
1827 for (const ELFYAML::Section *S : Sections) {
1828 ++SecNdx;
1829
1830 size_t Index = ReorderMap.empty() ? SecNdx : ReorderMap.lookup(S->Name);
1831 if (!SN2I.addName(S->Name, Index))
1832 llvm_unreachable("buildSectionIndex() failed");
1833
1834 if (!ExcludedSectionHeaders.count(S->Name))
1835 DotShStrtab.add(ELFYAML::dropUniqueSuffix(S->Name));
1836 }
1837
1838 DotShStrtab.finalize();
1839 }
1840
buildSymbolIndexes()1841 template <class ELFT> void ELFState<ELFT>::buildSymbolIndexes() {
1842 auto Build = [this](ArrayRef<ELFYAML::Symbol> V, NameToIdxMap &Map) {
1843 for (size_t I = 0, S = V.size(); I < S; ++I) {
1844 const ELFYAML::Symbol &Sym = V[I];
1845 if (!Sym.Name.empty() && !Map.addName(Sym.Name, I + 1))
1846 reportError("repeated symbol name: '" + Sym.Name + "'");
1847 }
1848 };
1849
1850 if (Doc.Symbols)
1851 Build(*Doc.Symbols, SymN2I);
1852 if (Doc.DynamicSymbols)
1853 Build(*Doc.DynamicSymbols, DynSymN2I);
1854 }
1855
finalizeStrings()1856 template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
1857 // Add the regular symbol names to .strtab section.
1858 if (Doc.Symbols)
1859 for (const ELFYAML::Symbol &Sym : *Doc.Symbols)
1860 DotStrtab.add(ELFYAML::dropUniqueSuffix(Sym.Name));
1861 DotStrtab.finalize();
1862
1863 // Add the dynamic symbol names to .dynstr section.
1864 if (Doc.DynamicSymbols)
1865 for (const ELFYAML::Symbol &Sym : *Doc.DynamicSymbols)
1866 DotDynstr.add(ELFYAML::dropUniqueSuffix(Sym.Name));
1867
1868 // SHT_GNU_verdef and SHT_GNU_verneed sections might also
1869 // add strings to .dynstr section.
1870 for (const ELFYAML::Chunk *Sec : Doc.getSections()) {
1871 if (auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
1872 if (VerNeed->VerneedV) {
1873 for (const ELFYAML::VerneedEntry &VE : *VerNeed->VerneedV) {
1874 DotDynstr.add(VE.File);
1875 for (const ELFYAML::VernauxEntry &Aux : VE.AuxV)
1876 DotDynstr.add(Aux.Name);
1877 }
1878 }
1879 } else if (auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
1880 if (VerDef->Entries)
1881 for (const ELFYAML::VerdefEntry &E : *VerDef->Entries)
1882 for (StringRef Name : E.VerNames)
1883 DotDynstr.add(Name);
1884 }
1885 }
1886
1887 DotDynstr.finalize();
1888 }
1889
1890 template <class ELFT>
writeELF(raw_ostream & OS,ELFYAML::Object & Doc,yaml::ErrorHandler EH,uint64_t MaxSize)1891 bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
1892 yaml::ErrorHandler EH, uint64_t MaxSize) {
1893 ELFState<ELFT> State(Doc, EH);
1894 if (State.HasError)
1895 return false;
1896
1897 // Finalize .strtab and .dynstr sections. We do that early because want to
1898 // finalize the string table builders before writing the content of the
1899 // sections that might want to use them.
1900 State.finalizeStrings();
1901
1902 State.buildSectionIndex();
1903 State.buildSymbolIndexes();
1904
1905 if (State.HasError)
1906 return false;
1907
1908 std::vector<Elf_Phdr> PHeaders;
1909 State.initProgramHeaders(PHeaders);
1910
1911 // XXX: This offset is tightly coupled with the order that we write
1912 // things to `OS`.
1913 const size_t SectionContentBeginOffset =
1914 sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
1915 // It is quite easy to accidentally create output with yaml2obj that is larger
1916 // than intended, for example, due to an issue in the YAML description.
1917 // We limit the maximum allowed output size, but also provide a command line
1918 // option to change this limitation.
1919 ContiguousBlobAccumulator CBA(SectionContentBeginOffset, MaxSize);
1920
1921 std::vector<Elf_Shdr> SHeaders;
1922 State.initSectionHeaders(SHeaders, CBA);
1923
1924 // Now we can decide segment offsets.
1925 State.setProgramHeaderLayout(PHeaders, SHeaders);
1926
1927 // If needed, align the start of the section header table, which is written
1928 // after all section data.
1929 const bool HasSectionHeaders =
1930 !Doc.SectionHeaders || !Doc.SectionHeaders->NoHeaders.getValueOr(false);
1931 Optional<uint64_t> SHOff;
1932 if (HasSectionHeaders)
1933 SHOff = State.alignToOffset(CBA, sizeof(typename ELFT::uint),
1934 /*Offset=*/None);
1935 bool ReachedLimit = SHOff.getValueOr(CBA.getOffset()) +
1936 arrayDataSize(makeArrayRef(SHeaders)) >
1937 MaxSize;
1938 if (Error E = CBA.takeLimitError()) {
1939 // We report a custom error message instead below.
1940 consumeError(std::move(E));
1941 ReachedLimit = true;
1942 }
1943
1944 if (ReachedLimit)
1945 State.reportError(
1946 "the desired output size is greater than permitted. Use the "
1947 "--max-size option to change the limit");
1948
1949 if (State.HasError)
1950 return false;
1951
1952 State.writeELFHeader(OS, SHOff);
1953 writeArrayData(OS, makeArrayRef(PHeaders));
1954 CBA.writeBlobToStream(OS);
1955 if (HasSectionHeaders)
1956 writeArrayData(OS, makeArrayRef(SHeaders));
1957 return true;
1958 }
1959
1960 namespace llvm {
1961 namespace yaml {
1962
yaml2elf(llvm::ELFYAML::Object & Doc,raw_ostream & Out,ErrorHandler EH,uint64_t MaxSize)1963 bool yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH,
1964 uint64_t MaxSize) {
1965 bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1966 bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1967 if (Is64Bit) {
1968 if (IsLE)
1969 return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH, MaxSize);
1970 return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH, MaxSize);
1971 }
1972 if (IsLE)
1973 return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH, MaxSize);
1974 return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH, MaxSize);
1975 }
1976
1977 } // namespace yaml
1978 } // namespace llvm
1979