use super::{branch26, insn_cell, of, page};
use crate::{
error::{Error, Result},
reloc::{
Arch, Check, Field, Needs, RelExpr, Resolver, Spec, Write, WriteKind,
pc_value,
},
symbol::SymbolId,
};
pub const R_AARCH64_NONE: u32 = 0x000;
pub const R_AARCH64_ABS64: u32 = 0x101;
pub const R_AARCH64_ABS32: u32 = 0x102;
pub const R_AARCH64_ABS16: u32 = 0x103;
pub const R_AARCH64_PREL64: u32 = 0x104;
pub const R_AARCH64_PREL32: u32 = 0x105;
pub const R_AARCH64_PREL16: u32 = 0x106;
pub const R_AARCH64_MOVW_UABS_G0: u32 = 0x107;
pub const R_AARCH64_MOVW_UABS_G0_NC: u32 = 0x108;
pub const R_AARCH64_MOVW_UABS_G1: u32 = 0x109;
pub const R_AARCH64_MOVW_UABS_G1_NC: u32 = 0x10a;
pub const R_AARCH64_MOVW_UABS_G2: u32 = 0x10b;
pub const R_AARCH64_MOVW_UABS_G2_NC: u32 = 0x10c;
pub const R_AARCH64_MOVW_UABS_G3: u32 = 0x10d;
pub const R_AARCH64_MOVW_SABS_G0: u32 = 0x10e;
pub const R_AARCH64_MOVW_SABS_G1: u32 = 0x10f;
pub const R_AARCH64_MOVW_SABS_G2: u32 = 0x110;
pub const R_AARCH64_LD_PREL_LO19: u32 = 0x111;
pub const R_AARCH64_ADR_PREL_LO21: u32 = 0x112;
pub const R_AARCH64_ADR_PREL_PG_HI21: u32 = 0x113;
pub const R_AARCH64_ADR_PREL_PG_HI21_NC: u32 = 0x114;
pub const R_AARCH64_ADD_ABS_LO12_NC: u32 = 0x115;
pub const R_AARCH64_LDST8_ABS_LO12_NC: u32 = 0x116;
pub const R_AARCH64_TSTBR14: u32 = 0x117;
pub const R_AARCH64_CONDBR19: u32 = 0x118;
pub const R_AARCH64_MOVW_PREL_G0: u32 = 0x11f;
pub const R_AARCH64_MOVW_PREL_G0_NC: u32 = 0x120;
pub const R_AARCH64_MOVW_PREL_G1: u32 = 0x121;
pub const R_AARCH64_MOVW_PREL_G1_NC: u32 = 0x122;
pub const R_AARCH64_MOVW_PREL_G2: u32 = 0x123;
pub const R_AARCH64_MOVW_PREL_G2_NC: u32 = 0x124;
pub const R_AARCH64_MOVW_PREL_G3: u32 = 0x125;
pub const R_AARCH64_JUMP26: u32 = 0x11a;
pub const R_AARCH64_CALL26: u32 = 0x11b;
pub const R_AARCH64_LDST16_ABS_LO12_NC: u32 = 0x11c;
pub const R_AARCH64_LDST32_ABS_LO12_NC: u32 = 0x11d;
pub const R_AARCH64_LDST64_ABS_LO12_NC: u32 = 0x11e;
pub const R_AARCH64_LDST128_ABS_LO12_NC: u32 = 0x12b;
pub const R_AARCH64_GOT_LD_PREL19: u32 = 0x135;
pub const R_AARCH64_ADR_GOT_PAGE: u32 = 0x137;
pub const R_AARCH64_PLT32: u32 = 0x13a;
pub const R_AARCH64_LD64_GOT_LO12_NC: u32 = 0x138;
pub const R_AARCH64_GOTPCREL32: u32 = 0x13b;
pub const R_AARCH64_COPY: u32 = 0x400;
pub const R_AARCH64_TLS_DTPMOD64: u32 = 1028;
pub const R_AARCH64_TLS_DTPREL64: u32 = 1029;
pub const R_AARCH64_TLS_TPREL64: u32 = 1030;
pub const R_AARCH64_GLOB_DAT: u32 = 0x401;
pub const R_AARCH64_JUMP_SLOT: u32 = 0x402;
pub const R_AARCH64_RELATIVE: u32 = 0x403;
pub const R_AARCH64_IRELATIVE: u32 = 0x408;
pub const R_AARCH64_TLSGD_ADR_PAGE21: u32 = 0x201;
pub const R_AARCH64_TLSLD_ADR_PAGE21: u32 = 0x206;
pub const R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: u32 = 0x21d;
pub const R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: u32 = 0x21e;
pub const R_AARCH64_TLSLE_MOVW_TPREL_G2: u32 = 0x220;
pub const R_AARCH64_TLSLE_MOVW_TPREL_G1: u32 = 0x221;
pub const R_AARCH64_TLSLE_MOVW_TPREL_G1_NC: u32 = 0x222;
pub const R_AARCH64_TLSLE_MOVW_TPREL_G0: u32 = 0x223;
pub const R_AARCH64_TLSLE_MOVW_TPREL_G0_NC: u32 = 0x224;
pub const R_AARCH64_TLSLE_ADD_TPREL_HI12: u32 = 0x225;
pub const R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: u32 = 0x227;
pub const R_AARCH64_TLSDESC_ADR_PAGE21: u32 = 0x232;
pub const R_AARCH64_TLSDESC_LD64_LO12: u32 = 0x233;
pub const R_AARCH64_TLSDESC_ADD_LO12: u32 = 0x234;
pub const R_AARCH64_TLSDESC_CALL: u32 = 0x239;
pub const R_AARCH64_TLSDESC: u32 = 0x407;
pub struct AArch64;
impl Arch for AArch64 {
fn spec(r_type: u32) -> Result<Spec> {
Ok(match r_type {
R_AARCH64_NONE => of(RelExpr::None, Write::Bytes(WriteKind::W64)),
R_AARCH64_ABS64 => of(RelExpr::Abs, Write::Bytes(WriteKind::W64)),
R_AARCH64_ABS32 => of(RelExpr::Abs, Write::Bytes(WriteKind::W32SU)),
R_AARCH64_ABS16 => of(RelExpr::Abs, Write::Bytes(WriteKind::W16SU)),
R_AARCH64_PREL64 => of(RelExpr::Pc, Write::Bytes(WriteKind::W64)),
R_AARCH64_PREL32 => of(RelExpr::Pc, Write::Bytes(WriteKind::W32SU)),
R_AARCH64_PREL16 => of(RelExpr::Pc, Write::Bytes(WriteKind::W16SU)),
R_AARCH64_ADD_ABS_LO12_NC | R_AARCH64_TLSLE_ADD_TPREL_LO12_NC => {
of(RelExpr::Abs, Write::Field(imm12()))
}
R_AARCH64_MOVW_UABS_G0 => of(RelExpr::Abs, Write::Field(movw(0))),
R_AARCH64_MOVW_UABS_G0_NC => {
of(RelExpr::Abs, Write::Field(movw_nc(0)))
}
R_AARCH64_MOVW_UABS_G1 => of(RelExpr::Abs, Write::Field(movw(16))),
R_AARCH64_MOVW_UABS_G1_NC => {
of(RelExpr::Abs, Write::Field(movw_nc(16)))
}
R_AARCH64_MOVW_UABS_G2 => of(RelExpr::Abs, Write::Field(movw(32))),
R_AARCH64_MOVW_UABS_G2_NC => {
of(RelExpr::Abs, Write::Field(movw_nc(32)))
}
R_AARCH64_MOVW_UABS_G3 => {
of(RelExpr::Abs, Write::Field(movw_nc(48)))
}
R_AARCH64_CALL26 | R_AARCH64_JUMP26 => {
of(RelExpr::PltPc, Write::Field(branch26()))
}
R_AARCH64_CONDBR19 => of(RelExpr::PltPc, Write::Field(branch19())),
R_AARCH64_LD_PREL_LO19 => of(RelExpr::Pc, Write::Field(branch19())),
R_AARCH64_GOT_LD_PREL19 => {
of(RelExpr::GotPc, Write::Field(branch19()))
}
R_AARCH64_PLT32 => {
of(RelExpr::PltPc, Write::Bytes(WriteKind::W32S))
}
R_AARCH64_MOVW_SABS_G0
| R_AARCH64_MOVW_SABS_G1
| R_AARCH64_MOVW_SABS_G2
| R_AARCH64_MOVW_PREL_G0
| R_AARCH64_MOVW_PREL_G0_NC
| R_AARCH64_MOVW_PREL_G1
| R_AARCH64_MOVW_PREL_G1_NC
| R_AARCH64_MOVW_PREL_G2
| R_AARCH64_MOVW_PREL_G2_NC
| R_AARCH64_MOVW_PREL_G3 => {
of(RelExpr::Escape, Write::Field(movw(0)))
}
R_AARCH64_TSTBR14 => of(RelExpr::PltPc, Write::Field(branch14())),
R_AARCH64_GOTPCREL32 => {
of(RelExpr::GotPc, Write::Bytes(WriteKind::W32S))
}
R_AARCH64_TLSLE_ADD_TPREL_HI12 => {
of(RelExpr::Abs, Write::Field(add_tprel_hi12()))
}
R_AARCH64_TLSLE_MOVW_TPREL_G0
| R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
| R_AARCH64_TLSLE_MOVW_TPREL_G1
| R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
| R_AARCH64_TLSLE_MOVW_TPREL_G2
| R_AARCH64_LDST8_ABS_LO12_NC
| R_AARCH64_LDST16_ABS_LO12_NC
| R_AARCH64_LDST32_ABS_LO12_NC
| R_AARCH64_LDST64_ABS_LO12_NC
| R_AARCH64_LDST128_ABS_LO12_NC
| R_AARCH64_LD64_GOT_LO12_NC
| R_AARCH64_ADR_PREL_PG_HI21
| R_AARCH64_ADR_PREL_PG_HI21_NC
| R_AARCH64_ADR_PREL_LO21
| R_AARCH64_ADR_GOT_PAGE
| R_AARCH64_TLSGD_ADR_PAGE21
| R_AARCH64_TLSLD_ADR_PAGE21
| R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
| R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
| R_AARCH64_TLSDESC_ADR_PAGE21
| R_AARCH64_TLSDESC_LD64_LO12
| R_AARCH64_TLSDESC_ADD_LO12
| R_AARCH64_TLSDESC_CALL
| R_AARCH64_TLSDESC => {
of(RelExpr::Escape, Write::Field(insn_cell(4)))
}
other => return Err(Error::UnsupportedReloc(other)),
})
}
fn relax_covers(r_type: u32) -> bool {
Self::relax_lead(r_type) != 0 || is_dynamic_tls(r_type)
}
fn relax_required<R: Resolver>(
r_type: u32,
_sym: Option<SymbolId>,
_resolver: &R,
) -> bool {
is_dynamic_tls(r_type)
}
fn scan_needs(r_type: u32) -> Result<Needs> {
let base = Self::spec(r_type)?.expr.needs();
Ok(
if r_type == R_AARCH64_ADR_GOT_PAGE
|| r_type == R_AARCH64_LD64_GOT_LO12_NC
{
Needs { got: true, ..base }
} else {
base
},
)
}
fn escape<R: Resolver>(
r_type: u32,
sym: Option<SymbolId>,
addend: i64,
place: u64,
resolver: &R,
out: &mut [u8],
) -> Result<()> {
match r_type {
R_AARCH64_ADR_PREL_PG_HI21 | R_AARCH64_ADR_PREL_PG_HI21_NC => {
let s_a = RelExpr::Abs.compute(sym, addend, 0, resolver)?;
write_page21(r_type, out, s_a, place)
}
R_AARCH64_ADR_PREL_LO21 => {
let val =
pc_value::<Self, R>(r_type, sym, addend, place, resolver)?;
check_range(r_type, adr_lo21(), val)?;
write_adr(out, val)
}
R_AARCH64_ADR_GOT_PAGE => {
let got = RelExpr::Got.compute(sym, addend, 0, resolver)?;
write_page21(r_type, out, got, place)
}
R_AARCH64_LDST8_ABS_LO12_NC
| R_AARCH64_LDST16_ABS_LO12_NC
| R_AARCH64_LDST32_ABS_LO12_NC
| R_AARCH64_LDST64_ABS_LO12_NC
| R_AARCH64_LDST128_ABS_LO12_NC => {
let val = RelExpr::Abs.compute(sym, addend, 0, resolver)?;
write_ldst12(r_type, out, val, ldst_scale(r_type))
}
R_AARCH64_LD64_GOT_LO12_NC => {
let val = RelExpr::Got.compute(sym, addend, 0, resolver)?;
write_ldst12(r_type, out, val, 3)
}
R_AARCH64_TLSLE_MOVW_TPREL_G0
| R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
| R_AARCH64_TLSLE_MOVW_TPREL_G1
| R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
| R_AARCH64_TLSLE_MOVW_TPREL_G2 => {
let val = RelExpr::Abs.compute(sym, addend, 0, resolver)?;
write_movw(r_type, out, val)
}
R_AARCH64_MOVW_SABS_G0
| R_AARCH64_MOVW_SABS_G1
| R_AARCH64_MOVW_SABS_G2 => {
let val = RelExpr::Abs.compute(sym, addend, place, resolver)?;
write_movw(r_type, out, val)
}
R_AARCH64_MOVW_PREL_G0
| R_AARCH64_MOVW_PREL_G0_NC
| R_AARCH64_MOVW_PREL_G1
| R_AARCH64_MOVW_PREL_G1_NC
| R_AARCH64_MOVW_PREL_G2
| R_AARCH64_MOVW_PREL_G2_NC
| R_AARCH64_MOVW_PREL_G3 => {
let val =
pc_value::<Self, R>(r_type, sym, addend, place, resolver)?;
write_movw(r_type, out, val)
}
_ => Err(Error::UnsupportedReloc(r_type)),
}
}
fn undef_weak_pc(r_type: u32, addend: i64, _place: u64) -> u64 {
let a = addend.cast_unsigned();
match r_type {
R_AARCH64_CALL26 | R_AARCH64_CONDBR19 | R_AARCH64_JUMP26
| R_AARCH64_TSTBR14 => a.wrapping_add(4),
_ => a,
}
}
}
const fn is_dynamic_tls(r_type: u32) -> bool {
matches!(
r_type,
R_AARCH64_TLSGD_ADR_PAGE21
| R_AARCH64_TLSLD_ADR_PAGE21
| R_AARCH64_TLSDESC_ADR_PAGE21
| R_AARCH64_TLSDESC_LD64_LO12
| R_AARCH64_TLSDESC_ADD_LO12
| R_AARCH64_TLSDESC_CALL
| R_AARCH64_TLSDESC
)
}
const fn imm12() -> Field {
Field::new(4, 0, 10, 12, Check::None)
}
const fn add_tprel_hi12() -> Field {
Field::new(4, 12, 10, 12, Check::Unsigned)
}
const fn ldst_scale(r_type: u32) -> u8 {
match r_type {
R_AARCH64_LDST16_ABS_LO12_NC => 1,
R_AARCH64_LDST32_ABS_LO12_NC => 2,
R_AARCH64_LDST64_ABS_LO12_NC => 3,
R_AARCH64_LDST128_ABS_LO12_NC => 4,
_ => 0,
}
}
const fn movw(base: u8) -> Field {
Field::new(4, base, 5, 16, Check::Unsigned)
}
const fn movw_nc(base: u8) -> Field {
Field::new(4, base, 5, 16, Check::None)
}
const fn movw_slice(r_type: u32) -> Option<(u8, Option<u32>)> {
match r_type {
R_AARCH64_TLSLE_MOVW_TPREL_G0
| R_AARCH64_MOVW_SABS_G0
| R_AARCH64_MOVW_PREL_G0 => Some((0, Some(17))),
R_AARCH64_TLSLE_MOVW_TPREL_G0_NC | R_AARCH64_MOVW_PREL_G0_NC => {
Some((0, None))
}
R_AARCH64_TLSLE_MOVW_TPREL_G1
| R_AARCH64_MOVW_SABS_G1
| R_AARCH64_MOVW_PREL_G1 => Some((16, Some(33))),
R_AARCH64_TLSLE_MOVW_TPREL_G1_NC | R_AARCH64_MOVW_PREL_G1_NC => {
Some((16, None))
}
R_AARCH64_TLSLE_MOVW_TPREL_G2
| R_AARCH64_MOVW_SABS_G2
| R_AARCH64_MOVW_PREL_G2 => Some((32, Some(49))),
R_AARCH64_MOVW_PREL_G2_NC => Some((32, None)),
R_AARCH64_MOVW_PREL_G3 => Some((48, None)),
_ => None,
}
}
const fn branch19() -> Field {
Field::scaled(4, 2, 5, 19, Check::Signed)
}
const fn branch14() -> Field {
Field::scaled(4, 2, 5, 14, Check::Signed)
}
const fn adr_page21() -> Field {
Field::new(4, 12, 0, 21, Check::Signed)
}
const fn adr_lo21() -> Field {
Field::new(4, 0, 0, 21, Check::Signed)
}
const fn ldst_lo12(scale: u8) -> Field {
Field::scaled(4, scale, 10, 12, Check::None)
}
fn check_range(r_type: u32, field: Field, value: u64) -> Result<()> {
if field.fits(value) {
Ok(())
} else {
Err(Error::RelocOverflow(r_type))
}
}
fn write_ldst12(
r_type: u32,
out: &mut [u8],
value: u64,
scale: u8,
) -> Result<()> {
if out.len() != 4 {
return Err(Error::OutOfRange("LDST lo12 relocation slot"));
}
if !ldst_lo12(scale).aligned(value) {
return Err(Error::RelocMisaligned(r_type));
}
let scaled = (value & 0xFFF) >> scale;
let cell = u32::from_le_bytes([out[0], out[1], out[2], out[3]]);
#[allow(clippy::cast_possible_truncation)]
let field = (scaled as u32) << 10;
let mask: u32 = 0xFFF << 10;
let patched = (cell & !mask) | field;
out.copy_from_slice(&patched.to_le_bytes());
Ok(())
}
fn write_movw(r_type: u32, out: &mut [u8], value: u64) -> Result<()> {
let Some((shift, check)) = movw_slice(r_type) else {
return Err(Error::UnsupportedReloc(r_type));
};
if let Some(bits) = check
&& !super::fits_signed(value, bits)
{
return Err(Error::RelocOverflow(r_type));
}
write_smovw(out, value >> shift)
}
fn write_smovw(out: &mut [u8], imm: u64) -> Result<()> {
if out.len() != 4 {
return Err(Error::OutOfRange("MOVW relocation slot"));
}
let mut cell = u32::from_le_bytes([out[0], out[1], out[2], out[3]]);
#[allow(clippy::cast_possible_truncation)]
let mut imm = imm as u32;
if cell & (1 << 29) == 0 {
if imm & 0x1_0000 == 0 {
cell |= 1 << 30;
} else {
imm ^= 0xFFFF;
cell &= !(1 << 30);
}
}
let mask: u32 = 0xFFFF << 5;
let patched = (cell & !mask) | ((imm & 0xFFFF) << 5);
out.copy_from_slice(&patched.to_le_bytes());
Ok(())
}
fn write_page21(
r_type: u32,
out: &mut [u8],
value: u64,
place: u64,
) -> Result<()> {
let delta = page(value).wrapping_sub(page(place));
if r_type != R_AARCH64_ADR_PREL_PG_HI21_NC {
check_range(r_type, adr_page21(), delta)?;
}
write_adr(out, delta >> 12)
}
fn write_adr(out: &mut [u8], imm: u64) -> Result<()> {
if out.len() != 4 {
return Err(Error::OutOfRange("ADR relocation slot"));
}
let cell = u32::from_le_bytes([out[0], out[1], out[2], out[3]]);
#[allow(clippy::cast_possible_truncation)]
let imm = imm as u32;
let imm_lo = (imm & 0x3) << 29;
let imm_hi = (imm & 0x001F_FFFC) << 3;
let mask: u32 = (0x3 << 29) | (0x001F_FFFC << 3);
let patched = (cell & !mask) | imm_lo | imm_hi;
out.copy_from_slice(&patched.to_le_bytes());
Ok(())
}