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