Skip to content

[LLD][COFF] Introduce Symbol::getDefined helper. (NFC) #151253

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
Jul 31, 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
12 changes: 2 additions & 10 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,13 +1318,9 @@ void LinkerDriver::convertResources() {
}

void LinkerDriver::maybeCreateECExportThunk(StringRef name, Symbol *&sym) {
Defined *def;
if (!sym)
return;
if (auto undef = dyn_cast<Undefined>(sym))
def = undef->getDefinedWeakAlias();
else
def = dyn_cast<Defined>(sym);
Defined *def = sym->getDefined();
if (!def)
return;

Expand Down Expand Up @@ -1356,11 +1352,7 @@ void LinkerDriver::createECExportThunks() {
Symbol *sym = ctx.symtab.find(targetName);
if (!sym)
continue;
Defined *targetSym;
if (auto undef = dyn_cast<Undefined>(sym))
targetSym = undef->getDefinedWeakAlias();
else
targetSym = dyn_cast<Defined>(sym);
Defined *targetSym = sym->getDefined();
if (!targetSym)
continue;

Expand Down
8 changes: 8 additions & 0 deletions lld/COFF/Symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ bool Symbol::isLive() const {
return true;
}

Defined *Symbol::getDefined() {
if (auto d = dyn_cast<Defined>(this))
return d;
if (auto u = dyn_cast<Undefined>(this))
Copy link
Member

Choose a reason for hiding this comment

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

This (very) vaguely changes the logic from before - previously we tested dyn_cast<Undefined> before, while we now test them in different order. (In theory this could have a performance aspect, but that's not relevant.) What I wanted to say here, is that as long as only one of dyn_cast<Defined> and dyn_cast<Undefined> can succeed, i.e. they are mutually exclusive, then this should be fine - and this mildly rewritten form looks ok.

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 believe the assumption that only one of them can succeed is safe. I wrote it in this order because I’d expect Defined to be more common, but as you said, the performance impact is not really relevant.

return u->getDefinedWeakAlias();
return nullptr;
}

void Symbol::replaceKeepingName(Symbol *other, size_t size) {
StringRef origName = getName();
memcpy(this, other, size);
Expand Down
4 changes: 4 additions & 0 deletions lld/COFF/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ class Symbol {
symbolKind == LazyDLLSymbolKind;
}

// Get the Defined symbol associated with this symbol, either itself or its
// weak alias.
Defined *getDefined();

private:
void computeName();

Expand Down