18012Sjulian/* $NetBSD: nlist_elf32.c,v 1.39 2016/02/26 17:12:53 christos Exp $ */
212090Sgibbs
38012Sjulian/*
415329Sgibbs * Copyright (c) 1996 Christopher G. Demetriou
512090Sgibbs * All rights reserved.
615329Sgibbs *
712090Sgibbs * Redistribution and use in source and binary forms, with or without
812090Sgibbs * modification, are permitted provided that the following conditions
912090Sgibbs * are met:
1012090Sgibbs * 1. Redistributions of source code must retain the above copyright
1112090Sgibbs *    notice, this list of conditions and the following disclaimer.
1212090Sgibbs * 2. Redistributions in binary form must reproduce the above copyright
1312090Sgibbs *    notice, this list of conditions and the following disclaimer in the
1412090Sgibbs *    documentation and/or other materials provided with the distribution.
1512090Sgibbs * 3. All advertising materials mentioning features or use of this software
1615329Sgibbs *    must display the following acknowledgement:
1715329Sgibbs *          This product includes software developed for the
188012Sjulian *          NetBSD Project.  See http://www.NetBSD.org/ for
1915329Sgibbs *          information about NetBSD.
2015329Sgibbs * 4. The name of the author may not be used to endorse or promote products
2115329Sgibbs *    derived from this software without specific prior written permission.
2215329Sgibbs *
2315329Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2415329Sgibbs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2515329Sgibbs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2615329Sgibbs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2715329Sgibbs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2815329Sgibbs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2915329Sgibbs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3015329Sgibbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3149360Smdodd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
328012Sjulian * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
338012Sjulian *
3412090Sgibbs * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
3512090Sgibbs */
368012Sjulian
3713691Sgibbs#include <sys/cdefs.h>
3812090Sgibbs#if defined(LIBC_SCCS) && !defined(lint)
3912122Sgibbs__RCSID("$NetBSD: nlist_elf32.c,v 1.39 2016/02/26 17:12:53 christos Exp $");
4012122Sgibbs#endif /* LIBC_SCCS and not lint */
4112122Sgibbs
4212122Sgibbs/* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
4312122Sgibbs#ifndef ELFSIZE
4412122Sgibbs#define	ELFSIZE		32
4512090Sgibbs#endif
4623855Sjoerg
4712090Sgibbs#include "namespace.h"
4813691Sgibbs#include <sys/param.h>
4912090Sgibbs#include <sys/mman.h>
5045791Speter#include <sys/stat.h>
5145791Speter#include <sys/file.h>
5245791Speter#include <sys/ioctl.h>
5345791Speter
5445791Speter#include <assert.h>
5545791Speter#include <errno.h>
5649360Smdodd#include <stdio.h>
5749360Smdodd#include <string.h>
5849360Smdodd#include <unistd.h>
5945791Speter#include <nlist.h>
6045791Speter
6145791Speter#include "nlist_private.h"
6245791Speter#if defined(NLIST_ELF32) || defined(NLIST_ELF64)
6345791Speter#include <sys/exec_elf.h>
6445791Speter#endif
6545791Speter
6645791Speter#include <sys/ksyms.h>		/* after sys/exec_elf.h */
6745791Speter
6845791Speter#if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
6945791Speter    (defined(NLIST_ELF64) && (ELFSIZE == 64))
7045791Speter
7145791Speter/* No need to check for off < 0 because it is unsigned */
7245791Speter#define	check(off, size)	(off + size > mappedsize)
7345791Speter#define	BAD			goto out
7445791Speter#define	BADUNMAP		goto unmap
7545791Speter
7645791Speterint
7745791SpeterELFNAMEEND(__fdnlist)(int fd, struct nlist *list)
7845791Speter{
7945791Speter	struct stat st;
8045791Speter	Elf_Ehdr ehdr;
8149360Smdodd#if defined(_LP64) || ELFSIZE == 32 || defined(ELF64_MACHDEP_ID)
8245791Speter#if (ELFSIZE == 32)
8313691Sgibbs	Elf32_Half nshdr;
8413691Sgibbs#elif (ELFSIZE == 64)
8513691Sgibbs	Elf64_Word nshdr;
8613691Sgibbs#endif
8713691Sgibbs	/* Only support 64+32 mode on LP64 and those that have defined */
8845791Speter	/* ELF64_MACHDEP_ID, otherwise no support for 64 mode on ILP32 */
8945791Speter	Elf_Ehdr *ehdrp;
9013691Sgibbs	Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
9112090Sgibbs	Elf_Sym *symp;
92	Elf_Off shdr_off;
93	Elf_Word shdr_size;
94	struct nlist *p;
95	char *mappedfile, *strtab;
96	size_t mappedsize, nsyms;
97	int nent;
98#endif
99	int rv;
100	size_t i;
101
102	_DIAGASSERT(fd != -1);
103	_DIAGASSERT(list != NULL);
104
105	rv = -1;
106
107	/*
108	 * If we can't fstat() the file, something bad is going on.
109	 */
110	if (fstat(fd, &st) < 0)
111		BAD;
112
113	/*
114	 * Map the file in its entirety.
115	 */
116	if ((uintmax_t)st.st_size > (uintmax_t)SIZE_T_MAX) {
117		errno = EFBIG;
118		BAD;
119	}
120
121	/*
122	 * Read the elf header of the file.
123	 */
124	if ((ssize_t)(i = pread(fd, &ehdr, sizeof(Elf_Ehdr), (off_t)0)) == -1)
125		BAD;
126
127	/*
128	 * Check that the elf header is correct.
129	 */
130	if (i != sizeof(Elf_Ehdr))
131		BAD;
132	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
133	    ehdr.e_ident[EI_CLASS] != ELFCLASS)
134		BAD;
135
136	switch (ehdr.e_machine) {
137	ELFDEFNNAME(MACHDEP_ID_CASES)
138
139	default:
140		BAD;
141	}
142#if defined(_LP64) || ELFSIZE == 32 || defined(ELF64_MACHDEP_ID)
143	symshdrp = symstrshdrp = NULL;
144
145	/* Only support 64+32 mode on LP64 and those that have defined */
146	/* ELF64_MACHDEP_ID, otherwise no support for 64 mode on ILP32 */
147	if (S_ISCHR(st.st_mode)) {
148		const char *nlistname;
149		Elf_Sym sym;
150
151		/*
152		 * Character device; assume /dev/ksyms.
153		 */
154		nent = 0;
155		for (p = list; !ISLAST(p); ++p) {
156			struct ksyms_gsymbol kg;
157			int error;
158
159			p->n_other = 0;
160			p->n_desc = 0;
161			nlistname = N_NAME(p);
162			if (*nlistname == '_')
163				nlistname++;
164
165			memset(&kg, 0, sizeof(kg));
166			kg.kg_name = nlistname;
167#ifdef OKIOCGSYMBOL
168			struct ksyms_ogsymbol okg;
169			error = ioctl(fd, KIOCGSYMBOL, &kg);
170			if (error == 0) {
171				sym = kg.kg_sym;
172			} else if (error && errno == ENOTTY) {
173				memset(&okg, 0, sizeof(okg));
174				okg.kg_name = nlistname;
175				okg.kg_sym = &sym;
176				error = ioctl(fd, OKIOCGSYMBOL, &okg);
177			}
178#else
179			kg.kg_sym = &sym;
180			error = ioctl(fd, KIOCGSYMBOL, &kg);
181#endif
182			if (error == 0
183#if !defined(_LP64) && ELFSIZE == 64
184#if __mips__
185			    && (intptr_t)sym.st_value == (intmax_t)sym.st_value
186#else
187			    && (uintptr_t)sym.st_value == sym.st_value
188#endif
189#endif
190			    && /*CONSTCOND*/1) {
191				p->n_value = (uintptr_t)sym.st_value;
192				switch (ELF_ST_TYPE(sym.st_info)) {
193				case STT_NOTYPE:
194					p->n_type = N_UNDF;
195					break;
196				case STT_COMMON:
197				case STT_OBJECT:
198					p->n_type = N_DATA;
199					break;
200				case STT_FUNC:
201					p->n_type = N_TEXT;
202					break;
203				case STT_FILE:
204					p->n_type = N_FN;
205					break;
206				default:
207					p->n_type = 0;
208					/* catch other enumerations for gcc */
209					break;
210				}
211				if (ELF_ST_BIND(sym.st_info) != STB_LOCAL)
212					p->n_type |= N_EXT;
213			} else {
214				nent++;
215				p->n_value = 0;
216				p->n_type = 0;
217			}
218		}
219		return nent;
220	}
221
222	mappedsize = (size_t)st.st_size;
223	mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
224	    fd, (off_t)0);
225	if (mappedfile == (char *)-1)
226		BAD;
227
228	/*
229	 * Make sure we can access the executable's header
230	 * directly, and make sure the recognize the executable
231	 * as an ELF binary.
232	 */
233	if (check(0, sizeof *ehdrp))
234		BADUNMAP;
235	ehdrp = (Elf_Ehdr *)(void *)&mappedfile[0];
236
237	/*
238	 * Find the symbol list and string table.
239	 */
240	nshdr = ehdrp->e_shnum;
241	shdr_off = ehdrp->e_shoff;
242	shdr_size = ehdrp->e_shentsize * nshdr;
243
244	if (check(shdr_off, shdr_size) ||
245	    (sizeof *shdrp != ehdrp->e_shentsize))
246		BADUNMAP;
247	shdrp = (void *)&mappedfile[(size_t)shdr_off];
248
249	for (i = 0; i < nshdr; i++) {
250		if (shdrp[i].sh_type == SHT_SYMTAB) {
251			symshdrp = &shdrp[i];
252			symstrshdrp = &shdrp[shdrp[i].sh_link];
253		}
254	}
255
256	/* Make sure we're not stripped. */
257	if (symshdrp == NULL || symshdrp->sh_offset == 0)
258		BADUNMAP;
259
260	/* Make sure the symbols and strings are safely mapped. */
261	if (check(symshdrp->sh_offset, symshdrp->sh_size))
262		BADUNMAP;
263	if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
264		BADUNMAP;
265
266	symp = (void *)&mappedfile[(size_t)symshdrp->sh_offset];
267	nsyms = (size_t)(symshdrp->sh_size / sizeof(*symp));
268	strtab = &mappedfile[(size_t)symstrshdrp->sh_offset];
269
270	/*
271	 * Clean out any left-over information for all valid entries.
272	 * Type and value are defined to be 0 if not found; historical
273	 * versions cleared other and desc as well.
274	 *
275	 * XXX Clearing anything other than n_type and n_value violates
276	 * the semantics given in the man page.
277	 */
278	nent = 0;
279	for (p = list; !ISLAST(p); ++p) {
280		p->n_type = 0;
281		p->n_other = 0;
282		p->n_desc = 0;
283		p->n_value = 0;
284		++nent;
285	}
286
287	for (i = 0; i < nsyms; i++) {
288		for (p = list; !ISLAST(p); ++p) {
289			const char *nlistname;
290			char *symtabname;
291
292			/* This may be incorrect */
293			nlistname = N_NAME(p);
294			if (*nlistname == '_')
295				nlistname++;
296
297			symtabname = &strtab[symp[i].st_name];
298
299			if (!strcmp(symtabname, nlistname)) {
300				/*
301				 * Translate (roughly) from ELF to nlist
302				 */
303				p->n_value = (uintptr_t)symp[i].st_value;
304				switch (ELF_ST_TYPE(symp[i].st_info)) {
305				case STT_NOTYPE:
306					p->n_type = N_UNDF;
307					break;
308				case STT_OBJECT:
309				case STT_COMMON:
310					p->n_type = N_DATA;
311					break;
312				case STT_FUNC:
313					p->n_type = N_TEXT;
314					break;
315				case STT_FILE:
316					p->n_type = N_FN;
317					break;
318				default:
319					/* catch other enumerations for gcc */
320					break;
321				}
322				if (ELF_ST_BIND(symp[i].st_info) != STB_LOCAL)
323					p->n_type |= N_EXT;
324				p->n_desc = 0;			/* XXX */
325				p->n_other = 0;			/* XXX */
326
327				if (--nent <= 0)
328					goto done;
329				break;	/* into next run of outer loop */
330			}
331		}
332	}
333
334done:
335	rv = nent;
336unmap:
337	munmap(mappedfile, mappedsize);
338#endif /* _LP64 || ELFSIZE == 32 || ELF64_MACHDEP_ID */
339out:
340	return (rv);
341}
342
343#endif
344