MCPcopy Create free account
hub / github.com/Vector35/binaryninja-api / apply_relocation

Method apply_relocation

arch/riscv/src/lib.rs:2400–2648  ·  view source on GitHub ↗
(
        &self,
        bv: &BinaryView,
        arch: &CoreArchitecture,
        reloc: &Relocation,
        dest: &mut [u8],
    )

Source from the content-addressed store, hash-verified

2398 }
2399
2400 fn apply_relocation(
2401 &self,
2402 bv: &BinaryView,
2403 arch: &CoreArchitecture,
2404 reloc: &Relocation,
2405 dest: &mut [u8],
2406 ) -> bool {
2407 let info = reloc.info();
2408 match info.native_type {
2409 Self::R_RISCV_BRANCH => {
2410 let opcode = u32::from_le_bytes(match dest[0..4].try_into() {
2411 Ok(bytes) => bytes,
2412 Err(_) => return false,
2413 });
2414 let offset = reloc
2415 .target()
2416 .wrapping_add(info.addend as u64)
2417 .wrapping_sub(info.address) as u32;
2418 let opcode = Self::replace_b_imm(opcode, offset);
2419 dest[0..4].copy_from_slice(&opcode.to_le_bytes());
2420 true
2421 }
2422 Self::R_RISCV_JAL => {
2423 let opcode = u32::from_le_bytes(match dest[0..4].try_into() {
2424 Ok(bytes) => bytes,
2425 Err(_) => return false,
2426 });
2427 let offset = reloc
2428 .target()
2429 .wrapping_add(info.addend as u64)
2430 .wrapping_sub(info.address) as u32;
2431 let opcode = Self::replace_j_imm(opcode, offset);
2432 dest[0..4].copy_from_slice(&opcode.to_le_bytes());
2433 true
2434 }
2435 Self::R_RISCV_CALL | Self::R_RISCV_CALL_PLT => {
2436 let u_opcode = u32::from_le_bytes(match dest[0..4].try_into() {
2437 Ok(bytes) => bytes,
2438 Err(_) => return false,
2439 });
2440 let i_opcode = u32::from_le_bytes(match dest[4..8].try_into() {
2441 Ok(bytes) => bytes,
2442 Err(_) => return false,
2443 });
2444 let offset = reloc
2445 .target()
2446 .wrapping_add(info.addend as u64)
2447 .wrapping_sub(info.address) as u32;
2448 let high_offset = (offset.wrapping_add(0x800)) >> 12;
2449 let low_offset = offset & 0xfff;
2450 let u_opcode = Self::replace_u_imm(u_opcode, high_offset);
2451 let i_opcode = Self::replace_i_imm(i_opcode, low_offset);
2452 dest[0..4].copy_from_slice(&u_opcode.to_le_bytes());
2453 dest[4..8].copy_from_slice(&i_opcode.to_le_bytes());
2454 true
2455 }
2456 Self::R_RISCV_PCREL_HI20 => {
2457 let opcode = u32::from_le_bytes(match dest[0..4].try_into() {

Callers

nothing calls this directly

Calls 6

findMethod · 0.80
infoMethod · 0.45
targetMethod · 0.45
iterMethod · 0.45
relocations_atMethod · 0.45

Tested by

no test coverage detected