Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly multiple definition

Tags:

assembly

Giving the code

section .data                          

 msg     db      "Hello, world!",0xA    
 len     equ     $ - msg                 

 section .text                         

                         ;we must export the entry point to the ELF linker or
     global _start       
 _start:

         mov     eax,4   
         mov     ebx,1   
         mov     ecx,msg 
         mov     edx,len 
         int     0x80    


         mov     eax,1   
         xor     ebx,ebx
         int     0x80

when try to run it, it shows with the command

linux1[8]% nasm -f elf -l hello.lst hello.asm
linux1[9]% ls
hello.asm  hello.lst  hello.o
linux1[10]% gcc -o hello hello.o
hello.o: In function `_start':
hello.asm:(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o:(.text+0x0): first defined here
hello.o: could not read symbols: File in wrong format
collect2: ld returned 1 exit status

How to fix the multiple definition problem? I only define the _start once, how it comes out said multiple definition? Thanks

like image 842
Mika Avatar asked Sep 30 '15 00:09

Mika


1 Answers

You are using gcc to link and that will by default add the C libraries which expect entry point main and already contain a _start that invokes main. That's why you have the multiple definition.

If you do not need the C library (and in this code doesn't), but still would like to use gcc for linking, try gcc -nostdlib -m32 -o hello hello.o.

The wrong format error is due to trying to produce a 64 bit executable from a 32 bit object file. Adding the -m32 fixes that so you get a 32 bit executable (since your code is 32 bit). If you intend to create a 64 bit program, use -f elf64 for nasm and of course write 64 bit compatible code.

like image 80
Jester Avatar answered Sep 22 '22 01:09

Jester