Skip to content
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
31 changes: 30 additions & 1 deletion src/cmd/rel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ use crate::{
path::native_path,
rel::{
PERMITTED_SECTIONS, RelHeader, RelReloc, RelSectionHeader, RelWriteInfo,
print_relocations, process_rel, process_rel_header, process_rel_sections, write_rel,
SmallDataSection, print_relocations, process_rel, process_rel_header,
process_rel_sections, write_rel,
},
},
vfs::open_file,
Expand Down Expand Up @@ -261,6 +262,29 @@ fn resolve_relocations(
Ok(resolved)
}

/// Collect the static module's small data sections and their base registers.
fn small_data_sections(file: &File) -> Vec<SmallDataSection> {
let symbol_address = |name: &str| file.symbol_by_name(name).map(|s| s.address() as u32);
let mut result = Vec::new();
for section in file.sections() {
let (base_name, register) = match section.name() {
Ok(".sdata" | ".sbss") => ("_SDA_BASE_", 13),
Ok(".sdata2" | ".sbss2") => ("_SDA2_BASE_", 2),
_ => continue,
};
let Some(base) = symbol_address(base_name) else {
continue;
};
result.push(SmallDataSection {
start: section.address() as u32,
end: (section.address() + section.size()) as u32,
base,
register,
});
}
result
}

type RelInfo = (RelHeader, Vec<RelSectionHeader>, Option<Vec<SectionDef>>);

fn make(args: MakeArgs) -> Result<()> {
Expand Down Expand Up @@ -375,6 +399,10 @@ fn make(args: MakeArgs) -> Result<()> {

// Write RELs
let start = Instant::now();
let small_data = match modules.iter().find(|m| m.module_id == 0) {
Some(dol) => small_data_sections(&dol.file),
None => Vec::new(),
};
for (module_info, relocations) in modules.iter().skip(1).zip(relocations) {
let _span = info_span!("file", path = %module_info.path).entered();
let mut info = RelWriteInfo {
Expand All @@ -388,6 +416,7 @@ fn make(args: MakeArgs) -> Result<()> {
quiet: args.no_warn,
section_align: None,
section_exec: None,
small_data: small_data.clone(),
};
if let Some((header, section_headers, section_defs)) =
existing_headers.get(&module_info.module_id)
Expand Down
78 changes: 65 additions & 13 deletions src/util/rel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,32 +621,81 @@ pub struct RelReloc {
pub original_target_section: u8,
}

/// A small data section of the static module (DOL), addressed relative to a base register.
#[derive(Clone, Debug)]
pub struct SmallDataSection {
/// Start address of the section.
pub start: u32,
/// End address of the section (exclusive).
pub end: u32,
/// Address the 16-bit offset is relative to (`_SDA_BASE_` or `_SDA2_BASE_`).
pub base: u32,
/// Base register: 13 for `.sdata`/`.sbss`, 2 for `.sdata2`/`.sbss2`.
pub register: u8,
}

#[inline]
fn reloc_can_be_applied(_module_id: u32, rel_reloc: &RelReloc) -> bool {
matches!(rel_reloc.kind, ObjRelocKind::PpcRel24)
match rel_reloc.kind {
ObjRelocKind::PpcRel24 => true,
// Resolve SDA21 relocations from DOL (unsupported by OSLink)
ObjRelocKind::PpcEmbSda21 => rel_reloc.module_id == 0,
_ => false,
}
}

#[inline]
fn skip_reloc(module_id: u32, rel_reloc: &RelReloc) -> bool {
rel_reloc.module_id == module_id
&& rel_reloc.section == rel_reloc.target_section
&& matches!(rel_reloc.kind, ObjRelocKind::PpcRel24)
match rel_reloc.kind {
ObjRelocKind::PpcRel24 => {
rel_reloc.module_id == module_id && rel_reloc.section == rel_reloc.target_section
}
ObjRelocKind::PpcEmbSda21 => rel_reloc.module_id == 0,
_ => false,
}
}

fn apply_relocation(
data: &mut [u8],
module_id: u32,
info: &RelWriteInfo,
rel_reloc: &RelReloc,
header: &RelHeader,
) -> Result<()> {
let diff = if rel_reloc.module_id == module_id && rel_reloc.section == rel_reloc.target_section
{
rel_reloc.addend as i32 - rel_reloc.address as i32
} else if header.unresolved_section == rel_reloc.section {
header.unresolved_offset as i32 - rel_reloc.address as i32
} else {
if rel_reloc.kind == ObjRelocKind::PpcEmbSda21 {
// Against the static module, the addend is the absolute target address.
let target = rel_reloc.addend;
let section =
info.small_data.iter().find(|s| (s.start..s.end).contains(&target)).with_context(
|| {
format!(
"R_PPC_EMB_SDA21 relocation at {:#X} targets {:#X}, \
outside the static module's small data sections",
rel_reloc.address, target
)
},
)?;
let offset = target as i64 - section.base as i64;
ensure!(
(-0x8000..0x8000).contains(&offset),
"R_PPC_EMB_SDA21 relocation at {:#X} out of range ({:#X})",
rel_reloc.address,
offset
);
let ins_ref = array_ref_mut!(data, (rel_reloc.address & !3) as usize, 4);
let ins = u32::from_be_bytes(*ins_ref);
let ins = (ins & !0x1fffff) | ((section.register as u32) << 16) | (offset as u32 & 0xffff);
*ins_ref = ins.to_be_bytes();
return Ok(());
};
}

let diff =
if rel_reloc.module_id == info.module_id && rel_reloc.section == rel_reloc.target_section {
rel_reloc.addend as i32 - rel_reloc.address as i32
} else if header.unresolved_section == rel_reloc.section {
header.unresolved_offset as i32 - rel_reloc.address as i32
} else {
return Ok(());
};
let ins_ref = array_ref_mut!(data, rel_reloc.address as usize, 4);
let mut ins = u32::from_be_bytes(*ins_ref);
match rel_reloc.kind {
Expand Down Expand Up @@ -691,6 +740,9 @@ pub struct RelWriteInfo {
/// This is used to match empty sections: mwld will emit them with
/// NULL type, but the original REL may have them marked executable.
pub section_exec: Option<Vec<bool>>,
/// Small data sections of the static module, for resolving
/// R_PPC_EMB_SDA21 relocations against it.
pub small_data: Vec<SmallDataSection>,
}

pub const PERMITTED_SECTIONS: [&str; 7] =
Expand Down Expand Up @@ -1038,7 +1090,7 @@ where
if apply_relocations.iter().any(|r| r.original_section == section_index) {
let mut data = section_data.into_owned();
for reloc in apply_relocations.iter().filter(|r| r.original_section == section_index) {
apply_relocation(&mut data, info.module_id, reloc, &header)?;
apply_relocation(&mut data, info, reloc, &header)?;
}
section_data = data.into_cow();
}
Expand Down
Loading