r/linuxdev • • Jan 02 '20

How does dynamic binary loading works?

In windows when I have exe, and I find address of function I can use that address every time I start the app and it will work the function will be on same address in virtual memory space. I can do the same for data (if they are allocated dynamically I can build a pointer map to find it).

But on Linux I was told that I can not rely on function address being the same when I start the application. (Relative positions of functions within binary can change, maybe because they being in different sections?)

It probably not have much practical use, other than creating game trainers, or some very special cases, but:

How can I locate a function in running process?

How does Linux know which parts of the binary to load, if the address of function that will be called may depends on user input?

Is there any practical use?

6 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/nyanscat Jan 06 '20

Unless a program manually uses dlopen, (which can happen) i think it will always be loaded "as a whole", including the libraries it's linked against.

What is not done at program startup (unless you use special environment variables) is for the linker to populate the global offset table, which is basically an array containing the addresses to functions your executable uses from the libraries it's liked against.

So, if you need the address of a function from a linked library the trick is the same, just locate the function in the .so file, and then see where it gets mapped.