xref: /illumos-gate/usr/src/lib/efcode/engine/extend.c (revision 09e6639b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <sys/shm.h>
297c478bd9Sstevel@tonic-gate #include <dlfcn.h>
307c478bd9Sstevel@tonic-gate #include <fcode/private.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate static void
do_dlopen(fcode_env_t * env)337c478bd9Sstevel@tonic-gate do_dlopen(fcode_env_t *env)
347c478bd9Sstevel@tonic-gate {
357c478bd9Sstevel@tonic-gate 	char *name;
367c478bd9Sstevel@tonic-gate 	int mode;
377c478bd9Sstevel@tonic-gate 	void *pl;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 	mode = POP(DS);
407c478bd9Sstevel@tonic-gate 	name = pop_a_string(env, NULL);
417c478bd9Sstevel@tonic-gate 	pl = dlopen(name, mode);
427c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)pl);
437c478bd9Sstevel@tonic-gate }
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static void
do_extend(fcode_env_t * env)467c478bd9Sstevel@tonic-gate do_extend(fcode_env_t *env)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	parse_word(env);
497c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)RTLD_NOW);
507c478bd9Sstevel@tonic-gate 	do_dlopen(env);
517c478bd9Sstevel@tonic-gate 	drop(env);
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static void
do_dlclose(fcode_env_t * env)557c478bd9Sstevel@tonic-gate do_dlclose(fcode_env_t *env)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	void *pl = (void *)POP(DS);
58*09e6639bSToomas Soome 	(void) dlclose(pl);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static void
do_dlsym(fcode_env_t * env)627c478bd9Sstevel@tonic-gate do_dlsym(fcode_env_t *env)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	char *name;
657c478bd9Sstevel@tonic-gate 	fstack_t d;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	name = pop_a_string(env, NULL);
687c478bd9Sstevel@tonic-gate 	d = POP(DS);
697c478bd9Sstevel@tonic-gate 	d = (fstack_t)dlsym((void *) d, name);
707c478bd9Sstevel@tonic-gate 	PUSH(DS, d);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static void
do_dlexec(fcode_env_t * env)747c478bd9Sstevel@tonic-gate do_dlexec(fcode_env_t *env)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	int args;
777c478bd9Sstevel@tonic-gate 	fstack_t a, b, c, d;
787c478bd9Sstevel@tonic-gate 	fstack_t (*fn0)(void);
797c478bd9Sstevel@tonic-gate 	fstack_t (*fn1)(fstack_t);
807c478bd9Sstevel@tonic-gate 	fstack_t (*fn2)(fstack_t, fstack_t);
817c478bd9Sstevel@tonic-gate 	fstack_t (*fn3)(fstack_t, fstack_t, fstack_t);
827c478bd9Sstevel@tonic-gate 	fstack_t (*fn4)(fstack_t, fstack_t, fstack_t, fstack_t);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	args = POP(DS);
857c478bd9Sstevel@tonic-gate 	a = POP(DS);
867c478bd9Sstevel@tonic-gate 	switch (args) {
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	case 0:
897c478bd9Sstevel@tonic-gate 		fn0 = (fstack_t (*)(void)) a;
907c478bd9Sstevel@tonic-gate 		a = fn0();
917c478bd9Sstevel@tonic-gate 		PUSH(DS, a);
927c478bd9Sstevel@tonic-gate 		break;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	case 1:
957c478bd9Sstevel@tonic-gate 		fn1 = (fstack_t (*)(fstack_t)) a;
967c478bd9Sstevel@tonic-gate 		a = POP(DS);
977c478bd9Sstevel@tonic-gate 		a = fn1(a);
987c478bd9Sstevel@tonic-gate 		PUSH(DS, a);
997c478bd9Sstevel@tonic-gate 		break;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	case 2:
1027c478bd9Sstevel@tonic-gate 		fn2 = (fstack_t (*)(fstack_t, fstack_t))a;
1037c478bd9Sstevel@tonic-gate 		a = POP(DS);
1047c478bd9Sstevel@tonic-gate 		b = POP(DS);
1057c478bd9Sstevel@tonic-gate 		a = fn2(a, b);
1067c478bd9Sstevel@tonic-gate 		PUSH(DS, a);
1077c478bd9Sstevel@tonic-gate 		break;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	case 3:
1107c478bd9Sstevel@tonic-gate 		fn3 = (fstack_t (*)(fstack_t, fstack_t, fstack_t))a;
1117c478bd9Sstevel@tonic-gate 		a = POP(DS);
1127c478bd9Sstevel@tonic-gate 		b = POP(DS);
1137c478bd9Sstevel@tonic-gate 		c = POP(DS);
1147c478bd9Sstevel@tonic-gate 		a = fn3(a, b, c);
1157c478bd9Sstevel@tonic-gate 		PUSH(DS, a);
1167c478bd9Sstevel@tonic-gate 		break;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	case 4:
1197c478bd9Sstevel@tonic-gate 		fn4 = (fstack_t (*)(fstack_t, fstack_t, fstack_t, fstack_t))a;
1207c478bd9Sstevel@tonic-gate 		a = POP(DS);
1217c478bd9Sstevel@tonic-gate 		b = POP(DS);
1227c478bd9Sstevel@tonic-gate 		c = POP(DS);
1237c478bd9Sstevel@tonic-gate 		d = POP(DS);
1247c478bd9Sstevel@tonic-gate 		a = fn4(a, b, c, d);
1257c478bd9Sstevel@tonic-gate 		PUSH(DS, a);
1267c478bd9Sstevel@tonic-gate 		break;
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #pragma init(_init)
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static void
_init(void)1337c478bd9Sstevel@tonic-gate _init(void)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	ASSERT(env);
1387c478bd9Sstevel@tonic-gate 	NOTICE;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	FORTH(0,		"dl-open",	do_dlopen);
1417c478bd9Sstevel@tonic-gate 	FORTH(0,		"dl-close",	do_dlclose);
1427c478bd9Sstevel@tonic-gate 	FORTH(0,		"dl-sym",	do_dlsym);
1437c478bd9Sstevel@tonic-gate 	FORTH(0,		"dl-exec",	do_dlexec);
1447c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"extend-from",	do_extend);
1457c478bd9Sstevel@tonic-gate }
146