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