blob: 365e9ada12f2f13e9fb7a51985390c2966b123c3 [file] [log] [blame]
Petr Hosek05a04cb2017-08-01 00:33:58 +00001//===- 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
12using namespace llvm;
13using namespace object;
14using namespace ELF;
15
16template <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
33void 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
Petr Hosekc4df10e2017-08-04 21:09:26 +000045void 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
Petr Hosek05a04cb2017-08-01 00:33:58 +000052void SectionBase::finalize() {}
53
54template <class ELFT>
55void 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
71void 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
78void StringTableSection::addString(StringRef Name) {
79 StrTabBuilder.add(Name);
80 Size = StrTabBuilder.getSize();
81}
82
83uint32_t StringTableSection::findIndex(StringRef Name) const {
84 return StrTabBuilder.getOffset(Name);
85}
86
87void StringTableSection::finalize() { StrTabBuilder.finalize(); }
88
89void StringTableSection::writeSection(FileOutputBuffer &Out) const {
90 StrTabBuilder.write(Out.getBufferStart() + Offset);
91}
92
Petr Hosek79cee9e2017-08-29 02:12:03 +000093void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
94 SectionBase *DefinedIn, uint64_t Value,
95 uint64_t Sz) {
96 Symbol Sym;
97 Sym.Name = Name;
98 Sym.Binding = Bind;
99 Sym.Type = Type;
100 Sym.DefinedIn = DefinedIn;
101 Sym.Value = Value;
102 Sym.Size = Sz;
103 Sym.Index = Symbols.size();
104 Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
105 Size += this->EntrySize;
106}
107
108void SymbolTableSection::finalize() {
109 // Make sure SymbolNames is finalized before getting name indexes.
110 SymbolNames->finalize();
111
112 uint32_t MaxLocalIndex = 0;
113 for (auto &Sym : Symbols) {
114 Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
115 if (Sym->Binding == STB_LOCAL)
116 MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
117 }
118 // Now we need to set the Link and Info fields.
119 Link = SymbolNames->Index;
120 Info = MaxLocalIndex + 1;
121}
122
123void SymbolTableSection::addSymbolNames() {
124 // Add all of our strings to SymbolNames so that SymbolNames has the right
125 // size before layout is decided.
126 for (auto &Sym : Symbols)
127 SymbolNames->addString(Sym->Name);
128}
129
130const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
131 if (Symbols.size() <= Index)
132 error("Invalid symbol index: " + Twine(Index));
133 return Symbols[Index].get();
134}
135
136template <class ELFT>
137void SymbolTableSectionImpl<ELFT>::writeSection(
138 llvm::FileOutputBuffer &Out) const {
139 uint8_t *Buf = Out.getBufferStart();
140 Buf += Offset;
141 typename ELFT::Sym *Sym = reinterpret_cast<typename ELFT::Sym *>(Buf);
142 // Loop though symbols setting each entry of the symbol table.
143 for (auto &Symbol : Symbols) {
144 Sym->st_name = Symbol->NameIndex;
145 Sym->st_value = Symbol->Value;
146 Sym->st_size = Symbol->Size;
147 Sym->setBinding(Symbol->Binding);
148 Sym->setType(Symbol->Type);
149 if (Symbol->DefinedIn)
150 Sym->st_shndx = Symbol->DefinedIn->Index;
151 else
152 Sym->st_shndx = SHN_UNDEF;
153 ++Sym;
154 }
155}
156
Petr Hosekd7df9b22017-09-06 23:41:02 +0000157template <class ELFT> void RelocationSection<ELFT>::finalize() {
158 this->Link = Symbols->Index;
159 this->Info = SecToApplyRel->Index;
160}
161
162template <class ELFT>
163void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
164
165template <class ELFT>
166void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
167 Rela.r_addend = Addend;
168}
169
170template <class ELFT>
171template <class T>
172void RelocationSection<ELFT>::writeRel(T *Buf) const {
173 for (const auto &Reloc : Relocations) {
174 Buf->r_offset = Reloc.Offset;
175 setAddend(*Buf, Reloc.Addend);
176 Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
177 ++Buf;
178 }
179}
180
181template <class ELFT>
182void RelocationSection<ELFT>::writeSection(llvm::FileOutputBuffer &Out) const {
183 uint8_t *Buf = Out.getBufferStart() + Offset;
184 if (Type == SHT_REL)
185 writeRel(reinterpret_cast<Elf_Rel *>(Buf));
186 else
187 writeRel(reinterpret_cast<Elf_Rela *>(Buf));
188}
189
Petr Hosek05a04cb2017-08-01 00:33:58 +0000190// Returns true IFF a section is wholly inside the range of a segment
191static bool sectionWithinSegment(const SectionBase &Section,
192 const Segment &Segment) {
193 // If a section is empty it should be treated like it has a size of 1. This is
194 // to clarify the case when an empty section lies on a boundary between two
195 // segments and ensures that the section "belongs" to the second segment and
196 // not the first.
197 uint64_t SecSize = Section.Size ? Section.Size : 1;
198 return Segment.Offset <= Section.OriginalOffset &&
199 Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
200}
201
202template <class ELFT>
203void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) {
204 uint32_t Index = 0;
205 for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
Petr Hosekc4df10e2017-08-04 21:09:26 +0000206 ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
207 (size_t)Phdr.p_filesz};
208 Segments.emplace_back(llvm::make_unique<Segment>(Data));
Petr Hosek05a04cb2017-08-01 00:33:58 +0000209 Segment &Seg = *Segments.back();
210 Seg.Type = Phdr.p_type;
211 Seg.Flags = Phdr.p_flags;
Petr Hosek3f383832017-08-26 01:32:20 +0000212 Seg.OriginalOffset = Phdr.p_offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000213 Seg.Offset = Phdr.p_offset;
214 Seg.VAddr = Phdr.p_vaddr;
215 Seg.PAddr = Phdr.p_paddr;
216 Seg.FileSize = Phdr.p_filesz;
217 Seg.MemSize = Phdr.p_memsz;
218 Seg.Align = Phdr.p_align;
219 Seg.Index = Index++;
220 for (auto &Section : Sections) {
221 if (sectionWithinSegment(*Section, Seg)) {
222 Seg.addSection(&*Section);
223 if (!Section->ParentSegment ||
224 Section->ParentSegment->Offset > Seg.Offset) {
225 Section->ParentSegment = &Seg;
226 }
227 }
228 }
229 }
230}
231
232template <class ELFT>
Petr Hosek79cee9e2017-08-29 02:12:03 +0000233void Object<ELFT>::initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
234 SymbolTableSection *SymTab) {
235
236 SymTab->Size = 0;
237 if (SymbolTable->Link - 1 >= Sections.size())
238 error("Symbol table has link index of " + Twine(SymbolTable->Link) +
239 " which is not a valid index");
240
241 if (auto StrTab =
242 dyn_cast<StringTableSection>(Sections[SymbolTable->Link - 1].get()))
243 SymTab->setStrTab(StrTab);
244 else
245 error("Symbol table has link index of " + Twine(SymbolTable->Link) +
246 "which is not a string table");
247
248 const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
249 StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
250
251 for (const auto &Sym : unwrapOrError(ElfFile.symbols(&Shdr))) {
252 SectionBase *DefSection = nullptr;
253 StringRef Name = unwrapOrError(Sym.getName(StrTabData));
254 if (Sym.st_shndx != SHN_UNDEF) {
255 if (Sym.st_shndx >= Sections.size())
256 error("Symbol '" + Name +
257 "' is defined in invalid section with index " +
258 Twine(Sym.st_shndx));
259 DefSection = Sections[Sym.st_shndx - 1].get();
260 }
261 SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
262 Sym.getValue(), Sym.st_size);
263 }
264}
265
266template <class ELFT>
Petr Hosekd7df9b22017-09-06 23:41:02 +0000267static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
268
269template <class ELFT>
270static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
271 ToSet = Rela.r_addend;
272}
273
274template <class ELFT, class T>
275void initRelocations(RelocationSection<ELFT> *Relocs,
276 SymbolTableSection *SymbolTable, T RelRange) {
277 for (const auto &Rel : RelRange) {
278 Relocation ToAdd;
279 ToAdd.Offset = Rel.r_offset;
280 getAddend(ToAdd.Addend, Rel);
281 ToAdd.Type = Rel.getType(false);
282 ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
283 Relocs->addRelocation(ToAdd);
284 }
285}
286
287template <class ELFT>
Petr Hosek05a04cb2017-08-01 00:33:58 +0000288std::unique_ptr<SectionBase>
289Object<ELFT>::makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
290 const Elf_Shdr &Shdr) {
291 ArrayRef<uint8_t> Data;
292 switch (Shdr.sh_type) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000293 case SHT_REL:
294 case SHT_RELA:
295 return llvm::make_unique<RelocationSection<ELFT>>();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000296 case SHT_STRTAB:
297 return llvm::make_unique<StringTableSection>();
Petr Hosek79cee9e2017-08-29 02:12:03 +0000298 case SHT_SYMTAB: {
299 auto SymTab = llvm::make_unique<SymbolTableSectionImpl<ELFT>>();
300 SymbolTable = SymTab.get();
301 return std::move(SymTab);
302 }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000303 case SHT_NOBITS:
304 return llvm::make_unique<Section>(Data);
305 default:
306 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
307 return llvm::make_unique<Section>(Data);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000308 }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000309}
310
311template <class ELFT>
312void Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) {
313 uint32_t Index = 0;
314 for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
315 if (Index == 0) {
316 ++Index;
317 continue;
318 }
319 SecPtr Sec = makeSection(ElfFile, Shdr);
320 Sec->Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
321 Sec->Type = Shdr.sh_type;
322 Sec->Flags = Shdr.sh_flags;
323 Sec->Addr = Shdr.sh_addr;
324 Sec->Offset = Shdr.sh_offset;
325 Sec->OriginalOffset = Shdr.sh_offset;
326 Sec->Size = Shdr.sh_size;
327 Sec->Link = Shdr.sh_link;
328 Sec->Info = Shdr.sh_info;
329 Sec->Align = Shdr.sh_addralign;
330 Sec->EntrySize = Shdr.sh_entsize;
331 Sec->Index = Index++;
332 Sections.push_back(std::move(Sec));
333 }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000334
335 // Now that all of the sections have been added we can fill out some extra
336 // details about symbol tables.
337 if (SymbolTable)
338 initSymbolTable(ElfFile, SymbolTable);
Petr Hosekd7df9b22017-09-06 23:41:02 +0000339
340 // Now that all sections and symbols have been added we can add
341 // relocations that reference symbols and set the link and info fields for
342 // relocation sections.
343 for (auto &Section : Sections) {
344 if (auto RelSec = dyn_cast<RelocationSection<ELFT>>(Section.get())) {
345 if (RelSec->Link - 1 >= Sections.size() || RelSec->Link == 0) {
346 error("Link field value " + Twine(RelSec->Link) + " in section " +
347 RelSec->Name + " is invalid");
348 }
349 if (RelSec->Info - 1 >= Sections.size() || RelSec->Info == 0) {
350 error("Info field value " + Twine(RelSec->Link) + " in section " +
351 RelSec->Name + " is invalid");
352 }
353 auto SymTab =
354 dyn_cast<SymbolTableSection>(Sections[RelSec->Link - 1].get());
355 if (SymTab == nullptr) {
356 error("Link field of relocation section " + RelSec->Name +
357 " is not a symbol table");
358 }
359 RelSec->setSymTab(SymTab);
360 RelSec->setSection(Sections[RelSec->Info - 1].get());
361 auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
362 if (RelSec->Type == SHT_REL)
363 initRelocations(RelSec, SymTab, unwrapOrError(ElfFile.rels(Shdr)));
364 else
365 initRelocations(RelSec, SymTab, unwrapOrError(ElfFile.relas(Shdr)));
366 }
367 }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000368}
369
Petr Hosek05a04cb2017-08-01 00:33:58 +0000370template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) {
371 const auto &ElfFile = *Obj.getELFFile();
372 const auto &Ehdr = *ElfFile.getHeader();
373
374 std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Ident);
375 Type = Ehdr.e_type;
376 Machine = Ehdr.e_machine;
377 Version = Ehdr.e_version;
378 Entry = Ehdr.e_entry;
379 Flags = Ehdr.e_flags;
380
381 readSectionHeaders(ElfFile);
382 readProgramHeaders(ElfFile);
383
384 SectionNames =
385 dyn_cast<StringTableSection>(Sections[Ehdr.e_shstrndx - 1].get());
386}
387
Petr Hosek05a04cb2017-08-01 00:33:58 +0000388template <class ELFT>
389void Object<ELFT>::writeHeader(FileOutputBuffer &Out) const {
390 uint8_t *Buf = Out.getBufferStart();
391 Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(Buf);
392 std::copy(Ident, Ident + 16, Ehdr.e_ident);
393 Ehdr.e_type = Type;
394 Ehdr.e_machine = Machine;
395 Ehdr.e_version = Version;
396 Ehdr.e_entry = Entry;
397 Ehdr.e_phoff = sizeof(Elf_Ehdr);
398 Ehdr.e_shoff = SHOffset;
399 Ehdr.e_flags = Flags;
400 Ehdr.e_ehsize = sizeof(Elf_Ehdr);
401 Ehdr.e_phentsize = sizeof(Elf_Phdr);
402 Ehdr.e_phnum = Segments.size();
403 Ehdr.e_shentsize = sizeof(Elf_Shdr);
404 Ehdr.e_shnum = Sections.size() + 1;
405 Ehdr.e_shstrndx = SectionNames->Index;
406}
407
408template <class ELFT>
409void Object<ELFT>::writeProgramHeaders(FileOutputBuffer &Out) const {
410 for (auto &Phdr : Segments)
411 Phdr->template writeHeader<ELFT>(Out);
412}
413
414template <class ELFT>
415void Object<ELFT>::writeSectionHeaders(FileOutputBuffer &Out) const {
416 uint8_t *Buf = Out.getBufferStart() + SHOffset;
417 // This reference serves to write the dummy section header at the begining
418 // of the file.
419 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(Buf);
420 Shdr.sh_name = 0;
421 Shdr.sh_type = SHT_NULL;
422 Shdr.sh_flags = 0;
423 Shdr.sh_addr = 0;
424 Shdr.sh_offset = 0;
425 Shdr.sh_size = 0;
426 Shdr.sh_link = 0;
427 Shdr.sh_info = 0;
428 Shdr.sh_addralign = 0;
429 Shdr.sh_entsize = 0;
430
431 for (auto &Section : Sections)
432 Section->template writeHeader<ELFT>(Out);
433}
434
435template <class ELFT>
436void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const {
437 for (auto &Section : Sections)
438 Section->writeSection(Out);
439}
440
Petr Hosekc4df10e2017-08-04 21:09:26 +0000441template <class ELFT> void ELFObject<ELFT>::sortSections() {
442 // Put all sections in offset order. Maintain the ordering as closely as
443 // possible while meeting that demand however.
444 auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
445 return A->OriginalOffset < B->OriginalOffset;
446 };
447 std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
448 CompareSections);
449}
450
451template <class ELFT> void ELFObject<ELFT>::assignOffsets() {
Petr Hosek3f383832017-08-26 01:32:20 +0000452 // The size of ELF + program headers will not change so it is ok to assume
453 // that the first offset of the first segment is a good place to start
454 // outputting sections. This covers both the standard case and the PT_PHDR
455 // case.
456 uint64_t Offset;
457 if (!this->Segments.empty()) {
458 Offset = this->Segments[0]->Offset;
459 } else {
460 Offset = sizeof(Elf_Ehdr);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000461 }
Petr Hosek3f383832017-08-26 01:32:20 +0000462 // The only way a segment should move is if a section was between two
463 // segments and that section was removed. If that section isn't in a segment
464 // then it's acceptable, but not ideal, to simply move it to after the
465 // segments. So we can simply layout segments one after the other accounting
466 // for alignment.
467 for (auto &Segment : this->Segments) {
468 Offset = alignTo(Offset, Segment->Align);
469 Segment->Offset = Offset;
470 Offset += Segment->FileSize;
471 }
472 // Now the offset of every segment has been set we can assign the offsets
473 // of each section. For sections that are covered by a segment we should use
474 // the segment's original offset and the section's original offset to compute
475 // the offset from the start of the segment. Using the offset from the start
476 // of the segment we can assign a new offset to the section. For sections not
477 // covered by segments we can just bump Offset to the next valid location.
478 uint32_t Index = 1;
479 for (auto &Section : this->Sections) {
480 Section->Index = Index++;
481 if (Section->ParentSegment != nullptr) {
482 auto Segment = Section->ParentSegment;
483 Section->Offset =
484 Segment->Offset + (Section->OriginalOffset - Segment->OriginalOffset);
485 } else {
486 Offset = alignTo(Offset, Section->Offset);
487 Section->Offset = Offset;
488 if (Section->Type != SHT_NOBITS)
489 Offset += Section->Size;
490 }
491 }
492
Petr Hosekc4df10e2017-08-04 21:09:26 +0000493 Offset = alignTo(Offset, sizeof(typename ELFT::Addr));
494 this->SHOffset = Offset;
495}
496
497template <class ELFT> size_t ELFObject<ELFT>::totalSize() const {
498 // We already have the section header offset so we can calculate the total
499 // size by just adding up the size of each section header.
500 return this->SHOffset + this->Sections.size() * sizeof(Elf_Shdr) +
501 sizeof(Elf_Shdr);
502}
503
504template <class ELFT> void ELFObject<ELFT>::write(FileOutputBuffer &Out) const {
505 this->writeHeader(Out);
506 this->writeProgramHeaders(Out);
507 this->writeSectionData(Out);
508 this->writeSectionHeaders(Out);
509}
510
511template <class ELFT> void ELFObject<ELFT>::finalize() {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000512 // Make sure we add the names of all the sections.
Petr Hosekc4df10e2017-08-04 21:09:26 +0000513 for (const auto &Section : this->Sections) {
514 this->SectionNames->addString(Section->Name);
515 }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000516 // Make sure we add the names of all the symbols.
517 this->SymbolTable->addSymbolNames();
Petr Hosekc4df10e2017-08-04 21:09:26 +0000518
519 sortSections();
520 assignOffsets();
521
522 // Finalize SectionNames first so that we can assign name indexes.
523 this->SectionNames->finalize();
524 // Finally now that all offsets and indexes have been set we can finalize any
525 // remaining issues.
526 uint64_t Offset = this->SHOffset + sizeof(Elf_Shdr);
527 for (auto &Section : this->Sections) {
528 Section->HeaderOffset = Offset;
529 Offset += sizeof(Elf_Shdr);
530 Section->NameIndex = this->SectionNames->findIndex(Section->Name);
531 Section->finalize();
532 }
533
534 for (auto &Segment : this->Segments)
535 Segment->finalize();
536}
537
538template <class ELFT> size_t BinaryObject<ELFT>::totalSize() const {
539 return TotalSize;
540}
541
542template <class ELFT>
543void BinaryObject<ELFT>::write(FileOutputBuffer &Out) const {
544 for (auto &Segment : this->Segments) {
Petr Hosekd53951d2017-08-04 23:18:18 +0000545 // GNU objcopy does not output segments that do not cover a section. Such
546 // segments can sometimes be produced by LLD due to how LLD handles PT_PHDR.
547 if (Segment->Type == llvm::ELF::PT_LOAD &&
548 Segment->firstSection() != nullptr) {
Petr Hosekc4df10e2017-08-04 21:09:26 +0000549 Segment->writeSegment(Out);
550 }
551 }
552}
553
554template <class ELFT> void BinaryObject<ELFT>::finalize() {
555 for (auto &Segment : this->Segments)
556 Segment->finalize();
557
558 // Put all segments in offset order.
559 auto CompareSegments = [](const SegPtr &A, const SegPtr &B) {
560 return A->Offset < B->Offset;
561 };
562 std::sort(std::begin(this->Segments), std::end(this->Segments),
563 CompareSegments);
564
565 uint64_t Offset = 0;
566 for (auto &Segment : this->Segments) {
Petr Hosekd53951d2017-08-04 23:18:18 +0000567 if (Segment->Type == llvm::ELF::PT_LOAD &&
568 Segment->firstSection() != nullptr) {
Petr Hosekc4df10e2017-08-04 21:09:26 +0000569 Offset = alignTo(Offset, Segment->Align);
570 Segment->Offset = Offset;
571 Offset += Segment->FileSize;
572 }
573 }
574 TotalSize = Offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000575}
576
Petr Hosek35fdbd52017-08-01 05:31:50 +0000577template class Object<ELF64LE>;
578template class Object<ELF64BE>;
579template class Object<ELF32LE>;
580template class Object<ELF32BE>;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000581
582template class ELFObject<ELF64LE>;
583template class ELFObject<ELF64BE>;
584template class ELFObject<ELF32LE>;
585template class ELFObject<ELF32BE>;
586
587template class BinaryObject<ELF64LE>;
588template class BinaryObject<ELF64BE>;
589template class BinaryObject<ELF32LE>;
590template class BinaryObject<ELF32BE>;