1 //===- Object.cpp -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm-objcopy.h"
13 using namespace object;
16 template <class ELFT> void Segment::writeHeader(FileOutputBuffer &Out) const {
17 typedef typename ELFT::Ehdr Elf_Ehdr;
18 typedef typename ELFT::Phdr Elf_Phdr;
20 uint8_t *Buf = Out.getBufferStart();
21 Buf += sizeof(Elf_Ehdr) + Index * sizeof(Elf_Phdr);
22 Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(Buf);
25 Phdr.p_offset = Offset;
28 Phdr.p_filesz = FileSize;
29 Phdr.p_memsz = MemSize;
33 void Segment::finalize() {
34 auto FirstSec = firstSection();
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);
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);
52 void SectionBase::finalize() {}
55 void SectionBase::writeHeader(FileOutputBuffer &Out) const {
56 uint8_t *Buf = Out.getBufferStart();
58 typename ELFT::Shdr &Shdr = *reinterpret_cast<typename ELFT::Shdr *>(Buf);
59 Shdr.sh_name = NameIndex;
61 Shdr.sh_flags = Flags;
63 Shdr.sh_offset = Offset;
67 Shdr.sh_addralign = Align;
68 Shdr.sh_entsize = EntrySize;
71 void Section::writeSection(FileOutputBuffer &Out) const {
72 if (Type == SHT_NOBITS)
74 uint8_t *Buf = Out.getBufferStart() + Offset;
75 std::copy(std::begin(Contents), std::end(Contents), Buf);
78 void StringTableSection::addString(StringRef Name) {
79 StrTabBuilder.add(Name);
80 Size = StrTabBuilder.getSize();
83 uint32_t StringTableSection::findIndex(StringRef Name) const {
84 return StrTabBuilder.getOffset(Name);
87 void StringTableSection::finalize() { StrTabBuilder.finalize(); }
89 void StringTableSection::writeSection(FileOutputBuffer &Out) const {
90 StrTabBuilder.write(Out.getBufferStart() + Offset);
93 static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
99 if (Machine == EM_HEXAGON) {
101 case SHN_HEXAGON_SCOMMON:
102 case SHN_HEXAGON_SCOMMON_2:
103 case SHN_HEXAGON_SCOMMON_4:
104 case SHN_HEXAGON_SCOMMON_8:
111 uint16_t Symbol::getShndx() const {
112 if (DefinedIn != nullptr) {
113 return DefinedIn->Index;
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:
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);
128 llvm_unreachable("Symbol with invalid ShndxType encountered");
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) {
138 Sym.DefinedIn = DefinedIn;
139 if (DefinedIn == nullptr) {
140 if (Shndx >= SHN_LORESERVE)
141 Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
143 Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
147 Sym.Index = Symbols.size();
148 Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
149 Size += this->EntrySize;
152 void SymbolTableSection::finalize() {
153 // Make sure SymbolNames is finalized before getting name indexes.
154 SymbolNames->finalize();
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);
162 // Now we need to set the Link and Info fields.
163 Link = SymbolNames->Index;
164 Info = MaxLocalIndex + 1;
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);
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();
180 template <class ELFT>
181 void SymbolTableSectionImpl<ELFT>::writeSection(
182 llvm::FileOutputBuffer &Out) const {
183 uint8_t *Buf = Out.getBufferStart();
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();
198 template <class ELFT> void RelocationSection<ELFT>::finalize() {
199 this->Link = Symbols->Index;
200 this->Info = SecToApplyRel->Index;
203 template <class ELFT>
204 void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
206 template <class ELFT>
207 void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
208 Rela.r_addend = Addend;
211 template <class ELFT>
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);
222 template <class ELFT>
223 void RelocationSection<ELFT>::writeSection(llvm::FileOutputBuffer &Out) const {
224 uint8_t *Buf = Out.getBufferStart() + Offset;
226 writeRel(reinterpret_cast<Elf_Rel *>(Buf));
228 writeRel(reinterpret_cast<Elf_Rela *>(Buf));
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
238 uint64_t SecSize = Section.Size ? Section.Size : 1;
239 return Segment.Offset <= Section.OriginalOffset &&
240 Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
243 template <class ELFT>
244 void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) {
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;
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;
273 template <class ELFT>
274 void Object<ELFT>::initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
275 SymbolTableSection *SymTab) {
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");
283 dyn_cast<StringTableSection>(Sections[SymbolTable->Link - 1].get()))
284 SymTab->setStrTab(StrTab);
286 error("Symbol table has link index of " + Twine(SymbolTable->Link) +
287 "which is not a string table");
289 const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
290 StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
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)) {
299 "' has unsupported value greater than or equal to SHN_LORESERVE: " +
300 Twine(Sym.st_shndx));
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();
309 SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
310 Sym.getValue(), Sym.st_shndx, Sym.st_size);
314 template <class ELFT>
315 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
317 template <class ELFT>
318 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
319 ToSet = Rela.r_addend;
322 template <class ELFT, class T>
323 void initRelocations(RelocationSection<ELFT> *Relocs,
324 SymbolTableSection *SymbolTable, T RelRange) {
325 for (const auto &Rel : RelRange) {
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);
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) {
343 return llvm::make_unique<RelocationSection<ELFT>>();
345 return llvm::make_unique<StringTableSection>();
347 auto SymTab = llvm::make_unique<SymbolTableSectionImpl<ELFT>>();
348 SymbolTable = SymTab.get();
349 return std::move(SymTab);
352 return llvm::make_unique<Section>(Data);
354 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
355 return llvm::make_unique<Section>(Data);
359 template <class ELFT>
360 void Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) {
362 for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
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));
383 // Now that all of the sections have been added we can fill out some extra
384 // details about symbol tables.
386 initSymbolTable(ElfFile, SymbolTable);
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");
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");
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");
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)));
413 initRelocations(RelSec, SymTab, unwrapOrError(ElfFile.relas(Shdr)));
418 template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) {
419 const auto &ElfFile = *Obj.getELFFile();
420 const auto &Ehdr = *ElfFile.getHeader();
422 std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Ident);
424 Machine = Ehdr.e_machine;
425 Version = Ehdr.e_version;
426 Entry = Ehdr.e_entry;
427 Flags = Ehdr.e_flags;
429 readSectionHeaders(ElfFile);
430 readProgramHeaders(ElfFile);
433 dyn_cast<StringTableSection>(Sections[Ehdr.e_shstrndx - 1].get());
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);
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;
456 template <class ELFT>
457 void Object<ELFT>::writeProgramHeaders(FileOutputBuffer &Out) const {
458 for (auto &Phdr : Segments)
459 Phdr->template writeHeader<ELFT>(Out);
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
467 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(Buf);
469 Shdr.sh_type = SHT_NULL;
476 Shdr.sh_addralign = 0;
479 for (auto &Section : Sections)
480 Section->template writeHeader<ELFT>(Out);
483 template <class ELFT>
484 void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const {
485 for (auto &Section : Sections)
486 Section->writeSection(Out);
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;
495 std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
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
505 if (!this->Segments.empty()) {
506 Offset = this->Segments[0]->Offset;
508 Offset = sizeof(Elf_Ehdr);
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
515 for (auto &Segment : this->Segments) {
516 Offset = alignTo(Offset, Segment->Align);
517 Segment->Offset = Offset;
518 Offset += Segment->FileSize;
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.
527 for (auto &Section : this->Sections) {
528 Section->Index = Index++;
529 if (Section->ParentSegment != nullptr) {
530 auto Segment = Section->ParentSegment;
532 Segment->Offset + (Section->OriginalOffset - Segment->OriginalOffset);
534 Offset = alignTo(Offset, Section->Offset);
535 Section->Offset = Offset;
536 if (Section->Type != SHT_NOBITS)
537 Offset += Section->Size;
541 Offset = alignTo(Offset, sizeof(typename ELFT::Addr));
542 this->SHOffset = Offset;
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) +
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);
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);
564 // Make sure we add the names of all the symbols.
565 this->SymbolTable->addSymbolNames();
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
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);
582 for (auto &Segment : this->Segments)
586 template <class ELFT> size_t BinaryObject<ELFT>::totalSize() const {
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);
602 template <class ELFT> void BinaryObject<ELFT>::finalize() {
603 for (auto &Segment : this->Segments)
606 // Put all segments in offset order.
607 auto CompareSegments = [](const SegPtr &A, const SegPtr &B) {
608 return A->Offset < B->Offset;
610 std::sort(std::begin(this->Segments), std::end(this->Segments),
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;
625 template class Object<ELF64LE>;
626 template class Object<ELF64BE>;
627 template class Object<ELF32LE>;
628 template class Object<ELF32BE>;
630 template class ELFObject<ELF64LE>;
631 template class ELFObject<ELF64BE>;
632 template class ELFObject<ELF32LE>;
633 template class ELFObject<ELF32BE>;
635 template class BinaryObject<ELF64LE>;
636 template class BinaryObject<ELF64BE>;
637 template class BinaryObject<ELF32LE>;
638 template class BinaryObject<ELF32BE>;