Creating a C Library: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
m syntax highlighting
Line 26: Line 26:


Below is a simple implementation of crt0.s for x86_64. It assumes that the program loader has put *argv and *envp on the stack, and that %rdi contains argc, %rsi contains argv, %rdx contains envc, and %rcx contains envp
Below is a simple implementation of crt0.s for x86_64. It assumes that the program loader has put *argv and *envp on the stack, and that %rdi contains argc, %rsi contains argv, %rdx contains envc, and %rcx contains envp
<source lang="asm">
<pre>
.section .text
.section .text


Line 58: Line 58:
call exit
call exit
.size _start, . - _start
.size _start, . - _start
</pre>
</source>


This implementation is careful to set up the end of the stack frame linked list. If you compile your files without optimization or you use -fno-omit-frame-pointer, then each function adds itself to this linked list. This is very useful if you wish to add calltracing support. In that case, you'll need to know when you have reached the end, which is why we add an explicit zero in the above code..
This implementation is careful to set up the end of the stack frame linked list. If you compile your files without optimization or you use -fno-omit-frame-pointer, then each function adds itself to this linked list. This is very useful if you wish to add calltracing support. In that case, you'll need to know when you have reached the end, which is why we add an explicit zero in the above code..
Line 75: Line 75:


Hence an crti.s implementation will simply be (x86_64):
Hence an crti.s implementation will simply be (x86_64):
<source lang="asm">
<pre>
.section .init
.section .init
.global _init
.global _init
Line 89: Line 89:
movq %rsp, %rbp
movq %rsp, %rbp
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */
</pre>
</source>


and a simple implementation of crtn.s will be (x86_64):
and a simple implementation of crtn.s will be (x86_64):


<source lang="asm">
<pre>
.section .init
.section .init
/* gcc will nicely put the contents of crtend.o's .init section here. */
/* gcc will nicely put the contents of crtend.o's .init section here. */
Line 103: Line 103:
popq %rbp
popq %rbp
ret
ret
</pre>
</source>


Finally, you simply need to assemble your crt0.o, crti.o, and crtn.o files and install them in your system library directory. Your _start function is now able to set up the standard library, call the global constructors, and call exit(main(argc, argv)). Don't forget to call your _fini function in your exit function, or the global destructors won't be run, leading to subtle bugs.
Finally, you simply need to assemble your crt0.o, crti.o, and crtn.o files and install them in your system library directory. Your _start function is now able to set up the standard library, call the global constructors, and call exit(main(argc, argv)). Don't forget to call your _fini function in your exit function, or the global destructors won't be run, leading to subtle bugs.