title: load-time ELF relocation
body: <p>I am writing a simple user-space ELF loader under Linux (why? for 'fun').  My loader at the moment is quite simple and is designed to load only statically-linked ELF files containing position-independent code.</p>
  
  <p>Normally, when a program is loaded by the kernel's ELF loader, it is loaded into its own address space.  As such, the data segment and code segment can be loaded at the correct virtual address as specified in the ELF segments.</p>
  
  <p>In my case, however, I am requesting addresses from the kernel via <code>mmap</code>, and may or may not get the addresses requested in the ELF segments.  This is not a problem for the code segment since it is position independent.  However, if the data segment is not loaded at the expected address, code will not be able to properly reference anything stored in the data segment.</p>
  
  <p>Indeed, my loader appears to work fine with a simple assembly executable that does not contain any data.  But as soon as I add a data segment and reference it, the executable fails to run correctly or SEGFAULTs.</p>
  
  <p>How, if possible, can I fixup any references to the data segment to point to the correct place?  Is there a relocation section stored in the (static) ELF file for this purpose?</p>