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/SmallVector.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/ADT/iterator.h"
21 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/MC/MCAsmBackend.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCAssembler.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCELFExtras.h"
27 #include "llvm/MC/MCELFObjectWriter.h"
28 #include "llvm/MC/MCExpr.h"
29 #include "llvm/MC/MCFixup.h"
30 #include "llvm/MC/MCFixupKindInfo.h"
31 #include "llvm/MC/MCFragment.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/MCTargetOptions.h"
38 #include "llvm/MC/MCValue.h"
39 #include "llvm/MC/StringTableBuilder.h"
40 #include "llvm/Support/Alignment.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Compression.h"
44 #include "llvm/Support/Endian.h"
45 #include "llvm/Support/EndianStream.h"
46 #include "llvm/Support/Error.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/LEB128.h"
49 #include "llvm/Support/MathExtras.h"
50 #include "llvm/Support/SMLoc.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include "llvm/TargetParser/Host.h"
66 #define DEBUG_TYPE "reloc-info"
72 bool isDwoSection(const MCSectionELF
&Sec
) {
73 return Sec
.getName().ends_with(".dwo");
76 class SymbolTableWriter
{
80 // indexes we are going to write to .symtab_shndx.
81 std::vector
<uint32_t> ShndxIndexes
;
83 // The numbel of symbols written so far.
86 void createSymtabShndx();
88 template <typename T
> void write(T Value
);
91 SymbolTableWriter(ELFWriter
&EWriter
, bool Is64Bit
);
93 void writeSymbol(uint32_t name
, uint8_t info
, uint64_t value
, uint64_t size
,
94 uint8_t other
, uint32_t shndx
, bool Reserved
);
96 ArrayRef
<uint32_t> getShndxIndexes() const { return ShndxIndexes
; }
100 ELFObjectWriter
&OWriter
;
101 support::endian::Writer W
;
109 static uint64_t symbolValue(const MCAssembler
&Asm
, const MCSymbol
&Sym
);
110 static bool isInSymtab(const MCAssembler
&Asm
, const MCSymbolELF
&Symbol
,
111 bool Used
, bool Renamed
);
113 /// Helper struct for containing some precomputed information on symbols.
114 struct ELFSymbolData
{
115 const MCSymbolELF
*Symbol
;
117 uint32_t SectionIndex
;
122 /// @name Symbol Table Data
125 StringTableBuilder StrTabBuilder
{StringTableBuilder::ELF
};
129 // This holds the symbol table index of the last local symbol.
130 unsigned LastLocalSymbolIndex
= ~0u;
131 // This holds the .strtab section index.
132 unsigned StringTableIndex
= ~0u;
133 // This holds the .symtab section index.
134 unsigned SymbolTableIndex
= ~0u;
136 // Sections in the order they are to be output in the section table.
137 std::vector
<MCSectionELF
*> SectionTable
;
138 unsigned addToSectionTable(MCSectionELF
*Sec
);
140 // TargetObjectWriter wrappers.
141 bool is64Bit() const;
143 uint64_t align(Align Alignment
);
145 bool maybeWriteCompression(uint32_t ChType
, uint64_t Size
,
146 SmallVectorImpl
<uint8_t> &CompressedContents
,
150 ELFWriter(ELFObjectWriter
&OWriter
, raw_pwrite_stream
&OS
,
151 bool IsLittleEndian
, DwoMode Mode
)
152 : OWriter(OWriter
), W(OS
, IsLittleEndian
? llvm::endianness::little
153 : llvm::endianness::big
),
156 void WriteWord(uint64_t Word
) {
158 W
.write
<uint64_t>(Word
);
160 W
.write
<uint32_t>(Word
);
163 template <typename T
> void write(T Val
) {
167 void writeHeader(const MCAssembler
&Asm
);
169 void writeSymbol(const MCAssembler
&Asm
, SymbolTableWriter
&Writer
,
170 uint32_t StringIndex
, ELFSymbolData
&MSD
);
172 // Map from a signature symbol to the group section index
173 using RevGroupMapTy
= DenseMap
<const MCSymbol
*, unsigned>;
175 /// Compute the symbol table data
177 /// \param Asm - The assembler.
178 /// \param RevGroupMap - Maps a signature symbol to the group section.
179 void computeSymbolTable(MCAssembler
&Asm
, const RevGroupMapTy
&RevGroupMap
);
181 void writeAddrsigSection();
183 MCSectionELF
*createRelocationSection(MCContext
&Ctx
,
184 const MCSectionELF
&Sec
);
186 void writeSectionHeader(const MCAssembler
&Asm
);
188 void writeSectionData(const MCAssembler
&Asm
, MCSection
&Sec
);
190 void WriteSecHdrEntry(uint32_t Name
, uint32_t Type
, uint64_t Flags
,
191 uint64_t Address
, uint64_t Offset
, uint64_t Size
,
192 uint32_t Link
, uint32_t Info
, MaybeAlign Alignment
,
195 void writeRelocations(const MCAssembler
&Asm
, const MCSectionELF
&Sec
);
197 uint64_t writeObject(MCAssembler
&Asm
);
198 void writeSection(uint32_t GroupSymbolIndex
, uint64_t Offset
, uint64_t Size
,
199 const MCSectionELF
&Section
);
201 } // end anonymous namespace
203 uint64_t ELFWriter::align(Align Alignment
) {
204 uint64_t Offset
= W
.OS
.tell();
205 uint64_t NewOffset
= alignTo(Offset
, Alignment
);
206 W
.OS
.write_zeros(NewOffset
- Offset
);
210 unsigned ELFWriter::addToSectionTable(MCSectionELF
*Sec
) {
211 SectionTable
.push_back(Sec
);
212 StrTabBuilder
.add(Sec
->getName());
213 return SectionTable
.size();
216 void SymbolTableWriter::createSymtabShndx() {
217 if (!ShndxIndexes
.empty())
220 ShndxIndexes
.resize(NumWritten
);
223 template <typename T
> void SymbolTableWriter::write(T Value
) {
224 EWriter
.write(Value
);
227 SymbolTableWriter::SymbolTableWriter(ELFWriter
&EWriter
, bool Is64Bit
)
228 : EWriter(EWriter
), Is64Bit(Is64Bit
), NumWritten(0) {}
230 void SymbolTableWriter::writeSymbol(uint32_t name
, uint8_t info
, uint64_t value
,
231 uint64_t size
, uint8_t other
,
232 uint32_t shndx
, bool Reserved
) {
233 bool LargeIndex
= shndx
>= ELF::SHN_LORESERVE
&& !Reserved
;
238 if (!ShndxIndexes
.empty()) {
240 ShndxIndexes
.push_back(shndx
);
242 ShndxIndexes
.push_back(0);
245 uint16_t Index
= LargeIndex
? uint16_t(ELF::SHN_XINDEX
) : shndx
;
248 write(name
); // st_name
249 write(info
); // st_info
250 write(other
); // st_other
251 write(Index
); // st_shndx
252 write(value
); // st_value
253 write(size
); // st_size
255 write(name
); // st_name
256 write(uint32_t(value
)); // st_value
257 write(uint32_t(size
)); // st_size
258 write(info
); // st_info
259 write(other
); // st_other
260 write(Index
); // st_shndx
266 bool ELFWriter::is64Bit() const {
267 return OWriter
.TargetObjectWriter
->is64Bit();
270 // Emit the ELF header.
271 void ELFWriter::writeHeader(const MCAssembler
&Asm
) {
277 // emitWord method behaves differently for ELF32 and ELF64, writing
278 // 4 bytes in the former and 8 in the latter.
280 W
.OS
<< ELF::ElfMagic
; // e_ident[EI_MAG0] to e_ident[EI_MAG3]
282 W
.OS
<< char(is64Bit() ? ELF::ELFCLASS64
: ELF::ELFCLASS32
); // e_ident[EI_CLASS]
285 W
.OS
<< char(W
.Endian
== llvm::endianness::little
? ELF::ELFDATA2LSB
288 W
.OS
<< char(ELF::EV_CURRENT
); // e_ident[EI_VERSION]
290 uint8_t OSABI
= OWriter
.TargetObjectWriter
->getOSABI();
291 W
.OS
<< char(OSABI
== ELF::ELFOSABI_NONE
&& OWriter
.seenGnuAbi()
292 ? int(ELF::ELFOSABI_GNU
)
294 // e_ident[EI_ABIVERSION]
295 W
.OS
<< char(OWriter
.OverrideABIVersion
296 ? *OWriter
.OverrideABIVersion
297 : OWriter
.TargetObjectWriter
->getABIVersion());
299 W
.OS
.write_zeros(ELF::EI_NIDENT
- ELF::EI_PAD
);
301 W
.write
<uint16_t>(ELF::ET_REL
); // e_type
303 W
.write
<uint16_t>(OWriter
.TargetObjectWriter
->getEMachine()); // e_machine = target
305 W
.write
<uint32_t>(ELF::EV_CURRENT
); // e_version
306 WriteWord(0); // e_entry, no entry point in .o file
307 WriteWord(0); // e_phoff, no program header for .o
308 WriteWord(0); // e_shoff = sec hdr table off in bytes
310 // e_flags = whatever the target wants
311 W
.write
<uint32_t>(OWriter
.getELFHeaderEFlags());
313 // e_ehsize = ELF header size
314 W
.write
<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr
)
315 : sizeof(ELF::Elf32_Ehdr
));
317 W
.write
<uint16_t>(0); // e_phentsize = prog header entry size
318 W
.write
<uint16_t>(0); // e_phnum = # prog header entries = 0
320 // e_shentsize = Section header entry size
321 W
.write
<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr
)
322 : sizeof(ELF::Elf32_Shdr
));
324 // e_shnum = # of section header ents
325 W
.write
<uint16_t>(0);
327 // e_shstrndx = Section # of '.strtab'
328 assert(StringTableIndex
< ELF::SHN_LORESERVE
);
329 W
.write
<uint16_t>(StringTableIndex
);
332 uint64_t ELFWriter::symbolValue(const MCAssembler
&Asm
, const MCSymbol
&Sym
) {
334 return Sym
.getCommonAlignment()->value();
337 if (!Asm
.getSymbolOffset(Sym
, Res
))
340 if (Asm
.isThumbFunc(&Sym
))
346 static uint8_t mergeTypeForSet(uint8_t origType
, uint8_t newType
) {
347 uint8_t Type
= newType
;
349 // Propagation rules:
350 // IFUNC > FUNC > OBJECT > NOTYPE
351 // TLS_OBJECT > OBJECT > NOTYPE
353 // dont let the new type degrade the old type
357 case ELF::STT_GNU_IFUNC
:
358 if (Type
== ELF::STT_FUNC
|| Type
== ELF::STT_OBJECT
||
359 Type
== ELF::STT_NOTYPE
|| Type
== ELF::STT_TLS
)
360 Type
= ELF::STT_GNU_IFUNC
;
363 if (Type
== ELF::STT_OBJECT
|| Type
== ELF::STT_NOTYPE
||
364 Type
== ELF::STT_TLS
)
365 Type
= ELF::STT_FUNC
;
367 case ELF::STT_OBJECT
:
368 if (Type
== ELF::STT_NOTYPE
)
369 Type
= ELF::STT_OBJECT
;
372 if (Type
== ELF::STT_OBJECT
|| Type
== ELF::STT_NOTYPE
||
373 Type
== ELF::STT_GNU_IFUNC
|| Type
== ELF::STT_FUNC
)
381 static bool isIFunc(const MCSymbolELF
*Symbol
) {
382 while (Symbol
->getType() != ELF::STT_GNU_IFUNC
) {
383 const MCSymbolRefExpr
*Value
;
384 if (!Symbol
->isVariable() ||
385 !(Value
= dyn_cast
<MCSymbolRefExpr
>(Symbol
->getVariableValue())) ||
386 Value
->getKind() != MCSymbolRefExpr::VK_None
||
387 mergeTypeForSet(Symbol
->getType(), ELF::STT_GNU_IFUNC
) != ELF::STT_GNU_IFUNC
)
389 Symbol
= &cast
<MCSymbolELF
>(Value
->getSymbol());
394 void ELFWriter::writeSymbol(const MCAssembler
&Asm
, SymbolTableWriter
&Writer
,
395 uint32_t StringIndex
, ELFSymbolData
&MSD
) {
396 const auto &Symbol
= cast
<MCSymbolELF
>(*MSD
.Symbol
);
397 const MCSymbolELF
*Base
=
398 cast_or_null
<MCSymbolELF
>(Asm
.getBaseSymbol(Symbol
));
400 // This has to be in sync with when computeSymbolTable uses SHN_ABS or
402 bool IsReserved
= !Base
|| Symbol
.isCommon();
404 // Binding and Type share the same byte as upper and lower nibbles
405 uint8_t Binding
= Symbol
.getBinding();
406 uint8_t Type
= Symbol
.getType();
407 if (isIFunc(&Symbol
))
408 Type
= ELF::STT_GNU_IFUNC
;
410 Type
= mergeTypeForSet(Type
, Base
->getType());
412 uint8_t Info
= (Binding
<< 4) | Type
;
414 // Other and Visibility share the same byte with Visibility using the lower
416 uint8_t Visibility
= Symbol
.getVisibility();
417 uint8_t Other
= Symbol
.getOther() | Visibility
;
419 uint64_t Value
= symbolValue(Asm
, *MSD
.Symbol
);
422 const MCExpr
*ESize
= MSD
.Symbol
->getSize();
423 if (!ESize
&& Base
) {
424 // For expressions like .set y, x+1, if y's size is unset, inherit from x.
425 ESize
= Base
->getSize();
427 // For `.size x, 2; y = x; .size y, 1; z = y; z1 = z; .symver y, y@v1`, z,
428 // z1, and y@v1's st_size equals y's. However, `Base` is `x` which will give
429 // us 2. Follow the MCSymbolRefExpr assignment chain, which covers most
430 // needs. MCBinaryExpr is not handled.
431 const MCSymbolELF
*Sym
= &Symbol
;
432 while (Sym
->isVariable()) {
434 dyn_cast
<MCSymbolRefExpr
>(Sym
->getVariableValue(false))) {
435 Sym
= cast
<MCSymbolELF
>(&Expr
->getSymbol());
438 ESize
= Sym
->getSize();
446 if (!ESize
->evaluateKnownAbsolute(Res
, Asm
))
447 report_fatal_error("Size expression must be absolute.");
451 // Write out the symbol table entry
452 Writer
.writeSymbol(StringIndex
, Info
, Value
, Size
, Other
, MSD
.SectionIndex
,
456 bool ELFWriter::isInSymtab(const MCAssembler
&Asm
, const MCSymbolELF
&Symbol
,
457 bool Used
, bool Renamed
) {
458 if (Symbol
.isVariable()) {
459 const MCExpr
*Expr
= Symbol
.getVariableValue();
460 // Target Expressions that are always inlined do not appear in the symtab
461 if (const auto *T
= dyn_cast
<MCTargetExpr
>(Expr
))
462 if (T
->inlineAssignedExpr())
464 if (const MCSymbolRefExpr
*Ref
= dyn_cast
<MCSymbolRefExpr
>(Expr
)) {
465 if (Ref
->getKind() == MCSymbolRefExpr::VK_WEAKREF
)
476 if (Symbol
.isVariable() && Symbol
.isUndefined()) {
477 // FIXME: this is here just to diagnose the case of a var = commmon_sym.
478 Asm
.getBaseSymbol(Symbol
);
482 if (Symbol
.isTemporary())
485 if (Symbol
.getType() == ELF::STT_SECTION
)
491 void ELFWriter::computeSymbolTable(MCAssembler
&Asm
,
492 const RevGroupMapTy
&RevGroupMap
) {
493 MCContext
&Ctx
= Asm
.getContext();
494 SymbolTableWriter
Writer(*this, is64Bit());
497 unsigned EntrySize
= is64Bit() ? ELF::SYMENTRY_SIZE64
: ELF::SYMENTRY_SIZE32
;
498 MCSectionELF
*SymtabSection
=
499 Ctx
.getELFSection(".symtab", ELF::SHT_SYMTAB
, 0, EntrySize
);
500 SymtabSection
->setAlignment(is64Bit() ? Align(8) : Align(4));
501 SymbolTableIndex
= addToSectionTable(SymtabSection
);
503 uint64_t SecStart
= align(SymtabSection
->getAlign());
505 // The first entry is the undefined symbol entry.
506 Writer
.writeSymbol(0, 0, 0, 0, 0, 0, false);
508 std::vector
<ELFSymbolData
> LocalSymbolData
;
509 std::vector
<ELFSymbolData
> ExternalSymbolData
;
510 MutableArrayRef
<std::pair
<std::string
, size_t>> FileNames
=
511 OWriter
.getFileNames();
512 for (const std::pair
<std::string
, size_t> &F
: FileNames
)
513 StrTabBuilder
.add(F
.first
);
515 // Add the data for the symbols.
516 bool HasLargeSectionIndex
= false;
517 for (auto It
: llvm::enumerate(Asm
.symbols())) {
518 const auto &Symbol
= cast
<MCSymbolELF
>(It
.value());
519 bool Used
= Symbol
.isUsedInReloc();
520 bool WeakrefUsed
= Symbol
.isWeakrefUsedInReloc();
521 bool isSignature
= Symbol
.isSignature();
523 if (!isInSymtab(Asm
, Symbol
, Used
|| WeakrefUsed
|| isSignature
,
524 OWriter
.Renames
.count(&Symbol
)))
527 if (Symbol
.isTemporary() && Symbol
.isUndefined()) {
528 Ctx
.reportError(SMLoc(), "Undefined temporary symbol " + Symbol
.getName());
533 MSD
.Symbol
= cast
<MCSymbolELF
>(&Symbol
);
534 MSD
.Order
= It
.index();
536 bool Local
= Symbol
.getBinding() == ELF::STB_LOCAL
;
537 assert(Local
|| !Symbol
.isTemporary());
539 if (Symbol
.isAbsolute()) {
540 MSD
.SectionIndex
= ELF::SHN_ABS
;
541 } else if (Symbol
.isCommon()) {
542 if (Symbol
.isTargetCommon()) {
543 MSD
.SectionIndex
= Symbol
.getIndex();
546 MSD
.SectionIndex
= ELF::SHN_COMMON
;
548 } else if (Symbol
.isUndefined()) {
549 if (isSignature
&& !Used
) {
550 MSD
.SectionIndex
= RevGroupMap
.lookup(&Symbol
);
551 if (MSD
.SectionIndex
>= ELF::SHN_LORESERVE
)
552 HasLargeSectionIndex
= true;
554 MSD
.SectionIndex
= ELF::SHN_UNDEF
;
557 const MCSectionELF
&Section
=
558 static_cast<const MCSectionELF
&>(Symbol
.getSection());
560 // We may end up with a situation when section symbol is technically
561 // defined, but should not be. That happens because we explicitly
562 // pre-create few .debug_* sections to have accessors.
563 // And if these sections were not really defined in the code, but were
564 // referenced, we simply error out.
565 if (!Section
.isRegistered()) {
566 assert(static_cast<const MCSymbolELF
&>(Symbol
).getType() ==
568 Ctx
.reportError(SMLoc(),
569 "Undefined section reference: " + Symbol
.getName());
573 if (Mode
== NonDwoOnly
&& isDwoSection(Section
))
575 MSD
.SectionIndex
= Section
.getOrdinal();
576 assert(MSD
.SectionIndex
&& "Invalid section index!");
577 if (MSD
.SectionIndex
>= ELF::SHN_LORESERVE
)
578 HasLargeSectionIndex
= true;
581 // Temporary symbols generated for certain assembler features (.eh_frame,
582 // .debug_line) of an empty name may be referenced by relocations due to
583 // linker relaxation. Rename them to ".L0 " to match the gas fake label name
584 // and allow ld/objcopy --discard-locals to discard such symbols.
585 StringRef Name
= Symbol
.getName();
589 // Sections have their own string table
590 if (Symbol
.getType() != ELF::STT_SECTION
) {
592 StrTabBuilder
.add(Name
);
596 LocalSymbolData
.push_back(MSD
);
598 ExternalSymbolData
.push_back(MSD
);
601 // This holds the .symtab_shndx section index.
602 unsigned SymtabShndxSectionIndex
= 0;
604 if (HasLargeSectionIndex
) {
605 MCSectionELF
*SymtabShndxSection
=
606 Ctx
.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX
, 0, 4);
607 SymtabShndxSectionIndex
= addToSectionTable(SymtabShndxSection
);
608 SymtabShndxSection
->setAlignment(Align(4));
611 StrTabBuilder
.finalize();
613 // Make the first STT_FILE precede previous local symbols.
615 auto FileNameIt
= FileNames
.begin();
616 if (!FileNames
.empty())
617 FileNames
[0].second
= 0;
619 for (ELFSymbolData
&MSD
: LocalSymbolData
) {
620 // Emit STT_FILE symbols before their associated local symbols.
621 for (; FileNameIt
!= FileNames
.end() && FileNameIt
->second
<= MSD
.Order
;
623 Writer
.writeSymbol(StrTabBuilder
.getOffset(FileNameIt
->first
),
624 ELF::STT_FILE
| ELF::STB_LOCAL
, 0, 0, ELF::STV_DEFAULT
,
629 unsigned StringIndex
= MSD
.Symbol
->getType() == ELF::STT_SECTION
631 : StrTabBuilder
.getOffset(MSD
.Name
);
632 MSD
.Symbol
->setIndex(Index
++);
633 writeSymbol(Asm
, Writer
, StringIndex
, MSD
);
635 for (; FileNameIt
!= FileNames
.end(); ++FileNameIt
) {
636 Writer
.writeSymbol(StrTabBuilder
.getOffset(FileNameIt
->first
),
637 ELF::STT_FILE
| ELF::STB_LOCAL
, 0, 0, ELF::STV_DEFAULT
,
642 // Write the symbol table entries.
643 LastLocalSymbolIndex
= Index
;
645 for (ELFSymbolData
&MSD
: ExternalSymbolData
) {
646 unsigned StringIndex
= StrTabBuilder
.getOffset(MSD
.Name
);
647 MSD
.Symbol
->setIndex(Index
++);
648 writeSymbol(Asm
, Writer
, StringIndex
, MSD
);
649 assert(MSD
.Symbol
->getBinding() != ELF::STB_LOCAL
);
652 uint64_t SecEnd
= W
.OS
.tell();
653 SymtabSection
->setOffsets(SecStart
, SecEnd
);
655 ArrayRef
<uint32_t> ShndxIndexes
= Writer
.getShndxIndexes();
656 if (ShndxIndexes
.empty()) {
657 assert(SymtabShndxSectionIndex
== 0);
660 assert(SymtabShndxSectionIndex
!= 0);
662 SecStart
= W
.OS
.tell();
663 MCSectionELF
*SymtabShndxSection
= SectionTable
[SymtabShndxSectionIndex
- 1];
664 for (uint32_t Index
: ShndxIndexes
)
666 SecEnd
= W
.OS
.tell();
667 SymtabShndxSection
->setOffsets(SecStart
, SecEnd
);
670 void ELFWriter::writeAddrsigSection() {
671 for (const MCSymbol
*Sym
: OWriter
.getAddrsigSyms())
672 if (Sym
->getIndex() != 0)
673 encodeULEB128(Sym
->getIndex(), W
.OS
);
676 MCSectionELF
*ELFWriter::createRelocationSection(MCContext
&Ctx
,
677 const MCSectionELF
&Sec
) {
678 if (OWriter
.Relocations
[&Sec
].empty())
681 unsigned Flags
= ELF::SHF_INFO_LINK
;
682 if (Sec
.getFlags() & ELF::SHF_GROUP
)
683 Flags
= ELF::SHF_GROUP
;
685 const StringRef SectionName
= Sec
.getName();
686 const MCTargetOptions
*TO
= Ctx
.getTargetOptions();
687 if (TO
&& TO
->Crel
) {
688 MCSectionELF
*RelaSection
=
689 Ctx
.createELFRelSection(".crel" + SectionName
, ELF::SHT_CREL
, Flags
,
690 /*EntrySize=*/1, Sec
.getGroup(), &Sec
);
694 const bool Rela
= OWriter
.usesRela(TO
, Sec
);
697 EntrySize
= is64Bit() ? sizeof(ELF::Elf64_Rela
) : sizeof(ELF::Elf32_Rela
);
699 EntrySize
= is64Bit() ? sizeof(ELF::Elf64_Rel
) : sizeof(ELF::Elf32_Rel
);
701 MCSectionELF
*RelaSection
=
702 Ctx
.createELFRelSection(((Rela
? ".rela" : ".rel") + SectionName
),
703 Rela
? ELF::SHT_RELA
: ELF::SHT_REL
, Flags
,
704 EntrySize
, Sec
.getGroup(), &Sec
);
705 RelaSection
->setAlignment(is64Bit() ? Align(8) : Align(4));
709 // Include the debug info compression header.
710 bool ELFWriter::maybeWriteCompression(
711 uint32_t ChType
, uint64_t Size
,
712 SmallVectorImpl
<uint8_t> &CompressedContents
, Align Alignment
) {
714 is64Bit() ? sizeof(ELF::Elf64_Chdr
) : sizeof(ELF::Elf32_Chdr
);
715 if (Size
<= HdrSize
+ CompressedContents
.size())
717 // Platform specific header is followed by compressed data.
719 // Write Elf64_Chdr header.
720 write(static_cast<ELF::Elf64_Word
>(ChType
));
721 write(static_cast<ELF::Elf64_Word
>(0)); // ch_reserved field.
722 write(static_cast<ELF::Elf64_Xword
>(Size
));
723 write(static_cast<ELF::Elf64_Xword
>(Alignment
.value()));
725 // Write Elf32_Chdr header otherwise.
726 write(static_cast<ELF::Elf32_Word
>(ChType
));
727 write(static_cast<ELF::Elf32_Word
>(Size
));
728 write(static_cast<ELF::Elf32_Word
>(Alignment
.value()));
733 void ELFWriter::writeSectionData(const MCAssembler
&Asm
, MCSection
&Sec
) {
734 MCSectionELF
&Section
= static_cast<MCSectionELF
&>(Sec
);
735 StringRef SectionName
= Section
.getName();
736 auto &Ctx
= Asm
.getContext();
737 const DebugCompressionType CompressionType
=
738 Ctx
.getTargetOptions() ? Ctx
.getTargetOptions()->CompressDebugSections
739 : DebugCompressionType::None
;
740 if (CompressionType
== DebugCompressionType::None
||
741 !SectionName
.starts_with(".debug_")) {
742 Asm
.writeSectionData(W
.OS
, &Section
);
746 SmallVector
<char, 128> UncompressedData
;
747 raw_svector_ostream
VecOS(UncompressedData
);
748 Asm
.writeSectionData(VecOS
, &Section
);
749 ArrayRef
<uint8_t> Uncompressed
=
750 ArrayRef(reinterpret_cast<uint8_t *>(UncompressedData
.data()),
751 UncompressedData
.size());
753 SmallVector
<uint8_t, 128> Compressed
;
755 switch (CompressionType
) {
756 case DebugCompressionType::None
:
757 llvm_unreachable("has been handled");
758 case DebugCompressionType::Zlib
:
759 ChType
= ELF::ELFCOMPRESS_ZLIB
;
761 case DebugCompressionType::Zstd
:
762 ChType
= ELF::ELFCOMPRESS_ZSTD
;
765 compression::compress(compression::Params(CompressionType
), Uncompressed
,
767 if (!maybeWriteCompression(ChType
, UncompressedData
.size(), Compressed
,
769 W
.OS
<< UncompressedData
;
773 Section
.setFlags(Section
.getFlags() | ELF::SHF_COMPRESSED
);
774 // Alignment field should reflect the requirements of
775 // the compressed section header.
776 Section
.setAlignment(is64Bit() ? Align(8) : Align(4));
777 W
.OS
<< toStringRef(Compressed
);
780 void ELFWriter::WriteSecHdrEntry(uint32_t Name
, uint32_t Type
, uint64_t Flags
,
781 uint64_t Address
, uint64_t Offset
,
782 uint64_t Size
, uint32_t Link
, uint32_t Info
,
783 MaybeAlign Alignment
, uint64_t EntrySize
) {
784 W
.write
<uint32_t>(Name
); // sh_name: index into string table
785 W
.write
<uint32_t>(Type
); // sh_type
786 WriteWord(Flags
); // sh_flags
787 WriteWord(Address
); // sh_addr
788 WriteWord(Offset
); // sh_offset
789 WriteWord(Size
); // sh_size
790 W
.write
<uint32_t>(Link
); // sh_link
791 W
.write
<uint32_t>(Info
); // sh_info
792 WriteWord(Alignment
? Alignment
->value() : 0); // sh_addralign
793 WriteWord(EntrySize
); // sh_entsize
797 static void encodeCrel(ArrayRef
<ELFRelocationEntry
> Relocs
, raw_ostream
&OS
) {
798 using uint
= std::conditional_t
<Is64
, uint64_t, uint32_t>;
799 ELF::encodeCrel
<Is64
>(OS
, Relocs
, [&](const ELFRelocationEntry
&R
) {
800 uint32_t SymIdx
= R
.Symbol
? R
.Symbol
->getIndex() : 0;
801 return ELF::Elf_Crel
<Is64
>{static_cast<uint
>(R
.Offset
), SymIdx
, R
.Type
,
802 std::make_signed_t
<uint
>(R
.Addend
)};
806 void ELFWriter::writeRelocations(const MCAssembler
&Asm
,
807 const MCSectionELF
&Sec
) {
808 std::vector
<ELFRelocationEntry
> &Relocs
= OWriter
.Relocations
[&Sec
];
809 const MCTargetOptions
*TO
= Asm
.getContext().getTargetOptions();
810 const bool Rela
= OWriter
.usesRela(TO
, Sec
);
812 // Sort the relocation entries. MIPS needs this.
813 OWriter
.TargetObjectWriter
->sortRelocs(Asm
, Relocs
);
815 if (OWriter
.TargetObjectWriter
->getEMachine() == ELF::EM_MIPS
) {
816 for (const ELFRelocationEntry
&Entry
: Relocs
) {
817 uint32_t SymIdx
= Entry
.Symbol
? Entry
.Symbol
->getIndex() : 0;
820 write(uint32_t(SymIdx
));
821 write(OWriter
.TargetObjectWriter
->getRSsym(Entry
.Type
));
822 write(OWriter
.TargetObjectWriter
->getRType3(Entry
.Type
));
823 write(OWriter
.TargetObjectWriter
->getRType2(Entry
.Type
));
824 write(OWriter
.TargetObjectWriter
->getRType(Entry
.Type
));
828 write(uint32_t(Entry
.Offset
));
829 ELF::Elf32_Rela ERE32
;
830 ERE32
.setSymbolAndType(SymIdx
, Entry
.Type
);
833 write(uint32_t(Entry
.Addend
));
835 OWriter
.TargetObjectWriter
->getRType2(Entry
.Type
)) {
836 write(uint32_t(Entry
.Offset
));
837 ERE32
.setSymbolAndType(0, RType
);
842 OWriter
.TargetObjectWriter
->getRType3(Entry
.Type
)) {
843 write(uint32_t(Entry
.Offset
));
844 ERE32
.setSymbolAndType(0, RType
);
850 } else if (TO
&& TO
->Crel
) {
852 encodeCrel
<true>(Relocs
, W
.OS
);
854 encodeCrel
<false>(Relocs
, W
.OS
);
856 for (const ELFRelocationEntry
&Entry
: Relocs
) {
857 uint32_t Symidx
= Entry
.Symbol
? Entry
.Symbol
->getIndex() : 0;
861 ERE
.setSymbolAndType(Symidx
, Entry
.Type
);
866 write(uint32_t(Entry
.Offset
));
868 ERE
.setSymbolAndType(Symidx
, Entry
.Type
);
871 write(uint32_t(Entry
.Addend
));
877 void ELFWriter::writeSection(uint32_t GroupSymbolIndex
, uint64_t Offset
,
878 uint64_t Size
, const MCSectionELF
&Section
) {
879 uint64_t sh_link
= 0;
880 uint64_t sh_info
= 0;
882 switch(Section
.getType()) {
887 case ELF::SHT_DYNAMIC
:
888 llvm_unreachable("SHT_DYNAMIC in a relocatable object");
892 case ELF::SHT_CREL
: {
893 sh_link
= SymbolTableIndex
;
894 assert(sh_link
&& ".symtab not found");
895 const MCSection
*InfoSection
= Section
.getLinkedToSection();
896 sh_info
= InfoSection
->getOrdinal();
900 case ELF::SHT_SYMTAB
:
901 sh_link
= StringTableIndex
;
902 sh_info
= LastLocalSymbolIndex
;
905 case ELF::SHT_SYMTAB_SHNDX
:
906 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE
:
907 case ELF::SHT_LLVM_ADDRSIG
:
908 sh_link
= SymbolTableIndex
;
912 sh_link
= SymbolTableIndex
;
913 sh_info
= GroupSymbolIndex
;
917 if (Section
.getFlags() & ELF::SHF_LINK_ORDER
) {
918 // If the value in the associated metadata is not a definition, Sym will be
919 // undefined. Represent this with sh_link=0.
920 const MCSymbol
*Sym
= Section
.getLinkedToSymbol();
921 if (Sym
&& Sym
->isInSection())
922 sh_link
= Sym
->getSection().getOrdinal();
925 WriteSecHdrEntry(StrTabBuilder
.getOffset(Section
.getName()),
926 Section
.getType(), Section
.getFlags(), 0, Offset
, Size
,
927 sh_link
, sh_info
, Section
.getAlign(),
928 Section
.getEntrySize());
931 void ELFWriter::writeSectionHeader(const MCAssembler
&Asm
) {
932 const unsigned NumSections
= SectionTable
.size();
934 // Null section first.
935 uint64_t FirstSectionSize
=
936 (NumSections
+ 1) >= ELF::SHN_LORESERVE
? NumSections
+ 1 : 0;
937 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize
, 0, 0, std::nullopt
, 0);
939 for (const MCSectionELF
*Section
: SectionTable
) {
940 uint32_t GroupSymbolIndex
;
941 unsigned Type
= Section
->getType();
942 if (Type
!= ELF::SHT_GROUP
)
943 GroupSymbolIndex
= 0;
945 GroupSymbolIndex
= Section
->getGroup()->getIndex();
947 std::pair
<uint64_t, uint64_t> Offsets
= Section
->getOffsets();
949 if (Type
== ELF::SHT_NOBITS
)
950 Size
= Asm
.getSectionAddressSize(*Section
);
952 Size
= Offsets
.second
- Offsets
.first
;
954 writeSection(GroupSymbolIndex
, Offsets
.first
, Size
, *Section
);
958 uint64_t ELFWriter::writeObject(MCAssembler
&Asm
) {
959 uint64_t StartOffset
= W
.OS
.tell();
961 MCContext
&Ctx
= Asm
.getContext();
962 MCSectionELF
*StrtabSection
=
963 Ctx
.getELFSection(".strtab", ELF::SHT_STRTAB
, 0);
964 StringTableIndex
= addToSectionTable(StrtabSection
);
966 RevGroupMapTy RevGroupMap
;
968 // Write out the ELF header ...
971 // ... then the sections ...
972 SmallVector
<std::pair
<MCSectionELF
*, SmallVector
<unsigned>>, 0> Groups
;
973 // Map from group section index to group
974 SmallVector
<unsigned, 0> GroupMap
;
975 SmallVector
<MCSectionELF
*> Relocations
;
976 for (MCSection
&Sec
: Asm
) {
977 MCSectionELF
&Section
= static_cast<MCSectionELF
&>(Sec
);
978 if (Mode
== NonDwoOnly
&& isDwoSection(Section
))
980 if (Mode
== DwoOnly
&& !isDwoSection(Section
))
983 // Remember the offset into the file for this section.
984 const uint64_t SecStart
= align(Section
.getAlign());
986 const MCSymbolELF
*SignatureSymbol
= Section
.getGroup();
987 writeSectionData(Asm
, Section
);
989 uint64_t SecEnd
= W
.OS
.tell();
990 Section
.setOffsets(SecStart
, SecEnd
);
992 MCSectionELF
*RelSection
= createRelocationSection(Ctx
, Section
);
994 unsigned *GroupIdxEntry
= nullptr;
995 if (SignatureSymbol
) {
996 GroupIdxEntry
= &RevGroupMap
[SignatureSymbol
];
997 if (!*GroupIdxEntry
) {
998 MCSectionELF
*Group
=
999 Ctx
.createELFGroupSection(SignatureSymbol
, Section
.isComdat());
1000 *GroupIdxEntry
= addToSectionTable(Group
);
1001 Group
->setAlignment(Align(4));
1003 GroupMap
.resize(*GroupIdxEntry
+ 1);
1004 GroupMap
[*GroupIdxEntry
] = Groups
.size();
1005 Groups
.emplace_back(Group
, SmallVector
<unsigned>{});
1009 Section
.setOrdinal(addToSectionTable(&Section
));
1011 RelSection
->setOrdinal(addToSectionTable(RelSection
));
1012 Relocations
.push_back(RelSection
);
1015 if (GroupIdxEntry
) {
1016 auto &Members
= Groups
[GroupMap
[*GroupIdxEntry
]];
1017 Members
.second
.push_back(Section
.getOrdinal());
1019 Members
.second
.push_back(RelSection
->getOrdinal());
1022 OWriter
.TargetObjectWriter
->addTargetSectionFlags(Ctx
, Section
);
1025 for (auto &[Group
, Members
] : Groups
) {
1026 // Remember the offset into the file for this section.
1027 const uint64_t SecStart
= align(Group
->getAlign());
1029 write(uint32_t(Group
->isComdat() ? unsigned(ELF::GRP_COMDAT
) : 0));
1030 W
.write
<unsigned>(Members
);
1032 uint64_t SecEnd
= W
.OS
.tell();
1033 Group
->setOffsets(SecStart
, SecEnd
);
1036 if (Mode
== DwoOnly
) {
1037 // dwo files don't have symbol tables or relocations, but they do have
1039 StrTabBuilder
.finalize();
1041 MCSectionELF
*AddrsigSection
;
1042 if (OWriter
.getEmitAddrsigSection()) {
1043 AddrsigSection
= Ctx
.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG
,
1045 addToSectionTable(AddrsigSection
);
1048 // Compute symbol table information.
1049 computeSymbolTable(Asm
, RevGroupMap
);
1051 for (MCSectionELF
*RelSection
: Relocations
) {
1052 // Remember the offset into the file for this section.
1053 const uint64_t SecStart
= align(RelSection
->getAlign());
1055 writeRelocations(Asm
,
1056 cast
<MCSectionELF
>(*RelSection
->getLinkedToSection()));
1058 uint64_t SecEnd
= W
.OS
.tell();
1059 RelSection
->setOffsets(SecStart
, SecEnd
);
1062 if (OWriter
.getEmitAddrsigSection()) {
1063 uint64_t SecStart
= W
.OS
.tell();
1064 writeAddrsigSection();
1065 uint64_t SecEnd
= W
.OS
.tell();
1066 AddrsigSection
->setOffsets(SecStart
, SecEnd
);
1071 uint64_t SecStart
= W
.OS
.tell();
1072 StrTabBuilder
.write(W
.OS
);
1073 StrtabSection
->setOffsets(SecStart
, W
.OS
.tell());
1076 const uint64_t SectionHeaderOffset
= align(is64Bit() ? Align(8) : Align(4));
1078 // ... then the section header table ...
1079 writeSectionHeader(Asm
);
1081 uint16_t NumSections
= support::endian::byte_swap
<uint16_t>(
1082 (SectionTable
.size() + 1 >= ELF::SHN_LORESERVE
) ? (uint16_t)ELF::SHN_UNDEF
1083 : SectionTable
.size() + 1,
1085 unsigned NumSectionsOffset
;
1087 auto &Stream
= static_cast<raw_pwrite_stream
&>(W
.OS
);
1090 support::endian::byte_swap
<uint64_t>(SectionHeaderOffset
, W
.Endian
);
1091 Stream
.pwrite(reinterpret_cast<char *>(&Val
), sizeof(Val
),
1092 offsetof(ELF::Elf64_Ehdr
, e_shoff
));
1093 NumSectionsOffset
= offsetof(ELF::Elf64_Ehdr
, e_shnum
);
1096 support::endian::byte_swap
<uint32_t>(SectionHeaderOffset
, W
.Endian
);
1097 Stream
.pwrite(reinterpret_cast<char *>(&Val
), sizeof(Val
),
1098 offsetof(ELF::Elf32_Ehdr
, e_shoff
));
1099 NumSectionsOffset
= offsetof(ELF::Elf32_Ehdr
, e_shnum
);
1101 Stream
.pwrite(reinterpret_cast<char *>(&NumSections
), sizeof(NumSections
),
1104 return W
.OS
.tell() - StartOffset
;
1107 ELFObjectWriter::ELFObjectWriter(std::unique_ptr
<MCELFObjectTargetWriter
> MOTW
,
1108 raw_pwrite_stream
&OS
, bool IsLittleEndian
)
1109 : TargetObjectWriter(std::move(MOTW
)), OS(OS
),
1110 IsLittleEndian(IsLittleEndian
) {}
1111 ELFObjectWriter::ELFObjectWriter(std::unique_ptr
<MCELFObjectTargetWriter
> MOTW
,
1112 raw_pwrite_stream
&OS
,
1113 raw_pwrite_stream
&DwoOS
, bool IsLittleEndian
)
1114 : TargetObjectWriter(std::move(MOTW
)), OS(OS
), DwoOS(&DwoOS
),
1115 IsLittleEndian(IsLittleEndian
) {}
1117 void ELFObjectWriter::reset() {
1118 ELFHeaderEFlags
= 0;
1120 OverrideABIVersion
.reset();
1121 Relocations
.clear();
1124 MCObjectWriter::reset();
1127 bool ELFObjectWriter::hasRelocationAddend() const {
1128 return TargetObjectWriter
->hasRelocationAddend();
1131 void ELFObjectWriter::executePostLayoutBinding(MCAssembler
&Asm
) {
1132 // The presence of symbol versions causes undefined symbols and
1133 // versions declared with @@@ to be renamed.
1134 for (const Symver
&S
: Symvers
) {
1135 StringRef AliasName
= S
.Name
;
1136 const auto &Symbol
= cast
<MCSymbolELF
>(*S
.Sym
);
1137 size_t Pos
= AliasName
.find('@');
1138 assert(Pos
!= StringRef::npos
);
1140 StringRef Prefix
= AliasName
.substr(0, Pos
);
1141 StringRef Rest
= AliasName
.substr(Pos
);
1142 StringRef Tail
= Rest
;
1143 if (Rest
.starts_with("@@@"))
1144 Tail
= Rest
.substr(Symbol
.isUndefined() ? 2 : 1);
1147 cast
<MCSymbolELF
>(Asm
.getContext().getOrCreateSymbol(Prefix
+ Tail
));
1148 Asm
.registerSymbol(*Alias
);
1149 const MCExpr
*Value
= MCSymbolRefExpr::create(&Symbol
, Asm
.getContext());
1150 Alias
->setVariableValue(Value
);
1152 // Aliases defined with .symvar copy the binding from the symbol they alias.
1153 // This is the first place we are able to copy this information.
1154 Alias
->setBinding(Symbol
.getBinding());
1155 Alias
->setVisibility(Symbol
.getVisibility());
1156 Alias
->setOther(Symbol
.getOther());
1158 if (!Symbol
.isUndefined() && S
.KeepOriginalSym
)
1161 if (Symbol
.isUndefined() && Rest
.starts_with("@@") &&
1162 !Rest
.starts_with("@@@")) {
1163 Asm
.getContext().reportError(S
.Loc
, "default version symbol " +
1164 AliasName
+ " must be defined");
1168 if (Renames
.count(&Symbol
) && Renames
[&Symbol
] != Alias
) {
1169 Asm
.getContext().reportError(S
.Loc
, Twine("multiple versions for ") +
1174 Renames
.insert(std::make_pair(&Symbol
, Alias
));
1177 for (const MCSymbol
*&Sym
: AddrsigSyms
) {
1178 if (const MCSymbol
*R
= Renames
.lookup(cast
<MCSymbolELF
>(Sym
)))
1180 if (Sym
->isInSection() && Sym
->getName().starts_with(".L"))
1181 Sym
= Sym
->getSection().getBeginSymbol();
1182 Sym
->setUsedInReloc();
1186 // It is always valid to create a relocation with a symbol. It is preferable
1187 // to use a relocation with a section if that is possible. Using the section
1188 // allows us to omit some local symbols from the symbol table.
1189 bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler
&Asm
,
1191 const MCSymbolELF
*Sym
,
1193 unsigned Type
) const {
1194 const MCSymbolRefExpr
*RefA
= Val
.getSymA();
1195 // A PCRel relocation to an absolute value has no symbol (or section). We
1196 // represent that with a relocation to a null section.
1200 MCSymbolRefExpr::VariantKind Kind
= RefA
->getKind();
1204 // The .odp creation emits a relocation against the symbol ".TOC." which
1205 // create a R_PPC64_TOC relocation. However the relocation symbol name
1206 // in final object creation should be NULL, since the symbol does not
1207 // really exist, it is just the reference to TOC base for the current
1208 // object file. Since the symbol is undefined, returning false results
1209 // in a relocation with a null section which is the desired result.
1210 case MCSymbolRefExpr::VK_PPC_TOCBASE
:
1213 // These VariantKind cause the relocation to refer to something other than
1214 // the symbol itself, like a linker generated table. Since the address of
1215 // symbol is not relevant, we cannot replace the symbol with the
1216 // section and patch the difference in the addend.
1217 case MCSymbolRefExpr::VK_GOT
:
1218 case MCSymbolRefExpr::VK_PLT
:
1219 case MCSymbolRefExpr::VK_GOTPCREL
:
1220 case MCSymbolRefExpr::VK_GOTPCREL_NORELAX
:
1221 case MCSymbolRefExpr::VK_PPC_GOT_LO
:
1222 case MCSymbolRefExpr::VK_PPC_GOT_HI
:
1223 case MCSymbolRefExpr::VK_PPC_GOT_HA
:
1227 // An undefined symbol is not in any section, so the relocation has to point
1228 // to the symbol itself.
1229 assert(Sym
&& "Expected a symbol");
1230 if (Sym
->isUndefined())
1233 // For memory-tagged symbols, ensure that the relocation uses the symbol. For
1234 // tagged symbols, we emit an empty relocation (R_AARCH64_NONE) in a special
1235 // section (SHT_AARCH64_MEMTAG_GLOBALS_STATIC) to indicate to the linker that
1236 // this global needs to be tagged. In addition, the linker needs to know
1237 // whether to emit a special addend when relocating `end` symbols, and this
1238 // can only be determined by the attributes of the symbol itself.
1239 if (Sym
->isMemtag())
1242 unsigned Binding
= Sym
->getBinding();
1245 llvm_unreachable("Invalid Binding");
1246 case ELF::STB_LOCAL
:
1249 // If the symbol is weak, it might be overridden by a symbol in another
1250 // file. The relocation has to point to the symbol so that the linker
1253 case ELF::STB_GLOBAL
:
1254 case ELF::STB_GNU_UNIQUE
:
1255 // Global ELF symbols can be preempted by the dynamic linker. The relocation
1256 // has to point to the symbol for a reason analogous to the STB_WEAK case.
1260 // Keep symbol type for a local ifunc because it may result in an IRELATIVE
1261 // reloc that the dynamic loader will use to resolve the address at startup
1263 if (Sym
->getType() == ELF::STT_GNU_IFUNC
)
1266 // If a relocation points to a mergeable section, we have to be careful.
1267 // If the offset is zero, a relocation with the section will encode the
1268 // same information. With a non-zero offset, the situation is different.
1269 // For example, a relocation can point 42 bytes past the end of a string.
1270 // If we change such a relocation to use the section, the linker would think
1271 // that it pointed to another string and subtracting 42 at runtime will
1272 // produce the wrong value.
1273 if (Sym
->isInSection()) {
1274 auto &Sec
= cast
<MCSectionELF
>(Sym
->getSection());
1275 unsigned Flags
= Sec
.getFlags();
1276 if (Flags
& ELF::SHF_MERGE
) {
1280 // gold<2.34 incorrectly ignored the addend for R_386_GOTOFF (9)
1281 // (http://sourceware.org/PR16794).
1282 if (TargetObjectWriter
->getEMachine() == ELF::EM_386
&&
1283 Type
== ELF::R_386_GOTOFF
)
1286 // ld.lld handles R_MIPS_HI16/R_MIPS_LO16 separately, not as a whole, so
1287 // it doesn't know that an R_MIPS_HI16 with implicit addend 1 and an
1288 // R_MIPS_LO16 with implicit addend -32768 represents 32768, which is in
1289 // range of a MergeInputSection. We could introduce a new RelExpr member
1290 // (like R_RISCV_PC_INDIRECT for R_RISCV_PCREL_HI20 / R_RISCV_PCREL_LO12)
1291 // but the complexity is unnecessary given that GNU as keeps the original
1292 // symbol for this case as well.
1293 if (TargetObjectWriter
->getEMachine() == ELF::EM_MIPS
&&
1294 !hasRelocationAddend())
1298 // Most TLS relocations use a got, so they need the symbol. Even those that
1299 // are just an offset (@tpoff), require a symbol in gold versions before
1300 // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
1301 // http://sourceware.org/PR16773.
1302 if (Flags
& ELF::SHF_TLS
)
1306 // If the symbol is a thumb function the final relocation must set the lowest
1307 // bit. With a symbol that is done by just having the symbol have that bit
1308 // set, so we would lose the bit if we relocated with the section.
1309 // FIXME: We could use the section but add the bit to the relocation value.
1310 if (Asm
.isThumbFunc(Sym
))
1313 if (TargetObjectWriter
->needsRelocateWithSymbol(Val
, *Sym
, Type
))
1318 bool ELFObjectWriter::checkRelocation(MCContext
&Ctx
, SMLoc Loc
,
1319 const MCSectionELF
*From
,
1320 const MCSectionELF
*To
) {
1322 if (isDwoSection(*From
)) {
1323 Ctx
.reportError(Loc
, "A dwo section may not contain relocations");
1326 if (To
&& isDwoSection(*To
)) {
1327 Ctx
.reportError(Loc
, "A relocation may not refer to a dwo section");
1334 void ELFObjectWriter::recordRelocation(MCAssembler
&Asm
,
1335 const MCFragment
*Fragment
,
1336 const MCFixup
&Fixup
, MCValue Target
,
1337 uint64_t &FixedValue
) {
1338 MCAsmBackend
&Backend
= Asm
.getBackend();
1339 bool IsPCRel
= Backend
.getFixupKindInfo(Fixup
.getKind()).Flags
&
1340 MCFixupKindInfo::FKF_IsPCRel
;
1341 const MCSectionELF
&FixupSection
= cast
<MCSectionELF
>(*Fragment
->getParent());
1342 uint64_t C
= Target
.getConstant();
1343 uint64_t FixupOffset
= Asm
.getFragmentOffset(*Fragment
) + Fixup
.getOffset();
1344 MCContext
&Ctx
= Asm
.getContext();
1345 const MCTargetOptions
*TO
= Ctx
.getTargetOptions();
1347 if (const MCSymbolRefExpr
*RefB
= Target
.getSymB()) {
1348 const auto &SymB
= cast
<MCSymbolELF
>(RefB
->getSymbol());
1349 if (SymB
.isUndefined()) {
1350 Ctx
.reportError(Fixup
.getLoc(),
1351 Twine("symbol '") + SymB
.getName() +
1352 "' can not be undefined in a subtraction expression");
1356 assert(!SymB
.isAbsolute() && "Should have been folded");
1357 const MCSection
&SecB
= SymB
.getSection();
1358 if (&SecB
!= &FixupSection
) {
1359 Ctx
.reportError(Fixup
.getLoc(),
1360 "Cannot represent a difference across sections");
1364 assert(!IsPCRel
&& "should have been folded");
1366 C
+= FixupOffset
- Asm
.getSymbolOffset(SymB
);
1369 // We either rejected the fixup or folded B into C at this point.
1370 const MCSymbolRefExpr
*RefA
= Target
.getSymA();
1371 const auto *SymA
= RefA
? cast
<MCSymbolELF
>(&RefA
->getSymbol()) : nullptr;
1373 bool ViaWeakRef
= false;
1374 if (SymA
&& SymA
->isVariable()) {
1375 const MCExpr
*Expr
= SymA
->getVariableValue();
1376 if (const auto *Inner
= dyn_cast
<MCSymbolRefExpr
>(Expr
)) {
1377 if (Inner
->getKind() == MCSymbolRefExpr::VK_WEAKREF
) {
1378 SymA
= cast
<MCSymbolELF
>(&Inner
->getSymbol());
1384 const MCSectionELF
*SecA
= (SymA
&& SymA
->isInSection())
1385 ? cast
<MCSectionELF
>(&SymA
->getSection())
1387 if (!checkRelocation(Ctx
, Fixup
.getLoc(), &FixupSection
, SecA
))
1390 unsigned Type
= TargetObjectWriter
->getRelocType(Ctx
, Target
, Fixup
, IsPCRel
);
1391 const auto *Parent
= cast
<MCSectionELF
>(Fragment
->getParent());
1392 // Emiting relocation with sybmol for CG Profile to help with --cg-profile.
1393 bool RelocateWithSymbol
=
1394 shouldRelocateWithSymbol(Asm
, Target
, SymA
, C
, Type
) ||
1395 (Parent
->getType() == ELF::SHT_LLVM_CALL_GRAPH_PROFILE
);
1396 uint64_t Addend
= 0;
1398 FixedValue
= !RelocateWithSymbol
&& SymA
&& !SymA
->isUndefined()
1399 ? C
+ Asm
.getSymbolOffset(*SymA
)
1401 if (usesRela(TO
, FixupSection
)) {
1402 Addend
= FixedValue
;
1406 if (!RelocateWithSymbol
) {
1407 const auto *SectionSymbol
=
1408 SecA
? cast
<MCSymbolELF
>(SecA
->getBeginSymbol()) : nullptr;
1410 SectionSymbol
->setUsedInReloc();
1411 ELFRelocationEntry
Rec(FixupOffset
, SectionSymbol
, Type
, Addend
, SymA
, C
);
1412 Relocations
[&FixupSection
].push_back(Rec
);
1416 const MCSymbolELF
*RenamedSymA
= SymA
;
1418 if (const MCSymbolELF
*R
= Renames
.lookup(SymA
))
1422 RenamedSymA
->setIsWeakrefUsedInReloc();
1424 RenamedSymA
->setUsedInReloc();
1426 ELFRelocationEntry
Rec(FixupOffset
, RenamedSymA
, Type
, Addend
, SymA
, C
);
1427 Relocations
[&FixupSection
].push_back(Rec
);
1430 bool ELFObjectWriter::usesRela(const MCTargetOptions
*TO
,
1431 const MCSectionELF
&Sec
) const {
1432 return (hasRelocationAddend() &&
1433 Sec
.getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE
) ||
1437 bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1438 const MCAssembler
&Asm
, const MCSymbol
&SA
, const MCFragment
&FB
,
1439 bool InSet
, bool IsPCRel
) const {
1440 const auto &SymA
= cast
<MCSymbolELF
>(SA
);
1443 if (SymA
.getBinding() != ELF::STB_LOCAL
||
1444 SymA
.getType() == ELF::STT_GNU_IFUNC
)
1447 return &SymA
.getSection() == FB
.getParent();
1450 uint64_t ELFObjectWriter::writeObject(MCAssembler
&Asm
) {
1452 ELFWriter(*this, OS
, IsLittleEndian
,
1453 DwoOS
? ELFWriter::NonDwoOnly
: ELFWriter::AllSections
)
1456 Size
+= ELFWriter(*this, *DwoOS
, IsLittleEndian
, ELFWriter::DwoOnly
)