dlopen

Automatically executed functions when loading shared libraries

落爺英雄遲暮 提交于 2019-11-26 05:20:41
问题 When loading shared libraries in Windows, LoadLibrary() call causes DllMain in library to execute for each new process and thread library attaches to, and for each process and thread library deattaches from. Is there similar mechanism for Mac OS X, Linux and possibly other POSIX-compatible OSs? 回答1: You can define an on-load function for a linux library using the .init mechanism. This is the same as specifying the load-time entry point for a binary (e.g. using something other than main as the

building a .so that is also an executable

你。 提交于 2019-11-26 05:20:05
问题 So everyone probably knows that glibc\'s /lib/libc.so.6 can be executed in the shell like a normal executable in which cases it prints its version information and exits. This is done via defining an entry point in the .so. For some cases it could be interesting to use this for other projects too. Unfortunately, the low-level entry point you can set by ld\'s -e option is a bit too low-level: the dynamic loader is not available so you cannot call any proper library functions. glibc for this

Linux下显示运行时链接(运行时加载)

那年仲夏 提交于 2019-11-25 19:25:53
目录 介绍 如何加载动态库 dlopen() 第一个参数: 被加载动态库的路径 第二个参数: flag表示函数符号的解析方式 dlopen 返回值 dlsym() 参数: 返回值 符号优先级 dlerror() dlclose() show code 内容学习自《 程序员的自我修养 链接装载与库》 如果只是想知道如何使用如何加载动态库和那4个函数的使用,可以直接从如何加载动态库开始看。 介绍 支持动态链接的系统往往都支持一种更加灵活的模块加载方式,叫做显式运行时链接(Explicit Run-time Linking),有时候也叫做运行时加载。也就是让程序自己在运行时控制加载指定的模块,并且可以在不需要该模块时将其卸载。从前面我们了解到的来看,如果动态链接器可以在运行时将共享模块装载进内存并且可以进行重定位等操作,那么这种运行时加载在理论上也是很容易实现的。而且一般的共享对象不需要进行任何修改就可以进行运行时装载,这种共享对象往往被叫做动态装载库(Dynamic Loading Library),其实本质上它跟一般的共享对象没什么区别,只是程序开发者使用它的角度不同。 这种运行时加载使得程序的模块组织变得很灵活,可以用来实现一些诸如插件、驱动等功能。当程序需要用到某个插件或者驱动的时候,才将相应的模块装载进来,而不需要从一开始就将他们全部装载进来,从而减少了程序启动时间和内存使用