1set	prototyped
2hdr	dl,dlfcn,dll,rld_interface,mach-o/dyld
3sys	ldr
4lib	dlopen -ldl
5lib	dllload,loadbind,shl_load -ldl
6tst	dll_DYNAMIC link{
7	#include <sys/types.h>
8	#include <link.h>
9	extern struct link_dynamic _DYNAMIC;
10	int
11	main()
12	{
13		return _DYNAMIC.ld_version;
14	}
15}end
16tst	run{
17	lib=
18	for d in  /shlib /usr/shlib /lib /usr/lib
19	do	if	test -d $d
20		then	for s in "*.*" "*[!a]*"
21			do	for b in libc
22				do	for i in $d/$b.$s
23					do	if	test -f $i
24						then	lib=$i
25						fi
26					done
27					case $lib in
28					?*)	break 3 ;;
29					esac
30				done
31			done
32		fi
33	done
34	case $lib in
35	*.[0-9]*.[0-9]*)
36		i=`echo $lib | sed 's,\([^0-9]*[0-9]*\).*,\1,'`
37		if	test -f $i
38		then	lib=$i
39		fi
40		;;
41	esac
42	# some run time linkers barf with /lib/xxx if
43	# /usr/lib/xxx is there
44	case $lib in
45	/usr*)	;;
46	*)	if	test -f /usr$lib
47		then	lib=/usr$lib
48		fi
49		;;
50	esac
51	case $lib in
52	"")	lib=/lib/libc.so.1 ;;
53	esac
54	case $lib in
55	/usr/lib/*)
56		case `package` in
57		sgi.mips3)
58			abi=/lib32
59			;;
60		sgi.mips4)
61			abi=/lib64
62			;;
63		*)	abi=
64			;;
65		esac
66		case $abi in
67		?*)	if	test -d $abi
68			then	lib=`echo $lib | sed 's,/usr/lib/,,'`
69				lib=$abi/$lib
70			fi
71			;;
72		esac
73		;;
74	esac
75	echo "#define _DLL_NEXT_PATH	\"$lib\""
76}end
77tst	- output{
78	#if defined(__MVS__) && !defined(__SUSV3)
79	#define __SUSV3		1
80	#endif
81	#if _hdr_dlfcn && _lib_dlopen
82	#include <dlfcn.h>
83	#endif
84	#if _hdr_rld_interface
85	#include <rld_interface.h>
86	#endif
87	int
88	main()
89	{
90		int		i;
91	#if _hdr_rld_interface
92		void*		dll;
93		static char*	local[] = { "__elf_header", "_call_add_gp_range", "_etext" };
94	#endif
95		printf("\n");
96		printf("#if defined(__MVS__) && !defined(__SUSV3)\n");
97		printf("#define __SUSV3		1\n");
98		printf("#endif\n");
99	#if _hdr_dlfcn && _lib_dlopen
100		printf("#include <dlfcn.h>\n");
101	#endif
102	#ifndef RTLD_LAZY
103		i = 0;
104		printf("\n");
105		printf("#define RTLD_LAZY	1\n");
106	#else
107		i = 1;
108	#endif
109	#ifndef RTLD_NOW
110		if (i)
111		{
112			i = 0;
113			printf("\n");
114		}
115		printf("#define RTLD_NOW	2\n");
116	#endif
117	#ifndef RTLD_GLOBAL
118		if (i)
119		{
120			i = 0;
121			printf("\n");
122		}
123		printf("#define RTLD_GLOBAL	0\n");
124	#endif
125	#ifndef RTLD_LOCAL
126		if (i)
127		{
128			i = 0;
129			printf("\n");
130		}
131		printf("#define RTLD_LOCAL	0\n");
132	#endif
133	#ifndef RTLD_PARENT
134		if (i)
135		{
136			i = 0;
137			printf("\n");
138		}
139		printf("#define RTLD_PARENT	0\n");
140	#endif
141	#if defined(_hdr_mach_o_dyld) && !defined(RTLD_NEXT)
142		if (i)
143		{
144			i = 0;
145			printf("\n");
146		}
147		printf("#define RTLD_NEXT	((void*)16)\n");
148	#endif
149	#if _hdr_rld_interface
150		if (!(dll = dlopen(0, RTLD_LAZY)))
151			i = -1;
152		else
153		{
154			for (i = 0; i < sizeof(local) / sizeof(local[0]); i++)
155				if (dlsym(dll, local[i]))
156					break;
157			if (i >= sizeof(local) / sizeof(local[0]))
158				i = -1;
159		}
160		if (i >= 0)
161		{
162			printf("\n");
163			printf("#define _DLL_RLD_SYM		%s\n", local[i]);
164			printf("#define _DLL_RLD_SYM_STR	\"%s\"\n", local[i]);
165			printf("#define _DLL_RLD_SYM_TYPE	void*\n");
166		}
167	#endif
168		printf("\n");
169		printf("#define DLL_INFO_PREVER	0x0001	/* pre-suffix style version */\n");
170		printf("#define DLL_INFO_DOTVER	0x0002	/* post-suffix style version */\n");
171		printf("\n");
172		printf("typedef unsigned long (*Dll_plugin_version_f)(void);\n");
173		printf("typedef int (*Dllerror_f)(void*, void*, int, ...);\n");
174		printf("\n");
175		printf("typedef struct Dllinfo_s\n");
176		printf("{\n");
177		printf("	char**	sibling;	/* sibling dirs on $PATH */\n");
178		printf("	char*	prefix;		/* library name prefix */\n");
179		printf("	char*	suffix;		/* library name suffix */\n");
180		printf("	char*	env;		/* library path env var */\n");
181		printf("	int	flags;		/* DLL_INFO_* flags */\n");
182		printf("#ifdef _DLLINFO_PRIVATE_\n");
183		printf("	_DLLINFO_PRIVATE_\n");
184		printf("#endif\n");
185		printf("} Dllinfo_t;\n");
186		printf("\n");
187		printf("typedef struct Dllnames_s\n");
188		printf("{\n");
189		printf("	char*		id;\n");
190		printf("	char*		name;\n");
191		printf("	char*		base;\n");
192		printf("	char*		type;\n");
193		printf("	char*		opts;\n");
194		printf("	char*		path;\n");
195		printf("	char		data[1024];\n");
196		printf("} Dllnames_t;\n");
197		printf("\n");
198		printf("typedef struct Dllent_s\n");
199		printf("{\n");
200		printf("	char*		path;\n");
201		printf("	char*		name;\n");
202		printf("#ifdef _DLLENT_PRIVATE_\n");
203		printf("	_DLLENT_PRIVATE_\n");
204		printf("#endif\n");
205		printf("} Dllent_t;\n");
206		printf("\n");
207		printf("typedef struct Dllscan_s\n");
208		printf("{\n");
209		printf("	void*		pad;\n");
210		printf("#ifdef _DLLSCAN_PRIVATE_\n");
211		printf("	_DLLSCAN_PRIVATE_\n");
212		printf("#endif\n");
213		printf("} Dllscan_t;\n");
214	#if !_hdr_dlfcn || !_lib_dlopen
215		printf("\n");
216		printf("extern void*		dlopen(const char*, int);\n");
217		printf("extern void*		dlsym(void*, const char*);\n");
218		printf("extern int		dlclose(void*);\n");
219		printf("extern char*		dlerror(void);\n");
220	#endif
221		printf("\n");
222		printf("#if _BLD_dll && defined(__EXPORT__)\n");
223		printf("#define extern          __EXPORT__\n");
224		printf("#endif\n");
225		printf("\n");
226		printf("extern Dllinfo_t*	dllinfo(void);\n");
227		printf("extern void*		dllplugin(const char*, const char*, const char*, unsigned long, unsigned long*, int, char*, size_t);\n");
228		printf("extern void*		dllplug(const char*, const char*, const char*, int, char*, size_t);\n");
229		printf("extern void*		dllfind(const char*, const char*, int, char*, size_t);\n");
230		printf("extern Dllnames_t*	dllnames(const char*, const char*, Dllnames_t*);\n");
231		printf("extern void*		dll_lib(Dllnames_t*, unsigned long, Dllerror_f, void*);\n");
232		printf("extern void*		dllmeth(const char*, const char*, unsigned long);\n");
233		printf("extern void*		dllopen(const char*, int);\n");
234		printf("extern void*		dllnext(int);\n");
235		printf("extern void*		dlllook(void*, const char*);\n");
236		printf("extern int		dllcheck(void*, const char*, unsigned long, unsigned long*);\n");
237		printf("extern unsigned long	dllversion(void*, const char*);\n");
238		printf("extern char*		dllerror(int);\n");
239	#if _hdr_rld_interface
240		if (i >= 0)
241		{
242			printf("\n");
243			printf("extern void*		_dll_next(int, _DLL_RLD_SYM_TYPE*);\n");
244			printf("#define dllnext(f)	_dll_next(f, &_DLL_RLD_SYM)\n");
245		}
246	#endif
247		printf("\n");
248		printf("extern Dllscan_t*	dllsopen(const char*, const char*, const char*);\n");
249		printf("extern Dllent_t*	dllsread(Dllscan_t*);\n");
250		printf("extern int		dllsclose(Dllscan_t*);\n");
251		printf("\n");
252		printf("#undef  extern\n");
253	#if _hdr_rld_interface
254		if (i >= 0)
255		{
256			printf("\n");
257			printf("extern _DLL_RLD_SYM_TYPE _DLL_RLD_SYM;\n");
258		}
259	#endif
260		printf("\n");
261		return 0;
262	}
263}end
264