Add copyright verbiage to Perl scripts; update LICENSE
[nasm/sigaren-mirror.git] / rdoff / rdf2bin.c
blobc23b18c4b1d2dcaed392fdb3b164c80fc0b95fe1
1 /*
2 * rdf2bin.c - convert an RDOFF object file to flat binary
3 */
5 #include "compiler.h"
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
11 #include "rdfload.h"
12 #include "nasmlib.h"
14 int32_t origin = 0;
15 int align = 16;
17 char *getfilename(char *pathname)
19 char *lastslash = pathname - 1;
20 char *i = pathname;
22 while (*i) {
23 if (*i == '/')
24 lastslash = i;
25 i++;
27 return lastslash + 1;
30 int main(int argc, char **argv)
32 rdfmodule *m;
33 bool err;
34 FILE *of;
35 char *padding;
36 int codepad, datapad, bsspad = 0;
38 if (argc < 2) {
39 puts("Usage: rdf2bin [-o relocation-origin] [-p segment-alignment] " "input-file output-file");
40 puts(" rdf2com [-p segment-alignment] input-file output-file");
41 return 1;
44 if (!nasm_stricmp(getfilename(*argv), "rdf2com")) {
45 origin = 0x100;
47 argv++, argc--;
49 while (argc > 2) {
50 if (!strcmp(*argv, "-o")) {
51 argv++, argc--;
52 origin = readnum(*argv, &err);
53 if (err) {
54 fprintf(stderr, "rdf2bin: invalid parameter: %s\n", *argv);
55 return 1;
57 } else if (!strcmp(*argv, "-p")) {
58 argv++, argc--;
59 align = readnum(*argv, &err);
60 if (err) {
61 fprintf(stderr, "rdf2bin: invalid parameter: %s\n", *argv);
62 return 1;
64 } else if (!strcmp(*argv, "-b")) {
65 argv++, argc--;
66 bsspad = readnum(*argv, &err);
67 if (err) {
68 fprintf(stderr, "rdf2bin: invalid parameter: %s\n", *argv);
69 return 1;
71 } else
72 break;
74 argv++, argc--;
76 if (argc < 2) {
77 puts("rdf2bin: required parameter missing");
78 return -1;
80 m = rdfload(*argv);
82 if (!m) {
83 rdfperror("rdf2bin", *argv);
84 return 1;
86 printf("relocating %s: origin=%"PRIx32", align=%d\n", *argv, origin, align);
88 m->textrel = origin;
89 m->datarel = origin + m->f.seg[0].length;
90 if (m->datarel % align != 0) {
91 codepad = align - (m->datarel % align);
92 m->datarel += codepad;
93 } else
94 codepad = 0;
96 m->bssrel = m->datarel + m->f.seg[1].length;
97 if (m->bssrel % align != 0) {
98 datapad = align - (m->bssrel % align);
99 m->bssrel += datapad;
100 } else
101 datapad = 0;
103 printf("code: %08"PRIx32"\ndata: %08"PRIx32"\nbss: %08"PRIx32"\n",
104 m->textrel, m->datarel, m->bssrel);
106 rdf_relocate(m);
108 argv++;
110 of = fopen(*argv, "wb");
111 if (!of) {
112 fprintf(stderr, "rdf2bin: could not open output file %s\n", *argv);
113 return 1;
116 padding = malloc(align);
117 if (!padding) {
118 fprintf(stderr, "rdf2bin: out of memory\n");
119 return 1;
122 if (fwrite(m->t, 1, m->f.seg[0].length, of) != (size_t)m->f.seg[0].length ||
123 fwrite(padding, 1, codepad, of) != (size_t)codepad ||
124 fwrite(m->d, 1, m->f.seg[1].length, of) != (size_t)m->f.seg[1].length) {
125 fprintf(stderr, "rdf2bin: error writing to %s\n", *argv);
126 return 1;
129 if (bsspad) {
130 void *p = calloc(bsspad -= (m->bssrel - origin), 1);
131 fwrite(p, 1, bsspad, of);
134 fclose(of);
135 return 0;