1*63488d73Sthorpej /* $NetBSD: nlist.c,v 1.14 1996/09/30 18:27:02 thorpej Exp $ */ 2*63488d73Sthorpej 361f28255Scgd /*- 4c347bc09Scgd * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved. 5ec1b7778Spk * Copyright (c) 1990, 1993 6ec1b7778Spk * The Regents of the University of California. All rights reserved. 761f28255Scgd * 861f28255Scgd * Redistribution and use in source and binary forms, with or without 961f28255Scgd * modification, are permitted provided that the following conditions 1061f28255Scgd * are met: 1161f28255Scgd * 1. Redistributions of source code must retain the above copyright 1261f28255Scgd * notice, this list of conditions and the following disclaimer. 1361f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright 1461f28255Scgd * notice, this list of conditions and the following disclaimer in the 1561f28255Scgd * documentation and/or other materials provided with the distribution. 1661f28255Scgd * 3. All advertising materials mentioning features or use of this software 1761f28255Scgd * must display the following acknowledgement: 1861f28255Scgd * This product includes software developed by the University of 1961f28255Scgd * California, Berkeley and its contributors. 2061f28255Scgd * 4. Neither the name of the University nor the names of its contributors 2161f28255Scgd * may be used to endorse or promote products derived from this software 2261f28255Scgd * without specific prior written permission. 2361f28255Scgd * 2461f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2561f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2661f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2761f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2861f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2961f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3061f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3161f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3261f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3361f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3461f28255Scgd * SUCH DAMAGE. 3561f28255Scgd */ 3661f28255Scgd 3761f28255Scgd #ifndef lint 38*63488d73Sthorpej #if 0 39*63488d73Sthorpej static char sccsid[] = "from: @(#)nlist.c 8.1 (Berkeley) 6/6/93"; 40*63488d73Sthorpej #else 41*63488d73Sthorpej static char *rcsid = "$NetBSD: nlist.c,v 1.14 1996/09/30 18:27:02 thorpej Exp $"; 42*63488d73Sthorpej #endif 4361f28255Scgd #endif /* not lint */ 4461f28255Scgd 4561f28255Scgd #include <sys/param.h> 46ec1b7778Spk 4761f28255Scgd #include <a.out.h> 4861f28255Scgd #include <db.h> 49ec1b7778Spk #include <err.h> 5061f28255Scgd #include <errno.h> 51ec1b7778Spk #include <fcntl.h> 5261f28255Scgd #include <kvm.h> 53ec1b7778Spk #include <limits.h> 5461f28255Scgd #include <stdio.h> 5561f28255Scgd #include <stdlib.h> 56ec1b7778Spk #include <string.h> 57ec1b7778Spk #include <unistd.h> 58ec1b7778Spk 59ec1b7778Spk #include "extern.h" 6061f28255Scgd 61c347bc09Scgd static struct { 62c347bc09Scgd int (*knlist) __P((const char *, DB *)); 63c347bc09Scgd } knlist_fmts[] = { 64c347bc09Scgd #ifdef NLIST_AOUT 65c347bc09Scgd { create_knlist_aout }, 66c347bc09Scgd #endif 67c347bc09Scgd #ifdef NLIST_ECOFF 68c347bc09Scgd { create_knlist_ecoff }, 69c347bc09Scgd #endif 70c347bc09Scgd #ifdef NLIST_ELF32 71c347bc09Scgd { create_knlist_elf32 }, 72c347bc09Scgd #endif 73c347bc09Scgd #ifdef NLIST_ELF64 74c347bc09Scgd { create_knlist_elf64 }, 75c347bc09Scgd #endif 76c347bc09Scgd }; 776ff21e94Spk 786ff21e94Spk void 7961f28255Scgd create_knlist(name, db) 80c347bc09Scgd const char *name; 8161f28255Scgd DB *db; 8261f28255Scgd { 83c347bc09Scgd int i; 8461f28255Scgd 85c347bc09Scgd for (i = 0; i < sizeof(knlist_fmts) / sizeof(knlist_fmts[0]); i++) 86c347bc09Scgd if ((*knlist_fmts[i].knlist)(name, db) != -1) 87c347bc09Scgd return; 88c347bc09Scgd warnx("%s: file format not recognized", name); 89c347bc09Scgd punt(); 9061f28255Scgd } 9136c35601Sgwr 9236c35601Sgwr /* 9336c35601Sgwr * XXX: Using this value from machine/param.h introduces a 9436c35601Sgwr * XXX: machine dependency on this program, so /usr can not 9536c35601Sgwr * XXX: be shared between (i.e.) several m68k machines. 9636c35601Sgwr * Instead of compiling in KERNTEXTOFF or KERNBASE, try to 9736c35601Sgwr * determine the text start address from a standard symbol. 9836c35601Sgwr * For backward compatibility, use the old compiled-in way 9936c35601Sgwr * when the standard symbol name is not found. 10036c35601Sgwr */ 10136c35601Sgwr #ifndef KERNTEXTOFF 10236c35601Sgwr #define KERNTEXTOFF KERNBASE 10336c35601Sgwr #endif 10436c35601Sgwr 105c347bc09Scgd u_long 10636c35601Sgwr get_kerntext(name) 107c347bc09Scgd const char *name; 10836c35601Sgwr { 109c347bc09Scgd struct nlist nl[2]; 11036c35601Sgwr 11136c35601Sgwr bzero((caddr_t)nl, sizeof(nl)); 112c347bc09Scgd nl[0].n_un.n_name = "_kernel_text"; 11336c35601Sgwr 11436c35601Sgwr if (nlist(name, nl) != 0) 11536c35601Sgwr return (KERNTEXTOFF); 11636c35601Sgwr 11736c35601Sgwr return (nl[0].n_value); 11836c35601Sgwr } 119