xref: /onnv-gate/usr/src/cmd/fs.d/cachefs/cachefspack/elfrd.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) 1996-1997, 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 <locale.h>
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <sys/stat.h>
32*0Sstevel@tonic-gate #include <sys/param.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <fcntl.h>
35*0Sstevel@tonic-gate #include <link.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <stdlib.h>
38*0Sstevel@tonic-gate #include <dirent.h>
39*0Sstevel@tonic-gate #include <search.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include "libelf.h"
42*0Sstevel@tonic-gate #include "elfrd.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate extern int verbose;
45*0Sstevel@tonic-gate extern char *mstrdup(const char *);
46*0Sstevel@tonic-gate extern void *mmalloc(size_t size);
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  * Given the name of an executable and a function call the function for
50*0Sstevel@tonic-gate  * all shared objects needed to link the executable. The function will only
51*0Sstevel@tonic-gate  * be called once. A list of filenames for which the function has been called
52*0Sstevel@tonic-gate  * is maintained, this is used to exclude filenames.
53*0Sstevel@tonic-gate  */
54*0Sstevel@tonic-gate void
process_executable(char * pathname,int (* func)(char *,char *,DIR *,int))55*0Sstevel@tonic-gate process_executable(char *pathname, int (*func)(char *, char *, DIR *, int))
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate 	struct sobj *get_share_obj(char *, struct libpath *, int);
58*0Sstevel@tonic-gate 	struct sobj *sop;
59*0Sstevel@tonic-gate 	struct sobj *psop;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #ifdef DEBUG
62*0Sstevel@tonic-gate 	printf("process_executable: pathname = %s\n", pathname);
63*0Sstevel@tonic-gate 	fflush(stdout);
64*0Sstevel@tonic-gate #endif /* debug */
65*0Sstevel@tonic-gate 	sop = get_share_obj(pathname, &libp_hd, GSO_ADDEXCLD);
66*0Sstevel@tonic-gate #ifdef DEBUG
67*0Sstevel@tonic-gate 	printf("process_executable: sop = %x\n", sop);
68*0Sstevel@tonic-gate 	fflush(stdout);
69*0Sstevel@tonic-gate #endif /* debug */
70*0Sstevel@tonic-gate 	if (verbose) {
71*0Sstevel@tonic-gate 		if ((int)sop < 0)  {
72*0Sstevel@tonic-gate 			fprintf(stderr,
73*0Sstevel@tonic-gate 			    gettext(
74*0Sstevel@tonic-gate 			    "cachefspack: unable to get shared objects - %s\n"),
75*0Sstevel@tonic-gate 			    pathname);
76*0Sstevel@tonic-gate 		}
77*0Sstevel@tonic-gate 	}
78*0Sstevel@tonic-gate 	if ((int)sop > 0) {
79*0Sstevel@tonic-gate 		while (sop->so_next != (struct sobj *)0) {
80*0Sstevel@tonic-gate #ifdef DEBUG
81*0Sstevel@tonic-gate 			printf("process_executable: sop->so_name = %s\n",
82*0Sstevel@tonic-gate 			    sop->so_name);
83*0Sstevel@tonic-gate 			fflush(stdout);
84*0Sstevel@tonic-gate #endif /* DEBUG */
85*0Sstevel@tonic-gate 			func_dir_path(sop->so_name, func);
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 			psop = sop;
88*0Sstevel@tonic-gate 			sop = sop->so_next;
89*0Sstevel@tonic-gate 			free(psop->so_name);
90*0Sstevel@tonic-gate 			free(psop);
91*0Sstevel@tonic-gate 		}
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  * Given the name of an executable, a list of directories to use in the
97*0Sstevel@tonic-gate  * library search and a list of library names to exclude, return all
98*0Sstevel@tonic-gate  * shared object needed by the executable.
99*0Sstevel@tonic-gate  *
100*0Sstevel@tonic-gate  * RETURNS: A pointer to a list of shared objects
101*0Sstevel@tonic-gate  */
102*0Sstevel@tonic-gate struct sobj *
get_share_obj(char * fpath,struct libpath * libpath,int flag)103*0Sstevel@tonic-gate get_share_obj(char *fpath, struct libpath *libpath, int flag)
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate 	static int name_cnt = 0;
106*0Sstevel@tonic-gate 	static struct sobj *so, *hd_so;
107*0Sstevel@tonic-gate 	static int depth = 0;
108*0Sstevel@tonic-gate 	static struct libpath *rpath, hd_rpath;
109*0Sstevel@tonic-gate 	int found_file = 0;
110*0Sstevel@tonic-gate 	int error;
111*0Sstevel@tonic-gate 	int fd;
112*0Sstevel@tonic-gate 	Elf *elfp;
113*0Sstevel@tonic-gate 	Elf32_Ehdr *Ehdr;
114*0Sstevel@tonic-gate 	Elf_Scn *scn;
115*0Sstevel@tonic-gate 	Elf32_Shdr *shdr;
116*0Sstevel@tonic-gate 	Elf32_Dyn *dyn;
117*0Sstevel@tonic-gate 	size_t	dynsz;
118*0Sstevel@tonic-gate 	char *name;
119*0Sstevel@tonic-gate 	void * get_scndata();
120*0Sstevel@tonic-gate 	struct sobj *alloc_sobj();
121*0Sstevel@tonic-gate 	struct sobj *add_so();
122*0Sstevel@tonic-gate 	char pathtmp[MAXPATHLEN];
123*0Sstevel@tonic-gate 	ENTRY hitem, *hitemp;
124*0Sstevel@tonic-gate 	int fileopn;
125*0Sstevel@tonic-gate 	int elfbgn = 0;
126*0Sstevel@tonic-gate 	Elf_Kind file_type;
127*0Sstevel@tonic-gate 	int buf;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	/*
130*0Sstevel@tonic-gate 	 * Open a file and perform necessary operations to find the sections
131*0Sstevel@tonic-gate 	 * in an elf format file. If the specified file is not elf format
132*0Sstevel@tonic-gate 	 * return an error.
133*0Sstevel@tonic-gate 	 */
134*0Sstevel@tonic-gate 	depth++;
135*0Sstevel@tonic-gate 	if (depth == 1) {
136*0Sstevel@tonic-gate 		/*
137*0Sstevel@tonic-gate 		 * Find the ending exclude shared object element.
138*0Sstevel@tonic-gate 		 */
139*0Sstevel@tonic-gate 		rpath = &hd_rpath;
140*0Sstevel@tonic-gate #ifdef DEBUG
141*0Sstevel@tonic-gate 		printf("dbg: rpath = %x\n", rpath);
142*0Sstevel@tonic-gate #endif /* DEBUG */
143*0Sstevel@tonic-gate 		rpath->lp_path = " ";
144*0Sstevel@tonic-gate 		rpath->lp_next = (struct libpath *)0;
145*0Sstevel@tonic-gate 		rpath->lp_level = 0;
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	fileopn = 0;
149*0Sstevel@tonic-gate 	error = ERR_NOERROR;
150*0Sstevel@tonic-gate 	fd = open(fpath, O_RDONLY);
151*0Sstevel@tonic-gate 	if (fd < 0) {
152*0Sstevel@tonic-gate 		error = ERR_NOFILE;
153*0Sstevel@tonic-gate 		goto out;
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 	fileopn = 1;
156*0Sstevel@tonic-gate /* work around */
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate  * elf_begin() core dumps when passed a file descriptor for a file
159*0Sstevel@tonic-gate  * which does not have read permission, but was opened RDONLY because the
160*0Sstevel@tonic-gate  * user doing the open was root. To avoid this problem, make sure we can
161*0Sstevel@tonic-gate  * read the first byte of the file. If we can't, skip the file. This is a
162*0Sstevel@tonic-gate  * temporary workaround until elf_begin() is fixed.
163*0Sstevel@tonic-gate  */
164*0Sstevel@tonic-gate 	if (read(fd, &buf, sizeof (buf)) < 0) {
165*0Sstevel@tonic-gate #ifdef DEBUG
166*0Sstevel@tonic-gate 		printf("read failed\n");
167*0Sstevel@tonic-gate 		fflush(stdout);
168*0Sstevel@tonic-gate #endif /* DEBUG */
169*0Sstevel@tonic-gate 		error = ERR_NOFILE;
170*0Sstevel@tonic-gate 		goto out;
171*0Sstevel@tonic-gate 	}
172*0Sstevel@tonic-gate 	lseek(fd, 0, SEEK_SET);
173*0Sstevel@tonic-gate /* work around end */
174*0Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
175*0Sstevel@tonic-gate 		error = ERR_NOELFVER;
176*0Sstevel@tonic-gate 		goto out;
177*0Sstevel@tonic-gate 	}
178*0Sstevel@tonic-gate 	elfbgn = 0;
179*0Sstevel@tonic-gate 	if ((elfp = elf_begin(fd,  ELF_C_READ, (Elf *)0)) == NULL) {
180*0Sstevel@tonic-gate 		error = ERR_NOELFBEG;
181*0Sstevel@tonic-gate 		goto out;
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 	elfbgn = 1;
184*0Sstevel@tonic-gate 	file_type = elf_kind(elfp);
185*0Sstevel@tonic-gate #ifdef DEBUG
186*0Sstevel@tonic-gate 	printf("file_type = %x\n", file_type);
187*0Sstevel@tonic-gate 	fflush(stdout);
188*0Sstevel@tonic-gate #endif /* DEBUG */
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	if (file_type != ELF_K_ELF) {
191*0Sstevel@tonic-gate 		goto out;
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 	if ((Ehdr = elf32_getehdr(elfp)) == NULL) {
194*0Sstevel@tonic-gate 		error = ERR_NOELFBEG;
195*0Sstevel@tonic-gate 		goto out;
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate #ifdef DEBUG
198*0Sstevel@tonic-gate 	printf("dbg: depth = %d\n", depth);
199*0Sstevel@tonic-gate #endif /* DEBUG */
200*0Sstevel@tonic-gate 	/*
201*0Sstevel@tonic-gate 	 * Scan all sections of the elf file to locate the dynamic section
202*0Sstevel@tonic-gate 	 */
203*0Sstevel@tonic-gate 	scn = 0;
204*0Sstevel@tonic-gate 	while ((scn = elf_nextscn(elfp, scn)) != 0) {
205*0Sstevel@tonic-gate 		if ((shdr = elf32_getshdr(scn)) == NULL) {
206*0Sstevel@tonic-gate 			error = ERR_NOELFSHD;
207*0Sstevel@tonic-gate 			goto out;
208*0Sstevel@tonic-gate 		}
209*0Sstevel@tonic-gate 		if (shdr->sh_type != SHT_DYNAMIC) {
210*0Sstevel@tonic-gate 			continue;
211*0Sstevel@tonic-gate 		}
212*0Sstevel@tonic-gate 		/*
213*0Sstevel@tonic-gate 		 * The first pass of the dynamic section locates all
214*0Sstevel@tonic-gate 		 * directories specified by "ld -R..". A stack is created
215*0Sstevel@tonic-gate 		 * for the search, this allows shared libraries, dependant
216*0Sstevel@tonic-gate 		 * on other shared libraries, built with "ld -R" to work
217*0Sstevel@tonic-gate 		 * properly.
218*0Sstevel@tonic-gate 		 *
219*0Sstevel@tonic-gate 		 */
220*0Sstevel@tonic-gate 		if ((dyn = (Elf32_Dyn *)get_scndata(scn, &dynsz)) == 0) {
221*0Sstevel@tonic-gate 			error = ERR_NOELFSDT;
222*0Sstevel@tonic-gate 			goto out;
223*0Sstevel@tonic-gate 		}
224*0Sstevel@tonic-gate 		while (dyn->d_tag != DT_NULL) {
225*0Sstevel@tonic-gate 			if (dyn->d_tag == DT_RPATH) {
226*0Sstevel@tonic-gate 				name = (char *)elf_strptr(elfp,
227*0Sstevel@tonic-gate 				    (size_t)shdr->sh_link, dyn->d_un.d_ptr);
228*0Sstevel@tonic-gate #ifdef DEBUG
229*0Sstevel@tonic-gate 				printf("DT_RPATH: name = %s\n", name);
230*0Sstevel@tonic-gate #endif /* DEBUG */
231*0Sstevel@tonic-gate 				rpath = stk_libpath(rpath, name, depth);
232*0Sstevel@tonic-gate 			}
233*0Sstevel@tonic-gate 			dyn++;
234*0Sstevel@tonic-gate 		}
235*0Sstevel@tonic-gate 		/*
236*0Sstevel@tonic-gate 		 * Find all needed shared objects. Do this recursively
237*0Sstevel@tonic-gate 		 * so libraries dependant on other libraries are found.
238*0Sstevel@tonic-gate 		 * Also, try a list of libraries to exclude. Since
239*0Sstevel@tonic-gate 		 * this routine is used by cachefspack, it is only neccessary
240*0Sstevel@tonic-gate 		 * to pack a library once. For example, libc is used by lots
241*0Sstevel@tonic-gate 		 * of commands, we need not return its name to cachefspack
242*0Sstevel@tonic-gate 		 * except the first time we find it.
243*0Sstevel@tonic-gate 		 */
244*0Sstevel@tonic-gate 		if ((dyn = (Elf32_Dyn *)get_scndata(scn, &dynsz)) == 0) {
245*0Sstevel@tonic-gate 			error = ERR_NOELFSDT;
246*0Sstevel@tonic-gate 			goto out;
247*0Sstevel@tonic-gate 		}
248*0Sstevel@tonic-gate 		for (; dyn->d_tag != DT_NULL; dyn++) {
249*0Sstevel@tonic-gate 			if (dyn->d_tag == DT_NEEDED) {
250*0Sstevel@tonic-gate 				name = (char *)elf_strptr(elfp,
251*0Sstevel@tonic-gate 				    (size_t)shdr->sh_link, dyn->d_un.d_ptr);
252*0Sstevel@tonic-gate 				if (name != 0) {
253*0Sstevel@tonic-gate #ifdef DEBUG
254*0Sstevel@tonic-gate 					printf("chk: %s\n", name);
255*0Sstevel@tonic-gate 					fflush(stdout);
256*0Sstevel@tonic-gate #endif /* DEBUG */
257*0Sstevel@tonic-gate 					found_file = libsrch(name, libpath,
258*0Sstevel@tonic-gate 					    pathtmp);
259*0Sstevel@tonic-gate #ifdef DEBUG
260*0Sstevel@tonic-gate 					printf("dbg:  1 found_file = %d\n",
261*0Sstevel@tonic-gate 					    found_file);
262*0Sstevel@tonic-gate 					fflush(stdout);
263*0Sstevel@tonic-gate #endif /* DEBUG */
264*0Sstevel@tonic-gate 					if (!found_file) {
265*0Sstevel@tonic-gate 						found_file = libsrch(name,
266*0Sstevel@tonic-gate 						    rpath, pathtmp);
267*0Sstevel@tonic-gate 					}
268*0Sstevel@tonic-gate #ifdef DEBUG
269*0Sstevel@tonic-gate 					printf("dbg:  2 found_file = %d\n",
270*0Sstevel@tonic-gate 					    found_file);
271*0Sstevel@tonic-gate 					fflush(stdout);
272*0Sstevel@tonic-gate #endif /* DEBUG */
273*0Sstevel@tonic-gate 					if (!found_file) {
274*0Sstevel@tonic-gate 						continue;
275*0Sstevel@tonic-gate 					}
276*0Sstevel@tonic-gate 					if (name_cnt == 0) {
277*0Sstevel@tonic-gate 						so = alloc_sobj();
278*0Sstevel@tonic-gate 						hd_so = so;
279*0Sstevel@tonic-gate 					}
280*0Sstevel@tonic-gate 					/*
281*0Sstevel@tonic-gate 					 * See if file already in list
282*0Sstevel@tonic-gate 					 */
283*0Sstevel@tonic-gate 					hitem.key = mstrdup(pathtmp);
284*0Sstevel@tonic-gate 					hitem.data = 0;
285*0Sstevel@tonic-gate 					hitemp = hsearch(hitem, FIND);
286*0Sstevel@tonic-gate 					if (hitemp != NULL) {
287*0Sstevel@tonic-gate #ifdef DEBUG
288*0Sstevel@tonic-gate 						printf("found so: %s\n",
289*0Sstevel@tonic-gate 						    pathtmp);
290*0Sstevel@tonic-gate 						printf("hitemp.key = %s\n",
291*0Sstevel@tonic-gate 						    hitemp->key);
292*0Sstevel@tonic-gate #endif /* DEBUG */
293*0Sstevel@tonic-gate 						continue;
294*0Sstevel@tonic-gate 					}
295*0Sstevel@tonic-gate #ifdef DEBUG
296*0Sstevel@tonic-gate 					printf("do : %s\n", pathtmp);
297*0Sstevel@tonic-gate 					fflush(stdout);
298*0Sstevel@tonic-gate #endif /* DEBUG */
299*0Sstevel@tonic-gate 					name_cnt++;
300*0Sstevel@tonic-gate 					so = add_so(so, pathtmp);
301*0Sstevel@tonic-gate 					if (flag & GSO_ADDEXCLD) {
302*0Sstevel@tonic-gate #ifdef DEBUG
303*0Sstevel@tonic-gate 						printf("adding so: %s\n",
304*0Sstevel@tonic-gate 						    pathtmp);
305*0Sstevel@tonic-gate #endif /* DEBUG */
306*0Sstevel@tonic-gate 						hitem.key = mstrdup(pathtmp);
307*0Sstevel@tonic-gate 						hitem.data = 0;
308*0Sstevel@tonic-gate 						if (hsearch(hitem, ENTER) ==
309*0Sstevel@tonic-gate 						    NULL) {
310*0Sstevel@tonic-gate 							error = ERR_HASHFULL;
311*0Sstevel@tonic-gate 							goto out;
312*0Sstevel@tonic-gate 						}
313*0Sstevel@tonic-gate 					}
314*0Sstevel@tonic-gate 					get_share_obj(pathtmp, libpath, flag);
315*0Sstevel@tonic-gate 				} else {
316*0Sstevel@tonic-gate 					if (name_cnt > 0) {
317*0Sstevel@tonic-gate 						goto out;
318*0Sstevel@tonic-gate 					} else {
319*0Sstevel@tonic-gate 						error = ERR_NOELFNAM;
320*0Sstevel@tonic-gate 						goto out;
321*0Sstevel@tonic-gate 					}
322*0Sstevel@tonic-gate 				}
323*0Sstevel@tonic-gate 			}
324*0Sstevel@tonic-gate 		}
325*0Sstevel@tonic-gate 	}
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate out:
328*0Sstevel@tonic-gate #ifdef DEBUG
329*0Sstevel@tonic-gate 	printf("error = %x\n", error);
330*0Sstevel@tonic-gate 	fflush(stdout);
331*0Sstevel@tonic-gate #endif /* DEBUG */
332*0Sstevel@tonic-gate 	depth--;
333*0Sstevel@tonic-gate #ifdef DEBUG
334*0Sstevel@tonic-gate 	printf("ret: depth = %d\n", depth);
335*0Sstevel@tonic-gate 	fflush(stdout);
336*0Sstevel@tonic-gate #endif /* DEBUG */
337*0Sstevel@tonic-gate 	if (fileopn) {
338*0Sstevel@tonic-gate 		close(fd);
339*0Sstevel@tonic-gate 		if (elfbgn) {
340*0Sstevel@tonic-gate 			if ((error != ERR_NOFILE) && (error != ERR_NOELFVER)) {
341*0Sstevel@tonic-gate 				elf_end(elfp);
342*0Sstevel@tonic-gate 			}
343*0Sstevel@tonic-gate 		}
344*0Sstevel@tonic-gate 	}
345*0Sstevel@tonic-gate 	if (name_cnt == 0) {
346*0Sstevel@tonic-gate 		return ((struct sobj *)ERR_NOERROR);
347*0Sstevel@tonic-gate 	}
348*0Sstevel@tonic-gate 	while (rpath->lp_level > depth) {
349*0Sstevel@tonic-gate #ifdef DEBUG
350*0Sstevel@tonic-gate 		printf("ret: rpath->lp_level = %d\n", rpath->lp_level);
351*0Sstevel@tonic-gate 		fflush(stdout);
352*0Sstevel@tonic-gate #endif /* DEBUG */
353*0Sstevel@tonic-gate 		rpath = pop_libpath(rpath);
354*0Sstevel@tonic-gate 	}
355*0Sstevel@tonic-gate 	if (depth == 0) {
356*0Sstevel@tonic-gate 		name_cnt = 0;
357*0Sstevel@tonic-gate 	}
358*0Sstevel@tonic-gate 	if (error == ERR_NOERROR) {
359*0Sstevel@tonic-gate 		return (hd_so);
360*0Sstevel@tonic-gate 	} else {
361*0Sstevel@tonic-gate 		return ((struct sobj *)error);
362*0Sstevel@tonic-gate 	}
363*0Sstevel@tonic-gate }
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate /*
367*0Sstevel@tonic-gate  * Get the section descriptor and set the size of the
368*0Sstevel@tonic-gate  * data returned.  Data is byte-order converted.
369*0Sstevel@tonic-gate  */
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate void *
get_scndata(fd_scn,size)372*0Sstevel@tonic-gate get_scndata(fd_scn, size)
373*0Sstevel@tonic-gate Elf_Scn *fd_scn;
374*0Sstevel@tonic-gate size_t    *size;
375*0Sstevel@tonic-gate {
376*0Sstevel@tonic-gate 	Elf_Data *p_data;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	p_data = 0;
379*0Sstevel@tonic-gate 	if ((p_data = elf_getdata(fd_scn, p_data)) == 0 ||
380*0Sstevel@tonic-gate 		p_data->d_size == 0)
381*0Sstevel@tonic-gate 	{
382*0Sstevel@tonic-gate 		return (NULL);
383*0Sstevel@tonic-gate 	}
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 	*size = p_data->d_size;
386*0Sstevel@tonic-gate 	return (p_data->d_buf);
387*0Sstevel@tonic-gate }
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate /*
390*0Sstevel@tonic-gate  * Allocate a shared object structure
391*0Sstevel@tonic-gate  *
392*0Sstevel@tonic-gate  * RETURNS: A pointer to the allocated structure
393*0Sstevel@tonic-gate  */
394*0Sstevel@tonic-gate struct sobj *
alloc_sobj()395*0Sstevel@tonic-gate alloc_sobj()
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate 	struct sobj *so;
398*0Sstevel@tonic-gate 	so = (struct sobj *)mmalloc(sizeof (struct sobj));
399*0Sstevel@tonic-gate 	so->so_name = " ";
400*0Sstevel@tonic-gate 	so->so_next = (struct sobj *)0;
401*0Sstevel@tonic-gate 	return (so);
402*0Sstevel@tonic-gate }
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate /*
406*0Sstevel@tonic-gate  * Add an object to a shared object list
407*0Sstevel@tonic-gate  *
408*0Sstevel@tonic-gate  * RETURNS: The tail of the shared object list
409*0Sstevel@tonic-gate  */
410*0Sstevel@tonic-gate struct sobj *
add_so(struct sobj * so,char * path)411*0Sstevel@tonic-gate add_so(struct sobj *so, char *path)
412*0Sstevel@tonic-gate {
413*0Sstevel@tonic-gate 	if (so == (struct sobj *)0) {
414*0Sstevel@tonic-gate 		so = alloc_sobj();
415*0Sstevel@tonic-gate 	}
416*0Sstevel@tonic-gate 	so->so_name = mstrdup(path);
417*0Sstevel@tonic-gate 	so->so_next = alloc_sobj();
418*0Sstevel@tonic-gate 	so = so->so_next;
419*0Sstevel@tonic-gate 	return (so);
420*0Sstevel@tonic-gate }
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate /*
423*0Sstevel@tonic-gate  * Determine if name concatenated with a library directory path yields
424*0Sstevel@tonic-gate  * a file name that exists.
425*0Sstevel@tonic-gate  *
426*0Sstevel@tonic-gate  * RETURNS: True(1) or False(0)
427*0Sstevel@tonic-gate  *	    if true - fullpath arg contains a pointer to the full path name
428*0Sstevel@tonic-gate  *			of the file
429*0Sstevel@tonic-gate  */
430*0Sstevel@tonic-gate int
libsrch(char * name,struct libpath * libpath,char * fullpath)431*0Sstevel@tonic-gate libsrch(char *name, struct libpath *libpath, char *fullpath)
432*0Sstevel@tonic-gate {
433*0Sstevel@tonic-gate 	struct stat64 statbuf;
434*0Sstevel@tonic-gate 	struct libpath *lp;
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate #ifdef DEBUG
437*0Sstevel@tonic-gate 	printf("libsrch: libpath = %x\n", libpath);
438*0Sstevel@tonic-gate 	fflush(stdout);
439*0Sstevel@tonic-gate #endif /* DEBUG */
440*0Sstevel@tonic-gate 	lp = libpath;
441*0Sstevel@tonic-gate 	if (lp == NULL) {
442*0Sstevel@tonic-gate 		return (0);
443*0Sstevel@tonic-gate 	}
444*0Sstevel@tonic-gate #ifdef DEBUG
445*0Sstevel@tonic-gate 	printf("libsrch: 1 lp->lp_next = %x\n", lp->lp_next);
446*0Sstevel@tonic-gate 	fflush(stdout);
447*0Sstevel@tonic-gate #endif /* DEBUG */
448*0Sstevel@tonic-gate 	while (lp->lp_next != (struct libpath *)0) {
449*0Sstevel@tonic-gate 		strcpy(fullpath, lp->lp_path);
450*0Sstevel@tonic-gate 		strcat(fullpath, "/");
451*0Sstevel@tonic-gate 		strcat(fullpath, name);
452*0Sstevel@tonic-gate 		lp = lp->lp_next;
453*0Sstevel@tonic-gate #ifdef DEBUG
454*0Sstevel@tonic-gate 		printf("libsrch: 2 lp->lp_next = %x\n", lp->lp_next);
455*0Sstevel@tonic-gate 		fflush(stdout);
456*0Sstevel@tonic-gate #endif /* DEBUG */
457*0Sstevel@tonic-gate 		/*
458*0Sstevel@tonic-gate 		 * stat - if file break
459*0Sstevel@tonic-gate 		 */
460*0Sstevel@tonic-gate 		if (stat64(fullpath, &statbuf)
461*0Sstevel@tonic-gate 		    == 0) {
462*0Sstevel@tonic-gate #ifdef DEBUG
463*0Sstevel@tonic-gate 			printf("libsrch: found - %s\n", fullpath);
464*0Sstevel@tonic-gate 			fflush(stdout);
465*0Sstevel@tonic-gate #endif /* DEBUG */
466*0Sstevel@tonic-gate 			return (1);
467*0Sstevel@tonic-gate 		}
468*0Sstevel@tonic-gate 	}
469*0Sstevel@tonic-gate #ifdef DEBUG
470*0Sstevel@tonic-gate 	printf("libsrch: NOT found - %s\n", name);
471*0Sstevel@tonic-gate 	fflush(stdout);
472*0Sstevel@tonic-gate #endif /* DEBUG */
473*0Sstevel@tonic-gate 	return (0);
474*0Sstevel@tonic-gate }
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate /*
477*0Sstevel@tonic-gate  * Add path to the libpath list(add at the tail of the list).
478*0Sstevel@tonic-gate  *
479*0Sstevel@tonic-gate  * RETURNS: The new tail of the list
480*0Sstevel@tonic-gate  */
481*0Sstevel@tonic-gate struct libpath *
add_libpath(struct libpath * lp,char * path,int level)482*0Sstevel@tonic-gate add_libpath(struct libpath *lp, char *path, int level)
483*0Sstevel@tonic-gate {
484*0Sstevel@tonic-gate 	char *s;
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	lp->lp_level = level;
487*0Sstevel@tonic-gate 	s = mstrdup(path);
488*0Sstevel@tonic-gate 	if (s != (char *)0) {
489*0Sstevel@tonic-gate 		lp->lp_path = s;
490*0Sstevel@tonic-gate 	}
491*0Sstevel@tonic-gate 	lp->lp_next = (struct libpath *)mmalloc(sizeof (struct libpath));
492*0Sstevel@tonic-gate 	lp = lp->lp_next;
493*0Sstevel@tonic-gate 	lp->lp_next = (struct libpath *)0;
494*0Sstevel@tonic-gate 	lp->lp_level = 0;
495*0Sstevel@tonic-gate 	lp->lp_path = " ";
496*0Sstevel@tonic-gate 	return (lp);
497*0Sstevel@tonic-gate }
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate /*
500*0Sstevel@tonic-gate  * Add directory/directories in name to libpath stack(as head of the stack)
501*0Sstevel@tonic-gate  * at the level specified.
502*0Sstevel@tonic-gate  *
503*0Sstevel@tonic-gate  * RETURNS: the new head of the stack
504*0Sstevel@tonic-gate  */
505*0Sstevel@tonic-gate struct libpath *
stk_libpath(struct libpath * hd,char * name,int level)506*0Sstevel@tonic-gate stk_libpath(struct libpath *hd, char *name, int level)
507*0Sstevel@tonic-gate {
508*0Sstevel@tonic-gate 	struct libpath *lp, *prev_lp;
509*0Sstevel@tonic-gate 	char *s, *t;
510*0Sstevel@tonic-gate 	char *tok;
511*0Sstevel@tonic-gate 	char *freeit;
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate #ifdef DEBUG
514*0Sstevel@tonic-gate 	printf("stk_libpath: name = %s\n", name);
515*0Sstevel@tonic-gate 	fflush(stdout);
516*0Sstevel@tonic-gate #endif /* DEBUG */
517*0Sstevel@tonic-gate 	s = mstrdup(name);
518*0Sstevel@tonic-gate 	freeit = s;
519*0Sstevel@tonic-gate 	prev_lp = hd;
520*0Sstevel@tonic-gate 	while (1) {
521*0Sstevel@tonic-gate 		tok = strtok(s, ":");
522*0Sstevel@tonic-gate 		if (tok == (char *)NULL)
523*0Sstevel@tonic-gate 		    break;
524*0Sstevel@tonic-gate 		s = (char *)0;
525*0Sstevel@tonic-gate 		lp = (struct libpath *)mmalloc(sizeof (struct libpath));
526*0Sstevel@tonic-gate 		lp->lp_level = level;
527*0Sstevel@tonic-gate 		t = mstrdup(tok);
528*0Sstevel@tonic-gate 		lp->lp_path = t;
529*0Sstevel@tonic-gate 		lp->lp_next = prev_lp;
530*0Sstevel@tonic-gate 		prev_lp = lp;
531*0Sstevel@tonic-gate 	}
532*0Sstevel@tonic-gate #ifdef DEBUG
533*0Sstevel@tonic-gate 	printf("stk_libpath: lp = %x\n", lp);
534*0Sstevel@tonic-gate 	fflush(stdout);
535*0Sstevel@tonic-gate #endif /* DEBUG */
536*0Sstevel@tonic-gate 	free(freeit);
537*0Sstevel@tonic-gate 	return (lp);
538*0Sstevel@tonic-gate }
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate /*
541*0Sstevel@tonic-gate  * Free up a libpath stack entry.
542*0Sstevel@tonic-gate  *
543*0Sstevel@tonic-gate  * RETURNS: the new head of the stack
544*0Sstevel@tonic-gate  */
545*0Sstevel@tonic-gate struct libpath *
pop_libpath(struct libpath * lp)546*0Sstevel@tonic-gate pop_libpath(struct libpath *lp)
547*0Sstevel@tonic-gate {
548*0Sstevel@tonic-gate 	struct libpath *tlp;
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 	tlp = lp;
551*0Sstevel@tonic-gate 	lp = lp->lp_next;
552*0Sstevel@tonic-gate 	free(tlp->lp_path);
553*0Sstevel@tonic-gate 	free(tlp);
554*0Sstevel@tonic-gate 	return (lp);
555*0Sstevel@tonic-gate }
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate /*
558*0Sstevel@tonic-gate  * Crack the LD_LIBRARY_PATH environment variable. Make a list of libraries
559*0Sstevel@tonic-gate  * to search.
560*0Sstevel@tonic-gate  */
561*0Sstevel@tonic-gate void
get_libsrch_path(struct libpath * libhd)562*0Sstevel@tonic-gate get_libsrch_path(struct libpath *libhd)
563*0Sstevel@tonic-gate {
564*0Sstevel@tonic-gate 	char *s;
565*0Sstevel@tonic-gate 	char *tok = (char *) 1;
566*0Sstevel@tonic-gate 	struct libpath *lp;
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 	lp = libhd;
569*0Sstevel@tonic-gate 	s = getenv("LD_LIBRARY_PATH");
570*0Sstevel@tonic-gate 	if (s != (char *)NULL) {
571*0Sstevel@tonic-gate 		while (1) {
572*0Sstevel@tonic-gate 			tok = strtok(s, ":");
573*0Sstevel@tonic-gate 			if (tok == (char *) NULL)
574*0Sstevel@tonic-gate 				break;
575*0Sstevel@tonic-gate 			s = (char *) 0;
576*0Sstevel@tonic-gate 			lp = add_libpath(lp, tok, 0);
577*0Sstevel@tonic-gate 		}
578*0Sstevel@tonic-gate 	}
579*0Sstevel@tonic-gate 	add_libpath(lp, "/usr/lib", 0);
580*0Sstevel@tonic-gate }
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate #ifdef DEBUG
prt_sop_lst(struct sobj * sop,char * str)584*0Sstevel@tonic-gate prt_sop_lst(struct sobj *sop, char * str)
585*0Sstevel@tonic-gate {
586*0Sstevel@tonic-gate 	printf("\n\n\n%s - sop = %x\n", str, sop);
587*0Sstevel@tonic-gate 	fflush(stdout);
588*0Sstevel@tonic-gate 	if ((int)sop < 0)  {
589*0Sstevel@tonic-gate 		fprintf(stderr, "get_share_obj: failed\n");
590*0Sstevel@tonic-gate 		exit(1);
591*0Sstevel@tonic-gate 	}
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 	if ((int)sop > 0) {
594*0Sstevel@tonic-gate 		while (sop->so_next != (struct sobj *) 0) {
595*0Sstevel@tonic-gate 			printf("sop->so_name = %s\n", sop->so_name);
596*0Sstevel@tonic-gate 			sop = sop->so_next;
597*0Sstevel@tonic-gate 		}
598*0Sstevel@tonic-gate 	}
599*0Sstevel@tonic-gate }
600*0Sstevel@tonic-gate #endif /* DEBUG */
601