xref: /titanic_52/usr/src/cmd/sgs/prof/common/symintLoad.c (revision b7d62af5b42f0da2eb668e8d33d24d2f4fdd98a8)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1988 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 /*
34  *	File: symintLoad.c
35  *	Date: 12/15/88
36  *
37  *	This file provides code to build the profiling symbol array
38  *	(array of PROF_SYMBOL).  This array contains all of the
39  *	symbol table information plus selected debug information for
40  *	each file and each function that has a coverage array.
41  *
42  *	The symbol table contains entries for every file, every
43  *	function, and every coverage array.  The debug information
44  *	has corresponding entries except that there are no entries
45  *	for the coverage arrays.  (This may change later.)
46  *
47  *	The algorithm for building the profiling symbol array
48  *	consists of scanning the symbol table for file, function,
49  *	and coverage array entries and building an entry for each.
50  *	The construction of an entry is constrained by the
51  *	following factors:
52  *
53  *		- An entry is built for every file.
54  *
55  *		- An entry is built for a function only if there
56  *		is a corresponding coverage array for the function.
57  *
58  *		- Entries must be ordered in the sense that each
59  *		non-file entry points to its owner file and each
60  *		file entry points to the next file (or null).
61  *
62  *		- The assembler specification (see C Issue 5 3B2
63  *		Assembler System Test Specification by Howe, p. 28)
64  *		states that all local symbols follow their file
65  *		symbol in the symbol table.  This allows us to relate
66  *		a function and its coverage array to the file that
67  *		contains it.
68  *
69  *		- For each symbol included in the profiling symbol
70  *		array, all corresponding symbol table information must
71  *		be present together with selected debug information.
72  *		Therefore, the correspondence between a symbol table
73  *		entry and a debug entry must be established.
74  *
75  *		- Although duplicate (static) function names may appear,
76  *		the names are unique within a given file.  Also, the
77  *		value (address) of each function is included in both
78  *		the symbol table information and the debug information.
79  *		This provides a verifable correspondence between these
80  *		information sets.
81  *
82  */
83 
84 #include "string.h"
85 #include "symint.h"
86 #include "debug.h"
87 
88 static PROF_FILE	*profPtr;
89 
90 /* LINTED: set but not used */
91 static int	prstsym_size;	/* size of a symbol table symbol */
92 
93 static PROF_SYMBOL	*prsym_list_p = 0;	/* the list to return. */
94 
95 /*
96  * _symintLoad(proffilePtr)
97  * proffilePtr	- PROF_FILE pointer returned by _symintOpen().
98  *
99  * returns PROF_SYMBOL * - pointer to the malloc-ed array of
100  *			   symbol information entries, or
101  *			   NULL if fails.
102  *
103  *
104  * This routine builds the interface data structure from the data
105  * already loaded during _symintOpen().
106  *
107  * Prof:
108  *
109  * 	1. Allocate a duplicate copy of the symbol table
110  *	   data.  (For Prof, a PROF_SYMBOL is just
111  *	   a structure containing an Elf32_Sym!)
112  *
113  * 	2. Set internal parameters to reflect this.
114  *
115  *
116  * Problems are dealt with by issuing an _err_exit().
117  *
118  */
119 PROF_SYMBOL *
120 _symintLoad(PROF_FILE *proffilePtr)
121 {
122 	Elf_Data	*symdat_p;
123 
124 	DEBUG_LOC("_symintLoad: top");
125 
126 	profPtr = proffilePtr;
127 
128 	/*
129 	 * sanity checks.
130 	 */
131 	DEBUG_EXP(printf("profPtr = %x\n", profPtr));
132 	DEBUG_EXP(printf("profPtr->pf_symdat_p = %x\n", profPtr->pf_symdat_p));
133 	DEBUG_EXP(printf("profPtr->pf_nstsyms = %x\n", profPtr->pf_nstsyms));
134 
135 	assert(profPtr != 0);
136 	assert(profPtr->pf_symdat_p != 0);
137 	assert(profPtr->pf_nstsyms != 0);
138 
139 	symdat_p = profPtr->pf_symdat_p;
140 	DEBUG_EXP(printf("symdat_p->d_size = %x\n", symdat_p->d_size));
141 
142 	prstsym_size = (symdat_p->d_size / profPtr->pf_nstsyms);
143 	DEBUG_EXP(printf("_symintLoad: prstsym_size = %d\n",
144 	    prstsym_size));
145 
146 	/*
147 	 * alloc a new copy of the array, and
148 	 *  do a bit-wise copy since the structures
149 	 *  ARE THE SAME SIZE & (effectively) HAVE THE SAME FIELDS!
150 	 *  Set the descriptive `parameters' accordingly.
151 	 *
152 	 * (We'll take a copy, to simplify the 'Drop'
153 	 *  logic.)
154 	 */
155 
156 	{
157 	int st_size;	/* size of symbol table data */
158 
159 	st_size = symdat_p->d_size;
160 
161 	NO_DEBUG_LOC("_symintLoad: before malloc for symbol list (PROF)");
162 	prsym_list_p = (PROF_SYMBOL *)_Malloc(st_size, 1);
163 	NO_DEBUG_LOC("_symintLoad: after malloc for symbol list (PROF)");
164 
165 	NO_DEBUG_LOC("_symintLoad: before memcpy for symbol list (PROF)");
166 	(void) memcpy((char *)&(prsym_list_p->ps_sym), symdat_p->d_buf,
167 	    st_size);
168 
169 	profPtr->pf_nsyms = profPtr->pf_nstsyms;
170 	}
171 
172 	DEBUG_LOC("_symintLoad: bottom");
173 	return (prsym_list_p);
174 }
175