Skip to content

Commit 457ee10

Browse files
committed
Emit FORCEACTIVE in LCF & various fixes
1 parent d9e1ae2 commit 457ee10

File tree

9 files changed

+49
-21
lines changed

9 files changed

+49
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "decomp-toolkit"
33
description = "Yet another GameCube/Wii decompilation toolkit."
44
authors = ["Luke Street <[email protected]>"]
55
license = "MIT OR Apache-2.0"
6-
version = "0.3.3"
6+
version = "0.3.4"
77
edition = "2021"
88
publish = false
99
build = "build.rs"

assets/ldscript.lcf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ FORCEFILES
2626

2727
FORCEACTIVE
2828
{
29+
$FORCEACTIVE
2930
}

src/cmd/dol.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub struct ProjectConfig {
119119
/// Version of the MW `.comment` section format.
120120
/// If not present, no `.comment` sections will be written.
121121
pub mw_comment_version: Option<u8>,
122+
#[serde(default)]
122123
pub modules: Vec<ModuleConfig>,
123124
// Analysis options
124125
#[serde(default = "bool_true")]
@@ -258,6 +259,14 @@ fn split(args: SplitArgs) -> Result<()> {
258259
if let Some((symbol_index, symbol)) =
259260
obj.symbols.for_relocation(target, rel_reloc.kind)?
260261
{
262+
if symbol.flags.is_local() {
263+
bail!(
264+
"Module {} relocation to {:#010X} found local symbol {}",
265+
module_id,
266+
symbol.address,
267+
symbol.name
268+
);
269+
}
261270
let addend = target as i64 - symbol.address as i64;
262271
if addend != 0 {
263272
bail!(
@@ -268,7 +277,7 @@ fn split(args: SplitArgs) -> Result<()> {
268277
addend
269278
);
270279
}
271-
obj.symbols.set_externally_referenced(symbol_index, true);
280+
obj.symbols.flags(symbol_index).set_force_active(true);
272281
} else {
273282
// Add label
274283
let target_section = obj.section_at(target)?;
@@ -577,6 +586,10 @@ fn diff(args: DiffArgs) -> Result<()> {
577586
apply_map_file(&args.map_file, &mut linked_obj)?;
578587

579588
for orig_sym in obj.symbols.iter() {
589+
if orig_sym.kind == ObjSymbolKind::Section || orig_sym.section.is_none() {
590+
continue;
591+
}
592+
580593
let linked_sym = linked_obj
581594
.symbols
582595
.at_address(orig_sym.address as u32)

src/obj/mod.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ flags! {
3434
Common,
3535
Hidden,
3636
ForceActive,
37-
// Same as ForceActive, but used internally
38-
ExternallyReferenced,
3937
}
4038
}
4139

@@ -74,11 +72,6 @@ impl ObjSymbolFlagSet {
7472
#[inline]
7573
pub fn is_force_active(&self) -> bool { self.0.contains(ObjSymbolFlags::ForceActive) }
7674

77-
#[inline]
78-
pub fn is_externally_referenced(&self) -> bool {
79-
self.0.contains(ObjSymbolFlags::ExternallyReferenced)
80-
}
81-
8275
#[inline]
8376
pub fn set_scope(&mut self, scope: ObjSymbolScope) {
8477
match scope {
@@ -101,11 +94,11 @@ impl ObjSymbolFlagSet {
10194
}
10295

10396
#[inline]
104-
pub fn set_externally_referenced(&mut self, value: bool) {
97+
pub fn set_force_active(&mut self, value: bool) {
10598
if value {
106-
self.0 |= ObjSymbolFlags::ExternallyReferenced;
99+
self.0 |= ObjSymbolFlags::ForceActive;
107100
} else {
108-
self.0 &= !ObjSymbolFlags::ExternallyReferenced;
101+
self.0 &= !ObjSymbolFlags::ForceActive;
109102
}
110103
}
111104
}
@@ -562,8 +555,9 @@ impl ObjSymbols {
562555
Ok(result)
563556
}
564557

565-
pub fn set_externally_referenced(&mut self, idx: SymbolIndex, value: bool) {
566-
self.symbols[idx].flags.set_externally_referenced(value);
558+
#[inline]
559+
pub fn flags(&mut self, idx: SymbolIndex) -> &mut ObjSymbolFlagSet {
560+
&mut self.symbols[idx].flags
567561
}
568562
}
569563

@@ -629,6 +623,18 @@ impl ObjInfo {
629623

630624
pub fn section_data(&self, start: u32, end: u32) -> Result<(&ObjSection, &[u8])> {
631625
let section = self.section_at(start)?;
626+
ensure!(
627+
section.contains_range(start..end),
628+
"Range {:#010X}-{:#010X} outside of section {}: {:#010X}-{:#010X}",
629+
start,
630+
end,
631+
section.name,
632+
section.address,
633+
section.address + section.size
634+
);
635+
if section.kind == ObjSectionKind::Bss {
636+
return Ok((section, &[]));
637+
}
632638
let data = if end == 0 {
633639
&section.data[(start as u64 - section.address) as usize..]
634640
} else {

src/obj/split.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn split_ctors_dtors(obj: &mut ObjInfo, section_start: u32, section_end: u32) ->
9797

9898
// Hack to avoid deadstripping
9999
for symbol_idx in referenced_symbols {
100-
obj.symbols.set_externally_referenced(symbol_idx, true);
100+
obj.symbols.flags(symbol_idx).set_force_active(true);
101101
}
102102

103103
Ok(())

src/util/comment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl CommentSym {
196196
vis_flags |= 0xD;
197197
}
198198
let mut active_flags = 0;
199-
if symbol.flags.is_force_active() || symbol.flags.is_externally_referenced() {
199+
if symbol.flags.is_force_active() {
200200
active_flags |= 0x8; // TODO what is 0x10?
201201
}
202202
Self { align, vis_flags, active_flags }

src/util/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ fn write_symbol<W: Write>(w: &mut W, obj: &ObjInfo, symbol: &ObjSymbol) -> Resul
200200
if symbol.flags.is_hidden() {
201201
write!(w, " hidden")?;
202202
}
203-
if symbol.flags.is_force_active() {
204-
write!(w, " force_active")?;
205-
}
203+
// if symbol.flags.is_force_active() {
204+
// write!(w, " force_active")?;
205+
// }
206206
if obj.blocked_ranges.contains_key(&(symbol.address as u32)) {
207207
write!(w, " noreloc")?;
208208
}

src/util/lcf.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ pub fn generate_ldscript(obj: &ObjInfo, auto_force_files: bool) -> Result<String
2323
force_files.push(obj_path.file_name().unwrap().to_str().unwrap().to_string());
2424
}
2525

26+
let mut force_active = vec![];
27+
for symbol in obj.symbols.iter() {
28+
if symbol.flags.is_force_active() && symbol.flags.is_global() {
29+
force_active.push(symbol.name.clone());
30+
}
31+
}
32+
2633
// Hack to handle missing .sbss2 section... what's the proper way?
2734
let last_section_name = obj.sections.last().unwrap().name.clone();
2835
let last_section_symbol = format!("_f_{}", last_section_name.trim_start_matches('.'));
@@ -31,7 +38,8 @@ pub fn generate_ldscript(obj: &ObjInfo, auto_force_files: bool) -> Result<String
3138
.replacen("$SECTIONS", &section_defs, 1)
3239
.replace("$LAST_SECTION_SYMBOL", &last_section_symbol)
3340
.replace("$LAST_SECTION_NAME", &last_section_name)
34-
.replacen("$STACKSIZE", &format!("{:#X}", stack_size), 1);
41+
.replacen("$STACKSIZE", &format!("{:#X}", stack_size), 1)
42+
.replacen("$FORCEACTIVE", &force_active.join("\n "), 1);
3543
out = if auto_force_files {
3644
out.replacen("$FORCEFILES", &force_files.join("\n "), 1)
3745
} else {

0 commit comments

Comments
 (0)