title: Sharing executable memory pages in Linux?
body: <p>Is it possible to share executable pages on Linux for the sake of preserving space? I know that there are shared memory APIs that can be used to share memory between different processes but I don't think that is meant to be used for that.</p>
  
  <p>Basically, I want to have a shared memory region where some commonly used shared libraries can be loaded into. I want to get the dynamic linker to link against the preloaded (read only) images instead of having to load all of the shared library images into every single process (which seems like a waste). </p>
  
  <p><em>Is this possible on the Linux kernel?</em> The Darwin kernel implements this using a feature of Mach VM known as <strong>commpages</strong> (the dyld shared cache is stored there). The commpages are accesible to and shared between every process.</p>
  
  <p>Just to clarify, I know what shared objects (libraries) are. Currently, what the dynamic linker does on Linux is it loads all the required libraries into the program's address space, which means that each application that links against libc (for example) will have an image of libc somewhere in its address space. On Darwin, this issue can be eliminated by having the executable (and other read only) sections of libc on <strong>a set of shared memory pages</strong>. The writable sections of the shared images are still separate.</p>
  
  <p><strong>Edit:</strong> I know that the ELF format does not support separating DATA and TEXT segments of the shared libraries. I'm not using ELF, <em>I'm using a different binary format (with my own binfmt kernel module and my own dynamic linker)</em>. I'm interested if the Linux kernel supports a commpage-like feature.</p>
  
  <p><strong>Edit 2:</strong> The only way I can think of doing this would be to allocate a big slab of memory in the kernel and map it into every binary that gets executed. The first time any binary is executed, the dynamic linker could unprotect it, fill it with the desired data and protect it. Then somehow, the kernel would have to make sure that the memory segment is not modified by anything else as it would open a massive security hole. Another </p>