]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-objcopy/ELF/Object.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-objcopy / ELF / Object.cpp
1 //===- Object.cpp ---------------------------------------------------------===//
2 //
3 //                      The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "Object.h"
11 #include "llvm-objcopy.h"
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/ADT/iterator_range.h"
17 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/MC/MCTargetOptions.h"
19 #include "llvm/Object/ELFObjectFile.h"
20 #include "llvm/Support/Compression.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/FileOutputBuffer.h"
23 #include "llvm/Support/Path.h"
24 #include <algorithm>
25 #include <cstddef>
26 #include <cstdint>
27 #include <iterator>
28 #include <utility>
29 #include <vector>
30
31 namespace llvm {
32 namespace objcopy {
33 namespace elf {
34
35 using namespace object;
36 using namespace ELF;
37
38 template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
39   uint8_t *B = Buf.getBufferStart();
40   B += Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr);
41   Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B);
42   Phdr.p_type = Seg.Type;
43   Phdr.p_flags = Seg.Flags;
44   Phdr.p_offset = Seg.Offset;
45   Phdr.p_vaddr = Seg.VAddr;
46   Phdr.p_paddr = Seg.PAddr;
47   Phdr.p_filesz = Seg.FileSize;
48   Phdr.p_memsz = Seg.MemSize;
49   Phdr.p_align = Seg.Align;
50 }
51
52 void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
53 void SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {}
54 void SectionBase::initialize(SectionTableRef SecTable) {}
55 void SectionBase::finalize() {}
56 void SectionBase::markSymbols() {}
57
58 template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
59   uint8_t *B = Buf.getBufferStart();
60   B += Sec.HeaderOffset;
61   Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
62   Shdr.sh_name = Sec.NameIndex;
63   Shdr.sh_type = Sec.Type;
64   Shdr.sh_flags = Sec.Flags;
65   Shdr.sh_addr = Sec.Addr;
66   Shdr.sh_offset = Sec.Offset;
67   Shdr.sh_size = Sec.Size;
68   Shdr.sh_link = Sec.Link;
69   Shdr.sh_info = Sec.Info;
70   Shdr.sh_addralign = Sec.Align;
71   Shdr.sh_entsize = Sec.EntrySize;
72 }
73
74 template <class ELFT> void ELFSectionSizer<ELFT>::visit(Section &Sec) {}
75
76 template <class ELFT>
77 void ELFSectionSizer<ELFT>::visit(OwnedDataSection &Sec) {}
78
79 template <class ELFT>
80 void ELFSectionSizer<ELFT>::visit(StringTableSection &Sec) {}
81
82 template <class ELFT>
83 void ELFSectionSizer<ELFT>::visit(DynamicRelocationSection &Sec) {}
84
85 template <class ELFT>
86 void ELFSectionSizer<ELFT>::visit(SymbolTableSection &Sec) {
87   Sec.EntrySize = sizeof(Elf_Sym);
88   Sec.Size = Sec.Symbols.size() * Sec.EntrySize;
89   // Align to the largest field in Elf_Sym.
90   Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
91 }
92
93 template <class ELFT>
94 void ELFSectionSizer<ELFT>::visit(RelocationSection &Sec) {
95   Sec.EntrySize = Sec.Type == SHT_REL ? sizeof(Elf_Rel) : sizeof(Elf_Rela);
96   Sec.Size = Sec.Relocations.size() * Sec.EntrySize;
97   // Align to the largest field in Elf_Rel(a).
98   Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
99 }
100
101 template <class ELFT>
102 void ELFSectionSizer<ELFT>::visit(GnuDebugLinkSection &Sec) {}
103
104 template <class ELFT> void ELFSectionSizer<ELFT>::visit(GroupSection &Sec) {}
105
106 template <class ELFT>
107 void ELFSectionSizer<ELFT>::visit(SectionIndexSection &Sec) {}
108
109 template <class ELFT>
110 void ELFSectionSizer<ELFT>::visit(CompressedSection &Sec) {}
111
112 template <class ELFT>
113 void ELFSectionSizer<ELFT>::visit(DecompressedSection &Sec) {}
114
115 void BinarySectionWriter::visit(const SectionIndexSection &Sec) {
116   error("Cannot write symbol section index table '" + Sec.Name + "' ");
117 }
118
119 void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
120   error("Cannot write symbol table '" + Sec.Name + "' out to binary");
121 }
122
123 void BinarySectionWriter::visit(const RelocationSection &Sec) {
124   error("Cannot write relocation section '" + Sec.Name + "' out to binary");
125 }
126
127 void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
128   error("Cannot write '" + Sec.Name + "' out to binary");
129 }
130
131 void BinarySectionWriter::visit(const GroupSection &Sec) {
132   error("Cannot write '" + Sec.Name + "' out to binary");
133 }
134
135 void SectionWriter::visit(const Section &Sec) {
136   if (Sec.Type == SHT_NOBITS)
137     return;
138   uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
139   llvm::copy(Sec.Contents, Buf);
140 }
141
142 void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); }
143
144 void Section::accept(MutableSectionVisitor &Visitor) { Visitor.visit(*this); }
145
146 void SectionWriter::visit(const OwnedDataSection &Sec) {
147   uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
148   llvm::copy(Sec.Data, Buf);
149 }
150
151 static const std::vector<uint8_t> ZlibGnuMagic = {'Z', 'L', 'I', 'B'};
152
153 static bool isDataGnuCompressed(ArrayRef<uint8_t> Data) {
154   return Data.size() > ZlibGnuMagic.size() &&
155          std::equal(ZlibGnuMagic.begin(), ZlibGnuMagic.end(), Data.data());
156 }
157
158 template <class ELFT>
159 static std::tuple<uint64_t, uint64_t>
160 getDecompressedSizeAndAlignment(ArrayRef<uint8_t> Data) {
161   const bool IsGnuDebug = isDataGnuCompressed(Data);
162   const uint64_t DecompressedSize =
163       IsGnuDebug
164           ? support::endian::read64be(reinterpret_cast<const uint64_t *>(
165                 Data.data() + ZlibGnuMagic.size()))
166           : reinterpret_cast<const Elf_Chdr_Impl<ELFT> *>(Data.data())->ch_size;
167   const uint64_t DecompressedAlign =
168       IsGnuDebug ? 1
169                  : reinterpret_cast<const Elf_Chdr_Impl<ELFT> *>(Data.data())
170                        ->ch_addralign;
171
172   return std::make_tuple(DecompressedSize, DecompressedAlign);
173 }
174
175 template <class ELFT>
176 void ELFSectionWriter<ELFT>::visit(const DecompressedSection &Sec) {
177   uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
178
179   if (!zlib::isAvailable()) {
180     std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
181     return;
182   }
183
184   const size_t DataOffset = isDataGnuCompressed(Sec.OriginalData)
185                                 ? (ZlibGnuMagic.size() + sizeof(Sec.Size))
186                                 : sizeof(Elf_Chdr_Impl<ELFT>);
187
188   StringRef CompressedContent(
189       reinterpret_cast<const char *>(Sec.OriginalData.data()) + DataOffset,
190       Sec.OriginalData.size() - DataOffset);
191
192   SmallVector<char, 128> DecompressedContent;
193   if (Error E = zlib::uncompress(CompressedContent, DecompressedContent,
194                                  static_cast<size_t>(Sec.Size)))
195     reportError(Sec.Name, std::move(E));
196
197   std::copy(DecompressedContent.begin(), DecompressedContent.end(), Buf);
198 }
199
200 void BinarySectionWriter::visit(const DecompressedSection &Sec) {
201   error("Cannot write compressed section '" + Sec.Name + "' ");
202 }
203
204 void DecompressedSection::accept(SectionVisitor &Visitor) const {
205   Visitor.visit(*this);
206 }
207
208 void DecompressedSection::accept(MutableSectionVisitor &Visitor) {
209   Visitor.visit(*this);
210 }
211
212 void OwnedDataSection::accept(SectionVisitor &Visitor) const {
213   Visitor.visit(*this);
214 }
215
216 void OwnedDataSection::accept(MutableSectionVisitor &Visitor) {
217   Visitor.visit(*this);
218 }
219
220 void BinarySectionWriter::visit(const CompressedSection &Sec) {
221   error("Cannot write compressed section '" + Sec.Name + "' ");
222 }
223
224 template <class ELFT>
225 void ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
226   uint8_t *Buf = Out.getBufferStart();
227   Buf += Sec.Offset;
228
229   if (Sec.CompressionType == DebugCompressionType::None) {
230     std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
231     return;
232   }
233
234   if (Sec.CompressionType == DebugCompressionType::GNU) {
235     const char *Magic = "ZLIB";
236     memcpy(Buf, Magic, strlen(Magic));
237     Buf += strlen(Magic);
238     const uint64_t DecompressedSize =
239         support::endian::read64be(&Sec.DecompressedSize);
240     memcpy(Buf, &DecompressedSize, sizeof(DecompressedSize));
241     Buf += sizeof(DecompressedSize);
242   } else {
243     Elf_Chdr_Impl<ELFT> Chdr;
244     Chdr.ch_type = ELF::ELFCOMPRESS_ZLIB;
245     Chdr.ch_size = Sec.DecompressedSize;
246     Chdr.ch_addralign = Sec.DecompressedAlign;
247     memcpy(Buf, &Chdr, sizeof(Chdr));
248     Buf += sizeof(Chdr);
249   }
250
251   std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf);
252 }
253
254 CompressedSection::CompressedSection(const SectionBase &Sec,
255                                      DebugCompressionType CompressionType)
256     : SectionBase(Sec), CompressionType(CompressionType),
257       DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
258
259   if (!zlib::isAvailable()) {
260     CompressionType = DebugCompressionType::None;
261     return;
262   }
263
264   if (Error E = zlib::compress(
265           StringRef(reinterpret_cast<const char *>(OriginalData.data()),
266                     OriginalData.size()),
267           CompressedData))
268     reportError(Name, std::move(E));
269
270   size_t ChdrSize;
271   if (CompressionType == DebugCompressionType::GNU) {
272     Name = ".z" + Sec.Name.substr(1);
273     ChdrSize = sizeof("ZLIB") - 1 + sizeof(uint64_t);
274   } else {
275     Flags |= ELF::SHF_COMPRESSED;
276     ChdrSize =
277         std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>),
278                           sizeof(object::Elf_Chdr_Impl<object::ELF64BE>)),
279                  std::max(sizeof(object::Elf_Chdr_Impl<object::ELF32LE>),
280                           sizeof(object::Elf_Chdr_Impl<object::ELF32BE>)));
281   }
282   Size = ChdrSize + CompressedData.size();
283   Align = 8;
284 }
285
286 CompressedSection::CompressedSection(ArrayRef<uint8_t> CompressedData,
287                                      uint64_t DecompressedSize,
288                                      uint64_t DecompressedAlign)
289     : CompressionType(DebugCompressionType::None),
290       DecompressedSize(DecompressedSize), DecompressedAlign(DecompressedAlign) {
291   OriginalData = CompressedData;
292 }
293
294 void CompressedSection::accept(SectionVisitor &Visitor) const {
295   Visitor.visit(*this);
296 }
297
298 void CompressedSection::accept(MutableSectionVisitor &Visitor) {
299   Visitor.visit(*this);
300 }
301
302 void StringTableSection::addString(StringRef Name) {
303   StrTabBuilder.add(Name);
304   Size = StrTabBuilder.getSize();
305 }
306
307 uint32_t StringTableSection::findIndex(StringRef Name) const {
308   return StrTabBuilder.getOffset(Name);
309 }
310
311 void StringTableSection::finalize() { StrTabBuilder.finalize(); }
312
313 void SectionWriter::visit(const StringTableSection &Sec) {
314   Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
315 }
316
317 void StringTableSection::accept(SectionVisitor &Visitor) const {
318   Visitor.visit(*this);
319 }
320
321 void StringTableSection::accept(MutableSectionVisitor &Visitor) {
322   Visitor.visit(*this);
323 }
324
325 template <class ELFT>
326 void ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
327   uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
328   auto *IndexesBuffer = reinterpret_cast<Elf_Word *>(Buf);
329   llvm::copy(Sec.Indexes, IndexesBuffer);
330 }
331
332 void SectionIndexSection::initialize(SectionTableRef SecTable) {
333   Size = 0;
334   setSymTab(SecTable.getSectionOfType<SymbolTableSection>(
335       Link,
336       "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
337       "Link field value " + Twine(Link) + " in section " + Name +
338           " is not a symbol table"));
339   Symbols->setShndxTable(this);
340 }
341
342 void SectionIndexSection::finalize() { Link = Symbols->Index; }
343
344 void SectionIndexSection::accept(SectionVisitor &Visitor) const {
345   Visitor.visit(*this);
346 }
347
348 void SectionIndexSection::accept(MutableSectionVisitor &Visitor) {
349   Visitor.visit(*this);
350 }
351
352 static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
353   switch (Index) {
354   case SHN_ABS:
355   case SHN_COMMON:
356     return true;
357   }
358   if (Machine == EM_HEXAGON) {
359     switch (Index) {
360     case SHN_HEXAGON_SCOMMON:
361     case SHN_HEXAGON_SCOMMON_2:
362     case SHN_HEXAGON_SCOMMON_4:
363     case SHN_HEXAGON_SCOMMON_8:
364       return true;
365     }
366   }
367   return false;
368 }
369
370 // Large indexes force us to clarify exactly what this function should do. This
371 // function should return the value that will appear in st_shndx when written
372 // out.
373 uint16_t Symbol::getShndx() const {
374   if (DefinedIn != nullptr) {
375     if (DefinedIn->Index >= SHN_LORESERVE)
376       return SHN_XINDEX;
377     return DefinedIn->Index;
378   }
379   switch (ShndxType) {
380   // This means that we don't have a defined section but we do need to
381   // output a legitimate section index.
382   case SYMBOL_SIMPLE_INDEX:
383     return SHN_UNDEF;
384   case SYMBOL_ABS:
385   case SYMBOL_COMMON:
386   case SYMBOL_HEXAGON_SCOMMON:
387   case SYMBOL_HEXAGON_SCOMMON_2:
388   case SYMBOL_HEXAGON_SCOMMON_4:
389   case SYMBOL_HEXAGON_SCOMMON_8:
390   case SYMBOL_XINDEX:
391     return static_cast<uint16_t>(ShndxType);
392   }
393   llvm_unreachable("Symbol with invalid ShndxType encountered");
394 }
395
396 bool Symbol::isCommon() const { return getShndx() == SHN_COMMON; }
397
398 void SymbolTableSection::assignIndices() {
399   uint32_t Index = 0;
400   for (auto &Sym : Symbols)
401     Sym->Index = Index++;
402 }
403
404 void SymbolTableSection::addSymbol(Twine Name, uint8_t Bind, uint8_t Type,
405                                    SectionBase *DefinedIn, uint64_t Value,
406                                    uint8_t Visibility, uint16_t Shndx,
407                                    uint64_t Size) {
408   Symbol Sym;
409   Sym.Name = Name.str();
410   Sym.Binding = Bind;
411   Sym.Type = Type;
412   Sym.DefinedIn = DefinedIn;
413   if (DefinedIn != nullptr)
414     DefinedIn->HasSymbol = true;
415   if (DefinedIn == nullptr) {
416     if (Shndx >= SHN_LORESERVE)
417       Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
418     else
419       Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
420   }
421   Sym.Value = Value;
422   Sym.Visibility = Visibility;
423   Sym.Size = Size;
424   Sym.Index = Symbols.size();
425   Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
426   Size += this->EntrySize;
427 }
428
429 void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
430   if (SectionIndexTable == Sec)
431     SectionIndexTable = nullptr;
432   if (SymbolNames == Sec) {
433     error("String table " + SymbolNames->Name +
434           " cannot be removed because it is referenced by the symbol table " +
435           this->Name);
436   }
437   removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; });
438 }
439
440 void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
441   std::for_each(std::begin(Symbols) + 1, std::end(Symbols),
442                 [Callable](SymPtr &Sym) { Callable(*Sym); });
443   std::stable_partition(
444       std::begin(Symbols), std::end(Symbols),
445       [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; });
446   assignIndices();
447 }
448
449 void SymbolTableSection::removeSymbols(
450     function_ref<bool(const Symbol &)> ToRemove) {
451   Symbols.erase(
452       std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
453                      [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
454       std::end(Symbols));
455   Size = Symbols.size() * EntrySize;
456   assignIndices();
457 }
458
459 void SymbolTableSection::initialize(SectionTableRef SecTable) {
460   Size = 0;
461   setStrTab(SecTable.getSectionOfType<StringTableSection>(
462       Link,
463       "Symbol table has link index of " + Twine(Link) +
464           " which is not a valid index",
465       "Symbol table has link index of " + Twine(Link) +
466           " which is not a string table"));
467 }
468
469 void SymbolTableSection::finalize() {
470   // Make sure SymbolNames is finalized before getting name indexes.
471   SymbolNames->finalize();
472
473   uint32_t MaxLocalIndex = 0;
474   for (auto &Sym : Symbols) {
475     Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
476     if (Sym->Binding == STB_LOCAL)
477       MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
478   }
479   // Now we need to set the Link and Info fields.
480   Link = SymbolNames->Index;
481   Info = MaxLocalIndex + 1;
482 }
483
484 void SymbolTableSection::prepareForLayout() {
485   // Add all potential section indexes before file layout so that the section
486   // index section has the approprite size.
487   if (SectionIndexTable != nullptr) {
488     for (const auto &Sym : Symbols) {
489       if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
490         SectionIndexTable->addIndex(Sym->DefinedIn->Index);
491       else
492         SectionIndexTable->addIndex(SHN_UNDEF);
493     }
494   }
495   // Add all of our strings to SymbolNames so that SymbolNames has the right
496   // size before layout is decided.
497   for (auto &Sym : Symbols)
498     SymbolNames->addString(Sym->Name);
499 }
500
501 const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
502   if (Symbols.size() <= Index)
503     error("Invalid symbol index: " + Twine(Index));
504   return Symbols[Index].get();
505 }
506
507 Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) {
508   return const_cast<Symbol *>(
509       static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index));
510 }
511
512 template <class ELFT>
513 void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
514   uint8_t *Buf = Out.getBufferStart();
515   Buf += Sec.Offset;
516   Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Buf);
517   // Loop though symbols setting each entry of the symbol table.
518   for (auto &Symbol : Sec.Symbols) {
519     Sym->st_name = Symbol->NameIndex;
520     Sym->st_value = Symbol->Value;
521     Sym->st_size = Symbol->Size;
522     Sym->st_other = Symbol->Visibility;
523     Sym->setBinding(Symbol->Binding);
524     Sym->setType(Symbol->Type);
525     Sym->st_shndx = Symbol->getShndx();
526     ++Sym;
527   }
528 }
529
530 void SymbolTableSection::accept(SectionVisitor &Visitor) const {
531   Visitor.visit(*this);
532 }
533
534 void SymbolTableSection::accept(MutableSectionVisitor &Visitor) {
535   Visitor.visit(*this);
536 }
537
538 template <class SymTabType>
539 void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
540     const SectionBase *Sec) {
541   if (Symbols == Sec) {
542     error("Symbol table " + Symbols->Name +
543           " cannot be removed because it is "
544           "referenced by the relocation "
545           "section " +
546           this->Name);
547   }
548 }
549
550 template <class SymTabType>
551 void RelocSectionWithSymtabBase<SymTabType>::initialize(
552     SectionTableRef SecTable) {
553   if (Link != SHN_UNDEF)
554     setSymTab(SecTable.getSectionOfType<SymTabType>(
555         Link,
556         "Link field value " + Twine(Link) + " in section " + Name +
557             " is invalid",
558         "Link field value " + Twine(Link) + " in section " + Name +
559             " is not a symbol table"));
560
561   if (Info != SHN_UNDEF)
562     setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) +
563                                              " in section " + Name +
564                                              " is invalid"));
565   else
566     setSection(nullptr);
567 }
568
569 template <class SymTabType>
570 void RelocSectionWithSymtabBase<SymTabType>::finalize() {
571   this->Link = Symbols ? Symbols->Index : 0;
572
573   if (SecToApplyRel != nullptr)
574     this->Info = SecToApplyRel->Index;
575 }
576
577 template <class ELFT>
578 static void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
579
580 template <class ELFT>
581 static void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
582   Rela.r_addend = Addend;
583 }
584
585 template <class RelRange, class T>
586 static void writeRel(const RelRange &Relocations, T *Buf) {
587   for (const auto &Reloc : Relocations) {
588     Buf->r_offset = Reloc.Offset;
589     setAddend(*Buf, Reloc.Addend);
590     Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
591     ++Buf;
592   }
593 }
594
595 template <class ELFT>
596 void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
597   uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
598   if (Sec.Type == SHT_REL)
599     writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf));
600   else
601     writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf));
602 }
603
604 void RelocationSection::accept(SectionVisitor &Visitor) const {
605   Visitor.visit(*this);
606 }
607
608 void RelocationSection::accept(MutableSectionVisitor &Visitor) {
609   Visitor.visit(*this);
610 }
611
612 void RelocationSection::removeSymbols(
613     function_ref<bool(const Symbol &)> ToRemove) {
614   for (const Relocation &Reloc : Relocations)
615     if (ToRemove(*Reloc.RelocSymbol))
616       error("not stripping symbol '" + Reloc.RelocSymbol->Name +
617             "' because it is named in a relocation");
618 }
619
620 void RelocationSection::markSymbols() {
621   for (const Relocation &Reloc : Relocations)
622     Reloc.RelocSymbol->Referenced = true;
623 }
624
625 void SectionWriter::visit(const DynamicRelocationSection &Sec) {
626   llvm::copy(Sec.Contents,
627             Out.getBufferStart() + Sec.Offset);
628 }
629
630 void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
631   Visitor.visit(*this);
632 }
633
634 void DynamicRelocationSection::accept(MutableSectionVisitor &Visitor) {
635   Visitor.visit(*this);
636 }
637
638 void Section::removeSectionReferences(const SectionBase *Sec) {
639   if (LinkSection == Sec) {
640     error("Section " + LinkSection->Name +
641           " cannot be removed because it is "
642           "referenced by the section " +
643           this->Name);
644   }
645 }
646
647 void GroupSection::finalize() {
648   this->Info = Sym->Index;
649   this->Link = SymTab->Index;
650 }
651
652 void GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
653   if (ToRemove(*Sym)) {
654     error("Symbol " + Sym->Name +
655           " cannot be removed because it is "
656           "referenced by the section " +
657           this->Name + "[" + Twine(this->Index) + "]");
658   }
659 }
660
661 void GroupSection::markSymbols() {
662   if (Sym)
663     Sym->Referenced = true;
664 }
665
666 void Section::initialize(SectionTableRef SecTable) {
667   if (Link != ELF::SHN_UNDEF) {
668     LinkSection =
669         SecTable.getSection(Link, "Link field value " + Twine(Link) +
670                                       " in section " + Name + " is invalid");
671     if (LinkSection->Type == ELF::SHT_SYMTAB)
672       LinkSection = nullptr;
673   }
674 }
675
676 void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; }
677
678 void GnuDebugLinkSection::init(StringRef File, StringRef Data) {
679   FileName = sys::path::filename(File);
680   // The format for the .gnu_debuglink starts with the file name and is
681   // followed by a null terminator and then the CRC32 of the file. The CRC32
682   // should be 4 byte aligned. So we add the FileName size, a 1 for the null
683   // byte, and then finally push the size to alignment and add 4.
684   Size = alignTo(FileName.size() + 1, 4) + 4;
685   // The CRC32 will only be aligned if we align the whole section.
686   Align = 4;
687   Type = ELF::SHT_PROGBITS;
688   Name = ".gnu_debuglink";
689   // For sections not found in segments, OriginalOffset is only used to
690   // establish the order that sections should go in. By using the maximum
691   // possible offset we cause this section to wind up at the end.
692   OriginalOffset = std::numeric_limits<uint64_t>::max();
693   JamCRC CRC;
694   CRC.update(ArrayRef<char>(Data.data(), Data.size()));
695   // The CRC32 value needs to be complemented because the JamCRC dosn't
696   // finalize the CRC32 value. It also dosn't negate the initial CRC32 value
697   // but it starts by default at 0xFFFFFFFF which is the complement of zero.
698   CRC32 = ~CRC.getCRC();
699 }
700
701 GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) {
702   // Read in the file to compute the CRC of it.
703   auto DebugOrErr = MemoryBuffer::getFile(File);
704   if (!DebugOrErr)
705     error("'" + File + "': " + DebugOrErr.getError().message());
706   auto Debug = std::move(*DebugOrErr);
707   init(File, Debug->getBuffer());
708 }
709
710 template <class ELFT>
711 void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
712   auto Buf = Out.getBufferStart() + Sec.Offset;
713   char *File = reinterpret_cast<char *>(Buf);
714   Elf_Word *CRC =
715       reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
716   *CRC = Sec.CRC32;
717   llvm::copy(Sec.FileName, File);
718 }
719
720 void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
721   Visitor.visit(*this);
722 }
723
724 void GnuDebugLinkSection::accept(MutableSectionVisitor &Visitor) {
725   Visitor.visit(*this);
726 }
727
728 template <class ELFT>
729 void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
730   ELF::Elf32_Word *Buf =
731       reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
732   *Buf++ = Sec.FlagWord;
733   for (const auto *S : Sec.GroupMembers)
734     support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index);
735 }
736
737 void GroupSection::accept(SectionVisitor &Visitor) const {
738   Visitor.visit(*this);
739 }
740
741 void GroupSection::accept(MutableSectionVisitor &Visitor) {
742   Visitor.visit(*this);
743 }
744
745 // Returns true IFF a section is wholly inside the range of a segment
746 static bool sectionWithinSegment(const SectionBase &Section,
747                                  const Segment &Segment) {
748   // If a section is empty it should be treated like it has a size of 1. This is
749   // to clarify the case when an empty section lies on a boundary between two
750   // segments and ensures that the section "belongs" to the second segment and
751   // not the first.
752   uint64_t SecSize = Section.Size ? Section.Size : 1;
753   return Segment.Offset <= Section.OriginalOffset &&
754          Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
755 }
756
757 // Returns true IFF a segment's original offset is inside of another segment's
758 // range.
759 static bool segmentOverlapsSegment(const Segment &Child,
760                                    const Segment &Parent) {
761
762   return Parent.OriginalOffset <= Child.OriginalOffset &&
763          Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset;
764 }
765
766 static bool compareSegmentsByOffset(const Segment *A, const Segment *B) {
767   // Any segment without a parent segment should come before a segment
768   // that has a parent segment.
769   if (A->OriginalOffset < B->OriginalOffset)
770     return true;
771   if (A->OriginalOffset > B->OriginalOffset)
772     return false;
773   return A->Index < B->Index;
774 }
775
776 static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) {
777   if (A->PAddr < B->PAddr)
778     return true;
779   if (A->PAddr > B->PAddr)
780     return false;
781   return A->Index < B->Index;
782 }
783
784 void BinaryELFBuilder::initFileHeader() {
785   Obj->Flags = 0x0;
786   Obj->Type = ET_REL;
787   Obj->OSABI = ELFOSABI_NONE;
788   Obj->ABIVersion = 0;
789   Obj->Entry = 0x0;
790   Obj->Machine = EMachine;
791   Obj->Version = 1;
792 }
793
794 void BinaryELFBuilder::initHeaderSegment() { Obj->ElfHdrSegment.Index = 0; }
795
796 StringTableSection *BinaryELFBuilder::addStrTab() {
797   auto &StrTab = Obj->addSection<StringTableSection>();
798   StrTab.Name = ".strtab";
799
800   Obj->SectionNames = &StrTab;
801   return &StrTab;
802 }
803
804 SymbolTableSection *BinaryELFBuilder::addSymTab(StringTableSection *StrTab) {
805   auto &SymTab = Obj->addSection<SymbolTableSection>();
806
807   SymTab.Name = ".symtab";
808   SymTab.Link = StrTab->Index;
809
810   // The symbol table always needs a null symbol
811   SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0);
812
813   Obj->SymbolTable = &SymTab;
814   return &SymTab;
815 }
816
817 void BinaryELFBuilder::addData(SymbolTableSection *SymTab) {
818   auto Data = ArrayRef<uint8_t>(
819       reinterpret_cast<const uint8_t *>(MemBuf->getBufferStart()),
820       MemBuf->getBufferSize());
821   auto &DataSection = Obj->addSection<Section>(Data);
822   DataSection.Name = ".data";
823   DataSection.Type = ELF::SHT_PROGBITS;
824   DataSection.Size = Data.size();
825   DataSection.Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
826
827   std::string SanitizedFilename = MemBuf->getBufferIdentifier().str();
828   std::replace_if(std::begin(SanitizedFilename), std::end(SanitizedFilename),
829                   [](char C) { return !isalnum(C); }, '_');
830   Twine Prefix = Twine("_binary_") + SanitizedFilename;
831
832   SymTab->addSymbol(Prefix + "_start", STB_GLOBAL, STT_NOTYPE, &DataSection,
833                     /*Value=*/0, STV_DEFAULT, 0, 0);
834   SymTab->addSymbol(Prefix + "_end", STB_GLOBAL, STT_NOTYPE, &DataSection,
835                     /*Value=*/DataSection.Size, STV_DEFAULT, 0, 0);
836   SymTab->addSymbol(Prefix + "_size", STB_GLOBAL, STT_NOTYPE, nullptr,
837                     /*Value=*/DataSection.Size, STV_DEFAULT, SHN_ABS, 0);
838 }
839
840 void BinaryELFBuilder::initSections() {
841   for (auto &Section : Obj->sections()) {
842     Section.initialize(Obj->sections());
843   }
844 }
845
846 std::unique_ptr<Object> BinaryELFBuilder::build() {
847   initFileHeader();
848   initHeaderSegment();
849   StringTableSection *StrTab = addStrTab();
850   SymbolTableSection *SymTab = addSymTab(StrTab);
851   initSections();
852   addData(SymTab);
853
854   return std::move(Obj);
855 }
856
857 template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) {
858   for (auto &Parent : Obj.segments()) {
859     // Every segment will overlap with itself but we don't want a segment to
860     // be it's own parent so we avoid that situation.
861     if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) {
862       // We want a canonical "most parental" segment but this requires
863       // inspecting the ParentSegment.
864       if (compareSegmentsByOffset(&Parent, &Child))
865         if (Child.ParentSegment == nullptr ||
866             compareSegmentsByOffset(&Parent, Child.ParentSegment)) {
867           Child.ParentSegment = &Parent;
868         }
869     }
870   }
871 }
872
873 template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() {
874   uint32_t Index = 0;
875   for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
876     ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
877                            (size_t)Phdr.p_filesz};
878     Segment &Seg = Obj.addSegment(Data);
879     Seg.Type = Phdr.p_type;
880     Seg.Flags = Phdr.p_flags;
881     Seg.OriginalOffset = Phdr.p_offset;
882     Seg.Offset = Phdr.p_offset;
883     Seg.VAddr = Phdr.p_vaddr;
884     Seg.PAddr = Phdr.p_paddr;
885     Seg.FileSize = Phdr.p_filesz;
886     Seg.MemSize = Phdr.p_memsz;
887     Seg.Align = Phdr.p_align;
888     Seg.Index = Index++;
889     for (auto &Section : Obj.sections()) {
890       if (sectionWithinSegment(Section, Seg)) {
891         Seg.addSection(&Section);
892         if (!Section.ParentSegment ||
893             Section.ParentSegment->Offset > Seg.Offset) {
894           Section.ParentSegment = &Seg;
895         }
896       }
897     }
898   }
899
900   auto &ElfHdr = Obj.ElfHdrSegment;
901   ElfHdr.Index = Index++;
902
903   const auto &Ehdr = *ElfFile.getHeader();
904   auto &PrHdr = Obj.ProgramHdrSegment;
905   PrHdr.Type = PT_PHDR;
906   PrHdr.Flags = 0;
907   // The spec requires us to have p_vaddr % p_align == p_offset % p_align.
908   // Whereas this works automatically for ElfHdr, here OriginalOffset is
909   // always non-zero and to ensure the equation we assign the same value to
910   // VAddr as well.
911   PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = Ehdr.e_phoff;
912   PrHdr.PAddr = 0;
913   PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum;
914   // The spec requires us to naturally align all the fields.
915   PrHdr.Align = sizeof(Elf_Addr);
916   PrHdr.Index = Index++;
917
918   // Now we do an O(n^2) loop through the segments in order to match up
919   // segments.
920   for (auto &Child : Obj.segments())
921     setParentSegment(Child);
922   setParentSegment(ElfHdr);
923   setParentSegment(PrHdr);
924 }
925
926 template <class ELFT>
927 void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) {
928   auto SecTable = Obj.sections();
929   auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>(
930       GroupSec->Link,
931       "Link field value " + Twine(GroupSec->Link) + " in section " +
932           GroupSec->Name + " is invalid",
933       "Link field value " + Twine(GroupSec->Link) + " in section " +
934           GroupSec->Name + " is not a symbol table");
935   auto Sym = SymTab->getSymbolByIndex(GroupSec->Info);
936   if (!Sym)
937     error("Info field value " + Twine(GroupSec->Info) + " in section " +
938           GroupSec->Name + " is not a valid symbol index");
939   GroupSec->setSymTab(SymTab);
940   GroupSec->setSymbol(Sym);
941   if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) ||
942       GroupSec->Contents.empty())
943     error("The content of the section " + GroupSec->Name + " is malformed");
944   const ELF::Elf32_Word *Word =
945       reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data());
946   const ELF::Elf32_Word *End =
947       Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word);
948   GroupSec->setFlagWord(*Word++);
949   for (; Word != End; ++Word) {
950     uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word);
951     GroupSec->addMember(SecTable.getSection(
952         Index, "Group member index " + Twine(Index) + " in section " +
953                    GroupSec->Name + " is invalid"));
954   }
955 }
956
957 template <class ELFT>
958 void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
959   const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
960   StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
961   ArrayRef<Elf_Word> ShndxData;
962
963   auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr));
964   for (const auto &Sym : Symbols) {
965     SectionBase *DefSection = nullptr;
966     StringRef Name = unwrapOrError(Sym.getName(StrTabData));
967
968     if (Sym.st_shndx == SHN_XINDEX) {
969       if (SymTab->getShndxTable() == nullptr)
970         error("Symbol '" + Name +
971               "' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists.");
972       if (ShndxData.data() == nullptr) {
973         const Elf_Shdr &ShndxSec =
974             *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index));
975         ShndxData = unwrapOrError(
976             ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec));
977         if (ShndxData.size() != Symbols.size())
978           error("Symbol section index table does not have the same number of "
979                 "entries as the symbol table.");
980       }
981       Elf_Word Index = ShndxData[&Sym - Symbols.begin()];
982       DefSection = Obj.sections().getSection(
983           Index,
984           "Symbol '" + Name + "' has invalid section index " + Twine(Index));
985     } else if (Sym.st_shndx >= SHN_LORESERVE) {
986       if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
987         error(
988             "Symbol '" + Name +
989             "' has unsupported value greater than or equal to SHN_LORESERVE: " +
990             Twine(Sym.st_shndx));
991       }
992     } else if (Sym.st_shndx != SHN_UNDEF) {
993       DefSection = Obj.sections().getSection(
994           Sym.st_shndx, "Symbol '" + Name +
995                             "' is defined has invalid section index " +
996                             Twine(Sym.st_shndx));
997     }
998
999     SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
1000                       Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size);
1001   }
1002 }
1003
1004 template <class ELFT>
1005 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
1006
1007 template <class ELFT>
1008 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
1009   ToSet = Rela.r_addend;
1010 }
1011
1012 template <class T>
1013 static void initRelocations(RelocationSection *Relocs,
1014                             SymbolTableSection *SymbolTable, T RelRange) {
1015   for (const auto &Rel : RelRange) {
1016     Relocation ToAdd;
1017     ToAdd.Offset = Rel.r_offset;
1018     getAddend(ToAdd.Addend, Rel);
1019     ToAdd.Type = Rel.getType(false);
1020     ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
1021     Relocs->addRelocation(ToAdd);
1022   }
1023 }
1024
1025 SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) {
1026   if (Index == SHN_UNDEF || Index > Sections.size())
1027     error(ErrMsg);
1028   return Sections[Index - 1].get();
1029 }
1030
1031 template <class T>
1032 T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg,
1033                                      Twine TypeErrMsg) {
1034   if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
1035     return Sec;
1036   error(TypeErrMsg);
1037 }
1038
1039 template <class ELFT>
1040 SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
1041   ArrayRef<uint8_t> Data;
1042   switch (Shdr.sh_type) {
1043   case SHT_REL:
1044   case SHT_RELA:
1045     if (Shdr.sh_flags & SHF_ALLOC) {
1046       Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
1047       return Obj.addSection<DynamicRelocationSection>(Data);
1048     }
1049     return Obj.addSection<RelocationSection>();
1050   case SHT_STRTAB:
1051     // If a string table is allocated we don't want to mess with it. That would
1052     // mean altering the memory image. There are no special link types or
1053     // anything so we can just use a Section.
1054     if (Shdr.sh_flags & SHF_ALLOC) {
1055       Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
1056       return Obj.addSection<Section>(Data);
1057     }
1058     return Obj.addSection<StringTableSection>();
1059   case SHT_HASH:
1060   case SHT_GNU_HASH:
1061     // Hash tables should refer to SHT_DYNSYM which we're not going to change.
1062     // Because of this we don't need to mess with the hash tables either.
1063     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
1064     return Obj.addSection<Section>(Data);
1065   case SHT_GROUP:
1066     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
1067     return Obj.addSection<GroupSection>(Data);
1068   case SHT_DYNSYM:
1069     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
1070     return Obj.addSection<DynamicSymbolTableSection>(Data);
1071   case SHT_DYNAMIC:
1072     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
1073     return Obj.addSection<DynamicSection>(Data);
1074   case SHT_SYMTAB: {
1075     auto &SymTab = Obj.addSection<SymbolTableSection>();
1076     Obj.SymbolTable = &SymTab;
1077     return SymTab;
1078   }
1079   case SHT_SYMTAB_SHNDX: {
1080     auto &ShndxSection = Obj.addSection<SectionIndexSection>();
1081     Obj.SectionIndexTable = &ShndxSection;
1082     return ShndxSection;
1083   }
1084   case SHT_NOBITS:
1085     return Obj.addSection<Section>(Data);
1086   default: {
1087     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
1088
1089     if (isDataGnuCompressed(Data) || (Shdr.sh_flags & ELF::SHF_COMPRESSED)) {
1090       uint64_t DecompressedSize, DecompressedAlign;
1091       std::tie(DecompressedSize, DecompressedAlign) =
1092           getDecompressedSizeAndAlignment<ELFT>(Data);
1093       return Obj.addSection<CompressedSection>(Data, DecompressedSize,
1094                                                DecompressedAlign);
1095     }
1096
1097     return Obj.addSection<Section>(Data);
1098   }
1099   }
1100 }
1101
1102 template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
1103   uint32_t Index = 0;
1104   for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
1105     if (Index == 0) {
1106       ++Index;
1107       continue;
1108     }
1109     auto &Sec = makeSection(Shdr);
1110     Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
1111     Sec.Type = Shdr.sh_type;
1112     Sec.Flags = Shdr.sh_flags;
1113     Sec.Addr = Shdr.sh_addr;
1114     Sec.Offset = Shdr.sh_offset;
1115     Sec.OriginalOffset = Shdr.sh_offset;
1116     Sec.Size = Shdr.sh_size;
1117     Sec.Link = Shdr.sh_link;
1118     Sec.Info = Shdr.sh_info;
1119     Sec.Align = Shdr.sh_addralign;
1120     Sec.EntrySize = Shdr.sh_entsize;
1121     Sec.Index = Index++;
1122     Sec.OriginalData =
1123         ArrayRef<uint8_t>(ElfFile.base() + Shdr.sh_offset,
1124                           (Shdr.sh_type == SHT_NOBITS) ? 0 : Shdr.sh_size);
1125   }
1126
1127   // If a section index table exists we'll need to initialize it before we
1128   // initialize the symbol table because the symbol table might need to
1129   // reference it.
1130   if (Obj.SectionIndexTable)
1131     Obj.SectionIndexTable->initialize(Obj.sections());
1132
1133   // Now that all of the sections have been added we can fill out some extra
1134   // details about symbol tables. We need the symbol table filled out before
1135   // any relocations.
1136   if (Obj.SymbolTable) {
1137     Obj.SymbolTable->initialize(Obj.sections());
1138     initSymbolTable(Obj.SymbolTable);
1139   }
1140
1141   // Now that all sections and symbols have been added we can add
1142   // relocations that reference symbols and set the link and info fields for
1143   // relocation sections.
1144   for (auto &Section : Obj.sections()) {
1145     if (&Section == Obj.SymbolTable)
1146       continue;
1147     Section.initialize(Obj.sections());
1148     if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
1149       auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
1150       if (RelSec->Type == SHT_REL)
1151         initRelocations(RelSec, Obj.SymbolTable,
1152                         unwrapOrError(ElfFile.rels(Shdr)));
1153       else
1154         initRelocations(RelSec, Obj.SymbolTable,
1155                         unwrapOrError(ElfFile.relas(Shdr)));
1156     } else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) {
1157       initGroupSection(GroupSec);
1158     }
1159   }
1160 }
1161
1162 template <class ELFT> void ELFBuilder<ELFT>::build() {
1163   const auto &Ehdr = *ElfFile.getHeader();
1164
1165   Obj.OSABI = Ehdr.e_ident[EI_OSABI];
1166   Obj.ABIVersion = Ehdr.e_ident[EI_ABIVERSION];
1167   Obj.Type = Ehdr.e_type;
1168   Obj.Machine = Ehdr.e_machine;
1169   Obj.Version = Ehdr.e_version;
1170   Obj.Entry = Ehdr.e_entry;
1171   Obj.Flags = Ehdr.e_flags;
1172
1173   readSectionHeaders();
1174   readProgramHeaders();
1175
1176   uint32_t ShstrIndex = Ehdr.e_shstrndx;
1177   if (ShstrIndex == SHN_XINDEX)
1178     ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link;
1179
1180   Obj.SectionNames =
1181       Obj.sections().template getSectionOfType<StringTableSection>(
1182           ShstrIndex,
1183           "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
1184               " in elf header " + " is invalid",
1185           "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
1186               " in elf header " + " is not a string table");
1187 }
1188
1189 // A generic size function which computes sizes of any random access range.
1190 template <class R> size_t size(R &&Range) {
1191   return static_cast<size_t>(std::end(Range) - std::begin(Range));
1192 }
1193
1194 Writer::~Writer() {}
1195
1196 Reader::~Reader() {}
1197
1198 std::unique_ptr<Object> BinaryReader::create() const {
1199   return BinaryELFBuilder(MInfo.EMachine, MemBuf).build();
1200 }
1201
1202 std::unique_ptr<Object> ELFReader::create() const {
1203   auto Obj = llvm::make_unique<Object>();
1204   if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) {
1205     ELFBuilder<ELF32LE> Builder(*O, *Obj);
1206     Builder.build();
1207     return Obj;
1208   } else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) {
1209     ELFBuilder<ELF64LE> Builder(*O, *Obj);
1210     Builder.build();
1211     return Obj;
1212   } else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) {
1213     ELFBuilder<ELF32BE> Builder(*O, *Obj);
1214     Builder.build();
1215     return Obj;
1216   } else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
1217     ELFBuilder<ELF64BE> Builder(*O, *Obj);
1218     Builder.build();
1219     return Obj;
1220   }
1221   error("Invalid file type");
1222 }
1223
1224 template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
1225   uint8_t *B = Buf.getBufferStart();
1226   Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(B);
1227   std::fill(Ehdr.e_ident, Ehdr.e_ident + 16, 0);
1228   Ehdr.e_ident[EI_MAG0] = 0x7f;
1229   Ehdr.e_ident[EI_MAG1] = 'E';
1230   Ehdr.e_ident[EI_MAG2] = 'L';
1231   Ehdr.e_ident[EI_MAG3] = 'F';
1232   Ehdr.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1233   Ehdr.e_ident[EI_DATA] =
1234       ELFT::TargetEndianness == support::big ? ELFDATA2MSB : ELFDATA2LSB;
1235   Ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1236   Ehdr.e_ident[EI_OSABI] = Obj.OSABI;
1237   Ehdr.e_ident[EI_ABIVERSION] = Obj.ABIVersion;
1238
1239   Ehdr.e_type = Obj.Type;
1240   Ehdr.e_machine = Obj.Machine;
1241   Ehdr.e_version = Obj.Version;
1242   Ehdr.e_entry = Obj.Entry;
1243   // We have to use the fully-qualified name llvm::size
1244   // since some compilers complain on ambiguous resolution.
1245   Ehdr.e_phnum = llvm::size(Obj.segments());
1246   Ehdr.e_phoff = (Ehdr.e_phnum != 0) ? Obj.ProgramHdrSegment.Offset : 0;
1247   Ehdr.e_phentsize = (Ehdr.e_phnum != 0) ? sizeof(Elf_Phdr) : 0;
1248   Ehdr.e_flags = Obj.Flags;
1249   Ehdr.e_ehsize = sizeof(Elf_Ehdr);
1250   if (WriteSectionHeaders && size(Obj.sections()) != 0) {
1251     Ehdr.e_shentsize = sizeof(Elf_Shdr);
1252     Ehdr.e_shoff = Obj.SHOffset;
1253     // """
1254     // If the number of sections is greater than or equal to
1255     // SHN_LORESERVE (0xff00), this member has the value zero and the actual
1256     // number of section header table entries is contained in the sh_size field
1257     // of the section header at index 0.
1258     // """
1259     auto Shnum = size(Obj.sections()) + 1;
1260     if (Shnum >= SHN_LORESERVE)
1261       Ehdr.e_shnum = 0;
1262     else
1263       Ehdr.e_shnum = Shnum;
1264     // """
1265     // If the section name string table section index is greater than or equal
1266     // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff)
1267     // and the actual index of the section name string table section is
1268     // contained in the sh_link field of the section header at index 0.
1269     // """
1270     if (Obj.SectionNames->Index >= SHN_LORESERVE)
1271       Ehdr.e_shstrndx = SHN_XINDEX;
1272     else
1273       Ehdr.e_shstrndx = Obj.SectionNames->Index;
1274   } else {
1275     Ehdr.e_shentsize = 0;
1276     Ehdr.e_shoff = 0;
1277     Ehdr.e_shnum = 0;
1278     Ehdr.e_shstrndx = 0;
1279   }
1280 }
1281
1282 template <class ELFT> void ELFWriter<ELFT>::writePhdrs() {
1283   for (auto &Seg : Obj.segments())
1284     writePhdr(Seg);
1285 }
1286
1287 template <class ELFT> void ELFWriter<ELFT>::writeShdrs() {
1288   uint8_t *B = Buf.getBufferStart() + Obj.SHOffset;
1289   // This reference serves to write the dummy section header at the begining
1290   // of the file. It is not used for anything else
1291   Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
1292   Shdr.sh_name = 0;
1293   Shdr.sh_type = SHT_NULL;
1294   Shdr.sh_flags = 0;
1295   Shdr.sh_addr = 0;
1296   Shdr.sh_offset = 0;
1297   // See writeEhdr for why we do this.
1298   uint64_t Shnum = size(Obj.sections()) + 1;
1299   if (Shnum >= SHN_LORESERVE)
1300     Shdr.sh_size = Shnum;
1301   else
1302     Shdr.sh_size = 0;
1303   // See writeEhdr for why we do this.
1304   if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE)
1305     Shdr.sh_link = Obj.SectionNames->Index;
1306   else
1307     Shdr.sh_link = 0;
1308   Shdr.sh_info = 0;
1309   Shdr.sh_addralign = 0;
1310   Shdr.sh_entsize = 0;
1311
1312   for (auto &Sec : Obj.sections())
1313     writeShdr(Sec);
1314 }
1315
1316 template <class ELFT> void ELFWriter<ELFT>::writeSectionData() {
1317   for (auto &Sec : Obj.sections())
1318     Sec.accept(*SecWriter);
1319 }
1320
1321 void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) {
1322
1323   auto Iter = std::stable_partition(
1324       std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
1325         if (ToRemove(*Sec))
1326           return false;
1327         if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
1328           if (auto ToRelSec = RelSec->getSection())
1329             return !ToRemove(*ToRelSec);
1330         }
1331         return true;
1332       });
1333   if (SymbolTable != nullptr && ToRemove(*SymbolTable))
1334     SymbolTable = nullptr;
1335   if (SectionNames != nullptr && ToRemove(*SectionNames))
1336     SectionNames = nullptr;
1337   if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable))
1338     SectionIndexTable = nullptr;
1339   // Now make sure there are no remaining references to the sections that will
1340   // be removed. Sometimes it is impossible to remove a reference so we emit
1341   // an error here instead.
1342   for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
1343     for (auto &Segment : Segments)
1344       Segment->removeSection(RemoveSec.get());
1345     for (auto &KeepSec : make_range(std::begin(Sections), Iter))
1346       KeepSec->removeSectionReferences(RemoveSec.get());
1347   }
1348   // Now finally get rid of them all togethor.
1349   Sections.erase(Iter, std::end(Sections));
1350 }
1351
1352 void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
1353   if (!SymbolTable)
1354     return;
1355
1356   for (const SecPtr &Sec : Sections)
1357     Sec->removeSymbols(ToRemove);
1358 }
1359
1360 void Object::sortSections() {
1361   // Put all sections in offset order. Maintain the ordering as closely as
1362   // possible while meeting that demand however.
1363   auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
1364     return A->OriginalOffset < B->OriginalOffset;
1365   };
1366   std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
1367                    CompareSections);
1368 }
1369
1370 static uint64_t alignToAddr(uint64_t Offset, uint64_t Addr, uint64_t Align) {
1371   // Calculate Diff such that (Offset + Diff) & -Align == Addr & -Align.
1372   if (Align == 0)
1373     Align = 1;
1374   auto Diff =
1375       static_cast<int64_t>(Addr % Align) - static_cast<int64_t>(Offset % Align);
1376   // We only want to add to Offset, however, so if Diff < 0 we can add Align and
1377   // (Offset + Diff) & -Align == Addr & -Align will still hold.
1378   if (Diff < 0)
1379     Diff += Align;
1380   return Offset + Diff;
1381 }
1382
1383 // Orders segments such that if x = y->ParentSegment then y comes before x.
1384 static void orderSegments(std::vector<Segment *> &Segments) {
1385   std::stable_sort(std::begin(Segments), std::end(Segments),
1386                    compareSegmentsByOffset);
1387 }
1388
1389 // This function finds a consistent layout for a list of segments starting from
1390 // an Offset. It assumes that Segments have been sorted by OrderSegments and
1391 // returns an Offset one past the end of the last segment.
1392 static uint64_t LayoutSegments(std::vector<Segment *> &Segments,
1393                                uint64_t Offset) {
1394   assert(std::is_sorted(std::begin(Segments), std::end(Segments),
1395                         compareSegmentsByOffset));
1396   // The only way a segment should move is if a section was between two
1397   // segments and that section was removed. If that section isn't in a segment
1398   // then it's acceptable, but not ideal, to simply move it to after the
1399   // segments. So we can simply layout segments one after the other accounting
1400   // for alignment.
1401   for (auto &Segment : Segments) {
1402     // We assume that segments have been ordered by OriginalOffset and Index
1403     // such that a parent segment will always come before a child segment in
1404     // OrderedSegments. This means that the Offset of the ParentSegment should
1405     // already be set and we can set our offset relative to it.
1406     if (Segment->ParentSegment != nullptr) {
1407       auto Parent = Segment->ParentSegment;
1408       Segment->Offset =
1409           Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset;
1410     } else {
1411       Offset = alignToAddr(Offset, Segment->VAddr, Segment->Align);
1412       Segment->Offset = Offset;
1413     }
1414     Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
1415   }
1416   return Offset;
1417 }
1418
1419 // This function finds a consistent layout for a list of sections. It assumes
1420 // that the ->ParentSegment of each section has already been laid out. The
1421 // supplied starting Offset is used for the starting offset of any section that
1422 // does not have a ParentSegment. It returns either the offset given if all
1423 // sections had a ParentSegment or an offset one past the last section if there
1424 // was a section that didn't have a ParentSegment.
1425 template <class Range>
1426 static uint64_t layoutSections(Range Sections, uint64_t Offset) {
1427   // Now the offset of every segment has been set we can assign the offsets
1428   // of each section. For sections that are covered by a segment we should use
1429   // the segment's original offset and the section's original offset to compute
1430   // the offset from the start of the segment. Using the offset from the start
1431   // of the segment we can assign a new offset to the section. For sections not
1432   // covered by segments we can just bump Offset to the next valid location.
1433   uint32_t Index = 1;
1434   for (auto &Section : Sections) {
1435     Section.Index = Index++;
1436     if (Section.ParentSegment != nullptr) {
1437       auto Segment = *Section.ParentSegment;
1438       Section.Offset =
1439           Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
1440     } else {
1441       Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
1442       Section.Offset = Offset;
1443       if (Section.Type != SHT_NOBITS)
1444         Offset += Section.Size;
1445     }
1446   }
1447   return Offset;
1448 }
1449
1450 template <class ELFT> void ELFWriter<ELFT>::initEhdrSegment() {
1451   auto &ElfHdr = Obj.ElfHdrSegment;
1452   ElfHdr.Type = PT_PHDR;
1453   ElfHdr.Flags = 0;
1454   ElfHdr.OriginalOffset = ElfHdr.Offset = 0;
1455   ElfHdr.VAddr = 0;
1456   ElfHdr.PAddr = 0;
1457   ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr);
1458   ElfHdr.Align = 0;
1459 }
1460
1461 template <class ELFT> void ELFWriter<ELFT>::assignOffsets() {
1462   // We need a temporary list of segments that has a special order to it
1463   // so that we know that anytime ->ParentSegment is set that segment has
1464   // already had its offset properly set.
1465   std::vector<Segment *> OrderedSegments;
1466   for (auto &Segment : Obj.segments())
1467     OrderedSegments.push_back(&Segment);
1468   OrderedSegments.push_back(&Obj.ElfHdrSegment);
1469   OrderedSegments.push_back(&Obj.ProgramHdrSegment);
1470   orderSegments(OrderedSegments);
1471   // Offset is used as the start offset of the first segment to be laid out.
1472   // Since the ELF Header (ElfHdrSegment) must be at the start of the file,
1473   // we start at offset 0.
1474   uint64_t Offset = 0;
1475   Offset = LayoutSegments(OrderedSegments, Offset);
1476   Offset = layoutSections(Obj.sections(), Offset);
1477   // If we need to write the section header table out then we need to align the
1478   // Offset so that SHOffset is valid.
1479   if (WriteSectionHeaders)
1480     Offset = alignTo(Offset, sizeof(Elf_Addr));
1481   Obj.SHOffset = Offset;
1482 }
1483
1484 template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const {
1485   // We already have the section header offset so we can calculate the total
1486   // size by just adding up the size of each section header.
1487   auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
1488   return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) +
1489          NullSectionSize;
1490 }
1491
1492 template <class ELFT> void ELFWriter<ELFT>::write() {
1493   writeEhdr();
1494   writePhdrs();
1495   writeSectionData();
1496   if (WriteSectionHeaders)
1497     writeShdrs();
1498   if (auto E = Buf.commit())
1499     reportError(Buf.getName(), errorToErrorCode(std::move(E)));
1500 }
1501
1502 template <class ELFT> void ELFWriter<ELFT>::finalize() {
1503   // It could happen that SectionNames has been removed and yet the user wants
1504   // a section header table output. We need to throw an error if a user tries
1505   // to do that.
1506   if (Obj.SectionNames == nullptr && WriteSectionHeaders)
1507     error("Cannot write section header table because section header string "
1508           "table was removed.");
1509
1510   Obj.sortSections();
1511
1512   // We need to assign indexes before we perform layout because we need to know
1513   // if we need large indexes or not. We can assign indexes first and check as
1514   // we go to see if we will actully need large indexes.
1515   bool NeedsLargeIndexes = false;
1516   if (size(Obj.sections()) >= SHN_LORESERVE) {
1517     auto Sections = Obj.sections();
1518     NeedsLargeIndexes =
1519         std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(),
1520                     [](const SectionBase &Sec) { return Sec.HasSymbol; });
1521     // TODO: handle case where only one section needs the large index table but
1522     // only needs it because the large index table hasn't been removed yet.
1523   }
1524
1525   if (NeedsLargeIndexes) {
1526     // This means we definitely need to have a section index table but if we
1527     // already have one then we should use it instead of making a new one.
1528     if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) {
1529       // Addition of a section to the end does not invalidate the indexes of
1530       // other sections and assigns the correct index to the new section.
1531       auto &Shndx = Obj.addSection<SectionIndexSection>();
1532       Obj.SymbolTable->setShndxTable(&Shndx);
1533       Shndx.setSymTab(Obj.SymbolTable);
1534     }
1535   } else {
1536     // Since we don't need SectionIndexTable we should remove it and all
1537     // references to it.
1538     if (Obj.SectionIndexTable != nullptr) {
1539       Obj.removeSections([this](const SectionBase &Sec) {
1540         return &Sec == Obj.SectionIndexTable;
1541       });
1542     }
1543   }
1544
1545   // Make sure we add the names of all the sections. Importantly this must be
1546   // done after we decide to add or remove SectionIndexes.
1547   if (Obj.SectionNames != nullptr)
1548     for (const auto &Section : Obj.sections()) {
1549       Obj.SectionNames->addString(Section.Name);
1550     }
1551
1552   initEhdrSegment();
1553
1554   // Before we can prepare for layout the indexes need to be finalized.
1555   // Also, the output arch may not be the same as the input arch, so fix up
1556   // size-related fields before doing layout calculations.
1557   uint64_t Index = 0;
1558   auto SecSizer = llvm::make_unique<ELFSectionSizer<ELFT>>();
1559   for (auto &Sec : Obj.sections()) {
1560     Sec.Index = Index++;
1561     Sec.accept(*SecSizer);
1562   }
1563
1564   // The symbol table does not update all other sections on update. For
1565   // instance, symbol names are not added as new symbols are added. This means
1566   // that some sections, like .strtab, don't yet have their final size.
1567   if (Obj.SymbolTable != nullptr)
1568     Obj.SymbolTable->prepareForLayout();
1569
1570   assignOffsets();
1571
1572   // Finalize SectionNames first so that we can assign name indexes.
1573   if (Obj.SectionNames != nullptr)
1574     Obj.SectionNames->finalize();
1575   // Finally now that all offsets and indexes have been set we can finalize any
1576   // remaining issues.
1577   uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
1578   for (auto &Section : Obj.sections()) {
1579     Section.HeaderOffset = Offset;
1580     Offset += sizeof(Elf_Shdr);
1581     if (WriteSectionHeaders)
1582       Section.NameIndex = Obj.SectionNames->findIndex(Section.Name);
1583     Section.finalize();
1584   }
1585
1586   Buf.allocate(totalSize());
1587   SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(Buf);
1588 }
1589
1590 void BinaryWriter::write() {
1591   for (auto &Section : Obj.sections()) {
1592     if ((Section.Flags & SHF_ALLOC) == 0)
1593       continue;
1594     Section.accept(*SecWriter);
1595   }
1596   if (auto E = Buf.commit())
1597     reportError(Buf.getName(), errorToErrorCode(std::move(E)));
1598 }
1599
1600 void BinaryWriter::finalize() {
1601   // TODO: Create a filter range to construct OrderedSegments from so that this
1602   // code can be deduped with assignOffsets above. This should also solve the
1603   // todo below for LayoutSections.
1604   // We need a temporary list of segments that has a special order to it
1605   // so that we know that anytime ->ParentSegment is set that segment has
1606   // already had it's offset properly set. We only want to consider the segments
1607   // that will affect layout of allocated sections so we only add those.
1608   std::vector<Segment *> OrderedSegments;
1609   for (auto &Section : Obj.sections()) {
1610     if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) {
1611       OrderedSegments.push_back(Section.ParentSegment);
1612     }
1613   }
1614
1615   // For binary output, we're going to use physical addresses instead of
1616   // virtual addresses, since a binary output is used for cases like ROM
1617   // loading and physical addresses are intended for ROM loading.
1618   // However, if no segment has a physical address, we'll fallback to using
1619   // virtual addresses for all.
1620   if (all_of(OrderedSegments,
1621              [](const Segment *Seg) { return Seg->PAddr == 0; }))
1622     for (Segment *Seg : OrderedSegments)
1623       Seg->PAddr = Seg->VAddr;
1624
1625   std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments),
1626                    compareSegmentsByPAddr);
1627
1628   // Because we add a ParentSegment for each section we might have duplicate
1629   // segments in OrderedSegments. If there were duplicates then LayoutSegments
1630   // would do very strange things.
1631   auto End =
1632       std::unique(std::begin(OrderedSegments), std::end(OrderedSegments));
1633   OrderedSegments.erase(End, std::end(OrderedSegments));
1634
1635   uint64_t Offset = 0;
1636
1637   // Modify the first segment so that there is no gap at the start. This allows
1638   // our layout algorithm to proceed as expected while not writing out the gap
1639   // at the start.
1640   if (!OrderedSegments.empty()) {
1641     auto Seg = OrderedSegments[0];
1642     auto Sec = Seg->firstSection();
1643     auto Diff = Sec->OriginalOffset - Seg->OriginalOffset;
1644     Seg->OriginalOffset += Diff;
1645     // The size needs to be shrunk as well.
1646     Seg->FileSize -= Diff;
1647     // The PAddr needs to be increased to remove the gap before the first
1648     // section.
1649     Seg->PAddr += Diff;
1650     uint64_t LowestPAddr = Seg->PAddr;
1651     for (auto &Segment : OrderedSegments) {
1652       Segment->Offset = Segment->PAddr - LowestPAddr;
1653       Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
1654     }
1655   }
1656
1657   // TODO: generalize LayoutSections to take a range. Pass a special range
1658   // constructed from an iterator that skips values for which a predicate does
1659   // not hold. Then pass such a range to LayoutSections instead of constructing
1660   // AllocatedSections here.
1661   std::vector<SectionBase *> AllocatedSections;
1662   for (auto &Section : Obj.sections()) {
1663     if ((Section.Flags & SHF_ALLOC) == 0)
1664       continue;
1665     AllocatedSections.push_back(&Section);
1666   }
1667   layoutSections(make_pointee_range(AllocatedSections), Offset);
1668
1669   // Now that every section has been laid out we just need to compute the total
1670   // file size. This might not be the same as the offset returned by
1671   // LayoutSections, because we want to truncate the last segment to the end of
1672   // its last section, to match GNU objcopy's behaviour.
1673   TotalSize = 0;
1674   for (const auto &Section : AllocatedSections) {
1675     if (Section->Type != SHT_NOBITS)
1676       TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
1677   }
1678
1679   Buf.allocate(TotalSize);
1680   SecWriter = llvm::make_unique<BinarySectionWriter>(Buf);
1681 }
1682
1683 template class ELFBuilder<ELF64LE>;
1684 template class ELFBuilder<ELF64BE>;
1685 template class ELFBuilder<ELF32LE>;
1686 template class ELFBuilder<ELF32BE>;
1687
1688 template class ELFWriter<ELF64LE>;
1689 template class ELFWriter<ELF64BE>;
1690 template class ELFWriter<ELF32LE>;
1691 template class ELFWriter<ELF32BE>;
1692
1693 } // end namespace elf
1694 } // end namespace objcopy
1695 } // end namespace llvm