Move config.h to a subdirectory, add MSVC-specific config file
[nasm.git] / rdoff / rdf2bin.c
blob1e33075c1175b144d48cea00a59f5d3d12f1b15b
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 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
9 * conditions are met:
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 * rdf2bin.c - convert an RDOFF object file to flat binary
38 #include "compiler.h"
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <errno.h>
46 #include "rdfload.h"
47 #include "nasmlib.h"
49 const char *progname;
51 static uint32_t origin = 0;
52 static bool origin_def = false;
53 static uint32_t align = 16;
54 static bool align_def = false;
56 struct output_format {
57 const char *name;
58 const char *mode;
59 int (*init)(FILE *f);
60 int (*output)(FILE *f, void *data, uint32_t bytes, uint32_t where);
61 int (*fini)(FILE *f);
64 static int null_init_fini(FILE *f)
66 (void)f;
67 return 0;
70 static int com_init(FILE *f)
72 (void)f;
73 if (!origin_def)
74 origin = 0x100;
75 return 0;
78 static int output_bin(FILE *f, void *data, uint32_t bytes, uint32_t where)
80 static uint32_t offset = 0; /* Current file offset, if applicable */
81 size_t pad;
83 if (where-origin < offset) {
84 fprintf(stderr, "%s: internal error: backwards movement\n", progname);
85 exit(1);
88 pad = (where-origin) - offset;
89 fwritezero(pad, f);
90 offset += pad;
92 if (fwrite(data, 1, bytes, f) != bytes)
93 return -1;
94 offset += bytes;
96 return 0;
99 static int write_ith_record(FILE *f, unsigned int len, uint16_t addr,
100 uint8_t type, void *data)
102 char buf[1+2+4+2+255*2+2+2];
103 char *p = buf;
104 uint8_t csum, *dptr = data;
105 unsigned int i;
107 if (len > 255) {
108 fprintf(stderr, "%s: internal error: invalid ith record size\n",
109 progname);
110 exit(1);
113 csum = len + addr + (addr >> 8) + type;
114 for (i = 0; i < len; i++)
115 csum += dptr[i];
116 csum = -csum;
118 p += sprintf(p, ":%02X%04X%02X", len, addr, type);
119 for (i = 0; i < len; i++)
120 p += sprintf(p, "%02X", dptr[i]);
121 p += sprintf(p, "%02X\n", csum);
123 if (fwrite(buf, 1, p-buf, f) != (size_t)(p-buf))
124 return -1;
126 return 0;
129 static int output_ith(FILE *f, void *data, uint32_t bytes, uint32_t where)
131 static uint32_t last = 0; /* Last address written */
132 uint8_t abuf[2];
133 uint8_t *dbuf = data;
134 uint32_t chunk;
136 while (bytes) {
137 if ((where ^ last) & ~0xffff) {
138 abuf[0] = where >> 24;
139 abuf[1] = where >> 16;
140 if (write_ith_record(f, 2, 0, 4, abuf))
141 return -1;
144 /* Output up to 32 bytes, but always end on an aligned boundary */
145 chunk = 32 - (where & 31);
146 if (bytes < chunk)
147 chunk = bytes;
149 if (write_ith_record(f, chunk, (uint16_t)where, 0, dbuf))
150 return -1;
152 dbuf += chunk;
153 last = where + chunk - 1;
154 where += chunk;
155 bytes -= chunk;
157 return 0;
160 static int fini_ith(FILE *f)
162 /* XXX: entry point? */
163 return write_ith_record(f, 0, 0, 1, NULL);
166 static int write_srecord(FILE *f, unsigned int len, unsigned int alen,
167 uint32_t addr, uint8_t type, void *data)
169 char buf[2+2+8+255*2+2+2];
170 char *p = buf;
171 uint8_t csum, *dptr = data;
172 unsigned int i;
174 if (len > 255) {
175 fprintf(stderr, "%s: internal error: invalid srec record size\n",
176 progname);
177 exit(1);
180 switch (alen) {
181 case 2:
182 addr &= 0xffff;
183 break;
184 case 3:
185 addr &= 0xffffff;
186 break;
187 case 4:
188 break;
189 default:
190 fprintf(stderr, "%s: internal error: invalid srec address length\n",
191 progname);
192 exit(1);
195 csum = (len+alen+1) + addr + (addr >> 8) + (addr >> 16) + (addr >> 24);
196 for (i = 0; i < len; i++)
197 csum += dptr[i];
198 csum = 0xff-csum;
200 p += sprintf(p, "S%c%02X%0*X", type, len+alen+1, alen*2, addr);
201 for (i = 0; i < len; i++)
202 p += sprintf(p, "%02X", dptr[i]);
203 p += sprintf(p, "%02X\n", csum);
205 if (fwrite(buf, 1, p-buf, f) != (size_t)(p-buf))
206 return -1;
208 return 0;
211 static int init_srec(FILE *f)
213 return write_srecord(f, 0, 2, 0, '0', NULL);
216 static int fini_srec(FILE *f)
218 /* XXX: entry point? */
219 return write_srecord(f, 0, 4, 0, '7', NULL);
222 static int output_srec(FILE *f, void *data, uint32_t bytes, uint32_t where)
224 uint8_t *dbuf = data;
225 unsigned int chunk;
227 while (bytes) {
228 /* Output up to 32 bytes, but always end on an aligned boundary */
229 chunk = 32 - (where & 31);
230 if (bytes < chunk)
231 chunk = bytes;
233 if (write_srecord(f, chunk, 4, where, '3', dbuf))
234 return -1;
236 dbuf += chunk;
237 where += chunk;
238 bytes -= chunk;
240 return 0;
243 static struct output_format output_formats[] = {
244 { "bin", "wb", null_init_fini, output_bin, null_init_fini },
245 { "com", "wb", com_init, output_bin, null_init_fini },
246 { "ith", "wt", null_init_fini, output_ith, fini_ith },
247 { "ihx", "wt", null_init_fini, output_ith, fini_ith },
248 { "srec", "wt", init_srec, output_srec, fini_srec },
249 { NULL, NULL, NULL, NULL, NULL }
252 static const char *getformat(const char *pathname)
254 const char *p;
255 static char fmt_buf[16];
258 * Search backwards for the string "rdf2" followed by a string
259 * of alphanumeric characters. This should handle path prefixes,
260 * as well as extensions (e.g. C:\FOO\RDF2SREC.EXE).
262 for (p = strchr(pathname, '\0')-1 ; p >= pathname ; p--) {
263 if (!nasm_stricmp(p, "rdf2")) {
264 const char *q = p+4;
265 char *r = fmt_buf;
266 while (isalnum(*q) && r < fmt_buf+sizeof fmt_buf-1)
267 *r++ = *q++;
268 *r = '\0';
269 if (fmt_buf[0])
270 return fmt_buf;
273 return NULL;
276 static void usage(void)
278 fprintf(stderr,
279 "Usage: %s [options] input-file output-file\n"
280 "Options:\n"
281 " -o origin Specify the relocation origin\n"
282 " -p alignment Specify minimum segment alignment\n"
283 " -f format Select format (bin, com, ith, srec)\n"
284 " -q Run quiet\n"
285 " -v Run verbose\n",
286 progname);
289 int main(int argc, char **argv)
291 rdfmodule *m;
292 bool err;
293 FILE *of;
294 int codepad, datapad;
295 const char *format = NULL;
296 const struct output_format *fmt;
297 bool quiet = false;
299 progname = argv[0];
301 if (argc < 2) {
302 usage();
303 return 1;
306 argv++, argc--;
308 while (argc > 2) {
309 if (argv[0][0] == '-' && argv[0][1] && !argv[0][2]) {
310 switch (argv[0][1]) {
311 case 'o':
312 argv++, argc--;
313 origin = readnum(*argv, &err);
314 if (err) {
315 fprintf(stderr, "%s: invalid parameter: %s\n",
316 progname, *argv);
317 return 1;
319 origin_def = true;
320 break;
321 case 'p':
322 argv++, argc--;
323 align = readnum(*argv, &err);
324 if (err) {
325 fprintf(stderr, "%s: invalid parameter: %s\n",
326 progname, *argv);
327 return 1;
329 align_def = true;
330 break;
331 case 'f':
332 argv++, argc--;
333 format = *argv;
334 break;
335 case 'q':
336 quiet = true;
337 break;
338 case 'v':
339 quiet = false;
340 break;
341 case 'h':
342 usage();
343 return 0;
344 default:
345 fprintf(stderr, "%s: unknown option: %s\n",
346 progname, *argv);
347 return 1;
350 argv++, argc--;
353 if (argc < 2) {
354 usage();
355 return 1;
358 if (!format)
359 format = getformat(progname);
361 if (!format) {
362 fprintf(stderr, "%s: unable to determine desired output format\n",
363 progname);
364 return 1;
367 for (fmt = output_formats; fmt->name; fmt++) {
368 if (!nasm_stricmp(format, fmt->name))
369 break;
372 if (!fmt->name) {
373 fprintf(stderr, "%s: unknown output format: %s\n", progname, format);
374 return 1;
377 m = rdfload(*argv);
379 if (!m) {
380 rdfperror(progname, *argv);
381 return 1;
384 if (!quiet)
385 printf("relocating %s: origin=%"PRIx32", align=%d\n",
386 *argv, origin, align);
388 m->textrel = origin;
389 m->datarel = origin + m->f.seg[0].length;
390 if (m->datarel % align != 0) {
391 codepad = align - (m->datarel % align);
392 m->datarel += codepad;
393 } else
394 codepad = 0;
396 m->bssrel = m->datarel + m->f.seg[1].length;
397 if (m->bssrel % align != 0) {
398 datapad = align - (m->bssrel % align);
399 m->bssrel += datapad;
400 } else
401 datapad = 0;
403 if (!quiet)
404 printf("code: %08"PRIx32"\ndata: %08"PRIx32"\nbss: %08"PRIx32"\n",
405 m->textrel, m->datarel, m->bssrel);
407 rdf_relocate(m);
409 argv++;
411 of = fopen(*argv, fmt->mode);
412 if (!of) {
413 fprintf(stderr, "%s: could not open output file %s: %s\n",
414 progname, *argv, strerror(errno));
415 return 1;
418 if (fmt->init(of) ||
419 fmt->output(of, m->t, m->f.seg[0].length, m->textrel) ||
420 fmt->output(of, m->d, m->f.seg[1].length, m->datarel) ||
421 fmt->fini(of)) {
422 fprintf(stderr, "%s: error writing to %s: %s\n",
423 progname, *argv, strerror(errno));
424 return 1;
427 fclose(of);
428 return 0;