下面就是动态链接器的入口。 /* Initial entry point code for the dynamic linker. The C function `_dl_start' is the real entry point; its return value is the user program's entry point. */
#define RTLD_START asm ("\ .text\n\ .globl _start\n\ .globl _dl_start_user\n\ _start:\n\ pushl %esp\n\ call _dl_start\n\/*该函数返回时候,%eax中存放着user entry point address*/ popl %ebx\n\/*%ebx放着是esp的内容*/ _dl_start_user:\n\ # Save the user entry point address in %edi.\n\ movl %eax, %edi\n\/*入口地址放在%edi*/
# Point %ebx at the GOT. call 0f\n\ 0: popl %ebx\n\ addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx\n\
# Store the highest stack address\n\ movl __libc_stack_end@GOT(%ebx), %eax\n\ movl %esp, (%eax)\n\/*把栈顶%esp放到GOT的__libc_stack_end中*/
# See if we were run as a command with the executable file\n\ # name as an extra leading argument.\n\ movl _dl_skip_args@GOT(%ebx), %eax\n\ movl (%eax), %eax\n\
# Pop the original argument count.\n\ popl %ecx\n\
# Subtract _dl_skip_args from it.\n\ subl %eax, %ecx\n\
# Adjust the stack pointer to skip _dl_skip_args words.\n\ leal (%esp,%eax,4), %esp\n\
# Push back the modified argument count.\n\ pushl %ecx\n\
# Push the searchlist of the main object as argument in\n\ # _dl_init_next call below.\n\ movl _dl_main_searchlist@GOT(%ebx), %eax\n\ movl (%eax), %esi\n\ 0: movl %esi,%eax\n\
# Call _dl_init_next to return the address of an initializer\n\ # function to run.\n\ call _dl_init_next@PLT\n\/*该函数返回初始化函数的地址,返回地址放在%eax中*/
# Check for zero return, when out of initializers.\n\ testl %eax, %eax\n\ jz 1f\n\
# Call the shared object initializer function.\n\ # NOTE: We depend only on the registers (%ebx, %esi and %edi)\n\ # and the return address pushed by this call;\n\ # the initializer is called with the stack just\n\ # as it appears on entry, and it is free to move\n\ # the stack around, as long as it winds up jumping to\n\ # the return address on the top of the stack.\n\ call *%eax\n\/*调用共享object初始化函数*/
# Loop to call _dl_init_next for the next initializer.\n\ jmp 0b\n\
# Pass our finalizer function to the user in %edx, as per ELF ABI.\n\ movl _dl_fini@GOT(%ebx), %edx\n\
# Jump to the user's entry point.\n\ jmp *%edi\n\ .previous\n\ ");
sysdeps\i386\start.s中 user's entry也就是下面的_start例程
/* This is the canonical entry point, usually the first thing in the text segment. The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry point runs, most registers' values are unspecified, except for:
%edx Contains a function pointer to be registered with `atexit'. This is how the dynamic linker arranges to have DT_FINI functions called for shared libraries that have been loaded before this code runs.
%esp The stack contains the arguments and environment: 0(%esp) argc 4(%esp) argv[0] ... (4*argc)(%esp) NULL (4*(argc+1))(%esp) envp[0] ... NULL */
.text .globl _start _start: /* Clear the frame pointer. The ABI suggests this be done, to mark the outermost frame obviously. */ xorl %ebp, %ebp
/* Extract the arguments as encoded on the stack and set up the arguments for `main': argc, argv. envp will be determined later in __libc_start_main. */ popl %esi /* Pop the argument count. */ movl %esp, %ecx /* argv starts just at the current stack top.*/
/* Before pushing the arguments align the stack to a double word boundary to avoid penalties from misaligned accesses. Thanks to Edward Seidl for pointing this out. */ andl $0xfffffff8, %esp pushl %eax /* Push garbage because we allocate 28 more bytes. */
/* Provide the highest stack address to the user code (for stacks which grow downwards). */ pushl %esp
pushl %edx /* Push address of the shared library termination function. */
/* Push address of our own entry points to .fini and .init. */ pushl $_fini pushl $_init
pushl %ecx /* Push second argument: argv. */ pushl %esi /* Push first argument: argc. */
pushl $main
/* Call the user's main function, and exit with its value. But let the libc call main. */ call __libc_start_main
/* the PPC SVR4 ABI says that the top thing on the stack will be a NULL pointer, so if not we assume that we're being called as a statically-linked program by Linux... */ if (*stack_on_entry != NULL) { /* ...in which case, we have argc as the top thing on the stack, followed by argv (NULL-terminated), envp (likewise), and the auxilary vector. */ argc = *(int *) stack_on_entry; argv = stack_on_entry + 1; envp = argv + argc + 1; auxvec = envp; while (*(char **) auxvec != NULL) ++auxvec; ++auxvec; rtld_fini = NULL; }
/* Store something that has some relationship to the end of the stack, for backtraces. This variable should be thread-specific. */ __libc_stack_end = stack_on_entry + 4;
/* Set the global _environ variable correctly. */ __environ = envp;
/* Register the destructor of the dynamic linker if there is any. */ if (rtld_fini != NULL) atexit (rtld_fini);/*替动态连接器安排destructor*/
/* Call the initializer of the libc. */
__libc_init_first (argc, argv, envp);/*一个空函数*/
/* Register the destructor of the program, if any. */ if (stinfo->fini) atexit (stinfo->fini);/*安排程序自己的destructor*/
/* Call the initializer of the program, if any. */
/*运行程序的初始化函数*/ if (stinfo->init) stinfo->init (argc, argv, __environ, auxvec);
/* Run initializers for MAP and its dependencies, in inverse dependency order (that is, leaf nodes first). */
ElfW(Addr) internal_function _dl_init_next (struct r_scope_elem *searchlist) { unsigned int i;
/* The search list for symbol lookup is a flat list in top-down dependency order, so processing that list from back to front gets us breadth-first leaf-to-root order. */
i = searchlist->r_nlist; while (i-- > 0) { struct link_map *l = searchlist->r_list[i];
if (l->l_init_called) /* This object is all done. */ continue;
if (l->l_init_running) { /* This object's initializer was just running. Now mark it as having run, so this object will be skipped in the future. */ l->l_init_running = 0; l->l_init_called = 1; continue; }
if (l->l_info[DT_INIT] && (l->l_name[0] != '\0' || l->l_type != lt_executable)) { /* Run this object's initializer. */ l->l_init_running = 1;
/* Print a debug message if wanted. */ if (_dl_debug_impcalls) _dl_debug_message (1, "\ncalling init: ", l->l_name[0] ? l->l_name : _dl_argv[0], "\n\n", NULL);