Skip to content

Commit 6b493ba

Browse files
authored
[LLD][COFF] Store reference to SymbolTable instead of COFFLinkerContext in InputFile (NFC) (#119296)
This change prepares for the introduction of separate hybrid namespaces. Hybrid images will require two `SymbolTable` instances, making it necessary to associate `InputFile` objects with the relevant one.
1 parent eb1f9cc commit 6b493ba

File tree

9 files changed

+135
-124
lines changed

9 files changed

+135
-124
lines changed

lld/COFF/Chunks.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ SectionChunk::SectionChunk(ObjFile *f, const coff_section *h, Kind k)
5656
// files will be built with -ffunction-sections or /Gy, so most things worth
5757
// stripping will be in a comdat.
5858
if (file)
59-
live = !file->ctx.config.doGC || !isCOMDAT();
59+
live = !file->symtab.ctx.config.doGC || !isCOMDAT();
6060
else
6161
live = true;
6262
}
@@ -129,7 +129,7 @@ void SectionChunk::applyRelX64(uint8_t *off, uint16_t type, OutputSection *os,
129129
case IMAGE_REL_AMD64_REL32_4: add32(off, s - p - 8); break;
130130
case IMAGE_REL_AMD64_REL32_5: add32(off, s - p - 9); break;
131131
case IMAGE_REL_AMD64_SECTION:
132-
applySecIdx(off, os, file->ctx.outputSections.size());
132+
applySecIdx(off, os, file->symtab.ctx.outputSections.size());
133133
break;
134134
case IMAGE_REL_AMD64_SECREL: applySecRel(this, off, os, s); break;
135135
default:
@@ -149,7 +149,7 @@ void SectionChunk::applyRelX86(uint8_t *off, uint16_t type, OutputSection *os,
149149
case IMAGE_REL_I386_DIR32NB: add32(off, s); break;
150150
case IMAGE_REL_I386_REL32: add32(off, s - p - 4); break;
151151
case IMAGE_REL_I386_SECTION:
152-
applySecIdx(off, os, file->ctx.outputSections.size());
152+
applySecIdx(off, os, file->symtab.ctx.outputSections.size());
153153
break;
154154
case IMAGE_REL_I386_SECREL: applySecRel(this, off, os, s); break;
155155
default:
@@ -225,7 +225,7 @@ void SectionChunk::applyRelARM(uint8_t *off, uint16_t type, OutputSection *os,
225225
case IMAGE_REL_ARM_BRANCH24T: applyBranch24T(off, sx - p - 4); break;
226226
case IMAGE_REL_ARM_BLX23T: applyBranch24T(off, sx - p - 4); break;
227227
case IMAGE_REL_ARM_SECTION:
228-
applySecIdx(off, os, file->ctx.outputSections.size());
228+
applySecIdx(off, os, file->symtab.ctx.outputSections.size());
229229
break;
230230
case IMAGE_REL_ARM_SECREL: applySecRel(this, off, os, s); break;
231231
case IMAGE_REL_ARM_REL32: add32(off, sx - p - 4); break;
@@ -346,7 +346,7 @@ void SectionChunk::applyRelARM64(uint8_t *off, uint16_t type, OutputSection *os,
346346
case IMAGE_REL_ARM64_SECREL_HIGH12A: applySecRelHigh12A(this, off, os, s); break;
347347
case IMAGE_REL_ARM64_SECREL_LOW12L: applySecRelLdr(this, off, os, s); break;
348348
case IMAGE_REL_ARM64_SECTION:
349-
applySecIdx(off, os, file->ctx.outputSections.size());
349+
applySecIdx(off, os, file->symtab.ctx.outputSections.size());
350350
break;
351351
case IMAGE_REL_ARM64_REL32: add32(off, s - p - 4); break;
352352
default:
@@ -371,12 +371,12 @@ static void maybeReportRelocationToDiscarded(const SectionChunk *fromChunk,
371371
ObjFile *file = fromChunk->file;
372372
std::string name;
373373
if (sym) {
374-
name = toString(file->ctx, *sym);
374+
name = toString(file->symtab.ctx, *sym);
375375
} else {
376376
COFFSymbolRef coffSym =
377377
check(file->getCOFFObj()->getSymbol(rel.SymbolTableIndex));
378378
name = maybeDemangleSymbol(
379-
file->ctx, check(file->getCOFFObj()->getSymbolName(coffSym)));
379+
file->symtab.ctx, check(file->getCOFFObj()->getSymbolName(coffSym)));
380380
}
381381

382382
std::vector<std::string> symbolLocations =
@@ -428,23 +428,24 @@ void SectionChunk::applyRelocation(uint8_t *off,
428428
// section is needed to compute SECREL and SECTION relocations used in debug
429429
// info.
430430
Chunk *c = sym ? sym->getChunk() : nullptr;
431-
OutputSection *os = c ? file->ctx.getOutputSection(c) : nullptr;
431+
COFFLinkerContext &ctx = file->symtab.ctx;
432+
OutputSection *os = c ? ctx.getOutputSection(c) : nullptr;
432433

433434
// Skip the relocation if it refers to a discarded section, and diagnose it
434435
// as an error if appropriate. If a symbol was discarded early, it may be
435436
// null. If it was discarded late, the output section will be null, unless
436437
// it was an absolute or synthetic symbol.
437438
if (!sym ||
438439
(!os && !isa<DefinedAbsolute>(sym) && !isa<DefinedSynthetic>(sym))) {
439-
maybeReportRelocationToDiscarded(this, sym, rel, file->ctx.config.mingw);
440+
maybeReportRelocationToDiscarded(this, sym, rel, ctx.config.mingw);
440441
return;
441442
}
442443

443444
uint64_t s = sym->getRVA();
444445

445446
// Compute the RVA of the relocation for relative relocations.
446447
uint64_t p = rva + rel.VirtualAddress;
447-
uint64_t imageBase = file->ctx.config.imageBase;
448+
uint64_t imageBase = ctx.config.imageBase;
448449
switch (getArch()) {
449450
case Triple::x86_64:
450451
applyRelX64(off, rel.Type, os, s, p, imageBase);
@@ -670,7 +671,7 @@ void SectionChunk::getRuntimePseudoRelocs(
670671
toString(file));
671672
continue;
672673
}
673-
int addressSizeInBits = file->ctx.config.is64() ? 64 : 32;
674+
int addressSizeInBits = file->symtab.ctx.config.is64() ? 64 : 32;
674675
if (sizeInBits < addressSizeInBits) {
675676
warn("runtime pseudo relocation in " + toString(file) + " against " +
676677
"symbol " + target->getName() + " is too narrow (only " +
@@ -1099,7 +1100,7 @@ void CHPERedirectionChunk::writeTo(uint8_t *buf) const {
10991100
}
11001101

11011102
ImportThunkChunkARM64EC::ImportThunkChunkARM64EC(ImportFile *file)
1102-
: ImportThunkChunk(file->ctx, file->impSym), file(file) {}
1103+
: ImportThunkChunk(file->symtab.ctx, file->impSym), file(file) {}
11031104

11041105
size_t ImportThunkChunkARM64EC::getSize() const {
11051106
if (!extended)
@@ -1123,7 +1124,7 @@ void ImportThunkChunkARM64EC::writeTo(uint8_t *buf) const {
11231124
applyArm64Addr(buf + 8, exitThunkRVA, rva + 8, 12);
11241125
applyArm64Imm(buf + 12, exitThunkRVA & 0xfff, 0);
11251126

1126-
Defined *helper = cast<Defined>(file->ctx.config.arm64ECIcallHelper);
1127+
Defined *helper = cast<Defined>(file->symtab.ctx.config.arm64ECIcallHelper);
11271128
if (extended) {
11281129
// Replace last instruction with an inline range extension thunk.
11291130
memcpy(buf + 16, arm64Thunk, sizeof(arm64Thunk));
@@ -1137,7 +1138,7 @@ void ImportThunkChunkARM64EC::writeTo(uint8_t *buf) const {
11371138
bool ImportThunkChunkARM64EC::verifyRanges() {
11381139
if (extended)
11391140
return true;
1140-
auto helper = cast<Defined>(file->ctx.config.arm64ECIcallHelper);
1141+
auto helper = cast<Defined>(file->symtab.ctx.config.arm64ECIcallHelper);
11411142
return isInt<28>(helper->getRVA() - rva - 16);
11421143
}
11431144

lld/COFF/DLL.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,14 @@ class AuxImportChunk : public NonSectionChunk {
160160
void writeTo(uint8_t *buf) const override {
161161
uint64_t impchkVA = 0;
162162
if (file->impchkThunk)
163-
impchkVA = file->impchkThunk->getRVA() + file->ctx.config.imageBase;
163+
impchkVA =
164+
file->impchkThunk->getRVA() + file->symtab.ctx.config.imageBase;
164165
write64le(buf, impchkVA);
165166
}
166167

167168
void getBaserels(std::vector<Baserel> *res) override {
168169
if (file->impchkThunk)
169-
res->emplace_back(rva, file->ctx.config.machine);
170+
res->emplace_back(rva, file->symtab.ctx.config.machine);
170171
}
171172

172173
private:

lld/COFF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ void LinkerDriver::addBuffer(std::unique_ptr<MemoryBuffer> mb,
241241
break;
242242
case file_magic::pecoff_executable:
243243
if (ctx.config.mingw) {
244-
ctx.symtab.addFile(make<DLLFile>(ctx, mbref));
244+
ctx.symtab.addFile(make<DLLFile>(ctx.symtab, mbref));
245245
break;
246246
}
247247
if (filename.ends_with_insensitive(".dll")) {

0 commit comments

Comments
 (0)