Skip to content

Commit 210e898

Browse files
authored
[lld/mac] Resolve defined symbols before undefined symbols in bitcode (#67445)
Ports https://reviews.llvm.org/D106293 to bitcode, or bd448f01a6 from ELF to MachO. See also #59162 for some vaguely related discussion.
1 parent 2b7227d commit 210e898

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

lld/MachO/InputFiles.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,9 +2291,16 @@ void BitcodeFile::parse() {
22912291
// Convert LTO Symbols to LLD Symbols in order to perform resolution. The
22922292
// "winning" symbol will then be marked as Prevailing at LTO compilation
22932293
// time.
2294-
symbols.clear();
2295-
for (const lto::InputFile::Symbol &objSym : obj->symbols())
2296-
symbols.push_back(createBitcodeSymbol(objSym, *this));
2294+
symbols.resize(obj->symbols().size());
2295+
2296+
// Process defined symbols first. See the comment at the end of
2297+
// ObjFile<>::parseSymbols.
2298+
for (auto it : llvm::enumerate(obj->symbols()))
2299+
if (!it.value().isUndefined())
2300+
symbols[it.index()] = createBitcodeSymbol(it.value(), *this);
2301+
for (auto it : llvm::enumerate(obj->symbols()))
2302+
if (it.value().isUndefined())
2303+
symbols[it.index()] = createBitcodeSymbol(it.value(), *this);
22972304
}
22982305

22992306
void BitcodeFile::parseLazy() {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; REQUIRES: aarch64
2+
; RUN: rm -rf %t; split-file %s %t
3+
4+
;; Test that a weak symbol in a direct .o file wins over
5+
;; a weak symbol in a .a file.
6+
;; Like weak-definition-in-main-file.s, but in bitcode.
7+
8+
; RUN: llvm-as %t/test.ll -o %t/test.o
9+
; RUN: llvm-as %t/weakfoo.ll -o %t/weakfoo.o
10+
11+
; RUN: llvm-ar --format=darwin rcs %t/weakfoo.a %t/weakfoo.o
12+
13+
; PREFER-DIRECT-OBJECT-NOT: O __TEXT,weak _foo
14+
15+
; RUN: %lld -lSystem -o %t/out %t/weakfoo.a %t/test.o
16+
; RUN: llvm-objdump --syms %t/out | FileCheck %s --check-prefix=PREFER-DIRECT-OBJECT
17+
18+
;--- weakfoo.ll
19+
target triple = "x86_64-apple-macosx10.15.0"
20+
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
21+
22+
define void @baz() noinline optnone {
23+
ret void
24+
}
25+
26+
define weak void @foo() noinline optnone section "__TEXT,weak" {
27+
ret void
28+
}
29+
30+
;--- test.ll
31+
target triple = "x86_64-apple-macosx10.15.0"
32+
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
33+
34+
declare void @baz();
35+
36+
define weak void @foo() noinline optnone {
37+
ret void
38+
}
39+
40+
define void @main() {
41+
; This pulls in weakfoo.a due to the __baz undef, but __foo should
42+
; still be resolved against the weak symbol in this file.
43+
call void @baz()
44+
call void @foo()
45+
ret void
46+
}

0 commit comments

Comments
 (0)