Skip to content

Commit

Permalink
[lld-macho] Add support for category names in ConcatInputSection's (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
alx32 committed May 6, 2024
1 parent 5fa24ac commit 9fc0b18
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions lld/MachO/ObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,26 @@ ObjcCategoryChecker::ObjcCategoryChecker()
roClassLayout(target->wordSize), listHeaderLayout(target->wordSize),
methodLayout(target->wordSize) {}

// \p r must point to an offset within a cstring section.
// \p r must point to an offset within a CStringInputSection or a
// ConcatInputSection
static StringRef getReferentString(const Reloc &r) {
if (auto *isec = r.referent.dyn_cast<InputSection *>())
return cast<CStringInputSection>(isec)->getStringRefAtOffset(r.addend);

auto *sym = cast<Defined>(r.referent.get<Symbol *>());
return cast<CStringInputSection>(sym->isec())
->getStringRefAtOffset(sym->value + r.addend);
auto *symIsec = sym->isec();
auto symOffset = sym->value + r.addend;

if (auto *s = dyn_cast_or_null<CStringInputSection>(symIsec))
return s->getStringRefAtOffset(symOffset);

if (isa<ConcatInputSection>(symIsec)) {
auto strData = symIsec->data.slice(symOffset);
const char *pszData = reinterpret_cast<const char *>(strData.data());
return StringRef(pszData, strnlen(pszData, strData.size()));
}

llvm_unreachable("unknown reference section in getReferentString");
}

void ObjcCategoryChecker::parseMethods(const ConcatInputSection *methodsIsec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ __OBJC_$_CATEGORY_MyBaseClass_$_Category01:
.quad 0
.long 64 ; 0x40
.space 4
.section __TEXT,__objc_classname,cstring_literals
.section __DATA,__objc_const
l_OBJC_CLASS_NAME_.1: ; @OBJC_CLASS_NAME_.1
.asciz "Category02"
.section __TEXT,__objc_methname,cstring_literals
Expand Down

0 comments on commit 9fc0b18

Please sign in to comment.