File: riscv-undefined-weak.s

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (102 lines) | stat: -rw-r--r-- 3,106 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# REQUIRES: riscv
# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.o
# RUN: llvm-readobj -r %t.o | FileCheck --check-prefix=RELOC %s

# RUN: ld.lld -e absolute %t.o -o %t
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefixes=CHECK,PC %s
# RUN: llvm-readelf -x .data %t | FileCheck --check-prefixes=HEX,HEX-WITHOUT-PLT %s

# RUN: ld.lld -e absolute %t.o -o %t --export-dynamic
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefixes=CHECK,PLT %s
# RUN: llvm-readelf -x .data %t | FileCheck --check-prefixes=HEX,HEX-WITH-PLT %s

.weak target
.global absolute, relative, branch

## Absolute relocations are resolved to 0.
# RELOC:      0x0 R_RISCV_HI20 target 0x1
# RELOC-NEXT: 0x4 R_RISCV_LO12_I target 0x1

# CHECK-LABEL: <absolute>:
# CHECK-NEXT:  lui t0, 0
# CHECK-NEXT:  addi t0, t0, 1
absolute:
  lui t0, %hi(target+1)
  addi t0, t0, %lo(target+1)

## Currently, PC-relative relocations are resolved to 0.
# RELOC-NEXT: 0x8 R_RISCV_PCREL_HI20 target 0x0
# RELOC-NEXT: 0xC R_RISCV_PCREL_LO12_I .Lpcrel_hi0 0x0
# RELOC-NEXT: 0x10 R_RISCV_PCREL_HI20 target 0x2
# RELOC-NEXT: 0x14 R_RISCV_PCREL_LO12_S .Lpcrel_hi1 0x0

## 1048559 should be -0x11.
# CHECK-LABEL: <relative>:
# CHECK-NEXT:  11{{...}}: auipc a1, 1048559
# PC-NEXT:     addi a1, a1, -352
# PLT-NEXT:    addi a1, a1, -792
# CHECK-LABEL: <.Lpcrel_hi1>:
# CHECK-NEXT:  11{{...}}: auipc t1, 1048559
# PC-NEXT:     sd a2, -358(t1)
# PLT-NEXT:    sd a2, -798(t1)
relative:
  la a1, target
  sd a2, target+2, t1

## Branch relocations
## If .dynsym does not exist, an undefined weak symbol is non-preemptible.
## Treat them as PC relative relocations.
# RELOC:      0x18 R_RISCV_CALL_PLT target 0x0
# RELOC-NEXT: 0x20 R_RISCV_JAL target 0x0
# RELOC-NEXT: 0x24 R_RISCV_BRANCH target 0x0

# PC-LABEL:    <branch>:
# PC-NEXT:     auipc ra, 0
# PC-NEXT:     jalr ra
# PC-NEXT:     [[#%x,ADDR:]]:
# PC-SAME:                    j 0x[[#ADDR]]
# PC-NEXT:     [[#%x,ADDR:]]:
# PC-SAME:                    beqz zero, 0x[[#ADDR]]

## If .dynsym exists, an undefined weak symbol is preemptible.
## We create a PLT entry and redirect the reference to it.
# PLT-LABEL:   <branch>:
# PLT-NEXT:    auipc ra, 0
# PLT-NEXT:    jalr 56(ra)
# PLT-NEXT:    [[#%x,ADDR:]]:
# PLT-SAME:                   j 0x[[#ADDR]]
# PLT-NEXT:    [[#%x,ADDR:]]:
# PLT-SAME:                   beqz zero, 0x[[#ADDR]]
branch:
  call target
  jal x0, target
  beq x0, x0, target

## Absolute relocations are resolved to 0.
# RELOC:      0x0 R_RISCV_64 target 0x3
# RELOC-NEXT: 0x8 R_RISCV_32 target 0x4
# RELOC-NEXT: 0xC R_RISCV_PLT32 target 0x0

# HEX:      section '.data':
# HEX-NEXT: 03000000 00000000 04000000
# HEX-WITHOUT-PLT-SAME: 00000000

## A plt entry is created for target, so this is the offset between the
## plt entry and this address.
##
##   S = 0x11360 (the address of the plt entry for target)
##   A = 0
##   P = 0x1343c (the address of `.`)
##
##   S - A + P = -0x0x20dc = 0xffffdf24
# HEX-WITH-PLT-SAME: 24dfffff

.data
.p2align 3
.quad target+3
.long target+4

# PC-NOT:      .plt:
# PLT:         .plt:

.word target@plt - .