Is it possible to pass arguments to my library, which is loaded with LD_PRELOAD:
LD_PRELOAD=lib.so ./program
How can I pass arguments to this library?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Normally I’d do this by using environment variables. For example with something like:
lets you do:
this doesn’t have to be used in a constructor (which is a GCC extension), but that’s often a handy place to use them.
What I’ve done in the past has been to use this, combined with a shell script wrapper that looks like it’s a “normal” application. The shell script takes its arguments and pushes them into the environment variables your library expects before calling
execto load the program you want to interpose. It “feels” right to users that way without being too fragile or intrusive.You could also do this by reading
/proc/self/cmdlineto read the command line of the current process directly if you’d rather. Personally I’d keep clear of interfering with the process you’re working with as much as possible though.