xref: /onnv-gate/usr/src/lib/efcode/engine/extend.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 2000 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <sys/shm.h>
31*0Sstevel@tonic-gate #include <dlfcn.h>
32*0Sstevel@tonic-gate #include <fcode/private.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate static void
do_dlopen(fcode_env_t * env)35*0Sstevel@tonic-gate do_dlopen(fcode_env_t *env)
36*0Sstevel@tonic-gate {
37*0Sstevel@tonic-gate 	char *name;
38*0Sstevel@tonic-gate 	int mode;
39*0Sstevel@tonic-gate 	void *pl;
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate 	mode = POP(DS);
42*0Sstevel@tonic-gate 	name = pop_a_string(env, NULL);
43*0Sstevel@tonic-gate 	pl = dlopen(name, mode);
44*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)pl);
45*0Sstevel@tonic-gate }
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static void
do_extend(fcode_env_t * env)48*0Sstevel@tonic-gate do_extend(fcode_env_t *env)
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate 	parse_word(env);
51*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)RTLD_NOW);
52*0Sstevel@tonic-gate 	do_dlopen(env);
53*0Sstevel@tonic-gate 	drop(env);
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate static void
do_dlclose(fcode_env_t * env)57*0Sstevel@tonic-gate do_dlclose(fcode_env_t *env)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate 	void *pl = (void *)POP(DS);
60*0Sstevel@tonic-gate 	dlclose(pl);
61*0Sstevel@tonic-gate }
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate static void
do_dlsym(fcode_env_t * env)64*0Sstevel@tonic-gate do_dlsym(fcode_env_t *env)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate 	char *name;
67*0Sstevel@tonic-gate 	fstack_t d;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	name = pop_a_string(env, NULL);
70*0Sstevel@tonic-gate 	d = POP(DS);
71*0Sstevel@tonic-gate 	d = (fstack_t)dlsym((void *) d, name);
72*0Sstevel@tonic-gate 	PUSH(DS, d);
73*0Sstevel@tonic-gate }
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate static void
do_dlexec(fcode_env_t * env)76*0Sstevel@tonic-gate do_dlexec(fcode_env_t *env)
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate 	int args;
79*0Sstevel@tonic-gate 	fstack_t a, b, c, d;
80*0Sstevel@tonic-gate 	fstack_t (*fn0)(void);
81*0Sstevel@tonic-gate 	fstack_t (*fn1)(fstack_t);
82*0Sstevel@tonic-gate 	fstack_t (*fn2)(fstack_t, fstack_t);
83*0Sstevel@tonic-gate 	fstack_t (*fn3)(fstack_t, fstack_t, fstack_t);
84*0Sstevel@tonic-gate 	fstack_t (*fn4)(fstack_t, fstack_t, fstack_t, fstack_t);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	args = POP(DS);
87*0Sstevel@tonic-gate 	a = POP(DS);
88*0Sstevel@tonic-gate 	switch (args) {
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	case 0:
91*0Sstevel@tonic-gate 		fn0 = (fstack_t (*)(void)) a;
92*0Sstevel@tonic-gate 		a = fn0();
93*0Sstevel@tonic-gate 		PUSH(DS, a);
94*0Sstevel@tonic-gate 		break;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	case 1:
97*0Sstevel@tonic-gate 		fn1 = (fstack_t (*)(fstack_t)) a;
98*0Sstevel@tonic-gate 		a = POP(DS);
99*0Sstevel@tonic-gate 		a = fn1(a);
100*0Sstevel@tonic-gate 		PUSH(DS, a);
101*0Sstevel@tonic-gate 		break;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	case 2:
104*0Sstevel@tonic-gate 		fn2 = (fstack_t (*)(fstack_t, fstack_t))a;
105*0Sstevel@tonic-gate 		a = POP(DS);
106*0Sstevel@tonic-gate 		b = POP(DS);
107*0Sstevel@tonic-gate 		a = fn2(a, b);
108*0Sstevel@tonic-gate 		PUSH(DS, a);
109*0Sstevel@tonic-gate 		break;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	case 3:
112*0Sstevel@tonic-gate 		fn3 = (fstack_t (*)(fstack_t, fstack_t, fstack_t))a;
113*0Sstevel@tonic-gate 		a = POP(DS);
114*0Sstevel@tonic-gate 		b = POP(DS);
115*0Sstevel@tonic-gate 		c = POP(DS);
116*0Sstevel@tonic-gate 		a = fn3(a, b, c);
117*0Sstevel@tonic-gate 		PUSH(DS, a);
118*0Sstevel@tonic-gate 		break;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	case 4:
121*0Sstevel@tonic-gate 		fn4 = (fstack_t (*)(fstack_t, fstack_t, fstack_t, fstack_t))a;
122*0Sstevel@tonic-gate 		a = POP(DS);
123*0Sstevel@tonic-gate 		b = POP(DS);
124*0Sstevel@tonic-gate 		c = POP(DS);
125*0Sstevel@tonic-gate 		d = POP(DS);
126*0Sstevel@tonic-gate 		a = fn4(a, b, c, d);
127*0Sstevel@tonic-gate 		PUSH(DS, a);
128*0Sstevel@tonic-gate 		break;
129*0Sstevel@tonic-gate 	}
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate #pragma init(_init)
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate static void
_init(void)135*0Sstevel@tonic-gate _init(void)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	ASSERT(env);
140*0Sstevel@tonic-gate 	NOTICE;
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	FORTH(0,		"dl-open",	do_dlopen);
143*0Sstevel@tonic-gate 	FORTH(0,		"dl-close",	do_dlclose);
144*0Sstevel@tonic-gate 	FORTH(0,		"dl-sym",	do_dlsym);
145*0Sstevel@tonic-gate 	FORTH(0,		"dl-exec",	do_dlexec);
146*0Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"extend-from",	do_extend);
147*0Sstevel@tonic-gate }
148