xref: /illumos-gate/usr/src/cmd/sgs/libelf/misc/nlist.c (revision 7c478bd9)
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 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1998 by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.13	*/
32 
33 #include "libelf.h"
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #include <nlist.h>
38 #include <note.h>
39 #include "syms.h"
40 #include "gelf.h"
41 
42 #undef n_name		/* This undef is to handle a #define in syms.h */
43 			/* which conflicts with the member nlist->n_name */
44 			/* as defined in nlist.h */
45 
46 
47 #define	SPACE 100		/* number of symbols read at a time */
48 #define	ISELF (strncmp(magic_buf, ELFMAG, SELFMAG) == 0)
49 
50 
51 int
52 end_elf_job(int fd, Elf * elfdes)
53 {
54 	(void) elf_end(elfdes);
55 	(void) close(fd);
56 	return (-1);
57 }
58 
59 
60 int
61 _elf_nlist(int fd, struct nlist * list)
62 {
63 	Elf	   *elfdes;	/* ELF descriptor */
64 	GElf_Ehdr  ehdr;	/* ELF Ehdr */
65 	GElf_Shdr  s_buf;	/* buffer storing section header */
66 	Elf_Data   *symdata;	/* buffer points to symbol table */
67 	Elf_Scn    *secidx = 0;	/* index of the section header table */
68 	GElf_Sym   sym;		/* buffer storing one symbol information */
69 	unsigned   strtab;	/* index of symbol name in string table */
70 	long	   count;	/* number of symbols */
71 	long	   ii;		/* loop control */
72 
73 	if (elf_version(EV_CURRENT) == EV_NONE) {
74 		(void) close(fd);
75 		return (-1);
76 	}
77 	elfdes = elf_begin(fd, ELF_C_READ, (Elf *)0);
78 	if (gelf_getehdr(elfdes, &ehdr) == 0)
79 		return (end_elf_job(fd, elfdes));
80 
81 	while ((secidx = elf_nextscn(elfdes, secidx)) != 0) {
82 		if ((gelf_getshdr(secidx, &s_buf)) == 0)
83 			return (end_elf_job(fd, elfdes));
84 		if (s_buf.sh_type != SHT_SYMTAB) /* not symbol table */
85 			continue;
86 		symdata = elf_getdata(secidx, (Elf_Data *)0);
87 		if (symdata == 0)
88 			return (end_elf_job(fd, elfdes));
89 		if (symdata->d_size == 0)
90 			break;
91 		strtab = s_buf.sh_link;
92 		count = symdata->d_size / s_buf.sh_entsize;
93 		for (ii = 1; ii < count; ++ii) {
94 			struct nlist *p;
95 			register char *name;
96 			/* LINTED */
97 			(void) gelf_getsym(symdata, (int)ii, &sym);
98 			name = elf_strptr(elfdes, strtab, (size_t)sym.st_name);
99 			if (name == 0)
100 				continue;
101 			for (p = list; p->n_name && p->n_name[0]; ++p) {
102 				if (strcmp(p->n_name, name))
103 					continue;
104 				p->n_value = (long)sym.st_value;
105 				p->n_type = GELF_ST_TYPE(sym.st_info);
106 				p->n_scnum = sym.st_shndx;
107 				break;
108 			}
109 		}
110 		break;
111 		/*
112 		 * Currently there is only one symbol table section
113 		 * in an object file, but this restriction may be
114 		 * relaxed in the future.
115 		 */
116 	}
117 	(void) elf_end(elfdes);
118 	(void) close(fd);
119 	return (0);
120 }
121 
122 
123 NOTE(SCHEME_PROTECTS_DATA("user provides buffers", nlist))
124 
125 int
126 nlist(const char * name, struct nlist * list)
127 {
128 	register struct nlist *p;
129 	char magic_buf[EI_NIDENT];
130 	int fd;
131 
132 	for (p = list; p->n_name && p->n_name[0]; p++) { /* n_name can be ptr */
133 		p->n_type = 0;
134 		p->n_value = 0L;
135 		p->n_scnum = 0;
136 		p->n_sclass = 0;
137 		p->n_numaux = 0;
138 	}
139 
140 	if ((fd = open(name, 0)) < 0)
141 		return (-1);
142 	if (read(fd, magic_buf, EI_NIDENT) == -1) {
143 		(void) close(fd);
144 		return (-1);
145 	}
146 
147 	if (lseek(fd, 0L, 0) == -1L) {	/* rewind to beginning of object file */
148 		(void) close(fd);
149 		return (-1);
150 	}
151 
152 #ifndef _LP64
153 	if (ISELF && (magic_buf[EI_CLASS] == ELFCLASS32))
154 #else
155 	if (ISELF)		/* 64-bit case handles both Elf32 and Elf64 */
156 #endif
157 		return (_elf_nlist(fd, list));
158 	else {
159 		(void) close(fd);
160 		return (-1);
161 	}
162 }
163