Skip to content

[lld][ELF] Use the containing symbol, not the referenced symbol, for undefined symbol errors. #70800

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
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
30 changes: 29 additions & 1 deletion lld/ELF/Relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,34 @@ static const Symbol *getAlternativeSpelling(const Undefined &sym,
return nullptr;
}

static Symbol &
getSymAtOffset(InputSectionBase &sec, uint64_t off, Symbol &sym) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would getSymContainingOffset() be a better name, as we're looking for the symbol with off in the range [sym->off,, sym->off + sym->size) .

ArrayRef<Symbol *> syms;
switch (config->ekind) {
case ELF32LEKind:
syms = sec.getFile<ELF32LE>()->getSymbols();
break;
case ELF32BEKind:
syms = sec.getFile<ELF32BE>()->getSymbols();
break;
case ELF64LEKind:
syms = sec.getFile<ELF64LE>()->getSymbols();
break;
case ELF64BEKind:
syms = sec.getFile<ELF64BE>()->getSymbols();
break;
default:
llvm_unreachable("");
}
// Get the symbol that contains this offset.
for (Symbol *b : syms)
if (auto *d = dyn_cast_or_null<Defined>(b))
if (d->section == &sec && d->value <= off && off < d->value + d->size)
return *d;
// Fall back to the supplied symbol otherwise.
return sym;
}

static void reportUndefinedSymbol(const UndefinedDiag &undef,
bool correctSpelling) {
Undefined &sym = *undef.sym;
Expand Down Expand Up @@ -739,7 +767,7 @@ static void reportUndefinedSymbol(const UndefinedDiag &undef,
uint64_t offset = l.offset;

msg += "\n>>> referenced by ";
std::string src = sec.getSrcMsg(sym, offset);
std::string src = sec.getSrcMsg(getSymAtOffset(sec, offset, sym), offset);
if (!src.empty())
msg += src + "\n>>> ";
msg += sec.getObjMsg(offset);
Expand Down