1 //===- Object.cpp ---------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
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/Object/ELFObjectFile.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/FileOutputBuffer.h"
21 #include "llvm/Support/Path.h"
30 using namespace llvm::objcopy;
31 using namespace object;
36 void FileBuffer::allocate(size_t Size) {
37 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
38 FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable);
39 handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &E) {
40 error("failed to open " + getName() + ": " + E.message());
42 Buf = std::move(*BufferOrErr);
45 Error FileBuffer::commit() { return Buf->commit(); }
47 uint8_t *FileBuffer::getBufferStart() {
48 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
51 void MemBuffer::allocate(size_t Size) {
52 Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName());
55 Error MemBuffer::commit() { return Error::success(); }
57 uint8_t *MemBuffer::getBufferStart() {
58 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
61 std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() {
62 return std::move(Buf);
65 template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
66 using Elf_Phdr = typename ELFT::Phdr;
68 uint8_t *B = Buf.getBufferStart();
69 B += Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr);
70 Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B);
71 Phdr.p_type = Seg.Type;
72 Phdr.p_flags = Seg.Flags;
73 Phdr.p_offset = Seg.Offset;
74 Phdr.p_vaddr = Seg.VAddr;
75 Phdr.p_paddr = Seg.PAddr;
76 Phdr.p_filesz = Seg.FileSize;
77 Phdr.p_memsz = Seg.MemSize;
78 Phdr.p_align = Seg.Align;
81 void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
82 void SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {}
83 void SectionBase::initialize(SectionTableRef SecTable) {}
84 void SectionBase::finalize() {}
85 void SectionBase::markSymbols() {}
87 template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
88 uint8_t *B = Buf.getBufferStart();
89 B += Sec.HeaderOffset;
90 typename ELFT::Shdr &Shdr = *reinterpret_cast<typename ELFT::Shdr *>(B);
91 Shdr.sh_name = Sec.NameIndex;
92 Shdr.sh_type = Sec.Type;
93 Shdr.sh_flags = Sec.Flags;
94 Shdr.sh_addr = Sec.Addr;
95 Shdr.sh_offset = Sec.Offset;
96 Shdr.sh_size = Sec.Size;
97 Shdr.sh_link = Sec.Link;
98 Shdr.sh_info = Sec.Info;
99 Shdr.sh_addralign = Sec.Align;
100 Shdr.sh_entsize = Sec.EntrySize;
103 SectionVisitor::~SectionVisitor() {}
105 void BinarySectionWriter::visit(const SectionIndexSection &Sec) {
106 error("Cannot write symbol section index table '" + Sec.Name + "' ");
109 void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
110 error("Cannot write symbol table '" + Sec.Name + "' out to binary");
113 void BinarySectionWriter::visit(const RelocationSection &Sec) {
114 error("Cannot write relocation section '" + Sec.Name + "' out to binary");
117 void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
118 error("Cannot write '" + Sec.Name + "' out to binary");
121 void BinarySectionWriter::visit(const GroupSection &Sec) {
122 error("Cannot write '" + Sec.Name + "' out to binary");
125 void SectionWriter::visit(const Section &Sec) {
126 if (Sec.Type == SHT_NOBITS)
128 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
129 std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), Buf);
132 void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); }
134 void SectionWriter::visit(const OwnedDataSection &Sec) {
135 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
136 std::copy(std::begin(Sec.Data), std::end(Sec.Data), Buf);
139 void OwnedDataSection::accept(SectionVisitor &Visitor) const {
140 Visitor.visit(*this);
143 void StringTableSection::addString(StringRef Name) {
144 StrTabBuilder.add(Name);
145 Size = StrTabBuilder.getSize();
148 uint32_t StringTableSection::findIndex(StringRef Name) const {
149 return StrTabBuilder.getOffset(Name);
152 void StringTableSection::finalize() { StrTabBuilder.finalize(); }
154 void SectionWriter::visit(const StringTableSection &Sec) {
155 Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
158 void StringTableSection::accept(SectionVisitor &Visitor) const {
159 Visitor.visit(*this);
162 template <class ELFT>
163 void ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
164 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
165 auto *IndexesBuffer = reinterpret_cast<typename ELFT::Word *>(Buf);
166 std::copy(std::begin(Sec.Indexes), std::end(Sec.Indexes), IndexesBuffer);
169 void SectionIndexSection::initialize(SectionTableRef SecTable) {
171 setSymTab(SecTable.getSectionOfType<SymbolTableSection>(
173 "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
174 "Link field value " + Twine(Link) + " in section " + Name +
175 " is not a symbol table"));
176 Symbols->setShndxTable(this);
179 void SectionIndexSection::finalize() { Link = Symbols->Index; }
181 void SectionIndexSection::accept(SectionVisitor &Visitor) const {
182 Visitor.visit(*this);
185 static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
191 if (Machine == EM_HEXAGON) {
193 case SHN_HEXAGON_SCOMMON:
194 case SHN_HEXAGON_SCOMMON_2:
195 case SHN_HEXAGON_SCOMMON_4:
196 case SHN_HEXAGON_SCOMMON_8:
203 // Large indexes force us to clarify exactly what this function should do. This
204 // function should return the value that will appear in st_shndx when written
206 uint16_t Symbol::getShndx() const {
207 if (DefinedIn != nullptr) {
208 if (DefinedIn->Index >= SHN_LORESERVE)
210 return DefinedIn->Index;
213 // This means that we don't have a defined section but we do need to
214 // output a legitimate section index.
215 case SYMBOL_SIMPLE_INDEX:
219 case SYMBOL_HEXAGON_SCOMMON:
220 case SYMBOL_HEXAGON_SCOMMON_2:
221 case SYMBOL_HEXAGON_SCOMMON_4:
222 case SYMBOL_HEXAGON_SCOMMON_8:
224 return static_cast<uint16_t>(ShndxType);
226 llvm_unreachable("Symbol with invalid ShndxType encountered");
229 void SymbolTableSection::assignIndices() {
231 for (auto &Sym : Symbols)
232 Sym->Index = Index++;
235 void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
236 SectionBase *DefinedIn, uint64_t Value,
237 uint8_t Visibility, uint16_t Shndx,
243 Sym.DefinedIn = DefinedIn;
244 if (DefinedIn != nullptr)
245 DefinedIn->HasSymbol = true;
246 if (DefinedIn == nullptr) {
247 if (Shndx >= SHN_LORESERVE)
248 Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
250 Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
253 Sym.Visibility = Visibility;
255 Sym.Index = Symbols.size();
256 Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
257 Size += this->EntrySize;
260 void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
261 if (SectionIndexTable == Sec)
262 SectionIndexTable = nullptr;
263 if (SymbolNames == Sec) {
264 error("String table " + SymbolNames->Name +
265 " cannot be removed because it is referenced by the symbol table " +
268 removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; });
271 void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
272 std::for_each(std::begin(Symbols) + 1, std::end(Symbols),
273 [Callable](SymPtr &Sym) { Callable(*Sym); });
274 std::stable_partition(
275 std::begin(Symbols), std::end(Symbols),
276 [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; });
280 void SymbolTableSection::removeSymbols(
281 function_ref<bool(const Symbol &)> ToRemove) {
283 std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
284 [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
286 Size = Symbols.size() * EntrySize;
290 void SymbolTableSection::initialize(SectionTableRef SecTable) {
292 setStrTab(SecTable.getSectionOfType<StringTableSection>(
294 "Symbol table has link index of " + Twine(Link) +
295 " which is not a valid index",
296 "Symbol table has link index of " + Twine(Link) +
297 " which is not a string table"));
300 void SymbolTableSection::finalize() {
301 // Make sure SymbolNames is finalized before getting name indexes.
302 SymbolNames->finalize();
304 uint32_t MaxLocalIndex = 0;
305 for (auto &Sym : Symbols) {
306 Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
307 if (Sym->Binding == STB_LOCAL)
308 MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
310 // Now we need to set the Link and Info fields.
311 Link = SymbolNames->Index;
312 Info = MaxLocalIndex + 1;
315 void SymbolTableSection::prepareForLayout() {
316 // Add all potential section indexes before file layout so that the section
317 // index section has the approprite size.
318 if (SectionIndexTable != nullptr) {
319 for (const auto &Sym : Symbols) {
320 if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
321 SectionIndexTable->addIndex(Sym->DefinedIn->Index);
323 SectionIndexTable->addIndex(SHN_UNDEF);
326 // Add all of our strings to SymbolNames so that SymbolNames has the right
327 // size before layout is decided.
328 for (auto &Sym : Symbols)
329 SymbolNames->addString(Sym->Name);
332 const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
333 if (Symbols.size() <= Index)
334 error("Invalid symbol index: " + Twine(Index));
335 return Symbols[Index].get();
338 Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) {
339 return const_cast<Symbol *>(
340 static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index));
343 template <class ELFT>
344 void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
345 uint8_t *Buf = Out.getBufferStart();
347 typename ELFT::Sym *Sym = reinterpret_cast<typename ELFT::Sym *>(Buf);
348 // Loop though symbols setting each entry of the symbol table.
349 for (auto &Symbol : Sec.Symbols) {
350 Sym->st_name = Symbol->NameIndex;
351 Sym->st_value = Symbol->Value;
352 Sym->st_size = Symbol->Size;
353 Sym->st_other = Symbol->Visibility;
354 Sym->setBinding(Symbol->Binding);
355 Sym->setType(Symbol->Type);
356 Sym->st_shndx = Symbol->getShndx();
361 void SymbolTableSection::accept(SectionVisitor &Visitor) const {
362 Visitor.visit(*this);
365 template <class SymTabType>
366 void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
367 const SectionBase *Sec) {
368 if (Symbols == Sec) {
369 error("Symbol table " + Symbols->Name +
370 " cannot be removed because it is "
371 "referenced by the relocation "
377 template <class SymTabType>
378 void RelocSectionWithSymtabBase<SymTabType>::initialize(
379 SectionTableRef SecTable) {
380 setSymTab(SecTable.getSectionOfType<SymTabType>(
382 "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
383 "Link field value " + Twine(Link) + " in section " + Name +
384 " is not a symbol table"));
386 if (Info != SHN_UNDEF)
387 setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) +
388 " in section " + Name +
394 template <class SymTabType>
395 void RelocSectionWithSymtabBase<SymTabType>::finalize() {
396 this->Link = Symbols->Index;
397 if (SecToApplyRel != nullptr)
398 this->Info = SecToApplyRel->Index;
401 template <class ELFT>
402 static void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
404 template <class ELFT>
405 static void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
406 Rela.r_addend = Addend;
409 template <class RelRange, class T>
410 static void writeRel(const RelRange &Relocations, T *Buf) {
411 for (const auto &Reloc : Relocations) {
412 Buf->r_offset = Reloc.Offset;
413 setAddend(*Buf, Reloc.Addend);
414 Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
419 template <class ELFT>
420 void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
421 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
422 if (Sec.Type == SHT_REL)
423 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf));
425 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf));
428 void RelocationSection::accept(SectionVisitor &Visitor) const {
429 Visitor.visit(*this);
432 void RelocationSection::removeSymbols(
433 function_ref<bool(const Symbol &)> ToRemove) {
434 for (const Relocation &Reloc : Relocations)
435 if (ToRemove(*Reloc.RelocSymbol))
436 error("not stripping symbol `" + Reloc.RelocSymbol->Name +
437 "' because it is named in a relocation");
440 void RelocationSection::markSymbols() {
441 for (const Relocation &Reloc : Relocations)
442 Reloc.RelocSymbol->Referenced = true;
445 void SectionWriter::visit(const DynamicRelocationSection &Sec) {
446 std::copy(std::begin(Sec.Contents), std::end(Sec.Contents),
447 Out.getBufferStart() + Sec.Offset);
450 void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
451 Visitor.visit(*this);
454 void Section::removeSectionReferences(const SectionBase *Sec) {
455 if (LinkSection == Sec) {
456 error("Section " + LinkSection->Name +
457 " cannot be removed because it is "
458 "referenced by the section " +
463 void GroupSection::finalize() {
464 this->Info = Sym->Index;
465 this->Link = SymTab->Index;
468 void GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
469 if (ToRemove(*Sym)) {
470 error("Symbol " + Sym->Name +
471 " cannot be removed because it is "
472 "referenced by the section " +
473 this->Name + "[" + Twine(this->Index) + "]");
477 void GroupSection::markSymbols() {
479 Sym->Referenced = true;
482 void Section::initialize(SectionTableRef SecTable) {
483 if (Link != ELF::SHN_UNDEF) {
485 SecTable.getSection(Link, "Link field value " + Twine(Link) +
486 " in section " + Name + " is invalid");
487 if (LinkSection->Type == ELF::SHT_SYMTAB)
488 LinkSection = nullptr;
492 void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; }
494 void GnuDebugLinkSection::init(StringRef File, StringRef Data) {
495 FileName = sys::path::filename(File);
496 // The format for the .gnu_debuglink starts with the file name and is
497 // followed by a null terminator and then the CRC32 of the file. The CRC32
498 // should be 4 byte aligned. So we add the FileName size, a 1 for the null
499 // byte, and then finally push the size to alignment and add 4.
500 Size = alignTo(FileName.size() + 1, 4) + 4;
501 // The CRC32 will only be aligned if we align the whole section.
503 Type = ELF::SHT_PROGBITS;
504 Name = ".gnu_debuglink";
505 // For sections not found in segments, OriginalOffset is only used to
506 // establish the order that sections should go in. By using the maximum
507 // possible offset we cause this section to wind up at the end.
508 OriginalOffset = std::numeric_limits<uint64_t>::max();
510 crc.update(ArrayRef<char>(Data.data(), Data.size()));
511 // The CRC32 value needs to be complemented because the JamCRC dosn't
512 // finalize the CRC32 value. It also dosn't negate the initial CRC32 value
513 // but it starts by default at 0xFFFFFFFF which is the complement of zero.
514 CRC32 = ~crc.getCRC();
517 GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) {
518 // Read in the file to compute the CRC of it.
519 auto DebugOrErr = MemoryBuffer::getFile(File);
521 error("'" + File + "': " + DebugOrErr.getError().message());
522 auto Debug = std::move(*DebugOrErr);
523 init(File, Debug->getBuffer());
526 template <class ELFT>
527 void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
528 auto Buf = Out.getBufferStart() + Sec.Offset;
529 char *File = reinterpret_cast<char *>(Buf);
531 reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
533 std::copy(std::begin(Sec.FileName), std::end(Sec.FileName), File);
536 void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
537 Visitor.visit(*this);
540 template <class ELFT>
541 void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
542 ELF::Elf32_Word *Buf =
543 reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
544 *Buf++ = Sec.FlagWord;
545 for (const auto *S : Sec.GroupMembers)
546 support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index);
549 void GroupSection::accept(SectionVisitor &Visitor) const {
550 Visitor.visit(*this);
553 // Returns true IFF a section is wholly inside the range of a segment
554 static bool sectionWithinSegment(const SectionBase &Section,
555 const Segment &Segment) {
556 // If a section is empty it should be treated like it has a size of 1. This is
557 // to clarify the case when an empty section lies on a boundary between two
558 // segments and ensures that the section "belongs" to the second segment and
560 uint64_t SecSize = Section.Size ? Section.Size : 1;
561 return Segment.Offset <= Section.OriginalOffset &&
562 Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
565 // Returns true IFF a segment's original offset is inside of another segment's
567 static bool segmentOverlapsSegment(const Segment &Child,
568 const Segment &Parent) {
570 return Parent.OriginalOffset <= Child.OriginalOffset &&
571 Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset;
574 static bool compareSegmentsByOffset(const Segment *A, const Segment *B) {
575 // Any segment without a parent segment should come before a segment
576 // that has a parent segment.
577 if (A->OriginalOffset < B->OriginalOffset)
579 if (A->OriginalOffset > B->OriginalOffset)
581 return A->Index < B->Index;
584 static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) {
585 if (A->PAddr < B->PAddr)
587 if (A->PAddr > B->PAddr)
589 return A->Index < B->Index;
592 template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) {
593 for (auto &Parent : Obj.segments()) {
594 // Every segment will overlap with itself but we don't want a segment to
595 // be it's own parent so we avoid that situation.
596 if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) {
597 // We want a canonical "most parental" segment but this requires
598 // inspecting the ParentSegment.
599 if (compareSegmentsByOffset(&Parent, &Child))
600 if (Child.ParentSegment == nullptr ||
601 compareSegmentsByOffset(&Parent, Child.ParentSegment)) {
602 Child.ParentSegment = &Parent;
608 template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() {
610 for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
611 ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
612 (size_t)Phdr.p_filesz};
613 Segment &Seg = Obj.addSegment(Data);
614 Seg.Type = Phdr.p_type;
615 Seg.Flags = Phdr.p_flags;
616 Seg.OriginalOffset = Phdr.p_offset;
617 Seg.Offset = Phdr.p_offset;
618 Seg.VAddr = Phdr.p_vaddr;
619 Seg.PAddr = Phdr.p_paddr;
620 Seg.FileSize = Phdr.p_filesz;
621 Seg.MemSize = Phdr.p_memsz;
622 Seg.Align = Phdr.p_align;
624 for (auto &Section : Obj.sections()) {
625 if (sectionWithinSegment(Section, Seg)) {
626 Seg.addSection(&Section);
627 if (!Section.ParentSegment ||
628 Section.ParentSegment->Offset > Seg.Offset) {
629 Section.ParentSegment = &Seg;
635 auto &ElfHdr = Obj.ElfHdrSegment;
636 // Creating multiple PT_PHDR segments technically is not valid, but PT_LOAD
637 // segments must not overlap, and other types fit even less.
638 ElfHdr.Type = PT_PHDR;
640 ElfHdr.OriginalOffset = ElfHdr.Offset = 0;
643 ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr);
645 ElfHdr.Index = Index++;
647 const auto &Ehdr = *ElfFile.getHeader();
648 auto &PrHdr = Obj.ProgramHdrSegment;
649 PrHdr.Type = PT_PHDR;
651 // The spec requires us to have p_vaddr % p_align == p_offset % p_align.
652 // Whereas this works automatically for ElfHdr, here OriginalOffset is
653 // always non-zero and to ensure the equation we assign the same value to
655 PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = Ehdr.e_phoff;
657 PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum;
658 // The spec requires us to naturally align all the fields.
659 PrHdr.Align = sizeof(Elf_Addr);
660 PrHdr.Index = Index++;
662 // Now we do an O(n^2) loop through the segments in order to match up
664 for (auto &Child : Obj.segments())
665 setParentSegment(Child);
666 setParentSegment(ElfHdr);
667 setParentSegment(PrHdr);
670 template <class ELFT>
671 void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) {
672 auto SecTable = Obj.sections();
673 auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>(
675 "Link field value " + Twine(GroupSec->Link) + " in section " +
676 GroupSec->Name + " is invalid",
677 "Link field value " + Twine(GroupSec->Link) + " in section " +
678 GroupSec->Name + " is not a symbol table");
679 auto Sym = SymTab->getSymbolByIndex(GroupSec->Info);
681 error("Info field value " + Twine(GroupSec->Info) + " in section " +
682 GroupSec->Name + " is not a valid symbol index");
683 GroupSec->setSymTab(SymTab);
684 GroupSec->setSymbol(Sym);
685 if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) ||
686 GroupSec->Contents.empty())
687 error("The content of the section " + GroupSec->Name + " is malformed");
688 const ELF::Elf32_Word *Word =
689 reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data());
690 const ELF::Elf32_Word *End =
691 Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word);
692 GroupSec->setFlagWord(*Word++);
693 for (; Word != End; ++Word) {
694 uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word);
695 GroupSec->addMember(SecTable.getSection(
696 Index, "Group member index " + Twine(Index) + " in section " +
697 GroupSec->Name + " is invalid"));
701 template <class ELFT>
702 void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
703 const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
704 StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
705 ArrayRef<Elf_Word> ShndxData;
707 auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr));
708 for (const auto &Sym : Symbols) {
709 SectionBase *DefSection = nullptr;
710 StringRef Name = unwrapOrError(Sym.getName(StrTabData));
712 if (Sym.st_shndx == SHN_XINDEX) {
713 if (SymTab->getShndxTable() == nullptr)
714 error("Symbol '" + Name +
715 "' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists.");
716 if (ShndxData.data() == nullptr) {
717 const Elf_Shdr &ShndxSec =
718 *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index));
719 ShndxData = unwrapOrError(
720 ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec));
721 if (ShndxData.size() != Symbols.size())
722 error("Symbol section index table does not have the same number of "
723 "entries as the symbol table.");
725 Elf_Word Index = ShndxData[&Sym - Symbols.begin()];
726 DefSection = Obj.sections().getSection(
728 "Symbol '" + Name + "' has invalid section index " +
730 } else if (Sym.st_shndx >= SHN_LORESERVE) {
731 if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
734 "' has unsupported value greater than or equal to SHN_LORESERVE: " +
735 Twine(Sym.st_shndx));
737 } else if (Sym.st_shndx != SHN_UNDEF) {
738 DefSection = Obj.sections().getSection(
739 Sym.st_shndx, "Symbol '" + Name +
740 "' is defined has invalid section index " +
741 Twine(Sym.st_shndx));
744 SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
745 Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size);
749 template <class ELFT>
750 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
752 template <class ELFT>
753 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
754 ToSet = Rela.r_addend;
758 static void initRelocations(RelocationSection *Relocs,
759 SymbolTableSection *SymbolTable, T RelRange) {
760 for (const auto &Rel : RelRange) {
762 ToAdd.Offset = Rel.r_offset;
763 getAddend(ToAdd.Addend, Rel);
764 ToAdd.Type = Rel.getType(false);
765 ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
766 Relocs->addRelocation(ToAdd);
770 SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) {
771 if (Index == SHN_UNDEF || Index > Sections.size())
773 return Sections[Index - 1].get();
777 T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg,
779 if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
784 template <class ELFT>
785 SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
786 ArrayRef<uint8_t> Data;
787 switch (Shdr.sh_type) {
790 if (Shdr.sh_flags & SHF_ALLOC) {
791 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
792 return Obj.addSection<DynamicRelocationSection>(Data);
794 return Obj.addSection<RelocationSection>();
796 // If a string table is allocated we don't want to mess with it. That would
797 // mean altering the memory image. There are no special link types or
798 // anything so we can just use a Section.
799 if (Shdr.sh_flags & SHF_ALLOC) {
800 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
801 return Obj.addSection<Section>(Data);
803 return Obj.addSection<StringTableSection>();
806 // Hash tables should refer to SHT_DYNSYM which we're not going to change.
807 // Because of this we don't need to mess with the hash tables either.
808 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
809 return Obj.addSection<Section>(Data);
811 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
812 return Obj.addSection<GroupSection>(Data);
814 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
815 return Obj.addSection<DynamicSymbolTableSection>(Data);
817 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
818 return Obj.addSection<DynamicSection>(Data);
820 auto &SymTab = Obj.addSection<SymbolTableSection>();
821 Obj.SymbolTable = &SymTab;
824 case SHT_SYMTAB_SHNDX: {
825 auto &ShndxSection = Obj.addSection<SectionIndexSection>();
826 Obj.SectionIndexTable = &ShndxSection;
830 return Obj.addSection<Section>(Data);
832 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
833 return Obj.addSection<Section>(Data);
837 template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
839 for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
844 auto &Sec = makeSection(Shdr);
845 Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
846 Sec.Type = Shdr.sh_type;
847 Sec.Flags = Shdr.sh_flags;
848 Sec.Addr = Shdr.sh_addr;
849 Sec.Offset = Shdr.sh_offset;
850 Sec.OriginalOffset = Shdr.sh_offset;
851 Sec.Size = Shdr.sh_size;
852 Sec.Link = Shdr.sh_link;
853 Sec.Info = Shdr.sh_info;
854 Sec.Align = Shdr.sh_addralign;
855 Sec.EntrySize = Shdr.sh_entsize;
859 // If a section index table exists we'll need to initialize it before we
860 // initialize the symbol table because the symbol table might need to
862 if (Obj.SectionIndexTable)
863 Obj.SectionIndexTable->initialize(Obj.sections());
865 // Now that all of the sections have been added we can fill out some extra
866 // details about symbol tables. We need the symbol table filled out before
868 if (Obj.SymbolTable) {
869 Obj.SymbolTable->initialize(Obj.sections());
870 initSymbolTable(Obj.SymbolTable);
873 // Now that all sections and symbols have been added we can add
874 // relocations that reference symbols and set the link and info fields for
875 // relocation sections.
876 for (auto &Section : Obj.sections()) {
877 if (&Section == Obj.SymbolTable)
879 Section.initialize(Obj.sections());
880 if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
881 auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
882 if (RelSec->Type == SHT_REL)
883 initRelocations(RelSec, Obj.SymbolTable,
884 unwrapOrError(ElfFile.rels(Shdr)));
886 initRelocations(RelSec, Obj.SymbolTable,
887 unwrapOrError(ElfFile.relas(Shdr)));
888 } else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) {
889 initGroupSection(GroupSec);
894 template <class ELFT> void ELFBuilder<ELFT>::build() {
895 const auto &Ehdr = *ElfFile.getHeader();
897 std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Obj.Ident);
898 Obj.Type = Ehdr.e_type;
899 Obj.Machine = Ehdr.e_machine;
900 Obj.Version = Ehdr.e_version;
901 Obj.Entry = Ehdr.e_entry;
902 Obj.Flags = Ehdr.e_flags;
904 readSectionHeaders();
905 readProgramHeaders();
907 uint32_t ShstrIndex = Ehdr.e_shstrndx;
908 if (ShstrIndex == SHN_XINDEX)
909 ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link;
912 Obj.sections().template getSectionOfType<StringTableSection>(
914 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
915 " in elf header " + " is invalid",
916 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
917 " in elf header " + " is not a string table");
920 // A generic size function which computes sizes of any random access range.
921 template <class R> size_t size(R &&Range) {
922 return static_cast<size_t>(std::end(Range) - std::begin(Range));
929 ElfType ELFReader::getElfType() const {
930 if (isa<ELFObjectFile<ELF32LE>>(Bin))
932 if (isa<ELFObjectFile<ELF64LE>>(Bin))
934 if (isa<ELFObjectFile<ELF32BE>>(Bin))
936 if (isa<ELFObjectFile<ELF64BE>>(Bin))
938 llvm_unreachable("Invalid ELFType");
941 std::unique_ptr<Object> ELFReader::create() const {
942 auto Obj = llvm::make_unique<Object>();
943 if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) {
944 ELFBuilder<ELF32LE> Builder(*o, *Obj);
947 } else if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) {
948 ELFBuilder<ELF64LE> Builder(*o, *Obj);
951 } else if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) {
952 ELFBuilder<ELF32BE> Builder(*o, *Obj);
955 } else if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
956 ELFBuilder<ELF64BE> Builder(*o, *Obj);
960 error("Invalid file type");
963 template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
964 uint8_t *B = Buf.getBufferStart();
965 Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(B);
966 std::copy(Obj.Ident, Obj.Ident + 16, Ehdr.e_ident);
967 Ehdr.e_type = Obj.Type;
968 Ehdr.e_machine = Obj.Machine;
969 Ehdr.e_version = Obj.Version;
970 Ehdr.e_entry = Obj.Entry;
971 Ehdr.e_phoff = Obj.ProgramHdrSegment.Offset;
972 Ehdr.e_flags = Obj.Flags;
973 Ehdr.e_ehsize = sizeof(Elf_Ehdr);
974 Ehdr.e_phentsize = sizeof(Elf_Phdr);
975 Ehdr.e_phnum = size(Obj.segments());
976 Ehdr.e_shentsize = sizeof(Elf_Shdr);
977 if (WriteSectionHeaders) {
978 Ehdr.e_shoff = Obj.SHOffset;
980 // If the number of sections is greater than or equal to
981 // SHN_LORESERVE (0xff00), this member has the value zero and the actual
982 // number of section header table entries is contained in the sh_size field
983 // of the section header at index 0.
985 auto Shnum = size(Obj.sections()) + 1;
986 if (Shnum >= SHN_LORESERVE)
989 Ehdr.e_shnum = Shnum;
991 // If the section name string table section index is greater than or equal
992 // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff)
993 // and the actual index of the section name string table section is
994 // contained in the sh_link field of the section header at index 0.
996 if (Obj.SectionNames->Index >= SHN_LORESERVE)
997 Ehdr.e_shstrndx = SHN_XINDEX;
999 Ehdr.e_shstrndx = Obj.SectionNames->Index;
1003 Ehdr.e_shstrndx = 0;
1007 template <class ELFT> void ELFWriter<ELFT>::writePhdrs() {
1008 for (auto &Seg : Obj.segments())
1012 template <class ELFT> void ELFWriter<ELFT>::writeShdrs() {
1013 uint8_t *B = Buf.getBufferStart() + Obj.SHOffset;
1014 // This reference serves to write the dummy section header at the begining
1015 // of the file. It is not used for anything else
1016 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
1018 Shdr.sh_type = SHT_NULL;
1022 // See writeEhdr for why we do this.
1023 uint64_t Shnum = size(Obj.sections()) + 1;
1024 if (Shnum >= SHN_LORESERVE)
1025 Shdr.sh_size = Shnum;
1028 // See writeEhdr for why we do this.
1029 if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE)
1030 Shdr.sh_link = Obj.SectionNames->Index;
1034 Shdr.sh_addralign = 0;
1035 Shdr.sh_entsize = 0;
1037 for (auto &Sec : Obj.sections())
1041 template <class ELFT> void ELFWriter<ELFT>::writeSectionData() {
1042 for (auto &Sec : Obj.sections())
1043 Sec.accept(*SecWriter);
1046 void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) {
1048 auto Iter = std::stable_partition(
1049 std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
1052 if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
1053 if (auto ToRelSec = RelSec->getSection())
1054 return !ToRemove(*ToRelSec);
1058 if (SymbolTable != nullptr && ToRemove(*SymbolTable))
1059 SymbolTable = nullptr;
1060 if (SectionNames != nullptr && ToRemove(*SectionNames))
1061 SectionNames = nullptr;
1062 if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable))
1063 SectionIndexTable = nullptr;
1064 // Now make sure there are no remaining references to the sections that will
1065 // be removed. Sometimes it is impossible to remove a reference so we emit
1066 // an error here instead.
1067 for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
1068 for (auto &Segment : Segments)
1069 Segment->removeSection(RemoveSec.get());
1070 for (auto &KeepSec : make_range(std::begin(Sections), Iter))
1071 KeepSec->removeSectionReferences(RemoveSec.get());
1073 // Now finally get rid of them all togethor.
1074 Sections.erase(Iter, std::end(Sections));
1077 void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
1081 for (const SecPtr &Sec : Sections)
1082 Sec->removeSymbols(ToRemove);
1085 void Object::sortSections() {
1086 // Put all sections in offset order. Maintain the ordering as closely as
1087 // possible while meeting that demand however.
1088 auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
1089 return A->OriginalOffset < B->OriginalOffset;
1091 std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
1095 static uint64_t alignToAddr(uint64_t Offset, uint64_t Addr, uint64_t Align) {
1096 // Calculate Diff such that (Offset + Diff) & -Align == Addr & -Align.
1100 static_cast<int64_t>(Addr % Align) - static_cast<int64_t>(Offset % Align);
1101 // We only want to add to Offset, however, so if Diff < 0 we can add Align and
1102 // (Offset + Diff) & -Align == Addr & -Align will still hold.
1105 return Offset + Diff;
1108 // Orders segments such that if x = y->ParentSegment then y comes before x.
1109 static void OrderSegments(std::vector<Segment *> &Segments) {
1110 std::stable_sort(std::begin(Segments), std::end(Segments),
1111 compareSegmentsByOffset);
1114 // This function finds a consistent layout for a list of segments starting from
1115 // an Offset. It assumes that Segments have been sorted by OrderSegments and
1116 // returns an Offset one past the end of the last segment.
1117 static uint64_t LayoutSegments(std::vector<Segment *> &Segments,
1119 assert(std::is_sorted(std::begin(Segments), std::end(Segments),
1120 compareSegmentsByOffset));
1121 // The only way a segment should move is if a section was between two
1122 // segments and that section was removed. If that section isn't in a segment
1123 // then it's acceptable, but not ideal, to simply move it to after the
1124 // segments. So we can simply layout segments one after the other accounting
1126 for (auto &Segment : Segments) {
1127 // We assume that segments have been ordered by OriginalOffset and Index
1128 // such that a parent segment will always come before a child segment in
1129 // OrderedSegments. This means that the Offset of the ParentSegment should
1130 // already be set and we can set our offset relative to it.
1131 if (Segment->ParentSegment != nullptr) {
1132 auto Parent = Segment->ParentSegment;
1134 Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset;
1136 Offset = alignToAddr(Offset, Segment->VAddr, Segment->Align);
1137 Segment->Offset = Offset;
1139 Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
1144 // This function finds a consistent layout for a list of sections. It assumes
1145 // that the ->ParentSegment of each section has already been laid out. The
1146 // supplied starting Offset is used for the starting offset of any section that
1147 // does not have a ParentSegment. It returns either the offset given if all
1148 // sections had a ParentSegment or an offset one past the last section if there
1149 // was a section that didn't have a ParentSegment.
1150 template <class Range>
1151 static uint64_t LayoutSections(Range Sections, uint64_t Offset) {
1152 // Now the offset of every segment has been set we can assign the offsets
1153 // of each section. For sections that are covered by a segment we should use
1154 // the segment's original offset and the section's original offset to compute
1155 // the offset from the start of the segment. Using the offset from the start
1156 // of the segment we can assign a new offset to the section. For sections not
1157 // covered by segments we can just bump Offset to the next valid location.
1159 for (auto &Section : Sections) {
1160 Section.Index = Index++;
1161 if (Section.ParentSegment != nullptr) {
1162 auto Segment = *Section.ParentSegment;
1164 Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
1166 Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
1167 Section.Offset = Offset;
1168 if (Section.Type != SHT_NOBITS)
1169 Offset += Section.Size;
1175 template <class ELFT> void ELFWriter<ELFT>::assignOffsets() {
1176 // We need a temporary list of segments that has a special order to it
1177 // so that we know that anytime ->ParentSegment is set that segment has
1178 // already had its offset properly set.
1179 std::vector<Segment *> OrderedSegments;
1180 for (auto &Segment : Obj.segments())
1181 OrderedSegments.push_back(&Segment);
1182 OrderedSegments.push_back(&Obj.ElfHdrSegment);
1183 OrderedSegments.push_back(&Obj.ProgramHdrSegment);
1184 OrderSegments(OrderedSegments);
1185 // Offset is used as the start offset of the first segment to be laid out.
1186 // Since the ELF Header (ElfHdrSegment) must be at the start of the file,
1187 // we start at offset 0.
1188 uint64_t Offset = 0;
1189 Offset = LayoutSegments(OrderedSegments, Offset);
1190 Offset = LayoutSections(Obj.sections(), Offset);
1191 // If we need to write the section header table out then we need to align the
1192 // Offset so that SHOffset is valid.
1193 if (WriteSectionHeaders)
1194 Offset = alignTo(Offset, sizeof(typename ELFT::Addr));
1195 Obj.SHOffset = Offset;
1198 template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const {
1199 // We already have the section header offset so we can calculate the total
1200 // size by just adding up the size of each section header.
1201 auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
1202 return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) +
1206 template <class ELFT> void ELFWriter<ELFT>::write() {
1210 if (WriteSectionHeaders)
1212 if (auto E = Buf.commit())
1213 reportError(Buf.getName(), errorToErrorCode(std::move(E)));
1216 template <class ELFT> void ELFWriter<ELFT>::finalize() {
1217 // It could happen that SectionNames has been removed and yet the user wants
1218 // a section header table output. We need to throw an error if a user tries
1220 if (Obj.SectionNames == nullptr && WriteSectionHeaders)
1221 error("Cannot write section header table because section header string "
1222 "table was removed.");
1226 // We need to assign indexes before we perform layout because we need to know
1227 // if we need large indexes or not. We can assign indexes first and check as
1228 // we go to see if we will actully need large indexes.
1229 bool NeedsLargeIndexes = false;
1230 if (size(Obj.sections()) >= SHN_LORESERVE) {
1231 auto Sections = Obj.sections();
1233 std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(),
1234 [](const SectionBase &Sec) { return Sec.HasSymbol; });
1235 // TODO: handle case where only one section needs the large index table but
1236 // only needs it because the large index table hasn't been removed yet.
1239 if (NeedsLargeIndexes) {
1240 // This means we definitely need to have a section index table but if we
1241 // already have one then we should use it instead of making a new one.
1242 if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) {
1243 // Addition of a section to the end does not invalidate the indexes of
1244 // other sections and assigns the correct index to the new section.
1245 auto &Shndx = Obj.addSection<SectionIndexSection>();
1246 Obj.SymbolTable->setShndxTable(&Shndx);
1247 Shndx.setSymTab(Obj.SymbolTable);
1250 // Since we don't need SectionIndexTable we should remove it and all
1251 // references to it.
1252 if (Obj.SectionIndexTable != nullptr) {
1253 Obj.removeSections([this](const SectionBase &Sec) {
1254 return &Sec == Obj.SectionIndexTable;
1259 // Make sure we add the names of all the sections. Importantly this must be
1260 // done after we decide to add or remove SectionIndexes.
1261 if (Obj.SectionNames != nullptr)
1262 for (const auto &Section : Obj.sections()) {
1263 Obj.SectionNames->addString(Section.Name);
1266 // Before we can prepare for layout the indexes need to be finalized.
1268 for (auto &Sec : Obj.sections())
1269 Sec.Index = Index++;
1271 // The symbol table does not update all other sections on update. For
1272 // instance, symbol names are not added as new symbols are added. This means
1273 // that some sections, like .strtab, don't yet have their final size.
1274 if (Obj.SymbolTable != nullptr)
1275 Obj.SymbolTable->prepareForLayout();
1279 // Finalize SectionNames first so that we can assign name indexes.
1280 if (Obj.SectionNames != nullptr)
1281 Obj.SectionNames->finalize();
1282 // Finally now that all offsets and indexes have been set we can finalize any
1283 // remaining issues.
1284 uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
1285 for (auto &Section : Obj.sections()) {
1286 Section.HeaderOffset = Offset;
1287 Offset += sizeof(Elf_Shdr);
1288 if (WriteSectionHeaders)
1289 Section.NameIndex = Obj.SectionNames->findIndex(Section.Name);
1293 Buf.allocate(totalSize());
1294 SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(Buf);
1297 void BinaryWriter::write() {
1298 for (auto &Section : Obj.sections()) {
1299 if ((Section.Flags & SHF_ALLOC) == 0)
1301 Section.accept(*SecWriter);
1303 if (auto E = Buf.commit())
1304 reportError(Buf.getName(), errorToErrorCode(std::move(E)));
1307 void BinaryWriter::finalize() {
1308 // TODO: Create a filter range to construct OrderedSegments from so that this
1309 // code can be deduped with assignOffsets above. This should also solve the
1310 // todo below for LayoutSections.
1311 // We need a temporary list of segments that has a special order to it
1312 // so that we know that anytime ->ParentSegment is set that segment has
1313 // already had it's offset properly set. We only want to consider the segments
1314 // that will affect layout of allocated sections so we only add those.
1315 std::vector<Segment *> OrderedSegments;
1316 for (auto &Section : Obj.sections()) {
1317 if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) {
1318 OrderedSegments.push_back(Section.ParentSegment);
1322 // For binary output, we're going to use physical addresses instead of
1323 // virtual addresses, since a binary output is used for cases like ROM
1324 // loading and physical addresses are intended for ROM loading.
1325 // However, if no segment has a physical address, we'll fallback to using
1326 // virtual addresses for all.
1327 if (std::all_of(std::begin(OrderedSegments), std::end(OrderedSegments),
1328 [](const Segment *Segment) { return Segment->PAddr == 0; }))
1329 for (const auto &Segment : OrderedSegments)
1330 Segment->PAddr = Segment->VAddr;
1332 std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments),
1333 compareSegmentsByPAddr);
1335 // Because we add a ParentSegment for each section we might have duplicate
1336 // segments in OrderedSegments. If there were duplicates then LayoutSegments
1337 // would do very strange things.
1339 std::unique(std::begin(OrderedSegments), std::end(OrderedSegments));
1340 OrderedSegments.erase(End, std::end(OrderedSegments));
1342 uint64_t Offset = 0;
1344 // Modify the first segment so that there is no gap at the start. This allows
1345 // our layout algorithm to proceed as expected while not out writing out the
1346 // gap at the start.
1347 if (!OrderedSegments.empty()) {
1348 auto Seg = OrderedSegments[0];
1349 auto Sec = Seg->firstSection();
1350 auto Diff = Sec->OriginalOffset - Seg->OriginalOffset;
1351 Seg->OriginalOffset += Diff;
1352 // The size needs to be shrunk as well.
1353 Seg->FileSize -= Diff;
1354 // The PAddr needs to be increased to remove the gap before the first
1357 uint64_t LowestPAddr = Seg->PAddr;
1358 for (auto &Segment : OrderedSegments) {
1359 Segment->Offset = Segment->PAddr - LowestPAddr;
1360 Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
1364 // TODO: generalize LayoutSections to take a range. Pass a special range
1365 // constructed from an iterator that skips values for which a predicate does
1366 // not hold. Then pass such a range to LayoutSections instead of constructing
1367 // AllocatedSections here.
1368 std::vector<SectionBase *> AllocatedSections;
1369 for (auto &Section : Obj.sections()) {
1370 if ((Section.Flags & SHF_ALLOC) == 0)
1372 AllocatedSections.push_back(&Section);
1374 LayoutSections(make_pointee_range(AllocatedSections), Offset);
1376 // Now that every section has been laid out we just need to compute the total
1377 // file size. This might not be the same as the offset returned by
1378 // LayoutSections, because we want to truncate the last segment to the end of
1379 // its last section, to match GNU objcopy's behaviour.
1381 for (const auto &Section : AllocatedSections) {
1382 if (Section->Type != SHT_NOBITS)
1383 TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
1386 Buf.allocate(TotalSize);
1387 SecWriter = llvm::make_unique<BinarySectionWriter>(Buf);
1393 template class ELFBuilder<ELF64LE>;
1394 template class ELFBuilder<ELF64BE>;
1395 template class ELFBuilder<ELF32LE>;
1396 template class ELFBuilder<ELF32BE>;
1398 template class ELFWriter<ELF64LE>;
1399 template class ELFWriter<ELF64BE>;
1400 template class ELFWriter<ELF32LE>;
1401 template class ELFWriter<ELF32BE>;
1402 } // end namespace objcopy
1403 } // end namespace llvm