Skip to content

Commit b7e12ca

Browse files
committed
[lld-macho] If export_size is zero, export_off must be zero
Otherwise tools like codesign_allocate will choke. We were already handling this correctly for the other DYLD_INFO sections. Doing this correctly is a bit subtle: we don't know if export_size will be zero until we have run `ExportSection::finalizeContents()`. However, we must still add the ExportSection to the `__LINKEDIT` segment in order that it gets sorted during `sortSectionsAndSegments()`. Reviewed By: #lld-macho, oontvoo Differential Revision: https://reviews.llvm.org/D112589
1 parent 665060e commit b7e12ca

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

lld/MachO/SyntheticSections.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ class ExportSection final : public LinkEditSection {
345345
ExportSection();
346346
void finalizeContents() override;
347347
uint64_t getRawSize() const override { return size; }
348+
bool isNeeded() const override { return size; }
348349
void writeTo(uint8_t *buf) const override;
349350

350351
bool hasWeakSymbol = false;

lld/MachO/Writer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,12 @@ template <class LP> void Writer::createOutputSections() {
968968

969969
for (SyntheticSection *ssec : syntheticSections) {
970970
auto it = concatOutputSections.find({ssec->segname, ssec->name});
971-
if (ssec->isNeeded()) {
971+
// We add all LinkEdit sections here because we don't know if they are
972+
// needed until their finalizeContents() methods get called later. While
973+
// this means that we add some redundant sections to __LINKEDIT, there is
974+
// is no redundancy in the output, as we do not emit section headers for
975+
// any LinkEdit sections.
976+
if (ssec->isNeeded() || ssec->segname == segment_names::linkEdit) {
972977
if (it == concatOutputSections.end()) {
973978
getOrCreateOutputSegment(ssec->segname)->addOutputSection(ssec);
974979
} else {

lld/test/MachO/no-unneeded-dyld-info.s

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# REQUIRES: x86
22
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
3-
# RUN: %lld -o %t %t.o
3+
# RUN: %lld -dylib -o %t %t.o
44
# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s
55

66
# CHECK: cmd LC_DYLD_INFO_ONLY
@@ -13,7 +13,8 @@
1313
# CHECK-NEXT: weak_bind_size 0
1414
# CHECK-NEXT: lazy_bind_off 0
1515
# CHECK-NEXT: lazy_bind_size 0
16+
# CHECK-NEXT: export_off 0
17+
# CHECK-NEXT: export_size 0
1618

17-
.globl _main
18-
_main:
19-
ret
19+
_not_exported:
20+
.space 1

0 commit comments

Comments
 (0)