use rayon::prelude::*;
use rustc_hash::FxHashSet;
use super::{LinkMode, copy_id_set, dynsym::DynSym, is_runtime_import};
use crate::{
elf::{
Rela64, Sym64, SymbolTable,
constants::{SHF_ALLOC, SHF_WRITE, SHT_NOBITS, STB_LOCAL},
},
endian::{I64, U64},
error::{Error, Result},
input::InputFile,
layout::{FileResolver, GotKind, Layout, Sect},
linker::{Context, in_input_order},
reloc::{DynRelocs, RelExpr, Target},
symbol::{DefSource, Definition, SymbolId, SymbolKind, version_stem},
tls::{TlsBlock, block_offset},
util::show,
};
const GOT_ENTRY: u64 = 8;
pub(super) fn build_rela_dyn(
ctx: &Context<'_>,
layout: &Layout,
target: Target,
names: &[&[u8]],
table: &DynSym,
) -> Result<Vec<Rela64>> {
let relocs = target.dyn_relocs();
let mut relative = Vec::new();
let mut sym_based = Vec::new();
let copy_ids = copy_id_set(layout);
let fixed = FixedImports::new(layout, ©_ids);
let mut out = RelaSink {
relative: &mut relative,
sym_based: &mut sym_based,
};
push_got_entries(ctx, layout, relocs, names, table, fixed, &mut out);
let dr = DataRelocCtx {
scope: DataRelocScope::new(ctx, layout, fixed),
layout,
table,
abs64: relocs.abs64,
relative_type: relocs.relative,
};
scan_data_relocs(dr, &mut out)?;
for slot in &layout.copy_slots {
let Some(name) = names.get(slot.id.0) else {
continue;
};
let Some(sym_idx) = table.index_of(name) else {
continue;
};
out.sym_based
.push(make_rela(slot.addr, sym_idx, relocs.copy, 0));
}
relative.append(&mut sym_based);
Ok(relative)
}
fn push_got_entries(
ctx: &Context<'_>,
layout: &Layout,
relocs: DynRelocs,
names: &[&[u8]],
table: &DynSym,
fixed: FixedImports<'_>,
out: &mut RelaSink<'_>,
) {
let shared = layout.mode() == LinkMode::Shared;
let tls = TlsRelocs {
relocs,
loader_owns_tp: shared,
ctx,
target: layout.target,
block: layout.tls_block(),
};
let mut index = 0u64;
for key in &layout.got_keys {
let slot = layout
.region(Sect::Got)
.vaddr
.wrapping_add(index * GOT_ENTRY);
let addend = layout
.got_values
.get(usize::try_from(index).unwrap_or(0))
.copied()
.unwrap_or_default();
index = index.saturating_add(key.kind.slots());
let global = key.global_id();
let dyn_idx = global
.and_then(|id| names.get(id.0))
.and_then(|name| table.index_of(name));
if key.kind != GotKind::Addr {
out.sym_based.extend(tls_entry(
&tls, key.kind, global, dyn_idx, slot, addend,
));
continue;
}
let kind = global
.map_or(Some(SlotReloc::Relative), |id| {
classify_slot(ctx, shared, fixed, id)
})
.filter(|k| {
!matches!(k, SlotReloc::Relative)
|| relative_applies(layout.pie)
});
match kind {
Some(SlotReloc::ByName(_)) => {
if let Some(sym_idx) = dyn_idx {
out.sym_based.push(make_rela(
slot,
sym_idx,
relocs.glob_dat,
0,
));
}
}
Some(SlotReloc::Relative) => {
out.relative
.push(make_rela(slot, 0, relocs.relative, addend));
}
None => {}
}
}
}
struct TlsRelocs<'a, 'b> {
relocs: DynRelocs,
loader_owns_tp: bool,
ctx: &'a Context<'b>,
target: Target,
block: Option<TlsBlock>,
}
impl TlsRelocs<'_, '_> {
fn block_offset(&self, filled: u64) -> Option<u64> {
self.block
.as_ref()
.map(|b| block_offset(self.target, filled, b))
}
}
fn tls_entry(
tls: &TlsRelocs<'_, '_>,
kind: GotKind,
global: Option<SymbolId>,
dyn_idx: Option<u32>,
slot: u64,
filled: u64,
) -> Vec<Rela64> {
let relocs = tls.relocs;
let mut out = Vec::new();
match kind {
GotKind::Addr => {}
GotKind::TlsOffset => {
let wanted = global.is_some_and(|id| {
tls.loader_owns_tp || is_runtime_import(tls.ctx, id)
});
match (wanted, dyn_idx) {
(true, Some(sym_idx)) => {
out.push(make_rela(slot, sym_idx, relocs.tpoff, 0));
}
(true, None) => {
if let Some(off) = tls.block_offset(filled) {
out.push(make_rela(slot, 0, relocs.tpoff, off));
}
}
(false, _) => {}
}
}
GotKind::TlsModule => {
out.push(make_rela(slot, dyn_idx.unwrap_or(0), relocs.dtpmod, 0));
if let Some(sym_idx) = dyn_idx {
out.push(make_rela(
slot.wrapping_add(GOT_ENTRY),
sym_idx,
relocs.dtpoff,
0,
));
}
}
}
out
}
struct DataRelocSection<'a> {
file: usize,
section: u16,
writable: bool,
symtab: &'a SymbolTable<'a>,
entries: &'a [Rela64],
}
impl DataRelocSection<'_> {
fn absolute(&self, abs64: u32) -> impl Iterator<Item = &Rela64> {
self.entries.iter().filter(move |r| r.r_type() == abs64)
}
fn symbol(&self, r: &Rela64) -> Option<&Sym64> {
self.symtab.syms.get(r.sym() as usize)
}
}
fn for_each_data_reloc_section_of(
ctx: &Context<'_>,
file: usize,
input: &InputFile<'_>,
candidates: Option<&[u16]>,
mut visit: impl FnMut(&DataRelocSection<'_>) -> Result<()>,
) -> Result<()> {
let obj = input.object()?;
let Some(symtab) = input.symbol_table()? else {
return Ok(());
};
let mut all = Vec::new();
if candidates.is_none() {
all.extend(
(0..obj.sections().len()).filter_map(|i| u16::try_from(i).ok()),
);
}
let sections: &[u16] = candidates.unwrap_or(&all);
for §ion in sections {
let Some(shdr) = obj.sections().get(usize::from(section)) else {
continue;
};
let flags = shdr.sh_flags.get();
if flags & SHF_ALLOC == 0 || shdr.sh_type.get() == SHT_NOBITS {
continue;
}
if ctx.folding.is_folded((file, section)) {
continue;
}
let Some(entries) = input.relocations(section)? else {
continue;
};
visit(&DataRelocSection {
file,
section,
writable: flags & SHF_WRITE != 0,
symtab: &symtab,
entries,
})?;
}
Ok(())
}
fn map_data_reloc_files<T>(
ctx: &Context<'_>,
candidates: Option<&[Vec<u16>]>,
visit: impl Fn(&DataRelocSection<'_>, &mut T) -> Result<()> + Sync,
) -> Result<Vec<T>>
where
T: Default + Send,
{
let mut rows: Vec<Result<T>> = Vec::new();
ctx.files
.par_iter()
.enumerate()
.map(|(file, input)| {
let mut acc = T::default();
let list = candidates.and_then(|c| c.get(file)).map(Vec::as_slice);
for_each_data_reloc_section_of(ctx, file, input, list, |sec| {
visit(sec, &mut acc)
})?;
Ok(acc)
})
.collect_into_vec(&mut rows);
in_input_order(rows)
}
#[derive(Clone, Copy)]
pub(super) enum SlotReloc {
Relative,
ByName(SymbolId),
}
pub(super) const fn relative_applies(pie: bool) -> bool {
pie
}
#[derive(Clone, Copy)]
pub(super) struct FixedImports<'a> {
copy: &'a FxHashSet<SymbolId>,
canonical: &'a FxHashSet<SymbolId>,
}
impl<'a> FixedImports<'a> {
pub(super) fn new(
layout: &'a Layout,
copy: &'a FxHashSet<SymbolId>,
) -> Self {
Self {
copy,
canonical: &layout.canonical_plt,
}
}
fn contains(self, id: SymbolId) -> bool {
self.copy.contains(&id) || self.canonical.contains(&id)
}
pub(super) fn is_copy(self, id: SymbolId) -> bool {
self.copy.contains(&id)
}
}
#[derive(Clone, Copy)]
struct DataRelocScope<'a, 'data> {
ctx: &'a Context<'data>,
fixed: FixedImports<'a>,
shared: bool,
pie: bool,
}
impl<'a, 'data> DataRelocScope<'a, 'data> {
fn new(
ctx: &'a Context<'data>,
layout: &'a Layout,
fixed: FixedImports<'a>,
) -> Self {
Self {
ctx,
fixed,
shared: layout.mode() == LinkMode::Shared,
pie: layout.pie,
}
}
fn emitted(self, kind: SlotReloc, writable: bool) -> Option<SlotReloc> {
match kind {
SlotReloc::ByName(id) => (!self.resolves_to_zero(id, writable))
.then_some(SlotReloc::ByName(id)),
SlotReloc::Relative => {
relative_applies(self.pie).then_some(SlotReloc::Relative)
}
}
}
fn resolves_to_zero(self, id: SymbolId, writable: bool) -> bool {
if self.pie && writable {
return false;
}
let weak_undef = self.ctx.symbols.symbol(id).is_some_and(|s| {
matches!(s.kind, SymbolKind::Undefined { weak: true })
});
weak_undef
&& self
.ctx
.dep_exports
.get(self.ctx.symbols.name(id))
.is_none()
}
}
#[derive(Clone, Copy)]
struct DataRelocCtx<'a, 'data> {
scope: DataRelocScope<'a, 'data>,
layout: &'a Layout,
table: &'a DynSym,
abs64: u32,
relative_type: u32,
}
fn classify_data_reloc(
scope: DataRelocScope<'_, '_>,
sec: &DataRelocSection<'_>,
r: &Rela64,
) -> Result<Option<SlotReloc>> {
let Some(sym) = sec.symbol(r) else {
return Ok(None);
};
let verdict = if sym.bind() == STB_LOCAL {
Some(SlotReloc::Relative)
} else {
scope.ctx.global_id(sec.file, r.sym()).and_then(|id| {
classify_slot(scope.ctx, scope.shared, scope.fixed, id)
})
};
let Some(kind) = verdict.and_then(|k| scope.emitted(k, sec.writable))
else {
return Ok(None);
};
if !sec.writable {
return Err(text_reloc(sec.symtab.name(sym)));
}
Ok(Some(kind))
}
fn text_reloc(name: &[u8]) -> Error {
Error::TextRelocation(show(name))
}
pub(super) fn classify_slot(
ctx: &Context<'_>,
shared: bool,
fixed: FixedImports<'_>,
id: SymbolId,
) -> Option<SlotReloc> {
if fixed.contains(id) {
return Some(SlotReloc::Relative);
}
classify_global(ctx, shared, id)
}
pub(super) fn classify_global(
ctx: &Context<'_>,
shared: bool,
id: SymbolId,
) -> Option<SlotReloc> {
let sym = ctx.symbols.symbol(id)?;
if sym.is_preemptible(shared) {
return Some(SlotReloc::ByName(id));
}
match &sym.kind {
SymbolKind::Defined(def) => {
definition_is_address(ctx, id, def).then_some(SlotReloc::Relative)
}
SymbolKind::Common { .. } => Some(SlotReloc::Relative),
SymbolKind::Undefined { .. } => None,
}
}
fn definition_is_address(
ctx: &Context<'_>,
id: SymbolId,
def: &Definition,
) -> bool {
matches!(def.source, DefSource::Section { .. })
|| ctx.defsyms.iter().any(|&(defsym, _)| defsym == id)
}
struct RelaSink<'a> {
relative: &'a mut Vec<Rela64>,
sym_based: &'a mut Vec<Rela64>,
}
impl RelaSink<'_> {
fn push_relative(&mut self, r_type: u32, slot: u64, value: u64) {
self.relative.push(make_rela(slot, 0, r_type, value));
}
}
fn scan_data_relocs(
dr: DataRelocCtx<'_, '_>,
out: &mut RelaSink<'_>,
) -> Result<()> {
let rows = map_data_reloc_files(
dr.scope.ctx,
Some(&dr.layout.abs_candidates),
|sec, acc: &mut RelaRow| {
let Some(vaddr) = dr.layout.section_vaddr(sec.file, sec.section)
else {
return Ok(());
};
let resolver = dr.layout.resolver(sec.file);
let mut sink = RelaSink {
relative: &mut acc.relative,
sym_based: &mut acc.sym_based,
};
for r in sec.absolute(dr.abs64) {
let slot = vaddr.wrapping_add(r.r_offset.get());
emit_data_reloc(dr, sec, &resolver, r, slot, &mut sink)?;
}
Ok(())
},
)?;
for mut row in rows {
out.relative.append(&mut row.relative);
out.sym_based.append(&mut row.sym_based);
}
Ok(())
}
#[derive(Default)]
struct RelaRow {
relative: Vec<Rela64>,
sym_based: Vec<Rela64>,
}
fn emit_data_reloc(
dr: DataRelocCtx<'_, '_>,
sec: &DataRelocSection<'_>,
resolver: &FileResolver<'_>,
r: &Rela64,
slot: u64,
out: &mut RelaSink<'_>,
) -> Result<()> {
match classify_data_reloc(dr.scope, sec, r)? {
None => Ok(()),
Some(SlotReloc::Relative) => {
let sym = (r.sym() != 0)
.then(|| SymbolId(usize::try_from(r.sym()).unwrap_or(0)));
let addend = dr.scope.ctx.merge.addend(sec.symtab, sec.file, r);
let value = RelExpr::Abs.compute(sym, addend, 0, resolver)?;
out.push_relative(dr.relative_type, slot, value);
Ok(())
}
Some(SlotReloc::ByName(id)) => emit_by_name(dr, sec, r, slot, id, out),
}
}
fn emit_by_name(
dr: DataRelocCtx<'_, '_>,
sec: &DataRelocSection<'_>,
r: &Rela64,
slot: u64,
id: SymbolId,
out: &mut RelaSink<'_>,
) -> Result<()> {
let Some(sym) = sec.symbol(r) else {
return Ok(());
};
let name = version_stem(sec.symtab.name(sym));
if let Some(sym_idx) = dr.table.index_of(name) {
out.sym_based.push(make_rela(
slot,
sym_idx,
dr.abs64,
r.r_addend.get().cast_unsigned(),
));
return Ok(());
}
let strong_import = dr.scope.ctx.symbols.symbol(id).is_some_and(|s| {
matches!(s.kind, SymbolKind::Undefined { weak: false })
});
if !strong_import
|| dr.scope.shared
|| dr.scope.ctx.dep_exports.get(name).is_some()
{
return Ok(());
}
Err(Error::UndefinedReference(show(name)))
}
#[derive(Default)]
pub(super) struct DataRelocSurvey {
pub(super) relative: u64,
pub(super) glob: u64,
pub(super) imports: Vec<SymbolId>,
}
#[derive(Default)]
struct SurveyRow {
relative: u64,
glob: u64,
imports: Vec<SymbolId>,
}
pub(super) fn survey_data_relocs(
ctx: &Context<'_>,
layout: &Layout,
fixed: FixedImports<'_>,
abs64: u32,
) -> Result<DataRelocSurvey> {
let scope = DataRelocScope::new(ctx, layout, fixed);
let rows = map_data_reloc_files(ctx, None, |sec, acc: &mut SurveyRow| {
for r in sec.absolute(abs64) {
match classify_data_reloc(scope, sec, r)? {
Some(SlotReloc::Relative) => {
acc.relative = acc.relative.saturating_add(1);
}
Some(SlotReloc::ByName(id)) => {
acc.glob = acc.glob.saturating_add(1);
if is_runtime_import(ctx, id)
&& !ctx.symbols.name(id).is_empty()
{
acc.imports.push(id);
}
}
None => {}
}
}
Ok(())
})?;
let mut out = DataRelocSurvey::default();
let mut seen: FxHashSet<SymbolId> = FxHashSet::default();
for row in rows {
out.relative = out.relative.saturating_add(row.relative);
out.glob = out.glob.saturating_add(row.glob);
out.imports
.extend(row.imports.into_iter().filter(|&id| seen.insert(id)));
}
Ok(out)
}
#[allow(clippy::cast_possible_truncation)]
fn make_rela(offset: u64, sym: u32, r_type: u32, addend: u64) -> Rela64 {
Rela64 {
r_offset: U64::new(offset),
r_info: U64::new((u64::from(sym) << 32) | u64::from(r_type)),
r_addend: I64::new(addend.cast_signed()),
}
}
pub(super) fn count_relative(rela: &[Rela64], relative_type: u32) -> u32 {
let mut n = 0u32;
for r in rela {
if (r.r_info.get() & 0xffff_ffff) as u32 == relative_type {
n = n.saturating_add(1);
} else {
break;
}
}
n
}