2 * rdfdump.c - dump RDOFF file header.
13 long translatelong(long in
) { /* translate from little endian to
14 local representation */
18 i
= (unsigned char *)&in
;
27 int16
translateshort(int16 in
) {
31 i
= (unsigned char *)&in
;
37 void print_header(long length
, int rdf_version
)
39 char buf
[129],t
,l
,s
,flags
;
46 if (rdf_version
>= 2) {
47 fread(&reclen
,1,1,infile
);
50 case RDFREC_GENERIC
: /* generic record */
51 printf(" generic record (length=%d)\n", (int)reclen
);
52 fseek(infile
, reclen
, SEEK_CUR
);
55 case RDFREC_RELOC
: /* relocation record */
56 case RDFREC_SEGRELOC
: /* segment relocation */
60 fread(&rs
,2,1,infile
);
61 printf(" %s: location (%04x:%08lx), length %d, "
62 "referred seg %04x\n", t
== 1 ? "relocation" : "seg relocation",
63 (int)s
,translatelong(o
),(int)l
,
65 if (rdf_version
>= 2 && reclen
!= 8)
66 printf(" warning: reclen != 8\n");
67 if (rdf_version
== 1) length
-= 9;
68 if (rdf_version
== 1 && t
== 6)
69 printf(" warning: seg relocation not supported in RDOFF1\n");
72 case RDFREC_IMPORT
: /* import record */
73 case RDFREC_FARIMPORT
: /* import far symbol */
74 fread(&rs
,2,1,infile
);
77 if (rdf_version
== 1) {
79 fread(&buf
[ll
],1,1,infile
);
82 for (;ll
< reclen
- 2; ll
++)
83 fread(&buf
[ll
],1,1,infile
);
86 printf(" %simport: segment %04x = %s\n",t
== 7 ? "far " : "",
87 translateshort(rs
),buf
);
88 if (rdf_version
== 1) length
-= ll
+ 3;
89 if (rdf_version
== 1 && t
== 7)
90 printf (" warning: far import not supported in RDOFF1\n");
93 case RDFREC_GLOBAL
: /* export record */
94 fread(&flags
,1,1,infile
);
99 if (rdf_version
== 1) {
101 fread(&buf
[ll
],1,1,infile
);
104 for (; ll
< reclen
- 6; ll
++)
105 fread(&buf
[ll
],1,1,infile
);
107 if (flags
& SYM_GLOBAL
)
111 if (flags
& SYM_FUNCTION
) printf(" proc");
112 if (flags
& SYM_DATA
) printf(" data");
113 printf(": (%04x:%08lx) = %s\n",(int)s
,translatelong(o
),buf
);
114 if (rdf_version
== 1) length
-= ll
+ 6;
117 case RDFREC_DLL
: /* DLL and Module records */
121 if (rdf_version
== 1) {
123 fread(&buf
[ll
],1,1,infile
);
126 for (; ll
< reclen
; ll
++)
127 fread(&buf
[ll
],1,1,infile
);
129 if (t
==4) printf(" dll: %s\n",buf
);
130 else printf(" module: %s\n",buf
);
131 if (rdf_version
== 1) length
-= ll
+ 1;
134 case RDFREC_BSS
: /* BSS reservation */
135 fread(&ll
,4,1,infile
);
136 printf(" bss reservation: %08lx bytes\n",translatelong(ll
));
137 if (rdf_version
== 1) length
-= 5;
138 if (rdf_version
> 1 && reclen
!= 4)
139 printf(" warning: reclen != 4\n");
142 case RDFREC_COMMON
: {
143 unsigned short seg
, align
;
146 fread(&seg
, 2, 1, infile
);
147 fread(&size
, 4, 1, infile
);
148 fread(&align
, 2, 1, infile
);
149 for (ll
= 0; ll
< reclen
- 8; ll
++)
150 fread(buf
+ll
, 1, 1, infile
);
151 printf(" common: segment %04x = %s, %ld:%d\n", translateshort(seg
),
152 buf
, translatelong(size
), translateshort(align
));
157 printf(" unrecognized record (type %d", (int)t
);
158 if (rdf_version
> 1) {
159 printf(", length %d",(int)reclen
);
160 fseek(infile
,reclen
,SEEK_CUR
);
164 if (rdf_version
!= 1) length
-= 2 + reclen
;
168 char * knowntypes
[8] = {"NULL", "text", "data", "object comment",
169 "linked comment", "loader comment",
170 "symbolic debug", "line number debug"};
172 char * translatesegmenttype(int16 type
) {
173 if (type
< 8) return knowntypes
[type
];
174 if (type
< 0x0020) return "reserved";
175 if (type
< 0x1000) return "reserved - moscow";
176 if (type
< 0x8000) return "reserved - system dependant";
177 if (type
< 0xFFFF) return "reserved - other";
178 if (type
== 0xFFFF) return "invalid type code";
179 return "type code out of range";
182 int main(int argc
,char **argv
) {
188 int foundnullsegment
= 0;
190 long segmentcontentlength
= 0;
192 long headerlength
= 0;
193 long objectlength
= 0;
195 puts("RDOFF Dump utility v2.1\n(c) Copyright 1996,99,2000 Julian R Hall, Yuri M Zaporogets");
198 fputs("Usage: rdfdump [-v] <filename>\n",stderr
);
202 if (! strcmp (argv
[1], "-v") )
207 fputs("required parameter missing\n",stderr
);
213 infile
= fopen(argv
[1],"rb");
215 fprintf(stderr
,"rdfdump: Could not open %s\n",argv
[1]);
219 fread(id
,6,1,infile
);
220 if (strncmp(id
,"RDOFF",5)) {
221 fputs("rdfdump: File does not contain valid RDOFF header\n",stderr
);
225 printf("File %s: RDOFF version %c\n\n",argv
[1],id
[5]);
226 if (id
[5] < '1' || id
[5] > '2') {
227 fprintf(stderr
,"rdfdump: unknown RDOFF version '%c'\n",id
[5]);
230 version
= id
[5] - '0';
233 fread(&l
, 4, 1, infile
);
234 objectlength
= translatelong(l
);
235 printf("Object content size: %ld bytes\n", objectlength
);
238 fread(&l
,4,1,infile
);
239 headerlength
= translatelong(l
);
240 printf("Header (%ld bytes):\n",headerlength
);
241 print_header(headerlength
, version
);
244 fread(&l
,4,1,infile
);
245 l
= translatelong(l
);
246 printf("\nText segment length = %ld bytes\n",l
);
249 fread(id
,1,1,infile
);
251 if (offset
% 16 == 0)
252 printf("\n%08lx ", offset
);
253 printf(" %02x",(int) (unsigned char)id
[0]);
257 if (verbose
) printf("\n\n");
259 fread(&l
,4,1,infile
);
260 l
= translatelong(l
);
261 printf("Data segment length = %ld bytes\n",l
);
267 fread(id
,1,1,infile
);
268 if (offset
% 16 == 0)
269 printf("\n%08lx ", offset
);
270 printf(" %02x",(int) (unsigned char) id
[0]);
279 fread(&s
,2,1,infile
);
280 s
= translateshort(s
);
282 printf("\nNULL segment\n");
283 foundnullsegment
= 1;
286 printf("\nSegment:\n Type = %04X (%s)\n",(int)s
,
287 translatesegmenttype(s
));
290 fread(&s
,2,1,infile
);
291 printf(" Number = %04X\n",(int)translateshort(s
));
292 fread(&s
,2,1,infile
);
293 printf(" Resrvd = %04X\n",(int)translateshort(s
));
294 fread(&l
,4,1,infile
);
295 l
= translatelong(l
);
296 printf(" Length = %ld bytes\n",l
);
297 segmentcontentlength
+= l
;
301 fread(id
,1,1,infile
);
303 if (offset
% 16 == 0)
304 printf("\n%08lx ", offset
);
305 printf(" %02x",(int) (unsigned char)id
[0]);
309 if (verbose
) printf("\n");
310 } while (!feof(infile
));
311 if (! foundnullsegment
)
312 printf("\nWarning: unexpected end of file - "
313 "NULL segment not found\n");
315 printf("\nTotal number of segments: %d\n", nsegments
);
316 printf("Total segment content length: %ld bytes\n",segmentcontentlength
);
318 /* calculate what the total object content length should have been */
319 l
= segmentcontentlength
+ 10 * (nsegments
+1) + headerlength
+ 4;
320 if (l
!= objectlength
)
321 printf("Warning: actual object length (%ld) != "
322 "stored object length (%ld)\n", l
, objectlength
);