Skip to content

Commit

Permalink
[ELF][RISCV] Allow PT_LOAD to have overlapping p_offset ranges on EM_…
Browse files Browse the repository at this point in the history
…RISCV

Port the D64906 technique to RISC-V. It deletes 3 alignments at
PT_LOAD boundaries for the default case: the size of a RISC-V binary
decreases by at most 12kb.

llvm-svn: 370192
  • Loading branch information
MaskRay committed Aug 28, 2019
1 parent ab65945 commit 523f999
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 156 deletions.
6 changes: 3 additions & 3 deletions lld/ELF/Writer.cpp
Expand Up @@ -2224,9 +2224,9 @@ template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
// and non-executable segments.
//
// TODO Enable this technique on all targets.
bool enable =
config->emachine != EM_HEXAGON && config->emachine != EM_MIPS &&
config->emachine != EM_RISCV && config->emachine != EM_X86_64;
bool enable = config->emachine != EM_HEXAGON &&
config->emachine != EM_MIPS &&
config->emachine != EM_X86_64;

if (!enable || (config->zSeparateCode && prev &&
(prev->p_flags & PF_X) != (p->p_flags & PF_X)))
Expand Down
17 changes: 10 additions & 7 deletions lld/test/ELF/riscv-gp.s
@@ -1,19 +1,22 @@
# REQUIRES: riscv
# RUN: llvm-mc -filetype=obj -triple=riscv32 %s -o %t.32.o
# RUN: ld.lld -pie %t.32.o -o %t.32
# RUN: llvm-readelf -s %t.32 | FileCheck --check-prefix=SYM %s
# RUN: llvm-readelf -S %t.32 | FileCheck --check-prefix=SEC %s
# RUN: llvm-readelf -s %t.32 | FileCheck --check-prefix=SYM32 %s
# RUN: llvm-readelf -S %t.32 | FileCheck --check-prefix=SEC32 %s
# RUN: not ld.lld -shared %t.32.o -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s

# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.64.o
# RUN: ld.lld -pie %t.64.o -o %t.64
# RUN: llvm-readelf -s %t.64 | FileCheck --check-prefix=SYM %s
# RUN: llvm-readelf -S %t.64 | FileCheck --check-prefix=SEC %s
# RUN: llvm-readelf -s %t.64 | FileCheck --check-prefix=SYM64 %s
# RUN: llvm-readelf -S %t.64 | FileCheck --check-prefix=SEC64 %s
# RUN: not ld.lld -shared %t.64.o -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s

## __global_pointer$ = .sdata+0x800 = 0x3800
# SEC: [ 7] .sdata PROGBITS {{0*}}00003000
# SYM: {{0*}}00003800 0 NOTYPE GLOBAL DEFAULT 7 __global_pointer$
## __global_pointer$ = .sdata+0x800 = 0x39b8
# SEC32: [ 7] .sdata PROGBITS {{0*}}000031b8
# SYM32: {{0*}}000039b8 0 NOTYPE GLOBAL DEFAULT 7 __global_pointer$

# SEC64: [ 7] .sdata PROGBITS {{0*}}000032d0
# SYM64: {{0*}}00003ad0 0 NOTYPE GLOBAL DEFAULT 7 __global_pointer$

## __global_pointer$ - 0x1000 = 4096*3-2048
# DIS: 1000: auipc gp, 3
Expand Down
65 changes: 33 additions & 32 deletions lld/test/ELF/riscv-plt.s
Expand Up @@ -2,18 +2,18 @@
# RUN: echo '.globl bar, weak; .type bar,@function; .type weak,@function; bar: weak:' > %t1.s

# RUN: llvm-mc -filetype=obj -triple=riscv32 %t1.s -o %t1.32.o
# RUN: ld.lld -shared %t1.32.o -o %t1.32.so
# RUN: ld.lld -shared %t1.32.o -soname=t1.32.so -o %t1.32.so
# RUN: llvm-mc -filetype=obj -triple=riscv32 %s -o %t.32.o
# RUN: ld.lld %t.32.o %t1.32.so -o %t.32
# RUN: ld.lld %t.32.o %t1.32.so -z separate-code -o %t.32
# RUN: llvm-readelf -S -s %t.32 | FileCheck --check-prefixes=SEC,NM %s
# RUN: llvm-readobj -r %t.32 | FileCheck --check-prefix=RELOC32 %s
# RUN: llvm-readelf -x .got.plt %t.32 | FileCheck --check-prefix=GOTPLT32 %s
# RUN: llvm-objdump -d --no-show-raw-insn %t.32 | FileCheck --check-prefixes=DIS,DIS32 %s

# RUN: llvm-mc -filetype=obj -triple=riscv64 %t1.s -o %t1.64.o
# RUN: ld.lld -shared %t1.64.o -o %t1.64.so
# RUN: ld.lld -shared %t1.64.o -soname=t1.64.so -o %t1.64.so
# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.64.o
# RUN: ld.lld %t.64.o %t1.64.so -o %t.64
# RUN: ld.lld %t.64.o %t1.64.so -z separate-code -o %t.64
# RUN: llvm-readelf -S -s %t.64 | FileCheck --check-prefixes=SEC,NM %s
# RUN: llvm-readobj -r %t.64 | FileCheck --check-prefix=RELOC64 %s
# RUN: llvm-readelf -x .got.plt %t.64 | FileCheck --check-prefix=GOTPLT64 %s
Expand All @@ -29,63 +29,64 @@
## The .got.plt slots relocated by .rela.plt point to .plt
## This is required by glibc.
# RELOC32: .rela.plt {
# RELOC32-NEXT: 0x13008 R_RISCV_JUMP_SLOT bar 0x0
# RELOC32-NEXT: 0x1300C R_RISCV_JUMP_SLOT weak 0x0
# RELOC32-NEXT: 0x13070 R_RISCV_JUMP_SLOT bar 0x0
# RELOC32-NEXT: 0x13074 R_RISCV_JUMP_SLOT weak 0x0
# RELOC32-NEXT: }
# GOTPLT32: section '.got.plt'
# GOTPLT32-NEXT: 0x00013000 00000000 00000000 30100100 30100100
# GOTPLT32-NEXT: 0x00013068 00000000 00000000 30100100 30100100

# RELOC64: .rela.plt {
# RELOC64-NEXT: 0x13010 R_RISCV_JUMP_SLOT bar 0x0
# RELOC64-NEXT: 0x13018 R_RISCV_JUMP_SLOT weak 0x0
# RELOC64-NEXT: 0x130E0 R_RISCV_JUMP_SLOT bar 0x0
# RELOC64-NEXT: 0x130E8 R_RISCV_JUMP_SLOT weak 0x0
# RELOC64-NEXT: }
# GOTPLT64: section '.got.plt'
# GOTPLT64-NEXT: 0x00013000 00000000 00000000 00000000 00000000
# GOTPLT64-NEXT: 0x00013010 30100100 00000000 30100100 00000000
# GOTPLT64-NEXT: 0x000130d0 00000000 00000000 00000000 00000000
# GOTPLT64-NEXT: 0x000130e0 30100100 00000000 30100100 00000000

# DIS: _start:
## Direct call
## foo - . = 0x11020-0x11000 = 32
# DIS-NEXT: auipc ra, 0
# DIS-NEXT: 11004: jalr 32(ra)
## bar@plt - . = 0x11050-0x1100c = 72
# DIS-NEXT: auipc ra, 0
# DIS-NEXT: 1100c: jalr 72(ra)
## bar@plt - . = 0x11050-0x11014 = 64
# DIS-NEXT: auipc ra, 0
# DIS-NEXT: 11014: jalr 64(ra)
## weak@plt - . = 0x11060-0x1101c = 72
# DIS-NEXT: auipc ra, 0
# DIS-NEXT: 1101c: jalr 72(ra)
# DIS-NEXT: 11000: auipc ra, 0
# DIS-NEXT: jalr 32(ra)
## bar@plt - . = 0x11050-0x11008 = 72
# DIS-NEXT: 11008: auipc ra, 0
# DIS-NEXT: jalr 72(ra)
## bar@plt - . = 0x11050-0x11010 = 64
# DIS-NEXT: 11010: auipc ra, 0
# DIS-NEXT: jalr 64(ra)
## weak@plt - . = 0x11060-0x11018 = 72
# DIS-NEXT: 11018: auipc ra, 0
# DIS-NEXT: jalr 72(ra)
# DIS: foo:
# DIS-NEXT: 11020:

# DIS: Disassembly of section .plt:
# DIS: .plt:
# DIS-NEXT: auipc t2, 2
# DIS-NEXT: sub t1, t1, t3
## .got.plt - .plt = 0x13000 - 0x11030 = 4096*2-48
# DIS32-NEXT: lw t3, -48(t2)
# DIS64-NEXT: ld t3, -48(t2)
## .got.plt - .plt = 0x13068 - 0x11030 = 4096*2+56
# DIS32-NEXT: lw t3, 56(t2)
# DIS64-NEXT: ld t3, 160(t2)
# DIS-NEXT: addi t1, t1, -44
# DIS-NEXT: addi t0, t2, -48
# DIS32-NEXT: addi t0, t2, 56
# DIS64-NEXT: addi t0, t2, 160
# DIS32-NEXT: srli t1, t1, 2
# DIS64-NEXT: srli t1, t1, 1
# DIS32-NEXT: lw t0, 4(t0)
# DIS64-NEXT: ld t0, 8(t0)
# DIS-NEXT: jr t3

## 32-bit: &.got.plt[bar]-. = 0x13008-0x11050 = 4096*2-72
## 32-bit: &.got.plt[bar]-. = 0x13070-0x11050 = 4096*2+32
# DIS: 11050: auipc t3, 2
# DIS32-NEXT: lw t3, -72(t3)
# DIS64-NEXT: ld t3, -64(t3)
# DIS32-NEXT: lw t3, 32(t3)
# DIS64-NEXT: ld t3, 144(t3)
# DIS-NEXT: jalr t1, t3
# DIS-NEXT: nop

## 32-bit: &.got.plt[weak]-. = 0x1300c-0x11060 = 4096*2-84
## 32-bit: &.got.plt[weak]-. = 0x13074-0x11060 = 4096*2+20
# DIS: 11060: auipc t3, 2
# DIS32-NEXT: lw t3, -84(t3)
# DIS64-NEXT: ld t3, -72(t3)
# DIS32-NEXT: lw t3, 20(t3)
# DIS64-NEXT: ld t3, 136(t3)
# DIS-NEXT: jalr t1, t3
# DIS-NEXT: nop

Expand Down
19 changes: 10 additions & 9 deletions lld/test/ELF/riscv-reloc-copy.s
@@ -1,23 +1,24 @@
# REQUIRES: riscv
# RUN: llvm-mc -filetype=obj -triple=riscv32 %p/Inputs/relocation-copy.s -o %t1.o
# RUN: ld.lld -shared %t1.o -o %t1.so
# RUN: ld.lld -shared %t1.o -soname=t1.so -o %t1.so
# RUN: llvm-mc -filetype=obj -triple=riscv32 %s -o %t.o
# RUN: ld.lld %t.o %t1.so -o %t
# RUN: llvm-readobj -r %t | FileCheck --check-prefix=RELOC %s
# RUN: llvm-readobj -r %t | FileCheck --check-prefixes=REL,REL32 %s
# RUN: llvm-nm -S %t | FileCheck --check-prefix=NM32 %s

# RUN: llvm-mc -filetype=obj -triple=riscv64 %p/Inputs/relocation-copy.s -o %t1.o
# RUN: ld.lld -shared %t1.o -o %t1.so
# RUN: ld.lld -shared %t1.o -soname=t1.so -o %t1.so
# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.o
# RUN: ld.lld %t.o %t1.so -o %t
# RUN: llvm-readobj -r %t | FileCheck --check-prefix=RELOC %s
# RUN: llvm-readobj -r %t | FileCheck --check-prefixes=REL,REL64 %s
# RUN: llvm-nm -S %t | FileCheck --check-prefix=NM64 %s

# RELOC: .rela.dyn {
# RELOC-NEXT: 0x13000 R_RISCV_COPY x 0x0
# RELOC-NEXT: }
# REL: .rela.dyn {
# REL32-NEXT: 0x13210 R_RISCV_COPY x 0x0
# REL64-NEXT: 0x13360 R_RISCV_COPY x 0x0
# REL-NEXT: }

# NM32: 00013000 00000004 B x
# NM64: 0000000000013000 0000000000000004 B x
# NM32: 00013210 00000004 B x
# NM64: 0000000000013360 0000000000000004 B x

la a0, x
50 changes: 27 additions & 23 deletions lld/test/ELF/riscv-reloc-got.s
@@ -1,59 +1,63 @@
# REQUIRES: riscv
# RUN: echo '.globl b; b:' | llvm-mc -filetype=obj -triple=riscv32 - -o %t1.o
# RUN: ld.lld -shared %t1.o -o %t1.so
# RUN: ld.lld -shared %t1.o -soname=t1.so -o %t1.so

# RUN: llvm-mc -filetype=obj -triple=riscv32 -position-independent %s -o %t.o
# RUN: ld.lld %t.o %t1.so -o %t
# RUN: llvm-readelf -S %t | FileCheck --check-prefix=SEC32 %s
# RUN: llvm-readobj -r %t | FileCheck --check-prefix=RELOC32 %s
# RUN: llvm-nm %t | FileCheck --check-prefix=NM %s
# RUN: llvm-nm %t | FileCheck --check-prefix=NM32 %s
# RUN: llvm-readobj -x .got %t | FileCheck --check-prefix=HEX32 %s
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=DIS32 %s

# RUN: echo '.globl b; b:' | llvm-mc -filetype=obj -triple=riscv64 - -o %t1.o
# RUN: ld.lld -shared %t1.o -o %t1.so
# RUN: ld.lld -shared %t1.o -soname=t1.so -o %t1.so

# RUN: llvm-mc -filetype=obj -triple=riscv64 -position-independent %s -o %t.o
# RUN: ld.lld %t.o %t1.so -o %t
# RUN: llvm-readelf -S %t | FileCheck --check-prefix=SEC64 %s
# RUN: llvm-readobj -r %t | FileCheck --check-prefix=RELOC64 %s
# RUN: llvm-nm %t | FileCheck --check-prefix=NM %s
# RUN: llvm-nm %t | FileCheck --check-prefix=NM64 %s
# RUN: llvm-readobj -x .got %t | FileCheck --check-prefix=HEX64 %s
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=DIS64 %s

# SEC: .got PROGBITS 00012060 020060 00000c
# SEC32: .got PROGBITS 0001220c 00020c 00000c
# SEC64: .got PROGBITS 0000000000012358 000358 000018

# RELOC32: .rela.dyn {
# RELOC32-NEXT: 0x12068 R_RISCV_32 b 0x0
# RELOC32-NEXT: 0x12214 R_RISCV_32 b 0x0
# RELOC32-NEXT: }

# RELOC64: .rela.dyn {
# RELOC64-NEXT: 0x120D0 R_RISCV_64 b 0x0
# RELOC64-NEXT: 0x12368 R_RISCV_64 b 0x0
# RELOC64-NEXT: }

# NM: 00013000 d a
# NM32: 00013218 d a
# NM64: 0000000000013370 d a

## .got[0] = _DYNAMIC
## .got[1] = a (filled at link time)
## .got[2] = 0 (relocated by R_RISCV_64 at runtime)
# HEX32: section '.got':
# HEX32: 0x00012060 00200100 00300100 00000000
# HEX32: 0x0001220c ac210100 18320100 00000000

# HEX64: section '.got':
# HEX64: 0x000120c0 00200100 00000000 00300100 00000000
# HEX64: 0x000120d0 00000000 00000000
# HEX64: 0x00012358 98220100 00000000 70330100 00000000
# HEX64: 0x00012368 00000000 00000000

## &.got[1]-. = 0x12060-0x11000 = 4096*1+100
# DIS32: 11000: auipc a0, 1
# DIS32-NEXT: lw a0, 100(a0)
## &.got[2]-. = 0x12064-0x11008 = 4096*1+96
# DIS32: 11008: auipc a0, 1
# DIS32-NEXT: lw a0, 96(a0)
## &.got[1]-. = 0x12210-0x1119c = 4096*1+116
# DIS32: 1119c: auipc a0, 1
# DIS32-NEXT: lw a0, 116(a0)
## &.got[2]-. = 0x12214-0x111a4 = 4096*1+112
# DIS32: 111a4: auipc a0, 1
# DIS32-NEXT: lw a0, 112(a0)

## &.got[1]-. = 0x120c8-0x11000 = 4096*1+100
# DIS64: 11000: auipc a0, 1
# DIS64-NEXT: ld a0, 200(a0)
## &.got[2]-. = 0x120d0-0x11008 = 4096*1+200
# DIS64: 11008: auipc a0, 1
# DIS64-NEXT: ld a0, 200(a0)
## &.got[1]-. = 0x12360-0x11288 = 4096*1+216
# DIS64: 11288: auipc a0, 1
# DIS64-NEXT: ld a0, 216(a0)
## &.got[2]-. = 0x12368-0x11290 = 4096*1+216
# DIS64: 11290: auipc a0, 1
# DIS64-NEXT: ld a0, 216(a0)

la a0,a
la a0,b
Expand Down
58 changes: 29 additions & 29 deletions lld/test/ELF/riscv-tls-gd.s
Expand Up @@ -46,42 +46,42 @@
# RUN: llvm-readelf -x .got %t.64 | FileCheck --check-prefix=IE64-GOT %s

# GD32-REL: .rela.dyn {
# GD32-REL-NEXT: 0x2070 R_RISCV_TLS_DTPMOD32 a 0x0
# GD32-REL-NEXT: 0x2074 R_RISCV_TLS_DTPREL32 a 0x0
# GD32-REL-NEXT: 0x2078 R_RISCV_TLS_DTPMOD32 b 0x0
# GD32-REL-NEXT: 0x207C R_RISCV_TLS_DTPREL32 b 0x0
# GD32-REL-NEXT: 0x2310 R_RISCV_TLS_DTPMOD32 a 0x0
# GD32-REL-NEXT: 0x2314 R_RISCV_TLS_DTPREL32 a 0x0
# GD32-REL-NEXT: 0x2318 R_RISCV_TLS_DTPMOD32 b 0x0
# GD32-REL-NEXT: 0x231C R_RISCV_TLS_DTPREL32 b 0x0
# GD32-REL-NEXT: }

## &DTPMOD(a) - . = 0x2070 - 0x1000 = 4096*1+112
# GD32: 1000: auipc a0, 1
# GD32-NEXT: addi a0, a0, 112
## &DTPMOD(a) - . = 0x2310 - 0x1250 = 4096*1+192
# GD32: 1250: auipc a0, 1
# GD32-NEXT: addi a0, a0, 192
# GD32-NEXT: auipc ra, 0
# GD32-NEXT: jalr 56(ra)

## &DTPMOD(b) - . = 0x2078 - 0x1010 = 4096*1+104
# GD32: 1010: auipc a0, 1
# GD32-NEXT: addi a0, a0, 104
## &DTPMOD(b) - . = 0x2318 - 0x1260 = 4096*1+184
# GD32: 1260: auipc a0, 1
# GD32-NEXT: addi a0, a0, 184
# GD32-NEXT: auipc ra, 0
# GD32-NEXT: jalr 40(ra)

# GD64-REL: .rela.dyn {
# GD64-REL-NEXT: 0x20E0 R_RISCV_TLS_DTPMOD64 a 0x0
# GD64-REL-NEXT: 0x20E8 R_RISCV_TLS_DTPREL64 a 0x0
# GD64-REL-NEXT: 0x20F0 R_RISCV_TLS_DTPMOD64 b 0x0
# GD64-REL-NEXT: 0x20F8 R_RISCV_TLS_DTPREL64 b 0x0
# GD64-REL-NEXT: 0x24D0 R_RISCV_TLS_DTPMOD64 a 0x0
# GD64-REL-NEXT: 0x24D8 R_RISCV_TLS_DTPREL64 a 0x0
# GD64-REL-NEXT: 0x24E0 R_RISCV_TLS_DTPMOD64 b 0x0
# GD64-REL-NEXT: 0x24E8 R_RISCV_TLS_DTPREL64 b 0x0
# GD64-REL-NEXT: }

## &DTPMOD(a) - . = 0x20e0 - 0x1000 = 4096*1+224
# GD64: 1000: auipc a0, 1
# GD64-NEXT: addi a0, a0, 224
## &DTPMOD(a) - . = 0x24d0 - 0x1398 = 4096*1+312
# GD64: 1398: auipc a0, 1
# GD64-NEXT: addi a0, a0, 312
# GD64-NEXT: auipc ra, 0
# GD64-NEXT: jalr 56(ra)
# GD64-NEXT: jalr 64(ra)

## &DTPMOD(b) - . = 0x20f0 - 0x1010 = 4096*1+224
# GD64: 1010: auipc a0, 1
# GD64-NEXT: addi a0, a0, 224
## &DTPMOD(b) - . = 0x24e0 - 0x13a8 = 4096*1+312
# GD64: 13a8: auipc a0, 1
# GD64-NEXT: addi a0, a0, 312
# GD64-NEXT: auipc ra, 0
# GD64-NEXT: jalr 40(ra)
# GD64-NEXT: jalr 48(ra)

# NOREL: no relocations

Expand All @@ -97,19 +97,19 @@
## a is local - relaxed to LE - its DTPMOD/DTPREL slots are link-time constants.
## b is external - DTPMOD/DTPREL dynamic relocations are required.
# IE32-REL: .rela.dyn {
# IE32-REL-NEXT: 0x12068 R_RISCV_TLS_DTPMOD32 b 0x0
# IE32-REL-NEXT: 0x1206C R_RISCV_TLS_DTPREL32 b 0x0
# IE32-REL-NEXT: 0x12230 R_RISCV_TLS_DTPMOD32 b 0x0
# IE32-REL-NEXT: 0x12234 R_RISCV_TLS_DTPREL32 b 0x0
# IE32-REL-NEXT: }
# IE32-GOT: section '.got':
# IE32-GOT-NEXT: 0x00012060 01000000 08f8ffff 00000000 00000000
# IE32-GOT-NEXT: 0x00012228 01000000 08f8ffff 00000000 00000000

# IE64-REL: .rela.dyn {
# IE64-REL-NEXT: 0x120D0 R_RISCV_TLS_DTPMOD64 b 0x0
# IE64-REL-NEXT: 0x120D8 R_RISCV_TLS_DTPREL64 b 0x0
# IE64-REL-NEXT: 0x12390 R_RISCV_TLS_DTPMOD64 b 0x0
# IE64-REL-NEXT: 0x12398 R_RISCV_TLS_DTPREL64 b 0x0
# IE64-REL-NEXT: }
# IE64-GOT: section '.got':
# IE64-GOT-NEXT: 0x000120c0 01000000 00000000 08f8ffff ffffffff
# IE64-GOT-NEXT: 0x000120d0 00000000 00000000 00000000 00000000
# IE64-GOT-NEXT: 0x00012380 01000000 00000000 08f8ffff ffffffff
# IE64-GOT-NEXT: 0x00012390 00000000 00000000 00000000 00000000

la.tls.gd a0,a
call __tls_get_addr@plt
Expand Down

0 comments on commit 523f999

Please sign in to comment.