xref: /illumos-gate/usr/src/lib/efcode/engine/extend.c (revision 09e6639b)
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 #include <stdio.h>
28 #include <sys/shm.h>
29 #include <dlfcn.h>
30 #include <fcode/private.h>
31 
32 static void
do_dlopen(fcode_env_t * env)33 do_dlopen(fcode_env_t *env)
34 {
35 	char *name;
36 	int mode;
37 	void *pl;
38 
39 	mode = POP(DS);
40 	name = pop_a_string(env, NULL);
41 	pl = dlopen(name, mode);
42 	PUSH(DS, (fstack_t)pl);
43 }
44 
45 static void
do_extend(fcode_env_t * env)46 do_extend(fcode_env_t *env)
47 {
48 	parse_word(env);
49 	PUSH(DS, (fstack_t)RTLD_NOW);
50 	do_dlopen(env);
51 	drop(env);
52 }
53 
54 static void
do_dlclose(fcode_env_t * env)55 do_dlclose(fcode_env_t *env)
56 {
57 	void *pl = (void *)POP(DS);
58 	(void) dlclose(pl);
59 }
60 
61 static void
do_dlsym(fcode_env_t * env)62 do_dlsym(fcode_env_t *env)
63 {
64 	char *name;
65 	fstack_t d;
66 
67 	name = pop_a_string(env, NULL);
68 	d = POP(DS);
69 	d = (fstack_t)dlsym((void *) d, name);
70 	PUSH(DS, d);
71 }
72 
73 static void
do_dlexec(fcode_env_t * env)74 do_dlexec(fcode_env_t *env)
75 {
76 	int args;
77 	fstack_t a, b, c, d;
78 	fstack_t (*fn0)(void);
79 	fstack_t (*fn1)(fstack_t);
80 	fstack_t (*fn2)(fstack_t, fstack_t);
81 	fstack_t (*fn3)(fstack_t, fstack_t, fstack_t);
82 	fstack_t (*fn4)(fstack_t, fstack_t, fstack_t, fstack_t);
83 
84 	args = POP(DS);
85 	a = POP(DS);
86 	switch (args) {
87 
88 	case 0:
89 		fn0 = (fstack_t (*)(void)) a;
90 		a = fn0();
91 		PUSH(DS, a);
92 		break;
93 
94 	case 1:
95 		fn1 = (fstack_t (*)(fstack_t)) a;
96 		a = POP(DS);
97 		a = fn1(a);
98 		PUSH(DS, a);
99 		break;
100 
101 	case 2:
102 		fn2 = (fstack_t (*)(fstack_t, fstack_t))a;
103 		a = POP(DS);
104 		b = POP(DS);
105 		a = fn2(a, b);
106 		PUSH(DS, a);
107 		break;
108 
109 	case 3:
110 		fn3 = (fstack_t (*)(fstack_t, fstack_t, fstack_t))a;
111 		a = POP(DS);
112 		b = POP(DS);
113 		c = POP(DS);
114 		a = fn3(a, b, c);
115 		PUSH(DS, a);
116 		break;
117 
118 	case 4:
119 		fn4 = (fstack_t (*)(fstack_t, fstack_t, fstack_t, fstack_t))a;
120 		a = POP(DS);
121 		b = POP(DS);
122 		c = POP(DS);
123 		d = POP(DS);
124 		a = fn4(a, b, c, d);
125 		PUSH(DS, a);
126 		break;
127 	}
128 }
129 
130 #pragma init(_init)
131 
132 static void
_init(void)133 _init(void)
134 {
135 	fcode_env_t *env = initial_env;
136 
137 	ASSERT(env);
138 	NOTICE;
139 
140 	FORTH(0,		"dl-open",	do_dlopen);
141 	FORTH(0,		"dl-close",	do_dlclose);
142 	FORTH(0,		"dl-sym",	do_dlsym);
143 	FORTH(0,		"dl-exec",	do_dlexec);
144 	FORTH(IMMEDIATE,	"extend-from",	do_extend);
145 }
146