-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnm.c
122 lines (111 loc) · 2.8 KB
/
nm.c
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
/*
* For now this only works on object files. It's mostly here so I can
* check the assembler output looks valid.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <stdint.h>
#include <ctype.h>
#include "obj.h"
static char *arg0;
static int err;
static int show_debug = 1;
static int show_name;
static int show_undef;
static char segname[] = "ACDBZXSLsb?????U";
static int do_nm(FILE *fp, const char *name)
{
static struct objhdr oh;
off_t base;
uint8_t type;
int c;
uint16_t addr;
char symname[NAMELEN + 1];
if (show_name)
printf("%s:\n", name);
if (fread(&oh, sizeof(oh), 1, fp) != 1 ||
oh.o_magic != MAGIC_OBJ) {
fprintf(stderr, "%s: %s: not a valid object file.\n", arg0, name);
return 1;
}
base = oh.o_symbase;
if (base == 0) {
fprintf(stderr, "%s: %s: no symbols.\n", arg0, name);
return 0;
}
if (fseek(fp, base, SEEK_SET)) {
fprintf(stderr, "%s: %s: truncated file ?\n", arg0, name);
return 1;
}
while (1) {
if (base >= oh.o_dbgbase && show_debug == 0)
break;
c = fgetc(fp);
if (c == EOF)
return 0;
type = (uint8_t)c;
base++;
fread(symname, NAMELEN, 1, fp);
base += NAMELEN;
symname[NAMELEN] = 0;
/* Address if defined */
addr = fgetc(fp);
addr |= fgetc(fp) << 8;
base += 2;
c = segname[type & S_SEGMENT];
if (!(type & S_UNKNOWN)) {
/* Showing undefined only */
if (show_undef)
continue;
} else {
if (c != 'U')
c = tolower(c);
addr = 0;
}
printf("%04X %c %s\n", addr, c, symname);
}
return 0;
}
int main(int argc, char *argv[])
{
int opt;
arg0 = argv[0];
while ((opt = getopt(argc,argv,"oAug")) != -1) {
switch(opt) {
case 'o':
case 'A':
show_name = 1;
break;
case 'u':
show_undef = 1;
break;
case 'g':
show_debug = 0;
break;
default:
fprintf(stderr, "%s: name ...\n", argv[0]);
exit(1);
}
}
/* Show names if multiple arguments */
if (optind - argc > 1)
show_name = 1;
if (optind >= argc)
do_nm(stdin, "-");
else while (optind < argc) {
FILE *fp = fopen(argv[optind], "r");
if (fp == NULL) {
perror(argv[optind]);
err |= 1;
} else {
printf("%s:\n", argv[optind]);
err |= do_nm(fp, argv[optind]);
fclose(fp);
}
optind++;
}
exit(err);
}