-
Notifications
You must be signed in to change notification settings - Fork 2
/
write.c
542 lines (333 loc) · 16 KB
/
write.c
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/******************************************************************************
* @file write.c
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "as.h"
#include "fixup.h"
#include "frag.h"
#include "intel.h"
#include "lib.h"
#include "listing.h"
#include "report.h"
#include "section.h"
#include "stdint.h"
#include "symbol.h"
#include "write.h"
static unsigned long relax_align (unsigned long address, unsigned long alignment) {
unsigned long mask, new_address;
mask = ~(~((uint32_t) 0) << alignment);
new_address = (address + mask) & ~mask;
return new_address - address;
}
static void relax_section (section_t section) {
struct frag *root_frag, *frag;
unsigned long address, frag_count, max_iterations;
unsigned long alignment_needed;
long change;
int changed;
section_set (section);
root_frag = current_frag_chain->first_frag;
address = 0;
for (frag_count = 0, frag = root_frag; frag; frag_count++, frag = frag->next) {
frag->relax_marker = 0;
frag->address = address;
address += frag->fixed_size;
switch (frag->relax_type) {
case RELAX_TYPE_NONE_NEEDED:
break;
case RELAX_TYPE_ALIGN:
case RELAX_TYPE_ALIGN_CODE:
alignment_needed = relax_align (address, frag->offset);
if (frag->relax_subtype != 0 && alignment_needed > frag->relax_subtype) {
alignment_needed = 0;
}
address += alignment_needed;
break;
case RELAX_TYPE_CALL: {
if (frag->symbol) {
unsigned long old_frag_fixed_size = frag->fixed_size;
frag->fixed_size += 4;
address += (frag->fixed_size - old_frag_fixed_size);
}
break;
}
case RELAX_TYPE_ORG:
case RELAX_TYPE_SPACE:
break;
case RELAX_TYPE_MACHINE_DEPENDENT:
address += machine_dependent_estimate_size_before_relax (frag, section);
break;
default:
report_at (__FILE__, __LINE__, REPORT_INTERNAL_ERROR, "invalid relax type");
exit (EXIT_FAILURE);
}
}
/**
* Prevents an infinite loop caused by frag growing because of a symbol that moves when the frag grows.
*
* Example:
*
* .org _abc + 2
* _abc:
*/
max_iterations = frag_count * frag_count;
/* Too many frags might cause an overflow. */
if (max_iterations < frag_count) {
max_iterations = frag_count;
}
do {
change = 0;
changed = 0;
for (frag = root_frag; frag; frag = frag->next) {
long growth = 0;
unsigned long old_address;
unsigned long old_offset;
unsigned long new_offset;
frag->relax_marker = !frag->relax_marker;
old_address = frag->address;
frag->address += change;
switch (frag->relax_type) {
case RELAX_TYPE_NONE_NEEDED:
growth = 0;
break;
case RELAX_TYPE_ALIGN:
case RELAX_TYPE_ALIGN_CODE:
old_offset = relax_align (old_address + frag->fixed_size, frag->offset);
new_offset = relax_align (frag->address + frag->fixed_size, frag->offset);
if (frag->relax_subtype != 0) {
if (old_offset > frag->relax_subtype) {
old_offset = 0;
}
if (new_offset > frag->relax_subtype) {
new_offset = 0;
}
}
growth = new_offset - old_offset;
break;
case RELAX_TYPE_CALL: {
growth = 0;
if (frag->symbol && !frag->symbol_seen) {
frag->symbol_seen = 1;
if (frag->far_call > 0) {
frag->far_call--;
fixup_new (frag, frag->opcode_offset_in_buf, 4, frag->symbol, frag->offset, 0, RELOC_TYPE_CALL, 1);
} else {
fixup_new (frag, frag->opcode_offset_in_buf, 4, frag->symbol, frag->offset, 0, RELOC_TYPE_CALL, state->model >= 4 && state->model < 7);
}
}
break;
}
case RELAX_TYPE_ORG: {
unsigned long target = frag->offset;
if (frag->symbol) {
target += symbol_get_value (frag->symbol);
}
growth = target - (frag->next->address + change);
if (frag->address + frag->fixed_size > target) {
report_at (frag->filename, frag->line_number, REPORT_ERROR, "attempt to move .org backwards");
growth = 0;
/* Changes the frag so no more errors appear because of it. */
frag->relax_type = RELAX_TYPE_ALIGN;
frag->offset = 0;
frag->fixed_size = frag->next->address + change - frag->address;
}
break;
}
case RELAX_TYPE_SPACE:
growth = 0;
if (frag->symbol) {
long amount = symbol_get_value (frag->symbol);
if (symbol_get_section (frag->symbol) != absolute_section || symbol_is_undefined (frag->symbol)) {
report_at (frag->filename, frag->line_number, REPORT_ERROR, ".space specifies non-absolute value");
/* Prevents the error from repeating. */
frag->symbol = NULL;
} else if (amount < 0) {
report_at (frag->filename, frag->line_number, REPORT_WARNING, ".space with negative value, ignoring");
frag->symbol = NULL;
} else {
growth = old_address + frag->fixed_size + amount - frag->next->address;
}
}
break;
case RELAX_TYPE_MACHINE_DEPENDENT:
growth = machine_dependent_relax_frag (frag, section, change);
break;
default:
report_at (__FILE__, __LINE__, REPORT_INTERNAL_ERROR, "invalid relax type");
exit (EXIT_FAILURE);
}
if (growth) {
change += growth;
changed = 1;
}
}
} while (changed && --max_iterations);
if (changed) {
report_at (NULL, 0, REPORT_FATAL_ERROR, "Infinite loop encountered whilst attempting to compute the addresses in section %s", section_get_name (section));
exit (EXIT_FAILURE);
}
}
static void finish_frags_after_relaxation (section_t section) {
struct frag *root_frag, *frag;
section_set (section);
root_frag = current_frag_chain->first_frag;
for (frag = root_frag; frag; frag = frag->next) {
switch (frag->relax_type) {
case RELAX_TYPE_NONE_NEEDED:
break;
case RELAX_TYPE_ALIGN:
case RELAX_TYPE_ALIGN_CODE:
case RELAX_TYPE_ORG:
case RELAX_TYPE_SPACE: {
unsigned char fill, *p;
offset_t i;
frag->offset = frag->next->address - (frag->address + frag->fixed_size);
if (((long) (frag->offset)) < 0) {
report_at (frag->filename, frag->line_number, REPORT_ERROR, "attempt to .org/.space backward (%li)", frag->offset);
frag->offset = 0;
}
p = finished_frag_increase_fixed_size_by_frag_offset (frag);
fill = *p;
for (i = 0; i < frag->offset; i++) {
p[i] = fill;
}
break;
}
case RELAX_TYPE_CALL: {
unsigned char fill = 0, *p;
offset_t i;
frag->offset = frag->next->address - (frag->address + frag->fixed_size);
if (((long) (frag->offset)) < 0) {
frag->offset = 0;
}
p = finished_frag_increase_fixed_size_by_frag_offset (frag);
for (i = 0; i < frag->offset; i++) {
p[i] = fill;
}
break;
}
case RELAX_TYPE_MACHINE_DEPENDENT:
machine_dependent_finish_frag (frag);
break;
default:
report_at (__FILE__, __LINE__, REPORT_INTERNAL_ERROR, "invalid relax type");
exit (EXIT_FAILURE);
}
}
}
static void adjust_reloc_symbols_of_section (section_t section) {
struct fixup *fixup;
section_set (section);
for (fixup = current_frag_chain->first_fixup; fixup; fixup = fixup->next) {
if (fixup->done) { continue; }
if (fixup->add_symbol) {
struct symbol *symbol = fixup->add_symbol;
/* Resolves symbols that have not been resolved yet (expression symbols). */
symbol_resolve_value (symbol);
if (symbol_uses_reloc_symbol (symbol)) {
fixup->add_number += symbol_get_value_expression (symbol)->add_number;
symbol = symbol_get_value_expression (symbol)->add_symbol;
fixup->add_symbol = symbol;
}
if (symbol_force_reloc (symbol)) {
continue;
}
if (symbol_get_section (symbol) == absolute_section) {
continue;
}
fixup->add_number += symbol_get_value (symbol);
fixup->add_symbol = section_symbol (symbol_get_section (symbol));
}
}
}
static unsigned long fixup_section (section_t section) {
struct fixup *fixup;
section_t add_symbol_section;
unsigned long add_number, section_reloc_count = 0;
section_set (section);
for (fixup = current_frag_chain->first_fixup; fixup; fixup = fixup->next) {
if (fixup->done) { continue; }
add_number = fixup->add_number;
if (fixup->add_symbol) {
add_symbol_section = symbol_get_section (fixup->add_symbol);
if ((add_symbol_section == section) && !machine_dependent_force_relocation_local (fixup)) {
add_number += symbol_get_value (fixup->add_symbol);
fixup->add_number = add_number;
if (fixup->pcrel) {
add_number -= machine_dependent_pcrel_from (fixup);
fixup->pcrel = 0;
}
fixup->add_symbol = NULL;
} else if (add_symbol_section == absolute_section || (fixup->reloc_type == RELOC_TYPE_CALL && !symbol_is_undefined (fixup->add_symbol))) {
add_number += symbol_get_value (fixup->add_symbol);
fixup->add_number = add_number;
fixup->add_symbol = NULL;
}
}
if (fixup->pcrel) {
add_number -= machine_dependent_pcrel_from (fixup);
}
machine_dependent_apply_fixup (fixup, add_number);
if (fixup->done == 0) { section_reloc_count++; }
}
return section_reloc_count;
}
void write_object_file (struct object_format *obj_fmt) {
struct symbol *symbol;
section_t section;
value_t val = 0;
sections_chain_subsection_frags ();
for (section = sections; section; section = section_get_next_section (section)) {
relax_section (section);
}
for (section = sections; section; section = section_get_next_section (section)) {
finish_frags_after_relaxation (section);
}
if (state->end_sym) {
struct symbol *symbol;
for (symbol = symbols; symbol; symbol = symbol->next) {
if (!symbol_is_undefined (symbol) && symbol_get_section (symbol) != absolute_section) {
if (strcmp (symbol->name, state->end_sym) == 0) {
val = symbol_get_value (symbol);
break;
}
}
}
if (symbol) {
struct frag *frag;
int found_frag = 0;
section_set (symbol->section);
for (frag = current_frag_chain->first_frag; frag; frag = frag->next) {
if (found_frag) {
frag->address -= val;
continue;
}
if (frag->address + frag->size >= val) {
val -= frag->address;
memcpy (frag->buf, frag->buf + val, frag->fixed_size - val);
frag->fixed_size -= val;
found_frag = 1;
current_frag_chain->first_frag = frag;
continue;
}
}
}
}
if (obj_fmt->adjust_code) {
(obj_fmt->adjust_code) ();
}
finalize_symbols = 1;
for (symbol = symbols; symbol; symbol = symbol->next) {
symbol_resolve_value (symbol);
}
for (section = sections; section; section = section_get_next_section (section)) {
adjust_reloc_symbols_of_section (section);
}
for (section = sections; section; section = section_get_next_section (section)) {
fixup_section (section);
}
if (obj_fmt->write_object) {
(obj_fmt->write_object) ();
}
}