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