use super::{GOT_ENTRY, Layout};
use crate::{symbol::SymbolId, tls};
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
pub enum GotKind {
Addr,
TlsOffset,
TlsModule,
}
impl GotKind {
pub const fn slots(self) -> u64 {
match self {
Self::Addr | Self::TlsOffset => 1,
Self::TlsModule => 2,
}
}
}
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
pub enum GotOwner {
Global(SymbolId),
Local(usize, u32),
Module,
}
impl GotOwner {
pub fn of(id: Option<SymbolId>, file: usize, sym_idx: u32) -> Self {
id.map_or(Self::Local(file, sym_idx), Self::Global)
}
pub const MODULE: Self = Self::Module;
}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub struct GotKey {
pub kind: GotKind,
pub owner: GotOwner,
}
impl core::hash::Hash for GotKey {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
const TAG_SHIFT: u32 = 60;
let (tag, payload) = match self.owner {
GotOwner::Global(id) => (0u64, id.0 as u64),
GotOwner::Local(file, sym_idx) => {
(1, (file as u64) << 32 | u64::from(sym_idx))
}
GotOwner::Module => (2, 0),
};
let kind = self.kind as u64;
state.write_u64(payload ^ ((tag | (kind << 2)) << TAG_SHIFT));
}
}
impl GotKey {
pub const fn new(kind: GotKind, owner: GotOwner) -> Self {
Self { kind, owner }
}
pub const fn addr(id: SymbolId) -> Self {
Self::new(GotKind::Addr, GotOwner::Global(id))
}
pub fn global_id(&self) -> Option<SymbolId> {
match self.owner {
GotOwner::Global(id) => Some(id),
GotOwner::Local(..) | GotOwner::Module => None,
}
}
pub const MODULE: Self = Self::new(GotKind::TlsModule, GotOwner::Module);
}
pub(super) fn got_entry(layout: &Layout, key: GotKey) -> Option<u32> {
if layout.got.size == 0 {
return None;
}
layout.got_index.get(&key).copied()
}
pub(super) fn got_slot(layout: &Layout, key: GotKey) -> u64 {
let Some(index) = got_entry(layout, key) else {
return 0;
};
layout.got.vaddr.wrapping_add(u64::from(index) * GOT_ENTRY)
}
pub(super) fn plt_slot(layout: &Layout, key: GotKey) -> u64 {
if layout.plt.size == 0 {
return 0;
}
let Some(&index) = layout.plt_index.get(&key) else {
return 0;
};
let spec = layout.target.plt_spec();
let entry_off = spec
.header_size
.wrapping_add(u64::from(index) * spec.entry_size);
layout.plt.vaddr.wrapping_add(entry_off)
}
pub(super) fn fill_got(global_addr: &[u64], layout: &mut Layout) {
let block = layout.tls_block();
let target = layout.target;
let mut values = core::mem::take(&mut layout.got_values);
for (key, &index) in &layout.got_index {
let value = match key.owner {
GotOwner::Global(id) => {
global_addr.get(id.0).copied().unwrap_or_default()
}
GotOwner::Local(file, sym_idx) => layout
.sym
.get(file)
.and_then(|r| r.addr.get(sym_idx as usize))
.copied()
.unwrap_or_default(),
GotOwner::Module => continue,
};
let index = index as usize;
if key.kind == GotKind::TlsModule {
let offset = block
.map(|b| tls::block_offset(target, value, &b))
.unwrap_or_default();
if let Some(slot) = values.get_mut(index + 1) {
*slot = offset;
}
continue;
}
if let Some(slot) = values.get_mut(index) {
*slot = value;
}
}
layout.got_values = values;
}