llvm-project/lld/test/ELF/ppc64-long-branch.s

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

92 lines
3.1 KiB
ArmAsm
Raw Normal View History

# REQUIRES: ppc
# RUN: echo 'SECTIONS { \
# RUN: .text_low 0x2000: { *(.text_low) } \
# RUN: .text_high 0x2002000 : { *(.text_high) } \
# RUN: }' > %t.script
# RUN: llvm-mc -filetype=obj -triple=ppc64le %s -o %t.o
# RUN: ld.lld -T %t.script %t.o -o %t
# RUN: llvm-readelf -S -r %t | FileCheck --check-prefix=SEC %s
# RUN: llvm-readelf -x .branch_lt %t | FileCheck --check-prefix=BRANCH-LE %s
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
# RUN: llvm-nm --no-sort %t | FileCheck --check-prefix=NM %s
# RUN: llvm-mc -filetype=obj -triple=ppc64 %s -o %t.o
# RUN: ld.lld -T %t.script %t.o -o %t
# RUN: llvm-readelf -S -r %t | FileCheck --check-prefix=SEC %s
# RUN: llvm-readelf -x .branch_lt %t | FileCheck --check-prefix=BRANCH-BE %s
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
# RUN: llvm-nm --no-sort %t | FileCheck --check-prefix=NM %s
# SEC: Name Type Address Off Size ES Flg Lk Inf Al
# SEC: .got PROGBITS 0000000002002030 2002030 000008 00 WA 0 0 8
# SEC: .branch_lt PROGBITS 0000000002002038 2002038 000018 00 WA 0 0 8
# SEC: There are no relocations in this file.
## high@localentry (high+8), .text_high+16 and .text_low+8
# BRANCH-LE: 0x02002038 08200002 00000000 10200002 00000000
# BRANCH-LE-NEXT: 0x02002048 08200000 00000000
# BRANCH-BE: 0x02002038 00000000 02002008 00000000 02002010
# BRANCH-BE-NEXT: 0x02002048 00000000 00002008
# CHECK: <_start>:
# CHECK-NEXT: 2000: bl 0x2020
# CHECK-NEXT: bl 0x2020
# CHECK-NEXT: bl 0x2020
# CHECK-NEXT: bl 0x2002008
## &.branch_lt[0] - .TOC. = .branch_lt - (.got+0x8000) = -32760
# CHECK: <__long_branch_high>:
# CHECK-NEXT: 2020: addis 12, 2, 0
# CHECK-NEXT: ld 12, -32760(12)
# CHECK-NEXT: mtctr 12
# CHECK-NEXT: bctr
## &.branch_lt[1] - .TOC. = .branch_lt - (.got+0x8000) = -32752
# CHECK: <__long_branch_>:
# CHECK-NEXT: 2030: addis 12, 2, 0
# CHECK-NEXT: ld 12, -32752(12)
# CHECK-NEXT: mtctr 12
# CHECK-NEXT: bctr
.section .text_low, "ax", %progbits
.globl _start
_start:
bl high # Need a thunk
bl high # Need a thunk
bl high # Need a thunk
bl high
bl .text_high+16 # Need a thunk
blr
# CHECK: Disassembly of section .text_high:
# CHECK-EMPTY:
# CHECK-NEXT: <high>:
# CHECK-NEXT: 2002000: addis 2, 12, 1
# CHECK-NEXT: addi 2, 2, -32720
# CHECK-NEXT: bl 0x2008
# CHECK-NEXT: bl 0x2002020
# CHECK: <__long_branch_>:
# CHECK-NEXT: 2002020: addis 12, 2, 0
[ELF][PPC] Allow PT_LOAD to have overlapping p_offset ranges This change affects the non-linker script case (precisely, when the `SECTIONS` command is not used). It deletes 3 alignments at PT_LOAD boundaries for the default case: the size of a powerpc64 binary can be decreased by at most 192kb. The technique can be ported to other targets. Let me demonstrate the idea with a maxPageSize=65536 example: When assigning the address to the first output section of a new PT_LOAD, if the end p_vaddr of the previous PT_LOAD is 0x10020, we advance to the next multiple of maxPageSize: 0x20000. The new PT_LOAD will thus have p_vaddr=0x20000. Because p_offset and p_vaddr are congruent modulo maxPageSize, p_offset will be 0x20000, leaving a p_offset gap [0x10020, 0x20000) in the output. Alternatively, if we advance to 0x20020, the new PT_LOAD will have p_vaddr=0x20020. We can pick either 0x10020 or 0x20020 for p_offset! Obviously 0x10020 is the choice because it leaves no gap. At runtime, p_vaddr will be rounded down by pagesize (65536 if pagesize=maxPageSize). This PT_LOAD will load additional initial contents from p_offset ranges [0x10000,0x10020), which will also be loaded by the previous PT_LOAD. This is fine if -z noseparate-code is in effect or if we are not transiting between executable and non-executable segments. ld.bfd -z noseparate-code leverages this technique to keep output small. This patch implements the technique in lld, which is mostly effective on targets with large defaultMaxPageSize (AArch64/MIPS/PPC: 65536). The 3 removed alignments can save almost 3*65536 bytes. Two places that rely on p_vaddr%pagesize = 0 have to be updated. 1) We used to round p_memsz(PT_GNU_RELRO) up to commonPageSize (defaults to 4096 on all targets). Now p_vaddr%commonPageSize may be non-zero. The updated formula takes account of that factor. 2) Our TP offsets formulae are only correct if p_vaddr%p_align = 0. Fix them. See the updated comments in InputSection.cpp for details. On targets that we enable the technique (only PPC64 now), we can potentially make `p_vaddr(PT_TLS)%p_align(PT_TLS) != 0` if `sh_addralign(.tdata) < sh_addralign(.tbss)` This exposes many problems in ld.so implementations, especially the offsets of dynamic TLS blocks. Known issues: FreeBSD 13.0-CURRENT rtld-elf (i386/amd64/powerpc/arm64) glibc (HEAD) i386 and x86_64 https://sourceware.org/bugzilla/show_bug.cgi?id=24606 musl<=1.1.22 on TLS Variant I architectures (aarch64/powerpc64/...) So, force p_vaddr%p_align = 0 by rounding dot up to p_align(PT_TLS). The technique will be enabled (with updated tests) for other targets in subsequent patches. Reviewed By: ruiu Differential Revision: https://reviews.llvm.org/D64906 llvm-svn: 369343
2019-08-20 16:34:25 +08:00
# CHECK-NEXT: ld 12, -32744(12)
# CHECK-NEXT: mtctr 12
# CHECK-NEXT: bctr
.section .text_high, "ax", %progbits
.globl high
high:
addis 2, 12, .TOC.-high@ha
addi 2, 2, .TOC.-high@l
.localentry high, 8
bl .text_low+8
bl .text_low+8 # Need a thunk
blr
# NM: d .TOC.
# NM-NEXT: t __long_branch_high
# NM-NEXT: t __long_branch_{{$}}
# NM-NEXT: t __long_branch_{{$}}
# NM-NEXT: T _start
# NM-NEXT: T high