1*84d9c625SLionel Sambuc /* $NetBSD: ldd_elfxx.c,v 1.6 2012/07/08 00:53:44 matt Exp $ */
24b999f19SBen Gras
34b999f19SBen Gras /*-
44b999f19SBen Gras * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
54b999f19SBen Gras * All rights reserved.
64b999f19SBen Gras *
74b999f19SBen Gras * This code is derived from software contributed to The NetBSD Foundation
84b999f19SBen Gras * by Paul Kranenburg.
94b999f19SBen Gras *
104b999f19SBen Gras * Redistribution and use in source and binary forms, with or without
114b999f19SBen Gras * modification, are permitted provided that the following conditions
124b999f19SBen Gras * are met:
134b999f19SBen Gras * 1. Redistributions of source code must retain the above copyright
144b999f19SBen Gras * notice, this list of conditions and the following disclaimer.
154b999f19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
164b999f19SBen Gras * notice, this list of conditions and the following disclaimer in the
174b999f19SBen Gras * documentation and/or other materials provided with the distribution.
184b999f19SBen Gras *
194b999f19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204b999f19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214b999f19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224b999f19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234b999f19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244b999f19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254b999f19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264b999f19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274b999f19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284b999f19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294b999f19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
304b999f19SBen Gras */
314b999f19SBen Gras
324b999f19SBen Gras /*
334b999f19SBen Gras * Copyright 1996 John D. Polstra.
344b999f19SBen Gras * Copyright 1996 Matt Thomas <matt@3am-software.com>
354b999f19SBen Gras * All rights reserved.
364b999f19SBen Gras *
374b999f19SBen Gras * Redistribution and use in source and binary forms, with or without
384b999f19SBen Gras * modification, are permitted provided that the following conditions
394b999f19SBen Gras * are met:
404b999f19SBen Gras * 1. Redistributions of source code must retain the above copyright
414b999f19SBen Gras * notice, this list of conditions and the following disclaimer.
424b999f19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
434b999f19SBen Gras * notice, this list of conditions and the following disclaimer in the
444b999f19SBen Gras * documentation and/or other materials provided with the distribution.
454b999f19SBen Gras * 3. All advertising materials mentioning features or use of this software
464b999f19SBen Gras * must display the following acknowledgement:
474b999f19SBen Gras * This product includes software developed by John Polstra.
484b999f19SBen Gras * 4. The name of the author may not be used to endorse or promote products
494b999f19SBen Gras * derived from this software without specific prior written permission.
504b999f19SBen Gras *
514b999f19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
524b999f19SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
534b999f19SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
544b999f19SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
554b999f19SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
564b999f19SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
574b999f19SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
584b999f19SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
594b999f19SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
604b999f19SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
614b999f19SBen Gras */
624b999f19SBen Gras
634b999f19SBen Gras #include <sys/cdefs.h>
644b999f19SBen Gras #ifndef lint
65*84d9c625SLionel Sambuc __RCSID("$NetBSD: ldd_elfxx.c,v 1.6 2012/07/08 00:53:44 matt Exp $");
664b999f19SBen Gras #endif /* not lint */
674b999f19SBen Gras
684b999f19SBen Gras #include <sys/types.h>
694b999f19SBen Gras #include <sys/mman.h>
704b999f19SBen Gras #include <sys/wait.h>
714b999f19SBen Gras
724b999f19SBen Gras #include <dirent.h>
734b999f19SBen Gras #include <err.h>
744b999f19SBen Gras #include <errno.h>
754b999f19SBen Gras #include <fcntl.h>
764b999f19SBen Gras #include <stdarg.h>
774b999f19SBen Gras #include <stdio.h>
784b999f19SBen Gras #include <stdlib.h>
794b999f19SBen Gras #include <string.h>
804b999f19SBen Gras #include <unistd.h>
814b999f19SBen Gras #include <ctype.h>
824b999f19SBen Gras
834b999f19SBen Gras #include "debug.h"
844b999f19SBen Gras #include "rtld.h"
854b999f19SBen Gras #include "ldd.h"
864b999f19SBen Gras
87*84d9c625SLionel Sambuc static void print_needed(Obj_Entry *, const char *, const char *);
88*84d9c625SLionel Sambuc static void fmtprint(const char *, Obj_Entry *, const char *, const char *);
89*84d9c625SLionel Sambuc
904b999f19SBen Gras /*
91*84d9c625SLionel Sambuc * elfxx_ldd() - bit-size independent ELF ldd implementation.
924b999f19SBen Gras * returns 0 on success and -1 on failure.
934b999f19SBen Gras */
944b999f19SBen Gras int
ELFNAME(ldd)954b999f19SBen Gras ELFNAME(ldd)(int fd, char *path, const char *fmt1, const char *fmt2)
964b999f19SBen Gras {
974b999f19SBen Gras struct stat st;
984b999f19SBen Gras
994b999f19SBen Gras if (lseek(fd, 0, SEEK_SET) < 0 ||
1004b999f19SBen Gras fstat(fd, &st) < 0) {
1014b999f19SBen Gras _rtld_error("%s: %s", path, strerror(errno));
1024b999f19SBen Gras return -1;
1034b999f19SBen Gras }
1044b999f19SBen Gras
1054b999f19SBen Gras _rtld_pagesz = sysconf(_SC_PAGESIZE);
1064b999f19SBen Gras
1074b999f19SBen Gras #ifdef RTLD_ARCH_SUBDIR
1084b999f19SBen Gras _rtld_add_paths(path, &_rtld_default_paths,
1094b999f19SBen Gras RTLD_DEFAULT_LIBRARY_PATH "/" RTLD_ARCH_SUBDIR);
1104b999f19SBen Gras #endif
1114b999f19SBen Gras _rtld_add_paths(path, &_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH);
1124b999f19SBen Gras
1134b999f19SBen Gras _rtld_paths = NULL;
1144b999f19SBen Gras _rtld_trust = (st.st_mode & (S_ISUID | S_ISGID)) == 0;
1154b999f19SBen Gras if (_rtld_trust)
1164b999f19SBen Gras _rtld_add_paths(path, &_rtld_paths, getenv("LD_LIBRARY_PATH"));
1174b999f19SBen Gras
1184b999f19SBen Gras _rtld_process_hints(path, &_rtld_paths, &_rtld_xforms, _PATH_LD_HINTS);
1194b999f19SBen Gras _rtld_objmain = _rtld_map_object(xstrdup(path), fd, &st);
1204b999f19SBen Gras if (_rtld_objmain == NULL)
1214b999f19SBen Gras return -1;
1224b999f19SBen Gras
1234b999f19SBen Gras _rtld_objmain->path = xstrdup(path);
1244b999f19SBen Gras _rtld_digest_dynamic(path, _rtld_objmain);
1254b999f19SBen Gras
1264b999f19SBen Gras /* Link the main program into the list of objects. */
1274b999f19SBen Gras *_rtld_objtail = _rtld_objmain;
1284b999f19SBen Gras _rtld_objtail = &_rtld_objmain->next;
1294b999f19SBen Gras ++_rtld_objmain->refcount;
1304b999f19SBen Gras
1314b999f19SBen Gras (void) _rtld_load_needed_objects(_rtld_objmain, 0);
1324b999f19SBen Gras
1334b999f19SBen Gras if (fmt1 == NULL)
1344b999f19SBen Gras printf("%s:\n", _rtld_objmain->path);
1354b999f19SBen Gras main_local = path;
1364b999f19SBen Gras main_progname = _rtld_objmain->path;
1374b999f19SBen Gras print_needed(_rtld_objmain, fmt1, fmt2);
1384b999f19SBen Gras
1394b999f19SBen Gras while (_rtld_objlist != NULL) {
1404b999f19SBen Gras Obj_Entry *obj = _rtld_objlist;
1414b999f19SBen Gras _rtld_objlist = obj->next;
1424b999f19SBen Gras while (obj->rpaths != NULL) {
1434b999f19SBen Gras const Search_Path *rpath = obj->rpaths;
1444b999f19SBen Gras obj->rpaths = rpath->sp_next;
1454b999f19SBen Gras xfree(__UNCONST(rpath->sp_path));
1464b999f19SBen Gras xfree(__UNCONST(rpath));
1474b999f19SBen Gras }
1484b999f19SBen Gras while (obj->needed != NULL) {
1494b999f19SBen Gras const Needed_Entry *needed = obj->needed;
1504b999f19SBen Gras obj->needed = needed->next;
1514b999f19SBen Gras xfree(__UNCONST(needed));
1524b999f19SBen Gras }
1534b999f19SBen Gras (void) munmap(obj->mapbase, obj->mapsize);
1544b999f19SBen Gras xfree(obj->path);
1554b999f19SBen Gras xfree(obj);
1564b999f19SBen Gras }
1574b999f19SBen Gras
1584b999f19SBen Gras _rtld_objmain = NULL;
1594b999f19SBen Gras _rtld_objtail = &_rtld_objlist;
1604b999f19SBen Gras /* Need to free _rtld_paths? */
1614b999f19SBen Gras
1624b999f19SBen Gras return 0;
1634b999f19SBen Gras }
164*84d9c625SLionel Sambuc
165*84d9c625SLionel Sambuc void
fmtprint(const char * libname,Obj_Entry * obj,const char * fmt1,const char * fmt2)166*84d9c625SLionel Sambuc fmtprint(const char *libname, Obj_Entry *obj, const char *fmt1,
167*84d9c625SLionel Sambuc const char *fmt2)
168*84d9c625SLionel Sambuc {
169*84d9c625SLionel Sambuc const char *libpath = obj ? obj->path : "not found";
170*84d9c625SLionel Sambuc char libnamebuf[200];
171*84d9c625SLionel Sambuc char *libmajor = NULL;
172*84d9c625SLionel Sambuc const char *fmt;
173*84d9c625SLionel Sambuc char *cp;
174*84d9c625SLionel Sambuc int c;
175*84d9c625SLionel Sambuc
176*84d9c625SLionel Sambuc if (strncmp(libname, "lib", 3) == 0 &&
177*84d9c625SLionel Sambuc (cp = strstr(libname, ".so")) != NULL) {
178*84d9c625SLionel Sambuc size_t i = cp - (libname + 3);
179*84d9c625SLionel Sambuc
180*84d9c625SLionel Sambuc if (i >= sizeof(libnamebuf))
181*84d9c625SLionel Sambuc i = sizeof(libnamebuf) - 1;
182*84d9c625SLionel Sambuc (void)memcpy(libnamebuf, libname + 3, i);
183*84d9c625SLionel Sambuc libnamebuf[i] = '\0';
184*84d9c625SLionel Sambuc if (cp[3] && isdigit((unsigned char)cp[4]))
185*84d9c625SLionel Sambuc libmajor = &cp[4];
186*84d9c625SLionel Sambuc libname = libnamebuf;
187*84d9c625SLionel Sambuc }
188*84d9c625SLionel Sambuc
189*84d9c625SLionel Sambuc if (fmt1 == NULL)
190*84d9c625SLionel Sambuc fmt1 = libmajor != NULL ?
191*84d9c625SLionel Sambuc "\t-l%o.%m => %p\n" :
192*84d9c625SLionel Sambuc "\t-l%o => %p\n";
193*84d9c625SLionel Sambuc if (fmt2 == NULL)
194*84d9c625SLionel Sambuc fmt2 = "\t%o => %p\n";
195*84d9c625SLionel Sambuc
196*84d9c625SLionel Sambuc fmt = libname == libnamebuf ? fmt1 : fmt2;
197*84d9c625SLionel Sambuc while ((c = *fmt++) != '\0') {
198*84d9c625SLionel Sambuc switch (c) {
199*84d9c625SLionel Sambuc default:
200*84d9c625SLionel Sambuc putchar(c);
201*84d9c625SLionel Sambuc continue;
202*84d9c625SLionel Sambuc case '\\':
203*84d9c625SLionel Sambuc switch (c = *fmt) {
204*84d9c625SLionel Sambuc case '\0':
205*84d9c625SLionel Sambuc continue;
206*84d9c625SLionel Sambuc case 'n':
207*84d9c625SLionel Sambuc putchar('\n');
208*84d9c625SLionel Sambuc break;
209*84d9c625SLionel Sambuc case 't':
210*84d9c625SLionel Sambuc putchar('\t');
211*84d9c625SLionel Sambuc break;
212*84d9c625SLionel Sambuc }
213*84d9c625SLionel Sambuc break;
214*84d9c625SLionel Sambuc case '%':
215*84d9c625SLionel Sambuc switch (c = *fmt) {
216*84d9c625SLionel Sambuc case '\0':
217*84d9c625SLionel Sambuc continue;
218*84d9c625SLionel Sambuc case '%':
219*84d9c625SLionel Sambuc default:
220*84d9c625SLionel Sambuc putchar(c);
221*84d9c625SLionel Sambuc break;
222*84d9c625SLionel Sambuc case 'A':
223*84d9c625SLionel Sambuc printf("%s", main_local);
224*84d9c625SLionel Sambuc break;
225*84d9c625SLionel Sambuc case 'a':
226*84d9c625SLionel Sambuc printf("%s", main_progname);
227*84d9c625SLionel Sambuc break;
228*84d9c625SLionel Sambuc case 'o':
229*84d9c625SLionel Sambuc printf("%s", libname);
230*84d9c625SLionel Sambuc break;
231*84d9c625SLionel Sambuc case 'm':
232*84d9c625SLionel Sambuc printf("%s", libmajor);
233*84d9c625SLionel Sambuc break;
234*84d9c625SLionel Sambuc case 'n':
235*84d9c625SLionel Sambuc /* XXX: not supported for elf */
236*84d9c625SLionel Sambuc break;
237*84d9c625SLionel Sambuc case 'p':
238*84d9c625SLionel Sambuc printf("%s", libpath);
239*84d9c625SLionel Sambuc break;
240*84d9c625SLionel Sambuc case 'x':
241*84d9c625SLionel Sambuc printf("%p", obj ? obj->mapbase : 0);
242*84d9c625SLionel Sambuc break;
243*84d9c625SLionel Sambuc }
244*84d9c625SLionel Sambuc break;
245*84d9c625SLionel Sambuc }
246*84d9c625SLionel Sambuc ++fmt;
247*84d9c625SLionel Sambuc }
248*84d9c625SLionel Sambuc }
249*84d9c625SLionel Sambuc
250*84d9c625SLionel Sambuc void
print_needed(Obj_Entry * obj,const char * fmt1,const char * fmt2)251*84d9c625SLionel Sambuc print_needed(Obj_Entry *obj, const char *fmt1, const char *fmt2)
252*84d9c625SLionel Sambuc {
253*84d9c625SLionel Sambuc const Needed_Entry *needed;
254*84d9c625SLionel Sambuc
255*84d9c625SLionel Sambuc for (needed = obj->needed; needed != NULL; needed = needed->next) {
256*84d9c625SLionel Sambuc const char *libname = obj->strtab + needed->name;
257*84d9c625SLionel Sambuc
258*84d9c625SLionel Sambuc if (needed->obj != NULL) {
259*84d9c625SLionel Sambuc if (!needed->obj->printed) {
260*84d9c625SLionel Sambuc fmtprint(libname, needed->obj, fmt1, fmt2);
261*84d9c625SLionel Sambuc needed->obj->printed = 1;
262*84d9c625SLionel Sambuc print_needed(needed->obj, fmt1, fmt2);
263*84d9c625SLionel Sambuc }
264*84d9c625SLionel Sambuc } else {
265*84d9c625SLionel Sambuc fmtprint(libname, needed->obj, fmt1, fmt2);
266*84d9c625SLionel Sambuc }
267*84d9c625SLionel Sambuc }
268*84d9c625SLionel Sambuc }
269