NASM 0.94
[nasm/avx512.git] / rdoff / rdf2bin.c
blob97b45b4c0c1a5a39d9fffa74dfa478cd607077ac
1 /* rdf2bin: convert an RDOFF object file to flat binary */
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
7 #include "rdfload.h"
8 #include "rdoff.h"
9 #include "nasmlib.h"
11 long origin = 0;
12 int align = 16;
14 char *getfilename(char * pathname)
16 char * lastslash = pathname - 1;
17 char * i = pathname;
19 while ( *i ) {
20 if (*i == '/') lastslash = i;
21 i++;
23 return lastslash + 1;
26 int main(int argc, char **argv)
28 rdfmodule * m;
29 int tmp;
30 FILE *of;
31 char * padding;
32 int codepad, datapad;
34 if (argc < 2) {
35 puts("Usage: rdf2bin [-o relocation-origin] [-p segment-alignment] "
36 "input-file output-file");
37 puts(" rdf2com [-p segment-alignment] input-file output-file");
38 return 1;
41 if (! nasm_stricmp(getfilename(*argv),"rdf2com")) {
42 origin = 0x100;
44 argv++, argc--;
46 while (argc > 2) {
47 if (! strcmp(*argv,"-o")) {
48 argv++, argc--;
49 origin = readnum(*argv, &tmp);
50 if (tmp) {
51 fprintf(stderr,"rdf2bin: invalid parameter: %s\n",*argv);
52 return 1;
54 } else if (! strcmp(*argv,"-p")) {
55 argv++, argc--;
56 align = readnum(*argv, &tmp);
57 if (tmp) {
58 fprintf(stderr,"rdf2bin: invalid parameter: %s\n",*argv);
59 return 1;
61 } else
62 break;
64 argv++, argc--;
66 if (argc < 2) {
67 puts("rdf2bin: required parameter missing");
68 return -1;
70 m = rdfload(*argv);
72 if (! m)
74 rdfperror("rdf2bin",*argv);
75 return 1;
77 printf("relocating %s: origin=%lx, align=%d\n",*argv,origin,align);
79 m->textrel = origin;
80 m->datarel = origin + m->f.code_len;
81 if (m->datarel % align != 0) {
82 codepad = align - (m->datarel % align);
83 m->datarel += codepad;
85 else
86 codepad = 0;
88 m->bssrel = m->datarel + m->f.data_len;
89 if (m->bssrel % align != 0) {
90 datapad = align - (m->bssrel % align);
91 m->bssrel += datapad;
93 else
94 datapad = 0;
96 printf("code: %08lx\ndata: %08lx\nbss: %08lx\n",
97 m->textrel, m->datarel, m->bssrel);
99 rdf_relocate(m);
101 argv++;
103 of = fopen(*argv,"wb");
104 if (!of) {
105 fprintf(stderr,"rdf2bin: could not open output file %s\n",*argv);
106 return 1;
109 padding = malloc(align);
110 if (!padding) {
111 fprintf(stderr,"rdf2bin: out of memory\n");
112 return 1;
115 if (fwrite(m->t,1,m->f.code_len,of) != m->f.code_len ||
116 fwrite(padding,1,codepad,of) != codepad ||
117 fwrite(m->d,1,m->f.data_len,of) != m->f.data_len)
119 fprintf(stderr,"rdf2bin: error writing to %s\n", *argv);
120 return 1;
123 fclose(of);
124 return 0;