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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "config.h"
#include "linker.h"
#include "types.h"
#include "exceptions.h"
#include "hash.h"
/* Les quelques structures de données utiles */
typedef struct object_t {
Uint32 s_text, s_data, s_bss, *text, *data, textstart, datastart, bssstart;
} object_t;
typedef struct symbol_t {
char *name;
int objindex;
Uint32 offset;
int type;
struct symbol_t *next;
} symbol_t;
/* Et les variables globales */
Uint32 startpoint = -1, textsize = 0, datasize = 0, bsssize = 0;
object_t **objects;
symbol_t *symbols, *psymbols;
int objindex = 0, nbrsymbs = 0;
_TableauVariable symbs;
/* Quelques fonctions pour nous simplifier la vie... */
static FILE *openfilewriting(char *name)
{
FILE *f;
if (!(f = fopen(name, "wb"))) {
pushcontext(strerror(errno));
exception(1, _("Error writing file"));
}
return f;
}
static FILE *openfilereading(char *name)
{
FILE *f;
if (!(f = fopen(name, "rb"))) {
pushcontext(strerror(errno));
exception(1, _("Error reading file"));
}
return f;
}
static void writeword(Uint32 a, FILE * f)
{
if (fwrite(&a, sizeof(unsigned long int), 1, f) != 1) {
if (ferror(f)) {
pushcontext(strerror(errno));
}
exception(1, _("Error writing file"));
}
}
static Uint32 readword(FILE * f)
{
Uint32 a;
if (fread(&a, sizeof(a), 1, f) != 1) {
exception(1, _("premature end of file"));
}
return a;
}
static char *readstring(FILE * f)
{
Uint32 s;
char *r;
int i;
s = readword(f);
r = (char *) Emalloc(s + 1);
for (i = 0; i < s; i++) {
r[i] = readword(f);
}
r[i] = '\0';
return r;
}
/* Rajoute un symbole dans la pile */
static void addsymbol(char *name, int offset, int type)
{
symbol_t *newsymbol;
newsymbol = (symbol_t *) Emalloc(sizeof(symbol_t));
newsymbol->next = NULL;
newsymbol->type = type;
newsymbol->offset = offset;
newsymbol->objindex = objindex;
newsymbol->name = name;
psymbols->next = newsymbol;
psymbols = newsymbol;
if (newsymbol->type & 1) {
nbrsymbs++;
} else {
InsererVarDansTab(&symbs, CreerElement(name, newsymbol));
}
}
/* Rajoute un fichier dans les structures */
void addfile(char *nom)
{
FILE *f;
Uint32 start, nbsymbols, type, offset;
int i;
char *snom, errctx[BUFSIZ];
f = openfilereading(nom);
sprintf(errctx, _("Processing file %s"), nom);
pushcontext(errctx);
if (readword(f) != 0x4f424e4e) {
exception(1, _("Bad signature"));
}
readword(f); /* Taille du fichier */
start = readword(f);
if ((startpoint != -1) && (start != -1)) {
exception(1, _("Startpoint already defined."));
}
startpoint = start + textsize;
objects[objindex]->s_text = readword(f);
objects[objindex]->s_data = readword(f);
objects[objindex]->s_bss = readword(f);
readword(f); /* Taille de la table des symboles */
nbsymbols = readword(f);
pushcontext(_("Reading symbols"));
for (i = 0; i < nbsymbols; i++) {
type = readword(f);
offset = readword(f);
snom = readstring(f);
addsymbol(snom, offset, type);
}
popcontext();
objects[objindex]->textstart = textsize;
objects[objindex]->datastart = datasize;
objects[objindex]->bssstart = bsssize;
objects[objindex]->text = (Uint32 *) Emalloc(objects[objindex]->s_text * sizeof(Uint32));
objects[objindex]->data = (Uint32 *) Emalloc(objects[objindex]->s_data * sizeof(Uint32));
pushcontext(_("Reading text and data segments"));
for (i = 0; i < objects[objindex]->s_text; i++) {
objects[objindex]->text[i] = readword(f);
}
for (i = 0; i < objects[objindex]->s_data; i++) {
objects[objindex]->data[i] = readword(f);
}
popcontext();
fclose(f);
textsize += objects[objindex]->s_text;
datasize += objects[objindex]->s_data;
bsssize += objects[objindex]->s_bss;
objindex++;
popcontext();
}
/* Simplification de vie... */
static void dumptab(Uint32 * tab, int s, FILE * f)
{
int i;
for (i = 0; i < s; i++) {
writeword(tab[i], f);
}
}
/* Nous dumpons la mémoire dans le fichier */
static void dumptext(object_t * obj, FILE * f)
{
dumptab(obj->text, obj->s_text, f);
}
static void dumpdata(object_t * obj, FILE * f)
{
dumptab(obj->data, obj->s_data, f);
}
/* Cette fonction va calculer les quelques relogements statiques et dynamiques que nous a laissé l'assembleur */
static void dumprelog(FILE * f)
{
symbol_t *s = symbols, *t;
char trouve, err[BUFSIZ];
Uint32 decal;
for (s = s->next; s; s = s->next) {
if (s->type & 1) {
t = (symbol_t *) NomVarToVar(s->name, symbs, &trouve);
if (!trouve) {
sprintf(err, _("Symbol %s not found"), s->name);
exception(1, err);
}
switch (s->type) {
case 1: /* text */
switch (t->type) {
case 0:
decal = objects[t->objindex]->textstart + t->offset;
break;
case 2:
decal = textsize + objects[t->objindex]->datastart + t->offset;
break;
case 4:
decal = textsize + datasize + objects[t->objindex]->bssstart + t->offset;
break;
default:
exception(1, _("Internal error"));
break;
}
#ifdef DEBUG
fprintf(stderr, "Relogement effectué sur %i (text), de %i octets pour le symbole %s\n", s->offset, decal, s->name);
#endif
objects[s->objindex]->text[s->offset] += decal;
writeword(objects[s->objindex]->textstart + s->offset, f);
break;
case 3: /* data */
switch (t->type) {
case 0:
decal = objects[t->objindex]->textstart + t->offset;
break;
case 2:
decal = textsize + objects[t->objindex]->datastart + t->offset;
break;
case 4:
decal = textsize + datasize + objects[t->objindex]->bssstart + t->offset;
break;
default:
exception(1, _("Internal error"));
break;
}
#ifdef DEBUG
fprintf(stderr, "Relogement effectué sur %i (data), de %i octets pour le symbole %s\n", s->offset, decal, s->name);
#endif
objects[s->objindex]->data[s->offset] += decal;
writeword(textsize + objects[s->objindex]->datastart + s->offset, f);
break;
default:
exception(1, _("Internal error"));
break;
}
}
}
}
/* Cette fonction sert à écrire le fichier de sortie. */
void dumpfile(char *nom)
{
FILE *f;
int i;
pushcontext(_("Writing output file"));
f = openfilewriting(nom);
if (startpoint == -1) {
exception(1, _("No startpoint defined."));
}
pushcontext(_("Writing headers"));
writeword(0x58454e4e, f);
writeword(nbrsymbs + textsize + datasize + 7, f);
writeword(startpoint, f);
writeword(textsize, f);
writeword(datasize, f);
writeword(bsssize, f);
writeword(nbrsymbs, f);
popcontext();
pushcontext(_("Writing relocating informations"));
dumprelog(f);
popcontext();
pushcontext(_("Writing text segments"));
for (i = 0; i < objindex; i++) {
dumptext(objects[i], f);
}
popcontext();
pushcontext(_("Writing data segments"));
for (i = 0; i < objindex; i++) {
dumpdata(objects[i], f);
}
popcontext();
popcontext();
fprintf(stderr, _("Statistics: %i words of text, %i words of data and reserving %i words\n"), textsize, datasize, bsssize);
fprintf(stderr, _("Output file size: %i words containing %i relocating offsets.\n"), ftell(f), nbrsymbs);
fclose(f);
}
/* Fonctions d'initialisations et de libération de mémoire */
void init(int n)
{
int i;
Initialise(&symbs);
objects = (object_t **) Emalloc(n * sizeof(object_t *));
psymbols = symbols = (symbol_t *) Emalloc(sizeof(symbol_t));
for (i = 0; i < n; i++) {
objects[i] = (object_t *) Emalloc(sizeof(object_t));
objects[i]->s_text = objects[i]->s_data = objects[i]->s_bss = objects[i]->textstart = objects[i]->datastart = 0;
objects[i]->text = objects[i]->data = NULL;
}
symbols->next = NULL;
}
void free_symbol(symbol_t * s)
{
if (s->next)
free_symbol(s);
free(s->name);
free(s);
}
void flush(void)
{
int i;
DetruitTab(&symbs);
for (i = 0; i < objindex; i++) {
if (objects[i]->text)
free(objects[i]->text);
if (objects[i]->data) ;
free(objects[i]->data);
free(objects[i]);
}
free(objects);
}
|