Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(llvm): Fix ADD_ABS_LO12_NC relocation #5230

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/compiler/src/engine/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,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/compiler/src/types/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,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 @@ -161,7 +171,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 @@ -197,10 +206,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
Loading