Skip to content

Commit

Permalink
bridgehook: fix finding symbols in dyld shared cache
Browse files Browse the repository at this point in the history
  • Loading branch information
asdfugil committed Jul 16, 2024
1 parent b14d412 commit 8082401
Showing 1 changed file with 9 additions and 27 deletions.
36 changes: 9 additions & 27 deletions src/bridgehook/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef void* MSImageRef;

int DobbyHook(void *address, void *fake_func, void **out_origin_func);
int DobbyCodePatch(void *address, uint8_t *buffer, uint32_t buffer_size);
void *DobbySymbolResolver(const char *image_name, const char *symbol_name_pattern);

BH_EXPORT
void MSHookFunction(void *address, void *fake_func, void **out_origin_func) {
Expand Down Expand Up @@ -56,36 +57,17 @@ void MSCloseImage(const char* file) {

BH_EXPORT
void *MSFindSymbol(MSImageRef image, const char *name) {
void* buf = (char*)image;

struct load_command *after_header = buf + sizeof(struct mach_header_64);
struct mach_header_64 *header = buf;
struct symtab_command *symtab_cmd = NULL;

for (uint32_t i = 0; i < header->ncmds; i++) {
if (after_header->cmd == LC_SYMTAB) {
symtab_cmd = (struct symtab_command *) after_header;
uint32_t file_index = 0;
for (uint32_t i = 0; i < _dyld_image_count(); i++) {
if (image == _dyld_get_image_header(i)) {
file_index = i;
break;
}

after_header = (struct load_command *) ((char *) after_header + after_header->cmdsize);
}

if (!symtab_cmd) return NULL;

struct nlist_64 *symtab = buf + symtab_cmd->symoff;
char *strtab = buf + symtab_cmd->stroff;

for (uint32_t i = 0; i < symtab_cmd->nsyms; i++) {
struct nlist_64 *symbol_nlist = symtab + i;
char *sym_name = strtab + symbol_nlist->n_un.n_strx;

if (strcmp(sym_name, name) == 0) {
return (buf + symbol_nlist->n_value);
}
}

return NULL;
if (file_index)
return DobbySymbolResolver(_dyld_get_image_name(file_index), name);
else
return NULL;
}

BH_EXPORT
Expand Down

0 comments on commit 8082401

Please sign in to comment.