[lld] b7e12ca - [lld-macho] If export_size is zero, export_off must be zero

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 27 11:59:40 PDT 2021


Author: Jez Ng
Date: 2021-10-27T14:58:42-04:00
New Revision: b7e12ca7aab73a00d7a7a2fbbd5214dde9353335

URL: https://github.com/llvm/llvm-project/commit/b7e12ca7aab73a00d7a7a2fbbd5214dde9353335
DIFF: https://github.com/llvm/llvm-project/commit/b7e12ca7aab73a00d7a7a2fbbd5214dde9353335.diff

LOG: [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

Added: 
    

Modified: 
    lld/MachO/SyntheticSections.h
    lld/MachO/Writer.cpp
    lld/test/MachO/no-unneeded-dyld-info.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index cc7af64a69818..49b68c77672e8 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -345,6 +345,7 @@ class ExportSection final : public LinkEditSection {
   ExportSection();
   void finalizeContents() override;
   uint64_t getRawSize() const override { return size; }
+  bool isNeeded() const override { return size; }
   void writeTo(uint8_t *buf) const override;
 
   bool hasWeakSymbol = false;

diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 27ad7b2802bad..c2aef4d554002 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -968,7 +968,12 @@ template <class LP> void Writer::createOutputSections() {
 
   for (SyntheticSection *ssec : syntheticSections) {
     auto it = concatOutputSections.find({ssec->segname, ssec->name});
-    if (ssec->isNeeded()) {
+    // We add all LinkEdit sections here because we don't know if they are
+    // needed until their finalizeContents() methods get called later. While
+    // this means that we add some redundant sections to __LINKEDIT, there is
+    // is no redundancy in the output, as we do not emit section headers for
+    // any LinkEdit sections.
+    if (ssec->isNeeded() || ssec->segname == segment_names::linkEdit) {
       if (it == concatOutputSections.end()) {
         getOrCreateOutputSegment(ssec->segname)->addOutputSection(ssec);
       } else {

diff  --git a/lld/test/MachO/no-unneeded-dyld-info.s b/lld/test/MachO/no-unneeded-dyld-info.s
index b69950f23692b..cf92adf9455f9 100644
--- a/lld/test/MachO/no-unneeded-dyld-info.s
+++ b/lld/test/MachO/no-unneeded-dyld-info.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
-# RUN: %lld -o %t %t.o
+# RUN: %lld -dylib -o %t %t.o
 # RUN: llvm-objdump --macho --all-headers %t | FileCheck %s
 
 # CHECK:                 cmd LC_DYLD_INFO_ONLY
@@ -13,7 +13,8 @@
 # CHECK-NEXT: weak_bind_size 0
 # CHECK-NEXT:  lazy_bind_off 0
 # CHECK-NEXT: lazy_bind_size 0
+# CHECK-NEXT: export_off     0
+# CHECK-NEXT: export_size    0
 
-.globl _main
-_main:
-  ret
+_not_exported:
+  .space 1


        


More information about the llvm-commits mailing list