Repository navigation
Expand file tree
/
Copy pathnm.c
More file actions
executable file
·226 lines (206 loc) · 5.1 KB
/
Copy pathnm.c
File metadata and controls
executable file
·226 lines (206 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* nm - print name list. Author: Dick van Veen */
/* Dick van Veen: veench@cs.vu.nl */
#include <sys/types.h>
#include <a.out.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* Read the name list in memory, sort it, and print it. */
/* Nm [-gnopru] [file] ...
*
* flags:
* -d address in decimal
* -g print only external symbols.
* -n sort numerically rather than alphabetically.
* -o prepend file name to each line rather than only once.
* -p don't sort, pint n symbol-table order.
* -r sort in reverse order.
* -u print only undefined symbols.
*
* - when no file name is present, a.out is assumed.
*
* NOTE: no archives are supported because assembly files don't
* have symbol tables.
*
*/
#define A_OUT "a.out"
int d_flag;
int g_flag;
int n_flag;
int o_flag;
int p_flag;
int r_flag;
int u_flag;
char io_buf[BUFSIZ]; /* io buffer */
struct exec header; /* header of a.out file */
int stbl_elems; /* #elements in symbol table */
_PROTOTYPE(int main, (int argc, char **argv));
_PROTOTYPE(int nm_sort, (const void *tmp_stbl1, const void *tmp_stbl2));
_PROTOTYPE(void nm, (char *file));
_PROTOTYPE(int read_header, (int fd));
_PROTOTYPE(void nm_print, (char *file, struct nlist *stbl));
int main(argc, argv)
int argc;
char **argv;
{
argv++;
while (*argv != 0 && **argv == '-') {
*argv += 1;
while (**argv != '\0') {
switch (**argv) {
case 'd': d_flag = 1; break;
case 'g': g_flag = 1; break;
case 'n': n_flag = 1; break;
case 'o': o_flag = 1; break;
case 'p': p_flag = 1; break;
case 'r': r_flag = 1; break;
case 'u': u_flag = 1; break;
default:
fprintf(stderr, "illegal flag: -%c\n", **argv);
exit(-1);
}
*argv += 1;
}
argv++;
}
setbuf(stdin, io_buf);
if (*argv == 0)
nm(A_OUT);
else
while (*argv != 0) {
nm(*argv);
argv++;
}
return(0);
}
int nm_sort(tmp_stbl1, tmp_stbl2)
_CONST void *tmp_stbl1, *tmp_stbl2;
{
struct nlist *stbl1 = (struct nlist *)tmp_stbl1;
struct nlist *stbl2 = (struct nlist *)tmp_stbl2;
int cmp;
if (n_flag) { /* sort numerically */
if ((stbl1->n_sclass & N_SECT) <
(stbl2->n_sclass & N_SECT))
cmp = -1;
else if ((stbl1->n_sclass & N_SECT) >
(stbl2->n_sclass & N_SECT))
cmp = 1;
else if (stbl1->n_value < stbl2->n_value)
cmp = -1;
else if (stbl1->n_value > stbl2->n_value)
cmp = 1;
else
cmp = strncmp(stbl1->n_name, stbl2->n_name, (size_t)8);
} else {
cmp = strncmp(stbl1->n_name, stbl2->n_name, (size_t)8);
if (cmp == 0) {
if (stbl1->n_value < stbl2->n_value)
cmp = -1;
else if (stbl1->n_value > stbl2->n_value)
cmp = 1;
}
}
if (r_flag) cmp = -cmp; /* reverse sort */
return(cmp);
}
void nm(file)
char *file;
{
struct nlist *stbl;
int fd;
fd = open(file, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "can't open %s\n", file);
return;
}
if (read_header(fd)) {
fprintf(stderr, "%s: no executable file\n", file);
close(fd);
return;
}
if (header.a_syms == 0) {
close(fd);
return;
}
if ((size_t) header.a_syms != header.a_syms) {
fprintf(stderr, "%s: symbol table too large to allocate\n", file);
close(fd);
return;
}
if ((int) header.a_syms != header.a_syms) {
/* This is necessary because we are too lazy to iterate the read. */
fprintf(stderr, "%s: symbol table too large to read\n", file);
close(fd);
return;
}
stbl = (struct nlist *) malloc((size_t) header.a_syms);
if (stbl == NULL) {
fprintf(stderr, "%s: can't allocate symbol table\n", file);
close(fd);
return;
}
if (read(fd, (char *) stbl, (unsigned) header.a_syms) != header.a_syms) {
fprintf(stderr, "%s: can't read symbol table\n", file);
free(stbl);
close(fd);
return;
}
stbl_elems = (int) header.a_syms / sizeof(struct nlist);
if (!p_flag) qsort(stbl, (size_t)stbl_elems, sizeof(struct nlist), nm_sort);
nm_print(file, stbl);
free(stbl);
close(fd);
}
int read_header(fd)
int fd;
{
if (read(fd, (char *) &header, sizeof(struct exec)) != sizeof(struct exec))
return(1);
if (BADMAG(header)) return(1);
lseek(fd, A_SYMPOS(header), SEEK_SET);
return(0);
}
void nm_print(file, stbl)
char *file;
register struct nlist *stbl;
{
struct nlist *last;
char name[9];
int n_sclass;
char type;
name[8] = '\0';
if (!o_flag) printf("%s:\n", file);
for (last = &stbl[stbl_elems]; stbl != last; stbl++) {
if (g_flag && (stbl->n_sclass & N_CLASS) != C_EXT) continue;
if (u_flag && (stbl->n_sclass & N_SECT) != N_UNDF) continue;
n_sclass = stbl->n_sclass & N_SECT;
if (n_sclass == N_ABS)
type = 'a';
else if (n_sclass == N_TEXT)
type = 't';
else if (n_sclass == N_DATA)
type = 'd';
else if (n_sclass == N_BSS)
type = 'b';
else
type = 'u';
if ((stbl->n_sclass & N_CLASS) == C_EXT) type += 'A' - 'a';
strncpy(name, stbl->n_name, (size_t)8);
if (d_flag) {
/* Offsets in decimal. */
if (o_flag)
printf("%s:%08ld %c %s\n",file,stbl->n_value,type,name);
else
printf("%08ld %c %s\n", stbl->n_value, type, name);
} else {
/* Offsets in hex. */
if (o_flag)
printf("%s:%08lx %c %s\n",file,stbl->n_value,type,name);
else
printf("%08lx %c %s\n", stbl->n_value, type, name);
}
}
}