load warning: cannot find entry symbol _start

2019-04-30 00:40发布

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.

4条回答
小情绪 Triste *
2楼-- · 2019-04-30 00:53

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.

查看更多
男人必须洒脱
3楼-- · 2019-04-30 01:12

Instead of main you should use _start to indicate where nasm assembler should start executing. foe eg:

section .text
global _start
_start:
mov ebx, 1
mov ecx, msg
mov edx, len
mov eax, 4
int 0x80
mov eax, 1
int 0x80
查看更多
Deceive 欺骗
4楼-- · 2019-04-30 01:13

I would suggest that you link your object files (however they are produced) with gcc, not ld.

gcc will call ld with the appropriate options, since it knows more about the source code and will create whatever is necessary for the assumptions that ld makes.

查看更多
叛逆
5楼-- · 2019-04-30 01:14

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 of rsp.)

The error message is from ld, not from nasm. 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 the global 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 not main(), and doesn't receive argc and argv arguments. (Or maybe the 32bit ABI does put those on the stack at process start time, in the same order main() 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.

查看更多
登录 后发表回答