Skip to content

[LLD][COFF] Add support for MinGW auto-export on ARM64X #125862

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
Feb 6, 2025
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
57 changes: 30 additions & 27 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,43 +1382,46 @@ void LinkerDriver::maybeExportMinGWSymbols(const opt::InputArgList &args) {
if (!ctx.config.dll)
return;

if (!ctx.symtab.exports.empty())
if (ctx.symtab.hadExplicitExports ||
(ctx.hybridSymtab && ctx.hybridSymtab->hadExplicitExports))
return;
if (args.hasArg(OPT_exclude_all_symbols))
return;
}

AutoExporter exporter(ctx, excludedSymbols);
ctx.forEachSymtab([&](SymbolTable &symtab) {
AutoExporter exporter(symtab, excludedSymbols);

for (auto *arg : args.filtered(OPT_wholearchive_file))
if (std::optional<StringRef> path = findFile(arg->getValue()))
exporter.addWholeArchive(*path);
for (auto *arg : args.filtered(OPT_wholearchive_file))
if (std::optional<StringRef> path = findFile(arg->getValue()))
exporter.addWholeArchive(*path);

for (auto *arg : args.filtered(OPT_exclude_symbols)) {
SmallVector<StringRef, 2> vec;
StringRef(arg->getValue()).split(vec, ',');
for (StringRef sym : vec)
exporter.addExcludedSymbol(ctx.symtab.mangle(sym));
}
for (auto *arg : args.filtered(OPT_exclude_symbols)) {
SmallVector<StringRef, 2> vec;
StringRef(arg->getValue()).split(vec, ',');
for (StringRef sym : vec)
exporter.addExcludedSymbol(symtab.mangle(sym));
}

ctx.symtab.forEachSymbol([&](Symbol *s) {
auto *def = dyn_cast<Defined>(s);
if (!exporter.shouldExport(def))
return;
symtab.forEachSymbol([&](Symbol *s) {
auto *def = dyn_cast<Defined>(s);
if (!exporter.shouldExport(def))
return;

if (!def->isGCRoot) {
def->isGCRoot = true;
ctx.config.gcroot.push_back(def);
}
if (!def->isGCRoot) {
def->isGCRoot = true;
ctx.config.gcroot.push_back(def);
}

Export e;
e.name = def->getName();
e.sym = def;
if (Chunk *c = def->getChunk())
if (!(c->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))
e.data = true;
s->isUsedInRegularObj = true;
ctx.symtab.exports.push_back(e);
Export e;
e.name = def->getName();
e.sym = def;
if (Chunk *c = def->getChunk())
if (!(c->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))
e.data = true;
s->isUsedInRegularObj = true;
symtab.exports.push_back(e);
});
});
}

Expand Down
9 changes: 4 additions & 5 deletions lld/COFF/MinGW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ using namespace lld;
using namespace lld::coff;

AutoExporter::AutoExporter(
COFFLinkerContext &ctx,
const llvm::DenseSet<StringRef> &manualExcludeSymbols)
: manualExcludeSymbols(manualExcludeSymbols), ctx(ctx) {
SymbolTable &symtab, const llvm::DenseSet<StringRef> &manualExcludeSymbols)
: manualExcludeSymbols(manualExcludeSymbols), symtab(symtab) {
excludeLibs = {
"libgcc",
"libgcc_s",
Expand Down Expand Up @@ -84,7 +83,7 @@ AutoExporter::AutoExporter(
"_NULL_THUNK_DATA",
};

if (ctx.config.machine == I386) {
if (symtab.machine == I386) {
excludeSymbols = {
"__NULL_IMPORT_DESCRIPTOR",
"__pei386_runtime_relocator",
Expand Down Expand Up @@ -151,7 +150,7 @@ bool AutoExporter::shouldExport(Defined *sym) const {
return false;

// If a corresponding __imp_ symbol exists and is defined, don't export it.
if (ctx.symtab.find(("__imp_" + sym->getName()).str()))
if (symtab.find(("__imp_" + sym->getName()).str()))
return false;

// Check that file is non-null before dereferencing it, symbols not
Expand Down
4 changes: 2 additions & 2 deletions lld/COFF/MinGW.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class COFFLinkerContext;
// symbols for MinGW.
class AutoExporter {
public:
AutoExporter(COFFLinkerContext &ctx,
AutoExporter(SymbolTable &symtab,
const llvm::DenseSet<StringRef> &manualExcludeSymbols);

void addWholeArchive(StringRef path);
Expand All @@ -42,7 +42,7 @@ class AutoExporter {
bool shouldExport(Defined *sym) const;

private:
COFFLinkerContext &ctx;
SymbolTable &symtab;
};

void writeDefFile(COFFLinkerContext &, StringRef name,
Expand Down
93 changes: 93 additions & 0 deletions lld/test/COFF/arm64x-export-all.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// REQUIRES: aarch64

// RUN: llvm-mc -filetype=obj -triple=aarch64-windows %s -o %t.arm64.obj
// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows %s -o %t.arm64ec.obj
// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows %S/Inputs/loadconfig-arm64ec.s -o %t-loadconfig-arm64ec.obj
// RUN: llvm-mc -filetype=obj -triple=aarch64-windows %S/Inputs/loadconfig-arm64.s -o %t-loadconfig-arm64.obj

// Check that all symbols are exported in both EC and native views.

// RUN: lld-link -machine:arm64x -lldmingw -dll -noentry -out:%t.dll %t.arm64.obj %t.arm64ec.obj %t-loadconfig-arm64.obj %t-loadconfig-arm64ec.obj

// RUN: llvm-readobj --coff-exports %t.dll | FileCheck --check-prefix=EXP %s
// EXP: Format: COFF-ARM64X
// EXP-NEXT: Arch: aarch64
// EXP-NEXT: AddressSize: 64bit
// EXP-NEXT: Export {
// EXP-NEXT: Ordinal: 1
// EXP-NEXT: Name: _load_config_used
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note; I guess this is a symbol we could include in excludeSymbols, as it doesn't make sense to autoexport this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will do that in a followup. Thanks!

// EXP-NEXT: RVA:
// EXP-NEXT: }
// EXP-NEXT: Export {
// EXP-NEXT: Ordinal: 2
// EXP-NEXT: Name: sym
// EXP-NEXT: RVA: 0x2000
// EXP-NEXT: }
// EXP-NEXT: Export {
// EXP-NEXT: Ordinal: 3
// EXP-NEXT: Name: sym2
// EXP-NEXT: RVA: 0x2004
// EXP-NEXT: }
// EXP-NEXT: HybridObject {
// EXP-NEXT: Format: COFF-ARM64EC
// EXP-NEXT: Arch: aarch64
// EXP-NEXT: AddressSize: 64bit
// EXP-NEXT: Export {
// EXP-NEXT: Ordinal: 1
// EXP-NEXT: Name: __chpe_metadata
// EXP-NEXT: RVA:
// EXP-NEXT: }
// EXP-NEXT: Export {
// EXP-NEXT: Ordinal: 2
// EXP-NEXT: Name: __os_arm64x_dispatch_icall
// EXP-NEXT: RVA: 0x12B0
// EXP-NEXT: }
// EXP-NEXT: Export {
// EXP-NEXT: Ordinal: 3
// EXP-NEXT: Name: __os_arm64x_dispatch_ret
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for all these arm64ec internals - we probably don't want to export them. But that's a difference concern from this PR anyway.

// EXP-NEXT: RVA:
// EXP-NEXT: }
// EXP-NEXT: Export {
// EXP-NEXT: Ordinal: 4
// EXP-NEXT: Name: _load_config_used
// EXP-NEXT: RVA:
// EXP-NEXT: }
// EXP-NEXT: Export {
// EXP-NEXT: Ordinal: 5
// EXP-NEXT: Name: sym
// EXP-NEXT: RVA: 0x2008
// EXP-NEXT: }
// EXP-NEXT: Export {
// EXP-NEXT: Ordinal: 6
// EXP-NEXT: Name: sym2
// EXP-NEXT: RVA: 0x200C
// EXP-NEXT: }
// EXP-NEXT: }

// Check that an explicit export in the EC view is respected, preventing symbols from being auto-exported in both EC and native views.

// RUN: lld-link -machine:arm64x -lldmingw -dll -noentry -out:%t2.dll %t.arm64.obj %t.arm64ec.obj -export:sym \
// RUN: %t-loadconfig-arm64.obj %t-loadconfig-arm64ec.obj

// RUN: llvm-readobj --coff-exports %t2.dll | FileCheck --check-prefix=EXP2 %s
// EXP2: Format: COFF-ARM64X
// EXP2-NEXT: Arch: aarch64
// EXP2-NEXT: AddressSize: 64bit
// EXP2-NEXT: HybridObject {
// EXP2-NEXT: Format: COFF-ARM64EC
// EXP2-NEXT: Arch: aarch64
// EXP2-NEXT: AddressSize: 64bit
// EXP2-NEXT: Export {
// EXP2-NEXT: Ordinal: 1
// EXP2-NEXT: Name: sym
// EXP2-NEXT: RVA: 0x2008
// EXP2-NEXT: }
// EXP2-NEXT: }

.data
.globl sym
sym:
.word 0
.globl sym2
sym2:
.word 0