xref: /titanic_41/usr/src/tools/tokenize/forth_preload.c (revision 391647d5ee9b25dc5307abb55f583388e08b2dd7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This preload library must be applied to forth after libthread is
31  * folded into libc because forth/tokenize.exe is not ABI compliant; it
32  * uses all of the %g registers, including %g7, for its internal purposes.
33  * This preload library interposes on all of the external calls made
34  * from forth/tokenize.exe and, assuming that forth is single-threaded,
35  * sets %g7 properly for use inside libc and restores it to forth's
36  * use on return from the interposed-upon function.
37  */
38 
39 enum ix {
40 	ix___filbuf = 0,
41 	ix___flsbuf,
42 	ix__dgettext,
43 	ix__exit,
44 	ix_access,
45 	ix_atexit,
46 	ix_atoi,
47 	ix_cfgetospeed,
48 	ix_chdir,
49 	ix_close,
50 	ix_exit,
51 	ix_exit_handler,
52 	ix_fclose,
53 	ix_fflush,
54 	ix_fgetc,
55 	ix_fileno,
56 	ix_fopen,
57 	ix_fprintf,
58 	ix_fputc,
59 	ix_fputs,
60 	ix_fread,
61 	ix_free,
62 	ix_fseek,
63 	ix_fstat,
64 	ix_ftell,
65 	ix_fwrite,
66 	ix_getcwd,
67 	ix_getenv,
68 	ix_getopt,
69 	ix_getwd,
70 	ix_ioctl,
71 	ix_isatty,
72 	ix_kill,
73 	ix_localtime,
74 	ix_lseek,
75 	ix_malloc,
76 	ix_memcpy,
77 	ix_memset,
78 	ix_open,
79 	ix_perror,
80 	ix_printf,
81 	ix_psignal,
82 	ix_putchar,
83 	ix_read,
84 	ix_sbrk,
85 	ix_signal,
86 	ix_sigset,
87 	ix_snprintf,
88 	ix_sprintf,
89 	ix_stat,
90 	ix_strcat,
91 	ix_strchr,
92 	ix_strcmp,
93 	ix_strcpy,
94 	ix_strdup,
95 	ix_strlen,
96 	ix_strncmp,
97 	ix_strncpy,
98 	ix_strrchr,
99 	ix_system,
100 	ix_tcgetattr,
101 	ix_tcsetattr,
102 	ix_tgetent,
103 	ix_tgetflag,
104 	ix_tgetnum,
105 	ix_tgetstr,
106 	ix_tgoto,
107 	ix_time,
108 	ix_tputs,
109 	ix_tzset,
110 	ix_ungetc,
111 	ix_unlink,
112 	ix_write
113 };
114 
115 typedef long (*realfunc_t)(long, long, long, long, long, long);
116 
117 struct intpose {
118 	char fname[12];
119 	realfunc_t realfunc;
120 } intpose[] = {
121 	{ "__filbuf",		0 },
122 	{ "__flsbuf",		0 },
123 	{ "_dgettext",		0 },
124 	{ "_exit",		0 },
125 	{ "access",		0 },
126 	{ "atexit",		0 },
127 	{ "atoi",		0 },
128 	{ "cfgetospeed",	0 },
129 	{ "chdir",		0 },
130 	{ "close",		0 },
131 	{ "exit",		0 },
132 	{ "exit_handler",	0 },
133 	{ "fclose",		0 },
134 	{ "fflush",		0 },
135 	{ "fgetc",		0 },
136 	{ "fileno",		0 },
137 	{ "fopen",		0 },
138 	{ "fprintf",		0 },
139 	{ "fputc",		0 },
140 	{ "fputs",		0 },
141 	{ "fread",		0 },
142 	{ "free",		0 },
143 	{ "fseek",		0 },
144 	{ "fstat",		0 },
145 	{ "ftell",		0 },
146 	{ "fwrite",		0 },
147 	{ "getcwd",		0 },
148 	{ "getenv",		0 },
149 	{ "getopt",		0 },
150 	{ "getwd",		0 },
151 	{ "ioctl",		0 },
152 	{ "isatty",		0 },
153 	{ "kill",		0 },
154 	{ "localtime",		0 },
155 	{ "lseek",		0 },
156 	{ "malloc",		0 },
157 	{ "memcpy",		0 },
158 	{ "memset",		0 },
159 	{ "open",		0 },
160 	{ "perror",		0 },
161 	{ "printf",		0 },
162 	{ "psignal",		0 },
163 	{ "putchar",		0 },
164 	{ "read",		0 },
165 	{ "sbrk",		0 },
166 	{ "signal",		0 },
167 	{ "sigset",		0 },
168 	{ "snprintf",		0 },
169 	{ "sprintf",		0 },
170 	{ "stat",		0 },
171 	{ "strcat",		0 },
172 	{ "strchr",		0 },
173 	{ "strcmp",		0 },
174 	{ "strcpy",		0 },
175 	{ "strdup",		0 },
176 	{ "strlen",		0 },
177 	{ "strncmp",		0 },
178 	{ "strncpy",		0 },
179 	{ "strrchr",		0 },
180 	{ "system",		0 },
181 	{ "tcgetattr",		0 },
182 	{ "tcsetattr",		0 },
183 	{ "tgetent",		0 },
184 	{ "tgetflag",		0 },
185 	{ "tgetnum",		0 },
186 	{ "tgetstr",		0 },
187 	{ "tgoto",		0 },
188 	{ "time",		0 },
189 	{ "tputs",		0 },
190 	{ "tzset",		0 },
191 	{ "ungetc",		0 },
192 	{ "unlink",		0 },
193 	{ "write",		0 },
194 };
195 
196 #define	RTLD_NEXT	(void *)-1
197 extern	void	*dlsym(void *handle, const char *name);
198 
199 static	long	global_g7 = -1;
200 
201 long	get_g7(void);
202 void	set_g7(long);
203 
204 static long
205 callfunc(struct intpose *ip,
206 	long a0, long a1, long a2, long a3, long a4, long a5)
207 {
208 	realfunc_t realfunc;
209 	long my_g7;
210 	long rv;
211 
212 	my_g7 = get_g7();
213 	if (global_g7 == -1)
214 		global_g7 = my_g7;
215 	set_g7(global_g7);
216 	if ((realfunc = ip->realfunc) == 0)
217 		ip->realfunc = realfunc =
218 		    (realfunc_t)dlsym(RTLD_NEXT, ip->fname);
219 	rv = realfunc(a0, a1, a2, a3, a4, a5);
220 	set_g7(my_g7);
221 	return (rv);
222 }
223 
224 #define	ipose(func)							\
225 long									\
226 func(long a0, long a1, long a2, long a3, long a4, long a5)		\
227 {									\
228 	return (callfunc(&intpose[ix_##func], a0, a1, a2, a3, a4, a5));	\
229 }
230 
231 ipose(__filbuf)
232 ipose(__flsbuf)
233 ipose(_dgettext)
234 ipose(_exit)
235 ipose(access)
236 ipose(atexit)
237 ipose(atoi)
238 ipose(cfgetospeed)
239 ipose(chdir)
240 ipose(close)
241 ipose(exit)
242 ipose(exit_handler)
243 ipose(fclose)
244 ipose(fflush)
245 ipose(fgetc)
246 ipose(fileno)
247 ipose(fopen)
248 ipose(fprintf)
249 ipose(fputc)
250 ipose(fputs)
251 ipose(fread)
252 ipose(free)
253 ipose(fseek)
254 ipose(fstat)
255 ipose(ftell)
256 ipose(fwrite)
257 ipose(getcwd)
258 ipose(getenv)
259 ipose(getopt)
260 ipose(getwd)
261 ipose(ioctl)
262 ipose(isatty)
263 ipose(kill)
264 ipose(localtime)
265 ipose(lseek)
266 ipose(malloc)
267 ipose(memcpy)
268 ipose(memset)
269 ipose(open)
270 ipose(perror)
271 ipose(printf)
272 ipose(psignal)
273 ipose(putchar)
274 ipose(read)
275 ipose(sbrk)
276 ipose(signal)
277 ipose(sigset)
278 ipose(snprintf)
279 ipose(sprintf)
280 ipose(stat)
281 ipose(strcat)
282 ipose(strchr)
283 ipose(strcmp)
284 ipose(strcpy)
285 ipose(strdup)
286 ipose(strlen)
287 ipose(strncmp)
288 ipose(strncpy)
289 ipose(strrchr)
290 ipose(system)
291 ipose(tcgetattr)
292 ipose(tcsetattr)
293 ipose(tgetent)
294 ipose(tgetflag)
295 ipose(tgetnum)
296 ipose(tgetstr)
297 ipose(tgoto)
298 ipose(time)
299 ipose(tputs)
300 ipose(tzset)
301 ipose(ungetc)
302 ipose(unlink)
303 ipose(write)
304