[lld-macho] dead-strip objc stubs (#79726)

This supports dead-strip for objc stubs.
This commit is contained in:
Kyungwoo Lee 2024-01-29 23:29:57 -08:00 committed by GitHub
parent 67f0a6917c
commit cb46c61817
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 52 additions and 6 deletions

View File

@ -1275,11 +1275,10 @@ static void foldIdenticalLiterals() {
static void addSynthenticMethnames() {
std::string &data = *make<std::string>();
llvm::raw_string_ostream os(data);
const int prefixLength = ObjCStubsSection::symbolPrefix.size();
for (Symbol *sym : symtab->getSymbols())
if (isa<Undefined>(sym))
if (sym->getName().starts_with(ObjCStubsSection::symbolPrefix))
os << sym->getName().drop_front(prefixLength) << '\0';
if (ObjCStubsSection::isObjCStubSymbol(sym))
os << ObjCStubsSection::getMethname(sym) << '\0';
if (data.empty())
return;

View File

@ -814,9 +814,19 @@ ObjCStubsSection::ObjCStubsSection()
: target->objcStubsSmallAlignment;
}
bool ObjCStubsSection::isObjCStubSymbol(Symbol *sym) {
return sym->getName().starts_with(symbolPrefix);
}
StringRef ObjCStubsSection::getMethname(Symbol *sym) {
assert(isObjCStubSymbol(sym) && "not an objc stub");
auto name = sym->getName();
StringRef methname = name.drop_front(symbolPrefix.size());
return methname;
}
void ObjCStubsSection::addEntry(Symbol *sym) {
assert(sym->getName().starts_with(symbolPrefix) && "not an objc stub");
StringRef methname = sym->getName().drop_front(symbolPrefix.size());
StringRef methname = getMethname(sym);
offsets.push_back(
in.objcMethnameSection->getStringOffset(methname).outSecOff);

View File

@ -332,6 +332,8 @@ public:
void setUp();
static constexpr llvm::StringLiteral symbolPrefix = "_objc_msgSend$";
static bool isObjCStubSymbol(Symbol *sym);
static StringRef getMethname(Symbol *sym);
private:
std::vector<Defined *> symbols;

View File

@ -736,8 +736,16 @@ void Writer::scanSymbols() {
dysym->getFile()->refState =
std::max(dysym->getFile()->refState, dysym->getRefState());
} else if (isa<Undefined>(sym)) {
if (sym->getName().starts_with(ObjCStubsSection::symbolPrefix))
if (ObjCStubsSection::isObjCStubSymbol(sym)) {
// When -dead_strip is enabled, we don't want to emit any dead stubs.
// Although this stub symbol is yet undefined, addSym() was called
// during MarkLive.
if (config->deadStrip) {
if (!sym->isLive())
continue;
}
in.objcStubs->addEntry(sym);
}
}
}

View File

@ -0,0 +1,27 @@
# REQUIRES: aarch64
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t.o
# RUN: %lld -arch arm64 -lSystem -U _objc_msgSend -o %t.out %t.o
# RUN: llvm-nm %t.out | FileCheck %s
# RUN: %lld -arch arm64 -lSystem -U _objc_msgSend -dead_strip -o %t.out %t.o
# RUN: llvm-nm %t.out | FileCheck %s --check-prefix=DEAD
# CHECK: _foo
# CHECK: _objc_msgSend$length
# DEAD-NOT: _foo
# DEAD-NOT: _objc_msgSend$length
.section __TEXT,__text
.globl _foo
_foo:
bl _objc_msgSend$length
ret
.globl _main
_main:
ret
.subsections_via_symbols