Skip to content

[LLD] [COFF] Handle manually defined __imp_ pointers in LTO #70777

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
Nov 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
6 changes: 6 additions & 0 deletions lld/COFF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,12 @@ 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
39 changes: 39 additions & 0 deletions lld/test/COFF/lto-imp-prefix.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
; REQUIRES: x86

; 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: lld-link /entry:entry %t.main.obj %t.other.obj /out:%t.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"

define void @entry() {
entry:
tail call void @importedFunc()
tail call void @other()
ret void
}

declare dllimport void @importedFunc()

declare void @other()

;--- other.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 @importedFuncReplacement

define internal void @importedFuncReplacement() {
entry:
ret void
}

define void @other() {
entry:
ret void
}