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

Function patch_la

crates/asm-to-binary/src/object_linker.rs:481–501  ·  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

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)
483 .wrapping_add(addend)
484 .wrapping_sub(site_abs as i64);
485 let (hi20, lo12) = pcrel_split(offset);
486
487 let auipc_patched = (auipc_word & 0x0000_0FFF) | (((hi20 << 12) as u32) & 0xFFFF_F000);
488 let jalr_patched = (jalr_word & !(0xFFF << 20)) | (((lo12 as u32) & 0xFFF) << 20);
489
490 write_u32_at(section_bytes, site, auipc_patched)?;
491 write_u32_at(section_bytes, site + 4, jalr_patched)?;
492 Ok(())
493}
494
495fn patch_jal(
496 section_bytes: &mut [u8],
497 site: usize,
498 site_abs: u64,
499 target_abs: u64,
500 addend: i64,
501) -> Result<(), LinkerError> {
502 let jal_word = read_u32_at(section_bytes, site)?;
503 let offset = (target_abs as i64)
504 .wrapping_add(addend)

Callers 1

linkMethod · 0.85

Calls 3

read_u32_atFunction · 0.85
write_u32_atFunction · 0.85
pcrel_splitFunction · 0.70

Tested by

no test coverage detected