diff options
Diffstat (limited to 'source')
| -rw-r--r-- | source/ucli/elf.cc | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/source/ucli/elf.cc b/source/ucli/elf.cc index 25db08b..f742e87 100644 --- a/source/ucli/elf.cc +++ b/source/ucli/elf.cc @@ -266,6 +266,55 @@ static void get_all_symbols(std::set<symbol> &ss, symbol_sections_ctx *si, // } // } +bool is_stripped(const char* path) { + char command[256]; + snprintf(command, sizeof(command), "file %s", path); + + FILE* fp = popen(command, "r"); + if (fp == NULL) { + // handle error + return false; + } + + char output[256]; + fgets(output, sizeof(output), fp); + pclose(fp); + + return strstr(output, "stripped") != NULL; +} + +#define MAX_LINE_LENGTH 1024 +void get_symbol_from_elf_gdb(std::set<symbol> &ss, const char *path) { + FILE *fp; + char cmd[MAX_LINE_LENGTH]; + char line[MAX_LINE_LENGTH]; + // 构建 GDB 命令 + snprintf(cmd, sizeof(cmd), + "gdb -batch -ex \"file %s\" -ex \"info functions\"", path); + + // 执行 GDB 命令并获取输出 + fp = popen(cmd, "r"); + if (fp == NULL) { + perror("popen"); + return ; + } + + // 读取并解析 GDB 的输出 + while (fgets(line, sizeof(line), fp) != NULL) { + unsigned long address; + char name[MAX_LINE_LENGTH]; + + // 解析函数名和地址 + if (sscanf(line, "%lx %s", &address, name) == 2) { + printf("Name: %s, Address: %lx\n", name, address); + } + } + + // 关闭 GDB 进程 + pclose(fp); + // +} + bool get_symbol_from_elf(std::set<symbol> &ss, const char *path) { static int first_init = 0; @@ -276,6 +325,13 @@ bool get_symbol_from_elf(std::set<symbol> &ss, const char *path) { int is_reloc = 0; elf_version(EV_CURRENT); + + // Check if the file attribute contains "stripped" + if (is_stripped(path)){ + get_symbol_from_elf_gdb(ss, path); + return true; + } + int fd = open(path, O_RDONLY); Elf *elf = elf_begin(fd, ELF_C_READ, NULL); |
