Skip to content

Commit 0df9189

Browse files
authored
[lld-macho] Fix crash: ObjC category merge + relative method lists (#104081)
A crash was happening when both ObjC Category Merging and Relative method lists were enabled. ObjC Category Merging creates new data sections and adds them by calling `addInputSection`. `addInputSection` uses the symbols within the added section to determine which container to actually add the section to. The issue is that ObjC Category merging is calling `addInputSection` before actually adding the relevant symbols the the added section. This causes `addInputSection` to add the `InputSection` to the wrong container, eventually resulting in a crash. To fix this, we ensure that ObjC Category Merging calls `addInputSection` only after the symbols have been added to the `InputSection`.
1 parent bd47ba7 commit 0df9189

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

lld/MachO/ObjC.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,6 @@ Defined *ObjcCategoryMerger::emitAndLinkProtocolList(
851851
infoCategoryWriter.catPtrListInfo.align);
852852
listSec->parent = infoCategoryWriter.catPtrListInfo.outputSection;
853853
listSec->live = true;
854-
addInputSection(listSec);
855854

856855
listSec->parent = infoCategoryWriter.catPtrListInfo.outputSection;
857856

@@ -867,6 +866,7 @@ Defined *ObjcCategoryMerger::emitAndLinkProtocolList(
867866

868867
ptrListSym->used = true;
869868
parentSym->getObjectFile()->symbols.push_back(ptrListSym);
869+
addInputSection(listSec);
870870

871871
createSymbolReference(parentSym, ptrListSym, linkAtOffset,
872872
infoCategoryWriter.catBodyInfo.relocTemplate);
@@ -911,7 +911,6 @@ void ObjcCategoryMerger::emitAndLinkPointerList(
911911
infoCategoryWriter.catPtrListInfo.align);
912912
listSec->parent = infoCategoryWriter.catPtrListInfo.outputSection;
913913
listSec->live = true;
914-
addInputSection(listSec);
915914

916915
listSec->parent = infoCategoryWriter.catPtrListInfo.outputSection;
917916

@@ -927,6 +926,7 @@ void ObjcCategoryMerger::emitAndLinkPointerList(
927926

928927
ptrListSym->used = true;
929928
parentSym->getObjectFile()->symbols.push_back(ptrListSym);
929+
addInputSection(listSec);
930930

931931
createSymbolReference(parentSym, ptrListSym, linkAtOffset,
932932
infoCategoryWriter.catBodyInfo.relocTemplate);
@@ -952,7 +952,6 @@ ObjcCategoryMerger::emitCatListEntrySec(const std::string &forCategoryName,
952952
bodyData, infoCategoryWriter.catListInfo.align);
953953
newCatList->parent = infoCategoryWriter.catListInfo.outputSection;
954954
newCatList->live = true;
955-
addInputSection(newCatList);
956955

957956
newCatList->parent = infoCategoryWriter.catListInfo.outputSection;
958957

@@ -968,6 +967,7 @@ ObjcCategoryMerger::emitCatListEntrySec(const std::string &forCategoryName,
968967

969968
catListSym->used = true;
970969
objFile->symbols.push_back(catListSym);
970+
addInputSection(newCatList);
971971
return catListSym;
972972
}
973973

@@ -990,7 +990,6 @@ Defined *ObjcCategoryMerger::emitCategoryBody(const std::string &name,
990990
bodyData, infoCategoryWriter.catBodyInfo.align);
991991
newBodySec->parent = infoCategoryWriter.catBodyInfo.outputSection;
992992
newBodySec->live = true;
993-
addInputSection(newBodySec);
994993

995994
std::string symName =
996995
objc::symbol_names::category + baseClassName + "(" + name + ")";
@@ -1003,6 +1002,7 @@ Defined *ObjcCategoryMerger::emitCategoryBody(const std::string &name,
10031002

10041003
catBodySym->used = true;
10051004
objFile->symbols.push_back(catBodySym);
1005+
addInputSection(newBodySec);
10061006

10071007
createSymbolReference(catBodySym, nameSym, catLayout.nameOffset,
10081008
infoCategoryWriter.catBodyInfo.relocTemplate);
@@ -1223,7 +1223,6 @@ void ObjcCategoryMerger::generateCatListForNonErasedCategories(
12231223
infoCategoryWriter.catListInfo.align);
12241224
listSec->parent = infoCategoryWriter.catListInfo.outputSection;
12251225
listSec->live = true;
1226-
addInputSection(listSec);
12271226

12281227
std::string slotSymName = "<__objc_catlist slot for category ";
12291228
slotSymName += nonErasedCatBody->getName();
@@ -1238,6 +1237,7 @@ void ObjcCategoryMerger::generateCatListForNonErasedCategories(
12381237

12391238
catListSlotSym->used = true;
12401239
objFile->symbols.push_back(catListSlotSym);
1240+
addInputSection(listSec);
12411241

12421242
// Now link the category body into the newly created slot
12431243
createSymbolReference(catListSlotSym, nonErasedCatBody, 0,

lld/test/MachO/objc-category-merging-minimal.s

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
## Create our main testing dylib - linking against the fake dylib above
1010
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-macos -o merge_cat_minimal.o merge_cat_minimal.s
1111
# RUN: %lld -arch arm64 -dylib -o merge_cat_minimal_no_merge.dylib a64_fakedylib.dylib merge_cat_minimal.o
12-
# RUN: %lld -arch arm64 -dylib -o merge_cat_minimal_merge.dylib -objc_category_merging a64_fakedylib.dylib merge_cat_minimal.o
12+
# RUN: %lld -objc_relative_method_lists -arch arm64 -dylib -o merge_cat_minimal_merge.dylib -objc_category_merging a64_fakedylib.dylib merge_cat_minimal.o
1313

1414
## Now verify that the flag caused category merging to happen appropriatelly
1515
# RUN: llvm-objdump --objc-meta-data --macho merge_cat_minimal_no_merge.dylib | FileCheck %s --check-prefixes=NO_MERGE_CATS
1616
# RUN: llvm-objdump --objc-meta-data --macho merge_cat_minimal_merge.dylib | FileCheck %s --check-prefixes=MERGE_CATS
1717

1818
############ Test merging multiple categories into the base class ############
1919
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-macos -o merge_base_class_minimal.o merge_base_class_minimal.s
20-
# RUN: %lld -arch arm64 -dylib -o merge_base_class_minimal_yes_merge.dylib -objc_category_merging merge_base_class_minimal.o merge_cat_minimal.o
20+
# RUN: %lld -arch arm64 -dylib -objc_relative_method_lists -o merge_base_class_minimal_yes_merge.dylib -objc_category_merging merge_base_class_minimal.o merge_cat_minimal.o
2121
# RUN: %lld -arch arm64 -dylib -o merge_base_class_minimal_no_merge.dylib merge_base_class_minimal.o merge_cat_minimal.o
2222

2323
# RUN: llvm-objdump --objc-meta-data --macho merge_base_class_minimal_no_merge.dylib | FileCheck %s --check-prefixes=NO_MERGE_INTO_BASE
@@ -37,14 +37,14 @@ MERGE_CATS-NOT: __OBJC_$_CATEGORY_MyBaseClass_$_Category02
3737
MERGE_CATS: __OBJC_$_CATEGORY_MyBaseClass(Category01|Category02)
3838
MERGE_CATS-NEXT: name {{.*}} Category01|Category02
3939
MERGE_CATS: instanceMethods
40-
MERGE_CATS-NEXT: 24
41-
MERGE_CATS-NEXT: 2
40+
MERGE_CATS-NEXT: entsize 12 (relative)
41+
MERGE_CATS-NEXT: count 2
4242
MERGE_CATS-NEXT: name {{.*}} cat01_InstanceMethod
4343
MERGE_CATS-NEXT: types {{.*}} v16@0:8
44-
MERGE_CATS-NEXT: imp -[MyBaseClass(Category01) cat01_InstanceMethod]
44+
MERGE_CATS-NEXT: imp {{.*}} -[MyBaseClass(Category01) cat01_InstanceMethod]
4545
MERGE_CATS-NEXT: name {{.*}} cat02_InstanceMethod
4646
MERGE_CATS-NEXT: types {{.*}} v16@0:8
47-
MERGE_CATS-NEXT: imp -[MyBaseClass(Category02) cat02_InstanceMethod]
47+
MERGE_CATS-NEXT: imp {{.*}} -[MyBaseClass(Category02) cat02_InstanceMethod]
4848
MERGE_CATS-NEXT: classMethods 0x0
4949
MERGE_CATS-NEXT: protocols 0x0
5050
MERGE_CATS-NEXT: instanceProperties 0x0
@@ -69,17 +69,17 @@ YES_MERGE_INTO_BASE-NOT: __OBJC_$_CATEGORY_MyBaseClass_$_Category02
6969
YES_MERGE_INTO_BASE: _OBJC_CLASS_$_MyBaseClass
7070
YES_MERGE_INTO_BASE-NEXT: _OBJC_METACLASS_$_MyBaseClass
7171
YES_MERGE_INTO_BASE: baseMethods
72-
YES_MERGE_INTO_BASE-NEXT: entsize 24
72+
YES_MERGE_INTO_BASE-NEXT: entsize 12 (relative)
7373
YES_MERGE_INTO_BASE-NEXT: count 3
7474
YES_MERGE_INTO_BASE-NEXT: name {{.*}} cat01_InstanceMethod
7575
YES_MERGE_INTO_BASE-NEXT: types {{.*}} v16@0:8
76-
YES_MERGE_INTO_BASE-NEXT: imp -[MyBaseClass(Category01) cat01_InstanceMethod]
76+
YES_MERGE_INTO_BASE-NEXT: imp {{.*}} -[MyBaseClass(Category01) cat01_InstanceMethod]
7777
YES_MERGE_INTO_BASE-NEXT: name {{.*}} cat02_InstanceMethod
7878
YES_MERGE_INTO_BASE-NEXT: types {{.*}} v16@0:8
79-
YES_MERGE_INTO_BASE-NEXT: imp -[MyBaseClass(Category02) cat02_InstanceMethod]
79+
YES_MERGE_INTO_BASE-NEXT: imp {{.*}} -[MyBaseClass(Category02) cat02_InstanceMethod]
8080
YES_MERGE_INTO_BASE-NEXT: name {{.*}} baseInstanceMethod
8181
YES_MERGE_INTO_BASE-NEXT: types {{.*}} v16@0:8
82-
YES_MERGE_INTO_BASE-NEXT: imp -[MyBaseClass baseInstanceMethod]
82+
YES_MERGE_INTO_BASE-NEXT: imp {{.*}} -[MyBaseClass baseInstanceMethod]
8383

8484

8585
#### Check merge swift category into base class ###

0 commit comments

Comments
 (0)