Skip to content

Commit 2f9ae0b

Browse files
authored
ELF: Rename Relocations.cpp functions and rewrite the file-level comment. NFC
Pull Request: #161229
1 parent d96c32c commit 2f9ae0b

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

lld/ELF/Relocations.cpp

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,22 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file contains platform-independent functions to process relocations.
10-
// I'll describe the overview of this file here.
9+
// This file implements the core relocation processing logic. It analyzes
10+
// relocations and determines what auxiliary data structures (GOT, PLT, copy
11+
// relocations) need to be created during linking.
1112
//
12-
// Simple relocations are easy to handle for the linker. For example,
13-
// for R_X86_64_PC64 relocs, the linker just has to fix up locations
14-
// with the relative offsets to the target symbols. It would just be
15-
// reading records from relocation sections and applying them to output.
13+
// The main entry point is scanRelocations<ELFT>(), which calls scanSection()
14+
// to process all relocations within an input section. For each relocation,
15+
// scan() analyzes the type and target, and determines whether a synthetic
16+
// section entry or dynamic relocation is needed.
1617
//
17-
// But not all relocations are that easy to handle. For example, for
18-
// R_386_GOTOFF relocs, the linker has to create new GOT entries for
19-
// symbols if they don't exist, and fix up locations with GOT entry
20-
// offsets from the beginning of GOT section. So there is more than
21-
// fixing addresses in relocation processing.
18+
// Note: This file analyzes what needs to be done but doesn't apply the
19+
// actual relocations - that happens later in InputSection::writeTo().
20+
// Instead, it populates Relocation objects in InputSectionBase::relocations
21+
// and creates necessary synthetic sections (GOT, PLT, etc.).
2222
//
23-
// ELF defines a large number of complex relocations.
24-
//
25-
// The functions in this file analyze relocations and do whatever needs
26-
// to be done. It includes, but not limited to, the following.
27-
//
28-
// - create GOT/PLT entries
29-
// - create new relocations in .dynsym to let the dynamic linker resolve
30-
// them at runtime (since ELF supports dynamic linking, not all
31-
// relocations can be resolved at link-time)
32-
// - create COPY relocs and reserve space in .bss
33-
// - replace expensive relocs (in terms of runtime cost) with cheap ones
34-
// - error out infeasible combinations such as PIC and non-relative relocs
35-
//
36-
// Note that the functions in this file don't actually apply relocations
37-
// because it doesn't know about the output file nor the output file buffer.
38-
// It instead stores Relocation objects to InputSection's Relocations
39-
// vector to let it apply later in InputSection::writeTo.
23+
// In addition, this file implements the core Thunk creation logic, called
24+
// during finalizeAddressDependentContent().
4025
//
4126
//===----------------------------------------------------------------------===//
4227

@@ -466,14 +451,14 @@ class RelocationScanner {
466451
int64_t computeMipsAddend(const RelTy &rel, RelExpr expr, bool isLocal) const;
467452
bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym,
468453
uint64_t relOff) const;
469-
void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym,
470-
int64_t addend) const;
454+
void process(RelExpr expr, RelType type, uint64_t offset, Symbol &sym,
455+
int64_t addend) const;
471456
unsigned handleTlsRelocation(RelExpr expr, RelType type, uint64_t offset,
472457
Symbol &sym, int64_t addend);
473458

474459
template <class ELFT, class RelTy>
475-
void scanOne(typename Relocs<RelTy>::const_iterator &i);
476-
template <class ELFT, class RelTy> void scan(Relocs<RelTy> rels);
460+
void scan(typename Relocs<RelTy>::const_iterator &i);
461+
template <class ELFT, class RelTy> void scanSectionImpl(Relocs<RelTy> rels);
477462
};
478463
} // namespace
479464

@@ -961,7 +946,7 @@ static bool canDefineSymbolInExecutable(Ctx &ctx, Symbol &sym) {
961946
}
962947

963948
// Returns true if a given relocation can be computed at link-time.
964-
// This only handles relocation types expected in processAux.
949+
// This only handles relocation types expected in process().
965950
//
966951
// For instance, we know the offset from a relocation to its target at
967952
// link-time if the relocation is PC-relative and refers a
@@ -1052,8 +1037,8 @@ bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type,
10521037
// sections. Given that it is ro, we will need an extra PT_LOAD. This
10531038
// complicates things for the dynamic linker and means we would have to reserve
10541039
// space for the extra PT_LOAD even if we end up not using it.
1055-
void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
1056-
Symbol &sym, int64_t addend) const {
1040+
void RelocationScanner::process(RelExpr expr, RelType type, uint64_t offset,
1041+
Symbol &sym, int64_t addend) const {
10571042
// If non-ifunc non-preemptible, change PLT to direct call and optimize GOT
10581043
// indirection.
10591044
const bool isIfunc = sym.isGnuIFunc();
@@ -1493,7 +1478,7 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type,
14931478
}
14941479

14951480
template <class ELFT, class RelTy>
1496-
void RelocationScanner::scanOne(typename Relocs<RelTy>::const_iterator &i) {
1481+
void RelocationScanner::scan(typename Relocs<RelTy>::const_iterator &i) {
14971482
const RelTy &rel = *i;
14981483
uint32_t symIndex = rel.getSymbol(ctx.arg.isMips64EL);
14991484
Symbol &sym = sec->getFile<ELFT>()->getSymbol(symIndex);
@@ -1587,7 +1572,7 @@ void RelocationScanner::scanOne(typename Relocs<RelTy>::const_iterator &i) {
15871572
}
15881573

15891574
// Process TLS relocations, including TLS optimizations. Note that
1590-
// R_TPREL and R_TPREL_NEG relocations are resolved in processAux.
1575+
// R_TPREL and R_TPREL_NEG relocations are resolved in process().
15911576
//
15921577
// Some RISCV TLSDESC relocations reference a local NOTYPE symbol,
15931578
// but we need to process them in handleTlsRelocation.
@@ -1599,7 +1584,7 @@ void RelocationScanner::scanOne(typename Relocs<RelTy>::const_iterator &i) {
15991584
}
16001585
}
16011586

1602-
processAux(expr, type, offset, sym, addend);
1587+
process(expr, type, offset, sym, addend);
16031588
}
16041589

16051590
// R_PPC64_TLSGD/R_PPC64_TLSLD is required to mark `bl __tls_get_addr` for
@@ -1642,7 +1627,7 @@ static void checkPPC64TLSRelax(InputSectionBase &sec, Relocs<RelTy> rels) {
16421627
}
16431628

16441629
template <class ELFT, class RelTy>
1645-
void RelocationScanner::scan(Relocs<RelTy> rels) {
1630+
void RelocationScanner::scanSectionImpl(Relocs<RelTy> rels) {
16461631
// Not all relocations end up in Sec->Relocations, but a lot do.
16471632
sec->relocations.reserve(rels.size());
16481633

@@ -1660,12 +1645,12 @@ void RelocationScanner::scan(Relocs<RelTy> rels) {
16601645

16611646
if constexpr (RelTy::IsCrel) {
16621647
for (auto i = rels.begin(); i != rels.end();)
1663-
scanOne<ELFT, RelTy>(i);
1648+
scan<ELFT, RelTy>(i);
16641649
} else {
16651650
// The non-CREL code path has additional check for PPC64 TLS.
16661651
end = static_cast<const void *>(rels.end());
16671652
for (auto i = rels.begin(); i != end;)
1668-
scanOne<ELFT, RelTy>(i);
1653+
scan<ELFT, RelTy>(i);
16691654
}
16701655

16711656
// Sort relocations by offset for more efficient searching for
@@ -1686,11 +1671,11 @@ void RelocationScanner::scanSection(InputSectionBase &s, bool isEH) {
16861671
getter = OffsetGetter(s);
16871672
const RelsOrRelas<ELFT> rels = s.template relsOrRelas<ELFT>(!isEH);
16881673
if (rels.areRelocsCrel())
1689-
scan<ELFT>(rels.crels);
1674+
scanSectionImpl<ELFT>(rels.crels);
16901675
else if (rels.areRelocsRel())
1691-
scan<ELFT>(rels.rels);
1676+
scanSectionImpl<ELFT>(rels.rels);
16921677
else
1693-
scan<ELFT>(rels.relas);
1678+
scanSectionImpl<ELFT>(rels.relas);
16941679
}
16951680

16961681
template <class ELFT> void elf::scanRelocations(Ctx &ctx) {

0 commit comments

Comments
 (0)