load warning: cannot find entry symbol _start

不问归期 提交于 2019-12-22 04:36:16

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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



回答5:


To compile and execute your program you can create bash script as follow:

compile64.sh

!/bin//bash
echo "Assembling with Nasm"
nasm -f elf64 -o $1.o $1.asm
echo "Linking ... "
gcc -o $1 $1.o
echo "Done !"

$ ./compile64 nameOftheFile  (without extension)



回答6:


there is some issue in your programs like some syntactical mistakes like you can not assign registers value to constant because constant can not hold any value, for storing constants value we use variable

while assembling your program i am getting below mentioned asseble time errors

no such instruction: msg db 72ello,world!440xa' assign.S:3: Error: no such instruction:len equ $ - msg' assign.S:4: Error: no such instruction: section .text' assign.S:5: Error: no such instruction:global main' assign.S:7: Error: too many memory references for mov' assign.S:8: Error: too many memory references formov' assign.S:9: Error: too many memory references for mov' assign.S:10: Error: too many memory references formov' assign.S:11: Error: operand size mismatch for int' assign.S:12: Error: too many memory references formov' assign.S:13: Error: operand size mismatch for `int'

Here is code which give you same output on your gnu compiler with 32bit intel processor

.section .rodata msgp: .string "Hello World"

    .section .text
    .globl  main
    .type   main,@function

main:
    pushl   $msgp
    call    printf
    addl    $4,%esp

    pushl   $0
    call    exit

save this code with some name latest take Hello.S asseble with $ as -o Hello.o Hello.S link with $ ld -o Hello.o -lc -dynamic-linker /lib.ld.linux.so.2 -e main -Hello.o To run $ ./Hello

hope it will help you



来源:https://stackoverflow.com/questions/34758769/load-warning-cannot-find-entry-symbol-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!