Force GNU linker to generate 32 bit ELF executables

Hunter McMillen picture Hunter McMillen · Apr 14, 2013 · Viewed 17.1k times · Source

Hi I am currently generating x86 assembly for a compiler that I am writing and am having some trouble linking the file on my 64-bit VM (the assembly code is 32 bit).

I was able to assemble the object file fine with this command:

as --32 mult.S -o mult.o

but I can't seem to find any options for ld that make it generate a 32-bit ELF file:

ld <some-option?> mult.o -o mult

Any help would be great.

Answer

Employed Russian picture Employed Russian · Apr 14, 2013

ld <some-option?> mult.o -o mult

ld -m elf_i386 mult.o -o mult

You can get a list of available architectures with:

ld -V

Sample output:

GNU ld (GNU Binutils for Ubuntu) 2.24
  Supported emulations:
   elf_x86_64
   elf32_x86_64
   elf_i386
   i386linux
   elf_l1om
   elf_k1om
   i386pep
   i386pe

However, that shouldn't be necessary: ld looks at the first object, and should automatically select emulation based on the format of that object.