daily update
[deliverable/binutils-gdb.git] / gas / write.c
CommitLineData
252b5132 1/* write.c - emit .o file
4b95cf5c 2 Copyright (C) 1986-2014 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
efaf0ba4 21/* This thing should be set up to do byteordering correctly. But... */
252b5132
RH
22
23#include "as.h"
24#include "subsegs.h"
25#include "obstack.h"
26#include "output-file.h"
220e750f 27#include "dwarf2dbg.h"
e7ff5c73 28#include "libbfd.h"
0acf065b 29#include "compress-debug.h"
252b5132 30
252b5132 31#ifndef TC_FORCE_RELOCATION
a161fe53 32#define TC_FORCE_RELOCATION(FIX) \
ae6063d4 33 (generic_force_reloc (FIX))
252b5132
RH
34#endif
35
a161fe53
AM
36#ifndef TC_FORCE_RELOCATION_ABS
37#define TC_FORCE_RELOCATION_ABS(FIX) \
38 (TC_FORCE_RELOCATION (FIX))
39#endif
40
41#ifndef TC_FORCE_RELOCATION_LOCAL
42#define TC_FORCE_RELOCATION_LOCAL(FIX) \
43 (!(FIX)->fx_pcrel \
a161fe53
AM
44 || TC_FORCE_RELOCATION (FIX))
45#endif
46
47#ifndef TC_FORCE_RELOCATION_SUB_SAME
48#define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
426318c5 49 (! SEG_NORMAL (SEG))
a161fe53
AM
50#endif
51
9a97a5d7
AM
52#ifndef md_register_arithmetic
53# define md_register_arithmetic 1
54#endif
55
a161fe53 56#ifndef TC_FORCE_RELOCATION_SUB_ABS
9a97a5d7
AM
57#define TC_FORCE_RELOCATION_SUB_ABS(FIX, SEG) \
58 (!md_register_arithmetic && (SEG) == reg_section)
a161fe53
AM
59#endif
60
61#ifndef TC_FORCE_RELOCATION_SUB_LOCAL
62#ifdef DIFF_EXPR_OK
5db484ff
AM
63#define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) \
64 (!md_register_arithmetic && (SEG) == reg_section)
a161fe53 65#else
5db484ff 66#define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) 1
a161fe53
AM
67#endif
68#endif
69
70#ifndef TC_VALIDATE_FIX_SUB
71#ifdef UNDEFINED_DIFFERENCE_OK
72/* The PA needs this for PIC code generation. */
5db484ff
AM
73#define TC_VALIDATE_FIX_SUB(FIX, SEG) \
74 (md_register_arithmetic || (SEG) != reg_section)
a161fe53 75#else
5db484ff
AM
76#define TC_VALIDATE_FIX_SUB(FIX, SEG) \
77 ((md_register_arithmetic || (SEG) != reg_section) \
78 && ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
79 || (FIX)->fx_r_type == BFD_RELOC_GPREL16))
a161fe53 80#endif
252b5132
RH
81#endif
82
58a77e41
EC
83#ifndef TC_LINKRELAX_FIXUP
84#define TC_LINKRELAX_FIXUP(SEG) 1
85#endif
86
a161fe53
AM
87#ifndef MD_APPLY_SYM_VALUE
88#define MD_APPLY_SYM_VALUE(FIX) 1
8f36cd18 89#endif
ef99799a 90
87548816
NC
91#ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
92#define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
93#endif
94
252b5132 95#ifndef MD_PCREL_FROM_SECTION
df44284e 96#define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
252b5132
RH
97#endif
98
c9cd7160
L
99#ifndef TC_FAKE_LABEL
100#define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
101#endif
102
d31f0f6d
AM
103/* Positive values of TC_FX_SIZE_SLACK allow a target to define
104 fixups that far past the end of a frag. Having such fixups
105 is of course most most likely a bug in setting fx_size correctly.
106 A negative value disables the fixup check entirely, which is
107 appropriate for something like the Renesas / SuperH SH_COUNT
108 reloc. */
109#ifndef TC_FX_SIZE_SLACK
110#define TC_FX_SIZE_SLACK(FIX) 0
111#endif
112
6386f3a7
AM
113/* Used to control final evaluation of expressions. */
114int finalize_syms = 0;
e46d99eb 115
252b5132 116int symbol_table_frozen;
a161fe53
AM
117
118symbolS *abs_section_sym;
119
26346241
AM
120/* Remember the value of dot when parsing expressions. */
121addressT dot_value;
122
8e723a10
CLT
123/* The frag that dot_value is based from. */
124fragS *dot_frag;
125
05e9452c
AM
126/* Relocs generated by ".reloc" pseudo. */
127struct reloc_list* reloc_list;
128
d7342424 129void print_fixup (fixS *);
252b5132 130
252b5132
RH
131/* We generally attach relocs to frag chains. However, after we have
132 chained these all together into a segment, any relocs we add after
133 that must be attached to a segment. This will include relocs added
134 in md_estimate_size_for_relax, for example. */
135static int frags_chained = 0;
252b5132
RH
136
137static int n_fixups;
138
df44284e 139#define RELOC_ENUM enum bfd_reloc_code_real
df44284e 140
efaf0ba4
NC
141/* Create a fixS in obstack 'notes'. */
142
252b5132 143static fixS *
d7342424
KH
144fix_new_internal (fragS *frag, /* Which frag? */
145 int where, /* Where in that frag? */
146 int size, /* 1, 2, or 4 usually. */
147 symbolS *add_symbol, /* X_add_symbol. */
148 symbolS *sub_symbol, /* X_op_symbol. */
149 offsetT offset, /* X_add_number. */
150 int pcrel, /* TRUE if PC-relative relocation. */
032fbb7a 151 RELOC_ENUM r_type /* Relocation type. */,
c865e45b 152 int at_beginning) /* Add to the start of the list? */
252b5132
RH
153{
154 fixS *fixP;
155
156 n_fixups++;
157
1e9cc1c2 158 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
252b5132
RH
159
160 fixP->fx_frag = frag;
161 fixP->fx_where = where;
162 fixP->fx_size = size;
163 /* We've made fx_size a narrow field; check that it's wide enough. */
164 if (fixP->fx_size != size)
165 {
166 as_bad (_("field fx_size too small to hold %d"), size);
167 abort ();
168 }
169 fixP->fx_addsy = add_symbol;
170 fixP->fx_subsy = sub_symbol;
171 fixP->fx_offset = offset;
26346241 172 fixP->fx_dot_value = dot_value;
8e723a10 173 fixP->fx_dot_frag = dot_frag;
252b5132 174 fixP->fx_pcrel = pcrel;
252b5132 175 fixP->fx_r_type = r_type;
252b5132
RH
176 fixP->fx_im_disp = 0;
177 fixP->fx_pcrel_adjust = 0;
178 fixP->fx_bit_fixP = 0;
179 fixP->fx_addnumber = 0;
180 fixP->fx_tcbit = 0;
20ee54e8 181 fixP->fx_tcbit2 = 0;
252b5132
RH
182 fixP->fx_done = 0;
183 fixP->fx_no_overflow = 0;
184 fixP->fx_signed = 0;
185
186#ifdef USING_CGEN
187 fixP->fx_cgen.insn = NULL;
188 fixP->fx_cgen.opinfo = 0;
189#endif
190
191#ifdef TC_FIX_TYPE
efaf0ba4 192 TC_INIT_FIX_DATA (fixP);
252b5132
RH
193#endif
194
195 as_where (&fixP->fx_file, &fixP->fx_line);
196
252b5132
RH
197 {
198
252b5132
RH
199 fixS **seg_fix_rootP = (frags_chained
200 ? &seg_info (now_seg)->fix_root
201 : &frchain_now->fix_root);
202 fixS **seg_fix_tailP = (frags_chained
203 ? &seg_info (now_seg)->fix_tail
204 : &frchain_now->fix_tail);
252b5132 205
c865e45b
RS
206 if (at_beginning)
207 {
208 fixP->fx_next = *seg_fix_rootP;
209 *seg_fix_rootP = fixP;
210 if (fixP->fx_next == NULL)
211 *seg_fix_tailP = fixP;
212 }
252b5132 213 else
c865e45b
RS
214 {
215 fixP->fx_next = NULL;
216 if (*seg_fix_tailP)
217 (*seg_fix_tailP)->fx_next = fixP;
218 else
219 *seg_fix_rootP = fixP;
220 *seg_fix_tailP = fixP;
221 }
252b5132
RH
222 }
223
224 return fixP;
225}
226
227/* Create a fixup relative to a symbol (plus a constant). */
228
229fixS *
d7342424
KH
230fix_new (fragS *frag, /* Which frag? */
231 int where, /* Where in that frag? */
232 int size, /* 1, 2, or 4 usually. */
233 symbolS *add_symbol, /* X_add_symbol. */
234 offsetT offset, /* X_add_number. */
235 int pcrel, /* TRUE if PC-relative relocation. */
236 RELOC_ENUM r_type /* Relocation type. */)
252b5132
RH
237{
238 return fix_new_internal (frag, where, size, add_symbol,
c865e45b 239 (symbolS *) NULL, offset, pcrel, r_type, FALSE);
252b5132
RH
240}
241
242/* Create a fixup for an expression. Currently we only support fixups
243 for difference expressions. That is itself more than most object
244 file formats support anyhow. */
245
246fixS *
d7342424
KH
247fix_new_exp (fragS *frag, /* Which frag? */
248 int where, /* Where in that frag? */
249 int size, /* 1, 2, or 4 usually. */
250 expressionS *exp, /* Expression. */
251 int pcrel, /* TRUE if PC-relative relocation. */
252 RELOC_ENUM r_type /* Relocation type. */)
252b5132
RH
253{
254 symbolS *add = NULL;
255 symbolS *sub = NULL;
256 offsetT off = 0;
257
258 switch (exp->X_op)
259 {
260 case O_absent:
261 break;
262
37006e43
NC
263 case O_register:
264 as_bad (_("register value used as expression"));
265 break;
266
252b5132
RH
267 case O_add:
268 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
269 the difference expression cannot immediately be reduced. */
270 {
271 symbolS *stmp = make_expr_symbol (exp);
58a77e41 272
252b5132
RH
273 exp->X_op = O_symbol;
274 exp->X_op_symbol = 0;
275 exp->X_add_symbol = stmp;
276 exp->X_add_number = 0;
58a77e41 277
252b5132
RH
278 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
279 }
280
281 case O_symbol_rva:
282 add = exp->X_add_symbol;
283 off = exp->X_add_number;
252b5132 284 r_type = BFD_RELOC_RVA;
252b5132
RH
285 break;
286
287 case O_uminus:
288 sub = exp->X_add_symbol;
289 off = exp->X_add_number;
290 break;
291
292 case O_subtract:
293 sub = exp->X_op_symbol;
294 /* Fall through. */
295 case O_symbol:
296 add = exp->X_add_symbol;
efaf0ba4 297 /* Fall through. */
252b5132
RH
298 case O_constant:
299 off = exp->X_add_number;
300 break;
301
302 default:
303 add = make_expr_symbol (exp);
304 break;
305 }
306
c865e45b
RS
307 return fix_new_internal (frag, where, size, add, sub, off, pcrel,
308 r_type, FALSE);
309}
310
311/* Create a fixup at the beginning of FRAG. The arguments are the same
312 as for fix_new, except that WHERE is implicitly 0. */
313
314fixS *
315fix_at_start (fragS *frag, int size, symbolS *add_symbol,
316 offsetT offset, int pcrel, RELOC_ENUM r_type)
317{
318 return fix_new_internal (frag, 0, size, add_symbol,
319 (symbolS *) NULL, offset, pcrel, r_type, TRUE);
252b5132
RH
320}
321
ae6063d4
AM
322/* Generic function to determine whether a fixup requires a relocation. */
323int
d7342424 324generic_force_reloc (fixS *fix)
ae6063d4 325{
ae6063d4
AM
326 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
327 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
328 return 1;
7be1c489 329
61e192bf
NC
330 if (fix->fx_addsy == NULL)
331 return 0;
332
ae6063d4
AM
333 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
334}
335
252b5132
RH
336/* Append a string onto another string, bumping the pointer along. */
337void
d7342424 338append (char **charPP, char *fromP, unsigned long length)
252b5132 339{
efaf0ba4 340 /* Don't trust memcpy() of 0 chars. */
252b5132
RH
341 if (length == 0)
342 return;
343
344 memcpy (*charPP, fromP, length);
345 *charPP += length;
346}
347
efaf0ba4
NC
348/* This routine records the largest alignment seen for each segment.
349 If the beginning of the segment is aligned on the worst-case
350 boundary, all of the other alignments within it will work. At
351 least one object format really uses this info. */
352
252b5132 353void
d7342424
KH
354record_alignment (/* Segment to which alignment pertains. */
355 segT seg,
356 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
357 boundary, 2 => 4-byte boundary, etc.) */
358 int align)
252b5132
RH
359{
360 if (seg == absolute_section)
361 return;
7be1c489 362
252b5132
RH
363 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
364 bfd_set_section_alignment (stdoutput, seg, align);
252b5132
RH
365}
366
0a9ef439 367int
d7342424 368get_recorded_alignment (segT seg)
0a9ef439
RH
369{
370 if (seg == absolute_section)
371 return 0;
7be1c489 372
0a9ef439 373 return bfd_get_section_alignment (stdoutput, seg);
0a9ef439
RH
374}
375
252b5132
RH
376/* Reset the section indices after removing the gas created sections. */
377
378static void
e7ff5c73 379renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
252b5132
RH
380{
381 int *countp = (int *) countparg;
382
383 sec->index = *countp;
384 ++*countp;
385}
386
252b5132 387static fragS *
d7342424 388chain_frchains_together_1 (segT section, struct frchain *frchp)
252b5132
RH
389{
390 fragS dummy, *prev_frag = &dummy;
252b5132 391 fixS fix_dummy, *prev_fix = &fix_dummy;
252b5132 392
c9049d30 393 for (; frchp; frchp = frchp->frch_next)
252b5132
RH
394 {
395 prev_frag->fr_next = frchp->frch_root;
396 prev_frag = frchp->frch_last;
9c2799c2 397 gas_assert (prev_frag->fr_type != 0);
252b5132
RH
398 if (frchp->fix_root != (fixS *) NULL)
399 {
400 if (seg_info (section)->fix_root == (fixS *) NULL)
401 seg_info (section)->fix_root = frchp->fix_root;
402 prev_fix->fx_next = frchp->fix_root;
403 seg_info (section)->fix_tail = frchp->fix_tail;
404 prev_fix = frchp->fix_tail;
405 }
252b5132 406 }
4fc0b46e
AM
407 gas_assert (prev_frag != &dummy
408 && prev_frag->fr_type != 0);
252b5132
RH
409 prev_frag->fr_next = 0;
410 return prev_frag;
411}
412
252b5132 413static void
d7342424
KH
414chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
415 segT section,
e7ff5c73 416 void *xxx ATTRIBUTE_UNUSED)
252b5132
RH
417{
418 segment_info_type *info;
419
420 /* BFD may have introduced its own sections without using
421 subseg_new, so it is possible that seg_info is NULL. */
422 info = seg_info (section);
423 if (info != (segment_info_type *) NULL)
efaf0ba4
NC
424 info->frchainP->frch_last
425 = chain_frchains_together_1 (section, info->frchainP);
252b5132
RH
426
427 /* Now that we've chained the frags together, we must add new fixups
428 to the segment, not to the frag chain. */
429 frags_chained = 1;
430}
431
252b5132 432static void
d7342424 433cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
252b5132
RH
434{
435 switch (fragP->fr_type)
436 {
437 case rs_align:
438 case rs_align_code:
0a9ef439 439 case rs_align_test:
252b5132
RH
440 case rs_org:
441 case rs_space:
442#ifdef HANDLE_ALIGN
443 HANDLE_ALIGN (fragP);
444#endif
445 know (fragP->fr_next != NULL);
446 fragP->fr_offset = (fragP->fr_next->fr_address
447 - fragP->fr_address
448 - fragP->fr_fix) / fragP->fr_var;
449 if (fragP->fr_offset < 0)
450 {
14ad458a
ILT
451 as_bad_where (fragP->fr_file, fragP->fr_line,
452 _("attempt to .org/.space backwards? (%ld)"),
453 (long) fragP->fr_offset);
3f3cdb03 454 fragP->fr_offset = 0;
252b5132
RH
455 }
456 fragP->fr_type = rs_fill;
457 break;
458
459 case rs_fill:
460 break;
461
462 case rs_leb128:
463 {
464 valueT value = S_GET_VALUE (fragP->fr_symbol);
465 int size;
466
467 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
468 fragP->fr_subtype);
469
470 fragP->fr_fix += size;
471 fragP->fr_type = rs_fill;
472 fragP->fr_var = 0;
473 fragP->fr_offset = 0;
474 fragP->fr_symbol = NULL;
475 }
476 break;
477
478 case rs_cfa:
479 eh_frame_convert_frag (fragP);
480 break;
481
220e750f
RH
482 case rs_dwarf2dbg:
483 dwarf2dbg_convert_frag (fragP);
484 break;
485
252b5132 486 case rs_machine_dependent:
252b5132 487 md_convert_frag (stdoutput, sec, fragP);
252b5132 488
9c2799c2 489 gas_assert (fragP->fr_next == NULL
252b5132
RH
490 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
491 == fragP->fr_fix));
492
efaf0ba4
NC
493 /* After md_convert_frag, we make the frag into a ".space 0".
494 md_convert_frag() should set up any fixSs and constants
495 required. */
252b5132
RH
496 frag_wane (fragP);
497 break;
498
499#ifndef WORKING_DOT_WORD
500 case rs_broken_word:
501 {
502 struct broken_word *lie;
503
504 if (fragP->fr_subtype)
505 {
506 fragP->fr_fix += md_short_jump_size;
507 for (lie = (struct broken_word *) (fragP->fr_symbol);
508 lie && lie->dispfrag == fragP;
509 lie = lie->next_broken_word)
510 if (lie->added == 1)
511 fragP->fr_fix += md_long_jump_size;
512 }
513 frag_wane (fragP);
514 }
515 break;
516#endif
517
518 default:
519 BAD_CASE (fragP->fr_type);
520 break;
521 }
09b935ac
AM
522#ifdef md_frag_check
523 md_frag_check (fragP);
524#endif
252b5132
RH
525}
526
32638454
AM
527struct relax_seg_info
528{
529 int pass;
530 int changed;
531};
d7342424 532
252b5132 533static void
32638454 534relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
e46d99eb
AM
535{
536 segment_info_type *seginfo = seg_info (sec);
32638454 537 struct relax_seg_info *info = (struct relax_seg_info *) xxx;
e46d99eb
AM
538
539 if (seginfo && seginfo->frchainP
32638454
AM
540 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
541 info->changed = 1;
e46d99eb
AM
542}
543
e46d99eb 544static void
e7ff5c73 545size_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
252b5132
RH
546{
547 flagword flags;
548 fragS *fragp;
549 segment_info_type *seginfo;
550 int x;
551 valueT size, newsize;
552
553 subseg_change (sec, 0);
554
252b5132
RH
555 seginfo = seg_info (sec);
556 if (seginfo && seginfo->frchainP)
557 {
252b5132
RH
558 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
559 cvt_frag_to_fill (sec, fragp);
560 for (fragp = seginfo->frchainP->frch_root;
561 fragp->fr_next;
562 fragp = fragp->fr_next)
efaf0ba4
NC
563 /* Walk to last elt. */
564 ;
252b5132
RH
565 size = fragp->fr_address + fragp->fr_fix;
566 }
567 else
568 size = 0;
569
e46d99eb 570 flags = bfd_get_section_flags (abfd, sec);
f3d2b04b
KT
571 if (size == 0 && bfd_get_section_size (sec) != 0 &&
572 (flags & SEC_HAS_CONTENTS) != 0)
573 return;
e46d99eb 574
252b5132
RH
575 if (size > 0 && ! seginfo->bss)
576 flags |= SEC_HAS_CONTENTS;
577
e5435dec 578 flags &= ~SEC_RELOC;
252b5132 579 x = bfd_set_section_flags (abfd, sec, flags);
9c2799c2 580 gas_assert (x);
252b5132
RH
581
582 newsize = md_section_align (sec, size);
583 x = bfd_set_section_size (abfd, sec, newsize);
9c2799c2 584 gas_assert (x);
252b5132
RH
585
586 /* If the size had to be rounded up, add some padding in the last
587 non-empty frag. */
9c2799c2 588 gas_assert (newsize >= size);
252b5132
RH
589 if (size != newsize)
590 {
591 fragS *last = seginfo->frchainP->frch_last;
592 fragp = seginfo->frchainP->frch_root;
593 while (fragp->fr_next != last)
594 fragp = fragp->fr_next;
595 last->fr_address = size;
18e1d487
AM
596 if ((newsize - size) % fragp->fr_var == 0)
597 fragp->fr_offset += (newsize - size) / fragp->fr_var;
598 else
599 /* If we hit this abort, it's likely due to subsegs_finish not
600 providing sufficient alignment on the last frag, and the
601 machine dependent code using alignment frags with fr_var
602 greater than 1. */
603 abort ();
252b5132
RH
604 }
605
606#ifdef tc_frob_section
607 tc_frob_section (sec);
608#endif
609#ifdef obj_frob_section
610 obj_frob_section (sec);
611#endif
612}
613
614#ifdef DEBUG2
615static void
988392e2 616dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
252b5132 617{
252b5132
RH
618 segment_info_type *seginfo = seg_info (sec);
619 fixS *fixp = seginfo->fix_root;
620
621 if (!fixp)
622 return;
623
624 fprintf (stream, "sec %s relocs:\n", sec->name);
625 while (fixp)
626 {
627 symbolS *s = fixp->fx_addsy;
e723ef7c
ILT
628
629 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
630 (int) fixp->fx_r_type);
631 if (s == NULL)
632 fprintf (stream, "no sym\n");
633 else
252b5132 634 {
e723ef7c
ILT
635 print_symbol_value_1 (stream, s);
636 fprintf (stream, "\n");
252b5132 637 }
252b5132
RH
638 fixp = fixp->fx_next;
639 }
640}
641#else
642#define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
643#endif
644
645#ifndef EMIT_SECTION_SYMBOLS
646#define EMIT_SECTION_SYMBOLS 1
647#endif
648
05e9452c
AM
649/* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
650 and check for validity. Convert RELOC_LIST from using U.A fields
651 to U.B fields. */
652static void
653resolve_reloc_expr_symbols (void)
654{
080f9e4f 655 bfd_vma addr_mask = 1;
05e9452c
AM
656 struct reloc_list *r;
657
080f9e4f
MR
658 /* Avoid a shift by the width of type. */
659 addr_mask <<= bfd_arch_bits_per_address (stdoutput) - 1;
660 addr_mask <<= 1;
661 addr_mask -= 1;
662
05e9452c
AM
663 for (r = reloc_list; r; r = r->next)
664 {
080f9e4f 665 reloc_howto_type *howto = r->u.a.howto;
05e9452c
AM
666 expressionS *symval;
667 symbolS *sym;
668 bfd_vma offset, addend;
669 asection *sec;
05e9452c
AM
670
671 resolve_symbol_value (r->u.a.offset_sym);
672 symval = symbol_get_value_expression (r->u.a.offset_sym);
673
674 offset = 0;
675 sym = NULL;
676 if (symval->X_op == O_constant)
677 sym = r->u.a.offset_sym;
678 else if (symval->X_op == O_symbol)
679 {
680 sym = symval->X_add_symbol;
681 offset = symval->X_add_number;
682 symval = symbol_get_value_expression (symval->X_add_symbol);
683 }
684 if (sym == NULL
685 || symval->X_op != O_constant
686 || (sec = S_GET_SEGMENT (sym)) == NULL
687 || !SEG_NORMAL (sec))
688 {
689 as_bad_where (r->file, r->line, _("invalid offset expression"));
690 sec = NULL;
691 }
692 else
693 offset += S_GET_VALUE (sym);
694
695 sym = NULL;
696 addend = r->u.a.addend;
697 if (r->u.a.sym != NULL)
698 {
699 resolve_symbol_value (r->u.a.sym);
700 symval = symbol_get_value_expression (r->u.a.sym);
701 if (symval->X_op == O_constant)
702 sym = r->u.a.sym;
703 else if (symval->X_op == O_symbol)
704 {
705 sym = symval->X_add_symbol;
706 addend += symval->X_add_number;
707 symval = symbol_get_value_expression (symval->X_add_symbol);
708 }
709 if (symval->X_op != O_constant)
710 {
711 as_bad_where (r->file, r->line, _("invalid reloc expression"));
712 sec = NULL;
713 }
714 else if (sym != NULL)
7230378d 715 {
080f9e4f
MR
716 /* Convert relocs against local symbols to refer to the
717 corresponding section symbol plus offset instead. Keep
718 PC-relative relocs of the REL variety intact though to
719 prevent the offset from overflowing the relocated field,
720 unless it has enough bits to cover the whole address
721 space. */
722 if (S_IS_LOCAL (sym) && !symbol_section_p (sym)
d6101ac2
MR
723 && (sec->use_rela_p
724 || (howto->partial_inplace
725 && (!howto->pc_relative
726 || howto->src_mask == addr_mask))))
7230378d
AM
727 {
728 asection *symsec = S_GET_SEGMENT (sym);
729 if (!(((symsec->flags & SEC_MERGE) != 0
730 && addend != 0)
731 || (symsec->flags & SEC_THREAD_LOCAL) != 0))
732 {
733 addend += S_GET_VALUE (sym);
734 sym = section_symbol (symsec);
735 }
736 }
737 symbol_mark_used_in_reloc (sym);
738 }
05e9452c
AM
739 }
740 if (sym == NULL)
741 {
742 if (abs_section_sym == NULL)
743 abs_section_sym = section_symbol (absolute_section);
744 sym = abs_section_sym;
745 }
746
05e9452c
AM
747 r->u.b.sec = sec;
748 r->u.b.s = symbol_get_bfdsym (sym);
749 r->u.b.r.sym_ptr_ptr = &r->u.b.s;
750 r->u.b.r.address = offset;
751 r->u.b.r.addend = addend;
752 r->u.b.r.howto = howto;
753 }
754}
755
df44284e
AM
756/* This pass over fixups decides whether symbols can be replaced with
757 section symbols. */
758
252b5132 759static void
d7342424
KH
760adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
761 asection *sec,
e7ff5c73 762 void *xxx ATTRIBUTE_UNUSED)
252b5132
RH
763{
764 segment_info_type *seginfo = seg_info (sec);
765 fixS *fixp;
766
767 if (seginfo == NULL)
768 return;
769
770 dump_section_relocs (abfd, sec, stderr);
771
772 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
773 if (fixp->fx_done)
efaf0ba4
NC
774 /* Ignore it. */
775 ;
252b5132
RH
776 else if (fixp->fx_addsy)
777 {
778 symbolS *sym;
779 asection *symsec;
780
781#ifdef DEBUG5
782 fprintf (stderr, "\n\nadjusting fixup:\n");
783 print_fixup (fixp);
784#endif
785
786 sym = fixp->fx_addsy;
787
788 /* All symbols should have already been resolved at this
789 point. It is possible to see unresolved expression
790 symbols, though, since they are not in the regular symbol
791 table. */
df44284e 792 resolve_symbol_value (sym);
efaf0ba4 793
49309057 794 if (fixp->fx_subsy != NULL)
6386f3a7 795 resolve_symbol_value (fixp->fx_subsy);
252b5132 796
60938e80
L
797 /* If this symbol is equated to an undefined or common symbol,
798 convert the fixup to being against that symbol. */
b4b24b79
AM
799 while (symbol_equated_reloc_p (sym)
800 || S_IS_WEAKREFR (sym))
252b5132 801 {
b4b24b79
AM
802 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
803 if (sym == newsym)
804 break;
49309057 805 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
b4b24b79
AM
806 fixp->fx_addsy = newsym;
807 sym = newsym;
252b5132
RH
808 }
809
df44284e 810 if (symbol_mri_common_p (sym))
252b5132 811 {
b4b24b79
AM
812 fixp->fx_offset += S_GET_VALUE (sym);
813 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
df44284e 814 continue;
252b5132
RH
815 }
816
a161fe53
AM
817 /* If the symbol is undefined, common, weak, or global (ELF
818 shared libs), we can't replace it with the section symbol. */
ae6063d4 819 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
a161fe53
AM
820 continue;
821
822 /* Is there some other (target cpu dependent) reason we can't adjust
823 this one? (E.g. relocations involving function addresses on
824 the PA. */
825#ifdef tc_fix_adjustable
826 if (! tc_fix_adjustable (fixp))
827 continue;
828#endif
829
830 /* Since we're reducing to section symbols, don't attempt to reduce
831 anything that's already using one. */
832 if (symbol_section_p (sym))
833 continue;
834
252b5132 835 symsec = S_GET_SEGMENT (sym);
252b5132
RH
836 if (symsec == NULL)
837 abort ();
efaf0ba4 838
252b5132
RH
839 if (bfd_is_abs_section (symsec))
840 {
a161fe53 841 /* The fixup_segment routine normally will not use this
b4b24b79 842 symbol in a relocation. */
df44284e 843 continue;
252b5132
RH
844 }
845
7565ed77 846 /* Don't try to reduce relocs which refer to non-local symbols
b4b24b79
AM
847 in .linkonce sections. It can lead to confusion when a
848 debugging section refers to a .linkonce section. I hope
849 this will always be correct. */
7565ed77 850 if (symsec != sec && ! S_IS_LOCAL (sym))
252b5132 851 {
a161fe53
AM
852 if ((symsec->flags & SEC_LINK_ONCE) != 0
853 || (IS_ELF
854 /* The GNU toolchain uses an extension for ELF: a
855 section beginning with the magic string
856 .gnu.linkonce is a linkonce section. */
857 && strncmp (segment_name (symsec), ".gnu.linkonce",
858 sizeof ".gnu.linkonce" - 1) == 0))
859 continue;
252b5132 860 }
f7460f5f
JJ
861
862 /* Never adjust a reloc against local symbol in a merge section
863 with non-zero addend. */
5045ec7a
AM
864 if ((symsec->flags & SEC_MERGE) != 0
865 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
a161fe53 866 continue;
13ae64f3
JJ
867
868 /* Never adjust a reloc against TLS local symbol. */
df44284e 869 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
a161fe53 870 continue;
252b5132 871
a161fe53 872 /* We refetch the segment when calling section_symbol, rather
252b5132 873 than using symsec, because S_GET_VALUE may wind up changing
efaf0ba4 874 the section when it calls resolve_symbol_value. */
252b5132
RH
875 fixp->fx_offset += S_GET_VALUE (sym);
876 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
e723ef7c
ILT
877#ifdef DEBUG5
878 fprintf (stderr, "\nadjusted fixup:\n");
879 print_fixup (fixp);
880#endif
252b5132 881 }
252b5132
RH
882
883 dump_section_relocs (abfd, sec, stderr);
884}
885
e7ff5c73
AM
886/* fixup_segment()
887
888 Go through all the fixS's in a segment and see which ones can be
889 handled now. (These consist of fixS where we have since discovered
890 the value of a symbol, or the address of the frag involved.)
891 For each one, call md_apply_fix to put the fix into the frag data.
89d67ed9
AM
892 Ones that we couldn't completely handle here will be output later
893 by emit_relocations. */
e7ff5c73 894
89d67ed9 895static void
e7ff5c73
AM
896fixup_segment (fixS *fixP, segT this_segment)
897{
e7ff5c73
AM
898 valueT add_number;
899 fragS *fragP;
900 segT add_symbol_segment = absolute_section;
901
902 if (fixP != NULL && abs_section_sym == NULL)
903 abs_section_sym = section_symbol (absolute_section);
904
905 /* If the linker is doing the relaxing, we must not do any fixups.
906
907 Well, strictly speaking that's not true -- we could do any that
908 are PC-relative and don't cross regions that could change size.
909 And for the i960 we might be able to turn callx/callj into bal
910 anyways in cases where we know the maximum displacement. */
911 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
912 {
913 for (; fixP; fixP = fixP->fx_next)
914 if (!fixP->fx_done)
915 {
916 if (fixP->fx_addsy == NULL)
917 {
918 /* There was no symbol required by this relocation.
919 However, BFD doesn't really handle relocations
920 without symbols well. So fake up a local symbol in
921 the absolute section. */
922 fixP->fx_addsy = abs_section_sym;
923 }
924 symbol_mark_used_in_reloc (fixP->fx_addsy);
925 if (fixP->fx_subsy != NULL)
926 symbol_mark_used_in_reloc (fixP->fx_subsy);
e7ff5c73 927 }
89d67ed9 928 return;
e7ff5c73
AM
929 }
930
931 for (; fixP; fixP = fixP->fx_next)
932 {
933#ifdef DEBUG5
934 fprintf (stderr, "\nprocessing fixup:\n");
935 print_fixup (fixP);
936#endif
937
938 fragP = fixP->fx_frag;
939 know (fragP);
940#ifdef TC_VALIDATE_FIX
941 TC_VALIDATE_FIX (fixP, this_segment, skip);
942#endif
943 add_number = fixP->fx_offset;
944
e7ff5c73
AM
945 if (fixP->fx_addsy != NULL)
946 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
947
948 if (fixP->fx_subsy != NULL)
949 {
950 segT sub_symbol_segment;
951 resolve_symbol_value (fixP->fx_subsy);
952 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
953 if (fixP->fx_addsy != NULL
954 && sub_symbol_segment == add_symbol_segment
c969da64
RS
955 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
956 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
e7ff5c73
AM
957 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
958 {
959 add_number += S_GET_VALUE (fixP->fx_addsy);
960 add_number -= S_GET_VALUE (fixP->fx_subsy);
961 fixP->fx_offset = add_number;
962 fixP->fx_addsy = NULL;
963 fixP->fx_subsy = NULL;
964#ifdef TC_M68K
965 /* See the comment below about 68k weirdness. */
966 fixP->fx_pcrel = 0;
967#endif
968 }
969 else if (sub_symbol_segment == absolute_section
c969da64 970 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
9a97a5d7 971 && !TC_FORCE_RELOCATION_SUB_ABS (fixP, add_symbol_segment))
e7ff5c73
AM
972 {
973 add_number -= S_GET_VALUE (fixP->fx_subsy);
974 fixP->fx_offset = add_number;
975 fixP->fx_subsy = NULL;
976 }
977 else if (sub_symbol_segment == this_segment
c969da64 978 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
5db484ff 979 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP, add_symbol_segment))
e7ff5c73
AM
980 {
981 add_number -= S_GET_VALUE (fixP->fx_subsy);
982 fixP->fx_offset = (add_number + fixP->fx_dot_value
8e723a10 983 + fixP->fx_dot_frag->fr_address);
e7ff5c73
AM
984
985 /* Make it pc-relative. If the back-end code has not
986 selected a pc-relative reloc, cancel the adjustment
987 we do later on all pc-relative relocs. */
988 if (0
989#ifdef TC_M68K
990 /* Do this for m68k even if it's already described
991 as pc-relative. On the m68k, an operand of
992 "pc@(foo-.-2)" should address "foo" in a
993 pc-relative mode. */
994 || 1
995#endif
996 || !fixP->fx_pcrel)
997 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
998 fixP->fx_subsy = NULL;
999 fixP->fx_pcrel = 1;
1000 }
586ec8c1 1001 else if (!TC_VALIDATE_FIX_SUB (fixP, add_symbol_segment))
e7ff5c73 1002 {
9a97a5d7
AM
1003 if (!md_register_arithmetic
1004 && (add_symbol_segment == reg_section
1005 || sub_symbol_segment == reg_section))
1006 as_bad_where (fixP->fx_file, fixP->fx_line,
1007 _("register value used as expression"));
1008 else
1009 as_bad_where (fixP->fx_file, fixP->fx_line,
1010 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
1011 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
1012 segment_name (add_symbol_segment),
1013 S_GET_NAME (fixP->fx_subsy),
1014 segment_name (sub_symbol_segment));
e7ff5c73 1015 }
586ec8c1
NC
1016 else if (sub_symbol_segment != undefined_section
1017 && ! bfd_is_com_section (sub_symbol_segment)
1018 && MD_APPLY_SYM_VALUE (fixP))
1019 add_number -= S_GET_VALUE (fixP->fx_subsy);
e7ff5c73
AM
1020 }
1021
1022 if (fixP->fx_addsy)
1023 {
1fc5d88e 1024 if (add_symbol_segment == this_segment
c969da64 1025 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
e7ff5c73
AM
1026 && !TC_FORCE_RELOCATION_LOCAL (fixP))
1027 {
1028 /* This fixup was made when the symbol's segment was
1029 SEG_UNKNOWN, but it is now in the local segment.
1030 So we know how to do the address without relocation. */
1031 add_number += S_GET_VALUE (fixP->fx_addsy);
1032 fixP->fx_offset = add_number;
1033 if (fixP->fx_pcrel)
1034 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1035 fixP->fx_addsy = NULL;
1036 fixP->fx_pcrel = 0;
1037 }
1038 else if (add_symbol_segment == absolute_section
c969da64 1039 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
e7ff5c73
AM
1040 && !TC_FORCE_RELOCATION_ABS (fixP))
1041 {
1042 add_number += S_GET_VALUE (fixP->fx_addsy);
1043 fixP->fx_offset = add_number;
1044 fixP->fx_addsy = NULL;
1045 }
1046 else if (add_symbol_segment != undefined_section
1047 && ! bfd_is_com_section (add_symbol_segment)
1048 && MD_APPLY_SYM_VALUE (fixP))
1049 add_number += S_GET_VALUE (fixP->fx_addsy);
1050 }
1051
1052 if (fixP->fx_pcrel)
1053 {
1054 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1055 if (!fixP->fx_done && fixP->fx_addsy == NULL)
1056 {
1057 /* There was no symbol required by this relocation.
1058 However, BFD doesn't really handle relocations
1059 without symbols well. So fake up a local symbol in
1060 the absolute section. */
1061 fixP->fx_addsy = abs_section_sym;
1062 }
1063 }
1064
1065 if (!fixP->fx_done)
1066 md_apply_fix (fixP, &add_number, this_segment);
1067
1068 if (!fixP->fx_done)
1069 {
e7ff5c73
AM
1070 if (fixP->fx_addsy == NULL)
1071 fixP->fx_addsy = abs_section_sym;
1072 symbol_mark_used_in_reloc (fixP->fx_addsy);
1073 if (fixP->fx_subsy != NULL)
1074 symbol_mark_used_in_reloc (fixP->fx_subsy);
1075 }
1076
1077 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
1078 {
1079 if (fixP->fx_size < sizeof (valueT))
1080 {
1081 valueT mask;
1082
1083 mask = 0;
1084 mask--; /* Set all bits to one. */
1085 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
1086 if ((add_number & mask) != 0 && (add_number & mask) != mask)
1087 {
1088 char buf[50], buf2[50];
1089 sprint_value (buf, fragP->fr_address + fixP->fx_where);
1090 if (add_number > 1000)
1091 sprint_value (buf2, add_number);
1092 else
1093 sprintf (buf2, "%ld", (long) add_number);
1094 as_bad_where (fixP->fx_file, fixP->fx_line,
1095 _("value of %s too large for field of %d bytes at %s"),
1096 buf2, fixP->fx_size, buf);
1097 } /* Generic error checking. */
1098 }
1099#ifdef WARN_SIGNED_OVERFLOW_WORD
1100 /* Warn if a .word value is too large when treated as a signed
1101 number. We already know it is not too negative. This is to
1102 catch over-large switches generated by gcc on the 68k. */
1103 if (!flag_signed_overflow_ok
1104 && fixP->fx_size == 2
1105 && add_number > 0x7fff)
1106 as_bad_where (fixP->fx_file, fixP->fx_line,
1107 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1108 (long) add_number,
1109 (long) (fragP->fr_address + fixP->fx_where));
1110#endif
1111 } /* Not a bit fix. */
1112
1113#ifdef TC_VALIDATE_FIX
1114 skip: ATTRIBUTE_UNUSED_LABEL
1115 ;
1116#endif
1117#ifdef DEBUG5
1118 fprintf (stderr, "result:\n");
1119 print_fixup (fixP);
1120#endif
1121 } /* For each fixS in this segment. */
e7ff5c73
AM
1122}
1123
a161fe53 1124static void
d7342424
KH
1125fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
1126 asection *sec,
e7ff5c73 1127 void *xxx ATTRIBUTE_UNUSED)
a161fe53
AM
1128{
1129 segment_info_type *seginfo = seg_info (sec);
1130
1131 fixup_segment (seginfo->fix_root, sec);
1132}
1133
e5435dec
AM
1134static void
1135install_reloc (asection *sec, arelent *reloc, fragS *fragp,
1136 char *file, unsigned int line)
1137{
1138 char *err;
1139 bfd_reloc_status_type s;
76d12939
AM
1140 asymbol *sym;
1141
1142 if (reloc->sym_ptr_ptr != NULL
1143 && (sym = *reloc->sym_ptr_ptr) != NULL
1144 && (sym->flags & BSF_KEEP) == 0
1145 && ((sym->flags & BSF_SECTION_SYM) == 0
55f8b826
AM
1146 || (EMIT_SECTION_SYMBOLS
1147 && !bfd_is_abs_section (sym->section))))
76d12939 1148 as_bad_where (file, line, _("redefined symbol cannot be used on reloc"));
e5435dec
AM
1149
1150 s = bfd_install_relocation (stdoutput, reloc,
1151 fragp->fr_literal, fragp->fr_address,
1152 sec, &err);
1153 switch (s)
1154 {
1155 case bfd_reloc_ok:
1156 break;
1157 case bfd_reloc_overflow:
1158 as_bad_where (file, line, _("relocation overflow"));
1159 break;
1160 case bfd_reloc_outofrange:
1161 as_bad_where (file, line, _("relocation out of range"));
1162 break;
1163 default:
1164 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1165 file, line, s);
1166 }
1167}
1168
7230378d
AM
1169static fragS *
1170get_frag_for_reloc (fragS *last_frag,
1171 const segment_info_type *seginfo,
1172 const struct reloc_list *r)
1173{
1174 fragS *f;
34bca508 1175
7230378d
AM
1176 for (f = last_frag; f != NULL; f = f->fr_next)
1177 if (f->fr_address <= r->u.b.r.address
1178 && r->u.b.r.address < f->fr_address + f->fr_fix)
1179 return f;
1180
1181 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
1182 if (f->fr_address <= r->u.b.r.address
1183 && r->u.b.r.address < f->fr_address + f->fr_fix)
1184 return f;
1185
1186 as_bad_where (r->file, r->line,
1187 _("reloc not within (fixed part of) section"));
1188 return NULL;
1189}
1190
252b5132 1191static void
e7ff5c73 1192write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
252b5132
RH
1193{
1194 segment_info_type *seginfo = seg_info (sec);
252b5132 1195 unsigned int n;
05e9452c 1196 struct reloc_list *my_reloc_list, **rp, *r;
252b5132
RH
1197 arelent **relocs;
1198 fixS *fixp;
7230378d 1199 fragS *last_frag;
252b5132
RH
1200
1201 /* If seginfo is NULL, we did not create this section; don't do
1202 anything with it. */
1203 if (seginfo == NULL)
1204 return;
1205
252b5132
RH
1206 n = 0;
1207 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
e5435dec
AM
1208 if (!fixp->fx_done)
1209 n++;
1210
1211#ifdef RELOC_EXPANSION_POSSIBLE
1212 n *= MAX_RELOC_EXPANSION;
1213#endif
252b5132 1214
05e9452c
AM
1215 /* Extract relocs for this section from reloc_list. */
1216 rp = &reloc_list;
1217 my_reloc_list = NULL;
1218 while ((r = *rp) != NULL)
1219 {
1220 if (r->u.b.sec == sec)
1221 {
1222 *rp = r->next;
1223 r->next = my_reloc_list;
1224 my_reloc_list = r;
1225 n++;
1226 }
1227 else
1228 rp = &r->next;
1229 }
1230
1e9cc1c2 1231 relocs = (arelent **) xcalloc (n, sizeof (arelent *));
252b5132 1232
7230378d
AM
1233 n = 0;
1234 r = my_reloc_list;
1235 last_frag = NULL;
252b5132
RH
1236 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1237 {
d31f0f6d
AM
1238 int fx_size, slack;
1239 offsetT loc;
7230378d
AM
1240 arelent **reloc;
1241#ifndef RELOC_EXPANSION_POSSIBLE
1242 arelent *rel;
1243
1244 reloc = &rel;
1245#endif
252b5132
RH
1246
1247 if (fixp->fx_done)
e5435dec 1248 continue;
252b5132 1249
d31f0f6d
AM
1250 fx_size = fixp->fx_size;
1251 slack = TC_FX_SIZE_SLACK (fixp);
1252 if (slack > 0)
1253 fx_size = fx_size > slack ? fx_size - slack : 0;
1254 loc = fixp->fx_where + fx_size;
e5435dec 1255 if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
d31f0f6d
AM
1256 as_bad_where (fixp->fx_file, fixp->fx_line,
1257 _("internal error: fixup not contained within frag"));
252b5132 1258
e5435dec 1259#ifndef RELOC_EXPANSION_POSSIBLE
7230378d 1260 *reloc = tc_gen_reloc (sec, fixp);
e5435dec 1261#else
7230378d
AM
1262 reloc = tc_gen_reloc (sec, fixp);
1263#endif
252b5132 1264
7230378d
AM
1265 while (*reloc)
1266 {
1267 while (r != NULL && r->u.b.r.address < (*reloc)->address)
1268 {
1269 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1270 if (f != NULL)
1271 {
1272 last_frag = f;
1273 relocs[n++] = &r->u.b.r;
1274 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1275 }
1276 r = r->next;
1277 }
1278 relocs[n++] = *reloc;
1279 install_reloc (sec, *reloc, fixp->fx_frag,
1280 fixp->fx_file, fixp->fx_line);
1281#ifndef RELOC_EXPANSION_POSSIBLE
1282 break;
1283#else
1284 reloc++;
e5435dec 1285#endif
7230378d
AM
1286 }
1287 }
d31f0f6d 1288
7230378d
AM
1289 while (r != NULL)
1290 {
1291 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1292 if (f != NULL)
1293 {
1294 last_frag = f;
1295 relocs[n++] = &r->u.b.r;
1296 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1297 }
1298 r = r->next;
252b5132 1299 }
252b5132
RH
1300
1301#ifdef DEBUG4
1302 {
1596988d 1303 unsigned int k, j, nsyms;
252b5132
RH
1304 asymbol **sympp;
1305 sympp = bfd_get_outsymbols (stdoutput);
1306 nsyms = bfd_get_symcount (stdoutput);
1596988d
TG
1307 for (k = 0; k < n; k++)
1308 if (((*relocs[k]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
1309 {
1310 for (j = 0; j < nsyms; j++)
1596988d 1311 if (sympp[j] == *relocs[k]->sym_ptr_ptr)
252b5132
RH
1312 break;
1313 if (j == nsyms)
1314 abort ();
1315 }
1316 }
1317#endif
1318
1319 if (n)
e5435dec
AM
1320 {
1321 flagword flags = bfd_get_section_flags (abfd, sec);
1322 flags |= SEC_RELOC;
1323 bfd_set_section_flags (abfd, sec, flags);
1324 bfd_set_reloc (stdoutput, sec, relocs, n);
1325 }
252b5132 1326
945a1a6b
ILT
1327#ifdef SET_SECTION_RELOCS
1328 SET_SECTION_RELOCS (sec, relocs, n);
1329#endif
1330
252b5132
RH
1331#ifdef DEBUG3
1332 {
1596988d
TG
1333 unsigned int k;
1334
252b5132 1335 fprintf (stderr, "relocs for sec %s\n", sec->name);
1596988d 1336 for (k = 0; k < n; k++)
252b5132 1337 {
1596988d
TG
1338 arelent *rel = relocs[k];
1339 asymbol *s = *rel->sym_ptr_ptr;
988392e2 1340 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1596988d
TG
1341 k, rel, (unsigned long)rel->address, s->name,
1342 (unsigned long)rel->addend);
252b5132
RH
1343 }
1344 }
1345#endif
1346}
1347
0acf065b
CC
1348static int
1349compress_frag (struct z_stream_s *strm, const char *contents, int in_size,
1350 fragS **last_newf, struct obstack *ob)
1351{
1352 int out_size;
1353 int total_out_size = 0;
1354 fragS *f = *last_newf;
1355 char *next_out;
1356 int avail_out;
1357
1358 /* Call the compression routine repeatedly until it has finished
1359 processing the frag. */
1360 while (in_size > 0)
1361 {
1362 /* Reserve all the space available in the current chunk.
1363 If none is available, start a new frag. */
1364 avail_out = obstack_room (ob);
1365 if (avail_out <= 0)
1366 {
1367 obstack_finish (ob);
1368 f = frag_alloc (ob);
1369 f->fr_type = rs_fill;
1370 (*last_newf)->fr_next = f;
1371 *last_newf = f;
1372 avail_out = obstack_room (ob);
1373 }
1374 if (avail_out <= 0)
1375 as_fatal (_("can't extend frag"));
1376 next_out = obstack_next_free (ob);
1377 obstack_blank_fast (ob, avail_out);
1378 out_size = compress_data (strm, &contents, &in_size,
1379 &next_out, &avail_out);
1380 if (out_size < 0)
1381 return -1;
1382
1383 f->fr_fix += out_size;
1384 total_out_size += out_size;
1385
1386 /* Return unused space. */
1387 if (avail_out > 0)
1388 obstack_blank_fast (ob, -avail_out);
1389 }
1390
1391 return total_out_size;
1392}
1393
1394static void
1395compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1396{
1397 segment_info_type *seginfo = seg_info (sec);
1398 fragS *f;
1399 fragS *first_newf;
1400 fragS *last_newf;
1401 struct obstack *ob = &seginfo->frchainP->frch_obstack;
1402 bfd_size_type uncompressed_size = (bfd_size_type) sec->size;
1403 bfd_size_type compressed_size;
1404 const char *section_name;
1405 char *compressed_name;
1406 char *header;
1407 struct z_stream_s *strm;
1408 int x;
4a114e3e 1409 flagword flags = bfd_get_section_flags (abfd, sec);
0acf065b
CC
1410
1411 if (seginfo == NULL
6ac88ef3 1412 || sec->size < 32
4a114e3e 1413 || (flags & (SEC_ALLOC | SEC_HAS_CONTENTS)) == SEC_ALLOC)
0acf065b
CC
1414 return;
1415
1416 section_name = bfd_get_section_name (stdoutput, sec);
1417 if (strncmp (section_name, ".debug_", 7) != 0)
1418 return;
1419
1420 strm = compress_init ();
1421 if (strm == NULL)
1422 return;
1423
1424 /* Create a new frag to contain the "ZLIB" header. */
1425 first_newf = frag_alloc (ob);
1426 if (obstack_room (ob) < 12)
1427 first_newf = frag_alloc (ob);
1428 if (obstack_room (ob) < 12)
1429 as_fatal (_("can't extend frag %u chars"), 12);
1430 last_newf = first_newf;
1431 obstack_blank_fast (ob, 12);
1432 last_newf->fr_type = rs_fill;
1433 last_newf->fr_fix = 12;
1434 header = last_newf->fr_literal;
1435 memcpy (header, "ZLIB", 4);
1436 header[11] = uncompressed_size; uncompressed_size >>= 8;
1437 header[10] = uncompressed_size; uncompressed_size >>= 8;
1438 header[9] = uncompressed_size; uncompressed_size >>= 8;
1439 header[8] = uncompressed_size; uncompressed_size >>= 8;
1440 header[7] = uncompressed_size; uncompressed_size >>= 8;
1441 header[6] = uncompressed_size; uncompressed_size >>= 8;
1442 header[5] = uncompressed_size; uncompressed_size >>= 8;
1443 header[4] = uncompressed_size;
1444 compressed_size = 12;
1445
1446 /* Stream the frags through the compression engine, adding new frags
1447 as necessary to accomodate the compressed output. */
1448 for (f = seginfo->frchainP->frch_root;
1449 f;
1450 f = f->fr_next)
1451 {
1452 offsetT fill_size;
1453 char *fill_literal;
1454 offsetT count;
1455 int out_size;
1456
1457 gas_assert (f->fr_type == rs_fill);
1458 if (f->fr_fix)
1459 {
1460 out_size = compress_frag (strm, f->fr_literal, f->fr_fix,
1461 &last_newf, ob);
1462 if (out_size < 0)
1463 return;
1464 compressed_size += out_size;
1465 }
1466 fill_literal = f->fr_literal + f->fr_fix;
1467 fill_size = f->fr_var;
1468 count = f->fr_offset;
1469 gas_assert (count >= 0);
1470 if (fill_size && count)
1471 {
1472 while (count--)
1473 {
1474 out_size = compress_frag (strm, fill_literal, (int) fill_size,
1475 &last_newf, ob);
1476 if (out_size < 0)
1477 return;
1478 compressed_size += out_size;
1479 }
1480 }
1481 }
1482
1483 /* Flush the compression state. */
1484 for (;;)
1485 {
1486 int avail_out;
1487 char *next_out;
1488 int out_size;
1489
1490 /* Reserve all the space available in the current chunk.
1491 If none is available, start a new frag. */
1492 avail_out = obstack_room (ob);
1493 if (avail_out <= 0)
1494 {
1495 fragS *newf;
1496
1497 obstack_finish (ob);
1498 newf = frag_alloc (ob);
1499 newf->fr_type = rs_fill;
1500 last_newf->fr_next = newf;
1501 last_newf = newf;
1502 avail_out = obstack_room (ob);
1503 }
1504 if (avail_out <= 0)
1505 as_fatal (_("can't extend frag"));
1506 next_out = obstack_next_free (ob);
1507 obstack_blank_fast (ob, avail_out);
1508 x = compress_finish (strm, &next_out, &avail_out, &out_size);
1509 if (x < 0)
1510 return;
1511
1512 last_newf->fr_fix += out_size;
1513 compressed_size += out_size;
1514
1515 /* Return unused space. */
1516 if (avail_out > 0)
1517 obstack_blank_fast (ob, -avail_out);
1518
1519 if (x == 0)
1520 break;
1521 }
1522
1523 /* Replace the uncompressed frag list with the compressed frag list. */
1524 seginfo->frchainP->frch_root = first_newf;
1525 seginfo->frchainP->frch_last = last_newf;
1526
1527 /* Update the section size and its name. */
1528 x = bfd_set_section_size (abfd, sec, compressed_size);
1529 gas_assert (x);
1530 compressed_name = (char *) xmalloc (strlen (section_name) + 2);
1531 compressed_name[0] = '.';
1532 compressed_name[1] = 'z';
1533 strcpy (compressed_name + 2, section_name + 1);
1534 bfd_section_name (stdoutput, sec) = compressed_name;
1535}
1536
252b5132 1537static void
d7342424
KH
1538write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1539 asection *sec,
e7ff5c73 1540 void *xxx ATTRIBUTE_UNUSED)
252b5132
RH
1541{
1542 segment_info_type *seginfo = seg_info (sec);
685736be 1543 addressT offset = 0;
252b5132
RH
1544 fragS *f;
1545
1546 /* Write out the frags. */
1547 if (seginfo == NULL
efaf0ba4 1548 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
252b5132
RH
1549 return;
1550
1551 for (f = seginfo->frchainP->frch_root;
1552 f;
1553 f = f->fr_next)
1554 {
1555 int x;
685736be 1556 addressT fill_size;
252b5132 1557 char *fill_literal;
685736be 1558 offsetT count;
252b5132 1559
9c2799c2 1560 gas_assert (f->fr_type == rs_fill);
252b5132
RH
1561 if (f->fr_fix)
1562 {
1563 x = bfd_set_section_contents (stdoutput, sec,
1564 f->fr_literal, (file_ptr) offset,
1565 (bfd_size_type) f->fr_fix);
b34976b6 1566 if (!x)
885afe7b
AM
1567 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1568 bfd_errmsg (bfd_get_error ()));
252b5132
RH
1569 offset += f->fr_fix;
1570 }
1571 fill_literal = f->fr_literal + f->fr_fix;
1572 fill_size = f->fr_var;
1573 count = f->fr_offset;
9c2799c2 1574 gas_assert (count >= 0);
252b5132
RH
1575 if (fill_size && count)
1576 {
1577 char buf[256];
efaf0ba4 1578 if (fill_size > sizeof (buf))
252b5132 1579 {
efaf0ba4 1580 /* Do it the old way. Can this ever happen? */
252b5132
RH
1581 while (count--)
1582 {
1583 x = bfd_set_section_contents (stdoutput, sec,
1584 fill_literal,
1585 (file_ptr) offset,
1586 (bfd_size_type) fill_size);
b34976b6 1587 if (!x)
885afe7b
AM
1588 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1589 bfd_errmsg (bfd_get_error ()));
252b5132
RH
1590 offset += fill_size;
1591 }
1592 }
1593 else
1594 {
1595 /* Build a buffer full of fill objects and output it as
1596 often as necessary. This saves on the overhead of
1597 potentially lots of bfd_set_section_contents calls. */
1598 int n_per_buf, i;
1599 if (fill_size == 1)
1600 {
1601 n_per_buf = sizeof (buf);
1602 memset (buf, *fill_literal, n_per_buf);
1603 }
1604 else
1605 {
1606 char *bufp;
efaf0ba4 1607 n_per_buf = sizeof (buf) / fill_size;
252b5132 1608 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
efaf0ba4 1609 memcpy (bufp, fill_literal, fill_size);
252b5132
RH
1610 }
1611 for (; count > 0; count -= n_per_buf)
1612 {
1613 n_per_buf = n_per_buf > count ? count : n_per_buf;
efaf0ba4
NC
1614 x = bfd_set_section_contents
1615 (stdoutput, sec, buf, (file_ptr) offset,
1616 (bfd_size_type) n_per_buf * fill_size);
b34976b6 1617 if (!x)
a22429b9
NC
1618 as_fatal (_("cannot write to output file '%s': %s"),
1619 stdoutput->filename,
1620 bfd_errmsg (bfd_get_error ()));
252b5132
RH
1621 offset += n_per_buf * fill_size;
1622 }
1623 }
1624 }
1625 }
1626}
252b5132 1627
252b5132 1628static void
d7342424 1629merge_data_into_text (void)
252b5132 1630{
252b5132
RH
1631 seg_info (text_section)->frchainP->frch_last->fr_next =
1632 seg_info (data_section)->frchainP->frch_root;
1633 seg_info (text_section)->frchainP->frch_last =
1634 seg_info (data_section)->frchainP->frch_last;
1635 seg_info (data_section)->frchainP = 0;
252b5132 1636}
252b5132 1637
252b5132 1638static void
d7342424 1639set_symtab (void)
252b5132
RH
1640{
1641 int nsyms;
1642 asymbol **asympp;
1643 symbolS *symp;
b34976b6 1644 bfd_boolean result;
252b5132
RH
1645
1646 /* Count symbols. We can't rely on a count made by the loop in
1647 write_object_file, because *_frob_file may add a new symbol or
1648 two. */
1649 nsyms = 0;
1650 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1651 nsyms++;
1652
1653 if (nsyms)
1654 {
1655 int i;
0e1a166b 1656 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
252b5132 1657
1e9cc1c2 1658 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
252b5132
RH
1659 symp = symbol_rootP;
1660 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1661 {
49309057 1662 asympp[i] = symbol_get_bfdsym (symp);
76d12939
AM
1663 if (asympp[i]->flags != BSF_SECTION_SYM
1664 || !(bfd_is_const_section (asympp[i]->section)
1665 && asympp[i]->section->symbol == asympp[i]))
1666 asympp[i]->flags |= BSF_KEEP;
49309057 1667 symbol_mark_written (symp);
252b5132
RH
1668 }
1669 }
1670 else
1671 asympp = 0;
1672 result = bfd_set_symtab (stdoutput, asympp, nsyms);
9c2799c2 1673 gas_assert (result);
252b5132
RH
1674 symbol_table_frozen = 1;
1675}
252b5132
RH
1676
1677/* Finish the subsegments. After every sub-segment, we fake an
1678 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1679 ".fill 0" because that is the kind of frag that requires least
1680 thought. ".align" frags like to have a following frag since that
1681 makes calculating their intended length trivial. */
1682
1683#ifndef SUB_SEGMENT_ALIGN
18e1d487 1684#ifdef HANDLE_ALIGN
436d9e46 1685/* The last subsegment gets an alignment corresponding to the alignment
18e1d487
AM
1686 of the section. This allows proper nop-filling at the end of
1687 code-bearing sections. */
1688#define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
c9049d30 1689 (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0)
18e1d487 1690#else
18e1d487 1691#define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
252b5132
RH
1692#endif
1693#endif
1694
85024cd8 1695static void
d7342424 1696subsegs_finish (void)
252b5132
RH
1697{
1698 struct frchain *frchainP;
c9049d30 1699 asection *s;
252b5132 1700
c9049d30 1701 for (s = stdoutput->sections; s; s = s->next)
252b5132 1702 {
c9049d30
AM
1703 segment_info_type *seginfo = seg_info (s);
1704 if (!seginfo)
1705 continue;
0a9ef439 1706
c9049d30
AM
1707 for (frchainP = seginfo->frchainP;
1708 frchainP != NULL;
1709 frchainP = frchainP->frch_next)
381a1ab3 1710 {
c9049d30
AM
1711 int alignment = 0;
1712
1713 subseg_set (s, frchainP->frch_subseg);
381a1ab3 1714
c9049d30
AM
1715 /* This now gets called even if we had errors. In that case,
1716 any alignment is meaningless, and, moreover, will look weird
1717 if we are generating a listing. */
1718 if (!had_errors ())
1719 {
1720 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1721 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1722 && now_seg->entsize)
381a1ab3 1723 {
c9049d30
AM
1724 unsigned int entsize = now_seg->entsize;
1725 int entalign = 0;
1726
1727 while ((entsize & 1) == 0)
1728 {
1729 ++entalign;
1730 entsize >>= 1;
1731 }
1732 if (entalign > alignment)
1733 alignment = entalign;
381a1ab3 1734 }
381a1ab3 1735 }
0a9ef439 1736
c9049d30
AM
1737 if (subseg_text_p (now_seg))
1738 frag_align_code (alignment, 0);
1739 else
1740 frag_align (alignment, 0, 0);
252b5132 1741
c9049d30
AM
1742 /* frag_align will have left a new frag.
1743 Use this last frag for an empty ".fill".
252b5132 1744
c9049d30
AM
1745 For this segment ...
1746 Create a last frag. Do not leave a "being filled in frag". */
1747 frag_wane (frag_now);
1748 frag_now->fr_fix = 0;
1749 know (frag_now->fr_next == NULL);
1750 }
252b5132
RH
1751 }
1752}
1753
1754/* Write the object file. */
1755
1756void
d7342424 1757write_object_file (void)
252b5132 1758{
32638454 1759 struct relax_seg_info rsi;
7be1c489 1760#ifndef WORKING_DOT_WORD
efaf0ba4 1761 fragS *fragP; /* Track along all frags. */
252b5132
RH
1762#endif
1763
85024cd8
AM
1764 subsegs_finish ();
1765
16a87420
IS
1766#ifdef md_pre_output_hook
1767 md_pre_output_hook;
1768#endif
1769
cdaa5616
IS
1770#ifdef md_pre_relax_hook
1771 md_pre_relax_hook;
1772#endif
1773
252b5132
RH
1774 /* From now on, we don't care about sub-segments. Build one frag chain
1775 for each segment. Linked thru fr_next. */
1776
252b5132
RH
1777 /* Remove the sections created by gas for its own purposes. */
1778 {
252b5132
RH
1779 int i;
1780
5daa8fe7
L
1781 bfd_section_list_remove (stdoutput, reg_section);
1782 bfd_section_list_remove (stdoutput, expr_section);
1783 stdoutput->section_count -= 2;
252b5132
RH
1784 i = 0;
1785 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1786 }
1787
1788 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
252b5132
RH
1789
1790 /* We have two segments. If user gave -R flag, then we must put the
1791 data frags into the text segment. Do this before relaxing so
1792 we know to take advantage of -R and make shorter addresses. */
252b5132
RH
1793 if (flag_readonly_data_in_text)
1794 {
1795 merge_data_into_text ();
1796 }
252b5132 1797
32638454 1798 rsi.pass = 0;
e46d99eb
AM
1799 while (1)
1800 {
aacb5251
HPN
1801#ifndef WORKING_DOT_WORD
1802 /* We need to reset the markers in the broken word list and
1803 associated frags between calls to relax_segment (via
1804 relax_seg). Since the broken word list is global, we do it
1805 once per round, rather than locally in relax_segment for each
1806 segment. */
1807 struct broken_word *brokp;
1808
1809 for (brokp = broken_words;
1810 brokp != (struct broken_word *) NULL;
1811 brokp = brokp->next_broken_word)
1812 {
1813 brokp->added = 0;
1814
1815 if (brokp->dispfrag != (fragS *) NULL
1816 && brokp->dispfrag->fr_type == rs_broken_word)
1817 brokp->dispfrag->fr_subtype = 0;
1818 }
1819#endif
1820
32638454
AM
1821 rsi.changed = 0;
1822 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
1823 rsi.pass++;
1824 if (!rsi.changed)
e46d99eb
AM
1825 break;
1826 }
e027f3e8 1827
87548816
NC
1828 /* Note - Most ports will use the default value of
1829 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1830 local symbols to be resolved, removing their frag information.
1831 Some ports however, will not have finished relaxing all of
1832 their frags and will still need the local symbol frag
1833 information. These ports can set
1834 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1835 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1836
e46d99eb 1837 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
e46d99eb 1838
38b87a1b
NC
1839 /* Relaxation has completed. Freeze all syms. */
1840 finalize_syms = 1;
1841
e0001a05
NC
1842#ifdef md_post_relax_hook
1843 md_post_relax_hook;
1844#endif
1845
252b5132
RH
1846#ifndef WORKING_DOT_WORD
1847 {
1848 struct broken_word *lie;
1849 struct broken_word **prevP;
1850
1851 prevP = &broken_words;
1852 for (lie = broken_words; lie; lie = lie->next_broken_word)
1853 if (!lie->added)
1854 {
1855 expressionS exp;
1856
1857 subseg_change (lie->seg, lie->subseg);
1858 exp.X_op = O_subtract;
1859 exp.X_add_symbol = lie->add;
1860 exp.X_op_symbol = lie->sub;
1861 exp.X_add_number = lie->addnum;
252b5132
RH
1862#ifdef TC_CONS_FIX_NEW
1863 TC_CONS_FIX_NEW (lie->frag,
efaf0ba4 1864 lie->word_goes_here - lie->frag->fr_literal,
62ebcb5c 1865 2, &exp, TC_PARSE_CONS_RETURN_NONE);
252b5132
RH
1866#else
1867 fix_new_exp (lie->frag,
1868 lie->word_goes_here - lie->frag->fr_literal,
1869 2, &exp, 0, BFD_RELOC_16);
1870#endif
252b5132
RH
1871 *prevP = lie->next_broken_word;
1872 }
1873 else
1874 prevP = &(lie->next_broken_word);
1875
1876 for (lie = broken_words; lie;)
1877 {
1878 struct broken_word *untruth;
1879 char *table_ptr;
1880 addressT table_addr;
1881 addressT from_addr, to_addr;
1882 int n, m;
1883
1884 subseg_change (lie->seg, lie->subseg);
1885 fragP = lie->dispfrag;
1886
1887 /* Find out how many broken_words go here. */
1888 n = 0;
efaf0ba4
NC
1889 for (untruth = lie;
1890 untruth && untruth->dispfrag == fragP;
1891 untruth = untruth->next_broken_word)
252b5132
RH
1892 if (untruth->added == 1)
1893 n++;
1894
1895 table_ptr = lie->dispfrag->fr_opcode;
efaf0ba4
NC
1896 table_addr = (lie->dispfrag->fr_address
1897 + (table_ptr - lie->dispfrag->fr_literal));
252b5132
RH
1898 /* Create the jump around the long jumps. This is a short
1899 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1900 from_addr = table_addr;
1901 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
efaf0ba4
NC
1902 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1903 lie->add);
252b5132
RH
1904 table_ptr += md_short_jump_size;
1905 table_addr += md_short_jump_size;
1906
efaf0ba4
NC
1907 for (m = 0;
1908 lie && lie->dispfrag == fragP;
1909 m++, lie = lie->next_broken_word)
252b5132
RH
1910 {
1911 if (lie->added == 2)
1912 continue;
efaf0ba4 1913 /* Patch the jump table. */
187af7df 1914 for (untruth = (struct broken_word *) (fragP->fr_symbol);
efaf0ba4
NC
1915 untruth && untruth->dispfrag == fragP;
1916 untruth = untruth->next_broken_word)
252b5132
RH
1917 {
1918 if (untruth->use_jump == lie)
187af7df
HPN
1919 {
1920 /* This is the offset from ??? to table_ptr+0.
1921 The target is the same for all users of this
1922 md_long_jump, but the "sub" bases (and hence the
1923 offsets) may be different. */
1924 addressT to_word = table_addr - S_GET_VALUE (untruth->sub);
1925#ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1926 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word, untruth);
1927#endif
1928 md_number_to_chars (untruth->word_goes_here, to_word, 2);
1929 }
252b5132
RH
1930 }
1931
efaf0ba4
NC
1932 /* Install the long jump. */
1933 /* This is a long jump from table_ptr+0 to the final target. */
252b5132
RH
1934 from_addr = table_addr;
1935 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
efaf0ba4
NC
1936 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1937 lie->add);
252b5132
RH
1938 table_ptr += md_long_jump_size;
1939 table_addr += md_long_jump_size;
1940 }
1941 }
1942 }
efaf0ba4 1943#endif /* not WORKING_DOT_WORD */
252b5132 1944
252b5132
RH
1945 /* Resolve symbol values. This needs to be done before processing
1946 the relocations. */
1947 if (symbol_rootP)
1948 {
1949 symbolS *symp;
1950
1951 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
6386f3a7 1952 resolve_symbol_value (symp);
252b5132 1953 }
49309057 1954 resolve_local_symbol_values ();
05e9452c 1955 resolve_reloc_expr_symbols ();
252b5132
RH
1956
1957 PROGRESS (1);
1958
1959#ifdef tc_frob_file_before_adjust
1960 tc_frob_file_before_adjust ();
1961#endif
1962#ifdef obj_frob_file_before_adjust
1963 obj_frob_file_before_adjust ();
1964#endif
1965
efaf0ba4 1966 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
252b5132 1967
a161fe53
AM
1968#ifdef tc_frob_file_before_fix
1969 tc_frob_file_before_fix ();
1970#endif
1971#ifdef obj_frob_file_before_fix
1972 obj_frob_file_before_fix ();
1973#endif
1974
1975 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1976
252b5132
RH
1977 /* Set up symbol table, and write it out. */
1978 if (symbol_rootP)
1979 {
1980 symbolS *symp;
aaac53f5 1981 bfd_boolean skip_next_symbol = FALSE;
252b5132
RH
1982
1983 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1984 {
1985 int punt = 0;
1986 const char *name;
1987
aaac53f5
HPN
1988 if (skip_next_symbol)
1989 {
1990 /* Don't do anything besides moving the value of the
1991 symbol from the GAS value-field to the BFD value-field. */
1992 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1993 skip_next_symbol = FALSE;
1994 continue;
1995 }
1996
49309057 1997 if (symbol_mri_common_p (symp))
252b5132
RH
1998 {
1999 if (S_IS_EXTERNAL (symp))
2000 as_bad (_("%s: global symbols not supported in common sections"),
2001 S_GET_NAME (symp));
2002 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2003 continue;
2004 }
2005
2006 name = S_GET_NAME (symp);
2007 if (name)
2008 {
efaf0ba4
NC
2009 const char *name2 =
2010 decode_local_label_name ((char *) S_GET_NAME (symp));
252b5132
RH
2011 /* They only differ if `name' is a fb or dollar local
2012 label name. */
2013 if (name2 != name && ! S_IS_DEFINED (symp))
0e389e77 2014 as_bad (_("local label `%s' is not defined"), name2);
252b5132
RH
2015 }
2016
2017 /* Do it again, because adjust_reloc_syms might introduce
2018 more symbols. They'll probably only be section symbols,
2019 but they'll still need to have the values computed. */
6386f3a7 2020 resolve_symbol_value (symp);
252b5132
RH
2021
2022 /* Skip symbols which were equated to undefined or common
b4b24b79 2023 symbols. */
06e77878
AO
2024 if (symbol_equated_reloc_p (symp)
2025 || S_IS_WEAKREFR (symp))
252b5132 2026 {
91d6fa6a
NC
2027 const char *sname = S_GET_NAME (symp);
2028
60938e80 2029 if (S_IS_COMMON (symp)
91d6fa6a 2030 && !TC_FAKE_LABEL (sname)
06e77878 2031 && !S_IS_WEAKREFR (symp)
60938e80
L
2032 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
2033 {
2034 expressionS *e = symbol_get_value_expression (symp);
91d6fa6a 2035
60938e80 2036 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
91d6fa6a 2037 sname, S_GET_NAME (e->X_add_symbol));
60938e80 2038 }
d0548f34
L
2039 if (S_GET_SEGMENT (symp) == reg_section)
2040 {
2041 /* Report error only if we know the symbol name. */
2042 if (S_GET_NAME (symp) != reg_section->name)
2043 as_bad (_("can't make global register symbol `%s'"),
91d6fa6a 2044 sname);
d0548f34 2045 }
252b5132
RH
2046 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2047 continue;
2048 }
2049
252b5132
RH
2050#ifdef obj_frob_symbol
2051 obj_frob_symbol (symp, punt);
2052#endif
2053#ifdef tc_frob_symbol
49309057 2054 if (! punt || symbol_used_in_reloc_p (symp))
252b5132
RH
2055 tc_frob_symbol (symp, punt);
2056#endif
2057
2058 /* If we don't want to keep this symbol, splice it out of
2059 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
2060 want section symbols. Otherwise, we skip local symbols
2061 and symbols that the frob_symbol macros told us to punt,
2062 but we keep such symbols if they are used in relocs. */
a161fe53
AM
2063 if (symp == abs_section_sym
2064 || (! EMIT_SECTION_SYMBOLS
2065 && symbol_section_p (symp))
e97b3f28 2066 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
252b5132
RH
2067 opposites. Sometimes the former checks flags and the
2068 latter examines the name... */
e97b3f28 2069 || (!S_IS_EXTERNAL (symp)
461b725f
AO
2070 && (punt || S_IS_LOCAL (symp) ||
2071 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
49309057 2072 && ! symbol_used_in_reloc_p (symp)))
252b5132
RH
2073 {
2074 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
efaf0ba4 2075
252b5132
RH
2076 /* After symbol_remove, symbol_next(symp) still returns
2077 the one that came after it in the chain. So we don't
2078 need to do any extra cleanup work here. */
252b5132
RH
2079 continue;
2080 }
2081
2082 /* Make sure we really got a value for the symbol. */
49309057 2083 if (! symbol_resolved_p (symp))
252b5132 2084 {
0e389e77 2085 as_bad (_("can't resolve value for symbol `%s'"),
252b5132 2086 S_GET_NAME (symp));
49309057 2087 symbol_mark_resolved (symp);
252b5132
RH
2088 }
2089
2090 /* Set the value into the BFD symbol. Up til now the value
2091 has only been kept in the gas symbolS struct. */
49309057 2092 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
aaac53f5
HPN
2093
2094 /* A warning construct is a warning symbol followed by the
2095 symbol warned about. Don't let anything object-format or
2096 target-specific muck with it; it's ready for output. */
2097 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
2098 skip_next_symbol = TRUE;
252b5132
RH
2099 }
2100 }
2101
2102 PROGRESS (1);
2103
2104 /* Now do any format-specific adjustments to the symbol table, such
2105 as adding file symbols. */
2106#ifdef tc_adjust_symtab
2107 tc_adjust_symtab ();
2108#endif
2109#ifdef obj_adjust_symtab
2110 obj_adjust_symtab ();
2111#endif
2112
d0548f34
L
2113 /* Stop if there is an error. */
2114 if (had_errors ())
2115 return;
2116
252b5132
RH
2117 /* Now that all the sizes are known, and contents correct, we can
2118 start writing to the file. */
2ae08483 2119 set_symtab ();
252b5132
RH
2120
2121 /* If *_frob_file changes the symbol value at this point, it is
2122 responsible for moving the changed value into symp->bsym->value
2123 as well. Hopefully all symbol value changing can be done in
2124 *_frob_symbol. */
2125#ifdef tc_frob_file
2126 tc_frob_file ();
2127#endif
2128#ifdef obj_frob_file
2129 obj_frob_file ();
2130#endif
f3d2b04b
KT
2131#ifdef obj_coff_generate_pdata
2132 obj_coff_generate_pdata ();
2133#endif
252b5132
RH
2134 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2135
f1c2d4af 2136#ifdef tc_frob_file_after_relocs
252b5132
RH
2137 tc_frob_file_after_relocs ();
2138#endif
2139#ifdef obj_frob_file_after_relocs
2140 obj_frob_file_after_relocs ();
2141#endif
f1c2d4af 2142
0acf065b
CC
2143 /* Once all relocations have been written, we can compress the
2144 contents of the debug sections. This needs to be done before
2145 we start writing any sections, because it will affect the file
2146 layout, which is fixed once we start writing contents. */
2147 if (flag_compress_debug)
2148 bfd_map_over_sections (stdoutput, compress_debug, (char *) 0);
2149
252b5132 2150 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
252b5132 2151}
252b5132
RH
2152
2153#ifdef TC_GENERIC_RELAX_TABLE
252b5132
RH
2154/* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2155
2156long
d7342424 2157relax_frag (segT segment, fragS *fragP, long stretch)
252b5132
RH
2158{
2159 const relax_typeS *this_type;
2160 const relax_typeS *start_type;
2161 relax_substateT next_state;
2162 relax_substateT this_state;
685736be 2163 offsetT growth;
38686296
AM
2164 offsetT aim;
2165 addressT target;
2166 addressT address;
2167 symbolS *symbolP;
2168 const relax_typeS *table;
2169
2170 target = fragP->fr_offset;
2171 address = fragP->fr_address;
2172 table = TC_GENERIC_RELAX_TABLE;
252b5132
RH
2173 this_state = fragP->fr_subtype;
2174 start_type = this_type = table + this_state;
38686296 2175 symbolP = fragP->fr_symbol;
252b5132
RH
2176
2177 if (symbolP)
2178 {
c842b53a
ILT
2179 fragS *sym_frag;
2180
2181 sym_frag = symbol_get_frag (symbolP);
2182
252b5132 2183#ifndef DIFF_EXPR_OK
c842b53a 2184 know (sym_frag != NULL);
252b5132 2185#endif
a161fe53 2186 know (S_GET_SEGMENT (symbolP) != absolute_section
c842b53a 2187 || sym_frag == &zero_address_frag);
ac62c346 2188 target += S_GET_VALUE (symbolP);
252b5132 2189
e35a414d
AM
2190 /* If SYM_FRAG has yet to be reached on this pass, assume it
2191 will move by STRETCH just as we did, unless there is an
2192 alignment frag between here and SYM_FRAG. An alignment may
2193 well absorb any STRETCH, and we don't want to choose a larger
2194 branch insn by overestimating the needed reach of this
2195 branch. It isn't critical to calculate TARGET exactly; We
2196 know we'll be doing another pass if STRETCH is non-zero. */
252b5132 2197
c842b53a 2198 if (stretch != 0
38686296
AM
2199 && sym_frag->relax_marker != fragP->relax_marker
2200 && S_GET_SEGMENT (symbolP) == segment)
252b5132 2201 {
e35a414d
AM
2202 if (stretch < 0
2203 || sym_frag->region == fragP->region)
2204 target += stretch;
e5940dff
AM
2205 /* If we get here we know we have a forward branch. This
2206 relax pass may have stretched previous instructions so
2207 far that omitting STRETCH would make the branch
2208 negative. Don't allow this in case the negative reach is
2209 large enough to require a larger branch instruction. */
2210 else if (target < address)
2211 target = fragP->fr_next->fr_address + stretch;
252b5132
RH
2212 }
2213 }
2214
2215 aim = target - address - fragP->fr_fix;
2216#ifdef TC_PCREL_ADJUST
efaf0ba4
NC
2217 /* Currently only the ns32k family needs this. */
2218 aim += TC_PCREL_ADJUST (fragP);
efaf0ba4 2219#endif
59c871b4
BE
2220
2221#ifdef md_prepare_relax_scan
2222 /* Formerly called M68K_AIM_KLUDGE. */
252b5132
RH
2223 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2224#endif
2225
2226 if (aim < 0)
2227 {
efaf0ba4 2228 /* Look backwards. */
252b5132
RH
2229 for (next_state = this_type->rlx_more; next_state;)
2230 if (aim >= this_type->rlx_backward)
2231 next_state = 0;
2232 else
2233 {
efaf0ba4 2234 /* Grow to next state. */
252b5132
RH
2235 this_state = next_state;
2236 this_type = table + this_state;
2237 next_state = this_type->rlx_more;
2238 }
2239 }
2240 else
2241 {
efaf0ba4 2242 /* Look forwards. */
252b5132
RH
2243 for (next_state = this_type->rlx_more; next_state;)
2244 if (aim <= this_type->rlx_forward)
2245 next_state = 0;
2246 else
2247 {
efaf0ba4 2248 /* Grow to next state. */
252b5132
RH
2249 this_state = next_state;
2250 this_type = table + this_state;
2251 next_state = this_type->rlx_more;
2252 }
2253 }
2254
2255 growth = this_type->rlx_length - start_type->rlx_length;
2256 if (growth != 0)
2257 fragP->fr_subtype = this_state;
2258 return growth;
2259}
2260
efaf0ba4 2261#endif /* defined (TC_GENERIC_RELAX_TABLE) */
252b5132
RH
2262
2263/* Relax_align. Advance location counter to next address that has 'alignment'
2264 lowest order bits all 0s, return size of adjustment made. */
2265static relax_addressT
d7342424
KH
2266relax_align (register relax_addressT address, /* Address now. */
2267 register int alignment /* Alignment (binary). */)
252b5132
RH
2268{
2269 relax_addressT mask;
2270 relax_addressT new_address;
2271
2272 mask = ~((~0) << alignment);
2273 new_address = (address + mask) & (~mask);
2274#ifdef LINKER_RELAXING_SHRINKS_ONLY
2275 if (linkrelax)
2276 /* We must provide lots of padding, so the linker can discard it
2277 when needed. The linker will not add extra space, ever. */
2278 new_address += (1 << alignment);
2279#endif
2280 return (new_address - address);
2281}
2282
efaf0ba4
NC
2283/* Now we have a segment, not a crowd of sub-segments, we can make
2284 fr_address values.
58a77e41 2285
efaf0ba4 2286 Relax the frags.
58a77e41 2287
efaf0ba4
NC
2288 After this, all frags in this segment have addresses that are correct
2289 within the segment. Since segments live in different file addresses,
2290 these frag addresses may not be the same as final object-file
2291 addresses. */
2292
e46d99eb 2293int
32638454 2294relax_segment (struct frag *segment_frag_root, segT segment, int pass)
252b5132 2295{
4111faa5
NC
2296 unsigned long frag_count;
2297 struct frag *fragP;
2298 relax_addressT address;
e35a414d 2299 int region;
e46d99eb
AM
2300 int ret;
2301
efaf0ba4 2302 /* In case md_estimate_size_before_relax() wants to make fixSs. */
252b5132
RH
2303 subseg_change (segment, 0);
2304
2305 /* For each frag in segment: count and store (a 1st guess of)
2306 fr_address. */
2307 address = 0;
e35a414d 2308 region = 0;
4111faa5
NC
2309 for (frag_count = 0, fragP = segment_frag_root;
2310 fragP;
2311 fragP = fragP->fr_next, frag_count ++)
252b5132 2312 {
e35a414d 2313 fragP->region = region;
38686296 2314 fragP->relax_marker = 0;
252b5132
RH
2315 fragP->fr_address = address;
2316 address += fragP->fr_fix;
2317
2318 switch (fragP->fr_type)
2319 {
2320 case rs_fill:
2321 address += fragP->fr_offset * fragP->fr_var;
2322 break;
2323
2324 case rs_align:
2325 case rs_align_code:
0a9ef439 2326 case rs_align_test:
252b5132
RH
2327 {
2328 addressT offset = relax_align (address, (int) fragP->fr_offset);
2329
2330 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2331 offset = 0;
2332
2333 if (offset % fragP->fr_var != 0)
2334 {
54d3cad9
AM
2335 as_bad_where (fragP->fr_file, fragP->fr_line,
2336 _("alignment padding (%lu bytes) not a multiple of %ld"),
2337 (unsigned long) offset, (long) fragP->fr_var);
252b5132
RH
2338 offset -= (offset % fragP->fr_var);
2339 }
2340
2341 address += offset;
e35a414d 2342 region += 1;
252b5132
RH
2343 }
2344 break;
2345
2346 case rs_org:
252b5132 2347 /* Assume .org is nugatory. It will grow with 1st relax. */
e35a414d
AM
2348 region += 1;
2349 break;
2350
2351 case rs_space:
252b5132
RH
2352 break;
2353
2354 case rs_machine_dependent:
e0890092
AM
2355 /* If fr_symbol is an expression, this call to
2356 resolve_symbol_value sets up the correct segment, which will
2357 likely be needed in md_estimate_size_before_relax. */
2358 if (fragP->fr_symbol)
2359 resolve_symbol_value (fragP->fr_symbol);
2360
252b5132
RH
2361 address += md_estimate_size_before_relax (fragP, segment);
2362 break;
2363
2364#ifndef WORKING_DOT_WORD
efaf0ba4 2365 /* Broken words don't concern us yet. */
252b5132
RH
2366 case rs_broken_word:
2367 break;
2368#endif
2369
2370 case rs_leb128:
efaf0ba4 2371 /* Initial guess is always 1; doing otherwise can result in
252b5132
RH
2372 stable solutions that are larger than the minimum. */
2373 address += fragP->fr_offset = 1;
2374 break;
2375
2376 case rs_cfa:
2377 address += eh_frame_estimate_size_before_relax (fragP);
2378 break;
2379
220e750f
RH
2380 case rs_dwarf2dbg:
2381 address += dwarf2dbg_estimate_size_before_relax (fragP);
2382 break;
2383
252b5132
RH
2384 default:
2385 BAD_CASE (fragP->fr_type);
2386 break;
efaf0ba4
NC
2387 }
2388 }
252b5132
RH
2389
2390 /* Do relax(). */
2391 {
4111faa5 2392 unsigned long max_iterations;
d1c5f158
AM
2393
2394 /* Cumulative address adjustment. */
2395 offsetT stretch;
2396
2397 /* Have we made any adjustment this pass? We can't just test
2398 stretch because one piece of code may have grown and another
2399 shrank. */
2400 int stretched;
2401
2402 /* Most horrible, but gcc may give us some exception data that
2403 is impossible to assemble, of the form
2404
2405 .align 4
2406 .byte 0, 0
2407 .uleb128 end - start
2408 start:
2409 .space 128*128 - 1
2410 .align 4
2411 end:
2412
2413 If the leb128 is two bytes in size, then end-start is 128*128,
2414 which requires a three byte leb128. If the leb128 is three
2415 bytes in size, then end-start is 128*128-1, which requires a
2416 two byte leb128. We work around this dilemma by inserting
2417 an extra 4 bytes of alignment just after the .align. This
2418 works because the data after the align is accessed relative to
2419 the end label.
2420
2421 This counter is used in a tiny state machine to detect
2422 whether a leb128 followed by an align is impossible to
2423 relax. */
2424 int rs_leb128_fudge = 0;
252b5132 2425
4111faa5
NC
2426 /* We want to prevent going into an infinite loop where one frag grows
2427 depending upon the location of a symbol which is in turn moved by
2428 the growing frag. eg:
2429
b4b24b79
AM
2430 foo = .
2431 .org foo+16
2432 foo = .
4111faa5 2433
b4b24b79 2434 So we dictate that this algorithm can be at most O2. */
4111faa5
NC
2435 max_iterations = frag_count * frag_count;
2436 /* Check for overflow. */
2437 if (max_iterations < frag_count)
2438 max_iterations = frag_count;
2439
32638454 2440 ret = 0;
252b5132
RH
2441 do
2442 {
e46d99eb
AM
2443 stretch = 0;
2444 stretched = 0;
58a77e41 2445
252b5132
RH
2446 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2447 {
685736be 2448 offsetT growth = 0;
252b5132
RH
2449 addressT was_address;
2450 offsetT offset;
2451 symbolS *symbolP;
2452
38686296 2453 fragP->relax_marker ^= 1;
252b5132
RH
2454 was_address = fragP->fr_address;
2455 address = fragP->fr_address += stretch;
2456 symbolP = fragP->fr_symbol;
2457 offset = fragP->fr_offset;
2458
2459 switch (fragP->fr_type)
2460 {
efaf0ba4 2461 case rs_fill: /* .fill never relaxes. */
252b5132
RH
2462 growth = 0;
2463 break;
2464
2465#ifndef WORKING_DOT_WORD
2466 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2467 for it I do not want to write it. I do not want to have
2468 anything to do with it. This is not the proper way to
2469 implement this misfeature. */
2470 case rs_broken_word:
2471 {
2472 struct broken_word *lie;
2473 struct broken_word *untruth;
2474
2475 /* Yes this is ugly (storing the broken_word pointer
2476 in the symbol slot). Still, this whole chunk of
2477 code is ugly, and I don't feel like doing anything
2478 about it. Think of it as stubbornness in action. */
2479 growth = 0;
2480 for (lie = (struct broken_word *) (fragP->fr_symbol);
2481 lie && lie->dispfrag == fragP;
2482 lie = lie->next_broken_word)
2483 {
2484
2485 if (lie->added)
2486 continue;
2487
ac62c346 2488 offset = (S_GET_VALUE (lie->add)
252b5132 2489 + lie->addnum
ac62c346 2490 - S_GET_VALUE (lie->sub));
252b5132
RH
2491 if (offset <= -32768 || offset >= 32767)
2492 {
2493 if (flag_warn_displacement)
2494 {
2495 char buf[50];
2496 sprint_value (buf, (addressT) lie->addnum);
54d3cad9
AM
2497 as_warn_where (fragP->fr_file, fragP->fr_line,
2498 _(".word %s-%s+%s didn't fit"),
2499 S_GET_NAME (lie->add),
2500 S_GET_NAME (lie->sub),
2501 buf);
252b5132 2502 }
252b5132
RH
2503 if (fragP->fr_subtype == 0)
2504 {
2505 fragP->fr_subtype++;
2506 growth += md_short_jump_size;
2507 }
187af7df
HPN
2508
2509 /* Redirect *all* words of this table with the same
2510 target, lest we have to handle the case where the
2511 same target but with a offset that fits on this
2512 round overflows at the next relaxation round. */
2513 for (untruth = (struct broken_word *) (fragP->fr_symbol);
252b5132
RH
2514 untruth && untruth->dispfrag == lie->dispfrag;
2515 untruth = untruth->next_broken_word)
49309057
ILT
2516 if ((symbol_get_frag (untruth->add)
2517 == symbol_get_frag (lie->add))
2518 && (S_GET_VALUE (untruth->add)
2519 == S_GET_VALUE (lie->add)))
252b5132
RH
2520 {
2521 untruth->added = 2;
2522 untruth->use_jump = lie;
2523 }
187af7df
HPN
2524
2525 lie->added = 1;
252b5132
RH
2526 growth += md_long_jump_size;
2527 }
2528 }
2529
2530 break;
efaf0ba4 2531 } /* case rs_broken_word */
252b5132
RH
2532#endif
2533 case rs_align:
2534 case rs_align_code:
0a9ef439 2535 case rs_align_test:
252b5132
RH
2536 {
2537 addressT oldoff, newoff;
2538
2539 oldoff = relax_align (was_address + fragP->fr_fix,
2540 (int) offset);
2541 newoff = relax_align (address + fragP->fr_fix,
2542 (int) offset);
2543
2544 if (fragP->fr_subtype != 0)
2545 {
2546 if (oldoff > fragP->fr_subtype)
2547 oldoff = 0;
2548 if (newoff > fragP->fr_subtype)
2549 newoff = 0;
2550 }
2551
2552 growth = newoff - oldoff;
d1c5f158
AM
2553
2554 /* If this align happens to follow a leb128 and
2555 we have determined that the leb128 is bouncing
2556 in size, then break the cycle by inserting an
2557 extra alignment. */
2558 if (growth < 0
2559 && (rs_leb128_fudge & 16) != 0
2560 && (rs_leb128_fudge & 15) >= 2)
2561 {
2562 segment_info_type *seginfo = seg_info (segment);
2563 struct obstack *ob = &seginfo->frchainP->frch_obstack;
2564 struct frag *newf;
2565
2566 newf = frag_alloc (ob);
2567 obstack_blank_fast (ob, fragP->fr_var);
2568 obstack_finish (ob);
2569 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2570 memcpy (newf->fr_literal,
2571 fragP->fr_literal + fragP->fr_fix,
2572 fragP->fr_var);
2573 newf->fr_type = rs_fill;
20f5cfbd 2574 newf->fr_address = address + fragP->fr_fix + newoff;
d1c5f158
AM
2575 newf->fr_fix = 0;
2576 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2577 / fragP->fr_var);
2578 if (newf->fr_offset * newf->fr_var
2579 != (offsetT) 1 << fragP->fr_offset)
2580 {
2581 newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
2582 newf->fr_var = 1;
2583 }
20f5cfbd 2584 /* Include size of new frag in GROWTH. */
d1c5f158 2585 growth += newf->fr_offset * newf->fr_var;
20f5cfbd
AM
2586 /* Adjust the new frag address for the amount
2587 we'll add when we process the new frag. */
2588 newf->fr_address -= stretch + growth;
d1c5f158
AM
2589 newf->relax_marker ^= 1;
2590 fragP->fr_next = newf;
2591#ifdef DEBUG
2592 as_warn (_("padding added"));
2593#endif
2594 }
252b5132
RH
2595 }
2596 break;
2597
2598 case rs_org:
2599 {
e46d99eb
AM
2600 addressT target = offset;
2601 addressT after;
252b5132
RH
2602
2603 if (symbolP)
2604 {
b4b24b79
AM
2605 /* Convert from an actual address to an octet offset
2606 into the section. Here it is assumed that the
2607 section's VMA is zero, and can omit subtracting it
2608 from the symbol's value to get the address offset. */
2609 know (S_GET_SEGMENT (symbolP)->vma == 0);
6e917903 2610 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
ac62c346 2611 }
252b5132
RH
2612
2613 know (fragP->fr_next);
6afe8e98 2614 after = fragP->fr_next->fr_address + stretch;
252b5132
RH
2615 growth = target - after;
2616 if (growth < 0)
2617 {
32638454
AM
2618 growth = 0;
2619
2620 /* Don't error on first few frag relax passes.
2621 The symbol might be an expression involving
2622 symbol values from other sections. If those
2623 sections have not yet been processed their
2624 frags will all have zero addresses, so we
2625 will calculate incorrect values for them. The
2626 number of passes we allow before giving an
2627 error is somewhat arbitrary. It should be at
2628 least one, with larger values requiring
2629 increasingly contrived dependencies between
2630 frags to trigger a false error. */
2631 if (pass < 2)
2632 {
2633 /* Force another pass. */
2634 ret = 1;
2635 break;
2636 }
2637
252b5132
RH
2638 /* Growth may be negative, but variable part of frag
2639 cannot have fewer than 0 chars. That is, we can't
efaf0ba4 2640 .org backwards. */
14ad458a 2641 as_bad_where (fragP->fr_file, fragP->fr_line,
685736be 2642 _("attempt to move .org backwards"));
14ad458a
ILT
2643
2644 /* We've issued an error message. Change the
b4b24b79 2645 frag to avoid cascading errors. */
14ad458a
ILT
2646 fragP->fr_type = rs_align;
2647 fragP->fr_subtype = 0;
2648 fragP->fr_offset = 0;
6afe8e98 2649 fragP->fr_fix = after - address;
252b5132 2650 }
252b5132 2651 }
6afe8e98 2652 break;
252b5132
RH
2653
2654 case rs_space:
54d3cad9 2655 growth = 0;
252b5132
RH
2656 if (symbolP)
2657 {
766c03c9
AM
2658 offsetT amount;
2659
2660 amount = S_GET_VALUE (symbolP);
9e40345d 2661 if (S_GET_SEGMENT (symbolP) != absolute_section
252b5132
RH
2662 || S_IS_COMMON (symbolP)
2663 || ! S_IS_DEFINED (symbolP))
252b5132 2664 {
54d3cad9
AM
2665 as_bad_where (fragP->fr_file, fragP->fr_line,
2666 _(".space specifies non-absolute value"));
2667 /* Prevent repeat of this error message. */
2668 fragP->fr_symbol = 0;
2669 }
2670 else if (amount < 0)
2671 {
32638454
AM
2672 /* Don't error on first few frag relax passes.
2673 See rs_org comment for a longer explanation. */
2674 if (pass < 2)
2675 {
2676 ret = 1;
2677 break;
2678 }
2679
54d3cad9
AM
2680 as_warn_where (fragP->fr_file, fragP->fr_line,
2681 _(".space or .fill with negative value, ignored"));
766c03c9 2682 fragP->fr_symbol = 0;
252b5132 2683 }
54d3cad9 2684 else
050be34e 2685 growth = (was_address + fragP->fr_fix + amount
54d3cad9 2686 - fragP->fr_next->fr_address);
252b5132 2687 }
252b5132
RH
2688 break;
2689
2690 case rs_machine_dependent:
2691#ifdef md_relax_frag
c842b53a 2692 growth = md_relax_frag (segment, fragP, stretch);
252b5132
RH
2693#else
2694#ifdef TC_GENERIC_RELAX_TABLE
2695 /* The default way to relax a frag is to look through
2696 TC_GENERIC_RELAX_TABLE. */
c842b53a 2697 growth = relax_frag (segment, fragP, stretch);
efaf0ba4 2698#endif /* TC_GENERIC_RELAX_TABLE */
252b5132
RH
2699#endif
2700 break;
2701
2702 case rs_leb128:
2703 {
2704 valueT value;
685736be 2705 offsetT size;
252b5132 2706
6386f3a7 2707 value = resolve_symbol_value (fragP->fr_symbol);
252b5132
RH
2708 size = sizeof_leb128 (value, fragP->fr_subtype);
2709 growth = size - fragP->fr_offset;
2710 fragP->fr_offset = size;
2711 }
2712 break;
2713
2714 case rs_cfa:
2715 growth = eh_frame_relax_frag (fragP);
2716 break;
2717
220e750f
RH
2718 case rs_dwarf2dbg:
2719 growth = dwarf2dbg_relax_frag (fragP);
2720 break;
2721
252b5132
RH
2722 default:
2723 BAD_CASE (fragP->fr_type);
2724 break;
2725 }
2726 if (growth)
2727 {
2728 stretch += growth;
e46d99eb 2729 stretched = 1;
d1c5f158
AM
2730 if (fragP->fr_type == rs_leb128)
2731 rs_leb128_fudge += 16;
2732 else if (fragP->fr_type == rs_align
2733 && (rs_leb128_fudge & 16) != 0
2734 && stretch == 0)
2735 rs_leb128_fudge += 16;
2736 else
2737 rs_leb128_fudge = 0;
252b5132 2738 }
4111faa5 2739 }
d1c5f158
AM
2740
2741 if (stretch == 0
2742 && (rs_leb128_fudge & 16) == 0
2743 && (rs_leb128_fudge & -16) != 0)
2744 rs_leb128_fudge += 1;
2745 else
2746 rs_leb128_fudge = 0;
252b5132 2747 }
4111faa5
NC
2748 /* Until nothing further to relax. */
2749 while (stretched && -- max_iterations);
2750
2751 if (stretched)
2752 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2753 segment_name (segment));
2754 }
252b5132 2755
e46d99eb
AM
2756 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2757 if (fragP->last_fr_address != fragP->fr_address)
2758 {
2759 fragP->last_fr_address = fragP->fr_address;
2760 ret = 1;
2761 }
2762 return ret;
efaf0ba4 2763}
252b5132 2764
252b5132 2765void
d7342424 2766number_to_chars_bigendian (char *buf, valueT val, int n)
252b5132 2767{
937149dd 2768 if (n <= 0)
252b5132
RH
2769 abort ();
2770 while (n--)
2771 {
2772 buf[n] = val & 0xff;
2773 val >>= 8;
2774 }
2775}
2776
2777void
d7342424 2778number_to_chars_littleendian (char *buf, valueT val, int n)
252b5132 2779{
937149dd 2780 if (n <= 0)
252b5132
RH
2781 abort ();
2782 while (n--)
2783 {
2784 *buf++ = val & 0xff;
2785 val >>= 8;
2786 }
2787}
2788
2789void
d7342424 2790write_print_statistics (FILE *file)
252b5132 2791{
6d4d30bb 2792 fprintf (file, "fixups: %d\n", n_fixups);
252b5132
RH
2793}
2794
efaf0ba4 2795/* For debugging. */
252b5132
RH
2796extern int indent_level;
2797
2798void
d7342424 2799print_fixup (fixS *fixp)
252b5132
RH
2800{
2801 indent_level = 1;
d2df793a
NC
2802 fprintf (stderr, "fix ");
2803 fprintf_vma (stderr, (bfd_vma)((bfd_hostptr_t) fixp));
2804 fprintf (stderr, " %s:%d",fixp->fx_file, fixp->fx_line);
252b5132
RH
2805 if (fixp->fx_pcrel)
2806 fprintf (stderr, " pcrel");
2807 if (fixp->fx_pcrel_adjust)
2808 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2809 if (fixp->fx_im_disp)
2810 {
2811#ifdef TC_NS32K
2812 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2813#else
2814 fprintf (stderr, " im_disp");
2815#endif
2816 }
2817 if (fixp->fx_tcbit)
2818 fprintf (stderr, " tcbit");
2819 if (fixp->fx_done)
2820 fprintf (stderr, " done");
d2df793a
NC
2821 fprintf (stderr, "\n size=%d frag=", fixp->fx_size);
2822 fprintf_vma (stderr, (bfd_vma) ((bfd_hostptr_t) fixp->fx_frag));
2823 fprintf (stderr, " where=%ld offset=%lx addnumber=%lx",
2824 (long) fixp->fx_where,
0af1713e
AM
2825 (unsigned long) fixp->fx_offset,
2826 (unsigned long) fixp->fx_addnumber);
252b5132
RH
2827 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2828 fixp->fx_r_type);
252b5132
RH
2829 if (fixp->fx_addsy)
2830 {
2831 fprintf (stderr, "\n +<");
2832 print_symbol_value_1 (stderr, fixp->fx_addsy);
2833 fprintf (stderr, ">");
2834 }
2835 if (fixp->fx_subsy)
2836 {
2837 fprintf (stderr, "\n -<");
2838 print_symbol_value_1 (stderr, fixp->fx_subsy);
2839 fprintf (stderr, ">");
2840 }
2841 fprintf (stderr, "\n");
2842#ifdef TC_FIX_DATA_PRINT
2843 TC_FIX_DATA_PRINT (stderr, fixp);
2844#endif
2845}
This page took 0.863573 seconds and 4 git commands to generate.