NASM 0.98.03
[nasm/avx512.git] / rdoff / rdfdump.c
blobcb79a64d89bcb5672a562e478c2f1627e462e804
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 FILE *infile;
7 typedef unsigned short int16;
9 long translatelong(long in) { /* translate from little endian to
10 local representation */
11 long r;
12 unsigned char *i;
14 i = (unsigned char *)&in;
15 r = i[3];
16 r = (r << 8) + i[2];
17 r = (r << 8) + i[1];
18 r = (r << 8) + *i;
20 return r;
23 int translateshort(int16 in) {
24 int r;
25 unsigned char *i;
27 i = (unsigned char *)&in;
28 r = (i[1] << 8) + *i;
30 return r;
33 void print_header(long length, int rdf_version) {
34 char buf[129],t,s,l;
35 unsigned char reclen;
36 long o,ll;
37 int16 rs;
39 while (length > 0) {
40 fread(&t,1,1,infile);
41 if (rdf_version >= 2) {
42 fread(&reclen,1,1,infile);
44 switch(t) {
45 case 1: /* relocation record */
46 case 6: /* segment relocation */
47 fread(&s,1,1,infile);
48 fread(&o,4,1,infile);
49 fread(&l,1,1,infile);
50 fread(&rs,2,1,infile);
51 printf(" %s: location (%04x:%08lx), length %d, "
52 "referred seg %04x\n", t == 1 ? "relocation" : "seg relocation",
53 (int)s,translatelong(o),(int)l,
54 translateshort(rs));
55 if (rdf_version >= 2 && reclen != 8)
56 printf(" warning: reclen != 8\n");
57 if (rdf_version == 1) length -= 9;
58 if (rdf_version == 1 && t == 6)
59 printf(" warning: seg relocation not supported in RDOFF1\n");
60 break;
61 case 2: /* import record */
62 case 7: /* import far symbol */
63 fread(&rs,2,1,infile);
64 ll = 0;
66 if (rdf_version == 1) {
67 do {
68 fread(&buf[ll],1,1,infile);
69 } while (buf[ll++]);
71 else
73 for (;ll < reclen - 2; ll++)
74 fread(&buf[ll],1,1,infile);
77 printf(" %simport: segment %04x = %s\n",t == 7 ? "far " : "",
78 translateshort(rs),buf);
79 if (rdf_version == 1) length -= ll + 3;
80 if (rdf_version == 1 && t == 7)
81 printf (" warning: far import not supported in RDOFF1\n");
82 break;
83 case 3: /* export record */
84 fread(&s,1,1,infile);
85 fread(&o,4,1,infile);
86 ll = 0;
88 if (rdf_version == 1) {
89 do {
90 fread(&buf[ll],1,1,infile);
91 } while (buf[ll++]);
93 else
95 for (; ll < reclen - 5; ll ++)
96 fread(&buf[ll],1,1,infile);
98 printf(" export: (%04x:%08lx) = %s\n",(int)s,translatelong(o),buf);
99 if (rdf_version == 1) length -= ll + 6;
100 break;
101 case 4: /* DLL record */
102 ll = 0;
104 if (rdf_version == 1) {
105 do {
106 fread(&buf[ll],1,1,infile);
107 } while (buf[ll++]);
109 else
111 for (; ll < reclen - 1; ll++)
112 fread(&buf[ll],1,1,infile);
114 printf(" dll: %s\n",buf);
115 if (rdf_version == 1) length -= ll + 1;
116 break;
117 case 5: /* BSS reservation */
118 fread(&ll,4,1,infile);
119 printf(" bss reservation: %08lx bytes\n",translatelong(ll));
120 if (rdf_version == 1) length -= 5;
121 if (rdf_version > 1 && reclen != 4)
122 printf(" warning: reclen != 4\n");
123 break;
124 default:
125 printf(" unrecognised record (type %d",(int)t);
126 if (rdf_version > 1) printf(", length %d",(int)reclen);
127 printf(")\n");
128 if (rdf_version == 1) length --;
130 if (rdf_version != 1) length -= 2 + reclen;
134 char * knowntypes[8] = {"NULL", "text", "data", "object comment",
135 "linked comment", "loader comment",
136 "symbolic debug", "line number debug"};
138 char * translatesegmenttype(int16 type) {
139 if (type < 8) return knowntypes[type];
140 if (type < 0x0020) return "reserved";
141 if (type < 0x1000) return "reserved - moscow";
142 if (type < 0x8000) return "reserved - system dependant";
143 if (type < 0xFFFF) return "reserved - other";
144 if (type == 0xFFFF) return "invalid type code";
145 return "type code out of range";
148 int main(int argc,char **argv) {
149 char id[7];
150 long l;
151 int16 s;
152 int verbose = 0;
153 long offset;
154 int foundnullsegment = 0;
155 int version;
156 long segmentcontentlength = 0;
157 int nsegments = 0;
158 long headerlength = 0;
159 long objectlength = 0;
161 puts("RDOFF Dump utility v2.0 (C) Copyright 1996 Julian R Hall");
163 if (argc < 2) {
164 fputs("Usage: rdfdump [-v] <filename>\n",stderr);
165 exit(1);
168 if (! strcmp (argv[1], "-v") )
170 verbose = 1;
171 if (argc < 3)
173 fputs("required parameter missing\n",stderr);
174 exit(1);
176 argv++;
179 infile = fopen(argv[1],"rb");
180 if (! infile) {
181 fprintf(stderr,"rdfdump: Could not open %s",argv[1]);
182 exit(1);
185 fread(id,6,1,infile);
186 if (strncmp(id,"RDOFF",5)) {
187 fputs("rdfdump: File does not contain valid RDOFF header\n",stderr);
188 exit(1);
191 printf("File %s: RDOFF version %c\n\n",argv[1],id[5]);
192 if (id[5] < '1' || id[5] > '2') {
193 fprintf(stderr,"rdfdump: unknown RDOFF version '%c'\n",id[5]);
194 exit(1);
196 version = id[5] - '0';
198 if (version > 1) {
199 fread(&l, 4, 1, infile);
200 objectlength = translatelong(l);
201 printf("Object content size: %ld bytes\n", objectlength);
204 fread(&l,4,1,infile);
205 headerlength = translatelong(l);
206 printf("Header (%ld bytes):\n",headerlength);
207 print_header(headerlength, version);
209 if (version == 1) {
210 fread(&l,4,1,infile);
211 l = translatelong(l);
212 printf("\nText segment length = %ld bytes\n",l);
213 offset = 0;
214 while(l--) {
215 fread(id,1,1,infile);
216 if (verbose) {
217 if (offset % 16 == 0)
218 printf("\n%08lx ", offset);
219 printf(" %02x",(int) (unsigned char)id[0]);
220 offset++;
223 if (verbose) printf("\n\n");
225 fread(&l,4,1,infile);
226 l = translatelong(l);
227 printf("Data segment length = %ld bytes\n",l);
229 if (verbose)
231 offset = 0;
232 while (l--) {
233 fread(id,1,1,infile);
234 if (offset % 16 == 0)
235 printf("\n%08lx ", offset);
236 printf(" %02x",(int) (unsigned char) id[0]);
237 offset++;
239 printf("\n");
242 else
244 do {
245 fread(&s,2,1,infile);
246 s = translateshort(s);
247 if (!s) {
248 printf("\nNULL segment\n");
249 foundnullsegment = 1;
250 break;
252 printf("\nSegment:\n Type = %04X (%s)\n",(int)s,
253 translatesegmenttype(s));
254 nsegments++;
256 fread(&s,2,1,infile);
257 printf(" Number = %04X\n",(int)translateshort(s));
258 fread(&s,2,1,infile);
259 printf(" Resrvd = %04X\n",(int)translateshort(s));
260 fread(&l,4,1,infile);
261 l = translatelong(l);
262 printf(" Length = %ld bytes\n",l);
263 segmentcontentlength += l;
265 offset = 0;
266 while(l--) {
267 fread(id,1,1,infile);
268 if (verbose) {
269 if (offset % 16 == 0)
270 printf("\n%08lx ", offset);
271 printf(" %02x",(int) (unsigned char)id[0]);
272 offset++;
275 if (verbose) printf("\n");
276 } while (!feof(infile));
277 if (! foundnullsegment)
278 printf("\nWarning: unexpected end of file - "
279 "NULL segment not found\n");
281 printf("\nTotal number of segments: %d\n", nsegments);
282 printf("Total segment content length: %ld bytes\n",segmentcontentlength);
284 /* calculate what the total object content length should have been */
285 l = segmentcontentlength + 10 * (nsegments+1) + headerlength + 4;
286 if (l != objectlength)
287 printf("Warning: actual object length (%ld) != "
288 "stored object length (%ld)\n", l, objectlength);
290 fclose(infile);
291 return 0;