Skip to content
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

[LTO] [LLD] Don't alias the __imp_func and func symbol resolutions #71376

Merged
merged 1 commit into from
Nov 21, 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
4 changes: 3 additions & 1 deletion lld/COFF/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,10 @@ void SymbolTable::reportUnresolvable() {
StringRef name = undef->getName();
if (name.starts_with("__imp_")) {
Symbol *imp = find(name.substr(strlen("__imp_")));
if (imp && isa<Defined>(imp))
if (Defined *def = dyn_cast_or_null<Defined>(imp)) {
def->isUsedInRegularObj = true;
continue;
}
}
if (name.contains("_PchSym_"))
continue;
Expand Down
5 changes: 1 addition & 4 deletions lld/test/COFF/lto-dllimport.ll
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
; RUN: lld-link %t.main.obj %t.other.obj -entry:main -out:%t.exe
; RUN: lld-link %t.main.bc %t.other.bc -entry:main -out:%t.exe
; RUN: lld-link %t.main.bc %t.other.obj -entry:main -out:%t.exe

;; This test currently fails if combining the regular object file main.obj
;; with the bitcode object file other.bc.
; RUN-skipped: lld-link %t.main.obj %t.other.bc -entry:main -out:%t.exe
; RUN: lld-link %t.main.obj %t.other.bc -entry:main -out:%t.exe

;--- main.ll
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
Expand Down
29 changes: 25 additions & 4 deletions lld/test/COFF/lto-imp-prefix.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
; RUN: rm -rf %t.dir
; RUN: split-file %s %t.dir
; RUN: llvm-as %t.dir/main.ll -o %t.main.obj
; RUN: llvm-as %t.dir/other.ll -o %t.other.obj
; RUN: llvm-as %t.dir/other1.ll -o %t.other1.obj
; RUN: llvm-as %t.dir/other2.ll -o %t.other2.obj

; RUN: lld-link /entry:entry %t.main.obj %t.other.obj /out:%t.exe /subsystem:console /debug:symtab
; 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.

; RUN: llvm-nm %t.exe | FileCheck %s
; RUN: llvm-nm %t1.exe | FileCheck %s

; CHECK: __imp_unusedFunc

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

;--- main.ll
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"
Expand All @@ -30,7 +33,7 @@ declare dllimport void @importedFunc()

declare void @other()

;--- other.ll
;--- other1.ll
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

Expand All @@ -52,3 +55,21 @@ define void @other() {
entry:
ret void
}

;--- other2.ll
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@__imp_importedFunc = global ptr @importedFunc

; Test with two external symbols with the same name, with/without the __imp_
; prefix.
define void @importedFunc() {
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps add a comment to emphasize that unprefixed symbol is external.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, to clarify that this testcase explicitly tests providing two external symbols from the same object file, with/without prefix.

entry:
ret void
}

define void @other() {
entry:
ret void
}
8 changes: 1 addition & 7 deletions llvm/lib/LTO/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,7 @@ void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
assert(ResI != ResE);
SymbolResolution Res = *ResI++;

StringRef Name = Sym.getName();
// Strip the __imp_ prefix from COFF dllimport symbols (similar to the
// way they are handled by lld), otherwise we can end up with two
// global resolutions (one with and one for a copy of the symbol without).
if (TT.isOSBinFormatCOFF() && Name.startswith("__imp_"))
Name = Name.substr(strlen("__imp_"));
auto &GlobalRes = GlobalResolutions[Name];
auto &GlobalRes = GlobalResolutions[Sym.getName()];
GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
if (Res.Prevailing) {
assert(!GlobalRes.Prevailing &&
Expand Down