Update dependencies
[nasm/avx512.git] / rdoff / rdfdump.c
blob626fd2a106fbfab688046d223b94d5e62ed1d1fe
1 /*
2 * rdfdump.c - dump RDOFF file header.
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #define RDOFF_UTILS
11 #include "rdoff.h"
13 #define PROGRAM_VERSION "2.3"
15 FILE *infile;
17 void print_header(long length, int rdf_version)
19 char buf[129], t, l, s, flags;
20 unsigned char reclen;
21 long o, ll;
22 uint16 rs;
24 while (length > 0) {
25 fread(&t, 1, 1, infile);
26 if (rdf_version >= 2) {
27 fread(&reclen, 1, 1, infile);
29 switch (t) {
30 case RDFREC_GENERIC: /* generic record */
31 printf(" generic record (length=%d)\n", (int) reclen);
32 fseek(infile, reclen, SEEK_CUR);
33 break;
35 case RDFREC_RELOC: /* relocation record */
36 case RDFREC_SEGRELOC: /* segment relocation */
37 fread(&s, 1, 1, infile);
38 fread(&o, 4, 1, infile);
39 fread(&l, 1, 1, infile);
40 fread(&rs, 2, 1, infile);
41 printf(" %s: location (%04x:%08lx), length %d, "
42 "referred seg %04x\n", t == 1 ? "relocation" : "seg relocation",
43 (int) s, translatelong(o), (int) l, translateshort(rs));
44 if (rdf_version >= 2 && reclen != 8)
45 printf(" warning: reclen != 8\n");
46 if (rdf_version == 1)
47 length -= 9;
48 if (rdf_version == 1 && t == 6)
49 printf(" warning: seg relocation not supported in RDOFF1\n");
50 break;
52 case RDFREC_IMPORT: /* import record */
53 case RDFREC_FARIMPORT: /* import far symbol */
54 fread(&flags, 1, 1, infile);
55 fread(&rs, 2, 1, infile);
56 ll = 0;
58 if (rdf_version == 1) {
59 do {
60 fread(&buf[ll], 1, 1, infile);
61 } while (buf[ll++]);
62 } else {
63 for (; ll < reclen - 3; ll++)
64 fread(&buf[ll], 1, 1, infile);
67 if (t == 7)
68 printf("far ");
69 printf((flags & SYM_IMPORT) ? " import" : " extern");
70 if (flags & SYM_FUNCTION)
71 printf(" proc");
72 if (flags & SYM_DATA)
73 printf(" data");
74 printf(": segment %04x = %s\n", translateshort(rs), buf);
75 if (rdf_version == 1)
76 length -= ll + 3;
77 if (rdf_version == 1 && t == 7)
78 printf(" warning: far import not supported in RDOFF1\n");
79 break;
81 case RDFREC_GLOBAL: /* export record */
82 fread(&flags, 1, 1, infile);
83 fread(&s, 1, 1, infile);
84 fread(&o, 4, 1, infile);
85 ll = 0;
87 if (rdf_version == 1) {
88 do {
89 fread(&buf[ll], 1, 1, infile);
90 } while (buf[ll++]);
91 } else {
92 for (; ll < reclen - 6; ll++)
93 fread(&buf[ll], 1, 1, infile);
95 printf((flags & SYM_GLOBAL) ? " export" : " public");
96 if (flags & SYM_FUNCTION)
97 printf(" proc");
98 if (flags & SYM_DATA)
99 printf(" data");
100 printf(": (%04x:%08lx) = %s\n", (int) s, translatelong(o), buf);
101 if (rdf_version == 1)
102 length -= ll + 6;
103 break;
105 case RDFREC_DLL: /* DLL and Module records */
106 case RDFREC_MODNAME:
107 ll = 0;
108 if (rdf_version == 1) {
109 do {
110 fread(&buf[ll], 1, 1, infile);
111 } while (buf[ll++]);
112 } else {
113 for (; ll < reclen; ll++)
114 fread(&buf[ll], 1, 1, infile);
116 if (t == 4)
117 printf(" dll: %s\n", buf);
118 else
119 printf(" module: %s\n", buf);
120 if (rdf_version == 1)
121 length -= ll + 1;
122 break;
124 case RDFREC_BSS: /* BSS reservation */
125 fread(&ll, 4, 1, infile);
126 printf(" bss reservation: %08lx bytes\n", translatelong(ll));
127 if (rdf_version == 1)
128 length -= 5;
129 if (rdf_version > 1 && reclen != 4)
130 printf(" warning: reclen != 4\n");
131 break;
133 case RDFREC_COMMON: {
134 unsigned short seg, align;
135 unsigned long size;
137 fread(&seg, 2, 1, infile);
138 fread(&size, 4, 1, infile);
139 fread(&align, 2, 1, infile);
140 for (ll = 0; ll < reclen - 8; ll++)
141 fread(buf + ll, 1, 1, infile);
142 printf(" common: segment %04x = %s, %ld:%d\n", translateshort(seg),
143 buf, translatelong(size), translateshort(align));
144 break;
147 default:
148 printf(" unrecognized record (type %d", (int) t);
149 if (rdf_version > 1) {
150 printf(", length %d", (int) reclen);
151 fseek(infile, reclen, SEEK_CUR);
152 } else
153 length--;
154 printf(")\n");
156 if (rdf_version != 1)
157 length -= 2 + reclen;
161 int main(int argc, char **argv)
163 char id[7];
164 long l;
165 uint16 s;
166 int verbose = 0;
167 long offset;
168 int foundnullsegment = 0;
169 int version;
170 long segmentcontentlength = 0;
171 int nsegments = 0;
172 long headerlength = 0;
173 long objectlength = 0;
175 printf("RDOFF dump utility, version %s\n", PROGRAM_VERSION);
176 printf("RDOFF2 revision %s\n", RDOFF2_REVISION);
177 puts("Copyright (c) 1996,99 Julian R Hall\n"
178 "Improvements and fixes (c) 2002-2004 RET & COM Research.");
180 if (argc < 2) {
181 fputs("Usage: rdfdump [-v] <filename>\n", stderr);
182 exit(1);
185 if (!strcmp(argv[1], "-v")) {
186 verbose = 1;
187 if (argc < 3) {
188 fputs("required parameter missing\n", stderr);
189 exit(1);
191 argv++;
194 infile = fopen(argv[1], "rb");
195 if (!infile) {
196 fprintf(stderr, "rdfdump: Could not open %s\n", argv[1]);
197 exit(1);
200 fread(id, 6, 1, infile);
201 if (strncmp(id, "RDOFF", 5)) {
202 fputs("rdfdump: File does not contain valid RDOFF header\n", stderr);
203 exit(1);
206 printf("File %s: RDOFF version %c\n\n", argv[1], id[5]);
207 if (id[5] < '1' || id[5] > '2') {
208 fprintf(stderr, "rdfdump: unknown RDOFF version '%c'\n", id[5]);
209 exit(1);
211 version = id[5] - '0';
213 if (version > 1) {
214 fread(&l, 4, 1, infile);
215 objectlength = translatelong(l);
216 printf("Object content size: %ld bytes\n", objectlength);
219 fread(&l, 4, 1, infile);
220 headerlength = translatelong(l);
221 printf("Header (%ld bytes):\n", headerlength);
222 print_header(headerlength, version);
224 if (version == 1) {
225 fread(&l, 4, 1, infile);
226 l = translatelong(l);
227 printf("\nText segment length = %ld bytes\n", l);
228 offset = 0;
229 while (l--) {
230 fread(id, 1, 1, infile);
231 if (verbose) {
232 if (offset % 16 == 0)
233 printf("\n%08lx ", offset);
234 printf(" %02x", (int) (unsigned char) id[0]);
235 offset++;
238 if (verbose)
239 printf("\n\n");
241 fread(&l, 4, 1, infile);
242 l = translatelong(l);
243 printf("Data segment length = %ld bytes\n", l);
245 if (verbose) {
246 offset = 0;
247 while (l--) {
248 fread(id, 1, 1, infile);
249 if (offset % 16 == 0)
250 printf("\n%08lx ", offset);
251 printf(" %02x", (int) (unsigned char) id[0]);
252 offset++;
254 printf("\n");
256 } else {
257 do {
258 fread(&s, 2, 1, infile);
259 s = translateshort(s);
260 if (!s) {
261 printf("\nNULL segment\n");
262 foundnullsegment = 1;
263 break;
265 printf("\nSegment:\n Type = %04X (%s)\n", (int) s, translatesegmenttype(s));
266 nsegments++;
268 fread(&s, 2, 1, infile);
269 printf(" Number = %04X\n", (int) translateshort(s));
270 fread(&s, 2, 1, infile);
271 printf(" Resrvd = %04X\n", (int) translateshort(s));
272 fread(&l, 4, 1, infile);
273 l = translatelong(l);
274 printf(" Length = %ld bytes\n", l);
275 segmentcontentlength += l;
277 offset = 0;
278 while (l--) {
279 fread(id, 1, 1, infile);
280 if (verbose) {
281 if (offset % 16 == 0)
282 printf("\n%08lx ", offset);
283 printf(" %02x", (int) (unsigned char) id[0]);
284 offset++;
287 if (verbose)
288 printf("\n");
289 } while (!feof(infile));
290 if (!foundnullsegment)
291 printf("\nWarning: unexpected end of file - " "NULL segment not found\n");
293 printf("\nTotal number of segments: %d\n", nsegments);
294 printf("Total segment content length: %ld bytes\n", segmentcontentlength);
296 /* calculate what the total object content length should have been */
297 l = segmentcontentlength + 10 * (nsegments + 1) + headerlength + 4;
298 if (l != objectlength)
299 printf("Warning: actual object length (%ld) != " "stored object length (%ld)\n", l, objectlength);
301 fclose(infile);
302 return 0;