Update dependencies
[nasm/avx512.git] / rdoff / rdf2bin.c
blobaee6afb2e8c7f960a317e94ad98d7bb579450480
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 == '/') lastslash = i;
22 i++;
24 return lastslash + 1;
27 int main(int argc, char **argv)
29 rdfmodule * m;
30 int tmp;
31 FILE *of;
32 char * padding;
33 int codepad, datapad, bsspad=0;
35 if (argc < 2) {
36 puts("Usage: rdf2bin [-o relocation-origin] [-p segment-alignment] "
37 "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;
92 else
93 codepad = 0;
95 m->bssrel = m->datarel + m->f.seg[1].length;
96 if (m->bssrel % align != 0) {
97 datapad = align - (m->bssrel % align);
98 m->bssrel += datapad;
100 else
101 datapad = 0;
103 printf("code: %08lx\ndata: %08lx\nbss: %08lx\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) != m->f.seg[0].length ||
123 fwrite(padding,1,codepad,of) != codepad ||
124 fwrite(m->d,1,m->f.seg[1].length,of) != m->f.seg[1].length)
126 fprintf(stderr,"rdf2bin: error writing to %s\n", *argv);
127 return 1;
130 if (bsspad) {
131 void *p = calloc(bsspad-=(m->bssrel - origin),1);
132 fwrite(p,1,bsspad,of);
135 fclose(of);
136 return 0;