Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Both your assertions are actually correct (to some extent).</p> <p>In the case of an executable ELF file, the linker relies on a linker script to assign an address in the virtual space to every symbol of the program (these are grouped into sections which all have a start address and size). You can see the default script that is used by invoking <code>ld --verbose</code>. The sections of the binary and their addresses can be seen using tools like <code>readelf</code> or <code>objdump</code>, e.g. <code>readelf -l /bin/cat</code>. Then if you run <code>cat /proc/self/maps</code> you should that the addresses at which <code>/bin/cat</code> is mapped do match. So the <code>execve</code> kernel system call does that: replace the current process' address space with a new one to which the executable file given as argument is mapped.</p> <p>Of course if every bit of code was assigned a static address you would meet problems with shared libraries. Shared libraries use position-independant code, so they can be mapped about everywhere in the process address space. Here the kernel takes the decision as to how to proceed.</p> <p><code>mmap</code> does neither of (1) or (2), it just maps a memory or part of a file at a given address of the address space (or let the kernel decide which address to use). Actually it is used to map the shared libraries that a program uses. To see how, run <code>strace /bin/true</code> and see how <code>execve</code> is called first to create the process' address space from the binary file, and how the libc file is opened and relevant sections mmap'ed with the right permissions by the program loader:</p> <pre><code>execve("/bin/true", ["/bin/true"], [/* 69 vars */]) = 0 ... open("/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 mmap(NULL, 3804080, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f224f351000 mprotect(0x7f224f4e8000, 2097152, PROT_NONE) = 0 mmap(0x7f224f6e8000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x197000) = 0x7f224f6e8000 </code></pre> <p>The following articles may also be worth reading:</p> <ul> <li><a href="http://www.linuxjournal.com/article/1060" rel="nofollow">http://www.linuxjournal.com/article/1060</a></li> <li><a href="http://www.linuxforums.org/articles/understanding-elf-using-readelf-and-objdump_125.html" rel="nofollow">http://www.linuxforums.org/articles/understanding-elf-using-readelf-and-objdump_125.html</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload