static ElfW(Addr) _attributeused__ dlstart (void *arg) { #ifdef DONTUSEBOOTSTRAPMAP…
- User
static ElfW(Addr) attribute_used _dl_start (void *arg) { #ifdef DONT_USE_BOOTSTRAP_MAP rtld_timer_start (&start_time); #else struct dl_start_final_info info; rtld_timer_start (&info.start_time); #endif
/* Partly clean the
bootstrap_map' structure up. Don't usememset' since it might not be built in or inlined and we cannot make function calls at this point. Use '__builtin_memset' if we know it is available. We do not have to clear the memory if we do not have to use the temporary bootstrap_map. Global variables are initialized to zero by default. */ #ifndef DONT_USE_BOOTSTRAP_MAPifdef HAVE_BUILTIN_MEMSET
__builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
else
for (size_t cnt = 0; cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]); ++cnt) bootstrap_map.l_info[cnt] = 0;
endif
#endif
/* Figure out the run-time load address of the dynamic linker itself. */ bootstrap_map.l_addr = elf_machine_load_address ();
/* Read our own dynamic section and fill in the info array. */ bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic (); bootstrap_map.l_ld_readonly = DL_RO_DYN_SECTION; elf_get_dynamic_info (&bootstrap_map, true, false);
#if NO_TLS_OFFSET != 0 bootstrap_map.l_tls_offset = NO_TLS_OFFSET; #endif
#ifdef ELF_MACHINE_BEFORE_RTLD_RELOC ELF_MACHINE_BEFORE_RTLD_RELOC (&bootstrap_map, bootstrap_map.l_info); #endif
if (bootstrap_map.l_addr) {
/* Relocate ourselves so we can do normal function calls and data access using the global offset table. */ELF_DYNAMIC_RELOCATE (&bootstrap_map, NULL, 0, 0, 0); }bootstrap_map.l_relocated = 1;
/* Please note that we don't allow profiling of this object and therefore need not test whether we have to allocate the array for the relocation results (as done in dl-reloc.c). */
/* Now life is sane; we can call functions and access global data. Set up to use the operating system facilities, and find out from the operating system's program loader where to find the program header table in core. Put the rest of _dl_start into a separate function, that way the compiler cannot put accesses to the GOT before ELF_DYNAMIC_RELOCATE. */ #ifdef DONT_USE_BOOTSTRAP_MAP return _dl_start_final (arg); #else return _dl_start_final (arg, &info); #endif }
解释此函数
- Assistant
Excellent snippet—you’ve just jumped into the primordial soup of the GNU dynamic linker (glibc’s
ld.so). This function_dl_startis basically the very first step executed when the dynamic linker (ld-linux.so) itself comes alive. Think of it as the linker waking up, rubbing its eyes, and preparing its environment so it can properly relocate itself and then finish initialization.Let’s carefully unwrap what’s going on.
What this function is
_dl_startis the entrypoint of the dynamic linker when it is loaded by the kernel. The kernel gives control to it before your actual program runs, and_dl_start’s job is to bootstrap everything so that the dynamic linker can then relocate itself, parse shared object headers, and eventually resolve symbols before passing control to the main program.It’s written in “careful C with some assembly sprinkled around” style, because at this moment no normal library services can be assumed to work. Even functions like
memsetmay not yet be available, hence all this defensive coding.
Step-by-step breakdown
1. Timer setup
#ifdef DONT_USE_BOOTSTRAP_MAP rtld_timer_start (&start_time); #else struct dl_start_final_info info; rtld_timer_start (&info.start_time); #endif- Initializes timing information for runtime loader profiling/debugging.
- Either uses a global timers variable or a local
info.start_time, depending on configuration.
2. Preparing the bootstrap map
__builtin_memset (bootstrap_map.l_info, '\0', sizeof (...));Here,
bootstrap_mapis a structure representing the dynamic linker itself (treated as if it’s a shared object).
Why memset manually? Because:- At this point, unnecessary function calls (like a regular
memsetfrom libc) are not yet safe. - Global variables are zero-initialized, but this is double-safety and applies selectively to some fields.
So it clears out
l_info, which is where parsing of the ELF dynamic section will be stored.
3. Find the linker’s load address
bootstrap_map.l_addr = elf_machine_load_address ();This calculates at what address the linker was actually mapped into memory by the kernel (the load base).
This is crucial since all relocations (fixing pointers, symbol addresses, etc.) depend on knowing this base.
4. Identify its dynamic section
bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic(); bootstrap_map.l_ld_readonly = DL_RO_DYN_SECTION; elf_get_dynamic_info (&bootstrap_map, true, false);elf_machine_dynamic()gives the offset to the linker's own.dynamicsection..dynamicholds key runtime metadata: needed libraries, relocation tables, symbol tables.elf_get_dynamic_infoparses that section and fillsl_infowith structured pointers.
This is essentially the linker reading its own recipe card.
5. Handle TLS offset if necessary
bootstrap_map.l_tls_offset = NO_TLS_OFFSET;If thread-local storage (TLS) needs special handling, initialize here.
6. Architecture-specific early relocations
#ifdef ELF_MACHINE_BEFORE_RTLD_RELOC ELF_MACHINE_BEFORE_RTLD_RELOC (&bootstrap_map, bootstrap_map.l_info); #endifSome architectures (looking at you, HPPA/SPARC) require setup before doing the general relocation dance.
7. Self-relocation
if (bootstrap_map.l_addr) ELF_DYNAMIC_RELOCATE (&bootstrap_map, NULL, 0, 0, 0); bootstrap_map.l_relocated = 1;This is the magic step:
The dynamic linker applies relocations to itself, fixing up all GOT/PLT entries so it can safely call normal functions and access global variables.Before this, even a simple global variable access could point to nonsense.
After this, the linker can "stand up straight" and use its own normal code without worrying.
8. Transition to the final startup
return _dl_start_final(arg, &info);Once the linker is fully self-relocated and safe to operate, it calls
_dl_start_final.
This function—not this half-cooked bootstrapper—handles the “real work”: looking at the program headers, preparing the link map for the program being launched, relocating that program and its shared objects, and finally handing control over tomain.
Intuitive metaphor
Imagine the dynamic linker as a mechanic who wakes up inside a car engine and must:
- Put on his own glasses (bootstrap map init).
- Look around and realize where the heck he was dropped (compute load address).
- Pull out his own user manual (
.dynamic). - Patch up his own joints so he can actually move without falling apart (self-relocation).
- Only once he can stand normally, he then starts fixing the actual car (loading and relocating your program’s shared libraries).
Summary
_dl_startis the bootstrap function of the GNU dynamic linker. Its critical jobs are:- Initialize minimal data structures (
bootstrap_map). - Figure out where it was loaded and parse its own ELF metadata.
- Relocate itself so it can start working like a normal dynamic linker.
- Then delegate to
_dl_start_final, which does the heavy lifting of loading your program and its dependencies.
It’s highly defensive, low-level code—because at this early stage, the runtime environment is extremely fragile. Think of it as "linker CPR."
Would you like me to also show a high-level diagram flow of `_dl_start → _dl_start_final → program startup* (from kernel → ld.so → user main)?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.