Skip to content

[LLD][COFF] Support alternate names in both symbol tables on ARM64X #127619

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 21, 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
3 changes: 0 additions & 3 deletions lld/COFF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,6 @@ struct Configuration {
// Used for /failifmismatch.
std::map<StringRef, std::pair<StringRef, InputFile *>> mustMatch;

// Used for /alternatename.
std::map<StringRef, StringRef> alternateNames;

// Used for /order.
llvm::StringMap<int> order;

Expand Down
46 changes: 22 additions & 24 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ void LinkerDriver::parseDirectives(InputFile *file) {
parseAligncomm(arg->getValue());
break;
case OPT_alternatename:
parseAlternateName(arg->getValue());
file->symtab.parseAlternateName(arg->getValue());
break;
case OPT_defaultlib:
if (std::optional<StringRef> path = findLibIfNew(arg->getValue()))
Expand Down Expand Up @@ -1896,7 +1896,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {

// Handle /alternatename
for (auto *arg : args.filtered(OPT_alternatename))
parseAlternateName(arg->getValue());
mainSymtab.parseAlternateName(arg->getValue());

// Handle /include
for (auto *arg : args.filtered(OPT_incl))
Expand Down Expand Up @@ -2515,32 +2515,30 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
if (e.source != ExportSource::Directives)
e.symbolName = symtab.mangleMaybe(e.sym);
}
});

// Add weak aliases. Weak aliases is a mechanism to give remaining
// undefined symbols final chance to be resolved successfully.
for (auto pair : config->alternateNames) {
StringRef from = pair.first;
StringRef to = pair.second;
Symbol *sym = ctx.symtab.find(from);
if (!sym)
continue;
if (auto *u = dyn_cast<Undefined>(sym)) {
if (u->weakAlias) {
// On ARM64EC, anti-dependency aliases are treated as undefined
// symbols unless a demangled symbol aliases a defined one, which is
// part of the implementation.
if (!isArm64EC(ctx.config.machine) || !u->isAntiDep)
continue;
if (!isa<Undefined>(u->weakAlias) &&
!isArm64ECMangledFunctionName(u->getName()))
continue;
// Add weak aliases. Weak aliases is a mechanism to give remaining
// undefined symbols final chance to be resolved successfully.
for (auto pair : symtab.alternateNames) {
StringRef from = pair.first;
StringRef to = pair.second;
Symbol *sym = symtab.find(from);
if (!sym)
continue;
if (auto *u = dyn_cast<Undefined>(sym)) {
if (u->weakAlias) {
// On ARM64EC, anti-dependency aliases are treated as undefined
// symbols unless a demangled symbol aliases a defined one, which
// is part of the implementation.
if (!symtab.isEC() || !u->isAntiDep)
continue;
if (!isa<Undefined>(u->weakAlias) &&
!isArm64ECMangledFunctionName(u->getName()))
continue;
}
u->setWeakAlias(symtab.addUndefined(to));
}
u->setWeakAlias(ctx.symtab.addUndefined(to));
}
}

ctx.forEachSymtab([&](SymbolTable &symtab) {
// If any inputs are bitcode files, the LTO code generator may create
// references to library functions that are not explicit in the bitcode
// file's symbol table. If any of those library functions are defined in
Expand Down
1 change: 0 additions & 1 deletion lld/COFF/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ class LinkerDriver {
void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major,
uint32_t *minor, bool *gotVersion = nullptr);

void parseAlternateName(StringRef);
void parseMerge(StringRef);
void parsePDBPageSize(StringRef);
void parseSection(StringRef);
Expand Down
12 changes: 0 additions & 12 deletions lld/COFF/DriverUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,6 @@ void LinkerDriver::parseSubsystem(StringRef arg, WindowsSubsystem *sys,
*gotVersion = !ver.empty();
}

// Parse a string of the form of "<from>=<to>".
// Results are directly written to Config.
void LinkerDriver::parseAlternateName(StringRef s) {
auto [from, to] = s.split('=');
if (from.empty() || to.empty())
Fatal(ctx) << "/alternatename: invalid argument: " << s;
auto it = ctx.config.alternateNames.find(from);
if (it != ctx.config.alternateNames.end() && it->second != to)
Fatal(ctx) << "/alternatename: conflicts: " << s;
ctx.config.alternateNames.insert(it, std::make_pair(from, to));
}

// Parse a string of the form of "<from>=<to>".
// Results are directly written to Config.
void LinkerDriver::parseMerge(StringRef s) {
Expand Down
11 changes: 11 additions & 0 deletions lld/COFF/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,17 @@ void SymbolTable::parseModuleDefs(StringRef path) {
}
}

// Parse a string of the form of "<from>=<to>".
void SymbolTable::parseAlternateName(StringRef s) {
auto [from, to] = s.split('=');
if (from.empty() || to.empty())
Fatal(ctx) << "/alternatename: invalid argument: " << s;
auto it = alternateNames.find(from);
if (it != alternateNames.end() && it->second != to)
Fatal(ctx) << "/alternatename: conflicts: " << s;
alternateNames.insert(it, std::make_pair(from, to));
}

Symbol *SymbolTable::addUndefined(StringRef name) {
return addUndefined(name, nullptr, false);
}
Expand Down
4 changes: 4 additions & 0 deletions lld/COFF/SymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,13 @@ class SymbolTable {
// A list of wrapped symbols.
std::vector<WrappedSymbol> wrapped;

// Used for /alternatename.
std::map<StringRef, StringRef> alternateNames;

void fixupExports();
void assignExportOrdinals();
void parseModuleDefs(StringRef path);
void parseAlternateName(StringRef);

// Iterates symbols in non-determinstic hash table order.
template <typename T> void forEachSymbol(T callback) {
Expand Down
44 changes: 44 additions & 0 deletions lld/test/COFF/arm64x-altnames.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// REQUIRES: aarch64
// RUN: split-file %s %t.dir && cd %t.dir

// RUN: llvm-mc -filetype=obj -triple=aarch64-windows test.s -o test-arm64.obj
// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows test.s -o test-arm64ec.obj
// RUN: llvm-mc -filetype=obj -triple=aarch64-windows drectve.s -o drectve-arm64.obj
// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows drectve.s -o drectve-arm64ec.obj

// Check that the -alternatename command-line argument applies only to the EC namespace.

// RUN: not lld-link -out:out.dll -machine:arm64x -dll -noentry test-arm64.obj test-arm64ec.obj -alternatename:sym=altsym \
// RUN: 2>&1 | FileCheck --check-prefix=ERR-NATIVE %s

// ERR-NATIVE-NOT: test-arm64ec.obj
// ERR-NATIVE: lld-link: error: undefined symbol: sym
// ERR-NATIVE-NEXT: >>> referenced by test-arm64.obj:(.test)
// ERR-NATIVE-NOT: test-arm64ec.obj

// Check that the -alternatename .drectve directive applies only to the namespace in which it is defined.

// RUN: not lld-link -out:out.dll -machine:arm64x -dll -noentry test-arm64.obj test-arm64ec.obj drectve-arm64ec.obj \
// RUN: 2>&1 | FileCheck --check-prefix=ERR-NATIVE %s

// RUN: not lld-link -out:out.dll -machine:arm64x -dll -noentry test-arm64.obj test-arm64ec.obj drectve-arm64.obj \
// RUN: 2>&1 | FileCheck --check-prefix=ERR-EC %s

// ERR-EC-NOT: test-arm64.obj
// ERR-EC: lld-link: error: undefined symbol: sym
// ERR-EC-NEXT: >>> referenced by test-arm64ec.obj:(.test)
// ERR-EC-NOT: test-arm64.obj

// RUN: lld-link -out:out.dll -machine:arm64x -dll -noentry test-arm64.obj test-arm64ec.obj drectve-arm64.obj drectve-arm64ec.obj

#--- test.s
.section .test,"dr"
.rva sym
.data
.globl altsym
altsym:
.word 0

#--- drectve.s
.section .drectve
.ascii "-alternatename:sym=altsym"