Skip to content

Commit 97302e5

Browse files
authored
dtk dol apply: skip updating anonymous symbols by default (#97)
* Add --relaxed flag to dtk dol apply to skip updating anonymous symbols * Invert --relaxed switch -> --full
1 parent 18987ed commit 97302e5

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/cmd/dol.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ pub struct ApplyArgs {
132132
#[argp(positional, from_str_fn(native_path))]
133133
/// linked ELF
134134
elf_file: Utf8NativePathBuf,
135+
#[argp(switch)]
136+
/// always update anonymous local symbol names, even if they are similar
137+
full: bool,
135138
}
136139

137140
#[derive(FromArgs, PartialEq, Eq, Debug)]
@@ -1841,6 +1844,31 @@ fn diff(args: DiffArgs) -> Result<()> {
18411844
Ok(())
18421845
}
18431846

1847+
fn are_local_anonymous_names_similar<'a>(left: &'a ObjSymbol, right: &'a ObjSymbol) -> bool {
1848+
if left.flags.scope() != ObjSymbolScope::Local || right.flags.scope() != ObjSymbolScope::Local {
1849+
return false;
1850+
}
1851+
1852+
let is_at_symbol =
1853+
|name: &str| name.starts_with('@') && name[1..].chars().all(|c| c.is_numeric());
1854+
1855+
if is_at_symbol(&left.name) && is_at_symbol(&right.name) {
1856+
// consider e.g. @8280 -> @8536 equal
1857+
return true;
1858+
}
1859+
1860+
let split_dollar_symbol = |name: &'a str| -> Option<&'a str> {
1861+
name.rsplit_once('$')
1862+
.and_then(|(prefix, suffix)| suffix.chars().all(|c| c.is_numeric()).then_some(prefix))
1863+
};
1864+
1865+
// consider e.g. __arraydtor$3926 -> __arraydtor$7669 equal
1866+
match (split_dollar_symbol(&left.name), split_dollar_symbol(&right.name)) {
1867+
(Some(left_prefix), Some(right_prefix)) => left_prefix == right_prefix,
1868+
_ => false,
1869+
}
1870+
}
1871+
18441872
fn apply(args: ApplyArgs) -> Result<()> {
18451873
log::info!("Loading {}", args.config);
18461874
let mut config_file = open_file(&args.config, true)?;
@@ -1891,7 +1919,9 @@ fn apply(args: ApplyArgs) -> Result<()> {
18911919
let mut updated_sym = orig_sym.clone();
18921920
let is_globalized = linked_sym.name.ends_with(&format!("_{:08X}", linked_sym.address));
18931921
if (is_globalized && !linked_sym.name.starts_with(&orig_sym.name))
1894-
|| (!is_globalized && linked_sym.name != orig_sym.name)
1922+
|| (!is_globalized
1923+
&& (linked_sym.name != orig_sym.name
1924+
&& (args.full || !are_local_anonymous_names_similar(linked_sym, orig_sym))))
18951925
{
18961926
log::info!(
18971927
"Changing name of {} (type {:?}) to {}",

0 commit comments

Comments
 (0)