Update for newer RH releases
[nasm/avx512.git] / rdoff / rdf2bin.c
blob938b4a92e2da2092ef56d17d40d8b817edbf74c3
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, bsspad=0;
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 if (! strcmp(*argv,"-b")) {
62 argv++, argc--;
63 bsspad = readnum(*argv, &tmp);
64 if (tmp) {
65 fprintf(stderr,"rdf2bin: invalid parameter: %s\n",*argv);
66 return 1;
68 } else
69 break;
71 argv++, argc--;
73 if (argc < 2) {
74 puts("rdf2bin: required parameter missing");
75 return -1;
77 m = rdfload(*argv);
79 if (! m) {
80 rdfperror("rdf2bin",*argv);
81 return 1;
83 printf("relocating %s: origin=%lx, align=%d\n",*argv,origin,align);
85 m->textrel = origin;
86 m->datarel = origin + m->f.seg[0].length;
87 if (m->datarel % align != 0) {
88 codepad = align - (m->datarel % align);
89 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;
99 else
100 datapad = 0;
102 printf("code: %08lx\ndata: %08lx\nbss: %08lx\n",
103 m->textrel, m->datarel, m->bssrel);
105 rdf_relocate(m);
107 argv++;
109 of = fopen(*argv,"wb");
110 if (!of) {
111 fprintf(stderr,"rdf2bin: could not open output file %s\n",*argv);
112 return 1;
115 padding = malloc(align);
116 if (!padding) {
117 fprintf(stderr,"rdf2bin: out of memory\n");
118 return 1;
121 if (fwrite(m->t,1,m->f.seg[0].length,of) != m->f.seg[0].length ||
122 fwrite(padding,1,codepad,of) != codepad ||
123 fwrite(m->d,1,m->f.seg[1].length,of) != 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;