加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Linux动态库函数的详解

发布时间:2020-12-15 05:12:16 所属栏目:安全 来源:网络整理
导读:Linux动态库函数的详解 加载动态库 void *dlopen(const char *filename,int flag); flag的可能值: RTLD_LAZY RTLD_NOW RTLD_GLOBAL RTLD_LOCAL RTLD_NODELETE (since glibc 2.2) RTLD_NOLOAD (since glibc 2.2) RTLD_DEEPBIND 这些flag的具体含义可使用man

Linux动态库函数的详解

加载动态库

void *dlopen(const char *filename,int flag);

flag的可能值:

  •         RTLD_LAZY
  •         RTLD_NOW
  •         RTLD_GLOBAL
  •         RTLD_LOCAL
  •         RTLD_NODELETE (since glibc 2.2)
  •         RTLD_NOLOAD (since glibc 2.2)
  •         RTLD_DEEPBIND

这些flag的具体含义可使用man查看

返回动态库中最近的一次错误

char *dlerror(void);

根据动态库句柄和函数名称,返回函数的地址

void *dlsym(void *handle,const char *symbol);

如果使用dlopen(NULL,<flag>)得到句柄,通过这个句柄可以查找所有函数符号,只要那个函数符号所在的dll在加载时指定了RTLD_GLOBAL 

关闭动态库

int dlclose(void *handle);

根据函数地址,返回函数名称和其它信息

注意传入地址只要落在函数代码空间范围之内即可,不需要是函数的起始地址

int dladdr(void *addr,Dl_info *info);
 
typedef struct {
   const char *dli_fname;/* Filename of defining object */
   void *dli_fbase;   /* Load address of that object */
   const char *dli_sname;/* Name of nearest lower symbol */
   void *dli_saddr;   /* Exact value of nearest symbol */
  } Dl_info;

 构造函数和析构函数(这两个是属性,而不是函数)

__attribute__((constructor))
__attribute__((destructor))

例如:

__attribute__((constructor)) void before_main() 
{ 
    printf("before main/n"); 
} 
 
__attribute__((destructor)) void after_main() 
{ 
  printf("after main/n"); 
} 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读