Skip to content

Commit

Permalink
Add plthook_enum_with_prot() (plthook_elf.c and plthook_osx.c)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubo committed Aug 4, 2024
1 parent 5e6fcb7 commit 419ac61
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions plthook.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ int plthook_replace(plthook_t *plthook, const char *funcname, void *funcaddr, vo
void plthook_close(plthook_t *plthook);
const char *plthook_error(void);

/* enumerate entries with memory protection information (bitwise-OR of PROT_READ, PROT_WRITE and PROT_EXEC)
*
* source: plthook_elf.c and plthook_osx.c
*/
int plthook_enum_with_prot(plthook_t *plthook, unsigned int *pos, const char **name_out, void ***addr_out, int *prot);

#ifdef __cplusplus
} /* extern "C" */
#endif
Expand Down
11 changes: 11 additions & 0 deletions plthook_elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -825,12 +825,20 @@ static int check_rel(const plthook_t *plthook, const Elf_Plt_Rel *plt, Elf_Xword
}

int plthook_enum(plthook_t *plthook, unsigned int *pos, const char **name_out, void ***addr_out)
{
return plthook_enum_with_prot(plthook, pos, name_out, addr_out, NULL);
}

int plthook_enum_with_prot(plthook_t *plthook, unsigned int *pos, const char **name_out, void ***addr_out, int *prot)
{
while (*pos < plthook->rela_plt_cnt) {
const Elf_Plt_Rel *plt = plthook->rela_plt + *pos;
int rv = check_rel(plthook, plt, R_JUMP_SLOT, name_out, addr_out);
(*pos)++;
if (rv >= 0) {
if (rv == 0 && prot != NULL) {
*prot = plthook_get_mem_prot(plthook, *addr_out);
}
return rv;
}
}
Expand All @@ -840,6 +848,9 @@ int plthook_enum(plthook_t *plthook, unsigned int *pos, const char **name_out, v
int rv = check_rel(plthook, plt, R_GLOBAL_DATA, name_out, addr_out);
(*pos)++;
if (rv >= 0) {
if (rv == 0 && prot != NULL) {
*prot = plthook_get_mem_prot(plthook, *addr_out);
}
return rv;
}
}
Expand Down
8 changes: 8 additions & 0 deletions plthook_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,11 @@ static int get_mem_prot(plthook_t *plthook, void *addr)
}

int plthook_enum(plthook_t *plthook, unsigned int *pos, const char **name_out, void ***addr_out)
{
return plthook_enum_with_prot(plthook, pos, name_out, addr_out, NULL);
}

int plthook_enum_with_prot(plthook_t *plthook, unsigned int *pos, const char **name_out, void ***addr_out, int *prot)
{
if (*pos >= plthook->num_entries) {
*name_out = NULL;
Expand All @@ -1036,6 +1041,9 @@ int plthook_enum(plthook_t *plthook, unsigned int *pos, const char **name_out, v
*name_out = plthook->entries[*pos].name;
*addr_out = plthook->entries[*pos].addr;
(*pos)++;
if (prot != NULL) {
*prot = get_mem_prot(plthook, *addr_out);
}
return 0;
}

Expand Down

0 comments on commit 419ac61

Please sign in to comment.