简体   繁体   中英

Loading shared object to specific processes in Linux

I want to load a shared object to certain processes, there are certain conditions that are required

  • loading to only specific processes and not all of them
  • it has to be done before the process code starts executing
  • the processes are not mine

What are the available ways to support this functionality on Linux?

Can it be accomplished with "/etc/ld.so.preload" or "LD_PRELOAD=/my/lib.so"? Is a kernel module needed for this?

LD_PRELOAD could be used to load a specific generic tiny library which on its side will load any additional library the process needs with dlopen() service. Typically, applications able to load plugins, look into a specific directory and call dlopen() for all the library files found into it.
You need to define an entry point in the generic library which will be triggered at loading time. With gcc you can define this entry point with a constructor attribute :

void __attribute__ ((constructor)) lib_initialize(void);

void lib_initialize(void)
{
  // Look into a specific directory to see if there are libs to
  // load with dlopen()
}

This mechanism does not need any specific kernel module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM