0

I have this segmentation fault for a simple hello world program. I am currently running 64 bit Ubuntu x86_64 arch.. uname-a:

Linux ubuntu 4.4.0-28-generic #47-Ubuntu SMP Fri Jun 24 10:09:13 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

And here is my code:

section.data ;Constant
            msg:    db "Hello World!"
            msg_L:   equ $-msg  ; Current - msg1

section.bss ;Varialble

section.text ; Code
        global _start:

_start:
        mov eax,4
        mov ebx,1; Where to wrte it out. Terminal
        mov ecx, msg
        mov edx, msg_L
        int 80h

        mov eax, 1 ; EXIT COMMAND
        mov ebx,0 ; No Eror
        int 80h

I run it using the commands: nasm -f elf64 first.asm ld -elf_x86_64 -o first first.o

And as a result i get the common Error Segmentation Fault. Anything wrong with this? Help would be appreciated!

EDIT

Tried:

sudo apt-get install libc6-dev-i386
nasm -f elf32 first.asm
gcc -m32 first.o -o first

And when i do gcc it gives me:

first.o: In function `section.bss':
first.asm:(.text+0xc): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib32/crt1.o:(.text+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib32/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
amanuel2
  • 4,508
  • 4
  • 36
  • 67

1 Answers1

1

Your asm is providing _start symbol, not main.

gcc is trying to link it as C program, so it provides it's own setup code (with it's own _start label), and looks for main to start your code.

Either change your label to main and live with full C-like initialization, or use gcc -m32 first.o -o first -nostdlib to omit stdlib startup code during linking.

Ped7g
  • 16,236
  • 3
  • 26
  • 63