11use std:: {
22 cmp:: min,
33 collections:: { BTreeMap , HashMap , btree_map:: Entry , hash_map} ,
4+ ffi:: CStr ,
45 fs,
56 fs:: DirBuilder ,
67 io:: { Cursor , Seek , Write } ,
@@ -13,6 +14,7 @@ use anyhow::{Context, Result, anyhow, bail};
1314use argp:: FromArgs ;
1415use cwdemangle:: demangle;
1516use itertools:: Itertools ;
17+ use object:: ObjectSection ;
1618use rayon:: prelude:: * ;
1719use serde:: { Deserialize , Serialize } ;
1820use tracing:: { debug, info, info_span} ;
@@ -43,7 +45,7 @@ use crate::{
4345 comment:: MWComment ,
4446 config:: {
4547 SectionAddressRef , apply_splits_file, apply_symbols_file, is_auto_symbol,
46- signed_hex_serde, write_splits_file, write_symbols_file,
48+ section_addr_ref_from_str , signed_hex_serde, write_splits_file, write_symbols_file,
4749 } ,
4850 dep:: DepFile ,
4951 diff:: { calc_diff_ranges, print_diff, process_code} ,
@@ -80,6 +82,7 @@ enum SubCommand {
8082 Diff ( DiffArgs ) ,
8183 Apply ( ApplyArgs ) ,
8284 Config ( ConfigArgs ) ,
85+ Strings ( StringsArgs ) ,
8386}
8487
8588#[ derive( FromArgs , PartialEq , Eq , Debug ) ]
@@ -139,6 +142,21 @@ pub struct ApplyArgs {
139142 full : bool ,
140143}
141144
145+ #[ derive( FromArgs , PartialEq , Eq , Debug ) ]
146+ /// Applies updated symbols from a linked ELF to the project configuration.
147+ #[ argp( subcommand, name = "strings" ) ]
148+ pub struct StringsArgs {
149+ #[ argp( positional, from_str_fn( native_path) ) ]
150+ /// linked ELF
151+ elf_file : Utf8NativePathBuf ,
152+ #[ argp( positional, from_str_fn( section_addr_ref_from_str) ) ]
153+ /// position of string table to split
154+ address : SectionAddressRef ,
155+ /// Max count strings to split
156+ #[ argp( option, default = "20" ) ]
157+ count : u32 ,
158+ }
159+
142160#[ derive( FromArgs , PartialEq , Eq , Debug ) ]
143161/// Generates a project configuration file from a DOL (& RELs).
144162#[ argp( subcommand, name = "config" ) ]
@@ -468,6 +486,7 @@ pub fn run(args: Args) -> Result<()> {
468486 SubCommand :: Diff ( c_args) => diff ( c_args) ,
469487 SubCommand :: Apply ( c_args) => apply ( c_args) ,
470488 SubCommand :: Config ( c_args) => config ( c_args) ,
489+ SubCommand :: Strings ( c_args) => strings ( c_args) ,
471490 }
472491}
473492
@@ -525,19 +544,19 @@ fn apply_selfile(obj: &mut ObjInfo, buf: &[u8]) -> Result<()> {
525544 if let Some ( ( existing_symbol_idx, existing_symbol) ) = existing_symbol {
526545 log:: debug!( "Mapping symbol {} to {}" , symbol. name, existing_symbol. name) ;
527546 obj. symbols . replace ( existing_symbol_idx, ObjSymbol {
528- name : symbol. name . clone ( ) ,
529- demangled_name : symbol. demangled_name . clone ( ) ,
530- address : address as u64 ,
531- section,
532- size : existing_symbol. size ,
533- size_known : existing_symbol. size_known ,
534- flags : ObjSymbolFlagSet ( existing_symbol. flags . 0 | ObjSymbolFlags :: Exported ) ,
535- kind : existing_symbol. kind ,
536- align : existing_symbol. align ,
537- data_kind : existing_symbol. data_kind ,
538- name_hash : existing_symbol. name_hash ,
539- demangled_name_hash : existing_symbol. demangled_name_hash ,
540- } ) ?;
547+ name : symbol. name . clone ( ) ,
548+ demangled_name : symbol. demangled_name . clone ( ) ,
549+ address : address as u64 ,
550+ section,
551+ size : existing_symbol. size ,
552+ size_known : existing_symbol. size_known ,
553+ flags : ObjSymbolFlagSet ( existing_symbol. flags . 0 | ObjSymbolFlags :: Exported ) ,
554+ kind : existing_symbol. kind ,
555+ align : existing_symbol. align ,
556+ data_kind : existing_symbol. data_kind ,
557+ name_hash : existing_symbol. name_hash ,
558+ demangled_name_hash : existing_symbol. demangled_name_hash ,
559+ } ) ?;
541560 } else {
542561 log:: debug!( "Creating symbol {} at {:#010X}" , symbol. name, address) ;
543562 obj. symbols . add (
@@ -2143,10 +2162,10 @@ fn config(args: ConfigArgs) -> Result<()> {
21432162 let header = process_rel_header ( & mut entry) ?;
21442163 entry. rewind ( ) ?;
21452164 modules. push ( ( header. module_id , ModuleConfig {
2146- object : path. with_unix_encoding ( ) ,
2147- hash : Some ( file_sha1_string ( & mut entry) ?) ,
2148- ..Default :: default ( )
2149- } ) ) ;
2165+ object : path. with_unix_encoding ( ) ,
2166+ hash : Some ( file_sha1_string ( & mut entry) ?) ,
2167+ ..Default :: default ( )
2168+ } ) ) ;
21502169 }
21512170 "sel" => {
21522171 config. selfile = Some ( path. with_unix_encoding ( ) ) ;
@@ -2218,10 +2237,10 @@ fn apply_add_relocations(obj: &mut ObjInfo, relocations: &[AddRelocationConfig])
22182237 }
22192238 } ;
22202239 obj. sections [ section] . relocations . replace ( address, ObjReloc {
2221- kind : reloc. kind ,
2222- target_symbol,
2223- addend : reloc. addend ,
2224- module : None ,
2240+ kind : reloc. kind ,
2241+ target_symbol,
2242+ addend : reloc. addend ,
2243+ module : None ,
22252244 } ) ;
22262245 }
22272246 Ok ( ( ) )
@@ -2398,6 +2417,39 @@ fn extracted_path(target_dir: &Utf8NativePath, path: &Utf8UnixPath) -> Utf8Nativ
23982417 target_path
23992418}
24002419
2420+ fn strings ( args : StringsArgs ) -> Result < ( ) > {
2421+ let mut file = open_file ( & args. elf_file , true ) ?;
2422+ let data = file. map ( ) ?;
2423+
2424+ let obj_file = object:: read:: File :: parse ( data) ?;
2425+ let section = args. address . resolve_in_file ( & obj_file) ?;
2426+ let section_name = section. name ( ) ?;
2427+
2428+ let mut relative = ( args. address . address as u64 - section. address ( ) ) as usize ;
2429+ let section_data = section. data ( ) ?;
2430+
2431+ for _ in 0 ..args. count {
2432+ let symbol_data = & section_data[ relative..] ;
2433+ let str = match CStr :: from_bytes_until_nul ( symbol_data) {
2434+ Ok ( v) => v,
2435+ Err ( _) => break ,
2436+ } ;
2437+
2438+ let size = str. count_bytes ( ) + 1 ;
2439+
2440+ println ! (
2441+ "str_{0:08X} = {1}:0x{0:08X}; // type:object size:0x{2:X} scope:local data:string" ,
2442+ relative as u64 + section. address( ) ,
2443+ section_name,
2444+ size
2445+ ) ;
2446+
2447+ relative += size;
2448+ }
2449+
2450+ Ok ( ( ) )
2451+ }
2452+
24012453#[ cfg( test) ]
24022454mod test {
24032455 use super :: * ;
0 commit comments