2 * Copyright (c) 1989 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)nlist.c 5.8 (Berkeley) 2/23/91";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/types.h>
45 typedef struct nlist NLIST
;
46 #define _strx n_un.n_strx
47 #define _name n_un.n_name
48 #define ISVALID(p) (p->_name && p->_name[0])
55 register NLIST
*p
, *s
;
59 off_t strings_offset
, symbol_offset
, symbol_size
, lseek();
60 int entries
, len
, maxlen
;
65 if (!(fsym
= fopen(name
, "r")))
67 if (fread((char *)&ebuf
, sizeof(struct exec
), 1, fsym
) != 1 ||
71 symbol_offset
= N_SYMOFF(ebuf
);
72 symbol_size
= ebuf
.a_syms
;
73 strings_offset
= symbol_offset
+ symbol_size
;
74 if (fseek(fsym
, symbol_offset
, SEEK_SET
))
77 if (!(fstr
= fopen(name
, "r")))
81 * clean out any left-over information for all valid entries.
82 * Type and value defined to be 0 if not found; historical
83 * versions cleared other and desc as well. Also figure out
84 * the largest string length so don't read any more of the
85 * string table than we have to.
87 for (p
= list
, entries
= maxlen
= 0; ISVALID(p
); ++p
, ++entries
) {
92 if ((len
= strlen(p
->_name
)) > maxlen
)
95 if (++maxlen
> sizeof(sbuf
)) { /* for the NULL */
96 (void)fprintf(stderr
, "nlist: symbol too large.\n");
101 for (s
= &nbuf
; symbol_size
; symbol_size
-= sizeof(NLIST
)) {
102 if (fread((char *)s
, sizeof(NLIST
), 1, fsym
) != 1)
104 if (!s
->_strx
|| s
->n_type
&N_STAB
)
106 if (fseek(fstr
, strings_offset
+ s
->_strx
, SEEK_SET
))
108 (void)fread(sbuf
, sizeof(sbuf
[0]), maxlen
, fstr
);
109 for (p
= list
; ISVALID(p
); p
++)
110 if (!strcmp(p
->_name
, sbuf
)) {
111 p
->n_value
= s
->n_value
;
112 p
->n_type
= s
->n_type
;
113 p
->n_desc
= s
->n_desc
;
114 p
->n_other
= s
->n_other
;
119 done2
: (void)fclose(fstr
);
120 done1
: (void)fclose(fsym
);