1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2014 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * rdfdump.c - dump RDOFF file header.
45 #define PROGRAM_VERSION "2.3"
49 static void print_header(int32_t length
, int rdf_version
)
51 char buf
[129], t
, l
, s
, flags
;
57 nasm_read(&t
, 1, infile
);
58 if (rdf_version
>= 2) {
59 nasm_read(&reclen
, 1, infile
);
62 case RDFREC_GENERIC
: /* generic record */
63 printf(" generic record (length=%d)\n", (int)reclen
);
64 fseek(infile
, reclen
, SEEK_CUR
);
67 case RDFREC_RELOC
: /* relocation record */
68 case RDFREC_SEGRELOC
: /* segment relocation */
69 nasm_read(&s
, 1, infile
);
70 nasm_read(&o
, 4, infile
);
71 nasm_read(&l
, 1, infile
);
72 nasm_read(&rs
, 2, infile
);
73 printf(" %s: location (%04x:%08"PRIx32
"), length %d, "
74 "referred seg %04x\n",
75 t
== 1 ? "relocation" : "seg relocation", (int)s
,
76 translateint32_t(o
), (int)l
, translateint16_t(rs
));
77 if (rdf_version
>= 2 && reclen
!= 8)
78 printf(" warning: reclen != 8\n");
81 if (rdf_version
== 1 && t
== 6)
83 (" warning: seg relocation not supported in RDOFF1\n");
86 case RDFREC_IMPORT
: /* import record */
87 case RDFREC_FARIMPORT
: /* import far symbol */
88 nasm_read(&flags
, 1, infile
);
89 nasm_read(&rs
, 2, infile
);
92 if (rdf_version
== 1) {
94 nasm_read(&buf
[ll
], 1, infile
);
97 for (; ll
< reclen
- 3; ll
++)
98 nasm_read(&buf
[ll
], 1, infile
);
103 printf((flags
& SYM_IMPORT
) ? " import" : " extern");
104 if (flags
& SYM_FUNCTION
)
106 if (flags
& SYM_DATA
)
108 printf(": segment %04x = %s\n", translateint16_t(rs
), buf
);
109 if (rdf_version
== 1)
111 if (rdf_version
== 1 && t
== 7)
113 (" warning: far import not supported in RDOFF1\n");
116 case RDFREC_GLOBAL
: /* export record */
117 nasm_read(&flags
, 1, infile
);
118 nasm_read(&s
, 1, infile
);
119 nasm_read(&o
, 4, infile
);
122 if (rdf_version
== 1) {
124 nasm_read(&buf
[ll
], 1, infile
);
127 for (; ll
< reclen
- 6; ll
++)
128 nasm_read(&buf
[ll
], 1, infile
);
130 printf((flags
& SYM_GLOBAL
) ? " export" : " public");
131 if (flags
& SYM_FUNCTION
)
133 if (flags
& SYM_DATA
)
135 printf(": (%04x:%08"PRIx32
") = %s\n", (int)s
, translateint32_t(o
), buf
);
136 if (rdf_version
== 1)
140 case RDFREC_DLL
: /* DLL and Module records */
143 if (rdf_version
== 1) {
145 nasm_read(&buf
[ll
], 1, infile
);
148 for (; ll
< reclen
; ll
++)
149 nasm_read(&buf
[ll
], 1, infile
);
152 printf(" dll: %s\n", buf
);
154 printf(" module: %s\n", buf
);
155 if (rdf_version
== 1)
159 case RDFREC_BSS
: /* BSS reservation */
160 nasm_read(&ll
, 4, infile
);
161 printf(" bss reservation: %08"PRIx32
" bytes\n", translateint32_t(ll
));
162 if (rdf_version
== 1)
164 if (rdf_version
> 1 && reclen
!= 4)
165 printf(" warning: reclen != 4\n");
172 nasm_read(&seg
, 2, infile
);
173 nasm_read(&size
, 4, infile
);
174 nasm_read(&align
, 2, infile
);
175 for (ll
= 0; ll
< reclen
- 8; ll
++)
176 nasm_read(buf
+ ll
, 1, infile
);
177 printf(" common: segment %04x = %s, %"PRId32
":%d\n",
178 translateint16_t(seg
), buf
, translateint32_t(size
),
179 translateint16_t(align
));
184 printf(" unrecognized record (type %d", (int)t
);
185 if (rdf_version
> 1) {
186 printf(", length %d", (int)reclen
);
187 fseek(infile
, reclen
, SEEK_CUR
);
192 if (rdf_version
!= 1)
193 length
-= 2 + reclen
;
197 int main(int argc
, char **argv
)
204 int foundnullsegment
= 0;
206 int32_t segmentcontentlength
= 0;
208 int32_t headerlength
= 0;
209 int32_t objectlength
= 0;
211 printf("RDOFF dump utility, version %s\n", PROGRAM_VERSION
);
212 printf("RDOFF2 revision %s\n", RDOFF2_REVISION
);
213 puts("Copyright (c) 1996,99 Julian R Hall\n"
214 "Improvements and fixes (c) 2002-2004 RET & COM Research.");
217 fputs("Usage: rdfdump [-v] <filename>\n", stderr
);
223 if (!strcmp(argv
[1], "-v")) {
226 fputs("required parameter missing\n", stderr
);
232 infile
= fopen(argv
[1], "rb");
234 fprintf(stderr
, "rdfdump: Could not open %s\n", argv
[1]);
238 nasm_read(id
, 6, infile
);
239 if (strncmp(id
, "RDOFF", 5)) {
240 fputs("rdfdump: File does not contain valid RDOFF header\n",
245 printf("File %s: RDOFF version %c\n\n", argv
[1], id
[5]);
246 if (id
[5] < '1' || id
[5] > '2') {
247 fprintf(stderr
, "rdfdump: unknown RDOFF version '%c'\n", id
[5]);
250 version
= id
[5] - '0';
253 nasm_read(&l
, 4, infile
);
254 objectlength
= translateint32_t(l
);
255 printf("Object content size: %"PRId32
" bytes\n", objectlength
);
258 nasm_read(&l
, 4, infile
);
259 headerlength
= translateint32_t(l
);
260 printf("Header (%"PRId32
" bytes):\n", headerlength
);
261 print_header(headerlength
, version
);
264 nasm_read(&l
, 4, infile
);
265 l
= translateint32_t(l
);
266 printf("\nText segment length = %"PRId32
" bytes\n", l
);
269 nasm_read(id
, 1, infile
);
271 if (offset
% 16 == 0)
272 printf("\n%08"PRIx32
" ", offset
);
273 printf(" %02x", (int)(uint8_t)id
[0]);
280 nasm_read(&l
, 4, infile
);
281 l
= translateint32_t(l
);
282 printf("Data segment length = %"PRId32
" bytes\n", l
);
287 nasm_read(id
, 1, infile
);
288 if (offset
% 16 == 0)
289 printf("\n%08"PRIx32
" ", offset
);
290 printf(" %02x", (int)(uint8_t)id
[0]);
297 nasm_read(&s
, 2, infile
);
298 s
= translateint16_t(s
);
300 printf("\nNULL segment\n");
301 foundnullsegment
= 1;
304 printf("\nSegment:\n Type = %04X (%s)\n", (int)s
,
305 translatesegmenttype(s
));
308 nasm_read(&s
, 2, infile
);
309 printf(" Number = %04X\n", (int)translateint16_t(s
));
310 nasm_read(&s
, 2, infile
);
311 printf(" Resrvd = %04X\n", (int)translateint16_t(s
));
312 nasm_read(&l
, 4, infile
);
313 l
= translateint32_t(l
);
314 printf(" Length = %"PRId32
" bytes\n", l
);
315 segmentcontentlength
+= l
;
319 nasm_read(id
, 1, infile
);
321 if (offset
% 16 == 0)
322 printf("\n%08"PRIx32
" ", offset
);
323 printf(" %02x", (int)(uint8_t)id
[0]);
329 } while (!feof(infile
));
330 if (!foundnullsegment
)
331 printf("\nWarning: unexpected end of file - "
332 "NULL segment not found\n");
334 printf("\nTotal number of segments: %d\n", nsegments
);
335 printf("Total segment content length: %"PRId32
" bytes\n",
336 segmentcontentlength
);
338 /* calculate what the total object content length should have been */
339 l
= segmentcontentlength
+ 10 * (nsegments
+ 1) + headerlength
+ 4;
340 if (l
!= objectlength
)
341 printf("Warning: actual object length (%"PRId32
") != "
342 "stored object length (%"PRId32
")\n", l
, objectlength
);