1 //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements ELF object file writer information.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/MC/MCAsmBackend.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCAsmLayout.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCFixupKindInfo.h"
26 #include "llvm/MC/MCObjectWriter.h"
27 #include "llvm/MC/MCSectionELF.h"
28 #include "llvm/MC/MCSymbolELF.h"
29 #include "llvm/MC/MCValue.h"
30 #include "llvm/MC/StringTableBuilder.h"
31 #include "llvm/Support/Compression.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ELF.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/StringSaver.h"
41 #define DEBUG_TYPE "reloc-info"
45 typedef DenseMap<const MCSectionELF *, uint32_t> SectionIndexMapTy;
47 class ELFObjectWriter;
49 class SymbolTableWriter {
50 ELFObjectWriter &EWriter;
53 // indexes we are going to write to .symtab_shndx.
54 std::vector<uint32_t> ShndxIndexes;
56 // The numbel of symbols written so far.
59 void createSymtabShndx();
61 template <typename T> void write(T Value);
64 SymbolTableWriter(ELFObjectWriter &EWriter, bool Is64Bit);
66 void writeSymbol(uint32_t name, uint8_t info, uint64_t value, uint64_t size,
67 uint8_t other, uint32_t shndx, bool Reserved);
69 ArrayRef<uint32_t> getShndxIndexes() const { return ShndxIndexes; }
72 class ELFObjectWriter : public MCObjectWriter {
73 static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
74 static uint64_t SymbolValue(const MCSymbol &Sym, const MCAsmLayout &Layout);
75 static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
76 bool Used, bool Renamed);
78 /// Helper struct for containing some precomputed information on symbols.
79 struct ELFSymbolData {
80 const MCSymbolELF *Symbol;
81 uint32_t SectionIndex;
84 // Support lexicographic sorting.
85 bool operator<(const ELFSymbolData &RHS) const {
86 unsigned LHSType = Symbol->getType();
87 unsigned RHSType = RHS.Symbol->getType();
88 if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION)
90 if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
92 if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
93 return SectionIndex < RHS.SectionIndex;
94 return Name < RHS.Name;
98 /// The target specific ELF writer instance.
99 std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
101 DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
103 llvm::DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>>
107 /// @name Symbol Table Data
110 BumpPtrAllocator Alloc;
111 StringSaver VersionSymSaver{Alloc};
112 StringTableBuilder StrTabBuilder{StringTableBuilder::ELF};
116 // This holds the symbol table index of the last local symbol.
117 unsigned LastLocalSymbolIndex;
118 // This holds the .strtab section index.
119 unsigned StringTableIndex;
120 // This holds the .symtab section index.
121 unsigned SymbolTableIndex;
123 // Sections in the order they are to be output in the section table.
124 std::vector<const MCSectionELF *> SectionTable;
125 unsigned addToSectionTable(const MCSectionELF *Sec);
127 // TargetObjectWriter wrappers.
128 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
129 bool hasRelocationAddend() const {
130 return TargetObjectWriter->hasRelocationAddend();
132 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
133 bool IsPCRel) const {
134 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel);
137 void align(unsigned Alignment);
140 ELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_pwrite_stream &OS,
142 : MCObjectWriter(OS, IsLittleEndian), TargetObjectWriter(MOTW) {}
144 void reset() override {
147 StrTabBuilder.clear();
148 SectionTable.clear();
149 MCObjectWriter::reset();
152 ~ELFObjectWriter() override;
154 void WriteWord(uint64_t W) {
161 template <typename T> void write(T Val) {
163 support::endian::Writer<support::little>(getStream()).write(Val);
165 support::endian::Writer<support::big>(getStream()).write(Val);
168 void writeHeader(const MCAssembler &Asm);
170 void writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
171 ELFSymbolData &MSD, const MCAsmLayout &Layout);
173 // Start and end offset of each section
174 typedef std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>>
177 bool shouldRelocateWithSymbol(const MCAssembler &Asm,
178 const MCSymbolRefExpr *RefA,
179 const MCSymbol *Sym, uint64_t C,
180 unsigned Type) const;
182 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
183 const MCFragment *Fragment, const MCFixup &Fixup,
184 MCValue Target, bool &IsPCRel,
185 uint64_t &FixedValue) override;
187 // Map from a signature symbol to the group section index
188 typedef DenseMap<const MCSymbol *, unsigned> RevGroupMapTy;
190 /// Compute the symbol table data
192 /// \param Asm - The assembler.
193 /// \param SectionIndexMap - Maps a section to its index.
194 /// \param RevGroupMap - Maps a signature symbol to the group section.
195 void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
196 const SectionIndexMapTy &SectionIndexMap,
197 const RevGroupMapTy &RevGroupMap,
198 SectionOffsetsTy &SectionOffsets);
200 MCSectionELF *createRelocationSection(MCContext &Ctx,
201 const MCSectionELF &Sec);
203 const MCSectionELF *createStringTable(MCContext &Ctx);
205 void executePostLayoutBinding(MCAssembler &Asm,
206 const MCAsmLayout &Layout) override;
208 void writeSectionHeader(const MCAsmLayout &Layout,
209 const SectionIndexMapTy &SectionIndexMap,
210 const SectionOffsetsTy &SectionOffsets);
212 void writeSectionData(const MCAssembler &Asm, MCSection &Sec,
213 const MCAsmLayout &Layout);
215 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
216 uint64_t Address, uint64_t Offset, uint64_t Size,
217 uint32_t Link, uint32_t Info, uint64_t Alignment,
220 void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
222 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
223 const MCSymbol &SymA,
224 const MCFragment &FB,
226 bool IsPCRel) const override;
228 bool isWeak(const MCSymbol &Sym) const override;
230 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
231 void writeSection(const SectionIndexMapTy &SectionIndexMap,
232 uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size,
233 const MCSectionELF &Section);
237 void ELFObjectWriter::align(unsigned Alignment) {
238 uint64_t Padding = OffsetToAlignment(getStream().tell(), Alignment);
242 unsigned ELFObjectWriter::addToSectionTable(const MCSectionELF *Sec) {
243 SectionTable.push_back(Sec);
244 StrTabBuilder.add(Sec->getSectionName());
245 return SectionTable.size();
248 void SymbolTableWriter::createSymtabShndx() {
249 if (!ShndxIndexes.empty())
252 ShndxIndexes.resize(NumWritten);
255 template <typename T> void SymbolTableWriter::write(T Value) {
256 EWriter.write(Value);
259 SymbolTableWriter::SymbolTableWriter(ELFObjectWriter &EWriter, bool Is64Bit)
260 : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {}
262 void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
263 uint64_t size, uint8_t other,
264 uint32_t shndx, bool Reserved) {
265 bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
270 if (!ShndxIndexes.empty()) {
272 ShndxIndexes.push_back(shndx);
274 ShndxIndexes.push_back(0);
277 uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
280 write(name); // st_name
281 write(info); // st_info
282 write(other); // st_other
283 write(Index); // st_shndx
284 write(value); // st_value
285 write(size); // st_size
287 write(name); // st_name
288 write(uint32_t(value)); // st_value
289 write(uint32_t(size)); // st_size
290 write(info); // st_info
291 write(other); // st_other
292 write(Index); // st_shndx
298 bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
299 const MCFixupKindInfo &FKI =
300 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
302 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
305 ELFObjectWriter::~ELFObjectWriter()
308 // Emit the ELF header.
309 void ELFObjectWriter::writeHeader(const MCAssembler &Asm) {
315 // emitWord method behaves differently for ELF32 and ELF64, writing
316 // 4 bytes in the former and 8 in the latter.
318 writeBytes(ELF::ElfMagic); // e_ident[EI_MAG0] to e_ident[EI_MAG3]
320 write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
323 write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
325 write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
327 write8(TargetObjectWriter->getOSABI());
328 write8(0); // e_ident[EI_ABIVERSION]
330 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
332 write16(ELF::ET_REL); // e_type
334 write16(TargetObjectWriter->getEMachine()); // e_machine = target
336 write32(ELF::EV_CURRENT); // e_version
337 WriteWord(0); // e_entry, no entry point in .o file
338 WriteWord(0); // e_phoff, no program header for .o
339 WriteWord(0); // e_shoff = sec hdr table off in bytes
341 // e_flags = whatever the target wants
342 write32(Asm.getELFHeaderEFlags());
344 // e_ehsize = ELF header size
345 write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
347 write16(0); // e_phentsize = prog header entry size
348 write16(0); // e_phnum = # prog header entries = 0
350 // e_shentsize = Section header entry size
351 write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
353 // e_shnum = # of section header ents
356 // e_shstrndx = Section # of '.shstrtab'
357 assert(StringTableIndex < ELF::SHN_LORESERVE);
358 write16(StringTableIndex);
361 uint64_t ELFObjectWriter::SymbolValue(const MCSymbol &Sym,
362 const MCAsmLayout &Layout) {
363 if (Sym.isCommon() && Sym.isExternal())
364 return Sym.getCommonAlignment();
367 if (!Layout.getSymbolOffset(Sym, Res))
370 if (Layout.getAssembler().isThumbFunc(&Sym))
376 void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
377 const MCAsmLayout &Layout) {
378 // The presence of symbol versions causes undefined symbols and
379 // versions declared with @@@ to be renamed.
381 for (const MCSymbol &A : Asm.symbols()) {
382 const auto &Alias = cast<MCSymbolELF>(A);
384 if (!Alias.isVariable())
386 auto *Ref = dyn_cast<MCSymbolRefExpr>(Alias.getVariableValue());
389 const auto &Symbol = cast<MCSymbolELF>(Ref->getSymbol());
391 StringRef AliasName = Alias.getName();
392 size_t Pos = AliasName.find('@');
393 if (Pos == StringRef::npos)
396 // Aliases defined with .symvar copy the binding from the symbol they alias.
397 // This is the first place we are able to copy this information.
398 Alias.setExternal(Symbol.isExternal());
399 Alias.setBinding(Symbol.getBinding());
401 StringRef Rest = AliasName.substr(Pos);
402 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
405 // FIXME: produce a better error message.
406 if (Symbol.isUndefined() && Rest.startswith("@@") &&
407 !Rest.startswith("@@@"))
408 report_fatal_error("A @@ version cannot be undefined");
410 Renames.insert(std::make_pair(&Symbol, &Alias));
414 static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
415 uint8_t Type = newType;
417 // Propagation rules:
418 // IFUNC > FUNC > OBJECT > NOTYPE
419 // TLS_OBJECT > OBJECT > NOTYPE
421 // dont let the new type degrade the old type
425 case ELF::STT_GNU_IFUNC:
426 if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
427 Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
428 Type = ELF::STT_GNU_IFUNC;
431 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
432 Type == ELF::STT_TLS)
433 Type = ELF::STT_FUNC;
435 case ELF::STT_OBJECT:
436 if (Type == ELF::STT_NOTYPE)
437 Type = ELF::STT_OBJECT;
440 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
441 Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
449 void ELFObjectWriter::writeSymbol(SymbolTableWriter &Writer,
450 uint32_t StringIndex, ELFSymbolData &MSD,
451 const MCAsmLayout &Layout) {
452 const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol);
453 const MCSymbolELF *Base =
454 cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol));
456 // This has to be in sync with when computeSymbolTable uses SHN_ABS or
458 bool IsReserved = !Base || Symbol.isCommon();
460 // Binding and Type share the same byte as upper and lower nibbles
461 uint8_t Binding = Symbol.getBinding();
462 uint8_t Type = Symbol.getType();
464 Type = mergeTypeForSet(Type, Base->getType());
466 uint8_t Info = (Binding << 4) | Type;
468 // Other and Visibility share the same byte with Visibility using the lower
470 uint8_t Visibility = Symbol.getVisibility();
471 uint8_t Other = Symbol.getOther() | Visibility;
473 uint64_t Value = SymbolValue(*MSD.Symbol, Layout);
476 const MCExpr *ESize = MSD.Symbol->getSize();
478 ESize = Base->getSize();
482 if (!ESize->evaluateKnownAbsolute(Res, Layout))
483 report_fatal_error("Size expression must be absolute.");
487 // Write out the symbol table entry
488 Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex,
492 // It is always valid to create a relocation with a symbol. It is preferable
493 // to use a relocation with a section if that is possible. Using the section
494 // allows us to omit some local symbols from the symbol table.
495 bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
496 const MCSymbolRefExpr *RefA,
497 const MCSymbol *S, uint64_t C,
498 unsigned Type) const {
499 const auto *Sym = cast_or_null<MCSymbolELF>(S);
500 // A PCRel relocation to an absolute value has no symbol (or section). We
501 // represent that with a relocation to a null section.
505 MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
509 // The .odp creation emits a relocation against the symbol ".TOC." which
510 // create a R_PPC64_TOC relocation. However the relocation symbol name
511 // in final object creation should be NULL, since the symbol does not
512 // really exist, it is just the reference to TOC base for the current
513 // object file. Since the symbol is undefined, returning false results
514 // in a relocation with a null section which is the desired result.
515 case MCSymbolRefExpr::VK_PPC_TOCBASE:
518 // These VariantKind cause the relocation to refer to something other than
519 // the symbol itself, like a linker generated table. Since the address of
520 // symbol is not relevant, we cannot replace the symbol with the
521 // section and patch the difference in the addend.
522 case MCSymbolRefExpr::VK_GOT:
523 case MCSymbolRefExpr::VK_PLT:
524 case MCSymbolRefExpr::VK_GOTPCREL:
525 case MCSymbolRefExpr::VK_Mips_GOT:
526 case MCSymbolRefExpr::VK_PPC_GOT_LO:
527 case MCSymbolRefExpr::VK_PPC_GOT_HI:
528 case MCSymbolRefExpr::VK_PPC_GOT_HA:
532 // An undefined symbol is not in any section, so the relocation has to point
533 // to the symbol itself.
534 assert(Sym && "Expected a symbol");
535 if (Sym->isUndefined())
538 unsigned Binding = Sym->getBinding();
541 llvm_unreachable("Invalid Binding");
545 // If the symbol is weak, it might be overridden by a symbol in another
546 // file. The relocation has to point to the symbol so that the linker
549 case ELF::STB_GLOBAL:
550 // Global ELF symbols can be preempted by the dynamic linker. The relocation
551 // has to point to the symbol for a reason analogous to the STB_WEAK case.
555 // If a relocation points to a mergeable section, we have to be careful.
556 // If the offset is zero, a relocation with the section will encode the
557 // same information. With a non-zero offset, the situation is different.
558 // For example, a relocation can point 42 bytes past the end of a string.
559 // If we change such a relocation to use the section, the linker would think
560 // that it pointed to another string and subtracting 42 at runtime will
561 // produce the wrong value.
562 auto &Sec = cast<MCSectionELF>(Sym->getSection());
563 unsigned Flags = Sec.getFlags();
564 if (Flags & ELF::SHF_MERGE) {
568 // It looks like gold has a bug (http://sourceware.org/PR16794) and can
569 // only handle section relocations to mergeable sections if using RELA.
570 if (!hasRelocationAddend())
574 // Most TLS relocations use a got, so they need the symbol. Even those that
575 // are just an offset (@tpoff), require a symbol in gold versions before
576 // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
577 // http://sourceware.org/PR16773.
578 if (Flags & ELF::SHF_TLS)
581 // If the symbol is a thumb function the final relocation must set the lowest
582 // bit. With a symbol that is done by just having the symbol have that bit
583 // set, so we would lose the bit if we relocated with the section.
584 // FIXME: We could use the section but add the bit to the relocation value.
585 if (Asm.isThumbFunc(Sym))
588 if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type))
593 // True if the assembler knows nothing about the final value of the symbol.
594 // This doesn't cover the comdat issues, since in those cases the assembler
595 // can at least know that all symbols in the section will move together.
596 static bool isWeak(const MCSymbolELF &Sym) {
597 if (Sym.getType() == ELF::STT_GNU_IFUNC)
600 switch (Sym.getBinding()) {
602 llvm_unreachable("Unknown binding");
605 case ELF::STB_GLOBAL:
608 case ELF::STB_GNU_UNIQUE:
613 void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
614 const MCAsmLayout &Layout,
615 const MCFragment *Fragment,
616 const MCFixup &Fixup, MCValue Target,
617 bool &IsPCRel, uint64_t &FixedValue) {
618 const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
619 uint64_t C = Target.getConstant();
620 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
622 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
623 assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
624 "Should not have constructed this");
626 // Let A, B and C being the components of Target and R be the location of
627 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
628 // If it is pcrel, we want to compute (A - B + C - R).
630 // In general, ELF has no relocations for -B. It can only represent (A + C)
631 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
632 // replace B to implement it: (A - R - K + C)
634 Asm.getContext().reportError(
636 "No relocation available to represent this relative expression");
640 const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol());
642 if (SymB.isUndefined()) {
643 Asm.getContext().reportError(
645 Twine("symbol '") + SymB.getName() +
646 "' can not be undefined in a subtraction expression");
650 assert(!SymB.isAbsolute() && "Should have been folded");
651 const MCSection &SecB = SymB.getSection();
652 if (&SecB != &FixupSection) {
653 Asm.getContext().reportError(
654 Fixup.getLoc(), "Cannot represent a difference across sections");
658 if (::isWeak(SymB)) {
659 Asm.getContext().reportError(
660 Fixup.getLoc(), "Cannot represent a subtraction with a weak symbol");
664 uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
665 uint64_t K = SymBOffset - FixupOffset;
670 // We either rejected the fixup or folded B into C at this point.
671 const MCSymbolRefExpr *RefA = Target.getSymA();
672 const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
674 bool ViaWeakRef = false;
675 if (SymA && SymA->isVariable()) {
676 const MCExpr *Expr = SymA->getVariableValue();
677 if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
678 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
679 SymA = cast<MCSymbolELF>(&Inner->getSymbol());
685 unsigned Type = GetRelocType(Target, Fixup, IsPCRel);
686 bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type);
687 if (!RelocateWithSymbol && SymA && !SymA->isUndefined())
688 C += Layout.getSymbolOffset(*SymA);
691 if (hasRelocationAddend()) {
698 if (!RelocateWithSymbol) {
699 const MCSection *SecA =
700 (SymA && !SymA->isUndefined()) ? &SymA->getSection() : nullptr;
701 auto *ELFSec = cast_or_null<MCSectionELF>(SecA);
702 const auto *SectionSymbol =
703 ELFSec ? cast<MCSymbolELF>(ELFSec->getBeginSymbol()) : nullptr;
705 SectionSymbol->setUsedInReloc();
706 ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend);
707 Relocations[&FixupSection].push_back(Rec);
712 if (const MCSymbolELF *R = Renames.lookup(SymA))
716 SymA->setIsWeakrefUsedInReloc();
718 SymA->setUsedInReloc();
720 ELFRelocationEntry Rec(FixupOffset, SymA, Type, Addend);
721 Relocations[&FixupSection].push_back(Rec);
725 bool ELFObjectWriter::isInSymtab(const MCAsmLayout &Layout,
726 const MCSymbolELF &Symbol, bool Used,
728 if (Symbol.isVariable()) {
729 const MCExpr *Expr = Symbol.getVariableValue();
730 if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
731 if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
742 if (Symbol.isVariable() && Symbol.isUndefined()) {
743 // FIXME: this is here just to diagnose the case of a var = commmon_sym.
744 Layout.getBaseSymbol(Symbol);
748 if (Symbol.isUndefined() && !Symbol.isBindingSet())
751 if (Symbol.isTemporary())
754 if (Symbol.getType() == ELF::STT_SECTION)
760 void ELFObjectWriter::computeSymbolTable(
761 MCAssembler &Asm, const MCAsmLayout &Layout,
762 const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
763 SectionOffsetsTy &SectionOffsets) {
764 MCContext &Ctx = Asm.getContext();
765 SymbolTableWriter Writer(*this, is64Bit());
768 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
769 MCSectionELF *SymtabSection =
770 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, "");
771 SymtabSection->setAlignment(is64Bit() ? 8 : 4);
772 SymbolTableIndex = addToSectionTable(SymtabSection);
774 align(SymtabSection->getAlignment());
775 uint64_t SecStart = getStream().tell();
777 // The first entry is the undefined symbol entry.
778 Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
780 std::vector<ELFSymbolData> LocalSymbolData;
781 std::vector<ELFSymbolData> ExternalSymbolData;
783 // Add the data for the symbols.
784 bool HasLargeSectionIndex = false;
785 for (const MCSymbol &S : Asm.symbols()) {
786 const auto &Symbol = cast<MCSymbolELF>(S);
787 bool Used = Symbol.isUsedInReloc();
788 bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
789 bool isSignature = Symbol.isSignature();
791 if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
792 Renames.count(&Symbol)))
795 if (Symbol.isTemporary() && Symbol.isUndefined()) {
796 Ctx.reportError(SMLoc(), "Undefined temporary symbol");
801 MSD.Symbol = cast<MCSymbolELF>(&Symbol);
803 bool Local = Symbol.getBinding() == ELF::STB_LOCAL;
804 assert(Local || !Symbol.isTemporary());
806 if (Symbol.isAbsolute()) {
807 MSD.SectionIndex = ELF::SHN_ABS;
808 } else if (Symbol.isCommon()) {
810 MSD.SectionIndex = ELF::SHN_COMMON;
811 } else if (Symbol.isUndefined()) {
812 if (isSignature && !Used) {
813 MSD.SectionIndex = RevGroupMap.lookup(&Symbol);
814 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
815 HasLargeSectionIndex = true;
817 MSD.SectionIndex = ELF::SHN_UNDEF;
820 const MCSectionELF &Section =
821 static_cast<const MCSectionELF &>(Symbol.getSection());
822 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
823 assert(MSD.SectionIndex && "Invalid section index!");
824 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
825 HasLargeSectionIndex = true;
828 // The @@@ in symbol version is replaced with @ in undefined symbols and @@
831 // FIXME: All name handling should be done before we get to the writer,
832 // including dealing with GNU-style version suffixes. Fixing this isn't
835 // We thus have to be careful to not perform the symbol version replacement
838 // The ELF format is used on Windows by the MCJIT engine. Thus, on
839 // Windows, the ELFObjectWriter can encounter symbols mangled using the MS
840 // Visual Studio C++ name mangling scheme. Symbols mangled using the MSVC
841 // C++ name mangling can legally have "@@@" as a sub-string. In that case,
842 // the EFLObjectWriter should not interpret the "@@@" sub-string as
843 // specifying GNU-style symbol versioning. The ELFObjectWriter therefore
844 // checks for the MSVC C++ name mangling prefix which is either "?", "@?",
845 // "__imp_?" or "__imp_@?".
847 // It would have been interesting to perform the MS mangling prefix check
848 // only when the target triple is of the form *-pc-windows-elf. But, it
849 // seems that this information is not easily accessible from the
851 StringRef Name = Symbol.getName();
853 if (!Name.startswith("?") && !Name.startswith("@?") &&
854 !Name.startswith("__imp_?") && !Name.startswith("__imp_@?")) {
855 // This symbol isn't following the MSVC C++ name mangling convention. We
856 // can thus safely interpret the @@@ in symbol names as specifying symbol
858 size_t Pos = Name.find("@@@");
859 if (Pos != StringRef::npos) {
860 Buf += Name.substr(0, Pos);
861 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
862 Buf += Name.substr(Pos + Skip);
863 Name = VersionSymSaver.save(Buf.c_str());
867 // Sections have their own string table
868 if (Symbol.getType() != ELF::STT_SECTION) {
870 StrTabBuilder.add(Name);
874 LocalSymbolData.push_back(MSD);
876 ExternalSymbolData.push_back(MSD);
879 // This holds the .symtab_shndx section index.
880 unsigned SymtabShndxSectionIndex = 0;
882 if (HasLargeSectionIndex) {
883 MCSectionELF *SymtabShndxSection =
884 Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");
885 SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);
886 SymtabShndxSection->setAlignment(4);
889 ArrayRef<std::string> FileNames = Asm.getFileNames();
890 for (const std::string &Name : FileNames)
891 StrTabBuilder.add(Name);
893 StrTabBuilder.finalize();
895 for (const std::string &Name : FileNames)
896 Writer.writeSymbol(StrTabBuilder.getOffset(Name),
897 ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
900 // Symbols are required to be in lexicographic order.
901 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
902 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
904 // Set the symbol indices. Local symbols must come before all other
905 // symbols with non-local bindings.
906 unsigned Index = FileNames.size() + 1;
908 for (ELFSymbolData &MSD : LocalSymbolData) {
909 unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION
911 : StrTabBuilder.getOffset(MSD.Name);
912 MSD.Symbol->setIndex(Index++);
913 writeSymbol(Writer, StringIndex, MSD, Layout);
916 // Write the symbol table entries.
917 LastLocalSymbolIndex = Index;
919 for (ELFSymbolData &MSD : ExternalSymbolData) {
920 unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
921 MSD.Symbol->setIndex(Index++);
922 writeSymbol(Writer, StringIndex, MSD, Layout);
923 assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
926 uint64_t SecEnd = getStream().tell();
927 SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);
929 ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();
930 if (ShndxIndexes.empty()) {
931 assert(SymtabShndxSectionIndex == 0);
934 assert(SymtabShndxSectionIndex != 0);
936 SecStart = getStream().tell();
937 const MCSectionELF *SymtabShndxSection =
938 SectionTable[SymtabShndxSectionIndex - 1];
939 for (uint32_t Index : ShndxIndexes)
941 SecEnd = getStream().tell();
942 SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
946 ELFObjectWriter::createRelocationSection(MCContext &Ctx,
947 const MCSectionELF &Sec) {
948 if (Relocations[&Sec].empty())
951 const StringRef SectionName = Sec.getSectionName();
952 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
953 RelaSectionName += SectionName;
956 if (hasRelocationAddend())
957 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
959 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
962 if (Sec.getFlags() & ELF::SHF_GROUP)
963 Flags = ELF::SHF_GROUP;
965 MCSectionELF *RelaSection = Ctx.createELFRelSection(
966 RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
967 Flags, EntrySize, Sec.getGroup(), &Sec);
968 RelaSection->setAlignment(is64Bit() ? 8 : 4);
972 // Include the debug info compression header:
973 // "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
974 // useful for consumers to preallocate a buffer to decompress into.
976 prependCompressionHeader(uint64_t Size,
977 SmallVectorImpl<char> &CompressedContents) {
978 const StringRef Magic = "ZLIB";
979 if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
981 if (sys::IsLittleEndianHost)
982 sys::swapByteOrder(Size);
983 CompressedContents.insert(CompressedContents.begin(),
984 Magic.size() + sizeof(Size), 0);
985 std::copy(Magic.begin(), Magic.end(), CompressedContents.begin());
986 std::copy(reinterpret_cast<char *>(&Size),
987 reinterpret_cast<char *>(&Size + 1),
988 CompressedContents.begin() + Magic.size());
992 void ELFObjectWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
993 const MCAsmLayout &Layout) {
994 MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
995 StringRef SectionName = Section.getSectionName();
997 // Compressing debug_frame requires handling alignment fragments which is
998 // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
999 // for writing to arbitrary buffers) for little benefit.
1000 if (!Asm.getContext().getAsmInfo()->compressDebugSections() ||
1001 !SectionName.startswith(".debug_") || SectionName == ".debug_frame") {
1002 Asm.writeSectionData(&Section, Layout);
1006 SmallVector<char, 128> UncompressedData;
1007 raw_svector_ostream VecOS(UncompressedData);
1008 raw_pwrite_stream &OldStream = getStream();
1010 Asm.writeSectionData(&Section, Layout);
1011 setStream(OldStream);
1013 SmallVector<char, 128> CompressedContents;
1014 zlib::Status Success = zlib::compress(
1015 StringRef(UncompressedData.data(), UncompressedData.size()),
1016 CompressedContents);
1017 if (Success != zlib::StatusOK) {
1018 getStream() << UncompressedData;
1022 if (!prependCompressionHeader(UncompressedData.size(), CompressedContents)) {
1023 getStream() << UncompressedData;
1026 Asm.getContext().renameELFSection(&Section,
1027 (".z" + SectionName.drop_front(1)).str());
1028 getStream() << CompressedContents;
1031 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1032 uint64_t Flags, uint64_t Address,
1033 uint64_t Offset, uint64_t Size,
1034 uint32_t Link, uint32_t Info,
1036 uint64_t EntrySize) {
1037 write32(Name); // sh_name: index into string table
1038 write32(Type); // sh_type
1039 WriteWord(Flags); // sh_flags
1040 WriteWord(Address); // sh_addr
1041 WriteWord(Offset); // sh_offset
1042 WriteWord(Size); // sh_size
1043 write32(Link); // sh_link
1044 write32(Info); // sh_info
1045 WriteWord(Alignment); // sh_addralign
1046 WriteWord(EntrySize); // sh_entsize
1049 void ELFObjectWriter::writeRelocations(const MCAssembler &Asm,
1050 const MCSectionELF &Sec) {
1051 std::vector<ELFRelocationEntry> &Relocs = Relocations[&Sec];
1053 // Sort the relocation entries. Most targets just sort by Offset, but some
1054 // (e.g., MIPS) have additional constraints.
1055 TargetObjectWriter->sortRelocs(Asm, Relocs);
1057 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1058 const ELFRelocationEntry &Entry = Relocs[e - i - 1];
1059 unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
1062 write(Entry.Offset);
1063 if (TargetObjectWriter->isN64()) {
1064 write(uint32_t(Index));
1066 write(TargetObjectWriter->getRSsym(Entry.Type));
1067 write(TargetObjectWriter->getRType3(Entry.Type));
1068 write(TargetObjectWriter->getRType2(Entry.Type));
1069 write(TargetObjectWriter->getRType(Entry.Type));
1071 struct ELF::Elf64_Rela ERE64;
1072 ERE64.setSymbolAndType(Index, Entry.Type);
1073 write(ERE64.r_info);
1075 if (hasRelocationAddend())
1076 write(Entry.Addend);
1078 write(uint32_t(Entry.Offset));
1080 struct ELF::Elf32_Rela ERE32;
1081 ERE32.setSymbolAndType(Index, Entry.Type);
1082 write(ERE32.r_info);
1084 if (hasRelocationAddend())
1085 write(uint32_t(Entry.Addend));
1090 const MCSectionELF *ELFObjectWriter::createStringTable(MCContext &Ctx) {
1091 const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1];
1092 getStream() << StrTabBuilder.data();
1093 return StrtabSection;
1096 void ELFObjectWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
1097 uint32_t GroupSymbolIndex, uint64_t Offset,
1098 uint64_t Size, const MCSectionELF &Section) {
1099 uint64_t sh_link = 0;
1100 uint64_t sh_info = 0;
1102 switch(Section.getType()) {
1107 case ELF::SHT_DYNAMIC:
1108 llvm_unreachable("SHT_DYNAMIC in a relocatable object");
1111 case ELF::SHT_RELA: {
1112 sh_link = SymbolTableIndex;
1113 assert(sh_link && ".symtab not found");
1114 const MCSectionELF *InfoSection = Section.getAssociatedSection();
1115 sh_info = SectionIndexMap.lookup(InfoSection);
1119 case ELF::SHT_SYMTAB:
1120 case ELF::SHT_DYNSYM:
1121 sh_link = StringTableIndex;
1122 sh_info = LastLocalSymbolIndex;
1125 case ELF::SHT_SYMTAB_SHNDX:
1126 sh_link = SymbolTableIndex;
1129 case ELF::SHT_GROUP:
1130 sh_link = SymbolTableIndex;
1131 sh_info = GroupSymbolIndex;
1135 if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1136 Section.getType() == ELF::SHT_ARM_EXIDX)
1137 sh_link = SectionIndexMap.lookup(Section.getAssociatedSection());
1139 WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()),
1140 Section.getType(), Section.getFlags(), 0, Offset, Size,
1141 sh_link, sh_info, Section.getAlignment(),
1142 Section.getEntrySize());
1145 void ELFObjectWriter::writeSectionHeader(
1146 const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
1147 const SectionOffsetsTy &SectionOffsets) {
1148 const unsigned NumSections = SectionTable.size();
1150 // Null section first.
1151 uint64_t FirstSectionSize =
1152 (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
1153 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
1155 for (const MCSectionELF *Section : SectionTable) {
1156 uint32_t GroupSymbolIndex;
1157 unsigned Type = Section->getType();
1158 if (Type != ELF::SHT_GROUP)
1159 GroupSymbolIndex = 0;
1161 GroupSymbolIndex = Section->getGroup()->getIndex();
1163 const std::pair<uint64_t, uint64_t> &Offsets =
1164 SectionOffsets.find(Section)->second;
1166 if (Type == ELF::SHT_NOBITS)
1167 Size = Layout.getSectionAddressSize(Section);
1169 Size = Offsets.second - Offsets.first;
1171 writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
1176 void ELFObjectWriter::writeObject(MCAssembler &Asm,
1177 const MCAsmLayout &Layout) {
1178 MCContext &Ctx = Asm.getContext();
1179 MCSectionELF *StrtabSection =
1180 Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
1181 StringTableIndex = addToSectionTable(StrtabSection);
1183 RevGroupMapTy RevGroupMap;
1184 SectionIndexMapTy SectionIndexMap;
1186 std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
1188 // Write out the ELF header ...
1191 // ... then the sections ...
1192 SectionOffsetsTy SectionOffsets;
1193 std::vector<MCSectionELF *> Groups;
1194 std::vector<MCSectionELF *> Relocations;
1195 for (MCSection &Sec : Asm) {
1196 MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
1198 align(Section.getAlignment());
1200 // Remember the offset into the file for this section.
1201 uint64_t SecStart = getStream().tell();
1203 const MCSymbolELF *SignatureSymbol = Section.getGroup();
1204 writeSectionData(Asm, Section, Layout);
1206 uint64_t SecEnd = getStream().tell();
1207 SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
1209 MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
1211 if (SignatureSymbol) {
1212 Asm.registerSymbol(*SignatureSymbol);
1213 unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
1215 MCSectionELF *Group = Ctx.createELFGroupSection(SignatureSymbol);
1216 GroupIdx = addToSectionTable(Group);
1217 Group->setAlignment(4);
1218 Groups.push_back(Group);
1220 std::vector<const MCSectionELF *> &Members =
1221 GroupMembers[SignatureSymbol];
1222 Members.push_back(&Section);
1224 Members.push_back(RelSection);
1227 SectionIndexMap[&Section] = addToSectionTable(&Section);
1229 SectionIndexMap[RelSection] = addToSectionTable(RelSection);
1230 Relocations.push_back(RelSection);
1234 for (MCSectionELF *Group : Groups) {
1235 align(Group->getAlignment());
1237 // Remember the offset into the file for this section.
1238 uint64_t SecStart = getStream().tell();
1240 const MCSymbol *SignatureSymbol = Group->getGroup();
1241 assert(SignatureSymbol);
1242 write(uint32_t(ELF::GRP_COMDAT));
1243 for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
1244 uint32_t SecIndex = SectionIndexMap.lookup(Member);
1248 uint64_t SecEnd = getStream().tell();
1249 SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
1252 // Compute symbol table information.
1253 computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap, SectionOffsets);
1255 for (MCSectionELF *RelSection : Relocations) {
1256 align(RelSection->getAlignment());
1258 // Remember the offset into the file for this section.
1259 uint64_t SecStart = getStream().tell();
1261 writeRelocations(Asm, *RelSection->getAssociatedSection());
1263 uint64_t SecEnd = getStream().tell();
1264 SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
1268 uint64_t SecStart = getStream().tell();
1269 const MCSectionELF *Sec = createStringTable(Ctx);
1270 uint64_t SecEnd = getStream().tell();
1271 SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);
1274 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1275 align(NaturalAlignment);
1277 const unsigned SectionHeaderOffset = getStream().tell();
1279 // ... then the section header table ...
1280 writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
1282 uint16_t NumSections = (SectionTable.size() + 1 >= ELF::SHN_LORESERVE)
1283 ? (uint16_t)ELF::SHN_UNDEF
1284 : SectionTable.size() + 1;
1285 if (sys::IsLittleEndianHost != IsLittleEndian)
1286 sys::swapByteOrder(NumSections);
1287 unsigned NumSectionsOffset;
1290 uint64_t Val = SectionHeaderOffset;
1291 if (sys::IsLittleEndianHost != IsLittleEndian)
1292 sys::swapByteOrder(Val);
1293 getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1294 offsetof(ELF::Elf64_Ehdr, e_shoff));
1295 NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
1297 uint32_t Val = SectionHeaderOffset;
1298 if (sys::IsLittleEndianHost != IsLittleEndian)
1299 sys::swapByteOrder(Val);
1300 getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1301 offsetof(ELF::Elf32_Ehdr, e_shoff));
1302 NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
1304 getStream().pwrite(reinterpret_cast<char *>(&NumSections),
1305 sizeof(NumSections), NumSectionsOffset);
1308 bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1309 const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
1310 bool InSet, bool IsPCRel) const {
1311 const auto &SymA = cast<MCSymbolELF>(SA);
1317 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
1321 bool ELFObjectWriter::isWeak(const MCSymbol &S) const {
1322 const auto &Sym = cast<MCSymbolELF>(S);
1326 // It is invalid to replace a reference to a global in a comdat
1327 // with a reference to a local since out of comdat references
1328 // to a local are forbidden.
1329 // We could try to return false for more cases, like the reference
1330 // being in the same comdat or Sym being an alias to another global,
1331 // but it is not clear if it is worth the effort.
1332 if (Sym.getBinding() != ELF::STB_GLOBAL)
1335 if (!Sym.isInSection())
1338 const auto &Sec = cast<MCSectionELF>(Sym.getSection());
1339 return Sec.getGroup();
1342 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1343 raw_pwrite_stream &OS,
1344 bool IsLittleEndian) {
1345 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);