MCPcopy Create free account
hub / github.com/LPC4/Full-Stack / patch_jal

Function patch_jal

crates/asm-to-binary/src/object_linker.rs:447–479  ·  view source on GitHub ↗
(
    section_bytes: &mut [u8],
    site: usize,
    site_abs: u64,
    target_abs: u64,
    addend: i64,
)

Source from the content-addressed store, hash-verified

445
446fn read_u32_at(buf: &[u8], off: usize) -> Result<u32, LinkerError> {
447 let end = off + 4;
448 let Some(bytes) = buf.get(off..end) else {
449 return Err(LinkerError::new(format!(
450 "relocation read at offset {off} exceeds section size {}",
451 buf.len()
452 )));
453 };
454 let arr: [u8; 4] = bytes
455 .try_into()
456 .map_err(|_| LinkerError::new("invalid word slice while reading relocation site"))?;
457 Ok(u32::from_le_bytes(arr))
458}
459
460fn write_u32_at(buf: &mut [u8], off: usize, word: u32) -> Result<(), LinkerError> {
461 let end = off + 4;
462 let Some(dst) = buf.get_mut(off..end) else {
463 return Err(LinkerError::new(format!(
464 "relocation write at offset {off} exceeds section size {}",
465 buf.len()
466 )));
467 };
468 dst.copy_from_slice(&word.to_le_bytes());
469 Ok(())
470}
471
472fn patch_call_pair(
473 section_bytes: &mut [u8],
474 site: usize,
475 site_abs: u64,
476 target_abs: u64,
477 addend: i64,
478) -> Result<(), LinkerError> {
479 let auipc_word = read_u32_at(section_bytes, site)?;
480 let jalr_word = read_u32_at(section_bytes, site + 4)?;
481
482 let offset = (target_abs as i64)

Callers 1

linkMethod · 0.85

Calls 3

read_u32_atFunction · 0.85
write_u32_atFunction · 0.85
containsMethod · 0.45

Tested by

no test coverage detected