diff --git a/lld/MachO/MergedOutputSection.cpp b/lld/MachO/MergedOutputSection.cpp --- a/lld/MachO/MergedOutputSection.cpp +++ b/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(); } diff --git a/lld/test/MachO/section-merge.s b/lld/test/MachO/section-merge.s --- a/lld/test/MachO/section-merge.s +++ b/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