Skip to content

Commit 9fc0b18

Browse files
authored
[lld-macho] Add support for category names in ConcatInputSection's (#90850)
In some cases we see strings from categories being part of "data" sections (Ex:`__objc_const`), not part of of sections marked as `cstring_literals`. Since lld treats these sections differently we need to explicitly implement support for reading strings from the non-`cstring_literals` sections. Adding a test that previously would result in an assert.
1 parent 5fa24ac commit 9fc0b18

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lld/MachO/ObjC.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,26 @@ ObjcCategoryChecker::ObjcCategoryChecker()
186186
roClassLayout(target->wordSize), listHeaderLayout(target->wordSize),
187187
methodLayout(target->wordSize) {}
188188

189-
// \p r must point to an offset within a cstring section.
189+
// \p r must point to an offset within a CStringInputSection or a
190+
// ConcatInputSection
190191
static StringRef getReferentString(const Reloc &r) {
191192
if (auto *isec = r.referent.dyn_cast<InputSection *>())
192193
return cast<CStringInputSection>(isec)->getStringRefAtOffset(r.addend);
194+
193195
auto *sym = cast<Defined>(r.referent.get<Symbol *>());
194-
return cast<CStringInputSection>(sym->isec())
195-
->getStringRefAtOffset(sym->value + r.addend);
196+
auto *symIsec = sym->isec();
197+
auto symOffset = sym->value + r.addend;
198+
199+
if (auto *s = dyn_cast_or_null<CStringInputSection>(symIsec))
200+
return s->getStringRefAtOffset(symOffset);
201+
202+
if (isa<ConcatInputSection>(symIsec)) {
203+
auto strData = symIsec->data.slice(symOffset);
204+
const char *pszData = reinterpret_cast<const char *>(strData.data());
205+
return StringRef(pszData, strnlen(pszData, strData.size()));
206+
}
207+
208+
llvm_unreachable("unknown reference section in getReferentString");
196209
}
197210

198211
void ObjcCategoryChecker::parseMethods(const ConcatInputSection *methodsIsec,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ __OBJC_$_CATEGORY_MyBaseClass_$_Category01:
118118
.quad 0
119119
.long 64 ; 0x40
120120
.space 4
121-
.section __TEXT,__objc_classname,cstring_literals
121+
.section __DATA,__objc_const
122122
l_OBJC_CLASS_NAME_.1: ; @OBJC_CLASS_NAME_.1
123123
.asciz "Category02"
124124
.section __TEXT,__objc_methname,cstring_literals

0 commit comments

Comments
 (0)