summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChih-Mao Chen <pkmx.tw@gmail.com>2019-04-09 11:39:50 +0000
committerChih-Mao Chen <pkmx.tw@gmail.com>2019-04-09 11:39:50 +0000
commit00100b5d0d1c9b09ceb376f44a439be35973880b (patch)
treed5487185e964ae30b42d3ef7b6ef59acc5a7a8bf
parent949570ce39da590c59fecb4cd24501351aaa64fb (diff)
[RISCV] Rewrite tests to use llvm-mc/llvm-objdump
Previously the tests in lld for RISC-V were given in yaml format as LLVM's RISC-V assembler support was incomplete. Now that the assembler/disassembler has matured we can rewrite all tests to use LLVM's tools. Differential Revision: https://reviews.llvm.org/D60415 llvm-svn: 357996
-rw-r--r--lld/test/ELF/riscv-branch.test118
-rw-r--r--lld/test/ELF/riscv-call.test94
-rw-r--r--lld/test/ELF/riscv-hi20-lo12.test85
-rw-r--r--lld/test/ELF/riscv-jal-error.test92
-rw-r--r--lld/test/ELF/riscv-jal.test160
-rw-r--r--lld/test/ELF/riscv-pcrel-hilo.test102
-rw-r--r--test/ELF/riscv-branch.s32
-rw-r--r--test/ELF/riscv-call.s32
-rw-r--r--test/ELF/riscv-hi20-lo12.s40
-rw-r--r--test/ELF/riscv-jal.s34
-rw-r--r--test/ELF/riscv-pcrel-hilo.s41
11 files changed, 179 insertions, 651 deletions
diff --git a/lld/test/ELF/riscv-branch.test b/lld/test/ELF/riscv-branch.test
deleted file mode 100644
index c4eeceda649f..000000000000
--- a/lld/test/ELF/riscv-branch.test
+++ /dev/null
@@ -1,118 +0,0 @@
-# .option norelax
-# .global _start
-# _start:
-# beq x0, x0, _start
-#
-# .section .reloc_max, "ax", @progbits
-# L1:
-# beq x0, x0, L1 + 0xffe
-#
-# .section .reloc_min, "ax", @progbits
-# L2:
-# beq x0, x0, L2 - 0x1000
-#
-# REQUIRES: riscv
-# RUN: yaml2obj %s -o %t.o
-# RUN: ld.lld %t.o -o %t
-# RUN: obj2yaml %t | FileCheck %s
-#
-# CHECK: - Name: .text
-# CHECK: Content: '63000000'
-# 11000: 00000063 beqz zero,11000 <_start>
-#
-# CHECK: - Name: .reloc_max
-# CHECK: Content: E30F007E
-# 11004: 7e000fe3 beqz zero,12002
-#
-# CHECK: - Name: .reloc_min
-# CHECK: Content: '63000080'
-# 11008: 80000063 beqz zero,10008
-
---- !ELF
-FileHeader:
- Class: ELFCLASS32
- Data: ELFDATA2LSB
- Type: ET_REL
- Machine: EM_RISCV
- Flags: [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
-Sections:
- - Name: .text
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000002
- Content: '63000000'
- - Name: .rela.text
- Type: SHT_RELA
- Flags: [ SHF_INFO_LINK ]
- Link: .symtab
- AddressAlign: 0x0000000000000004
- Info: .text
- Relocations:
- - Offset: 0x0000000000000000
- Symbol: _start
- Type: R_RISCV_BRANCH
- - Name: .data
- Type: SHT_PROGBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
- Content: ''
- - Name: .bss
- Type: SHT_NOBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
- - Name: .reloc_max
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000001
- Content: E30F007E
- - Name: .rela.reloc_max
- Type: SHT_RELA
- Flags: [ SHF_INFO_LINK ]
- Link: .symtab
- AddressAlign: 0x0000000000000004
- Info: .reloc_max
- Relocations:
- - Offset: 0x0000000000000000
- Symbol: L1
- Type: R_RISCV_BRANCH
- Addend: 4094
- - Name: .reloc_min
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000001
- Content: '63000080'
- - Name: .rela.reloc_min
- Type: SHT_RELA
- Flags: [ SHF_INFO_LINK ]
- Link: .symtab
- AddressAlign: 0x0000000000000004
- Info: .reloc_min
- Relocations:
- - Offset: 0x0000000000000000
- Symbol: L2
- Type: R_RISCV_BRANCH
- Addend: -4096
-Symbols:
- - Name: .text
- Type: STT_SECTION
- Section: .text
- - Name: .data
- Type: STT_SECTION
- Section: .data
- - Name: .bss
- Type: STT_SECTION
- Section: .bss
- - Name: .reloc_max
- Type: STT_SECTION
- Section: .reloc_max
- - Name: L1
- Section: .reloc_max
- - Name: .reloc_min
- Type: STT_SECTION
- Section: .reloc_min
- - Name: L2
- Section: .reloc_min
- - Name: _start
- Section: .text
- Binding: STB_GLOBAL
-...
diff --git a/lld/test/ELF/riscv-call.test b/lld/test/ELF/riscv-call.test
deleted file mode 100644
index da689358b070..000000000000
--- a/lld/test/ELF/riscv-call.test
+++ /dev/null
@@ -1,94 +0,0 @@
-# .option norelax
-# .global _start
-# _start:
-# call _start + 4
-#
-# .section .reloc_neg, "ax", @progbits
-# L1:
-# call L1 - 4
-#
-# REQUIRES: riscv
-# RUN: yaml2obj %s -o %t.o
-# RUN: ld.lld %t.o -o %t
-# RUN: obj2yaml %t | FileCheck %s
-#
-# CHECK: - Name: .text
-# CHECK: Content: '97000000E7804000'
-#
-# 11000: 00000097 auipc ra,0x0
-# 11004: 004080e7 jalr 4(ra)
-#
-# CHECK: - Name: .reloc_neg
-# CHECK: Content: 97000000E780C0FF
-#
-# 11008: 00000097 auipc ra,0x0
-# 1100c: ffc080e7 jalr -4(ra)
-
---- !ELF
-FileHeader:
- Class: ELFCLASS32
- Data: ELFDATA2LSB
- Type: ET_REL
- Machine: EM_RISCV
- Flags: [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
-Sections:
- - Name: .text
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000002
- Content: '97000000E7800000'
- - Name: .rela.text
- Type: SHT_RELA
- Flags: [ SHF_INFO_LINK ]
- Link: .symtab
- AddressAlign: 0x0000000000000004
- Info: .text
- Relocations:
- - Offset: 0x0000000000000000
- Symbol: _start
- Type: R_RISCV_CALL
- Addend: 4
- - Name: .data
- Type: SHT_PROGBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
- Content: ''
- - Name: .bss
- Type: SHT_NOBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
- - Name: .reloc_neg
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000001
- Content: '97000000E7800000'
- - Name: .rela.reloc_neg
- Type: SHT_RELA
- Flags: [ SHF_INFO_LINK ]
- Link: .symtab
- AddressAlign: 0x0000000000000004
- Info: .reloc_neg
- Relocations:
- - Offset: 0x0000000000000000
- Symbol: L1
- Type: R_RISCV_CALL
- Addend: -4
-Symbols:
- - Name: .text
- Type: STT_SECTION
- Section: .text
- - Name: .data
- Type: STT_SECTION
- Section: .data
- - Name: .bss
- Type: STT_SECTION
- Section: .bss
- - Name: .reloc_neg
- Type: STT_SECTION
- Section: .reloc_neg
- - Name: L1
- Section: .reloc_neg
- - Name: _start
- Section: .text
- Binding: STB_GLOBAL
-...
diff --git a/lld/test/ELF/riscv-hi20-lo12.test b/lld/test/ELF/riscv-hi20-lo12.test
deleted file mode 100644
index 15d1e35d82ae..000000000000
--- a/lld/test/ELF/riscv-hi20-lo12.test
+++ /dev/null
@@ -1,85 +0,0 @@
-# .option norelax
-# .global _start
-#
-# .section .reloc_12345678, "ax", @progbits
-# _start:
-# foo = 0x12345678
-# lui a0, %hi(foo)
-# addi a0, a0, %lo(foo)
-# lw a0, %lo(foo)(a0)
-#
-# .section .reloc_fedcba98, "ax", @progbits
-# foo = 0xfedcba98
-# lui a0, %hi(foo)
-# addi a0, a0, %lo(foo)
-#
-# REQUIRES: riscv
-# RUN: yaml2obj %s -o %t.o
-# RUN: ld.lld %t.o -o %t
-# RUN: obj2yaml %t | FileCheck %s
-#
-# CHECK: - Name: .reloc_12345678
-# CHECK: Content: '375534121305856703258567'
-# 11000: 12345537 lui a0,0x12345
-# 11004: 67850513 addi a0,a0,1656 # 12345678 <__global_pointer$+0x12332e78>
-# 11008: 67852503 lw a0,1656(a0)
-#
-# CHECK: - Name: .reloc_fedcba98
-# CHECK: Content: 37C5DCFE130585A9
-# 1100c: fedcc537 lui a0,0xfedcc
-# 11010: a9850513 addi a0,a0,-1384 # fedcba98 <__global_pointer$+0xfedb9298>
-
---- !ELF
-FileHeader:
- Class: ELFCLASS32
- Data: ELFDATA2LSB
- Type: ET_REL
- Machine: EM_RISCV
- Flags: [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
-Sections:
- - Name: .text
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000002
- Content: ''
- - Name: .data
- Type: SHT_PROGBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
- Content: ''
- - Name: .bss
- Type: SHT_NOBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
- - Name: .reloc_12345678
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000001
- Content: '375534121305856703258567'
- - Name: .reloc_fedcba98
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000001
- Content: 37C5DCFE130585A9
-Symbols:
- - Name: .text
- Type: STT_SECTION
- Section: .text
- - Name: .data
- Type: STT_SECTION
- Section: .data
- - Name: .bss
- Type: STT_SECTION
- Section: .bss
- - Name: .reloc_12345678
- Type: STT_SECTION
- Section: .reloc_12345678
- - Name: foo
- Value: 0x00000000FEDCBA98
- - Name: .reloc_fedcba98
- Type: STT_SECTION
- Section: .reloc_fedcba98
- - Name: _start
- Section: .reloc_12345678
- Binding: STB_GLOBAL
-...
diff --git a/lld/test/ELF/riscv-jal-error.test b/lld/test/ELF/riscv-jal-error.test
deleted file mode 100644
index 113efa5c5f15..000000000000
--- a/lld/test/ELF/riscv-jal-error.test
+++ /dev/null
@@ -1,92 +0,0 @@
-# .option norelax
-# .global _start
-#
-# _start:
-# L1:
-# jal x0, L1 + 0x100000
-# L2:
-# jal x0, L2 - 0x100002
-# L3:
-# jal x0, L3 + 1
-# L4:
-# c.jal L4 + 1
-#
-# REQUIRES: riscv
-# RUN: yaml2obj %s -o %t.o
-# RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
-#
-# CHECK: {{.*}}(.text+0x0): relocation R_RISCV_JAL out of range
-# CHECK: {{.*}}(.text+0x4): relocation R_RISCV_JAL out of range
-# CHECK: {{.*}}(.text+0x8): improper alignment for relocation R_RISCV_JAL
-# CHECK: {{.*}}(.text+0xC): improper alignment for relocation R_RISCV_RVC_JUMP
-
---- !ELF
-FileHeader:
- Class: ELFCLASS32
- Data: ELFDATA2LSB
- Type: ET_REL
- Machine: EM_RISCV
- Flags: [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
-Sections:
- - Name: .text
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000002
- Content: 6F0000806FF0FF7F6F0000000120
- - Name: .rela.text
- Type: SHT_RELA
- Flags: [ SHF_INFO_LINK ]
- Link: .symtab
- AddressAlign: 0x0000000000000004
- Info: .text
- Relocations:
- - Offset: 0x0000000000000000
- Symbol: L1
- Type: R_RISCV_JAL
- Addend: 1048576
- - Offset: 0x0000000000000004
- Symbol: L2
- Type: R_RISCV_JAL
- Addend: -1048578
- - Offset: 0x0000000000000008
- Symbol: L3
- Type: R_RISCV_JAL
- Addend: 1
- - Offset: 0x000000000000000C
- Symbol: L4
- Type: R_RISCV_RVC_JUMP
- Addend: 1
- - Name: .data
- Type: SHT_PROGBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
- Content: ''
- - Name: .bss
- Type: SHT_NOBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
-Symbols:
- - Name: .text
- Type: STT_SECTION
- Section: .text
- - Name: .data
- Type: STT_SECTION
- Section: .data
- - Name: .bss
- Type: STT_SECTION
- Section: .bss
- - Name: L1
- Section: .text
- - Name: L2
- Section: .text
- Value: 0x0000000000000004
- - Name: L3
- Section: .text
- Value: 0x0000000000000008
- - Name: L4
- Section: .text
- Value: 0x000000000000000C
- - Name: _start
- Section: .text
- Binding: STB_GLOBAL
-...
diff --git a/lld/test/ELF/riscv-jal.test b/lld/test/ELF/riscv-jal.test
deleted file mode 100644
index 54412694ea65..000000000000
--- a/lld/test/ELF/riscv-jal.test
+++ /dev/null
@@ -1,160 +0,0 @@
-# .option norelax
-# .global _start
-#
-# .section .reloc_zero, "ax", @progbits
-# _start:
-# L1:
-# jal x0, L1
-# L2:
-# c.jal L2
-#
-# .section .reloc_max, "ax", @progbits
-# L3:
-# jal x0, L3 + 0xffffe
-# L4:
-# c.jal L4 + 0x7fe
-#
-# .section .reloc_min, "ax", @progbits
-# L5:
-# jal x0, L5 - 0x100000
-# L6:
-# c.jal L6 - 0x800
-#
-# REQUIRES: riscv
-# RUN: yaml2obj %s -o %t.o
-# RUN: ld.lld %t.o -o %t
-# RUN: obj2yaml %t | FileCheck %s
-#
-# CHECK: - Name: .reloc_zero
-# CHECK: Content: 6F0000000120
-# 11000: 0000006f j 11000
-# 11004: 2001 jal 11004
-#
-# CHECK: - Name: .reloc_max
-# CHECK: Content: 6FF0FF7FFD2F
-# 11006: 7ffff06f j 111004
-# 1100a: 2ffd jal 11808
-#
-# CHECK: - Name: .reloc_min
-# CHECK: Content: 6F0000800130
-# 1100c: 8000006f j fff1100c
-# 11010: 3001 jal 10810
-
---- !ELF
-FileHeader:
- Class: ELFCLASS32
- Data: ELFDATA2LSB
- Type: ET_REL
- Machine: EM_RISCV
- Flags: [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
-Sections:
- - Name: .text
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000002
- Content: ''
- - Name: .data
- Type: SHT_PROGBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
- Content: ''
- - Name: .bss
- Type: SHT_NOBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
- - Name: .reloc_zero
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000001
- Content: 6F0000000120
- - Name: .rela.reloc_zero
- Type: SHT_RELA
- Flags: [ SHF_INFO_LINK ]
- Link: .symtab
- AddressAlign: 0x0000000000000004
- Info: .reloc_zero
- Relocations:
- - Offset: 0x0000000000000000
- Symbol: L1
- Type: R_RISCV_JAL
- - Offset: 0x0000000000000004
- Symbol: L2
- Type: R_RISCV_RVC_JUMP
- - Name: .reloc_max
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000001
- Content: 6FF0FF7FFD2F
- - Name: .rela.reloc_max
- Type: SHT_RELA
- Flags: [ SHF_INFO_LINK ]
- Link: .symtab
- AddressAlign: 0x0000000000000004
- Info: .reloc_max
- Relocations:
- - Offset: 0x0000000000000000
- Symbol: L3
- Type: R_RISCV_JAL
- Addend: 1048574
- - Offset: 0x0000000000000004
- Symbol: L4
- Type: R_RISCV_RVC_JUMP
- Addend: 2046
- - Name: .reloc_min
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000001
- Content: 6F0000800130
- - Name: .rela.reloc_min
- Type: SHT_RELA
- Flags: [ SHF_INFO_LINK ]
- Link: .symtab
- AddressAlign: 0x0000000000000004
- Info: .reloc_min
- Relocations:
- - Offset: 0x0000000000000000
- Symbol: L5
- Type: R_RISCV_JAL
- Addend: -1048576
- - Offset: 0x0000000000000004
- Symbol: L6
- Type: R_RISCV_RVC_JUMP
- Addend: -2048
-Symbols:
- - Name: .text
- Type: STT_SECTION
- Section: .text
- - Name: .data
- Type: STT_SECTION
- Section: .data
- - Name: .bss
- Type: STT_SECTION
- Section: .bss
- - Name: .reloc_zero
- Type: STT_SECTION
- Section: .reloc_zero
- - Name: L1
- Section: .reloc_zero
- - Name: L2
- Section: .reloc_zero
- Value: 0x0000000000000004
- - Name: .reloc_max
- Type: STT_SECTION
- Section: .reloc_max
- - Name: L3
- Section: .reloc_max
- - Name: L4
- Section: .reloc_max
- Value: 0x0000000000000004
- - Name: .reloc_min
- Type: STT_SECTION
- Section: .reloc_min
- - Name: L5
- Section: .reloc_min
- - Name: L6
- Section: .reloc_min
- Value: 0x0000000000000004
- - Name: _start
- Section: .reloc_zero
- Binding: STB_GLOBAL
-...
diff --git a/lld/test/ELF/riscv-pcrel-hilo.test b/lld/test/ELF/riscv-pcrel-hilo.test
deleted file mode 100644
index 2cfb711aac00..000000000000
--- a/lld/test/ELF/riscv-pcrel-hilo.test
+++ /dev/null
@@ -1,102 +0,0 @@
-# .option norelax
-# .global _start
-#
-# _start:
-# auipc a0, %pcrel_hi(_start + 4)
-# addi a0, a0, %pcrel_lo(_start)
-#
-# .section .reloc_neg, "ax", @progbits
-# L1:
-# auipc a0, %pcrel_hi(L1 - 2)
-# addi a0, a0, %pcrel_lo(L1)
-#
-#
-# REQUIRES: riscv
-# RUN: yaml2obj %s -o %t.o
-# RUN: ld.lld %t.o -o %t
-# RUN: obj2yaml %t | FileCheck %s
-#
-# CHECK: - Name: .text
-# CHECK: Content: '1705000013054500'
-# 11000: 00000517 auipc a0,0x0
-# 11004: 00450513 addi a0,a0,4 # 11004 <_start+0x4>
-#
-# CHECK: - Name: .reloc_neg
-# CHECK: Content: 170500001305E5FF
-# 11008: 00000517 auipc a0,0x0
-# 1100c: ffe50513 addi a0,a0,-2 # 11006 <_start+0x6>
-
---- !ELF
-FileHeader:
- Class: ELFCLASS32
- Data: ELFDATA2LSB
- Type: ET_REL
- Machine: EM_RISCV
- Flags: [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
-Sections:
- - Name: .text
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000002
- Content: '1705000013050500'
- - Name: .rela.text
- Type: SHT_RELA
- Flags: [ SHF_INFO_LINK ]
- Link: .symtab
- AddressAlign: 0x0000000000000004
- Info: .text
- Relocations:
- - Offset: 0x0000000000000000
- Symbol: _start
- Type: R_RISCV_PCREL_HI20
- Addend: 4
- - Offset: 0x0000000000000004
- Symbol: _start
- Type: R_RISCV_PCREL_LO12_I
- - Name: .data
- Type: SHT_PROGBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
- Content: ''
- - Name: .bss
- Type: SHT_NOBITS
- Flags: [ SHF_WRITE, SHF_ALLOC ]
- AddressAlign: 0x0000000000000001
- - Name: .reloc_neg
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000001
- Content: '1705000013050500'
- - Name: .rela.reloc_neg
- Type: SHT_RELA
- Flags: [ SHF_INFO_LINK ]
- Link: .symtab
- AddressAlign: 0x0000000000000004
- Info: .reloc_neg
- Relocations:
- - Offset: 0x0000000000000000
- Symbol: L1
- Type: R_RISCV_PCREL_HI20
- Addend: -2
- - Offset: 0x0000000000000004
- Symbol: L1
- Type: R_RISCV_PCREL_LO12_I
-Symbols:
- - Name: .text
- Type: STT_SECTION
- Section: .text
- - Name: .data
- Type: STT_SECTION
- Section: .data
- - Name: .bss
- Type: STT_SECTION
- Section: .bss
- - Name: .reloc_neg
- Type: STT_SECTION
- Section: .reloc_neg
- - Name: L1
- Section: .reloc_neg
- - Name: _start
- Section: .text
- Binding: STB_GLOBAL
-...
diff --git a/test/ELF/riscv-branch.s b/test/ELF/riscv-branch.s
new file mode 100644
index 000000000000..247b6f3c2240
--- /dev/null
+++ b/test/ELF/riscv-branch.s
@@ -0,0 +1,32 @@
+# REQUIRES: riscv
+
+# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=-relax %s -o %t.rv32.o
+# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=-relax %s -o %t.rv64.o
+
+# RUN: ld.lld %t.rv32.o --defsym foo=_start+4 --defsym bar=_start -o %t.rv32
+# RUN: ld.lld %t.rv64.o --defsym foo=_start+4 --defsym bar=_start -o %t.rv64
+# RUN: llvm-objdump -d %t.rv32 | FileCheck %s
+# RUN: llvm-objdump -d %t.rv64 | FileCheck %s
+# CHECK: 63 02 00 00 beqz zero, 4
+# CHECK: e3 1e 00 fe bnez zero, -4
+#
+# RUN: ld.lld %t.rv32.o --defsym foo=_start+0xffe --defsym bar=_start+4-0x1000 -o %t.rv32.limits
+# RUN: ld.lld %t.rv64.o --defsym foo=_start+0xffe --defsym bar=_start+4-0x1000 -o %t.rv64.limits
+# RUN: llvm-objdump -d %t.rv32.limits | FileCheck --check-prefix=LIMITS %s
+# RUN: llvm-objdump -d %t.rv64.limits | FileCheck --check-prefix=LIMITS %s
+# LIMITS: e3 0f 00 7e beqz zero, 4094
+# LIMITS-NEXT: 63 10 00 80 bnez zero, -4096
+
+# RUN: not ld.lld %t.rv32.o --defsym foo=_start+0x1000 --defsym bar=_start+4-0x1002 -o %t 2>&1 | FileCheck --check-prefix=ERROR-RANGE %s
+# RUN: not ld.lld %t.rv64.o --defsym foo=_start+0x1000 --defsym bar=_start+4-0x1002 -o %t 2>&1 | FileCheck --check-prefix=ERROR-RANGE %s
+# ERROR-RANGE: relocation R_RISCV_BRANCH out of range: 2048 is not in [-2048, 2047]
+# ERROR-RANGE-NEXT: relocation R_RISCV_BRANCH out of range: -2049 is not in [-2048, 2047]
+
+# RUN: not ld.lld %t.rv32.o --defsym foo=_start+1 --defsym bar=_start-1 -o %t 2>&1 | FileCheck --check-prefix=ERROR-ALIGN %s
+# RUN: not ld.lld %t.rv64.o --defsym foo=_start+1 --defsym bar=_start-1 -o %t 2>&1 | FileCheck --check-prefix=ERROR-ALIGN %s
+# ERROR-ALIGN: improper alignment for relocation R_RISCV_BRANCH: 0x1 is not aligned to 2 bytes
+
+.global _start
+_start:
+ beq x0, x0, foo
+ bne x0, x0, bar
diff --git a/test/ELF/riscv-call.s b/test/ELF/riscv-call.s
new file mode 100644
index 000000000000..d5b9b370e807
--- /dev/null
+++ b/test/ELF/riscv-call.s
@@ -0,0 +1,32 @@
+# REQUIRES: riscv
+
+# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=-relax %s -o %t.rv32.o
+# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=-relax %s -o %t.rv64.o
+
+# RUN: ld.lld %t.rv32.o --defsym foo=_start+8 --defsym bar=_start -o %t.rv32
+# RUN: ld.lld %t.rv64.o --defsym foo=_start+8 --defsym bar=_start -o %t.rv64
+# RUN: llvm-objdump -d %t.rv32 | FileCheck %s
+# RUN: llvm-objdump -d %t.rv64 | FileCheck %s
+# CHECK: 97 00 00 00 auipc ra, 0
+# CHECK-NEXT: e7 80 80 00 jalr ra, ra, 8
+# CHECK: 97 00 00 00 auipc ra, 0
+# CHECK-NEXT: e7 80 80 ff jalr ra, ra, -8
+
+# RUN: ld.lld %t.rv32.o --defsym foo=_start+0x7ffff7ff --defsym bar=_start+8-0x80000800 -o %t.rv32.limits
+# RUN: ld.lld %t.rv64.o --defsym foo=_start+0x7ffff7ff --defsym bar=_start+8-0x80000800 -o %t.rv64.limits
+# RUN: llvm-objdump -d %t.rv32.limits | FileCheck --check-prefix=LIMITS %s
+# RUN: llvm-objdump -d %t.rv64.limits | FileCheck --check-prefix=LIMITS %s
+# LIMITS: 97 f0 ff 7f auipc ra, 524287
+# LIMITS-NEXT: e7 80 f0 7f jalr ra, ra, 2047
+# LIMITS-NEXT: 97 00 00 80 auipc ra, 524288
+# LIMITS-NEXT: e7 80 00 80 jalr ra, ra, -2048
+
+# RUN: ld.lld %t.rv32.o --defsym foo=_start+0x7ffff800 --defsym bar=_start+8-0x80000801 -o %t
+# RUN: not ld.lld %t.rv64.o --defsym foo=_start+0x7ffff800 --defsym bar=_start+8-0x80000801 -o %t 2>&1 | FileCheck --check-prefix=ERROR %s
+# ERROR: relocation R_RISCV_CALL out of range: 524288 is not in [-524288, 524287]
+# ERROR-NEXT: relocation R_RISCV_CALL out of range: -524289 is not in [-524288, 524287]
+
+.global _start
+_start:
+ call foo
+ call bar
diff --git a/test/ELF/riscv-hi20-lo12.s b/test/ELF/riscv-hi20-lo12.s
new file mode 100644
index 000000000000..24d2d65027d6
--- /dev/null
+++ b/test/ELF/riscv-hi20-lo12.s
@@ -0,0 +1,40 @@
+# REQUIRES: riscv
+
+# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=-relax %s -o %t.rv32.o
+# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=-relax %s -o %t.rv64.o
+
+# RUN: ld.lld %t.rv32.o --defsym foo=0 --defsym bar=42 -o %t.rv32
+# RUN: ld.lld %t.rv64.o --defsym foo=0 --defsym bar=42 -o %t.rv64
+# RUN: llvm-objdump -d %t.rv32 | FileCheck %s
+# RUN: llvm-objdump -d %t.rv64 | FileCheck %s
+# CHECK: 37 05 00 00 lui a0, 0
+# CHECK-NEXT: 13 05 05 00 mv a0, a0
+# CHECK-NEXT: 23 20 a5 00 sw a0, 0(a0)
+# CHECK-NEXT: b7 05 00 00 lui a1, 0
+# CHECK-NEXT: 93 85 a5 02 addi a1, a1, 42
+# CHECK-NEXT: 23 a5 b5 02 sw a1, 42(a1)
+
+# RUN: ld.lld %t.rv32.o --defsym foo=0x7ffff7ff --defsym bar=0x7ffff800 -o %t.rv32.limits
+# RUN: ld.lld %t.rv64.o --defsym foo=0x7ffff7ff --defsym bar=0xffffffff7ffff800 -o %t.rv64.limits
+# RUN: llvm-objdump -d %t.rv32.limits | FileCheck --check-prefix=LIMITS %s
+# RUN: llvm-objdump -d %t.rv64.limits | FileCheck --check-prefix=LIMITS %s
+# LIMITS: 37 f5 ff 7f lui a0, 524287
+# LIMITS-NEXT: 13 05 f5 7f addi a0, a0, 2047
+# LIMITS-NEXT: a3 2f a5 7e sw a0, 2047(a0)
+# LIMITS-NEXT: b7 05 00 80 lui a1, 524288
+# LIMITS-NEXT: 93 85 05 80 addi a1, a1, -2048
+# LIMITS-NEXT: 23 a0 b5 80 sw a1, -2048(a1)
+
+# RUN: not ld.lld %t.rv64.o --defsym foo=0x7ffff800 --defsym bar=0xffffffff7ffff7ff -o %t 2>&1 | FileCheck --check-prefix ERROR %s
+# ERROR: relocation R_RISCV_HI20 out of range: 524288 is not in [-524288, 524287]
+# ERROR-NEXT: relocation R_RISCV_HI20 out of range: -524289 is not in [-524288, 524287]
+
+.global _start
+
+_start:
+ lui a0, %hi(foo)
+ addi a0, a0, %lo(foo)
+ sw a0, %lo(foo)(a0)
+ lui a1, %hi(bar)
+ addi a1, a1, %lo(bar)
+ sw a1, %lo(bar)(a1)
diff --git a/test/ELF/riscv-jal.s b/test/ELF/riscv-jal.s
new file mode 100644
index 000000000000..5979a5e1110f
--- /dev/null
+++ b/test/ELF/riscv-jal.s
@@ -0,0 +1,34 @@
+# REQUIRES: riscv
+
+# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=-relax %s -o %t.rv32.o
+# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=-relax %s -o %t.rv64.o
+
+# RUN: ld.lld %t.rv32.o --defsym foo=_start+4 --defsym bar=_start -o %t.rv32
+# RUN: ld.lld %t.rv64.o --defsym foo=_start+4 --defsym bar=_start -o %t.rv64
+# RUN: llvm-objdump -d %t.rv32 | FileCheck %s
+# RUN: llvm-objdump -d %t.rv64 | FileCheck %s
+# CHECK: 6f 00 40 00 j 4
+# CHECK: ef f0 df ff jal -4
+
+# RUN: ld.lld %t.rv32.o --defsym foo=_start+0xffffe --defsym bar=_start+4-0x100000 -o %t.rv32.limits
+# RUN: ld.lld %t.rv64.o --defsym foo=_start+0xffffe --defsym bar=_start+4-0x100000 -o %t.rv64.limits
+# RUN: llvm-objdump -d %t.rv32.limits | FileCheck --check-prefix=LIMITS %s
+# RUN: llvm-objdump -d %t.rv64.limits | FileCheck --check-prefix=LIMITS %s
+# LIMITS: 6f f0 ff 7f j 1048574
+# LIMITS-NEXT: ef 00 00 80 jal -1048576
+
+# RUN: not ld.lld %t.rv32.o --defsym foo=_start+0x100000 --defsym bar=_start+4-0x100002 -o %t 2>&1 | FileCheck --check-prefix=ERROR-RANGE %s
+# RUN: not ld.lld %t.rv64.o --defsym foo=_start+0x100000 --defsym bar=_start+4-0x100002 -o %t 2>&1 | FileCheck --check-prefix=ERROR-RANGE %s
+# ERROR-RANGE: relocation R_RISCV_JAL out of range: 524288 is not in [-524288, 524287]
+# ERROR-RANGE-NEXT: relocation R_RISCV_JAL out of range: -524289 is not in [-524288, 524287]
+
+# RUN: not ld.lld %t.rv32.o --defsym foo=_start+1 --defsym bar=_start+4+3 -o %t 2>&1 | FileCheck --check-prefix=ERROR-ALIGN %s
+# RUN: not ld.lld %t.rv64.o --defsym foo=_start+1 --defsym bar=_start+4+3 -o %t 2>&1 | FileCheck --check-prefix=ERROR-ALIGN %s
+# ERROR-ALIGN: improper alignment for relocation R_RISCV_JAL: 0x1 is not aligned to 2 bytes
+# ERROR-ALIGN-NEXT: improper alignment for relocation R_RISCV_JAL: 0x3 is not aligned to 2 bytes
+
+.global _start
+
+_start:
+ jal x0, foo
+ jal x1, bar
diff --git a/test/ELF/riscv-pcrel-hilo.s b/test/ELF/riscv-pcrel-hilo.s
new file mode 100644
index 000000000000..324e6ceadd14
--- /dev/null
+++ b/test/ELF/riscv-pcrel-hilo.s
@@ -0,0 +1,41 @@
+# REQUIRES: riscv
+
+# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=-relax %s -o %t.rv32.o
+# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=-relax %s -o %t.rv64.o
+
+# RUN: ld.lld %t.rv32.o --defsym foo=_start+12 --defsym bar=_start -o %t.rv32
+# RUN: ld.lld %t.rv64.o --defsym foo=_start+12 --defsym bar=_start -o %t.rv64
+# RUN: llvm-objdump -d %t.rv32 | FileCheck %s
+# RUN: llvm-objdump -d %t.rv64 | FileCheck %s
+# CHECK: 17 05 00 00 auipc a0, 0
+# CHECK-NEXT: 13 05 c5 00 addi a0, a0, 12
+# CHECK-NEXT: 23 26 05 00 sw zero, 12(a0)
+# CHECK: 17 05 00 00 auipc a0, 0
+# CHECK-NEXT: 13 05 45 ff addi a0, a0, -12
+# CHECK-NEXT: 23 2a 05 fe sw zero, -12(a0)
+
+# RUN: ld.lld %t.rv32.o --defsym foo=_start+0x7ffff7ff --defsym bar=_start+12-0x80000800 -o %t.rv32.limits
+# RUN: ld.lld %t.rv64.o --defsym foo=_start+0x7ffff7ff --defsym bar=_start+12-0x80000800 -o %t.rv64.limits
+# RUN: llvm-objdump -d %t.rv32.limits | FileCheck --check-prefix=LIMITS %s
+# RUN: llvm-objdump -d %t.rv64.limits | FileCheck --check-prefix=LIMITS %s
+# LIMITS: 17 f5 ff 7f auipc a0, 524287
+# LIMITS-NEXT: 13 05 f5 7f addi a0, a0, 2047
+# LIMITS-NEXT: a3 2f 05 7e sw zero, 2047(a0)
+# LIMITS: 17 05 00 80 auipc a0, 524288
+# LIMITS-NEXT: 13 05 05 80 addi a0, a0, -2048
+# LIMITS-NEXT: 23 20 05 80 sw zero, -2048(a0)
+
+# RUN: ld.lld %t.rv32.o --defsym foo=_start+0x7ffff800 --defsym bar=_start+12-0x80000801 -o %t
+# RUN: not ld.lld %t.rv64.o --defsym foo=_start+0x7ffff800 --defsym bar=_start+12-0x80000801 -o %t 2>&1 | FileCheck --check-prefix=ERROR %s
+# ERROR: relocation R_RISCV_PCREL_HI20 out of range: 524288 is not in [-524288, 524287]
+# ERROR-NEXT: relocation R_RISCV_PCREL_HI20 out of range: -524289 is not in [-524288, 524287]
+
+.global _start
+_start:
+ auipc a0, %pcrel_hi(foo)
+ addi a0, a0, %pcrel_lo(_start)
+ sw x0, %pcrel_lo(_start)(a0)
+.L1:
+ auipc a0, %pcrel_hi(bar)
+ addi a0, a0, %pcrel_lo(.L1)
+ sw x0, %pcrel_lo(.L1)(a0)