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