Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / lib / csu / hppa / crt0.c
blobcccc8d04411c5a9d90f22842e1c08c85b34b8ca6
1 /* $NetBSD: crt0.c,v 1.6 2004/05/17 10:44:22 skrll Exp $ */
3 /*
4 * Copyright (c) 2002 Matt Fredette
5 * Copyright (c) 1999 Klaus Klein
6 * Copyright (c) 1995 Christopher G. Demetriou
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the
20 * NetBSD Project. See http://www.NetBSD.org/ for
21 * information about NetBSD.
22 * 4. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
39 #include "common.h"
41 static void ___start(struct ps_strings *,
42 void (*cleanup)(void), const Obj_Entry *, int)
43 #ifdef __GNUC__
44 __attribute__((__used__))
45 #endif
48 __asm("\n"
49 " .text \n"
50 " .align 4 \n"
51 " .globl _start \n"
52 " .globl __start \n"
53 " .type _start,@function \n"
54 " .type __start,@function \n"
55 "_start: \n"
56 "__start: \n"
57 " .import _GLOBAL_OFFSET_TABLE_ \n"
58 "\n"
59 " bl L$lpc, %r27 \n"
60 " depi 0, 31, 2, %r27 \n"
61 "L$lpc: addil L'_GLOBAL_OFFSET_TABLE_ - ($PIC_pcrel$0 - 8), %r27 \n"
62 " ldo R'_GLOBAL_OFFSET_TABLE_ - ($PIC_pcrel$0 - 12)(%r1),%r27 \n"
63 " copy %r27, %r19 \n"
64 " b ___start \n"
65 " copy %r27, %arg3 \n");
67 static void
68 ___start(struct ps_strings *ps_strings,
69 void (*cleanup)(void), /* from shared loader */
70 const Obj_Entry *obj, /* from shared loader */
71 int dp)
73 int argc;
74 char **argv;
75 int fini_plabel[2];
77 argc = ps_strings->ps_nargvstr;
78 argv = ps_strings->ps_argvstr;
79 environ = ps_strings->ps_envstr;
81 if ((__progname = argv[0]) != NULL) { /* NULL ptr if argc = 0 */
82 if ((__progname = _strrchr(__progname, '/')) == NULL)
83 __progname = argv[0];
84 else
85 __progname++;
88 if (ps_strings != (struct ps_strings *)0)
89 __ps_strings = ps_strings;
91 #ifdef DYNAMIC
93 * XXX fredette - when not compiling PIC, you currently
94 * can't detect an undefined weak symbol by seeing if
95 * its address is NULL. The compiler emits code to find
96 * _DYNAMIC relative to %dp, the assembler notes the
97 * needed relocations, but when the linker sees that the
98 * (weak) symbol isn't defined it drops the ball - the
99 * relocations are never filled, and the binary ends up
100 * with code that sees an address of %dp plus zero,
101 * which != NULL.
103 * Arguably the linker could/should distinguish between
104 * code that is after a weak undefined symbol's contents
105 * from code that is after its address. In the first case,
106 * it would warn and/or bail. In the second case, it
107 * would fix up instructions to give a symbol address
108 * of NULL.
110 * For now, we take the easy way out and compare &_DYNAMIC
111 * to %dp, as well as to NULL.
113 if (&_DYNAMIC != NULL && (int)&_DYNAMIC != dp)
114 _rtld_setup(cleanup, obj);
115 #endif
117 #ifdef MCRT0
118 atexit(_mcleanup);
119 monstartup((u_long)&_eprol, (u_long)&_etext);
120 #endif
123 * Since crt0.o, crtbegin.o, and crtend.o are always
124 * compiled PIC, they must have %r19 set correctly on
125 * entry to any function they contain. However, when
126 * a program is linked statically, the linker does
127 * not fill a PLABEL relocation with a pointer to a
128 * true PLABEL, it just fills it with the offset of the
129 * function. This shows the linker's assumption that
130 * when linking statically, *all* of the code has *not*
131 * been compiled PIC. I guess to assume otherwise
132 * would be a performance hit, as you would end up
133 * with unnecessary PLABELs for function pointers.
135 * But here, passing the address of the PIC _fini to
136 * atexit, we must make sure that we pass a PLABEL.
138 fini_plabel[0] = (int)_fini;
139 if (fini_plabel[0] & 2)
140 /* _fini is already a PLABEL. */
141 atexit(_fini);
142 else {
143 fini_plabel[1] = dp;
144 atexit((void (*)(void))(((int)fini_plabel) | 2));
146 _init();
148 exit(main(argc, argv, environ));
152 * NOTE: Leave the RCS ID _after_ __start(), in case it gets placed in .text.
154 #if defined(LIBC_SCCS) && !defined(lint)
155 __RCSID("$NetBSD: crt0.c,v 1.6 2004/05/17 10:44:22 skrll Exp $");
156 #endif /* LIBC_SCCS and not lint */
158 #include "common.c"