xref: /minix3/lib/csu/README (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
184d9c625SLionel SambucIntroduction
284d9c625SLionel Sambuc
3*0a6a1f1dSLionel SambucThis document covers the native NetBSD compiler runtime.
484d9c625SLionel Sambuc
584d9c625SLionel SambucMachine independent sources can be found in common. The crtbegin.c in
684d9c625SLionel Sambucthat directory is a useful template for deriving compact assembler
784d9c625SLionel Sambucversions. That is preferable to decouple the result from changes in the
884d9c625SLionel Sambuccompiler logic.
984d9c625SLionel Sambuc
1084d9c625SLionel SambucA new platform should provide the following content in
1184d9c625SLionel Sambucarch/${MACHINE_ARCH} or arch/${MACHINE_CPU}:
1284d9c625SLionel Sambuc- Makefile.inc: provides ELFSIZE corresponding to 32/64bit file format.
1384d9c625SLionel Sambuc  If using the common C code instead of crtbegin.S also provide a -I option
1484d9c625SLionel Sambuc  to find crtbegin.h in your arch subdir.
1584d9c625SLionel Sambuc- crt0.S: provides setup code and the call to ___start.
1684d9c625SLionel Sambuc- crtbegin.S or crtbegin.h: see below
1784d9c625SLionel Sambuc- crtend.S: see below, most likely just a copy of an existing architecture
1884d9c625SLionel Sambuc- crti.S: prefix part of .init/.fini sections, i.e. to ensure stack alignment
1984d9c625SLionel Sambuc- crtn.S: suffix part of the .init/.fini sections, i.e. return to caller.
2084d9c625SLionel Sambuc
2184d9c625SLionel Sambuc
22e83f7ba2SBen GrasOverview of the common runtime support
23e83f7ba2SBen Gras
24e83f7ba2SBen GrasThe common runtime support contains two modules, crtbegin and crtend.
25e83f7ba2SBen Grascrtbegin is linked before all other object files of the program or
26e83f7ba2SBen Grasdynamic library, crtend after all other object files.  They frame the
27e83f7ba2SBen Graslists of constructors, destructors, Java types and exception handling frames.
28e83f7ba2SBen Gras
29e83f7ba2SBen GrasIf done correctly, crtend contains no code and is therefore position
30e83f7ba2SBen Grasindependent.  crtendS.o is therefore just a link to crtend.o.
31e83f7ba2SBen Gras
32e83f7ba2SBen Grascrtbegin should be position-independent code.  crtbeginT.o doesn't have
33e83f7ba2SBen Grasto be PIC as it is statically linked.  The overhead is generally not
34e83f7ba2SBen Grasworth the trouble though.
35e83f7ba2SBen Gras
36e83f7ba2SBen Gras
37e83f7ba2SBen GrasSection types:
38e83f7ba2SBen Gras.ctor: writeable
39e83f7ba2SBen Gras.dtor: writeable
40e83f7ba2SBen Gras.eh_frame: read-only if platform allows mixing read-only and read-write
41e83f7ba2SBen Grassections.  This is supported by GNU ld.
42e83f7ba2SBen Gras.jcr: writeable
43e83f7ba2SBen Gras.init: executable
44e83f7ba2SBen Gras.fini: executable
45e83f7ba2SBen Gras
46e83f7ba2SBen Gras
47e83f7ba2SBen GrasNon-local symbols:
48e83f7ba2SBen Gras
49e83f7ba2SBen GrasWeak references:
50e83f7ba2SBen Gras- _Jv_RegisterClasses,
51e83f7ba2SBen Gras- __cxa_finalize (crtbeginS.o)
52e83f7ba2SBen Gras- __deregister_frame_info
53e83f7ba2SBen Gras- __register_frame_info
54e83f7ba2SBen Gras
55e83f7ba2SBen GrasHidden:
56e83f7ba2SBen Gras- __dso_handle: pointer to self for crtbeginS.o, NULL otherwise.
57e83f7ba2SBen Gras- __CTOR_LIST_END__
58e83f7ba2SBen Gras
59e83f7ba2SBen Gras
60e83f7ba2SBen GrasInitialisation (called from .init):
61e83f7ba2SBen Gras
62e83f7ba2SBen Gras1.  Check that the init code hasn't started already, otherwise bail out.
63e83f7ba2SBen Gras2.  If __register_frame_info is NULL, skip to 4
64e83f7ba2SBen Gras3.  Call __register_frame_info with start of .eh_frame as first argument
65e83f7ba2SBen Gras    and a data object of at least 8 pointers as second argument.
66e83f7ba2SBen Gras4:  If _Jv_RegisterClasses is NULL, skip to 6
67e83f7ba2SBen Gras5:  Call _Jv_RegisterClasses with the first pointer of the .jcr section
68e83f7ba2SBen Gras    as argument.
69e83f7ba2SBen Gras6:  Iterate from the end of the .ctor section to the start.  Skip the
70e83f7ba2SBen Gras    terminating NULL and stop when reaching the starting (void *)-1 element.
71e83f7ba2SBen Gras    Call the pointers as void (*)(void) functions.
72e83f7ba2SBen Gras
73e83f7ba2SBen Gras
74e83f7ba2SBen GrasDeinitialisation (called from .fini):
75e83f7ba2SBen Gras
76e83f7ba2SBen Gras1.  Check if the init code has already started, otherwise bail out.
77e83f7ba2SBen Gras2.  If this is not crtbeginS.o or __cxa_finalize is NULL, skip to 4.
78e83f7ba2SBen Gras3.  Call __cxa_finalize with a pointer into this Dynamic Shared Object (DSO)
79e83f7ba2SBen Gras    as first argument.
80e83f7ba2SBen Gras4.  Iterate from the start of the .dtor section to the send.  Skip the
81e83f7ba2SBen Gras    initial (void *)-1 and stop when reaching the terminating NULL element.
82e83f7ba2SBen Gras    Call the pointers as void (*)(void) functions.
83e83f7ba2SBen Gras5.  If __deregister_frame_info is NULL, return.
84e83f7ba2SBen Gras6.  Call __deregister_frame_info with the start of .eh_frame as the argument.
8584d9c625SLionel Sambuc
8684d9c625SLionel Sambuc
8784d9c625SLionel SambucSince most of this can easily be done in C code, instead of providing a
8884d9c625SLionel Sambuccrtbegin.S you can also chose to use the generic C implementation. Provide
8984d9c625SLionel Sambuca crtbegin.h file instead of crtbegin.S. In there put inline assembler
9084d9c625SLionel Sambucstubs (mostly copied from some other arch) and implement calls to the
9184d9c625SLionel Sambuchelper functions __do_global_ctors_aux/__do_global_dtors_aux.
92