xref: /titanic_44/usr/src/cmd/sgs/gprof/common/lookup.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1990-1998,2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "gprof.h"
30 
31 /*
32  * look up an address in a sorted-by-address namelist
33  * this deals with misses by mapping them to the next lower
34  * entry point.
35  */
36 static   int	searchmsg = 0; /* Emit the diagnostic only once */
37 
38 nltype *
39 nllookup(mod_info_t *module, pctype address, pctype *nxtsym)
40 {
41 	size_t	low = 0, middle, high = module->nname - 1;
42 	pctype	keyval;
43 	nltype	*mnl = module->nl;
44 
45 	/*
46 	 * If this is the program executable in which we are looking up
47 	 * a symbol, then the actual load address will be the same as the
48 	 * address specified in the ELF file. For shared objects, the
49 	 * load address may differ from what is specified in the file. In
50 	 * this case, we may need to look for a different value altogether.
51 	 */
52 	keyval = module->txt_origin + (address - module->load_base);
53 
54 	if (keyval < mnl[low].value) {
55 		if (nxtsym) {
56 			*nxtsym = module->load_base +
57 					(mnl[low].value - module->txt_origin);
58 		}
59 		return (NULL);
60 	}
61 
62 	if (keyval >= mnl[high].value) {
63 		if (nxtsym)
64 			*nxtsym = module->load_end;
65 		return (&mnl[high]);
66 	}
67 
68 	while (low != high) {
69 		middle = (high + low) >> 1;
70 
71 		if (mnl[middle].value <= keyval &&
72 					    mnl[middle + 1].value > keyval) {
73 			if (nxtsym) {
74 				*nxtsym = module->load_base +
75 						    (mnl[middle + 1].value -
76 						    module->txt_origin);
77 			}
78 			return (&mnl[middle]);
79 		}
80 
81 		if (mnl[middle].value > keyval) {
82 			high = middle;
83 		} else {
84 			low = middle + 1;
85 		}
86 	}
87 
88 	if (searchmsg++ == 0)
89 		fprintf(stderr, "[nllookup] binary search fails???\n");
90 
91 	/* must never reach here! */
92 	return (0);
93 }
94 
95 arctype *
96 arclookup(nltype *parentp, nltype *childp)
97 {
98 	arctype	*arcp;
99 
100 	if (parentp == 0 || childp == 0) {
101 		fprintf(stderr, "[arclookup] parentp == 0 || childp == 0\n");
102 		return (0);
103 	}
104 #ifdef DEBUG
105 	if (debug & LOOKUPDEBUG) {
106 		printf("[arclookup] parent %s child %s\n",
107 		    parentp->name, childp->name);
108 	}
109 #endif /* DEBUG */
110 
111 	for (arcp = parentp->children; arcp; arcp = arcp->arc_childlist) {
112 #ifdef DEBUG
113 		if (debug & LOOKUPDEBUG) {
114 			printf("[arclookup]\t arc_parent %s arc_child %s\n",
115 			    arcp->arc_parentp->name,
116 			    arcp->arc_childp->name);
117 		}
118 #endif /* DEBUG */
119 		if (arcp->arc_childp == childp) {
120 			return (arcp);
121 		}
122 	}
123 	return (0);
124 }
125