OSDN Git Service

gas/ChangeLog:
[pf3gnuchains/pf3gnuchains3x.git] / gas / write.c
1 /* write.c - emit .o file
2    Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3    1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4    Free Software Foundation, Inc.
5
6    This file is part of GAS, the GNU Assembler.
7
8    GAS is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GAS is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GAS; see the file COPYING.  If not, write to the Free
20    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21    02110-1301, USA.  */
22
23 /* This thing should be set up to do byteordering correctly.  But...  */
24
25 #include "as.h"
26 #include "subsegs.h"
27 #include "obstack.h"
28 #include "output-file.h"
29 #include "dwarf2dbg.h"
30
31 #ifndef TC_ADJUST_RELOC_COUNT
32 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
33 #endif
34
35 #ifndef TC_FORCE_RELOCATION
36 #define TC_FORCE_RELOCATION(FIX)                \
37   (generic_force_reloc (FIX))
38 #endif
39
40 #ifndef TC_FORCE_RELOCATION_ABS
41 #define TC_FORCE_RELOCATION_ABS(FIX)            \
42   (TC_FORCE_RELOCATION (FIX))
43 #endif
44
45 #ifndef TC_FORCE_RELOCATION_LOCAL
46 #define TC_FORCE_RELOCATION_LOCAL(FIX)          \
47   (!(FIX)->fx_pcrel                             \
48    || (FIX)->fx_plt                             \
49    || TC_FORCE_RELOCATION (FIX))
50 #endif
51
52 #ifndef TC_FORCE_RELOCATION_SUB_SAME
53 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG)  \
54   (! SEG_NORMAL (SEG))
55 #endif
56
57 #ifndef TC_FORCE_RELOCATION_SUB_ABS
58 #define TC_FORCE_RELOCATION_SUB_ABS(FIX)        0
59 #endif
60
61 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
62 #ifdef DIFF_EXPR_OK
63 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX)      0
64 #else
65 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX)      1
66 #endif
67 #endif
68
69 #ifndef TC_VALIDATE_FIX_SUB
70 #ifdef UNDEFINED_DIFFERENCE_OK
71 /* The PA needs this for PIC code generation.  */
72 #define TC_VALIDATE_FIX_SUB(FIX) 1
73 #else
74 #define TC_VALIDATE_FIX_SUB(FIX)                \
75   ((FIX)->fx_r_type == BFD_RELOC_GPREL32        \
76    || (FIX)->fx_r_type == BFD_RELOC_GPREL16)
77 #endif
78 #endif
79
80 #ifndef TC_LINKRELAX_FIXUP
81 #define TC_LINKRELAX_FIXUP(SEG) 1
82 #endif
83
84 #ifndef MD_APPLY_SYM_VALUE
85 #define MD_APPLY_SYM_VALUE(FIX) 1
86 #endif
87
88 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
89 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
90 #endif
91
92 #ifndef MD_PCREL_FROM_SECTION
93 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
94 #endif
95
96 #ifndef TC_FAKE_LABEL
97 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
98 #endif
99
100 /* Used to control final evaluation of expressions.  */
101 int finalize_syms = 0;
102
103 int symbol_table_frozen;
104
105 symbolS *abs_section_sym;
106
107 /* Remember the value of dot when parsing expressions.  */
108 addressT dot_value;
109
110 void print_fixup (fixS *);
111
112 static void renumber_sections (bfd *, asection *, PTR);
113
114 /* We generally attach relocs to frag chains.  However, after we have
115    chained these all together into a segment, any relocs we add after
116    that must be attached to a segment.  This will include relocs added
117    in md_estimate_size_for_relax, for example.  */
118 static int frags_chained = 0;
119
120 static int n_fixups;
121
122 #define RELOC_ENUM enum bfd_reloc_code_real
123
124 static fixS *fix_new_internal (fragS *, int where, int size,
125                                symbolS *add, symbolS *sub,
126                                offsetT offset, int pcrel,
127                                RELOC_ENUM r_type);
128 static long fixup_segment (fixS *, segT);
129 static relax_addressT relax_align (relax_addressT addr, int align);
130 static fragS *chain_frchains_together_1 (segT, struct frchain *);
131 static void chain_frchains_together (bfd *, segT, PTR);
132 static void cvt_frag_to_fill (segT, fragS *);
133 static void adjust_reloc_syms (bfd *, asection *, PTR);
134 static void fix_segment (bfd *, asection *, PTR);
135 static void write_relocs (bfd *, asection *, PTR);
136 static void write_contents (bfd *, asection *, PTR);
137 static void set_symtab (void);
138 static void merge_data_into_text (void);
139
140 /* Create a fixS in obstack 'notes'.  */
141
142 static fixS *
143 fix_new_internal (fragS *frag,          /* Which frag?  */
144                   int where,            /* Where in that frag?  */
145                   int size,             /* 1, 2, or 4 usually.  */
146                   symbolS *add_symbol,  /* X_add_symbol.  */
147                   symbolS *sub_symbol,  /* X_op_symbol.  */
148                   offsetT offset,       /* X_add_number.  */
149                   int pcrel,            /* TRUE if PC-relative relocation.  */
150                   RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type.  */)
151 {
152   fixS *fixP;
153
154   n_fixups++;
155
156   fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
157
158   fixP->fx_frag = frag;
159   fixP->fx_where = where;
160   fixP->fx_size = size;
161   /* We've made fx_size a narrow field; check that it's wide enough.  */
162   if (fixP->fx_size != size)
163     {
164       as_bad (_("field fx_size too small to hold %d"), size);
165       abort ();
166     }
167   fixP->fx_addsy = add_symbol;
168   fixP->fx_subsy = sub_symbol;
169   fixP->fx_offset = offset;
170   fixP->fx_dot_value = dot_value;
171   fixP->fx_pcrel = pcrel;
172   fixP->fx_plt = 0;
173   fixP->fx_r_type = r_type;
174   fixP->fx_im_disp = 0;
175   fixP->fx_pcrel_adjust = 0;
176   fixP->fx_bit_fixP = 0;
177   fixP->fx_addnumber = 0;
178   fixP->fx_tcbit = 0;
179   fixP->fx_done = 0;
180   fixP->fx_no_overflow = 0;
181   fixP->fx_signed = 0;
182
183 #ifdef USING_CGEN
184   fixP->fx_cgen.insn = NULL;
185   fixP->fx_cgen.opinfo = 0;
186 #endif
187
188 #ifdef TC_FIX_TYPE
189   TC_INIT_FIX_DATA (fixP);
190 #endif
191
192   as_where (&fixP->fx_file, &fixP->fx_line);
193
194   /* Usually, we want relocs sorted numerically, but while
195      comparing to older versions of gas that have relocs
196      reverse sorted, it is convenient to have this compile
197      time option.  xoxorich.  */
198   {
199
200     fixS **seg_fix_rootP = (frags_chained
201                             ? &seg_info (now_seg)->fix_root
202                             : &frchain_now->fix_root);
203     fixS **seg_fix_tailP = (frags_chained
204                             ? &seg_info (now_seg)->fix_tail
205                             : &frchain_now->fix_tail);
206
207 #ifdef REVERSE_SORT_RELOCS
208
209     fixP->fx_next = *seg_fix_rootP;
210     *seg_fix_rootP = fixP;
211
212 #else /* REVERSE_SORT_RELOCS  */
213
214     fixP->fx_next = NULL;
215
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
222 #endif /* REVERSE_SORT_RELOCS  */
223   }
224
225   return fixP;
226 }
227
228 /* Create a fixup relative to a symbol (plus a constant).  */
229
230 fixS *
231 fix_new (fragS *frag,           /* Which frag?  */
232          int where,                     /* Where in that frag?  */
233          int size,                      /* 1, 2, or 4 usually.  */
234          symbolS *add_symbol,   /* X_add_symbol.  */
235          offsetT offset,                /* X_add_number.  */
236          int pcrel,                     /* TRUE if PC-relative relocation.  */
237          RELOC_ENUM r_type              /* Relocation type.  */)
238 {
239   return fix_new_internal (frag, where, size, add_symbol,
240                            (symbolS *) NULL, offset, pcrel, r_type);
241 }
242
243 /* Create a fixup for an expression.  Currently we only support fixups
244    for difference expressions.  That is itself more than most object
245    file formats support anyhow.  */
246
247 fixS *
248 fix_new_exp (fragS *frag,               /* Which frag?  */
249              int where,                 /* Where in that frag?  */
250              int size,                  /* 1, 2, or 4 usually.  */
251              expressionS *exp,          /* Expression.  */
252              int pcrel,                 /* TRUE if PC-relative relocation.  */
253              RELOC_ENUM r_type          /* Relocation type.  */)
254 {
255   symbolS *add = NULL;
256   symbolS *sub = NULL;
257   offsetT off = 0;
258
259   switch (exp->X_op)
260     {
261     case O_absent:
262       break;
263
264     case O_register:
265       as_bad (_("register value used as expression"));
266       break;
267
268     case O_add:
269       /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
270          the difference expression cannot immediately be reduced.  */
271       {
272         symbolS *stmp = make_expr_symbol (exp);
273
274         exp->X_op = O_symbol;
275         exp->X_op_symbol = 0;
276         exp->X_add_symbol = stmp;
277         exp->X_add_number = 0;
278
279         return fix_new_exp (frag, where, size, exp, pcrel, r_type);
280       }
281
282     case O_symbol_rva:
283       add = exp->X_add_symbol;
284       off = exp->X_add_number;
285       r_type = BFD_RELOC_RVA;
286       break;
287
288     case O_uminus:
289       sub = exp->X_add_symbol;
290       off = exp->X_add_number;
291       break;
292
293     case O_subtract:
294       sub = exp->X_op_symbol;
295       /* Fall through.  */
296     case O_symbol:
297       add = exp->X_add_symbol;
298       /* Fall through.  */
299     case O_constant:
300       off = exp->X_add_number;
301       break;
302
303     default:
304       add = make_expr_symbol (exp);
305       break;
306     }
307
308   return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
309 }
310
311 /* Generic function to determine whether a fixup requires a relocation.  */
312 int
313 generic_force_reloc (fixS *fix)
314 {
315   if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
316       || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
317     return 1;
318
319   if (fix->fx_addsy == NULL)
320     return 0;
321
322   return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
323 }
324
325 /* Append a string onto another string, bumping the pointer along.  */
326 void
327 append (char **charPP, char *fromP, unsigned long length)
328 {
329   /* Don't trust memcpy() of 0 chars.  */
330   if (length == 0)
331     return;
332
333   memcpy (*charPP, fromP, length);
334   *charPP += length;
335 }
336
337 /* This routine records the largest alignment seen for each segment.
338    If the beginning of the segment is aligned on the worst-case
339    boundary, all of the other alignments within it will work.  At
340    least one object format really uses this info.  */
341
342 void
343 record_alignment (/* Segment to which alignment pertains.  */
344                   segT seg,
345                   /* Alignment, as a power of 2 (e.g., 1 => 2-byte
346                      boundary, 2 => 4-byte boundary, etc.)  */
347                   int align)
348 {
349   if (seg == absolute_section)
350     return;
351
352   if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
353     bfd_set_section_alignment (stdoutput, seg, align);
354 }
355
356 int
357 get_recorded_alignment (segT seg)
358 {
359   if (seg == absolute_section)
360     return 0;
361
362   return bfd_get_section_alignment (stdoutput, seg);
363 }
364
365 /* Reset the section indices after removing the gas created sections.  */
366
367 static void
368 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, PTR countparg)
369 {
370   int *countp = (int *) countparg;
371
372   sec->index = *countp;
373   ++*countp;
374 }
375
376 static fragS *
377 chain_frchains_together_1 (segT section, struct frchain *frchp)
378 {
379   fragS dummy, *prev_frag = &dummy;
380   fixS fix_dummy, *prev_fix = &fix_dummy;
381
382   for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
383     {
384       prev_frag->fr_next = frchp->frch_root;
385       prev_frag = frchp->frch_last;
386       assert (prev_frag->fr_type != 0);
387       if (frchp->fix_root != (fixS *) NULL)
388         {
389           if (seg_info (section)->fix_root == (fixS *) NULL)
390             seg_info (section)->fix_root = frchp->fix_root;
391           prev_fix->fx_next = frchp->fix_root;
392           seg_info (section)->fix_tail = frchp->fix_tail;
393           prev_fix = frchp->fix_tail;
394         }
395     }
396   assert (prev_frag->fr_type != 0);
397   prev_frag->fr_next = 0;
398   return prev_frag;
399 }
400
401 static void
402 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
403                          segT section,
404                          PTR xxx ATTRIBUTE_UNUSED)
405 {
406   segment_info_type *info;
407
408   /* BFD may have introduced its own sections without using
409      subseg_new, so it is possible that seg_info is NULL.  */
410   info = seg_info (section);
411   if (info != (segment_info_type *) NULL)
412     info->frchainP->frch_last
413       = chain_frchains_together_1 (section, info->frchainP);
414
415   /* Now that we've chained the frags together, we must add new fixups
416      to the segment, not to the frag chain.  */
417   frags_chained = 1;
418 }
419
420 static void
421 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
422 {
423   switch (fragP->fr_type)
424     {
425     case rs_align:
426     case rs_align_code:
427     case rs_align_test:
428     case rs_org:
429     case rs_space:
430 #ifdef HANDLE_ALIGN
431       HANDLE_ALIGN (fragP);
432 #endif
433       know (fragP->fr_next != NULL);
434       fragP->fr_offset = (fragP->fr_next->fr_address
435                           - fragP->fr_address
436                           - fragP->fr_fix) / fragP->fr_var;
437       if (fragP->fr_offset < 0)
438         {
439           as_bad_where (fragP->fr_file, fragP->fr_line,
440                         _("attempt to .org/.space backwards? (%ld)"),
441                         (long) fragP->fr_offset);
442           fragP->fr_offset = 0;
443         }
444       fragP->fr_type = rs_fill;
445       break;
446
447     case rs_fill:
448       break;
449
450     case rs_leb128:
451       {
452         valueT value = S_GET_VALUE (fragP->fr_symbol);
453         int size;
454
455         size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
456                               fragP->fr_subtype);
457
458         fragP->fr_fix += size;
459         fragP->fr_type = rs_fill;
460         fragP->fr_var = 0;
461         fragP->fr_offset = 0;
462         fragP->fr_symbol = NULL;
463       }
464       break;
465
466     case rs_cfa:
467       eh_frame_convert_frag (fragP);
468       break;
469
470     case rs_dwarf2dbg:
471       dwarf2dbg_convert_frag (fragP);
472       break;
473
474     case rs_machine_dependent:
475       md_convert_frag (stdoutput, sec, fragP);
476
477       assert (fragP->fr_next == NULL
478               || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
479                   == fragP->fr_fix));
480
481       /* After md_convert_frag, we make the frag into a ".space 0".
482          md_convert_frag() should set up any fixSs and constants
483          required.  */
484       frag_wane (fragP);
485       break;
486
487 #ifndef WORKING_DOT_WORD
488     case rs_broken_word:
489       {
490         struct broken_word *lie;
491
492         if (fragP->fr_subtype)
493           {
494             fragP->fr_fix += md_short_jump_size;
495             for (lie = (struct broken_word *) (fragP->fr_symbol);
496                  lie && lie->dispfrag == fragP;
497                  lie = lie->next_broken_word)
498               if (lie->added == 1)
499                 fragP->fr_fix += md_long_jump_size;
500           }
501         frag_wane (fragP);
502       }
503       break;
504 #endif
505
506     default:
507       BAD_CASE (fragP->fr_type);
508       break;
509     }
510 #ifdef md_frag_check
511   md_frag_check (fragP);
512 #endif
513 }
514
515 static void relax_seg (bfd *, asection *, PTR);
516
517 static void
518 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, PTR xxx)
519 {
520   segment_info_type *seginfo = seg_info (sec);
521
522   if (seginfo && seginfo->frchainP
523       && relax_segment (seginfo->frchainP->frch_root, sec))
524     {
525       int *result = (int *) xxx;
526       *result = 1;
527     }
528 }
529
530 static void size_seg (bfd *, asection *, PTR);
531
532 static void
533 size_seg (bfd *abfd, asection *sec, PTR xxx ATTRIBUTE_UNUSED)
534 {
535   flagword flags;
536   fragS *fragp;
537   segment_info_type *seginfo;
538   int x;
539   valueT size, newsize;
540
541   subseg_change (sec, 0);
542
543   seginfo = seg_info (sec);
544   if (seginfo && seginfo->frchainP)
545     {
546       for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
547         cvt_frag_to_fill (sec, fragp);
548       for (fragp = seginfo->frchainP->frch_root;
549            fragp->fr_next;
550            fragp = fragp->fr_next)
551         /* Walk to last elt.  */
552         ;
553       size = fragp->fr_address + fragp->fr_fix;
554     }
555   else
556     size = 0;
557
558   flags = bfd_get_section_flags (abfd, sec);
559
560   if (size > 0 && ! seginfo->bss)
561     flags |= SEC_HAS_CONTENTS;
562
563   /* @@ This is just an approximation.  */
564   if (seginfo && seginfo->fix_root)
565     flags |= SEC_RELOC;
566   else
567     flags &= ~SEC_RELOC;
568   x = bfd_set_section_flags (abfd, sec, flags);
569   assert (x);
570
571   newsize = md_section_align (sec, size);
572   x = bfd_set_section_size (abfd, sec, newsize);
573   assert (x);
574
575   /* If the size had to be rounded up, add some padding in the last
576      non-empty frag.  */
577   assert (newsize >= size);
578   if (size != newsize)
579     {
580       fragS *last = seginfo->frchainP->frch_last;
581       fragp = seginfo->frchainP->frch_root;
582       while (fragp->fr_next != last)
583         fragp = fragp->fr_next;
584       last->fr_address = size;
585       if ((newsize - size) % fragp->fr_var == 0)
586         fragp->fr_offset += (newsize - size) / fragp->fr_var;
587       else
588         /* If we hit this abort, it's likely due to subsegs_finish not
589            providing sufficient alignment on the last frag, and the
590            machine dependent code using alignment frags with fr_var
591            greater than 1.  */
592         abort ();
593     }
594
595 #ifdef tc_frob_section
596   tc_frob_section (sec);
597 #endif
598 #ifdef obj_frob_section
599   obj_frob_section (sec);
600 #endif
601 }
602
603 #ifdef DEBUG2
604 static void
605 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
606 {
607   segment_info_type *seginfo = seg_info (sec);
608   fixS *fixp = seginfo->fix_root;
609
610   if (!fixp)
611     return;
612
613   fprintf (stream, "sec %s relocs:\n", sec->name);
614   while (fixp)
615     {
616       symbolS *s = fixp->fx_addsy;
617
618       fprintf (stream, "  %08lx: type %d ", (unsigned long) fixp,
619                (int) fixp->fx_r_type);
620       if (s == NULL)
621         fprintf (stream, "no sym\n");
622       else
623         {
624           print_symbol_value_1 (stream, s);
625           fprintf (stream, "\n");
626         }
627       fixp = fixp->fx_next;
628     }
629 }
630 #else
631 #define dump_section_relocs(ABFD,SEC,STREAM)    ((void) 0)
632 #endif
633
634 #ifndef EMIT_SECTION_SYMBOLS
635 #define EMIT_SECTION_SYMBOLS 1
636 #endif
637
638 /* This pass over fixups decides whether symbols can be replaced with
639    section symbols.  */
640
641 static void
642 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
643                    asection *sec,
644                    PTR xxx ATTRIBUTE_UNUSED)
645 {
646   segment_info_type *seginfo = seg_info (sec);
647   fixS *fixp;
648
649   if (seginfo == NULL)
650     return;
651
652   dump_section_relocs (abfd, sec, stderr);
653
654   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
655     if (fixp->fx_done)
656       /* Ignore it.  */
657       ;
658     else if (fixp->fx_addsy)
659       {
660         symbolS *sym;
661         asection *symsec;
662
663 #ifdef DEBUG5
664         fprintf (stderr, "\n\nadjusting fixup:\n");
665         print_fixup (fixp);
666 #endif
667
668         sym = fixp->fx_addsy;
669
670         /* All symbols should have already been resolved at this
671            point.  It is possible to see unresolved expression
672            symbols, though, since they are not in the regular symbol
673            table.  */
674         resolve_symbol_value (sym);
675
676         if (fixp->fx_subsy != NULL)
677           resolve_symbol_value (fixp->fx_subsy);
678
679         /* If this symbol is equated to an undefined or common symbol,
680            convert the fixup to being against that symbol.  */
681         if (symbol_equated_reloc_p (sym)
682             || S_IS_WEAKREFR (sym))
683           {
684             symbolS *new_sym
685               = symbol_get_value_expression (sym)->X_add_symbol;
686             const char *name = S_GET_NAME (sym);
687             if (!S_IS_COMMON (new_sym)
688                 && !TC_FAKE_LABEL (name)
689                 && !S_IS_WEAKREFR (sym)
690                 && (!S_IS_EXTERNAL (sym) || S_IS_LOCAL (sym)))
691               as_bad (_("Local symbol `%s' can't be equated to undefined symbol `%s'"),
692                       name, S_GET_NAME (new_sym));
693             fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
694             sym = new_sym;
695             fixp->fx_addsy = sym;
696           }
697
698         if (symbol_mri_common_p (sym))
699           {
700             /* These symbols are handled specially in fixup_segment.  */
701             continue;
702           }
703
704         /* If the symbol is undefined, common, weak, or global (ELF
705            shared libs), we can't replace it with the section symbol.  */
706         if (S_FORCE_RELOC (fixp->fx_addsy, 1))
707           continue;
708
709         /* Is there some other (target cpu dependent) reason we can't adjust
710            this one?  (E.g. relocations involving function addresses on
711            the PA.  */
712 #ifdef tc_fix_adjustable
713         if (! tc_fix_adjustable (fixp))
714           continue;
715 #endif
716
717         /* Since we're reducing to section symbols, don't attempt to reduce
718            anything that's already using one.  */
719         if (symbol_section_p (sym))
720           continue;
721
722         symsec = S_GET_SEGMENT (sym);
723         if (symsec == NULL)
724           abort ();
725
726         if (bfd_is_abs_section (symsec))
727           {
728             /* The fixup_segment routine normally will not use this
729                symbol in a relocation.  */
730             continue;
731           }
732
733         /* Don't try to reduce relocs which refer to non-local symbols
734            in .linkonce sections.  It can lead to confusion when a
735            debugging section refers to a .linkonce section.  I hope
736            this will always be correct.  */
737         if (symsec != sec && ! S_IS_LOCAL (sym))
738           {
739             if ((symsec->flags & SEC_LINK_ONCE) != 0
740                 || (IS_ELF
741                     /* The GNU toolchain uses an extension for ELF: a
742                        section beginning with the magic string
743                        .gnu.linkonce is a linkonce section.  */
744                     && strncmp (segment_name (symsec), ".gnu.linkonce",
745                                 sizeof ".gnu.linkonce" - 1) == 0))
746               continue;
747           }
748
749         /* Never adjust a reloc against local symbol in a merge section
750            with non-zero addend.  */
751         if ((symsec->flags & SEC_MERGE) != 0
752             && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
753           continue;
754
755         /* Never adjust a reloc against TLS local symbol.  */
756         if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
757           continue;
758
759         /* We refetch the segment when calling section_symbol, rather
760            than using symsec, because S_GET_VALUE may wind up changing
761            the section when it calls resolve_symbol_value.  */
762         fixp->fx_offset += S_GET_VALUE (sym);
763         fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
764 #ifdef DEBUG5
765         fprintf (stderr, "\nadjusted fixup:\n");
766         print_fixup (fixp);
767 #endif
768       }
769
770   dump_section_relocs (abfd, sec, stderr);
771 }
772
773 static void
774 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
775              asection *sec,
776              PTR xxx ATTRIBUTE_UNUSED)
777 {
778   segment_info_type *seginfo = seg_info (sec);
779
780   fixup_segment (seginfo->fix_root, sec);
781 }
782
783 static void
784 write_relocs (bfd *abfd, asection *sec, PTR xxx ATTRIBUTE_UNUSED)
785 {
786   segment_info_type *seginfo = seg_info (sec);
787   unsigned int i;
788   unsigned int n;
789   arelent **relocs;
790   fixS *fixp;
791   char *err;
792
793   /* If seginfo is NULL, we did not create this section; don't do
794      anything with it.  */
795   if (seginfo == NULL)
796     return;
797
798   n = 0;
799   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
800     n++;
801
802 #ifndef RELOC_EXPANSION_POSSIBLE
803   /* Set up reloc information as well.  */
804   relocs = (arelent **) xcalloc (n, sizeof (arelent *));
805
806   i = 0;
807   for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
808     {
809       arelent *reloc;
810       bfd_reloc_status_type s;
811       symbolS *sym;
812
813       if (fixp->fx_done)
814         {
815           n--;
816           continue;
817         }
818
819       /* If this is an undefined symbol which was equated to another
820          symbol, then generate the reloc against the latter symbol
821          rather than the former.  */
822       sym = fixp->fx_addsy;
823       while (symbol_equated_reloc_p (sym))
824         {
825           symbolS *n;
826
827           /* We must avoid looping, as that can occur with a badly
828              written program.  */
829           n = symbol_get_value_expression (sym)->X_add_symbol;
830           if (n == sym)
831             break;
832           fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
833           sym = n;
834         }
835       fixp->fx_addsy = sym;
836
837       reloc = tc_gen_reloc (sec, fixp);
838       if (!reloc)
839         {
840           n--;
841           continue;
842         }
843
844       /*
845         This test is triggered inappropriately for the SH:
846          if (fixp->fx_where + fixp->fx_size
847              > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
848              abort ();
849       */
850
851       s = bfd_install_relocation (stdoutput, reloc,
852                                   fixp->fx_frag->fr_literal,
853                                   fixp->fx_frag->fr_address,
854                                   sec, &err);
855       switch (s)
856         {
857         case bfd_reloc_ok:
858           break;
859         case bfd_reloc_overflow:
860           as_bad_where (fixp->fx_file, fixp->fx_line,
861                         _("relocation overflow"));
862           break;
863         case bfd_reloc_outofrange:
864           as_bad_where (fixp->fx_file, fixp->fx_line,
865                         _("relocation out of range"));
866           break;
867         default:
868           as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
869                     fixp->fx_file, fixp->fx_line, s);
870         }
871       relocs[i++] = reloc;
872     }
873 #else
874   n = n * MAX_RELOC_EXPANSION;
875   /* Set up reloc information as well.  */
876   relocs = (arelent **) xcalloc (n, sizeof (arelent *));
877
878   i = 0;
879   for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
880     {
881       arelent **reloc;
882       bfd_reloc_status_type s;
883       symbolS *sym;
884       int j;
885
886       if (fixp->fx_done)
887         {
888           n--;
889           continue;
890         }
891
892       /* If this is an undefined symbol which was equated to another
893          symbol, then generate the reloc against the latter symbol
894          rather than the former.  */
895       sym = fixp->fx_addsy;
896       while (symbol_equated_reloc_p (sym))
897         {
898           symbolS *n;
899
900           /* We must avoid looping, as that can occur with a badly
901              written program.  */
902           n = symbol_get_value_expression (sym)->X_add_symbol;
903           if (n == sym)
904             break;
905           fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
906           sym = n;
907         }
908       fixp->fx_addsy = sym;
909
910       reloc = tc_gen_reloc (sec, fixp);
911
912       for (j = 0; reloc[j]; j++)
913         {
914           relocs[i++] = reloc[j];
915           assert (i <= n);
916         }
917       if (fixp->fx_where + fixp->fx_size
918           > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
919         as_bad_where (fixp->fx_file, fixp->fx_line,
920                       _("internal error: fixup not contained within frag"));
921       for (j = 0; reloc[j]; j++)
922         {
923           s = bfd_install_relocation (stdoutput, reloc[j],
924                                       fixp->fx_frag->fr_literal,
925                                       fixp->fx_frag->fr_address,
926                                       sec, &err);
927           switch (s)
928             {
929             case bfd_reloc_ok:
930               break;
931             case bfd_reloc_overflow:
932               as_bad_where (fixp->fx_file, fixp->fx_line,
933                             _("relocation overflow"));
934               break;
935             case bfd_reloc_outofrange:
936               as_bad_where (fixp->fx_file, fixp->fx_line,
937                             _("relocation out of range"));
938               break;
939             default:
940               as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
941                         fixp->fx_file, fixp->fx_line, s);
942             }
943         }
944     }
945   n = i;
946 #endif
947
948 #ifdef DEBUG4
949   {
950     unsigned int i, j, nsyms;
951     asymbol **sympp;
952     sympp = bfd_get_outsymbols (stdoutput);
953     nsyms = bfd_get_symcount (stdoutput);
954     for (i = 0; i < n; i++)
955       if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
956         {
957           for (j = 0; j < nsyms; j++)
958             if (sympp[j] == *relocs[i]->sym_ptr_ptr)
959               break;
960           if (j == nsyms)
961             abort ();
962         }
963   }
964 #endif
965
966   if (n)
967     bfd_set_reloc (stdoutput, sec, relocs, n);
968   else
969     bfd_set_section_flags (abfd, sec,
970                            (bfd_get_section_flags (abfd, sec)
971                             & (flagword) ~SEC_RELOC));
972
973 #ifdef SET_SECTION_RELOCS
974   SET_SECTION_RELOCS (sec, relocs, n);
975 #endif
976
977 #ifdef DEBUG3
978   {
979     unsigned int i;
980     arelent *r;
981     asymbol *s;
982     fprintf (stderr, "relocs for sec %s\n", sec->name);
983     for (i = 0; i < n; i++)
984       {
985         r = relocs[i];
986         s = *r->sym_ptr_ptr;
987         fprintf (stderr, "  reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
988                  i, r, (unsigned long)r->address, s->name, (unsigned long)r->addend);
989       }
990   }
991 #endif
992 }
993
994 static void
995 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
996                 asection *sec,
997                 PTR xxx ATTRIBUTE_UNUSED)
998 {
999   segment_info_type *seginfo = seg_info (sec);
1000   addressT offset = 0;
1001   fragS *f;
1002
1003   /* Write out the frags.  */
1004   if (seginfo == NULL
1005       || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1006     return;
1007
1008   for (f = seginfo->frchainP->frch_root;
1009        f;
1010        f = f->fr_next)
1011     {
1012       int x;
1013       addressT fill_size;
1014       char *fill_literal;
1015       offsetT count;
1016
1017       assert (f->fr_type == rs_fill);
1018       if (f->fr_fix)
1019         {
1020           x = bfd_set_section_contents (stdoutput, sec,
1021                                         f->fr_literal, (file_ptr) offset,
1022                                         (bfd_size_type) f->fr_fix);
1023           if (!x)
1024             {
1025               bfd_perror (stdoutput->filename);
1026               as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1027               exit (EXIT_FAILURE);
1028             }
1029           offset += f->fr_fix;
1030         }
1031       fill_literal = f->fr_literal + f->fr_fix;
1032       fill_size = f->fr_var;
1033       count = f->fr_offset;
1034       assert (count >= 0);
1035       if (fill_size && count)
1036         {
1037           char buf[256];
1038           if (fill_size > sizeof (buf))
1039             {
1040               /* Do it the old way. Can this ever happen?  */
1041               while (count--)
1042                 {
1043                   x = bfd_set_section_contents (stdoutput, sec,
1044                                                 fill_literal,
1045                                                 (file_ptr) offset,
1046                                                 (bfd_size_type) fill_size);
1047                   if (!x)
1048                     {
1049                       bfd_perror (stdoutput->filename);
1050                       as_perror (_("FATAL: Can't write %s"),
1051                                  stdoutput->filename);
1052                       exit (EXIT_FAILURE);
1053                     }
1054                   offset += fill_size;
1055                 }
1056             }
1057           else
1058             {
1059               /* Build a buffer full of fill objects and output it as
1060                  often as necessary. This saves on the overhead of
1061                  potentially lots of bfd_set_section_contents calls.  */
1062               int n_per_buf, i;
1063               if (fill_size == 1)
1064                 {
1065                   n_per_buf = sizeof (buf);
1066                   memset (buf, *fill_literal, n_per_buf);
1067                 }
1068               else
1069                 {
1070                   char *bufp;
1071                   n_per_buf = sizeof (buf) / fill_size;
1072                   for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1073                     memcpy (bufp, fill_literal, fill_size);
1074                 }
1075               for (; count > 0; count -= n_per_buf)
1076                 {
1077                   n_per_buf = n_per_buf > count ? count : n_per_buf;
1078                   x = bfd_set_section_contents
1079                     (stdoutput, sec, buf, (file_ptr) offset,
1080                      (bfd_size_type) n_per_buf * fill_size);
1081                   if (!x)
1082                     as_fatal (_("cannot write to output file"));
1083                   offset += n_per_buf * fill_size;
1084                 }
1085             }
1086         }
1087     }
1088 }
1089
1090 static void
1091 merge_data_into_text (void)
1092 {
1093   seg_info (text_section)->frchainP->frch_last->fr_next =
1094     seg_info (data_section)->frchainP->frch_root;
1095   seg_info (text_section)->frchainP->frch_last =
1096     seg_info (data_section)->frchainP->frch_last;
1097   seg_info (data_section)->frchainP = 0;
1098 }
1099
1100 static void
1101 set_symtab (void)
1102 {
1103   int nsyms;
1104   asymbol **asympp;
1105   symbolS *symp;
1106   bfd_boolean result;
1107   extern PTR bfd_alloc (bfd *, bfd_size_type);
1108
1109   /* Count symbols.  We can't rely on a count made by the loop in
1110      write_object_file, because *_frob_file may add a new symbol or
1111      two.  */
1112   nsyms = 0;
1113   for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1114     nsyms++;
1115
1116   if (nsyms)
1117     {
1118       int i;
1119       bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1120
1121       asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1122       symp = symbol_rootP;
1123       for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1124         {
1125           asympp[i] = symbol_get_bfdsym (symp);
1126           symbol_mark_written (symp);
1127         }
1128     }
1129   else
1130     asympp = 0;
1131   result = bfd_set_symtab (stdoutput, asympp, nsyms);
1132   assert (result);
1133   symbol_table_frozen = 1;
1134 }
1135
1136 /* Finish the subsegments.  After every sub-segment, we fake an
1137    ".align ...".  This conforms to BSD4.2 brane-damage.  We then fake
1138    ".fill 0" because that is the kind of frag that requires least
1139    thought.  ".align" frags like to have a following frag since that
1140    makes calculating their intended length trivial.  */
1141
1142 #ifndef SUB_SEGMENT_ALIGN
1143 #ifdef HANDLE_ALIGN
1144 /* The last subsegment gets an alignment corresponding to the alignment
1145    of the section.  This allows proper nop-filling at the end of
1146    code-bearing sections.  */
1147 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN)                                 \
1148   (!(FRCHAIN)->frch_next || (FRCHAIN)->frch_next->frch_seg != (SEG)     \
1149    ? get_recorded_alignment (SEG) : 0)
1150 #else
1151 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1152 #endif
1153 #endif
1154
1155 void
1156 subsegs_finish (void)
1157 {
1158   struct frchain *frchainP;
1159
1160   for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1161     {
1162       int alignment = 0;
1163
1164       subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1165
1166       /* This now gets called even if we had errors.  In that case,
1167          any alignment is meaningless, and, moreover, will look weird
1168          if we are generating a listing.  */
1169       if (!had_errors ())
1170         {
1171           alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1172           if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1173               && now_seg->entsize)
1174             {
1175               unsigned int entsize = now_seg->entsize;
1176               int entalign = 0;
1177
1178               while ((entsize & 1) == 0)
1179                 {
1180                   ++entalign;
1181                   entsize >>= 1;
1182                 }
1183               if (entalign > alignment)
1184                 alignment = entalign;
1185             }
1186         }
1187
1188       if (subseg_text_p (now_seg))
1189         frag_align_code (alignment, 0);
1190       else
1191         frag_align (alignment, 0, 0);
1192
1193       /* frag_align will have left a new frag.
1194          Use this last frag for an empty ".fill".
1195
1196          For this segment ...
1197          Create a last frag. Do not leave a "being filled in frag".  */
1198       frag_wane (frag_now);
1199       frag_now->fr_fix = 0;
1200       know (frag_now->fr_next == NULL);
1201     }
1202 }
1203
1204 /* Write the object file.  */
1205
1206 void
1207 write_object_file (void)
1208 {
1209 #ifndef WORKING_DOT_WORD
1210   fragS *fragP;                 /* Track along all frags.  */
1211 #endif
1212
1213   /* Do we really want to write it?  */
1214   {
1215     int n_warns, n_errs;
1216     n_warns = had_warnings ();
1217     n_errs = had_errors ();
1218     /* The -Z flag indicates that an object file should be generated,
1219        regardless of warnings and errors.  */
1220     if (flag_always_generate_output)
1221       {
1222         if (n_warns || n_errs)
1223           as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1224                    n_errs, n_errs == 1 ? "" : "s",
1225                    n_warns, n_warns == 1 ? "" : "s");
1226       }
1227     else
1228       {
1229         if (n_errs)
1230           as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1231                     n_errs, n_errs == 1 ? "" : "s",
1232                     n_warns, n_warns == 1 ? "" : "s");
1233       }
1234   }
1235
1236 #ifdef  OBJ_VMS
1237   /* Under VMS we try to be compatible with VAX-11 "C".  Thus, we call
1238      a routine to check for the definition of the procedure "_main",
1239      and if so -- fix it up so that it can be program entry point.  */
1240   vms_check_for_main ();
1241 #endif /* OBJ_VMS  */
1242
1243   /* From now on, we don't care about sub-segments.  Build one frag chain
1244      for each segment. Linked thru fr_next.  */
1245
1246   /* Remove the sections created by gas for its own purposes.  */
1247   {
1248     int i;
1249
1250     bfd_section_list_remove (stdoutput, reg_section);
1251     bfd_section_list_remove (stdoutput, expr_section);
1252     stdoutput->section_count -= 2;
1253     i = 0;
1254     bfd_map_over_sections (stdoutput, renumber_sections, &i);
1255   }
1256
1257   bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1258
1259   /* We have two segments. If user gave -R flag, then we must put the
1260      data frags into the text segment. Do this before relaxing so
1261      we know to take advantage of -R and make shorter addresses.  */
1262   if (flag_readonly_data_in_text)
1263     {
1264       merge_data_into_text ();
1265     }
1266
1267   while (1)
1268     {
1269       int changed;
1270
1271 #ifndef WORKING_DOT_WORD
1272       /* We need to reset the markers in the broken word list and
1273          associated frags between calls to relax_segment (via
1274          relax_seg).  Since the broken word list is global, we do it
1275          once per round, rather than locally in relax_segment for each
1276          segment.  */
1277       struct broken_word *brokp;
1278
1279       for (brokp = broken_words;
1280            brokp != (struct broken_word *) NULL;
1281            brokp = brokp->next_broken_word)
1282         {
1283           brokp->added = 0;
1284
1285           if (brokp->dispfrag != (fragS *) NULL
1286               && brokp->dispfrag->fr_type == rs_broken_word)
1287             brokp->dispfrag->fr_subtype = 0;
1288         }
1289 #endif
1290
1291       changed = 0;
1292       bfd_map_over_sections (stdoutput, relax_seg, &changed);
1293       if (!changed)
1294         break;
1295     }
1296
1297   /* Note - Most ports will use the default value of
1298      TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1.  This will force
1299      local symbols to be resolved, removing their frag information.
1300      Some ports however, will not have finished relaxing all of
1301      their frags and will still need the local symbol frag
1302      information.  These ports can set
1303      TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0.  */
1304   finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1305
1306   bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1307
1308   /* Relaxation has completed.  Freeze all syms.  */
1309   finalize_syms = 1;
1310
1311 #ifdef md_post_relax_hook
1312   md_post_relax_hook;
1313 #endif
1314
1315 #ifndef WORKING_DOT_WORD
1316   {
1317     struct broken_word *lie;
1318     struct broken_word **prevP;
1319
1320     prevP = &broken_words;
1321     for (lie = broken_words; lie; lie = lie->next_broken_word)
1322       if (!lie->added)
1323         {
1324           expressionS exp;
1325
1326           subseg_change (lie->seg, lie->subseg);
1327           exp.X_op = O_subtract;
1328           exp.X_add_symbol = lie->add;
1329           exp.X_op_symbol = lie->sub;
1330           exp.X_add_number = lie->addnum;
1331 #ifdef TC_CONS_FIX_NEW
1332           TC_CONS_FIX_NEW (lie->frag,
1333                            lie->word_goes_here - lie->frag->fr_literal,
1334                            2, &exp);
1335 #else
1336           fix_new_exp (lie->frag,
1337                        lie->word_goes_here - lie->frag->fr_literal,
1338                        2, &exp, 0, BFD_RELOC_16);
1339 #endif
1340           *prevP = lie->next_broken_word;
1341         }
1342       else
1343         prevP = &(lie->next_broken_word);
1344
1345     for (lie = broken_words; lie;)
1346       {
1347         struct broken_word *untruth;
1348         char *table_ptr;
1349         addressT table_addr;
1350         addressT from_addr, to_addr;
1351         int n, m;
1352
1353         subseg_change (lie->seg, lie->subseg);
1354         fragP = lie->dispfrag;
1355
1356         /* Find out how many broken_words go here.  */
1357         n = 0;
1358         for (untruth = lie;
1359              untruth && untruth->dispfrag == fragP;
1360              untruth = untruth->next_broken_word)
1361           if (untruth->added == 1)
1362             n++;
1363
1364         table_ptr = lie->dispfrag->fr_opcode;
1365         table_addr = (lie->dispfrag->fr_address
1366                       + (table_ptr - lie->dispfrag->fr_literal));
1367         /* Create the jump around the long jumps.  This is a short
1368            jump from table_ptr+0 to table_ptr+n*long_jump_size.  */
1369         from_addr = table_addr;
1370         to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1371         md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1372                               lie->add);
1373         table_ptr += md_short_jump_size;
1374         table_addr += md_short_jump_size;
1375
1376         for (m = 0;
1377              lie && lie->dispfrag == fragP;
1378              m++, lie = lie->next_broken_word)
1379           {
1380             if (lie->added == 2)
1381               continue;
1382             /* Patch the jump table.  */
1383             /* This is the offset from ??? to table_ptr+0.  */
1384             to_addr = table_addr - S_GET_VALUE (lie->sub);
1385 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1386             TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1387 #endif
1388             md_number_to_chars (lie->word_goes_here, to_addr, 2);
1389             for (untruth = lie->next_broken_word;
1390                  untruth && untruth->dispfrag == fragP;
1391                  untruth = untruth->next_broken_word)
1392               {
1393                 if (untruth->use_jump == lie)
1394                   md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1395               }
1396
1397             /* Install the long jump.  */
1398             /* This is a long jump from table_ptr+0 to the final target.  */
1399             from_addr = table_addr;
1400             to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1401             md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1402                                  lie->add);
1403             table_ptr += md_long_jump_size;
1404             table_addr += md_long_jump_size;
1405           }
1406       }
1407   }
1408 #endif /* not WORKING_DOT_WORD  */
1409
1410   /* Resolve symbol values.  This needs to be done before processing
1411      the relocations.  */
1412   if (symbol_rootP)
1413     {
1414       symbolS *symp;
1415
1416       for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1417         resolve_symbol_value (symp);
1418     }
1419   resolve_local_symbol_values ();
1420
1421   PROGRESS (1);
1422
1423 #ifdef tc_frob_file_before_adjust
1424   tc_frob_file_before_adjust ();
1425 #endif
1426 #ifdef obj_frob_file_before_adjust
1427   obj_frob_file_before_adjust ();
1428 #endif
1429
1430   bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1431
1432 #ifdef tc_frob_file_before_fix
1433   tc_frob_file_before_fix ();
1434 #endif
1435 #ifdef obj_frob_file_before_fix
1436   obj_frob_file_before_fix ();
1437 #endif
1438
1439   bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1440
1441   /* Set up symbol table, and write it out.  */
1442   if (symbol_rootP)
1443     {
1444       symbolS *symp;
1445       bfd_boolean skip_next_symbol = FALSE;
1446
1447       for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1448         {
1449           int punt = 0;
1450           const char *name;
1451
1452           if (skip_next_symbol)
1453             {
1454               /* Don't do anything besides moving the value of the
1455                  symbol from the GAS value-field to the BFD value-field.  */
1456               symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1457               skip_next_symbol = FALSE;
1458               continue;
1459             }
1460
1461           if (symbol_mri_common_p (symp))
1462             {
1463               if (S_IS_EXTERNAL (symp))
1464                 as_bad (_("%s: global symbols not supported in common sections"),
1465                         S_GET_NAME (symp));
1466               symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1467               continue;
1468             }
1469
1470           name = S_GET_NAME (symp);
1471           if (name)
1472             {
1473               const char *name2 =
1474                 decode_local_label_name ((char *) S_GET_NAME (symp));
1475               /* They only differ if `name' is a fb or dollar local
1476                  label name.  */
1477               if (name2 != name && ! S_IS_DEFINED (symp))
1478                 as_bad (_("local label `%s' is not defined"), name2);
1479             }
1480
1481           /* Do it again, because adjust_reloc_syms might introduce
1482              more symbols.  They'll probably only be section symbols,
1483              but they'll still need to have the values computed.  */
1484           resolve_symbol_value (symp);
1485
1486           /* Skip symbols which were equated to undefined or common
1487              symbols.  */
1488           if (symbol_equated_reloc_p (symp)
1489               || S_IS_WEAKREFR (symp))
1490             {
1491               const char *name = S_GET_NAME (symp);
1492               if (S_IS_COMMON (symp)
1493                   && !TC_FAKE_LABEL (name)
1494                   && !S_IS_WEAKREFR (symp)
1495                   && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
1496                 {
1497                   expressionS *e = symbol_get_value_expression (symp);
1498                   as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
1499                           name, S_GET_NAME (e->X_add_symbol));
1500                 }
1501               symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1502               continue;
1503             }
1504
1505           /* So far, common symbols have been treated like undefined symbols.
1506              Put them in the common section now.  */
1507           if (S_IS_DEFINED (symp) == 0
1508               && S_GET_VALUE (symp) != 0)
1509             S_SET_SEGMENT (symp, bfd_com_section_ptr);
1510
1511 #ifdef obj_frob_symbol
1512           obj_frob_symbol (symp, punt);
1513 #endif
1514 #ifdef tc_frob_symbol
1515           if (! punt || symbol_used_in_reloc_p (symp))
1516             tc_frob_symbol (symp, punt);
1517 #endif
1518
1519           /* If we don't want to keep this symbol, splice it out of
1520              the chain now.  If EMIT_SECTION_SYMBOLS is 0, we never
1521              want section symbols.  Otherwise, we skip local symbols
1522              and symbols that the frob_symbol macros told us to punt,
1523              but we keep such symbols if they are used in relocs.  */
1524           if (symp == abs_section_sym
1525               || (! EMIT_SECTION_SYMBOLS
1526                   && symbol_section_p (symp))
1527               /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
1528                  opposites.  Sometimes the former checks flags and the
1529                  latter examines the name...  */
1530               || (!S_IS_EXTERNAL (symp)
1531                   && (punt || S_IS_LOCAL (symp) || S_IS_WEAKREFD (symp))
1532                   && ! symbol_used_in_reloc_p (symp)))
1533             {
1534               symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1535
1536               /* After symbol_remove, symbol_next(symp) still returns
1537                  the one that came after it in the chain.  So we don't
1538                  need to do any extra cleanup work here.  */
1539               continue;
1540             }
1541
1542           /* Make sure we really got a value for the symbol.  */
1543           if (! symbol_resolved_p (symp))
1544             {
1545               as_bad (_("can't resolve value for symbol `%s'"),
1546                       S_GET_NAME (symp));
1547               symbol_mark_resolved (symp);
1548             }
1549
1550           /* Set the value into the BFD symbol.  Up til now the value
1551              has only been kept in the gas symbolS struct.  */
1552           symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1553
1554           /* A warning construct is a warning symbol followed by the
1555              symbol warned about.  Don't let anything object-format or
1556              target-specific muck with it; it's ready for output.  */
1557           if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
1558             skip_next_symbol = TRUE;
1559         }
1560     }
1561
1562   PROGRESS (1);
1563
1564   /* Now do any format-specific adjustments to the symbol table, such
1565      as adding file symbols.  */
1566 #ifdef tc_adjust_symtab
1567   tc_adjust_symtab ();
1568 #endif
1569 #ifdef obj_adjust_symtab
1570   obj_adjust_symtab ();
1571 #endif
1572
1573   /* Now that all the sizes are known, and contents correct, we can
1574      start writing to the file.  */
1575   set_symtab ();
1576
1577   /* If *_frob_file changes the symbol value at this point, it is
1578      responsible for moving the changed value into symp->bsym->value
1579      as well.  Hopefully all symbol value changing can be done in
1580      *_frob_symbol.  */
1581 #ifdef tc_frob_file
1582   tc_frob_file ();
1583 #endif
1584 #ifdef obj_frob_file
1585   obj_frob_file ();
1586 #endif
1587
1588   bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1589
1590 #ifdef tc_frob_file_after_relocs
1591   tc_frob_file_after_relocs ();
1592 #endif
1593 #ifdef obj_frob_file_after_relocs
1594   obj_frob_file_after_relocs ();
1595 #endif
1596
1597   bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1598 }
1599
1600 #ifdef TC_GENERIC_RELAX_TABLE
1601 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE.  */
1602
1603 long
1604 relax_frag (segT segment, fragS *fragP, long stretch)
1605 {
1606   const relax_typeS *this_type;
1607   const relax_typeS *start_type;
1608   relax_substateT next_state;
1609   relax_substateT this_state;
1610   offsetT growth;
1611   offsetT aim;
1612   addressT target;
1613   addressT address;
1614   symbolS *symbolP;
1615   const relax_typeS *table;
1616
1617   target = fragP->fr_offset;
1618   address = fragP->fr_address;
1619   table = TC_GENERIC_RELAX_TABLE;
1620   this_state = fragP->fr_subtype;
1621   start_type = this_type = table + this_state;
1622   symbolP = fragP->fr_symbol;
1623
1624   if (symbolP)
1625     {
1626       fragS *sym_frag;
1627
1628       sym_frag = symbol_get_frag (symbolP);
1629
1630 #ifndef DIFF_EXPR_OK
1631       know (sym_frag != NULL);
1632 #endif
1633       know (S_GET_SEGMENT (symbolP) != absolute_section
1634             || sym_frag == &zero_address_frag);
1635       target += S_GET_VALUE (symbolP);
1636
1637       /* If frag has yet to be reached on this pass,
1638          assume it will move by STRETCH just as we did.
1639          If this is not so, it will be because some frag
1640          between grows, and that will force another pass.  */
1641
1642       if (stretch != 0
1643           && sym_frag->relax_marker != fragP->relax_marker
1644           && S_GET_SEGMENT (symbolP) == segment)
1645         {
1646           target += stretch;
1647         }
1648     }
1649
1650   aim = target - address - fragP->fr_fix;
1651 #ifdef TC_PCREL_ADJUST
1652   /* Currently only the ns32k family needs this.  */
1653   aim += TC_PCREL_ADJUST (fragP);
1654 #endif
1655
1656 #ifdef md_prepare_relax_scan
1657   /* Formerly called M68K_AIM_KLUDGE.  */
1658   md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
1659 #endif
1660
1661   if (aim < 0)
1662     {
1663       /* Look backwards.  */
1664       for (next_state = this_type->rlx_more; next_state;)
1665         if (aim >= this_type->rlx_backward)
1666           next_state = 0;
1667         else
1668           {
1669             /* Grow to next state.  */
1670             this_state = next_state;
1671             this_type = table + this_state;
1672             next_state = this_type->rlx_more;
1673           }
1674     }
1675   else
1676     {
1677       /* Look forwards.  */
1678       for (next_state = this_type->rlx_more; next_state;)
1679         if (aim <= this_type->rlx_forward)
1680           next_state = 0;
1681         else
1682           {
1683             /* Grow to next state.  */
1684             this_state = next_state;
1685             this_type = table + this_state;
1686             next_state = this_type->rlx_more;
1687           }
1688     }
1689
1690   growth = this_type->rlx_length - start_type->rlx_length;
1691   if (growth != 0)
1692     fragP->fr_subtype = this_state;
1693   return growth;
1694 }
1695
1696 #endif /* defined (TC_GENERIC_RELAX_TABLE)  */
1697
1698 /* Relax_align. Advance location counter to next address that has 'alignment'
1699    lowest order bits all 0s, return size of adjustment made.  */
1700 static relax_addressT
1701 relax_align (register relax_addressT address,   /* Address now.  */
1702              register int alignment     /* Alignment (binary).  */)
1703 {
1704   relax_addressT mask;
1705   relax_addressT new_address;
1706
1707   mask = ~((~0) << alignment);
1708   new_address = (address + mask) & (~mask);
1709 #ifdef LINKER_RELAXING_SHRINKS_ONLY
1710   if (linkrelax)
1711     /* We must provide lots of padding, so the linker can discard it
1712        when needed.  The linker will not add extra space, ever.  */
1713     new_address += (1 << alignment);
1714 #endif
1715   return (new_address - address);
1716 }
1717
1718 /* Now we have a segment, not a crowd of sub-segments, we can make
1719    fr_address values.
1720
1721    Relax the frags.
1722
1723    After this, all frags in this segment have addresses that are correct
1724    within the segment. Since segments live in different file addresses,
1725    these frag addresses may not be the same as final object-file
1726    addresses.  */
1727
1728 int
1729 relax_segment (struct frag *segment_frag_root, segT segment)
1730 {
1731   unsigned long frag_count;
1732   struct frag *fragP;
1733   relax_addressT address;
1734   int ret;
1735
1736   /* In case md_estimate_size_before_relax() wants to make fixSs.  */
1737   subseg_change (segment, 0);
1738
1739   /* For each frag in segment: count and store  (a 1st guess of)
1740      fr_address.  */
1741   address = 0;
1742   for (frag_count = 0, fragP = segment_frag_root;
1743        fragP;
1744        fragP = fragP->fr_next, frag_count ++)
1745     {
1746       fragP->relax_marker = 0;
1747       fragP->fr_address = address;
1748       address += fragP->fr_fix;
1749
1750       switch (fragP->fr_type)
1751         {
1752         case rs_fill:
1753           address += fragP->fr_offset * fragP->fr_var;
1754           break;
1755
1756         case rs_align:
1757         case rs_align_code:
1758         case rs_align_test:
1759           {
1760             addressT offset = relax_align (address, (int) fragP->fr_offset);
1761
1762             if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
1763               offset = 0;
1764
1765             if (offset % fragP->fr_var != 0)
1766               {
1767                 as_bad_where (fragP->fr_file, fragP->fr_line,
1768                               _("alignment padding (%lu bytes) not a multiple of %ld"),
1769                               (unsigned long) offset, (long) fragP->fr_var);
1770                 offset -= (offset % fragP->fr_var);
1771               }
1772
1773             address += offset;
1774           }
1775           break;
1776
1777         case rs_org:
1778         case rs_space:
1779           /* Assume .org is nugatory. It will grow with 1st relax.  */
1780           break;
1781
1782         case rs_machine_dependent:
1783           /* If fr_symbol is an expression, this call to
1784              resolve_symbol_value sets up the correct segment, which will
1785              likely be needed in md_estimate_size_before_relax.  */
1786           if (fragP->fr_symbol)
1787             resolve_symbol_value (fragP->fr_symbol);
1788
1789           address += md_estimate_size_before_relax (fragP, segment);
1790           break;
1791
1792 #ifndef WORKING_DOT_WORD
1793           /* Broken words don't concern us yet.  */
1794         case rs_broken_word:
1795           break;
1796 #endif
1797
1798         case rs_leb128:
1799           /* Initial guess is always 1; doing otherwise can result in
1800              stable solutions that are larger than the minimum.  */
1801           address += fragP->fr_offset = 1;
1802           break;
1803
1804         case rs_cfa:
1805           address += eh_frame_estimate_size_before_relax (fragP);
1806           break;
1807
1808         case rs_dwarf2dbg:
1809           address += dwarf2dbg_estimate_size_before_relax (fragP);
1810           break;
1811
1812         default:
1813           BAD_CASE (fragP->fr_type);
1814           break;
1815         }
1816     }
1817
1818   /* Do relax().  */
1819   {
1820     unsigned long max_iterations;
1821     offsetT stretch;    /* May be any size, 0 or negative.  */
1822     /* Cumulative number of addresses we have relaxed this pass.
1823        We may have relaxed more than one address.  */
1824     int stretched;      /* Have we stretched on this pass?  */
1825     /* This is 'cuz stretch may be zero, when, in fact some piece of code
1826        grew, and another shrank.  If a branch instruction doesn't fit anymore,
1827        we could be scrod.  */
1828
1829     /* We want to prevent going into an infinite loop where one frag grows
1830        depending upon the location of a symbol which is in turn moved by
1831        the growing frag.  eg:
1832
1833          foo = .
1834          .org foo+16
1835          foo = .
1836
1837         So we dictate that this algorithm can be at most O2.  */
1838     max_iterations = frag_count * frag_count;
1839     /* Check for overflow.  */
1840     if (max_iterations < frag_count)
1841       max_iterations = frag_count;
1842
1843     do
1844       {
1845         stretch = 0;
1846         stretched = 0;
1847
1848         for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1849           {
1850             offsetT growth = 0;
1851             addressT was_address;
1852             offsetT offset;
1853             symbolS *symbolP;
1854
1855             fragP->relax_marker ^= 1;
1856             was_address = fragP->fr_address;
1857             address = fragP->fr_address += stretch;
1858             symbolP = fragP->fr_symbol;
1859             offset = fragP->fr_offset;
1860
1861             switch (fragP->fr_type)
1862               {
1863               case rs_fill:     /* .fill never relaxes.  */
1864                 growth = 0;
1865                 break;
1866
1867 #ifndef WORKING_DOT_WORD
1868                 /* JF:  This is RMS's idea.  I do *NOT* want to be blamed
1869                    for it I do not want to write it.  I do not want to have
1870                    anything to do with it.  This is not the proper way to
1871                    implement this misfeature.  */
1872               case rs_broken_word:
1873                 {
1874                   struct broken_word *lie;
1875                   struct broken_word *untruth;
1876
1877                   /* Yes this is ugly (storing the broken_word pointer
1878                      in the symbol slot).  Still, this whole chunk of
1879                      code is ugly, and I don't feel like doing anything
1880                      about it.  Think of it as stubbornness in action.  */
1881                   growth = 0;
1882                   for (lie = (struct broken_word *) (fragP->fr_symbol);
1883                        lie && lie->dispfrag == fragP;
1884                        lie = lie->next_broken_word)
1885                     {
1886
1887                       if (lie->added)
1888                         continue;
1889
1890                       offset = (S_GET_VALUE (lie->add)
1891                                 + lie->addnum
1892                                 - S_GET_VALUE (lie->sub));
1893                       if (offset <= -32768 || offset >= 32767)
1894                         {
1895                           if (flag_warn_displacement)
1896                             {
1897                               char buf[50];
1898                               sprint_value (buf, (addressT) lie->addnum);
1899                               as_warn_where (fragP->fr_file, fragP->fr_line,
1900                                              _(".word %s-%s+%s didn't fit"),
1901                                              S_GET_NAME (lie->add),
1902                                              S_GET_NAME (lie->sub),
1903                                              buf);
1904                             }
1905                           lie->added = 1;
1906                           if (fragP->fr_subtype == 0)
1907                             {
1908                               fragP->fr_subtype++;
1909                               growth += md_short_jump_size;
1910                             }
1911                           for (untruth = lie->next_broken_word;
1912                                untruth && untruth->dispfrag == lie->dispfrag;
1913                                untruth = untruth->next_broken_word)
1914                             if ((symbol_get_frag (untruth->add)
1915                                  == symbol_get_frag (lie->add))
1916                                 && (S_GET_VALUE (untruth->add)
1917                                     == S_GET_VALUE (lie->add)))
1918                               {
1919                                 untruth->added = 2;
1920                                 untruth->use_jump = lie;
1921                               }
1922                           growth += md_long_jump_size;
1923                         }
1924                     }
1925
1926                   break;
1927                 }               /* case rs_broken_word  */
1928 #endif
1929               case rs_align:
1930               case rs_align_code:
1931               case rs_align_test:
1932                 {
1933                   addressT oldoff, newoff;
1934
1935                   oldoff = relax_align (was_address + fragP->fr_fix,
1936                                         (int) offset);
1937                   newoff = relax_align (address + fragP->fr_fix,
1938                                         (int) offset);
1939
1940                   if (fragP->fr_subtype != 0)
1941                     {
1942                       if (oldoff > fragP->fr_subtype)
1943                         oldoff = 0;
1944                       if (newoff > fragP->fr_subtype)
1945                         newoff = 0;
1946                     }
1947
1948                   growth = newoff - oldoff;
1949                 }
1950                 break;
1951
1952               case rs_org:
1953                 {
1954                   addressT target = offset;
1955                   addressT after;
1956
1957                   if (symbolP)
1958                     {
1959                       /* Convert from an actual address to an octet offset
1960                          into the section.  Here it is assumed that the
1961                          section's VMA is zero, and can omit subtracting it
1962                          from the symbol's value to get the address offset.  */
1963                       know (S_GET_SEGMENT (symbolP)->vma == 0);
1964                       target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
1965                     }
1966
1967                   know (fragP->fr_next);
1968                   after = fragP->fr_next->fr_address;
1969                   growth = target - after;
1970                   if (growth < 0)
1971                     {
1972                       /* Growth may be negative, but variable part of frag
1973                          cannot have fewer than 0 chars.  That is, we can't
1974                          .org backwards.  */
1975                       as_bad_where (fragP->fr_file, fragP->fr_line,
1976                                     _("attempt to move .org backwards"));
1977
1978                       /* We've issued an error message.  Change the
1979                          frag to avoid cascading errors.  */
1980                       fragP->fr_type = rs_align;
1981                       fragP->fr_subtype = 0;
1982                       fragP->fr_offset = 0;
1983                       fragP->fr_fix = after - was_address;
1984                       growth = stretch;
1985                     }
1986
1987                   /* This is an absolute growth factor  */
1988                   growth -= stretch;
1989                   break;
1990                 }
1991
1992               case rs_space:
1993                 growth = 0;
1994                 if (symbolP)
1995                   {
1996                     offsetT amount;
1997
1998                     amount = S_GET_VALUE (symbolP);
1999                     if (S_GET_SEGMENT (symbolP) != absolute_section
2000                         || S_IS_COMMON (symbolP)
2001                         || ! S_IS_DEFINED (symbolP))
2002                       {
2003                         as_bad_where (fragP->fr_file, fragP->fr_line,
2004                                       _(".space specifies non-absolute value"));
2005                         /* Prevent repeat of this error message.  */
2006                         fragP->fr_symbol = 0;
2007                       }
2008                     else if (amount < 0)
2009                       {
2010                         as_warn_where (fragP->fr_file, fragP->fr_line,
2011                                        _(".space or .fill with negative value, ignored"));
2012                         fragP->fr_symbol = 0;
2013                       }
2014                     else
2015                       growth = (was_address + fragP->fr_fix + amount
2016                                 - fragP->fr_next->fr_address);
2017                   }
2018                 break;
2019
2020               case rs_machine_dependent:
2021 #ifdef md_relax_frag
2022                 growth = md_relax_frag (segment, fragP, stretch);
2023 #else
2024 #ifdef TC_GENERIC_RELAX_TABLE
2025                 /* The default way to relax a frag is to look through
2026                    TC_GENERIC_RELAX_TABLE.  */
2027                 growth = relax_frag (segment, fragP, stretch);
2028 #endif /* TC_GENERIC_RELAX_TABLE  */
2029 #endif
2030                 break;
2031
2032               case rs_leb128:
2033                 {
2034                   valueT value;
2035                   offsetT size;
2036
2037                   value = resolve_symbol_value (fragP->fr_symbol);
2038                   size = sizeof_leb128 (value, fragP->fr_subtype);
2039                   growth = size - fragP->fr_offset;
2040                   fragP->fr_offset = size;
2041                 }
2042                 break;
2043
2044               case rs_cfa:
2045                 growth = eh_frame_relax_frag (fragP);
2046                 break;
2047
2048               case rs_dwarf2dbg:
2049                 growth = dwarf2dbg_relax_frag (fragP);
2050                 break;
2051
2052               default:
2053                 BAD_CASE (fragP->fr_type);
2054                 break;
2055               }
2056             if (growth)
2057               {
2058                 stretch += growth;
2059                 stretched = 1;
2060               }
2061           }
2062       }
2063     /* Until nothing further to relax.  */
2064     while (stretched && -- max_iterations);
2065
2066     if (stretched)
2067       as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2068                 segment_name (segment));
2069   }
2070
2071   ret = 0;
2072   for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2073     if (fragP->last_fr_address != fragP->fr_address)
2074       {
2075         fragP->last_fr_address = fragP->fr_address;
2076         ret = 1;
2077       }
2078   return ret;
2079 }
2080
2081 /* fixup_segment()
2082
2083    Go through all the fixS's in a segment and see which ones can be
2084    handled now.  (These consist of fixS where we have since discovered
2085    the value of a symbol, or the address of the frag involved.)
2086    For each one, call md_apply_fix to put the fix into the frag data.
2087
2088    Result is a count of how many relocation structs will be needed to
2089    handle the remaining fixS's that we couldn't completely handle here.
2090    These will be output later by emit_relocations().  */
2091
2092 static long
2093 fixup_segment (fixS *fixP, segT this_segment)
2094 {
2095   long seg_reloc_count = 0;
2096   valueT add_number;
2097   fragS *fragP;
2098   segT add_symbol_segment = absolute_section;
2099
2100   if (fixP != NULL && abs_section_sym == NULL)
2101     abs_section_sym = section_symbol (absolute_section);
2102
2103   /* If the linker is doing the relaxing, we must not do any fixups.
2104
2105      Well, strictly speaking that's not true -- we could do any that
2106      are PC-relative and don't cross regions that could change size.
2107      And for the i960 we might be able to turn callx/callj into bal
2108      anyways in cases where we know the maximum displacement.  */
2109   if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
2110     {
2111       for (; fixP; fixP = fixP->fx_next)
2112         if (!fixP->fx_done)
2113           {
2114             if (fixP->fx_addsy == NULL)
2115               {
2116                 /* There was no symbol required by this relocation.
2117                    However, BFD doesn't really handle relocations
2118                    without symbols well. So fake up a local symbol in
2119                    the absolute section.  */
2120                 fixP->fx_addsy = abs_section_sym;
2121               }
2122             symbol_mark_used_in_reloc (fixP->fx_addsy);
2123             if (fixP->fx_subsy != NULL)
2124               symbol_mark_used_in_reloc (fixP->fx_subsy);
2125             seg_reloc_count++;
2126           }
2127       TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2128       return seg_reloc_count;
2129     }
2130
2131   for (; fixP; fixP = fixP->fx_next)
2132     {
2133 #ifdef DEBUG5
2134       fprintf (stderr, "\nprocessing fixup:\n");
2135       print_fixup (fixP);
2136 #endif
2137
2138       fragP = fixP->fx_frag;
2139       know (fragP);
2140 #ifdef TC_VALIDATE_FIX
2141       TC_VALIDATE_FIX (fixP, this_segment, skip);
2142 #endif
2143       add_number = fixP->fx_offset;
2144
2145       if (fixP->fx_addsy != NULL
2146           && symbol_mri_common_p (fixP->fx_addsy))
2147         {
2148           add_number += S_GET_VALUE (fixP->fx_addsy);
2149           fixP->fx_offset = add_number;
2150           fixP->fx_addsy
2151             = symbol_get_value_expression (fixP->fx_addsy)->X_add_symbol;
2152         }
2153
2154       if (fixP->fx_addsy != NULL)
2155         add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
2156
2157       if (fixP->fx_subsy != NULL)
2158         {
2159           segT sub_symbol_segment;
2160           resolve_symbol_value (fixP->fx_subsy);
2161           sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
2162           if (fixP->fx_addsy != NULL
2163               && sub_symbol_segment == add_symbol_segment
2164               && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
2165             {
2166               add_number += S_GET_VALUE (fixP->fx_addsy);
2167               add_number -= S_GET_VALUE (fixP->fx_subsy);
2168               fixP->fx_offset = add_number;
2169               fixP->fx_addsy = NULL;
2170               fixP->fx_subsy = NULL;
2171 #ifdef TC_M68K
2172               /* See the comment below about 68k weirdness.  */
2173               fixP->fx_pcrel = 0;
2174 #endif
2175             }
2176           else if (sub_symbol_segment == absolute_section
2177                    && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
2178             {
2179               add_number -= S_GET_VALUE (fixP->fx_subsy);
2180               fixP->fx_offset = add_number;
2181               fixP->fx_subsy = NULL;
2182             }
2183           else if (sub_symbol_segment == this_segment
2184                    && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
2185             {
2186               add_number -= S_GET_VALUE (fixP->fx_subsy);
2187               fixP->fx_offset = (add_number + fixP->fx_dot_value
2188                                  + fixP->fx_frag->fr_address);
2189
2190               /* Make it pc-relative.  If the back-end code has not
2191                  selected a pc-relative reloc, cancel the adjustment
2192                  we do later on all pc-relative relocs.  */
2193               if (0
2194 #ifdef TC_M68K
2195                   /* Do this for m68k even if it's already described
2196                      as pc-relative.  On the m68k, an operand of
2197                      "pc@(foo-.-2)" should address "foo" in a
2198                      pc-relative mode.  */
2199                   || 1
2200 #endif
2201                   || !fixP->fx_pcrel)
2202                 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
2203               fixP->fx_subsy = NULL;
2204               fixP->fx_pcrel = 1;
2205             }
2206           else if (!TC_VALIDATE_FIX_SUB (fixP))
2207             {
2208               as_bad_where (fixP->fx_file, fixP->fx_line,
2209                             _("can't resolve `%s' {%s section} - `%s' {%s section}"),
2210                             fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
2211                             segment_name (add_symbol_segment),
2212                             S_GET_NAME (fixP->fx_subsy),
2213                             segment_name (sub_symbol_segment));
2214             }
2215         }
2216
2217       if (fixP->fx_addsy)
2218         {
2219           if (add_symbol_segment == this_segment
2220               && !TC_FORCE_RELOCATION_LOCAL (fixP))
2221             {
2222               /* This fixup was made when the symbol's segment was
2223                  SEG_UNKNOWN, but it is now in the local segment.
2224                  So we know how to do the address without relocation.  */
2225               add_number += S_GET_VALUE (fixP->fx_addsy);
2226               fixP->fx_offset = add_number;
2227               if (fixP->fx_pcrel)
2228                 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2229               fixP->fx_addsy = NULL;
2230               fixP->fx_pcrel = 0;
2231             }
2232           else if (add_symbol_segment == absolute_section
2233                    && !TC_FORCE_RELOCATION_ABS (fixP))
2234             {
2235               add_number += S_GET_VALUE (fixP->fx_addsy);
2236               fixP->fx_offset = add_number;
2237               fixP->fx_addsy = NULL;
2238             }
2239           else if (add_symbol_segment != undefined_section
2240                    && ! bfd_is_com_section (add_symbol_segment)
2241                    && MD_APPLY_SYM_VALUE (fixP))
2242             add_number += S_GET_VALUE (fixP->fx_addsy);
2243         }
2244
2245       if (fixP->fx_pcrel)
2246         {
2247           add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2248           if (!fixP->fx_done && fixP->fx_addsy == NULL)
2249             {
2250               /* There was no symbol required by this relocation.
2251                  However, BFD doesn't really handle relocations
2252                  without symbols well. So fake up a local symbol in
2253                  the absolute section.  */
2254               fixP->fx_addsy = abs_section_sym;
2255             }
2256         }
2257
2258       if (!fixP->fx_done)
2259         md_apply_fix (fixP, &add_number, this_segment);
2260
2261       if (!fixP->fx_done)
2262         {
2263           ++seg_reloc_count;
2264           if (fixP->fx_addsy == NULL)
2265             fixP->fx_addsy = abs_section_sym;
2266           symbol_mark_used_in_reloc (fixP->fx_addsy);
2267           if (fixP->fx_subsy != NULL)
2268             symbol_mark_used_in_reloc (fixP->fx_subsy);
2269         }
2270
2271       if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
2272         {
2273           if (fixP->fx_size < sizeof (valueT))
2274             {
2275               valueT mask;
2276
2277               mask = 0;
2278               mask--;           /* Set all bits to one.  */
2279               mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
2280               if ((add_number & mask) != 0 && (add_number & mask) != mask)
2281                 {
2282                   char buf[50], buf2[50];
2283                   sprint_value (buf, fragP->fr_address + fixP->fx_where);
2284                   if (add_number > 1000)
2285                     sprint_value (buf2, add_number);
2286                   else
2287                     sprintf (buf2, "%ld", (long) add_number);
2288                   as_bad_where (fixP->fx_file, fixP->fx_line,
2289                                 _("value of %s too large for field of %d bytes at %s"),
2290                                 buf2, fixP->fx_size, buf);
2291                 } /* Generic error checking.  */
2292             }
2293 #ifdef WARN_SIGNED_OVERFLOW_WORD
2294           /* Warn if a .word value is too large when treated as a signed
2295              number.  We already know it is not too negative.  This is to
2296              catch over-large switches generated by gcc on the 68k.  */
2297           if (!flag_signed_overflow_ok
2298               && fixP->fx_size == 2
2299               && add_number > 0x7fff)
2300             as_bad_where (fixP->fx_file, fixP->fx_line,
2301                           _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2302                           (long) add_number,
2303                           (long) (fragP->fr_address + fixP->fx_where));
2304 #endif
2305         }                       /* Not a bit fix.  */
2306
2307 #ifdef TC_VALIDATE_FIX
2308     skip:  ATTRIBUTE_UNUSED_LABEL
2309       ;
2310 #endif
2311 #ifdef DEBUG5
2312       fprintf (stderr, "result:\n");
2313       print_fixup (fixP);
2314 #endif
2315     }                           /* For each fixS in this segment.  */
2316
2317   TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2318   return seg_reloc_count;
2319 }
2320
2321 void
2322 number_to_chars_bigendian (char *buf, valueT val, int n)
2323 {
2324   if (n <= 0)
2325     abort ();
2326   while (n--)
2327     {
2328       buf[n] = val & 0xff;
2329       val >>= 8;
2330     }
2331 }
2332
2333 void
2334 number_to_chars_littleendian (char *buf, valueT val, int n)
2335 {
2336   if (n <= 0)
2337     abort ();
2338   while (n--)
2339     {
2340       *buf++ = val & 0xff;
2341       val >>= 8;
2342     }
2343 }
2344
2345 void
2346 write_print_statistics (FILE *file)
2347 {
2348   fprintf (file, "fixups: %d\n", n_fixups);
2349 }
2350
2351 /* For debugging.  */
2352 extern int indent_level;
2353
2354 void
2355 print_fixup (fixS *fixp)
2356 {
2357   indent_level = 1;
2358   fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2359   if (fixp->fx_pcrel)
2360     fprintf (stderr, " pcrel");
2361   if (fixp->fx_pcrel_adjust)
2362     fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2363   if (fixp->fx_im_disp)
2364     {
2365 #ifdef TC_NS32K
2366       fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2367 #else
2368       fprintf (stderr, " im_disp");
2369 #endif
2370     }
2371   if (fixp->fx_tcbit)
2372     fprintf (stderr, " tcbit");
2373   if (fixp->fx_done)
2374     fprintf (stderr, " done");
2375   fprintf (stderr, "\n    size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2376            fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2377            (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2378   fprintf (stderr, "\n    %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2379            fixp->fx_r_type);
2380   if (fixp->fx_addsy)
2381     {
2382       fprintf (stderr, "\n   +<");
2383       print_symbol_value_1 (stderr, fixp->fx_addsy);
2384       fprintf (stderr, ">");
2385     }
2386   if (fixp->fx_subsy)
2387     {
2388       fprintf (stderr, "\n   -<");
2389       print_symbol_value_1 (stderr, fixp->fx_subsy);
2390       fprintf (stderr, ">");
2391     }
2392   fprintf (stderr, "\n");
2393 #ifdef TC_FIX_DATA_PRINT
2394   TC_FIX_DATA_PRINT (stderr, fixp);
2395 #endif
2396 }