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