What is the use of "--oformat = elf32-i386"?

I have a confusion about two GNU linker command line options --- ld. But before my question, I will show you some prerequisites.

These days I am reading Richard Bloom's Professional Assembler, and cpuid.s

- the sample assembly code from this book. This book introduces 32-bit assembly programming, but mine is a 64-bit OS.

To generate 32 bit output, just following the sample book. I am using the following command to build and link the code:

as --32 -o cpuid.o cpuid.s
ld --oformat=elf32-i386 -o cpuid cpuid.o

      

however, the second step ld

fails with "ld: i386, the architecture of the input file` cpuid2.o 'is incompatible with the output of i386: x86-64 ".

After some google, I found that I need to use the option -melf_i386

:

ld -melf_i386 -o cpuid cpuid.o

      

Yes, this time the link is successful, but I don't know why the official GNU documentation says:

you can use the `--oformat 'option to specify the binary format for the output object

I am using 32 bit object file cpuid.o

, and say ld

to generate 32 bit output explicitly via --oformat=elf32-i386

optoin, I think it shouldn't be a problem. But why should I use a parameter -melf_i386

? So what are the reasons for the existing ones --oformat

?

I tried to figure out anwser but couldn't, I found the two most relevant links below, but didn't answer my question:

http://www.linuxquestions.org/questions/linux-software-2/relocatable-linking-on-x86-64-for-i386-872812/

Building a 32-bit Application on 64-bit Ubuntu

Any help would be appreciated. Thank...

+3


source to share


2 answers


The output format does not necessarily match the architecture, although it could be for elf32-i386 as well. But for others like ihex or bin, it is not possible to choose a suitable architecture emulation. This is why it just defaults to the one it was compiled for, which appears to be x86-64 in your case.



+2


source


as --32

      

means you are targeting a 32 bit arch, but



ld --oformat=elf32-i386

      

means that you are asking ld to make the exe format of the file as elf in 32-bit for the i386 platform, whereas -melf_i386 asks the linker to generate / emulate an object file for the i386 machine. So --oformat sets the linker to generate an object according to the target arch, while -m asks the linker to emulate the target arch.

+2


source







All Articles