Skip to content

Commit cb46c61

Browse files
authored
[lld-macho] dead-strip objc stubs (#79726)
This supports dead-strip for objc stubs.
1 parent 67f0a69 commit cb46c61

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

lld/MachO/Driver.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,11 +1275,10 @@ static void foldIdenticalLiterals() {
12751275
static void addSynthenticMethnames() {
12761276
std::string &data = *make<std::string>();
12771277
llvm::raw_string_ostream os(data);
1278-
const int prefixLength = ObjCStubsSection::symbolPrefix.size();
12791278
for (Symbol *sym : symtab->getSymbols())
12801279
if (isa<Undefined>(sym))
1281-
if (sym->getName().starts_with(ObjCStubsSection::symbolPrefix))
1282-
os << sym->getName().drop_front(prefixLength) << '\0';
1280+
if (ObjCStubsSection::isObjCStubSymbol(sym))
1281+
os << ObjCStubsSection::getMethname(sym) << '\0';
12831282

12841283
if (data.empty())
12851284
return;

lld/MachO/SyntheticSections.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,9 +814,19 @@ ObjCStubsSection::ObjCStubsSection()
814814
: target->objcStubsSmallAlignment;
815815
}
816816

817+
bool ObjCStubsSection::isObjCStubSymbol(Symbol *sym) {
818+
return sym->getName().starts_with(symbolPrefix);
819+
}
820+
821+
StringRef ObjCStubsSection::getMethname(Symbol *sym) {
822+
assert(isObjCStubSymbol(sym) && "not an objc stub");
823+
auto name = sym->getName();
824+
StringRef methname = name.drop_front(symbolPrefix.size());
825+
return methname;
826+
}
827+
817828
void ObjCStubsSection::addEntry(Symbol *sym) {
818-
assert(sym->getName().starts_with(symbolPrefix) && "not an objc stub");
819-
StringRef methname = sym->getName().drop_front(symbolPrefix.size());
829+
StringRef methname = getMethname(sym);
820830
offsets.push_back(
821831
in.objcMethnameSection->getStringOffset(methname).outSecOff);
822832

lld/MachO/SyntheticSections.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ class ObjCStubsSection final : public SyntheticSection {
332332
void setUp();
333333

334334
static constexpr llvm::StringLiteral symbolPrefix = "_objc_msgSend$";
335+
static bool isObjCStubSymbol(Symbol *sym);
336+
static StringRef getMethname(Symbol *sym);
335337

336338
private:
337339
std::vector<Defined *> symbols;

lld/MachO/Writer.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,16 @@ void Writer::scanSymbols() {
736736
dysym->getFile()->refState =
737737
std::max(dysym->getFile()->refState, dysym->getRefState());
738738
} else if (isa<Undefined>(sym)) {
739-
if (sym->getName().starts_with(ObjCStubsSection::symbolPrefix))
739+
if (ObjCStubsSection::isObjCStubSymbol(sym)) {
740+
// When -dead_strip is enabled, we don't want to emit any dead stubs.
741+
// Although this stub symbol is yet undefined, addSym() was called
742+
// during MarkLive.
743+
if (config->deadStrip) {
744+
if (!sym->isLive())
745+
continue;
746+
}
740747
in.objcStubs->addEntry(sym);
748+
}
741749
}
742750
}
743751

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# REQUIRES: aarch64
2+
3+
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t.o
4+
5+
# RUN: %lld -arch arm64 -lSystem -U _objc_msgSend -o %t.out %t.o
6+
# RUN: llvm-nm %t.out | FileCheck %s
7+
# RUN: %lld -arch arm64 -lSystem -U _objc_msgSend -dead_strip -o %t.out %t.o
8+
# RUN: llvm-nm %t.out | FileCheck %s --check-prefix=DEAD
9+
10+
# CHECK: _foo
11+
# CHECK: _objc_msgSend$length
12+
13+
# DEAD-NOT: _foo
14+
# DEAD-NOT: _objc_msgSend$length
15+
16+
.section __TEXT,__text
17+
18+
.globl _foo
19+
_foo:
20+
bl _objc_msgSend$length
21+
ret
22+
23+
.globl _main
24+
_main:
25+
ret
26+
27+
.subsections_via_symbols

0 commit comments

Comments
 (0)