2005-06-12 09:21:25 +04:00
|
|
|
/* $NetBSD: nlist_elf32.c,v 1.28 2005/06/12 05:21:27 lukem Exp $ */
|
1996-09-28 02:23:03 +04:00
|
|
|
|
|
|
|
/*
|
2000-06-14 21:24:02 +04:00
|
|
|
* Copyright (c) 1996 Christopher G. Demetriou
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
1996-09-28 02:23:03 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
2000-06-14 21:24:02 +04:00
|
|
|
* This product includes software developed for the
|
2003-07-26 23:24:24 +04:00
|
|
|
* NetBSD Project. See http://www.NetBSD.org/ for
|
2000-06-14 21:24:02 +04:00
|
|
|
* information about NetBSD.
|
1996-10-01 04:32:52 +04:00
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
2000-06-14 21:24:02 +04:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
1996-10-01 04:32:52 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2000-06-14 21:24:02 +04:00
|
|
|
*
|
|
|
|
* <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
|
1996-09-28 02:23:03 +04:00
|
|
|
*/
|
|
|
|
|
2005-06-12 09:21:25 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
|
|
|
__RCSID("$NetBSD: nlist_elf32.c,v 1.28 2005/06/12 05:21:27 lukem Exp $");
|
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
1996-09-28 02:23:03 +04:00
|
|
|
/* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
|
|
|
|
#ifndef ELFSIZE
|
|
|
|
#define ELFSIZE 32
|
|
|
|
#endif
|
|
|
|
|
1998-10-14 15:56:28 +04:00
|
|
|
#include "namespace.h"
|
1996-09-28 02:23:03 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
2003-05-11 16:47:42 +04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/ksyms.h>
|
1996-09-28 02:23:03 +04:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <assert.h>
|
1996-09-28 02:23:03 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <a.out.h> /* for 'struct nlist' declaration */
|
|
|
|
|
|
|
|
#include "nlist_private.h"
|
|
|
|
#if defined(NLIST_ELF32) || defined(NLIST_ELF64)
|
|
|
|
#include <sys/exec_elf.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
|
|
|
|
(defined(NLIST_ELF64) && (ELFSIZE == 64))
|
|
|
|
|
1998-11-13 13:26:19 +03:00
|
|
|
/* No need to check for off < 0 because it is unsigned */
|
|
|
|
#define check(off, size) (off + size > mappedsize)
|
|
|
|
#define BAD goto out
|
|
|
|
#define BADUNMAP goto unmap
|
1996-10-01 04:32:52 +04:00
|
|
|
|
1996-09-28 02:23:03 +04:00
|
|
|
int
|
1996-10-01 03:51:05 +04:00
|
|
|
ELFNAMEEND(__fdnlist)(fd, list)
|
1998-02-03 21:23:37 +03:00
|
|
|
int fd;
|
|
|
|
struct nlist *list;
|
1996-09-28 02:23:03 +04:00
|
|
|
{
|
1996-10-01 03:51:05 +04:00
|
|
|
struct stat st;
|
1996-10-01 04:32:52 +04:00
|
|
|
struct nlist *p;
|
|
|
|
char *mappedfile, *strtab;
|
|
|
|
size_t mappedsize;
|
2003-05-11 16:47:42 +04:00
|
|
|
Elf_Ehdr *ehdrp, ehdr;
|
1996-10-01 04:32:52 +04:00
|
|
|
Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
|
|
|
|
Elf_Sym *symp;
|
1996-10-03 08:55:31 +04:00
|
|
|
Elf_Off shdr_off;
|
|
|
|
Elf_Word shdr_size;
|
1996-10-01 04:32:52 +04:00
|
|
|
#if (ELFSIZE == 32)
|
|
|
|
Elf32_Half nshdr;
|
|
|
|
#elif (ELFSIZE == 64)
|
|
|
|
Elf64_Half nshdr;
|
|
|
|
#endif
|
1998-11-13 13:26:19 +03:00
|
|
|
size_t i, nsyms;
|
1996-10-01 04:32:52 +04:00
|
|
|
int rv, nent;
|
1996-10-01 03:51:05 +04:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(fd != -1);
|
|
|
|
_DIAGASSERT(list != NULL);
|
|
|
|
|
1996-10-01 04:32:52 +04:00
|
|
|
rv = -1;
|
1996-09-28 02:23:03 +04:00
|
|
|
|
1997-07-17 04:54:27 +04:00
|
|
|
symshdrp = symstrshdrp = NULL;
|
|
|
|
|
1996-10-01 04:32:52 +04:00
|
|
|
/*
|
1996-10-01 17:35:34 +04:00
|
|
|
* If we can't fstat() the file, something bad is going on.
|
1996-10-01 04:32:52 +04:00
|
|
|
*/
|
|
|
|
if (fstat(fd, &st) < 0)
|
|
|
|
BAD;
|
1996-09-28 02:23:03 +04:00
|
|
|
|
1996-10-01 04:32:52 +04:00
|
|
|
/*
|
|
|
|
* Map the file in its entirety.
|
|
|
|
*/
|
1996-10-01 17:35:34 +04:00
|
|
|
if (st.st_size > SIZE_T_MAX) {
|
|
|
|
errno = EFBIG;
|
|
|
|
BAD;
|
|
|
|
}
|
2003-05-11 16:47:42 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the elf header of the file.
|
|
|
|
*/
|
2003-05-12 00:56:51 +04:00
|
|
|
if ((ssize_t)(i = pread(fd, &ehdr, sizeof(Elf_Ehdr), (off_t)0)) == -1)
|
2003-05-11 16:47:42 +04:00
|
|
|
BAD;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that the elf header is correct.
|
|
|
|
*/
|
|
|
|
if (i != sizeof(Elf_Ehdr))
|
|
|
|
BAD;
|
|
|
|
if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
|
|
|
|
ehdr.e_ident[EI_CLASS] != ELFCLASS)
|
|
|
|
BAD;
|
|
|
|
|
|
|
|
switch (ehdr.e_machine) {
|
|
|
|
ELFDEFNNAME(MACHDEP_ID_CASES)
|
|
|
|
|
|
|
|
default:
|
2005-06-01 09:24:54 +04:00
|
|
|
BAD;
|
2003-05-11 16:47:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISCHR(st.st_mode)) {
|
|
|
|
const char *nlistname;
|
|
|
|
struct ksyms_gsymbol kg;
|
|
|
|
Elf_Sym sym;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Character device; assume /dev/ksyms.
|
|
|
|
*/
|
|
|
|
nent = 0;
|
|
|
|
for (p = list; !ISLAST(p); ++p) {
|
|
|
|
|
|
|
|
p->n_other = 0;
|
|
|
|
p->n_desc = 0;
|
|
|
|
nlistname = p->n_un.n_name;
|
|
|
|
if (*nlistname == '_')
|
|
|
|
nlistname++;
|
|
|
|
|
|
|
|
kg.kg_name = nlistname;
|
|
|
|
kg.kg_sym = &sym;
|
|
|
|
if (ioctl(fd, KIOCGSYMBOL, &kg) == 0) {
|
|
|
|
p->n_value = sym.st_value;
|
2004-12-27 23:07:09 +03:00
|
|
|
switch (ELF_ST_TYPE(sym.st_info)) {
|
2003-05-11 16:47:42 +04:00
|
|
|
case STT_NOTYPE:
|
|
|
|
p->n_type = N_UNDF;
|
|
|
|
break;
|
|
|
|
case STT_OBJECT:
|
|
|
|
p->n_type = N_DATA;
|
|
|
|
break;
|
|
|
|
case STT_FUNC:
|
|
|
|
p->n_type = N_TEXT;
|
|
|
|
break;
|
|
|
|
case STT_FILE:
|
|
|
|
p->n_type = N_FN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
p->n_type = 0;
|
|
|
|
/* catch other enumerations for gcc */
|
|
|
|
break;
|
|
|
|
}
|
2004-12-28 03:08:08 +03:00
|
|
|
if (ELF_ST_BIND(sym.st_info) != STB_LOCAL)
|
2003-05-11 16:47:42 +04:00
|
|
|
p->n_type |= N_EXT;
|
|
|
|
} else {
|
|
|
|
nent++;
|
|
|
|
p->n_value = 0;
|
|
|
|
p->n_type = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nent;
|
|
|
|
}
|
|
|
|
|
1998-11-13 13:26:19 +03:00
|
|
|
mappedsize = (size_t)st.st_size;
|
1999-06-18 01:09:47 +04:00
|
|
|
mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
|
1998-11-13 13:26:19 +03:00
|
|
|
fd, (off_t)0);
|
1996-10-01 04:32:52 +04:00
|
|
|
if (mappedfile == (char *)-1)
|
|
|
|
BAD;
|
1996-09-28 02:23:03 +04:00
|
|
|
|
1996-10-01 04:32:52 +04:00
|
|
|
/*
|
|
|
|
* Make sure we can access the executable's header
|
|
|
|
* directly, and make sure the recognize the executable
|
|
|
|
* as an ELF binary.
|
|
|
|
*/
|
|
|
|
if (check(0, sizeof *ehdrp))
|
|
|
|
BADUNMAP;
|
1998-11-13 13:26:19 +03:00
|
|
|
ehdrp = (Elf_Ehdr *)(void *)&mappedfile[0];
|
1996-09-28 02:23:03 +04:00
|
|
|
|
1996-10-01 03:51:05 +04:00
|
|
|
/*
|
1996-10-01 04:32:52 +04:00
|
|
|
* Find the symbol list and string table.
|
1996-10-01 03:51:05 +04:00
|
|
|
*/
|
1996-10-01 04:32:52 +04:00
|
|
|
nshdr = ehdrp->e_shnum;
|
|
|
|
shdr_off = ehdrp->e_shoff;
|
|
|
|
shdr_size = ehdrp->e_shentsize * nshdr;
|
|
|
|
|
|
|
|
if (check(shdr_off, shdr_size) ||
|
|
|
|
(sizeof *shdrp != ehdrp->e_shentsize))
|
|
|
|
BADUNMAP;
|
1998-11-13 13:26:19 +03:00
|
|
|
shdrp = (Elf_Shdr *)(void *)&mappedfile[shdr_off];
|
1996-10-01 04:32:52 +04:00
|
|
|
|
|
|
|
for (i = 0; i < nshdr; i++) {
|
1999-10-25 17:55:06 +04:00
|
|
|
if (shdrp[i].sh_type == SHT_SYMTAB) {
|
1996-10-01 04:32:52 +04:00
|
|
|
symshdrp = &shdrp[i];
|
|
|
|
symstrshdrp = &shdrp[shdrp[i].sh_link];
|
1996-10-01 03:51:05 +04:00
|
|
|
}
|
|
|
|
}
|
1996-09-28 02:23:03 +04:00
|
|
|
|
1996-10-01 04:32:52 +04:00
|
|
|
/* Make sure we're not stripped. */
|
1997-07-17 04:54:27 +04:00
|
|
|
if (symshdrp == NULL || symshdrp->sh_offset == 0)
|
1996-10-01 04:32:52 +04:00
|
|
|
BADUNMAP;
|
|
|
|
|
|
|
|
/* Make sure the symbols and strings are safely mapped. */
|
|
|
|
if (check(symshdrp->sh_offset, symshdrp->sh_size))
|
|
|
|
BADUNMAP;
|
|
|
|
if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
|
|
|
|
BADUNMAP;
|
|
|
|
|
1998-11-13 13:26:19 +03:00
|
|
|
symp = (Elf_Sym *)(void *)&mappedfile[symshdrp->sh_offset];
|
1996-10-01 04:32:52 +04:00
|
|
|
nsyms = symshdrp->sh_size / sizeof(*symp);
|
|
|
|
strtab = &mappedfile[symstrshdrp->sh_offset];
|
1996-09-28 02:23:03 +04:00
|
|
|
|
1996-10-01 03:51:05 +04:00
|
|
|
/*
|
1996-10-01 17:35:34 +04:00
|
|
|
* Clean out any left-over information for all valid entries.
|
|
|
|
* Type and value are defined to be 0 if not found; historical
|
1996-10-01 04:32:52 +04:00
|
|
|
* versions cleared other and desc as well.
|
1996-10-01 03:51:05 +04:00
|
|
|
*
|
1996-10-01 17:35:34 +04:00
|
|
|
* XXX Clearing anything other than n_type and n_value violates
|
1996-10-01 03:51:05 +04:00
|
|
|
* the semantics given in the man page.
|
|
|
|
*/
|
|
|
|
nent = 0;
|
|
|
|
for (p = list; !ISLAST(p); ++p) {
|
|
|
|
p->n_type = 0;
|
|
|
|
p->n_other = 0;
|
|
|
|
p->n_desc = 0;
|
|
|
|
p->n_value = 0;
|
|
|
|
++nent;
|
|
|
|
}
|
1996-09-28 02:23:03 +04:00
|
|
|
|
1996-10-01 04:32:52 +04:00
|
|
|
for (i = 0; i < nsyms; i++) {
|
|
|
|
for (p = list; !ISLAST(p); ++p) {
|
1998-07-26 21:53:10 +04:00
|
|
|
const char *nlistname;
|
1996-10-01 04:32:52 +04:00
|
|
|
char *symtabname;
|
|
|
|
|
|
|
|
/* This may be incorrect */
|
|
|
|
nlistname = p->n_un.n_name;
|
|
|
|
if (*nlistname == '_')
|
|
|
|
nlistname++;
|
|
|
|
|
|
|
|
symtabname = &strtab[symp[i].st_name];
|
|
|
|
|
|
|
|
if (!strcmp(symtabname, nlistname)) {
|
1996-10-01 17:35:34 +04:00
|
|
|
/*
|
|
|
|
* Translate (roughly) from ELF to nlist
|
|
|
|
*/
|
1996-10-01 04:32:52 +04:00
|
|
|
p->n_value = symp[i].st_value;
|
2004-12-28 03:08:08 +03:00
|
|
|
switch (ELF_ST_TYPE(symp[i].st_info)) {
|
1999-10-25 17:55:06 +04:00
|
|
|
case STT_NOTYPE:
|
1996-10-01 04:32:52 +04:00
|
|
|
p->n_type = N_UNDF;
|
|
|
|
break;
|
1999-10-25 17:55:06 +04:00
|
|
|
case STT_OBJECT:
|
1996-10-01 04:32:52 +04:00
|
|
|
p->n_type = N_DATA;
|
|
|
|
break;
|
1999-10-25 17:55:06 +04:00
|
|
|
case STT_FUNC:
|
1996-10-01 04:32:52 +04:00
|
|
|
p->n_type = N_TEXT;
|
|
|
|
break;
|
1999-10-25 17:55:06 +04:00
|
|
|
case STT_FILE:
|
1996-10-01 04:32:52 +04:00
|
|
|
p->n_type = N_FN;
|
|
|
|
break;
|
1997-07-17 04:54:27 +04:00
|
|
|
default:
|
|
|
|
/* catch other enumerations for gcc */
|
|
|
|
break;
|
1996-10-01 03:51:05 +04:00
|
|
|
}
|
2004-12-28 03:08:08 +03:00
|
|
|
if (ELF_ST_BIND(symp[i].st_info) != STB_LOCAL)
|
1996-10-01 04:32:52 +04:00
|
|
|
p->n_type |= N_EXT;
|
|
|
|
p->n_desc = 0; /* XXX */
|
|
|
|
p->n_other = 0; /* XXX */
|
|
|
|
|
|
|
|
if (--nent <= 0)
|
|
|
|
goto done;
|
|
|
|
break; /* into next run of outer loop */
|
1996-10-01 03:51:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-09-28 02:23:03 +04:00
|
|
|
|
1996-10-01 04:32:52 +04:00
|
|
|
done:
|
|
|
|
rv = nent;
|
|
|
|
unmap:
|
|
|
|
munmap(mappedfile, mappedsize);
|
|
|
|
out:
|
|
|
|
return (rv);
|
1996-09-28 02:23:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|