Skip to content

Commit

Permalink
[lld-macho][nfc] Remove indirection when looking up common section me…
Browse files Browse the repository at this point in the history
…mbers

{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
  • Loading branch information
int3 committed Apr 7, 2022
1 parent fa784f6 commit f004ecf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
7 changes: 2 additions & 5 deletions lld/MachO/Driver.cpp
Expand Up @@ -986,14 +986,11 @@ static void gatherInputSections() {
int inputOrder = 0;
for (const InputFile *file : inputFiles) {
for (const Section *section : file->sections) {
const Subsections &subsections = section->subsections;
if (subsections.empty())
continue;
if (subsections[0].isec->getName() == section_names::compactUnwind)
if (section->name == section_names::compactUnwind)
// Compact unwind entries require special handling elsewhere.
continue;
ConcatOutputSection *osec = nullptr;
for (const Subsection &subsection : subsections) {
for (const Subsection &subsection : section->subsections) {
if (auto *isec = dyn_cast<ConcatInputSection>(subsection.isec)) {
if (isec->isCoalescedWeak())
continue;
Expand Down
21 changes: 12 additions & 9 deletions lld/MachO/InputFiles.cpp
Expand Up @@ -95,6 +95,10 @@ std::string lld::toString(const InputFile *f) {
return (f->archiveName + "(" + path::filename(f->getName()) + ")").str();
}

std::string lld::toString(const Section &sec) {
return (toString(sec.file) + ":(" + sec.name + ")").str();
}

SetVector<InputFile *> macho::inputFiles;
std::unique_ptr<TarWriter> macho::tar;
int InputFile::idCount = 0;
Expand Down Expand Up @@ -302,7 +306,7 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
" is too large");
continue;
}
const Section &section = *sections.back();
Section &section = *sections.back();
uint32_t align = 1 << sec.align;
ArrayRef<uint8_t> data = {isZeroFill(sec.flags) ? nullptr
: buf + sec.offset,
Expand All @@ -311,7 +315,7 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
auto splitRecords = [&](int recordSize) -> void {
if (data.empty())
return;
Subsections &subsections = sections.back()->subsections;
Subsections &subsections = section.subsections;
subsections.reserve(data.size() / recordSize);
for (uint64_t off = 0; off < data.size(); off += recordSize) {
auto *isec = make<ConcatInputSection>(
Expand All @@ -336,11 +340,11 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
} else {
isec = make<WordLiteralInputSection>(section, data, align);
}
sections.back()->subsections.push_back({0, isec});
section.subsections.push_back({0, isec});
} else if (auto recordSize = getRecordSize(segname, name)) {
splitRecords(*recordSize);
if (name == section_names::compactUnwind)
compactUnwindSection = sections.back();
compactUnwindSection = &section;
} else if (segname == segment_names::llvm) {
if (config->callGraphProfileSort && name == section_names::cgProfile)
checkError(parseCallGraph(data, callGraph));
Expand All @@ -359,7 +363,7 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
// parsing their relocations unnecessarily.
debugSections.push_back(isec);
} else {
sections.back()->subsections.push_back({0, isec});
section.subsections.push_back({0, isec});
}
}
}
Expand Down Expand Up @@ -724,8 +728,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
Subsections &subsections = sections[i]->subsections;
if (subsections.empty())
continue;
InputSection *lastIsec = subsections.back().isec;
if (lastIsec->getName() == section_names::ehFrame) {
if (sections[i]->name == section_names::ehFrame) {
// __TEXT,__eh_frame only has symbols and SUBTRACTOR relocs when ld64 -r
// adds local "EH_Frame1" and "func.eh". Ignore them because they have
// gone unused by Mac OS since Snow Leopard (10.6), vintage 2009.
Expand All @@ -738,7 +741,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
// Record-based sections have already been split into subsections during
// parseSections(), so we simply need to match Symbols to the corresponding
// subsection here.
if (getRecordSize(lastIsec->getSegName(), lastIsec->getName())) {
if (getRecordSize(sections[i]->segname, sections[i]->name)) {
for (size_t j = 0; j < symbolIndices.size(); ++j) {
uint32_t symIndex = symbolIndices[j];
const NList &sym = nList[symIndex];
Expand All @@ -747,7 +750,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
InputSection *isec =
findContainingSubsection(*sections[i], &symbolOffset);
if (symbolOffset != 0) {
error(toString(lastIsec) + ": symbol " + name +
error(toString(*sections[i]) + ": symbol " + name +
" at misaligned offset");
continue;
}
Expand Down
1 change: 1 addition & 0 deletions lld/MachO/InputFiles.h
Expand Up @@ -307,6 +307,7 @@ std::vector<const CommandType *> findCommands(const void *anyHdr,
} // namespace macho

std::string toString(const macho::InputFile *file);
std::string toString(const macho::Section &);
} // namespace lld

#endif

0 comments on commit f004ecf

Please sign in to comment.