-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymread.c
60 lines (51 loc) · 1.08 KB
/
symread.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
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include "flat.h"
int symread(const char *symfile)
{
FILE *fp;
struct flat_hdr header;
long n_syms;
if ((fp = fopen(symfile, "r")) == NULL) {
return 0;
}
if (flat_read_header(fp, &header) != 0) {
return 0;
}
fseek(fp, header.reloc_start + 4*header.reloc_count, SEEK_SET);
fread(&n_syms, sizeof(long), 1, fp);
n_syms = ntohl(n_syms);
if (!n_syms) {
fprintf(stderr, "No symbols.\n");
return 1;
}
while (n_syms-- > 0) {
unsigned int pos, addr = 0;
char *sym;
int len = 0;
char whence;
fread(&addr, sizeof(unsigned int), 1, fp);
addr = ntohl(addr);
pos = ftell(fp);
while ((fgetc(fp)) != '\0')
len++;
fseek(fp, pos, SEEK_SET);
sym = malloc(sizeof(char) * (len + 1));
fread(sym, sizeof(char), len + 1, fp);
fread(&whence, sizeof(char), 1, fp);
printf("%08x\t%s\n", addr, sym);
free(sym);
}
fclose(fp);
return 0;
}
int main(int argc, char **argv)
{
char *file = argv[1];
if (argc < 2) {
fprintf(stderr, "Usage: %s <bFLT file>\n", argv[0]);
return 1;
}
return symread(file);
}