Skip to content

[LLD] [COFF] Handle undefined weak symbols in LTO #70430

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 5, 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
2 changes: 2 additions & 0 deletions lld/COFF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,8 @@ void BitcodeFile::parse() {
fakeSC = &ctx.ltoDataSectionChunk.chunk;
if (objSym.isUndefined()) {
sym = ctx.symtab.addUndefined(symName, this, false);
if (objSym.isWeak())
sym->deferUndefined = true;
} else if (objSym.isCommon()) {
sym = ctx.symtab.addCommon(this, symName, objSym.getCommonSize());
} else if (objSym.isWeak() && objSym.isIndirect()) {
Expand Down
40 changes: 40 additions & 0 deletions lld/test/COFF/lto-weak-undefined.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
; REQUIRES: x86

Copy link
Member

Choose a reason for hiding this comment

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

Add a file-level comment about the purpose as this may be less intuitive.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I added this now.

;; Test linking of weak symbols with LTO. The weak symbol may be defined
;; by another object file, or may be left undefined. When compiling the
;; IR with an undefined weak symbol, the emitted object file will contain
;; a weak alias pointing at an absolute symbol for the address null.
;; Make sure both cases can be linked correctly.

; RUN: split-file %s %t.dir
; RUN: llvm-as %t.dir/main.ll -o %t.main.obj
; RUN: llvm-as %t.dir/optional.ll -o %t.optional.obj

; RUN: lld-link /entry:main %t.main.obj /out:%t-undef.exe
; RUN: lld-link /entry:main %t.main.obj %t.optional.obj /out:%t-def.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"
target triple = "x86_64-pc-windows-msvc"

define dso_local i32 @main() {
entry:
br i1 icmp ne (ptr @optionalFunc, ptr null), label %if.then, label %if.end

if.then:
tail call void @optionalFunc()
br label %if.end

if.end:
ret i32 0
}

declare extern_weak void @optionalFunc()

;--- optional.ll
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc"

define void @optionalFunc() {
ret void
}