Skip to content

Commit 141391a

Browse files
NuriAmariNuri Amari
andauthored
[lld] Fix -ObjC load behavior with LTO (#92162)
When -ObjC is passed, the linker must force load any object files that contain special sections that store Objective-C / Swift information that is used at runtime. This should work regadless if input files are bitcode or native, but it was not working with bitcode. This is because the sections that identify an object file that should be loaded were inconsistent when dealing with a native file vs bitcode file. In particular, bitcode files were not searched for `__TEXT,__swift` prefixed sections, while native files were. This means LLD wasn't loading certain bitcode files and forcing the user to introduce --force-load to their linker invocation for that archive. Co-authored-by: Nuri Amari <[email protected]>
1 parent 03c53c6 commit 141391a

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

lld/test/MachO/objc.s

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/has-objc-category.s -o %t/has-objc-category.o
66
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/has-objc-symbol-and-category.s -o %t/has-objc-symbol-and-category.o
77
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/has-swift.s -o %t/has-swift.o
8+
# RUN: llvm-as %t/has-swift-ir-loaded.ll -o %t/has-swift-ir-loaded.o
9+
# RUN: llvm-as %t/has-swift-ir-not-loaded.ll -o %t/has-swift-ir-not-loaded.o
810
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/has-swift-proto.s -o %t/has-swift-proto.o
911
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/no-objc.s -o %t/no-objc.o
1012
## Make sure we don't mis-parse a 32-bit file as 64-bit
1113
# RUN: llvm-mc -filetype=obj -triple=armv7-apple-watchos %t/no-objc.s -o %t/wrong-arch.o
12-
# RUN: llvm-ar rcs %t/libHasSomeObjC.a %t/no-objc.o %t/has-objc-symbol.o %t/has-objc-category.o %t/has-swift.o %t/has-swift-proto.o %t/wrong-arch.o
13-
# RUN: llvm-ar rcs %t/libHasSomeObjC2.a %t/no-objc.o %t/has-objc-symbol-and-category.o %t/has-swift.o %t/has-swift-proto.o %t/wrong-arch.o
14+
# RUN: llvm-ar rcs %t/libHasSomeObjC.a %t/no-objc.o %t/has-objc-symbol.o %t/has-objc-category.o %t/has-swift.o %t/has-swift-proto.o %t/has-swift-ir-loaded.o %t/has-swift-ir-not-loaded.o %t/wrong-arch.o
15+
# RUN: llvm-ar rcs %t/libHasSomeObjC2.a %t/no-objc.o %t/has-objc-symbol-and-category.o %t/has-swift.o %t/has-swift-proto.o %t/has-swift-ir-loaded.o %t/has-swift-ir-not-loaded.o %t/wrong-arch.o
1416

1517
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
1618

@@ -20,7 +22,7 @@
2022
# RUN: %lld -lSystem %t/test.o -o %t/test -L%t -lHasSomeObjC2 -ObjC
2123
# RUN: llvm-objdump --section-headers --syms %t/test | FileCheck %s --check-prefix=OBJC
2224

23-
# RUN: %no-fatal-warnings-lld -lSystem %t/test.o -o %t/test --start-lib %t/no-objc.o %t/has-objc-symbol.o %t/has-objc-category.o %t/has-swift.o %t/has-swift-proto.o %t/wrong-arch.o --end-lib -ObjC 2>&1 \
25+
# RUN: %no-fatal-warnings-lld -lSystem %t/test.o -o %t/test --start-lib %t/no-objc.o %t/has-objc-symbol.o %t/has-objc-category.o %t/has-swift.o %t/has-swift-proto.o %t/has-swift-ir-loaded.o %t/has-swift-ir-not-loaded.o %t/wrong-arch.o --end-lib -ObjC 2>&1 \
2426
# RUN: | FileCheck -check-prefix=WARNING %s
2527
# RUN: llvm-objdump --section-headers --syms %t/test | FileCheck %s --check-prefix=OBJC
2628

@@ -36,6 +38,7 @@
3638
# OBJC-NEXT: 4 has_objc_symbol {{.*}} DATA
3739
# OBJC-EMPTY:
3840
# OBJC-NEXT: SYMBOL TABLE:
41+
# OBJC-DAG: g O __TEXT,__swift _foo
3942
# OBJC-DAG: g F __TEXT,__text _main
4043
# OBJC-DAG: g F __TEXT,__text _OBJC_CLASS_$_MyObject
4144
# OBJC-DAG: g O __TEXT,__swift5_fieldmd $s7somelib4Blah_pMF
@@ -100,6 +103,20 @@ _has_dup:
100103
.section __TEXT,__swift
101104
.quad 0x1234
102105

106+
#--- has-swift-ir-loaded.ll
107+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
108+
target triple = "x86_64-apple-darwin"
109+
110+
@foo = global i64 1234, section "__TEXT,__swift"
111+
@llvm.used = appending global [1 x ptr] [ptr @foo]
112+
113+
#--- has-swift-ir-not-loaded.ll
114+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
115+
target triple = "x86_64-apple-darwin"
116+
117+
@bar = global i64 1234
118+
@llvm.used = appending global [1 x ptr] [ptr @bar]
119+
103120
#--- has-swift-proto.s
104121
.section __TEXT,__swift5_fieldmd
105122
.globl $s7somelib4Blah_pMF

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) {
306306
return error("Invalid section name record");
307307
// Check for the i386 and other (x86_64, ARM) conventions
308308
if (S.find("__DATA,__objc_catlist") != std::string::npos ||
309-
S.find("__OBJC,__category") != std::string::npos)
309+
S.find("__OBJC,__category") != std::string::npos ||
310+
S.find("__TEXT,__swift") != std::string::npos)
310311
return true;
311312
break;
312313
}

0 commit comments

Comments
 (0)