title: How to control PE segment order in Visual C++
body: <p>I am trying to write a packer/self extracting exe type program but I am having problems with the segment order created by Visual C++'s linker.</p>
  
  <p>Basically I have a stub program (the loader) that has a special variable defined like this:</p>
  
  <pre><code>#pragma const_seg(".blah")
  const char blah[1];
  #pragma const_seg()
  </code></pre>
  
  <p>And it does something with the data in <code>blah</code> (eg unpacks the data).</p>
  
  <p>A second program (the packer) then uses the loader program as a template for creating a new program with some data in <code>blah</code>.</p>
  
  <p>Basically the packer copies the loader file, then replaces the old <code>.blah</code> section with some new data. When the new file is executed the loader operates on this new data.</p>
  
  <p>Now modifying exe files is tricky work, so to make it simple I would like <code>.blah</code> to be the last segment in the file so that I can simply append data to the loader, then fix up a few size fields in the PE header.</p>
  
  <p>However I can't figure out how to control the order of segments in the Visual C++ linker to put <code>.blah</code> at the end of the file when it compiles the loader.</p>
  
  <p>Currently the section order is this:</p>
  
  <pre><code>.textbss
  .text
  .rdata
  .data
  .idata
  .blah
  .rsrc
  .reloc
  </code></pre>
  
  <p>As you can see <code>.rsrc</code> and <code>.reloc</code> are in the wrong place, I need them before <code>.blah</code>.</p>
  
  <p>What can I do to change this order?</p>