title: Address range of a dynamically loaded library under Linux
body: <p>I have a working program that loads plugins with <code>dlopen</code>.</p>
  
  <p>New requirement: at some point in the code, I'm given a pointer, and I need to test whether this pointer points into the code or static data of a plugin.</p>
  
  <pre><code>bool is_pointer_into_plugin(void *p, void *handle);
  </code></pre>
  
  <p>Equivalently, I need to retrieve the plugin into which a pointer points, if any. I also need to know if the pointer points into the main program's code or static data (and ideally, distinguish between read-only and read-write areas).</p>
  
  <pre><code>void *handle plugin_containing_pointer(void *p);
  </code></pre>
  
  <p>Equivalently, I need to be able to retrieve the extent (address and size) at which a plugin is mapped. I also need this information for the main program.</p>
  
  <p>How can I implement <code>is_pointer_into_plugin</code>, or <code>plugin_containing_pointer</code>, or something equivalent?</p>
  
  <p>I can change the call to <code>dlopen</code> if necessary. The lookup should be reasonably fast as possible, the load-time code doesn't need to be fast. Running the plugins in separate processes and communicating through shared memory is not an option.</p>
  
  <p>My program runs under Linux (and Windows, but that's <a href="https://stackoverflow.com/questions/12589461/address-range-of-a-dynamically-loaded-library-under-windows">another question</a>). Future portability to other unix systems (at least to OSX) would be a plus.</p>