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
57a5d89c4Sab  * Common Development and Distribution License (the "License").
67a5d89c4Sab  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2292ed1782Smike_s /*
23*1dd08564Sab  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2492ed1782Smike_s  * Use is subject to license terms.
2592ed1782Smike_s  */
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate /*
3392ed1782Smike_s  *	File: symintLoad.c
3492ed1782Smike_s  *	Date: 12/15/88
3592ed1782Smike_s  *
3692ed1782Smike_s  *	This file provides code to build the profiling symbol array
3792ed1782Smike_s  *	(array of PROF_SYMBOL).  This array contains all of the
3892ed1782Smike_s  *	symbol table information plus selected debug information for
3992ed1782Smike_s  *	each file and each function that has a coverage array.
4092ed1782Smike_s  *
4192ed1782Smike_s  *	The symbol table contains entries for every file, every
4292ed1782Smike_s  *	function, and every coverage array.  The debug information
4392ed1782Smike_s  *	has corresponding entries except that there are no entries
4492ed1782Smike_s  *	for the coverage arrays.  (This may change later.)
4592ed1782Smike_s  *
4692ed1782Smike_s  *	The algorithm for building the profiling symbol array
4792ed1782Smike_s  *	consists of scanning the symbol table for file, function,
4892ed1782Smike_s  *	and coverage array entries and building an entry for each.
4992ed1782Smike_s  *	The construction of an entry is constrained by the
5092ed1782Smike_s  *	following factors:
5192ed1782Smike_s  *
5292ed1782Smike_s  *		- An entry is built for every file.
5392ed1782Smike_s  *
5492ed1782Smike_s  *		- An entry is built for a function only if there
5592ed1782Smike_s  *		is a corresponding coverage array for the function.
5692ed1782Smike_s  *
5792ed1782Smike_s  *		- Entries must be ordered in the sense that each
5892ed1782Smike_s  *		non-file entry points to its owner file and each
5992ed1782Smike_s  *		file entry points to the next file (or null).
6092ed1782Smike_s  *
6192ed1782Smike_s  *		- The assembler specification (see C Issue 5 3B2
6292ed1782Smike_s  *		Assembler System Test Specification by Howe, p. 28)
6392ed1782Smike_s  *		states that all local symbols follow their file
6492ed1782Smike_s  *		symbol in the symbol table.  This allows us to relate
6592ed1782Smike_s  *		a function and its coverage array to the file that
6692ed1782Smike_s  *		contains it.
6792ed1782Smike_s  *
6892ed1782Smike_s  *		- For each symbol included in the profiling symbol
6992ed1782Smike_s  *		array, all corresponding symbol table information must
7092ed1782Smike_s  *		be present together with selected debug information.
7192ed1782Smike_s  *		Therefore, the correspondence between a symbol table
7292ed1782Smike_s  *		entry and a debug entry must be established.
7392ed1782Smike_s  *
7492ed1782Smike_s  *		- Although duplicate (static) function names may appear,
7592ed1782Smike_s  *		the names are unique within a given file.  Also, the
7692ed1782Smike_s  *		value (address) of each function is included in both
7792ed1782Smike_s  *		the symbol table information and the debug information.
7892ed1782Smike_s  *		This provides a verifable correspondence between these
7992ed1782Smike_s  *		information sets.
8092ed1782Smike_s  *
8192ed1782Smike_s  */
837c478bd9Sstevel@tonic-gate #include "string.h"
847c478bd9Sstevel@tonic-gate #include "symint.h"
857c478bd9Sstevel@tonic-gate #include "debug.h"
877c478bd9Sstevel@tonic-gate static PROF_FILE	*profPtr;
8992ed1782Smike_s /* LINTED: set but not used */
9092ed1782Smike_s static int	prstsym_size;	/* size of a symbol table symbol */
927c478bd9Sstevel@tonic-gate static PROF_SYMBOL	*prsym_list_p = 0;	/* the list to return. */
9492ed1782Smike_s /*
957c478bd9Sstevel@tonic-gate  * _symintLoad(proffilePtr)
967c478bd9Sstevel@tonic-gate  * proffilePtr	- PROF_FILE pointer returned by _symintOpen().
9792ed1782Smike_s  *
987c478bd9Sstevel@tonic-gate  * returns PROF_SYMBOL * - pointer to the malloc-ed array of
9992ed1782Smike_s  *			   symbol information entries, or
10092ed1782Smike_s  *			   NULL if fails.
10192ed1782Smike_s  *
10292ed1782Smike_s  *
1037c478bd9Sstevel@tonic-gate  * This routine builds the interface data structure from the data
1047c478bd9Sstevel@tonic-gate  * already loaded during _symintOpen().
10592ed1782Smike_s  *
1067c478bd9Sstevel@tonic-gate  * Prof:
10792ed1782Smike_s  *
1087c478bd9Sstevel@tonic-gate  * 	1. Allocate a duplicate copy of the symbol table
10992ed1782Smike_s  *	   data.  (For Prof, a PROF_SYMBOL is just
11092ed1782Smike_s  *	   a structure containing an Elf32_Sym!)
11192ed1782Smike_s  *
1127c478bd9Sstevel@tonic-gate  * 	2. Set internal parameters to reflect this.
11392ed1782Smike_s  *
11492ed1782Smike_s  *
1157c478bd9Sstevel@tonic-gate  * Problems are dealt with by issuing an _err_exit().
11692ed1782Smike_s  *
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate PROF_SYMBOL *
_symintLoad(PROF_FILE * proffilePtr)11992ed1782Smike_s _symintLoad(PROF_FILE *proffilePtr)
1207c478bd9Sstevel@tonic-gate {
1217a5d89c4Sab 	Elf_Data	*symdat_pri_p;
1227a5d89c4Sab 	Elf_Data	*symdat_aux_p;
1237a5d89c4Sab 	PROF_SYMBOL	*symlist;
1257c478bd9Sstevel@tonic-gate 	DEBUG_LOC("_symintLoad: top");
1277c478bd9Sstevel@tonic-gate 	profPtr = proffilePtr;
12992ed1782Smike_s 	/*
1307c478bd9Sstevel@tonic-gate 	 * sanity checks.
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	DEBUG_EXP(printf("profPtr = %x\n", profPtr));
1337a5d89c4Sab 	DEBUG_EXP(printf("profPtr->pf_symdat_p = %x\n",
1347a5d89c4Sab 	    profPtr->pf_symdat_pri_p));
1357c478bd9Sstevel@tonic-gate 	DEBUG_EXP(printf("profPtr->pf_nstsyms = %x\n", profPtr->pf_nstsyms));
13792ed1782Smike_s 	assert(profPtr != 0);
1387a5d89c4Sab 	assert(profPtr->pf_symdat_pri_p != 0);
13992ed1782Smike_s 	assert(profPtr->pf_nstsyms != 0);
1417a5d89c4Sab 	symdat_pri_p = profPtr->pf_symdat_pri_p;
1427a5d89c4Sab 	symdat_aux_p = profPtr->pf_symdat_aux_p;
1437a5d89c4Sab 	DEBUG_EXP(printf("symdat_pri_p->d_size = %x\n", symdat_pri_p->d_size));
1457a5d89c4Sab 	prstsym_size = (symdat_pri_p->d_size / profPtr->pf_nstsyms);
14692ed1782Smike_s 	DEBUG_EXP(printf("_symintLoad: prstsym_size = %d\n",
14792ed1782Smike_s 	    prstsym_size));
14992ed1782Smike_s 	/*
1507c478bd9Sstevel@tonic-gate 	 * alloc a new copy of the array, and
1517c478bd9Sstevel@tonic-gate 	 *  do a bit-wise copy since the structures
1527c478bd9Sstevel@tonic-gate 	 *  ARE THE SAME SIZE & (effectively) HAVE THE SAME FIELDS!
1537c478bd9Sstevel@tonic-gate 	 *  Set the descriptive `parameters' accordingly.
15492ed1782Smike_s 	 *
1557a5d89c4Sab 	 * If there is an auxiliary symbol table (.SUNW_ldynsym) augmenting
1567a5d89c4Sab 	 * the dynamic symbol table (.dynsym), then we copy both tables
1577a5d89c4Sab 	 * into our copy, with the auxiliary coming first.
1587a5d89c4Sab 	 *
1597a5d89c4Sab 	 * (We'll take a copy, to simplify the 'Drop' logic.)
1607c478bd9Sstevel@tonic-gate 	 */
1627c478bd9Sstevel@tonic-gate 	{
1637a5d89c4Sab 	size_t st_size;	/* size of symbol table data */
1657a5d89c4Sab 	st_size = symdat_pri_p->d_size;
1667a5d89c4Sab 	if (profPtr->pf_nstsyms_aux != 0)
1677a5d89c4Sab 		st_size += symdat_aux_p->d_size;
1697c478bd9Sstevel@tonic-gate 	NO_DEBUG_LOC("_symintLoad: before malloc for symbol list (PROF)");
1707a5d89c4Sab 	prsym_list_p = symlist = (PROF_SYMBOL *)_Malloc(st_size, 1);
1717c478bd9Sstevel@tonic-gate 	NO_DEBUG_LOC("_symintLoad: after malloc for symbol list (PROF)");
1737a5d89c4Sab 	if (profPtr->pf_nstsyms_aux > 0) {
1747a5d89c4Sab 		NO_DEBUG_LOC("_symintLoad: before memcpy for "
1757a5d89c4Sab 		    "auxiliary symbol list (PROF)");
1767a5d89c4Sab 		(void) memcpy(symlist, symdat_aux_p->d_buf,
1777a5d89c4Sab 		    symdat_aux_p->d_size);
1787a5d89c4Sab 		symlist += profPtr->pf_nstsyms_aux;
1797a5d89c4Sab 	}
1817c478bd9Sstevel@tonic-gate 	NO_DEBUG_LOC("_symintLoad: before memcpy for symbol list (PROF)");
1827a5d89c4Sab 	(void) memcpy(symlist, symdat_pri_p->d_buf, symdat_pri_p->d_size);
1847c478bd9Sstevel@tonic-gate 	profPtr->pf_nsyms = profPtr->pf_nstsyms;
1857c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 	DEBUG_LOC("_symintLoad: bottom");
18892ed1782Smike_s 	return (prsym_list_p);
1897c478bd9Sstevel@tonic-gate }