(
section_bytes: &mut [u8],
site: usize,
site_abs: u64,
target_abs: u64,
addend: i64,
)
| 445 | |
| 446 | fn 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 | |
| 460 | fn 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 | |
| 472 | fn 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) |
no test coverage detected