Strategies for finding binary dependencies
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add print_symbols functionality

+295 -3
+191
Cargo.lock
··· 57 57 version = "0.1.0" 58 58 dependencies = [ 59 59 "clap", 60 + "comfy-table", 61 + "elf", 60 62 "glob", 61 63 ] 64 + 65 + [[package]] 66 + name = "bitflags" 67 + version = "2.10.0" 68 + source = "registry+https://github.com/rust-lang/crates.io-index" 69 + checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" 70 + 71 + [[package]] 72 + name = "cfg-if" 73 + version = "1.0.4" 74 + source = "registry+https://github.com/rust-lang/crates.io-index" 75 + checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" 62 76 63 77 [[package]] 64 78 name = "clap" ··· 107 121 checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" 108 122 109 123 [[package]] 124 + name = "comfy-table" 125 + version = "7.2.2" 126 + source = "registry+https://github.com/rust-lang/crates.io-index" 127 + checksum = "958c5d6ecf1f214b4c2bbbbf6ab9523a864bd136dcf71a7e8904799acfe1ad47" 128 + dependencies = [ 129 + "crossterm", 130 + "unicode-segmentation", 131 + "unicode-width", 132 + ] 133 + 134 + [[package]] 135 + name = "crossterm" 136 + version = "0.29.0" 137 + source = "registry+https://github.com/rust-lang/crates.io-index" 138 + checksum = "d8b9f2e4c67f833b660cdb0a3523065869fb35570177239812ed4c905aeff87b" 139 + dependencies = [ 140 + "bitflags", 141 + "crossterm_winapi", 142 + "document-features", 143 + "parking_lot", 144 + "rustix", 145 + "winapi", 146 + ] 147 + 148 + [[package]] 149 + name = "crossterm_winapi" 150 + version = "0.9.1" 151 + source = "registry+https://github.com/rust-lang/crates.io-index" 152 + checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" 153 + dependencies = [ 154 + "winapi", 155 + ] 156 + 157 + [[package]] 158 + name = "document-features" 159 + version = "0.2.12" 160 + source = "registry+https://github.com/rust-lang/crates.io-index" 161 + checksum = "d4b8a88685455ed29a21542a33abd9cb6510b6b129abadabdcef0f4c55bc8f61" 162 + dependencies = [ 163 + "litrs", 164 + ] 165 + 166 + [[package]] 167 + name = "elf" 168 + version = "0.8.0" 169 + source = "registry+https://github.com/rust-lang/crates.io-index" 170 + checksum = "55dd888a213fc57e957abf2aa305ee3e8a28dbe05687a251f33b637cd46b0070" 171 + 172 + [[package]] 173 + name = "errno" 174 + version = "0.3.14" 175 + source = "registry+https://github.com/rust-lang/crates.io-index" 176 + checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" 177 + dependencies = [ 178 + "libc", 179 + "windows-sys", 180 + ] 181 + 182 + [[package]] 110 183 name = "glob" 111 184 version = "0.3.3" 112 185 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 125 198 checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" 126 199 127 200 [[package]] 201 + name = "libc" 202 + version = "0.2.180" 203 + source = "registry+https://github.com/rust-lang/crates.io-index" 204 + checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" 205 + 206 + [[package]] 207 + name = "linux-raw-sys" 208 + version = "0.11.0" 209 + source = "registry+https://github.com/rust-lang/crates.io-index" 210 + checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" 211 + 212 + [[package]] 213 + name = "litrs" 214 + version = "1.0.0" 215 + source = "registry+https://github.com/rust-lang/crates.io-index" 216 + checksum = "11d3d7f243d5c5a8b9bb5d6dd2b1602c0cb0b9db1621bafc7ed66e35ff9fe092" 217 + 218 + [[package]] 219 + name = "lock_api" 220 + version = "0.4.14" 221 + source = "registry+https://github.com/rust-lang/crates.io-index" 222 + checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" 223 + dependencies = [ 224 + "scopeguard", 225 + ] 226 + 227 + [[package]] 128 228 name = "once_cell_polyfill" 129 229 version = "1.70.2" 130 230 source = "registry+https://github.com/rust-lang/crates.io-index" 131 231 checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" 132 232 133 233 [[package]] 234 + name = "parking_lot" 235 + version = "0.12.5" 236 + source = "registry+https://github.com/rust-lang/crates.io-index" 237 + checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" 238 + dependencies = [ 239 + "lock_api", 240 + "parking_lot_core", 241 + ] 242 + 243 + [[package]] 244 + name = "parking_lot_core" 245 + version = "0.9.12" 246 + source = "registry+https://github.com/rust-lang/crates.io-index" 247 + checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" 248 + dependencies = [ 249 + "cfg-if", 250 + "libc", 251 + "redox_syscall", 252 + "smallvec", 253 + "windows-link", 254 + ] 255 + 256 + [[package]] 134 257 name = "proc-macro2" 135 258 version = "1.0.104" 136 259 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 149 272 ] 150 273 151 274 [[package]] 275 + name = "redox_syscall" 276 + version = "0.5.18" 277 + source = "registry+https://github.com/rust-lang/crates.io-index" 278 + checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" 279 + dependencies = [ 280 + "bitflags", 281 + ] 282 + 283 + [[package]] 284 + name = "rustix" 285 + version = "1.1.3" 286 + source = "registry+https://github.com/rust-lang/crates.io-index" 287 + checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34" 288 + dependencies = [ 289 + "bitflags", 290 + "errno", 291 + "libc", 292 + "linux-raw-sys", 293 + "windows-sys", 294 + ] 295 + 296 + [[package]] 297 + name = "scopeguard" 298 + version = "1.2.0" 299 + source = "registry+https://github.com/rust-lang/crates.io-index" 300 + checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" 301 + 302 + [[package]] 303 + name = "smallvec" 304 + version = "1.15.1" 305 + source = "registry+https://github.com/rust-lang/crates.io-index" 306 + checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" 307 + 308 + [[package]] 152 309 name = "strsim" 153 310 version = "0.11.1" 154 311 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 172 329 checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" 173 330 174 331 [[package]] 332 + name = "unicode-segmentation" 333 + version = "1.12.0" 334 + source = "registry+https://github.com/rust-lang/crates.io-index" 335 + checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" 336 + 337 + [[package]] 338 + name = "unicode-width" 339 + version = "0.2.2" 340 + source = "registry+https://github.com/rust-lang/crates.io-index" 341 + checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" 342 + 343 + [[package]] 175 344 name = "utf8parse" 176 345 version = "0.2.2" 177 346 source = "registry+https://github.com/rust-lang/crates.io-index" 178 347 checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" 348 + 349 + [[package]] 350 + name = "winapi" 351 + version = "0.3.9" 352 + source = "registry+https://github.com/rust-lang/crates.io-index" 353 + checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 354 + dependencies = [ 355 + "winapi-i686-pc-windows-gnu", 356 + "winapi-x86_64-pc-windows-gnu", 357 + ] 358 + 359 + [[package]] 360 + name = "winapi-i686-pc-windows-gnu" 361 + version = "0.4.0" 362 + source = "registry+https://github.com/rust-lang/crates.io-index" 363 + checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 364 + 365 + [[package]] 366 + name = "winapi-x86_64-pc-windows-gnu" 367 + version = "0.4.0" 368 + source = "registry+https://github.com/rust-lang/crates.io-index" 369 + checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 179 370 180 371 [[package]] 181 372 name = "windows-link"
+2
Cargo.toml
··· 7 7 8 8 [dependencies] 9 9 clap = { version = "4.5.53", features = ["derive"] } 10 + comfy-table = "7.2.2" 11 + elf = "0.8.0" 10 12 glob = "0.3.3"
+102 -3
src/bin/print_symbols.rs
··· 3 3 4 4 use std::ffi::OsString; 5 5 use std::path::Path; 6 + use comfy_table::{Cell, Table}; 6 7 7 8 use clap::Parser; 9 + use elf::ElfStream; 10 + use elf::endian::AnyEndian; 8 11 use glob::glob; 9 12 10 13 /// Print symbols for all ELF files in a directory ··· 16 19 dir: String, 17 20 } 18 21 22 + /* 23 + From https://github.com/cole14/rust-readelf/blob/419f78ac79d2e5b538ffe28777b54a487d953840/src/rust-readelf.rs 24 + SPDX-SnippetBegin 25 + SPDX-License-Identifier: MIT 26 + SPDX-SnippetCopyrightText: Christopher Cole 27 + */ 28 + fn print_dynamic_symbol_table(elf_file: &mut ElfStream<AnyEndian, std::fs::File>) { 29 + // Get the .dynsym table. If this file doesn't have one, then we're done 30 + let (dynsyms, dynstrs) = match elf_file 31 + .dynamic_symbol_table() 32 + .expect("Failed to get .dynsym and string table") 33 + { 34 + Some(tables) => tables, 35 + None => { 36 + return; 37 + } 38 + }; 39 + 40 + // Parse out all the symbols so that we can look up versions for them if needed. 41 + let symbols: Vec<(String, elf::symbol::Symbol)> = dynsyms 42 + .iter() 43 + .map(|sym| { 44 + ( 45 + dynstrs 46 + .get(sym.st_name as usize) 47 + .expect("Failed to get symbol name") 48 + .to_string(), 49 + sym, 50 + ) 51 + }) 52 + .collect(); 53 + 54 + let vertab = elf_file 55 + .symbol_version_table() 56 + .expect("Failed to parse GNU symbol versions"); 57 + 58 + let mut table = Table::new(); 59 + table.set_header([ 60 + "ndx", 61 + "value", 62 + "size", 63 + "type", 64 + "bind", 65 + "visibility", 66 + "shndx", 67 + "needs version", 68 + "name", 69 + ]); 70 + for (sym_idx, (sym_name, sym)) in symbols.iter().enumerate() { 71 + let needs_name = match &vertab { 72 + Some(vertab) => { 73 + if sym.is_undefined() { 74 + match vertab 75 + .get_requirement(sym_idx) 76 + .expect("Failed to parse symbol requirement") 77 + { 78 + Some(req) => req.name, 79 + None => "None", 80 + } 81 + } else { 82 + "None" 83 + } 84 + } 85 + None => "None", 86 + }; 87 + let cells: Vec<Cell> = vec![ 88 + sym_idx.into(), 89 + format!("{:#x}", sym.st_value).into(), 90 + sym.st_size.into(), 91 + elf::to_str::st_symtype_to_string(sym.st_symtype()).into(), 92 + elf::to_str::st_bind_to_string(sym.st_bind()).into(), 93 + elf::to_str::st_vis_to_string(sym.st_vis()).into(), 94 + sym.st_shndx.into(), 95 + needs_name.into(), 96 + sym_name.into(), 97 + ]; 98 + table.add_row(cells); 99 + } 100 + println!("{table}"); 101 + } 102 + /* SPDX-SnippetEnd */ 103 + 19 104 fn main() { 20 105 let args = Args::parse(); 21 106 22 107 let dir_path = OsString::from(args.dir); 23 108 let dir = Path::new(&dir_path); 24 - let glob_target_path = dir.join("**").join("*.so"); 25 - let glob_target = glob_target_path.to_str().expect("could not convert path to string"); 26 - let glob_res = glob(glob_target).expect("failed to read glob pattern"); 109 + let glob_so_target_path = dir.join("**").join("*.so"); 110 + let glob_so_target = glob_so_target_path 111 + .to_str() 112 + .expect("could not convert path to string"); 113 + let glob_bin_target_path = dir.join("**").join("*.bin"); 114 + let glob_bin_target = glob_bin_target_path 115 + .to_str() 116 + .expect("could not convert path to string"); 117 + let glob_res = glob(glob_so_target).expect("failed to read glob pattern") 118 + .chain(glob(glob_bin_target).expect("failed to read glob pattern")); 27 119 28 120 for filename in glob_res.map_while(Result::ok) { 29 121 println!("{:?}", filename); 122 + 123 + let filepath = std::path::PathBuf::from(filename); 124 + let file_stream = std::fs::File::open(filepath).expect("could not open file"); 125 + let mut elf_file = 126 + ElfStream::<AnyEndian, _>::open_stream(file_stream).expect("could not open ELF stream"); 127 + 128 + print_dynamic_symbol_table(&mut elf_file); 30 129 } 31 130 }