Skip to content

Commit e1a003d

Browse files
authored
[lld-macho][NFC] Refactor ObjCSelRefsSection => ObjCSelRefsHelper (#86456)
In a previous PR: #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.
1 parent e6f63a9 commit e1a003d

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

lld/MachO/Driver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
13941394
syntheticSections.clear();
13951395
thunkMap.clear();
13961396
unprocessedLCLinkerOptions.clear();
1397+
ObjCSelRefsHelper::cleanup();
13971398

13981399
firstTLVDataSection = nullptr;
13991400
tar = nullptr;

lld/MachO/SyntheticSections.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,9 @@ void StubHelperSection::setUp() {
806806
dyldPrivate->used = true;
807807
}
808808

809-
ObjCSelRefsSection::ObjCSelRefsSection()
810-
: SyntheticSection(segment_names::data, section_names::objcSelrefs) {}
811-
812-
void ObjCSelRefsSection::initialize() {
809+
llvm::DenseMap<llvm::CachedHashStringRef, ConcatInputSection *>
810+
ObjCSelRefsHelper::methnameToSelref;
811+
void ObjCSelRefsHelper::initialize() {
813812
// Do not fold selrefs without ICF.
814813
if (config->icfLevel == ICFLevel::none)
815814
return;
@@ -836,7 +835,9 @@ void ObjCSelRefsSection::initialize() {
836835
}
837836
}
838837

839-
ConcatInputSection *ObjCSelRefsSection::makeSelRef(StringRef methname) {
838+
void ObjCSelRefsHelper::cleanup() { methnameToSelref.clear(); }
839+
840+
ConcatInputSection *ObjCSelRefsHelper::makeSelRef(StringRef methname) {
840841
auto methnameOffset =
841842
in.objcMethnameSection->getStringOffset(methname).outSecOff;
842843

@@ -861,7 +862,7 @@ ConcatInputSection *ObjCSelRefsSection::makeSelRef(StringRef methname) {
861862
return objcSelref;
862863
}
863864

864-
ConcatInputSection *ObjCSelRefsSection::getSelRef(StringRef methname) {
865+
ConcatInputSection *ObjCSelRefsHelper::getSelRef(StringRef methname) {
865866
auto it = methnameToSelref.find(CachedHashStringRef(methname));
866867
if (it == methnameToSelref.end())
867868
return nullptr;
@@ -890,8 +891,8 @@ StringRef ObjCStubsSection::getMethname(Symbol *sym) {
890891
void ObjCStubsSection::addEntry(Symbol *sym) {
891892
StringRef methname = getMethname(sym);
892893
// We create a selref entry for each unique methname.
893-
if (!in.objcSelRefs->getSelRef(methname))
894-
in.objcSelRefs->makeSelRef(methname);
894+
if (!ObjCSelRefsHelper::getSelRef(methname))
895+
ObjCSelRefsHelper::makeSelRef(methname);
895896

896897
auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
897898
? target->objcStubsFastSize
@@ -940,7 +941,7 @@ void ObjCStubsSection::writeTo(uint8_t *buf) const {
940941
Defined *sym = symbols[i];
941942

942943
auto methname = getMethname(sym);
943-
InputSection *selRef = in.objcSelRefs->getSelRef(methname);
944+
InputSection *selRef = ObjCSelRefsHelper::getSelRef(methname);
944945
assert(selRef != nullptr && "no selref for methname");
945946
auto selrefAddr = selRef->getVA(0);
946947
target->writeObjCMsgSendStub(buf + stubOffset, sym, in.objcStubs->addr,

lld/MachO/SyntheticSections.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -315,24 +315,16 @@ class StubHelperSection final : public SyntheticSection {
315315
Defined *dyldPrivate = nullptr;
316316
};
317317

318-
class ObjCSelRefsSection final : public SyntheticSection {
318+
class ObjCSelRefsHelper {
319319
public:
320-
ObjCSelRefsSection();
321-
void initialize();
322-
323-
// This SyntheticSection does not do directly write data to the output, it is
324-
// just a placeholder for easily creating SyntheticInputSection's which will
325-
// be inserted into inputSections and handeled by the default writing
326-
// mechanism.
327-
uint64_t getSize() const override { return 0; }
328-
bool isNeeded() const override { return false; }
329-
void writeTo(uint8_t *buf) const override {}
320+
static void initialize();
321+
static void cleanup();
330322

331-
ConcatInputSection *getSelRef(StringRef methname);
332-
ConcatInputSection *makeSelRef(StringRef methname);
323+
static ConcatInputSection *getSelRef(StringRef methname);
324+
static ConcatInputSection *makeSelRef(StringRef methname);
333325

334326
private:
335-
llvm::DenseMap<llvm::CachedHashStringRef, ConcatInputSection *>
327+
static llvm::DenseMap<llvm::CachedHashStringRef, ConcatInputSection *>
336328
methnameToSelref;
337329
};
338330

@@ -813,7 +805,6 @@ struct InStruct {
813805
LazyPointerSection *lazyPointers = nullptr;
814806
StubsSection *stubs = nullptr;
815807
StubHelperSection *stubHelper = nullptr;
816-
ObjCSelRefsSection *objcSelRefs = nullptr;
817808
ObjCStubsSection *objcStubs = nullptr;
818809
UnwindInfoSection *unwindInfo = nullptr;
819810
ObjCImageInfoSection *objCImageInfo = nullptr;

lld/MachO/Writer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ static void addNonWeakDefinition(const Defined *defined) {
720720

721721
void Writer::scanSymbols() {
722722
TimeTraceScope timeScope("Scan symbols");
723-
in.objcSelRefs->initialize();
723+
ObjCSelRefsHelper::initialize();
724724
for (Symbol *sym : symtab->getSymbols()) {
725725
if (auto *defined = dyn_cast<Defined>(sym)) {
726726
if (!defined->isLive())
@@ -1359,7 +1359,6 @@ void macho::createSyntheticSections() {
13591359
in.got = make<GotSection>();
13601360
in.tlvPointers = make<TlvPointerSection>();
13611361
in.stubs = make<StubsSection>();
1362-
in.objcSelRefs = make<ObjCSelRefsSection>();
13631362
in.objcStubs = make<ObjCStubsSection>();
13641363
in.unwindInfo = makeUnwindInfoSection();
13651364
in.objCImageInfo = make<ObjCImageInfoSection>();

0 commit comments

Comments
 (0)