Skip to content

Commit

Permalink
fix(llvm): Fix ADD_ABS_LO12_NC relocation
Browse files Browse the repository at this point in the history
  • Loading branch information
xdoardo committed Nov 7, 2024
1 parent c9035d3 commit ce94b8f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
13 changes: 9 additions & 4 deletions lib/compiler/src/engine/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,15 @@ fn apply_relocation(
write_unaligned(reloc_address as *mut u32, op);
},
RelocationKind::Aarch64AddAbsLo12Nc => unsafe {
let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64);
let reloc_delta = (reloc_delta as u32 & 0xfff)
| (read_unaligned(reloc_address as *mut u32) & 0xFFC003FF);
write_unaligned(reloc_address as *mut u32, reloc_delta);
let (reloc_address, delta) = r.for_address(body, target_func_address as u64);

let delta = delta as isize;
let op = read_unaligned(reloc_address as *mut u32);
let imm = ((delta as u32) & 0xfff) << 10;
let mask = !((0xfff) << 10);
let op = (op & mask) | imm;

write_unaligned(reloc_address as *mut u32, op);
},
RelocationKind::Aarch64Ldst128AbsLo12Nc => unsafe {
let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64);
Expand Down
29 changes: 26 additions & 3 deletions lib/types/src/compilation/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ pub trait RelocationLike {
/// to that address.
///
/// The function returns the relocation address and the delta.
///
// # Nomenclature (from [1]@5.7.3.3)
//
// * S (when used on its own) is the address of the symbol.
// * A is the addend for the relocation.
// * P is the address of the place being relocated (derived from r_offset).
// * X is the result of a relocation operation, before any masking or bit-selection operation is applied
// * Page(expr) is the page address of the expression expr, defined as (expr & ~0xFFF). (This applies even if the machine page size supported by the platform has a different value.)
//
// [1]: https://github.com/ARM-software/abi-aa/blob/main/aaelf64/aaelf64.rst
fn for_address(&self, start: usize, target_func_address: u64) -> (usize, u64) {
match self.kind() {
RelocationKind::Abs8
Expand All @@ -164,7 +174,6 @@ pub trait RelocationLike {
| RelocationKind::Arm64Movw2
| RelocationKind::Arm64Movw3
| RelocationKind::RiscvPCRelLo12I
| RelocationKind::Aarch64AddAbsLo12Nc
| RelocationKind::Aarch64Ldst128AbsLo12Nc
| RelocationKind::Aarch64Ldst64AbsLo12Nc => {
let reloc_address = start + self.offset() as usize;
Expand Down Expand Up @@ -200,10 +209,24 @@ pub trait RelocationLike {
.wrapping_add(reloc_addend as u32);
(reloc_address, reloc_delta_u32 as u64)
}
RelocationKind::Aarch64AdrPrelLo21 => {
let s = target_func_address;
let p = start + self.offset() as usize;
let a = self.addend() as u64;

(p, s.wrapping_add(a).wrapping_sub(p as u64))
}

RelocationKind::Aarch64AddAbsLo12Nc => {
let s = target_func_address;
let p = start + self.offset() as usize;
let a = self.addend() as u64;

(p, s.wrapping_add(a))
}
RelocationKind::Arm64Call
| RelocationKind::RiscvCall
| RelocationKind::RiscvPCRelHi20
| RelocationKind::Aarch64AdrPrelLo21 => {
| RelocationKind::RiscvPCRelHi20 => {
let reloc_address = start + self.offset() as usize;
let reloc_addend = self.addend() as isize;
let reloc_delta_u32 = target_func_address
Expand Down

0 comments on commit ce94b8f

Please sign in to comment.