linux - ELF files and additional symbols -
i'm reading elf file format , i've noticed small hello world test program written in c++ contains additional initialization in _start
symbol:
0000000000400770 <_start>: ... 40077f: 49 c7 c0 60 09 40 00 mov $0x400960,%r8 400786: 48 c7 c1 f0 08 40 00 mov $0x4008f0,%rcx 40078d: 48 c7 c7 5d 08 40 00 mov $0x40085d,%rdi ...
40077f
__libc_csu_fini
.
4008f0
__libc_csu_init
.
40085d
main
.
shouldn't _start
main
? why not? happen if removed both of calls 40077f
, 40008f0
, replaced nop
? basically, significance of requiring libc?
looking @ glibc source code:
/* these functions passed __libc_start_main startup code. these statically linked each program. dynamically linked programs, module come libc_nonshared.a , differs libc.a module in doesn't call preinit array. */ void __libc_csu_init (int argc, char **argv, char **envp) { /* dynamically linked executables preinit array executed dynamic linker (before initializing shared object). */ #ifndef libc_nonshared /* static executables, preinit happens right before init. */ { const size_t size = __preinit_array_end - __preinit_array_start; size_t i; (i = 0; < size; i++) (*__preinit_array_start [i]) (argc, argv, envp); } #endif #ifndef no_initfini _init (); #endif const size_t size = __init_array_end - __init_array_start; (size_t = 0; < size; i++) (*__init_array_start [i]) (argc, argv, envp); } /* function should not used anymore. run executable's destructor other. cannot remove function, though. */ void __libc_csu_fini (void) { #ifndef libc_nonshared size_t = __fini_array_end - __fini_array_start; while (i-- > 0) (*__fini_array_start [i]) (); # ifndef no_initfini _fini (); # endif #endif }
this allow library initialization code run. libraries linked in program can tag functions __attribute__((constructor))
in gcc, , mechanism run functions before main
, allowing libraries initialize before program start.
Comments
Post a Comment