From 9fc56d847fd2764bcfba4368697eab4309384546 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Thu, 7 Nov 2024 08:44:24 -0700 Subject: [PATCH] Add `rename` field to extract configuration Allows renaming, for example, local statics from `test$1234` to `test` for inclusion in the source function. --- src/cmd/dol.rs | 7 ++++++- src/util/bin2c.rs | 23 +++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/cmd/dol.rs b/src/cmd/dol.rs index d70e45a..f21e2bb 100644 --- a/src/cmd/dol.rs +++ b/src/cmd/dol.rs @@ -296,6 +296,8 @@ pub struct ModuleConfig { pub struct ExtractConfig { /// The name of the symbol to extract. pub symbol: String, + /// Optionally rename the output symbol. (e.g. symbol$1234 -> symbol) + pub rename: Option, /// If specified, the symbol's data will be extracted to the given file. /// Path is relative to `out_dir/bin`. #[serde(with = "unix_path_serde_option", default, skip_serializing_if = "Option::is_none")] @@ -385,6 +387,7 @@ pub struct OutputModule { #[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct OutputExtract { pub symbol: String, + pub rename: Option, #[serde(with = "unix_path_serde_option")] pub binary: Option, #[serde(with = "unix_path_serde_option")] @@ -1014,7 +1017,8 @@ fn split_write_obj( if header_kind != HeaderKind::None { if let Some(header) = &extract.header { - let header_string = bin2c(symbol, section, data, header_kind); + let header_string = + bin2c(symbol, section, data, header_kind, extract.rename.as_deref()); let out_path = base_dir.join("include").join(header.with_encoding()); if let Some(parent) = out_path.parent() { DirBuilder::new().recursive(true).create(parent)?; @@ -1026,6 +1030,7 @@ fn split_write_obj( // Copy to output config out_config.extract.push(OutputExtract { symbol: symbol.name.clone(), + rename: extract.rename.clone(), binary: extract.binary.clone(), header: extract.header.clone(), header_type: header_kind.to_string(), diff --git a/src/util/bin2c.rs b/src/util/bin2c.rs index 601b441..ce5cea7 100644 --- a/src/util/bin2c.rs +++ b/src/util/bin2c.rs @@ -50,15 +50,26 @@ impl fmt::Display for HeaderKind { } /// Converts a binary blob into a C array. -pub fn bin2c(symbol: &ObjSymbol, section: &ObjSection, data: &[u8], kind: HeaderKind) -> String { +pub fn bin2c( + symbol: &ObjSymbol, + section: &ObjSection, + data: &[u8], + kind: HeaderKind, + rename: Option<&str>, +) -> String { match kind { HeaderKind::None => String::new(), - HeaderKind::Symbol => bin2c_symbol(symbol, section, data), + HeaderKind::Symbol => bin2c_symbol(symbol, section, data, rename), HeaderKind::Raw => bin2c_raw(data), } } -fn bin2c_symbol(symbol: &ObjSymbol, section: &ObjSection, data: &[u8]) -> String { +fn bin2c_symbol( + symbol: &ObjSymbol, + section: &ObjSection, + data: &[u8], + rename: Option<&str>, +) -> String { let mut output = String::new(); output.push_str(PROLOGUE); output.push_str(&format!( @@ -72,7 +83,11 @@ fn bin2c_symbol(symbol: &ObjSymbol, section: &ObjSection, data: &[u8]) -> String output.push_str("const "); } output.push_str("unsigned char "); - output.push_str(symbol.demangled_name.as_deref().unwrap_or(symbol.name.as_str())); + if let Some(rename) = rename { + output.push_str(rename); + } else { + output.push_str(symbol.demangled_name.as_deref().unwrap_or(symbol.name.as_str())); + } output.push_str(&format!("[] ATTRIBUTE_ALIGN({}) = {{", symbol.align.unwrap_or(4))); for (i, byte) in data.iter().enumerate() { if i % 16 == 0 {