-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[LLD][COFF][NFC] Create import thunks in ImportFile::parse. #107929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
In preparation for ARM64EC thunks. |
@llvm/pr-subscribers-platform-windows @llvm/pr-subscribers-lld-coff Author: Jacek Caban (cjacek) ChangesFull diff: https://github.com/llvm/llvm-project/pull/107929.diff 5 Files Affected:
diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp
index fa2d230075d9d3..dbb2630aef5354 100644
--- a/lld/COFF/InputFiles.cpp
+++ b/lld/COFF/InputFiles.cpp
@@ -1069,9 +1069,27 @@ void ImportFile::parse() {
// DLL functions just like regular non-DLL functions.)
if (hdr->getType() == llvm::COFF::IMPORT_CODE) {
if (ctx.config.machine != ARM64EC) {
- thunkSym = ctx.symtab.addImportThunk(name, impSym, hdr->Machine);
+ ImportThunkChunk *chunk;
+ switch (hdr->Machine) {
+ case AMD64:
+ chunk = make<ImportThunkChunkX64>(ctx, impSym);
+ break;
+ case I386:
+ chunk = make<ImportThunkChunkX86>(ctx, impSym);
+ break;
+ case ARM64:
+ chunk = make<ImportThunkChunkARM64>(ctx, impSym);
+ break;
+ case ARMNT:
+ chunk = make<ImportThunkChunkARM>(ctx, impSym);
+ break;
+ default:
+ llvm_unreachable("unknown machine type");
+ }
+ thunkSym = ctx.symtab.addImportThunk(name, impSym, chunk);
} else {
- thunkSym = ctx.symtab.addImportThunk(name, impSym, AMD64);
+ thunkSym = ctx.symtab.addImportThunk(
+ name, impSym, make<ImportThunkChunkX64>(ctx, impSym));
// FIXME: Add aux IAT symbols.
}
}
diff --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp
index bb7583bb9a7df4..c9b3d78e3de17a 100644
--- a/lld/COFF/SymbolTable.cpp
+++ b/lld/COFF/SymbolTable.cpp
@@ -784,11 +784,11 @@ DefinedImportData *SymbolTable::addImportData(StringRef n, ImportFile *f) {
}
Symbol *SymbolTable::addImportThunk(StringRef name, DefinedImportData *id,
- uint16_t machine) {
+ ImportThunkChunk *chunk) {
auto [s, wasInserted] = insert(name, nullptr);
s->isUsedInRegularObj = true;
if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
- replaceSymbol<DefinedImportThunk>(s, ctx, name, id, machine);
+ replaceSymbol<DefinedImportThunk>(s, ctx, name, id, chunk);
return s;
}
diff --git a/lld/COFF/SymbolTable.h b/lld/COFF/SymbolTable.h
index 51c6c79ec14463..3a277fc700e864 100644
--- a/lld/COFF/SymbolTable.h
+++ b/lld/COFF/SymbolTable.h
@@ -28,6 +28,7 @@ class COFFLinkerContext;
class Defined;
class DefinedAbsolute;
class DefinedRegular;
+class ImportThunkChunk;
class LazyArchive;
class SectionChunk;
class Symbol;
@@ -104,7 +105,7 @@ class SymbolTable {
CommonChunk *c = nullptr);
DefinedImportData *addImportData(StringRef n, ImportFile *f);
Symbol *addImportThunk(StringRef name, DefinedImportData *s,
- uint16_t machine);
+ ImportThunkChunk *chunk);
void addLibcall(StringRef name);
void addEntryThunk(Symbol *from, Symbol *to);
void initializeEntryThunks();
diff --git a/lld/COFF/Symbols.cpp b/lld/COFF/Symbols.cpp
index b098abb80d6f1e..5f4d797f74a2dd 100644
--- a/lld/COFF/Symbols.cpp
+++ b/lld/COFF/Symbols.cpp
@@ -107,22 +107,10 @@ COFFSymbolRef DefinedCOFF::getCOFFSymbol() {
uint64_t DefinedAbsolute::getRVA() { return va - ctx.config.imageBase; }
-static Chunk *makeImportThunk(COFFLinkerContext &ctx, DefinedImportData *s,
- uint16_t machine) {
- if (machine == AMD64)
- return make<ImportThunkChunkX64>(ctx, s);
- if (machine == I386)
- return make<ImportThunkChunkX86>(ctx, s);
- if (machine == ARM64)
- return make<ImportThunkChunkARM64>(ctx, s);
- assert(machine == ARMNT);
- return make<ImportThunkChunkARM>(ctx, s);
-}
-
DefinedImportThunk::DefinedImportThunk(COFFLinkerContext &ctx, StringRef name,
- DefinedImportData *s, uint16_t machine)
- : Defined(DefinedImportThunkKind, name), wrappedSym(s),
- data(makeImportThunk(ctx, s, machine)) {}
+ DefinedImportData *s,
+ ImportThunkChunk *chunk)
+ : Defined(DefinedImportThunkKind, name), wrappedSym(s), data(chunk) {}
Defined *Undefined::getWeakAlias() {
// A weak alias may be a weak alias to another symbol, so check recursively.
diff --git a/lld/COFF/Symbols.h b/lld/COFF/Symbols.h
index c427a062dc82b2..724330e4bab958 100644
--- a/lld/COFF/Symbols.h
+++ b/lld/COFF/Symbols.h
@@ -388,7 +388,7 @@ class DefinedImportData : public Defined {
class DefinedImportThunk : public Defined {
public:
DefinedImportThunk(COFFLinkerContext &ctx, StringRef name,
- DefinedImportData *s, uint16_t machine);
+ DefinedImportData *s, ImportThunkChunk *chunk);
static bool classof(const Symbol *s) {
return s->kind() == DefinedImportThunkKind;
|
@llvm/pr-subscribers-lld Author: Jacek Caban (cjacek) ChangesFull diff: https://github.com/llvm/llvm-project/pull/107929.diff 5 Files Affected:
diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp
index fa2d230075d9d3..dbb2630aef5354 100644
--- a/lld/COFF/InputFiles.cpp
+++ b/lld/COFF/InputFiles.cpp
@@ -1069,9 +1069,27 @@ void ImportFile::parse() {
// DLL functions just like regular non-DLL functions.)
if (hdr->getType() == llvm::COFF::IMPORT_CODE) {
if (ctx.config.machine != ARM64EC) {
- thunkSym = ctx.symtab.addImportThunk(name, impSym, hdr->Machine);
+ ImportThunkChunk *chunk;
+ switch (hdr->Machine) {
+ case AMD64:
+ chunk = make<ImportThunkChunkX64>(ctx, impSym);
+ break;
+ case I386:
+ chunk = make<ImportThunkChunkX86>(ctx, impSym);
+ break;
+ case ARM64:
+ chunk = make<ImportThunkChunkARM64>(ctx, impSym);
+ break;
+ case ARMNT:
+ chunk = make<ImportThunkChunkARM>(ctx, impSym);
+ break;
+ default:
+ llvm_unreachable("unknown machine type");
+ }
+ thunkSym = ctx.symtab.addImportThunk(name, impSym, chunk);
} else {
- thunkSym = ctx.symtab.addImportThunk(name, impSym, AMD64);
+ thunkSym = ctx.symtab.addImportThunk(
+ name, impSym, make<ImportThunkChunkX64>(ctx, impSym));
// FIXME: Add aux IAT symbols.
}
}
diff --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp
index bb7583bb9a7df4..c9b3d78e3de17a 100644
--- a/lld/COFF/SymbolTable.cpp
+++ b/lld/COFF/SymbolTable.cpp
@@ -784,11 +784,11 @@ DefinedImportData *SymbolTable::addImportData(StringRef n, ImportFile *f) {
}
Symbol *SymbolTable::addImportThunk(StringRef name, DefinedImportData *id,
- uint16_t machine) {
+ ImportThunkChunk *chunk) {
auto [s, wasInserted] = insert(name, nullptr);
s->isUsedInRegularObj = true;
if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
- replaceSymbol<DefinedImportThunk>(s, ctx, name, id, machine);
+ replaceSymbol<DefinedImportThunk>(s, ctx, name, id, chunk);
return s;
}
diff --git a/lld/COFF/SymbolTable.h b/lld/COFF/SymbolTable.h
index 51c6c79ec14463..3a277fc700e864 100644
--- a/lld/COFF/SymbolTable.h
+++ b/lld/COFF/SymbolTable.h
@@ -28,6 +28,7 @@ class COFFLinkerContext;
class Defined;
class DefinedAbsolute;
class DefinedRegular;
+class ImportThunkChunk;
class LazyArchive;
class SectionChunk;
class Symbol;
@@ -104,7 +105,7 @@ class SymbolTable {
CommonChunk *c = nullptr);
DefinedImportData *addImportData(StringRef n, ImportFile *f);
Symbol *addImportThunk(StringRef name, DefinedImportData *s,
- uint16_t machine);
+ ImportThunkChunk *chunk);
void addLibcall(StringRef name);
void addEntryThunk(Symbol *from, Symbol *to);
void initializeEntryThunks();
diff --git a/lld/COFF/Symbols.cpp b/lld/COFF/Symbols.cpp
index b098abb80d6f1e..5f4d797f74a2dd 100644
--- a/lld/COFF/Symbols.cpp
+++ b/lld/COFF/Symbols.cpp
@@ -107,22 +107,10 @@ COFFSymbolRef DefinedCOFF::getCOFFSymbol() {
uint64_t DefinedAbsolute::getRVA() { return va - ctx.config.imageBase; }
-static Chunk *makeImportThunk(COFFLinkerContext &ctx, DefinedImportData *s,
- uint16_t machine) {
- if (machine == AMD64)
- return make<ImportThunkChunkX64>(ctx, s);
- if (machine == I386)
- return make<ImportThunkChunkX86>(ctx, s);
- if (machine == ARM64)
- return make<ImportThunkChunkARM64>(ctx, s);
- assert(machine == ARMNT);
- return make<ImportThunkChunkARM>(ctx, s);
-}
-
DefinedImportThunk::DefinedImportThunk(COFFLinkerContext &ctx, StringRef name,
- DefinedImportData *s, uint16_t machine)
- : Defined(DefinedImportThunkKind, name), wrappedSym(s),
- data(makeImportThunk(ctx, s, machine)) {}
+ DefinedImportData *s,
+ ImportThunkChunk *chunk)
+ : Defined(DefinedImportThunkKind, name), wrappedSym(s), data(chunk) {}
Defined *Undefined::getWeakAlias() {
// A weak alias may be a weak alias to another symbol, so check recursively.
diff --git a/lld/COFF/Symbols.h b/lld/COFF/Symbols.h
index c427a062dc82b2..724330e4bab958 100644
--- a/lld/COFF/Symbols.h
+++ b/lld/COFF/Symbols.h
@@ -388,7 +388,7 @@ class DefinedImportData : public Defined {
class DefinedImportThunk : public Defined {
public:
DefinedImportThunk(COFFLinkerContext &ctx, StringRef name,
- DefinedImportData *s, uint16_t machine);
+ DefinedImportData *s, ImportThunkChunk *chunk);
static bool classof(const Symbol *s) {
return s->kind() == DefinedImportThunkKind;
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
lld/COFF/InputFiles.cpp
Outdated
@@ -1069,9 +1069,27 @@ void ImportFile::parse() { | |||
// DLL functions just like regular non-DLL functions.) | |||
if (hdr->getType() == llvm::COFF::IMPORT_CODE) { | |||
if (ctx.config.machine != ARM64EC) { | |||
thunkSym = ctx.symtab.addImportThunk(name, impSym, hdr->Machine); | |||
ImportThunkChunk *chunk; | |||
switch (hdr->Machine) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is micro code style stuff, but consider moving this switch into a helper function so you can write return make<Import*>()
. It's not stated explicitly, but it's kind of implied by https://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code , and https://llvm.org/docs/CodingStandards.html#don-t-use-default-labels-in-fully-covered-switches-over-enumerations , and it's a common pattern all over LLVM:
Type *mapSwitch(stuff) {
switch (machine) {
case 1:
return make<Import1>(stuff);
case 2:
return make<Import2>(stuff);
...
} // no default
llvm_unreachable("unknown machine");
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated with a version using such helper. Thanks for reviews.
No description provided.