Skip to content

Commit 4798494

Browse files
committed
Normalize MWCC class unique IDs on the demangled name
1 parent fba10a6 commit 4798494

1 file changed

Lines changed: 70 additions & 8 deletions

File tree

‎objdiff-core/src/obj/read.rs‎

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,40 @@ fn map_section_kind(section: &object::Section) -> SectionKind {
4040
}
4141
}
4242

43+
/// Normalize Metrowerks class names that are suffixed with a unique ID and the source filename.
44+
/// This is done on the demangled name as the mangled names include the length of the name before
45+
/// the name, which varies depending on the length of the unique ID.
46+
fn normalize_mwcc_unique_id_with_filename(demangled_name: &str) -> Option<String> {
47+
const DUMMY_UNIQUE_ID: &str = "$0000";
48+
let mut parts = Vec::new();
49+
let mut next = demangled_name;
50+
while let Some((prefix, suffix)) = next.split_once("$")
51+
&& let Some(idx) = suffix.chars().position(|c| !c.is_numeric())
52+
&& idx > 0
53+
{
54+
parts.push(prefix);
55+
next = &suffix[idx..];
56+
}
57+
parts.push(next);
58+
if parts.len() >= 2 {
59+
return Some(parts.join(DUMMY_UNIQUE_ID));
60+
}
61+
None
62+
}
63+
4364
/// Check if a symbol's name is partially compiler-generated, and if so normalize it for pairing.
4465
/// e.g. symbol$1234 and symbol$2345 will both be replaced with symbol$0000 internally.
45-
fn get_normalized_symbol_name(name: &str) -> Option<String> {
66+
fn get_normalized_symbol_name(name: &str, demangled_name: &Option<String>) -> Option<String> {
4667
const DUMMY_UNIQUE_ID: &str = "0000";
4768
const DUMMY_UNIQUE_MSVC_ID: &str = "00000000";
48-
if let Some((prefix, suffix)) = name.split_once("@class$")
69+
if let Some(demangled_name) = demangled_name
70+
&& let Some((_, suffix)) = name.split_once("$")
4971
&& let Some(idx) = suffix.chars().position(|c| !c.is_numeric())
5072
&& idx > 0
5173
{
52-
// Match Metrowerks anonymous class symbol names, ignoring the unique ID.
53-
// e.g. __dt__Q29dCamera_c23@class$3665d_camera_cppFv
54-
// and: __dt__Q29dCamera_c23@class$1727d_camera_cppFv
55-
let suffix = &suffix[idx..];
56-
Some(format!("{prefix}@class${DUMMY_UNIQUE_ID}{suffix}"))
74+
// Match Metrowerks symbols that have a class name, a unique ID, and the source filename.
75+
// Unlike the other normalizations, this is done on the demangled name.
76+
normalize_mwcc_unique_id_with_filename(demangled_name)
5777
} else if let Some((prefix, suffix)) = name.split_once('$')
5878
&& suffix.chars().all(char::is_numeric)
5979
{
@@ -173,7 +193,7 @@ fn map_symbol(
173193
.and_then(|m| m.virtual_addresses.as_ref())
174194
.and_then(|v| v.get(symbol.index().0).cloned());
175195
let section = symbol.section_index().and_then(|i| section_indices.get(i.0).copied());
176-
let normalized_name = get_normalized_symbol_name(&name);
196+
let normalized_name = get_normalized_symbol_name(&name, &demangled_name);
177197
if is_symbol_name_compiler_generated(&name) {
178198
flags |= SymbolFlag::CompilerGenerated;
179199
}
@@ -1204,6 +1224,48 @@ fn parse_mw_comment_syms(obj_file: &object::File) -> Result<Option<Vec<CommentSy
12041224
#[cfg(test)]
12051225
mod test {
12061226
use super::*;
1227+
use crate::diff::Demangler;
1228+
1229+
#[test]
1230+
fn test_get_normalized_symbol_name() {
1231+
// Metrowerks in-function static variable.
1232+
let left = "eff_scale$24884";
1233+
let right = "eff_scale$21920";
1234+
assert_eq!(
1235+
get_normalized_symbol_name(&left, &Demangler::Codewarrior.demangle(left)),
1236+
get_normalized_symbol_name(&right, &Demangler::Codewarrior.demangle(right))
1237+
);
1238+
1239+
// Metrowerks anonymous class. Uses @class prefix, unique ID, then filename suffix.
1240+
let left = "__dt__Q29dCamera_c23@class$3665d_camera_cppFv";
1241+
let right = "__dt__Q29dCamera_c23@class$1727d_camera_cppFv";
1242+
assert_eq!(
1243+
get_normalized_symbol_name(&left, &Demangler::Codewarrior.demangle(left)),
1244+
get_normalized_symbol_name(&right, &Demangler::Codewarrior.demangle(right))
1245+
);
1246+
// Metrowerks class defined in function body. Uses class name prefix, unique ID, then filename suffix.
1247+
let left = "makeDL__Q219TMBindShadowManager26TSetup1$2172ShadowUtil_cppFv";
1248+
let right = "makeDL__Q219TMBindShadowManager25TSetup1$874ShadowUtil_cppFv";
1249+
assert_eq!(
1250+
get_normalized_symbol_name(&left, &Demangler::Codewarrior.demangle(left)),
1251+
get_normalized_symbol_name(&right, &Demangler::Codewarrior.demangle(right))
1252+
);
1253+
// Metrowerks class defined in function body, but the unique ID appears again within the function parameters.
1254+
let left = "__as__Q210daB_ZANT_c31dZantSph_c$132966d_a_b_zant_cppFRCQ210daB_ZANT_c31dZantSph_c$132966d_a_b_zant_cpp";
1255+
let right = "__as__Q210daB_ZANT_c30dZantSph_c$55068d_a_b_zant_cppFRCQ210daB_ZANT_c30dZantSph_c$55068d_a_b_zant_cpp";
1256+
assert_eq!(
1257+
get_normalized_symbol_name(&left, &Demangler::Codewarrior.demangle(left)),
1258+
get_normalized_symbol_name(&right, &Demangler::Codewarrior.demangle(right))
1259+
);
1260+
1261+
// MSVC anonymous class.
1262+
let left = "?CheckContextOr@?A0x24773155@@YA_NPBVDataArray@@@Z";
1263+
let right = "?CheckContextOr@?A0xddf6240c@@YA_NPBVDataArray@@@Z";
1264+
assert_eq!(
1265+
get_normalized_symbol_name(&left, &Demangler::Codewarrior.demangle(left)),
1266+
get_normalized_symbol_name(&right, &Demangler::Codewarrior.demangle(right))
1267+
);
1268+
}
12071269

12081270
#[test]
12091271
fn test_combine_sections() {

0 commit comments

Comments
 (0)