[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
This commit is contained in:
Jez Ng 2020-06-13 20:06:29 -07:00
parent 74871cdad7
commit 525c7d8cda
2 changed files with 22 additions and 29 deletions

View File

@ -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;

View File

@ -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