xref: /freebsd/lib/libc/gen/nlist.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/mman.h>
37 #include <sys/stat.h>
38 #include <sys/file.h>
39 #include <arpa/inet.h>
40 
41 #include <errno.h>
42 #include <a.out.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include "un-namespace.h"
47 
48 #include <machine/elf.h>
49 #include <elf-hints.h>
50 
51 int __fdnlist(int, struct nlist *);
52 int __elf_fdnlist(int, struct nlist *);
53 int __elf_is_okay__(Elf_Ehdr *);
54 
55 int
56 nlist(const char *name, struct nlist *list)
57 {
58 	int fd, n;
59 
60 	fd = _open(name, O_RDONLY | O_CLOEXEC, 0);
61 	if (fd < 0)
62 		return (-1);
63 	n = __fdnlist(fd, list);
64 	(void)_close(fd);
65 	return (n);
66 }
67 
68 static struct nlist_handlers {
69 	int	(*fn)(int fd, struct nlist *list);
70 } nlist_fn[] = {
71 	{ __elf_fdnlist },
72 };
73 
74 int
75 __fdnlist(int fd, struct nlist *list)
76 {
77 	int n = -1;
78 	unsigned int i;
79 
80 	for (i = 0; i < nitems(nlist_fn); i++) {
81 		n = (nlist_fn[i].fn)(fd, list);
82 		if (n != -1)
83 			break;
84 	}
85 	return (n);
86 }
87 
88 #define	ISLAST(p)	(p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
89 
90 static void elf_sym_to_nlist(struct nlist *, Elf_Sym *, Elf_Shdr *, int);
91 
92 /*
93  * __elf_is_okay__ - Determine if ehdr really
94  * is ELF and valid for the target platform.
95  *
96  * WARNING:  This is NOT an ELF ABI function and
97  * as such its use should be restricted.
98  */
99 int
100 __elf_is_okay__(Elf_Ehdr *ehdr)
101 {
102 	int retval = 0;
103 	/*
104 	 * We need to check magic, class size, endianess,
105 	 * and version before we look at the rest of the
106 	 * Elf_Ehdr structure.  These few elements are
107 	 * represented in a machine independent fashion.
108 	 */
109 	if (IS_ELF(*ehdr) &&
110 	    ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS &&
111 	    ehdr->e_ident[EI_DATA] == ELF_TARG_DATA &&
112 	    ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) {
113 
114 		/* Now check the machine dependant header */
115 		if (ehdr->e_machine == ELF_TARG_MACH &&
116 		    ehdr->e_version == ELF_TARG_VER)
117 			retval = 1;
118 	}
119 	return retval;
120 }
121 
122 int
123 __elf_fdnlist(int fd, struct nlist *list)
124 {
125 	struct nlist *p;
126 	Elf_Off symoff = 0, symstroff = 0;
127 	Elf_Size symsize = 0, symstrsize = 0;
128 	Elf_Ssize cc, i;
129 	int nent = -1;
130 	int errsave;
131 	Elf_Sym sbuf[1024];
132 	Elf_Sym *s;
133 	Elf_Ehdr ehdr;
134 	char *strtab = NULL;
135 	Elf_Shdr *shdr = NULL;
136 	Elf_Size shdr_size;
137 	void *base;
138 	struct stat st;
139 
140 	/* Make sure obj is OK */
141 	if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
142 	    _read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) ||
143 	    !__elf_is_okay__(&ehdr) ||
144 	    _fstat(fd, &st) < 0)
145 		return (-1);
146 
147 	/* calculate section header table size */
148 	shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
149 
150 	/* Make sure it's not too big to mmap */
151 	if (shdr_size > SIZE_T_MAX) {
152 		errno = EFBIG;
153 		return (-1);
154 	}
155 
156 	/* mmap section header table */
157 	base = mmap(NULL, (size_t)shdr_size, PROT_READ, MAP_PRIVATE, fd,
158 	    (off_t)ehdr.e_shoff);
159 	if (base == MAP_FAILED)
160 		return (-1);
161 	shdr = (Elf_Shdr *)base;
162 
163 	/*
164 	 * Find the symbol table entry and it's corresponding
165 	 * string table entry.	Version 1.1 of the ABI states
166 	 * that there is only one symbol table but that this
167 	 * could change in the future.
168 	 */
169 	for (i = 0; i < ehdr.e_shnum; i++) {
170 		if (shdr[i].sh_type == SHT_SYMTAB) {
171 			symoff = shdr[i].sh_offset;
172 			symsize = shdr[i].sh_size;
173 			symstroff = shdr[shdr[i].sh_link].sh_offset;
174 			symstrsize = shdr[shdr[i].sh_link].sh_size;
175 			break;
176 		}
177 	}
178 
179 	/* Check for files too large to mmap. */
180 	if (symstrsize > SIZE_T_MAX) {
181 		errno = EFBIG;
182 		goto done;
183 	}
184 	/*
185 	 * Map string table into our address space.  This gives us
186 	 * an easy way to randomly access all the strings, without
187 	 * making the memory allocation permanent as with malloc/free
188 	 * (i.e., munmap will return it to the system).
189 	 */
190 	base = mmap(NULL, (size_t)symstrsize, PROT_READ, MAP_PRIVATE, fd,
191 	    (off_t)symstroff);
192 	if (base == MAP_FAILED)
193 		goto done;
194 	strtab = (char *)base;
195 
196 	/*
197 	 * clean out any left-over information for all valid entries.
198 	 * Type and value defined to be 0 if not found; historical
199 	 * versions cleared other and desc as well.  Also figure out
200 	 * the largest string length so don't read any more of the
201 	 * string table than we have to.
202 	 *
203 	 * XXX clearing anything other than n_type and n_value violates
204 	 * the semantics given in the man page.
205 	 */
206 	nent = 0;
207 	for (p = list; !ISLAST(p); ++p) {
208 		p->n_type = 0;
209 		p->n_other = 0;
210 		p->n_desc = 0;
211 		p->n_value = 0;
212 		++nent;
213 	}
214 
215 	/* Don't process any further if object is stripped. */
216 	if (symoff == 0)
217 		goto done;
218 
219 	if (lseek(fd, (off_t) symoff, SEEK_SET) == -1) {
220 		nent = -1;
221 		goto done;
222 	}
223 
224 	while (symsize > 0 && nent > 0) {
225 		cc = MIN(symsize, sizeof(sbuf));
226 		if (_read(fd, sbuf, cc) != cc)
227 			break;
228 		symsize -= cc;
229 		for (s = sbuf; cc > 0 && nent > 0; ++s, cc -= sizeof(*s)) {
230 			char *name;
231 			struct nlist *p;
232 
233 			name = strtab + s->st_name;
234 			if (name[0] == '\0')
235 				continue;
236 			for (p = list; !ISLAST(p); p++) {
237 				if ((p->n_un.n_name[0] == '_' &&
238 				    strcmp(name, p->n_un.n_name+1) == 0)
239 				    || strcmp(name, p->n_un.n_name) == 0) {
240 					elf_sym_to_nlist(p, s, shdr,
241 					    ehdr.e_shnum);
242 					if (--nent <= 0)
243 						break;
244 				}
245 			}
246 		}
247 	}
248   done:
249 	errsave = errno;
250 	if (strtab != NULL)
251 		munmap(strtab, symstrsize);
252 	if (shdr != NULL)
253 		munmap(shdr, shdr_size);
254 	errno = errsave;
255 	return (nent);
256 }
257 
258 /*
259  * Convert an Elf_Sym into an nlist structure.  This fills in only the
260  * n_value and n_type members.
261  */
262 static void
263 elf_sym_to_nlist(struct nlist *nl, Elf_Sym *s, Elf_Shdr *shdr, int shnum)
264 {
265 	nl->n_value = s->st_value;
266 
267 	switch (s->st_shndx) {
268 	case SHN_UNDEF:
269 	case SHN_COMMON:
270 		nl->n_type = N_UNDF;
271 		break;
272 	case SHN_ABS:
273 		nl->n_type = ELF_ST_TYPE(s->st_info) == STT_FILE ?
274 		    N_FN : N_ABS;
275 		break;
276 	default:
277 		if (s->st_shndx >= shnum)
278 			nl->n_type = N_UNDF;
279 		else {
280 			Elf_Shdr *sh = shdr + s->st_shndx;
281 
282 			nl->n_type = sh->sh_type == SHT_PROGBITS ?
283 			    (sh->sh_flags & SHF_WRITE ? N_DATA : N_TEXT) :
284 			    (sh->sh_type == SHT_NOBITS ? N_BSS : N_UNDF);
285 		}
286 		break;
287 	}
288 
289 	if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
290 	    ELF_ST_BIND(s->st_info) == STB_WEAK)
291 		nl->n_type |= N_EXT;
292 }
293