COFF: Don't add new undefined symbols for /alternatename.

Alternatename option is in the form of /alternatename:<from>=<to>.
It's effect is to resolve <from> as <to> if <from> is still undefined
at end of name resolution.

If <from> is not undefined but completely a new symbol, alternatename
shouldn't do anything. Previously, it introduced a new undefined
symbol for <from>, which resulted in undefined symbol error.

llvm-svn: 240161
This commit is contained in:
Rui Ueyama
2015-06-19 19:23:43 +00:00
parent 8ec15ea60a
commit 4d2834bd7b
3 changed files with 11 additions and 7 deletions

View File

@@ -525,12 +525,6 @@ bool LinkerDriver::link(int Argc, const char *Argv[]) {
for (auto &P : Config->AlternateNames) {
StringRef From = P.first;
StringRef To = P.second;
// If From is already resolved to a Defined type, do nothing.
// Otherwise, rename it to see if To can be resolved instead.
if (Symtab.find(From))
continue;
if (Config->Verbose)
llvm::outs() << "/alternatename:" << From << "=" << To << "\n";
if (auto EC = Symtab.rename(From, To)) {
llvm::errs() << EC.message() << "\n";
return false;

View File

@@ -232,10 +232,19 @@ std::error_code SymbolTable::addUndefined(StringRef Name) {
// Resolve To, and make From an alias to To.
std::error_code SymbolTable::rename(StringRef From, StringRef To) {
// If From is not undefined, do nothing.
// Otherwise, rename it to see if To can be resolved instead.
auto It = Symtab.find(From);
if (It == Symtab.end())
return std::error_code();
Symbol *Sym = It->second;
if (!isa<Undefined>(Sym->Body))
return std::error_code();
SymbolBody *Body = new (Alloc) Undefined(To);
if (auto EC = resolve(Body))
return EC;
Symtab[From]->Body = Body->getReplacement();
Sym->Body = Body->getReplacement();
Body->setBackref(Sym);
return std::error_code();
}

View File

@@ -4,6 +4,7 @@
# RUN: lld -flavor link2 /entry:foo /subsystem:console \
# RUN: /alternatename:foo=mainCRTStartup \
# RUN: /alternatename:foo=mainCRTStartup \
# RUN: /alternatename:nosuchsym1=nosuchsym2 \
# RUN: /out:%t.exe %t.obj
# RUN: yaml2obj < %s > %t.obj