1
0

proj/intercept.c: Parse readelf output correctly

This commit is contained in:
2025-05-07 11:52:53 +02:00
parent 478b826c57
commit 2ec7c9e7eb

View File

@@ -890,6 +890,9 @@ static int iter_lib_debug(struct dl_phdr_info *info, size_t size, void *data) {
name = name_buf; name = name_buf;
} }
char cmd_buf[512];
snprintf(cmd_buf, sizeof(cmd_buf), "readelf --debug-dump=decodedline '%s' | grep 0x | sort -k3", name_buf);
int fds[2]; int fds[2];
__real_pipe(fds); __real_pipe(fds);
pid_t pid = __real_fork(); pid_t pid = __real_fork();
@@ -902,13 +905,13 @@ static int iter_lib_debug(struct dl_phdr_info *info, size_t size, void *data) {
__real_close(STDERR_FILENO); __real_close(STDERR_FILENO);
__real_dup2(fds[1], STDOUT_FILENO); __real_dup2(fds[1], STDOUT_FILENO);
__real_close(fds[1]); __real_close(fds[1]);
__real_execle("/bin/readelf", "readelf", "--debug-dump=decodedline", name, NULL, NULL); __real_execle("/bin/sh", "sh", "-c", cmd_buf, NULL, NULL);
default: default:
// parent // parent
__real_close(fds[1]); // close write end __real_close(fds[1]); // close write end
} }
char state = 0; char state = 'A';
char buf[1024]; char buf[1024];
char line[3][256] = {0}; char line[3][256] = {0};
int n = 0, n_entries = 0; int n = 0, n_entries = 0;
@@ -916,17 +919,7 @@ static int iter_lib_debug(struct dl_phdr_info *info, size_t size, void *data) {
for (ssize_t num = -2; (num != -1 && num != 0) || (num == -1 && errno == EINTR); num = __real_read(fds[0], buf, sizeof(buf))) { for (ssize_t num = -2; (num != -1 && num != 0) || (num == -1 && errno == EINTR); num = __real_read(fds[0], buf, sizeof(buf))) {
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
char ch = buf[i]; char ch = buf[i];
if (state == 0 && ch == 'S') { if (state == 'A' || state == 'B' || state == 'C') {
state = 'S';
} else if (state == 'S' && ch == 't') {
state = 't';
} else if (state == 't' && ch == 'm') {
state = 'm';
} else if (state == 'm' && ch == 't') {
state = 'T';
} else if (state == 'T' && ch == '\n') {
state = 'A';
} else if (state == 'A' || state == 'B' || state == 'C') {
// file name | line number | starting address // file name | line number | starting address
if (ch == ' ') { if (ch == ' ') {
if (n == 0) if (n == 0)