Commit a8f6db91 authored by Teng Qin's avatar Teng Qin

Add a helper to loop over ELF load sections

parent a0dd71ec
......@@ -470,6 +470,41 @@ int bcc_elf_foreach_sym(const char *path, bcc_elf_symcb callback,
path, callback, (struct bcc_symbol_option*)option, payload, 0);
}
int bcc_elf_foreach_load_section(const char *path,
bcc_elf_load_sectioncb callback,
void *payload) {
Elf *e = NULL;
int fd = -1, err = -1, res;
size_t nhdrs, i;
if (openelf(path, &e, &fd) < 0)
goto exit;
if (elf_getphdrnum(e, &nhdrs) != 0)
goto exit;
GElf_Phdr header;
for (i = 0; i < nhdrs; i++) {
if (!gelf_getphdr(e, (int)i, &header))
continue;
if (header.p_type != PT_LOAD || !(header.p_flags & PF_X))
continue;
res = callback(header.p_vaddr, header.p_memsz, header.p_offset, payload);
if (res < 0) {
err = 1;
goto exit;
}
}
err = 0;
exit:
if (e)
elf_end(e);
if (fd >= 0)
close(fd);
return err;
}
static int loadaddr(Elf *e, uint64_t *addr) {
size_t phnum, i;
......
......@@ -39,13 +39,18 @@ typedef void (*bcc_elf_probecb)(const char *, const struct bcc_elf_usdt *,
// Callback returning a negative value indicates to stop the iteration
typedef int (*bcc_elf_symcb)(const char *, uint64_t, uint64_t, void *);
// Segment virtual address, memory size, file offset, payload
// Callback returns a negative value indicates to stop the iteration
// Callback returning a negative value indicates to stop the iteration
typedef int (*bcc_elf_load_sectioncb)(uint64_t, uint64_t, uint64_t, void *);
// Iterate over all USDT probes noted in a binary module
// Returns -1 on error, and 0 on success
int bcc_elf_foreach_usdt(const char *path, bcc_elf_probecb callback,
void *payload);
// Iterate over all executable load sections of an ELF
// Returns -1 on error, 1 if stopped by callback, and 0 on success
int bcc_elf_foreach_load_section(const char *path,
bcc_elf_load_sectioncb callback,
void *payload);
int bcc_elf_loadaddr(const char *path, uint64_t *address);
// Iterate over symbol table of a binary module
// Parameter "option" points to a bcc_symbol_option struct to indicate wheather
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment