MCPcopy Create free account
hub / github.com/CodingGay/BlackDex / xh_elf_replace_function

Function xh_elf_replace_function

Bcore/src/main/cpp/xhook/xh_elf.c:448–497  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

446}
447
448static int xh_elf_replace_function(xh_elf_t *self, const char *symbol, ElfW(Addr) addr, void *new_func, void **old_func)
449{
450 void *old_addr;
451 unsigned int old_prot = 0;
452 unsigned int need_prot = PROT_READ | PROT_WRITE;
453 int r;
454
455 //already replaced?
456 //here we assume that we always have read permission, is this a problem?
457 if(*(void **)addr == new_func) return 0;
458
459 //get old prot
460 if(0 != (r = xh_util_get_addr_protect(addr, self->pathname, &old_prot)))
461 {
462 XH_LOG_ERROR("get addr prot failed. ret: %d", r);
463 return r;
464 }
465
466 if(old_prot != need_prot)
467 {
468 //set new prot
469 if(0 != (r = xh_util_set_addr_protect(addr, need_prot)))
470 {
471 XH_LOG_ERROR("set addr prot failed. ret: %d", r);
472 return r;
473 }
474 }
475
476 //save old func
477 old_addr = *(void **)addr;
478 if(NULL != old_func) *old_func = old_addr;
479
480 //replace func
481 *(void **)addr = new_func; //segmentation fault sometimes
482
483 if(old_prot != need_prot)
484 {
485 //restore the old prot
486 if(0 != (r = xh_util_set_addr_protect(addr, old_prot)))
487 {
488 XH_LOG_WARN("restore addr prot failed. ret: %d", r);
489 }
490 }
491
492 //clear cache
493 xh_util_flush_instruction_cache(addr);
494
495 XH_LOG_INFO("XH_HK_OK %p: %p -> %p %s %s\n", (void *)addr, old_addr, new_func, symbol, self->pathname);
496 return 0;
497}
498
499static int xh_elf_check(xh_elf_t *self)
500{

Callers 1

Calls 3

xh_util_get_addr_protectFunction · 0.85
xh_util_set_addr_protectFunction · 0.85

Tested by

no test coverage detected