diff --git a/lld/MachO/OutputSection.h b/lld/MachO/OutputSection.h --- a/lld/MachO/OutputSection.h +++ b/lld/MachO/OutputSection.h @@ -36,8 +36,8 @@ // as-is so their file size is the same as their address space size. virtual uint64_t getFileSize() const { return getSize(); } - // Hidden sections omit header content, but body content is still present. - virtual bool isHidden() const { return !this->isNeeded(); } + // Hidden sections omit header content, but body content may still be present. + virtual bool isHidden() const { return false; } // Unneeded sections are omitted entirely (header and body). virtual bool isNeeded() const { return true; } diff --git a/lld/MachO/OutputSegment.cpp b/lld/MachO/OutputSegment.cpp --- a/lld/MachO/OutputSegment.cpp +++ b/lld/MachO/OutputSegment.cpp @@ -40,7 +40,7 @@ size_t count = 0; for (const OutputSegment::SectionMapEntry &i : sections) { OutputSection *os = i.second; - count += (os->isHidden() ? 0 : 1); + count += (os->isNeeded() && !os->isHidden() ? 1 : 0); } return count; } @@ -138,7 +138,7 @@ seg->sortOutputSections(&comparator); for (auto &p : seg->getSections()) { OutputSection *section = p.second; - if (!section->isHidden()) { + if (section->isNeeded() && !section->isHidden()) { section->index = ++sectionIndex; } } diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -114,7 +114,7 @@ c->maxprot = seg->maxProt; c->initprot = seg->initProt; - if (!seg->isNeeded()) + if (seg->getSections().empty()) return; c->vmaddr = seg->firstSection()->addr; @@ -125,6 +125,9 @@ for (auto &p : seg->getSections()) { StringRef s = p.first; OutputSection *section = p.second; + if (!section->isNeeded()) + continue; + c->filesize += section->getFileSize(); if (section->isHidden()) continue; @@ -366,7 +369,8 @@ for (OutputSegment *seg : outputSegments) { for (auto &p : seg->getSections()) { OutputSection *section = p.second; - section->writeTo(buf + section->fileOff); + if (section->isNeeded()) + section->writeTo(buf + section->fileOff); } } } diff --git a/lld/test/MachO/section-merge.s b/lld/test/MachO/section-merge.s --- a/lld/test/MachO/section-merge.s +++ b/lld/test/MachO/section-merge.s @@ -23,9 +23,9 @@ # DATA: {{0*}}[[#%x,BASE:]] <_some_function>: # DATA-NEXT: [[#BASE]]: 48 c7 c0 01 00 00 00 movq $1, %rax # DATA-NEXT: [[#BASE + 0x7]]: c3 retq -# DATA: {{0*}}[[#BASE + 0x8]] <_main>: -# DATA-NEXT: [[#BASE + 0x8]]: 48 c7 c0 00 00 00 00 movq $0, %rax -# DATA-NEXT: [[#BASE + 0xf]]: c3 retq +# DATA: {{0*}}[[#%x,MAIN:]] <_main>: +# DATA-NEXT: [[#MAIN]]: 48 c7 c0 00 00 00 00 movq $0, %rax +# DATA-NEXT: [[#MAIN + 0x7]]: c3 retq .section __TEXT,__text .global _main