[lld] 7996a1e - [lld-macho] Make sure ZeroFill sections are at the end of their segments

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 1 19:40:04 PDT 2020


Author: Jez Ng
Date: 2020-07-01T19:39:29-07:00
New Revision: 7996a1ef7020e4bda1252f6f26c26e1c78aa3eac

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

LOG: [lld-macho] Make sure ZeroFill sections are at the end of their segments

Summary:
ld64 does this, and references an internal rdar:// number as an explanation. No
idea what that rdar issue is, but in practice, it seems that not putting a BSS
section at the end can cause subsequent sections in the same segment to be
overwritten with zeroes.

Reviewers: #lld-macho

Subscribers: llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D81888

Added: 
    

Modified: 
    lld/MachO/Writer.cpp
    lld/test/MachO/bss.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 684633c3f9d1..c88e314e556d 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -358,6 +358,10 @@ static int sectionOrder(OutputSection *osec) {
         .Case(section_names::stringTable, -1)
         .Default(0);
   }
+  // ZeroFill sections must always be the at the end of their segments,
+  // otherwise subsequent sections may get overwritten with zeroes at runtime.
+  if (isZeroFill(osec->flags))
+    return std::numeric_limits<int>::max();
   return 0;
 }
 

diff  --git a/lld/test/MachO/bss.s b/lld/test/MachO/bss.s
index 7e7af12175f4..b56f02d2bcb5 100644
--- a/lld/test/MachO/bss.s
+++ b/lld/test/MachO/bss.s
@@ -3,9 +3,27 @@
 # RUN: lld -flavor darwinnew -o %t %t.o
 # RUN: llvm-readobj --section-headers --macho-segment %t | FileCheck %s
 
-## Check that __bss takes up zero file size and is at file offset zero.
+## Check that __bss takes up zero file size, is at file offset zero, and
+## appears at the end of its segment.
 
-# CHECK:        Name: __bss
+# CHECK:        Index: 1
+# CHECK-NEXT:   Name: __data
+# CHECK-NEXT:   Segment: __DATA
+# CHECK-NEXT:   Address:
+# CHECK-NEXT:   Size: 0x8
+# CHECK-NEXT:   Offset: 4096
+# CHECK-NEXT:   Alignment: 0
+# CHECK-NEXT:   RelocationOffset: 0x0
+# CHECK-NEXT:   RelocationCount: 0
+# CHECK-NEXT:   Type: Regular (0x0)
+# CHECK-NEXT:   Attributes [ (0x0)
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Reserved1: 0x0
+# CHECK-NEXT:   Reserved2: 0x0
+# CHECK-NEXT:   Reserved3: 0x0
+
+# CHECK:        Index: 2
+# CHECK-NEXT:   Name: __bss
 # CHECK-NEXT:   Segment: __DATA
 # CHECK-NEXT:   Address:
 # CHECK-NEXT:   Size: 0x4
@@ -23,9 +41,9 @@
 # CHECK:      Name: __DATA
 # CHECK-NEXT: Size:
 # CHECK-NEXT: vmaddr:
-# CHECK-NEXT: vmsize: 0x4
+# CHECK-NEXT: vmsize: 0xC
 # CHECK-NEXT: fileoff:
-# CHECK-NEXT: filesize: 0
+# CHECK-NEXT: filesize: 8
 
 .globl _main
 
@@ -36,3 +54,6 @@ _main:
 
 .bss
 .zero 4
+
+.data
+.quad 0x1234


        


More information about the llvm-commits mailing list