title: Address space for shared libraries loaded multiple times in the same process
body: <p>First off, I've already found <a href="http://www.akkadia.org/drepper/dsohowto.pdf" rel="nofollow">a</a> <a href="http://www.dabeaz.com/papers/CiSE/c5090.pdf" rel="nofollow">few</a> references which might answer my question. While I plan on reading them soon (i.e. after work), I'm still asking here in case the answer is trivial and does not require too much supplementary knowledge.</p>
  
  <p>Here is the situation: I am writing a shared library (let's call it libA.so) which needs to maintain a coherent internal (as in, static variables declared in the .c file) state within the same process.
  This library will be used by program P (i.e. P is compiled with <code>-lA</code>). If I understand everything so far, the address space for P will look something like this:</p>
  
  <pre><code> ______________
  | Program P    |
  | &lt;            |
  |  variables,  |
  |  functions   |
  |  from P      |
  | &gt;            |
  |              |
  | &lt;            |
  |  libA:       |
  |  variables,  |
  |  functions   |
  |  loaded (ie  |
  |  *copied*)   |
  |  from shared |
  |  object      |
  | &gt;            |
  | &lt;            |
  |  stuff from  |
  |  other       |
  |  libraries   |
  | &gt;            |
  |______________|
  </code></pre>
  
  <p>Now P will sometimes call <code>dlopen("libQ.so", ...)</code>. libQ.so also uses libA.so (i.e. was compiled with <code>-lA</code>). Since everything happens within the same process, I need libA to somehow hold the same state whether the calls come from P or Q.</p>
  
  <p>What I do not know is how this will translate in memory. Will it look like this:</p>
  
  <pre><code> ______________
  | Program P    |
  | &lt;            |
  |  P stuff     |
  | &gt;            |
  |              |
  | &lt;            |
  |  libA stuff, |
  |  loaded by P |
  | &gt;            | =&gt; A's code and variables are duplicated
  |              |
  | &lt;            |
  |  libQ stuff  |
  |  &lt;           |
  |   libA stuff,|
  |   loaded by Q|
  |  &gt;           |
  | &gt;            |
  |______________|
  </code></pre>
  
  <p>... or like this?</p>
  
  <pre><code> ______________
  | Program P    |
  | &lt;            |
  |  P stuff     |
  |  *libA       |
  |  *libQ       |
  | &gt;            |
  |              |
  | &lt;            |
  |  libA stuff, |
  |  loaded by P |
  | &gt;            | =&gt; A's code is loaded once, Q holds some sort of pointer to it
  |              |
  | &lt;            |
  |  libQ stuff  |
  |  *libA       |
  | &gt;            |
  |______________|
  </code></pre>
  
  <p>In the second case, keeping a consistent state for a single process is trivial; in the first case, it will require some more effort (e.g. some shared memory segment, using the process id as the second argument to <code>ftok()</code>).</p>
  
  <p>Of course, since I have a limited knowledge on how linking and loading works, the diagrams above may be completely wrong. For all I know, the shared libraries could be at a fixed space in memory, and every process accesses the same data and code. The behaviour could also depends on how A and/or P and/or Q were compiled. And this behaviour is probably not platform independent.</p>