1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Hans Huebner.
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-07-31 06:53:36 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1989, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-07-31 06:53:36 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
static char sccsid[] = "@(#)nm.c 8.1 (Berkeley) 6/6/93";
|
1997-07-31 06:53:36 +00:00
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
1999-08-28 01:08:13 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <a.out.h>
|
|
|
|
#include <ar.h>
|
1997-07-31 06:53:36 +00:00
|
|
|
#include <ctype.h>
|
1994-12-28 00:47:24 +00:00
|
|
|
#include <dirent.h>
|
1997-07-31 06:53:36 +00:00
|
|
|
#include <err.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <ranlib.h>
|
1997-07-31 06:53:36 +00:00
|
|
|
#include <stab.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-07-31 06:53:36 +00:00
|
|
|
#include <unistd.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int ignore_bad_archive_entries = 1;
|
|
|
|
int print_only_external_symbols;
|
|
|
|
int print_only_undefined_symbols;
|
|
|
|
int print_all_symbols;
|
|
|
|
int print_file_each_line;
|
1996-09-24 09:59:43 +00:00
|
|
|
int print_weak_symbols;
|
1994-05-27 12:33:43 +00:00
|
|
|
int fcount;
|
|
|
|
|
1995-12-19 08:12:07 +00:00
|
|
|
int rev, table;
|
1994-05-27 12:33:43 +00:00
|
|
|
int fname(), rname(), value();
|
|
|
|
int (*sfunc)() = fname;
|
|
|
|
|
|
|
|
/* some macros for symbol type (nlist.n_type) handling */
|
|
|
|
#define IS_DEBUGGER_SYMBOL(x) ((x) & N_STAB)
|
|
|
|
#define IS_EXTERNAL(x) ((x) & N_EXT)
|
|
|
|
#define SYMBOL_TYPE(x) ((x) & (N_TYPE | N_STAB))
|
1996-09-24 09:59:43 +00:00
|
|
|
#define SYMBOL_BIND(x) (((x) >> 4) & 0xf)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1997-07-31 06:53:36 +00:00
|
|
|
static void usage __P(( void ));
|
1996-07-12 19:08:36 +00:00
|
|
|
int process_file __P(( char * ));
|
|
|
|
int show_archive __P(( char *, FILE * ));
|
|
|
|
int show_objfile __P(( char *, FILE * ));
|
|
|
|
void print_symbol __P(( char *, struct nlist * ));
|
1996-09-24 09:59:43 +00:00
|
|
|
char typeletter __P((u_char));
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* main()
|
|
|
|
* parse command line, execute process_file() for each file
|
|
|
|
* specified on the command line.
|
|
|
|
*/
|
1996-07-12 19:08:36 +00:00
|
|
|
int
|
1994-05-27 12:33:43 +00:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
int ch, errors;
|
|
|
|
|
1997-03-29 04:34:07 +00:00
|
|
|
while ((ch = getopt(argc, argv, "agnoprtuwW")) != -1) {
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
print_all_symbols = 1;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
print_only_external_symbols = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
sfunc = value;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
print_file_each_line = 1;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
sfunc = NULL;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rev = 1;
|
|
|
|
break;
|
1995-12-19 08:12:07 +00:00
|
|
|
case 't':
|
|
|
|
table = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'u':
|
|
|
|
print_only_undefined_symbols = 1;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
ignore_bad_archive_entries = 0;
|
|
|
|
break;
|
1996-09-24 09:59:43 +00:00
|
|
|
case 'W':
|
|
|
|
print_weak_symbols = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fcount = argc - optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (rev && sfunc == fname)
|
|
|
|
sfunc = rname;
|
|
|
|
|
|
|
|
if (!fcount)
|
|
|
|
errors = process_file("a.out");
|
|
|
|
else {
|
|
|
|
errors = 0;
|
|
|
|
do {
|
|
|
|
errors |= process_file(*argv);
|
|
|
|
} while (*++argv);
|
|
|
|
}
|
|
|
|
exit(errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process_file()
|
|
|
|
* show symbols in the file given as an argument. Accepts archive and
|
|
|
|
* object files as input.
|
|
|
|
*/
|
1996-07-12 19:08:36 +00:00
|
|
|
int
|
1994-05-27 12:33:43 +00:00
|
|
|
process_file(fname)
|
|
|
|
char *fname;
|
|
|
|
{
|
|
|
|
struct exec exec_head;
|
|
|
|
FILE *fp;
|
|
|
|
int retval;
|
|
|
|
char magic[SARMAG];
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!(fp = fopen(fname, "r"))) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("cannot read %s", fname);
|
1994-05-27 12:33:43 +00:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
1995-12-19 08:12:07 +00:00
|
|
|
if (fcount > 1 && !table)
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)printf("\n%s:\n", fname);
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* first check whether this is an object file - read a object
|
|
|
|
* header, and skip back to the beginning
|
|
|
|
*/
|
|
|
|
if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("%s: bad format", fname);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)fclose(fp);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
rewind(fp);
|
|
|
|
|
|
|
|
/* this could be an archive */
|
|
|
|
if (N_BADMAG(exec_head)) {
|
|
|
|
if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
|
|
|
|
strncmp(magic, ARMAG, SARMAG)) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("%s: not object file or archive", fname);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)fclose(fp);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
retval = show_archive(fname, fp);
|
|
|
|
} else
|
|
|
|
retval = show_objfile(fname, fp);
|
|
|
|
(void)fclose(fp);
|
|
|
|
return(retval);
|
|
|
|
}
|
|
|
|
|
1994-12-28 00:47:24 +00:00
|
|
|
/* scat: concatenate strings, returning new concatenation point
|
|
|
|
* and permitting overlap.
|
|
|
|
*/
|
|
|
|
static char *scat(char *dest, char *src)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
int l1 = strlen(dest), l2 = strlen(src);
|
|
|
|
|
|
|
|
memmove(dest + l1, src, l2);
|
|
|
|
end = dest + l1 + l2;
|
|
|
|
*end = 0;
|
|
|
|
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* show_archive()
|
|
|
|
* show symbols in the given archive file
|
|
|
|
*/
|
1996-07-12 19:08:36 +00:00
|
|
|
int
|
1994-05-27 12:33:43 +00:00
|
|
|
show_archive(fname, fp)
|
|
|
|
char *fname;
|
|
|
|
FILE *fp;
|
|
|
|
{
|
|
|
|
struct ar_hdr ar_head;
|
|
|
|
struct exec exec_head;
|
|
|
|
int i, rval;
|
|
|
|
long last_ar_off;
|
1994-12-28 00:47:24 +00:00
|
|
|
char *p, *name, *ar_name;
|
1994-12-28 13:19:39 +00:00
|
|
|
int extra = strlen(fname) + 3;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-07-24 14:08:02 +00:00
|
|
|
if ((name = malloc(MAXNAMLEN + extra)) == NULL)
|
|
|
|
err(1, NULL);
|
1994-12-28 13:19:39 +00:00
|
|
|
ar_name = name + extra;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
rval = 0;
|
|
|
|
|
|
|
|
/* while there are more entries in the archive */
|
|
|
|
while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
|
|
|
|
/* bad archive entry - stop processing this archive */
|
|
|
|
if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("%s: bad format archive header", fname);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)free(name);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remember start position of current archive object */
|
|
|
|
last_ar_off = ftell(fp);
|
|
|
|
|
|
|
|
/* skip ranlib entries */
|
1994-12-28 00:47:24 +00:00
|
|
|
if (!bcmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
|
1994-05-27 12:33:43 +00:00
|
|
|
goto skip;
|
|
|
|
|
1994-12-28 00:47:24 +00:00
|
|
|
/* handle long names. If one is present, read it in at the
|
|
|
|
* end of the "name" buffer.
|
|
|
|
*/
|
|
|
|
if (!bcmp(ar_head.ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1))
|
|
|
|
{
|
|
|
|
size_t len = atoi(ar_head.ar_name + sizeof(AR_EFMT1) - 1);
|
|
|
|
|
|
|
|
if (len <= 0 || len > MAXNAMLEN)
|
|
|
|
{
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("illegal length for format 1 long name");
|
1994-12-28 00:47:24 +00:00
|
|
|
goto skip;
|
|
|
|
}
|
1994-12-28 13:19:39 +00:00
|
|
|
if (fread(ar_name, 1, len, fp) != len)
|
1994-12-28 00:47:24 +00:00
|
|
|
{
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("EOF reading format 1 long name");
|
1994-12-28 00:47:24 +00:00
|
|
|
(void)free(name);
|
|
|
|
return(1);
|
|
|
|
}
|
1994-12-28 13:19:39 +00:00
|
|
|
ar_name[len] = 0;
|
1994-12-28 00:47:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1994-12-28 13:19:39 +00:00
|
|
|
p = ar_head.ar_name;
|
1994-12-28 14:11:21 +00:00
|
|
|
for (i = 0; i < sizeof(ar_head.ar_name) && p[i] && p[i] != ' '; i++)
|
1994-12-28 13:19:39 +00:00
|
|
|
ar_name[i] = p[i];
|
|
|
|
ar_name[i] = 0;
|
1994-12-28 00:47:24 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* construct a name of the form "archive.a:obj.o:" for the
|
|
|
|
* current archive entry if the object name is to be printed
|
|
|
|
* on each output line
|
|
|
|
*/
|
|
|
|
p = name;
|
1994-12-28 00:47:24 +00:00
|
|
|
*p = 0;
|
1995-12-19 08:12:07 +00:00
|
|
|
if (print_file_each_line && !table)
|
1994-12-28 00:47:24 +00:00
|
|
|
{
|
|
|
|
p = scat(p, fname);
|
|
|
|
p = scat(p, ":");
|
|
|
|
}
|
|
|
|
p = scat(p, ar_name);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/* get and check current object's header */
|
1994-12-28 00:47:24 +00:00
|
|
|
if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("%s: premature EOF", name);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)free(name);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (N_BADMAG(exec_head)) {
|
|
|
|
if (!ignore_bad_archive_entries) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("%s: bad format", name);
|
1994-05-27 12:33:43 +00:00
|
|
|
rval = 1;
|
|
|
|
}
|
|
|
|
} else {
|
1998-07-06 21:45:12 +00:00
|
|
|
(void)fseek(fp, -(long)sizeof(exec_head),
|
1994-05-27 12:33:43 +00:00
|
|
|
SEEK_CUR);
|
1995-12-19 08:12:07 +00:00
|
|
|
if (!print_file_each_line && !table)
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)printf("\n%s:\n", name);
|
|
|
|
rval |= show_objfile(name, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* skip to next archive object - it starts at the next
|
|
|
|
* even byte boundary
|
|
|
|
*/
|
|
|
|
#define even(x) (((x) + 1) & ~1)
|
|
|
|
skip: if (fseek(fp, last_ar_off + even(atol(ar_head.ar_size)),
|
|
|
|
SEEK_SET)) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warn("%s", fname);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)free(name);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void)free(name);
|
|
|
|
return(rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* show_objfile()
|
|
|
|
* show symbols from the object file pointed to by fp. The current
|
|
|
|
* file pointer for fp is expected to be at the beginning of an a.out
|
|
|
|
* header.
|
|
|
|
*/
|
1996-07-12 19:08:36 +00:00
|
|
|
int
|
1994-05-27 12:33:43 +00:00
|
|
|
show_objfile(objname, fp)
|
|
|
|
char *objname;
|
|
|
|
FILE *fp;
|
|
|
|
{
|
|
|
|
register struct nlist *names, *np;
|
|
|
|
register int i, nnames, nrawnames;
|
|
|
|
struct exec head;
|
1998-07-06 21:45:12 +00:00
|
|
|
int32_t stabsize;
|
1994-05-27 12:33:43 +00:00
|
|
|
char *stab;
|
|
|
|
|
|
|
|
/* read a.out header */
|
|
|
|
if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("%s: cannot read header", objname);
|
1994-05-27 12:33:43 +00:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* skip back to the header - the N_-macros return values relative
|
|
|
|
* to the beginning of the a.out header
|
|
|
|
*/
|
1998-07-06 21:45:12 +00:00
|
|
|
if (fseek(fp, -(long)sizeof(head), SEEK_CUR)) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warn("%s", objname);
|
1994-05-27 12:33:43 +00:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stop if this is no valid object file */
|
|
|
|
if (N_BADMAG(head)) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("%s: bad format", objname);
|
1994-05-27 12:33:43 +00:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stop if the object file contains no symbol table */
|
|
|
|
if (!head.a_syms) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("%s: no name list", objname);
|
1994-05-27 12:33:43 +00:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fseek(fp, (long)N_SYMOFF(head), SEEK_CUR)) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warn("%s", objname);
|
1994-05-27 12:33:43 +00:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get memory for the symbol table */
|
2001-07-24 14:08:02 +00:00
|
|
|
if ((names = malloc((size_t)head.a_syms)) == NULL)
|
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
nrawnames = head.a_syms / sizeof(*names);
|
|
|
|
if (fread((char *)names, (size_t)head.a_syms, (size_t)1, fp) != 1) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("%s: cannot read symbol table", objname);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)free((char *)names);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Following the symbol table comes the string table. The first
|
|
|
|
* 4-byte-integer gives the total size of the string table
|
|
|
|
* _including_ the size specification itself.
|
|
|
|
*/
|
|
|
|
if (fread((char *)&stabsize, sizeof(stabsize), (size_t)1, fp) != 1) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("%s: cannot read stab size", objname);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)free((char *)names);
|
|
|
|
return(1);
|
|
|
|
}
|
2001-07-24 14:08:02 +00:00
|
|
|
if ((stab = malloc((size_t)stabsize)) == NULL)
|
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* read the string table offset by 4 - all indices into the string
|
|
|
|
* table include the size specification.
|
|
|
|
*/
|
|
|
|
stabsize -= 4; /* we already have the size */
|
|
|
|
if (fread(stab + 4, (size_t)stabsize, (size_t)1, fp) != 1) {
|
1997-07-31 06:53:36 +00:00
|
|
|
warnx("%s: stab truncated..", objname);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)free((char *)names);
|
|
|
|
(void)free(stab);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fix up the symbol table and filter out unwanted entries
|
|
|
|
*
|
|
|
|
* common symbols are characterized by a n_type of N_UNDF and a
|
|
|
|
* non-zero n_value -- change n_type to N_COMM for all such
|
|
|
|
* symbols to make life easier later.
|
|
|
|
*
|
|
|
|
* filter out all entries which we don't want to print anyway
|
|
|
|
*/
|
|
|
|
for (np = names, i = nnames = 0; i < nrawnames; np++, i++) {
|
|
|
|
if (SYMBOL_TYPE(np->n_type) == N_UNDF && np->n_value)
|
|
|
|
np->n_type = N_COMM | (np->n_type & N_EXT);
|
|
|
|
if (!print_all_symbols && IS_DEBUGGER_SYMBOL(np->n_type))
|
|
|
|
continue;
|
|
|
|
if (print_only_external_symbols && !IS_EXTERNAL(np->n_type))
|
|
|
|
continue;
|
|
|
|
if (print_only_undefined_symbols &&
|
|
|
|
SYMBOL_TYPE(np->n_type) != N_UNDF)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make n_un.n_name a character pointer by adding the string
|
|
|
|
* table's base to n_un.n_strx
|
|
|
|
*
|
|
|
|
* don't mess with zero offsets
|
|
|
|
*/
|
|
|
|
if (np->n_un.n_strx)
|
|
|
|
np->n_un.n_name = stab + np->n_un.n_strx;
|
|
|
|
else
|
|
|
|
np->n_un.n_name = "";
|
|
|
|
names[nnames++] = *np;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sort the symbol table if applicable */
|
|
|
|
if (sfunc)
|
|
|
|
qsort((char *)names, (size_t)nnames, sizeof(*names), sfunc);
|
|
|
|
|
|
|
|
/* print out symbols */
|
|
|
|
for (np = names, i = 0; i < nnames; np++, i++)
|
|
|
|
print_symbol(objname, np);
|
|
|
|
|
|
|
|
(void)free((char *)names);
|
|
|
|
(void)free(stab);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* print_symbol()
|
|
|
|
* show one symbol
|
|
|
|
*/
|
1996-07-12 19:08:36 +00:00
|
|
|
void
|
1994-05-27 12:33:43 +00:00
|
|
|
print_symbol(objname, sym)
|
|
|
|
char *objname;
|
|
|
|
register struct nlist *sym;
|
|
|
|
{
|
1996-09-24 09:59:43 +00:00
|
|
|
char *typestring();
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1995-12-19 08:12:07 +00:00
|
|
|
if (table) {
|
|
|
|
printf("%s|", objname);
|
|
|
|
if (SYMBOL_TYPE(sym->n_type) != N_UNDF)
|
1998-07-06 21:45:12 +00:00
|
|
|
(void)printf("%08lx", (u_long)sym->n_value);
|
1995-12-19 08:12:07 +00:00
|
|
|
(void)printf("|");
|
|
|
|
if (IS_DEBUGGER_SYMBOL(sym->n_type))
|
|
|
|
(void)printf("-|%02x %04x %5s|", sym->n_other,
|
|
|
|
sym->n_desc&0xffff, typestring(sym->n_type));
|
1996-09-24 09:59:43 +00:00
|
|
|
else {
|
|
|
|
putchar(typeletter(sym->n_type));
|
|
|
|
if (print_weak_symbols && SYMBOL_BIND(sym->n_other)== 2)
|
|
|
|
putchar('W');
|
|
|
|
putchar('|');
|
|
|
|
}
|
1995-12-19 08:12:07 +00:00
|
|
|
|
|
|
|
/* print the symbol's name */
|
|
|
|
(void)printf("%s\n",sym->n_un.n_name);
|
|
|
|
return;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
if (print_file_each_line)
|
|
|
|
(void)printf("%s:", objname);
|
|
|
|
|
|
|
|
/*
|
2001-02-06 11:21:58 +00:00
|
|
|
* handle undefined-only format separately (no space is
|
1994-05-27 12:33:43 +00:00
|
|
|
* left for symbol values, no type field is printed)
|
|
|
|
*/
|
|
|
|
if (print_only_undefined_symbols) {
|
|
|
|
(void)puts(sym->n_un.n_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print symbol's value */
|
|
|
|
if (SYMBOL_TYPE(sym->n_type) == N_UNDF)
|
|
|
|
(void)printf(" ");
|
|
|
|
else
|
1998-07-06 21:45:12 +00:00
|
|
|
(void)printf("%08lx", (u_long)sym->n_value);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/* print type information */
|
|
|
|
if (IS_DEBUGGER_SYMBOL(sym->n_type))
|
|
|
|
(void)printf(" - %02x %04x %5s ", sym->n_other,
|
|
|
|
sym->n_desc&0xffff, typestring(sym->n_type));
|
1996-09-24 09:59:43 +00:00
|
|
|
else {
|
|
|
|
putchar(' ');
|
|
|
|
putchar(typeletter(sym->n_type));
|
|
|
|
if (print_weak_symbols) {
|
|
|
|
if (SYMBOL_BIND(sym->n_other) == 2)
|
|
|
|
putchar('W');
|
|
|
|
else
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
putchar(' ');
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/* print the symbol's name */
|
|
|
|
(void)puts(sym->n_un.n_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* typestring()
|
|
|
|
* return the a description string for an STAB entry
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
typestring(type)
|
|
|
|
register u_char type;
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case N_BCOMM:
|
|
|
|
return("BCOMM");
|
|
|
|
case N_ECOML:
|
|
|
|
return("ECOML");
|
|
|
|
case N_ECOMM:
|
|
|
|
return("ECOMM");
|
|
|
|
case N_ENTRY:
|
|
|
|
return("ENTRY");
|
|
|
|
case N_FNAME:
|
|
|
|
return("FNAME");
|
|
|
|
case N_FUN:
|
|
|
|
return("FUN");
|
|
|
|
case N_GSYM:
|
|
|
|
return("GSYM");
|
|
|
|
case N_LBRAC:
|
|
|
|
return("LBRAC");
|
|
|
|
case N_LCSYM:
|
|
|
|
return("LCSYM");
|
|
|
|
case N_LENG:
|
|
|
|
return("LENG");
|
|
|
|
case N_LSYM:
|
|
|
|
return("LSYM");
|
|
|
|
case N_PC:
|
|
|
|
return("PC");
|
|
|
|
case N_PSYM:
|
|
|
|
return("PSYM");
|
|
|
|
case N_RBRAC:
|
|
|
|
return("RBRAC");
|
|
|
|
case N_RSYM:
|
|
|
|
return("RSYM");
|
|
|
|
case N_SLINE:
|
|
|
|
return("SLINE");
|
|
|
|
case N_SO:
|
|
|
|
return("SO");
|
|
|
|
case N_SOL:
|
|
|
|
return("SOL");
|
|
|
|
case N_SSYM:
|
|
|
|
return("SSYM");
|
|
|
|
case N_STSYM:
|
|
|
|
return("STSYM");
|
|
|
|
}
|
|
|
|
return("???");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* typeletter()
|
|
|
|
* return a description letter for the given basic type code of an
|
|
|
|
* symbol table entry. The return value will be upper case for
|
|
|
|
* external, lower case for internal symbols.
|
|
|
|
*/
|
|
|
|
char
|
|
|
|
typeletter(type)
|
|
|
|
u_char type;
|
|
|
|
{
|
|
|
|
switch(SYMBOL_TYPE(type)) {
|
|
|
|
case N_ABS:
|
|
|
|
return(IS_EXTERNAL(type) ? 'A' : 'a');
|
|
|
|
case N_BSS:
|
|
|
|
return(IS_EXTERNAL(type) ? 'B' : 'b');
|
|
|
|
case N_COMM:
|
|
|
|
return(IS_EXTERNAL(type) ? 'C' : 'c');
|
|
|
|
case N_DATA:
|
|
|
|
return(IS_EXTERNAL(type) ? 'D' : 'd');
|
|
|
|
case N_FN:
|
1996-09-24 15:56:26 +00:00
|
|
|
/* This one is overloaded. EXT = Filename, INT = warn refs */
|
|
|
|
return(IS_EXTERNAL(type) ? 'F' : 'w');
|
1996-09-24 09:59:43 +00:00
|
|
|
case N_INDR:
|
|
|
|
return(IS_EXTERNAL(type) ? 'I' : 'i');
|
1994-05-27 12:33:43 +00:00
|
|
|
case N_TEXT:
|
|
|
|
return(IS_EXTERNAL(type) ? 'T' : 't');
|
|
|
|
case N_UNDF:
|
|
|
|
return(IS_EXTERNAL(type) ? 'U' : 'u');
|
|
|
|
}
|
|
|
|
return('?');
|
|
|
|
}
|
|
|
|
|
1996-07-12 19:08:36 +00:00
|
|
|
int
|
1994-05-27 12:33:43 +00:00
|
|
|
fname(a0, b0)
|
|
|
|
void *a0, *b0;
|
|
|
|
{
|
|
|
|
struct nlist *a = a0, *b = b0;
|
|
|
|
|
|
|
|
return(strcmp(a->n_un.n_name, b->n_un.n_name));
|
|
|
|
}
|
|
|
|
|
1996-07-12 19:08:36 +00:00
|
|
|
int
|
1994-05-27 12:33:43 +00:00
|
|
|
rname(a0, b0)
|
|
|
|
void *a0, *b0;
|
|
|
|
{
|
|
|
|
struct nlist *a = a0, *b = b0;
|
|
|
|
|
|
|
|
return(strcmp(b->n_un.n_name, a->n_un.n_name));
|
|
|
|
}
|
|
|
|
|
1996-07-12 19:08:36 +00:00
|
|
|
int
|
1994-05-27 12:33:43 +00:00
|
|
|
value(a0, b0)
|
|
|
|
void *a0, *b0;
|
|
|
|
{
|
|
|
|
register struct nlist *a = a0, *b = b0;
|
|
|
|
|
|
|
|
if (SYMBOL_TYPE(a->n_type) == N_UNDF)
|
|
|
|
if (SYMBOL_TYPE(b->n_type) == N_UNDF)
|
|
|
|
return(0);
|
|
|
|
else
|
|
|
|
return(-1);
|
|
|
|
else if (SYMBOL_TYPE(b->n_type) == N_UNDF)
|
|
|
|
return(1);
|
|
|
|
if (rev) {
|
|
|
|
if (a->n_value == b->n_value)
|
|
|
|
return(rname(a0, b0));
|
|
|
|
return(b->n_value > a->n_value ? 1 : -1);
|
|
|
|
} else {
|
|
|
|
if (a->n_value == b->n_value)
|
|
|
|
return(fname(a0, b0));
|
|
|
|
return(a->n_value > b->n_value ? 1 : -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-07-31 06:53:36 +00:00
|
|
|
static void
|
1996-07-12 19:08:36 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
1997-07-31 06:53:36 +00:00
|
|
|
(void)fprintf(stderr, "usage: nm [-agnoprtuwW] [file ...]\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|