What is global _start in assembly language?

assembly, ld, linux, nasm

Solution

`global` directive is NASM specific. It is for exporting symbols in your code to where it points in the object code generated. Here you mark `_start` symbol global so its name is added in the object code (`a.o`). The linker (`ld`) can read that symbol in the object code and its value so it knows where to mark as an entry point in the output executable. When you run the executable it starts at where marked as `_start` in the code.

If a `global` directive missing for a symbol, that symbol will not be placed in the object code's export table so linker has no way of knowing about the symbol.

If you want to use a different entry point name other than `_start` (which is the default), you can specify `-e` parameter to ld like:

ld -e my_entry_point -o output_filename object_filename

Problem

This is my assembly level code ... ``` section .text global _start _start: mov eax, 4 mov ebx, 1 mov ecx, mesg mov edx, size int 0x80 exit: mov eax, 1 int 0x80 section .data mesg db 'KingKong',0xa size equ $-mesg ``` Output: ``` root@bt:~/Arena# nasm -f elf a.asm -o a.o root@bt:~/Arena# ld -o out a.o root@bt:~/Arena# ./out KingKong ``` My question is What is the global _start used for? I tried my luck with Mr.Google and I found that it is used to tell the starting point of my program. Why cant we just have the `_start` to tell where the program starts like the one given below which produces a kinda warning on the screen ``` section .text _start: mov eax, 4 mov ebx, 1 mov ecx, mesg mov edx, size int 0x80 exit: mov eax, 1 int 0x80 section .data mesg db 'KingKong',0xa size equ $-mesg root@bt:~/Arena# nasm -f elf a.asm root@bt:~/Arena# ld -e _start -o out a.o ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080 root@bt:~/Arena# ld -o out a.o ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080 ```

Original source

Related problems