summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>2001-08-17 14:25:27 +0000
committerMarc Espie <espie@cvs.openbsd.org>2001-08-17 14:25:27 +0000
commit9fa6fb4b7f8b3010aea7ad028fb4d982203b0578 (patch)
tree19df0cf856bd76d439edb557cf70ab4b4e1160e0
parent5d0f34d6a2d5e8e464a0e81b07dd640d992dfbe7 (diff)
Show indirect symbols in -e mode.
-rw-r--r--usr.bin/nm/nm.c111
1 files changed, 69 insertions, 42 deletions
diff --git a/usr.bin/nm/nm.c b/usr.bin/nm/nm.c
index 91ee3692b2b..4c999a8cab4 100644
--- a/usr.bin/nm/nm.c
+++ b/usr.bin/nm/nm.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: nm.c,v 1.12 2001/08/16 15:45:05 espie Exp $ */
+/* $OpenBSD: nm.c,v 1.13 2001/08/17 14:25:26 espie Exp $ */
/* $NetBSD: nm.c,v 1.7 1996/01/14 23:04:03 pk Exp $ */
/*
@@ -47,7 +47,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)nm.c 8.1 (Berkeley) 6/6/93";
#endif
-static char rcsid[] = "$OpenBSD: nm.c,v 1.12 2001/08/16 15:45:05 espie Exp $";
+static char rcsid[] = "$OpenBSD: nm.c,v 1.13 2001/08/17 14:25:26 espie Exp $";
#endif /* not lint */
#include <sys/param.h>
@@ -76,8 +76,10 @@ int show_extensions = 0;
int fcount;
int rev;
-int fname(), rname(), value();
-int (*sfunc)() = fname;
+int fname __P((const void *, const void *));
+int rname __P((const void *, const void *));
+int value __P((const void *, const void *));
+int (*sfunc) __P((const void *, const void *)) = fname;
/* some macros for symbol type (nlist.n_type) handling */
@@ -88,6 +90,8 @@ int (*sfunc)() = fname;
void *emalloc __P((size_t));
void pipe2cppfilt __P((void));
void usage __P((void));
+char *symname __P((struct nlist *));
+void print_symbol __P((const char *, struct nlist *));
/*
* main()
@@ -330,7 +334,8 @@ show_objfile(objname, fp)
char *objname;
FILE *fp;
{
- register struct nlist *names, *np;
+ struct nlist *names, *np;
+ struct nlist **snames;
register int i, nnames, nrawnames;
struct exec head;
long stabsize;
@@ -375,6 +380,7 @@ show_objfile(objname, fp)
/* get memory for the symbol table */
names = emalloc((size_t)head.a_syms);
nrawnames = head.a_syms / sizeof(*names);
+ snames = emalloc(nrawnames*sizeof(struct nlist *));
if (fread((char *)names, (size_t)head.a_syms, (size_t)1, fp) != 1) {
warnx("%s: cannot read symbol table", objname);
(void)free((char *)names);
@@ -417,16 +423,6 @@ show_objfile(objname, fp)
* 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
@@ -437,29 +433,55 @@ show_objfile(objname, fp)
np->n_un.n_name = stab + np->n_un.n_strx;
else
np->n_un.n_name = "";
- names[nnames++] = *np;
+ 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;
+
+ snames[nnames++] = np;
}
/* sort the symbol table if applicable */
if (sfunc)
- qsort((char *)names, (size_t)nnames, sizeof(*names), sfunc);
+ qsort(snames, (size_t)nnames, sizeof(*snames), sfunc);
/* print out symbols */
- for (np = names, i = 0; i < nnames; np++, i++)
- print_symbol(objname, np);
+ for (i = 0; i < nnames; i++) {
+ if (show_extensions && snames[i] != names &&
+ SYMBOL_TYPE((snames[i] -1)->n_type) == N_INDR)
+ continue;
+ print_symbol(objname, snames[i]);
+ }
- (void)free((char *)names);
+ (void)free(snames);
+ (void)free(names);
(void)free(stab);
return(0);
}
+char *
+symname(sym)
+ struct nlist *sym;
+{
+ if (demangle && sym->n_un.n_name[0] == '_')
+ return sym->n_un.n_name + 1;
+ else
+ return sym->n_un.n_name;
+}
+
/*
* print_symbol()
* show one symbol
*/
+void
print_symbol(objname, sym)
- char *objname;
- register struct nlist *sym;
+ const char *objname;
+ struct nlist *sym;
{
char *typestring(), typeletter(), *otherstring();
@@ -472,7 +494,9 @@ print_symbol(objname, sym)
*/
if (!print_only_undefined_symbols) {
/* print symbol's value */
- if (SYMBOL_TYPE(sym->n_type) == N_UNDF)
+ if (SYMBOL_TYPE(sym->n_type) == N_UNDF ||
+ (show_extensions && SYMBOL_TYPE(sym->n_type) == N_INDR &&
+ sym->n_value == 0))
(void)printf(" ");
else
(void)printf("%08lx", sym->n_value);
@@ -488,11 +512,11 @@ print_symbol(objname, sym)
(void)printf(" %c ", typeletter(sym->n_type));
}
- /* print the symbol's name */
- if (demangle && sym->n_un.n_name[0] == '_')
- (void)puts(sym->n_un.n_name + 1);
+ if (SYMBOL_TYPE(sym->n_type) == N_INDR && show_extensions) {
+ printf("%s -> %s\n", symname(sym), symname(sym+1));
+ }
else
- (void)puts(sym->n_un.n_name);
+ (void)puts(symname(sym));
}
#define AUX_OBJECT 1
@@ -612,42 +636,45 @@ typeletter(type)
return('?');
}
+int
fname(a0, b0)
- void *a0, *b0;
+ const void *a0, *b0;
{
- struct nlist *a = a0, *b = b0;
+ struct nlist * const *a = a0, * const *b = b0;
- return(strcmp(a->n_un.n_name, b->n_un.n_name));
+ return(strcmp((*a)->n_un.n_name, (*b)->n_un.n_name));
}
+int
rname(a0, b0)
- void *a0, *b0;
+ const void *a0, *b0;
{
- struct nlist *a = a0, *b = b0;
+ struct nlist * const *a = a0, * const *b = b0;
- return(strcmp(b->n_un.n_name, a->n_un.n_name));
+ return(strcmp((*b)->n_un.n_name, (*a)->n_un.n_name));
}
+int
value(a0, b0)
- void *a0, *b0;
+ const void *a0, *b0;
{
- register struct nlist *a = a0, *b = b0;
+ struct nlist * const *a = a0, * const *b = b0;
- if (SYMBOL_TYPE(a->n_type) == N_UNDF)
- if (SYMBOL_TYPE(b->n_type) == N_UNDF)
+ 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)
+ else if (SYMBOL_TYPE((*b)->n_type) == N_UNDF)
return(1);
if (rev) {
- if (a->n_value == b->n_value)
+ if ((*a)->n_value == (*b)->n_value)
return(rname(a0, b0));
- return(b->n_value > a->n_value ? 1 : -1);
+ return((*b)->n_value > (*a)->n_value ? 1 : -1);
} else {
- if (a->n_value == b->n_value)
+ if ((*a)->n_value == (*b)->n_value)
return(fname(a0, b0));
- return(a->n_value > b->n_value ? 1 : -1);
+ return((*a)->n_value > (*b)->n_value ? 1 : -1);
}
}