[lld] 525c7d8 - [lld-macho] Handle alignment correctly when merging InputSections
Jez Ng via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 17 20:42:00 PDT 2020
Author: Jez Ng
Date: 2020-06-17T20:41:28-07:00
New Revision: 525c7d8cda72c00c85de4989bcddefd4fb9a14a1
URL: https://github.com/llvm/llvm-project/commit/525c7d8cda72c00c85de4989bcddefd4fb9a14a1
DIFF: https://github.com/llvm/llvm-project/commit/525c7d8cda72c00c85de4989bcddefd4fb9a14a1.diff
LOG: [lld-macho] Handle alignment correctly when merging InputSections
Summary:
Previously, we weren't updating isecAddr when aligning InputSections,
resulting in truncated sections under the right conditions.
Reviewers: #lld-macho, compnerd
Reviewed By: #lld-macho, compnerd
Subscribers: smeenai, compnerd, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D81298
Added:
Modified:
lld/MachO/MergedOutputSection.cpp
lld/test/MachO/section-merge.s
Removed:
################################################################################
diff --git a/lld/MachO/MergedOutputSection.cpp b/lld/MachO/MergedOutputSection.cpp
index 1983736d7879..2d0be2538347 100644
--- a/lld/MachO/MergedOutputSection.cpp
+++ b/lld/MachO/MergedOutputSection.cpp
@@ -32,11 +32,13 @@ void MergedOutputSection::mergeInput(InputSection *input) {
void MergedOutputSection::finalize() {
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 += i->getSize();
- isecFileOff += i->getFileSize();
+ for (InputSection *isec : inputs) {
+ isecAddr = alignTo(isecAddr, isec->align);
+ isecFileOff = alignTo(isecFileOff, isec->align);
+ isec->outSecOff = isecAddr - addr;
+ isec->outSecFileOff = isecFileOff - fileOff;
+ isecAddr += isec->getSize();
+ isecFileOff += isec->getFileSize();
}
size = isecAddr - addr;
fileSize = isecFileOff - fileOff;
diff --git a/lld/test/MachO/section-merge.s b/lld/test/MachO/section-merge.s
index 33e1eddd3044..69c55a047b49 100644
--- a/lld/test/MachO/section-merge.s
+++ b/lld/test/MachO/section-merge.s
@@ -1,31 +1,22 @@
# 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: .byte 0xca" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/foo.o
+# RUN: echo ".globl _bar; .data; .p2align 2; _bar: .byte 0xfe" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/bar.o
+# RUN: echo ".globl _baz; .data; .p2align 3; _baz: .byte 0xba" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/baz.o
+# RUN: echo ".globl _qux; .data; .p2align 0; _qux: .quad 0xdeadbeef" | 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 --syms --section=__data --full-contents %t/output | FileCheck %s
+# CHECK: 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
-# 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
+# CHECK: Contents of section __data:
+# CHECK-NEXT: {{0*}}[[#ADDR]] ca000000 fe000000 baefbead de000000
.section __TEXT,__text
.global _main
More information about the llvm-commits
mailing list