I am trying to learn the C Calling conventions in assembly language. To do so, I made a simple program using the puts
function from the C standard library.
I assembled and linked the program with the following commands :-
nasm -f elf file.asm
gcc -m32 file.asm -o file
The nasm produces the right object file but when running the gcc to link the object files, I am getting error.
Looking at the error I have figured it out that I don't have the 32 bit version of glibc on my system. How can I install it. I already have installed this package installed.
I have 64 bit ubuntu 12.04 as my OS.
EDIT :- I have installed the following packages, but the problem is still not solved :-
1)ia32-libs
2) libc6-i386
This command will install the 32bit glibc libraries on 64 bit Ubuntu:
sudo apt-get install gcc-multilib
This is the proper syntax for linking assembly object code into an executable using gcc:
gcc -m32 objectfile.o -o executablefile
(nasm -felf32 already creates objectfile.o
; the .asm
file should not appear on GCC's command line. GCC can assemble+link a .S
file in one step using GAS syntax, but NASM is a separate package.)
I assembled and linked the program with the following commands :-
nasm -f elf file.asm
gcc -m32 file.asm -o file
This is wrong. Your first nasm
command is probably creating a file.o
file (and you should check that, e.g. with ls -l file.o
). The second gcc
command does not do what you wish.
But gcc
does not know about *.asm
file extensions (it knows about .S
for preprocessable GNU assembler syntax, and .s
for assembler code, but probably handle unknown extensions like .asm
as ELF object files by default, however file.asm
is not an ELF object file). You should try linking with
gcc -Wall -v -m32 file.o -o file
Notice that you give to GCC an object file in ELF (for the linker invoked by gcc
) which you previously produced with nasm
.
(you might later remove the -v
option to gcc
)
Alternatively, use the GNU as
assembler syntax (not the nasm one), name your file file.S
(if you want it to be preprocessed) or file.s
(without preprocessing) and use gcc -v -Wall -m32 file.s -o myprog
to compile it.
BTW, to understand more about calling conventions, read the x86-64 ABI spec (and the similar one for 32 bits x86 ...), make a small C example file some-example.c
, then run gcc -S -fverbose-asm -O some-example.c
and look into the produced some-example.s
with an editor or pager.
Learn also more about ELF then use readelf (& objdump
) appropriately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With