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"
42 #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 uint64_t SymbolValue(const MCSymbol &Sym, const MCAsmLayout &Layout);
74 static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
75 bool Used, bool Renamed);
77 /// Helper struct for containing some precomputed information on symbols.
78 struct ELFSymbolData {
79 const MCSymbolELF *Symbol;
80 uint32_t SectionIndex;
83 // Support lexicographic sorting.
84 bool operator<(const ELFSymbolData &RHS) const {
85 unsigned LHSType = Symbol->getType();
86 unsigned RHSType = RHS.Symbol->getType();
87 if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION)
89 if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
91 if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
92 return SectionIndex < RHS.SectionIndex;
93 return Name < RHS.Name;
97 /// The target specific ELF writer instance.
98 std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
100 DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
102 llvm::DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>>
106 /// @name Symbol Table Data
109 BumpPtrAllocator Alloc;
110 StringSaver VersionSymSaver{Alloc};
111 StringTableBuilder StrTabBuilder{StringTableBuilder::ELF};
115 // This holds the symbol table index of the last local symbol.
116 unsigned LastLocalSymbolIndex;
117 // This holds the .strtab section index.
118 unsigned StringTableIndex;
119 // This holds the .symtab section index.
120 unsigned SymbolTableIndex;
122 // Sections in the order they are to be output in the section table.
123 std::vector<const MCSectionELF *> SectionTable;
124 unsigned addToSectionTable(const MCSectionELF *Sec);
126 // TargetObjectWriter wrappers.
127 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
128 bool hasRelocationAddend() const {
129 return TargetObjectWriter->hasRelocationAddend();
131 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
132 const MCFixup &Fixup, bool IsPCRel) const {
133 return TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
136 void align(unsigned Alignment);
138 bool maybeWriteCompression(uint64_t Size,
139 SmallVectorImpl<char> &CompressedContents,
140 bool ZLibStyle, unsigned Alignment);
143 ELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_pwrite_stream &OS,
145 : MCObjectWriter(OS, IsLittleEndian), TargetObjectWriter(MOTW) {}
147 void reset() override {
150 StrTabBuilder.clear();
151 SectionTable.clear();
152 MCObjectWriter::reset();
155 ~ELFObjectWriter() override;
157 void WriteWord(uint64_t W) {
164 template <typename T> void write(T Val) {
166 support::endian::Writer<support::little>(getStream()).write(Val);
168 support::endian::Writer<support::big>(getStream()).write(Val);
171 void writeHeader(const MCAssembler &Asm);
173 void writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
174 ELFSymbolData &MSD, const MCAsmLayout &Layout);
176 // Start and end offset of each section
177 typedef std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>>
180 bool shouldRelocateWithSymbol(const MCAssembler &Asm,
181 const MCSymbolRefExpr *RefA,
182 const MCSymbol *Sym, uint64_t C,
183 unsigned Type) const;
185 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
186 const MCFragment *Fragment, const MCFixup &Fixup,
187 MCValue Target, bool &IsPCRel,
188 uint64_t &FixedValue) override;
190 // Map from a signature symbol to the group section index
191 typedef DenseMap<const MCSymbol *, unsigned> RevGroupMapTy;
193 /// Compute the symbol table data
195 /// \param Asm - The assembler.
196 /// \param SectionIndexMap - Maps a section to its index.
197 /// \param RevGroupMap - Maps a signature symbol to the group section.
198 void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
199 const SectionIndexMapTy &SectionIndexMap,
200 const RevGroupMapTy &RevGroupMap,
201 SectionOffsetsTy &SectionOffsets);
203 MCSectionELF *createRelocationSection(MCContext &Ctx,
204 const MCSectionELF &Sec);
206 const MCSectionELF *createStringTable(MCContext &Ctx);
208 void executePostLayoutBinding(MCAssembler &Asm,
209 const MCAsmLayout &Layout) override;
211 void writeSectionHeader(const MCAsmLayout &Layout,
212 const SectionIndexMapTy &SectionIndexMap,
213 const SectionOffsetsTy &SectionOffsets);
215 void writeSectionData(const MCAssembler &Asm, MCSection &Sec,
216 const MCAsmLayout &Layout);
218 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
219 uint64_t Address, uint64_t Offset, uint64_t Size,
220 uint32_t Link, uint32_t Info, uint64_t Alignment,
223 void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
225 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
226 const MCSymbol &SymA,
227 const MCFragment &FB, bool InSet,
228 bool IsPCRel) const override;
230 bool isWeak(const MCSymbol &Sym) const override;
232 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
233 void writeSection(const SectionIndexMapTy &SectionIndexMap,
234 uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size,
235 const MCSectionELF &Section);
237 } // end anonymous namespace
239 void ELFObjectWriter::align(unsigned Alignment) {
240 uint64_t Padding = OffsetToAlignment(getStream().tell(), Alignment);
244 unsigned ELFObjectWriter::addToSectionTable(const MCSectionELF *Sec) {
245 SectionTable.push_back(Sec);
246 StrTabBuilder.add(Sec->getSectionName());
247 return SectionTable.size();
250 void SymbolTableWriter::createSymtabShndx() {
251 if (!ShndxIndexes.empty())
254 ShndxIndexes.resize(NumWritten);
257 template <typename T> void SymbolTableWriter::write(T Value) {
258 EWriter.write(Value);
261 SymbolTableWriter::SymbolTableWriter(ELFObjectWriter &EWriter, bool Is64Bit)
262 : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {}
264 void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
265 uint64_t size, uint8_t other,
266 uint32_t shndx, bool Reserved) {
267 bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
272 if (!ShndxIndexes.empty()) {
274 ShndxIndexes.push_back(shndx);
276 ShndxIndexes.push_back(0);
279 uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
282 write(name); // st_name
283 write(info); // st_info
284 write(other); // st_other
285 write(Index); // st_shndx
286 write(value); // st_value
287 write(size); // st_size
289 write(name); // st_name
290 write(uint32_t(value)); // st_value
291 write(uint32_t(size)); // st_size
292 write(info); // st_info
293 write(other); // st_other
294 write(Index); // st_shndx
300 ELFObjectWriter::~ELFObjectWriter()
303 // Emit the ELF header.
304 void ELFObjectWriter::writeHeader(const MCAssembler &Asm) {
310 // emitWord method behaves differently for ELF32 and ELF64, writing
311 // 4 bytes in the former and 8 in the latter.
313 writeBytes(ELF::ElfMagic); // e_ident[EI_MAG0] to e_ident[EI_MAG3]
315 write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
318 write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
320 write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
322 write8(TargetObjectWriter->getOSABI());
323 write8(0); // e_ident[EI_ABIVERSION]
325 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
327 write16(ELF::ET_REL); // e_type
329 write16(TargetObjectWriter->getEMachine()); // e_machine = target
331 write32(ELF::EV_CURRENT); // e_version
332 WriteWord(0); // e_entry, no entry point in .o file
333 WriteWord(0); // e_phoff, no program header for .o
334 WriteWord(0); // e_shoff = sec hdr table off in bytes
336 // e_flags = whatever the target wants
337 write32(Asm.getELFHeaderEFlags());
339 // e_ehsize = ELF header size
340 write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
342 write16(0); // e_phentsize = prog header entry size
343 write16(0); // e_phnum = # prog header entries = 0
345 // e_shentsize = Section header entry size
346 write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
348 // e_shnum = # of section header ents
351 // e_shstrndx = Section # of '.shstrtab'
352 assert(StringTableIndex < ELF::SHN_LORESERVE);
353 write16(StringTableIndex);
356 uint64_t ELFObjectWriter::SymbolValue(const MCSymbol &Sym,
357 const MCAsmLayout &Layout) {
358 if (Sym.isCommon() && Sym.isExternal())
359 return Sym.getCommonAlignment();
362 if (!Layout.getSymbolOffset(Sym, Res))
365 if (Layout.getAssembler().isThumbFunc(&Sym))
371 void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
372 const MCAsmLayout &Layout) {
373 // Section symbols are used as definitions for undefined symbols with matching
374 // names. If there are multiple sections with the same name, the first one is
376 for (const MCSection &Sec : Asm) {
377 const MCSymbol *Begin = Sec.getBeginSymbol();
381 const MCSymbol *Alias = Asm.getContext().lookupSymbol(Begin->getName());
382 if (!Alias || !Alias->isUndefined())
386 std::make_pair(cast<MCSymbolELF>(Alias), cast<MCSymbolELF>(Begin)));
389 // The presence of symbol versions causes undefined symbols and
390 // versions declared with @@@ to be renamed.
391 for (const MCSymbol &A : Asm.symbols()) {
392 const auto &Alias = cast<MCSymbolELF>(A);
394 if (!Alias.isVariable())
396 auto *Ref = dyn_cast<MCSymbolRefExpr>(Alias.getVariableValue());
399 const auto &Symbol = cast<MCSymbolELF>(Ref->getSymbol());
401 StringRef AliasName = Alias.getName();
402 size_t Pos = AliasName.find('@');
403 if (Pos == StringRef::npos)
406 // Aliases defined with .symvar copy the binding from the symbol they alias.
407 // This is the first place we are able to copy this information.
408 Alias.setExternal(Symbol.isExternal());
409 Alias.setBinding(Symbol.getBinding());
411 StringRef Rest = AliasName.substr(Pos);
412 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
415 // FIXME: produce a better error message.
416 if (Symbol.isUndefined() && Rest.startswith("@@") &&
417 !Rest.startswith("@@@"))
418 report_fatal_error("A @@ version cannot be undefined");
420 Renames.insert(std::make_pair(&Symbol, &Alias));
424 static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
425 uint8_t Type = newType;
427 // Propagation rules:
428 // IFUNC > FUNC > OBJECT > NOTYPE
429 // TLS_OBJECT > OBJECT > NOTYPE
431 // dont let the new type degrade the old type
435 case ELF::STT_GNU_IFUNC:
436 if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
437 Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
438 Type = ELF::STT_GNU_IFUNC;
441 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
442 Type == ELF::STT_TLS)
443 Type = ELF::STT_FUNC;
445 case ELF::STT_OBJECT:
446 if (Type == ELF::STT_NOTYPE)
447 Type = ELF::STT_OBJECT;
450 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
451 Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
459 void ELFObjectWriter::writeSymbol(SymbolTableWriter &Writer,
460 uint32_t StringIndex, ELFSymbolData &MSD,
461 const MCAsmLayout &Layout) {
462 const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol);
463 const MCSymbolELF *Base =
464 cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol));
466 // This has to be in sync with when computeSymbolTable uses SHN_ABS or
468 bool IsReserved = !Base || Symbol.isCommon();
470 // Binding and Type share the same byte as upper and lower nibbles
471 uint8_t Binding = Symbol.getBinding();
472 uint8_t Type = Symbol.getType();
474 Type = mergeTypeForSet(Type, Base->getType());
476 uint8_t Info = (Binding << 4) | Type;
478 // Other and Visibility share the same byte with Visibility using the lower
480 uint8_t Visibility = Symbol.getVisibility();
481 uint8_t Other = Symbol.getOther() | Visibility;
483 uint64_t Value = SymbolValue(*MSD.Symbol, Layout);
486 const MCExpr *ESize = MSD.Symbol->getSize();
488 ESize = Base->getSize();
492 if (!ESize->evaluateKnownAbsolute(Res, Layout))
493 report_fatal_error("Size expression must be absolute.");
497 // Write out the symbol table entry
498 Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex,
502 // It is always valid to create a relocation with a symbol. It is preferable
503 // to use a relocation with a section if that is possible. Using the section
504 // allows us to omit some local symbols from the symbol table.
505 bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
506 const MCSymbolRefExpr *RefA,
507 const MCSymbol *S, uint64_t C,
508 unsigned Type) const {
509 const auto *Sym = cast_or_null<MCSymbolELF>(S);
510 // A PCRel relocation to an absolute value has no symbol (or section). We
511 // represent that with a relocation to a null section.
515 MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
519 // The .odp creation emits a relocation against the symbol ".TOC." which
520 // create a R_PPC64_TOC relocation. However the relocation symbol name
521 // in final object creation should be NULL, since the symbol does not
522 // really exist, it is just the reference to TOC base for the current
523 // object file. Since the symbol is undefined, returning false results
524 // in a relocation with a null section which is the desired result.
525 case MCSymbolRefExpr::VK_PPC_TOCBASE:
528 // These VariantKind cause the relocation to refer to something other than
529 // the symbol itself, like a linker generated table. Since the address of
530 // symbol is not relevant, we cannot replace the symbol with the
531 // section and patch the difference in the addend.
532 case MCSymbolRefExpr::VK_GOT:
533 case MCSymbolRefExpr::VK_PLT:
534 case MCSymbolRefExpr::VK_GOTPCREL:
535 case MCSymbolRefExpr::VK_PPC_GOT_LO:
536 case MCSymbolRefExpr::VK_PPC_GOT_HI:
537 case MCSymbolRefExpr::VK_PPC_GOT_HA:
541 // An undefined symbol is not in any section, so the relocation has to point
542 // to the symbol itself.
543 assert(Sym && "Expected a symbol");
544 if (Sym->isUndefined())
547 unsigned Binding = Sym->getBinding();
550 llvm_unreachable("Invalid Binding");
554 // If the symbol is weak, it might be overridden by a symbol in another
555 // file. The relocation has to point to the symbol so that the linker
558 case ELF::STB_GLOBAL:
559 // Global ELF symbols can be preempted by the dynamic linker. The relocation
560 // has to point to the symbol for a reason analogous to the STB_WEAK case.
564 // If a relocation points to a mergeable section, we have to be careful.
565 // If the offset is zero, a relocation with the section will encode the
566 // same information. With a non-zero offset, the situation is different.
567 // For example, a relocation can point 42 bytes past the end of a string.
568 // If we change such a relocation to use the section, the linker would think
569 // that it pointed to another string and subtracting 42 at runtime will
570 // produce the wrong value.
571 if (Sym->isInSection()) {
572 auto &Sec = cast<MCSectionELF>(Sym->getSection());
573 unsigned Flags = Sec.getFlags();
574 if (Flags & ELF::SHF_MERGE) {
578 // It looks like gold has a bug (http://sourceware.org/PR16794) and can
579 // only handle section relocations to mergeable sections if using RELA.
580 if (!hasRelocationAddend())
584 // Most TLS relocations use a got, so they need the symbol. Even those that
585 // are just an offset (@tpoff), require a symbol in gold versions before
586 // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
587 // http://sourceware.org/PR16773.
588 if (Flags & ELF::SHF_TLS)
592 // If the symbol is a thumb function the final relocation must set the lowest
593 // bit. With a symbol that is done by just having the symbol have that bit
594 // set, so we would lose the bit if we relocated with the section.
595 // FIXME: We could use the section but add the bit to the relocation value.
596 if (Asm.isThumbFunc(Sym))
599 if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type))
604 // True if the assembler knows nothing about the final value of the symbol.
605 // This doesn't cover the comdat issues, since in those cases the assembler
606 // can at least know that all symbols in the section will move together.
607 static bool isWeak(const MCSymbolELF &Sym) {
608 if (Sym.getType() == ELF::STT_GNU_IFUNC)
611 switch (Sym.getBinding()) {
613 llvm_unreachable("Unknown binding");
616 case ELF::STB_GLOBAL:
619 case ELF::STB_GNU_UNIQUE:
624 void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
625 const MCAsmLayout &Layout,
626 const MCFragment *Fragment,
627 const MCFixup &Fixup, MCValue Target,
628 bool &IsPCRel, uint64_t &FixedValue) {
629 const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
630 uint64_t C = Target.getConstant();
631 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
632 MCContext &Ctx = Asm.getContext();
634 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
635 assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
636 "Should not have constructed this");
638 // Let A, B and C being the components of Target and R be the location of
639 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
640 // If it is pcrel, we want to compute (A - B + C - R).
642 // In general, ELF has no relocations for -B. It can only represent (A + C)
643 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
644 // replace B to implement it: (A - R - K + C)
648 "No relocation available to represent this relative expression");
652 const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol());
654 if (SymB.isUndefined()) {
655 Ctx.reportError(Fixup.getLoc(),
656 Twine("symbol '") + SymB.getName() +
657 "' can not be undefined in a subtraction expression");
661 assert(!SymB.isAbsolute() && "Should have been folded");
662 const MCSection &SecB = SymB.getSection();
663 if (&SecB != &FixupSection) {
664 Ctx.reportError(Fixup.getLoc(),
665 "Cannot represent a difference across sections");
669 uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
670 uint64_t K = SymBOffset - FixupOffset;
675 // We either rejected the fixup or folded B into C at this point.
676 const MCSymbolRefExpr *RefA = Target.getSymA();
677 const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
679 bool ViaWeakRef = false;
680 if (SymA && SymA->isVariable()) {
681 const MCExpr *Expr = SymA->getVariableValue();
682 if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
683 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
684 SymA = cast<MCSymbolELF>(&Inner->getSymbol());
690 unsigned Type = getRelocType(Ctx, Target, Fixup, IsPCRel);
691 uint64_t OriginalC = C;
692 bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type);
693 if (!RelocateWithSymbol && SymA && !SymA->isUndefined())
694 C += Layout.getSymbolOffset(*SymA);
697 if (hasRelocationAddend()) {
704 if (!RelocateWithSymbol) {
705 const MCSection *SecA =
706 (SymA && !SymA->isUndefined()) ? &SymA->getSection() : nullptr;
707 auto *ELFSec = cast_or_null<MCSectionELF>(SecA);
708 const auto *SectionSymbol =
709 ELFSec ? cast<MCSymbolELF>(ELFSec->getBeginSymbol()) : nullptr;
711 SectionSymbol->setUsedInReloc();
712 ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA,
714 Relocations[&FixupSection].push_back(Rec);
718 const auto *RenamedSymA = SymA;
720 if (const MCSymbolELF *R = Renames.lookup(SymA))
724 RenamedSymA->setIsWeakrefUsedInReloc();
726 RenamedSymA->setUsedInReloc();
728 ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA,
730 Relocations[&FixupSection].push_back(Rec);
733 bool ELFObjectWriter::isInSymtab(const MCAsmLayout &Layout,
734 const MCSymbolELF &Symbol, bool Used,
736 if (Symbol.isVariable()) {
737 const MCExpr *Expr = Symbol.getVariableValue();
738 if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
739 if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
750 if (Symbol.isVariable() && Symbol.isUndefined()) {
751 // FIXME: this is here just to diagnose the case of a var = commmon_sym.
752 Layout.getBaseSymbol(Symbol);
756 if (Symbol.isUndefined() && !Symbol.isBindingSet())
759 if (Symbol.isTemporary())
762 if (Symbol.getType() == ELF::STT_SECTION)
768 void ELFObjectWriter::computeSymbolTable(
769 MCAssembler &Asm, const MCAsmLayout &Layout,
770 const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
771 SectionOffsetsTy &SectionOffsets) {
772 MCContext &Ctx = Asm.getContext();
773 SymbolTableWriter Writer(*this, is64Bit());
776 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
777 MCSectionELF *SymtabSection =
778 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, "");
779 SymtabSection->setAlignment(is64Bit() ? 8 : 4);
780 SymbolTableIndex = addToSectionTable(SymtabSection);
782 align(SymtabSection->getAlignment());
783 uint64_t SecStart = getStream().tell();
785 // The first entry is the undefined symbol entry.
786 Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
788 std::vector<ELFSymbolData> LocalSymbolData;
789 std::vector<ELFSymbolData> ExternalSymbolData;
791 // Add the data for the symbols.
792 bool HasLargeSectionIndex = false;
793 for (const MCSymbol &S : Asm.symbols()) {
794 const auto &Symbol = cast<MCSymbolELF>(S);
795 bool Used = Symbol.isUsedInReloc();
796 bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
797 bool isSignature = Symbol.isSignature();
799 if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
800 Renames.count(&Symbol)))
803 if (Symbol.isTemporary() && Symbol.isUndefined()) {
804 Ctx.reportError(SMLoc(), "Undefined temporary symbol");
809 MSD.Symbol = cast<MCSymbolELF>(&Symbol);
811 bool Local = Symbol.getBinding() == ELF::STB_LOCAL;
812 assert(Local || !Symbol.isTemporary());
814 if (Symbol.isAbsolute()) {
815 MSD.SectionIndex = ELF::SHN_ABS;
816 } else if (Symbol.isCommon()) {
818 MSD.SectionIndex = ELF::SHN_COMMON;
819 } else if (Symbol.isUndefined()) {
820 if (isSignature && !Used) {
821 MSD.SectionIndex = RevGroupMap.lookup(&Symbol);
822 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
823 HasLargeSectionIndex = true;
825 MSD.SectionIndex = ELF::SHN_UNDEF;
828 const MCSectionELF &Section =
829 static_cast<const MCSectionELF &>(Symbol.getSection());
830 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
831 assert(MSD.SectionIndex && "Invalid section index!");
832 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
833 HasLargeSectionIndex = true;
836 // The @@@ in symbol version is replaced with @ in undefined symbols and @@
839 // FIXME: All name handling should be done before we get to the writer,
840 // including dealing with GNU-style version suffixes. Fixing this isn't
843 // We thus have to be careful to not perform the symbol version replacement
846 // The ELF format is used on Windows by the MCJIT engine. Thus, on
847 // Windows, the ELFObjectWriter can encounter symbols mangled using the MS
848 // Visual Studio C++ name mangling scheme. Symbols mangled using the MSVC
849 // C++ name mangling can legally have "@@@" as a sub-string. In that case,
850 // the EFLObjectWriter should not interpret the "@@@" sub-string as
851 // specifying GNU-style symbol versioning. The ELFObjectWriter therefore
852 // checks for the MSVC C++ name mangling prefix which is either "?", "@?",
853 // "__imp_?" or "__imp_@?".
855 // It would have been interesting to perform the MS mangling prefix check
856 // only when the target triple is of the form *-pc-windows-elf. But, it
857 // seems that this information is not easily accessible from the
859 StringRef Name = Symbol.getName();
861 if (!Name.startswith("?") && !Name.startswith("@?") &&
862 !Name.startswith("__imp_?") && !Name.startswith("__imp_@?")) {
863 // This symbol isn't following the MSVC C++ name mangling convention. We
864 // can thus safely interpret the @@@ in symbol names as specifying symbol
866 size_t Pos = Name.find("@@@");
867 if (Pos != StringRef::npos) {
868 Buf += Name.substr(0, Pos);
869 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
870 Buf += Name.substr(Pos + Skip);
871 Name = VersionSymSaver.save(Buf.c_str());
875 // Sections have their own string table
876 if (Symbol.getType() != ELF::STT_SECTION) {
878 StrTabBuilder.add(Name);
882 LocalSymbolData.push_back(MSD);
884 ExternalSymbolData.push_back(MSD);
887 // This holds the .symtab_shndx section index.
888 unsigned SymtabShndxSectionIndex = 0;
890 if (HasLargeSectionIndex) {
891 MCSectionELF *SymtabShndxSection =
892 Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");
893 SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);
894 SymtabShndxSection->setAlignment(4);
897 ArrayRef<std::string> FileNames = Asm.getFileNames();
898 for (const std::string &Name : FileNames)
899 StrTabBuilder.add(Name);
901 StrTabBuilder.finalize();
903 for (const std::string &Name : FileNames)
904 Writer.writeSymbol(StrTabBuilder.getOffset(Name),
905 ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
908 // Symbols are required to be in lexicographic order.
909 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
910 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
912 // Set the symbol indices. Local symbols must come before all other
913 // symbols with non-local bindings.
914 unsigned Index = FileNames.size() + 1;
916 for (ELFSymbolData &MSD : LocalSymbolData) {
917 unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION
919 : StrTabBuilder.getOffset(MSD.Name);
920 MSD.Symbol->setIndex(Index++);
921 writeSymbol(Writer, StringIndex, MSD, Layout);
924 // Write the symbol table entries.
925 LastLocalSymbolIndex = Index;
927 for (ELFSymbolData &MSD : ExternalSymbolData) {
928 unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
929 MSD.Symbol->setIndex(Index++);
930 writeSymbol(Writer, StringIndex, MSD, Layout);
931 assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
934 uint64_t SecEnd = getStream().tell();
935 SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);
937 ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();
938 if (ShndxIndexes.empty()) {
939 assert(SymtabShndxSectionIndex == 0);
942 assert(SymtabShndxSectionIndex != 0);
944 SecStart = getStream().tell();
945 const MCSectionELF *SymtabShndxSection =
946 SectionTable[SymtabShndxSectionIndex - 1];
947 for (uint32_t Index : ShndxIndexes)
949 SecEnd = getStream().tell();
950 SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
954 ELFObjectWriter::createRelocationSection(MCContext &Ctx,
955 const MCSectionELF &Sec) {
956 if (Relocations[&Sec].empty())
959 const StringRef SectionName = Sec.getSectionName();
960 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
961 RelaSectionName += SectionName;
964 if (hasRelocationAddend())
965 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
967 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
970 if (Sec.getFlags() & ELF::SHF_GROUP)
971 Flags = ELF::SHF_GROUP;
973 MCSectionELF *RelaSection = Ctx.createELFRelSection(
974 RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
975 Flags, EntrySize, Sec.getGroup(), &Sec);
976 RelaSection->setAlignment(is64Bit() ? 8 : 4);
980 // Include the debug info compression header.
981 bool ELFObjectWriter::maybeWriteCompression(
982 uint64_t Size, SmallVectorImpl<char> &CompressedContents, bool ZLibStyle,
983 unsigned Alignment) {
986 is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
987 if (Size <= HdrSize + CompressedContents.size())
989 // Platform specific header is followed by compressed data.
991 // Write Elf64_Chdr header.
992 write(static_cast<ELF::Elf64_Word>(ELF::ELFCOMPRESS_ZLIB));
993 write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field.
994 write(static_cast<ELF::Elf64_Xword>(Size));
995 write(static_cast<ELF::Elf64_Xword>(Alignment));
997 // Write Elf32_Chdr header otherwise.
998 write(static_cast<ELF::Elf32_Word>(ELF::ELFCOMPRESS_ZLIB));
999 write(static_cast<ELF::Elf32_Word>(Size));
1000 write(static_cast<ELF::Elf32_Word>(Alignment));
1005 // "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
1006 // useful for consumers to preallocate a buffer to decompress into.
1007 const StringRef Magic = "ZLIB";
1008 if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
1010 write(ArrayRef<char>(Magic.begin(), Magic.size()));
1015 void ELFObjectWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
1016 const MCAsmLayout &Layout) {
1017 MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
1018 StringRef SectionName = Section.getSectionName();
1020 // Compressing debug_frame requires handling alignment fragments which is
1021 // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
1022 // for writing to arbitrary buffers) for little benefit.
1023 bool CompressionEnabled =
1024 Asm.getContext().getAsmInfo()->compressDebugSections() !=
1025 DebugCompressionType::DCT_None;
1026 if (!CompressionEnabled || !SectionName.startswith(".debug_") ||
1027 SectionName == ".debug_frame") {
1028 Asm.writeSectionData(&Section, Layout);
1032 SmallVector<char, 128> UncompressedData;
1033 raw_svector_ostream VecOS(UncompressedData);
1034 raw_pwrite_stream &OldStream = getStream();
1036 Asm.writeSectionData(&Section, Layout);
1037 setStream(OldStream);
1039 SmallVector<char, 128> CompressedContents;
1040 zlib::Status Success = zlib::compress(
1041 StringRef(UncompressedData.data(), UncompressedData.size()),
1042 CompressedContents);
1043 if (Success != zlib::StatusOK) {
1044 getStream() << UncompressedData;
1048 bool ZlibStyle = Asm.getContext().getAsmInfo()->compressDebugSections() ==
1049 DebugCompressionType::DCT_Zlib;
1050 if (!maybeWriteCompression(UncompressedData.size(), CompressedContents,
1051 ZlibStyle, Sec.getAlignment())) {
1052 getStream() << UncompressedData;
1057 // Set the compressed flag. That is zlib style.
1058 Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED);
1060 // Add "z" prefix to section name. This is zlib-gnu style.
1061 Asm.getContext().renameELFSection(&Section,
1062 (".z" + SectionName.drop_front(1)).str());
1063 getStream() << CompressedContents;
1066 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1067 uint64_t Flags, uint64_t Address,
1068 uint64_t Offset, uint64_t Size,
1069 uint32_t Link, uint32_t Info,
1071 uint64_t EntrySize) {
1072 write32(Name); // sh_name: index into string table
1073 write32(Type); // sh_type
1074 WriteWord(Flags); // sh_flags
1075 WriteWord(Address); // sh_addr
1076 WriteWord(Offset); // sh_offset
1077 WriteWord(Size); // sh_size
1078 write32(Link); // sh_link
1079 write32(Info); // sh_info
1080 WriteWord(Alignment); // sh_addralign
1081 WriteWord(EntrySize); // sh_entsize
1084 void ELFObjectWriter::writeRelocations(const MCAssembler &Asm,
1085 const MCSectionELF &Sec) {
1086 std::vector<ELFRelocationEntry> &Relocs = Relocations[&Sec];
1088 // We record relocations by pushing to the end of a vector. Reverse the vector
1089 // to get the relocations in the order they were created.
1090 // In most cases that is not important, but it can be for special sections
1091 // (.eh_frame) or specific relocations (TLS optimizations on SystemZ).
1092 std::reverse(Relocs.begin(), Relocs.end());
1094 // Sort the relocation entries. MIPS needs this.
1095 TargetObjectWriter->sortRelocs(Asm, Relocs);
1097 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1098 const ELFRelocationEntry &Entry = Relocs[e - i - 1];
1099 unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
1102 write(Entry.Offset);
1103 if (TargetObjectWriter->isN64()) {
1104 write(uint32_t(Index));
1106 write(TargetObjectWriter->getRSsym(Entry.Type));
1107 write(TargetObjectWriter->getRType3(Entry.Type));
1108 write(TargetObjectWriter->getRType2(Entry.Type));
1109 write(TargetObjectWriter->getRType(Entry.Type));
1111 struct ELF::Elf64_Rela ERE64;
1112 ERE64.setSymbolAndType(Index, Entry.Type);
1113 write(ERE64.r_info);
1115 if (hasRelocationAddend())
1116 write(Entry.Addend);
1118 write(uint32_t(Entry.Offset));
1120 struct ELF::Elf32_Rela ERE32;
1121 ERE32.setSymbolAndType(Index, Entry.Type);
1122 write(ERE32.r_info);
1124 if (hasRelocationAddend())
1125 write(uint32_t(Entry.Addend));
1130 const MCSectionELF *ELFObjectWriter::createStringTable(MCContext &Ctx) {
1131 const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1];
1132 StrTabBuilder.write(getStream());
1133 return StrtabSection;
1136 void ELFObjectWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
1137 uint32_t GroupSymbolIndex, uint64_t Offset,
1138 uint64_t Size, const MCSectionELF &Section) {
1139 uint64_t sh_link = 0;
1140 uint64_t sh_info = 0;
1142 switch(Section.getType()) {
1147 case ELF::SHT_DYNAMIC:
1148 llvm_unreachable("SHT_DYNAMIC in a relocatable object");
1151 case ELF::SHT_RELA: {
1152 sh_link = SymbolTableIndex;
1153 assert(sh_link && ".symtab not found");
1154 const MCSectionELF *InfoSection = Section.getAssociatedSection();
1155 sh_info = SectionIndexMap.lookup(InfoSection);
1159 case ELF::SHT_SYMTAB:
1160 case ELF::SHT_DYNSYM:
1161 sh_link = StringTableIndex;
1162 sh_info = LastLocalSymbolIndex;
1165 case ELF::SHT_SYMTAB_SHNDX:
1166 sh_link = SymbolTableIndex;
1169 case ELF::SHT_GROUP:
1170 sh_link = SymbolTableIndex;
1171 sh_info = GroupSymbolIndex;
1175 if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1176 Section.getType() == ELF::SHT_ARM_EXIDX)
1177 sh_link = SectionIndexMap.lookup(Section.getAssociatedSection());
1179 WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()),
1180 Section.getType(), Section.getFlags(), 0, Offset, Size,
1181 sh_link, sh_info, Section.getAlignment(),
1182 Section.getEntrySize());
1185 void ELFObjectWriter::writeSectionHeader(
1186 const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
1187 const SectionOffsetsTy &SectionOffsets) {
1188 const unsigned NumSections = SectionTable.size();
1190 // Null section first.
1191 uint64_t FirstSectionSize =
1192 (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
1193 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
1195 for (const MCSectionELF *Section : SectionTable) {
1196 uint32_t GroupSymbolIndex;
1197 unsigned Type = Section->getType();
1198 if (Type != ELF::SHT_GROUP)
1199 GroupSymbolIndex = 0;
1201 GroupSymbolIndex = Section->getGroup()->getIndex();
1203 const std::pair<uint64_t, uint64_t> &Offsets =
1204 SectionOffsets.find(Section)->second;
1206 if (Type == ELF::SHT_NOBITS)
1207 Size = Layout.getSectionAddressSize(Section);
1209 Size = Offsets.second - Offsets.first;
1211 writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
1216 void ELFObjectWriter::writeObject(MCAssembler &Asm,
1217 const MCAsmLayout &Layout) {
1218 MCContext &Ctx = Asm.getContext();
1219 MCSectionELF *StrtabSection =
1220 Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
1221 StringTableIndex = addToSectionTable(StrtabSection);
1223 RevGroupMapTy RevGroupMap;
1224 SectionIndexMapTy SectionIndexMap;
1226 std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
1228 // Write out the ELF header ...
1231 // ... then the sections ...
1232 SectionOffsetsTy SectionOffsets;
1233 std::vector<MCSectionELF *> Groups;
1234 std::vector<MCSectionELF *> Relocations;
1235 for (MCSection &Sec : Asm) {
1236 MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
1238 align(Section.getAlignment());
1240 // Remember the offset into the file for this section.
1241 uint64_t SecStart = getStream().tell();
1243 const MCSymbolELF *SignatureSymbol = Section.getGroup();
1244 writeSectionData(Asm, Section, Layout);
1246 uint64_t SecEnd = getStream().tell();
1247 SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
1249 MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
1251 if (SignatureSymbol) {
1252 Asm.registerSymbol(*SignatureSymbol);
1253 unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
1255 MCSectionELF *Group = Ctx.createELFGroupSection(SignatureSymbol);
1256 GroupIdx = addToSectionTable(Group);
1257 Group->setAlignment(4);
1258 Groups.push_back(Group);
1260 std::vector<const MCSectionELF *> &Members =
1261 GroupMembers[SignatureSymbol];
1262 Members.push_back(&Section);
1264 Members.push_back(RelSection);
1267 SectionIndexMap[&Section] = addToSectionTable(&Section);
1269 SectionIndexMap[RelSection] = addToSectionTable(RelSection);
1270 Relocations.push_back(RelSection);
1274 for (MCSectionELF *Group : Groups) {
1275 align(Group->getAlignment());
1277 // Remember the offset into the file for this section.
1278 uint64_t SecStart = getStream().tell();
1280 const MCSymbol *SignatureSymbol = Group->getGroup();
1281 assert(SignatureSymbol);
1282 write(uint32_t(ELF::GRP_COMDAT));
1283 for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
1284 uint32_t SecIndex = SectionIndexMap.lookup(Member);
1288 uint64_t SecEnd = getStream().tell();
1289 SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
1292 // Compute symbol table information.
1293 computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap, SectionOffsets);
1295 for (MCSectionELF *RelSection : Relocations) {
1296 align(RelSection->getAlignment());
1298 // Remember the offset into the file for this section.
1299 uint64_t SecStart = getStream().tell();
1301 writeRelocations(Asm, *RelSection->getAssociatedSection());
1303 uint64_t SecEnd = getStream().tell();
1304 SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
1308 uint64_t SecStart = getStream().tell();
1309 const MCSectionELF *Sec = createStringTable(Ctx);
1310 uint64_t SecEnd = getStream().tell();
1311 SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);
1314 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1315 align(NaturalAlignment);
1317 const uint64_t SectionHeaderOffset = getStream().tell();
1319 // ... then the section header table ...
1320 writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
1322 uint16_t NumSections = (SectionTable.size() + 1 >= ELF::SHN_LORESERVE)
1323 ? (uint16_t)ELF::SHN_UNDEF
1324 : SectionTable.size() + 1;
1325 if (sys::IsLittleEndianHost != IsLittleEndian)
1326 sys::swapByteOrder(NumSections);
1327 unsigned NumSectionsOffset;
1330 uint64_t Val = SectionHeaderOffset;
1331 if (sys::IsLittleEndianHost != IsLittleEndian)
1332 sys::swapByteOrder(Val);
1333 getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1334 offsetof(ELF::Elf64_Ehdr, e_shoff));
1335 NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
1337 uint32_t Val = SectionHeaderOffset;
1338 if (sys::IsLittleEndianHost != IsLittleEndian)
1339 sys::swapByteOrder(Val);
1340 getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1341 offsetof(ELF::Elf32_Ehdr, e_shoff));
1342 NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
1344 getStream().pwrite(reinterpret_cast<char *>(&NumSections),
1345 sizeof(NumSections), NumSectionsOffset);
1348 bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1349 const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
1350 bool InSet, bool IsPCRel) const {
1351 const auto &SymA = cast<MCSymbolELF>(SA);
1357 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
1361 bool ELFObjectWriter::isWeak(const MCSymbol &S) const {
1362 const auto &Sym = cast<MCSymbolELF>(S);
1366 // It is invalid to replace a reference to a global in a comdat
1367 // with a reference to a local since out of comdat references
1368 // to a local are forbidden.
1369 // We could try to return false for more cases, like the reference
1370 // being in the same comdat or Sym being an alias to another global,
1371 // but it is not clear if it is worth the effort.
1372 if (Sym.getBinding() != ELF::STB_GLOBAL)
1375 if (!Sym.isInSection())
1378 const auto &Sec = cast<MCSectionELF>(Sym.getSection());
1379 return Sec.getGroup();
1382 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1383 raw_pwrite_stream &OS,
1384 bool IsLittleEndian) {
1385 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);