title: What the usage of "--oformat=elf32-i386"?
body: <p>I have some confusion with two command line options of GNU linker --- ld. But before my question, I will show you some backgrounds. </p>
  
  <p>I am reading Richard Blum's <em>Professional Assembly Language</em> these days, and <code>cpuid.s</code> is an sample of assembly code from this book. This book introduces 32-bit assembly programming, but mine is a 64-bit OS.</p>
  
  <p>In order to generate the 32-bit output, just in accord with the book's sample. I uses the following two command to assembly and link the code:</p>
  
  <pre><code>as --32 -o cpuid.o cpuid.s
  ld --oformat=elf32-i386 -o cpuid cpuid.o
  </code></pre>
  
  <p>however, at the second step, <code>ld</code> fails with "ld: i386 architecture of input file `cpuid2.o' is incompatible with i386:x86-64 output".</p>
  
  <p>After some google, I find that I need to use <code>-melf_i386</code> option:</p>
  
  <pre><code>ld -melf_i386 -o cpuid cpuid.o
  </code></pre>
  
  <p>Yeah, this time the link is success, but I don't know why, in the GNU's official documentation, it says:</p>
  
  <blockquote>
    <p>you can use the `--oformat' option to specify the binary format for the output object </p>
  </blockquote>
  
  <p>I uses an 32-bit object file <code>cpuid.o</code>, and tell <code>ld</code> to generate 32-bit output explicitly through <code>--oformat=elf32-i386</code> optoin, I think it should be no trouble. But why must I use the <code>-melf_i386</code> option? So what the reasons of existing <code>--oformat</code>?</p>
  
  <p>I tried to googled the anwser, but failed, I found two most relevant links below, but not answers my question:</p>
  
  <p><a href="http://www.linuxquestions.org/questions/linux-software-2/relocatable-linking-on-x86-64-for-i386-872812/" rel="nofollow noreferrer">http://www.linuxquestions.org/questions/linux-software-2/relocatable-linking-on-x86-64-for-i386-872812/</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8421110/building-a-32-bit-app-in-64-bit-ubuntu">Building a 32-bit app in 64-bit Ubuntu</a></p>
  
  <p>Any help will be appreciated. Thanks...</p>