title: How can I read a custom section within the loader?
body: <p>I'm trying to embed information (a simple integer) inside the executable object file (Elf) of a process in Linux.</p>
  
  <p>I've accomplished that by writing the integer value as binary inside a file, and then by copying the binary file content using the objcopy command.</p>
  
  <pre><code>objcopy --add-section .customsection=binaryfile processElfFile newProcessElfFile
  </code></pre>
  
  <p>In this way, inside newProcessElfFile I have a perfectly working copy of the process with a new section containing the integer value, and I can see the section by using</p>
  
  <pre><code>readelf -e newProcessElfFile
  </code></pre>
  
  <p>I have also verified the section value being correct by using some C code on top of the Libelf library. Everything works fine.</p>
  
  <p>Now, I want to read the integer value contained in the new section and perform a printk when the elf file is loaded to be executed.</p>
  
  <p>In order to achieve this, I need to modify the loader code, which is kernel side.</p>
  
  <p>The problem now is that:</p>
  
  <ul>
  <li>I cannot write code inside the kernel which uses the libelf library, so I cannot access directly the section value as I do with my user-side program.</li>
  <li>The elf kernel loader, contained inside linux-VERSION/fs/binfmt_elf.c, in the function load_elf_binary(), doesn't read elf sections, but access the elf program headers, which point towards elf segments, not the single sections.</li>
  </ul>
  
  <p>In order to solve the problem I guess I need to link my custom section within a segment such that I can access it.</p>
  
  <p>So I have 2 related questions:</p>
  
  <ul>
  <li>Can I somehow read directly my custom section within the loader?</li>
  <li>If not, How can I make a segment link to the custom section, so that I can access it using the elf file program headers?</li>
  </ul>
  
  <p>Another possibility may be to add the integer value as an element of the already existent .rodata section, but I unfortunately don't know how to perform it and again how to access that section in the kernel.</p>