Skip to content

Commit

Permalink
Fix RISC-V trampoline code
Browse files Browse the repository at this point in the history
  • Loading branch information
tnayuki committed Feb 15, 2022
1 parent 07831af commit b80b612
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/engine-dylib/src/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ const AARCH64_TRAMPOLINE: [u8; 12] = [
const X86_64_TRAMPOLINE: [u8; 6] = [0xff, 0x25, 0x00, 0x00, 0x00, 0x00];

// AUIPC t1, #... 17 03 00 00
// JALR x0, #...(t1) 67 00 03 00
const RISCV64_TRAMPOLINE: [u8; 8] = [0x17, 0x03, 0x00, 0x00, 0x67, 0x00, 0x03, 0x00];
// LD t1, #...(t1) 03 33 03 00
// JR t1 67 00 03 00
const RISCV64_TRAMPOLINE: [u8; 12] = [0x17, 0x03, 0x00, 0x00, 0x03, 0x33, 0x03, 0x00, 0x67, 0x00, 0x03, 0x00];

fn emit_trampoline(
obj: &mut Object,
Expand Down Expand Up @@ -120,9 +121,11 @@ fn emit_trampoline(
.unwrap();
}
Architecture::Riscv64(_) => {
let reloc = match obj.format() {
BinaryFormat::Elf => RelocationKind::Elf(elf::R_RISCV_CALL_PLT),

let (reloc1, reloc2) = match obj.format() {
BinaryFormat::Elf => (
RelocationKind::Elf(elf::R_RISCV_PCREL_HI20),
RelocationKind::Elf(elf::R_RISCV_PCREL_LO12_I),
),
_ => panic!("Unsupported binary format on Riscv64"),
};
let offset = obj.add_symbol_data(libcall_symbol, text, &RISCV64_TRAMPOLINE, 4);
Expand All @@ -131,13 +134,25 @@ fn emit_trampoline(
Relocation {
offset: offset,
size: 32,
kind: reloc,
kind: reloc1,
encoding: RelocationEncoding::Generic,
symbol: trampoline_table_symbols[libcall as usize],
addend: 0,
},
)
.unwrap();
obj.add_relocation(
text,
Relocation {
offset: offset+4,
size: 32,
kind: reloc2,
encoding: RelocationEncoding::Generic,
symbol: libcall_symbol,
addend: 0,
},
)
.unwrap();
}
arch => panic!("Unsupported architecture: {}", arch),
};
Expand Down

0 comments on commit b80b612

Please sign in to comment.