|
| 1 | +# REQUIRES: loongarch |
| 2 | + |
| 3 | +# RUN: rm -rf %t && split-file %s %t && cd %t |
| 4 | + |
| 5 | +# RUN: llvm-mc -filetype=obj -triple=loongarch64 -mattr=+relax a.s -o a.64.o |
| 6 | +# RUN: llvm-mc -filetype=obj -triple=loongarch64 -mattr=+relax b.s -o b.64.o |
| 7 | +# RUN: ld.lld -shared -soname=b.so b.64.o -o b.64.so |
| 8 | +# RUN: ld.lld -T lds a.64.o b.64.so -o 64 |
| 9 | +# RUN: llvm-objdump -td --no-show-raw-insn 64 | FileCheck %s --check-prefix=RELAX |
| 10 | + |
| 11 | +## --no-relax disables relaxation. |
| 12 | +# RUN: ld.lld -T lds a.64.o b.64.so --no-relax -o 64.norelax |
| 13 | +# RUN: llvm-objdump -td --no-show-raw-insn 64.norelax | FileCheck %s --check-prefix=NORELAX |
| 14 | + |
| 15 | +# RELAX: {{0*}}00010000 g .text {{0*}}0000001c _start |
| 16 | +# RELAX: {{0*}}0001001c g .text {{0*}}00000000 _start_end |
| 17 | +# RELAX: {{0*}}00010808 g .mid {{0*}}00000000 mid_end |
| 18 | +# RELAX: {{0*}}10010010 g .high {{0*}}00000000 high_end |
| 19 | + |
| 20 | +# RELAX-LABEL: <_start>: |
| 21 | +## offset = 0x10018 - 0x10000 = 24 |
| 22 | +# RELAX-NEXT: 10000: bl 24 <a> |
| 23 | +# RELAX-NEXT: b 20 <a> |
| 24 | +# RELAX-NEXT: nop |
| 25 | +# RELAX-NEXT: nop |
| 26 | +## offset = .plt(0x10400)+32 - 0x10010 = 1040 |
| 27 | +# RELAX-NEXT: 10010: bl 1040 <bar+0x10420> |
| 28 | +# RELAX-NEXT: b 1036 <bar+0x10420> |
| 29 | +# RELAX-EMPTY: |
| 30 | +# RELAX-NEXT: <a>: |
| 31 | +# RELAX-NEXT: 10018: ret |
| 32 | +# RELAX-EMPTY: |
| 33 | + |
| 34 | +# RELAX-LABEL: <.mid>: |
| 35 | +## offset = 0x10000 - 0x10800 = -2048 |
| 36 | +# RELAX-NEXT: 10800: bl -2048 <_start> |
| 37 | +# RELAX-NEXT: b -2052 <_start> |
| 38 | +# RELAX-EMPTY: |
| 39 | + |
| 40 | +# RELAX-LABEL: <.mid2>: |
| 41 | +## offset = 0x10000 - 0x1010000 = -16777216 |
| 42 | +# RELAX-NEXT: 1010000: bl -16777216 <_start> |
| 43 | +# RELAX-NEXT: b -16777220 <_start> |
| 44 | +# RELAX-EMPTY: |
| 45 | + |
| 46 | +# RELAX-LABEL: <.high>: |
| 47 | +## offset = 0x10000 - 0x10010000 = -0x10000000, hi=-1024, lo18=0 |
| 48 | +# RELAX-NEXT: 10010000: pcaddu18i $ra, -1024 |
| 49 | +# RELAX-NEXT: jirl $ra, $ra, 0 |
| 50 | +# RELAX-NEXT: pcaddu18i $t0, -1024 |
| 51 | +# RELAX-NEXT: jirl $zero, $t0, -8 |
| 52 | +# RELAX-EMPTY: |
| 53 | + |
| 54 | + |
| 55 | +# NORELAX-LABEL: <_start>: |
| 56 | +## offset = 0x10020 - 0x10000 = 0x20, hi=0, lo18=32 |
| 57 | +# NORELAX-NEXT: 10000: pcaddu18i $ra, 0 |
| 58 | +# NORELAX-NEXT: jirl $ra, $ra, 32 |
| 59 | +## offset = 0x10020 - 0x10008 = 0x18, hi=0, lo18=24 |
| 60 | +# NORELAX-NEXT: 10008: pcaddu18i $t0, 0 |
| 61 | +# NORELAX-NEXT: jirl $zero, $t0, 24 |
| 62 | +## offset = .plt(0x10400)+32 - 0x10010 = 0x410, hi=0, lo18=1040 |
| 63 | +# NORELAX-NEXT: 10010: pcaddu18i $ra, 0 |
| 64 | +# NORELAX-NEXT: jirl $ra, $ra, 1040 |
| 65 | +## offset = .plt(0x10400)+32 - 0x10018 = 0x408, hi=0, lo18=1032 |
| 66 | +# NORELAX-NEXT: 10018: pcaddu18i $t0, 0 |
| 67 | +# NORELAX-NEXT: jirl $zero, $t0, 1032 |
| 68 | +# NORELAX-EMPTY: |
| 69 | +# NORELAX-NEXT: <a>: |
| 70 | +# NORELAX-NEXT: 10020: ret |
| 71 | +# NORELAX-EMPTY: |
| 72 | + |
| 73 | +# NORELAX-LABEL: <.mid>: |
| 74 | +## offset = 0x10000 - 0x10800 = -0x800, hi=0, lo18=-2048 |
| 75 | +# NORELAX-NEXT: 10800: pcaddu18i $ra, 0 |
| 76 | +# NORELAX-NEXT: jirl $ra, $ra, -2048 |
| 77 | +# NORELAX-NEXT: pcaddu18i $t0, 0 |
| 78 | +# NORELAX-NEXT: jirl $zero, $t0, -2056 |
| 79 | +# NORELAX-EMPTY: |
| 80 | + |
| 81 | +# NORELAX-LABEL: <.mid2>: |
| 82 | +## offset = 0x10000 - 0x1010000 = -0x1000000, hi=-64, lo18=0 |
| 83 | +# NORELAX-NEXT: 1010000: pcaddu18i $ra, -64 |
| 84 | +# NORELAX-NEXT: jirl $ra, $ra, 0 |
| 85 | +# NORELAX-NEXT: pcaddu18i $t0, -64 |
| 86 | +# NORELAX-NEXT: jirl $zero, $t0, -8 |
| 87 | +# NORELAX-EMPTY: |
| 88 | + |
| 89 | +# NORELAX-LABEL: <.high>: |
| 90 | +## offset = 0x10000 - 0x10010000 = -0x10000000, hi=-1024, lo18=0 |
| 91 | +# NORELAX-NEXT: 10010000: pcaddu18i $ra, -1024 |
| 92 | +# NORELAX-NEXT: jirl $ra, $ra, 0 |
| 93 | +# NORELAX-NEXT: pcaddu18i $t0, -1024 |
| 94 | +# NORELAX-NEXT: jirl $zero, $t0, -8 |
| 95 | +# NORELAX-EMPTY: |
| 96 | + |
| 97 | +#--- a.s |
| 98 | +.global _start, _start_end |
| 99 | +_start: |
| 100 | + call36 a # relaxed. la64: bl |
| 101 | + tail36 $t0, a@plt # relaxed. la64: b |
| 102 | +.balign 16 |
| 103 | + call36 bar # PLT call36 can be relaxed. la64: bl |
| 104 | + tail36 $t0, bar # PLT tail36 can be relaxed. la64: bl |
| 105 | + |
| 106 | +a: |
| 107 | + ret |
| 108 | +.size _start, . - _start |
| 109 | +_start_end: |
| 110 | + |
| 111 | +.section .mid,"ax",@progbits |
| 112 | + call36 _start@plt # relaxed. la64: bl |
| 113 | + tail36 $t0, _start@plt # relaxed. la64: b |
| 114 | + |
| 115 | +.section .mid2,"ax",@progbits |
| 116 | + call36 _start@plt # relaxed. la64: bl |
| 117 | + tail36 $t0, _start@plt # relaxed. la64: b |
| 118 | + |
| 119 | +.section .high,"ax",@progbits |
| 120 | + call36 _start@plt # exceed range, not relaxed |
| 121 | + tail36 $t0,_start@plt # exceed range, not relaxed |
| 122 | + |
| 123 | +#--- b.s |
| 124 | +.globl bar |
| 125 | +bar: |
| 126 | + ret |
| 127 | + |
| 128 | +#--- lds |
| 129 | +SECTIONS { |
| 130 | + .text 0x10000 : { *(.text) } |
| 131 | + .plt 0x10400 : { *(.plt) } |
| 132 | + .mid 0x10800 : { *(.mid); mid_end = .; } |
| 133 | + .mid2 0x1010000 : { *(.mid2) } |
| 134 | + .high 0x10010000 : { *(.high); high_end = .; } |
| 135 | +} |
0 commit comments