NASM 0.98.17
[nasm/avx512.git] / rdoff / v1 / rdfdump.c
blob080c2e73710b02a33519f05de84f7bbabfd7f31c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 FILE *infile;
7 long translatelong(long in) { /* translate from little endian to
8 local representation */
9 long r;
10 unsigned char *i;
12 i = (unsigned char *)&in;
13 r = i[3];
14 r = (r << 8) + i[2];
15 r = (r << 8) + i[1];
16 r = (r << 8) + *i;
18 return r;
21 int translateshort(short in) {
22 int r;
23 unsigned char *i;
25 i = (unsigned char *)&in;
26 r = (i[1] << 8) + *i;
28 return r;
30 void print_header(long length) {
31 char buf[129],t,s,l;
32 long o,ll;
33 short rs;
35 while (length > 0) {
36 fread(&t,1,1,infile);
37 switch(t) {
38 case 1: /* relocation record */
39 fread(&s,1,1,infile);
40 fread(&o,4,1,infile);
41 fread(&l,1,1,infile);
42 fread(&rs,2,1,infile);
43 printf(" relocation: location (%04x:%08lx), length %d, "
44 "referred seg %04x\n",(int)s,translatelong(o),(int)l,
45 translateshort(rs));
46 length -= 9;
47 break;
48 case 2: /* import record */
49 fread(&rs,2,1,infile);
50 ll = 0;
51 do {
52 fread(&buf[ll],1,1,infile);
53 } while (buf[ll++]);
54 printf(" import: segment %04x = %s\n",translateshort(rs),buf);
55 length -= ll + 3;
56 break;
57 case 3: /* export record */
58 fread(&s,1,1,infile);
59 fread(&o,4,1,infile);
60 ll = 0;
61 do {
62 fread(&buf[ll],1,1,infile);
63 } while (buf[ll++]);
64 printf(" export: (%04x:%08lx) = %s\n",(int)s,translatelong(o),buf);
65 length -= ll + 6;
66 break;
67 case 4: /* DLL record */
68 ll = 0;
69 do {
70 fread(&buf[ll],1,1,infile);
71 } while (buf[ll++]);
72 printf(" dll: %s\n",buf);
73 length -= ll + 1;
74 break;
75 case 5: /* BSS reservation */
76 fread(&ll,4,1,infile);
77 printf(" bss reservation: %08lx bytes\n",translatelong(ll));
78 length -= 5;
79 break;
80 default:
81 printf(" unrecognised record (type %d)\n",(int)t);
82 length --;
87 int main(int argc,char **argv) {
88 char id[7];
89 long l;
90 int verbose = 0;
91 long offset;
93 puts("RDOFF Dump utility v1.1 (C) Copyright 1996 Julian R Hall");
95 if (argc < 2) {
96 fputs("Usage: rdfdump [-v] <filename>\n",stderr);
97 exit(1);
100 if (! strcmp (argv[1], "-v") )
102 verbose = 1;
103 if (argc < 3)
105 fputs("required parameter missing\n",stderr);
106 exit(1);
108 argv++;
111 infile = fopen(argv[1],"rb");
112 if (! infile) {
113 fprintf(stderr,"rdfdump: Could not open %s",argv[1]);
114 exit(1);
117 fread(id,6,1,infile);
118 if (strncmp(id,"RDOFF",5)) {
119 fputs("rdfdump: File does not contain valid RDOFF header\n",stderr);
120 exit(1);
123 printf("File %s: RDOFF version %c\n\n",argv[1],id[5]);
124 if (id[5] < '1' || id[5] > '1') {
125 fprintf(stderr,"rdfdump: unknown RDOFF version '%c'\n",id[5]);
126 exit(1);
129 fread(&l,4,1,infile);
130 l = translatelong(l);
131 printf("Header (%ld bytes):\n",l);
132 print_header(l);
134 fread(&l,4,1,infile);
135 l = translatelong(l);
136 printf("\nText segment length = %ld bytes\n",l);
137 offset = 0;
138 while(l--) {
139 fread(id,1,1,infile);
140 if (verbose) {
141 if (offset % 16 == 0)
142 printf("\n%08lx ", offset);
143 printf(" %02x",(int) (unsigned char)id[0]);
144 offset++;
147 if (verbose) printf("\n\n");
149 fread(&l,4,1,infile);
150 l = translatelong(l);
151 printf("Data segment length = %ld bytes\n",l);
153 if (verbose)
155 offset = 0;
156 while (l--) {
157 fread(id,1,1,infile);
158 if (offset % 16 == 0)
159 printf("\n%08lx ", offset);
160 printf(" %02x",(int) (unsigned char) id[0]);
161 offset++;
163 printf("\n");
165 fclose(infile);
166 return 0;