Skip to content

Commit defd68d

Browse files
committed
[𝘀𝗽𝗿] changes to main this commit is based on
Created using spr 1.3.5 [skip ci]
1 parent dd36a69 commit defd68d

File tree

11 files changed

+72
-86
lines changed

11 files changed

+72
-86
lines changed

lld/ELF/Arch/Mips.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ template <class ELFT> class MIPS final : public TargetInfo {
4040
};
4141
} // namespace
4242

43+
uint64_t elf::getMipsPageAddr(uint64_t addr) {
44+
return (addr + 0x8000) & ~0xffff;
45+
}
46+
4347
template <class ELFT> MIPS<ELFT>::MIPS(Ctx &ctx) : TargetInfo(ctx) {
4448
gotPltHeaderEntriesNum = 2;
4549
defaultMaxPageSize = 65536;

lld/ELF/Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,8 @@ struct Ctx : CommonLinkerContext {
701701
std::unique_ptr<llvm::TarWriter> tar;
702702
// InputFile for linker created symbols with no source location.
703703
InputFile *internalFile = nullptr;
704+
// Dummy Undefined for relocations without a symbol.
705+
Undefined *dummySym = nullptr;
704706
// True if symbols can be exported (isExported) or preemptible.
705707
bool hasDynsym = false;
706708
// True if SHT_LLVM_SYMPART is used.

lld/ELF/Driver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3138,6 +3138,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
31383138
ctx.symtab->insert(arg->getValue())->traced = true;
31393139

31403140
ctx.internalFile = createInternalFile(ctx, "<internal>");
3141+
ctx.dummySym = make<Undefined>(ctx.internalFile, "", STB_LOCAL, 0, 0);
31413142

31423143
// Handle -u/--undefined before input files. If both a.a and b.so define foo,
31433144
// -u foo a.a b.so will extract a.a.

lld/ELF/InputSection.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,11 @@ uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r,
861861
return ctx.in.mipsGot->getVA() +
862862
ctx.in.mipsGot->getPageEntryOffset(file, *r.sym, a) -
863863
ctx.in.mipsGot->getGp(file);
864+
case RE_MIPS_OSEC_LOCAL_PAGE:
865+
// This is used by the MIPS multi-GOT implementation. It relocates
866+
// addresses of 64kb pages that lie inside the output section that sym is
867+
// a representative for.
868+
return getMipsPageAddr(r.sym->getOutputSection()->addr) + a;
864869
case RE_MIPS_GOT_OFF:
865870
case RE_MIPS_GOT_OFF32:
866871
// In case of MIPS if a GOT relocation has non-zero addend this addend

lld/ELF/Relocations.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -885,10 +885,12 @@ static void addPltEntry(Ctx &ctx, PltSection &plt, GotPltSection &gotPlt,
885885
RelocationBaseSection &rel, RelType type, Symbol &sym) {
886886
plt.addEntry(sym);
887887
gotPlt.addEntry(sym);
888-
rel.addReloc({type, &gotPlt, sym.getGotPltOffset(ctx),
889-
sym.isPreemptible ? DynamicReloc::AgainstSymbol
890-
: DynamicReloc::AddendOnlyWithTargetVA,
891-
sym, 0, R_ABS});
888+
if (sym.isPreemptible)
889+
rel.addReloc({type, &gotPlt, sym.getGotPltOffset(ctx),
890+
DynamicReloc::AgainstSymbol, sym, 0, R_ADDEND});
891+
else
892+
rel.addReloc({type, &gotPlt, sym.getGotPltOffset(ctx),
893+
DynamicReloc::AddendOnly, sym, 0, R_ABS});
892894
}
893895

894896
void elf::addGotEntry(Ctx &ctx, Symbol &sym) {
@@ -899,7 +901,7 @@ void elf::addGotEntry(Ctx &ctx, Symbol &sym) {
899901
if (sym.isPreemptible) {
900902
ctx.mainPart->relaDyn->addReloc({ctx.target->gotRel, ctx.in.got.get(), off,
901903
DynamicReloc::AgainstSymbol, sym, 0,
902-
R_ABS});
904+
R_ADDEND});
903905
return;
904906
}
905907

@@ -921,14 +923,14 @@ static void addGotAuthEntry(Ctx &ctx, Symbol &sym) {
921923
if (sym.isPreemptible) {
922924
ctx.mainPart->relaDyn->addReloc({R_AARCH64_AUTH_GLOB_DAT, ctx.in.got.get(),
923925
off, DynamicReloc::AgainstSymbol, sym, 0,
924-
R_ABS});
926+
R_ADDEND});
925927
return;
926928
}
927929

928930
// Signed GOT requires dynamic relocation.
929931
ctx.in.got->getPartition(ctx).relaDyn->addReloc(
930-
{R_AARCH64_AUTH_RELATIVE, ctx.in.got.get(), off,
931-
DynamicReloc::AddendOnlyWithTargetVA, sym, 0, R_ABS});
932+
{R_AARCH64_AUTH_RELATIVE, ctx.in.got.get(), off, DynamicReloc::AddendOnly,
933+
sym, 0, R_ABS});
932934
}
933935

934936
static void addTpOffsetGotEntry(Ctx &ctx, Symbol &sym) {
@@ -1160,8 +1162,8 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
11601162
part.relrAuthDyn->relocs.push_back({sec, sec->relocs().size() - 1});
11611163
} else {
11621164
part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, sec, offset,
1163-
DynamicReloc::AddendOnlyWithTargetVA, sym,
1164-
addend, R_ABS});
1165+
DynamicReloc::AddendOnly, sym, addend,
1166+
R_ABS});
11651167
}
11661168
return;
11671169
}
@@ -1948,13 +1950,12 @@ void elf::postScanRelocations(Ctx &ctx) {
19481950

19491951
GotSection *got = ctx.in.got.get();
19501952
if (ctx.needsTlsLd.load(std::memory_order_relaxed) && got->addTlsIndex()) {
1951-
static Undefined dummy(ctx.internalFile, "", STB_LOCAL, 0, 0);
19521953
if (ctx.arg.shared)
19531954
ctx.mainPart->relaDyn->addReloc(
19541955
{ctx.target->tlsModuleIndexRel, got, got->getTlsIndexOff()});
19551956
else
19561957
got->addConstant({R_ADDEND, ctx.target->symbolicRel,
1957-
got->getTlsIndexOff(), 1, &dummy});
1958+
got->getTlsIndexOff(), 1, ctx.dummySym});
19581959
}
19591960

19601961
assert(ctx.symAux.size() == 1);

lld/ELF/Relocations.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ enum RelExpr {
110110
RE_MIPS_GOT_LOCAL_PAGE,
111111
RE_MIPS_GOT_OFF,
112112
RE_MIPS_GOT_OFF32,
113+
RE_MIPS_OSEC_LOCAL_PAGE,
113114
RE_MIPS_TLSGD,
114115
RE_MIPS_TLSLD,
115116
RE_PPC32_PLTREL,

lld/ELF/SyntheticSections.cpp

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -769,10 +769,6 @@ void GotSection::writeTo(uint8_t *buf) {
769769
}
770770
}
771771

772-
static uint64_t getMipsPageAddr(uint64_t addr) {
773-
return (addr + 0x8000) & ~0xffff;
774-
}
775-
776772
static uint64_t getMipsPageCount(uint64_t size) {
777773
return (size + 0xfffe) / 0xffff + 1;
778774
}
@@ -786,7 +782,7 @@ void MipsGotSection::addEntry(InputFile &file, Symbol &sym, int64_t addend,
786782
FileGot &g = getGot(file);
787783
if (expr == RE_MIPS_GOT_LOCAL_PAGE) {
788784
if (const OutputSection *os = sym.getOutputSection())
789-
g.pagesMap.insert({os, {}});
785+
g.pagesMap.insert({os, {&sym}});
790786
else
791787
g.local16.insert({{nullptr, getMipsPageAddr(sym.getVA(ctx, addend))}, 0});
792788
} else if (sym.isTls())
@@ -1065,9 +1061,9 @@ void MipsGotSection::build() {
10651061
// for the TP-relative offset as we don't know how much other data will
10661062
// be allocated before us in the static TLS block.
10671063
if (s->isPreemptible || ctx.arg.shared)
1068-
ctx.mainPart->relaDyn->addReloc(
1069-
{ctx.target->tlsGotRel, this, offset,
1070-
DynamicReloc::AgainstSymbolWithTargetVA, *s, 0, R_ABS});
1064+
ctx.mainPart->relaDyn->addReloc({ctx.target->tlsGotRel, this, offset,
1065+
DynamicReloc::AgainstSymbol, *s, 0,
1066+
R_ABS});
10711067
}
10721068
for (std::pair<Symbol *, size_t> &p : got.dynTlsSymbols) {
10731069
Symbol *s = p.first;
@@ -1115,15 +1111,16 @@ void MipsGotSection::build() {
11151111
size_t pageCount = l.second.count;
11161112
for (size_t pi = 0; pi < pageCount; ++pi) {
11171113
uint64_t offset = (l.second.firstIndex + pi) * ctx.arg.wordsize;
1118-
ctx.mainPart->relaDyn->addReloc({ctx.target->relativeRel, this, offset,
1119-
l.first, int64_t(pi * 0x10000)});
1114+
ctx.mainPart->relaDyn->addReloc(
1115+
{ctx.target->relativeRel, this, offset, DynamicReloc::AddendOnly,
1116+
*l.second.repSym, int64_t(pi * 0x10000), RE_MIPS_OSEC_LOCAL_PAGE});
11201117
}
11211118
}
11221119
for (const std::pair<GotEntry, size_t> &p : got.local16) {
11231120
uint64_t offset = p.second * ctx.arg.wordsize;
11241121
ctx.mainPart->relaDyn->addReloc({ctx.target->relativeRel, this, offset,
1125-
DynamicReloc::AddendOnlyWithTargetVA,
1126-
*p.first.first, p.first.second, R_ABS});
1122+
DynamicReloc::AddendOnly, *p.first.first,
1123+
p.first.second, R_ABS});
11271124
}
11281125
}
11291126
}
@@ -1646,24 +1643,10 @@ uint64_t DynamicReloc::getOffset() const {
16461643
}
16471644

16481645
int64_t DynamicReloc::computeAddend(Ctx &ctx) const {
1649-
switch (kind) {
1650-
case AddendOnly:
1651-
assert(sym == nullptr);
1652-
return addend;
1653-
case AgainstSymbol:
1654-
assert(sym != nullptr);
1655-
return addend;
1656-
case AddendOnlyWithTargetVA:
1657-
case AgainstSymbolWithTargetVA: {
1658-
uint64_t ca = inputSec->getRelocTargetVA(
1659-
ctx, Relocation{expr, type, 0, addend, sym}, getOffset());
1660-
return ctx.arg.is64 ? ca : SignExtend64<32>(ca);
1661-
}
1662-
case MipsMultiGotPage:
1663-
assert(sym == nullptr);
1664-
return getMipsPageAddr(outputSec->addr) + addend;
1665-
}
1666-
llvm_unreachable("Unknown DynamicReloc::Kind enum");
1646+
assert(!isFinal && "addend already computed");
1647+
uint64_t ca = inputSec->getRelocTargetVA(
1648+
ctx, Relocation{expr, type, 0, addend, sym}, getOffset());
1649+
return ctx.arg.is64 ? ca : SignExtend64<32>(ca);
16671650
}
16681651

16691652
uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {
@@ -1701,10 +1684,10 @@ void RelocationBaseSection::addAddendOnlyRelocIfNonPreemptible(
17011684
// No need to write an addend to the section for preemptible symbols.
17021685
if (sym.isPreemptible)
17031686
addReloc({dynType, &isec, offsetInSec, DynamicReloc::AgainstSymbol, sym, 0,
1704-
R_ABS});
1687+
R_ADDEND});
17051688
else
1706-
addReloc(DynamicReloc::AddendOnlyWithTargetVA, dynType, isec, offsetInSec,
1707-
sym, 0, R_ABS, addendRelType);
1689+
addReloc(DynamicReloc::AddendOnly, dynType, isec, offsetInSec, sym, 0,
1690+
R_ABS, addendRelType);
17081691
}
17091692

17101693
void RelocationBaseSection::mergeRels() {
@@ -1744,17 +1727,17 @@ void RelocationBaseSection::finalizeContents() {
17441727
}
17451728
}
17461729

1747-
void DynamicReloc::computeRaw(Ctx &ctx, SymbolTableBaseSection *symt) {
1730+
void DynamicReloc::finalize(Ctx &ctx, SymbolTableBaseSection *symt) {
17481731
r_offset = getOffset();
17491732
r_sym = getSymIndex(symt);
17501733
addend = computeAddend(ctx);
1751-
kind = AddendOnly; // Catch errors
1734+
isFinal = true; // Catch errors
17521735
}
17531736

17541737
void RelocationBaseSection::computeRels() {
17551738
SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
17561739
parallelForEach(relocs, [&ctx = ctx, symTab](DynamicReloc &rel) {
1757-
rel.computeRaw(ctx, symTab);
1740+
rel.finalize(ctx, symTab);
17581741
});
17591742

17601743
auto irelative = std::stable_partition(

lld/ELF/SyntheticSections.h

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,11 @@ class MipsGotSection final : public SyntheticSection {
327327
size_t startIndex = 0;
328328

329329
struct PageBlock {
330+
Symbol *repSym; // Representative symbol for the OutputSection
330331
size_t firstIndex;
331332
size_t count;
332-
PageBlock() : firstIndex(0), count(0) {}
333+
PageBlock(Symbol *repSym = nullptr)
334+
: repSym(repSym), firstIndex(0), count(0) {}
333335
};
334336

335337
// Map output sections referenced by MIPS GOT relocations
@@ -419,60 +421,40 @@ class StringTableSection final : public SyntheticSection {
419421
class DynamicReloc {
420422
public:
421423
enum Kind {
422-
/// The resulting dynamic relocation does not reference a symbol (#sym must
423-
/// be nullptr) and uses #addend as the result of computeAddend(ctx).
424-
AddendOnly,
425424
/// The resulting dynamic relocation will not reference a symbol: #sym is
426425
/// only used to compute the addend with InputSection::getRelocTargetVA().
427426
/// Useful for various relative and TLS relocations (e.g. R_X86_64_TPOFF64).
428-
AddendOnlyWithTargetVA,
427+
AddendOnly,
429428
/// The resulting dynamic relocation references symbol #sym from the dynamic
430-
/// symbol table and uses #addend as the value of computeAddend(ctx).
429+
/// symbol table and uses InputSection::getRelocTargetVA() for the final
430+
/// addend.
431431
AgainstSymbol,
432-
/// The resulting dynamic relocation references symbol #sym from the dynamic
433-
/// symbol table and uses InputSection::getRelocTargetVA() + #addend for the
434-
/// final addend. It can be used for relocations that write the symbol VA as
435-
// the addend (e.g. R_MIPS_TLS_TPREL64) but still reference the symbol.
436-
AgainstSymbolWithTargetVA,
437-
/// This is used by the MIPS multi-GOT implementation. It relocates
438-
/// addresses of 64kb pages that lie inside the output section.
439-
MipsMultiGotPage,
440432
};
441-
/// This constructor records a relocation against a symbol.
433+
/// This constructor records a normal relocation.
442434
DynamicReloc(RelType type, const InputSectionBase *inputSec,
443435
uint64_t offsetInSec, Kind kind, Symbol &sym, int64_t addend,
444436
RelExpr expr)
445437
: sym(&sym), inputSec(inputSec), offsetInSec(offsetInSec), type(type),
446-
addend(addend), kind(kind), expr(expr) {}
438+
addend(addend), isAgainstSymbol(kind == AgainstSymbol), isFinal(false),
439+
expr(expr) {}
447440
/// This constructor records a relative relocation with no symbol.
448441
DynamicReloc(RelType type, const InputSectionBase *inputSec,
449442
uint64_t offsetInSec, int64_t addend = 0)
450-
: sym(nullptr), inputSec(inputSec), offsetInSec(offsetInSec), type(type),
451-
addend(addend), kind(AddendOnly), expr(R_ADDEND) {}
452-
/// This constructor records dynamic relocation settings used by the MIPS
453-
/// multi-GOT implementation.
454-
DynamicReloc(RelType type, const InputSectionBase *inputSec,
455-
uint64_t offsetInSec, const OutputSection *outputSec,
456-
int64_t addend)
457-
: sym(nullptr), outputSec(outputSec), inputSec(inputSec),
458-
offsetInSec(offsetInSec), type(type), addend(addend),
459-
kind(MipsMultiGotPage), expr(R_ADDEND) {}
443+
: DynamicReloc(type, inputSec, offsetInSec, AddendOnly,
444+
*inputSec->getCtx().dummySym, addend, R_ADDEND) {}
460445

461446
uint64_t getOffset() const;
462447
uint32_t getSymIndex(SymbolTableBaseSection *symTab) const;
463-
bool needsDynSymIndex() const {
464-
return kind == AgainstSymbol || kind == AgainstSymbolWithTargetVA;
465-
}
448+
bool needsDynSymIndex() const { return isAgainstSymbol; }
466449

467450
/// Computes the addend of the dynamic relocation. Note that this is not the
468451
/// same as the #addend member variable as it may also include the symbol
469452
/// address/the address of the corresponding GOT entry/etc.
470453
int64_t computeAddend(Ctx &) const;
471454

472-
void computeRaw(Ctx &, SymbolTableBaseSection *symt);
455+
void finalize(Ctx &, SymbolTableBaseSection *symt);
473456

474457
Symbol *sym;
475-
const OutputSection *outputSec = nullptr;
476458
const InputSectionBase *inputSec;
477459
uint64_t offsetInSec;
478460
uint64_t r_offset;
@@ -483,7 +465,15 @@ class DynamicReloc {
483465
int64_t addend;
484466

485467
private:
486-
Kind kind;
468+
/// Whether this was constructed with a Kind of AgainstSymbol.
469+
LLVM_PREFERRED_TYPE(bool)
470+
uint8_t isAgainstSymbol : 1;
471+
472+
/// The resulting dynamic relocation has already had its addend computed.
473+
/// Calling computeAddend() is an error.
474+
LLVM_PREFERRED_TYPE(bool)
475+
uint8_t isFinal : 1;
476+
487477
// The kind of expression used to calculate the added (required e.g. for
488478
// relative GOT relocations).
489479
RelExpr expr;
@@ -528,8 +518,8 @@ class RelocationBaseSection : public SyntheticSection {
528518
uint64_t offsetInSec, Symbol &sym, int64_t addend,
529519
RelType addendRelType, RelExpr expr) {
530520
assert(expr != R_ADDEND && "expected non-addend relocation expression");
531-
addReloc<shard>(DynamicReloc::AddendOnlyWithTargetVA, dynType, isec,
532-
offsetInSec, sym, addend, expr, addendRelType);
521+
addReloc<shard>(DynamicReloc::AddendOnly, dynType, isec, offsetInSec, sym,
522+
addend, expr, addendRelType);
533523
}
534524
/// Add a dynamic relocation using the target address of \p sym as the addend
535525
/// if \p sym is non-preemptible. Otherwise add a relocation against \p sym.

lld/ELF/Target.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ ErrorPlace elf::getErrorPlace(Ctx &ctx, const uint8_t *loc) {
105105
if (isecLoc <= loc && loc < isecLoc + isec->getSize()) {
106106
std::string objLoc = isec->getLocation(loc - isecLoc);
107107
// Return object file location and source file location.
108-
Undefined dummy(ctx.internalFile, "", STB_LOCAL, 0, 0);
109108
ELFSyncStream msg(ctx, DiagLevel::None);
110109
if (isec->file)
111-
msg << isec->getSrcMsg(dummy, loc - isecLoc);
110+
msg << isec->getSrcMsg(*ctx.dummySym, loc - isecLoc);
112111
return {isec, objLoc + ": ", std::string(msg.str())};
113112
}
114113
}

lld/ELF/Target.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ void processArmCmseSymbols(Ctx &);
214214
template <class ELFT> uint32_t calcMipsEFlags(Ctx &);
215215
uint8_t getMipsFpAbiFlag(Ctx &, InputFile *file, uint8_t oldFlag,
216216
uint8_t newFlag);
217+
uint64_t getMipsPageAddr(uint64_t addr);
217218
bool isMipsN32Abi(Ctx &, const InputFile &f);
218219
bool isMicroMips(Ctx &);
219220
bool isMipsR6(Ctx &);

0 commit comments

Comments
 (0)