Skip to content

Commit 44fcd77

Browse files
committed
[LLD][COFF][NFC] Create import thunks in ImportFile::parse.
1 parent 80c47ad commit 44fcd77

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

lld/COFF/InputFiles.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,9 +1069,27 @@ void ImportFile::parse() {
10691069
// DLL functions just like regular non-DLL functions.)
10701070
if (hdr->getType() == llvm::COFF::IMPORT_CODE) {
10711071
if (ctx.config.machine != ARM64EC) {
1072-
thunkSym = ctx.symtab.addImportThunk(name, impSym, hdr->Machine);
1072+
ImportThunkChunk *chunk;
1073+
switch (hdr->Machine) {
1074+
case AMD64:
1075+
chunk = make<ImportThunkChunkX64>(ctx, impSym);
1076+
break;
1077+
case I386:
1078+
chunk = make<ImportThunkChunkX86>(ctx, impSym);
1079+
break;
1080+
case ARM64:
1081+
chunk = make<ImportThunkChunkARM64>(ctx, impSym);
1082+
break;
1083+
case ARMNT:
1084+
chunk = make<ImportThunkChunkARM>(ctx, impSym);
1085+
break;
1086+
default:
1087+
llvm_unreachable("unknown machine type");
1088+
}
1089+
thunkSym = ctx.symtab.addImportThunk(name, impSym, chunk);
10731090
} else {
1074-
thunkSym = ctx.symtab.addImportThunk(name, impSym, AMD64);
1091+
thunkSym = ctx.symtab.addImportThunk(
1092+
name, impSym, make<ImportThunkChunkX64>(ctx, impSym));
10751093
// FIXME: Add aux IAT symbols.
10761094
}
10771095
}

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)