Skip to content

[LLD] [COFF] Don't preserve unnecessary __imp_ prefixed symbols #72989

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
Dec 4, 2023
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
19 changes: 13 additions & 6 deletions lld/COFF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,19 @@ void BitcodeFile::parse() {
sym = ctx.symtab.addUndefined(symName, this, false);
if (objSym.isWeak())
sym->deferUndefined = true;
// If one LTO object file references (i.e. has an undefined reference to)
// a symbol with an __imp_ prefix, the LTO compilation itself sees it
// as unprefixed but with a dllimport attribute instead, and doesn't
// understand the relation to a concrete IR symbol with the __imp_ prefix.
//
// For such cases, mark the symbol as used in a regular object (i.e. the
// symbol must be retained) so that the linker can associate the
// references in the end. If the symbol is defined in an import library
// or in a regular object file, this has no effect, but if it is defined
// in another LTO object file, this makes sure it is kept, to fulfill
// the reference when linking the output of the LTO compilation.
if (symName.starts_with("__imp_"))
sym->isUsedInRegularObj = true;
} else if (objSym.isCommon()) {
sym = ctx.symtab.addCommon(this, symName, objSym.getCommonSize());
} else if (objSym.isWeak() && objSym.isIndirect()) {
Expand All @@ -1063,12 +1076,6 @@ void BitcodeFile::parse() {
} else {
sym = ctx.symtab.addRegular(this, symName, nullptr, fakeSC, 0,
objSym.isWeak());
// Model all symbols with the __imp_ prefix as having external
// references. If one LTO object defines a __imp_<foo> symbol, and
// another LTO object refers to <foo> with dllimport, make sure the
// __imp_ symbol is kept.
if (symName.starts_with("__imp_"))
sym->isUsedInRegularObj = true;
}
symbols.push_back(sym);
if (objSym.isUsed())
Expand Down
8 changes: 2 additions & 6 deletions lld/test/COFF/lto-imp-prefix.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@

; RUN: lld-link /entry:entry %t.main.obj %t.other1.obj /out:%t1.exe /subsystem:console /debug:symtab

;; The current implementation for handling __imp_ symbols retains all of them.
;; Observe that this currently produces __imp_unusedFunc even if nothing
;; references unusedFunc in any form.

;; Check that we don't retain __imp_ prefixed symbols we don't need.
; RUN: llvm-nm %t1.exe | FileCheck %s

; CHECK: __imp_unusedFunc
; CHECK-NOT: __imp_unusedFunc

; RUN: lld-link /entry:entry %t.main.obj %t.other2.obj /out:%t2.exe /subsystem:console

Expand Down