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  */
22f07a2a2eScraigm 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T */
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
307c478bd9Sstevel@tonic-gate 
31149775e4SDan McDonald /* Copyright 2011 Nexenta Systems, Inc. All rights reserved. */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
357c478bd9Sstevel@tonic-gate /* include headers */
367c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "static_prof.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /* ========== elf_hash ==================================================== */
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * DESCRIPTION:
437c478bd9Sstevel@tonic-gate  * The hash function copied from libelf.so.1
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate /* ======================================================================== */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static unsigned long
my_elf_hash(const char * name)487c478bd9Sstevel@tonic-gate my_elf_hash(const char *name)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	unsigned long g, h = 0;
517c478bd9Sstevel@tonic-gate 	const unsigned char *nm = (unsigned char *) name;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	while (*nm != '\0') {
547c478bd9Sstevel@tonic-gate 		h = (h << 4) + *nm++;
557c478bd9Sstevel@tonic-gate 		if ((g = h & MASK) != 0)
567c478bd9Sstevel@tonic-gate 			h ^= g >> 24;
577c478bd9Sstevel@tonic-gate 		h &= ~MASK;
587c478bd9Sstevel@tonic-gate 	}
597c478bd9Sstevel@tonic-gate 	return (h);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /* ========== output_dtneeded ============================================= */
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * DESCRIPTION:
657c478bd9Sstevel@tonic-gate  * Outputs all the dt_needed entries if any.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate /* ======================================================================== */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static void
output_dtneeded(dt_list * list)707c478bd9Sstevel@tonic-gate output_dtneeded(dt_list * list)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	dt_list		*p = list;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	(void) fprintf(OUTPUT_FD, "#dtneeded:");
767c478bd9Sstevel@tonic-gate 	if (!p) {
777c478bd9Sstevel@tonic-gate 		(void) fprintf(OUTPUT_FD, "\n");
787c478bd9Sstevel@tonic-gate 		return;
797c478bd9Sstevel@tonic-gate 	} else {
807c478bd9Sstevel@tonic-gate 		while (p != NULL) {
817c478bd9Sstevel@tonic-gate 			(void) fprintf(OUTPUT_FD,
827c478bd9Sstevel@tonic-gate 			    " %s",
837c478bd9Sstevel@tonic-gate 			    p->libname);
847c478bd9Sstevel@tonic-gate 			p = p->next;
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 		(void) fprintf(OUTPUT_FD, "\n");
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* ========== store_binding =============================================== */
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * DESCRIPTION:
937c478bd9Sstevel@tonic-gate  * Read in the symbol binding information from the symbol table and
947c478bd9Sstevel@tonic-gate  * store them into the hash table of buckets.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate /* ======================================================================== */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static void
store_binding(binding_bucket * bind)997c478bd9Sstevel@tonic-gate store_binding(binding_bucket * bind)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	unsigned long   bktno;
1027c478bd9Sstevel@tonic-gate 	unsigned long   orig_bktno;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	bktno = my_elf_hash(bind->sym) % DEFBKTS;
1057c478bd9Sstevel@tonic-gate 	orig_bktno = bktno;
1067c478bd9Sstevel@tonic-gate 
107149775e4SDan McDonald 	while (bkts[bktno].sym != NULL) {
1087c478bd9Sstevel@tonic-gate 		bktno = (bktno + 1) % DEFBKTS;
109149775e4SDan McDonald 
110149775e4SDan McDonald 		if (bktno == orig_bktno)
111149775e4SDan McDonald 			exit(1);
1127c478bd9Sstevel@tonic-gate 	}
113149775e4SDan McDonald 
114149775e4SDan McDonald 	bkts[bktno].sym = bind->sym;
115149775e4SDan McDonald 	bkts[bktno].obj = bind->obj;
116149775e4SDan McDonald 	bkts[bktno].ref_lib = bind->ref_lib;
117149775e4SDan McDonald 	bkts[bktno].def_lib = bind->def_lib;
118149775e4SDan McDonald 	bkts[bktno].section = bind->section;
119149775e4SDan McDonald 	bkts[bktno].stbind = bind->stbind;
120149775e4SDan McDonald 	bkts[bktno].sttype = bind->sttype;
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
123149775e4SDan McDonald 
1247c478bd9Sstevel@tonic-gate /* ========== check_store_binding ========================================= */
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * DESCRIPTION:
1277c478bd9Sstevel@tonic-gate  * Check what's already on the hash table with the new symbol binding
1287c478bd9Sstevel@tonic-gate  * information from the dependencies and record it into the bucket.
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate /* ======================================================================== */
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static void
check_store_binding(binding_bucket * bind)1337c478bd9Sstevel@tonic-gate check_store_binding(binding_bucket * bind)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	unsigned long   bktno;
1367c478bd9Sstevel@tonic-gate 	unsigned long   orig_bktno;
1377c478bd9Sstevel@tonic-gate 	unsigned long   i;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	bktno = my_elf_hash(bind->sym) % DEFBKTS;
1407c478bd9Sstevel@tonic-gate 	orig_bktno = bktno;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (!bkts[bktno].sym)
1437c478bd9Sstevel@tonic-gate 		return;
1447c478bd9Sstevel@tonic-gate 	if (bkts[bktno].sym && (strcmp(bkts[bktno].sym, bind->sym)) == 0) {
1457c478bd9Sstevel@tonic-gate 		if (strcmp(bkts[bktno].ref_lib, "<Unknown>") == 0)
1467c478bd9Sstevel@tonic-gate 			if (strcmp(bkts[bktno].obj, bind->obj))
1477c478bd9Sstevel@tonic-gate 				bkts[bktno].ref_lib = bind->obj;
1487c478bd9Sstevel@tonic-gate 	} else {
1497c478bd9Sstevel@tonic-gate 		bktno = (bktno + 1) % DEFBKTS;
1507c478bd9Sstevel@tonic-gate 		for (i = bktno; i < DEFBKTS; i = (i + 1) % DEFBKTS) {
1517c478bd9Sstevel@tonic-gate 			if (i == orig_bktno)
1527c478bd9Sstevel@tonic-gate 				break;
1537c478bd9Sstevel@tonic-gate 			if (!bkts[i].sym)
1547c478bd9Sstevel@tonic-gate 				continue;
1557c478bd9Sstevel@tonic-gate 			if (bkts[i].sym &&
1567c478bd9Sstevel@tonic-gate 			    (strcmp(bkts[i].sym, bind->sym)) == 0) {
1577c478bd9Sstevel@tonic-gate 				if (strcmp(bkts[i].ref_lib, "<Unknown>") == 0)
1587c478bd9Sstevel@tonic-gate 					if (strcmp(bkts[i].obj, bind->obj))
1597c478bd9Sstevel@tonic-gate 						bkts[i].ref_lib = bind->obj;
1607c478bd9Sstevel@tonic-gate 				break;
1617c478bd9Sstevel@tonic-gate 			}
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /* ========== stringcompare =============================================== */
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * DESCRIPTION:
1697c478bd9Sstevel@tonic-gate  * Compares two strings for qsort().
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate /* ======================================================================== */
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static int
stringcompare(binding_bucket * a,binding_bucket * b)1747c478bd9Sstevel@tonic-gate stringcompare(binding_bucket * a,
1757c478bd9Sstevel@tonic-gate     binding_bucket * b)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	char		*x = "\0";
1787c478bd9Sstevel@tonic-gate 	char		*y = "\0";
1797c478bd9Sstevel@tonic-gate 	int		retcode;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (a->sym)
1827c478bd9Sstevel@tonic-gate 		x = a->sym;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (b->sym)
1857c478bd9Sstevel@tonic-gate 		y = b->sym;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	retcode = strcoll(x, y);
1887c478bd9Sstevel@tonic-gate 	return (retcode);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /* ========== profile_binding ============================================= */
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * DESCRIPTION:
1947c478bd9Sstevel@tonic-gate  * Output the bindings directly to stdout or a file.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate /* ======================================================================== */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate static void
profile_binding(binding_bucket * bind)1997c478bd9Sstevel@tonic-gate profile_binding(binding_bucket * bind)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	char		*ref_lib_ptr;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (bind->sym && strcmp(bind->ref_lib, "<Unknown>")) {
2047c478bd9Sstevel@tonic-gate 		if (ref_lib_ptr = strrchr(bind->ref_lib, (int)'/')) {
2057c478bd9Sstevel@tonic-gate 			ref_lib_ptr++;
2067c478bd9Sstevel@tonic-gate 			if (bind->stbind)
2077c478bd9Sstevel@tonic-gate 				(void) fprintf(OUTPUT_FD,
2087c478bd9Sstevel@tonic-gate 				    "%s|%s|%s|%s|%s|%s|%s\n",
2097c478bd9Sstevel@tonic-gate 				    ref_lib_ptr,
2107c478bd9Sstevel@tonic-gate 				    bind->section,
2117c478bd9Sstevel@tonic-gate 				    bind->stbind,
2127c478bd9Sstevel@tonic-gate 				    bind->sttype,
2137c478bd9Sstevel@tonic-gate 				    bind->sym,
2147c478bd9Sstevel@tonic-gate 				    bind->def_lib,
2157c478bd9Sstevel@tonic-gate 				    bind->obj);
2167c478bd9Sstevel@tonic-gate 		} else if (bind->stbind)
2177c478bd9Sstevel@tonic-gate 			(void) fprintf(OUTPUT_FD,
2187c478bd9Sstevel@tonic-gate 			    "%s|%s|%s|%s|%s|%s|%s\n",
2197c478bd9Sstevel@tonic-gate 			    bind->ref_lib,
2207c478bd9Sstevel@tonic-gate 			    bind->section,
2217c478bd9Sstevel@tonic-gate 			    bind->stbind,
2227c478bd9Sstevel@tonic-gate 			    bind->sttype,
2237c478bd9Sstevel@tonic-gate 			    bind->sym,
2247c478bd9Sstevel@tonic-gate 			    bind->def_lib,
2257c478bd9Sstevel@tonic-gate 			    bind->obj);
2267c478bd9Sstevel@tonic-gate 	} else if (bind->sym && bind->stbind)
2277c478bd9Sstevel@tonic-gate 		(void) fprintf(OUTPUT_FD,
2287c478bd9Sstevel@tonic-gate 		    "%s|%s|%s|%s|%s\n",
2297c478bd9Sstevel@tonic-gate 		    bind->obj,
2307c478bd9Sstevel@tonic-gate 		    bind->section,
2317c478bd9Sstevel@tonic-gate 		    bind->stbind,
2327c478bd9Sstevel@tonic-gate 		    bind->sttype,
2337c478bd9Sstevel@tonic-gate 		    bind->sym);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /* ========== output_binding ============================================== */
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * DESCRIPTION:
2397c478bd9Sstevel@tonic-gate  * Output the hash table to either stdout or a file.
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate /* ======================================================================== */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate static void
output_binding(char * prog_name,char * target)2447c478bd9Sstevel@tonic-gate output_binding(char *prog_name,
2457c478bd9Sstevel@tonic-gate     char *target)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	int		i;
2487c478bd9Sstevel@tonic-gate 	char		*ref_lib_ptr;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	qsort(bkts,
2517c478bd9Sstevel@tonic-gate 	    DEFBKTS,
2527c478bd9Sstevel@tonic-gate 	    sizeof (binding_bucket),
2537c478bd9Sstevel@tonic-gate 	    (int (*) (const void *, const void *)) stringcompare);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (oflag) {
2567c478bd9Sstevel@tonic-gate 		if ((OUTPUT_FD = fopen(outputfile, "w")) == NULL) {
2577c478bd9Sstevel@tonic-gate 			if (sflag)
2587c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2597c478bd9Sstevel@tonic-gate 				    "\nfopen failed to open <%s>...\n\n",
2607c478bd9Sstevel@tonic-gate 				    outputfile);
2617c478bd9Sstevel@tonic-gate 			exit(1);
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 	/* generates profile report */
2657c478bd9Sstevel@tonic-gate 	(void) fprintf(OUTPUT_FD,
2667c478bd9Sstevel@tonic-gate 	    "#generated by %s\n",
2677c478bd9Sstevel@tonic-gate 	    prog_name);
2687c478bd9Sstevel@tonic-gate 	(void) fprintf(OUTPUT_FD,
2697c478bd9Sstevel@tonic-gate 	    "#profiling symbols in .text section of %s\n",
2707c478bd9Sstevel@tonic-gate 	    target);
2717c478bd9Sstevel@tonic-gate 	output_dtneeded(dt_needed);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	for (i = 0; i < DEFBKTS; i++) {
2747c478bd9Sstevel@tonic-gate 		if (bkts[i].sym && strcmp(bkts[i].ref_lib, "<Unknown>")) {
2757c478bd9Sstevel@tonic-gate 			if (ref_lib_ptr = strrchr(bkts[i].ref_lib, (int)'/')) {
2767c478bd9Sstevel@tonic-gate 				ref_lib_ptr++;
2777c478bd9Sstevel@tonic-gate 				if (bkts[i].stbind)
2787c478bd9Sstevel@tonic-gate 					(void) fprintf(OUTPUT_FD,
2797c478bd9Sstevel@tonic-gate 					    "%s|%s|%s|%s|%s|%s|%s\n",
2807c478bd9Sstevel@tonic-gate 					    ref_lib_ptr,
2817c478bd9Sstevel@tonic-gate 					    bkts[i].section,
2827c478bd9Sstevel@tonic-gate 					    bkts[i].stbind,
2837c478bd9Sstevel@tonic-gate 					    bkts[i].sttype,
2847c478bd9Sstevel@tonic-gate 					    bkts[i].sym,
2857c478bd9Sstevel@tonic-gate 					    bkts[i].def_lib,
2867c478bd9Sstevel@tonic-gate 					    bkts[i].obj);
2877c478bd9Sstevel@tonic-gate 			} else if (bkts[i].stbind)
2887c478bd9Sstevel@tonic-gate 				(void) fprintf(OUTPUT_FD,
2897c478bd9Sstevel@tonic-gate 				    "%s|%s|%s|%s|%s|%s|%s\n",
2907c478bd9Sstevel@tonic-gate 				    bkts[i].ref_lib,
2917c478bd9Sstevel@tonic-gate 				    bkts[i].section,
2927c478bd9Sstevel@tonic-gate 				    bkts[i].stbind,
2937c478bd9Sstevel@tonic-gate 				    bkts[i].sttype,
2947c478bd9Sstevel@tonic-gate 				    bkts[i].sym,
2957c478bd9Sstevel@tonic-gate 				    bkts[i].def_lib,
2967c478bd9Sstevel@tonic-gate 				    bkts[i].obj);
2977c478bd9Sstevel@tonic-gate 		} else if (bkts[i].sym && bkts[i].stbind)
2987c478bd9Sstevel@tonic-gate 			(void) fprintf(OUTPUT_FD,
2997c478bd9Sstevel@tonic-gate 			    "%s|%s|%s|%s|%s\n",
3007c478bd9Sstevel@tonic-gate 			    bkts[i].obj,
3017c478bd9Sstevel@tonic-gate 			    bkts[i].section,
3027c478bd9Sstevel@tonic-gate 			    bkts[i].stbind,
3037c478bd9Sstevel@tonic-gate 			    bkts[i].sttype,
3047c478bd9Sstevel@tonic-gate 			    bkts[i].sym);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /* ========== obj_init ==================================================== */
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate  * DESCRIPTION:
3117c478bd9Sstevel@tonic-gate  * Open (object) file, get ELF descriptor, and verify that the file is
3127c478bd9Sstevel@tonic-gate  * an ELF file.
3137c478bd9Sstevel@tonic-gate  */
3147c478bd9Sstevel@tonic-gate /* ======================================================================== */
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate static int
obj_init(obj_list * c)3177c478bd9Sstevel@tonic-gate obj_init(obj_list * c)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	int		mode = O_RDONLY;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	/* open the file */
3227c478bd9Sstevel@tonic-gate 	if ((c->obj->fd = open(c->obj->ename, mode)) < 0) {
3237c478bd9Sstevel@tonic-gate 		if (sflag) {
3247c478bd9Sstevel@tonic-gate 			if (errno == ENOENT)
3257c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3267c478bd9Sstevel@tonic-gate 				    "Cannot open <<%s>> : \
3277c478bd9Sstevel@tonic-gate 				    No such file or directory.\n",
3287c478bd9Sstevel@tonic-gate 				    c->obj->ename);
3297c478bd9Sstevel@tonic-gate 			else if (errno == EMFILE)
3307c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3317c478bd9Sstevel@tonic-gate 				    "File <<%s>> : Already opened.\n",
3327c478bd9Sstevel@tonic-gate 				    c->obj->ename);
3337c478bd9Sstevel@tonic-gate 		}
33407c94cbfSToomas Soome 		c->obj->fd = 0;
3357c478bd9Sstevel@tonic-gate 		return (FAIL);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 	/*
3387c478bd9Sstevel@tonic-gate 	 * queries the ELF library's internal version.
3397c478bd9Sstevel@tonic-gate 	 * Passing ver equal to EV_NONE causes elf_version() to return
3407c478bd9Sstevel@tonic-gate 	 * the library's internal version, without altering the working
3417c478bd9Sstevel@tonic-gate 	 * version.  If ver is a version known to the library,
3427c478bd9Sstevel@tonic-gate 	 * elf_version() returns the previous or initial working
3437c478bd9Sstevel@tonic-gate 	 * version number.  Otherwise, the working version remains
3447c478bd9Sstevel@tonic-gate 	 * unchanged and elf_version() returns EV_NONE.
3457c478bd9Sstevel@tonic-gate 	 */
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/* check if libelf.so is at the right level */
3487c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
3497c478bd9Sstevel@tonic-gate 		if (sflag)
3507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3517c478bd9Sstevel@tonic-gate 			    "Library out of date in ELF access routines.\n");
3527c478bd9Sstevel@tonic-gate 		return (FAIL);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * Before the first call to elf_begin(), it must call
3567c478bd9Sstevel@tonic-gate 	 * elf_version() to coordinate versions.
3577c478bd9Sstevel@tonic-gate 	 */
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/*
3607c478bd9Sstevel@tonic-gate 	 * get elf descriptor just to examine the contents of an existing
3617c478bd9Sstevel@tonic-gate 	 * file
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 	if ((c->obj->elf = elf_begin(c->obj->fd, ELF_C_READ, (Elf *) 0))
3647c478bd9Sstevel@tonic-gate 	    == (Elf *) 0) {
3657c478bd9Sstevel@tonic-gate 		if (sflag)
3667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3677c478bd9Sstevel@tonic-gate 			    "File is not in executable and \
3687c478bd9Sstevel@tonic-gate 			    linking format(ELF).\n");
3697c478bd9Sstevel@tonic-gate 		return (FAIL);
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 	/* Rule out COFF, a.out and shell script files */
3727c478bd9Sstevel@tonic-gate 	if (elf_kind(c->obj->elf) == ELF_K_COFF) {
3737c478bd9Sstevel@tonic-gate 		if (sflag) {
3747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3757c478bd9Sstevel@tonic-gate 			    "File is not in executable \
3767c478bd9Sstevel@tonic-gate 			    and linking format(ELF) or archive.\n");
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 		return (FAIL);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	if (elf_kind(c->obj->elf) != ELF_K_AR &&
3817c478bd9Sstevel@tonic-gate 	    elf_kind(c->obj->elf) != ELF_K_ELF) {
3827c478bd9Sstevel@tonic-gate 		if (sflag) {
3837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3847c478bd9Sstevel@tonic-gate 			    "File is not in executable and linking \
3857c478bd9Sstevel@tonic-gate 			    format(ELF) or archive.\n");
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 		return (FAIL);
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	return (SUCCEED);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate /* ========== obj_elf_hdr ================================================= */
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate  * DESCRIPTION:
3957c478bd9Sstevel@tonic-gate  * Obtain the elf header, verify elf header information
3967c478bd9Sstevel@tonic-gate  */
3977c478bd9Sstevel@tonic-gate /* ======================================================================== */
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate static int
obj_elf_hdr(obj_list * c)4007c478bd9Sstevel@tonic-gate obj_elf_hdr(obj_list * c)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate #if	defined(_LP64)
4037c478bd9Sstevel@tonic-gate 	Elf64_Ehdr	*ptr;
4047c478bd9Sstevel@tonic-gate #else
4057c478bd9Sstevel@tonic-gate 	Elf32_Ehdr	*ptr;
4067c478bd9Sstevel@tonic-gate #endif
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * get the elf header if one is available for the ELF descriptor
4107c478bd9Sstevel@tonic-gate 	 * c->elf
4117c478bd9Sstevel@tonic-gate 	 */
4127c478bd9Sstevel@tonic-gate #if	defined(_LP64)
4137c478bd9Sstevel@tonic-gate 	if ((ptr = elf64_getehdr(c->obj->elf)) == (Elf64_Ehdr *) 0) {
4147c478bd9Sstevel@tonic-gate 		if (sflag)
4157c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4167c478bd9Sstevel@tonic-gate 			    "File is not in 64-bit format.\n");
4177c478bd9Sstevel@tonic-gate 		return (FAIL);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate #else
4207c478bd9Sstevel@tonic-gate 	if ((ptr = elf32_getehdr(c->obj->elf)) == (Elf32_Ehdr *) 0) {
4217c478bd9Sstevel@tonic-gate 		if (sflag)
4227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4237c478bd9Sstevel@tonic-gate 			    "File is not in 32-bit format.\n");
4247c478bd9Sstevel@tonic-gate 		return (FAIL);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate #endif
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/* if there is elf header, save the pointer */
4297c478bd9Sstevel@tonic-gate #if defined(_LP64)
4307c478bd9Sstevel@tonic-gate 	c->obj->ehdr = (Elf64_Ehdr *) ptr;
4317c478bd9Sstevel@tonic-gate #else
4327c478bd9Sstevel@tonic-gate 	c->obj->ehdr = (Elf32_Ehdr *) ptr;
4337c478bd9Sstevel@tonic-gate #endif
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/* e_ident[] is identification index which holds values */
4367c478bd9Sstevel@tonic-gate 	/*
4377c478bd9Sstevel@tonic-gate 	 * we could also use elf_getident() to retrieve file identification
4387c478bd9Sstevel@tonic-gate 	 * data.
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	/*
4427c478bd9Sstevel@tonic-gate 	 * e_ident[EI_CLASS] identifies the file's class:
4437c478bd9Sstevel@tonic-gate 	 * ELFCLASSNONE - invalid class
4447c478bd9Sstevel@tonic-gate 	 * ELFCLASS32   - 32-bit objects
4457c478bd9Sstevel@tonic-gate 	 * ELFCLASS64   - 64-bit objects
4467c478bd9Sstevel@tonic-gate 	 */
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate #if	defined(_LP64)
4497c478bd9Sstevel@tonic-gate 	if (ptr->e_ident[EI_CLASS] != ELFCLASS64) {
4507c478bd9Sstevel@tonic-gate 		if (sflag)
4517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4527c478bd9Sstevel@tonic-gate 			    "File is not in 64-bit format.\n");
4537c478bd9Sstevel@tonic-gate 		return (FAIL);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate #else
4567c478bd9Sstevel@tonic-gate 	if (ptr->e_ident[EI_CLASS] != ELFCLASS32) {
4577c478bd9Sstevel@tonic-gate 		if (sflag)
4587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4597c478bd9Sstevel@tonic-gate 			    "File is not in 32-bit format.\n");
4607c478bd9Sstevel@tonic-gate 		return (FAIL);
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate #endif
4637c478bd9Sstevel@tonic-gate 	/*
4647c478bd9Sstevel@tonic-gate 	 * e_ident[EI_DATA] specifies the data encoding of the
4657c478bd9Sstevel@tonic-gate 	 * processor-specific data in the object file:
4667c478bd9Sstevel@tonic-gate 	 * ELFDATANONE - invalid data encoding
4677c478bd9Sstevel@tonic-gate 	 * ELFDATA2LSB - specifies 2's complement values, with the least
4687c478bd9Sstevel@tonic-gate 	 * significant byte occupying the lowest address
4697c478bd9Sstevel@tonic-gate 	 * ELFDATA2MSB - specifies 2's complement values, with the most
4707c478bd9Sstevel@tonic-gate 	 * significant byte occupying the lowest address
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/*
4747c478bd9Sstevel@tonic-gate 	 * e_ident[EI_VERSION] specifies the ELF header version number.
4757c478bd9Sstevel@tonic-gate 	 * Currently, this value must be EV_CURRENT.
4767c478bd9Sstevel@tonic-gate 	 */
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (!(ptr->e_ident[EI_VERSION] == EV_CURRENT) &&
4797c478bd9Sstevel@tonic-gate 	    (ptr->e_version == EV_CURRENT)) {
4807c478bd9Sstevel@tonic-gate 		if (sflag)
4817c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4827c478bd9Sstevel@tonic-gate 			    "File is recorded in an \
4837c478bd9Sstevel@tonic-gate 			    incompatible ELF version.\n");
4847c478bd9Sstevel@tonic-gate 		return (FAIL);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	/* only interested in relocatable, shared object, or executable file */
4877c478bd9Sstevel@tonic-gate 	switch (ptr->e_type) {
4887c478bd9Sstevel@tonic-gate 	case ET_REL:
4897c478bd9Sstevel@tonic-gate 	case ET_EXEC:
4907c478bd9Sstevel@tonic-gate 	case ET_DYN:
4917c478bd9Sstevel@tonic-gate 		break;
4927c478bd9Sstevel@tonic-gate 	default:
4937c478bd9Sstevel@tonic-gate 		if (sflag) {
4947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4957c478bd9Sstevel@tonic-gate 			    "File is not relocatable, ");
4967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4977c478bd9Sstevel@tonic-gate 			    "executable, or shared object.\n");
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		return (FAIL);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	/*
5037c478bd9Sstevel@tonic-gate 	 * e_machine's value specifies the required architecture for an
5047c478bd9Sstevel@tonic-gate 	 * individual file
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate #if defined(__sparcv9)
5087c478bd9Sstevel@tonic-gate 	if (ptr->e_machine != EM_SPARCV9) {
5097c478bd9Sstevel@tonic-gate 		if (sflag)
5107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5117c478bd9Sstevel@tonic-gate 			    "File is not for 64-bit \
5127c478bd9Sstevel@tonic-gate 			    SPARC machine architecture.\n");
5137c478bd9Sstevel@tonic-gate 		return (FAIL);
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate #elif defined(__amd64)
5167c478bd9Sstevel@tonic-gate 	if (ptr->e_machine != EM_AMD64) {
5177c478bd9Sstevel@tonic-gate 		if (sflag)
5187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5197c478bd9Sstevel@tonic-gate 			    "File is not for 64-bit \
5207c478bd9Sstevel@tonic-gate 			    amd64 machine architecture.\n");
5217c478bd9Sstevel@tonic-gate 		return (FAIL);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate #elif defined(__i386)
5247c478bd9Sstevel@tonic-gate 	if (ptr->e_machine != EM_386) {
5257c478bd9Sstevel@tonic-gate 		if (sflag)
5267c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5277c478bd9Sstevel@tonic-gate 			    "File is not for 32-bit \
5287c478bd9Sstevel@tonic-gate 			    i386 machine architecture.\n");
5297c478bd9Sstevel@tonic-gate 		return (FAIL);
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate #else
5327c478bd9Sstevel@tonic-gate 	if (ptr->e_machine != EM_SPARC) {
5337c478bd9Sstevel@tonic-gate 		if (sflag)
5347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5357c478bd9Sstevel@tonic-gate 			    "File is not for 32-bit \
5367c478bd9Sstevel@tonic-gate 			    SPARC machine architecture.\n");
5377c478bd9Sstevel@tonic-gate 		return (FAIL);
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate #endif
5407c478bd9Sstevel@tonic-gate 	return (SUCCEED);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /* ========== obj_prog_hdr ============================================= */
5447c478bd9Sstevel@tonic-gate /*
5457c478bd9Sstevel@tonic-gate  * DESCRIPTION:
5467c478bd9Sstevel@tonic-gate  * For executable files and shared objects only, check if it has
5477c478bd9Sstevel@tonic-gate  * a program header table.
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate /* ===================================================================== */
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate static int
obj_prog_hdr(obj_list * c)5527c478bd9Sstevel@tonic-gate obj_prog_hdr(obj_list * c)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * Assume:  the elf header has already been read, and the file
5567c478bd9Sstevel@tonic-gate 	 * has already been determined to be
5577c478bd9Sstevel@tonic-gate 	 * executable, shared object, or relocatable
5587c478bd9Sstevel@tonic-gate 	 */
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	/*
5617c478bd9Sstevel@tonic-gate 	 * Program headers are meaningful only for executable and shared
5627c478bd9Sstevel@tonic-gate 	 * object files.  It is an array of structures, each describing a
5637c478bd9Sstevel@tonic-gate 	 * segment or other information needs to prepare the program for
5647c478bd9Sstevel@tonic-gate 	 * execution.
5657c478bd9Sstevel@tonic-gate 	 */
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/* skip if file is not executable or shared object */
5687c478bd9Sstevel@tonic-gate 	/* e_type == ET_REL meaning Relocatable file */
5697c478bd9Sstevel@tonic-gate 	if (c->obj->ehdr->e_type == ET_REL)
5707c478bd9Sstevel@tonic-gate 		return (SUCCEED);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/*
5737c478bd9Sstevel@tonic-gate 	 * ehdr->e_phoff holds the program header table's file offset in
5747c478bd9Sstevel@tonic-gate 	 * bytes.
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate 	/* If the file has no program header table, this member holds zero. */
5777c478bd9Sstevel@tonic-gate 	/*
5787c478bd9Sstevel@tonic-gate 	 * ehdr->e_phnum holds the number of entries in the program header
5797c478bd9Sstevel@tonic-gate 	 * table.
5807c478bd9Sstevel@tonic-gate 	 */
5817c478bd9Sstevel@tonic-gate 	/*
5827c478bd9Sstevel@tonic-gate 	 * If a file has no program header table, e_phnum holds the value
5837c478bd9Sstevel@tonic-gate 	 * zero.
5847c478bd9Sstevel@tonic-gate 	 */
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/* make sure there's a program header table */
5877c478bd9Sstevel@tonic-gate 	if ((c->obj->ehdr->e_phoff == 0) ||
5887c478bd9Sstevel@tonic-gate 	    (c->obj->ehdr->e_phnum == 0)) {
5897c478bd9Sstevel@tonic-gate 		if (sflag)
5907c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5917c478bd9Sstevel@tonic-gate 			    "File has no program header table.\n");
5927c478bd9Sstevel@tonic-gate 		return (FAIL);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 	return (SUCCEED);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate /* ========== find_dynamic_sect ========================================== */
5987c478bd9Sstevel@tonic-gate /*
5997c478bd9Sstevel@tonic-gate  * DESCRIPTION:
6007c478bd9Sstevel@tonic-gate  * Find the dynamic section.
6017c478bd9Sstevel@tonic-gate  */
6027c478bd9Sstevel@tonic-gate /* ======================================================================= */
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate static int
find_dynamic_sect(obj_list * c)6057c478bd9Sstevel@tonic-gate find_dynamic_sect(obj_list * c)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate #if	defined(_LP64)
6087c478bd9Sstevel@tonic-gate 	Elf64_Shdr	*scurrent;	/* temp 64 bit section pointer */
6097c478bd9Sstevel@tonic-gate #else
6107c478bd9Sstevel@tonic-gate 	Elf32_Shdr	*scurrent;	/* temp 32 bit section pointer */
6117c478bd9Sstevel@tonic-gate #endif
6127c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn;	/* temp section header pointer */
6137c478bd9Sstevel@tonic-gate 	Elf_Data	*ddata;	/* temp data header pointer */
6147c478bd9Sstevel@tonic-gate 	size_t		index;	/* temp section header table index */
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	c->obj->dynnames = NULL; /* init of dynamic string table ptr */
6177c478bd9Sstevel@tonic-gate 	c->obj->dynsect = NULL;	/* init of dynamic section ptr */
6187c478bd9Sstevel@tonic-gate 	c->obj->ddata = NULL;	/* init of dynamic strtab data ptr */
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/* only process executables and shared objects */
6217c478bd9Sstevel@tonic-gate 	if (c->obj->ehdr->e_type != ET_EXEC && c->obj->ehdr->e_type != ET_DYN)
6227c478bd9Sstevel@tonic-gate 		return (SUCCEED);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	if ((c->obj->ehdr->e_shoff == 0) || (c->obj->ehdr->e_shnum == 0)) {
6257c478bd9Sstevel@tonic-gate 		/* there are no sections */
6267c478bd9Sstevel@tonic-gate 		return (SUCCEED);
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 	/* search the section header table for dynamic section */
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/* start with null section; section index = 0 */
6317c478bd9Sstevel@tonic-gate 	scn = 0;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	while ((scn = elf_nextscn(c->obj->elf, scn)) != 0) {
6347c478bd9Sstevel@tonic-gate 		/* retrieve the section header */
6357c478bd9Sstevel@tonic-gate #if	defined(_LP64)
6367c478bd9Sstevel@tonic-gate 		scurrent = elf64_getshdr(scn);
6377c478bd9Sstevel@tonic-gate #else
6387c478bd9Sstevel@tonic-gate 		scurrent = elf32_getshdr(scn);
6397c478bd9Sstevel@tonic-gate #endif
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		/* check for dynamic section; (i.e., .dynamic) */
6427c478bd9Sstevel@tonic-gate 		if (scurrent->sh_type == SHT_DYNAMIC) {
6437c478bd9Sstevel@tonic-gate 			ddata = 0;
6447c478bd9Sstevel@tonic-gate 			if ((ddata = elf_getdata(scn, ddata)) == 0 ||
6457c478bd9Sstevel@tonic-gate 			    (ddata->d_size == 0))
6467c478bd9Sstevel@tonic-gate 				return (SUCCEED);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 			/* now, we got data of dynamic section */
6497c478bd9Sstevel@tonic-gate 			c->obj->dynsect = ddata->d_buf;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 			/* index to section header for dynamic string table */
6527c478bd9Sstevel@tonic-gate 			index = scurrent->sh_link;
6537c478bd9Sstevel@tonic-gate 			/* get scn descriptor of dynamic string table */
6547c478bd9Sstevel@tonic-gate 			scn = elf_getscn(c->obj->elf, index);
6557c478bd9Sstevel@tonic-gate 			/* get dynamic string table section header */
6567c478bd9Sstevel@tonic-gate #if	defined(_LP64)
6577c478bd9Sstevel@tonic-gate 			scurrent = elf64_getshdr(scn);
6587c478bd9Sstevel@tonic-gate #else
6597c478bd9Sstevel@tonic-gate 			scurrent = elf32_getshdr(scn);
6607c478bd9Sstevel@tonic-gate #endif
6617c478bd9Sstevel@tonic-gate 			/* get the dynamic string table data descriptor */
6627c478bd9Sstevel@tonic-gate 			c->obj->ddata = elf_getdata(scn, (c->obj->ddata));
6637c478bd9Sstevel@tonic-gate 			/* save the pointer to dynamic string table data */
6647c478bd9Sstevel@tonic-gate 			c->obj->dynnames = c->obj->ddata->d_buf;
6657c478bd9Sstevel@tonic-gate 			/*
6667c478bd9Sstevel@tonic-gate 			 * now, we got dynamic strtab and dynamic section
6677c478bd9Sstevel@tonic-gate 			 * information
6687c478bd9Sstevel@tonic-gate 			 */
6697c478bd9Sstevel@tonic-gate 			break;
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 	return (SUCCEED);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate /* ========== find_symtabs ================================================ */
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate  * DESCRIPTION:
6787c478bd9Sstevel@tonic-gate  * Find and check symbol tables for an application file
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate /* ======================================================================== */
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate static int
find_symtabs(obj_list * c)6837c478bd9Sstevel@tonic-gate find_symtabs(obj_list * c)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate #if	defined(_LP64)
6867c478bd9Sstevel@tonic-gate 	Elf64_Shdr	*shdr;
6877c478bd9Sstevel@tonic-gate #else
6887c478bd9Sstevel@tonic-gate 	Elf32_Shdr	*shdr;
6897c478bd9Sstevel@tonic-gate #endif
6907c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn, *scn2;
6917c478bd9Sstevel@tonic-gate 	Elf_Data	*data;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	c->obj->sym_tab = NULL;
6947c478bd9Sstevel@tonic-gate 	c->obj->sym_num = 0;
6957c478bd9Sstevel@tonic-gate 	c->obj->sym_names = NULL;
6967c478bd9Sstevel@tonic-gate 	c->obj->dsym_tab = NULL;
6977c478bd9Sstevel@tonic-gate 	c->obj->dsym_num = 0;
6987c478bd9Sstevel@tonic-gate 	c->obj->dsym_names = NULL;
6997c478bd9Sstevel@tonic-gate 	c->obj->sym_data = NULL;
7007c478bd9Sstevel@tonic-gate 	c->obj->dsym_data = NULL;
7017c478bd9Sstevel@tonic-gate 	scn = 0;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/*
7047c478bd9Sstevel@tonic-gate 	 * loop through the section header table looking for symbol tables.
7057c478bd9Sstevel@tonic-gate 	 * There must be one or two:  .symtab and .dynsym
7067c478bd9Sstevel@tonic-gate 	 * upon finding a symbol table, save its pointer in obj_com.
7077c478bd9Sstevel@tonic-gate 	 */
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	/* get section descriptor */
7107c478bd9Sstevel@tonic-gate 	while ((scn = elf_nextscn(c->obj->elf, scn)) != 0) {
7117c478bd9Sstevel@tonic-gate #if	defined(_LP64)
7127c478bd9Sstevel@tonic-gate 		Elf64_Sym	*syms;
7137c478bd9Sstevel@tonic-gate #else
7147c478bd9Sstevel@tonic-gate 		Elf32_Sym	*syms;
7157c478bd9Sstevel@tonic-gate #endif
7167c478bd9Sstevel@tonic-gate 		int		symn;
7177c478bd9Sstevel@tonic-gate 		char		*strs;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 		/* point to section header */
7207c478bd9Sstevel@tonic-gate #if	defined(_LP64)
7217c478bd9Sstevel@tonic-gate 		shdr = elf64_getshdr(scn);
7227c478bd9Sstevel@tonic-gate #else
7237c478bd9Sstevel@tonic-gate 		shdr = elf32_getshdr(scn);
7247c478bd9Sstevel@tonic-gate #endif
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		if (shdr == 0)
7277c478bd9Sstevel@tonic-gate 			return (FAIL);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		/* skip if this section is not a symbol table */
7307c478bd9Sstevel@tonic-gate 		if ((shdr->sh_type != SHT_DYNSYM) &&
7317c478bd9Sstevel@tonic-gate 		    (shdr->sh_type != SHT_SYMTAB))
7327c478bd9Sstevel@tonic-gate 			continue;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		/* get data descriptor for the symbol table itself */
7357c478bd9Sstevel@tonic-gate 		data = elf_getdata(scn, NULL);
7367c478bd9Sstevel@tonic-gate 		if (data == NULL)
7377c478bd9Sstevel@tonic-gate 			continue;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		/* save pointer to symbol table */
7407c478bd9Sstevel@tonic-gate #if	defined(_LP64)
7417c478bd9Sstevel@tonic-gate 		syms = (Elf64_Sym *) data->d_buf;
7427c478bd9Sstevel@tonic-gate #else
7437c478bd9Sstevel@tonic-gate 		syms = (Elf32_Sym *) data->d_buf;
7447c478bd9Sstevel@tonic-gate #endif
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 		/*
7477c478bd9Sstevel@tonic-gate 		 * now start looking for the string table associated with
7487c478bd9Sstevel@tonic-gate 		 * this symbol table section
7497c478bd9Sstevel@tonic-gate 		 */
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		/* get section descriptor first */
7527c478bd9Sstevel@tonic-gate 		scn2 = elf_getscn(c->obj->elf, shdr->sh_link);
7537c478bd9Sstevel@tonic-gate 		if (scn2 == NULL)
7547c478bd9Sstevel@tonic-gate 			continue;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		/* get data descriptor for the string table section */
7577c478bd9Sstevel@tonic-gate 		data = elf_getdata(scn2, NULL);
7587c478bd9Sstevel@tonic-gate 		if (data == NULL)
7597c478bd9Sstevel@tonic-gate 			continue;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		/* save pointer to name string table */
7627c478bd9Sstevel@tonic-gate 		strs = data->d_buf;
7637c478bd9Sstevel@tonic-gate 		symn = shdr->sh_size / shdr->sh_entsize;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		/* save information in obj_com */
7667c478bd9Sstevel@tonic-gate 		if (shdr->sh_type == SHT_SYMTAB) {
7677c478bd9Sstevel@tonic-gate 			c->obj->sym_tab = syms;
7687c478bd9Sstevel@tonic-gate 			c->obj->sym_num = symn;
7697c478bd9Sstevel@tonic-gate 			c->obj->sym_names = strs;
7707c478bd9Sstevel@tonic-gate 			c->obj->sym_data = data;
7717c478bd9Sstevel@tonic-gate 		} else {	/* must be the dynamic linking symbol table */
7727c478bd9Sstevel@tonic-gate 			c->obj->dsym_tab = syms;
7737c478bd9Sstevel@tonic-gate 			c->obj->dsym_num = symn;
7747c478bd9Sstevel@tonic-gate 			c->obj->dsym_names = strs;
7757c478bd9Sstevel@tonic-gate 			c->obj->dsym_data = data;
7767c478bd9Sstevel@tonic-gate 		}		/* end if */
7777c478bd9Sstevel@tonic-gate 	}			/* end while */
7787c478bd9Sstevel@tonic-gate 	return (SUCCEED);
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate /* ========== obj_app_symtab ============================================== */
7827c478bd9Sstevel@tonic-gate /*
7837c478bd9Sstevel@tonic-gate  * DESCRIPTION:
7847c478bd9Sstevel@tonic-gate  * Check existence of application's symbol tables.
7857c478bd9Sstevel@tonic-gate  */
7867c478bd9Sstevel@tonic-gate /* ======================================================================== */
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate static int
obj_app_symtab(obj_list * c)7897c478bd9Sstevel@tonic-gate obj_app_symtab(obj_list * c)
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 	/* issue error if a relocatable file has no symbol table */
7927c478bd9Sstevel@tonic-gate 	if (c->obj->sym_tab == NULL) {
7937c478bd9Sstevel@tonic-gate 		if (c->obj->ehdr->e_type == ET_REL) {
7947c478bd9Sstevel@tonic-gate 			if (sflag)
7957c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
7967c478bd9Sstevel@tonic-gate 				    "ELF error: no symbol \
7977c478bd9Sstevel@tonic-gate 				    table in object file.\n");
7987c478bd9Sstevel@tonic-gate 			return (FAIL);
7997c478bd9Sstevel@tonic-gate 		} else {
8007c478bd9Sstevel@tonic-gate 			if (c->obj->dsym_tab == NULL) {
8017c478bd9Sstevel@tonic-gate 				if (sflag) {
8027c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8037c478bd9Sstevel@tonic-gate 					    "Warning: Binary is \
8047c478bd9Sstevel@tonic-gate 					    completely statically \
8057c478bd9Sstevel@tonic-gate 					    linked and stripped.\n");
8067c478bd9Sstevel@tonic-gate 				}
8077c478bd9Sstevel@tonic-gate 				return (FAIL);
8087c478bd9Sstevel@tonic-gate 			}
8097c478bd9Sstevel@tonic-gate 			if (sflag)
8107c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8117c478bd9Sstevel@tonic-gate 				    "Binary is stripped.\n");
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate 	return (SUCCEED);
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate /* ========== obj_finis =================================================== */
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate  * DESCRIPTION:
8207c478bd9Sstevel@tonic-gate  * It checks the c->fd and c->elf pointers.  If they are not NULL,
8217c478bd9Sstevel@tonic-gate  * close the file descriptor and ELF descriptor.
8227c478bd9Sstevel@tonic-gate  */
8237c478bd9Sstevel@tonic-gate /* ======================================================================== */
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate static void
obj_finis(obj_list * c)8267c478bd9Sstevel@tonic-gate obj_finis(obj_list * c)
8277c478bd9Sstevel@tonic-gate {
8287c478bd9Sstevel@tonic-gate 	obj_list	*p;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	if (c) {
8317c478bd9Sstevel@tonic-gate 		while (c) {
8327c478bd9Sstevel@tonic-gate 			if (c->obj->elf != (Elf *) 0)
8337c478bd9Sstevel@tonic-gate 				(void) elf_end(c->obj->elf);
8347c478bd9Sstevel@tonic-gate 			if (c->obj->fd != 0)
8357c478bd9Sstevel@tonic-gate 				(void) close(c->obj->fd);
8367c478bd9Sstevel@tonic-gate 			p = c;
8377c478bd9Sstevel@tonic-gate 			c = c->next;
8387c478bd9Sstevel@tonic-gate 			free(p->obj);
8397c478bd9Sstevel@tonic-gate 			free(p);
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate /* ========= is_text_section ============================================== */
8457c478bd9Sstevel@tonic-gate /*
8467c478bd9Sstevel@tonic-gate  * DESCRIPTION:
8477c478bd9Sstevel@tonic-gate  * Scan through every section and returns TRUE(1) if the given section
8487c478bd9Sstevel@tonic-gate  * is ".text", otherwise, returns FALSE(0).
8497c478bd9Sstevel@tonic-gate  * INPUTS:        shndx - section header index
8507c478bd9Sstevel@tonic-gate  * elf_file - ELF descriptor of the object file under test
8517c478bd9Sstevel@tonic-gate  * ehdr - ELF header of the object file under test
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate /* ======================================================================== */
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate static int
is_text_section(int shndx,Elf * elf_file,Elf64_Ehdr * ehdr)8567c478bd9Sstevel@tonic-gate is_text_section(int shndx,
8577c478bd9Sstevel@tonic-gate     Elf * elf_file,
8587c478bd9Sstevel@tonic-gate #if	defined(_LP64)
8597c478bd9Sstevel@tonic-gate     Elf64_Ehdr * ehdr)
8607c478bd9Sstevel@tonic-gate #else
8617c478bd9Sstevel@tonic-gate     Elf32_Ehdr * ehdr)
8627c478bd9Sstevel@tonic-gate #endif
8637c478bd9Sstevel@tonic-gate {
8647c478bd9Sstevel@tonic-gate 	char		*sym_name;
8657c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn = elf_getscn(elf_file, shndx);
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	if (scn != NULL) {
8687c478bd9Sstevel@tonic-gate #if	defined(_LP64)
8697c478bd9Sstevel@tonic-gate 		Elf64_Shdr	*shdr;
8707c478bd9Sstevel@tonic-gate 		shdr = elf64_getshdr(scn);
8717c478bd9Sstevel@tonic-gate #else
8727c478bd9Sstevel@tonic-gate 		Elf32_Shdr	*shdr;
8737c478bd9Sstevel@tonic-gate 		shdr = elf32_getshdr(scn);
8747c478bd9Sstevel@tonic-gate #endif
8757c478bd9Sstevel@tonic-gate 		sym_name = elf_strptr(elf_file,
8767c478bd9Sstevel@tonic-gate 				    ehdr->e_shstrndx,
8777c478bd9Sstevel@tonic-gate 				    shdr->sh_name);
8787c478bd9Sstevel@tonic-gate 		if (strcmp(sym_name, ".text") == 0)
8797c478bd9Sstevel@tonic-gate 			return (1);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 	return (0);
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate /* ========== scan_archive_symbols ======================================= */
8857c478bd9Sstevel@tonic-gate /*
8867c478bd9Sstevel@tonic-gate  * DESCRIPTION:
8877c478bd9Sstevel@tonic-gate  * Scan through the archive symbol tables and write them out.
8887c478bd9Sstevel@tonic-gate  * INPUTS:        syms - pointer to application symbol table
8897c478bd9Sstevel@tonic-gate  * symn - number of entries in application symbol table
8907c478bd9Sstevel@tonic-gate  * buf  - first byte of application string table
8917c478bd9Sstevel@tonic-gate  */
8927c478bd9Sstevel@tonic-gate /* ======================================================================= */
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate static void
scan_archive_symbols(obj_list * c,Elf64_Sym * syms,int symn,char * buf,Elf * elf_file,Elf64_Ehdr * ehdr)8957c478bd9Sstevel@tonic-gate scan_archive_symbols(obj_list * c,
8967c478bd9Sstevel@tonic-gate #if	defined(_LP64)
8977c478bd9Sstevel@tonic-gate     Elf64_Sym * syms,
8987c478bd9Sstevel@tonic-gate #else
8997c478bd9Sstevel@tonic-gate     Elf32_Sym * syms,
9007c478bd9Sstevel@tonic-gate #endif
9017c478bd9Sstevel@tonic-gate     int symn,
9027c478bd9Sstevel@tonic-gate     char *buf,
9037c478bd9Sstevel@tonic-gate     Elf * elf_file,
9047c478bd9Sstevel@tonic-gate #if	defined(_LP64)
9057c478bd9Sstevel@tonic-gate     Elf64_Ehdr * ehdr)
9067c478bd9Sstevel@tonic-gate #else
9077c478bd9Sstevel@tonic-gate     Elf32_Ehdr * ehdr)
9087c478bd9Sstevel@tonic-gate #endif
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate #if	defined(_LP64)
9117c478bd9Sstevel@tonic-gate 	Elf64_Sym	*symtab_entry;
9127c478bd9Sstevel@tonic-gate #else
9137c478bd9Sstevel@tonic-gate 	Elf32_Sym	*symtab_entry;
9147c478bd9Sstevel@tonic-gate #endif
9157c478bd9Sstevel@tonic-gate 	int		i;
9167c478bd9Sstevel@tonic-gate 	char		*sym_name;
9177c478bd9Sstevel@tonic-gate 	int		sttype;
9187c478bd9Sstevel@tonic-gate 	int		stbind;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	symtab_entry = syms;
9217c478bd9Sstevel@tonic-gate 	for (i = 0; i < symn; i++, symtab_entry++) {
9227c478bd9Sstevel@tonic-gate 		binding_bucket *binding;
9237c478bd9Sstevel@tonic-gate 		/* look only at .text section symbols */
9247c478bd9Sstevel@tonic-gate 		if (!is_text_section(symtab_entry->st_shndx, elf_file, ehdr))
9257c478bd9Sstevel@tonic-gate 			continue;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		/* look only at weak and global symbols */
9287c478bd9Sstevel@tonic-gate #if	defined(_LP64)
9297c478bd9Sstevel@tonic-gate 		stbind = ELF64_ST_BIND(symtab_entry->st_info);
9307c478bd9Sstevel@tonic-gate #else
9317c478bd9Sstevel@tonic-gate 		stbind = ELF32_ST_BIND(symtab_entry->st_info);
9327c478bd9Sstevel@tonic-gate #endif
9337c478bd9Sstevel@tonic-gate 		if (stbind != STB_GLOBAL) {
9347c478bd9Sstevel@tonic-gate 			if (stbind != STB_WEAK)
9357c478bd9Sstevel@tonic-gate 				continue;
9367c478bd9Sstevel@tonic-gate 		}
9377c478bd9Sstevel@tonic-gate 		/* look only at functions and objects */
9387c478bd9Sstevel@tonic-gate #if	defined(_LP64)
9397c478bd9Sstevel@tonic-gate 		sttype = ELF64_ST_TYPE(symtab_entry->st_info);
9407c478bd9Sstevel@tonic-gate #else
9417c478bd9Sstevel@tonic-gate 		sttype = ELF32_ST_TYPE(symtab_entry->st_info);
9427c478bd9Sstevel@tonic-gate #endif
9437c478bd9Sstevel@tonic-gate 		if (sttype != STT_FUNC) {
9447c478bd9Sstevel@tonic-gate 			if (sttype != STT_OBJECT)
9457c478bd9Sstevel@tonic-gate 				continue;
9467c478bd9Sstevel@tonic-gate 		}
9477c478bd9Sstevel@tonic-gate 		sym_name = buf + symtab_entry->st_name;
9487c478bd9Sstevel@tonic-gate 		binding = (struct binding_bucket *)
9497c478bd9Sstevel@tonic-gate 			    malloc(sizeof (binding_bucket));
9507c478bd9Sstevel@tonic-gate 		binding->sym = sym_name;
9517c478bd9Sstevel@tonic-gate 		binding->obj = c->obj->ename;
9527c478bd9Sstevel@tonic-gate 		binding->section = "TEXT";
9537c478bd9Sstevel@tonic-gate 		binding->ref_lib = "<Unknown>";
9547c478bd9Sstevel@tonic-gate 		binding->def_lib = "*DIRECT*";
9557c478bd9Sstevel@tonic-gate 		if (stbind == STB_GLOBAL)
9567c478bd9Sstevel@tonic-gate 			binding->stbind = "GLOB";
9577c478bd9Sstevel@tonic-gate 		else if (stbind == STB_WEAK)
9587c478bd9Sstevel@tonic-gate 			binding->stbind = "WEAK";
9597c478bd9Sstevel@tonic-gate 		if (sttype == STT_FUNC)
9607c478bd9Sstevel@tonic-gate 			binding->sttype = "FUNC";
9617c478bd9Sstevel@tonic-gate 		else if (sttype == STT_OBJECT)
9627c478bd9Sstevel@tonic-gate 			binding->sttype = "OBJT";
9637c478bd9Sstevel@tonic-gate 		if (pflag)
9647c478bd9Sstevel@tonic-gate 			profile_binding(binding);
9657c478bd9Sstevel@tonic-gate 		else
9667c478bd9Sstevel@tonic-gate 			store_binding(binding);
9677c478bd9Sstevel@tonic-gate 	}			/* end for */
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate /* ========== scan_symbols ================================================ */
9717c478bd9Sstevel@tonic-gate /*
9727c478bd9Sstevel@tonic-gate  * DESCRIPTION:
9737c478bd9Sstevel@tonic-gate  * Scan through the symbol table and write them out.
9747c478bd9Sstevel@tonic-gate  * INPUTS:        syms - pointer to application symbol table
9757c478bd9Sstevel@tonic-gate  * symn - number of entries in application symbol table
9767c478bd9Sstevel@tonic-gate  * buf  - first byte of application string table
9777c478bd9Sstevel@tonic-gate  */
9787c478bd9Sstevel@tonic-gate /* ======================================================================== */
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate static void
scan_symbols(obj_list * c,Elf64_Sym * syms,int symn,char * buf)9817c478bd9Sstevel@tonic-gate scan_symbols(obj_list * c,
9827c478bd9Sstevel@tonic-gate #if	defined(_LP64)
9837c478bd9Sstevel@tonic-gate     Elf64_Sym * syms,
9847c478bd9Sstevel@tonic-gate #else
9857c478bd9Sstevel@tonic-gate     Elf32_Sym * syms,
9867c478bd9Sstevel@tonic-gate #endif
9877c478bd9Sstevel@tonic-gate     int symn,
9887c478bd9Sstevel@tonic-gate     char *buf)
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate #if	defined(_LP64)
9917c478bd9Sstevel@tonic-gate 	Elf64_Sym	*symtab_entry;
9927c478bd9Sstevel@tonic-gate #else
9937c478bd9Sstevel@tonic-gate 	Elf32_Sym	*symtab_entry;
9947c478bd9Sstevel@tonic-gate #endif
9957c478bd9Sstevel@tonic-gate 	int		i;
9967c478bd9Sstevel@tonic-gate 	char		*sym_name;
9977c478bd9Sstevel@tonic-gate 	int		sttype;
9987c478bd9Sstevel@tonic-gate 	int		stbind;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	symtab_entry = syms;
10017c478bd9Sstevel@tonic-gate 	if (pflag) {
10027c478bd9Sstevel@tonic-gate 		(void) fprintf(OUTPUT_FD,
10037c478bd9Sstevel@tonic-gate 		    "#profiling symbols in .text section of %s\n",
10047c478bd9Sstevel@tonic-gate 		    c->obj->ename);
10057c478bd9Sstevel@tonic-gate 		output_dtneeded(dt_needed);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 	for (i = 0; i < symn; i++, symtab_entry++) {
10087c478bd9Sstevel@tonic-gate 		binding_bucket *binding;
10097c478bd9Sstevel@tonic-gate 		/* look only at .text section symbols */
10107c478bd9Sstevel@tonic-gate 		if (!is_text_section(symtab_entry->st_shndx,
1011149775e4SDan McDonald 		    c->obj->elf, c->obj->ehdr))
10127c478bd9Sstevel@tonic-gate 			continue;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 		/* look only at weak and global symbols */
10157c478bd9Sstevel@tonic-gate #if	defined(_LP64)
10167c478bd9Sstevel@tonic-gate 		stbind = ELF64_ST_BIND(symtab_entry->st_info);
10177c478bd9Sstevel@tonic-gate #else
10187c478bd9Sstevel@tonic-gate 		stbind = ELF32_ST_BIND(symtab_entry->st_info);
10197c478bd9Sstevel@tonic-gate #endif
10207c478bd9Sstevel@tonic-gate 		if (stbind != STB_GLOBAL) {
10217c478bd9Sstevel@tonic-gate 			if (stbind != STB_WEAK)
10227c478bd9Sstevel@tonic-gate 				continue;
10237c478bd9Sstevel@tonic-gate 		}
10247c478bd9Sstevel@tonic-gate 		/* look only at functions and objects */
10257c478bd9Sstevel@tonic-gate #if	defined(_LP64)
10267c478bd9Sstevel@tonic-gate 		sttype = ELF64_ST_TYPE(symtab_entry->st_info);
10277c478bd9Sstevel@tonic-gate #else
10287c478bd9Sstevel@tonic-gate 		sttype = ELF32_ST_TYPE(symtab_entry->st_info);
10297c478bd9Sstevel@tonic-gate #endif
10307c478bd9Sstevel@tonic-gate 		if (sttype != STT_FUNC) {
10317c478bd9Sstevel@tonic-gate 			if (sttype != STT_OBJECT)
10327c478bd9Sstevel@tonic-gate 				continue;
10337c478bd9Sstevel@tonic-gate 		}
10347c478bd9Sstevel@tonic-gate 		sym_name = buf + symtab_entry->st_name;
10357c478bd9Sstevel@tonic-gate 		binding = (struct binding_bucket *)
1036149775e4SDan McDonald 		    malloc(sizeof (binding_bucket));
10377c478bd9Sstevel@tonic-gate 		binding->sym = sym_name;
10387c478bd9Sstevel@tonic-gate 		binding->obj = c->obj->ename;
10397c478bd9Sstevel@tonic-gate 		binding->section = "TEXT";
10407c478bd9Sstevel@tonic-gate 		binding->ref_lib = "<Unknown>";
10417c478bd9Sstevel@tonic-gate 		binding->def_lib = "*DIRECT*";
10427c478bd9Sstevel@tonic-gate 		if (stbind == STB_GLOBAL)
10437c478bd9Sstevel@tonic-gate 			binding->stbind = "GLOB";
10447c478bd9Sstevel@tonic-gate 		else if (stbind == STB_WEAK)
10457c478bd9Sstevel@tonic-gate 			binding->stbind = "WEAK";
10467c478bd9Sstevel@tonic-gate 		if (sttype == STT_FUNC)
10477c478bd9Sstevel@tonic-gate 			binding->sttype = "FUNC";
10487c478bd9Sstevel@tonic-gate 		else if (sttype == STT_OBJECT)
10497c478bd9Sstevel@tonic-gate 			binding->sttype = "OBJT";
10507c478bd9Sstevel@tonic-gate 		if (pflag)
10517c478bd9Sstevel@tonic-gate 			profile_binding(binding);
10527c478bd9Sstevel@tonic-gate 		else
10537c478bd9Sstevel@tonic-gate 			store_binding(binding);
10547c478bd9Sstevel@tonic-gate 	}			/* end for */
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate /* ========= bind_symbols ================================================= */
10587c478bd9Sstevel@tonic-gate /*
10597c478bd9Sstevel@tonic-gate  * DESCRIPTION:
10607c478bd9Sstevel@tonic-gate  * Scan through the dynamic symbol table and write them out.
10617c478bd9Sstevel@tonic-gate  * INPUTS:        syms - pointer to application symbol table
10627c478bd9Sstevel@tonic-gate  * symn - number of entries in application symbol table
10637c478bd9Sstevel@tonic-gate  * buf  - first byte of application string table
10647c478bd9Sstevel@tonic-gate  */
10657c478bd9Sstevel@tonic-gate /* ======================================================================== */
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate static void
bind_symbols(obj_list * c,Elf64_Sym * syms,int symn,char * buf)10687c478bd9Sstevel@tonic-gate bind_symbols(obj_list * c,
10697c478bd9Sstevel@tonic-gate #if	defined(_LP64)
10707c478bd9Sstevel@tonic-gate     Elf64_Sym * syms,
10717c478bd9Sstevel@tonic-gate #else
10727c478bd9Sstevel@tonic-gate     Elf32_Sym * syms,
10737c478bd9Sstevel@tonic-gate #endif
10747c478bd9Sstevel@tonic-gate     int symn,
10757c478bd9Sstevel@tonic-gate     char *buf)
10767c478bd9Sstevel@tonic-gate {
10777c478bd9Sstevel@tonic-gate #if	defined(_LP64)
10787c478bd9Sstevel@tonic-gate 	Elf64_Sym	*symtab_entry;
10797c478bd9Sstevel@tonic-gate #else
10807c478bd9Sstevel@tonic-gate 	Elf32_Sym	*symtab_entry;
10817c478bd9Sstevel@tonic-gate #endif
10827c478bd9Sstevel@tonic-gate 	int		i;
10837c478bd9Sstevel@tonic-gate 	char		*sym_name;
10847c478bd9Sstevel@tonic-gate 	binding_bucket	*binding;
10857c478bd9Sstevel@tonic-gate 	int		sttype;
10867c478bd9Sstevel@tonic-gate 	int		stbind;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	symtab_entry = syms;
10897c478bd9Sstevel@tonic-gate 	for (i = 0; i < symn; i++, symtab_entry++) {
10907c478bd9Sstevel@tonic-gate 		/* look only at global symbols */
10917c478bd9Sstevel@tonic-gate #if	defined(_LP64)
10927c478bd9Sstevel@tonic-gate 		stbind = ELF64_ST_BIND(symtab_entry->st_info);
10937c478bd9Sstevel@tonic-gate #else
10947c478bd9Sstevel@tonic-gate 		stbind = ELF32_ST_BIND(symtab_entry->st_info);
10957c478bd9Sstevel@tonic-gate #endif
10967c478bd9Sstevel@tonic-gate 		if (symtab_entry->st_shndx == SHN_UNDEF)
10977c478bd9Sstevel@tonic-gate 			continue;
10987c478bd9Sstevel@tonic-gate 		if (symtab_entry->st_shndx == SHN_ABS)
10997c478bd9Sstevel@tonic-gate 			continue;
11007c478bd9Sstevel@tonic-gate 		if (stbind != STB_GLOBAL) {
11017c478bd9Sstevel@tonic-gate 			if (stbind != STB_WEAK)
11027c478bd9Sstevel@tonic-gate 				continue;
11037c478bd9Sstevel@tonic-gate 		}
11047c478bd9Sstevel@tonic-gate 		/* look only at functions and objects */
11057c478bd9Sstevel@tonic-gate #if	defined(_LP64)
11067c478bd9Sstevel@tonic-gate 		sttype = ELF64_ST_TYPE(symtab_entry->st_info);
11077c478bd9Sstevel@tonic-gate #else
11087c478bd9Sstevel@tonic-gate 		sttype = ELF32_ST_TYPE(symtab_entry->st_info);
11097c478bd9Sstevel@tonic-gate #endif
11107c478bd9Sstevel@tonic-gate 		if (sttype != STT_FUNC) {
11117c478bd9Sstevel@tonic-gate 			if (sttype != STT_OBJECT)
11127c478bd9Sstevel@tonic-gate 				continue;
11137c478bd9Sstevel@tonic-gate 		}
11147c478bd9Sstevel@tonic-gate 		sym_name = buf + symtab_entry->st_name;
11157c478bd9Sstevel@tonic-gate 		binding = (binding_bucket *) malloc(sizeof (binding_bucket));
11167c478bd9Sstevel@tonic-gate 		binding->obj = c->obj->ename;
11177c478bd9Sstevel@tonic-gate 		binding->sym = sym_name;
11187c478bd9Sstevel@tonic-gate 		if (!pflag)
11197c478bd9Sstevel@tonic-gate 			check_store_binding(binding);
11207c478bd9Sstevel@tonic-gate 	}			/* end for */
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate /* ========== get_scnfd =================================================== */
11247c478bd9Sstevel@tonic-gate /*
11257c478bd9Sstevel@tonic-gate  * DESCRIPTION:
11267c478bd9Sstevel@tonic-gate  * Gets section descriptor for the associated string table
11277c478bd9Sstevel@tonic-gate  * and verifies that the type of the section pointed to is
11287c478bd9Sstevel@tonic-gate  * indeed of type STRTAB.  Returns a valid section descriptor
11297c478bd9Sstevel@tonic-gate  * or NULL on error.
11307c478bd9Sstevel@tonic-gate  */
11317c478bd9Sstevel@tonic-gate /* ======================================================================== */
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate static Elf_Scn *
get_scnfd(Elf * e_file,int shstrtab,int SCN_TYPE)11347c478bd9Sstevel@tonic-gate get_scnfd(Elf * e_file,
11357c478bd9Sstevel@tonic-gate     int shstrtab,
11367c478bd9Sstevel@tonic-gate     int SCN_TYPE)
11377c478bd9Sstevel@tonic-gate {
11387c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn_fd;
11397c478bd9Sstevel@tonic-gate #if	defined(_LP64)
11407c478bd9Sstevel@tonic-gate 	Elf64_Shdr	*shdr;
11417c478bd9Sstevel@tonic-gate #else
11427c478bd9Sstevel@tonic-gate 	Elf32_Shdr	*shdr;
11437c478bd9Sstevel@tonic-gate #endif
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	if ((scn_fd = elf_getscn(e_file, shstrtab)) == NULL)
11467c478bd9Sstevel@tonic-gate 		return (NULL);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate #if	defined(_LP64)
11497c478bd9Sstevel@tonic-gate 	shdr = elf64_getshdr(scn_fd);
11507c478bd9Sstevel@tonic-gate #else
11517c478bd9Sstevel@tonic-gate 	shdr = elf32_getshdr(scn_fd);
11527c478bd9Sstevel@tonic-gate #endif
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	if (shdr->sh_type != SCN_TYPE)
11557c478bd9Sstevel@tonic-gate 		return (NULL);
11567c478bd9Sstevel@tonic-gate 	return (scn_fd);
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate /* ========== print_symtab ================================================ */
11607c478bd9Sstevel@tonic-gate /*
11617c478bd9Sstevel@tonic-gate  * DESCRIPTION:
11627c478bd9Sstevel@tonic-gate  * Outputs symbol bindings from symbol table to hash table.
11637c478bd9Sstevel@tonic-gate  */
11647c478bd9Sstevel@tonic-gate /* ======================================================================== */
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate static void
print_symtab(obj_list * com,Elf * elf_file,Elf64_Ehdr * ehdr,Elf64_Shdr * shdr,Elf_Scn * p_sd,char * filename)11677c478bd9Sstevel@tonic-gate print_symtab(obj_list * com,
11687c478bd9Sstevel@tonic-gate     Elf * elf_file,
11697c478bd9Sstevel@tonic-gate #if	defined(_LP64)
11707c478bd9Sstevel@tonic-gate     Elf64_Ehdr * ehdr,
11717c478bd9Sstevel@tonic-gate     Elf64_Shdr * shdr,
11727c478bd9Sstevel@tonic-gate #else
11737c478bd9Sstevel@tonic-gate     Elf32_Ehdr * ehdr,
11747c478bd9Sstevel@tonic-gate     Elf32_Shdr * shdr,
11757c478bd9Sstevel@tonic-gate #endif
11767c478bd9Sstevel@tonic-gate     Elf_Scn * p_sd,
11777c478bd9Sstevel@tonic-gate     char *filename)
11787c478bd9Sstevel@tonic-gate {
11797c478bd9Sstevel@tonic-gate #if	defined(_LP64)
11807c478bd9Sstevel@tonic-gate 	Elf64_Sym	*syms;
11817c478bd9Sstevel@tonic-gate #else
11827c478bd9Sstevel@tonic-gate 	Elf32_Sym	*syms;
11837c478bd9Sstevel@tonic-gate #endif
11847c478bd9Sstevel@tonic-gate 	Elf_Data	*data;
11857c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn;
11867c478bd9Sstevel@tonic-gate 	int		count = 0;
11877c478bd9Sstevel@tonic-gate 	char		*strs, *fullname;
11887c478bd9Sstevel@tonic-gate 	obj_list	*c;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	c = (obj_list *) malloc(sizeof (obj_list));
11917c478bd9Sstevel@tonic-gate 	c->obj = (obj_com *) malloc(sizeof (obj_com));
11927c478bd9Sstevel@tonic-gate 	fullname = (char *)malloc(strlen(com->obj->ename)
1193*b172429eSMarco van Wieringen 	    + strlen(filename) + 3);
1194*b172429eSMarco van Wieringen 	(void) strcpy(fullname, com->obj->ename);
1195*b172429eSMarco van Wieringen 	(void) strcat(fullname, "(");
1196*b172429eSMarco van Wieringen 	(void) strcat(fullname, filename);
1197*b172429eSMarco van Wieringen 	(void) strcat(fullname, ")");
11987c478bd9Sstevel@tonic-gate 	c->obj->ename = fullname;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	if ((data = elf_getdata(p_sd, NULL)) == NULL) {
12017c478bd9Sstevel@tonic-gate 		if (sflag)
12027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12037c478bd9Sstevel@tonic-gate 			    "%s - No symbol table data\n",
12047c478bd9Sstevel@tonic-gate 			    c->obj->ename);
12057c478bd9Sstevel@tonic-gate 		return;
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate #if	defined(_LP64)
12087c478bd9Sstevel@tonic-gate 	syms = (Elf64_Sym *) data->d_buf;
12097c478bd9Sstevel@tonic-gate #else
12107c478bd9Sstevel@tonic-gate 	syms = (Elf32_Sym *) data->d_buf;
12117c478bd9Sstevel@tonic-gate #endif
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	scn = elf_getscn(elf_file, shdr->sh_link);
12147c478bd9Sstevel@tonic-gate 	if (scn == NULL)
12157c478bd9Sstevel@tonic-gate 		return;
12167c478bd9Sstevel@tonic-gate 	data = elf_getdata(scn, NULL);
12177c478bd9Sstevel@tonic-gate 	if (data == NULL)
12187c478bd9Sstevel@tonic-gate 		return;
12197c478bd9Sstevel@tonic-gate 	strs = data->d_buf;
12207c478bd9Sstevel@tonic-gate 	count = shdr->sh_size / shdr->sh_entsize;
12217c478bd9Sstevel@tonic-gate 	if (syms == NULL) {
12227c478bd9Sstevel@tonic-gate 		if (sflag)
12237c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12247c478bd9Sstevel@tonic-gate 			    "%s: Problem reading symbol data\n",
12257c478bd9Sstevel@tonic-gate 			    c->obj->ename);
12267c478bd9Sstevel@tonic-gate 		return;
12277c478bd9Sstevel@tonic-gate 	}
12287c478bd9Sstevel@tonic-gate 	c->obj->sym_tab = syms;
12297c478bd9Sstevel@tonic-gate 	c->obj->sym_num = count;
12307c478bd9Sstevel@tonic-gate 	c->obj->sym_names = strs;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	if (aflag)
12337c478bd9Sstevel@tonic-gate 		(void) scan_archive_symbols(c,
12347c478bd9Sstevel@tonic-gate 		    c->obj->sym_tab,
12357c478bd9Sstevel@tonic-gate 		    c->obj->sym_num,
12367c478bd9Sstevel@tonic-gate 		    c->obj->sym_names,
12377c478bd9Sstevel@tonic-gate 		    elf_file,
12387c478bd9Sstevel@tonic-gate 		    ehdr);
12397c478bd9Sstevel@tonic-gate 	else
12407c478bd9Sstevel@tonic-gate 		(void) bind_symbols(c,
12417c478bd9Sstevel@tonic-gate 		    c->obj->sym_tab,
12427c478bd9Sstevel@tonic-gate 		    c->obj->sym_num,
12437c478bd9Sstevel@tonic-gate 		    c->obj->sym_names);
12447c478bd9Sstevel@tonic-gate 	free(c->obj);
12457c478bd9Sstevel@tonic-gate 	free(c);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate /* ========== get_symtab ================================================== */
12497c478bd9Sstevel@tonic-gate /*
12507c478bd9Sstevel@tonic-gate  * DESCRIPTION:
12517c478bd9Sstevel@tonic-gate  * Gets the symbol table.  This function does not output the contents
12527c478bd9Sstevel@tonic-gate  * of the symbol table but sets up the parameters and then calls
12537c478bd9Sstevel@tonic-gate  * print_symtab() to output the symbol bindings.
12547c478bd9Sstevel@tonic-gate  */
12557c478bd9Sstevel@tonic-gate /* ======================================================================== */
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate static void
get_symtab(obj_list * c,Elf * elf_file,Elf64_Ehdr * ehdr,char * filename)12587c478bd9Sstevel@tonic-gate get_symtab(obj_list * c,
12597c478bd9Sstevel@tonic-gate     Elf * elf_file,
12607c478bd9Sstevel@tonic-gate #if	defined(_LP64)
12617c478bd9Sstevel@tonic-gate     Elf64_Ehdr * ehdr,
12627c478bd9Sstevel@tonic-gate #else
12637c478bd9Sstevel@tonic-gate     Elf32_Ehdr * ehdr,
12647c478bd9Sstevel@tonic-gate #endif
12657c478bd9Sstevel@tonic-gate     char *filename)
12667c478bd9Sstevel@tonic-gate {
12677c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn, *scnfd;
12687c478bd9Sstevel@tonic-gate 	Elf_Data	*data;
12697c478bd9Sstevel@tonic-gate #if	defined(_LP64)
12707c478bd9Sstevel@tonic-gate 	Elf64_Word	symtabtype;
12717c478bd9Sstevel@tonic-gate #else
12727c478bd9Sstevel@tonic-gate 	Elf32_Word	symtabtype;
12737c478bd9Sstevel@tonic-gate #endif
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	/* get section header string table */
12767c478bd9Sstevel@tonic-gate 	scnfd = get_scnfd(elf_file, ehdr->e_shstrndx, SHT_STRTAB);
12777c478bd9Sstevel@tonic-gate 	if (scnfd == NULL) {
12787c478bd9Sstevel@tonic-gate 		if (sflag)
12797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12807c478bd9Sstevel@tonic-gate 			    "%s: Could not get string table\n",
12817c478bd9Sstevel@tonic-gate 			    filename);
12827c478bd9Sstevel@tonic-gate 		return;
12837c478bd9Sstevel@tonic-gate 	}
12847c478bd9Sstevel@tonic-gate 	data = elf_getdata(scnfd, NULL);
12857c478bd9Sstevel@tonic-gate 	if (data->d_size == 0) {
12867c478bd9Sstevel@tonic-gate 		if (sflag)
12877c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12887c478bd9Sstevel@tonic-gate 			    "%s: No data in string table\n",
12897c478bd9Sstevel@tonic-gate 			    filename);
12907c478bd9Sstevel@tonic-gate 		return;
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 	symtabtype = SHT_SYMTAB;
12937c478bd9Sstevel@tonic-gate 	scn = 0;
12947c478bd9Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf_file, scn)) != 0) {
12957c478bd9Sstevel@tonic-gate #if	defined(_LP64)
12967c478bd9Sstevel@tonic-gate 		Elf64_Shdr	*shdr;
12977c478bd9Sstevel@tonic-gate 		if ((shdr = elf64_getshdr(scn)) == NULL)
12987c478bd9Sstevel@tonic-gate #else
12997c478bd9Sstevel@tonic-gate 		Elf32_Shdr	*shdr;
13007c478bd9Sstevel@tonic-gate 		if ((shdr = elf32_getshdr(scn)) == NULL)
13017c478bd9Sstevel@tonic-gate #endif
13027c478bd9Sstevel@tonic-gate 		{
13037c478bd9Sstevel@tonic-gate 			if (sflag)
13047c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
13057c478bd9Sstevel@tonic-gate 				    "%s: %s:\n",
13067c478bd9Sstevel@tonic-gate 				    filename,
13077c478bd9Sstevel@tonic-gate 				    elf_errmsg(-1));
13087c478bd9Sstevel@tonic-gate 			return;
13097c478bd9Sstevel@tonic-gate 		}
13107c478bd9Sstevel@tonic-gate 		if (shdr->sh_type == symtabtype)
13117c478bd9Sstevel@tonic-gate 			print_symtab(c, elf_file, ehdr, shdr, scn, filename);
13127c478bd9Sstevel@tonic-gate 	}			/* end while */
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate /* ========== process ===================================================== */
13167c478bd9Sstevel@tonic-gate /*
13177c478bd9Sstevel@tonic-gate  * DESCRIPTION:
13187c478bd9Sstevel@tonic-gate  * Gets the ELF header and, if it exists, call get_symtab() to begin
13197c478bd9Sstevel@tonic-gate  * processing of the file; otherwise, returns with a warning.
13207c478bd9Sstevel@tonic-gate  */
13217c478bd9Sstevel@tonic-gate /* ======================================================================== */
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate static void
process(obj_list * c,Elf * elf_file,char * filename)13247c478bd9Sstevel@tonic-gate process(obj_list * c,
13257c478bd9Sstevel@tonic-gate     Elf * elf_file,
13267c478bd9Sstevel@tonic-gate     char *filename)
13277c478bd9Sstevel@tonic-gate {
13287c478bd9Sstevel@tonic-gate #if	defined(_LP64)
13297c478bd9Sstevel@tonic-gate 	Elf64_Ehdr	*ehdr;
13307c478bd9Sstevel@tonic-gate #else
13317c478bd9Sstevel@tonic-gate 	Elf32_Ehdr	*ehdr;
13327c478bd9Sstevel@tonic-gate #endif
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate #if	defined(_LP64)
13357c478bd9Sstevel@tonic-gate 	if ((ehdr = elf64_getehdr(elf_file)) == NULL)
13367c478bd9Sstevel@tonic-gate #else
13377c478bd9Sstevel@tonic-gate 	if ((ehdr = elf32_getehdr(elf_file)) == NULL)
13387c478bd9Sstevel@tonic-gate #endif
13397c478bd9Sstevel@tonic-gate 	{
13407c478bd9Sstevel@tonic-gate 		if (sflag)
13417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13427c478bd9Sstevel@tonic-gate 			    "%s: %s\n",
13437c478bd9Sstevel@tonic-gate 			    filename, elf_errmsg(-1));
13447c478bd9Sstevel@tonic-gate 		return;
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 	get_symtab(c, elf_file, ehdr, filename);
13477c478bd9Sstevel@tonic-gate }
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate /* ========== process_archive ============================================= */
13507c478bd9Sstevel@tonic-gate /*
13517c478bd9Sstevel@tonic-gate  * DESCRIPTION:
13527c478bd9Sstevel@tonic-gate  * Processes member files of an archive.  This function provides
13537c478bd9Sstevel@tonic-gate  * a loop through an archive equivalent the processing of each_file
13547c478bd9Sstevel@tonic-gate  * for individual object file.
13557c478bd9Sstevel@tonic-gate  */
13567c478bd9Sstevel@tonic-gate /* ======================================================================== */
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate static int
process_archive(obj_list * c)13597c478bd9Sstevel@tonic-gate process_archive(obj_list * c)
13607c478bd9Sstevel@tonic-gate {
13617c478bd9Sstevel@tonic-gate 	Elf_Arhdr	*p_ar;
13627c478bd9Sstevel@tonic-gate 	Elf		*arf;
13637c478bd9Sstevel@tonic-gate 	Elf_Cmd		cmd = ELF_C_READ;
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	while ((arf = elf_begin(c->obj->fd, cmd, c->obj->elf)) != 0) {
13667c478bd9Sstevel@tonic-gate 		p_ar = elf_getarhdr(arf);
13677c478bd9Sstevel@tonic-gate 		if (p_ar == NULL) {
13687c478bd9Sstevel@tonic-gate 			if (sflag)
13697c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
13707c478bd9Sstevel@tonic-gate 				    "%s: %s\n",
13717c478bd9Sstevel@tonic-gate 				    c->obj->filename, elf_errmsg(-1));
13727c478bd9Sstevel@tonic-gate 			return (FAIL);
13737c478bd9Sstevel@tonic-gate 		}
13747c478bd9Sstevel@tonic-gate 		if ((int)strncmp(p_ar->ar_name, "/", 1) == 0) {
13757c478bd9Sstevel@tonic-gate 			cmd = elf_next(arf);
13767c478bd9Sstevel@tonic-gate 			(void) elf_end(arf);
13777c478bd9Sstevel@tonic-gate 			continue;
13787c478bd9Sstevel@tonic-gate 		}
13797c478bd9Sstevel@tonic-gate 		if (elf_kind(arf) == ELF_K_ELF) {
13807c478bd9Sstevel@tonic-gate 			process(c, arf, p_ar->ar_name);
13817c478bd9Sstevel@tonic-gate 		} else {
13827c478bd9Sstevel@tonic-gate 			cmd = elf_next(arf);
13837c478bd9Sstevel@tonic-gate 			(void) elf_end(arf);
13847c478bd9Sstevel@tonic-gate 			continue;
13857c478bd9Sstevel@tonic-gate 		}
13867c478bd9Sstevel@tonic-gate 		cmd = elf_next(arf);
13877c478bd9Sstevel@tonic-gate 		(void) elf_end(arf);
13887c478bd9Sstevel@tonic-gate 	}			/* end while */
13897c478bd9Sstevel@tonic-gate 	return (SUCCEED);
13907c478bd9Sstevel@tonic-gate }
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate /* ========== add_dtneeded ================================================ */
13937c478bd9Sstevel@tonic-gate /*
13947c478bd9Sstevel@tonic-gate  * DESCRIPTION:
13957c478bd9Sstevel@tonic-gate  * Inserts a new node into the linked list.  It is basically for
13967c478bd9Sstevel@tonic-gate  * generating a simple linked list of DT_NEEDED entries.
13977c478bd9Sstevel@tonic-gate  */
13987c478bd9Sstevel@tonic-gate /* ======================================================================== */
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate static dt_list *
add_dtneeded(dt_list * p,dt_list * node)14017c478bd9Sstevel@tonic-gate add_dtneeded(dt_list * p,
14027c478bd9Sstevel@tonic-gate     dt_list * node)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate 	dt_list		*head = p, *tail;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	if (!head)
14077c478bd9Sstevel@tonic-gate 		head = node;
14087c478bd9Sstevel@tonic-gate 	else {
14097c478bd9Sstevel@tonic-gate 		tail = head;
14107c478bd9Sstevel@tonic-gate 		if (strcmp(tail->libname, node->libname) == 0) {
14117c478bd9Sstevel@tonic-gate 			free(node);
14127c478bd9Sstevel@tonic-gate 			return (head);
14137c478bd9Sstevel@tonic-gate 		}
14147c478bd9Sstevel@tonic-gate 		while (tail->next != NULL) {
14157c478bd9Sstevel@tonic-gate 			tail = tail->next;
14167c478bd9Sstevel@tonic-gate 			if (strcmp(tail->libname, node->libname) == 0) {
14177c478bd9Sstevel@tonic-gate 				free(node);
14187c478bd9Sstevel@tonic-gate 				return (head);
14197c478bd9Sstevel@tonic-gate 			}
14207c478bd9Sstevel@tonic-gate 		}
14217c478bd9Sstevel@tonic-gate 		tail->next = node;
14227c478bd9Sstevel@tonic-gate 	}
14237c478bd9Sstevel@tonic-gate 	return (head);
14247c478bd9Sstevel@tonic-gate }
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate /* ========== find_dtneeded =============================================== */
14277c478bd9Sstevel@tonic-gate /*
14287c478bd9Sstevel@tonic-gate  * DESCRIPTION:
14297c478bd9Sstevel@tonic-gate  * Find the DT_NEEDED, DT_FILTER, and DT_AUXILIARY entries, and save
14307c478bd9Sstevel@tonic-gate  * them to link list.
14317c478bd9Sstevel@tonic-gate  */
14327c478bd9Sstevel@tonic-gate /* ======================================================================== */
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate static void
find_dtneeded(obj_list * c)14357c478bd9Sstevel@tonic-gate find_dtneeded(obj_list * c)
14367c478bd9Sstevel@tonic-gate {
14377c478bd9Sstevel@tonic-gate #if	defined(_LP64)
14387c478bd9Sstevel@tonic-gate 	Elf64_Dyn	*dcurrent; /* temp 64 bit dynamic table entry ptr */
14397c478bd9Sstevel@tonic-gate #else
14407c478bd9Sstevel@tonic-gate 	Elf32_Dyn	*dcurrent; /* temp 32 bit dynamic table entry ptr */
14417c478bd9Sstevel@tonic-gate #endif
14427c478bd9Sstevel@tonic-gate 	dt_list		*tmp_lib;
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	dcurrent = c->obj->dynsect;
14457c478bd9Sstevel@tonic-gate 	if (!dcurrent)
14467c478bd9Sstevel@tonic-gate 		return;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	/*
14497c478bd9Sstevel@tonic-gate 	 * If there are any DT_NEEDED
14507c478bd9Sstevel@tonic-gate 	 * entries, add them to the dt_needed list.
14517c478bd9Sstevel@tonic-gate 	 */
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	while (dcurrent->d_tag != DT_NULL) {
14547c478bd9Sstevel@tonic-gate 		if (dcurrent->d_tag == DT_NEEDED) {
14557c478bd9Sstevel@tonic-gate 			tmp_lib = (dt_list *) malloc(sizeof (dt_list));
14567c478bd9Sstevel@tonic-gate 			tmp_lib->libname = c->obj->dynnames +
1457149775e4SDan McDonald 			    dcurrent->d_un.d_val;
14587c478bd9Sstevel@tonic-gate 			tmp_lib->d_tag = dcurrent->d_tag;
14597c478bd9Sstevel@tonic-gate 			tmp_lib->next = NULL;
14607c478bd9Sstevel@tonic-gate 			dt_needed = add_dtneeded(dt_needed, tmp_lib);
14617c478bd9Sstevel@tonic-gate 		}
14627c478bd9Sstevel@tonic-gate 		dcurrent++;
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate /* ========= obj_elfcheck ================================================= */
14677c478bd9Sstevel@tonic-gate /*
14687c478bd9Sstevel@tonic-gate  * DESCRIPTION:
14697c478bd9Sstevel@tonic-gate  * It checks the elf header and saves its pointer if succeeds.
14707c478bd9Sstevel@tonic-gate  * It checks the program header and saves its pointer if succeed.
14717c478bd9Sstevel@tonic-gate  * It checks the section header table and saves its pointer to
14727c478bd9Sstevel@tonic-gate  * section header table and section header string table if it
14737c478bd9Sstevel@tonic-gate  * succeeds.  It finds dynsym symbol table and saves its pointer.
14747c478bd9Sstevel@tonic-gate  * It finds symtab and saves its pointers.
14757c478bd9Sstevel@tonic-gate  */
14767c478bd9Sstevel@tonic-gate /* ======================================================================== */
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate static int
obj_elfcheck(obj_list * c)14797c478bd9Sstevel@tonic-gate obj_elfcheck(obj_list * c)
14807c478bd9Sstevel@tonic-gate {
14817c478bd9Sstevel@tonic-gate 	/* open the file and ELF descriptor */
14827c478bd9Sstevel@tonic-gate 	if (obj_init(c) == FAIL) {
14837c478bd9Sstevel@tonic-gate 		obj_finis(c);
14847c478bd9Sstevel@tonic-gate 		return (FAIL);
14857c478bd9Sstevel@tonic-gate 	}
14867c478bd9Sstevel@tonic-gate 	/* if it is an archive library */
14877c478bd9Sstevel@tonic-gate 	if (elf_kind(c->obj->elf) == ELF_K_AR) {
14887c478bd9Sstevel@tonic-gate 		if (process_archive(c) == SUCCEED)
14897c478bd9Sstevel@tonic-gate 			return (SUCCEED);
14907c478bd9Sstevel@tonic-gate 		else
14917c478bd9Sstevel@tonic-gate 			return (FAIL);
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 	/* get the ELF header information */
14947c478bd9Sstevel@tonic-gate 	if (obj_elf_hdr(c) == FAIL) {
14957c478bd9Sstevel@tonic-gate 		obj_finis(c);
14967c478bd9Sstevel@tonic-gate 		return (FAIL);
14977c478bd9Sstevel@tonic-gate 	}
14987c478bd9Sstevel@tonic-gate 	/* get the program header for dynamic, etc. */
14997c478bd9Sstevel@tonic-gate 	if (obj_prog_hdr(c) == FAIL) {
15007c478bd9Sstevel@tonic-gate 		obj_finis(c);
15017c478bd9Sstevel@tonic-gate 		return (FAIL);
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate 	/* find and save pointers to application symbol tables */
15047c478bd9Sstevel@tonic-gate 	if (find_symtabs(c) == FAIL) {
15057c478bd9Sstevel@tonic-gate 		obj_finis(c);
15067c478bd9Sstevel@tonic-gate 		return (FAIL);
15077c478bd9Sstevel@tonic-gate 	}
15087c478bd9Sstevel@tonic-gate 	/* check the existence of application's symbol tables */
15097c478bd9Sstevel@tonic-gate 	if (obj_app_symtab(c) == FAIL) {
15107c478bd9Sstevel@tonic-gate 		obj_finis(c);
15117c478bd9Sstevel@tonic-gate 		return (FAIL);
15127c478bd9Sstevel@tonic-gate 	}
15137c478bd9Sstevel@tonic-gate 	/* find and save pointers to the dynamic section */
15147c478bd9Sstevel@tonic-gate 	if (find_dynamic_sect(c) == FAIL) {
15157c478bd9Sstevel@tonic-gate 		obj_finis(c);
15167c478bd9Sstevel@tonic-gate 		return (FAIL);
15177c478bd9Sstevel@tonic-gate 	}
15187c478bd9Sstevel@tonic-gate 	/*
15197c478bd9Sstevel@tonic-gate 	 * find the DT_NEEDED entries and save the name to dt_needed link
15207c478bd9Sstevel@tonic-gate 	 * list
15217c478bd9Sstevel@tonic-gate 	 */
15227c478bd9Sstevel@tonic-gate 	(void) find_dtneeded(c);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	return (SUCCEED);
15257c478bd9Sstevel@tonic-gate }
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate /* ========= analyze_dependency ========================================== */
15287c478bd9Sstevel@tonic-gate /*
15297c478bd9Sstevel@tonic-gate  * DESCRIPTION:
15307c478bd9Sstevel@tonic-gate  * Read in an dependency object file and analyze it.
15317c478bd9Sstevel@tonic-gate  * INPUTS:        dep_file - dependency object file name
15327c478bd9Sstevel@tonic-gate  */
15337c478bd9Sstevel@tonic-gate /* ======================================================================= */
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate static int
analyze_dependency(char * dep_file)15367c478bd9Sstevel@tonic-gate analyze_dependency(char *dep_file)
15377c478bd9Sstevel@tonic-gate {
15387c478bd9Sstevel@tonic-gate 	obj_list	*dep_obj;
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	if (!dep_file)
15417c478bd9Sstevel@tonic-gate 		return (SUCCEED);
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	dep_obj = (obj_list *) malloc(sizeof (obj_list));
15447c478bd9Sstevel@tonic-gate 	(void) memset(dep_obj, 0, sizeof (obj_list));
15457c478bd9Sstevel@tonic-gate 	dep_obj->obj = (obj_com *) malloc(sizeof (obj_com));
15467c478bd9Sstevel@tonic-gate 	(void) memset(dep_obj->obj, 0, sizeof (obj_com));
15477c478bd9Sstevel@tonic-gate 	dep_obj->next = NULL;
15487c478bd9Sstevel@tonic-gate 	dep_obj->obj->filename = dep_file;
15497c478bd9Sstevel@tonic-gate 	dep_obj->obj->ename = dep_obj->obj->filename;
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	if (obj_elfcheck(dep_obj) == FAIL)
15527c478bd9Sstevel@tonic-gate 		return (FAIL);
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	if (dep_obj->obj->dsym_names != NULL)
15557c478bd9Sstevel@tonic-gate 		bind_symbols(dep_obj,
15567c478bd9Sstevel@tonic-gate 		    dep_obj->obj->dsym_tab,
15577c478bd9Sstevel@tonic-gate 		    dep_obj->obj->dsym_num,
15587c478bd9Sstevel@tonic-gate 		    dep_obj->obj->dsym_names);
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	if (dep_obj->obj->sym_names != NULL)
15617c478bd9Sstevel@tonic-gate 		bind_symbols(dep_obj,
15627c478bd9Sstevel@tonic-gate 		    dep_obj->obj->sym_tab,
15637c478bd9Sstevel@tonic-gate 		    dep_obj->obj->sym_num,
15647c478bd9Sstevel@tonic-gate 		    dep_obj->obj->sym_names);
15657c478bd9Sstevel@tonic-gate 	return (SUCCEED);
15667c478bd9Sstevel@tonic-gate }
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate /* ========= analyze_main =============================================== */
15697c478bd9Sstevel@tonic-gate /*
15707c478bd9Sstevel@tonic-gate  * DESCRIPTION:
15717c478bd9Sstevel@tonic-gate  * Read in an object file and analyze it.
15727c478bd9Sstevel@tonic-gate  */
15737c478bd9Sstevel@tonic-gate /* ====================================================================== */
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate static void
analyze_main(obj_list * c)15767c478bd9Sstevel@tonic-gate analyze_main(obj_list * c)
15777c478bd9Sstevel@tonic-gate {
15787c478bd9Sstevel@tonic-gate 	int	i;
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	if (obj_elfcheck(c) == FAIL)
15817c478bd9Sstevel@tonic-gate 		exit(1);
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	aflag = FALSE;
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	if (c->obj->sym_names != NULL)
15867c478bd9Sstevel@tonic-gate 		scan_symbols(c,
15877c478bd9Sstevel@tonic-gate 		    c->obj->sym_tab,
15887c478bd9Sstevel@tonic-gate 		    c->obj->sym_num,
15897c478bd9Sstevel@tonic-gate 		    c->obj->sym_names);
15907c478bd9Sstevel@tonic-gate 	else if (c->obj->dsym_names != NULL)
15917c478bd9Sstevel@tonic-gate 		scan_symbols(c,
15927c478bd9Sstevel@tonic-gate 		    c->obj->dsym_tab,
15937c478bd9Sstevel@tonic-gate 		    c->obj->dsym_num,
15947c478bd9Sstevel@tonic-gate 		    c->obj->dsym_names);
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	if (c->obj->numfiles == 0)
15977c478bd9Sstevel@tonic-gate 		return;
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	for (i = 0; i < c->obj->numfiles; i++)
16007c478bd9Sstevel@tonic-gate 		(void) analyze_dependency(c->obj->filenames[i]);
16017c478bd9Sstevel@tonic-gate }
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate /* ========= analyze_args ================================================= */
16047c478bd9Sstevel@tonic-gate /*
16057c478bd9Sstevel@tonic-gate  * DESCRIPTION:
16067c478bd9Sstevel@tonic-gate  * Analyze the command-line options.
16077c478bd9Sstevel@tonic-gate  */
16087c478bd9Sstevel@tonic-gate /* ======================================================================== */
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate static int
analyze_args(obj_list * c,int argc,char * argv[])16117c478bd9Sstevel@tonic-gate analyze_args(obj_list * c,
16127c478bd9Sstevel@tonic-gate     int argc,
16137c478bd9Sstevel@tonic-gate     char *argv[])
16147c478bd9Sstevel@tonic-gate {
16157c478bd9Sstevel@tonic-gate 	extern char	*optarg;
16167c478bd9Sstevel@tonic-gate 	extern int	optind;
16177c478bd9Sstevel@tonic-gate 	int		option;
16187c478bd9Sstevel@tonic-gate 	int		i;
16197c478bd9Sstevel@tonic-gate 	char		*nameptr;
16207c478bd9Sstevel@tonic-gate 	char		slash = '/';
16217c478bd9Sstevel@tonic-gate 	int		errflg = 0;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	if ((nameptr = strrchr(argv[0], slash)) != NULL)
16247c478bd9Sstevel@tonic-gate 		nameptr++;
16257c478bd9Sstevel@tonic-gate 	else
16267c478bd9Sstevel@tonic-gate 		nameptr = argv[0];
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	while ((option = getopt(argc, argv, "pso:a")) != EOF) {
16297c478bd9Sstevel@tonic-gate 		switch (option) {
16307c478bd9Sstevel@tonic-gate 		case 'p':	/* just do profiling; write to stdout */
16317c478bd9Sstevel@tonic-gate 			pflag = 1;
16327c478bd9Sstevel@tonic-gate 			break;
16337c478bd9Sstevel@tonic-gate 		case 's':	/* silent mode to turn off stderr messages */
16347c478bd9Sstevel@tonic-gate 			sflag = 0;
16357c478bd9Sstevel@tonic-gate 			break;
16367c478bd9Sstevel@tonic-gate 		case 'o':	/* redirects the output */
16377c478bd9Sstevel@tonic-gate 			outputfile = optarg;
16387c478bd9Sstevel@tonic-gate 			oflag = 1;
16397c478bd9Sstevel@tonic-gate 			break;
16407c478bd9Sstevel@tonic-gate 		case 'a':	/* processes archive as input */
16417c478bd9Sstevel@tonic-gate 			aflag = 1;
16427c478bd9Sstevel@tonic-gate 			break;
16437c478bd9Sstevel@tonic-gate 		case '?':
16447c478bd9Sstevel@tonic-gate 		default:
16457c478bd9Sstevel@tonic-gate 			errflg++;
16467c478bd9Sstevel@tonic-gate 		}		/* end switch */
16477c478bd9Sstevel@tonic-gate 	}			/* end while */
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	/* exit if there are no files to process */
16507c478bd9Sstevel@tonic-gate 	if (optind >= argc)
16517c478bd9Sstevel@tonic-gate 		errflg++;
16527c478bd9Sstevel@tonic-gate 	if (errflg) {
16537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
16547c478bd9Sstevel@tonic-gate 		    "usage: %s [-p] [-s] [-o outputfile] ", nameptr);
16557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
16567c478bd9Sstevel@tonic-gate 		    "<archive>|<binary_executable>\n");
16577c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
16587c478bd9Sstevel@tonic-gate 		    "\t\t   [<archive>|<dynamic library>...]\n");
16597c478bd9Sstevel@tonic-gate 		return (FALSE);
16607c478bd9Sstevel@tonic-gate 	}			/* end if */
16617c478bd9Sstevel@tonic-gate 	c->obj->filename = argv[optind++];
16627c478bd9Sstevel@tonic-gate 	c->obj->ename = c->obj->filename;
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	/* compute number of files and save their pointers */
16657c478bd9Sstevel@tonic-gate 	c->obj->numfiles = argc - optind;
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	if (c->obj->numfiles > 0) {
16687c478bd9Sstevel@tonic-gate 		i = 0;
16697c478bd9Sstevel@tonic-gate 		c->obj->filenames = (char **)
1670149775e4SDan McDonald 		    malloc(sizeof (char *) * (c->obj->numfiles + 1));
16717c478bd9Sstevel@tonic-gate 		for (; optind < argc; i++, optind++)
16727c478bd9Sstevel@tonic-gate 			c->obj->filenames[i] = argv[optind];
16737c478bd9Sstevel@tonic-gate 	}
16747c478bd9Sstevel@tonic-gate 	return (TRUE);
16757c478bd9Sstevel@tonic-gate }
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate /* ======================================================================= */
16787c478bd9Sstevel@tonic-gate /*
16797c478bd9Sstevel@tonic-gate  * Here starts the main ()
16807c478bd9Sstevel@tonic-gate  */
16817c478bd9Sstevel@tonic-gate /* ======================================================================= */
16827c478bd9Sstevel@tonic-gate 
1683f07a2a2eScraigm int
main(int argc,char * argv[])1684f07a2a2eScraigm main(int argc, char *argv[])
16857c478bd9Sstevel@tonic-gate {
16867c478bd9Sstevel@tonic-gate 	obj_list	*main_obj;
16877c478bd9Sstevel@tonic-gate 	dt_list		*q;
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	main_obj = (obj_list *) malloc(sizeof (obj_list));
16907c478bd9Sstevel@tonic-gate 	(void) memset(main_obj, 0, sizeof (obj_list));
16917c478bd9Sstevel@tonic-gate 	main_obj->obj = (obj_com *) malloc(sizeof (obj_com));
16927c478bd9Sstevel@tonic-gate 	(void) memset(main_obj->obj, 0, sizeof (obj_com));
16937c478bd9Sstevel@tonic-gate 	main_obj->next = NULL;
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 	if (!analyze_args(main_obj, argc, argv))
16967c478bd9Sstevel@tonic-gate 		exit(1);
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	if (oflag && pflag) {
16997c478bd9Sstevel@tonic-gate 		if ((OUTPUT_FD = fopen(outputfile, "w")) == NULL) {
17007c478bd9Sstevel@tonic-gate 			if (sflag)
17017c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
17027c478bd9Sstevel@tonic-gate 				    "\nfopen failed to open <%s>...\n\n",
17037c478bd9Sstevel@tonic-gate 				    outputfile);
17047c478bd9Sstevel@tonic-gate 			exit(1);
17057c478bd9Sstevel@tonic-gate 		}
17067c478bd9Sstevel@tonic-gate 	}
17077c478bd9Sstevel@tonic-gate 	/* generates profile report if pflag is set */
17087c478bd9Sstevel@tonic-gate 	if (pflag)
17097c478bd9Sstevel@tonic-gate 		(void) fprintf(OUTPUT_FD,
17107c478bd9Sstevel@tonic-gate 		    "#generated by %s\n",
17117c478bd9Sstevel@tonic-gate 		    argv[0]);
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	/* analyze the input file */
17147c478bd9Sstevel@tonic-gate 	analyze_main(main_obj);
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	/* generates profile report */
17177c478bd9Sstevel@tonic-gate 	if (!pflag)
17187c478bd9Sstevel@tonic-gate 		output_binding(argv[0], main_obj->obj->ename);
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	/* close the library .so file descriptor and ELF descriptor */
17217c478bd9Sstevel@tonic-gate 	obj_finis(main_obj);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	/* de-allocates the dt_needed link list */
17247c478bd9Sstevel@tonic-gate 	if (dt_needed) {
17257c478bd9Sstevel@tonic-gate 		while (dt_needed) {
17267c478bd9Sstevel@tonic-gate 			q = dt_needed;
17277c478bd9Sstevel@tonic-gate 			dt_needed = dt_needed->next;
17287c478bd9Sstevel@tonic-gate 			free(q);
17297c478bd9Sstevel@tonic-gate 		}
17307c478bd9Sstevel@tonic-gate 	}
17317c478bd9Sstevel@tonic-gate 	/* close the output redirect file descriptor */
17327c478bd9Sstevel@tonic-gate 	if (oflag)
17337c478bd9Sstevel@tonic-gate 		(void) fclose(OUTPUT_FD);
17347c478bd9Sstevel@tonic-gate 
1735f07a2a2eScraigm 	return (0);
17367c478bd9Sstevel@tonic-gate }
1737