In an effort to understand the in's and out's of a POSIX libc implementation I have been making my own for the past year or so now by nibbling at the edges of the Single Unix Specification implementing function I believed to be within my skillset while trying to map out a plan on moving forward and I've come to the point where the next step is to create a runtime library.
Now I am trying to understand the logic behind how programs are linked to the c runtime library.
Reading what the gcc manual said about the initialization as well as the elf format text and some texted on embedded systems I've basically learned that the initialization code goes into it's own section .init and .fini, that the crti.o and crtn.o normally contain function stubs that are crtbegin and crtend flesh out by calling a series of functions specified in a list of function pointers that a linker script is supposed to define.
This leads me to my questions assuming that my summarization above is correct what is the logic behind designing the init and fini functions in such a way specifically in the way described below:
The prologue of a function (__init) appears in the .init section of crti.o; the epilogue appears in crtn.o. Likewise for the function __fini in the .fini section. Normally these files are provided by the operating system or by the GNU C library, but are provided by GCC for a few targets. The objects crtbegin.o and crtend.o are (for most targets) compiled from crtstuff.c. They contain, among other things, code fragments within the .init and .fini sections that branch to routines in the .text section. The linker will pull all parts of a section together, which results in a complete __init function that invokes the routines we need at startup.
Why wouldn't the library init and fini functions just directly call the library initialization and destruction functions directly instead of listing them in a linker script and spreading bits of the section across several object files?
Also where do the functions that would be listed in the linker list come from. From the quote referenced below I am assuming that these function are specific to the crt to say associate std[in,out,err] file descriptors with the file streams or initialate the locale_t variable to C locale etc, if that is the case where these functions stored.
The linker must build two lists of these functions—a list of initialization functions, called CTOR_LIST, and a list of termination functions, called DTOR_LIST.