exported symbol 被设置为空时怎么通过__builtin_return_address获得objective-c的方法名
时间: 2024-03-17 19:45:10 浏览: 111
如果 exported symbol 被设置为空,你可以通过 __builtin_return_address 函数获取调用栈上的指针,然后利用 Objective-C 运行时库中的函数进行符号化,从而获取方法名。
具体来说,你可以使用以下代码片段:
```objective-c
void *returnAddr = __builtin_return_address(0);
Dl_info info;
dladdr(returnAddr, &info);
NSString *symbolName = [NSString stringWithUTF8String:info.dli_sname];
NSString *methodName = NSStringFromSelector(NSSelectorFromString(symbolName));
```
其中,__builtin_return_address(0) 函数返回调用栈上的指针,dladdr 函数可以将指针转换为符号信息,NSStringFromSelector 函数可以将符号名转换为方法名。注意,此方法只适用于 Objective-C 方法,如果你需要获取 C 函数名,可以使用 info.dli_sname 直接获取。
相关问题
Exported symbol为空时怎么获取函数名
如果一个函数没有导出符号,无法通过`dladdr`函数获取函数名。但是我们可以通过解析可执行文件的符号表获取函数名。
首先需要使用`dlopen`函数打开可执行文件,然后通过`dlsym`函数获取可执行文件中函数的地址。接着使用`nm`命令或`libbfd`库解析可执行文件的符号表,找到函数的名称。
以下是一个示例代码,可以获取可执行文件中所有函数的名称:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <bfd.h>
#include <string.h>
#include <errno.h>
void print_function_name(void* addr) {
Dl_info info;
if (dladdr(addr, &info) && info.dli_sname) {
printf("%s\n", info.dli_sname);
return;
}
bfd* abfd;
bfd_init();
abfd = bfd_openr("/path/to/executable", 0);
if (!abfd) {
fprintf(stderr, "Failed to open executable: %s\n", strerror(errno));
return;
}
if (!bfd_check_format(abfd, bfd_object)) {
fprintf(stderr, "Invalid executable format\n");
bfd_close(abfd);
return;
}
long storage_needed = bfd_get_symtab_upper_bound(abfd);
if (storage_needed < 0) {
fprintf(stderr, "Failed to get symbol table size\n");
bfd_close(abfd);
return;
}
asymbol** symbol_table = (asymbol**)malloc(storage_needed);
long symbols_read = bfd_canonicalize_symtab(abfd, symbol_table);
if (symbols_read < 0) {
fprintf(stderr, "Failed to read symbol table\n");
bfd_close(abfd);
free(symbol_table);
return;
}
for (long i = 0; i < symbols_read; ++i) {
if (symbol_table[i]->flags & BSF_FUNCTION) {
bfd_vma value = symbol_table[i]->value;
if (value == (bfd_vma)addr) {
printf("%s\n", symbol_table[i]->name);
}
}
}
bfd_close(abfd);
free(symbol_table);
}
int main() {
// Call some functions
print_function_name((void*)&main);
print_function_name((void*)&printf);
print_function_name((void*)&print_function_name);
return 0;
}
```
需要注意的是,`libbfd`库是GNU binutils的一部分,需要安装binutils才能使用。另外,该库可能会影响程序性能,因为它需要解析可执行文件的符号表。
-exported_symbol[s_list] command line option
The "-exported_symbol[s_list]" command line option is used in the process of linking object files to create an executable file or a shared library in C++. It allows the programmer to specify a list of symbols that should be exported from the resulting binary.
When a C++ program is compiled, each source file is compiled separately into an object file. These object files contain the compiled code for the functions and variables defined in the source file. When these object files are linked together, the linker combines all the object files into a single executable or shared library.
During the linking process, the linker will discard any symbols that are not used by the program. By using the "-exported_symbol[s_list]" command line option, the programmer can ensure that certain symbols are not discarded by the linker, and are instead made available for use by other programs that link against the resulting binary.
For example, if a programmer wanted to export a function called "foo" from their shared library, they could use the following command line option:
```
-exported_symbol foo
```
This would ensure that the symbol "foo" is not discarded by the linker, and is available for other programs to use.
阅读全文