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