如何按名称获取函数地址? [英] How to get function address by name?

查看:75
本文介绍了如何按名称获取函数地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按名称获取函数的地址.

I'd like to get function's address by name.

例如,当前我正在使用 dlsym :

For example, currently I am using dlsym:

unsigned long get_func_addr(const char *func_name)
{
     return (unsigned long)dlsym(NULL, func_name);
}

但是, dlsym 仅适用于外部函数.它不适用于静态功能.我知道在不同的文件中可能有多个具有相同名称的静态函数.但是我至少需要获得一个带有名称的静态函数的地址.有时静态功能将被启用.但是如果用调试编译C文件也可以.我认为使用 -g 可以显示静态函数的符号表,但是如何访问它呢?

However, dlsym only works for extern function. It won't work for static function. I know there could multiple static functions with same name in different files. But I need to at least get one static function's address with the name. Sometime static function will be inlned. But it's OK if C file is compiled with debug. I think with -g, the symbol table of static functions is present, but how can I access it?

我不想创建用于将字符串映射到函数地址的表.我需要找到一种动态地做到这一点的方法.

I don't want to created a table for mapping the string to function address. I need to find a way to do it dynamically.

推荐答案

静态静态函数甚至不需要在二进制文件中存在,因此无法获取其地址.即使它确实存在,它也可能已经由编译器基于某些参数只能采用特定值的知识进行了修改,或者可能已经对调用约定进行了调整,使得它不能从外部调用,等等.确保存在静态功能的真实"版本是通过某个函数指针使其地址对其他模块可见.

A static function need not even exist in the binary, so there's no way to get its address. Even if it does exist, it might have been modified by the compiler based on the knowledge that certain arguments can only take particular values, or it might have had the calling convention adjusted such that it's not externally callable, etc. The only way you can be sure a "real" version of a static function exists is if its address is made visible to other modules via a function pointer.

这篇关于如何按名称获取函数地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆