xref: /freebsd/lib/libc/gen/nlist.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __SCCSID("@(#)nlist.c	8.1 (Berkeley) 6/4/93");
34 __FBSDID("$FreeBSD$");
35 
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/mman.h>
39 #include <sys/stat.h>
40 #include <sys/file.h>
41 #include <arpa/inet.h>
42 
43 #include <errno.h>
44 #include <a.out.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include "un-namespace.h"
49 
50 #include <machine/elf.h>
51 #include <elf-hints.h>
52 
53 int __fdnlist(int, struct nlist *);
54 int __elf_fdnlist(int, struct nlist *);
55 int __elf_is_okay__(Elf_Ehdr *);
56 
57 int
58 nlist(const char *name, struct nlist *list)
59 {
60 	int fd, n;
61 
62 	fd = _open(name, O_RDONLY | O_CLOEXEC, 0);
63 	if (fd < 0)
64 		return (-1);
65 	n = __fdnlist(fd, list);
66 	(void)_close(fd);
67 	return (n);
68 }
69 
70 static struct nlist_handlers {
71 	int	(*fn)(int fd, struct nlist *list);
72 } nlist_fn[] = {
73 	{ __elf_fdnlist },
74 };
75 
76 int
77 __fdnlist(int fd, struct nlist *list)
78 {
79 	int n = -1;
80 	unsigned int i;
81 
82 	for (i = 0; i < nitems(nlist_fn); i++) {
83 		n = (nlist_fn[i].fn)(fd, list);
84 		if (n != -1)
85 			break;
86 	}
87 	return (n);
88 }
89 
90 #define	ISLAST(p)	(p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
91 
92 static void elf_sym_to_nlist(struct nlist *, Elf_Sym *, Elf_Shdr *, int);
93 
94 /*
95  * __elf_is_okay__ - Determine if ehdr really
96  * is ELF and valid for the target platform.
97  *
98  * WARNING:  This is NOT an ELF ABI function and
99  * as such its use should be restricted.
100  */
101 int
102 __elf_is_okay__(Elf_Ehdr *ehdr)
103 {
104 	int retval = 0;
105 	/*
106 	 * We need to check magic, class size, endianess,
107 	 * and version before we look at the rest of the
108 	 * Elf_Ehdr structure.  These few elements are
109 	 * represented in a machine independent fashion.
110 	 */
111 	if (IS_ELF(*ehdr) &&
112 	    ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS &&
113 	    ehdr->e_ident[EI_DATA] == ELF_TARG_DATA &&
114 	    ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) {
115 
116 		/* Now check the machine dependant header */
117 		if (ehdr->e_machine == ELF_TARG_MACH &&
118 		    ehdr->e_version == ELF_TARG_VER)
119 			retval = 1;
120 	}
121 	return retval;
122 }
123 
124 int
125 __elf_fdnlist(int fd, struct nlist *list)
126 {
127 	struct nlist *p;
128 	Elf_Off symoff = 0, symstroff = 0;
129 	Elf_Size symsize = 0, symstrsize = 0;
130 	Elf_Ssize cc, i;
131 	int nent = -1;
132 	int errsave;
133 	Elf_Sym sbuf[1024];
134 	Elf_Sym *s;
135 	Elf_Ehdr ehdr;
136 	char *strtab = NULL;
137 	Elf_Shdr *shdr = NULL;
138 	Elf_Size shdr_size;
139 	void *base;
140 	struct stat st;
141 
142 	/* Make sure obj is OK */
143 	if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
144 	    _read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) ||
145 	    !__elf_is_okay__(&ehdr) ||
146 	    _fstat(fd, &st) < 0)
147 		return (-1);
148 
149 	/* calculate section header table size */
150 	shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
151 
152 	/* Make sure it's not too big to mmap */
153 	if (shdr_size > SIZE_T_MAX) {
154 		errno = EFBIG;
155 		return (-1);
156 	}
157 
158 	/* mmap section header table */
159 	base = mmap(NULL, (size_t)shdr_size, PROT_READ, MAP_PRIVATE, fd,
160 	    (off_t)ehdr.e_shoff);
161 	if (base == MAP_FAILED)
162 		return (-1);
163 	shdr = (Elf_Shdr *)base;
164 
165 	/*
166 	 * Find the symbol table entry and it's corresponding
167 	 * string table entry.	Version 1.1 of the ABI states
168 	 * that there is only one symbol table but that this
169 	 * could change in the future.
170 	 */
171 	for (i = 0; i < ehdr.e_shnum; i++) {
172 		if (shdr[i].sh_type == SHT_SYMTAB) {
173 			symoff = shdr[i].sh_offset;
174 			symsize = shdr[i].sh_size;
175 			symstroff = shdr[shdr[i].sh_link].sh_offset;
176 			symstrsize = shdr[shdr[i].sh_link].sh_size;
177 			break;
178 		}
179 	}
180 
181 	/* Check for files too large to mmap. */
182 	if (symstrsize > SIZE_T_MAX) {
183 		errno = EFBIG;
184 		goto done;
185 	}
186 	/*
187 	 * Map string table into our address space.  This gives us
188 	 * an easy way to randomly access all the strings, without
189 	 * making the memory allocation permanent as with malloc/free
190 	 * (i.e., munmap will return it to the system).
191 	 */
192 	base = mmap(NULL, (size_t)symstrsize, PROT_READ, MAP_PRIVATE, fd,
193 	    (off_t)symstroff);
194 	if (base == MAP_FAILED)
195 		goto done;
196 	strtab = (char *)base;
197 
198 	/*
199 	 * clean out any left-over information for all valid entries.
200 	 * Type and value defined to be 0 if not found; historical
201 	 * versions cleared other and desc as well.  Also figure out
202 	 * the largest string length so don't read any more of the
203 	 * string table than we have to.
204 	 *
205 	 * XXX clearing anything other than n_type and n_value violates
206 	 * the semantics given in the man page.
207 	 */
208 	nent = 0;
209 	for (p = list; !ISLAST(p); ++p) {
210 		p->n_type = 0;
211 		p->n_other = 0;
212 		p->n_desc = 0;
213 		p->n_value = 0;
214 		++nent;
215 	}
216 
217 	/* Don't process any further if object is stripped. */
218 	if (symoff == 0)
219 		goto done;
220 
221 	if (lseek(fd, (off_t) symoff, SEEK_SET) == -1) {
222 		nent = -1;
223 		goto done;
224 	}
225 
226 	while (symsize > 0 && nent > 0) {
227 		cc = MIN(symsize, sizeof(sbuf));
228 		if (_read(fd, sbuf, cc) != cc)
229 			break;
230 		symsize -= cc;
231 		for (s = sbuf; cc > 0 && nent > 0; ++s, cc -= sizeof(*s)) {
232 			char *name;
233 			struct nlist *p;
234 
235 			name = strtab + s->st_name;
236 			if (name[0] == '\0')
237 				continue;
238 			for (p = list; !ISLAST(p); p++) {
239 				if ((p->n_un.n_name[0] == '_' &&
240 				    strcmp(name, p->n_un.n_name+1) == 0)
241 				    || strcmp(name, p->n_un.n_name) == 0) {
242 					elf_sym_to_nlist(p, s, shdr,
243 					    ehdr.e_shnum);
244 					if (--nent <= 0)
245 						break;
246 				}
247 			}
248 		}
249 	}
250   done:
251 	errsave = errno;
252 	if (strtab != NULL)
253 		munmap(strtab, symstrsize);
254 	if (shdr != NULL)
255 		munmap(shdr, shdr_size);
256 	errno = errsave;
257 	return (nent);
258 }
259 
260 /*
261  * Convert an Elf_Sym into an nlist structure.  This fills in only the
262  * n_value and n_type members.
263  */
264 static void
265 elf_sym_to_nlist(struct nlist *nl, Elf_Sym *s, Elf_Shdr *shdr, int shnum)
266 {
267 	nl->n_value = s->st_value;
268 
269 	switch (s->st_shndx) {
270 	case SHN_UNDEF:
271 	case SHN_COMMON:
272 		nl->n_type = N_UNDF;
273 		break;
274 	case SHN_ABS:
275 		nl->n_type = ELF_ST_TYPE(s->st_info) == STT_FILE ?
276 		    N_FN : N_ABS;
277 		break;
278 	default:
279 		if (s->st_shndx >= shnum)
280 			nl->n_type = N_UNDF;
281 		else {
282 			Elf_Shdr *sh = shdr + s->st_shndx;
283 
284 			nl->n_type = sh->sh_type == SHT_PROGBITS ?
285 			    (sh->sh_flags & SHF_WRITE ? N_DATA : N_TEXT) :
286 			    (sh->sh_type == SHT_NOBITS ? N_BSS : N_UNDF);
287 		}
288 		break;
289 	}
290 
291 	if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
292 	    ELF_ST_BIND(s->st_info) == STB_WEAK)
293 		nl->n_type |= N_EXT;
294 }
295