Quantcast
Viewing latest article 2
Browse Latest Browse All 3

Answer by Alex Hoppus for Including a library in C, where does it place in the address space?

Actually if you have linux based pc, you can check it by yourself in the following way:

  1. create a simple c application with a infinite while loop inside of main().
  2. compile

    $ gcc -o main ./main.c -g

  3. launch

    $ gdb ./main

  4. Show mapping info

    (gdb) r

    (gdb) info proc mappings

    Mapped address spaces:

          Start Addr           End Addr       Size     Offset objfile
            0x400000           0x401000     0x1000        0x0 /tmp/main
            0x600000           0x601000     0x1000        0x0 /tmp/main
            0x601000           0x602000     0x1000     0x1000 /tmp/main
            0x602000           0x623000    0x21000        0x0 [heap]
      0x7ffff7a0d000     0x7ffff7bcd000   0x1c0000        0x0 /lib/x86_64-linux-gnu/libc-2.23.so
      0x7ffff7bcd000     0x7ffff7dcd000   0x200000   0x1c0000 /lib/x86_64-linux-gnu/libc-2.23.so
      0x7ffff7dcd000     0x7ffff7dd1000     0x4000   0x1c0000 /lib/x86_64-linux-gnu/libc-2.23.so
      0x7ffff7dd1000     0x7ffff7dd3000     0x2000   0x1c4000 /lib/x86_64-linux-gnu/libc-2.23.so
      0x7ffff7dd3000     0x7ffff7dd7000     0x4000        0x0
      0x7ffff7dd7000     0x7ffff7dfd000    0x26000        0x0 /lib/x86_64-linux-gnu/ld-2.23.so
      0x7ffff7fd4000     0x7ffff7fd7000     0x3000        0x0
      0x7ffff7ff6000     0x7ffff7ff8000     0x2000        0x0
      0x7ffff7ff8000     0x7ffff7ffa000     0x2000        0x0 [vvar]
      0x7ffff7ffa000     0x7ffff7ffc000     0x2000        0x0 [vdso]
      0x7ffff7ffc000     0x7ffff7ffd000     0x1000    0x25000 /lib/x86_64-linux-gnu/ld-2.23.so
      0x7ffff7ffd000     0x7ffff7ffe000     0x1000    0x26000 /lib/x86_64-linux-gnu/ld-2.23.so
      0x7ffff7ffe000     0x7ffff7fff000     0x1000        0x0
      0x7ffffffdd000     0x7ffffffff000    0x22000        0x0 [stack]
    

So we see, that ld-so has placed c library to the addresses 0x7ffff7bcd000 - 0x7ffff7dd5000. The offset field - is an offset in the ELF file itself. We can check which sections corresponds to which offset using readelf:

$ readelf -a /lib/x86_64-linux-gnu/libc-2.23.so | less

Foe example:

 [13] .text             PROGBITS         000000000001f8b0  0001f8b0
       0000000000153214  0000000000000000  AX       0     0     16

That means that .text section have offset 0x1f8b0. From the mapping above, we can conclude that virtual address of the beginning of the .text section in main app address space will be 0x7ffff7bcd000 + 0x1f8b0


Viewing latest article 2
Browse Latest Browse All 3

Trending Articles