sync
[bitrig.git] / lib / csu / hppa / crt0.c
blob69d2379b6d4ab81671696c45bc28d53dda91a723
1 /* $OpenBSD: crt0.c,v 1.13 2012/12/22 12:14:32 kettenis Exp $ */
3 /*
4 * Copyright (c) 2001 Michael Shalayeff
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
29 int global __asm ("$global$") = 0;
31 #include <sys/param.h>
32 #include <sys/syscall.h>
33 #include <sys/fcntl.h>
34 #include <sys/exec.h>
35 #include <stdlib.h>
36 #include <paths.h>
38 typedef char Obj_Entry;
41 * Lots of the chunks of this file cobbled together from pieces of
42 * other OpenBSD crt files, including the common code.
45 char **environ;
47 extern void __init(void);
48 extern void __fini(void);
50 #ifdef MCRT0
51 extern void monstartup(u_long, u_long);
52 extern void _mcleanup(void);
53 extern unsigned char etext, eprol;
54 #endif /* MCRT0 */
56 static char *__strrchr(const char *p, char ch);
58 char *__progname = "";
59 char __progname_storage[NAME_MAX+1];
61 void ___start(struct ps_strings *arginfo, void (*cleanup)(void));
63 __asm(
64 ".import $global$, data\n\t"
65 ".import ___start, code\n\t"
66 ".text\n\t"
67 ".align 4\n\t"
68 ".export _start, entry\n\t"
69 ".export __start, entry\n\t"
70 ".type _start,@function\n\t"
71 ".type __start,@function\n\t"
72 ".label _start\n\t"
73 ".label __start\n\t"
74 ".proc\n\t"
75 ".callinfo frame=0, calls\n\t"
76 ".entry\n\t"
77 "bl L$lpc, %r27\n\t"
78 "depi 0, 31, 2, %r27\n\t"
79 "L$lpc: addil L'$global$ - ($PIC_pcrel$0 - 8), %r27\n\t"
80 "ldo R'$global$ - ($PIC_pcrel$0 - 12)(%r1),%r27\n\t"
81 ".call\n\t"
82 "b ___start\n\t"
83 "copy %r27, %r19\n\t"
84 ".exit\n\t"
85 ".procend\n\t");
87 void
88 ___start(struct ps_strings *arginfo, void (*cleanup)(void))
90 char **argv, *namep;
91 char *s;
93 argv = arginfo->ps_argvstr;
94 environ = arginfo->ps_envstr;
95 if ((namep = argv[0]) != NULL) { /* NULL ptr if argc = 0 */
96 if ((__progname = __strrchr(namep, '/')) == NULL)
97 __progname = namep;
98 else
99 __progname++;
100 for (s = __progname_storage; *__progname &&
101 s < &__progname_storage[sizeof __progname_storage - 1]; )
102 *s++ = *__progname++;
103 *s = '\0';
104 __progname = __progname_storage;
107 if (cleanup)
108 atexit(cleanup);
110 #ifdef MCRT0
111 atexit(_mcleanup);
112 monstartup((u_long)&eprol, (u_long)&etext);
113 #endif
115 __init();
117 exit(main(arginfo->ps_nargvstr, argv, environ));
120 static char *
121 __strrchr(const char *p, char ch)
123 char *save;
125 for (save = NULL;; ++p) {
126 if (*p == ch)
127 save = (char *)p;
128 if (!*p)
129 return(save);
133 #ifdef MCRT0
134 __asm (".export eprol, entry\n\t.label eprol");
135 #endif