I have some confusion with two command line options of GNU linker --- ld. But before my question, I will show you some backgrounds.
I am reading Richard Blum's Professional Assembly Language these days, and cpuid.s
is an sample of assembly code from this book. This book introduces 32-bit assembly programming, but mine is a 64-bit OS.
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:
as --32 -o cpuid.o cpuid.s
ld --oformat=elf32-i386 -o cpuid cpuid.o
however, at the second step, ld
fails with "ld: i386 architecture of input file `cpuid2.o' is incompatible with i386:x86-64 output".
After some google, I find that I need to use -melf_i386
option:
ld -melf_i386 -o cpuid cpuid.o
Yeah, this time the link is success, but I don't know why, in the GNU's official documentation, it says:
you can use the `--oformat' option to specify the binary format for the output object
I uses an 32-bit object file cpuid.o
, and tell ld
to generate 32-bit output explicitly through --oformat=elf32-i386
optoin, I think it should be no trouble. But why must I use the -melf_i386
option? So what the reasons of existing --oformat
?
I tried to googled the anwser, but failed, I found two most relevant links below, but not answers my question:
Building a 32-bit app in 64-bit Ubuntu
Any help will be appreciated. Thanks...