]> Git Repo - binutils.git/blob - gas/write.c
bfd/
[binutils.git] / gas / write.c
1 /* write.c - emit .o file
2    Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3    1998, 1999, 2000, 2001
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 MD_PCREL_FROM_SECTION
56 #define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
57 #endif
58
59 #ifndef WORKING_DOT_WORD
60 extern CONST int md_short_jump_size;
61 extern CONST int md_long_jump_size;
62 #endif
63
64 /* Used to control final evaluation of expressions that are more
65    complex than symbol + constant.  1 means set final value for simple
66    expressions, 2 means set final value for more complex expressions.  */
67 int finalize_syms = 1;
68
69 int symbol_table_frozen;
70 void print_fixup PARAMS ((fixS *));
71
72 #ifdef BFD_ASSEMBLER
73 static void renumber_sections PARAMS ((bfd *, asection *, PTR));
74
75 /* We generally attach relocs to frag chains.  However, after we have
76    chained these all together into a segment, any relocs we add after
77    that must be attached to a segment.  This will include relocs added
78    in md_estimate_size_for_relax, for example.  */
79 static int frags_chained = 0;
80 #endif
81
82 #ifndef BFD_ASSEMBLER
83
84 #ifndef MANY_SEGMENTS
85 struct frag *text_frag_root;
86 struct frag *data_frag_root;
87 struct frag *bss_frag_root;
88
89 struct frag *text_last_frag;    /* Last frag in segment.  */
90 struct frag *data_last_frag;    /* Last frag in segment.  */
91 static struct frag *bss_last_frag;      /* Last frag in segment.  */
92 #endif
93
94 #ifndef BFD
95 static object_headers headers;
96 #endif
97
98 long string_byte_count;
99 char *next_object_file_charP;   /* Tracks object file bytes.  */
100
101 #ifndef OBJ_VMS
102 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
103 #endif
104
105 #endif /* BFD_ASSEMBLER  */
106
107 static int n_fixups;
108
109 #ifdef BFD_ASSEMBLER
110 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
111                                        symbolS *add, symbolS *sub,
112                                        offsetT offset, int pcrel,
113                                        bfd_reloc_code_real_type r_type));
114 #else
115 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
116                                        symbolS *add, symbolS *sub,
117                                        offsetT offset, int pcrel,
118                                        int r_type));
119 #endif
120 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
121 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
122 #endif
123 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
124 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
125 static fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
126 #endif
127 #ifdef BFD_ASSEMBLER
128 static void chain_frchains_together PARAMS ((bfd *, segT, PTR));
129 static void cvt_frag_to_fill PARAMS ((segT, fragS *));
130 static void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
131 static void write_relocs PARAMS ((bfd *, asection *, PTR));
132 static void write_contents PARAMS ((bfd *, asection *, PTR));
133 static void set_symtab PARAMS ((void));
134 #endif
135 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
136 static void merge_data_into_text PARAMS ((void));
137 #endif
138 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
139 static void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
140 static void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
141 static void relax_and_size_all_segments PARAMS ((void));
142 #endif
143 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
144 static void set_segment_vma PARAMS ((bfd *, asection *, PTR));
145 #endif
146
147 /* Create a fixS in obstack 'notes'.  */
148
149 static fixS *
150 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
151                   r_type)
152      fragS *frag;               /* Which frag?  */
153      int where;                 /* Where in that frag?  */
154      int size;                  /* 1, 2, or 4 usually.  */
155      symbolS *add_symbol;       /* X_add_symbol.  */
156      symbolS *sub_symbol;       /* X_op_symbol.  */
157      offsetT offset;            /* X_add_number.  */
158      int pcrel;                 /* TRUE if PC-relative relocation.  */
159 #ifdef BFD_ASSEMBLER
160      bfd_reloc_code_real_type r_type; /* Relocation type.  */
161 #else
162      int r_type;                /* Relocation type.  */
163 #endif
164 {
165   fixS *fixP;
166
167   n_fixups++;
168
169   fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
170
171   fixP->fx_frag = frag;
172   fixP->fx_where = where;
173   fixP->fx_size = size;
174   /* We've made fx_size a narrow field; check that it's wide enough.  */
175   if (fixP->fx_size != size)
176     {
177       as_bad (_("field fx_size too small to hold %d"), size);
178       abort ();
179     }
180   fixP->fx_addsy = add_symbol;
181   fixP->fx_subsy = sub_symbol;
182   fixP->fx_offset = offset;
183   fixP->fx_pcrel = pcrel;
184   fixP->fx_plt = 0;
185 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
186   fixP->fx_r_type = r_type;
187 #endif
188   fixP->fx_im_disp = 0;
189   fixP->fx_pcrel_adjust = 0;
190   fixP->fx_bit_fixP = 0;
191   fixP->fx_addnumber = 0;
192   fixP->fx_tcbit = 0;
193   fixP->fx_done = 0;
194   fixP->fx_no_overflow = 0;
195   fixP->fx_signed = 0;
196
197 #ifdef USING_CGEN
198   fixP->fx_cgen.insn = NULL;
199   fixP->fx_cgen.opinfo = 0;
200 #endif
201
202 #ifdef TC_FIX_TYPE
203   TC_INIT_FIX_DATA (fixP);
204 #endif
205
206   as_where (&fixP->fx_file, &fixP->fx_line);
207
208   /* Usually, we want relocs sorted numerically, but while
209      comparing to older versions of gas that have relocs
210      reverse sorted, it is convenient to have this compile
211      time option.  xoxorich.  */
212   {
213
214 #ifdef BFD_ASSEMBLER
215     fixS **seg_fix_rootP = (frags_chained
216                             ? &seg_info (now_seg)->fix_root
217                             : &frchain_now->fix_root);
218     fixS **seg_fix_tailP = (frags_chained
219                             ? &seg_info (now_seg)->fix_tail
220                             : &frchain_now->fix_tail);
221 #endif
222
223 #ifdef REVERSE_SORT_RELOCS
224
225     fixP->fx_next = *seg_fix_rootP;
226     *seg_fix_rootP = fixP;
227
228 #else /* REVERSE_SORT_RELOCS  */
229
230     fixP->fx_next = NULL;
231
232     if (*seg_fix_tailP)
233       (*seg_fix_tailP)->fx_next = fixP;
234     else
235       *seg_fix_rootP = fixP;
236     *seg_fix_tailP = fixP;
237
238 #endif /* REVERSE_SORT_RELOCS  */
239   }
240
241   return fixP;
242 }
243
244 /* Create a fixup relative to a symbol (plus a constant).  */
245
246 fixS *
247 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
248      fragS *frag;               /* Which frag?  */
249      int where;                 /* Where in that frag?  */
250      int size;                  /* 1, 2, or 4 usually.  */
251      symbolS *add_symbol;       /* X_add_symbol.  */
252      offsetT offset;            /* X_add_number.  */
253      int pcrel;                 /* TRUE if PC-relative relocation.  */
254 #ifdef BFD_ASSEMBLER
255      bfd_reloc_code_real_type r_type; /* Relocation type.  */
256 #else
257      int r_type;                /* Relocation type.  */
258 #endif
259 {
260   return fix_new_internal (frag, where, size, add_symbol,
261                            (symbolS *) NULL, offset, pcrel, r_type);
262 }
263
264 /* Create a fixup for an expression.  Currently we only support fixups
265    for difference expressions.  That is itself more than most object
266    file formats support anyhow.  */
267
268 fixS *
269 fix_new_exp (frag, where, size, exp, pcrel, r_type)
270      fragS *frag;               /* Which frag?  */
271      int where;                 /* Where in that frag?  */
272      int size;                  /* 1, 2, or 4 usually.  */
273      expressionS *exp;          /* Expression.  */
274      int pcrel;                 /* TRUE if PC-relative relocation.  */
275 #ifdef BFD_ASSEMBLER
276      bfd_reloc_code_real_type r_type; /* Relocation type.  */
277 #else
278      int r_type;                /* Relocation type.  */
279 #endif
280 {
281   symbolS *add = NULL;
282   symbolS *sub = NULL;
283   offsetT off = 0;
284
285   switch (exp->X_op)
286     {
287     case O_absent:
288       break;
289
290     case O_register:
291       as_bad (_("register value used as expression"));
292       break;
293
294     case O_add:
295       /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
296          the difference expression cannot immediately be reduced.  */
297       {
298         symbolS *stmp = make_expr_symbol (exp);
299
300         exp->X_op = O_symbol;
301         exp->X_op_symbol = 0;
302         exp->X_add_symbol = stmp;
303         exp->X_add_number = 0;
304
305         return fix_new_exp (frag, where, size, exp, pcrel, r_type);
306       }
307
308     case O_symbol_rva:
309       add = exp->X_add_symbol;
310       off = exp->X_add_number;
311
312 #if defined(BFD_ASSEMBLER)
313       r_type = BFD_RELOC_RVA;
314 #else
315 #if defined(TC_RVA_RELOC)
316       r_type = TC_RVA_RELOC;
317 #else
318       as_fatal (_("rva not supported"));
319 #endif
320 #endif
321       break;
322
323     case O_uminus:
324       sub = exp->X_add_symbol;
325       off = exp->X_add_number;
326       break;
327
328     case O_subtract:
329       sub = exp->X_op_symbol;
330       /* Fall through.  */
331     case O_symbol:
332       add = exp->X_add_symbol;
333       /* Fall through.  */
334     case O_constant:
335       off = exp->X_add_number;
336       break;
337
338     default:
339       add = make_expr_symbol (exp);
340       break;
341     }
342
343   return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
344 }
345
346 /* Append a string onto another string, bumping the pointer along.  */
347 void
348 append (charPP, fromP, length)
349      char **charPP;
350      char *fromP;
351      unsigned long length;
352 {
353   /* Don't trust memcpy() of 0 chars.  */
354   if (length == 0)
355     return;
356
357   memcpy (*charPP, fromP, length);
358   *charPP += length;
359 }
360
361 #ifndef BFD_ASSEMBLER
362 int section_alignment[SEG_MAXIMUM_ORDINAL];
363 #endif
364
365 /* This routine records the largest alignment seen for each segment.
366    If the beginning of the segment is aligned on the worst-case
367    boundary, all of the other alignments within it will work.  At
368    least one object format really uses this info.  */
369
370 void
371 record_alignment (seg, align)
372      /* Segment to which alignment pertains.  */
373      segT seg;
374      /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
375         boundary, etc.)  */
376      int align;
377 {
378   if (seg == absolute_section)
379     return;
380 #ifdef BFD_ASSEMBLER
381   if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
382     bfd_set_section_alignment (stdoutput, seg, align);
383 #else
384   if (align > section_alignment[(int) seg])
385     section_alignment[(int) seg] = align;
386 #endif
387 }
388
389 int
390 get_recorded_alignment (seg)
391      segT seg;
392 {
393   if (seg == absolute_section)
394     return 0;
395 #ifdef BFD_ASSEMBLER
396   return bfd_get_section_alignment (stdoutput, seg);
397 #else
398   return section_alignment[(int) seg];
399 #endif
400 }
401
402 #ifdef BFD_ASSEMBLER
403
404 /* Reset the section indices after removing the gas created sections.  */
405
406 static void
407 renumber_sections (abfd, sec, countparg)
408      bfd *abfd ATTRIBUTE_UNUSED;
409      asection *sec;
410      PTR countparg;
411 {
412   int *countp = (int *) countparg;
413
414   sec->index = *countp;
415   ++*countp;
416 }
417
418 #endif /* defined (BFD_ASSEMBLER)  */
419
420 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
421
422 static fragS *
423 chain_frchains_together_1 (section, frchp)
424      segT section;
425      struct frchain *frchp;
426 {
427   fragS dummy, *prev_frag = &dummy;
428 #ifdef BFD_ASSEMBLER
429   fixS fix_dummy, *prev_fix = &fix_dummy;
430 #endif
431
432   for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
433     {
434       prev_frag->fr_next = frchp->frch_root;
435       prev_frag = frchp->frch_last;
436       assert (prev_frag->fr_type != 0);
437 #ifdef BFD_ASSEMBLER
438       if (frchp->fix_root != (fixS *) NULL)
439         {
440           if (seg_info (section)->fix_root == (fixS *) NULL)
441             seg_info (section)->fix_root = frchp->fix_root;
442           prev_fix->fx_next = frchp->fix_root;
443           seg_info (section)->fix_tail = frchp->fix_tail;
444           prev_fix = frchp->fix_tail;
445         }
446 #endif
447     }
448   assert (prev_frag->fr_type != 0);
449   prev_frag->fr_next = 0;
450   return prev_frag;
451 }
452
453 #endif
454
455 #ifdef BFD_ASSEMBLER
456
457 static void
458 chain_frchains_together (abfd, section, xxx)
459      bfd *abfd ATTRIBUTE_UNUSED;
460      segT section;
461      PTR xxx ATTRIBUTE_UNUSED;
462 {
463   segment_info_type *info;
464
465   /* BFD may have introduced its own sections without using
466      subseg_new, so it is possible that seg_info is NULL.  */
467   info = seg_info (section);
468   if (info != (segment_info_type *) NULL)
469     info->frchainP->frch_last
470       = chain_frchains_together_1 (section, info->frchainP);
471
472   /* Now that we've chained the frags together, we must add new fixups
473      to the segment, not to the frag chain.  */
474   frags_chained = 1;
475 }
476
477 #endif
478
479 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
480
481 static void
482 remove_subsegs (head, seg, root, last)
483      frchainS *head;
484      int seg;
485      fragS **root;
486      fragS **last;
487 {
488   *root = head->frch_root;
489   *last = chain_frchains_together_1 (seg, head);
490 }
491
492 #endif /* BFD  */
493
494 #if defined (BFD_ASSEMBLER) || !defined (BFD)
495
496 #ifdef BFD_ASSEMBLER
497 static void
498 cvt_frag_to_fill (sec, fragP)
499      segT sec ATTRIBUTE_UNUSED;
500      fragS *fragP;
501 #else
502 static void
503 cvt_frag_to_fill (headersP, sec, fragP)
504      object_headers *headersP;
505      segT sec;
506      fragS *fragP;
507 #endif
508 {
509   switch (fragP->fr_type)
510     {
511     case rs_align:
512     case rs_align_code:
513     case rs_align_test:
514     case rs_org:
515     case rs_space:
516 #ifdef HANDLE_ALIGN
517       HANDLE_ALIGN (fragP);
518 #endif
519       know (fragP->fr_next != NULL);
520       fragP->fr_offset = (fragP->fr_next->fr_address
521                           - fragP->fr_address
522                           - fragP->fr_fix) / fragP->fr_var;
523       if (fragP->fr_offset < 0)
524         {
525           as_bad_where (fragP->fr_file, fragP->fr_line,
526                         _("attempt to .org/.space backwards? (%ld)"),
527                         (long) fragP->fr_offset);
528         }
529       fragP->fr_type = rs_fill;
530       break;
531
532     case rs_fill:
533       break;
534
535     case rs_leb128:
536       {
537         valueT value = S_GET_VALUE (fragP->fr_symbol);
538         int size;
539
540         size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
541                               fragP->fr_subtype);
542
543         fragP->fr_fix += size;
544         fragP->fr_type = rs_fill;
545         fragP->fr_var = 0;
546         fragP->fr_offset = 0;
547         fragP->fr_symbol = NULL;
548       }
549       break;
550
551     case rs_cfa:
552       eh_frame_convert_frag (fragP);
553       break;
554
555     case rs_dwarf2dbg:
556       dwarf2dbg_convert_frag (fragP);
557       break;
558
559     case rs_machine_dependent:
560 #ifdef BFD_ASSEMBLER
561       md_convert_frag (stdoutput, sec, fragP);
562 #else
563       md_convert_frag (headersP, sec, fragP);
564 #endif
565
566       assert (fragP->fr_next == NULL
567               || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
568                   == fragP->fr_fix));
569
570       /* After md_convert_frag, we make the frag into a ".space 0".
571          md_convert_frag() should set up any fixSs and constants
572          required.  */
573       frag_wane (fragP);
574       break;
575
576 #ifndef WORKING_DOT_WORD
577     case rs_broken_word:
578       {
579         struct broken_word *lie;
580
581         if (fragP->fr_subtype)
582           {
583             fragP->fr_fix += md_short_jump_size;
584             for (lie = (struct broken_word *) (fragP->fr_symbol);
585                  lie && lie->dispfrag == fragP;
586                  lie = lie->next_broken_word)
587               if (lie->added == 1)
588                 fragP->fr_fix += md_long_jump_size;
589           }
590         frag_wane (fragP);
591       }
592       break;
593 #endif
594
595     default:
596       BAD_CASE (fragP->fr_type);
597       break;
598     }
599 }
600
601 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD)  */
602
603 #ifdef BFD_ASSEMBLER
604 static void relax_seg PARAMS ((bfd *, asection *, PTR));
605 static void
606 relax_seg (abfd, sec, xxx)
607      bfd *abfd ATTRIBUTE_UNUSED;
608      asection *sec;
609      PTR xxx;
610 {
611   segment_info_type *seginfo = seg_info (sec);
612
613   if (seginfo && seginfo->frchainP
614       && relax_segment (seginfo->frchainP->frch_root, sec))
615     {
616       int *result = (int *) xxx;
617       *result = 1;
618     }
619 }
620
621 static void size_seg PARAMS ((bfd *, asection *, PTR));
622 static void
623 size_seg (abfd, sec, xxx)
624      bfd *abfd;
625      asection *sec;
626      PTR xxx ATTRIBUTE_UNUSED;
627 {
628   flagword flags;
629   fragS *fragp;
630   segment_info_type *seginfo;
631   int x;
632   valueT size, newsize;
633
634   subseg_change (sec, 0);
635
636   seginfo = seg_info (sec);
637   if (seginfo && seginfo->frchainP)
638     {
639       for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
640         cvt_frag_to_fill (sec, fragp);
641       for (fragp = seginfo->frchainP->frch_root;
642            fragp->fr_next;
643            fragp = fragp->fr_next)
644         /* Walk to last elt.  */
645         ;
646       size = fragp->fr_address + fragp->fr_fix;
647     }
648   else
649     size = 0;
650
651   flags = bfd_get_section_flags (abfd, sec);
652
653   if (size > 0 && ! seginfo->bss)
654     flags |= SEC_HAS_CONTENTS;
655
656   /* @@ This is just an approximation.  */
657   if (seginfo && seginfo->fix_root)
658     flags |= SEC_RELOC;
659   else
660     flags &= ~SEC_RELOC;
661   x = bfd_set_section_flags (abfd, sec, flags);
662   assert (x == true);
663
664   newsize = md_section_align (sec, size);
665   x = bfd_set_section_size (abfd, sec, newsize);
666   assert (x == true);
667
668   /* If the size had to be rounded up, add some padding in the last
669      non-empty frag.  */
670   assert (newsize >= size);
671   if (size != newsize)
672     {
673       fragS *last = seginfo->frchainP->frch_last;
674       fragp = seginfo->frchainP->frch_root;
675       while (fragp->fr_next != last)
676         fragp = fragp->fr_next;
677       last->fr_address = size;
678       fragp->fr_offset += newsize - size;
679     }
680
681 #ifdef tc_frob_section
682   tc_frob_section (sec);
683 #endif
684 #ifdef obj_frob_section
685   obj_frob_section (sec);
686 #endif
687 }
688
689 #ifdef DEBUG2
690 static void
691 dump_section_relocs (abfd, sec, stream_)
692      bfd *abfd ATTRIBUTE_UNUSED;
693      asection *sec;
694      char *stream_;
695 {
696   FILE *stream = (FILE *) stream_;
697   segment_info_type *seginfo = seg_info (sec);
698   fixS *fixp = seginfo->fix_root;
699
700   if (!fixp)
701     return;
702
703   fprintf (stream, "sec %s relocs:\n", sec->name);
704   while (fixp)
705     {
706       symbolS *s = fixp->fx_addsy;
707
708       fprintf (stream, "  %08lx: type %d ", (unsigned long) fixp,
709                (int) fixp->fx_r_type);
710       if (s == NULL)
711         fprintf (stream, "no sym\n");
712       else
713         {
714           print_symbol_value_1 (stream, s);
715           fprintf (stream, "\n");
716         }
717       fixp = fixp->fx_next;
718     }
719 }
720 #else
721 #define dump_section_relocs(ABFD,SEC,STREAM)    ((void) 0)
722 #endif
723
724 #ifndef EMIT_SECTION_SYMBOLS
725 #define EMIT_SECTION_SYMBOLS 1
726 #endif
727
728 static void
729 adjust_reloc_syms (abfd, sec, xxx)
730      bfd *abfd ATTRIBUTE_UNUSED;
731      asection *sec;
732      PTR xxx ATTRIBUTE_UNUSED;
733 {
734   segment_info_type *seginfo = seg_info (sec);
735   fixS *fixp;
736
737   if (seginfo == NULL)
738     return;
739
740   dump_section_relocs (abfd, sec, stderr);
741
742   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
743     if (fixp->fx_done)
744       /* Ignore it.  */
745       ;
746     else if (fixp->fx_addsy)
747       {
748         symbolS *sym;
749         asection *symsec;
750
751 #ifdef DEBUG5
752         fprintf (stderr, "\n\nadjusting fixup:\n");
753         print_fixup (fixp);
754 #endif
755
756         sym = fixp->fx_addsy;
757
758         /* All symbols should have already been resolved at this
759            point.  It is possible to see unresolved expression
760            symbols, though, since they are not in the regular symbol
761            table.  */
762         if (sym != NULL)
763           resolve_symbol_value (sym, finalize_syms);
764
765         if (fixp->fx_subsy != NULL)
766           resolve_symbol_value (fixp->fx_subsy, finalize_syms);
767
768         /* If this symbol is equated to an undefined symbol, convert
769            the fixup to being against that symbol.  */
770         if (sym != NULL && symbol_equated_p (sym)
771             && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
772           {
773             fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
774             sym = symbol_get_value_expression (sym)->X_add_symbol;
775             fixp->fx_addsy = sym;
776           }
777
778         if (sym != NULL && symbol_mri_common_p (sym))
779           {
780             /* These symbols are handled specially in fixup_segment.  */
781             goto done;
782           }
783
784         symsec = S_GET_SEGMENT (sym);
785
786         if (symsec == NULL)
787           abort ();
788
789         if (bfd_is_abs_section (symsec))
790           {
791             /* The fixup_segment routine will not use this symbol in a
792                relocation unless TC_FORCE_RELOCATION returns 1.  */
793             if (TC_FORCE_RELOCATION (fixp))
794               {
795                 symbol_mark_used_in_reloc (fixp->fx_addsy);
796 #ifdef UNDEFINED_DIFFERENCE_OK
797                 if (fixp->fx_subsy != NULL)
798                   symbol_mark_used_in_reloc (fixp->fx_subsy);
799 #endif
800               }
801             goto done;
802           }
803
804         /* If it's one of these sections, assume the symbol is
805            definitely going to be output.  The code in
806            md_estimate_size_before_relax in tc-mips.c uses this test
807            as well, so if you change this code you should look at that
808            code.  */
809         if (bfd_is_und_section (symsec)
810             || bfd_is_com_section (symsec))
811           {
812             symbol_mark_used_in_reloc (fixp->fx_addsy);
813 #ifdef UNDEFINED_DIFFERENCE_OK
814             /* We have the difference of an undefined symbol and some
815                other symbol.  Make sure to mark the other symbol as used
816                in a relocation so that it will always be output.  */
817             if (fixp->fx_subsy)
818               symbol_mark_used_in_reloc (fixp->fx_subsy);
819 #endif
820             goto done;
821           }
822
823         /* Don't try to reduce relocs which refer to non-local symbols
824            in .linkonce sections.  It can lead to confusion when a
825            debugging section refers to a .linkonce section.  I hope
826            this will always be correct.  */
827         if (symsec != sec && ! S_IS_LOCAL (sym))
828           {
829             boolean linkonce;
830
831             linkonce = false;
832 #ifdef BFD_ASSEMBLER
833             if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
834                 != 0)
835               linkonce = true;
836 #endif
837 #ifdef OBJ_ELF
838             /* The GNU toolchain uses an extension for ELF: a section
839                beginning with the magic string .gnu.linkonce is a
840                linkonce section.  */
841             if (strncmp (segment_name (symsec), ".gnu.linkonce",
842                          sizeof ".gnu.linkonce" - 1) == 0)
843               linkonce = true;
844 #endif
845
846             if (linkonce)
847               {
848                 symbol_mark_used_in_reloc (fixp->fx_addsy);
849 #ifdef UNDEFINED_DIFFERENCE_OK
850                 if (fixp->fx_subsy != NULL)
851                   symbol_mark_used_in_reloc (fixp->fx_subsy);
852 #endif
853                 goto done;
854               }
855           }
856
857         /* Since we're reducing to section symbols, don't attempt to reduce
858            anything that's already using one.  */
859         if (symbol_section_p (sym))
860           {
861             symbol_mark_used_in_reloc (fixp->fx_addsy);
862             goto done;
863           }
864
865 #ifdef BFD_ASSEMBLER
866         /* We can never adjust a reloc against a weak symbol.  If we
867            did, and the weak symbol was overridden by a real symbol
868            somewhere else, then our relocation would be pointing at
869            the wrong area of memory.  */
870         if (S_IS_WEAK (sym))
871           {
872             symbol_mark_used_in_reloc (fixp->fx_addsy);
873             goto done;
874           }
875
876         /* Never adjust a reloc against local symbol in a merge section.  */
877         if (symsec->flags & SEC_MERGE)
878           {
879             symbol_mark_used_in_reloc (fixp->fx_addsy);
880             goto done;
881           }
882 #endif
883
884         /* Is there some other reason we can't adjust this one?  (E.g.,
885            call/bal links in i960-bout symbols.)  */
886 #ifdef obj_fix_adjustable
887         if (! obj_fix_adjustable (fixp))
888           {
889             symbol_mark_used_in_reloc (fixp->fx_addsy);
890             goto done;
891           }
892 #endif
893
894         /* Is there some other (target cpu dependent) reason we can't adjust
895            this one?  (E.g. relocations involving function addresses on
896            the PA.  */
897 #ifdef tc_fix_adjustable
898         if (! tc_fix_adjustable (fixp))
899           {
900             symbol_mark_used_in_reloc (fixp->fx_addsy);
901             goto done;
902           }
903 #endif
904
905         /* If the section symbol isn't going to be output, the relocs
906            at least should still work.  If not, figure out what to do
907            when we run into that case.
908
909            We refetch the segment when calling section_symbol, rather
910            than using symsec, because S_GET_VALUE may wind up changing
911            the section when it calls resolve_symbol_value.  */
912         fixp->fx_offset += S_GET_VALUE (sym);
913         fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
914         symbol_mark_used_in_reloc (fixp->fx_addsy);
915 #ifdef DEBUG5
916         fprintf (stderr, "\nadjusted fixup:\n");
917         print_fixup (fixp);
918 #endif
919
920       done:
921         ;
922       }
923 #if 1 /* def RELOC_REQUIRES_SYMBOL  */
924     else
925       {
926         /* There was no symbol required by this relocation.  However,
927            BFD doesn't really handle relocations without symbols well.
928            (At least, the COFF support doesn't.)  So for now we fake up
929            a local symbol in the absolute section.  */
930
931         fixp->fx_addsy = section_symbol (absolute_section);
932 #if 0
933         fixp->fx_addsy->sy_used_in_reloc = 1;
934 #endif
935       }
936 #endif
937
938   dump_section_relocs (abfd, sec, stderr);
939 }
940
941 static void
942 write_relocs (abfd, sec, xxx)
943      bfd *abfd;
944      asection *sec;
945      PTR xxx ATTRIBUTE_UNUSED;
946 {
947   segment_info_type *seginfo = seg_info (sec);
948   unsigned int i;
949   unsigned int n;
950   arelent **relocs;
951   fixS *fixp;
952   char *err;
953
954   /* If seginfo is NULL, we did not create this section; don't do
955      anything with it.  */
956   if (seginfo == NULL)
957     return;
958
959   fixup_segment (seginfo->fix_root, sec);
960
961   n = 0;
962   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
963     n++;
964
965 #ifndef RELOC_EXPANSION_POSSIBLE
966   /* Set up reloc information as well.  */
967   relocs = (arelent **) xmalloc (n * sizeof (arelent *));
968   memset ((char *) relocs, 0, n * sizeof (arelent *));
969
970   i = 0;
971   for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
972     {
973       arelent *reloc;
974       bfd_reloc_status_type s;
975       symbolS *sym;
976
977       if (fixp->fx_done)
978         {
979           n--;
980           continue;
981         }
982
983       /* If this is an undefined symbol which was equated to another
984          symbol, then use generate the reloc against the latter symbol
985          rather than the former.  */
986       sym = fixp->fx_addsy;
987       while (symbol_equated_p (sym)
988              && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
989         {
990           symbolS *n;
991
992           /* We must avoid looping, as that can occur with a badly
993              written program.  */
994           n = symbol_get_value_expression (sym)->X_add_symbol;
995           if (n == sym)
996             break;
997           fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
998           sym = n;
999         }
1000       fixp->fx_addsy = sym;
1001
1002       reloc = tc_gen_reloc (sec, fixp);
1003       if (!reloc)
1004         {
1005           n--;
1006           continue;
1007         }
1008
1009 #if 0
1010       /* This test is triggered inappropriately for the SH.  */
1011       if (fixp->fx_where + fixp->fx_size
1012           > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1013         abort ();
1014 #endif
1015
1016       s = bfd_install_relocation (stdoutput, reloc,
1017                                   fixp->fx_frag->fr_literal,
1018                                   fixp->fx_frag->fr_address,
1019                                   sec, &err);
1020       switch (s)
1021         {
1022         case bfd_reloc_ok:
1023           break;
1024         case bfd_reloc_overflow:
1025           as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation overflow"));
1026           break;
1027         case bfd_reloc_outofrange:
1028           as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation out of range"));
1029           break;
1030         default:
1031           as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1032                     fixp->fx_file, fixp->fx_line, s);
1033         }
1034       relocs[i++] = reloc;
1035     }
1036 #else
1037   n = n * MAX_RELOC_EXPANSION;
1038   /* Set up reloc information as well.  */
1039   relocs = (arelent **) xmalloc (n * sizeof (arelent *));
1040
1041   i = 0;
1042   for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1043     {
1044       arelent **reloc;
1045       char *data;
1046       bfd_reloc_status_type s;
1047       symbolS *sym;
1048       int j;
1049
1050       if (fixp->fx_done)
1051         {
1052           n--;
1053           continue;
1054         }
1055
1056       /* If this is an undefined symbol which was equated to another
1057          symbol, then generate the reloc against the latter symbol
1058          rather than the former.  */
1059       sym = fixp->fx_addsy;
1060       while (symbol_equated_p (sym)
1061              && (! S_IS_DEFINED (sym) || S_IS_COMMON (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 *, size_t));
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
1382       asympp = (asymbol **) bfd_alloc (stdoutput,
1383                                        nsyms * sizeof (asymbol *));
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.\n"),
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.\n"),
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, *sec;
1511     int i;
1512
1513     seclist = &stdoutput->sections;
1514     while (seclist && *seclist)
1515       {
1516         sec = *seclist;
1517         while (sec == reg_section || sec == expr_section)
1518           {
1519             sec = sec->next;
1520             *seclist = sec;
1521             stdoutput->section_count--;
1522             if (!sec)
1523               break;
1524           }
1525         if (*seclist)
1526           seclist = &(*seclist)->next;
1527       }
1528     i = 0;
1529     bfd_map_over_sections (stdoutput, renumber_sections, &i);
1530   }
1531
1532   bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1533 #else
1534   remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1535   remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1536   remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1537 #endif
1538
1539   /* We have two segments. If user gave -R flag, then we must put the
1540      data frags into the text segment. Do this before relaxing so
1541      we know to take advantage of -R and make shorter addresses.  */
1542 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1543   if (flag_readonly_data_in_text)
1544     {
1545       merge_data_into_text ();
1546     }
1547 #endif
1548
1549 #ifdef BFD_ASSEMBLER
1550   while (1)
1551     {
1552       int changed;
1553
1554 #ifndef WORKING_DOT_WORD
1555       /* We need to reset the markers in the broken word list and
1556          associated frags between calls to relax_segment (via
1557          relax_seg).  Since the broken word list is global, we do it
1558          once per round, rather than locally in relax_segment for each
1559          segment.  */
1560       struct broken_word *brokp;
1561
1562       for (brokp = broken_words;
1563            brokp != (struct broken_word *) NULL;
1564            brokp = brokp->next_broken_word)
1565         {
1566           brokp->added = 0;
1567
1568           if (brokp->dispfrag != (fragS *) NULL
1569               && brokp->dispfrag->fr_type == rs_broken_word)
1570             brokp->dispfrag->fr_subtype = 0;
1571         }
1572 #endif
1573
1574       changed = 0;
1575       bfd_map_over_sections (stdoutput, relax_seg, &changed);
1576       if (!changed)
1577         break;
1578     }
1579   bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1580 #else
1581   relax_and_size_all_segments ();
1582 #endif /* BFD_ASSEMBLER  */
1583
1584   /* Relaxation has completed.  Freeze all syms.  */
1585   finalize_syms = 2;
1586
1587 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1588   /* Now that the segments have their final sizes, run through the
1589      sections and set their vma and lma. !BFD gas sets them, and BFD gas
1590      should too. Currently, only DJGPP uses this code, but other
1591      COFF targets may need to execute this too.  */
1592   bfd_map_over_sections (stdoutput, set_segment_vma, (char *) 0);
1593 #endif
1594
1595 #ifndef BFD_ASSEMBLER
1596   /* Crawl the symbol chain.
1597
1598      For each symbol whose value depends on a frag, take the address of
1599      that frag and subsume it into the value of the symbol.
1600      After this, there is just one way to lookup a symbol value.
1601      Values are left in their final state for object file emission.
1602      We adjust the values of 'L' local symbols, even if we do
1603      not intend to emit them to the object file, because their values
1604      are needed for fix-ups.
1605
1606      Unless we saw a -L flag, remove all symbols that begin with 'L'
1607      from the symbol chain.  (They are still pointed to by the fixes.)
1608
1609      Count the remaining symbols.
1610      Assign a symbol number to each symbol.
1611      Count the number of string-table chars we will emit.
1612      Put this info into the headers as appropriate.  */
1613   know (zero_address_frag.fr_address == 0);
1614   string_byte_count = sizeof (string_byte_count);
1615
1616   obj_crawl_symbol_chain (&headers);
1617
1618   if (string_byte_count == sizeof (string_byte_count))
1619     string_byte_count = 0;
1620
1621   H_SET_STRING_SIZE (&headers, string_byte_count);
1622
1623   /* Addresses of frags now reflect addresses we use in the object file.
1624      Symbol values are correct.
1625      Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1626      Also converting any machine-dependent frags using md_convert_frag();  */
1627   subseg_change (SEG_TEXT, 0);
1628
1629   for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1630     {
1631       /* At this point we have linked all the frags into a single
1632          chain.  However, cvt_frag_to_fill may call md_convert_frag
1633          which may call fix_new.  We need to ensure that fix_new adds
1634          the fixup to the right section.  */
1635       if (fragP == data_frag_root)
1636         subseg_change (SEG_DATA, 0);
1637
1638       cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1639
1640       /* Some assert macros don't work with # directives mixed in.  */
1641 #ifndef NDEBUG
1642       if (!(fragP->fr_next == NULL
1643 #ifdef OBJ_BOUT
1644             || fragP->fr_next == data_frag_root
1645 #endif
1646             || ((fragP->fr_next->fr_address - fragP->fr_address)
1647                 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1648         abort ();
1649 #endif
1650     }
1651 #endif /* ! BFD_ASSEMBLER  */
1652
1653 #ifndef WORKING_DOT_WORD
1654   {
1655     struct broken_word *lie;
1656     struct broken_word **prevP;
1657
1658     prevP = &broken_words;
1659     for (lie = broken_words; lie; lie = lie->next_broken_word)
1660       if (!lie->added)
1661         {
1662           expressionS exp;
1663
1664           subseg_change (lie->seg, lie->subseg);
1665           exp.X_op = O_subtract;
1666           exp.X_add_symbol = lie->add;
1667           exp.X_op_symbol = lie->sub;
1668           exp.X_add_number = lie->addnum;
1669 #ifdef BFD_ASSEMBLER
1670 #ifdef TC_CONS_FIX_NEW
1671           TC_CONS_FIX_NEW (lie->frag,
1672                            lie->word_goes_here - lie->frag->fr_literal,
1673                            2, &exp);
1674 #else
1675           fix_new_exp (lie->frag,
1676                        lie->word_goes_here - lie->frag->fr_literal,
1677                        2, &exp, 0, BFD_RELOC_16);
1678 #endif
1679 #else
1680 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1681           fix_new_exp (lie->frag,
1682                        lie->word_goes_here - lie->frag->fr_literal,
1683                        2, &exp, 0, NO_RELOC);
1684 #else
1685 #ifdef TC_NS32K
1686           fix_new_ns32k_exp (lie->frag,
1687                              lie->word_goes_here - lie->frag->fr_literal,
1688                              2, &exp, 0, 0, 2, 0, 0);
1689 #else
1690           fix_new_exp (lie->frag,
1691                        lie->word_goes_here - lie->frag->fr_literal,
1692                        2, &exp, 0, 0);
1693 #endif /* TC_NS32K  */
1694 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE  */
1695 #endif /* BFD_ASSEMBLER  */
1696           *prevP = lie->next_broken_word;
1697         }
1698       else
1699         prevP = &(lie->next_broken_word);
1700
1701     for (lie = broken_words; lie;)
1702       {
1703         struct broken_word *untruth;
1704         char *table_ptr;
1705         addressT table_addr;
1706         addressT from_addr, to_addr;
1707         int n, m;
1708
1709         subseg_change (lie->seg, lie->subseg);
1710         fragP = lie->dispfrag;
1711
1712         /* Find out how many broken_words go here.  */
1713         n = 0;
1714         for (untruth = lie;
1715              untruth && untruth->dispfrag == fragP;
1716              untruth = untruth->next_broken_word)
1717           if (untruth->added == 1)
1718             n++;
1719
1720         table_ptr = lie->dispfrag->fr_opcode;
1721         table_addr = (lie->dispfrag->fr_address
1722                       + (table_ptr - lie->dispfrag->fr_literal));
1723         /* Create the jump around the long jumps.  This is a short
1724            jump from table_ptr+0 to table_ptr+n*long_jump_size.  */
1725         from_addr = table_addr;
1726         to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1727         md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1728                               lie->add);
1729         table_ptr += md_short_jump_size;
1730         table_addr += md_short_jump_size;
1731
1732         for (m = 0;
1733              lie && lie->dispfrag == fragP;
1734              m++, lie = lie->next_broken_word)
1735           {
1736             if (lie->added == 2)
1737               continue;
1738             /* Patch the jump table.  */
1739             /* This is the offset from ??? to table_ptr+0.  */
1740             to_addr = table_addr - S_GET_VALUE (lie->sub);
1741 #ifdef BFD_ASSEMBLER
1742             to_addr -= symbol_get_frag (lie->sub)->fr_address;
1743 #endif
1744 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1745             TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1746 #endif
1747             md_number_to_chars (lie->word_goes_here, to_addr, 2);
1748             for (untruth = lie->next_broken_word;
1749                  untruth && untruth->dispfrag == fragP;
1750                  untruth = untruth->next_broken_word)
1751               {
1752                 if (untruth->use_jump == lie)
1753                   md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1754               }
1755
1756             /* Install the long jump.  */
1757             /* This is a long jump from table_ptr+0 to the final target.  */
1758             from_addr = table_addr;
1759             to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1760 #ifdef BFD_ASSEMBLER
1761             to_addr += symbol_get_frag (lie->add)->fr_address;
1762 #endif
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, 0, 0);
1881     bfd_write (the_object_file, 1, 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, finalize_syms);
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, finalize_syms);
1954
1955           /* Skip symbols which were equated to undefined or common
1956              symbols.  */
1957           if (symbol_equated_p (symp)
1958               && (! S_IS_DEFINED (symp) || S_IS_COMMON (symp)))
1959             {
1960               symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1961               continue;
1962             }
1963
1964           /* So far, common symbols have been treated like undefined symbols.
1965              Put them in the common section now.  */
1966           if (S_IS_DEFINED (symp) == 0
1967               && S_GET_VALUE (symp) != 0)
1968             S_SET_SEGMENT (symp, bfd_com_section_ptr);
1969 #if 0
1970           printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1971                   S_GET_NAME (symp), symp,
1972                   S_GET_VALUE (symp),
1973                   symbol_get_bfdsym (symp)->flags,
1974                   segment_name (S_GET_SEGMENT (symp)));
1975 #endif
1976
1977 #ifdef obj_frob_symbol
1978           obj_frob_symbol (symp, punt);
1979 #endif
1980 #ifdef tc_frob_symbol
1981           if (! punt || symbol_used_in_reloc_p (symp))
1982             tc_frob_symbol (symp, punt);
1983 #endif
1984
1985           /* If we don't want to keep this symbol, splice it out of
1986              the chain now.  If EMIT_SECTION_SYMBOLS is 0, we never
1987              want section symbols.  Otherwise, we skip local symbols
1988              and symbols that the frob_symbol macros told us to punt,
1989              but we keep such symbols if they are used in relocs.  */
1990           if ((! EMIT_SECTION_SYMBOLS
1991                && symbol_section_p (symp))
1992               /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1993                  opposites.  Sometimes the former checks flags and the
1994                  latter examines the name...  */
1995               || (!S_IS_EXTERN (symp)
1996                   && (S_IS_LOCAL (symp) || punt)
1997                   && ! symbol_used_in_reloc_p (symp)))
1998             {
1999               symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2000
2001               /* After symbol_remove, symbol_next(symp) still returns
2002                  the one that came after it in the chain.  So we don't
2003                  need to do any extra cleanup work here.  */
2004               continue;
2005             }
2006
2007           /* Make sure we really got a value for the symbol.  */
2008           if (! symbol_resolved_p (symp))
2009             {
2010               as_bad (_("can't resolve value for symbol \"%s\""),
2011                       S_GET_NAME (symp));
2012               symbol_mark_resolved (symp);
2013             }
2014
2015           /* Set the value into the BFD symbol.  Up til now the value
2016              has only been kept in the gas symbolS struct.  */
2017           symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2018         }
2019     }
2020
2021   PROGRESS (1);
2022
2023   /* Now do any format-specific adjustments to the symbol table, such
2024      as adding file symbols.  */
2025 #ifdef tc_adjust_symtab
2026   tc_adjust_symtab ();
2027 #endif
2028 #ifdef obj_adjust_symtab
2029   obj_adjust_symtab ();
2030 #endif
2031
2032   /* Now that all the sizes are known, and contents correct, we can
2033      start writing to the file.  */
2034   set_symtab ();
2035
2036   /* If *_frob_file changes the symbol value at this point, it is
2037      responsible for moving the changed value into symp->bsym->value
2038      as well.  Hopefully all symbol value changing can be done in
2039      *_frob_symbol.  */
2040 #ifdef tc_frob_file
2041   tc_frob_file ();
2042 #endif
2043 #ifdef obj_frob_file
2044   obj_frob_file ();
2045 #endif
2046
2047   bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2048
2049 #ifdef tc_frob_file_after_relocs
2050   tc_frob_file_after_relocs ();
2051 #endif
2052 #ifdef obj_frob_file_after_relocs
2053   obj_frob_file_after_relocs ();
2054 #endif
2055
2056   bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2057 #endif /* BFD_ASSEMBLER  */
2058 }
2059 #endif /* ! BFD  */
2060
2061 #ifdef TC_GENERIC_RELAX_TABLE
2062
2063 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE.  */
2064
2065 long
2066 relax_frag (segment, fragP, stretch)
2067      segT segment;
2068      fragS *fragP;
2069      long stretch;
2070 {
2071   const relax_typeS *this_type;
2072   const relax_typeS *start_type;
2073   relax_substateT next_state;
2074   relax_substateT this_state;
2075   long growth;
2076   offsetT aim;
2077   addressT target;
2078   addressT address;
2079   symbolS *symbolP;
2080   const relax_typeS *table;
2081
2082   target = fragP->fr_offset;
2083   address = fragP->fr_address;
2084   table = TC_GENERIC_RELAX_TABLE;
2085   this_state = fragP->fr_subtype;
2086   start_type = this_type = table + this_state;
2087   symbolP = fragP->fr_symbol;
2088
2089   if (symbolP)
2090     {
2091       fragS *sym_frag;
2092
2093       sym_frag = symbol_get_frag (symbolP);
2094
2095 #ifndef DIFF_EXPR_OK
2096 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2097       know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2098             || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2099             || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2100             || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2101 #endif
2102       know (sym_frag != NULL);
2103 #endif
2104       know (!(S_GET_SEGMENT (symbolP) == absolute_section)
2105             || sym_frag == &zero_address_frag);
2106       target += S_GET_VALUE (symbolP) + sym_frag->fr_address;
2107
2108       /* If frag has yet to be reached on this pass,
2109          assume it will move by STRETCH just as we did.
2110          If this is not so, it will be because some frag
2111          between grows, and that will force another pass.  */
2112
2113       if (stretch != 0
2114           && sym_frag->relax_marker != fragP->relax_marker
2115           && S_GET_SEGMENT (symbolP) == segment)
2116         {
2117           target += stretch;
2118         }
2119     }
2120
2121   aim = target - address - fragP->fr_fix;
2122 #ifdef TC_PCREL_ADJUST
2123   /* Currently only the ns32k family needs this.  */
2124   aim += TC_PCREL_ADJUST (fragP);
2125 /* #else */
2126   /* This machine doesn't want to use pcrel_adjust.
2127      In that case, pcrel_adjust should be zero.  */
2128 #if 0
2129   assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
2130 #endif
2131 #endif
2132 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE  */
2133   md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2134 #endif
2135
2136   if (aim < 0)
2137     {
2138       /* Look backwards.  */
2139       for (next_state = this_type->rlx_more; next_state;)
2140         if (aim >= this_type->rlx_backward)
2141           next_state = 0;
2142         else
2143           {
2144             /* Grow to next state.  */
2145             this_state = next_state;
2146             this_type = table + this_state;
2147             next_state = this_type->rlx_more;
2148           }
2149     }
2150   else
2151     {
2152       /* Look forwards.  */
2153       for (next_state = this_type->rlx_more; next_state;)
2154         if (aim <= this_type->rlx_forward)
2155           next_state = 0;
2156         else
2157           {
2158             /* Grow to next state.  */
2159             this_state = next_state;
2160             this_type = table + this_state;
2161             next_state = this_type->rlx_more;
2162           }
2163     }
2164
2165   growth = this_type->rlx_length - start_type->rlx_length;
2166   if (growth != 0)
2167     fragP->fr_subtype = this_state;
2168   return growth;
2169 }
2170
2171 #endif /* defined (TC_GENERIC_RELAX_TABLE)  */
2172
2173 /* Relax_align. Advance location counter to next address that has 'alignment'
2174    lowest order bits all 0s, return size of adjustment made.  */
2175 static relax_addressT
2176 relax_align (address, alignment)
2177      register relax_addressT address;   /* Address now.  */
2178      register int alignment;    /* Alignment (binary).  */
2179 {
2180   relax_addressT mask;
2181   relax_addressT new_address;
2182
2183   mask = ~((~0) << alignment);
2184   new_address = (address + mask) & (~mask);
2185 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2186   if (linkrelax)
2187     /* We must provide lots of padding, so the linker can discard it
2188        when needed.  The linker will not add extra space, ever.  */
2189     new_address += (1 << alignment);
2190 #endif
2191   return (new_address - address);
2192 }
2193
2194 /* Now we have a segment, not a crowd of sub-segments, we can make
2195    fr_address values.
2196
2197    Relax the frags.
2198
2199    After this, all frags in this segment have addresses that are correct
2200    within the segment. Since segments live in different file addresses,
2201    these frag addresses may not be the same as final object-file
2202    addresses.  */
2203
2204 int
2205 relax_segment (segment_frag_root, segment)
2206      struct frag *segment_frag_root;
2207      segT segment;
2208 {
2209   register struct frag *fragP;
2210   register relax_addressT address;
2211   int ret;
2212
2213 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2214   know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2215 #endif
2216   /* In case md_estimate_size_before_relax() wants to make fixSs.  */
2217   subseg_change (segment, 0);
2218
2219   /* For each frag in segment: count and store  (a 1st guess of)
2220      fr_address.  */
2221   address = 0;
2222   for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2223     {
2224       fragP->relax_marker = 0;
2225       fragP->fr_address = address;
2226       address += fragP->fr_fix;
2227
2228       switch (fragP->fr_type)
2229         {
2230         case rs_fill:
2231           address += fragP->fr_offset * fragP->fr_var;
2232           break;
2233
2234         case rs_align:
2235         case rs_align_code:
2236         case rs_align_test:
2237           {
2238             addressT offset = relax_align (address, (int) fragP->fr_offset);
2239
2240             if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2241               offset = 0;
2242
2243             if (offset % fragP->fr_var != 0)
2244               {
2245                 as_bad (_("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           address += md_estimate_size_before_relax (fragP, segment);
2261           break;
2262
2263 #ifndef WORKING_DOT_WORD
2264           /* Broken words don't concern us yet.  */
2265         case rs_broken_word:
2266           break;
2267 #endif
2268
2269         case rs_leb128:
2270           /* Initial guess is always 1; doing otherwise can result in
2271              stable solutions that are larger than the minimum.  */
2272           address += fragP->fr_offset = 1;
2273           break;
2274
2275         case rs_cfa:
2276           address += eh_frame_estimate_size_before_relax (fragP);
2277           break;
2278
2279         case rs_dwarf2dbg:
2280           address += dwarf2dbg_estimate_size_before_relax (fragP);
2281           break;
2282
2283         default:
2284           BAD_CASE (fragP->fr_type);
2285           break;
2286         }
2287     }
2288
2289   /* Do relax().  */
2290   {
2291     long stretch;       /* May be any size, 0 or negative.  */
2292     /* Cumulative number of addresses we have relaxed this pass.
2293        We may have relaxed more than one address.  */
2294     int stretched;      /* Have we stretched on this pass?  */
2295     /* This is 'cuz stretch may be zero, when, in fact some piece of code
2296        grew, and another shrank.  If a branch instruction doesn't fit anymore,
2297        we could be scrod.  */
2298
2299     do
2300       {
2301         stretch = 0;
2302         stretched = 0;
2303
2304         for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2305           {
2306             long growth = 0;
2307             addressT was_address;
2308             offsetT offset;
2309             symbolS *symbolP;
2310
2311             fragP->relax_marker ^= 1;
2312             was_address = fragP->fr_address;
2313             address = fragP->fr_address += stretch;
2314             symbolP = fragP->fr_symbol;
2315             offset = fragP->fr_offset;
2316
2317             switch (fragP->fr_type)
2318               {
2319               case rs_fill:     /* .fill never relaxes.  */
2320                 growth = 0;
2321                 break;
2322
2323 #ifndef WORKING_DOT_WORD
2324                 /* JF:  This is RMS's idea.  I do *NOT* want to be blamed
2325                    for it I do not want to write it.  I do not want to have
2326                    anything to do with it.  This is not the proper way to
2327                    implement this misfeature.  */
2328               case rs_broken_word:
2329                 {
2330                   struct broken_word *lie;
2331                   struct broken_word *untruth;
2332
2333                   /* Yes this is ugly (storing the broken_word pointer
2334                      in the symbol slot).  Still, this whole chunk of
2335                      code is ugly, and I don't feel like doing anything
2336                      about it.  Think of it as stubbornness in action.  */
2337                   growth = 0;
2338                   for (lie = (struct broken_word *) (fragP->fr_symbol);
2339                        lie && lie->dispfrag == fragP;
2340                        lie = lie->next_broken_word)
2341                     {
2342
2343                       if (lie->added)
2344                         continue;
2345
2346                       offset = (symbol_get_frag (lie->add)->fr_address
2347                                 + S_GET_VALUE (lie->add)
2348                                 + lie->addnum
2349                                 - (symbol_get_frag (lie->sub)->fr_address
2350                                    + S_GET_VALUE (lie->sub)));
2351                       if (offset <= -32768 || offset >= 32767)
2352                         {
2353                           if (flag_warn_displacement)
2354                             {
2355                               char buf[50];
2356                               sprint_value (buf, (addressT) lie->addnum);
2357                               as_warn (_(".word %s-%s+%s didn't fit"),
2358                                        S_GET_NAME (lie->add),
2359                                        S_GET_NAME (lie->sub),
2360                                        buf);
2361                             }
2362                           lie->added = 1;
2363                           if (fragP->fr_subtype == 0)
2364                             {
2365                               fragP->fr_subtype++;
2366                               growth += md_short_jump_size;
2367                             }
2368                           for (untruth = lie->next_broken_word;
2369                                untruth && untruth->dispfrag == lie->dispfrag;
2370                                untruth = untruth->next_broken_word)
2371                             if ((symbol_get_frag (untruth->add)
2372                                  == symbol_get_frag (lie->add))
2373                                 && (S_GET_VALUE (untruth->add)
2374                                     == S_GET_VALUE (lie->add)))
2375                               {
2376                                 untruth->added = 2;
2377                                 untruth->use_jump = lie;
2378                               }
2379                           growth += md_long_jump_size;
2380                         }
2381                     }
2382
2383                   break;
2384                 }               /* case rs_broken_word  */
2385 #endif
2386               case rs_align:
2387               case rs_align_code:
2388               case rs_align_test:
2389                 {
2390                   addressT oldoff, newoff;
2391
2392                   oldoff = relax_align (was_address + fragP->fr_fix,
2393                                         (int) offset);
2394                   newoff = relax_align (address + fragP->fr_fix,
2395                                         (int) offset);
2396
2397                   if (fragP->fr_subtype != 0)
2398                     {
2399                       if (oldoff > fragP->fr_subtype)
2400                         oldoff = 0;
2401                       if (newoff > fragP->fr_subtype)
2402                         newoff = 0;
2403                     }
2404
2405                   growth = newoff - oldoff;
2406                 }
2407                 break;
2408
2409               case rs_org:
2410                 {
2411                   addressT target = offset;
2412                   addressT after;
2413
2414                   if (symbolP)
2415                     {
2416 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2417                       know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2418                             || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2419                             || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2420                             || S_GET_SEGMENT (symbolP) == SEG_BSS);
2421                       know (symbolP->sy_frag);
2422                       know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2423                             || (symbolP->sy_frag == &zero_address_frag));
2424 #endif
2425                       target += (S_GET_VALUE (symbolP)
2426                                  + symbol_get_frag (symbolP)->fr_address);
2427                     }           /* if we have a symbol  */
2428
2429                   know (fragP->fr_next);
2430                   after = fragP->fr_next->fr_address;
2431                   growth = target - after;
2432                   if (growth < 0)
2433                     {
2434                       /* Growth may be negative, but variable part of frag
2435                          cannot have fewer than 0 chars.  That is, we can't
2436                          .org backwards.  */
2437                       as_bad_where (fragP->fr_file, fragP->fr_line,
2438                                     _("attempt to .org backwards ignored"));
2439
2440                       /* We've issued an error message.  Change the
2441                          frag to avoid cascading errors.  */
2442                       fragP->fr_type = rs_align;
2443                       fragP->fr_subtype = 0;
2444                       fragP->fr_offset = 0;
2445                       fragP->fr_fix = after - address;
2446                       growth = stretch;
2447                     }
2448
2449                   /* This is an absolute growth factor  */
2450                   growth -= stretch;
2451                   break;
2452                 }
2453
2454               case rs_space:
2455                 if (symbolP)
2456                   {
2457                     growth = S_GET_VALUE (symbolP);
2458                     if (symbol_get_frag (symbolP) != &zero_address_frag
2459                         || S_IS_COMMON (symbolP)
2460                         || ! S_IS_DEFINED (symbolP))
2461                       as_bad_where (fragP->fr_file, fragP->fr_line,
2462                                     _(".space specifies non-absolute value"));
2463                     fragP->fr_symbol = 0;
2464                     if (growth < 0)
2465                       {
2466                         as_warn (_(".space or .fill with negative value, ignored"));
2467                         growth = 0;
2468                       }
2469                   }
2470                 else
2471                   growth = 0;
2472                 break;
2473
2474               case rs_machine_dependent:
2475 #ifdef md_relax_frag
2476                 growth = md_relax_frag (segment, fragP, stretch);
2477 #else
2478 #ifdef TC_GENERIC_RELAX_TABLE
2479                 /* The default way to relax a frag is to look through
2480                    TC_GENERIC_RELAX_TABLE.  */
2481                 growth = relax_frag (segment, fragP, stretch);
2482 #endif /* TC_GENERIC_RELAX_TABLE  */
2483 #endif
2484                 break;
2485
2486               case rs_leb128:
2487                 {
2488                   valueT value;
2489                   int size;
2490
2491                   value = resolve_symbol_value (fragP->fr_symbol, 0);
2492                   size = sizeof_leb128 (value, fragP->fr_subtype);
2493                   growth = size - fragP->fr_offset;
2494                   fragP->fr_offset = size;
2495                 }
2496                 break;
2497
2498               case rs_cfa:
2499                 growth = eh_frame_relax_frag (fragP);
2500                 break;
2501
2502               case rs_dwarf2dbg:
2503                 growth = dwarf2dbg_relax_frag (fragP);
2504                 break;
2505
2506               default:
2507                 BAD_CASE (fragP->fr_type);
2508                 break;
2509               }
2510             if (growth)
2511               {
2512                 stretch += growth;
2513                 stretched = 1;
2514               }
2515           }                     /* For each frag in the segment.  */
2516       }
2517     while (stretched);          /* Until nothing further to relax.  */
2518   }                             /* do_relax  */
2519
2520   ret = 0;
2521   for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2522     if (fragP->last_fr_address != fragP->fr_address)
2523       {
2524         fragP->last_fr_address = fragP->fr_address;
2525         ret = 1;
2526       }
2527   return ret;
2528 }
2529
2530 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2531
2532 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2533 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2534 #endif
2535
2536 /* fixup_segment()
2537
2538    Go through all the fixS's in a segment and see which ones can be
2539    handled now.  (These consist of fixS where we have since discovered
2540    the value of a symbol, or the address of the frag involved.)
2541    For each one, call md_apply_fix to put the fix into the frag data.
2542
2543    Result is a count of how many relocation structs will be needed to
2544    handle the remaining fixS's that we couldn't completely handle here.
2545    These will be output later by emit_relocations().  */
2546
2547 static long
2548 fixup_segment (fixP, this_segment_type)
2549      register fixS *fixP;
2550      segT this_segment_type;    /* N_TYPE bits for segment.  */
2551 {
2552   long seg_reloc_count = 0;
2553   symbolS *add_symbolP;
2554   symbolS *sub_symbolP;
2555   valueT add_number;
2556   int size;
2557   char *place;
2558   long where;
2559   int pcrel, plt;
2560   fragS *fragP;
2561   segT add_symbol_segment = absolute_section;
2562
2563   /* If the linker is doing the relaxing, we must not do any fixups.
2564
2565      Well, strictly speaking that's not true -- we could do any that are
2566      PC-relative and don't cross regions that could change size.  And for the
2567      i960 (the only machine for which we've got a relaxing linker right now),
2568      we might be able to turn callx/callj into bal anyways in cases where we
2569      know the maximum displacement.  */
2570   if (linkrelax && TC_LINKRELAX_FIXUP (this_segment_type))
2571     {
2572       for (; fixP; fixP = fixP->fx_next)
2573         seg_reloc_count++;
2574       TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2575       return seg_reloc_count;
2576     }
2577
2578   for (; fixP; fixP = fixP->fx_next)
2579     {
2580 #ifdef DEBUG5
2581       fprintf (stderr, "\nprocessing fixup:\n");
2582       print_fixup (fixP);
2583 #endif
2584
2585       fragP = fixP->fx_frag;
2586       know (fragP);
2587       where = fixP->fx_where;
2588       place = fragP->fr_literal + where;
2589       size = fixP->fx_size;
2590       add_symbolP = fixP->fx_addsy;
2591 #ifdef TC_VALIDATE_FIX
2592       TC_VALIDATE_FIX (fixP, this_segment_type, skip);
2593 #endif
2594       sub_symbolP = fixP->fx_subsy;
2595       add_number = fixP->fx_offset;
2596       pcrel = fixP->fx_pcrel;
2597       plt = fixP->fx_plt;
2598
2599       if (add_symbolP != NULL
2600           && symbol_mri_common_p (add_symbolP))
2601         {
2602           know (add_symbolP->sy_value.X_op == O_symbol);
2603           add_number += S_GET_VALUE (add_symbolP);
2604           fixP->fx_offset = add_number;
2605           add_symbolP = fixP->fx_addsy =
2606             symbol_get_value_expression (add_symbolP)->X_add_symbol;
2607         }
2608
2609       if (add_symbolP)
2610         add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2611
2612       if (sub_symbolP)
2613         {
2614           resolve_symbol_value (sub_symbolP, finalize_syms);
2615           if (add_symbolP == NULL || add_symbol_segment == absolute_section)
2616             {
2617               if (add_symbolP != NULL)
2618                 {
2619                   add_number += S_GET_VALUE (add_symbolP);
2620                   add_symbolP = NULL;
2621                   fixP->fx_addsy = NULL;
2622                 }
2623
2624               /* It's just -sym.  */
2625               if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
2626                 {
2627                   add_number -= S_GET_VALUE (sub_symbolP);
2628                   fixP->fx_subsy = NULL;
2629                 }
2630               else if (pcrel
2631                        && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2632                 {
2633                   /* Should try converting to a constant.  */
2634                   goto bad_sub_reloc;
2635                 }
2636               else
2637               bad_sub_reloc:
2638                 as_bad_where (fixP->fx_file, fixP->fx_line,
2639                               _("Negative of non-absolute symbol %s"),
2640                               S_GET_NAME (sub_symbolP));
2641             }
2642           else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
2643                    && SEG_NORMAL (add_symbol_segment))
2644             {
2645               /* Difference of 2 symbols from same segment.
2646                  Can't make difference of 2 undefineds: 'value' means
2647                  something different for N_UNDF.  */
2648 #ifdef TC_I960
2649               /* Makes no sense to use the difference of 2 arbitrary symbols
2650                  as the target of a call instruction.  */
2651               if (fixP->fx_tcbit)
2652                 as_bad_where (fixP->fx_file, fixP->fx_line,
2653                               _("callj to difference of 2 symbols"));
2654 #endif /* TC_I960  */
2655               add_number += S_GET_VALUE (add_symbolP) -
2656                 S_GET_VALUE (sub_symbolP);
2657
2658               add_symbolP = NULL;
2659               pcrel = 0;        /* No further pcrel processing.  */
2660
2661               /* Let the target machine make the final determination
2662                  as to whether or not a relocation will be needed to
2663                  handle this fixup.  */
2664               if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
2665                 {
2666                   fixP->fx_pcrel = 0;
2667                   fixP->fx_addsy = NULL;
2668                   fixP->fx_subsy = NULL;
2669                 }
2670             }
2671           else
2672             {
2673               /* Different segments in subtraction.  */
2674               know (!(S_IS_EXTERNAL (sub_symbolP)
2675                       && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2676
2677               if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2678                 add_number -= S_GET_VALUE (sub_symbolP);
2679
2680 #ifdef DIFF_EXPR_OK
2681               else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
2682 #if 0
2683                        /* Do this even if it's already described as
2684                           pc-relative.  For example, on the m68k, an
2685                           operand of "pc@(foo-.-2)" should address
2686                           "foo" in a pc-relative mode.  */
2687                        && pcrel
2688 #endif
2689                        )
2690                 {
2691                   /* Make it pc-relative.  */
2692                   add_number += (MD_PCREL_FROM_SECTION (fixP, this_segment_type)
2693                                  - S_GET_VALUE (sub_symbolP));
2694                   pcrel = 1;
2695                   fixP->fx_pcrel = 1;
2696                   sub_symbolP = 0;
2697                   fixP->fx_subsy = 0;
2698                 }
2699 #endif
2700 #ifdef UNDEFINED_DIFFERENCE_OK
2701               /* The PA needs this for PIC code generation.  We basically
2702                  don't want to do anything if we have the difference of two
2703                  symbols at this point.  */
2704               else if (1)
2705                 {
2706                   /* Leave it alone.  */
2707                 }
2708 #endif
2709 #ifdef BFD_ASSEMBLER
2710               else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2711                        || fixP->fx_r_type == BFD_RELOC_GPREL16)
2712                 {
2713                   /* Leave it alone.  */
2714                 }
2715 #endif
2716               else
2717                 {
2718                   char buf[50];
2719                   sprint_value (buf, fragP->fr_address + where);
2720                   as_bad_where (fixP->fx_file, fixP->fx_line,
2721                                 _("Subtraction of two symbols in different sections \"%s\" {%s section} - \"%s\" {%s section} at file address %s."),
2722                                 S_GET_NAME (add_symbolP),
2723                                 segment_name (S_GET_SEGMENT (add_symbolP)),
2724                                 S_GET_NAME (sub_symbolP),
2725                                 segment_name (S_GET_SEGMENT (sub_symbolP)),
2726                                 buf);
2727                 }
2728             }
2729         }
2730
2731       if (add_symbolP)
2732         {
2733           if (add_symbol_segment == this_segment_type && pcrel && !plt
2734               && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
2735             {
2736               /* This fixup was made when the symbol's segment was
2737                  SEG_UNKNOWN, but it is now in the local segment.
2738                  So we know how to do the address without relocation.  */
2739 #ifdef TC_I960
2740               /* reloc_callj() may replace a 'call' with a 'calls' or a
2741                  'bal', in which cases it modifies *fixP as appropriate.
2742                  In the case of a 'calls', no further work is required,
2743                  and *fixP has been set up to make the rest of the code
2744                  below a no-op.  */
2745               reloc_callj (fixP);
2746 #endif /* TC_I960  */
2747
2748               add_number += S_GET_VALUE (add_symbolP);
2749               add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2750               /* Lie.  Don't want further pcrel processing.  */
2751               pcrel = 0;
2752
2753               /* Let the target machine make the final determination
2754                  as to whether or not a relocation will be needed to
2755                  handle this fixup.  */
2756               if (!TC_FORCE_RELOCATION (fixP))
2757                 {
2758                   fixP->fx_pcrel = 0;
2759                   fixP->fx_addsy = NULL;
2760                 }
2761             }
2762           else
2763             {
2764               if (add_symbol_segment == absolute_section
2765                   && ! pcrel)
2766                 {
2767 #ifdef TC_I960
2768                   /* See comment about reloc_callj() above.  */
2769                   reloc_callj (fixP);
2770 #endif /* TC_I960  */
2771                   add_number += S_GET_VALUE (add_symbolP);
2772
2773                   /* Let the target machine make the final determination
2774                      as to whether or not a relocation will be needed to
2775                      handle this fixup.  */
2776
2777                   if (!TC_FORCE_RELOCATION (fixP))
2778                     {
2779                       fixP->fx_addsy = NULL;
2780                       add_symbolP = NULL;
2781                     }
2782                 }
2783               else if (add_symbol_segment == undefined_section
2784 #ifdef BFD_ASSEMBLER
2785                        || bfd_is_com_section (add_symbol_segment)
2786 #endif
2787                        )
2788                 {
2789 #ifdef TC_I960
2790                   if ((int) fixP->fx_bit_fixP == 13)
2791                     {
2792                       /* This is a COBR instruction.  They have only a
2793                          13-bit displacement and are only to be used
2794                          for local branches: flag as error, don't generate
2795                          relocation.  */
2796                       as_bad_where (fixP->fx_file, fixP->fx_line,
2797                                     _("can't use COBR format with external label"));
2798                       fixP->fx_addsy = NULL;
2799                       fixP->fx_done = 1;
2800                       continue;
2801                     }           /* COBR.  */
2802 #endif /* TC_I960  */
2803
2804 #ifdef OBJ_COFF
2805 #ifdef TE_I386AIX
2806                   if (S_IS_COMMON (add_symbolP))
2807                     add_number += S_GET_VALUE (add_symbolP);
2808 #endif /* TE_I386AIX  */
2809 #endif /* OBJ_COFF  */
2810                   ++seg_reloc_count;
2811                 }
2812               else
2813                 {
2814                   seg_reloc_count++;
2815                   if (TC_FIX_ADJUSTABLE (fixP))
2816                     add_number += S_GET_VALUE (add_symbolP);
2817                 }
2818             }
2819         }
2820
2821       if (pcrel)
2822         {
2823           add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2824           if (add_symbolP == 0)
2825             {
2826 #ifndef BFD_ASSEMBLER
2827               fixP->fx_addsy = &abs_symbol;
2828 #else
2829               fixP->fx_addsy = section_symbol (absolute_section);
2830 #endif
2831               symbol_mark_used_in_reloc (fixP->fx_addsy);
2832               ++seg_reloc_count;
2833             }
2834         }
2835
2836       if (!fixP->fx_done)
2837         {
2838 #ifdef MD_APPLY_FIX3
2839           md_apply_fix3 (fixP, &add_number, this_segment_type);
2840 #else
2841 #ifdef BFD_ASSEMBLER
2842           md_apply_fix (fixP, &add_number);
2843 #else
2844           md_apply_fix (fixP, add_number);
2845 #endif
2846 #endif
2847
2848 #ifndef TC_HANDLES_FX_DONE
2849           /* If the tc-* files haven't been converted, assume it's handling
2850              it the old way, where a null fx_addsy means that the fix has
2851              been applied completely, and no further work is needed.  */
2852           if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2853             fixP->fx_done = 1;
2854 #endif
2855         }
2856
2857       if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
2858         {
2859           if ((size_t) size < sizeof (valueT))
2860             {
2861               valueT mask;
2862
2863               mask = 0;
2864               mask--;           /* Set all bits to one.  */
2865               mask <<= size * 8 - (fixP->fx_signed ? 1 : 0);
2866               if ((add_number & mask) != 0 && (add_number & mask) != mask)
2867                 {
2868                   char buf[50], buf2[50];
2869                   sprint_value (buf, fragP->fr_address + where);
2870                   if (add_number > 1000)
2871                     sprint_value (buf2, add_number);
2872                   else
2873                     sprintf (buf2, "%ld", (long) add_number);
2874                   as_bad_where (fixP->fx_file, fixP->fx_line,
2875                                 _("Value of %s too large for field of %d bytes at %s"),
2876                                 buf2, size, buf);
2877                 } /* Generic error checking.  */
2878             }
2879 #ifdef WARN_SIGNED_OVERFLOW_WORD
2880           /* Warn if a .word value is too large when treated as a signed
2881              number.  We already know it is not too negative.  This is to
2882              catch over-large switches generated by gcc on the 68k.  */
2883           if (!flag_signed_overflow_ok
2884               && size == 2
2885               && add_number > 0x7fff)
2886             as_bad_where (fixP->fx_file, fixP->fx_line,
2887                           _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
2888                           (long) add_number,
2889                           (unsigned long) (fragP->fr_address + where));
2890 #endif
2891         }                       /* Not a bit fix.  */
2892
2893 #ifdef TC_VALIDATE_FIX
2894     skip:  ATTRIBUTE_UNUSED_LABEL
2895       ;
2896 #endif
2897 #ifdef DEBUG5
2898       fprintf (stderr, "result:\n");
2899       print_fixup (fixP);
2900 #endif
2901     }                           /* For each fixS in this segment.  */
2902
2903   TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2904   return seg_reloc_count;
2905 }
2906
2907 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2908
2909 void
2910 number_to_chars_bigendian (buf, val, n)
2911      char *buf;
2912      valueT val;
2913      int n;
2914 {
2915   if ((size_t) n > sizeof (val) || n <= 0)
2916     abort ();
2917   while (n--)
2918     {
2919       buf[n] = val & 0xff;
2920       val >>= 8;
2921     }
2922 }
2923
2924 void
2925 number_to_chars_littleendian (buf, val, n)
2926      char *buf;
2927      valueT val;
2928      int n;
2929 {
2930   if ((size_t) n > sizeof (val) || n <= 0)
2931     abort ();
2932   while (n--)
2933     {
2934       *buf++ = val & 0xff;
2935       val >>= 8;
2936     }
2937 }
2938
2939 void
2940 write_print_statistics (file)
2941      FILE *file;
2942 {
2943   fprintf (file, "fixups: %d\n", n_fixups);
2944 }
2945
2946 /* For debugging.  */
2947 extern int indent_level;
2948
2949 void
2950 print_fixup (fixp)
2951      fixS *fixp;
2952 {
2953   indent_level = 1;
2954   fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2955   if (fixp->fx_pcrel)
2956     fprintf (stderr, " pcrel");
2957   if (fixp->fx_pcrel_adjust)
2958     fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2959   if (fixp->fx_im_disp)
2960     {
2961 #ifdef TC_NS32K
2962       fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2963 #else
2964       fprintf (stderr, " im_disp");
2965 #endif
2966     }
2967   if (fixp->fx_tcbit)
2968     fprintf (stderr, " tcbit");
2969   if (fixp->fx_done)
2970     fprintf (stderr, " done");
2971   fprintf (stderr, "\n    size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2972            fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2973            (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2974 #ifdef BFD_ASSEMBLER
2975   fprintf (stderr, "\n    %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2976            fixp->fx_r_type);
2977 #else
2978 #ifdef NEED_FX_R_TYPE
2979   fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2980 #endif
2981 #endif
2982   if (fixp->fx_addsy)
2983     {
2984       fprintf (stderr, "\n   +<");
2985       print_symbol_value_1 (stderr, fixp->fx_addsy);
2986       fprintf (stderr, ">");
2987     }
2988   if (fixp->fx_subsy)
2989     {
2990       fprintf (stderr, "\n   -<");
2991       print_symbol_value_1 (stderr, fixp->fx_subsy);
2992       fprintf (stderr, ">");
2993     }
2994   fprintf (stderr, "\n");
2995 #ifdef TC_FIX_DATA_PRINT
2996   TC_FIX_DATA_PRINT (stderr, fixp);
2997 #endif
2998 }
This page took 0.202254 seconds and 4 git commands to generate.