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 		}
334*07c94cbfSToomas 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)
1193149775e4SDan McDonald 	    + strlen(filename) + 2);
11947c478bd9Sstevel@tonic-gate 	(void *) strcpy(fullname, com->obj->ename);
11957c478bd9Sstevel@tonic-gate 	(void *) strcat(fullname, "(");
11967c478bd9Sstevel@tonic-gate 	(void *) strcat(fullname,