xref: /titanic_51/usr/src/cmd/sgs/gprof/common/lookup.c (revision 92ed17828163bd22e8650252460319b94ef9aa74)
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  */
22*92ed1782Smike_s 
237c478bd9Sstevel@tonic-gate /*
24*92ed1782Smike_s  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*92ed1782Smike_s  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "gprof.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * look up an address in a sorted-by-address namelist
347c478bd9Sstevel@tonic-gate  * this deals with misses by mapping them to the next lower
357c478bd9Sstevel@tonic-gate  * entry point.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate static   int	searchmsg = 0; /* Emit the diagnostic only once */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate nltype *
407c478bd9Sstevel@tonic-gate nllookup(mod_info_t *module, pctype address, pctype *nxtsym)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	size_t	low = 0, middle, high = module->nname - 1;
437c478bd9Sstevel@tonic-gate 	pctype	keyval;
447c478bd9Sstevel@tonic-gate 	nltype	*mnl = module->nl;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	/*
477c478bd9Sstevel@tonic-gate 	 * If this is the program executable in which we are looking up
487c478bd9Sstevel@tonic-gate 	 * a symbol, then the actual load address will be the same as the
497c478bd9Sstevel@tonic-gate 	 * address specified in the ELF file. For shared objects, the
507c478bd9Sstevel@tonic-gate 	 * load address may differ from what is specified in the file. In
517c478bd9Sstevel@tonic-gate 	 * this case, we may need to look for a different value altogether.
527c478bd9Sstevel@tonic-gate 	 */
537c478bd9Sstevel@tonic-gate 	keyval = module->txt_origin + (address - module->load_base);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	if (keyval < mnl[low].value) {
567c478bd9Sstevel@tonic-gate 		if (nxtsym) {
577c478bd9Sstevel@tonic-gate 			*nxtsym = module->load_base +
587c478bd9Sstevel@tonic-gate 					(mnl[low].value - module->txt_origin);
597c478bd9Sstevel@tonic-gate 		}
607c478bd9Sstevel@tonic-gate 		return (NULL);
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if (keyval >= mnl[high].value) {
647c478bd9Sstevel@tonic-gate 		if (nxtsym)
657c478bd9Sstevel@tonic-gate 			*nxtsym = module->load_end;
667c478bd9Sstevel@tonic-gate 		return (&mnl[high]);
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	while (low != high) {
707c478bd9Sstevel@tonic-gate 		middle = (high + low) >> 1;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 		if (mnl[middle].value <= keyval &&
737c478bd9Sstevel@tonic-gate 					    mnl[middle + 1].value > keyval) {
747c478bd9Sstevel@tonic-gate 			if (nxtsym) {
757c478bd9Sstevel@tonic-gate 				*nxtsym = module->load_base +
767c478bd9Sstevel@tonic-gate 						    (mnl[middle + 1].value -
777c478bd9Sstevel@tonic-gate 						    module->txt_origin);
787c478bd9Sstevel@tonic-gate 			}
797c478bd9Sstevel@tonic-gate 			return (&mnl[middle]);
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 		if (mnl[middle].value > keyval) {
837c478bd9Sstevel@tonic-gate 			high = middle;
847c478bd9Sstevel@tonic-gate 		} else {
857c478bd9Sstevel@tonic-gate 			low = middle + 1;
867c478bd9Sstevel@tonic-gate 		}
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (searchmsg++ == 0)
90*92ed1782Smike_s 		(void) fprintf(stderr, "[nllookup] binary search fails???\n");
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/* must never reach here! */
937c478bd9Sstevel@tonic-gate 	return (0);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate arctype *
977c478bd9Sstevel@tonic-gate arclookup(nltype *parentp, nltype *childp)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	arctype	*arcp;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (parentp == 0 || childp == 0) {
102*92ed1782Smike_s 		(void) fprintf(stderr,
103*92ed1782Smike_s 		    "[arclookup] parentp == 0 || childp == 0\n");
1047c478bd9Sstevel@tonic-gate 		return (0);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate #ifdef DEBUG
1077c478bd9Sstevel@tonic-gate 	if (debug & LOOKUPDEBUG) {
108*92ed1782Smike_s 		(void) printf("[arclookup] parent %s child %s\n",
1097c478bd9Sstevel@tonic-gate 		    parentp->name, childp->name);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	for (arcp = parentp->children; arcp; arcp = arcp->arc_childlist) {
1147c478bd9Sstevel@tonic-gate #ifdef DEBUG
1157c478bd9Sstevel@tonic-gate 		if (debug & LOOKUPDEBUG) {
116*92ed1782Smike_s 			(void) printf(
117*92ed1782Smike_s 			    "[arclookup]\t arc_parent %s arc_child %s\n",
1187c478bd9Sstevel@tonic-gate 			    arcp->arc_parentp->name,
1197c478bd9Sstevel@tonic-gate 			    arcp->arc_childp->name);
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1227c478bd9Sstevel@tonic-gate 		if (arcp->arc_childp == childp) {
1237c478bd9Sstevel@tonic-gate 			return (arcp);
1247c478bd9Sstevel@tonic-gate 		}
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 	return (0);
1277c478bd9Sstevel@tonic-gate }
128