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.
46 #define PROGRAM_VERSION "2.3"
50 static void print_header(int32_t length
, int rdf_version
)
52 char buf
[129], t
, l
, s
, flags
;
58 nasm_read(&t
, 1, infile
);
59 if (rdf_version
>= 2) {
60 nasm_read(&reclen
, 1, infile
);
63 case RDFREC_GENERIC
: /* generic record */
64 printf(" generic record (length=%d)\n", (int)reclen
);
65 fseek(infile
, reclen
, SEEK_CUR
);
68 case RDFREC_RELOC
: /* relocation record */
69 case RDFREC_SEGRELOC
: /* segment relocation */
70 nasm_read(&s
, 1, infile
);
71 nasm_read(&o
, 4, infile
);
72 nasm_read(&l
, 1, infile
);
73 nasm_read(&rs
, 2, infile
);
74 printf(" %s: location (%04x:%08"PRIx32
"), length %d, "
75 "referred seg %04x\n",
76 t
== 1 ? "relocation" : "seg relocation", (int)s
,
77 translateint32_t(o
), (int)l
, translateint16_t(rs
));
78 if (rdf_version
>= 2 && reclen
!= 8)
79 printf(" warning: reclen != 8\n");
82 if (rdf_version
== 1 && t
== 6)
84 (" warning: seg relocation not supported in RDOFF1\n");
87 case RDFREC_IMPORT
: /* import record */
88 case RDFREC_FARIMPORT
: /* import far symbol */
89 nasm_read(&flags
, 1, infile
);
90 nasm_read(&rs
, 2, infile
);
93 if (rdf_version
== 1) {
95 nasm_read(&buf
[ll
], 1, infile
);
98 for (; ll
< reclen
- 3; ll
++)
99 nasm_read(&buf
[ll
], 1, infile
);
104 printf((flags
& SYM_IMPORT
) ? " import" : " extern");
105 if (flags
& SYM_FUNCTION
)
107 if (flags
& SYM_DATA
)
109 printf(": segment %04x = %s\n", translateint16_t(rs
), buf
);
110 if (rdf_version
== 1)
112 if (rdf_version
== 1 && t
== 7)
114 (" warning: far import not supported in RDOFF1\n");
117 case RDFREC_GLOBAL
: /* export record */
118 nasm_read(&flags
, 1, infile
);
119 nasm_read(&s
, 1, infile
);
120 nasm_read(&o
, 4, infile
);
123 if (rdf_version
== 1) {
125 nasm_read(&buf
[ll
], 1, infile
);
128 for (; ll
< reclen
- 6; ll
++)
129 nasm_read(&buf
[ll
], 1, infile
);
131 printf((flags
& SYM_GLOBAL
) ? " export" : " public");
132 if (flags
& SYM_FUNCTION
)
134 if (flags
& SYM_DATA
)
136 printf(": (%04x:%08"PRIx32
") = %s\n", (int)s
, translateint32_t(o
), buf
);
137 if (rdf_version
== 1)
141 case RDFREC_DLL
: /* DLL and Module records */
144 if (rdf_version
== 1) {
146 nasm_read(&buf
[ll
], 1, infile
);
149 for (; ll
< reclen
; ll
++)
150 nasm_read(&buf
[ll
], 1, infile
);
153 printf(" dll: %s\n", buf
);
155 printf(" module: %s\n", buf
);
156 if (rdf_version
== 1)
160 case RDFREC_BSS
: /* BSS reservation */
161 nasm_read(&ll
, 4, infile
);
162 printf(" bss reservation: %08"PRIx32
" bytes\n", translateint32_t(ll
));
163 if (rdf_version
== 1)
165 if (rdf_version
> 1 && reclen
!= 4)
166 printf(" warning: reclen != 4\n");
173 nasm_read(&seg
, 2, infile
);
174 nasm_read(&size
, 4, infile
);
175 nasm_read(&align
, 2, infile
);
176 for (ll
= 0; ll
< reclen
- 8; ll
++)
177 nasm_read(buf
+ ll
, 1, infile
);
178 printf(" common: segment %04x = %s, %"PRId32
":%d\n",
179 translateint16_t(seg
), buf
, translateint32_t(size
),
180 translateint16_t(align
));
185 printf(" unrecognized record (type %d", (int)t
);
186 if (rdf_version
> 1) {
187 printf(", length %d", (int)reclen
);
188 fseek(infile
, reclen
, SEEK_CUR
);
193 if (rdf_version
!= 1)
194 length
-= 2 + reclen
;
198 int main(int argc
, char **argv
)
205 int foundnullsegment
= 0;
207 int32_t segmentcontentlength
= 0;
209 int32_t headerlength
= 0;
210 int32_t objectlength
= 0;
212 printf("RDOFF dump utility, version %s\n", PROGRAM_VERSION
);
213 printf("RDOFF2 revision %s\n", RDOFF2_REVISION
);
214 puts("Copyright (c) 1996,99 Julian R Hall\n"
215 "Improvements and fixes (c) 2002-2004 RET & COM Research.");
218 fputs("Usage: rdfdump [-v] <filename>\n", stderr
);
224 if (!strcmp(argv
[1], "-v")) {
227 fputs("required parameter missing\n", stderr
);
233 infile
= fopen(argv
[1], "rb");
235 fprintf(stderr
, "rdfdump: Could not open %s\n", argv
[1]);
239 nasm_read(id
, 6, infile
);
240 if (strncmp(id
, "RDOFF", 5)) {
241 fputs("rdfdump: File does not contain valid RDOFF header\n",
246 printf("File %s: RDOFF version %c\n\n", argv
[1], id
[5]);
247 if (id
[5] < '1' || id
[5] > '2') {
248 fprintf(stderr
, "rdfdump: unknown RDOFF version '%c'\n", id
[5]);
251 version
= id
[5] - '0';
254 nasm_read(&l
, 4, infile
);
255 objectlength
= translateint32_t(l
);
256 printf("Object content size: %"PRId32
" bytes\n", objectlength
);
259 nasm_read(&l
, 4, infile
);
260 headerlength
= translateint32_t(l
);
261 printf("Header (%"PRId32
" bytes):\n", headerlength
);
262 print_header(headerlength
, version
);
265 nasm_read(&l
, 4, infile
);
266 l
= translateint32_t(l
);
267 printf("\nText segment length = %"PRId32
" bytes\n", l
);
270 nasm_read(id
, 1, infile
);
272 if (offset
% 16 == 0)
273 printf("\n%08"PRIx32
" ", offset
);
274 printf(" %02x", (int)(uint8_t)id
[0]);
281 nasm_read(&l
, 4, infile
);
282 l
= translateint32_t(l
);
283 printf("Data segment length = %"PRId32
" bytes\n", l
);
288 nasm_read(id
, 1, infile
);
289 if (offset
% 16 == 0)
290 printf("\n%08"PRIx32
" ", offset
);
291 printf(" %02x", (int)(uint8_t)id
[0]);
298 nasm_read(&s
, 2, infile
);
299 s
= translateint16_t(s
);
301 printf("\nNULL segment\n");
302 foundnullsegment
= 1;
305 printf("\nSegment:\n Type = %04X (%s)\n", (int)s
,
306 translatesegmenttype(s
));
309 nasm_read(&s
, 2, infile
);
310 printf(" Number = %04X\n", (int)translateint16_t(s
));
311 nasm_read(&s
, 2, infile
);
312 printf(" Resrvd = %04X\n", (int)translateint16_t(s
));
313 nasm_read(&l
, 4, infile
);
314 l
= translateint32_t(l
);
315 printf(" Length = %"PRId32
" bytes\n", l
);
316 segmentcontentlength
+= l
;
320 nasm_read(id
, 1, infile
);
322 if (offset
% 16 == 0)
323 printf("\n%08"PRIx32
" ", offset
);
324 printf(" %02x", (int)(uint8_t)id
[0]);
330 } while (!feof(infile
));
331 if (!foundnullsegment
)
332 printf("\nWarning: unexpected end of file - "
333 "NULL segment not found\n");
335 printf("\nTotal number of segments: %d\n", nsegments
);
336 printf("Total segment content length: %"PRId32
" bytes\n",
337 segmentcontentlength
);
339 /* calculate what the total object content length should have been */
340 l
= segmentcontentlength
+ 10 * (nsegments
+ 1) + headerlength
+ 4;
341 if (l
!= objectlength
)
342 printf("Warning: actual object length (%"PRId32
") != "
343 "stored object length (%"PRId32
")\n", l
, objectlength
);