Skip to content

[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

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lld/COFF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,20 @@ MachineTypes ImportFile::getMachineType() const {
return MachineTypes(machine);
}

ImportThunkChunk *ImportFile::makeImportThunk() {
switch (hdr->Machine) {
case AMD64:
return make<ImportThunkChunkX64>(ctx, impSym);
case I386:
return make<ImportThunkChunkX86>(ctx, impSym);
case ARM64:
return make<ImportThunkChunkARM64>(ctx, impSym);
case ARMNT:
return make<ImportThunkChunkARM>(ctx, impSym);
}
llvm_unreachable("unknown machine type");
}

void ImportFile::parse() {
const auto *hdr =
reinterpret_cast<const coff_import_header *>(mb.getBufferStart());
Expand Down Expand Up @@ -1069,9 +1083,10 @@ 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);
thunkSym = ctx.symtab.addImportThunk(name, impSym, makeImportThunk());
} else {
thunkSym = ctx.symtab.addImportThunk(name, impSym, AMD64);
thunkSym = ctx.symtab.addImportThunk(
name, impSym, make<ImportThunkChunkX64>(ctx, impSym));
// FIXME: Add aux IAT symbols.
}
}
Expand Down
2 changes: 2 additions & 0 deletions lld/COFF/InputFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Defined;
class DefinedImportData;
class DefinedImportThunk;
class DefinedRegular;
class ImportThunkChunk;
class SectionChunk;
class Symbol;
class Undefined;
Expand Down Expand Up @@ -352,6 +353,7 @@ class ImportFile : public InputFile {

private:
void parse() override;
ImportThunkChunk *makeImportThunk();

public:
StringRef externalName;
Expand Down
4 changes: 2 additions & 2 deletions lld/COFF/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion lld/COFF/SymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class COFFLinkerContext;
class Defined;
class DefinedAbsolute;
class DefinedRegular;
class ImportThunkChunk;
class LazyArchive;
class SectionChunk;
class Symbol;
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 3 additions & 15 deletions lld/COFF/Symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lld/COFF/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading