Force GNU linker to generate 32 bit ELF executables

20,381

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.

Share:
20,381

Related videos on Youtube

Hunter McMillen
Author by

Hunter McMillen

Updated on July 09, 2022

Comments

  • Hunter McMillen
    Hunter McMillen almost 2 years

    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
    Hunter McMillen about 11 years
    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.
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com about 9 years
    Can saomeone explain the hard ones like elf32_x86_64 and i386linux, elf_l1om?
  • paradox
    paradox about 7 years
    can i ask you what does elf_i386 do? I searched through manual but couldn't find it
  • christopher westburry
    christopher westburry over 5 years
    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
  • smwikipedia
    smwikipedia over 4 years
    @CiroSantilli新疆改造中心法轮功六四事件 elf32_x86_64 is for the so-called x32 ABI. It leverages the 64-bit hardware while enforcing 32-bit pointer. See here: stackoverflow.com/questions/58654288/…