Skip to content

Commit 7e0008d

Browse files
authored
[LLD][COFF][NFC] Create import thunks in ImportFile::parse. (#107929)
1 parent 7041163 commit 7e0008d

File tree

6 files changed

+27
-21
lines changed

6 files changed

+27
-21
lines changed

lld/COFF/InputFiles.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,20 @@ MachineTypes ImportFile::getMachineType() const {
10091009
return MachineTypes(machine);
10101010
}
10111011

1012+
ImportThunkChunk *ImportFile::makeImportThunk() {
1013+
switch (hdr->Machine) {
1014+
case AMD64:
1015+
return make<ImportThunkChunkX64>(ctx, impSym);
1016+
case I386:
1017+
return make<ImportThunkChunkX86>(ctx, impSym);
1018+
case ARM64:
1019+
return make<ImportThunkChunkARM64>(ctx, impSym);
1020+
case ARMNT:
1021+
return make<ImportThunkChunkARM>(ctx, impSym);
1022+
}
1023+
llvm_unreachable("unknown machine type");
1024+
}
1025+
10121026
void ImportFile::parse() {
10131027
const auto *hdr =
10141028
reinterpret_cast<const coff_import_header *>(mb.getBufferStart());
@@ -1069,9 +1083,10 @@ void ImportFile::parse() {
10691083
// DLL functions just like regular non-DLL functions.)
10701084
if (hdr->getType() == llvm::COFF::IMPORT_CODE) {
10711085
if (ctx.config.machine != ARM64EC) {
1072-
thunkSym = ctx.symtab.addImportThunk(name, impSym, hdr->Machine);
1086+
thunkSym = ctx.symtab.addImportThunk(name, impSym, makeImportThunk());
10731087
} else {
1074-
thunkSym = ctx.symtab.addImportThunk(name, impSym, AMD64);
1088+
thunkSym = ctx.symtab.addImportThunk(
1089+
name, impSym, make<ImportThunkChunkX64>(ctx, impSym));
10751090
// FIXME: Add aux IAT symbols.
10761091
}
10771092
}

lld/COFF/InputFiles.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Defined;
5555
class DefinedImportData;
5656
class DefinedImportThunk;
5757
class DefinedRegular;
58+
class ImportThunkChunk;
5859
class SectionChunk;
5960
class Symbol;
6061
class Undefined;
@@ -352,6 +353,7 @@ class ImportFile : public InputFile {
352353

353354
private:
354355
void parse() override;
356+
ImportThunkChunk *makeImportThunk();
355357

356358
public:
357359
StringRef externalName;

lld/COFF/SymbolTable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,11 +784,11 @@ DefinedImportData *SymbolTable::addImportData(StringRef n, ImportFile *f) {
784784
}
785785

786786
Symbol *SymbolTable::addImportThunk(StringRef name, DefinedImportData *id,
787-
uint16_t machine) {
787+
ImportThunkChunk *chunk) {
788788
auto [s, wasInserted] = insert(name, nullptr);
789789
s->isUsedInRegularObj = true;
790790
if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
791-
replaceSymbol<DefinedImportThunk>(s, ctx, name, id, machine);
791+
replaceSymbol<DefinedImportThunk>(s, ctx, name, id, chunk);
792792
return s;
793793
}
794794

lld/COFF/SymbolTable.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class COFFLinkerContext;
2828
class Defined;
2929
class DefinedAbsolute;
3030
class DefinedRegular;
31+
class ImportThunkChunk;
3132
class LazyArchive;
3233
class SectionChunk;
3334
class Symbol;
@@ -104,7 +105,7 @@ class SymbolTable {
104105
CommonChunk *c = nullptr);
105106
DefinedImportData *addImportData(StringRef n, ImportFile *f);
106107
Symbol *addImportThunk(StringRef name, DefinedImportData *s,
107-
uint16_t machine);
108+
ImportThunkChunk *chunk);
108109
void addLibcall(StringRef name);
109110
void addEntryThunk(Symbol *from, Symbol *to);
110111
void initializeEntryThunks();

lld/COFF/Symbols.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,10 @@ COFFSymbolRef DefinedCOFF::getCOFFSymbol() {
107107

108108
uint64_t DefinedAbsolute::getRVA() { return va - ctx.config.imageBase; }
109109

110-
static Chunk *makeImportThunk(COFFLinkerContext &ctx, DefinedImportData *s,
111-
uint16_t machine) {
112-
if (machine == AMD64)
113-
return make<ImportThunkChunkX64>(ctx, s);
114-
if (machine == I386)
115-
return make<ImportThunkChunkX86>(ctx, s);
116-
if (machine == ARM64)
117-
return make<ImportThunkChunkARM64>(ctx, s);
118-
assert(machine == ARMNT);
119-
return make<ImportThunkChunkARM>(ctx, s);
120-
}
121-
122110
DefinedImportThunk::DefinedImportThunk(COFFLinkerContext &ctx, StringRef name,
123-
DefinedImportData *s, uint16_t machine)
124-
: Defined(DefinedImportThunkKind, name), wrappedSym(s),
125-
data(makeImportThunk(ctx, s, machine)) {}
111+
DefinedImportData *s,
112+
ImportThunkChunk *chunk)
113+
: Defined(DefinedImportThunkKind, name), wrappedSym(s), data(chunk) {}
126114

127115
Defined *Undefined::getWeakAlias() {
128116
// A weak alias may be a weak alias to another symbol, so check recursively.

lld/COFF/Symbols.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ class DefinedImportData : public Defined {
388388
class DefinedImportThunk : public Defined {
389389
public:
390390
DefinedImportThunk(COFFLinkerContext &ctx, StringRef name,
391-
DefinedImportData *s, uint16_t machine);
391+
DefinedImportData *s, ImportThunkChunk *chunk);
392392

393393
static bool classof(const Symbol *s) {
394394
return s->kind() == DefinedImportThunkKind;

0 commit comments

Comments
 (0)