[PATCH] D81298: [lld-macho] Handle alignment correctly when merging InputSections

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 5 12:55:07 PDT 2020


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

Previously, we weren't updating isecAddr when aligning InputSections,
resulting in truncated sections under the right conditions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81298

Files:
  lld/MachO/MergedOutputSection.cpp
  lld/test/MachO/section-merge.s


Index: lld/test/MachO/section-merge.s
===================================================================
--- lld/test/MachO/section-merge.s
+++ lld/test/MachO/section-merge.s
@@ -1,31 +1,19 @@
 # REQUIRES: x86
 # RUN: mkdir -p %t
-# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %p/Inputs/libhello.s \
-# RUN:   -o %t/libhello.o
-# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %p/Inputs/libgoodbye.s \
-# RUN:   -o %t/libgoodbye.o
-# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %p/Inputs/libfunction.s \
-# RUN:   -o %t/libfunction.o
-# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s \
-# RUN:   -o %t/main.o
-# RUN: lld -flavor darwinnew -o %t/output %t/libfunction.o %t/libgoodbye.o %t/libhello.o %t/main.o
+## Verify that we preserve alignment when merging sections.
+# RUN: echo ".globl _foo; .data; .p2align 0; _foo: .space 1" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/foo.o
+# RUN: echo ".globl _bar; .data; .p2align 2; _bar: .space 1" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/bar.o
+# RUN: echo ".globl _baz; .data; .p2align 3; _baz: .space 1" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/baz.o
+# RUN: echo ".globl _qux; .data; .p2align 0; _qux: .space 1" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/qux.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/main.o
+# RUN: lld -flavor darwinnew -o %t/output %t/foo.o %t/bar.o %t/baz.o %t/qux.o %t/main.o
 
-# RUN: llvm-objdump --syms %t/output | FileCheck %s
-# CHECK:      SYMBOL TABLE:
-# CHECK-DAG:  {{[0-9a-z]+}} g     O __TEXT,__cstring _goodbye_world
-# CHECK-DAG:  {{[0-9a-z]+}} g     O __TEXT,__cstring _hello_its_me
-# CHECK-DAG:  {{[0-9a-z]+}} g     O __TEXT,__cstring _hello_world
-# CHECK-DAG:  {{[0-9a-z]+}} g     F __TEXT,__text _main
-# CHECK-DAG:  {{[0-9a-z]+}} g     F __TEXT,__text _some_function
-
-# RUN: llvm-objdump -d %t/output | FileCheck %s --check-prefix DATA
-# DATA:      Disassembly of section __TEXT,__text:
-# DATA:        {{0*}}[[#%x,BASE:]] <_some_function>:
-# DATA-NEXT:             [[#BASE]]: 48 c7 c0 01 00 00 00          movq    $1, %rax
-# DATA-NEXT:       [[#BASE + 0x7]]: c3                            retq
-# DATA:        {{0*}}[[#%x,MAIN:]] <_main>:
-# DATA-NEXT:             [[#MAIN]]: 48 c7 c0 00 00 00 00          movq    $0, %rax
-# DATA-NEXT:       [[#MAIN + 0x7]]: c3                            retq
+# RUN: llvm-objdump --section-headers --syms %t/output | FileCheck %s
+# CHECK-LABEL: SYMBOL TABLE:
+# CHECK-DAG:   [[#%x, ADDR:]]      g     O __DATA,__data _foo
+# CHECK-DAG:   {{0*}}[[#ADDR+0x4]] g     O __DATA,__data _bar
+# CHECK-DAG:   {{0*}}[[#ADDR+0x8]] g     O __DATA,__data _baz
+# CHECK-DAG:   {{0*}}[[#ADDR+0x9]] g     O __DATA,__data _qux
 
 .section __TEXT,__text
 .global _main
Index: lld/MachO/MergedOutputSection.cpp
===================================================================
--- lld/MachO/MergedOutputSection.cpp
+++ lld/MachO/MergedOutputSection.cpp
@@ -33,8 +33,10 @@
   uint64_t isecAddr = addr;
   uint64_t isecFileOff = fileOff;
   for (InputSection *i : inputs) {
-    i->outSecOff = alignTo(isecAddr, i->align) - addr;
-    i->outSecFileOff = alignTo(isecFileOff, i->align) - fileOff;
+    isecAddr = alignTo(isecAddr, i->align);
+    isecFileOff = alignTo(isecFileOff, i->align);
+    i->outSecOff = isecAddr - addr;
+    i->outSecFileOff = isecFileOff - fileOff;
     isecAddr += i->getSize();
     isecFileOff += i->getFileSize();
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81298.268914.patch
Type: text/x-patch
Size: 3498 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200605/22f3207d/attachment.bin>


More information about the llvm-commits mailing list