Skip to content

Commit

Permalink
Add RISC-V relocation types
Browse files Browse the repository at this point in the history
Fixes #36.
  • Loading branch information
RyanGlScott committed Jul 28, 2024
1 parent 8afa838 commit 7e5b411
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 1 deletion.
1 change: 1 addition & 0 deletions elf-edit.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ library
Data.ElfEdit.Relocations.I386
Data.ElfEdit.Relocations.PPC32
Data.ElfEdit.Relocations.PPC64
Data.ElfEdit.Relocations.RISCV
Data.ElfEdit.Relocations.X86_64
Data.ElfEdit.Prim.Ehdr
Data.ElfEdit.Prim.File
Expand Down
3 changes: 3 additions & 0 deletions src/Data/ElfEdit/Prim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module Data.ElfEdit.Prim
, module Data.ElfEdit.Relocations.PPC32
-- ** PPC64 relocations
, module Data.ElfEdit.Relocations.PPC64
-- ** RISC-V relocations
, module Data.ElfEdit.Relocations.RISCV
-- ** Android-specific
, module Data.ElfEdit.Relocations.Android
) where
Expand All @@ -47,4 +49,5 @@ import Data.ElfEdit.Relocations.Common
import Data.ElfEdit.Relocations.I386
import Data.ElfEdit.Relocations.PPC32
import Data.ElfEdit.Relocations.PPC64
import Data.ElfEdit.Relocations.RISCV
import Data.ElfEdit.Relocations.X86_64
444 changes: 444 additions & 0 deletions src/Data/ElfEdit/Relocations/RISCV.hs

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all: simple.elf simple.static.elf libsymbol_versions.2.so fmax.elf ppc32-relocs.elf ppc64-relocs.elf
all: simple.elf simple.static.elf libsymbol_versions.2.so fmax.elf ppc32-relocs.elf ppc64-relocs.elf riscv32-relocs.elf riscv64-relocs.elf

simple.elf: Makefile simple.c
gcc simple.c -o simple.elf
Expand All @@ -17,3 +17,9 @@ ppc32-relocs.elf: ppc32-relocs.c

ppc64-relocs.elf: ppc64-relocs.c
powerpc64-linux-musl-gcc $< -o $@

riscv32-relocs.elf: riscv-relocs.c
riscv32-linux-musl-gcc $< -o $@

riscv64-relocs.elf: riscv-relocs.c
riscv64-linux-musl-gcc $< -o $@
34 changes: 34 additions & 0 deletions tests/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,40 @@ tests = T.testGroup "ELF Tests"
-- , (0x0000000000020028, Elf.R_PPC64_JMP_SLOT)
, (0x0000000000020030, Elf.R_PPC64_RELATIVE)
]
, T.testCase "RISC-V (32-bit) relocations" $
testRelocEntries
(Proxy @(Elf.RISCV_RelocationType 32))
"./tests/riscv32-relocs.elf"
[ (0x00001f18, Elf.R_RISCV_RELATIVE)
, (0x00001f1c, Elf.R_RISCV_RELATIVE)
, (0x00002000, Elf.R_RISCV_RELATIVE)
, (0x0000200c, Elf.R_RISCV_JUMP_SLOT)
, (0x00002014, Elf.R_RISCV_32)
, (0x00002018, Elf.R_RISCV_32)
, (0x0000201c, Elf.R_RISCV_32)
, (0x00002020, Elf.R_RISCV_32)
, (0x00002024, Elf.R_RISCV_32)
, (0x00002028, Elf.R_RISCV_RELATIVE)
, (0x0000202c, Elf.R_RISCV_32)
, (0x00002030, Elf.R_RISCV_32)
]
, T.testCase "RISC-V (64-bit) relocations" $
testRelocEntries
(Proxy @(Elf.RISCV_RelocationType 64))
"./tests/riscv64-relocs.elf"
[ (0x0000000000001e30, Elf.R_RISCV_RELATIVE)
, (0x0000000000001e38, Elf.R_RISCV_RELATIVE)
, (0x0000000000002000, Elf.R_RISCV_RELATIVE)
, (0x0000000000002018, Elf.R_RISCV_JUMP_SLOT)
, (0x0000000000002028, Elf.R_RISCV_64)
, (0x0000000000002030, Elf.R_RISCV_64)
, (0x0000000000002038, Elf.R_RISCV_64)
, (0x0000000000002040, Elf.R_RISCV_64)
, (0x0000000000002048, Elf.R_RISCV_64)
, (0x0000000000002050, Elf.R_RISCV_RELATIVE)
, (0x0000000000002058, Elf.R_RISCV_64)
, (0x0000000000002060, Elf.R_RISCV_64)
]
]
]

Expand Down
3 changes: 3 additions & 0 deletions tests/riscv-relocs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main(void) {
return 0;
}
Binary file added tests/riscv32-relocs.elf
Binary file not shown.
Binary file added tests/riscv64-relocs.elf
Binary file not shown.

0 comments on commit 7e5b411

Please sign in to comment.