Skip to content

Commit 6dd773b

Browse files
committed
[ELF] Pass Ctx &
1 parent 966bee7 commit 6dd773b

File tree

10 files changed

+61
-51
lines changed

10 files changed

+61
-51
lines changed

lld/ELF/AArch64ErrataFix.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,9 @@ Patch843419Section::Patch843419Section(Ctx &ctx, InputSection *p, uint64_t off)
398398
patchee(p), patcheeOffset(off) {
399399
this->parent = p->getParent();
400400
patchSym = addSyntheticLocal(
401-
saver().save("__CortexA53843419_" + utohexstr(getLDSTAddr())), STT_FUNC,
402-
0, getSize(), *this);
403-
addSyntheticLocal(saver().save("$x"), STT_NOTYPE, 0, 0, *this);
401+
ctx, saver().save("__CortexA53843419_" + utohexstr(getLDSTAddr())),
402+
STT_FUNC, 0, getSize(), *this);
403+
addSyntheticLocal(ctx, saver().save("$x"), STT_NOTYPE, 0, 0, *this);
404404
}
405405

406406
uint64_t Patch843419Section::getLDSTAddr() const {

lld/ELF/ARMErrataFix.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ Patch657417Section::Patch657417Section(Ctx &ctx, InputSection *p, uint64_t off,
141141
patchee(p), patcheeOffset(off), instr(instr), isARM(isARM) {
142142
parent = p->getParent();
143143
patchSym = addSyntheticLocal(
144-
saver().save("__CortexA8657417_" + utohexstr(getBranchAddr())), STT_FUNC,
145-
isARM ? 0 : 1, getSize(), *this);
146-
addSyntheticLocal(saver().save(isARM ? "$a" : "$t"), STT_NOTYPE, 0, 0, *this);
144+
ctx, saver().save("__CortexA8657417_" + utohexstr(getBranchAddr())),
145+
STT_FUNC, isARM ? 0 : 1, getSize(), *this);
146+
addSyntheticLocal(ctx, saver().save(isARM ? "$a" : "$t"), STT_NOTYPE, 0, 0,
147+
*this);
147148
}
148149

149150
uint64_t Patch657417Section::getBranchAddr() const {
@@ -259,6 +260,7 @@ struct ScanResult {
259260
// branch so the minimum offset for a patch is 4.
260261
static ScanResult scanCortexA8Errata657417(InputSection *isec, uint64_t &off,
261262
uint64_t limit) {
263+
Ctx &ctx = isec->getCtx();
262264
uint64_t isecAddr = isec->getVA(0);
263265
// Advance Off so that (isecAddr + off) modulo 0x1000 is at least 0xffa. We
264266
// need to check for a 32-bit instruction immediately before a 32-bit branch

lld/ELF/Arch/ARM.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,11 @@ void ARM::writePltHeader(uint8_t *buf) const {
300300

301301
void ARM::addPltHeaderSymbols(InputSection &isec) const {
302302
if (useThumbPLTs(ctx)) {
303-
addSyntheticLocal("$t", STT_NOTYPE, 0, 0, isec);
304-
addSyntheticLocal("$d", STT_NOTYPE, 12, 0, isec);
303+
addSyntheticLocal(ctx, "$t", STT_NOTYPE, 0, 0, isec);
304+
addSyntheticLocal(ctx, "$d", STT_NOTYPE, 12, 0, isec);
305305
} else {
306-
addSyntheticLocal("$a", STT_NOTYPE, 0, 0, isec);
307-
addSyntheticLocal("$d", STT_NOTYPE, 16, 0, isec);
306+
addSyntheticLocal(ctx, "$a", STT_NOTYPE, 0, 0, isec);
307+
addSyntheticLocal(ctx, "$d", STT_NOTYPE, 16, 0, isec);
308308
}
309309
}
310310

@@ -377,10 +377,10 @@ void ARM::writePlt(uint8_t *buf, const Symbol &sym,
377377

378378
void ARM::addPltSymbols(InputSection &isec, uint64_t off) const {
379379
if (useThumbPLTs(ctx)) {
380-
addSyntheticLocal("$t", STT_NOTYPE, off, 0, isec);
380+
addSyntheticLocal(ctx, "$t", STT_NOTYPE, off, 0, isec);
381381
} else {
382-
addSyntheticLocal("$a", STT_NOTYPE, off, 0, isec);
383-
addSyntheticLocal("$d", STT_NOTYPE, off + 12, 0, isec);
382+
addSyntheticLocal(ctx, "$a", STT_NOTYPE, off, 0, isec);
383+
addSyntheticLocal(ctx, "$d", STT_NOTYPE, off + 12, 0, isec);
384384
}
385385
}
386386

@@ -1397,7 +1397,7 @@ void ArmCmseSGSection::writeTo(uint8_t *buf) {
13971397
}
13981398

13991399
void ArmCmseSGSection::addMappingSymbol() {
1400-
addSyntheticLocal("$t", STT_NOTYPE, /*off=*/0, /*size=*/0, *this);
1400+
addSyntheticLocal(ctx, "$t", STT_NOTYPE, /*off=*/0, /*size=*/0, *this);
14011401
}
14021402

14031403
size_t ArmCmseSGSection::getSize() const {

lld/ELF/Arch/X86_64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ static bool isFallThruRelocation(InputSection &is, InputFile *file,
186186
return false;
187187

188188
uint64_t addrLoc = is.getOutputSection()->addr + is.outSecOff + r.offset;
189-
uint64_t targetOffset = is.getRelocTargetVA(ctx, r, addrLoc);
189+
uint64_t targetOffset = is.getRelocTargetVA(is.getCtx(), r, addrLoc);
190190

191191
// If this jmp is a fall thru, the target offset is the beginning of the
192192
// next section.

lld/ELF/InputFiles.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,8 +1324,8 @@ static bool isBitcodeNonCommonDef(MemoryBufferRef mb, StringRef symName,
13241324
}
13251325

13261326
template <class ELFT>
1327-
static bool isNonCommonDef(ELFKind ekind, MemoryBufferRef mb, StringRef symName,
1328-
StringRef archiveName) {
1327+
static bool isNonCommonDef(Ctx &ctx, ELFKind ekind, MemoryBufferRef mb,
1328+
StringRef symName, StringRef archiveName) {
13291329
ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(ctx, ekind, mb, archiveName);
13301330
obj->init();
13311331
StringRef stringtable = obj->getStringTable();
@@ -1343,13 +1343,13 @@ static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
13431343
StringRef archiveName) {
13441344
switch (getELFKind(mb, archiveName)) {
13451345
case ELF32LEKind:
1346-
return isNonCommonDef<ELF32LE>(ELF32LEKind, mb, symName, archiveName);
1346+
return isNonCommonDef<ELF32LE>(ctx, ELF32LEKind, mb, symName, archiveName);
13471347
case ELF32BEKind:
1348-
return isNonCommonDef<ELF32BE>(ELF32BEKind, mb, symName, archiveName);
1348+
return isNonCommonDef<ELF32BE>(ctx, ELF32BEKind, mb, symName, archiveName);
13491349
case ELF64LEKind:
1350-
return isNonCommonDef<ELF64LE>(ELF64LEKind, mb, symName, archiveName);
1350+
return isNonCommonDef<ELF64LE>(ctx, ELF64LEKind, mb, symName, archiveName);
13511351
case ELF64BEKind:
1352-
return isNonCommonDef<ELF64BE>(ELF64BEKind, mb, symName, archiveName);
1352+
return isNonCommonDef<ELF64BE>(ctx, ELF64BEKind, mb, symName, archiveName);
13531353
default:
13541354
llvm_unreachable("getELFKind");
13551355
}

lld/ELF/InputSection.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ void InputSection::copyRelocations(uint8_t *buf) {
437437
template <class ELFT, class RelTy, class RelIt>
438438
void InputSection::copyRelocations(uint8_t *buf,
439439
llvm::iterator_range<RelIt> rels) {
440-
const TargetInfo &target = *elf::ctx.target;
440+
Ctx &ctx = getCtx();
441+
const TargetInfo &target = *ctx.target;
441442
InputSectionBase *sec = getRelocatedSection();
442443
(void)sec->contentMaybeDecompress(); // uncompress if needed
443444

lld/ELF/LinkerScript.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,8 @@ static OutputSection *findByName(ArrayRef<SectionCommand *> vec,
861861
return nullptr;
862862
}
863863

864-
static OutputDesc *createSection(InputSectionBase *isec, StringRef outsecName) {
864+
static OutputDesc *createSection(Ctx &ctx, InputSectionBase *isec,
865+
StringRef outsecName) {
865866
OutputDesc *osd = ctx.script->createOutputSection(outsecName, "<internal>");
866867
osd->osec.recordSection(isec);
867868
return osd;
@@ -878,7 +879,7 @@ static OutputDesc *addInputSec(Ctx &ctx,
878879
// as-is because adding/removing members or merging them with other groups
879880
// change their semantics.
880881
if (isec->type == SHT_GROUP || (isec->flags & SHF_GROUP))
881-
return createSection(isec, outsecName);
882+
return createSection(ctx, isec, outsecName);
882883

883884
// Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
884885
// relocation sections .rela.foo and .rela.bar for example. Most tools do
@@ -895,7 +896,7 @@ static OutputDesc *addInputSec(Ctx &ctx,
895896
return nullptr;
896897
}
897898

898-
OutputDesc *osd = createSection(isec, outsecName);
899+
OutputDesc *osd = createSection(ctx, isec, outsecName);
899900
out->relocationSection = &osd->osec;
900901
return osd;
901902
}
@@ -966,7 +967,7 @@ static OutputDesc *addInputSec(Ctx &ctx,
966967
return nullptr;
967968
}
968969

969-
OutputDesc *osd = createSection(isec, outsecName);
970+
OutputDesc *osd = createSection(ctx, isec, outsecName);
970971
v.push_back(&osd->osec);
971972
return osd;
972973
}
@@ -982,7 +983,7 @@ void LinkerScript::addOrphanSections() {
982983

983984
StringRef name = getOutputSectionName(s);
984985
if (ctx.arg.unique) {
985-
v.push_back(createSection(s, name));
986+
v.push_back(createSection(ctx, s, name));
986987
} else if (OutputSection *sec = findByName(sectionCommands, name)) {
987988
sec->recordSection(s);
988989
} else {

lld/ELF/SyntheticSections.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static uint64_t readUint(Ctx &ctx, uint8_t *buf) {
6363
return ctx.arg.is64 ? read64(buf) : read32(buf);
6464
}
6565

66-
static void writeUint(uint8_t *buf, uint64_t val) {
66+
static void writeUint(Ctx &ctx, uint8_t *buf, uint64_t val) {
6767
if (ctx.arg.is64)
6868
write64(buf, val);
6969
else
@@ -264,7 +264,7 @@ MipsReginfoSection<ELFT>::create(Ctx &ctx) {
264264
return std::make_unique<MipsReginfoSection<ELFT>>(ctx, reginfo);
265265
}
266266

267-
InputSection *elf::createInterpSection(Ctx &) {
267+
InputSection *elf::createInterpSection(Ctx &ctx) {
268268
// StringSaver guarantees that the returned string ends with '\0'.
269269
StringRef s = saver().save(ctx.arg.dynamicLinker);
270270
ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1};
@@ -273,8 +273,9 @@ InputSection *elf::createInterpSection(Ctx &) {
273273
contents, ".interp");
274274
}
275275

276-
Defined *elf::addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,
277-
uint64_t size, InputSectionBase &section) {
276+
Defined *elf::addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type,
277+
uint64_t value, uint64_t size,
278+
InputSectionBase &section) {
278279
Defined *s = makeDefined(section.file, name, STB_LOCAL, STV_DEFAULT, type,
279280
value, size, &section);
280281
if (ctx.in.symTab)
@@ -1117,13 +1118,14 @@ void MipsGotSection::writeTo(uint8_t *buf) {
11171118
// we've been doing this for years, it is probably a safe bet to
11181119
// keep doing this for now. We really need to revisit this to see
11191120
// if we had to do this.
1120-
writeUint(buf + ctx.arg.wordsize, (uint64_t)1 << (ctx.arg.wordsize * 8 - 1));
1121+
writeUint(ctx, buf + ctx.arg.wordsize,
1122+
(uint64_t)1 << (ctx.arg.wordsize * 8 - 1));
11211123
for (const FileGot &g : gots) {
11221124
auto write = [&](size_t i, const Symbol *s, int64_t a) {
11231125
uint64_t va = a;
11241126
if (s)
11251127
va = s->getVA(a);
1126-
writeUint(buf + i * ctx.arg.wordsize, va);
1128+
writeUint(ctx, buf + i * ctx.arg.wordsize, va);
11271129
};
11281130
// Write 'page address' entries to the local part of the GOT.
11291131
for (const std::pair<const OutputSection *, FileGot::PageBlock> &l :
@@ -1304,7 +1306,7 @@ DynamicSection<ELFT>::DynamicSection(Ctx &ctx)
13041306
// .rela.dyn
13051307
//
13061308
// DT_RELASZ is the total size of the included sections.
1307-
static uint64_t addRelaSz(const RelocationBaseSection &relaDyn) {
1309+
static uint64_t addRelaSz(Ctx &ctx, const RelocationBaseSection &relaDyn) {
13081310
size_t size = relaDyn.getSize();
13091311
if (ctx.in.relaPlt->getParent() == relaDyn.getParent())
13101312
size += ctx.in.relaPlt->getSize();
@@ -1315,7 +1317,7 @@ static uint64_t addRelaSz(const RelocationBaseSection &relaDyn) {
13151317
// output section. When this occurs we cannot just use the OutputSection
13161318
// Size. Moreover the [DT_JMPREL, DT_JMPREL + DT_PLTRELSZ) is permitted to
13171319
// overlap with the [DT_RELA, DT_RELA + DT_RELASZ).
1318-
static uint64_t addPltRelSz() { return ctx.in.relaPlt->getSize(); }
1320+
static uint64_t addPltRelSz(Ctx &ctx) { return ctx.in.relaPlt->getSize(); }
13191321

13201322
// Add remaining entries to complete .dynamic contents.
13211323
template <class ELFT>
@@ -1405,7 +1407,7 @@ DynamicSection<ELFT>::computeContents() {
14051407
if (part.relaDyn->isNeeded()) {
14061408
addInSec(part.relaDyn->dynamicTag, *part.relaDyn);
14071409
entries.emplace_back(part.relaDyn->sizeDynamicTag,
1408-
addRelaSz(*part.relaDyn));
1410+
addRelaSz(ctx, *part.relaDyn));
14091411

14101412
bool isRela = ctx.arg.isRela;
14111413
addInt(isRela ? DT_RELAENT : DT_RELENT,
@@ -1437,7 +1439,7 @@ DynamicSection<ELFT>::computeContents() {
14371439
}
14381440
if (isMain && ctx.in.relaPlt->isNeeded()) {
14391441
addInSec(DT_JMPREL, *ctx.in.relaPlt);
1440-
entries.emplace_back(DT_PLTRELSZ, addPltRelSz());
1442+
entries.emplace_back(DT_PLTRELSZ, addPltRelSz(ctx));
14411443
switch (ctx.arg.emachine) {
14421444
case EM_MIPS:
14431445
addInSec(DT_MIPS_PLTGOT, *ctx.in.gotPlt);
@@ -2126,15 +2128,18 @@ SymbolTableBaseSection::SymbolTableBaseSection(Ctx &ctx,
21262128
// See "Global Offset Table" in Chapter 5 in the following document
21272129
// for detailed description:
21282130
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2129-
static bool sortMipsSymbols(const SymbolTableEntry &l,
2130-
const SymbolTableEntry &r) {
2131-
// Sort entries related to non-local preemptible symbols by GOT indexes.
2132-
// All other entries go to the beginning of a dynsym in arbitrary order.
2133-
if (l.sym->isInGot(ctx) && r.sym->isInGot(ctx))
2134-
return l.sym->getGotIdx(ctx) < r.sym->getGotIdx(ctx);
2135-
if (!l.sym->isInGot(ctx) && !r.sym->isInGot(ctx))
2136-
return false;
2137-
return !l.sym->isInGot(ctx);
2131+
static void sortMipsSymbols(Ctx &ctx, SmallVector<SymbolTableEntry, 0> &syms) {
2132+
llvm::stable_sort(syms,
2133+
[&](const SymbolTableEntry &l, const SymbolTableEntry &r) {
2134+
// Sort entries related to non-local preemptible symbols
2135+
// by GOT indexes. All other entries go to the beginning
2136+
// of a dynsym in arbitrary order.
2137+
if (l.sym->isInGot(ctx) && r.sym->isInGot(ctx))
2138+
return l.sym->getGotIdx(ctx) < r.sym->getGotIdx(ctx);
2139+
if (!l.sym->isInGot(ctx) && !r.sym->isInGot(ctx))
2140+
return false;
2141+
return !l.sym->isInGot(ctx);
2142+
});
21382143
}
21392144

21402145
void SymbolTableBaseSection::finalizeContents() {
@@ -2157,7 +2162,7 @@ void SymbolTableBaseSection::finalizeContents() {
21572162
// NB: It also sorts Symbols to meet the GNU hash table requirements.
21582163
getPartition().gnuHashTab->addSymbols(symbols);
21592164
} else if (ctx.arg.emachine == EM_MIPS) {
2160-
llvm::stable_sort(symbols, sortMipsSymbols);
2165+
sortMipsSymbols(ctx, symbols);
21612166
}
21622167

21632168
// Only the main partition's dynsym indexes are stored in the symbols
@@ -2440,7 +2445,7 @@ void GnuHashTableSection::writeTo(uint8_t *buf) {
24402445
uint64_t val = readUint(ctx, buf + i * ctx.arg.wordsize);
24412446
val |= uint64_t(1) << (sym.hash % c);
24422447
val |= uint64_t(1) << ((sym.hash >> Shift2) % c);
2443-
writeUint(buf + i * ctx.arg.wordsize, val);
2448+
writeUint(ctx, buf + i * ctx.arg.wordsize, val);
24442449
}
24452450
buf += ctx.arg.wordsize * maskWords;
24462451

lld/ELF/SyntheticSections.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,8 +1439,9 @@ bool canHaveMemtagGlobals(Ctx &);
14391439
template <typename ELFT> void writeEhdr(uint8_t *buf, Partition &part);
14401440
template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part);
14411441

1442-
Defined *addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,
1443-
uint64_t size, InputSectionBase &section);
1442+
Defined *addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type,
1443+
uint64_t value, uint64_t size,
1444+
InputSectionBase &section);
14441445

14451446
void addVerneed(Symbol *ss);
14461447

lld/ELF/Thunks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {
537537

538538
Defined *Thunk::addSymbol(StringRef name, uint8_t type, uint64_t value,
539539
InputSectionBase &section) {
540-
Defined *d = addSyntheticLocal(name, type, value, /*size=*/0, section);
540+
Defined *d = addSyntheticLocal(ctx, name, type, value, /*size=*/0, section);
541541
syms.push_back(d);
542542
return d;
543543
}

0 commit comments

Comments
 (0)