Skip to content

Commit f004ecf

Browse files
committed
[lld-macho][nfc] Remove indirection when looking up common section members
{D118797} means that we can now check the name/segname of a given section directly, instead of having to look those properties up on one of its subsections. This allows us to simplify our code. Reviewed By: #lld-macho, oontvoo Differential Revision: https://reviews.llvm.org/D123275
1 parent fa784f6 commit f004ecf

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

lld/MachO/Driver.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -986,14 +986,11 @@ static void gatherInputSections() {
986986
int inputOrder = 0;
987987
for (const InputFile *file : inputFiles) {
988988
for (const Section *section : file->sections) {
989-
const Subsections &subsections = section->subsections;
990-
if (subsections.empty())
991-
continue;
992-
if (subsections[0].isec->getName() == section_names::compactUnwind)
989+
if (section->name == section_names::compactUnwind)
993990
// Compact unwind entries require special handling elsewhere.
994991
continue;
995992
ConcatOutputSection *osec = nullptr;
996-
for (const Subsection &subsection : subsections) {
993+
for (const Subsection &subsection : section->subsections) {
997994
if (auto *isec = dyn_cast<ConcatInputSection>(subsection.isec)) {
998995
if (isec->isCoalescedWeak())
999996
continue;

lld/MachO/InputFiles.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ std::string lld::toString(const InputFile *f) {
9595
return (f->archiveName + "(" + path::filename(f->getName()) + ")").str();
9696
}
9797

98+
std::string lld::toString(const Section &sec) {
99+
return (toString(sec.file) + ":(" + sec.name + ")").str();
100+
}
101+
98102
SetVector<InputFile *> macho::inputFiles;
99103
std::unique_ptr<TarWriter> macho::tar;
100104
int InputFile::idCount = 0;
@@ -302,7 +306,7 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
302306
" is too large");
303307
continue;
304308
}
305-
const Section &section = *sections.back();
309+
Section &section = *sections.back();
306310
uint32_t align = 1 << sec.align;
307311
ArrayRef<uint8_t> data = {isZeroFill(sec.flags) ? nullptr
308312
: buf + sec.offset,
@@ -311,7 +315,7 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
311315
auto splitRecords = [&](int recordSize) -> void {
312316
if (data.empty())
313317
return;
314-
Subsections &subsections = sections.back()->subsections;
318+
Subsections &subsections = section.subsections;
315319
subsections.reserve(data.size() / recordSize);
316320
for (uint64_t off = 0; off < data.size(); off += recordSize) {
317321
auto *isec = make<ConcatInputSection>(
@@ -336,11 +340,11 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
336340
} else {
337341
isec = make<WordLiteralInputSection>(section, data, align);
338342
}
339-
sections.back()->subsections.push_back({0, isec});
343+
section.subsections.push_back({0, isec});
340344
} else if (auto recordSize = getRecordSize(segname, name)) {
341345
splitRecords(*recordSize);
342346
if (name == section_names::compactUnwind)
343-
compactUnwindSection = sections.back();
347+
compactUnwindSection = &section;
344348
} else if (segname == segment_names::llvm) {
345349
if (config->callGraphProfileSort && name == section_names::cgProfile)
346350
checkError(parseCallGraph(data, callGraph));
@@ -359,7 +363,7 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
359363
// parsing their relocations unnecessarily.
360364
debugSections.push_back(isec);
361365
} else {
362-
sections.back()->subsections.push_back({0, isec});
366+
section.subsections.push_back({0, isec});
363367
}
364368
}
365369
}
@@ -724,8 +728,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
724728
Subsections &subsections = sections[i]->subsections;
725729
if (subsections.empty())
726730
continue;
727-
InputSection *lastIsec = subsections.back().isec;
728-
if (lastIsec->getName() == section_names::ehFrame) {
731+
if (sections[i]->name == section_names::ehFrame) {
729732
// __TEXT,__eh_frame only has symbols and SUBTRACTOR relocs when ld64 -r
730733
// adds local "EH_Frame1" and "func.eh". Ignore them because they have
731734
// gone unused by Mac OS since Snow Leopard (10.6), vintage 2009.
@@ -738,7 +741,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
738741
// Record-based sections have already been split into subsections during
739742
// parseSections(), so we simply need to match Symbols to the corresponding
740743
// subsection here.
741-
if (getRecordSize(lastIsec->getSegName(), lastIsec->getName())) {
744+
if (getRecordSize(sections[i]->segname, sections[i]->name)) {
742745
for (size_t j = 0; j < symbolIndices.size(); ++j) {
743746
uint32_t symIndex = symbolIndices[j];
744747
const NList &sym = nList[symIndex];
@@ -747,7 +750,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
747750
InputSection *isec =
748751
findContainingSubsection(*sections[i], &symbolOffset);
749752
if (symbolOffset != 0) {
750-
error(toString(lastIsec) + ": symbol " + name +
753+
error(toString(*sections[i]) + ": symbol " + name +
751754
" at misaligned offset");
752755
continue;
753756
}

lld/MachO/InputFiles.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ std::vector<const CommandType *> findCommands(const void *anyHdr,
307307
} // namespace macho
308308

309309
std::string toString(const macho::InputFile *file);
310+
std::string toString(const macho::Section &);
310311
} // namespace lld
311312

312313
#endif

0 commit comments

Comments
 (0)