| 981 | } |
| 982 | |
| 983 | int xh_elf_hook(xh_elf_t *self, const char *symbol, void *new_func, void **old_func) |
| 984 | { |
| 985 | uint32_t symidx; |
| 986 | void *rel_common; |
| 987 | xh_elf_plain_reloc_iterator_t plain_iter; |
| 988 | xh_elf_packed_reloc_iterator_t packed_iter; |
| 989 | int found; |
| 990 | int r; |
| 991 | |
| 992 | if(NULL == self->pathname) |
| 993 | { |
| 994 | XH_LOG_ERROR("not inited\n"); |
| 995 | return XH_ERRNO_ELFINIT; //not inited? |
| 996 | } |
| 997 | |
| 998 | if(NULL == symbol || NULL == new_func) return XH_ERRNO_INVAL; |
| 999 | |
| 1000 | XH_LOG_INFO("hooking %s in %s\n", symbol, self->pathname); |
| 1001 | |
| 1002 | //find symbol index by symbol name |
| 1003 | if(0 != (r = xh_elf_find_symidx_by_name(self, symbol, &symidx))) return 0; |
| 1004 | |
| 1005 | //replace for .rel(a).plt |
| 1006 | if(0 != self->relplt) |
| 1007 | { |
| 1008 | xh_elf_plain_reloc_iterator_init(&plain_iter, self->relplt, self->relplt_sz, self->is_use_rela); |
| 1009 | while(NULL != (rel_common = xh_elf_plain_reloc_iterator_next(&plain_iter))) |
| 1010 | { |
| 1011 | if(0 != (r = xh_elf_find_and_replace_func(self, |
| 1012 | (self->is_use_rela ? ".rela.plt" : ".rel.plt"), 1, |
| 1013 | symbol, new_func, old_func, |
| 1014 | symidx, rel_common, &found))) return r; |
| 1015 | if(found) break; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | //replace for .rel(a).dyn |
| 1020 | if(0 != self->reldyn) |
| 1021 | { |
| 1022 | xh_elf_plain_reloc_iterator_init(&plain_iter, self->reldyn, self->reldyn_sz, self->is_use_rela); |
| 1023 | while(NULL != (rel_common = xh_elf_plain_reloc_iterator_next(&plain_iter))) |
| 1024 | { |
| 1025 | if(0 != (r = xh_elf_find_and_replace_func(self, |
| 1026 | (self->is_use_rela ? ".rela.dyn" : ".rel.dyn"), 0, |
| 1027 | symbol, new_func, old_func, |
| 1028 | symidx, rel_common, NULL))) return r; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | //replace for .rel(a).android |
| 1033 | if(0 != self->relandroid) |
| 1034 | { |
| 1035 | xh_elf_packed_reloc_iterator_init(&packed_iter, self->relandroid, self->relandroid_sz, self->is_use_rela); |
| 1036 | while(NULL != (rel_common = xh_elf_packed_reloc_iterator_next(&packed_iter))) |
| 1037 | { |
| 1038 | if(0 != (r = xh_elf_find_and_replace_func(self, |
| 1039 | (self->is_use_rela ? ".rela.android" : ".rel.android"), 0, |
| 1040 | symbol, new_func, old_func, |
no test coverage detected