Skip to content

Commit c9cbd4e

Browse files
aleksandr-urakovУраков Александр Сергеевичellishg
authored
[lld] Fix -ObjC load behavior with LTO for section names with whitespace (#146654)
This is a fix additional to #92162 In some cases, section names contain a whitespace between the segment name and the actual section name (e.g. `__TEXT, __swift5_proto`). It is confirmed by source code of the Swift compiler This fix allows LTO to work correctly with the `-ObjC` flag in that rare case when only a section with a whitespace in the name is present in the linked bitcode module, but there are no sections containing `__TEXT,__swift` --------- Co-authored-by: Ураков Александр Сергеевич <[email protected]> Co-authored-by: Ellis Hoag <[email protected]>
1 parent 6193dd5 commit c9cbd4e

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

lld/test/MachO/objc.s

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/has-swift.s -o %t/has-swift.o
88
# RUN: llvm-as %t/has-swift-ir-loaded.ll -o %t/has-swift-ir-loaded.o
99
# RUN: llvm-as %t/has-swift-ir-not-loaded.ll -o %t/has-swift-ir-not-loaded.o
10+
# RUN: llvm-as %t/has-swift-with-space-ir-loaded.ll -o %t/has-swift-with-space-ir-loaded.o
1011
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/has-swift-proto.s -o %t/has-swift-proto.o
1112
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/no-objc.s -o %t/no-objc.o
1213
## Make sure we don't mis-parse a 32-bit file as 64-bit
1314
# RUN: llvm-mc -filetype=obj -triple=armv7-apple-watchos %t/no-objc.s -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
15+
# 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/has-swift-with-space-ir-loaded.o %t/wrong-arch.o
16+
# 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/has-swift-with-space-ir-loaded.o %t/wrong-arch.o
1617

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

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

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 \
26+
# 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/has-swift-with-space-ir-loaded.o %t/wrong-arch.o --end-lib -ObjC 2>&1 \
2627
# RUN: | FileCheck -check-prefix=WARNING %s
2728
# RUN: llvm-objdump --section-headers --syms %t/test | FileCheck %s --check-prefix=OBJC
2829

@@ -34,14 +35,16 @@
3435
# OBJC-NEXT: 0 __text {{.*}} TEXT
3536
# OBJC-NEXT: 1 __swift {{.*}} DATA
3637
# OBJC-NEXT: 2 __swift5_fieldmd{{.*}} DATA
37-
# OBJC-NEXT: 3 __objc_catlist {{.*}} DATA
38-
# OBJC-NEXT: 4 has_objc_symbol {{.*}} DATA
38+
# OBJC-NEXT: 3 __swift5_proto {{.*}} DATA
39+
# OBJC-NEXT: 4 __objc_catlist {{.*}} DATA
40+
# OBJC-NEXT: 5 has_objc_symbol {{.*}} DATA
3941
# OBJC-EMPTY:
4042
# OBJC-NEXT: SYMBOL TABLE:
4143
# OBJC-DAG: g O __TEXT,__swift _foo
4244
# OBJC-DAG: g F __TEXT,__text _main
4345
# OBJC-DAG: g F __TEXT,__text _OBJC_CLASS_$_MyObject
4446
# OBJC-DAG: g O __TEXT,__swift5_fieldmd $s7somelib4Blah_pMF
47+
# OBJC-DAG: g O __TEXT,__swift5_proto _baz
4548

4649
# RUN: %lld -lSystem %t/test.o -o %t/test -L%t -lHasSomeObjC
4750
# RUN: llvm-objdump --section-headers --syms %t/test | FileCheck %s --check-prefix=NO-OBJC
@@ -117,6 +120,13 @@ target triple = "x86_64-apple-darwin"
117120
@bar = global i64 1234
118121
@llvm.used = appending global [1 x ptr] [ptr @bar]
119122

123+
#--- has-swift-with-space-ir-loaded.ll
124+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
125+
target triple = "x86_64-apple-darwin"
126+
127+
@baz = global i64 1234, section "__TEXT, __swift5_proto"
128+
@llvm.used = appending global [1 x ptr] [ptr @baz]
129+
120130
#--- has-swift-proto.s
121131
.section __TEXT,__swift5_fieldmd
122132
.globl $s7somelib4Blah_pMF

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,18 @@ static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) {
293293
std::string S;
294294
if (convertToString(Record, 0, S))
295295
return error("Invalid section name record");
296+
296297
// Check for the i386 and other (x86_64, ARM) conventions
297-
if (S.find("__DATA,__objc_catlist") != std::string::npos ||
298-
S.find("__OBJC,__category") != std::string::npos ||
299-
S.find("__TEXT,__swift") != std::string::npos)
298+
299+
auto [Segment, Section] = StringRef(S).split(",");
300+
Segment = Segment.trim();
301+
Section = Section.trim();
302+
303+
if (Segment == "__DATA" && Section.starts_with("__objc_catlist"))
304+
return true;
305+
if (Segment == "__OBJC" && Section.starts_with("__category"))
306+
return true;
307+
if (Segment == "__TEXT" && Section.starts_with("__swift"))
300308
return true;
301309
break;
302310
}

0 commit comments

Comments
 (0)