xref: /illumos-gate/usr/src/cmd/sgs/libelf/misc/nlist.c (revision e2f4f3da)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1998 by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include "libelf.h"
32*7c478bd9Sstevel@tonic-gate #include <unistd.h>
33*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <nlist.h>
36*7c478bd9Sstevel@tonic-gate #include "syms.h"
37*7c478bd9Sstevel@tonic-gate #include "gelf.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #undef n_name		/* This undef is to handle a #define in syms.h */
40*7c478bd9Sstevel@tonic-gate 			/* which conflicts with the member nlist->n_name */
41*7c478bd9Sstevel@tonic-gate 			/* as defined in nlist.h */
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #define	SPACE 100		/* number of symbols read at a time */
45*7c478bd9Sstevel@tonic-gate #define	ISELF (strncmp(magic_buf, ELFMAG, SELFMAG) == 0)
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate int
end_elf_job(int fd,Elf * elfdes)49*7c478bd9Sstevel@tonic-gate end_elf_job(int fd, Elf * elfdes)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	(void) elf_end(elfdes);
52*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
53*7c478bd9Sstevel@tonic-gate 	return (-1);
54*7c478bd9Sstevel@tonic-gate }
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate int
_elf_nlist(int fd,struct nlist * list)58*7c478bd9Sstevel@tonic-gate _elf_nlist(int fd, struct nlist * list)
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	Elf	   *elfdes;	/* ELF descriptor */
61*7c478bd9Sstevel@tonic-gate 	GElf_Ehdr  ehdr;	/* ELF Ehdr */
62*7c478bd9Sstevel@tonic-gate 	GElf_Shdr  s_buf;	/* buffer storing section header */
63*7c478bd9Sstevel@tonic-gate 	Elf_Data   *symdata;	/* buffer points to symbol table */
64*7c478bd9Sstevel@tonic-gate 	Elf_Scn    *secidx = 0;	/* index of the section header table */
65*7c478bd9Sstevel@tonic-gate 	GElf_Sym   sym;		/* buffer storing one symbol information */
66*7c478bd9Sstevel@tonic-gate 	unsigned   strtab;	/* index of symbol name in string table */
67*7c478bd9Sstevel@tonic-gate 	long	   count;	/* number of symbols */
68*7c478bd9Sstevel@tonic-gate 	long	   ii;		/* loop control */
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
71*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
72*7c478bd9Sstevel@tonic-gate 		return (-1);
73*7c478bd9Sstevel@tonic-gate 	}
74*7c478bd9Sstevel@tonic-gate 	elfdes = elf_begin(fd, ELF_C_READ, (Elf *)0);
75*7c478bd9Sstevel@tonic-gate 	if (gelf_getehdr(elfdes, &ehdr) == 0)
76*7c478bd9Sstevel@tonic-gate 		return (end_elf_job(fd, elfdes));
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	while ((secidx = elf_nextscn(elfdes, secidx)) != 0) {
79*7c478bd9Sstevel@tonic-gate 		if ((gelf_getshdr(secidx, &s_buf)) == 0)
80*7c478bd9Sstevel@tonic-gate 			return (end_elf_job(fd, elfdes));
81*7c478bd9Sstevel@tonic-gate 		if (s_buf.sh_type != SHT_SYMTAB) /* not symbol table */
82*7c478bd9Sstevel@tonic-gate 			continue;
83*7c478bd9Sstevel@tonic-gate 		symdata = elf_getdata(secidx, (Elf_Data *)0);
84*7c478bd9Sstevel@tonic-gate 		if (symdata == 0)
85*7c478bd9Sstevel@tonic-gate 			return (end_elf_job(fd, elfdes));
86*7c478bd9Sstevel@tonic-gate 		if (symdata->d_size == 0)
87*7c478bd9Sstevel@tonic-gate 			break;
88*7c478bd9Sstevel@tonic-gate 		strtab = s_buf.sh_link;
89*7c478bd9Sstevel@tonic-gate 		count = symdata->d_size / s_buf.sh_entsize;
90*7c478bd9Sstevel@tonic-gate 		for (ii = 1; ii < count; ++ii) {
91*7c478bd9Sstevel@tonic-gate 			struct nlist *p;
92*7c478bd9Sstevel@tonic-gate 			register char *name;
93*7c478bd9Sstevel@tonic-gate 			/* LINTED */
94*7c478bd9Sstevel@tonic-gate 			(void) gelf_getsym(symdata, (int)ii, &sym);
95*7c478bd9Sstevel@tonic-gate 			name = elf_strptr(elfdes, strtab, (size_t)sym.st_name);
96*7c478bd9Sstevel@tonic-gate 			if (name == 0)
97*7c478bd9Sstevel@tonic-gate 				continue;
98*7c478bd9Sstevel@tonic-gate 			for (p = list; p->n_name && p->n_name[0]; ++p) {
99*7c478bd9Sstevel@tonic-gate 				if (strcmp(p->n_name, name))
100*7c478bd9Sstevel@tonic-gate 					continue;
101*7c478bd9Sstevel@tonic-gate 				p->n_value = (long)sym.st_value;
102*7c478bd9Sstevel@tonic-gate 				p->n_type = GELF_ST_TYPE(sym.st_info);
103*7c478bd9Sstevel@tonic-gate 				p->n_scnum = sym.st_shndx;
104*7c478bd9Sstevel@tonic-gate 				break;
105*7c478bd9Sstevel@tonic-gate 			}
106*7c478bd9Sstevel@tonic-gate 		}
107*7c478bd9Sstevel@tonic-gate 		break;
108*7c478bd9Sstevel@tonic-gate 		/*
109*7c478bd9Sstevel@tonic-gate 		 * Currently there is only one symbol table section
110*7c478bd9Sstevel@tonic-gate 		 * in an object file, but this restriction may be
111*7c478bd9Sstevel@tonic-gate 		 * relaxed in the future.
112*7c478bd9Sstevel@tonic-gate 		 */
113*7c478bd9Sstevel@tonic-gate 	}
114*7c478bd9Sstevel@tonic-gate 	(void) elf_end(elfdes);
115*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
116*7c478bd9Sstevel@tonic-gate 	return (0);
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate int
nlist(const char * name,struct nlist * list)120*7c478bd9Sstevel@tonic-gate nlist(const char * name, struct nlist * list)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	register struct nlist *p;
123*7c478bd9Sstevel@tonic-gate 	char magic_buf[EI_NIDENT];
124*7c478bd9Sstevel@tonic-gate 	int fd;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	for (p = list; p->n_name && p->n_name[0]; p++) { /* n_name can be ptr */
127*7c478bd9Sstevel@tonic-gate 		p->n_type = 0;
128*7c478bd9Sstevel@tonic-gate 		p->n_value = 0L;
129*7c478bd9Sstevel@tonic-gate 		p->n_scnum = 0;
130*7c478bd9Sstevel@tonic-gate 		p->n_sclass = 0;
131*7c478bd9Sstevel@tonic-gate 		p->n_numaux = 0;
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if ((fd = open(name, 0)) < 0)
135*7c478bd9Sstevel@tonic-gate 		return (-1);
136*7c478bd9Sstevel@tonic-gate 	if (read(fd, magic_buf, EI_NIDENT) == -1) {
137*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
138*7c478bd9Sstevel@tonic-gate 		return (-1);
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	if (lseek(fd, 0L, 0) == -1L) {	/* rewind to beginning of object file */
142*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
143*7c478bd9Sstevel@tonic-gate 		return (-1);
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate #ifndef _LP64
147*7c478bd9Sstevel@tonic-gate 	if (ISELF && (magic_buf[EI_CLASS] == ELFCLASS32))
148*7c478bd9Sstevel@tonic-gate #else
149*7c478bd9Sstevel@tonic-gate 	if (ISELF)		/* 64-bit case handles both Elf32 and Elf64 */
150*7c478bd9Sstevel@tonic-gate #endif
151*7c478bd9Sstevel@tonic-gate 		return (_elf_nlist(fd, list));
152*7c478bd9Sstevel@tonic-gate 	else {
153*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
154*7c478bd9Sstevel@tonic-gate 		return (-1);
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate }
157