assembly - How does the linker find the main function? -


how linker find main function in x86-64 elf-format executable?

a generic overview, linker assigns address block of code identified symbol main. symbols in object files.

actually, doesn't assign real address assigns address relative base translated real address loader when program executed.

the actual entry point not main symbol in crt calls main. ld default looks symbol start unless specify something different.

the linked code ends in .text section of executable , (very simplified):

address | code 1000      somefunction ... 2000      start 2001        call 3000 ... 3000      main ... 

when linker writes elf header specify entry point address 2000.

you can relative address of main dumping contents of executable objdump. actual address @ runtime can read symbol funcptr ptr = main; funcptr defined pointer function signature of main.

typedef int (*funcptr)(int argc, char* argv[]);  int main(int argc, char* argv[]) {     funcptr ptr = main;     printf("%p\n", ptr);     return 0; } 

the address of main correctly resolved regardless if symbols have been stripped since linker first resolve symbol main relative address.

use objdump this:

$ objdump -f funcptr.exe   funcptr.exe:     file format pei-i386 architecture: i386, flags 0x0000013a: exec_p, has_debug, has_syms, has_locals, d_paged start address 0x00401000 

looking main specifically, on machine this:

$ objdump -d funcptr.exe | grep main   40102c:       e8 af 01 00 00          call   4011e0 <_cygwin_premain0>   401048:       e8 a3 01 00 00          call   4011f0 <_cygwin_premain1>   401064:       e8 97 01 00 00          call   401200 <_cygwin_premain2>   401080:       e8 8b 01 00 00          call   401210 <_cygwin_premain3> 00401170 <_main>:   401179:       e8 a2 00 00 00          call   401220 <___main> 004011e0 <_cygwin_premain0>: 004011f0 <_cygwin_premain1>: 00401200 <_cygwin_premain2>: 00401210 <_cygwin_premain3>: 00401220 <___main>: 

note on windows using cygwin results differ slightly. looks main lives @ 00401170 me.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -