Skip to content

Commit b387d5e

Browse files
committed
consistently use methnameToSelRef for icf
1 parent 52d31d4 commit b387d5e

File tree

2 files changed

+29
-60
lines changed

2 files changed

+29
-60
lines changed

lld/MachO/SyntheticSections.cpp

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ void ObjCStubsSection::initialize() {
836836
for (ConcatInputSection *isec : inputSections) {
837837
if (isec->shouldOmitFromOutput())
838838
continue;
839-
if (isec->getName() != "__objc_selrefs")
839+
if (isec->getName() != section_names::objcSelrefs)
840840
continue;
841841
// We expect a single relocation per selref entry to __objc_methname that
842842
// might be aggregated.
@@ -846,21 +846,38 @@ void ObjCStubsSection::initialize() {
846846
if (const auto *d = dyn_cast<Defined>(sym)) {
847847
auto *cisec = cast<CStringInputSection>(d->isec);
848848
auto methname = cisec->getStringRefAtOffset(d->value);
849-
methnameToselref[methname] = isec;
849+
methnameToSelref[CachedHashStringRef(methname)] = isec;
850850
}
851851
}
852852
}
853853
}
854854

855855
void ObjCStubsSection::addEntry(Symbol *sym) {
856856
StringRef methname = getMethname(sym);
857-
// We will create a selref entry for each unique methname.
858-
if (!methnameToselref.count(methname) &&
859-
!methnameToidxOffsetPair.count(methname)) {
857+
// We create a selref entry for each unique methname.
858+
if (!methnameToSelref.count(CachedHashStringRef(methname))) {
860859
auto methnameOffset =
861860
in.objcMethnameSection->getStringOffset(methname).outSecOff;
862-
auto selIndex = methnameToidxOffsetPair.size();
863-
methnameToidxOffsetPair[methname] = {selIndex, methnameOffset};
861+
862+
size_t wordSize = target->wordSize;
863+
uint8_t *selrefData = bAlloc().Allocate<uint8_t>(wordSize);
864+
write64le(selrefData, methnameOffset);
865+
auto *objcSelref = makeSyntheticInputSection(
866+
segment_names::data, section_names::objcSelrefs,
867+
S_LITERAL_POINTERS | S_ATTR_NO_DEAD_STRIP,
868+
ArrayRef<uint8_t>{selrefData, wordSize},
869+
/*align=*/wordSize);
870+
objcSelref->live = true;
871+
objcSelref->relocs.push_back(
872+
{/*type=*/target->unsignedRelocType,
873+
/*pcrel=*/false, /*length=*/3,
874+
/*offset=*/0,
875+
/*addend=*/static_cast<int64_t>(methnameOffset),
876+
/*referent=*/in.objcMethnameSection->isec});
877+
objcSelref->parent = ConcatOutputSection::getOrCreateForInput(objcSelref);
878+
inputSections.push_back(objcSelref);
879+
objcSelref->isFinal = true;
880+
methnameToSelref[CachedHashStringRef(methname)] = objcSelref;
864881
}
865882

866883
auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
@@ -895,35 +912,6 @@ void ObjCStubsSection::setUp() {
895912
if (!isa<Defined>(objcMsgSend))
896913
in.stubs->addEntry(objcMsgSend);
897914
}
898-
899-
size_t size = methnameToidxOffsetPair.size() * target->wordSize;
900-
uint8_t *selrefsData = bAlloc().Allocate<uint8_t>(size);
901-
for (const auto &[methname, idxOffsetPair] : methnameToidxOffsetPair) {
902-
const auto &[idx, offset] = idxOffsetPair;
903-
write64le(&selrefsData[idx * target->wordSize], offset);
904-
}
905-
906-
in.objcSelrefs =
907-
makeSyntheticInputSection(segment_names::data, section_names::objcSelrefs,
908-
S_LITERAL_POINTERS | S_ATTR_NO_DEAD_STRIP,
909-
ArrayRef<uint8_t>{selrefsData, size},
910-
/*align=*/target->wordSize);
911-
in.objcSelrefs->live = true;
912-
913-
for (const auto &[methname, idxOffsetPair] : methnameToidxOffsetPair) {
914-
const auto &[idx, offset] = idxOffsetPair;
915-
in.objcSelrefs->relocs.push_back(
916-
{/*type=*/target->unsignedRelocType,
917-
/*pcrel=*/false, /*length=*/3,
918-
/*offset=*/static_cast<uint32_t>(idx * target->wordSize),
919-
/*addend=*/offset * in.objcMethnameSection->align,
920-
/*referent=*/in.objcMethnameSection->isec});
921-
}
922-
923-
in.objcSelrefs->parent =
924-
ConcatOutputSection::getOrCreateForInput(in.objcSelrefs);
925-
inputSections.push_back(in.objcSelrefs);
926-
in.objcSelrefs->isFinal = true;
927915
}
928916

929917
uint64_t ObjCStubsSection::getSize() const {
@@ -934,29 +922,16 @@ uint64_t ObjCStubsSection::getSize() const {
934922
}
935923

936924
void ObjCStubsSection::writeTo(uint8_t *buf) const {
937-
assert(in.objcSelrefs->live);
938-
assert(in.objcSelrefs->isFinal);
939-
940925
uint64_t stubOffset = 0;
941926
for (size_t i = 0, n = symbols.size(); i < n; ++i) {
942927
Defined *sym = symbols[i];
943-
uint64_t selBaseAddr;
944-
uint64_t selIndex;
945928

946929
auto methname = getMethname(sym);
947-
auto j = methnameToselref.find(methname);
948-
if (j != methnameToselref.end()) {
949-
selBaseAddr = j->second->getVA(0);
950-
selIndex = 0;
951-
} else {
952-
auto k = methnameToidxOffsetPair.find(methname);
953-
assert(k != methnameToidxOffsetPair.end());
954-
selBaseAddr = in.objcSelrefs->getVA();
955-
selIndex = k->second.first;
956-
}
930+
auto j = methnameToSelref.find(CachedHashStringRef(methname));
931+
assert(j != methnameToSelref.end());
932+
auto selrefAddr = j->second->getVA(0);
957933
target->writeObjCMsgSendStub(buf + stubOffset, sym, in.objcStubs->addr,
958-
stubOffset, selBaseAddr, selIndex,
959-
objcMsgSend);
934+
stubOffset, selrefAddr, 0, objcMsgSend);
960935
}
961936
}
962937

lld/MachO/SyntheticSections.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,7 @@ class ObjCStubsSection final : public SyntheticSection {
338338

339339
private:
340340
std::vector<Defined *> symbols;
341-
// Existing mapping from methname to selref (0 index is assumed).
342-
llvm::StringMap<InputSection *> methnameToselref;
343-
// Newly created mapping from methname to the pair of index (selref) and
344-
// offset (methname).
345-
llvm::MapVector<StringRef, std::pair<uint32_t, uint32_t>>
346-
methnameToidxOffsetPair;
341+
llvm::DenseMap<llvm::CachedHashStringRef, InputSection *> methnameToSelref;
347342
Symbol *objcMsgSend = nullptr;
348343
};
349344

@@ -800,7 +795,6 @@ struct InStruct {
800795
StubsSection *stubs = nullptr;
801796
StubHelperSection *stubHelper = nullptr;
802797
ObjCStubsSection *objcStubs = nullptr;
803-
ConcatInputSection *objcSelrefs = nullptr;
804798
UnwindInfoSection *unwindInfo = nullptr;
805799
ObjCImageInfoSection *objCImageInfo = nullptr;
806800
ConcatInputSection *imageLoaderCache = nullptr;

0 commit comments

Comments
 (0)