I'm learning assembly programming. Below is the simple program that prints 'Hello, World!'. While the program runs perfectly, I'm getting the warning message while loading
ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080
Here is the code :
section .data
msg db 'Hello, world!', 0xa
len equ $ - msg
section .text
global main
main:
mov ebx, 1
mov ecx, msg
mov edx, len
mov eax, 4
int 0x80
mov eax, 1
int 0x80
Can anybody explain the meaning of this warning. I'm using nasm
with ubuntu 14
.
You can try to compile the assembly source file with nasm, generate the *.o file, and then use the ld link the *.o file with parameter -e main. This means that main is specified as the program entry.
Instead of main you should use _start to indicate where nasm assembler should start executing. foe eg:
I would suggest that you link your object files (however they are produced) with
gcc
, notld
.gcc
will callld
with the appropriate options, since it knows more about the source code and will create whatever is necessary for the assumptions thatld
makes.You don't say, but from the error messages and code I assume you're building your 32bit code with
nasm -felf32 hello32.asm && ld -melf_i386 -o hello32 hello32.o
(If you're actually building 64bit code, you're lucky that it happens to work, but it'll break as soon as you do anything with
esp
instead ofrsp
.)The error message is from
ld
, not fromnasm
. It says so right in the message. Tim's comment is correct:ld
looks for a_start
symbol in the files it links, but sets the entry point to the beginning of the text segment if it doesn't find one.It doesn't matter what other global/external symbols you define.
main
has no relevance at all here, and could point anywhere you want. It's only useful for a disassembly output and stuff like that. Your code would work exactly the same if you took out theglobal main
/main:
lines, or changed them to any other name.Labelling that as
main
is unwise, if you're building without the standard libc runtime start files. It's notmain()
, and doesn't receiveargc
andargv
arguments. (Or maybe the 32bit ABI does put those on the stack at process start time, in the same ordermain()
wants them. The 64bit ABI puts them on the stack, but the startup code that calls main has to load them into registers because 64bit uses a register-call ABI.) You also can't return from the entry point: there's no return address on the stack.