Skip to content

Commit b5be054

Browse files
committed
[LLD][COFF][NFC] Store live flag in ImportThunkChunk.
Instead of ImportFile. This is a preparation for ARM64EC support, which has both x86 and ARM64EC thunks and each of them needs a separate flag.
1 parent 82a3646 commit b5be054

File tree

10 files changed

+19
-15
lines changed

10 files changed

+19
-15
lines changed

lld/COFF/Chunks.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,10 @@ void StringChunk::writeTo(uint8_t *buf) const {
774774
buf[str.size()] = '\0';
775775
}
776776

777+
ImportThunkChunk::ImportThunkChunk(COFFLinkerContext &ctx, Defined *s)
778+
: NonSectionCodeChunk(ImportThunkKind), live(!ctx.config.doGC),
779+
impSymbol(s), ctx(ctx) {}
780+
777781
ImportThunkChunkX64::ImportThunkChunkX64(COFFLinkerContext &ctx, Defined *s)
778782
: ImportThunkChunk(ctx, s) {
779783
// Intel Optimization Manual says that all branch targets

lld/COFF/Chunks.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,13 @@ static const uint8_t importThunkARM64EC[] = {
557557
// contents will be a JMP instruction to some __imp_ symbol.
558558
class ImportThunkChunk : public NonSectionCodeChunk {
559559
public:
560-
ImportThunkChunk(COFFLinkerContext &ctx, Defined *s)
561-
: NonSectionCodeChunk(ImportThunkKind), impSymbol(s), ctx(ctx) {}
560+
ImportThunkChunk(COFFLinkerContext &ctx, Defined *s);
562561
static bool classof(const Chunk *c) { return c->kind() == ImportThunkKind; }
563562

563+
// We track the usage of the thunk symbol separately from the import file
564+
// to avoid generating unnecessary thunks.
565+
bool live;
566+
564567
protected:
565568
Defined *impSymbol;
566569
COFFLinkerContext &ctx;

lld/COFF/InputFiles.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ void ObjFile::enqueuePdbFile(StringRef path, ObjFile *fromFile) {
10021002
}
10031003

10041004
ImportFile::ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m)
1005-
: InputFile(ctx, ImportKind, m), live(!ctx.config.doGC), thunkLive(live) {}
1005+
: InputFile(ctx, ImportKind, m), live(!ctx.config.doGC) {}
10061006

10071007
MachineTypes ImportFile::getMachineType() const {
10081008
uint16_t machine =

lld/COFF/InputFiles.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,8 @@ class ImportFile : public InputFile {
371371
// are actually in use.
372372
//
373373
// If the Live bit is turned off by MarkLive, Writer will ignore dllimported
374-
// symbols provided by this import library member. We also track whether the
375-
// imported symbol is used separately from whether the thunk is used in order
376-
// to avoid creating unnecessary thunks.
374+
// symbols provided by this import library member.
377375
bool live;
378-
bool thunkLive;
379376
};
380377

381378
// Used for LTO.

lld/COFF/MapFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static void getSymbols(const COFFLinkerContext &ctx,
125125
if (!file->thunkSym)
126126
continue;
127127

128-
if (!file->thunkLive)
128+
if (!file->thunkSym->isLive())
129129
continue;
130130

131131
if (auto *thunkSym = dyn_cast<Defined>(file->thunkSym))

lld/COFF/MarkLive.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void markLive(COFFLinkerContext &ctx) {
5858
addImportFile(sym->file);
5959
} else if (auto *sym = dyn_cast<DefinedImportThunk>(b)) {
6060
addImportFile(sym->wrappedSym->file);
61-
sym->wrappedSym->file->thunkLive = true;
61+
sym->getChunk()->live = true;
6262
}
6363
};
6464

lld/COFF/PDB.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,8 +1527,8 @@ void PDBLinker::addImportFilesToPDB() {
15271527
if (!file->thunkSym)
15281528
continue;
15291529

1530-
if (!file->thunkLive)
1531-
continue;
1530+
if (!file->thunkSym->isLive())
1531+
continue;
15321532

15331533
std::string dll = StringRef(file->dllName).lower();
15341534
llvm::pdb::DbiModuleDescriptorBuilder *&mod = dllToModuleDbi[dll];

lld/COFF/Symbols.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ bool Symbol::isLive() const {
8484
if (auto *imp = dyn_cast<DefinedImportData>(this))
8585
return imp->file->live;
8686
if (auto *imp = dyn_cast<DefinedImportThunk>(this))
87-
return imp->wrappedSym->file->thunkLive;
87+
return imp->getChunk()->live;
8888
// Assume any other kind of symbol is live.
8989
return true;
9090
}

lld/COFF/Symbols.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,12 @@ class DefinedImportThunk : public Defined {
395395
}
396396

397397
uint64_t getRVA() { return data->getRVA(); }
398-
Chunk *getChunk() { return data; }
398+
ImportThunkChunk *getChunk() const { return data; }
399399

400400
DefinedImportData *wrappedSym;
401401

402402
private:
403-
Chunk *data;
403+
ImportThunkChunk *data;
404404
};
405405

406406
// If you have a symbol "foo" in your object file, a symbol name

lld/COFF/Writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ void Writer::appendImportThunks() {
12581258
if (!isa<DefinedImportThunk>(file->thunkSym))
12591259
fatal(toString(ctx, *file->thunkSym) + " was replaced");
12601260
DefinedImportThunk *thunk = cast<DefinedImportThunk>(file->thunkSym);
1261-
if (file->thunkLive)
1261+
if (thunk->getChunk()->live)
12621262
textSec->addChunk(thunk->getChunk());
12631263
if (file->impchkThunk)
12641264
textSec->addChunk(file->impchkThunk);

0 commit comments

Comments
 (0)