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

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 15 15:29:57 PDT 2020


int3 created this revision.
int3 added a reviewer: lld-macho.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81888

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


Index: lld/test/MachO/bss.s
===================================================================
--- lld/test/MachO/bss.s
+++ lld/test/MachO/bss.s
@@ -3,9 +3,27 @@
 # RUN: lld -flavor darwinnew -arch x86_64 -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 @@
 
 .bss
 .zero 4
+
+.data
+.quad 0x1234
Index: lld/MachO/Writer.cpp
===================================================================
--- lld/MachO/Writer.cpp
+++ lld/MachO/Writer.cpp
@@ -351,6 +351,10 @@
   StringRef segname = osec->parent->name;
   if (segname == segment_names::text && osec->name == section_names::header)
     return std::numeric_limits<int>::min();
+  // 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;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81888.270882.patch
Type: text/x-patch
Size: 1996 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200615/e2ffe3a7/attachment.bin>


More information about the llvm-commits mailing list