When Kernel or Dynamic linker loads ELF file into memory, they will not think about sharing. For example, you want to start same program twice. First process will load read-only part of ELF file with mmap; second process will do the same mmap (if aslr is active - second mmap will be into different virtual address). It is task of Page cache (VFS subsystem) to keep single copy of data in physical memory (with COPY-on-WRITE aka COW); and mmap will just setup mappings from virtual address in each process into single physical location. If any process will change a memory page; it will be copied on write to unique private physical memory.

Loading code is in glibc/elf/dl-load.c (_dl_map_object_from_fd) for and linux-kernel/fs/binfmt_elf.c for kernel's ELF loader (elf_map, load_elf_binary). Do a search for PT_LOAD.

So, global data and bss data is always privately mmaped in each process, and they are protected with COW.

symbol binding log in Linux/glibc (>2.1) with:

LD_DEBUG=bindings ./your_program

mmap /dev/zero 這種用法 , 出來還要memset (0) 阿?
Scott Tsai - 不用,但 mmap() 要以 PAGE_SIZE 為單位

how to write shared library

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License