2022-03-14 23:09:25 +00:00
|
|
|
#include <libreborn/elf.h>
|
|
|
|
|
2022-05-30 02:54:57 +00:00
|
|
|
// Find And Iterate Over All Segments In Current Binary
|
|
|
|
typedef struct {
|
|
|
|
segment_callback_t callback;
|
|
|
|
void *data;
|
|
|
|
} dl_iterate_callback_data;
|
|
|
|
static int dl_iterate_callback(struct dl_phdr_info *info, __attribute__((unused)) size_t size, void *data) {
|
|
|
|
dl_iterate_callback_data *callback_data = (dl_iterate_callback_data *) data;
|
|
|
|
// Only Search Current Program
|
|
|
|
if (strcmp(info->dlpi_name, "") == 0) {
|
|
|
|
for (int i = 0; i < info->dlpi_phnum; i++) {
|
|
|
|
// Only Executable Segemnts
|
|
|
|
if (info->dlpi_phdr[i].p_type == PT_LOAD && (info->dlpi_phdr[i].p_flags & PF_X) != 0) {
|
|
|
|
// Callback
|
|
|
|
(*callback_data->callback)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr, info->dlpi_phdr[i].p_memsz, callback_data->data);
|
|
|
|
}
|
2022-03-14 23:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-30 02:54:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void iterate_segments(segment_callback_t callback, void *data) {
|
|
|
|
dl_iterate_callback_data callback_data = {
|
|
|
|
.callback = callback,
|
|
|
|
.data = data
|
|
|
|
};
|
|
|
|
dl_iterate_phdr(dl_iterate_callback, (void *) &callback_data);
|
2022-03-14 23:09:25 +00:00
|
|
|
}
|