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