[lld] 7f673fc - [lld/mac] Fix alignment on subsections

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Sun May 9 18:02:22 PDT 2021


Author: Nico Weber
Date: 2021-05-09T21:00:56-04:00
New Revision: 7f673fcaa9a2cb4600f244792f8521ff19aa806c

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

LOG: [lld/mac] Fix alignment on subsections

On a section with alignment of 16, subsections aligned to 16-byte
boundaries should keep their 16-byte alignment.

Fixes PR50274. (The same bug could have happened with -order_file
previously.)

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

Added: 
    

Modified: 
    lld/MachO/InputFiles.cpp
    lld/test/MachO/weak-definition-gc.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index d33c3d50fbbc..bab2a7b263a9 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -580,6 +580,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
       return nList[lhs].n_value < nList[rhs].n_value;
     });
     uint64_t sectionAddr = sectionHeaders[i].addr;
+    uint32_t sectionAlign = 1u << sectionHeaders[i].align;
 
     // We populate subsecMap by repeatedly splitting the last (highest address)
     // subsection.
@@ -622,7 +623,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
       // TODO: ld64 appears to preserve the original alignment as well as each
       // subsection's offset from the last aligned address. We should consider
       // emulating that behavior.
-      nextIsec->align = MinAlign(isec->align, sym.n_value);
+      nextIsec->align = MinAlign(sectionAlign, sym.n_value);
       subsecMap.push_back({sym.n_value - sectionAddr, nextIsec});
       subsecEntry = subsecMap.back();
     }

diff  --git a/lld/test/MachO/weak-definition-gc.s b/lld/test/MachO/weak-definition-gc.s
index aea5a4cf5bc0..0660ccc524d4 100644
--- a/lld/test/MachO/weak-definition-gc.s
+++ b/lld/test/MachO/weak-definition-gc.s
@@ -53,6 +53,19 @@
 # RUN: %lld -dylib -o %t/out.dylib %t/weak-sub-alt.o %t/weak-sub-alt2.o
 # RUN: %lld -dylib -o %t/out.dylib %t/weak-sub-alt2.o %t/weak-sub-alt.o
 
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 %t/weak-aligned-1.s -o %t/weak-aligned-1.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 %t/weak-aligned-2.s -o %t/weak-aligned-2.o
+# RUN: %lld -o %t/out -lSystem %t/weak-aligned-1.o %t/weak-aligned-2.o
+# RUN: llvm-objdump --syms --section=__const --full-contents %t/out | FileCheck --check-prefix=ALIGN %s
+# ALIGN:      SYMBOL TABLE:
+# ALIGN-DAG:  [[#%x, ADDR:]]       l     O __DATA_CONST,__const _weak1
+# ALIGN-DAG:  {{0*}}[[#ADDR+ 0x4]] l     O __DATA_CONST,__const _weak3
+# ALIGN-DAG:  {{0*}}[[#ADDR+ 0x8]] l     O __DATA_CONST,__const _weak2
+# ALIGN-DAG:  {{0*}}[[#ADDR+0x10]] g     O __DATA_CONST,__const _aligned
+# ALIGN:      Contents of section __DATA_CONST,__const:
+# ALIGN-NEXT: {{0*}}[[#ADDR]]      11111111 33333333 22222222 00000000
+# ALIGN-NEXT: {{0*}}[[#ADDR+0x10]] 81818181 81818181 82828282 82828282
+
 #--- weak-sub.s
 .globl _foo, _bar
 .weak_definition _foo, _bar
@@ -129,3 +142,56 @@ _ref:
   callq _bar
 
 .subsections_via_symbols
+
+#--- weak-aligned-1.s
+.section __DATA,__const
+.p2align 3
+.globl _weak1
+.weak_def_can_be_hidden _weak1
+_weak1:
+  .4byte 0x11111111
+
+.globl _weak3
+.weak_def_can_be_hidden _weak3
+_weak3:
+  .4byte 0x33333333
+
+.subsections_via_symbols
+
+#--- weak-aligned-2.s
+# _weak1 and _weak3 are already in weak-aligned-1,
+# so from _weak1-3 in this file only _weak2 is used.
+# However, _aligned still has to stay aligned to a 16-byte boundary.
+.section __DATA,__const
+.p2align 3
+.globl _weak1
+.weak_def_can_be_hidden _weak1
+_weak1:
+  .4byte 0x11111111
+
+.globl _weak2
+.weak_def_can_be_hidden _weak2
+_weak2:
+  .4byte 0x22222222
+
+.globl _weak3
+.weak_def_can_be_hidden _weak3
+_weak3:
+  .4byte 0x33333333
+
+.section __DATA,__const
+.p2align 4
+.globl _aligned
+_aligned:
+  .8byte 0x8181818181818181
+  .8byte 0x8282828282828282
+
+.section __TEXT,__text
+.globl _main
+_main:
+  movl _weak1(%rip), %eax
+  movl _weak2(%rip), %ebx
+  movaps _aligned(%rip), %xmm0
+  retq
+
+.subsections_via_symbols


        


More information about the llvm-commits mailing list