Skip to content

[ELF] PROVIDE: fix spurious "symbol not found" #87530

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

Closed
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
17 changes: 10 additions & 7 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
@@ -2727,13 +2727,6 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
// Create dynamic sections for dynamic linking and static PIE.
config->hasDynSymTab = !ctx.sharedFiles.empty() || config->isPic;

script->addScriptReferencedSymbolsToSymTable();

// Prevent LTO from removing any definition referenced by -u.
for (StringRef name : config->undefined)
if (Defined *sym = dyn_cast_or_null<Defined>(symtab.find(name)))
sym->isUsedInRegularObj = true;

// If an entry symbol is in a static archive, pull out that file now.
if (Symbol *sym = symtab.find(config->entry))
handleUndefined(sym, "--entry");
@@ -2742,6 +2735,16 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
for (StringRef pat : args::getStrings(args, OPT_undefined_glob))
handleUndefinedGlob(pat);

// After potential archive member extraction involving ENTRY and
// -u/--undefined-glob, check whether PROVIDE symbols should be defined (the
// RHS may refer to definitions in just extracted object files).
script->addScriptReferencedSymbolsToSymTable();

// Prevent LTO from removing any definition referenced by -u.
for (StringRef name : config->undefined)
if (Defined *sym = dyn_cast_or_null<Defined>(symtab.find(name)))
sym->isUsedInRegularObj = true;

// Mark -init and -fini symbols so that the LTO doesn't eliminate them.
if (Symbol *sym = dyn_cast_or_null<Defined>(symtab.find(config->init)))
sym->isUsedInRegularObj = true;
25 changes: 25 additions & 0 deletions lld/test/ELF/linkerscript/symbolreferenced.s
Original file line number Diff line number Diff line change
@@ -50,6 +50,21 @@
# RUN: not ld.lld -T chain2.t a.o 2>&1 | FileCheck %s --check-prefix=ERR --implicit-check-not=error:
# ERR-COUNT-3: error: chain2.t:1: symbol not found: undef

## _start in a lazy object file references PROVIDE symbols. We extract _start
## earlier to avoid spurious "symbol not found" errors.
# RUN: llvm-mc -filetype=obj -triple=x86_64 undef.s -o undef.o
# RUN: llvm-mc -filetype=obj -triple=x86_64 start.s -o start.o
# RUN: ld.lld -T chain2.t undef.o --start-lib start.o --end-lib -o lazy
# RUN: llvm-nm lazy | FileCheck %s --check-prefix=LAZY
# RUN: ld.lld -e 0 -T chain2.t --undefined-glob '_start*' undef.o --start-lib start.o --end-lib -o lazy
# RUN: llvm-nm lazy | FileCheck %s --check-prefix=LAZY

# LAZY: T _start
# LAZY-NEXT: t f1
# LAZY-NEXT: T f2
# LAZY-NEXT: T newsym
# LAZY-NEXT: T unde

#--- a.s
.global _start
_start:
@@ -89,3 +104,13 @@ PROVIDE(newsym = f1);
PROVIDE(f2 = undef);
PROVIDE_HIDDEN(f1 = f2);
PROVIDE(newsym = f1);

#--- undef.s
.globl undef
undef: ret

#--- start.s
.globl _start
_start: ret
.data
.quad newsym