Apply Nindent to all .c and .h files
[nasm/avx512.git] / rdoff / rdf2bin.c
blobac51c0efbe498fe15e1b212c0480f1094f0351bb
1 /*
2 * rdf2bin.c - convert an RDOFF object file to flat binary
3 */
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
9 #include "rdfload.h"
10 #include "nasmlib.h"
12 long origin = 0;
13 int align = 16;
15 char *getfilename(char *pathname)
17 char *lastslash = pathname - 1;
18 char *i = pathname;
20 while (*i) {
21 if (*i == '/')
22 lastslash = i;
23 i++;
25 return lastslash + 1;
28 int main(int argc, char **argv)
30 rdfmodule *m;
31 int tmp;
32 FILE *of;
33 char *padding;
34 int codepad, datapad, bsspad = 0;
36 if (argc < 2) {
37 puts("Usage: rdf2bin [-o relocation-origin] [-p segment-alignment] " "input-file output-file");
38 puts(" rdf2com [-p segment-alignment] input-file output-file");
39 return 1;
42 if (!nasm_stricmp(getfilename(*argv), "rdf2com")) {
43 origin = 0x100;
45 argv++, argc--;
47 while (argc > 2) {
48 if (!strcmp(*argv, "-o")) {
49 argv++, argc--;
50 origin = readnum(*argv, &tmp);
51 if (tmp) {
52 fprintf(stderr, "rdf2bin: invalid parameter: %s\n", *argv);
53 return 1;
55 } else if (!strcmp(*argv, "-p")) {
56 argv++, argc--;
57 align = readnum(*argv, &tmp);
58 if (tmp) {
59 fprintf(stderr, "rdf2bin: invalid parameter: %s\n", *argv);
60 return 1;
62 } else if (!strcmp(*argv, "-b")) {
63 argv++, argc--;
64 bsspad = readnum(*argv, &tmp);
65 if (tmp) {
66 fprintf(stderr, "rdf2bin: invalid parameter: %s\n", *argv);
67 return 1;
69 } else
70 break;
72 argv++, argc--;
74 if (argc < 2) {
75 puts("rdf2bin: required parameter missing");
76 return -1;
78 m = rdfload(*argv);
80 if (!m) {
81 rdfperror("rdf2bin", *argv);
82 return 1;
84 printf("relocating %s: origin=%lx, align=%d\n", *argv, origin, align);
86 m->textrel = origin;
87 m->datarel = origin + m->f.seg[0].length;
88 if (m->datarel % align != 0) {
89 codepad = align - (m->datarel % align);
90 m->datarel += codepad;
91 } else
92 codepad = 0;
94 m->bssrel = m->datarel + m->f.seg[1].length;
95 if (m->bssrel % align != 0) {
96 datapad = align - (m->bssrel % align);
97 m->bssrel += datapad;
98 } else
99 datapad = 0;
101 printf("code: %08lx\ndata: %08lx\nbss: %08lx\n",
102 m->textrel, m->datarel, m->bssrel);
104 rdf_relocate(m);
106 argv++;
108 of = fopen(*argv, "wb");
109 if (!of) {
110 fprintf(stderr, "rdf2bin: could not open output file %s\n", *argv);
111 return 1;
114 padding = malloc(align);
115 if (!padding) {
116 fprintf(stderr, "rdf2bin: out of memory\n");
117 return 1;
120 if (fwrite(m->t, 1, m->f.seg[0].length, of) != m->f.seg[0].length ||
121 fwrite(padding, 1, codepad, of) != codepad ||
122 fwrite(m->d, 1, m->f.seg[1].length, of) != m->f.seg[1].length) {
123 fprintf(stderr, "rdf2bin: error writing to %s\n", *argv);
124 return 1;
127 if (bsspad) {
128 void *p = calloc(bsspad -= (m->bssrel - origin), 1);
129 fwrite(p, 1, bsspad, of);
132 fclose(of);
133 return 0;