Skip to content

Commit b5c510a

Browse files
committed
macho get_function_offset read function_name len
1 parent 63ca048 commit b5c510a

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/file_format/macho.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Support for parsing MachO files
22
3-
use crate::{Process, string::ArrayCString};
3+
use crate::Process;
44

55
use core::mem;
66

@@ -57,9 +57,10 @@ pub fn is_64_bit(process: &Process) -> Option<bool> {
5757
}
5858

5959
/// Finds the offset of a function in the bytes of a MachO file.
60-
pub fn get_function_offset<const N: usize>(macho_bytes: &[u8], function_name: &str) -> Option<u32> {
60+
pub fn get_function_offset(macho_bytes: &[u8], function_name: &[u8]) -> Option<u32> {
6161
let macho_offsets = MachOFormatOffsets::new();
6262
let number_of_commands: u32 = slice_read(macho_bytes, macho_offsets.number_of_commands).ok()?;
63+
let function_name_len = function_name.len();
6364

6465
let mut offset_to_next_command: usize = macho_offsets.load_commands as usize;
6566
for _i in 0..number_of_commands {
@@ -72,9 +73,10 @@ pub fn get_function_offset<const N: usize>(macho_bytes: &[u8], function_name: &s
7273

7374
for j in 0..(number_of_symbols as usize) {
7475
let symbol_name_offset: u32 = slice_read(macho_bytes, symbol_table_offset as usize + (j * macho_offsets.size_of_nlist_item)).ok()?;
75-
let symbol_name: ArrayCString<N> = slice_read(macho_bytes, (string_table_offset + symbol_name_offset) as usize).ok()?;
76+
let string_offset = string_table_offset as usize + symbol_name_offset as usize;
77+
let symbol_name: &[u8] = &macho_bytes[string_offset..(string_offset + function_name_len + 1)];
7678

77-
if symbol_name.matches(function_name) {
79+
if symbol_name[function_name_len] == 0 && symbol_name.starts_with(function_name) {
7880
return Some(slice_read(macho_bytes, symbol_table_offset as usize + (j * macho_offsets.size_of_nlist_item) + macho_offsets.nlist_value).ok()?);
7981
}
8082
}

src/game_engine/unity/mono.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ impl Module {
147147
let mono_module_path = contents_path.join("Frameworks").join("libmonobdwgc-2.0.dylib");
148148
let mono_module_bytes = file_read_all_bytes(mono_module_path).ok()?;
149149

150-
const MONO_ASSEMBLY_FOREACH: &str = "_mono_assembly_foreach";
151-
const MONO_ASSEMBLY_FOREACH_LEN_P1: usize = MONO_ASSEMBLY_FOREACH.len() + 1;
152-
let mono_assembly_foreach_offset: u32 = macho::get_function_offset::<MONO_ASSEMBLY_FOREACH_LEN_P1>(&mono_module_bytes, MONO_ASSEMBLY_FOREACH)?;
150+
let mono_assembly_foreach_offset: u32 = macho::get_function_offset(&mono_module_bytes, b"_mono_assembly_foreach")?;
153151

154152
let function_array: [u8; 0x100] = macho::slice_read(&mono_module_bytes, mono_assembly_foreach_offset as usize).ok()?;
155153
let sig_function_array: Signature<0x100> = Signature::Simple(function_array);

0 commit comments

Comments
 (0)