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