1 //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements ELF object file writer information.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCAsmLayout.h"
24 #include "llvm/MC/MCAssembler.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCELFObjectWriter.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCFixup.h"
29 #include "llvm/MC/MCFixupKindInfo.h"
30 #include "llvm/MC/MCFragment.h"
31 #include "llvm/MC/MCObjectFileInfo.h"
32 #include "llvm/MC/MCObjectWriter.h"
33 #include "llvm/MC/MCSection.h"
34 #include "llvm/MC/MCSectionELF.h"
35 #include "llvm/MC/MCSymbol.h"
36 #include "llvm/MC/MCSymbolELF.h"
37 #include "llvm/MC/MCValue.h"
38 #include "llvm/MC/StringTableBuilder.h"
39 #include "llvm/Support/Alignment.h"
40 #include "llvm/Support/Allocator.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/Compression.h"
43 #include "llvm/Support/EndianStream.h"
44 #include "llvm/Support/Error.h"
45 #include "llvm/Support/ErrorHandling.h"
46 #include "llvm/Support/Host.h"
47 #include "llvm/Support/LEB128.h"
48 #include "llvm/Support/MathExtras.h"
49 #include "llvm/Support/SMLoc.h"
50 #include "llvm/Support/StringSaver.h"
51 #include "llvm/Support/SwapByteOrder.h"
52 #include "llvm/Support/raw_ostream.h"
66 #define DEBUG_TYPE "reloc-info"
70 using SectionIndexMapTy
= DenseMap
<const MCSectionELF
*, uint32_t>;
72 class ELFObjectWriter
;
75 bool isDwoSection(const MCSectionELF
&Sec
) {
76 return Sec
.getName().endswith(".dwo");
79 class SymbolTableWriter
{
83 // indexes we are going to write to .symtab_shndx.
84 std::vector
<uint32_t> ShndxIndexes
;
86 // The numbel of symbols written so far.
89 void createSymtabShndx();
91 template <typename T
> void write(T Value
);
94 SymbolTableWriter(ELFWriter
&EWriter
, bool Is64Bit
);
96 void writeSymbol(uint32_t name
, uint8_t info
, uint64_t value
, uint64_t size
,
97 uint8_t other
, uint32_t shndx
, bool Reserved
);
99 ArrayRef
<uint32_t> getShndxIndexes() const { return ShndxIndexes
; }
103 ELFObjectWriter
&OWriter
;
104 support::endian::Writer W
;
112 static uint64_t SymbolValue(const MCSymbol
&Sym
, const MCAsmLayout
&Layout
);
113 static bool isInSymtab(const MCAsmLayout
&Layout
, const MCSymbolELF
&Symbol
,
114 bool Used
, bool Renamed
);
116 /// Helper struct for containing some precomputed information on symbols.
117 struct ELFSymbolData
{
118 const MCSymbolELF
*Symbol
;
120 uint32_t SectionIndex
;
125 /// @name Symbol Table Data
128 StringTableBuilder StrTabBuilder
{StringTableBuilder::ELF
};
132 // This holds the symbol table index of the last local symbol.
133 unsigned LastLocalSymbolIndex
;
134 // This holds the .strtab section index.
135 unsigned StringTableIndex
;
136 // This holds the .symtab section index.
137 unsigned SymbolTableIndex
;
139 // Sections in the order they are to be output in the section table.
140 std::vector
<const MCSectionELF
*> SectionTable
;
141 unsigned addToSectionTable(const MCSectionELF
*Sec
);
143 // TargetObjectWriter wrappers.
144 bool is64Bit() const;
145 bool usesRela(const MCSectionELF
&Sec
) const;
147 uint64_t align(unsigned Alignment
);
149 bool maybeWriteCompression(uint64_t Size
,
150 SmallVectorImpl
<char> &CompressedContents
,
151 bool ZLibStyle
, unsigned Alignment
);
154 ELFWriter(ELFObjectWriter
&OWriter
, raw_pwrite_stream
&OS
,
155 bool IsLittleEndian
, DwoMode Mode
)
157 W(OS
, IsLittleEndian
? support::little
: support::big
), Mode(Mode
) {}
159 void WriteWord(uint64_t Word
) {
161 W
.write
<uint64_t>(Word
);
163 W
.write
<uint32_t>(Word
);
166 template <typename T
> void write(T Val
) {
170 void writeHeader(const MCAssembler
&Asm
);
172 void writeSymbol(SymbolTableWriter
&Writer
, uint32_t StringIndex
,
173 ELFSymbolData
&MSD
, const MCAsmLayout
&Layout
);
175 // Start and end offset of each section
176 using SectionOffsetsTy
=
177 std::map
<const MCSectionELF
*, std::pair
<uint64_t, uint64_t>>;
179 // Map from a signature symbol to the group section index
180 using RevGroupMapTy
= DenseMap
<const MCSymbol
*, unsigned>;
182 /// Compute the symbol table data
184 /// \param Asm - The assembler.
185 /// \param SectionIndexMap - Maps a section to its index.
186 /// \param RevGroupMap - Maps a signature symbol to the group section.
187 void computeSymbolTable(MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
188 const SectionIndexMapTy
&SectionIndexMap
,
189 const RevGroupMapTy
&RevGroupMap
,
190 SectionOffsetsTy
&SectionOffsets
);
192 void writeAddrsigSection();
194 MCSectionELF
*createRelocationSection(MCContext
&Ctx
,
195 const MCSectionELF
&Sec
);
197 void writeSectionHeader(const MCAsmLayout
&Layout
,
198 const SectionIndexMapTy
&SectionIndexMap
,
199 const SectionOffsetsTy
&SectionOffsets
);
201 void writeSectionData(const MCAssembler
&Asm
, MCSection
&Sec
,
202 const MCAsmLayout
&Layout
);
204 void WriteSecHdrEntry(uint32_t Name
, uint32_t Type
, uint64_t Flags
,
205 uint64_t Address
, uint64_t Offset
, uint64_t Size
,
206 uint32_t Link
, uint32_t Info
, uint64_t Alignment
,
209 void writeRelocations(const MCAssembler
&Asm
, const MCSectionELF
&Sec
);
211 uint64_t writeObject(MCAssembler
&Asm
, const MCAsmLayout
&Layout
);
212 void writeSection(const SectionIndexMapTy
&SectionIndexMap
,
213 uint32_t GroupSymbolIndex
, uint64_t Offset
, uint64_t Size
,
214 const MCSectionELF
&Section
);
217 class ELFObjectWriter
: public MCObjectWriter
{
218 /// The target specific ELF writer instance.
219 std::unique_ptr
<MCELFObjectTargetWriter
> TargetObjectWriter
;
221 DenseMap
<const MCSectionELF
*, std::vector
<ELFRelocationEntry
>> Relocations
;
223 DenseMap
<const MCSymbolELF
*, const MCSymbolELF
*> Renames
;
225 bool SeenGnuAbi
= false;
226 bool EmitAddrsigSection
= false;
227 std::vector
<const MCSymbol
*> AddrsigSyms
;
229 bool hasRelocationAddend() const;
231 bool shouldRelocateWithSymbol(const MCAssembler
&Asm
,
232 const MCSymbolRefExpr
*RefA
,
233 const MCSymbolELF
*Sym
, uint64_t C
,
234 unsigned Type
) const;
237 ELFObjectWriter(std::unique_ptr
<MCELFObjectTargetWriter
> MOTW
)
238 : TargetObjectWriter(std::move(MOTW
)) {}
240 void reset() override
{
244 MCObjectWriter::reset();
247 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler
&Asm
,
248 const MCSymbol
&SymA
,
249 const MCFragment
&FB
, bool InSet
,
250 bool IsPCRel
) const override
;
252 virtual bool checkRelocation(MCContext
&Ctx
, SMLoc Loc
,
253 const MCSectionELF
*From
,
254 const MCSectionELF
*To
) {
258 void recordRelocation(MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
259 const MCFragment
*Fragment
, const MCFixup
&Fixup
,
260 MCValue Target
, uint64_t &FixedValue
) override
;
262 void executePostLayoutBinding(MCAssembler
&Asm
,
263 const MCAsmLayout
&Layout
) override
;
265 void markGnuAbi() override
{ SeenGnuAbi
= true; }
266 bool seenGnuAbi() const { return SeenGnuAbi
; }
267 void emitAddrsigSection() override
{ EmitAddrsigSection
= true; }
268 void addAddrsigSymbol(const MCSymbol
*Sym
) override
{
269 AddrsigSyms
.push_back(Sym
);
272 friend struct ELFWriter
;
275 class ELFSingleObjectWriter
: public ELFObjectWriter
{
276 raw_pwrite_stream
&OS
;
280 ELFSingleObjectWriter(std::unique_ptr
<MCELFObjectTargetWriter
> MOTW
,
281 raw_pwrite_stream
&OS
, bool IsLittleEndian
)
282 : ELFObjectWriter(std::move(MOTW
)), OS(OS
),
283 IsLittleEndian(IsLittleEndian
) {}
285 uint64_t writeObject(MCAssembler
&Asm
, const MCAsmLayout
&Layout
) override
{
286 return ELFWriter(*this, OS
, IsLittleEndian
, ELFWriter::AllSections
)
287 .writeObject(Asm
, Layout
);
290 friend struct ELFWriter
;
293 class ELFDwoObjectWriter
: public ELFObjectWriter
{
294 raw_pwrite_stream
&OS
, &DwoOS
;
298 ELFDwoObjectWriter(std::unique_ptr
<MCELFObjectTargetWriter
> MOTW
,
299 raw_pwrite_stream
&OS
, raw_pwrite_stream
&DwoOS
,
301 : ELFObjectWriter(std::move(MOTW
)), OS(OS
), DwoOS(DwoOS
),
302 IsLittleEndian(IsLittleEndian
) {}
304 virtual bool checkRelocation(MCContext
&Ctx
, SMLoc Loc
,
305 const MCSectionELF
*From
,
306 const MCSectionELF
*To
) override
{
307 if (isDwoSection(*From
)) {
308 Ctx
.reportError(Loc
, "A dwo section may not contain relocations");
311 if (To
&& isDwoSection(*To
)) {
312 Ctx
.reportError(Loc
, "A relocation may not refer to a dwo section");
318 uint64_t writeObject(MCAssembler
&Asm
, const MCAsmLayout
&Layout
) override
{
319 uint64_t Size
= ELFWriter(*this, OS
, IsLittleEndian
, ELFWriter::NonDwoOnly
)
320 .writeObject(Asm
, Layout
);
321 Size
+= ELFWriter(*this, DwoOS
, IsLittleEndian
, ELFWriter::DwoOnly
)
322 .writeObject(Asm
, Layout
);
327 } // end anonymous namespace
329 uint64_t ELFWriter::align(unsigned Alignment
) {
330 uint64_t Offset
= W
.OS
.tell(), NewOffset
= alignTo(Offset
, Alignment
);
331 W
.OS
.write_zeros(NewOffset
- Offset
);
335 unsigned ELFWriter::addToSectionTable(const MCSectionELF
*Sec
) {
336 SectionTable
.push_back(Sec
);
337 StrTabBuilder
.add(Sec
->getName());
338 return SectionTable
.size();
341 void SymbolTableWriter::createSymtabShndx() {
342 if (!ShndxIndexes
.empty())
345 ShndxIndexes
.resize(NumWritten
);
348 template <typename T
> void SymbolTableWriter::write(T Value
) {
349 EWriter
.write(Value
);
352 SymbolTableWriter::SymbolTableWriter(ELFWriter
&EWriter
, bool Is64Bit
)
353 : EWriter(EWriter
), Is64Bit(Is64Bit
), NumWritten(0) {}
355 void SymbolTableWriter::writeSymbol(uint32_t name
, uint8_t info
, uint64_t value
,
356 uint64_t size
, uint8_t other
,
357 uint32_t shndx
, bool Reserved
) {
358 bool LargeIndex
= shndx
>= ELF::SHN_LORESERVE
&& !Reserved
;
363 if (!ShndxIndexes
.empty()) {
365 ShndxIndexes
.push_back(shndx
);
367 ShndxIndexes
.push_back(0);
370 uint16_t Index
= LargeIndex
? uint16_t(ELF::SHN_XINDEX
) : shndx
;
373 write(name
); // st_name
374 write(info
); // st_info
375 write(other
); // st_other
376 write(Index
); // st_shndx
377 write(value
); // st_value
378 write(size
); // st_size
380 write(name
); // st_name
381 write(uint32_t(value
)); // st_value
382 write(uint32_t(size
)); // st_size
383 write(info
); // st_info
384 write(other
); // st_other
385 write(Index
); // st_shndx
391 bool ELFWriter::is64Bit() const {
392 return OWriter
.TargetObjectWriter
->is64Bit();
395 bool ELFWriter::usesRela(const MCSectionELF
&Sec
) const {
396 return OWriter
.hasRelocationAddend() &&
397 Sec
.getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE
;
400 // Emit the ELF header.
401 void ELFWriter::writeHeader(const MCAssembler
&Asm
) {
407 // emitWord method behaves differently for ELF32 and ELF64, writing
408 // 4 bytes in the former and 8 in the latter.
410 W
.OS
<< ELF::ElfMagic
; // e_ident[EI_MAG0] to e_ident[EI_MAG3]
412 W
.OS
<< char(is64Bit() ? ELF::ELFCLASS64
: ELF::ELFCLASS32
); // e_ident[EI_CLASS]
415 W
.OS
<< char(W
.Endian
== support::little
? ELF::ELFDATA2LSB
418 W
.OS
<< char(ELF::EV_CURRENT
); // e_ident[EI_VERSION]
420 uint8_t OSABI
= OWriter
.TargetObjectWriter
->getOSABI();
421 W
.OS
<< char(OSABI
== ELF::ELFOSABI_NONE
&& OWriter
.seenGnuAbi()
422 ? int(ELF::ELFOSABI_GNU
)
424 // e_ident[EI_ABIVERSION]
425 W
.OS
<< char(OWriter
.TargetObjectWriter
->getABIVersion());
427 W
.OS
.write_zeros(ELF::EI_NIDENT
- ELF::EI_PAD
);
429 W
.write
<uint16_t>(ELF::ET_REL
); // e_type
431 W
.write
<uint16_t>(OWriter
.TargetObjectWriter
->getEMachine()); // e_machine = target
433 W
.write
<uint32_t>(ELF::EV_CURRENT
); // e_version
434 WriteWord(0); // e_entry, no entry point in .o file
435 WriteWord(0); // e_phoff, no program header for .o
436 WriteWord(0); // e_shoff = sec hdr table off in bytes
438 // e_flags = whatever the target wants
439 W
.write
<uint32_t>(Asm
.getELFHeaderEFlags());
441 // e_ehsize = ELF header size
442 W
.write
<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr
)
443 : sizeof(ELF::Elf32_Ehdr
));
445 W
.write
<uint16_t>(0); // e_phentsize = prog header entry size
446 W
.write
<uint16_t>(0); // e_phnum = # prog header entries = 0
448 // e_shentsize = Section header entry size
449 W
.write
<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr
)
450 : sizeof(ELF::Elf32_Shdr
));
452 // e_shnum = # of section header ents
453 W
.write
<uint16_t>(0);
455 // e_shstrndx = Section # of '.strtab'
456 assert(StringTableIndex
< ELF::SHN_LORESERVE
);
457 W
.write
<uint16_t>(StringTableIndex
);
460 uint64_t ELFWriter::SymbolValue(const MCSymbol
&Sym
,
461 const MCAsmLayout
&Layout
) {
463 return Sym
.getCommonAlignment();
466 if (!Layout
.getSymbolOffset(Sym
, Res
))
469 if (Layout
.getAssembler().isThumbFunc(&Sym
))
475 static uint8_t mergeTypeForSet(uint8_t origType
, uint8_t newType
) {
476 uint8_t Type
= newType
;
478 // Propagation rules:
479 // IFUNC > FUNC > OBJECT > NOTYPE
480 // TLS_OBJECT > OBJECT > NOTYPE
482 // dont let the new type degrade the old type
486 case ELF::STT_GNU_IFUNC
:
487 if (Type
== ELF::STT_FUNC
|| Type
== ELF::STT_OBJECT
||
488 Type
== ELF::STT_NOTYPE
|| Type
== ELF::STT_TLS
)
489 Type
= ELF::STT_GNU_IFUNC
;
492 if (Type
== ELF::STT_OBJECT
|| Type
== ELF::STT_NOTYPE
||
493 Type
== ELF::STT_TLS
)
494 Type
= ELF::STT_FUNC
;
496 case ELF::STT_OBJECT
:
497 if (Type
== ELF::STT_NOTYPE
)
498 Type
= ELF::STT_OBJECT
;
501 if (Type
== ELF::STT_OBJECT
|| Type
== ELF::STT_NOTYPE
||
502 Type
== ELF::STT_GNU_IFUNC
|| Type
== ELF::STT_FUNC
)
510 static bool isIFunc(const MCSymbolELF
*Symbol
) {
511 while (Symbol
->getType() != ELF::STT_GNU_IFUNC
) {
512 const MCSymbolRefExpr
*Value
;
513 if (!Symbol
->isVariable() ||
514 !(Value
= dyn_cast
<MCSymbolRefExpr
>(Symbol
->getVariableValue())) ||
515 Value
->getKind() != MCSymbolRefExpr::VK_None
||
516 mergeTypeForSet(Symbol
->getType(), ELF::STT_GNU_IFUNC
) != ELF::STT_GNU_IFUNC
)
518 Symbol
= &cast
<MCSymbolELF
>(Value
->getSymbol());
523 void ELFWriter::writeSymbol(SymbolTableWriter
&Writer
, uint32_t StringIndex
,
524 ELFSymbolData
&MSD
, const MCAsmLayout
&Layout
) {
525 const auto &Symbol
= cast
<MCSymbolELF
>(*MSD
.Symbol
);
526 const MCSymbolELF
*Base
=
527 cast_or_null
<MCSymbolELF
>(Layout
.getBaseSymbol(Symbol
));
529 // This has to be in sync with when computeSymbolTable uses SHN_ABS or
531 bool IsReserved
= !Base
|| Symbol
.isCommon();
533 // Binding and Type share the same byte as upper and lower nibbles
534 uint8_t Binding
= Symbol
.getBinding();
535 uint8_t Type
= Symbol
.getType();
536 if (isIFunc(&Symbol
))
537 Type
= ELF::STT_GNU_IFUNC
;
539 Type
= mergeTypeForSet(Type
, Base
->getType());
541 uint8_t Info
= (Binding
<< 4) | Type
;
543 // Other and Visibility share the same byte with Visibility using the lower
545 uint8_t Visibility
= Symbol
.getVisibility();
546 uint8_t Other
= Symbol
.getOther() | Visibility
;
548 uint64_t Value
= SymbolValue(*MSD
.Symbol
, Layout
);
551 const MCExpr
*ESize
= MSD
.Symbol
->getSize();
553 ESize
= Base
->getSize();
557 if (!ESize
->evaluateKnownAbsolute(Res
, Layout
))
558 report_fatal_error("Size expression must be absolute.");
562 // Write out the symbol table entry
563 Writer
.writeSymbol(StringIndex
, Info
, Value
, Size
, Other
, MSD
.SectionIndex
,
567 bool ELFWriter::isInSymtab(const MCAsmLayout
&Layout
, const MCSymbolELF
&Symbol
,
568 bool Used
, bool Renamed
) {
569 if (Symbol
.isVariable()) {
570 const MCExpr
*Expr
= Symbol
.getVariableValue();
571 // Target Expressions that are always inlined do not appear in the symtab
572 if (const auto *T
= dyn_cast
<MCTargetExpr
>(Expr
))
573 if (T
->inlineAssignedExpr())
575 if (const MCSymbolRefExpr
*Ref
= dyn_cast
<MCSymbolRefExpr
>(Expr
)) {
576 if (Ref
->getKind() == MCSymbolRefExpr::VK_WEAKREF
)
587 if (Symbol
.isVariable() && Symbol
.isUndefined()) {
588 // FIXME: this is here just to diagnose the case of a var = commmon_sym.
589 Layout
.getBaseSymbol(Symbol
);
593 if (Symbol
.isTemporary())
596 if (Symbol
.getType() == ELF::STT_SECTION
)
602 void ELFWriter::computeSymbolTable(
603 MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
604 const SectionIndexMapTy
&SectionIndexMap
, const RevGroupMapTy
&RevGroupMap
,
605 SectionOffsetsTy
&SectionOffsets
) {
606 MCContext
&Ctx
= Asm
.getContext();
607 SymbolTableWriter
Writer(*this, is64Bit());
610 unsigned EntrySize
= is64Bit() ? ELF::SYMENTRY_SIZE64
: ELF::SYMENTRY_SIZE32
;
611 MCSectionELF
*SymtabSection
=
612 Ctx
.getELFSection(".symtab", ELF::SHT_SYMTAB
, 0, EntrySize
);
613 SymtabSection
->setAlignment(is64Bit() ? Align(8) : Align(4));
614 SymbolTableIndex
= addToSectionTable(SymtabSection
);
616 uint64_t SecStart
= align(SymtabSection
->getAlignment());
618 // The first entry is the undefined symbol entry.
619 Writer
.writeSymbol(0, 0, 0, 0, 0, 0, false);
621 std::vector
<ELFSymbolData
> LocalSymbolData
;
622 std::vector
<ELFSymbolData
> ExternalSymbolData
;
623 MutableArrayRef
<std::pair
<std::string
, size_t>> FileNames
=
625 for (const std::pair
<std::string
, size_t> &F
: FileNames
)
626 StrTabBuilder
.add(F
.first
);
628 // Add the data for the symbols.
629 bool HasLargeSectionIndex
= false;
630 for (auto It
: llvm::enumerate(Asm
.symbols())) {
631 const auto &Symbol
= cast
<MCSymbolELF
>(It
.value());
632 bool Used
= Symbol
.isUsedInReloc();
633 bool WeakrefUsed
= Symbol
.isWeakrefUsedInReloc();
634 bool isSignature
= Symbol
.isSignature();
636 if (!isInSymtab(Layout
, Symbol
, Used
|| WeakrefUsed
|| isSignature
,
637 OWriter
.Renames
.count(&Symbol
)))
640 if (Symbol
.isTemporary() && Symbol
.isUndefined()) {
641 Ctx
.reportError(SMLoc(), "Undefined temporary symbol " + Symbol
.getName());
646 MSD
.Symbol
= cast
<MCSymbolELF
>(&Symbol
);
647 MSD
.Order
= It
.index();
649 bool Local
= Symbol
.getBinding() == ELF::STB_LOCAL
;
650 assert(Local
|| !Symbol
.isTemporary());
652 if (Symbol
.isAbsolute()) {
653 MSD
.SectionIndex
= ELF::SHN_ABS
;
654 } else if (Symbol
.isCommon()) {
655 if (Symbol
.isTargetCommon()) {
656 MSD
.SectionIndex
= Symbol
.getIndex();
659 MSD
.SectionIndex
= ELF::SHN_COMMON
;
661 } else if (Symbol
.isUndefined()) {
662 if (isSignature
&& !Used
) {
663 MSD
.SectionIndex
= RevGroupMap
.lookup(&Symbol
);
664 if (MSD
.SectionIndex
>= ELF::SHN_LORESERVE
)
665 HasLargeSectionIndex
= true;
667 MSD
.SectionIndex
= ELF::SHN_UNDEF
;
670 const MCSectionELF
&Section
=
671 static_cast<const MCSectionELF
&>(Symbol
.getSection());
673 // We may end up with a situation when section symbol is technically
674 // defined, but should not be. That happens because we explicitly
675 // pre-create few .debug_* sections to have accessors.
676 // And if these sections were not really defined in the code, but were
677 // referenced, we simply error out.
678 if (!Section
.isRegistered()) {
679 assert(static_cast<const MCSymbolELF
&>(Symbol
).getType() ==
681 Ctx
.reportError(SMLoc(),
682 "Undefined section reference: " + Symbol
.getName());
686 if (Mode
== NonDwoOnly
&& isDwoSection(Section
))
688 MSD
.SectionIndex
= SectionIndexMap
.lookup(&Section
);
689 assert(MSD
.SectionIndex
&& "Invalid section index!");
690 if (MSD
.SectionIndex
>= ELF::SHN_LORESERVE
)
691 HasLargeSectionIndex
= true;
694 StringRef Name
= Symbol
.getName();
696 // Sections have their own string table
697 if (Symbol
.getType() != ELF::STT_SECTION
) {
699 StrTabBuilder
.add(Name
);
703 LocalSymbolData
.push_back(MSD
);
705 ExternalSymbolData
.push_back(MSD
);
708 // This holds the .symtab_shndx section index.
709 unsigned SymtabShndxSectionIndex
= 0;
711 if (HasLargeSectionIndex
) {
712 MCSectionELF
*SymtabShndxSection
=
713 Ctx
.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX
, 0, 4);
714 SymtabShndxSectionIndex
= addToSectionTable(SymtabShndxSection
);
715 SymtabShndxSection
->setAlignment(Align(4));
718 StrTabBuilder
.finalize();
720 // Make the first STT_FILE precede previous local symbols.
722 auto FileNameIt
= FileNames
.begin();
723 if (!FileNames
.empty())
724 FileNames
[0].second
= 0;
726 for (ELFSymbolData
&MSD
: LocalSymbolData
) {
727 // Emit STT_FILE symbols before their associated local symbols.
728 for (; FileNameIt
!= FileNames
.end() && FileNameIt
->second
<= MSD
.Order
;
730 Writer
.writeSymbol(StrTabBuilder
.getOffset(FileNameIt
->first
),
731 ELF::STT_FILE
| ELF::STB_LOCAL
, 0, 0, ELF::STV_DEFAULT
,
736 unsigned StringIndex
= MSD
.Symbol
->getType() == ELF::STT_SECTION
738 : StrTabBuilder
.getOffset(MSD
.Name
);
739 MSD
.Symbol
->setIndex(Index
++);
740 writeSymbol(Writer
, StringIndex
, MSD
, Layout
);
742 for (; FileNameIt
!= FileNames
.end(); ++FileNameIt
) {
743 Writer
.writeSymbol(StrTabBuilder
.getOffset(FileNameIt
->first
),
744 ELF::STT_FILE
| ELF::STB_LOCAL
, 0, 0, ELF::STV_DEFAULT
,
749 // Write the symbol table entries.
750 LastLocalSymbolIndex
= Index
;
752 for (ELFSymbolData
&MSD
: ExternalSymbolData
) {
753 unsigned StringIndex
= StrTabBuilder
.getOffset(MSD
.Name
);
754 MSD
.Symbol
->setIndex(Index
++);
755 writeSymbol(Writer
, StringIndex
, MSD
, Layout
);
756 assert(MSD
.Symbol
->getBinding() != ELF::STB_LOCAL
);
759 uint64_t SecEnd
= W
.OS
.tell();
760 SectionOffsets
[SymtabSection
] = std::make_pair(SecStart
, SecEnd
);
762 ArrayRef
<uint32_t> ShndxIndexes
= Writer
.getShndxIndexes();
763 if (ShndxIndexes
.empty()) {
764 assert(SymtabShndxSectionIndex
== 0);
767 assert(SymtabShndxSectionIndex
!= 0);
769 SecStart
= W
.OS
.tell();
770 const MCSectionELF
*SymtabShndxSection
=
771 SectionTable
[SymtabShndxSectionIndex
- 1];
772 for (uint32_t Index
: ShndxIndexes
)
774 SecEnd
= W
.OS
.tell();
775 SectionOffsets
[SymtabShndxSection
] = std::make_pair(SecStart
, SecEnd
);
778 void ELFWriter::writeAddrsigSection() {
779 for (const MCSymbol
*Sym
: OWriter
.AddrsigSyms
)
780 encodeULEB128(Sym
->getIndex(), W
.OS
);
783 MCSectionELF
*ELFWriter::createRelocationSection(MCContext
&Ctx
,
784 const MCSectionELF
&Sec
) {
785 if (OWriter
.Relocations
[&Sec
].empty())
788 const StringRef SectionName
= Sec
.getName();
789 bool Rela
= usesRela(Sec
);
790 std::string RelaSectionName
= Rela
? ".rela" : ".rel";
791 RelaSectionName
+= SectionName
;
795 EntrySize
= is64Bit() ? sizeof(ELF::Elf64_Rela
) : sizeof(ELF::Elf32_Rela
);
797 EntrySize
= is64Bit() ? sizeof(ELF::Elf64_Rel
) : sizeof(ELF::Elf32_Rel
);
800 if (Sec
.getFlags() & ELF::SHF_GROUP
)
801 Flags
= ELF::SHF_GROUP
;
803 MCSectionELF
*RelaSection
= Ctx
.createELFRelSection(
804 RelaSectionName
, Rela
? ELF::SHT_RELA
: ELF::SHT_REL
, Flags
, EntrySize
,
805 Sec
.getGroup(), &Sec
);
806 RelaSection
->setAlignment(is64Bit() ? Align(8) : Align(4));
810 // Include the debug info compression header.
811 bool ELFWriter::maybeWriteCompression(
812 uint64_t Size
, SmallVectorImpl
<char> &CompressedContents
, bool ZLibStyle
,
813 unsigned Alignment
) {
816 is64Bit() ? sizeof(ELF::Elf32_Chdr
) : sizeof(ELF::Elf64_Chdr
);
817 if (Size
<= HdrSize
+ CompressedContents
.size())
819 // Platform specific header is followed by compressed data.
821 // Write Elf64_Chdr header.
822 write(static_cast<ELF::Elf64_Word
>(ELF::ELFCOMPRESS_ZLIB
));
823 write(static_cast<ELF::Elf64_Word
>(0)); // ch_reserved field.
824 write(static_cast<ELF::Elf64_Xword
>(Size
));
825 write(static_cast<ELF::Elf64_Xword
>(Alignment
));
827 // Write Elf32_Chdr header otherwise.
828 write(static_cast<ELF::Elf32_Word
>(ELF::ELFCOMPRESS_ZLIB
));
829 write(static_cast<ELF::Elf32_Word
>(Size
));
830 write(static_cast<ELF::Elf32_Word
>(Alignment
));
835 // "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
836 // useful for consumers to preallocate a buffer to decompress into.
837 const StringRef Magic
= "ZLIB";
838 if (Size
<= Magic
.size() + sizeof(Size
) + CompressedContents
.size())
841 support::endian::write(W
.OS
, Size
, support::big
);
845 void ELFWriter::writeSectionData(const MCAssembler
&Asm
, MCSection
&Sec
,
846 const MCAsmLayout
&Layout
) {
847 MCSectionELF
&Section
= static_cast<MCSectionELF
&>(Sec
);
848 StringRef SectionName
= Section
.getName();
850 auto &MC
= Asm
.getContext();
851 const auto &MAI
= MC
.getAsmInfo();
853 // Compressing debug_frame requires handling alignment fragments which is
854 // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
855 // for writing to arbitrary buffers) for little benefit.
856 bool CompressionEnabled
=
857 MAI
->compressDebugSections() != DebugCompressionType::None
;
858 if (!CompressionEnabled
|| !SectionName
.startswith(".debug_") ||
859 SectionName
== ".debug_frame") {
860 Asm
.writeSectionData(W
.OS
, &Section
, Layout
);
864 assert((MAI
->compressDebugSections() == DebugCompressionType::Z
||
865 MAI
->compressDebugSections() == DebugCompressionType::GNU
) &&
866 "expected zlib or zlib-gnu style compression");
868 SmallVector
<char, 128> UncompressedData
;
869 raw_svector_ostream
VecOS(UncompressedData
);
870 Asm
.writeSectionData(VecOS
, &Section
, Layout
);
872 SmallVector
<char, 128> CompressedContents
;
873 if (Error E
= zlib::compress(
874 StringRef(UncompressedData
.data(), UncompressedData
.size()),
875 CompressedContents
)) {
876 consumeError(std::move(E
));
877 W
.OS
<< UncompressedData
;
881 bool ZlibStyle
= MAI
->compressDebugSections() == DebugCompressionType::Z
;
882 if (!maybeWriteCompression(UncompressedData
.size(), CompressedContents
,
883 ZlibStyle
, Sec
.getAlignment())) {
884 W
.OS
<< UncompressedData
;
889 // Set the compressed flag. That is zlib style.
890 Section
.setFlags(Section
.getFlags() | ELF::SHF_COMPRESSED
);
891 // Alignment field should reflect the requirements of
892 // the compressed section header.
893 Section
.setAlignment(is64Bit() ? Align(8) : Align(4));
895 // Add "z" prefix to section name. This is zlib-gnu style.
896 MC
.renameELFSection(&Section
, (".z" + SectionName
.drop_front(1)).str());
898 W
.OS
<< CompressedContents
;
901 void ELFWriter::WriteSecHdrEntry(uint32_t Name
, uint32_t Type
, uint64_t Flags
,
902 uint64_t Address
, uint64_t Offset
,
903 uint64_t Size
, uint32_t Link
, uint32_t Info
,
904 uint64_t Alignment
, uint64_t EntrySize
) {
905 W
.write
<uint32_t>(Name
); // sh_name: index into string table
906 W
.write
<uint32_t>(Type
); // sh_type
907 WriteWord(Flags
); // sh_flags
908 WriteWord(Address
); // sh_addr
909 WriteWord(Offset
); // sh_offset
910 WriteWord(Size
); // sh_size
911 W
.write
<uint32_t>(Link
); // sh_link
912 W
.write
<uint32_t>(Info
); // sh_info
913 WriteWord(Alignment
); // sh_addralign
914 WriteWord(EntrySize
); // sh_entsize
917 void ELFWriter::writeRelocations(const MCAssembler
&Asm
,
918 const MCSectionELF
&Sec
) {
919 std::vector
<ELFRelocationEntry
> &Relocs
= OWriter
.Relocations
[&Sec
];
921 // We record relocations by pushing to the end of a vector. Reverse the vector
922 // to get the relocations in the order they were created.
923 // In most cases that is not important, but it can be for special sections
924 // (.eh_frame) or specific relocations (TLS optimizations on SystemZ).
925 std::reverse(Relocs
.begin(), Relocs
.end());
927 // Sort the relocation entries. MIPS needs this.
928 OWriter
.TargetObjectWriter
->sortRelocs(Asm
, Relocs
);
930 const bool Rela
= usesRela(Sec
);
931 for (unsigned i
= 0, e
= Relocs
.size(); i
!= e
; ++i
) {
932 const ELFRelocationEntry
&Entry
= Relocs
[e
- i
- 1];
933 unsigned Index
= Entry
.Symbol
? Entry
.Symbol
->getIndex() : 0;
937 if (OWriter
.TargetObjectWriter
->getEMachine() == ELF::EM_MIPS
) {
938 write(uint32_t(Index
));
940 write(OWriter
.TargetObjectWriter
->getRSsym(Entry
.Type
));
941 write(OWriter
.TargetObjectWriter
->getRType3(Entry
.Type
));
942 write(OWriter
.TargetObjectWriter
->getRType2(Entry
.Type
));
943 write(OWriter
.TargetObjectWriter
->getRType(Entry
.Type
));
945 struct ELF::Elf64_Rela ERE64
;
946 ERE64
.setSymbolAndType(Index
, Entry
.Type
);
952 write(uint32_t(Entry
.Offset
));
954 struct ELF::Elf32_Rela ERE32
;
955 ERE32
.setSymbolAndType(Index
, Entry
.Type
);
959 write(uint32_t(Entry
.Addend
));
961 if (OWriter
.TargetObjectWriter
->getEMachine() == ELF::EM_MIPS
) {
963 OWriter
.TargetObjectWriter
->getRType2(Entry
.Type
)) {
964 write(uint32_t(Entry
.Offset
));
966 ERE32
.setSymbolAndType(0, RType
);
971 OWriter
.TargetObjectWriter
->getRType3(Entry
.Type
)) {
972 write(uint32_t(Entry
.Offset
));
974 ERE32
.setSymbolAndType(0, RType
);
983 void ELFWriter::writeSection(const SectionIndexMapTy
&SectionIndexMap
,
984 uint32_t GroupSymbolIndex
, uint64_t Offset
,
985 uint64_t Size
, const MCSectionELF
&Section
) {
986 uint64_t sh_link
= 0;
987 uint64_t sh_info
= 0;
989 switch(Section
.getType()) {
994 case ELF::SHT_DYNAMIC
:
995 llvm_unreachable("SHT_DYNAMIC in a relocatable object");
998 case ELF::SHT_RELA
: {
999 sh_link
= SymbolTableIndex
;
1000 assert(sh_link
&& ".symtab not found");
1001 const MCSection
*InfoSection
= Section
.getLinkedToSection();
1002 sh_info
= SectionIndexMap
.lookup(cast
<MCSectionELF
>(InfoSection
));
1006 case ELF::SHT_SYMTAB
:
1007 sh_link
= StringTableIndex
;
1008 sh_info
= LastLocalSymbolIndex
;
1011 case ELF::SHT_SYMTAB_SHNDX
:
1012 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE
:
1013 case ELF::SHT_LLVM_ADDRSIG
:
1014 sh_link
= SymbolTableIndex
;
1017 case ELF::SHT_GROUP
:
1018 sh_link
= SymbolTableIndex
;
1019 sh_info
= GroupSymbolIndex
;
1023 if (Section
.getFlags() & ELF::SHF_LINK_ORDER
) {
1024 // If the value in the associated metadata is not a definition, Sym will be
1025 // undefined. Represent this with sh_link=0.
1026 const MCSymbol
*Sym
= Section
.getLinkedToSymbol();
1027 if (Sym
&& Sym
->isInSection()) {
1028 const MCSectionELF
*Sec
= cast
<MCSectionELF
>(&Sym
->getSection());
1029 sh_link
= SectionIndexMap
.lookup(Sec
);
1033 WriteSecHdrEntry(StrTabBuilder
.getOffset(Section
.getName()),
1034 Section
.getType(), Section
.getFlags(), 0, Offset
, Size
,
1035 sh_link
, sh_info
, Section
.getAlignment(),
1036 Section
.getEntrySize());
1039 void ELFWriter::writeSectionHeader(
1040 const MCAsmLayout
&Layout
, const SectionIndexMapTy
&SectionIndexMap
,
1041 const SectionOffsetsTy
&SectionOffsets
) {
1042 const unsigned NumSections
= SectionTable
.size();
1044 // Null section first.
1045 uint64_t FirstSectionSize
=
1046 (NumSections
+ 1) >= ELF::SHN_LORESERVE
? NumSections
+ 1 : 0;
1047 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize
, 0, 0, 0, 0);
1049 for (const MCSectionELF
*Section
: SectionTable
) {
1050 uint32_t GroupSymbolIndex
;
1051 unsigned Type
= Section
->getType();
1052 if (Type
!= ELF::SHT_GROUP
)
1053 GroupSymbolIndex
= 0;
1055 GroupSymbolIndex
= Section
->getGroup()->getIndex();
1057 const std::pair
<uint64_t, uint64_t> &Offsets
=
1058 SectionOffsets
.find(Section
)->second
;
1060 if (Type
== ELF::SHT_NOBITS
)
1061 Size
= Layout
.getSectionAddressSize(Section
);
1063 Size
= Offsets
.second
- Offsets
.first
;
1065 writeSection(SectionIndexMap
, GroupSymbolIndex
, Offsets
.first
, Size
,
1070 uint64_t ELFWriter::writeObject(MCAssembler
&Asm
, const MCAsmLayout
&Layout
) {
1071 uint64_t StartOffset
= W
.OS
.tell();
1073 MCContext
&Ctx
= Asm
.getContext();
1074 MCSectionELF
*StrtabSection
=
1075 Ctx
.getELFSection(".strtab", ELF::SHT_STRTAB
, 0);
1076 StringTableIndex
= addToSectionTable(StrtabSection
);
1078 RevGroupMapTy RevGroupMap
;
1079 SectionIndexMapTy SectionIndexMap
;
1081 std::map
<const MCSymbol
*, std::vector
<const MCSectionELF
*>> GroupMembers
;
1083 // Write out the ELF header ...
1086 // ... then the sections ...
1087 SectionOffsetsTy SectionOffsets
;
1088 std::vector
<MCSectionELF
*> Groups
;
1089 std::vector
<MCSectionELF
*> Relocations
;
1090 for (MCSection
&Sec
: Asm
) {
1091 MCSectionELF
&Section
= static_cast<MCSectionELF
&>(Sec
);
1092 if (Mode
== NonDwoOnly
&& isDwoSection(Section
))
1094 if (Mode
== DwoOnly
&& !isDwoSection(Section
))
1097 // Remember the offset into the file for this section.
1098 const uint64_t SecStart
= align(Section
.getAlignment());
1100 const MCSymbolELF
*SignatureSymbol
= Section
.getGroup();
1101 writeSectionData(Asm
, Section
, Layout
);
1103 uint64_t SecEnd
= W
.OS
.tell();
1104 SectionOffsets
[&Section
] = std::make_pair(SecStart
, SecEnd
);
1106 MCSectionELF
*RelSection
= createRelocationSection(Ctx
, Section
);
1108 if (SignatureSymbol
) {
1109 unsigned &GroupIdx
= RevGroupMap
[SignatureSymbol
];
1111 MCSectionELF
*Group
=
1112 Ctx
.createELFGroupSection(SignatureSymbol
, Section
.isComdat());
1113 GroupIdx
= addToSectionTable(Group
);
1114 Group
->setAlignment(Align(4));
1115 Groups
.push_back(Group
);
1117 std::vector
<const MCSectionELF
*> &Members
=
1118 GroupMembers
[SignatureSymbol
];
1119 Members
.push_back(&Section
);
1121 Members
.push_back(RelSection
);
1124 SectionIndexMap
[&Section
] = addToSectionTable(&Section
);
1126 SectionIndexMap
[RelSection
] = addToSectionTable(RelSection
);
1127 Relocations
.push_back(RelSection
);
1130 OWriter
.TargetObjectWriter
->addTargetSectionFlags(Ctx
, Section
);
1133 for (MCSectionELF
*Group
: Groups
) {
1134 // Remember the offset into the file for this section.
1135 const uint64_t SecStart
= align(Group
->getAlignment());
1137 const MCSymbol
*SignatureSymbol
= Group
->getGroup();
1138 assert(SignatureSymbol
);
1139 write(uint32_t(Group
->isComdat() ? unsigned(ELF::GRP_COMDAT
) : 0));
1140 for (const MCSectionELF
*Member
: GroupMembers
[SignatureSymbol
]) {
1141 uint32_t SecIndex
= SectionIndexMap
.lookup(Member
);
1145 uint64_t SecEnd
= W
.OS
.tell();
1146 SectionOffsets
[Group
] = std::make_pair(SecStart
, SecEnd
);
1149 if (Mode
== DwoOnly
) {
1150 // dwo files don't have symbol tables or relocations, but they do have
1152 StrTabBuilder
.finalize();
1154 MCSectionELF
*AddrsigSection
;
1155 if (OWriter
.EmitAddrsigSection
) {
1156 AddrsigSection
= Ctx
.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG
,
1158 addToSectionTable(AddrsigSection
);
1161 // Compute symbol table information.
1162 computeSymbolTable(Asm
, Layout
, SectionIndexMap
, RevGroupMap
,
1165 for (MCSectionELF
*RelSection
: Relocations
) {
1166 // Remember the offset into the file for this section.
1167 const uint64_t SecStart
= align(RelSection
->getAlignment());
1169 writeRelocations(Asm
,
1170 cast
<MCSectionELF
>(*RelSection
->getLinkedToSection()));
1172 uint64_t SecEnd
= W
.OS
.tell();
1173 SectionOffsets
[RelSection
] = std::make_pair(SecStart
, SecEnd
);
1176 if (OWriter
.EmitAddrsigSection
) {
1177 uint64_t SecStart
= W
.OS
.tell();
1178 writeAddrsigSection();
1179 uint64_t SecEnd
= W
.OS
.tell();
1180 SectionOffsets
[AddrsigSection
] = std::make_pair(SecStart
, SecEnd
);
1185 uint64_t SecStart
= W
.OS
.tell();
1186 StrTabBuilder
.write(W
.OS
);
1187 SectionOffsets
[StrtabSection
] = std::make_pair(SecStart
, W
.OS
.tell());
1190 const uint64_t SectionHeaderOffset
= align(is64Bit() ? 8 : 4);
1192 // ... then the section header table ...
1193 writeSectionHeader(Layout
, SectionIndexMap
, SectionOffsets
);
1195 uint16_t NumSections
= support::endian::byte_swap
<uint16_t>(
1196 (SectionTable
.size() + 1 >= ELF::SHN_LORESERVE
) ? (uint16_t)ELF::SHN_UNDEF
1197 : SectionTable
.size() + 1,
1199 unsigned NumSectionsOffset
;
1201 auto &Stream
= static_cast<raw_pwrite_stream
&>(W
.OS
);
1204 support::endian::byte_swap
<uint64_t>(SectionHeaderOffset
, W
.Endian
);
1205 Stream
.pwrite(reinterpret_cast<char *>(&Val
), sizeof(Val
),
1206 offsetof(ELF::Elf64_Ehdr
, e_shoff
));
1207 NumSectionsOffset
= offsetof(ELF::Elf64_Ehdr
, e_shnum
);
1210 support::endian::byte_swap
<uint32_t>(SectionHeaderOffset
, W
.Endian
);
1211 Stream
.pwrite(reinterpret_cast<char *>(&Val
), sizeof(Val
),
1212 offsetof(ELF::Elf32_Ehdr
, e_shoff
));
1213 NumSectionsOffset
= offsetof(ELF::Elf32_Ehdr
, e_shnum
);
1215 Stream
.pwrite(reinterpret_cast<char *>(&NumSections
), sizeof(NumSections
),
1218 return W
.OS
.tell() - StartOffset
;
1221 bool ELFObjectWriter::hasRelocationAddend() const {
1222 return TargetObjectWriter
->hasRelocationAddend();
1225 void ELFObjectWriter::executePostLayoutBinding(MCAssembler
&Asm
,
1226 const MCAsmLayout
&Layout
) {
1227 // The presence of symbol versions causes undefined symbols and
1228 // versions declared with @@@ to be renamed.
1229 for (const MCAssembler::Symver
&S
: Asm
.Symvers
) {
1230 StringRef AliasName
= S
.Name
;
1231 const auto &Symbol
= cast
<MCSymbolELF
>(*S
.Sym
);
1232 size_t Pos
= AliasName
.find('@');
1233 assert(Pos
!= StringRef::npos
);
1235 StringRef Prefix
= AliasName
.substr(0, Pos
);
1236 StringRef Rest
= AliasName
.substr(Pos
);
1237 StringRef Tail
= Rest
;
1238 if (Rest
.startswith("@@@"))
1239 Tail
= Rest
.substr(Symbol
.isUndefined() ? 2 : 1);
1242 cast
<MCSymbolELF
>(Asm
.getContext().getOrCreateSymbol(Prefix
+ Tail
));
1243 Asm
.registerSymbol(*Alias
);
1244 const MCExpr
*Value
= MCSymbolRefExpr::create(&Symbol
, Asm
.getContext());
1245 Alias
->setVariableValue(Value
);
1247 // Aliases defined with .symvar copy the binding from the symbol they alias.
1248 // This is the first place we are able to copy this information.
1249 Alias
->setBinding(Symbol
.getBinding());
1250 Alias
->setVisibility(Symbol
.getVisibility());
1251 Alias
->setOther(Symbol
.getOther());
1253 if (!Symbol
.isUndefined() && S
.KeepOriginalSym
)
1256 if (Symbol
.isUndefined() && Rest
.startswith("@@") &&
1257 !Rest
.startswith("@@@")) {
1258 Asm
.getContext().reportError(S
.Loc
, "default version symbol " +
1259 AliasName
+ " must be defined");
1263 if (Renames
.count(&Symbol
) && Renames
[&Symbol
] != Alias
) {
1264 Asm
.getContext().reportError(S
.Loc
, Twine("multiple versions for ") +
1269 Renames
.insert(std::make_pair(&Symbol
, Alias
));
1272 for (const MCSymbol
*&Sym
: AddrsigSyms
) {
1273 if (const MCSymbol
*R
= Renames
.lookup(cast
<MCSymbolELF
>(Sym
)))
1275 if (Sym
->isInSection() && Sym
->getName().startswith(".L"))
1276 Sym
= Sym
->getSection().getBeginSymbol();
1277 Sym
->setUsedInReloc();
1281 // It is always valid to create a relocation with a symbol. It is preferable
1282 // to use a relocation with a section if that is possible. Using the section
1283 // allows us to omit some local symbols from the symbol table.
1284 bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler
&Asm
,
1285 const MCSymbolRefExpr
*RefA
,
1286 const MCSymbolELF
*Sym
,
1288 unsigned Type
) const {
1289 // A PCRel relocation to an absolute value has no symbol (or section). We
1290 // represent that with a relocation to a null section.
1294 MCSymbolRefExpr::VariantKind Kind
= RefA
->getKind();
1298 // The .odp creation emits a relocation against the symbol ".TOC." which
1299 // create a R_PPC64_TOC relocation. However the relocation symbol name
1300 // in final object creation should be NULL, since the symbol does not
1301 // really exist, it is just the reference to TOC base for the current
1302 // object file. Since the symbol is undefined, returning false results
1303 // in a relocation with a null section which is the desired result.
1304 case MCSymbolRefExpr::VK_PPC_TOCBASE
:
1307 // These VariantKind cause the relocation to refer to something other than
1308 // the symbol itself, like a linker generated table. Since the address of
1309 // symbol is not relevant, we cannot replace the symbol with the
1310 // section and patch the difference in the addend.
1311 case MCSymbolRefExpr::VK_GOT
:
1312 case MCSymbolRefExpr::VK_PLT
:
1313 case MCSymbolRefExpr::VK_GOTPCREL
:
1314 case MCSymbolRefExpr::VK_PPC_GOT_LO
:
1315 case MCSymbolRefExpr::VK_PPC_GOT_HI
:
1316 case MCSymbolRefExpr::VK_PPC_GOT_HA
:
1320 // An undefined symbol is not in any section, so the relocation has to point
1321 // to the symbol itself.
1322 assert(Sym
&& "Expected a symbol");
1323 if (Sym
->isUndefined())
1326 unsigned Binding
= Sym
->getBinding();
1329 llvm_unreachable("Invalid Binding");
1330 case ELF::STB_LOCAL
:
1333 // If the symbol is weak, it might be overridden by a symbol in another
1334 // file. The relocation has to point to the symbol so that the linker
1337 case ELF::STB_GLOBAL
:
1338 // Global ELF symbols can be preempted by the dynamic linker. The relocation
1339 // has to point to the symbol for a reason analogous to the STB_WEAK case.
1343 // Keep symbol type for a local ifunc because it may result in an IRELATIVE
1344 // reloc that the dynamic loader will use to resolve the address at startup
1346 if (Sym
->getType() == ELF::STT_GNU_IFUNC
)
1349 // If a relocation points to a mergeable section, we have to be careful.
1350 // If the offset is zero, a relocation with the section will encode the
1351 // same information. With a non-zero offset, the situation is different.
1352 // For example, a relocation can point 42 bytes past the end of a string.
1353 // If we change such a relocation to use the section, the linker would think
1354 // that it pointed to another string and subtracting 42 at runtime will
1355 // produce the wrong value.
1356 if (Sym
->isInSection()) {
1357 auto &Sec
= cast
<MCSectionELF
>(Sym
->getSection());
1358 unsigned Flags
= Sec
.getFlags();
1359 if (Flags
& ELF::SHF_MERGE
) {
1363 // gold<2.34 incorrectly ignored the addend for R_386_GOTOFF (9)
1364 // (http://sourceware.org/PR16794).
1365 if (TargetObjectWriter
->getEMachine() == ELF::EM_386
&&
1366 Type
== ELF::R_386_GOTOFF
)
1369 // ld.lld handles R_MIPS_HI16/R_MIPS_LO16 separately, not as a whole, so
1370 // it doesn't know that an R_MIPS_HI16 with implicit addend 1 and an
1371 // R_MIPS_LO16 with implicit addend -32768 represents 32768, which is in
1372 // range of a MergeInputSection. We could introduce a new RelExpr member
1373 // (like R_RISCV_PC_INDIRECT for R_RISCV_PCREL_HI20 / R_RISCV_PCREL_LO12)
1374 // but the complexity is unnecessary given that GNU as keeps the original
1375 // symbol for this case as well.
1376 if (TargetObjectWriter
->getEMachine() == ELF::EM_MIPS
&&
1377 !hasRelocationAddend())
1381 // Most TLS relocations use a got, so they need the symbol. Even those that
1382 // are just an offset (@tpoff), require a symbol in gold versions before
1383 // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
1384 // http://sourceware.org/PR16773.
1385 if (Flags
& ELF::SHF_TLS
)
1389 // If the symbol is a thumb function the final relocation must set the lowest
1390 // bit. With a symbol that is done by just having the symbol have that bit
1391 // set, so we would lose the bit if we relocated with the section.
1392 // FIXME: We could use the section but add the bit to the relocation value.
1393 if (Asm
.isThumbFunc(Sym
))
1396 if (TargetObjectWriter
->needsRelocateWithSymbol(*Sym
, Type
))
1401 void ELFObjectWriter::recordRelocation(MCAssembler
&Asm
,
1402 const MCAsmLayout
&Layout
,
1403 const MCFragment
*Fragment
,
1404 const MCFixup
&Fixup
, MCValue Target
,
1405 uint64_t &FixedValue
) {
1406 MCAsmBackend
&Backend
= Asm
.getBackend();
1407 bool IsPCRel
= Backend
.getFixupKindInfo(Fixup
.getKind()).Flags
&
1408 MCFixupKindInfo::FKF_IsPCRel
;
1409 const MCSectionELF
&FixupSection
= cast
<MCSectionELF
>(*Fragment
->getParent());
1410 uint64_t C
= Target
.getConstant();
1411 uint64_t FixupOffset
= Layout
.getFragmentOffset(Fragment
) + Fixup
.getOffset();
1412 MCContext
&Ctx
= Asm
.getContext();
1414 if (const MCSymbolRefExpr
*RefB
= Target
.getSymB()) {
1415 const auto &SymB
= cast
<MCSymbolELF
>(RefB
->getSymbol());
1416 if (SymB
.isUndefined()) {
1417 Ctx
.reportError(Fixup
.getLoc(),
1418 Twine("symbol '") + SymB
.getName() +
1419 "' can not be undefined in a subtraction expression");
1423 assert(!SymB
.isAbsolute() && "Should have been folded");
1424 const MCSection
&SecB
= SymB
.getSection();
1425 if (&SecB
!= &FixupSection
) {
1426 Ctx
.reportError(Fixup
.getLoc(),
1427 "Cannot represent a difference across sections");
1431 assert(!IsPCRel
&& "should have been folded");
1433 C
+= FixupOffset
- Layout
.getSymbolOffset(SymB
);
1436 // We either rejected the fixup or folded B into C at this point.
1437 const MCSymbolRefExpr
*RefA
= Target
.getSymA();
1438 const auto *SymA
= RefA
? cast
<MCSymbolELF
>(&RefA
->getSymbol()) : nullptr;
1440 bool ViaWeakRef
= false;
1441 if (SymA
&& SymA
->isVariable()) {
1442 const MCExpr
*Expr
= SymA
->getVariableValue();
1443 if (const auto *Inner
= dyn_cast
<MCSymbolRefExpr
>(Expr
)) {
1444 if (Inner
->getKind() == MCSymbolRefExpr::VK_WEAKREF
) {
1445 SymA
= cast
<MCSymbolELF
>(&Inner
->getSymbol());
1451 const MCSectionELF
*SecA
= (SymA
&& SymA
->isInSection())
1452 ? cast
<MCSectionELF
>(&SymA
->getSection())
1454 if (!checkRelocation(Ctx
, Fixup
.getLoc(), &FixupSection
, SecA
))
1457 unsigned Type
= TargetObjectWriter
->getRelocType(Ctx
, Target
, Fixup
, IsPCRel
);
1458 const auto *Parent
= cast
<MCSectionELF
>(Fragment
->getParent());
1459 // Emiting relocation with sybmol for CG Profile to help with --cg-profile.
1460 bool RelocateWithSymbol
=
1461 shouldRelocateWithSymbol(Asm
, RefA
, SymA
, C
, Type
) ||
1462 (Parent
->getType() == ELF::SHT_LLVM_CALL_GRAPH_PROFILE
);
1463 uint64_t Addend
= 0;
1465 FixedValue
= !RelocateWithSymbol
&& SymA
&& !SymA
->isUndefined()
1466 ? C
+ Layout
.getSymbolOffset(*SymA
)
1468 if (hasRelocationAddend()) {
1469 Addend
= FixedValue
;
1473 if (!RelocateWithSymbol
) {
1474 const auto *SectionSymbol
=
1475 SecA
? cast
<MCSymbolELF
>(SecA
->getBeginSymbol()) : nullptr;
1477 SectionSymbol
->setUsedInReloc();
1478 ELFRelocationEntry
Rec(FixupOffset
, SectionSymbol
, Type
, Addend
, SymA
, C
);
1479 Relocations
[&FixupSection
].push_back(Rec
);
1483 const MCSymbolELF
*RenamedSymA
= SymA
;
1485 if (const MCSymbolELF
*R
= Renames
.lookup(SymA
))
1489 RenamedSymA
->setIsWeakrefUsedInReloc();
1491 RenamedSymA
->setUsedInReloc();
1493 ELFRelocationEntry
Rec(FixupOffset
, RenamedSymA
, Type
, Addend
, SymA
, C
);
1494 Relocations
[&FixupSection
].push_back(Rec
);
1497 bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1498 const MCAssembler
&Asm
, const MCSymbol
&SA
, const MCFragment
&FB
,
1499 bool InSet
, bool IsPCRel
) const {
1500 const auto &SymA
= cast
<MCSymbolELF
>(SA
);
1503 if (SymA
.getBinding() != ELF::STB_LOCAL
||
1504 SymA
.getType() == ELF::STT_GNU_IFUNC
)
1507 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm
, SymA
, FB
,
1511 std::unique_ptr
<MCObjectWriter
>
1512 llvm::createELFObjectWriter(std::unique_ptr
<MCELFObjectTargetWriter
> MOTW
,
1513 raw_pwrite_stream
&OS
, bool IsLittleEndian
) {
1514 return std::make_unique
<ELFSingleObjectWriter
>(std::move(MOTW
), OS
,
1518 std::unique_ptr
<MCObjectWriter
>
1519 llvm::createELFDwoObjectWriter(std::unique_ptr
<MCELFObjectTargetWriter
> MOTW
,
1520 raw_pwrite_stream
&OS
, raw_pwrite_stream
&DwoOS
,
1521 bool IsLittleEndian
) {
1522 return std::make_unique
<ELFDwoObjectWriter
>(std::move(MOTW
), OS
, DwoOS
,