Skip to content

Commit 20e7d1a

Browse files
committed
use vector to store cstring sections
1 parent c9ee8b2 commit 20e7d1a

File tree

6 files changed

+28
-29
lines changed

6 files changed

+28
-29
lines changed

lld/MachO/Driver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,9 +1520,8 @@ static void foldIdenticalLiterals() {
15201520
// We always create a cStringSection, regardless of whether dedupLiterals is
15211521
// true. If it isn't, we simply create a non-deduplicating CStringSection.
15221522
// Either way, we must unconditionally finalize it here.
1523-
for (auto &[name, sec] : in.cStringSectionMap)
1523+
for (auto *sec : in.cStringSections)
15241524
sec->finalizeContents();
1525-
in.objcMethnameSection->finalizeContents();
15261525
in.wordLiteralSection->finalizeContents();
15271526
}
15281527

lld/MachO/InputSection.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,13 @@ void lld::macho::addInputSection(InputSection *inputSection) {
6363
isec->parent = osec;
6464
inputSections.push_back(isec);
6565
} else if (auto *isec = dyn_cast<CStringInputSection>(inputSection)) {
66-
if (isec->getName() == section_names::objcMethname) {
67-
if (in.objcMethnameSection->inputOrder == UnspecifiedInputOrder)
68-
in.objcMethnameSection->inputOrder = inputSectionsOrder++;
69-
in.objcMethnameSection->addInput(isec);
70-
} else {
71-
auto *osec = in.getOrCreateCStringSection(
72-
config->separateCstringLiteralSections ? isec->getName()
73-
: section_names::cString);
74-
if (osec->inputOrder == UnspecifiedInputOrder)
75-
osec->inputOrder = inputSectionsOrder++;
76-
osec->addInput(isec);
77-
}
66+
bool useSectionName = config->separateCstringLiteralSections ||
67+
isec->getName() == section_names::objcMethname;
68+
auto *osec = in.getOrCreateCStringSection(
69+
useSectionName ? isec->getName() : section_names::cString);
70+
if (osec->inputOrder == UnspecifiedInputOrder)
71+
osec->inputOrder = inputSectionsOrder++;
72+
osec->addInput(isec);
7873
} else if (auto *isec = dyn_cast<WordLiteralInputSection>(inputSection)) {
7974
if (in.wordLiteralSection->inputOrder == UnspecifiedInputOrder)
8075
in.wordLiteralSection->inputOrder = inputSectionsOrder++;

lld/MachO/MapFile.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,7 @@ void macho::writeMapFile() {
239239
printIsecArrSyms(textOsec->inputs, textOsec->getThunks());
240240
} else if (auto *concatOsec = dyn_cast<ConcatOutputSection>(osec)) {
241241
printIsecArrSyms(concatOsec->inputs);
242-
} else if (any_of(in.cStringSectionMap,
243-
[&](auto &it) { return osec == it.getValue(); }) ||
244-
osec == in.objcMethnameSection) {
242+
} else if (is_contained(in.cStringSections, osec)) {
245243
const auto &liveCStrings = info.liveCStringsForSection.lookup(osec);
246244
uint64_t lastAddr = 0; // strings will never start at address 0, so this
247245
// is a sentinel value

lld/MachO/ObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ Defined *ObjcCategoryMerger::emitCategoryName(const std::string &name,
10571057
newStringSec->splitIntoPieces();
10581058
newStringSec->pieces[0].live = true;
10591059
newStringSec->parent = infoCategoryWriter.catNameInfo.outputSection;
1060-
in.getOrCreateCStringSection(section_names::cString)->addInput(newStringSec);
1060+
in.cStringSection->addInput(newStringSec);
10611061
assert(newStringSec->pieces.size() == 1);
10621062

10631063
Defined *catNameSym = make<Defined>(

lld/MachO/SyntheticSections.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,8 @@ void writeChainedFixup(uint8_t *buf, const Symbol *sym, int64_t addend);
843843
struct InStruct {
844844
const uint8_t *bufferStart = nullptr;
845845
MachHeaderSection *header = nullptr;
846-
llvm::StringMap<CStringSection *> cStringSectionMap;
846+
llvm::SmallVector<CStringSection *> cStringSections;
847+
CStringSection *cStringSection = nullptr;
847848
DeduplicatedCStringSection *objcMethnameSection = nullptr;
848849
WordLiteralSection *wordLiteralSection = nullptr;
849850
RebaseSection *rebase = nullptr;
@@ -864,20 +865,25 @@ struct InStruct {
864865
ObjCMethListSection *objcMethList = nullptr;
865866
ChainedFixupsSection *chainedFixups = nullptr;
866867

867-
CStringSection *getOrCreateCStringSection(StringRef name) {
868-
auto it = cStringSectionMap.find(name);
869-
if (it != cStringSectionMap.end())
870-
return it->getValue();
868+
CStringSection *getOrCreateCStringSection(StringRef name,
869+
bool forceDedupStrings = false) {
870+
auto [it, didEmplace] =
871+
cStringSectionMap.try_emplace(name, cStringSections.size());
872+
if (!didEmplace)
873+
return cStringSections[it->getValue()];
871874

872875
std::string &nameData = *make<std::string>(name);
873876
CStringSection *sec;
874-
if (config->dedupStrings)
877+
if (config->dedupStrings || forceDedupStrings)
875878
sec = make<DeduplicatedCStringSection>(nameData.c_str());
876879
else
877880
sec = make<CStringSection>(nameData.c_str());
878-
cStringSectionMap[name] = sec;
881+
cStringSections.push_back(sec);
879882
return sec;
880883
}
884+
885+
private:
886+
llvm::StringMap<unsigned> cStringSectionMap;
881887
};
882888

883889
extern InStruct in;

lld/MachO/Writer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,10 +1377,11 @@ void macho::resetWriter() { LCDylib::resetInstanceCount(); }
13771377

13781378
void macho::createSyntheticSections() {
13791379
in.header = make<MachHeaderSection>();
1380-
// Materialize the cstring section
1381-
in.getOrCreateCStringSection(section_names::cString);
1382-
in.objcMethnameSection =
1383-
make<DeduplicatedCStringSection>(section_names::objcMethname);
1380+
// Materialize cstring and objcMethname sections
1381+
in.cStringSection = in.getOrCreateCStringSection(section_names::cString);
1382+
in.objcMethnameSection = cast<DeduplicatedCStringSection>(
1383+
in.getOrCreateCStringSection(section_names::objcMethname,
1384+
/*forceDedupStrings=*/true));
13841385
in.wordLiteralSection = make<WordLiteralSection>();
13851386
if (config->emitChainedFixups) {
13861387
in.chainedFixups = make<ChainedFixupsSection>();

0 commit comments

Comments
 (0)