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