OSDN Git Service

[llvm-objcopy] Add e_machine validity check for reserved section indexes
[android-x86/external-llvm.git] / tools / llvm-objcopy / Object.cpp
1 //===- Object.cpp -----------------------------------------------*- C++ -*-===//
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 #include "Object.h"
10 #include "llvm-objcopy.h"
11
12 using namespace llvm;
13 using namespace object;
14 using namespace ELF;
15
16 template <class ELFT> void Segment::writeHeader(FileOutputBuffer &Out) const {
17   typedef typename ELFT::Ehdr Elf_Ehdr;
18   typedef typename ELFT::Phdr Elf_Phdr;
19
20   uint8_t *Buf = Out.getBufferStart();
21   Buf += sizeof(Elf_Ehdr) + Index * sizeof(Elf_Phdr);
22   Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(Buf);
23   Phdr.p_type = Type;
24   Phdr.p_flags = Flags;
25   Phdr.p_offset = Offset;
26   Phdr.p_vaddr = VAddr;
27   Phdr.p_paddr = PAddr;
28   Phdr.p_filesz = FileSize;
29   Phdr.p_memsz = MemSize;
30   Phdr.p_align = Align;
31 }
32
33 void Segment::finalize() {
34   auto FirstSec = firstSection();
35   if (FirstSec) {
36     // It is possible for a gap to be at the begining of a segment. Because of
37     // this we need to compute the new offset based on how large this gap was
38     // in the source file. Section layout should have already ensured that this
39     // space is not used for something else.
40     uint64_t OriginalOffset = Offset;
41     Offset = FirstSec->Offset - (FirstSec->OriginalOffset - OriginalOffset);
42   }
43 }
44
45 void Segment::writeSegment(FileOutputBuffer &Out) const {
46   uint8_t *Buf = Out.getBufferStart() + Offset;
47   // We want to maintain segments' interstitial data and contents exactly.
48   // This lets us just copy segments directly.
49   std::copy(std::begin(Contents), std::end(Contents), Buf);
50 }
51
52 void SectionBase::finalize() {}
53
54 template <class ELFT>
55 void SectionBase::writeHeader(FileOutputBuffer &Out) const {
56   uint8_t *Buf = Out.getBufferStart();
57   Buf += HeaderOffset;
58   typename ELFT::Shdr &Shdr = *reinterpret_cast<typename ELFT::Shdr *>(Buf);
59   Shdr.sh_name = NameIndex;
60   Shdr.sh_type = Type;
61   Shdr.sh_flags = Flags;
62   Shdr.sh_addr = Addr;
63   Shdr.sh_offset = Offset;
64   Shdr.sh_size = Size;
65   Shdr.sh_link = Link;
66   Shdr.sh_info = Info;
67   Shdr.sh_addralign = Align;
68   Shdr.sh_entsize = EntrySize;
69 }
70
71 void Section::writeSection(FileOutputBuffer &Out) const {
72   if (Type == SHT_NOBITS)
73     return;
74   uint8_t *Buf = Out.getBufferStart() + Offset;
75   std::copy(std::begin(Contents), std::end(Contents), Buf);
76 }
77
78 void StringTableSection::addString(StringRef Name) {
79   StrTabBuilder.add(Name);
80   Size = StrTabBuilder.getSize();
81 }
82
83 uint32_t StringTableSection::findIndex(StringRef Name) const {
84   return StrTabBuilder.getOffset(Name);
85 }
86
87 void StringTableSection::finalize() { StrTabBuilder.finalize(); }
88
89 void StringTableSection::writeSection(FileOutputBuffer &Out) const {
90   StrTabBuilder.write(Out.getBufferStart() + Offset);
91 }
92
93 static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
94   switch (Index) {
95   case SHN_ABS:
96   case SHN_COMMON:
97     return true;
98   }
99   if (Machine == EM_HEXAGON) {
100     switch (Index) {
101     case SHN_HEXAGON_SCOMMON:
102     case SHN_HEXAGON_SCOMMON_2:
103     case SHN_HEXAGON_SCOMMON_4:
104     case SHN_HEXAGON_SCOMMON_8:
105       return true;
106     }
107   }
108   return false;
109 }
110
111 uint16_t Symbol::getShndx() const {
112   if (DefinedIn != nullptr) {
113     return DefinedIn->Index;
114   }
115   switch (ShndxType) {
116   // This means that we don't have a defined section but we do need to
117   // output a legitimate section index.
118   case SYMBOL_SIMPLE_INDEX:
119     return SHN_UNDEF;
120   case SYMBOL_ABS:
121   case SYMBOL_COMMON:
122   case SYMBOL_HEXAGON_SCOMMON:
123   case SYMBOL_HEXAGON_SCOMMON_2:
124   case SYMBOL_HEXAGON_SCOMMON_4:
125   case SYMBOL_HEXAGON_SCOMMON_8:
126     return static_cast<uint16_t>(ShndxType);
127   }
128   llvm_unreachable("Symbol with invalid ShndxType encountered");
129 }
130
131 void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
132                                    SectionBase *DefinedIn, uint64_t Value,
133                                    uint16_t Shndx, uint64_t Sz) {
134   Symbol Sym;
135   Sym.Name = Name;
136   Sym.Binding = Bind;
137   Sym.Type = Type;
138   Sym.DefinedIn = DefinedIn;
139   if (DefinedIn == nullptr) {
140     if (Shndx >= SHN_LORESERVE)
141       Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
142     else
143       Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
144   }
145   Sym.Value = Value;
146   Sym.Size = Sz;
147   Sym.Index = Symbols.size();
148   Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
149   Size += this->EntrySize;
150 }
151
152 void SymbolTableSection::finalize() {
153   // Make sure SymbolNames is finalized before getting name indexes.
154   SymbolNames->finalize();
155
156   uint32_t MaxLocalIndex = 0;
157   for (auto &Sym : Symbols) {
158     Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
159     if (Sym->Binding == STB_LOCAL)
160       MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
161   }
162   // Now we need to set the Link and Info fields.
163   Link = SymbolNames->Index;
164   Info = MaxLocalIndex + 1;
165 }
166
167 void SymbolTableSection::addSymbolNames() {
168   // Add all of our strings to SymbolNames so that SymbolNames has the right
169   // size before layout is decided.
170   for (auto &Sym : Symbols)
171     SymbolNames->addString(Sym->Name);
172 }
173
174 const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
175   if (Symbols.size() <= Index)
176     error("Invalid symbol index: " + Twine(Index));
177   return Symbols[Index].get();
178 }
179
180 template <class ELFT>
181 void SymbolTableSectionImpl<ELFT>::writeSection(
182     llvm::FileOutputBuffer &Out) const {
183   uint8_t *Buf = Out.getBufferStart();
184   Buf += Offset;
185   typename ELFT::Sym *Sym = reinterpret_cast<typename ELFT::Sym *>(Buf);
186   // Loop though symbols setting each entry of the symbol table.
187   for (auto &Symbol : Symbols) {
188     Sym->st_name = Symbol->NameIndex;
189     Sym->st_value = Symbol->Value;
190     Sym->st_size = Symbol->Size;
191     Sym->setBinding(Symbol->Binding);
192     Sym->setType(Symbol->Type);
193     Sym->st_shndx = Symbol->getShndx();
194     ++Sym;
195   }
196 }
197
198 template <class ELFT> void RelocationSection<ELFT>::finalize() {
199   this->Link = Symbols->Index;
200   this->Info = SecToApplyRel->Index;
201 }
202
203 template <class ELFT>
204 void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
205
206 template <class ELFT>
207 void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
208   Rela.r_addend = Addend;
209 }
210
211 template <class ELFT>
212 template <class T>
213 void RelocationSection<ELFT>::writeRel(T *Buf) const {
214   for (const auto &Reloc : Relocations) {
215     Buf->r_offset = Reloc.Offset;
216     setAddend(*Buf, Reloc.Addend);
217     Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
218     ++Buf;
219   }
220 }
221
222 template <class ELFT>
223 void RelocationSection<ELFT>::writeSection(llvm::FileOutputBuffer &Out) const {
224   uint8_t *Buf = Out.getBufferStart() + Offset;
225   if (Type == SHT_REL)
226     writeRel(reinterpret_cast<Elf_Rel *>(Buf));
227   else
228     writeRel(reinterpret_cast<Elf_Rela *>(Buf));
229 }
230
231 // Returns true IFF a section is wholly inside the range of a segment
232 static bool sectionWithinSegment(const SectionBase &Section,
233                                  const Segment &Segment) {
234   // If a section is empty it should be treated like it has a size of 1. This is
235   // to clarify the case when an empty section lies on a boundary between two
236   // segments and ensures that the section "belongs" to the second segment and
237   // not the first.
238   uint64_t SecSize = Section.Size ? Section.Size : 1;
239   return Segment.Offset <= Section.OriginalOffset &&
240          Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
241 }
242
243 template <class ELFT>
244 void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) {
245   uint32_t Index = 0;
246   for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
247     ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
248                            (size_t)Phdr.p_filesz};
249     Segments.emplace_back(llvm::make_unique<Segment>(Data));
250     Segment &Seg = *Segments.back();
251     Seg.Type = Phdr.p_type;
252     Seg.Flags = Phdr.p_flags;
253     Seg.OriginalOffset = Phdr.p_offset;
254     Seg.Offset = Phdr.p_offset;
255     Seg.VAddr = Phdr.p_vaddr;
256     Seg.PAddr = Phdr.p_paddr;
257     Seg.FileSize = Phdr.p_filesz;
258     Seg.MemSize = Phdr.p_memsz;
259     Seg.Align = Phdr.p_align;
260     Seg.Index = Index++;
261     for (auto &Section : Sections) {
262       if (sectionWithinSegment(*Section, Seg)) {
263         Seg.addSection(&*Section);
264         if (!Section->ParentSegment ||
265             Section->ParentSegment->Offset > Seg.Offset) {
266           Section->ParentSegment = &Seg;
267         }
268       }
269     }
270   }
271 }
272
273 template <class ELFT>
274 void Object<ELFT>::initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
275                                    SymbolTableSection *SymTab) {
276
277   SymTab->Size = 0;
278   if (SymbolTable->Link - 1 >= Sections.size())
279     error("Symbol table has link index of " + Twine(SymbolTable->Link) +
280           " which is not a valid index");
281
282   if (auto StrTab =
283           dyn_cast<StringTableSection>(Sections[SymbolTable->Link - 1].get()))
284     SymTab->setStrTab(StrTab);
285   else
286     error("Symbol table has link index of " + Twine(SymbolTable->Link) +
287           "which is not a string table");
288
289   const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
290   StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
291
292   for (const auto &Sym : unwrapOrError(ElfFile.symbols(&Shdr))) {
293     SectionBase *DefSection = nullptr;
294     StringRef Name = unwrapOrError(Sym.getName(StrTabData));
295     if (Sym.st_shndx >= SHN_LORESERVE) {
296       if (!isValidReservedSectionIndex(Sym.st_shndx, Machine)) {
297         error(
298             "Symbol '" + Name +
299             "' has unsupported value greater than or equal to SHN_LORESERVE: " +
300             Twine(Sym.st_shndx));
301       }
302     } else if (Sym.st_shndx != SHN_UNDEF) {
303       if (Sym.st_shndx >= Sections.size())
304         error("Symbol '" + Name +
305               "' is defined in invalid section with index " +
306               Twine(Sym.st_shndx));
307       DefSection = Sections[Sym.st_shndx - 1].get();
308     }
309     SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
310                       Sym.getValue(), Sym.st_shndx, Sym.st_size);
311   }
312 }
313
314 template <class ELFT>
315 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
316
317 template <class ELFT>
318 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
319   ToSet = Rela.r_addend;
320 }
321
322 template <class ELFT, class T>
323 void initRelocations(RelocationSection<ELFT> *Relocs,
324                      SymbolTableSection *SymbolTable, T RelRange) {
325   for (const auto &Rel : RelRange) {
326     Relocation ToAdd;
327     ToAdd.Offset = Rel.r_offset;
328     getAddend(ToAdd.Addend, Rel);
329     ToAdd.Type = Rel.getType(false);
330     ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
331     Relocs->addRelocation(ToAdd);
332   }
333 }
334
335 template <class ELFT>
336 std::unique_ptr<SectionBase>
337 Object<ELFT>::makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
338                           const Elf_Shdr &Shdr) {
339   ArrayRef<uint8_t> Data;
340   switch (Shdr.sh_type) {
341   case SHT_REL:
342   case SHT_RELA:
343     return llvm::make_unique<RelocationSection<ELFT>>();
344   case SHT_STRTAB:
345     return llvm::make_unique<StringTableSection>();
346   case SHT_SYMTAB: {
347     auto SymTab = llvm::make_unique<SymbolTableSectionImpl<ELFT>>();
348     SymbolTable = SymTab.get();
349     return std::move(SymTab);
350   }
351   case SHT_NOBITS:
352     return llvm::make_unique<Section>(Data);
353   default:
354     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
355     return llvm::make_unique<Section>(Data);
356   }
357 }
358
359 template <class ELFT>
360 void Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) {
361   uint32_t Index = 0;
362   for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
363     if (Index == 0) {
364       ++Index;
365       continue;
366     }
367     SecPtr Sec = makeSection(ElfFile, Shdr);
368     Sec->Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
369     Sec->Type = Shdr.sh_type;
370     Sec->Flags = Shdr.sh_flags;
371     Sec->Addr = Shdr.sh_addr;
372     Sec->Offset = Shdr.sh_offset;
373     Sec->OriginalOffset = Shdr.sh_offset;
374     Sec->Size = Shdr.sh_size;
375     Sec->Link = Shdr.sh_link;
376     Sec->Info = Shdr.sh_info;
377     Sec->Align = Shdr.sh_addralign;
378     Sec->EntrySize = Shdr.sh_entsize;
379     Sec->Index = Index++;
380     Sections.push_back(std::move(Sec));
381   }
382
383   // Now that all of the sections have been added we can fill out some extra
384   // details about symbol tables.
385   if (SymbolTable)
386     initSymbolTable(ElfFile, SymbolTable);
387
388   // Now that all sections and symbols have been added we can add
389   // relocations that reference symbols and set the link and info fields for
390   // relocation sections.
391   for (auto &Section : Sections) {
392     if (auto RelSec = dyn_cast<RelocationSection<ELFT>>(Section.get())) {
393       if (RelSec->Link - 1 >= Sections.size() || RelSec->Link == 0) {
394         error("Link field value " + Twine(RelSec->Link) + " in section " +
395               RelSec->Name + " is invalid");
396       }
397       if (RelSec->Info - 1 >= Sections.size() || RelSec->Info == 0) {
398         error("Info field value " + Twine(RelSec->Link) + " in section " +
399               RelSec->Name + " is invalid");
400       }
401       auto SymTab =
402           dyn_cast<SymbolTableSection>(Sections[RelSec->Link - 1].get());
403       if (SymTab == nullptr) {
404         error("Link field of relocation section " + RelSec->Name +
405               " is not a symbol table");
406       }
407       RelSec->setSymTab(SymTab);
408       RelSec->setSection(Sections[RelSec->Info - 1].get());
409       auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
410       if (RelSec->Type == SHT_REL)
411         initRelocations(RelSec, SymTab, unwrapOrError(ElfFile.rels(Shdr)));
412       else
413         initRelocations(RelSec, SymTab, unwrapOrError(ElfFile.relas(Shdr)));
414     }
415   }
416 }
417
418 template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) {
419   const auto &ElfFile = *Obj.getELFFile();
420   const auto &Ehdr = *ElfFile.getHeader();
421
422   std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Ident);
423   Type = Ehdr.e_type;
424   Machine = Ehdr.e_machine;
425   Version = Ehdr.e_version;
426   Entry = Ehdr.e_entry;
427   Flags = Ehdr.e_flags;
428
429   readSectionHeaders(ElfFile);
430   readProgramHeaders(ElfFile);
431
432   SectionNames =
433       dyn_cast<StringTableSection>(Sections[Ehdr.e_shstrndx - 1].get());
434 }
435
436 template <class ELFT>
437 void Object<ELFT>::writeHeader(FileOutputBuffer &Out) const {
438   uint8_t *Buf = Out.getBufferStart();
439   Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(Buf);
440   std::copy(Ident, Ident + 16, Ehdr.e_ident);
441   Ehdr.e_type = Type;
442   Ehdr.e_machine = Machine;
443   Ehdr.e_version = Version;
444   Ehdr.e_entry = Entry;
445   Ehdr.e_phoff = sizeof(Elf_Ehdr);
446   Ehdr.e_shoff = SHOffset;
447   Ehdr.e_flags = Flags;
448   Ehdr.e_ehsize = sizeof(Elf_Ehdr);
449   Ehdr.e_phentsize = sizeof(Elf_Phdr);
450   Ehdr.e_phnum = Segments.size();
451   Ehdr.e_shentsize = sizeof(Elf_Shdr);
452   Ehdr.e_shnum = Sections.size() + 1;
453   Ehdr.e_shstrndx = SectionNames->Index;
454 }
455
456 template <class ELFT>
457 void Object<ELFT>::writeProgramHeaders(FileOutputBuffer &Out) const {
458   for (auto &Phdr : Segments)
459     Phdr->template writeHeader<ELFT>(Out);
460 }
461
462 template <class ELFT>
463 void Object<ELFT>::writeSectionHeaders(FileOutputBuffer &Out) const {
464   uint8_t *Buf = Out.getBufferStart() + SHOffset;
465   // This reference serves to write the dummy section header at the begining
466   // of the file.
467   Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(Buf);
468   Shdr.sh_name = 0;
469   Shdr.sh_type = SHT_NULL;
470   Shdr.sh_flags = 0;
471   Shdr.sh_addr = 0;
472   Shdr.sh_offset = 0;
473   Shdr.sh_size = 0;
474   Shdr.sh_link = 0;
475   Shdr.sh_info = 0;
476   Shdr.sh_addralign = 0;
477   Shdr.sh_entsize = 0;
478
479   for (auto &Section : Sections)
480     Section->template writeHeader<ELFT>(Out);
481 }
482
483 template <class ELFT>
484 void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const {
485   for (auto &Section : Sections)
486     Section->writeSection(Out);
487 }
488
489 template <class ELFT> void ELFObject<ELFT>::sortSections() {
490   // Put all sections in offset order. Maintain the ordering as closely as
491   // possible while meeting that demand however.
492   auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
493     return A->OriginalOffset < B->OriginalOffset;
494   };
495   std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
496                    CompareSections);
497 }
498
499 template <class ELFT> void ELFObject<ELFT>::assignOffsets() {
500   // The size of ELF + program headers will not change so it is ok to assume
501   // that the first offset of the first segment is a good place to start
502   // outputting sections. This covers both the standard case and the PT_PHDR
503   // case.
504   uint64_t Offset;
505   if (!this->Segments.empty()) {
506     Offset = this->Segments[0]->Offset;
507   } else {
508     Offset = sizeof(Elf_Ehdr);
509   }
510   // The only way a segment should move is if a section was between two
511   // segments and that section was removed. If that section isn't in a segment
512   // then it's acceptable, but not ideal, to simply move it to after the
513   // segments. So we can simply layout segments one after the other accounting
514   // for alignment.
515   for (auto &Segment : this->Segments) {
516     Offset = alignTo(Offset, Segment->Align);
517     Segment->Offset = Offset;
518     Offset += Segment->FileSize;
519   }
520   // Now the offset of every segment has been set we can assign the offsets
521   // of each section. For sections that are covered by a segment we should use
522   // the segment's original offset and the section's original offset to compute
523   // the offset from the start of the segment. Using the offset from the start
524   // of the segment we can assign a new offset to the section. For sections not
525   // covered by segments we can just bump Offset to the next valid location.
526   uint32_t Index = 1;
527   for (auto &Section : this->Sections) {
528     Section->Index = Index++;
529     if (Section->ParentSegment != nullptr) {
530       auto Segment = Section->ParentSegment;
531       Section->Offset =
532           Segment->Offset + (Section->OriginalOffset - Segment->OriginalOffset);
533     } else {
534       Offset = alignTo(Offset, Section->Offset);
535       Section->Offset = Offset;
536       if (Section->Type != SHT_NOBITS)
537         Offset += Section->Size;
538     }
539   }
540
541   Offset = alignTo(Offset, sizeof(typename ELFT::Addr));
542   this->SHOffset = Offset;
543 }
544
545 template <class ELFT> size_t ELFObject<ELFT>::totalSize() const {
546   // We already have the section header offset so we can calculate the total
547   // size by just adding up the size of each section header.
548   return this->SHOffset + this->Sections.size() * sizeof(Elf_Shdr) +
549          sizeof(Elf_Shdr);
550 }
551
552 template <class ELFT> void ELFObject<ELFT>::write(FileOutputBuffer &Out) const {
553   this->writeHeader(Out);
554   this->writeProgramHeaders(Out);
555   this->writeSectionData(Out);
556   this->writeSectionHeaders(Out);
557 }
558
559 template <class ELFT> void ELFObject<ELFT>::finalize() {
560   // Make sure we add the names of all the sections.
561   for (const auto &Section : this->Sections) {
562     this->SectionNames->addString(Section->Name);
563   }
564   // Make sure we add the names of all the symbols.
565   this->SymbolTable->addSymbolNames();
566
567   sortSections();
568   assignOffsets();
569
570   // Finalize SectionNames first so that we can assign name indexes.
571   this->SectionNames->finalize();
572   // Finally now that all offsets and indexes have been set we can finalize any
573   // remaining issues.
574   uint64_t Offset = this->SHOffset + sizeof(Elf_Shdr);
575   for (auto &Section : this->Sections) {
576     Section->HeaderOffset = Offset;
577     Offset += sizeof(Elf_Shdr);
578     Section->NameIndex = this->SectionNames->findIndex(Section->Name);
579     Section->finalize();
580   }
581
582   for (auto &Segment : this->Segments)
583     Segment->finalize();
584 }
585
586 template <class ELFT> size_t BinaryObject<ELFT>::totalSize() const {
587   return TotalSize;
588 }
589
590 template <class ELFT>
591 void BinaryObject<ELFT>::write(FileOutputBuffer &Out) const {
592   for (auto &Segment : this->Segments) {
593     // GNU objcopy does not output segments that do not cover a section. Such
594     // segments can sometimes be produced by LLD due to how LLD handles PT_PHDR.
595     if (Segment->Type == llvm::ELF::PT_LOAD &&
596         Segment->firstSection() != nullptr) {
597       Segment->writeSegment(Out);
598     }
599   }
600 }
601
602 template <class ELFT> void BinaryObject<ELFT>::finalize() {
603   for (auto &Segment : this->Segments)
604     Segment->finalize();
605
606   // Put all segments in offset order.
607   auto CompareSegments = [](const SegPtr &A, const SegPtr &B) {
608     return A->Offset < B->Offset;
609   };
610   std::sort(std::begin(this->Segments), std::end(this->Segments),
611             CompareSegments);
612
613   uint64_t Offset = 0;
614   for (auto &Segment : this->Segments) {
615     if (Segment->Type == llvm::ELF::PT_LOAD &&
616         Segment->firstSection() != nullptr) {
617       Offset = alignTo(Offset, Segment->Align);
618       Segment->Offset = Offset;
619       Offset += Segment->FileSize;
620     }
621   }
622   TotalSize = Offset;
623 }
624
625 template class Object<ELF64LE>;
626 template class Object<ELF64BE>;
627 template class Object<ELF32LE>;
628 template class Object<ELF32BE>;
629
630 template class ELFObject<ELF64LE>;
631 template class ELFObject<ELF64BE>;
632 template class ELFObject<ELF32LE>;
633 template class ELFObject<ELF32BE>;
634
635 template class BinaryObject<ELF64LE>;
636 template class BinaryObject<ELF64BE>;
637 template class BinaryObject<ELF32LE>;
638 template class BinaryObject<ELF32BE>;