26

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.

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170

1 Answers1

39

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • 1
    Thank you, I actually just found this myself a few minutes ago. I had hoped that is what `ld` would do but for some reason it still said I had a mismatching architecture. – Hunter McMillen Apr 14 '13 at 21:24
  • Can saomeone explain the hard ones like `elf32_x86_64` and `i386linux`, `elf_l1om`? – Ciro Santilli OurBigBook.com Apr 21 '15 at 15:57
  • can i ask you what does elf_i386 do? I searched through manual but couldn't find it – paradox Apr 11 '17 at 04:08
  • for linux, you need the elf type, i386 stands for intel 80386, so `-m elf_i386` should be used for Linux in intel x86. So I believe it should end up doing what `-m32` does in GCC – christopher westburry Feb 15 '19 at 07:55
  • 1
    @CiroSantilli新疆改造中心法轮功六四事件 elf32_x86_64 is for the so-called x32 ABI. It leverages the 64-bit hardware while enforcing 32-bit pointer. See here: https://stackoverflow.com/questions/58654288/how-to-make-compiler-generate-a-elf32-x86-64-format-object-file – smwikipedia Nov 01 '19 at 07:20