understanding shared libraries using gcc

前端 未结 3 561
-上瘾入骨i
-上瘾入骨i 2021-02-11 00:17

I am trying to understand the following behavior of shared libraries in C

Machine One

$ cat one.c 
#include

         


        
3条回答
  •  独厮守ぢ
    2021-02-11 00:38

    The addresses are basically random numbers. Before secure implementations were devised, ldd would consistently indicate the memory addresses where the program sections were loaded. Since about five years ago, many flavors of Linux now intentionally randomize load addresses to frustrate would-be virus writers, etc. I compiled one.c (as t.c) and repeatedly executed ldd:

    [wally@zenetfedora .bin]$ cat t.c
    #include 
    int main()
    {
        printf ("%d", 45);
    }
    [wally@zenetfedora .bin]$ gcc -o t t.c -O3
    [wally@zenetfedora .bin]$ ldd t
        linux-gate.so.1 =>  (0x009e5000)
        libc.so.6 => /lib/libc.so.6 (0x002e4000)
        /lib/ld-linux.so.2 (0x002c2000)
    [wally@zenetfedora .bin]$ ldd t
        linux-gate.so.1 =>  (0x00b8d000)
        libc.so.6 => /lib/libc.so.6 (0x002e4000)
        /lib/ld-linux.so.2 (0x002c2000)
    [wally@zenetfedora .bin]$ ldd t
        linux-gate.so.1 =>  (0x00238000)
        libc.so.6 => /lib/libc.so.6 (0x002e4000)
        /lib/ld-linux.so.2 (0x002c2000)
    [wally@zenetfedora .bin]$ ldd t
        linux-gate.so.1 =>  (0x002a0000)
        libc.so.6 => /lib/libc.so.6 (0x002e4000)
        /lib/ld-linux.so.2 (0x002c2000)
    [wally@zenetfedora .bin]$ ldd t
        linux-gate.so.1 =>  (0x00f93000)
        libc.so.6 => /lib/libc.so.6 (0x002e4000)
        /lib/ld-linux.so.2 (0x002c2000)
    [wally@zenetfedora .bin]$ ldd t
        linux-gate.so.1 =>  (0x00c7a000)
        libc.so.6 => /lib/libc.so.6 (0x002e4000)
        /lib/ld-linux.so.2 (0x002c2000)
    [wally@zenetfedora .bin]$ ldd t
        linux-gate.so.1 =>  (0x00d1a000)
        libc.so.6 => /lib/libc.so.6 (0x002e4000)
        /lib/ld-linux.so.2 (0x002c2000)
    [wally@zenetfedora .bin]$ ldd t
        linux-gate.so.1 =>  (0x00d12000)
        libc.so.6 => /lib/libc.so.6 (0x002e4000)
        /lib/ld-linux.so.2 (0x002c2000)
    

    The crtl and ld-linux load addresses are consistent, but linux-gate is randomized.

    Libraries are needed because the C run time initialization and termination needs to run. Granted, those could largely be optimized away since stdin, stdout, stderr, etc., etc. don't need to be initialized. Still, the crtl is how main() gets called.

    Different flavors and versions of Linux have differences. The evolution of glib has had many twists and turns. Some stuff has been moved to other libraries. It's pretty much the same thing as why your local grocery store moves things around. It doesn't have much meaning.

提交回复
热议问题