7d2e9cda7457b6c1f8e50f28857a2410f4616a45
explain-dot-md.md
... | ... | @@ -25,4 +25,6 @@ |
25 | 25 | * ["How we get to `main()`" (video)](https://www.youtube.com/watch?v=dOfucXtyEsU) |
26 | 26 | * ["Everything you always wanted to know about Hello World" (video)](https://archive.fosdem.org/2017/schedule/event/hello_world/) |
27 | 27 | * [link-time GNU hash stuff (src)](https://sourceware.org/git/?p=binutils.git;a=blob_plain;f=bfd/elf.c) |
28 | -* [runtime GNU hash stuff (src)](https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=elf/dl-lookup.c) |
|
... | ... | \ No newline at end of file |
0 | +* [runtime GNU hash stuff (src)](https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=elf/dl-lookup.c) |
|
1 | +* ["Linkers and loaders" (book, pdf), very extensive](http://becbapatla.ac.in/cse/naveenv/docs/LL1.pdf) |
|
2 | + |
explain/crt.md
... | ... | @@ -28,3 +28,84 @@ isn't exactly a hassle anymore. |
28 | 28 | For exiting the program when not using `__libc_start_main`, you can just use a |
29 | 29 | bare syscall, or `int3`. |
30 | 30 | |
31 | +### What are all those crtfoo.o-files for? |
|
32 | + |
|
33 | +(Pilfered from [dev.gentoo.org](https://dev.gentoo.org/~vapier/crt.txt) -pcy) |
|
34 | + |
|
35 | +> Some definitions: |
|
36 | +> PIC - position independent code (-fPIC) |
|
37 | +> PIE - position independent executable (-fPIE -pie) |
|
38 | +> crt - C runtime |
|
39 | +> |
|
40 | +> |
|
41 | +> |
|
42 | +> crt0.o crt1.o etc... |
|
43 | +> Some systems use crt0.o, while some use crt1.o (and a few even use crt2.o |
|
44 | +> or higher). Most likely due to a transitionary phase that some targets |
|
45 | +> went through. The specific number is otherwise entirely arbitrary -- look |
|
46 | +> at the internal gcc port code to figure out what your target expects. All |
|
47 | +> that matters is that whatever gcc has encoded, your C library better use |
|
48 | +> the same name. |
|
49 | +> |
|
50 | +> This object is expected to contain the _start symbol which takes care of |
|
51 | +> bootstrapping the initial execution of the program. What exactly that |
|
52 | +> entails is highly libc dependent and as such, the object is provided by |
|
53 | +> the C library and cannot be mixed with other ones. |
|
54 | +> |
|
55 | +> On uClibc/glibc systems, this object initializes very early ABI requirements |
|
56 | +> (like the stack or frame pointer), setting up the argc/argv/env values, and |
|
57 | +> then passing pointers to the init/fini/main funcs to the internal libc main |
|
58 | +> which in turn does more general bootstrapping before finally calling the real |
|
59 | +> main function. |
|
60 | +> |
|
61 | +> glibc ports call this file 'start.S' while uClibc ports call this crt0.S or |
|
62 | +> crt1.S (depending on what their gcc expects). |
|
63 | +> |
|
64 | +> crti.o |
|
65 | +> Defines the function prologs for the .init and .fini sections (with the _init |
|
66 | +> and _fini symbols respectively). This way they can be called directly. These |
|
67 | +> symbols also trigger the linker to generate DT_INIT/DT_FINI dynamic ELF tags. |
|
68 | +> |
|
69 | +> These are to support the old style constructor/destructor system where all |
|
70 | +> .init/.fini sections get concatenated at link time. Not to be confused with |
|
71 | +> newer prioritized constructor/destructor .init_array/.fini_array sections and |
|
72 | +> DT_INIT_ARRAY/DT_FINI_ARRAY ELF tags. |
|
73 | +> |
|
74 | +> glibc ports used to call this 'initfini.c', but now use 'crti.S'. uClibc |
|
75 | +> also uses 'crti.S'. |
|
76 | +> |
|
77 | +> crtn.o |
|
78 | +> Defines the function epilogs for the .init/.fini sections. See crti.o. |
|
79 | +> |
|
80 | +> glibc ports used to call this 'initfini.c', but now use 'crtn.S'. uClibc |
|
81 | +> also uses 'crtn.S'. |
|
82 | +> |
|
83 | +> Scrt1.o |
|
84 | +> Used in place of crt1.o when generating PIEs. |
|
85 | +> gcrt1.o |
|
86 | +> Used in place of crt1.o when generating code with profiling information. |
|
87 | +> Compile with -pg. Produces output suitable for the gprof util. |
|
88 | +> Mcrt1.o |
|
89 | +> Like gcrt1.o, but is used with the prof utility. glibc installs this as |
|
90 | +> a dummy file as it's useless on linux systems. |
|
91 | +> |
|
92 | +> crtbegin.o |
|
93 | +> GCC uses this to find the start of the constructors. |
|
94 | +> crtbeginS.o |
|
95 | +> Used in place of crtbegin.o when generating shared objects/PIEs. |
|
96 | +> crtbeginT.o |
|
97 | +> Used in place of crtbegin.o when generating static executables. |
|
98 | +> crtend.o |
|
99 | +> GCC uses this to find the start of the destructors. |
|
100 | +> crtendS.o |
|
101 | +> Used in place of crtend.o when generating shared objects/PIEs. |
|
102 | +> |
|
103 | +> |
|
104 | +> |
|
105 | +> General linking order: |
|
106 | +> crt1.o crti.o crtbegin.o [-L paths] [user objects] [gcc libs] [C libs] [gcc libs] crtend.o crtn.o |
|
107 | +> |
|
108 | +> |
|
109 | +> |
|
110 | +> More references: |
|
111 | +> http://gcc.gnu.org/onlinedocs/gccint/Initialization.html |