1*f14fb602SLionel Sambuc /* $NetBSD: nlist_ecoff.c,v 1.23 2012/03/20 00:31:24 matt Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1996 Christopher G. Demetriou
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. All advertising materials mentioning features or use of this software
162fe8fb19SBen Gras * must display the following acknowledgement:
172fe8fb19SBen Gras * This product includes software developed for the
182fe8fb19SBen Gras * NetBSD Project. See http://www.NetBSD.org/ for
192fe8fb19SBen Gras * information about NetBSD.
202fe8fb19SBen Gras * 4. The name of the author may not be used to endorse or promote products
212fe8fb19SBen Gras * derived from this software without specific prior written permission.
222fe8fb19SBen Gras *
232fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
242fe8fb19SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
252fe8fb19SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
262fe8fb19SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
272fe8fb19SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
282fe8fb19SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
292fe8fb19SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
302fe8fb19SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
312fe8fb19SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
322fe8fb19SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
332fe8fb19SBen Gras *
342fe8fb19SBen Gras * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
352fe8fb19SBen Gras */
362fe8fb19SBen Gras
372fe8fb19SBen Gras #include <sys/cdefs.h>
382fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
39*f14fb602SLionel Sambuc __RCSID("$NetBSD: nlist_ecoff.c,v 1.23 2012/03/20 00:31:24 matt Exp $");
402fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
412fe8fb19SBen Gras
422fe8fb19SBen Gras #include "namespace.h"
432fe8fb19SBen Gras #include <sys/param.h>
442fe8fb19SBen Gras #include <sys/mman.h>
452fe8fb19SBen Gras #include <sys/stat.h>
462fe8fb19SBen Gras #include <sys/file.h>
472fe8fb19SBen Gras
482fe8fb19SBen Gras #include <assert.h>
492fe8fb19SBen Gras #include <errno.h>
502fe8fb19SBen Gras #include <stdio.h>
512fe8fb19SBen Gras #include <string.h>
522fe8fb19SBen Gras #include <unistd.h>
532fe8fb19SBen Gras #include <nlist.h>
542fe8fb19SBen Gras
552fe8fb19SBen Gras #include "nlist_private.h"
562fe8fb19SBen Gras #ifdef NLIST_ECOFF
572fe8fb19SBen Gras #include <sys/exec_ecoff.h>
582fe8fb19SBen Gras #endif
592fe8fb19SBen Gras
602fe8fb19SBen Gras #ifdef NLIST_ECOFF
61*f14fb602SLionel Sambuc #define check(off, size) \
62*f14fb602SLionel Sambuc ((size_t)off >= mappedsize || (size_t)(off + size) > mappedsize)
632fe8fb19SBen Gras
642fe8fb19SBen Gras int
__fdnlist_ecoff(int fd,struct nlist * list)65*f14fb602SLionel Sambuc __fdnlist_ecoff(int fd, struct nlist *list)
662fe8fb19SBen Gras {
672fe8fb19SBen Gras struct nlist *p;
68*f14fb602SLionel Sambuc const struct ecoff_exechdr *exechdrp;
69*f14fb602SLionel Sambuc const struct ecoff_symhdr *symhdrp;
70*f14fb602SLionel Sambuc const struct ecoff_extsym *esyms;
712fe8fb19SBen Gras struct stat st;
72*f14fb602SLionel Sambuc const char *mappedfile;
732fe8fb19SBen Gras size_t mappedsize;
742fe8fb19SBen Gras u_long symhdroff, extstroff;
752fe8fb19SBen Gras u_int symhdrsize;
762fe8fb19SBen Gras int rv, nent;
772fe8fb19SBen Gras long i, nesyms;
782fe8fb19SBen Gras
792fe8fb19SBen Gras _DIAGASSERT(fd != -1);
802fe8fb19SBen Gras _DIAGASSERT(list != NULL);
812fe8fb19SBen Gras
822fe8fb19SBen Gras rv = -1;
832fe8fb19SBen Gras
842fe8fb19SBen Gras /*
852fe8fb19SBen Gras * If we can't fstat() the file, something bad is going on.
862fe8fb19SBen Gras */
872fe8fb19SBen Gras if (fstat(fd, &st) < 0)
88*f14fb602SLionel Sambuc goto out;
892fe8fb19SBen Gras
902fe8fb19SBen Gras /*
912fe8fb19SBen Gras * Map the file in its entirety.
922fe8fb19SBen Gras */
932fe8fb19SBen Gras if ((uintmax_t)st.st_size > (uintmax_t)SIZE_T_MAX) {
942fe8fb19SBen Gras errno = EFBIG;
95*f14fb602SLionel Sambuc goto out;
962fe8fb19SBen Gras }
97*f14fb602SLionel Sambuc mappedsize = (size_t)st.st_size;
982fe8fb19SBen Gras mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
992fe8fb19SBen Gras fd, 0);
100*f14fb602SLionel Sambuc if (mappedfile == MAP_FAILED)
101*f14fb602SLionel Sambuc goto out;
1022fe8fb19SBen Gras
1032fe8fb19SBen Gras /*
1042fe8fb19SBen Gras * Make sure we can access the executable's header
1052fe8fb19SBen Gras * directly, and make sure the recognize the executable
1062fe8fb19SBen Gras * as an ECOFF binary.
1072fe8fb19SBen Gras */
1082fe8fb19SBen Gras if (check(0, sizeof *exechdrp))
109*f14fb602SLionel Sambuc goto unmap;
110*f14fb602SLionel Sambuc exechdrp = (const void *)mappedfile;
1112fe8fb19SBen Gras
1122fe8fb19SBen Gras if (ECOFF_BADMAG(exechdrp))
113*f14fb602SLionel Sambuc goto unmap;
1142fe8fb19SBen Gras
1152fe8fb19SBen Gras /*
1162fe8fb19SBen Gras * Find the symbol list.
1172fe8fb19SBen Gras */
1182fe8fb19SBen Gras symhdroff = exechdrp->f.f_symptr;
1192fe8fb19SBen Gras symhdrsize = exechdrp->f.f_nsyms;
1202fe8fb19SBen Gras
1212fe8fb19SBen Gras if ((symhdroff + sizeof *symhdrp) > mappedsize ||
1222fe8fb19SBen Gras sizeof *symhdrp != symhdrsize)
123*f14fb602SLionel Sambuc goto unmap;
124*f14fb602SLionel Sambuc symhdrp = (const void *)&mappedfile[symhdroff];
1252fe8fb19SBen Gras
1262fe8fb19SBen Gras nesyms = symhdrp->esymMax;
1272fe8fb19SBen Gras if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms))
128*f14fb602SLionel Sambuc goto unmap;
129*f14fb602SLionel Sambuc esyms = (const void *)&mappedfile[symhdrp->cbExtOffset];
1302fe8fb19SBen Gras extstroff = symhdrp->cbSsExtOffset;
1312fe8fb19SBen Gras
1322fe8fb19SBen Gras /*
1332fe8fb19SBen Gras * Clean out any left-over information for all valid entries.
1342fe8fb19SBen Gras * Type and value are defined to be 0 if not found; historical
1352fe8fb19SBen Gras * versions cleared other and desc as well.
1362fe8fb19SBen Gras *
1372fe8fb19SBen Gras * XXX Clearing anything other than n_type and n_value violates
1382fe8fb19SBen Gras * the semantics given in the man page.
1392fe8fb19SBen Gras */
1402fe8fb19SBen Gras nent = 0;
1412fe8fb19SBen Gras for (p = list; !ISLAST(p); ++p) {
1422fe8fb19SBen Gras p->n_type = 0;
1432fe8fb19SBen Gras p->n_other = 0;
1442fe8fb19SBen Gras p->n_desc = 0;
1452fe8fb19SBen Gras p->n_value = 0;
1462fe8fb19SBen Gras ++nent;
1472fe8fb19SBen Gras }
1482fe8fb19SBen Gras
1492fe8fb19SBen Gras for (i = 0; i < nesyms; i++) {
1502fe8fb19SBen Gras for (p = list; !ISLAST(p); p++) {
1512fe8fb19SBen Gras const char *nlistname;
152*f14fb602SLionel Sambuc const char *symtabname;
1532fe8fb19SBen Gras
1542fe8fb19SBen Gras /* This may be incorrect */
1552fe8fb19SBen Gras nlistname = N_NAME(p);
1562fe8fb19SBen Gras if (*nlistname == '_')
1572fe8fb19SBen Gras nlistname++;
1582fe8fb19SBen Gras
1592fe8fb19SBen Gras symtabname =
1602fe8fb19SBen Gras &mappedfile[extstroff + esyms[i].es_strindex];
1612fe8fb19SBen Gras
1622fe8fb19SBen Gras if (!strcmp(symtabname, nlistname)) {
1632fe8fb19SBen Gras /*
1642fe8fb19SBen Gras * Translate (roughly) from ECOFF to nlist
1652fe8fb19SBen Gras */
1662fe8fb19SBen Gras p->n_value = esyms[i].es_value;
1672fe8fb19SBen Gras p->n_type = N_EXT; /* XXX */
1682fe8fb19SBen Gras p->n_desc = 0; /* XXX */
1692fe8fb19SBen Gras p->n_other = 0; /* XXX */
1702fe8fb19SBen Gras
1712fe8fb19SBen Gras if (--nent <= 0)
1722fe8fb19SBen Gras goto done;
1732fe8fb19SBen Gras break; /* into next run of outer loop */
1742fe8fb19SBen Gras }
1752fe8fb19SBen Gras }
1762fe8fb19SBen Gras }
1772fe8fb19SBen Gras
1782fe8fb19SBen Gras done:
1792fe8fb19SBen Gras rv = nent;
1802fe8fb19SBen Gras unmap:
181*f14fb602SLionel Sambuc munmap(__UNCONST(mappedfile), mappedsize);
1822fe8fb19SBen Gras out:
183*f14fb602SLionel Sambuc return rv;
1842fe8fb19SBen Gras }
1852fe8fb19SBen Gras
1862fe8fb19SBen Gras #endif /* NLIST_ECOFF */
187