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  	*/
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
3592ed1782Smike_s #include <string.h>
3692ed1782Smike_s #include "symint.h"
377c478bd9Sstevel@tonic-gate #include "debug.h"
3992ed1782Smike_s /*
407c478bd9Sstevel@tonic-gate  * symintFcns.c -- symbol information interface routines.
4192ed1782Smike_s  *
427c478bd9Sstevel@tonic-gate  * these routines form a symbol information access
437c478bd9Sstevel@tonic-gate  * interface, for the profilers to get at object file
447c478bd9Sstevel@tonic-gate  * information.  this interface was designed to aid
457c478bd9Sstevel@tonic-gate  * in the COFF to ELF conversion of prof, lprof and friends.
4692ed1782Smike_s  *
477c478bd9Sstevel@tonic-gate  */
5092ed1782Smike_s /*
517c478bd9Sstevel@tonic-gate  * _symintOpen(aout_name)
527c478bd9Sstevel@tonic-gate  * aout_name 	- char string file name of object file
5392ed1782Smike_s  *		to open.
5492ed1782Smike_s  *
557c478bd9Sstevel@tonic-gate  * returns PROF_FILE * - pointer to the PROF_FILE structure built,
5692ed1782Smike_s  *			or NULL if fails.
577c478bd9Sstevel@tonic-gate  */
5992ed1782Smike_s /*
6092ed1782Smike_s  *
617c478bd9Sstevel@tonic-gate  * .H 3 "Executable File Open and Close"
6292ed1782Smike_s  *
637c478bd9Sstevel@tonic-gate  * Under COFF, the routine ldopen, given a file name, returns a pointer to a
647c478bd9Sstevel@tonic-gate  * structure called an LDFILE.  This descriptor is then passed to each of
657c478bd9Sstevel@tonic-gate  * the library routines (such as read header, read symbol table entry, etc)
667c478bd9Sstevel@tonic-gate  * to access the information contained in the file.  These calls are spread
677c478bd9Sstevel@tonic-gate  * throughout the profiling code.
6892ed1782Smike_s  *
697c478bd9Sstevel@tonic-gate  * Under ELF, the file must be opened using a system open call.  The file
707c478bd9Sstevel@tonic-gate  * descriptor is then passed to a routine which returns a pointer to an
717c478bd9Sstevel@tonic-gate  * Elf structure.  This pointer is then passed along to another routine which
727c478bd9Sstevel@tonic-gate  * returns a different pointer which is in turn passed along to another
737c478bd9Sstevel@tonic-gate  * routine.  In an attempt to avoid disturbing the current format of the
747c478bd9Sstevel@tonic-gate  * code (by having to pass around different types of pointers), we plan to
757c478bd9Sstevel@tonic-gate  * build a PROF_FILE descriptor which will then be passed around in the
767c478bd9Sstevel@tonic-gate  * same way as the pointer to LDFILE.
7792ed1782Smike_s  *
787c478bd9Sstevel@tonic-gate  * Thus, for ELF, an open will consist of opening the file and extracting
797c478bd9Sstevel@tonic-gate  * enough from it to fill in the PROF_FILE structure.  The code for the
807c478bd9Sstevel@tonic-gate  * open is as follows; the code for building the symbol table (extracting
817c478bd9Sstevel@tonic-gate  * information from the sections to fill an array of PROF_SYMBOLS) has
827c478bd9Sstevel@tonic-gate  * yet to be written.
8392ed1782Smike_s  *
847c478bd9Sstevel@tonic-gate  */
8692ed1782Smike_s /*
877a5d89c4Sab  * globals
887c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate static char
927c478bd9Sstevel@tonic-gate 	*fail_open_s =	"Unable to open file",
937c478bd9Sstevel@tonic-gate 	*fail_begin_s =	"Unable to read (begin) file",
947c478bd9Sstevel@tonic-gate 	*fail_ehdr_s =	"Unable to get elf header in",
957c478bd9Sstevel@tonic-gate 	*fail_sec_s =	"Unable to get section",
967c478bd9Sstevel@tonic-gate 	*fail_shd_s =	"Unable to get header for section",
977c478bd9Sstevel@tonic-gate 	*fail_dat_s =	"Unable to get data for section",
987c478bd9Sstevel@tonic-gate 	*fail_sym_s =	"Cannot find symbol table section in",
997c478bd9Sstevel@tonic-gate 	*fail_pfsym_s =	"Unable to process symbols in",
1007a5d89c4Sab 	*fail_buf_s =	"Data buffer is null for section",
1017a5d89c4Sab 	*fail_sym32_s =	"Cannot handle more than 2^32 symbols"
1027c478bd9Sstevel@tonic-gate 	;
10592ed1782Smike_s /*
1067c478bd9Sstevel@tonic-gate  * this points at the name of the executable.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate static  char *executableName;
11292ed1782Smike_s /*
1137c478bd9Sstevel@tonic-gate  * section_data_p() - return ptr to section data,
1147c478bd9Sstevel@tonic-gate  * 	given section ptr and name of section.
1157c478bd9Sstevel@tonic-gate  */
11792ed1782Smike_s static Elf_Data *
section_data_p(Elf_Scn * sec_p,char * str)11892ed1782Smike_s section_data_p(Elf_Scn *sec_p, char *str)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	Elf_Data *dat_p;
1227c478bd9Sstevel@tonic-gate 	if ((dat_p = elf_getdata(sec_p, NULL)) == NULL)
1237c478bd9Sstevel@tonic-gate 		_err_exit("%s %s in %s.", fail_dat_s, str, executableName);
12492ed1782Smike_s 	return (dat_p);
1257c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate PROF_FILE *
_symintOpen(char * aout_name)12992ed1782Smike_s _symintOpen(char *aout_name)
13092ed1782Smike_s {
1317c478bd9Sstevel@tonic-gate /*
13292ed1782Smike_s  * Elf file open operation
13392ed1782Smike_s  *
13492ed1782Smike_s  * - point at executable's name, globally
13592ed1782Smike_s  * - open file
13692ed1782Smike_s  * - align to current version
13792ed1782Smike_s  * - read the elf descriptor and header
13892ed1782Smike_s  * - read header-names section descriptor, header, and data
13992ed1782Smike_s  * - allocate space for all the section hdrs (pf_shdarr_p).
14092ed1782Smike_s  * - set a pointer to the header-names buffer
14192ed1782Smike_s  * - search the section headers for
14292ed1782Smike_s  *	- debug section header and data
14392ed1782Smike_s  *	- line section header and data
14492ed1782Smike_s  *	- symbol table header, data, strings, and number of symbols
14592ed1782Smike_s  *	  and copy each section hdr into our array.
14692ed1782Smike_s  *  - populate the PROF_SYMBOL array and anchor it in (pf_symarr_p).
14792ed1782Smike_s  */
1497c478bd9Sstevel@tonic-gate 	PROF_FILE	*pfile_p;	/* PROF_FILE ptr to return. */
1517c478bd9Sstevel@tonic-gate 	Elf		*telf_p;
1527c478bd9Sstevel@tonic-gate 	Elf_Scn		*tscn_p;
1537c478bd9Sstevel@tonic-gate 	Elf32_Shdr	*tshd_p;
1547c478bd9Sstevel@tonic-gate 	int		k;
1557a5d89c4Sab 	Elf64_Xword	nsyms_pri = 0, nsyms_aux = 0;
1577c478bd9Sstevel@tonic-gate 	executableName = aout_name;
1597c478bd9Sstevel@tonic-gate 	DEBUG_LOC("_symintOpen: top");
16092ed1782Smike_s 	if (aout_name == NULL) {
1617c478bd9Sstevel@tonic-gate 		_err_exit("name of executable is null\n");
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	DEBUG_EXP(printf("Attempting to open %s\n", aout_name));
16492ed1782Smike_s 	pfile_p = _Malloc(sizeof (PROF_FILE), 1);
1667c478bd9Sstevel@tonic-gate 	if ((pfile_p->pf_fildes = open(aout_name, O_RDONLY)) == -1)
1677c478bd9Sstevel@tonic-gate 		_err_exit("%s %s.", fail_open_s, aout_name);
1687c478bd9Sstevel@tonic-gate 	if ((elf_version(EV_CURRENT)) == EV_NONE)
1697c478bd9Sstevel@tonic-gate 		_err_exit("Elf library out of date");
17092ed1782Smike_s 	if ((pfile_p->pf_elf_p = elf_begin(pfile_p->pf_fildes,
17192ed1782Smike_s 	    ELF_C_READ, (Elf *)NULL)) == NULL)
1727c478bd9Sstevel@tonic-gate 		_err_exit("%s %s.", fail_begin_s, aout_name);
1747c478bd9Sstevel@tonic-gate 	DEBUG_EXP(printf("elfkind = %d\n", elf_kind(pfile_p->pf_elf_p)));
1757c478bd9Sstevel@tonic-gate 	if ((pfile_p->pf_elfhd_p = elf32_getehdr(pfile_p->pf_elf_p)) == NULL)
1767c478bd9Sstevel@tonic-gate 		_err_exit("%s %s.", fail_ehdr_s, aout_name);
1787c478bd9Sstevel@tonic-gate 	DEBUG_LOC("_symintOpen: after call to getehdr");
1797c478bd9Sstevel@tonic-gate 	telf_p = pfile_p->pf_elf_p;
1817c478bd9Sstevel@tonic-gate 	tscn_p = elf_getscn(telf_p, k = pfile_p->pf_elfhd_p->e_shstrndx);
1827c478bd9Sstevel@tonic-gate 	if (tscn_p == NULL)
1837c478bd9Sstevel@tonic-gate 		_err_exit("%s %d in %s.", fail_sec_s, k, aout_name);
1857a5d89c4Sab 	if (elf32_getshdr(tscn_p) == NULL)
18692ed1782Smike_s 		_err_exit("%s %s in %s.", fail_shd_s, "header names",
187*1dd08564Sab 		    aout_name);
1887c478bd9Sstevel@tonic-gate 	if ((pfile_p->pf_snmdat_p = elf_getdata(tscn_p, NULL)) == NULL)
18992ed1782Smike_s 		_err_exit("%s %s in %s.", fail_dat_s, "header names",
190*1dd08564Sab 		    aout_name);
19292ed1782Smike_s 	DEBUG_EXP(printf("Address of data header = 0x%lx\n",
19392ed1782Smike_s 	    pfile_p->pf_snmdat_p));
19492ed1782Smike_s 	DEBUG_EXP(printf("d_buf     = 0x%lx\n",
19592ed1782Smike_s 	    pfile_p->pf_snmdat_p->d_buf));
19692ed1782Smike_s 	DEBUG_EXP(printf("d_type    = %d\n",
19792ed1782Smike_s 	    pfile_p->pf_snmdat_p->d_type));
19892ed1782Smike_s 	DEBUG_EXP(printf("d_size    = %d\n",
19992ed1782Smike_s 	    pfile_p->pf_snmdat_p->d_size));
20092ed1782Smike_s 	DEBUG_EXP(printf("d_off     = %d\n",
20192ed1782Smike_s 	    pfile_p->pf_snmdat_p->d_off));
20292ed1782Smike_s 	DEBUG_EXP(printf("d_align   = %d\n",
20392ed1782Smike_s 	    pfile_p->pf_snmdat_p->d_align));
20492ed1782Smike_s 	DEBUG_EXP(printf("d_version = %d\n",
20592ed1782Smike_s 	    pfile_p->pf_snmdat_p->d_version));
2077c478bd9Sstevel@tonic-gate 	if (pfile_p->pf_snmdat_p->d_buf == NULL)
20892ed1782Smike_s 		_err_exit("%s %s in %s.", fail_buf_s, "header names",
20992ed1782Smike_s 		    aout_name);
2117c478bd9Sstevel@tonic-gate 	DEBUG_LOC("_symintOpen: after call to getdata (for header names)");
21392ed1782Smike_s 	pfile_p->pf_shdarr_p = _Malloc(pfile_p->pf_elfhd_p->e_shentsize,
21492ed1782Smike_s 	    pfile_p->pf_elfhd_p->e_shnum);
2167c478bd9Sstevel@tonic-gate 	{
217*1dd08564Sab #ifdef DEBUG
21892ed1782Smike_s 	char	*shdnms_p = (char *)pfile_p->pf_snmdat_p->d_buf;
219*1dd08564Sab #endif
22192ed1782Smike_s 	char	*dest_p = (char *)pfile_p->pf_shdarr_p;
22292ed1782Smike_s 	int	shdsize = pfile_p->pf_elfhd_p->e_shentsize;
223*1dd08564Sab 	int	i = 0;
2247a5d89c4Sab 	int		symtab_found = 0;
2267c478bd9Sstevel@tonic-gate 	tscn_p = 0;
22792ed1782Smike_s 	DEBUG_EXP(printf("Section header entry size = %d\n", shdsize));
22892ed1782Smike_s 	DEBUG_EXP(printf("First section header name = %s\n", &shdnms_p[1]));
2297a5d89c4Sab 	pfile_p->pf_symdat_aux_p = NULL;
2307a5d89c4Sab 	/*
2317a5d89c4Sab 	 * Scan the section headers looking for a symbol table. Our
2327a5d89c4Sab 	 * preference is to use .symtab, because it contains the full
2337a5d89c4Sab 	 * set of symbols. If we find it, we stop looking immediately
2347a5d89c4Sab 	 * and use it. In the absence of a .symtab section, we are
2357a5d89c4Sab 	 * willing to use the dynamic symbol table (.dynsym), possibly
2367a5d89c4Sab 	 * augmented by the .SUNW_ldynsym, which contains local symbols.
2377a5d89c4Sab 	 */
2387c478bd9Sstevel@tonic-gate 	while ((tscn_p = elf_nextscn(telf_p, tscn_p)) != NULL) {
2397c478bd9Sstevel@tonic-gate 		if ((tshd_p = elf32_getshdr(tscn_p)) == NULL)
2407c478bd9Sstevel@tonic-gate 			_err_exit("%s %d in %s.", fail_shd_s, i, aout_name);
241*1dd08564Sab 		i++;
24392ed1782Smike_s 		(void) memcpy(dest_p, tshd_p, shdsize);
24492ed1782Smike_s 		dest_p += shdsize;
24692ed1782Smike_s 		DEBUG_EXP(printf("index of section name = %d\n",
24792ed1782Smike_s 		    tshd_p->sh_name));
248*1dd08564Sab 		DEBUG_EXP(printf("_symintOpen: reading section %s\n",
249*1dd08564Sab 		    &shdnms_p[tshd_p->sh_name]));
2517a5d89c4Sab 		if (symtab_found)
2527a5d89c4Sab 			continue;
2537a5d89c4Sab 		switch (tshd_p->sh_type) {
2547a5d89c4Sab 		case SHT_SYMTAB:
2557c478bd9Sstevel@tonic-gate 			DEBUG_LOC("_symintOpen: found symbol section");
2567a5d89c4Sab 			pfile_p->pf_symstr_ndx = tshd_p->sh_link;
2577a5d89c4Sab 			pfile_p->pf_symdat_pri_p =
2587a5d89c4Sab 			    section_data_p(tscn_p, "symtab");
2597a5d89c4Sab 			nsyms_pri = tshd_p->sh_size / tshd_p->sh_entsize;
2607a5d89c4Sab 			/* Throw away .SUNW_ldynsym. It is for .dynsym only */
2617a5d89c4Sab 			nsyms_aux = 0;
2627a5d89c4Sab 			pfile_p->pf_symdat_aux_p = NULL;
2637a5d89c4Sab 			/* We have found the best symbol table. Stop looking */
2647a5d89c4Sab 			symtab_found = 1;
2657a5d89c4Sab 			break;
2677a5d89c4Sab 		case SHT_DYNSYM:
2687a5d89c4Sab 			/* We will use .dynsym if no .symtab is found */
2697a5d89c4Sab 			DEBUG_LOC("_symintOpen: found dynamic symbol section");
2707a5d89c4Sab 			pfile_p->pf_symstr_ndx = tshd_p->sh_link;
2717a5d89c4Sab 			pfile_p->pf_symdat_pri_p =
2727a5d89c4Sab 			    section_data_p(tscn_p, "dynsym");
2737a5d89c4Sab 			nsyms_pri = tshd_p->sh_size / tshd_p->sh_entsize;
2747a5d89c4Sab 			break;
2767a5d89c4Sab 		case SHT_SUNW_LDYNSYM:
2777a5d89c4Sab 			/* Auxiliary table, used with .dynsym */
2787a5d89c4Sab 			DEBUG_LOC("_symintOpen: found dynamic symbol section");
2797a5d89c4Sab 			pfile_p->pf_symdat_aux_p =
2807a5d89c4Sab 			    section_data_p(tscn_p, "SUNW_ldynsym");
2817a5d89c4Sab 			nsyms_aux = tshd_p->sh_size / tshd_p->sh_entsize;
2827a5d89c4Sab 			break;
2837c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	}
2887a5d89c4Sab 	if (pfile_p->pf_symdat_pri_p == NULL || pfile_p->pf_symstr_ndx == 0)
2897c478bd9Sstevel@tonic-gate 		_err_exit("%s %s.", fail_sym_s, executableName);
2917a5d89c4Sab 	pfile_p->pf_nstsyms = (int)(nsyms_pri + nsyms_aux);
2927a5d89c4Sab 	pfile_p->pf_nstsyms_aux = (int)nsyms_aux;
2937a5d89c4Sab 	if ((nsyms_pri + nsyms_aux) != (Elf64_Xword)pfile_p->pf_nstsyms)
2947a5d89c4Sab 		_err_exit("%s %s.", fail_sym32_s, executableName);
2977c478bd9Sstevel@tonic-gate 	DEBUG_LOC("_symintOpen: after for loop that reads the sections");
2997c478bd9Sstevel@tonic-gate 	DEBUG_LOC("_symintOpen: before call to _symintLoad");
3017c478bd9Sstevel@tonic-gate 	if ((pfile_p->pf_symarr_p = _symintLoad(pfile_p)) == NULL)
3027c478bd9Sstevel@tonic-gate 		_err_exit("%s %s.", fail_pfsym_s, executableName);
3047c478bd9Sstevel@tonic-gate 	DEBUG_LOC("_symintOpen: after call to _symintLoad");
3067c478bd9Sstevel@tonic-gate 	/*
30792ed1782Smike_s 	 * At this point we might want to include some consistency
30892ed1782Smike_s 	 * checks to be sure all is well.  For example, we can check
30992ed1782Smike_s 	 * symbol table consistency by comparing "the product of the
31092ed1782Smike_s 	 * number of symbols and the size of each symbol" to "the
31192ed1782Smike_s 	 * length of the symbol table data".
31292ed1782Smike_s 	 *
31392ed1782Smike_s 	 * Also, NULL may be a proper value (e.g., the debugger
31492ed1782Smike_s 	 * information when there is none) for some things that
31592ed1782Smike_s 	 * we cannot afford to be without.  We should check these
31692ed1782Smike_s 	 * at this point also.
31792ed1782Smike_s 	 */
3197c478bd9Sstevel@tonic-gate 	DEBUG_LOC("_symintOpen: bottom");
32092ed1782Smike_s 	return (pfile_p);
3217c478bd9Sstevel@tonic-gate }