[lld-macho][NFC] Refactor ObjCSelRefsSection => ObjCSelRefsHelper (#86456)

In a previous PR: https://github.com/llvm/llvm-project/pull/83878, the
intent was to make no functional changes, just refactor out the code for
reuse.
However, by creating `ObjCSelRefsSection` as a `SyntheticSection` - this
slightly changed the functionality of the application as the
`SyntheticSection` constructor registers the `SyntheticSection` as a
functional one - with an associated `SyntheticInputSection`.

With this change we remove this unintended consequence by making the
code not use a `SyntheticSection` as base, but just by having it be a
static helper.
This commit is contained in:
alx32 2024-03-25 06:55:11 -07:00 committed by GitHub
parent e6f63a942a
commit e1a003dbbd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 26 deletions

View File

@ -1394,6 +1394,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
syntheticSections.clear();
thunkMap.clear();
unprocessedLCLinkerOptions.clear();
ObjCSelRefsHelper::cleanup();
firstTLVDataSection = nullptr;
tar = nullptr;

View File

@ -806,10 +806,9 @@ void StubHelperSection::setUp() {
dyldPrivate->used = true;
}
ObjCSelRefsSection::ObjCSelRefsSection()
: SyntheticSection(segment_names::data, section_names::objcSelrefs) {}
void ObjCSelRefsSection::initialize() {
llvm::DenseMap<llvm::CachedHashStringRef, ConcatInputSection *>
ObjCSelRefsHelper::methnameToSelref;
void ObjCSelRefsHelper::initialize() {
// Do not fold selrefs without ICF.
if (config->icfLevel == ICFLevel::none)
return;
@ -836,7 +835,9 @@ void ObjCSelRefsSection::initialize() {
}
}
ConcatInputSection *ObjCSelRefsSection::makeSelRef(StringRef methname) {
void ObjCSelRefsHelper::cleanup() { methnameToSelref.clear(); }
ConcatInputSection *ObjCSelRefsHelper::makeSelRef(StringRef methname) {
auto methnameOffset =
in.objcMethnameSection->getStringOffset(methname).outSecOff;
@ -861,7 +862,7 @@ ConcatInputSection *ObjCSelRefsSection::makeSelRef(StringRef methname) {
return objcSelref;
}
ConcatInputSection *ObjCSelRefsSection::getSelRef(StringRef methname) {
ConcatInputSection *ObjCSelRefsHelper::getSelRef(StringRef methname) {
auto it = methnameToSelref.find(CachedHashStringRef(methname));
if (it == methnameToSelref.end())
return nullptr;
@ -890,8 +891,8 @@ StringRef ObjCStubsSection::getMethname(Symbol *sym) {
void ObjCStubsSection::addEntry(Symbol *sym) {
StringRef methname = getMethname(sym);
// We create a selref entry for each unique methname.
if (!in.objcSelRefs->getSelRef(methname))
in.objcSelRefs->makeSelRef(methname);
if (!ObjCSelRefsHelper::getSelRef(methname))
ObjCSelRefsHelper::makeSelRef(methname);
auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
? target->objcStubsFastSize
@ -940,7 +941,7 @@ void ObjCStubsSection::writeTo(uint8_t *buf) const {
Defined *sym = symbols[i];
auto methname = getMethname(sym);
InputSection *selRef = in.objcSelRefs->getSelRef(methname);
InputSection *selRef = ObjCSelRefsHelper::getSelRef(methname);
assert(selRef != nullptr && "no selref for methname");
auto selrefAddr = selRef->getVA(0);
target->writeObjCMsgSendStub(buf + stubOffset, sym, in.objcStubs->addr,

View File

@ -315,24 +315,16 @@ public:
Defined *dyldPrivate = nullptr;
};
class ObjCSelRefsSection final : public SyntheticSection {
class ObjCSelRefsHelper {
public:
ObjCSelRefsSection();
void initialize();
static void initialize();
static void cleanup();
// This SyntheticSection does not do directly write data to the output, it is
// just a placeholder for easily creating SyntheticInputSection's which will
// be inserted into inputSections and handeled by the default writing
// mechanism.
uint64_t getSize() const override { return 0; }
bool isNeeded() const override { return false; }
void writeTo(uint8_t *buf) const override {}
ConcatInputSection *getSelRef(StringRef methname);
ConcatInputSection *makeSelRef(StringRef methname);
static ConcatInputSection *getSelRef(StringRef methname);
static ConcatInputSection *makeSelRef(StringRef methname);
private:
llvm::DenseMap<llvm::CachedHashStringRef, ConcatInputSection *>
static llvm::DenseMap<llvm::CachedHashStringRef, ConcatInputSection *>
methnameToSelref;
};
@ -813,7 +805,6 @@ struct InStruct {
LazyPointerSection *lazyPointers = nullptr;
StubsSection *stubs = nullptr;
StubHelperSection *stubHelper = nullptr;
ObjCSelRefsSection *objcSelRefs = nullptr;
ObjCStubsSection *objcStubs = nullptr;
UnwindInfoSection *unwindInfo = nullptr;
ObjCImageInfoSection *objCImageInfo = nullptr;

View File

@ -720,7 +720,7 @@ static void addNonWeakDefinition(const Defined *defined) {
void Writer::scanSymbols() {
TimeTraceScope timeScope("Scan symbols");
in.objcSelRefs->initialize();
ObjCSelRefsHelper::initialize();
for (Symbol *sym : symtab->getSymbols()) {
if (auto *defined = dyn_cast<Defined>(sym)) {
if (!defined->isLive())
@ -1359,7 +1359,6 @@ void macho::createSyntheticSections() {
in.got = make<GotSection>();
in.tlvPointers = make<TlvPointerSection>();
in.stubs = make<StubsSection>();
in.objcSelRefs = make<ObjCSelRefsSection>();
in.objcStubs = make<ObjCStubsSection>();
in.unwindInfo = makeUnwindInfoSection();
in.objCImageInfo = make<ObjCImageInfoSection>();