Back to index

cell-binutils  2.17cvs20070401
write.c
Go to the documentation of this file.
00001 /* write.c - emit .o file
00002    Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
00003    1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
00004    Free Software Foundation, Inc.
00005 
00006    This file is part of GAS, the GNU Assembler.
00007 
00008    GAS is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2, or (at your option)
00011    any later version.
00012 
00013    GAS is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with GAS; see the file COPYING.  If not, write to the Free
00020    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
00021    02110-1301, USA.  */
00022 
00023 /* This thing should be set up to do byteordering correctly.  But...  */
00024 
00025 #include "as.h"
00026 #include "subsegs.h"
00027 #include "obstack.h"
00028 #include "output-file.h"
00029 #include "dwarf2dbg.h"
00030 #include "libbfd.h"
00031 
00032 #ifndef TC_ADJUST_RELOC_COUNT
00033 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
00034 #endif
00035 
00036 #ifndef TC_FORCE_RELOCATION
00037 #define TC_FORCE_RELOCATION(FIX)          \
00038   (generic_force_reloc (FIX))
00039 #endif
00040 
00041 #ifndef TC_FORCE_RELOCATION_ABS
00042 #define TC_FORCE_RELOCATION_ABS(FIX)             \
00043   (TC_FORCE_RELOCATION (FIX))
00044 #endif
00045 
00046 #ifndef TC_FORCE_RELOCATION_LOCAL
00047 #define TC_FORCE_RELOCATION_LOCAL(FIX)           \
00048   (!(FIX)->fx_pcrel                       \
00049    || TC_FORCE_RELOCATION (FIX))
00050 #endif
00051 
00052 #ifndef TC_FORCE_RELOCATION_SUB_SAME
00053 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG)   \
00054   (! SEG_NORMAL (SEG))
00055 #endif
00056 
00057 #ifndef TC_FORCE_RELOCATION_SUB_ABS
00058 #define TC_FORCE_RELOCATION_SUB_ABS(FIX)  0
00059 #endif
00060 
00061 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
00062 #ifdef DIFF_EXPR_OK
00063 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX)       0
00064 #else
00065 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX)       1
00066 #endif
00067 #endif
00068 
00069 #ifndef TC_VALIDATE_FIX_SUB
00070 #ifdef UNDEFINED_DIFFERENCE_OK
00071 /* The PA needs this for PIC code generation.  */
00072 #define TC_VALIDATE_FIX_SUB(FIX) 1
00073 #else
00074 #define TC_VALIDATE_FIX_SUB(FIX)          \
00075   ((FIX)->fx_r_type == BFD_RELOC_GPREL32  \
00076    || (FIX)->fx_r_type == BFD_RELOC_GPREL16)
00077 #endif
00078 #endif
00079 
00080 #ifndef TC_LINKRELAX_FIXUP
00081 #define TC_LINKRELAX_FIXUP(SEG) 1
00082 #endif
00083 
00084 #ifndef MD_APPLY_SYM_VALUE
00085 #define MD_APPLY_SYM_VALUE(FIX) 1
00086 #endif
00087 
00088 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
00089 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
00090 #endif
00091 
00092 #ifndef       MD_PCREL_FROM_SECTION
00093 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
00094 #endif
00095 
00096 #ifndef TC_FAKE_LABEL
00097 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
00098 #endif
00099 
00100 /* Positive values of TC_FX_SIZE_SLACK allow a target to define
00101    fixups that far past the end of a frag.  Having such fixups
00102    is of course most most likely a bug in setting fx_size correctly.
00103    A negative value disables the fixup check entirely, which is
00104    appropriate for something like the Renesas / SuperH SH_COUNT
00105    reloc.  */
00106 #ifndef TC_FX_SIZE_SLACK
00107 #define TC_FX_SIZE_SLACK(FIX) 0
00108 #endif
00109 
00110 /* Used to control final evaluation of expressions.  */
00111 int finalize_syms = 0;
00112 
00113 int symbol_table_frozen;
00114 
00115 symbolS *abs_section_sym;
00116 
00117 /* Remember the value of dot when parsing expressions.  */
00118 addressT dot_value;
00119 
00120 /* Relocs generated by ".reloc" pseudo.  */
00121 struct reloc_list* reloc_list;
00122 
00123 void print_fixup (fixS *);
00124 
00125 /* We generally attach relocs to frag chains.  However, after we have
00126    chained these all together into a segment, any relocs we add after
00127    that must be attached to a segment.  This will include relocs added
00128    in md_estimate_size_for_relax, for example.  */
00129 static int frags_chained = 0;
00130 
00131 static int n_fixups;
00132 
00133 #define RELOC_ENUM enum bfd_reloc_code_real
00134 
00135 /* Create a fixS in obstack 'notes'.  */
00136 
00137 static fixS *
00138 fix_new_internal (fragS *frag,            /* Which frag?  */
00139                 int where,         /* Where in that frag?  */
00140                 int size,          /* 1, 2, or 4 usually.  */
00141                 symbolS *add_symbol,      /* X_add_symbol.  */
00142                 symbolS *sub_symbol,      /* X_op_symbol.  */
00143                 offsetT offset,    /* X_add_number.  */
00144                 int pcrel,         /* TRUE if PC-relative relocation.  */
00145                 RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type.  */)
00146 {
00147   fixS *fixP;
00148 
00149   n_fixups++;
00150 
00151   fixP = obstack_alloc (&notes, sizeof (fixS));
00152 
00153   fixP->fx_frag = frag;
00154   fixP->fx_where = where;
00155   fixP->fx_size = size;
00156   /* We've made fx_size a narrow field; check that it's wide enough.  */
00157   if (fixP->fx_size != size)
00158     {
00159       as_bad (_("field fx_size too small to hold %d"), size);
00160       abort ();
00161     }
00162   fixP->fx_addsy = add_symbol;
00163   fixP->fx_subsy = sub_symbol;
00164   fixP->fx_offset = offset;
00165   fixP->fx_dot_value = dot_value;
00166   fixP->fx_pcrel = pcrel;
00167   fixP->fx_r_type = r_type;
00168   fixP->fx_im_disp = 0;
00169   fixP->fx_pcrel_adjust = 0;
00170   fixP->fx_bit_fixP = 0;
00171   fixP->fx_addnumber = 0;
00172   fixP->fx_tcbit = 0;
00173   fixP->fx_tcbit2 = 0;
00174   fixP->fx_done = 0;
00175   fixP->fx_no_overflow = 0;
00176   fixP->fx_signed = 0;
00177 
00178 #ifdef USING_CGEN
00179   fixP->fx_cgen.insn = NULL;
00180   fixP->fx_cgen.opinfo = 0;
00181 #endif
00182 
00183 #ifdef TC_FIX_TYPE
00184   TC_INIT_FIX_DATA (fixP);
00185 #endif
00186 
00187   as_where (&fixP->fx_file, &fixP->fx_line);
00188 
00189   /* Usually, we want relocs sorted numerically, but while
00190      comparing to older versions of gas that have relocs
00191      reverse sorted, it is convenient to have this compile
00192      time option.  xoxorich.  */
00193   {
00194 
00195     fixS **seg_fix_rootP = (frags_chained
00196                          ? &seg_info (now_seg)->fix_root
00197                          : &frchain_now->fix_root);
00198     fixS **seg_fix_tailP = (frags_chained
00199                          ? &seg_info (now_seg)->fix_tail
00200                          : &frchain_now->fix_tail);
00201 
00202 #ifdef REVERSE_SORT_RELOCS
00203 
00204     fixP->fx_next = *seg_fix_rootP;
00205     *seg_fix_rootP = fixP;
00206 
00207 #else /* REVERSE_SORT_RELOCS  */
00208 
00209     fixP->fx_next = NULL;
00210 
00211     if (*seg_fix_tailP)
00212       (*seg_fix_tailP)->fx_next = fixP;
00213     else
00214       *seg_fix_rootP = fixP;
00215     *seg_fix_tailP = fixP;
00216 
00217 #endif /* REVERSE_SORT_RELOCS  */
00218   }
00219 
00220   return fixP;
00221 }
00222 
00223 /* Create a fixup relative to a symbol (plus a constant).  */
00224 
00225 fixS *
00226 fix_new (fragS *frag,              /* Which frag?  */
00227         int where,                 /* Where in that frag?  */
00228         int size,                  /* 1, 2, or 4 usually.  */
00229         symbolS *add_symbol,       /* X_add_symbol.  */
00230         offsetT offset,            /* X_add_number.  */
00231         int pcrel,                 /* TRUE if PC-relative relocation.  */
00232         RELOC_ENUM r_type          /* Relocation type.  */)
00233 {
00234   return fix_new_internal (frag, where, size, add_symbol,
00235                         (symbolS *) NULL, offset, pcrel, r_type);
00236 }
00237 
00238 /* Create a fixup for an expression.  Currently we only support fixups
00239    for difference expressions.  That is itself more than most object
00240    file formats support anyhow.  */
00241 
00242 fixS *
00243 fix_new_exp (fragS *frag,          /* Which frag?  */
00244             int where,                    /* Where in that frag?  */
00245             int size,                     /* 1, 2, or 4 usually.  */
00246             expressionS *exp,             /* Expression.  */
00247             int pcrel,                    /* TRUE if PC-relative relocation.  */
00248             RELOC_ENUM r_type             /* Relocation type.  */)
00249 {
00250   symbolS *add = NULL;
00251   symbolS *sub = NULL;
00252   offsetT off = 0;
00253 
00254   switch (exp->X_op)
00255     {
00256     case O_absent:
00257       break;
00258 
00259     case O_register:
00260       as_bad (_("register value used as expression"));
00261       break;
00262 
00263     case O_add:
00264       /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
00265         the difference expression cannot immediately be reduced.  */
00266       {
00267        symbolS *stmp = make_expr_symbol (exp);
00268 
00269        exp->X_op = O_symbol;
00270        exp->X_op_symbol = 0;
00271        exp->X_add_symbol = stmp;
00272        exp->X_add_number = 0;
00273 
00274        return fix_new_exp (frag, where, size, exp, pcrel, r_type);
00275       }
00276 
00277     case O_symbol_rva:
00278       add = exp->X_add_symbol;
00279       off = exp->X_add_number;
00280       r_type = BFD_RELOC_RVA;
00281       break;
00282 
00283     case O_uminus:
00284       sub = exp->X_add_symbol;
00285       off = exp->X_add_number;
00286       break;
00287 
00288     case O_subtract:
00289       sub = exp->X_op_symbol;
00290       /* Fall through.  */
00291     case O_symbol:
00292       add = exp->X_add_symbol;
00293       /* Fall through.  */
00294     case O_constant:
00295       off = exp->X_add_number;
00296       break;
00297 
00298     default:
00299       add = make_expr_symbol (exp);
00300       break;
00301     }
00302 
00303   return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
00304 }
00305 
00306 /* Generic function to determine whether a fixup requires a relocation.  */
00307 int
00308 generic_force_reloc (fixS *fix)
00309 {
00310   if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
00311       || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
00312     return 1;
00313 
00314   if (fix->fx_addsy == NULL)
00315     return 0;
00316 
00317   return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
00318 }
00319 
00320 /* Append a string onto another string, bumping the pointer along.  */
00321 void
00322 append (char **charPP, char *fromP, unsigned long length)
00323 {
00324   /* Don't trust memcpy() of 0 chars.  */
00325   if (length == 0)
00326     return;
00327 
00328   memcpy (*charPP, fromP, length);
00329   *charPP += length;
00330 }
00331 
00332 /* This routine records the largest alignment seen for each segment.
00333    If the beginning of the segment is aligned on the worst-case
00334    boundary, all of the other alignments within it will work.  At
00335    least one object format really uses this info.  */
00336 
00337 void
00338 record_alignment (/* Segment to which alignment pertains.  */
00339                 segT seg,
00340                 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
00341                    boundary, 2 => 4-byte boundary, etc.)  */
00342                 int align)
00343 {
00344   if (seg == absolute_section)
00345     return;
00346 
00347   if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
00348     bfd_set_section_alignment (stdoutput, seg, align);
00349 }
00350 
00351 int
00352 get_recorded_alignment (segT seg)
00353 {
00354   if (seg == absolute_section)
00355     return 0;
00356 
00357   return bfd_get_section_alignment (stdoutput, seg);
00358 }
00359 
00360 /* Reset the section indices after removing the gas created sections.  */
00361 
00362 static void
00363 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
00364 {
00365   int *countp = (int *) countparg;
00366 
00367   sec->index = *countp;
00368   ++*countp;
00369 }
00370 
00371 static fragS *
00372 chain_frchains_together_1 (segT section, struct frchain *frchp)
00373 {
00374   fragS dummy, *prev_frag = &dummy;
00375   fixS fix_dummy, *prev_fix = &fix_dummy;
00376 
00377   for (; frchp; frchp = frchp->frch_next)
00378     {
00379       prev_frag->fr_next = frchp->frch_root;
00380       prev_frag = frchp->frch_last;
00381       assert (prev_frag->fr_type != 0);
00382       if (frchp->fix_root != (fixS *) NULL)
00383        {
00384          if (seg_info (section)->fix_root == (fixS *) NULL)
00385            seg_info (section)->fix_root = frchp->fix_root;
00386          prev_fix->fx_next = frchp->fix_root;
00387          seg_info (section)->fix_tail = frchp->fix_tail;
00388          prev_fix = frchp->fix_tail;
00389        }
00390     }
00391   assert (prev_frag->fr_type != 0);
00392   assert (prev_frag != &dummy);
00393   prev_frag->fr_next = 0;
00394   return prev_frag;
00395 }
00396 
00397 static void
00398 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
00399                       segT section,
00400                       void *xxx ATTRIBUTE_UNUSED)
00401 {
00402   segment_info_type *info;
00403 
00404   /* BFD may have introduced its own sections without using
00405      subseg_new, so it is possible that seg_info is NULL.  */
00406   info = seg_info (section);
00407   if (info != (segment_info_type *) NULL)
00408     info->frchainP->frch_last
00409       = chain_frchains_together_1 (section, info->frchainP);
00410 
00411   /* Now that we've chained the frags together, we must add new fixups
00412      to the segment, not to the frag chain.  */
00413   frags_chained = 1;
00414 }
00415 
00416 static void
00417 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
00418 {
00419   switch (fragP->fr_type)
00420     {
00421     case rs_align:
00422     case rs_align_code:
00423     case rs_align_test:
00424     case rs_org:
00425     case rs_space:
00426 #ifdef HANDLE_ALIGN
00427       HANDLE_ALIGN (fragP);
00428 #endif
00429       know (fragP->fr_next != NULL);
00430       fragP->fr_offset = (fragP->fr_next->fr_address
00431                        - fragP->fr_address
00432                        - fragP->fr_fix) / fragP->fr_var;
00433       if (fragP->fr_offset < 0)
00434        {
00435          as_bad_where (fragP->fr_file, fragP->fr_line,
00436                      _("attempt to .org/.space backwards? (%ld)"),
00437                      (long) fragP->fr_offset);
00438          fragP->fr_offset = 0;
00439        }
00440       fragP->fr_type = rs_fill;
00441       break;
00442 
00443     case rs_fill:
00444       break;
00445 
00446     case rs_leb128:
00447       {
00448        valueT value = S_GET_VALUE (fragP->fr_symbol);
00449        int size;
00450 
00451        size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
00452                            fragP->fr_subtype);
00453 
00454        fragP->fr_fix += size;
00455        fragP->fr_type = rs_fill;
00456        fragP->fr_var = 0;
00457        fragP->fr_offset = 0;
00458        fragP->fr_symbol = NULL;
00459       }
00460       break;
00461 
00462     case rs_cfa:
00463       eh_frame_convert_frag (fragP);
00464       break;
00465 
00466     case rs_dwarf2dbg:
00467       dwarf2dbg_convert_frag (fragP);
00468       break;
00469 
00470     case rs_machine_dependent:
00471       md_convert_frag (stdoutput, sec, fragP);
00472 
00473       assert (fragP->fr_next == NULL
00474              || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
00475                 == fragP->fr_fix));
00476 
00477       /* After md_convert_frag, we make the frag into a ".space 0".
00478         md_convert_frag() should set up any fixSs and constants
00479         required.  */
00480       frag_wane (fragP);
00481       break;
00482 
00483 #ifndef WORKING_DOT_WORD
00484     case rs_broken_word:
00485       {
00486        struct broken_word *lie;
00487 
00488        if (fragP->fr_subtype)
00489          {
00490            fragP->fr_fix += md_short_jump_size;
00491            for (lie = (struct broken_word *) (fragP->fr_symbol);
00492                lie && lie->dispfrag == fragP;
00493                lie = lie->next_broken_word)
00494              if (lie->added == 1)
00495               fragP->fr_fix += md_long_jump_size;
00496          }
00497        frag_wane (fragP);
00498       }
00499       break;
00500 #endif
00501 
00502     default:
00503       BAD_CASE (fragP->fr_type);
00504       break;
00505     }
00506 #ifdef md_frag_check
00507   md_frag_check (fragP);
00508 #endif
00509 }
00510 
00511 struct relax_seg_info
00512 {
00513   int pass;
00514   int changed;
00515 };
00516 
00517 static void
00518 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
00519 {
00520   segment_info_type *seginfo = seg_info (sec);
00521   struct relax_seg_info *info = (struct relax_seg_info *) xxx;
00522 
00523   if (seginfo && seginfo->frchainP
00524       && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
00525     info->changed = 1;
00526 }
00527 
00528 static void
00529 size_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
00530 {
00531   flagword flags;
00532   fragS *fragp;
00533   segment_info_type *seginfo;
00534   int x;
00535   valueT size, newsize;
00536 
00537   subseg_change (sec, 0);
00538 
00539   seginfo = seg_info (sec);
00540   if (seginfo && seginfo->frchainP)
00541     {
00542       for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
00543        cvt_frag_to_fill (sec, fragp);
00544       for (fragp = seginfo->frchainP->frch_root;
00545           fragp->fr_next;
00546           fragp = fragp->fr_next)
00547        /* Walk to last elt.  */
00548        ;
00549       size = fragp->fr_address + fragp->fr_fix;
00550     }
00551   else
00552     size = 0;
00553 
00554   flags = bfd_get_section_flags (abfd, sec);
00555 
00556   if (size > 0 && ! seginfo->bss)
00557     flags |= SEC_HAS_CONTENTS;
00558 
00559   flags &= ~SEC_RELOC;
00560   x = bfd_set_section_flags (abfd, sec, flags);
00561   assert (x);
00562 
00563   newsize = md_section_align (sec, size);
00564   x = bfd_set_section_size (abfd, sec, newsize);
00565   assert (x);
00566 
00567   /* If the size had to be rounded up, add some padding in the last
00568      non-empty frag.  */
00569   assert (newsize >= size);
00570   if (size != newsize)
00571     {
00572       fragS *last = seginfo->frchainP->frch_last;
00573       fragp = seginfo->frchainP->frch_root;
00574       while (fragp->fr_next != last)
00575        fragp = fragp->fr_next;
00576       last->fr_address = size;
00577       if ((newsize - size) % fragp->fr_var == 0)
00578        fragp->fr_offset += (newsize - size) / fragp->fr_var;
00579       else
00580        /* If we hit this abort, it's likely due to subsegs_finish not
00581           providing sufficient alignment on the last frag, and the
00582           machine dependent code using alignment frags with fr_var
00583           greater than 1.  */
00584        abort ();
00585     }
00586 
00587 #ifdef tc_frob_section
00588   tc_frob_section (sec);
00589 #endif
00590 #ifdef obj_frob_section
00591   obj_frob_section (sec);
00592 #endif
00593 }
00594 
00595 #ifdef DEBUG2
00596 static void
00597 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
00598 {
00599   segment_info_type *seginfo = seg_info (sec);
00600   fixS *fixp = seginfo->fix_root;
00601 
00602   if (!fixp)
00603     return;
00604 
00605   fprintf (stream, "sec %s relocs:\n", sec->name);
00606   while (fixp)
00607     {
00608       symbolS *s = fixp->fx_addsy;
00609 
00610       fprintf (stream, "  %08lx: type %d ", (unsigned long) fixp,
00611               (int) fixp->fx_r_type);
00612       if (s == NULL)
00613        fprintf (stream, "no sym\n");
00614       else
00615        {
00616          print_symbol_value_1 (stream, s);
00617          fprintf (stream, "\n");
00618        }
00619       fixp = fixp->fx_next;
00620     }
00621 }
00622 #else
00623 #define dump_section_relocs(ABFD,SEC,STREAM)     ((void) 0)
00624 #endif
00625 
00626 #ifndef EMIT_SECTION_SYMBOLS
00627 #define EMIT_SECTION_SYMBOLS 1
00628 #endif
00629 
00630 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
00631    and check for validity.  Convert RELOC_LIST from using U.A fields
00632    to U.B fields.  */
00633 static void
00634 resolve_reloc_expr_symbols (void)
00635 {
00636   struct reloc_list *r;
00637 
00638   for (r = reloc_list; r; r = r->next)
00639     {
00640       expressionS *symval;
00641       symbolS *sym;
00642       bfd_vma offset, addend;
00643       asection *sec;
00644       reloc_howto_type *howto;
00645 
00646       resolve_symbol_value (r->u.a.offset_sym);
00647       symval = symbol_get_value_expression (r->u.a.offset_sym);
00648 
00649       offset = 0;
00650       sym = NULL;
00651       if (symval->X_op == O_constant)
00652        sym = r->u.a.offset_sym;
00653       else if (symval->X_op == O_symbol)
00654        {
00655          sym = symval->X_add_symbol;
00656          offset = symval->X_add_number;
00657          symval = symbol_get_value_expression (symval->X_add_symbol);
00658        }
00659       if (sym == NULL
00660          || symval->X_op != O_constant
00661          || (sec = S_GET_SEGMENT (sym)) == NULL
00662          || !SEG_NORMAL (sec))
00663        {
00664          as_bad_where (r->file, r->line, _("invalid offset expression"));
00665          sec = NULL;
00666        }
00667       else
00668        offset += S_GET_VALUE (sym);
00669 
00670       sym = NULL;
00671       addend = r->u.a.addend;
00672       if (r->u.a.sym != NULL)
00673        {
00674          resolve_symbol_value (r->u.a.sym);
00675          symval = symbol_get_value_expression (r->u.a.sym);
00676          if (symval->X_op == O_constant)
00677            sym = r->u.a.sym;
00678          else if (symval->X_op == O_symbol)
00679            {
00680              sym = symval->X_add_symbol;
00681              addend += symval->X_add_number;
00682              symval = symbol_get_value_expression (symval->X_add_symbol);
00683            }
00684          if (symval->X_op != O_constant)
00685            {
00686              as_bad_where (r->file, r->line, _("invalid reloc expression"));
00687              sec = NULL;
00688            }
00689          else if (sym != NULL)
00690            symbol_mark_used_in_reloc (sym);
00691        }
00692       if (sym == NULL)
00693        {
00694          if (abs_section_sym == NULL)
00695            abs_section_sym = section_symbol (absolute_section);
00696          sym = abs_section_sym;
00697        }
00698 
00699       howto = r->u.a.howto;
00700 
00701       r->u.b.sec = sec;
00702       r->u.b.s = symbol_get_bfdsym (sym);
00703       r->u.b.r.sym_ptr_ptr = &r->u.b.s;
00704       r->u.b.r.address = offset;
00705       r->u.b.r.addend = addend;
00706       r->u.b.r.howto = howto;
00707     }
00708 }
00709 
00710 /* This pass over fixups decides whether symbols can be replaced with
00711    section symbols.  */
00712 
00713 static void
00714 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
00715                  asection *sec,
00716                  void *xxx ATTRIBUTE_UNUSED)
00717 {
00718   segment_info_type *seginfo = seg_info (sec);
00719   fixS *fixp;
00720 
00721   if (seginfo == NULL)
00722     return;
00723 
00724   dump_section_relocs (abfd, sec, stderr);
00725 
00726   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
00727     if (fixp->fx_done)
00728       /* Ignore it.  */
00729       ;
00730     else if (fixp->fx_addsy)
00731       {
00732        symbolS *sym;
00733        asection *symsec;
00734 
00735 #ifdef DEBUG5
00736        fprintf (stderr, "\n\nadjusting fixup:\n");
00737        print_fixup (fixp);
00738 #endif
00739 
00740        sym = fixp->fx_addsy;
00741 
00742        /* All symbols should have already been resolved at this
00743           point.  It is possible to see unresolved expression
00744           symbols, though, since they are not in the regular symbol
00745           table.  */
00746        resolve_symbol_value (sym);
00747 
00748        if (fixp->fx_subsy != NULL)
00749          resolve_symbol_value (fixp->fx_subsy);
00750 
00751        /* If this symbol is equated to an undefined or common symbol,
00752           convert the fixup to being against that symbol.  */
00753        while (symbol_equated_reloc_p (sym)
00754               || S_IS_WEAKREFR (sym))
00755          {
00756            symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
00757            if (sym == newsym)
00758              break;
00759            fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
00760            fixp->fx_addsy = newsym;
00761            sym = newsym;
00762          }
00763 
00764        if (symbol_mri_common_p (sym))
00765          {
00766            fixp->fx_offset += S_GET_VALUE (sym);
00767            fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
00768            continue;
00769          }
00770 
00771        /* If the symbol is undefined, common, weak, or global (ELF
00772           shared libs), we can't replace it with the section symbol.  */
00773        if (S_FORCE_RELOC (fixp->fx_addsy, 1))
00774          continue;
00775 
00776        /* Is there some other (target cpu dependent) reason we can't adjust
00777           this one?  (E.g. relocations involving function addresses on
00778           the PA.  */
00779 #ifdef tc_fix_adjustable
00780        if (! tc_fix_adjustable (fixp))
00781          continue;
00782 #endif
00783 
00784        /* Since we're reducing to section symbols, don't attempt to reduce
00785           anything that's already using one.  */
00786        if (symbol_section_p (sym))
00787          continue;
00788 
00789        symsec = S_GET_SEGMENT (sym);
00790        if (symsec == NULL)
00791          abort ();
00792 
00793        if (bfd_is_abs_section (symsec))
00794          {
00795            /* The fixup_segment routine normally will not use this
00796               symbol in a relocation.  */
00797            continue;
00798          }
00799 
00800        /* Don't try to reduce relocs which refer to non-local symbols
00801           in .linkonce sections.  It can lead to confusion when a
00802           debugging section refers to a .linkonce section.  I hope
00803           this will always be correct.  */
00804        if (symsec != sec && ! S_IS_LOCAL (sym))
00805          {
00806            if ((symsec->flags & SEC_LINK_ONCE) != 0
00807               || (IS_ELF
00808                   /* The GNU toolchain uses an extension for ELF: a
00809                      section beginning with the magic string
00810                      .gnu.linkonce is a linkonce section.  */
00811                   && strncmp (segment_name (symsec), ".gnu.linkonce",
00812                             sizeof ".gnu.linkonce" - 1) == 0))
00813              continue;
00814          }
00815 
00816        /* Never adjust a reloc against local symbol in a merge section
00817           with non-zero addend.  */
00818        if ((symsec->flags & SEC_MERGE) != 0
00819            && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
00820          continue;
00821 
00822        /* Never adjust a reloc against TLS local symbol.  */
00823        if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
00824          continue;
00825 
00826        /* We refetch the segment when calling section_symbol, rather
00827           than using symsec, because S_GET_VALUE may wind up changing
00828           the section when it calls resolve_symbol_value.  */
00829        fixp->fx_offset += S_GET_VALUE (sym);
00830        fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
00831 #ifdef DEBUG5
00832        fprintf (stderr, "\nadjusted fixup:\n");
00833        print_fixup (fixp);
00834 #endif
00835       }
00836 
00837   dump_section_relocs (abfd, sec, stderr);
00838 }
00839 
00840 /* fixup_segment()
00841 
00842    Go through all the fixS's in a segment and see which ones can be
00843    handled now.  (These consist of fixS where we have since discovered
00844    the value of a symbol, or the address of the frag involved.)
00845    For each one, call md_apply_fix to put the fix into the frag data.
00846 
00847    Result is a count of how many relocation structs will be needed to
00848    handle the remaining fixS's that we couldn't completely handle here.
00849    These will be output later by emit_relocations().  */
00850 
00851 static long
00852 fixup_segment (fixS *fixP, segT this_segment)
00853 {
00854   long seg_reloc_count = 0;
00855   valueT add_number;
00856   fragS *fragP;
00857   segT add_symbol_segment = absolute_section;
00858 
00859   if (fixP != NULL && abs_section_sym == NULL)
00860     abs_section_sym = section_symbol (absolute_section);
00861 
00862   /* If the linker is doing the relaxing, we must not do any fixups.
00863 
00864      Well, strictly speaking that's not true -- we could do any that
00865      are PC-relative and don't cross regions that could change size.
00866      And for the i960 we might be able to turn callx/callj into bal
00867      anyways in cases where we know the maximum displacement.  */
00868   if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
00869     {
00870       for (; fixP; fixP = fixP->fx_next)
00871        if (!fixP->fx_done)
00872          {
00873            if (fixP->fx_addsy == NULL)
00874              {
00875               /* There was no symbol required by this relocation.
00876                  However, BFD doesn't really handle relocations
00877                  without symbols well. So fake up a local symbol in
00878                  the absolute section.  */
00879               fixP->fx_addsy = abs_section_sym;
00880              }
00881            symbol_mark_used_in_reloc (fixP->fx_addsy);
00882            if (fixP->fx_subsy != NULL)
00883              symbol_mark_used_in_reloc (fixP->fx_subsy);
00884            seg_reloc_count++;
00885          }
00886       TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
00887       return seg_reloc_count;
00888     }
00889 
00890   for (; fixP; fixP = fixP->fx_next)
00891     {
00892 #ifdef DEBUG5
00893       fprintf (stderr, "\nprocessing fixup:\n");
00894       print_fixup (fixP);
00895 #endif
00896 
00897       fragP = fixP->fx_frag;
00898       know (fragP);
00899 #ifdef TC_VALIDATE_FIX
00900       TC_VALIDATE_FIX (fixP, this_segment, skip);
00901 #endif
00902       add_number = fixP->fx_offset;
00903 
00904       if (fixP->fx_addsy != NULL)
00905        add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
00906 
00907       if (fixP->fx_subsy != NULL)
00908        {
00909          segT sub_symbol_segment;
00910          resolve_symbol_value (fixP->fx_subsy);
00911          sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
00912          if (fixP->fx_addsy != NULL
00913              && sub_symbol_segment == add_symbol_segment
00914              && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
00915            {
00916              add_number += S_GET_VALUE (fixP->fx_addsy);
00917              add_number -= S_GET_VALUE (fixP->fx_subsy);
00918              fixP->fx_offset = add_number;
00919              fixP->fx_addsy = NULL;
00920              fixP->fx_subsy = NULL;
00921 #ifdef TC_M68K
00922              /* See the comment below about 68k weirdness.  */
00923              fixP->fx_pcrel = 0;
00924 #endif
00925            }
00926          else if (sub_symbol_segment == absolute_section
00927                  && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
00928            {
00929              add_number -= S_GET_VALUE (fixP->fx_subsy);
00930              fixP->fx_offset = add_number;
00931              fixP->fx_subsy = NULL;
00932            }
00933          else if (sub_symbol_segment == this_segment
00934                  && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
00935            {
00936              add_number -= S_GET_VALUE (fixP->fx_subsy);
00937              fixP->fx_offset = (add_number + fixP->fx_dot_value
00938                              + fixP->fx_frag->fr_address);
00939 
00940              /* Make it pc-relative.  If the back-end code has not
00941                selected a pc-relative reloc, cancel the adjustment
00942                we do later on all pc-relative relocs.  */
00943              if (0
00944 #ifdef TC_M68K
00945                 /* Do this for m68k even if it's already described
00946                    as pc-relative.  On the m68k, an operand of
00947                    "pc@(foo-.-2)" should address "foo" in a
00948                    pc-relative mode.  */
00949                 || 1
00950 #endif
00951                 || !fixP->fx_pcrel)
00952               add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
00953              fixP->fx_subsy = NULL;
00954              fixP->fx_pcrel = 1;
00955            }
00956          else if (!TC_VALIDATE_FIX_SUB (fixP))
00957            {
00958              as_bad_where (fixP->fx_file, fixP->fx_line,
00959                          _("can't resolve `%s' {%s section} - `%s' {%s section}"),
00960                          fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
00961                          segment_name (add_symbol_segment),
00962                          S_GET_NAME (fixP->fx_subsy),
00963                          segment_name (sub_symbol_segment));
00964            }
00965        }
00966 
00967       if (fixP->fx_addsy)
00968        {
00969          if (add_symbol_segment == this_segment
00970              && !TC_FORCE_RELOCATION_LOCAL (fixP))
00971            {
00972              /* This fixup was made when the symbol's segment was
00973                SEG_UNKNOWN, but it is now in the local segment.
00974                So we know how to do the address without relocation.  */
00975              add_number += S_GET_VALUE (fixP->fx_addsy);
00976              fixP->fx_offset = add_number;
00977              if (fixP->fx_pcrel)
00978               add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
00979              fixP->fx_addsy = NULL;
00980              fixP->fx_pcrel = 0;
00981            }
00982          else if (add_symbol_segment == absolute_section
00983                  && !TC_FORCE_RELOCATION_ABS (fixP))
00984            {
00985              add_number += S_GET_VALUE (fixP->fx_addsy);
00986              fixP->fx_offset = add_number;
00987              fixP->fx_addsy = NULL;
00988            }
00989          else if (add_symbol_segment != undefined_section
00990                  && ! bfd_is_com_section (add_symbol_segment)
00991                  && MD_APPLY_SYM_VALUE (fixP))
00992            add_number += S_GET_VALUE (fixP->fx_addsy);
00993        }
00994 
00995       if (fixP->fx_pcrel)
00996        {
00997          add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
00998          if (!fixP->fx_done && fixP->fx_addsy == NULL)
00999            {
01000              /* There was no symbol required by this relocation.
01001                However, BFD doesn't really handle relocations
01002                without symbols well. So fake up a local symbol in
01003                the absolute section.  */
01004              fixP->fx_addsy = abs_section_sym;
01005            }
01006        }
01007 
01008       if (!fixP->fx_done)
01009        md_apply_fix (fixP, &add_number, this_segment);
01010 
01011       if (!fixP->fx_done)
01012        {
01013          ++seg_reloc_count;
01014          if (fixP->fx_addsy == NULL)
01015            fixP->fx_addsy = abs_section_sym;
01016          symbol_mark_used_in_reloc (fixP->fx_addsy);
01017          if (fixP->fx_subsy != NULL)
01018            symbol_mark_used_in_reloc (fixP->fx_subsy);
01019        }
01020 
01021       if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
01022        {
01023          if (fixP->fx_size < sizeof (valueT))
01024            {
01025              valueT mask;
01026 
01027              mask = 0;
01028              mask--;        /* Set all bits to one.  */
01029              mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
01030              if ((add_number & mask) != 0 && (add_number & mask) != mask)
01031               {
01032                 char buf[50], buf2[50];
01033                 sprint_value (buf, fragP->fr_address + fixP->fx_where);
01034                 if (add_number > 1000)
01035                   sprint_value (buf2, add_number);
01036                 else
01037                   sprintf (buf2, "%ld", (long) add_number);
01038                 as_bad_where (fixP->fx_file, fixP->fx_line,
01039                             _("value of %s too large for field of %d bytes at %s"),
01040                             buf2, fixP->fx_size, buf);
01041               } /* Generic error checking.  */
01042            }
01043 #ifdef WARN_SIGNED_OVERFLOW_WORD
01044          /* Warn if a .word value is too large when treated as a signed
01045             number.  We already know it is not too negative.  This is to
01046             catch over-large switches generated by gcc on the 68k.  */
01047          if (!flag_signed_overflow_ok
01048              && fixP->fx_size == 2
01049              && add_number > 0x7fff)
01050            as_bad_where (fixP->fx_file, fixP->fx_line,
01051                        _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
01052                        (long) add_number,
01053                        (long) (fragP->fr_address + fixP->fx_where));
01054 #endif
01055        }                    /* Not a bit fix.  */
01056 
01057 #ifdef TC_VALIDATE_FIX
01058     skip:  ATTRIBUTE_UNUSED_LABEL
01059       ;
01060 #endif
01061 #ifdef DEBUG5
01062       fprintf (stderr, "result:\n");
01063       print_fixup (fixP);
01064 #endif
01065     }                       /* For each fixS in this segment.  */
01066 
01067   TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
01068   return seg_reloc_count;
01069 }
01070 
01071 static void
01072 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
01073             asection *sec,
01074             void *xxx ATTRIBUTE_UNUSED)
01075 {
01076   segment_info_type *seginfo = seg_info (sec);
01077 
01078   fixup_segment (seginfo->fix_root, sec);
01079 }
01080 
01081 static void
01082 install_reloc (asection *sec, arelent *reloc, fragS *fragp,
01083               char *file, unsigned int line)
01084 {
01085   char *err;
01086   bfd_reloc_status_type s;
01087 
01088   s = bfd_install_relocation (stdoutput, reloc,
01089                            fragp->fr_literal, fragp->fr_address,
01090                            sec, &err);
01091   switch (s)
01092     {
01093     case bfd_reloc_ok:
01094       break;
01095     case bfd_reloc_overflow:
01096       as_bad_where (file, line, _("relocation overflow"));
01097       break;
01098     case bfd_reloc_outofrange:
01099       as_bad_where (file, line, _("relocation out of range"));
01100       break;
01101     default:
01102       as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
01103               file, line, s);
01104     }
01105 }
01106 
01107 static void
01108 write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
01109 {
01110   segment_info_type *seginfo = seg_info (sec);
01111   unsigned int i;
01112   unsigned int n;
01113   struct reloc_list *my_reloc_list, **rp, *r;
01114   arelent **relocs;
01115   fixS *fixp;
01116 
01117   /* If seginfo is NULL, we did not create this section; don't do
01118      anything with it.  */
01119   if (seginfo == NULL)
01120     return;
01121 
01122   n = 0;
01123   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
01124     if (!fixp->fx_done)
01125       n++;
01126 
01127 #ifdef RELOC_EXPANSION_POSSIBLE
01128   n *= MAX_RELOC_EXPANSION;
01129 #endif
01130 
01131   /* Extract relocs for this section from reloc_list.  */
01132   rp = &reloc_list;
01133   my_reloc_list = NULL;
01134   while ((r = *rp) != NULL)
01135     {
01136       if (r->u.b.sec == sec)
01137        {
01138          *rp = r->next;
01139          r->next = my_reloc_list;
01140          my_reloc_list = r;
01141          n++;
01142        }
01143       else
01144        rp = &r->next;
01145     }
01146 
01147   relocs = xcalloc (n, sizeof (arelent *));
01148 
01149   i = 0;
01150   for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
01151     {
01152       int j;
01153       int fx_size, slack;
01154       offsetT loc;
01155 
01156       if (fixp->fx_done)
01157        continue;
01158 
01159       fx_size = fixp->fx_size;
01160       slack = TC_FX_SIZE_SLACK (fixp);
01161       if (slack > 0)
01162        fx_size = fx_size > slack ? fx_size - slack : 0;
01163       loc = fixp->fx_where + fx_size;
01164       if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
01165        as_bad_where (fixp->fx_file, fixp->fx_line,
01166                     _("internal error: fixup not contained within frag"));
01167 
01168 #ifndef RELOC_EXPANSION_POSSIBLE
01169       {
01170        arelent *reloc = tc_gen_reloc (sec, fixp);
01171 
01172        if (!reloc)
01173          continue;
01174        relocs[i++] = reloc;
01175        j = 1;
01176       }
01177 #else
01178       {
01179        arelent **reloc = tc_gen_reloc (sec, fixp);
01180 
01181        for (j = 0; reloc[j]; j++)
01182          relocs[i++] = reloc[j];
01183       }
01184 #endif
01185 
01186       for ( ; j != 0; --j)
01187        install_reloc (sec, relocs[i - j], fixp->fx_frag,
01188                      fixp->fx_file, fixp->fx_line);
01189     }
01190   n = i;
01191 
01192 #ifdef DEBUG4
01193   {
01194     unsigned int i, j, nsyms;
01195     asymbol **sympp;
01196     sympp = bfd_get_outsymbols (stdoutput);
01197     nsyms = bfd_get_symcount (stdoutput);
01198     for (i = 0; i < n; i++)
01199       if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
01200        {
01201          for (j = 0; j < nsyms; j++)
01202            if (sympp[j] == *relocs[i]->sym_ptr_ptr)
01203              break;
01204          if (j == nsyms)
01205            abort ();
01206        }
01207   }
01208 #endif
01209 
01210   for (r = my_reloc_list; r != NULL; r = r->next)
01211     {
01212       fragS *f;
01213       for (f = seginfo->frchainP->frch_root; f; f = f->fr_next)
01214        if (f->fr_address <= r->u.b.r.address
01215            && r->u.b.r.address < f->fr_address + f->fr_fix)
01216          break;
01217       if (f == NULL)
01218        as_bad_where (r->file, r->line,
01219                     _("reloc not within (fixed part of) section"));
01220       else
01221        {
01222          relocs[n++] = &r->u.b.r;
01223          install_reloc (sec, &r->u.b.r, f, r->file, r->line);
01224        }
01225     }
01226 
01227   if (n)
01228     {
01229       flagword flags = bfd_get_section_flags (abfd, sec);
01230       flags |= SEC_RELOC;
01231       bfd_set_section_flags (abfd, sec, flags);
01232       bfd_set_reloc (stdoutput, sec, relocs, n);
01233     }
01234 
01235 #ifdef SET_SECTION_RELOCS
01236   SET_SECTION_RELOCS (sec, relocs, n);
01237 #endif
01238 
01239 #ifdef DEBUG3
01240   {
01241     unsigned int i;
01242     arelent *r;
01243     asymbol *s;
01244     fprintf (stderr, "relocs for sec %s\n", sec->name);
01245     for (i = 0; i < n; i++)
01246       {
01247        r = relocs[i];
01248        s = *r->sym_ptr_ptr;
01249        fprintf (stderr, "  reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
01250                i, r, (unsigned long)r->address, s->name, (unsigned long)r->addend);
01251       }
01252   }
01253 #endif
01254 }
01255 
01256 static void
01257 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
01258               asection *sec,
01259               void *xxx ATTRIBUTE_UNUSED)
01260 {
01261   segment_info_type *seginfo = seg_info (sec);
01262   addressT offset = 0;
01263   fragS *f;
01264 
01265   /* Write out the frags.  */
01266   if (seginfo == NULL
01267       || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
01268     return;
01269 
01270   for (f = seginfo->frchainP->frch_root;
01271        f;
01272        f = f->fr_next)
01273     {
01274       int x;
01275       addressT fill_size;
01276       char *fill_literal;
01277       offsetT count;
01278 
01279       assert (f->fr_type == rs_fill);
01280       if (f->fr_fix)
01281        {
01282          x = bfd_set_section_contents (stdoutput, sec,
01283                                    f->fr_literal, (file_ptr) offset,
01284                                    (bfd_size_type) f->fr_fix);
01285          if (!x)
01286            as_fatal (_("can't write %s: %s"), stdoutput->filename,
01287                     bfd_errmsg (bfd_get_error ()));
01288          offset += f->fr_fix;
01289        }
01290       fill_literal = f->fr_literal + f->fr_fix;
01291       fill_size = f->fr_var;
01292       count = f->fr_offset;
01293       assert (count >= 0);
01294       if (fill_size && count)
01295        {
01296          char buf[256];
01297          if (fill_size > sizeof (buf))
01298            {
01299              /* Do it the old way. Can this ever happen?  */
01300              while (count--)
01301               {
01302                 x = bfd_set_section_contents (stdoutput, sec,
01303                                           fill_literal,
01304                                           (file_ptr) offset,
01305                                           (bfd_size_type) fill_size);
01306                 if (!x)
01307                   as_fatal (_("can't write %s: %s"), stdoutput->filename,
01308                            bfd_errmsg (bfd_get_error ()));
01309                 offset += fill_size;
01310               }
01311            }
01312          else
01313            {
01314              /* Build a buffer full of fill objects and output it as
01315                often as necessary. This saves on the overhead of
01316                potentially lots of bfd_set_section_contents calls.  */
01317              int n_per_buf, i;
01318              if (fill_size == 1)
01319               {
01320                 n_per_buf = sizeof (buf);
01321                 memset (buf, *fill_literal, n_per_buf);
01322               }
01323              else
01324               {
01325                 char *bufp;
01326                 n_per_buf = sizeof (buf) / fill_size;
01327                 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
01328                   memcpy (bufp, fill_literal, fill_size);
01329               }
01330              for (; count > 0; count -= n_per_buf)
01331               {
01332                 n_per_buf = n_per_buf > count ? count : n_per_buf;
01333                 x = bfd_set_section_contents
01334                   (stdoutput, sec, buf, (file_ptr) offset,
01335                    (bfd_size_type) n_per_buf * fill_size);
01336                 if (!x)
01337                   as_fatal (_("cannot write to output file"));
01338                 offset += n_per_buf * fill_size;
01339               }
01340            }
01341        }
01342     }
01343 }
01344 
01345 static void
01346 merge_data_into_text (void)
01347 {
01348   seg_info (text_section)->frchainP->frch_last->fr_next =
01349     seg_info (data_section)->frchainP->frch_root;
01350   seg_info (text_section)->frchainP->frch_last =
01351     seg_info (data_section)->frchainP->frch_last;
01352   seg_info (data_section)->frchainP = 0;
01353 }
01354 
01355 static void
01356 set_symtab (void)
01357 {
01358   int nsyms;
01359   asymbol **asympp;
01360   symbolS *symp;
01361   bfd_boolean result;
01362 
01363   /* Count symbols.  We can't rely on a count made by the loop in
01364      write_object_file, because *_frob_file may add a new symbol or
01365      two.  */
01366   nsyms = 0;
01367   for (symp = symbol_rootP; symp; symp = symbol_next (symp))
01368     nsyms++;
01369 
01370   if (nsyms)
01371     {
01372       int i;
01373       bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
01374 
01375       asympp = bfd_alloc (stdoutput, amt);
01376       symp = symbol_rootP;
01377       for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
01378        {
01379          asympp[i] = symbol_get_bfdsym (symp);
01380          symbol_mark_written (symp);
01381        }
01382     }
01383   else
01384     asympp = 0;
01385   result = bfd_set_symtab (stdoutput, asympp, nsyms);
01386   assert (result);
01387   symbol_table_frozen = 1;
01388 }
01389 
01390 /* Finish the subsegments.  After every sub-segment, we fake an
01391    ".align ...".  This conforms to BSD4.2 brane-damage.  We then fake
01392    ".fill 0" because that is the kind of frag that requires least
01393    thought.  ".align" frags like to have a following frag since that
01394    makes calculating their intended length trivial.  */
01395 
01396 #ifndef SUB_SEGMENT_ALIGN
01397 #ifdef HANDLE_ALIGN
01398 /* The last subsegment gets an alignment corresponding to the alignment
01399    of the section.  This allows proper nop-filling at the end of
01400    code-bearing sections.  */
01401 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN)                               \
01402   (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0)
01403 #else
01404 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
01405 #endif
01406 #endif
01407 
01408 void
01409 subsegs_finish (void)
01410 {
01411   struct frchain *frchainP;
01412   asection *s;
01413 
01414   for (s = stdoutput->sections; s; s = s->next)
01415     {
01416       segment_info_type *seginfo = seg_info (s);
01417       if (!seginfo)
01418        continue;
01419 
01420       for (frchainP = seginfo->frchainP;
01421           frchainP != NULL;
01422           frchainP = frchainP->frch_next)
01423        {
01424          int alignment = 0;
01425 
01426          subseg_set (s, frchainP->frch_subseg);
01427 
01428          /* This now gets called even if we had errors.  In that case,
01429             any alignment is meaningless, and, moreover, will look weird
01430             if we are generating a listing.  */
01431          if (!had_errors ())
01432            {
01433              alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
01434              if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
01435                 && now_seg->entsize)
01436               {
01437                 unsigned int entsize = now_seg->entsize;
01438                 int entalign = 0;
01439 
01440                 while ((entsize & 1) == 0)
01441                   {
01442                     ++entalign;
01443                     entsize >>= 1;
01444                   }
01445                 if (entalign > alignment)
01446                   alignment = entalign;
01447               }
01448            }
01449 
01450          if (subseg_text_p (now_seg))
01451            frag_align_code (alignment, 0);
01452          else
01453            frag_align (alignment, 0, 0);
01454 
01455          /* frag_align will have left a new frag.
01456             Use this last frag for an empty ".fill".
01457 
01458             For this segment ...
01459             Create a last frag. Do not leave a "being filled in frag".  */
01460          frag_wane (frag_now);
01461          frag_now->fr_fix = 0;
01462          know (frag_now->fr_next == NULL);
01463        }
01464     }
01465 }
01466 
01467 /* Write the object file.  */
01468 
01469 void
01470 write_object_file (void)
01471 {
01472   struct relax_seg_info rsi;
01473 #ifndef WORKING_DOT_WORD
01474   fragS *fragP;                    /* Track along all frags.  */
01475 #endif
01476 
01477   /* Do we really want to write it?  */
01478   {
01479     int n_warns, n_errs;
01480     n_warns = had_warnings ();
01481     n_errs = had_errors ();
01482     /* The -Z flag indicates that an object file should be generated,
01483        regardless of warnings and errors.  */
01484     if (flag_always_generate_output)
01485       {
01486        if (n_warns || n_errs)
01487          as_warn (_("%d error%s, %d warning%s, generating bad object file"),
01488                  n_errs, n_errs == 1 ? "" : "s",
01489                  n_warns, n_warns == 1 ? "" : "s");
01490       }
01491     else
01492       {
01493        if (n_errs)
01494          as_fatal (_("%d error%s, %d warning%s, no object file generated"),
01495                   n_errs, n_errs == 1 ? "" : "s",
01496                   n_warns, n_warns == 1 ? "" : "s");
01497       }
01498   }
01499 
01500 #ifdef OBJ_VMS
01501   /* Under VMS we try to be compatible with VAX-11 "C".  Thus, we call
01502      a routine to check for the definition of the procedure "_main",
01503      and if so -- fix it up so that it can be program entry point.  */
01504   vms_check_for_main ();
01505 #endif /* OBJ_VMS  */
01506 
01507   /* From now on, we don't care about sub-segments.  Build one frag chain
01508      for each segment. Linked thru fr_next.  */
01509 
01510   /* Remove the sections created by gas for its own purposes.  */
01511   {
01512     int i;
01513 
01514     bfd_section_list_remove (stdoutput, reg_section);
01515     bfd_section_list_remove (stdoutput, expr_section);
01516     stdoutput->section_count -= 2;
01517     i = 0;
01518     bfd_map_over_sections (stdoutput, renumber_sections, &i);
01519   }
01520 
01521   bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
01522 
01523   /* We have two segments. If user gave -R flag, then we must put the
01524      data frags into the text segment. Do this before relaxing so
01525      we know to take advantage of -R and make shorter addresses.  */
01526   if (flag_readonly_data_in_text)
01527     {
01528       merge_data_into_text ();
01529     }
01530 
01531   rsi.pass = 0;
01532   while (1)
01533     {
01534 #ifndef WORKING_DOT_WORD
01535       /* We need to reset the markers in the broken word list and
01536         associated frags between calls to relax_segment (via
01537         relax_seg).  Since the broken word list is global, we do it
01538         once per round, rather than locally in relax_segment for each
01539         segment.  */
01540       struct broken_word *brokp;
01541 
01542       for (brokp = broken_words;
01543           brokp != (struct broken_word *) NULL;
01544           brokp = brokp->next_broken_word)
01545        {
01546          brokp->added = 0;
01547 
01548          if (brokp->dispfrag != (fragS *) NULL
01549              && brokp->dispfrag->fr_type == rs_broken_word)
01550            brokp->dispfrag->fr_subtype = 0;
01551        }
01552 #endif
01553 
01554       rsi.changed = 0;
01555       bfd_map_over_sections (stdoutput, relax_seg, &rsi);
01556       rsi.pass++;
01557       if (!rsi.changed)
01558        break;
01559     }
01560 
01561   /* Note - Most ports will use the default value of
01562      TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1.  This will force
01563      local symbols to be resolved, removing their frag information.
01564      Some ports however, will not have finished relaxing all of
01565      their frags and will still need the local symbol frag
01566      information.  These ports can set
01567      TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0.  */
01568   finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
01569 
01570   bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
01571 
01572   /* Relaxation has completed.  Freeze all syms.  */
01573   finalize_syms = 1;
01574 
01575 #ifdef md_post_relax_hook
01576   md_post_relax_hook;
01577 #endif
01578 
01579 #ifndef WORKING_DOT_WORD
01580   {
01581     struct broken_word *lie;
01582     struct broken_word **prevP;
01583 
01584     prevP = &broken_words;
01585     for (lie = broken_words; lie; lie = lie->next_broken_word)
01586       if (!lie->added)
01587        {
01588          expressionS exp;
01589 
01590          subseg_change (lie->seg, lie->subseg);
01591          exp.X_op = O_subtract;
01592          exp.X_add_symbol = lie->add;
01593          exp.X_op_symbol = lie->sub;
01594          exp.X_add_number = lie->addnum;
01595 #ifdef TC_CONS_FIX_NEW
01596          TC_CONS_FIX_NEW (lie->frag,
01597                         lie->word_goes_here - lie->frag->fr_literal,
01598                         2, &exp);
01599 #else
01600          fix_new_exp (lie->frag,
01601                      lie->word_goes_here - lie->frag->fr_literal,
01602                      2, &exp, 0, BFD_RELOC_16);
01603 #endif
01604          *prevP = lie->next_broken_word;
01605        }
01606       else
01607        prevP = &(lie->next_broken_word);
01608 
01609     for (lie = broken_words; lie;)
01610       {
01611        struct broken_word *untruth;
01612        char *table_ptr;
01613        addressT table_addr;
01614        addressT from_addr, to_addr;
01615        int n, m;
01616 
01617        subseg_change (lie->seg, lie->subseg);
01618        fragP = lie->dispfrag;
01619 
01620        /* Find out how many broken_words go here.  */
01621        n = 0;
01622        for (untruth = lie;
01623             untruth && untruth->dispfrag == fragP;
01624             untruth = untruth->next_broken_word)
01625          if (untruth->added == 1)
01626            n++;
01627 
01628        table_ptr = lie->dispfrag->fr_opcode;
01629        table_addr = (lie->dispfrag->fr_address
01630                     + (table_ptr - lie->dispfrag->fr_literal));
01631        /* Create the jump around the long jumps.  This is a short
01632           jump from table_ptr+0 to table_ptr+n*long_jump_size.  */
01633        from_addr = table_addr;
01634        to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
01635        md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
01636                            lie->add);
01637        table_ptr += md_short_jump_size;
01638        table_addr += md_short_jump_size;
01639 
01640        for (m = 0;
01641             lie && lie->dispfrag == fragP;
01642             m++, lie = lie->next_broken_word)
01643          {
01644            if (lie->added == 2)
01645              continue;
01646            /* Patch the jump table.  */
01647            /* This is the offset from ??? to table_ptr+0.  */
01648            to_addr = table_addr - S_GET_VALUE (lie->sub);
01649 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
01650            TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
01651 #endif
01652            md_number_to_chars (lie->word_goes_here, to_addr, 2);
01653            for (untruth = lie->next_broken_word;
01654                untruth && untruth->dispfrag == fragP;
01655                untruth = untruth->next_broken_word)
01656              {
01657               if (untruth->use_jump == lie)
01658                 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
01659              }
01660 
01661            /* Install the long jump.  */
01662            /* This is a long jump from table_ptr+0 to the final target.  */
01663            from_addr = table_addr;
01664            to_addr = S_GET_VALUE (lie->add) + lie->addnum;
01665            md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
01666                              lie->add);
01667            table_ptr += md_long_jump_size;
01668            table_addr += md_long_jump_size;
01669          }
01670       }
01671   }
01672 #endif /* not WORKING_DOT_WORD  */
01673 
01674   /* Resolve symbol values.  This needs to be done before processing
01675      the relocations.  */
01676   if (symbol_rootP)
01677     {
01678       symbolS *symp;
01679 
01680       for (symp = symbol_rootP; symp; symp = symbol_next (symp))
01681        resolve_symbol_value (symp);
01682     }
01683   resolve_local_symbol_values ();
01684   resolve_reloc_expr_symbols ();
01685 
01686   PROGRESS (1);
01687 
01688 #ifdef tc_frob_file_before_adjust
01689   tc_frob_file_before_adjust ();
01690 #endif
01691 #ifdef obj_frob_file_before_adjust
01692   obj_frob_file_before_adjust ();
01693 #endif
01694 
01695   bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
01696 
01697 #ifdef tc_frob_file_before_fix
01698   tc_frob_file_before_fix ();
01699 #endif
01700 #ifdef obj_frob_file_before_fix
01701   obj_frob_file_before_fix ();
01702 #endif
01703 
01704   bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
01705 
01706   /* Set up symbol table, and write it out.  */
01707   if (symbol_rootP)
01708     {
01709       symbolS *symp;
01710       bfd_boolean skip_next_symbol = FALSE;
01711 
01712       for (symp = symbol_rootP; symp; symp = symbol_next (symp))
01713        {
01714          int punt = 0;
01715          const char *name;
01716 
01717          if (skip_next_symbol)
01718            {
01719              /* Don't do anything besides moving the value of the
01720                symbol from the GAS value-field to the BFD value-field.  */
01721              symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
01722              skip_next_symbol = FALSE;
01723              continue;
01724            }
01725 
01726          if (symbol_mri_common_p (symp))
01727            {
01728              if (S_IS_EXTERNAL (symp))
01729               as_bad (_("%s: global symbols not supported in common sections"),
01730                      S_GET_NAME (symp));
01731              symbol_remove (symp, &symbol_rootP, &symbol_lastP);
01732              continue;
01733            }
01734 
01735          name = S_GET_NAME (symp);
01736          if (name)
01737            {
01738              const char *name2 =
01739               decode_local_label_name ((char *) S_GET_NAME (symp));
01740              /* They only differ if `name' is a fb or dollar local
01741                label name.  */
01742              if (name2 != name && ! S_IS_DEFINED (symp))
01743               as_bad (_("local label `%s' is not defined"), name2);
01744            }
01745 
01746          /* Do it again, because adjust_reloc_syms might introduce
01747             more symbols.  They'll probably only be section symbols,
01748             but they'll still need to have the values computed.  */
01749          resolve_symbol_value (symp);
01750 
01751          /* Skip symbols which were equated to undefined or common
01752             symbols.  */
01753          if (symbol_equated_reloc_p (symp)
01754              || S_IS_WEAKREFR (symp))
01755            {
01756              const char *name = S_GET_NAME (symp);
01757              if (S_IS_COMMON (symp)
01758                 && !TC_FAKE_LABEL (name)
01759                 && !S_IS_WEAKREFR (symp)
01760                 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
01761               {
01762                 expressionS *e = symbol_get_value_expression (symp);
01763                 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
01764                        name, S_GET_NAME (e->X_add_symbol));
01765               }
01766              symbol_remove (symp, &symbol_rootP, &symbol_lastP);
01767              continue;
01768            }
01769 
01770 #ifdef obj_frob_symbol
01771          obj_frob_symbol (symp, punt);
01772 #endif
01773 #ifdef tc_frob_symbol
01774          if (! punt || symbol_used_in_reloc_p (symp))
01775            tc_frob_symbol (symp, punt);
01776 #endif
01777 
01778          /* If we don't want to keep this symbol, splice it out of
01779             the chain now.  If EMIT_SECTION_SYMBOLS is 0, we never
01780             want section symbols.  Otherwise, we skip local symbols
01781             and symbols that the frob_symbol macros told us to punt,
01782             but we keep such symbols if they are used in relocs.  */
01783          if (symp == abs_section_sym
01784              || (! EMIT_SECTION_SYMBOLS
01785                 && symbol_section_p (symp))
01786              /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
01787                opposites.  Sometimes the former checks flags and the
01788                latter examines the name...  */
01789              || (!S_IS_EXTERNAL (symp)
01790                 && (punt || S_IS_LOCAL (symp) ||
01791                     (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
01792                 && ! symbol_used_in_reloc_p (symp)))
01793            {
01794              symbol_remove (symp, &symbol_rootP, &symbol_lastP);
01795 
01796              /* After symbol_remove, symbol_next(symp) still returns
01797                the one that came after it in the chain.  So we don't
01798                need to do any extra cleanup work here.  */
01799              continue;
01800            }
01801 
01802          /* Make sure we really got a value for the symbol.  */
01803          if (! symbol_resolved_p (symp))
01804            {
01805              as_bad (_("can't resolve value for symbol `%s'"),
01806                     S_GET_NAME (symp));
01807              symbol_mark_resolved (symp);
01808            }
01809 
01810          /* Set the value into the BFD symbol.  Up til now the value
01811             has only been kept in the gas symbolS struct.  */
01812          symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
01813 
01814          /* A warning construct is a warning symbol followed by the
01815             symbol warned about.  Don't let anything object-format or
01816             target-specific muck with it; it's ready for output.  */
01817          if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
01818            skip_next_symbol = TRUE;
01819        }
01820     }
01821 
01822   PROGRESS (1);
01823 
01824   /* Now do any format-specific adjustments to the symbol table, such
01825      as adding file symbols.  */
01826 #ifdef tc_adjust_symtab
01827   tc_adjust_symtab ();
01828 #endif
01829 #ifdef obj_adjust_symtab
01830   obj_adjust_symtab ();
01831 #endif
01832 
01833   /* Now that all the sizes are known, and contents correct, we can
01834      start writing to the file.  */
01835   set_symtab ();
01836 
01837   /* If *_frob_file changes the symbol value at this point, it is
01838      responsible for moving the changed value into symp->bsym->value
01839      as well.  Hopefully all symbol value changing can be done in
01840      *_frob_symbol.  */
01841 #ifdef tc_frob_file
01842   tc_frob_file ();
01843 #endif
01844 #ifdef obj_frob_file
01845   obj_frob_file ();
01846 #endif
01847 
01848   bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
01849 
01850 #ifdef tc_frob_file_after_relocs
01851   tc_frob_file_after_relocs ();
01852 #endif
01853 #ifdef obj_frob_file_after_relocs
01854   obj_frob_file_after_relocs ();
01855 #endif
01856 
01857   bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
01858 }
01859 
01860 #ifdef TC_GENERIC_RELAX_TABLE
01861 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE.  */
01862 
01863 long
01864 relax_frag (segT segment, fragS *fragP, long stretch)
01865 {
01866   const relax_typeS *this_type;
01867   const relax_typeS *start_type;
01868   relax_substateT next_state;
01869   relax_substateT this_state;
01870   offsetT growth;
01871   offsetT aim;
01872   addressT target;
01873   addressT address;
01874   symbolS *symbolP;
01875   const relax_typeS *table;
01876 
01877   target = fragP->fr_offset;
01878   address = fragP->fr_address;
01879   table = TC_GENERIC_RELAX_TABLE;
01880   this_state = fragP->fr_subtype;
01881   start_type = this_type = table + this_state;
01882   symbolP = fragP->fr_symbol;
01883 
01884   if (symbolP)
01885     {
01886       fragS *sym_frag;
01887 
01888       sym_frag = symbol_get_frag (symbolP);
01889 
01890 #ifndef DIFF_EXPR_OK
01891       know (sym_frag != NULL);
01892 #endif
01893       know (S_GET_SEGMENT (symbolP) != absolute_section
01894            || sym_frag == &zero_address_frag);
01895       target += S_GET_VALUE (symbolP);
01896 
01897       /* If frag has yet to be reached on this pass,
01898         assume it will move by STRETCH just as we did.
01899         If this is not so, it will be because some frag
01900         between grows, and that will force another pass.  */
01901 
01902       if (stretch != 0
01903          && sym_frag->relax_marker != fragP->relax_marker
01904          && S_GET_SEGMENT (symbolP) == segment)
01905        {
01906          target += stretch;
01907        }
01908     }
01909 
01910   aim = target - address - fragP->fr_fix;
01911 #ifdef TC_PCREL_ADJUST
01912   /* Currently only the ns32k family needs this.  */
01913   aim += TC_PCREL_ADJUST (fragP);
01914 #endif
01915 
01916 #ifdef md_prepare_relax_scan
01917   /* Formerly called M68K_AIM_KLUDGE.  */
01918   md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
01919 #endif
01920 
01921   if (aim < 0)
01922     {
01923       /* Look backwards.  */
01924       for (next_state = this_type->rlx_more; next_state;)
01925        if (aim >= this_type->rlx_backward)
01926          next_state = 0;
01927        else
01928          {
01929            /* Grow to next state.  */
01930            this_state = next_state;
01931            this_type = table + this_state;
01932            next_state = this_type->rlx_more;
01933          }
01934     }
01935   else
01936     {
01937       /* Look forwards.  */
01938       for (next_state = this_type->rlx_more; next_state;)
01939        if (aim <= this_type->rlx_forward)
01940          next_state = 0;
01941        else
01942          {
01943            /* Grow to next state.  */
01944            this_state = next_state;
01945            this_type = table + this_state;
01946            next_state = this_type->rlx_more;
01947          }
01948     }
01949 
01950   growth = this_type->rlx_length - start_type->rlx_length;
01951   if (growth != 0)
01952     fragP->fr_subtype = this_state;
01953   return growth;
01954 }
01955 
01956 #endif /* defined (TC_GENERIC_RELAX_TABLE)  */
01957 
01958 /* Relax_align. Advance location counter to next address that has 'alignment'
01959    lowest order bits all 0s, return size of adjustment made.  */
01960 static relax_addressT
01961 relax_align (register relax_addressT address,    /* Address now.  */
01962             register int alignment /* Alignment (binary).  */)
01963 {
01964   relax_addressT mask;
01965   relax_addressT new_address;
01966 
01967   mask = ~((~0) << alignment);
01968   new_address = (address + mask) & (~mask);
01969 #ifdef LINKER_RELAXING_SHRINKS_ONLY
01970   if (linkrelax)
01971     /* We must provide lots of padding, so the linker can discard it
01972        when needed.  The linker will not add extra space, ever.  */
01973     new_address += (1 << alignment);
01974 #endif
01975   return (new_address - address);
01976 }
01977 
01978 /* Now we have a segment, not a crowd of sub-segments, we can make
01979    fr_address values.
01980 
01981    Relax the frags.
01982 
01983    After this, all frags in this segment have addresses that are correct
01984    within the segment. Since segments live in different file addresses,
01985    these frag addresses may not be the same as final object-file
01986    addresses.  */
01987 
01988 int
01989 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
01990 {
01991   unsigned long frag_count;
01992   struct frag *fragP;
01993   relax_addressT address;
01994   int ret;
01995 
01996   /* In case md_estimate_size_before_relax() wants to make fixSs.  */
01997   subseg_change (segment, 0);
01998 
01999   /* For each frag in segment: count and store  (a 1st guess of)
02000      fr_address.  */
02001   address = 0;
02002   for (frag_count = 0, fragP = segment_frag_root;
02003        fragP;
02004        fragP = fragP->fr_next, frag_count ++)
02005     {
02006       fragP->relax_marker = 0;
02007       fragP->fr_address = address;
02008       address += fragP->fr_fix;
02009 
02010       switch (fragP->fr_type)
02011        {
02012        case rs_fill:
02013          address += fragP->fr_offset * fragP->fr_var;
02014          break;
02015 
02016        case rs_align:
02017        case rs_align_code:
02018        case rs_align_test:
02019          {
02020            addressT offset = relax_align (address, (int) fragP->fr_offset);
02021 
02022            if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
02023              offset = 0;
02024 
02025            if (offset % fragP->fr_var != 0)
02026              {
02027               as_bad_where (fragP->fr_file, fragP->fr_line,
02028                            _("alignment padding (%lu bytes) not a multiple of %ld"),
02029                            (unsigned long) offset, (long) fragP->fr_var);
02030               offset -= (offset % fragP->fr_var);
02031              }
02032 
02033            address += offset;
02034          }
02035          break;
02036 
02037        case rs_org:
02038        case rs_space:
02039          /* Assume .org is nugatory. It will grow with 1st relax.  */
02040          break;
02041 
02042        case rs_machine_dependent:
02043          /* If fr_symbol is an expression, this call to
02044             resolve_symbol_value sets up the correct segment, which will
02045             likely be needed in md_estimate_size_before_relax.  */
02046          if (fragP->fr_symbol)
02047            resolve_symbol_value (fragP->fr_symbol);
02048 
02049          address += md_estimate_size_before_relax (fragP, segment);
02050          break;
02051 
02052 #ifndef WORKING_DOT_WORD
02053          /* Broken words don't concern us yet.  */
02054        case rs_broken_word:
02055          break;
02056 #endif
02057 
02058        case rs_leb128:
02059          /* Initial guess is always 1; doing otherwise can result in
02060             stable solutions that are larger than the minimum.  */
02061          address += fragP->fr_offset = 1;
02062          break;
02063 
02064        case rs_cfa:
02065          address += eh_frame_estimate_size_before_relax (fragP);
02066          break;
02067 
02068        case rs_dwarf2dbg:
02069          address += dwarf2dbg_estimate_size_before_relax (fragP);
02070          break;
02071 
02072        default:
02073          BAD_CASE (fragP->fr_type);
02074          break;
02075        }
02076     }
02077 
02078   /* Do relax().  */
02079   {
02080     unsigned long max_iterations;
02081 
02082     /* Cumulative address adjustment.  */
02083     offsetT stretch;
02084 
02085     /* Have we made any adjustment this pass?  We can't just test
02086        stretch because one piece of code may have grown and another
02087        shrank.  */
02088     int stretched;
02089 
02090     /* Most horrible, but gcc may give us some exception data that
02091        is impossible to assemble, of the form
02092 
02093        .align 4
02094        .byte 0, 0
02095        .uleb128 end - start
02096        start:
02097        .space 128*128 - 1
02098        .align 4
02099        end:
02100 
02101        If the leb128 is two bytes in size, then end-start is 128*128,
02102        which requires a three byte leb128.  If the leb128 is three
02103        bytes in size, then end-start is 128*128-1, which requires a
02104        two byte leb128.  We work around this dilemma by inserting
02105        an extra 4 bytes of alignment just after the .align.  This
02106        works because the data after the align is accessed relative to
02107        the end label.
02108 
02109        This counter is used in a tiny state machine to detect
02110        whether a leb128 followed by an align is impossible to
02111        relax.  */
02112     int rs_leb128_fudge = 0;
02113 
02114     /* We want to prevent going into an infinite loop where one frag grows
02115        depending upon the location of a symbol which is in turn moved by
02116        the growing frag.  eg:
02117 
02118         foo = .
02119         .org foo+16
02120         foo = .
02121 
02122        So we dictate that this algorithm can be at most O2.  */
02123     max_iterations = frag_count * frag_count;
02124     /* Check for overflow.  */
02125     if (max_iterations < frag_count)
02126       max_iterations = frag_count;
02127 
02128     ret = 0;
02129     do
02130       {
02131        stretch = 0;
02132        stretched = 0;
02133 
02134        for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
02135          {
02136            offsetT growth = 0;
02137            addressT was_address;
02138            offsetT offset;
02139            symbolS *symbolP;
02140 
02141            fragP->relax_marker ^= 1;
02142            was_address = fragP->fr_address;
02143            address = fragP->fr_address += stretch;
02144            symbolP = fragP->fr_symbol;
02145            offset = fragP->fr_offset;
02146 
02147            switch (fragP->fr_type)
02148              {
02149              case rs_fill:  /* .fill never relaxes.  */
02150               growth = 0;
02151               break;
02152 
02153 #ifndef WORKING_DOT_WORD
02154               /* JF:  This is RMS's idea.  I do *NOT* want to be blamed
02155                  for it I do not want to write it.  I do not want to have
02156                  anything to do with it.  This is not the proper way to
02157                  implement this misfeature.  */
02158              case rs_broken_word:
02159               {
02160                 struct broken_word *lie;
02161                 struct broken_word *untruth;
02162 
02163                 /* Yes this is ugly (storing the broken_word pointer
02164                    in the symbol slot).  Still, this whole chunk of
02165                    code is ugly, and I don't feel like doing anything
02166                    about it.  Think of it as stubbornness in action.  */
02167                 growth = 0;
02168                 for (lie = (struct broken_word *) (fragP->fr_symbol);
02169                      lie && lie->dispfrag == fragP;
02170                      lie = lie->next_broken_word)
02171                   {
02172 
02173                     if (lie->added)
02174                      continue;
02175 
02176                     offset = (S_GET_VALUE (lie->add)
02177                             + lie->addnum
02178                             - S_GET_VALUE (lie->sub));
02179                     if (offset <= -32768 || offset >= 32767)
02180                      {
02181                        if (flag_warn_displacement)
02182                          {
02183                            char buf[50];
02184                            sprint_value (buf, (addressT) lie->addnum);
02185                            as_warn_where (fragP->fr_file, fragP->fr_line,
02186                                         _(".word %s-%s+%s didn't fit"),
02187                                         S_GET_NAME (lie->add),
02188                                         S_GET_NAME (lie->sub),
02189                                         buf);
02190                          }
02191                        lie->added = 1;
02192                        if (fragP->fr_subtype == 0)
02193                          {
02194                            fragP->fr_subtype++;
02195                            growth += md_short_jump_size;
02196                          }
02197                        for (untruth = lie->next_broken_word;
02198                             untruth && untruth->dispfrag == lie->dispfrag;
02199                             untruth = untruth->next_broken_word)
02200                          if ((symbol_get_frag (untruth->add)
02201                              == symbol_get_frag (lie->add))
02202                             && (S_GET_VALUE (untruth->add)
02203                                 == S_GET_VALUE (lie->add)))
02204                            {
02205                             untruth->added = 2;
02206                             untruth->use_jump = lie;
02207                            }
02208                        growth += md_long_jump_size;
02209                      }
02210                   }
02211 
02212                 break;
02213               }             /* case rs_broken_word  */
02214 #endif
02215              case rs_align:
02216              case rs_align_code:
02217              case rs_align_test:
02218               {
02219                 addressT oldoff, newoff;
02220 
02221                 oldoff = relax_align (was_address + fragP->fr_fix,
02222                                    (int) offset);
02223                 newoff = relax_align (address + fragP->fr_fix,
02224                                    (int) offset);
02225 
02226                 if (fragP->fr_subtype != 0)
02227                   {
02228                     if (oldoff > fragP->fr_subtype)
02229                      oldoff = 0;
02230                     if (newoff > fragP->fr_subtype)
02231                      newoff = 0;
02232                   }
02233 
02234                 growth = newoff - oldoff;
02235 
02236                 /* If this align happens to follow a leb128 and
02237                    we have determined that the leb128 is bouncing
02238                    in size, then break the cycle by inserting an
02239                    extra alignment.  */
02240                 if (growth < 0
02241                     && (rs_leb128_fudge & 16) != 0
02242                     && (rs_leb128_fudge & 15) >= 2)
02243                   {
02244                     segment_info_type *seginfo = seg_info (segment);
02245                     struct obstack *ob = &seginfo->frchainP->frch_obstack;
02246                     struct frag *newf;
02247 
02248                     newf = frag_alloc (ob);
02249                     obstack_blank_fast (ob, fragP->fr_var);
02250                     obstack_finish (ob);
02251                     memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
02252                     memcpy (newf->fr_literal,
02253                            fragP->fr_literal + fragP->fr_fix,
02254                            fragP->fr_var);
02255                     newf->fr_type = rs_fill;
02256                     newf->fr_fix = 0;
02257                     newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
02258                                     / fragP->fr_var);
02259                     if (newf->fr_offset * newf->fr_var
02260                        != (offsetT) 1 << fragP->fr_offset)
02261                      {
02262                        newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
02263                        newf->fr_var = 1;
02264                      }
02265                     /* Include growth of new frag, because rs_fill
02266                       frags don't normally grow.  */
02267                     growth += newf->fr_offset * newf->fr_var;
02268                     /* The new frag address is newoff.  Adjust this
02269                       for the amount we'll add when we process the
02270                       new frag.  */
02271                     newf->fr_address = newoff - stretch - growth;
02272                     newf->relax_marker ^= 1;
02273                     fragP->fr_next = newf;
02274 #ifdef DEBUG
02275                     as_warn (_("padding added"));
02276 #endif
02277                   }
02278               }
02279               break;
02280 
02281              case rs_org:
02282               {
02283                 addressT target = offset;
02284                 addressT after;
02285 
02286                 if (symbolP)
02287                   {
02288                     /* Convert from an actual address to an octet offset
02289                       into the section.  Here it is assumed that the
02290                       section's VMA is zero, and can omit subtracting it
02291                       from the symbol's value to get the address offset.  */
02292                     know (S_GET_SEGMENT (symbolP)->vma == 0);
02293                     target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
02294                   }
02295 
02296                 know (fragP->fr_next);
02297                 after = fragP->fr_next->fr_address;
02298                 growth = target - after;
02299                 if (growth < 0)
02300                   {
02301                     growth = 0;
02302 
02303                     /* Don't error on first few frag relax passes.
02304                       The symbol might be an expression involving
02305                       symbol values from other sections.  If those
02306                       sections have not yet been processed their
02307                       frags will all have zero addresses, so we
02308                       will calculate incorrect values for them.  The
02309                       number of passes we allow before giving an
02310                       error is somewhat arbitrary.  It should be at
02311                       least one, with larger values requiring
02312                       increasingly contrived dependencies between
02313                       frags to trigger a false error.  */
02314                     if (pass < 2)
02315                      {
02316                        /* Force another pass.  */
02317                        ret = 1;
02318                        break;
02319                      }
02320 
02321                     /* Growth may be negative, but variable part of frag
02322                       cannot have fewer than 0 chars.  That is, we can't
02323                       .org backwards.  */
02324                     as_bad_where (fragP->fr_file, fragP->fr_line,
02325                                 _("attempt to move .org backwards"));
02326 
02327                     /* We've issued an error message.  Change the
02328                       frag to avoid cascading errors.  */
02329                     fragP->fr_type = rs_align;
02330                     fragP->fr_subtype = 0;
02331                     fragP->fr_offset = 0;
02332                     fragP->fr_fix = after - was_address;
02333                     break;
02334                   }
02335 
02336                 /* This is an absolute growth factor  */
02337                 growth -= stretch;
02338                 break;
02339               }
02340 
02341              case rs_space:
02342               growth = 0;
02343               if (symbolP)
02344                 {
02345                   offsetT amount;
02346 
02347                   amount = S_GET_VALUE (symbolP);
02348                   if (S_GET_SEGMENT (symbolP) != absolute_section
02349                      || S_IS_COMMON (symbolP)
02350                      || ! S_IS_DEFINED (symbolP))
02351                     {
02352                      as_bad_where (fragP->fr_file, fragP->fr_line,
02353                                   _(".space specifies non-absolute value"));
02354                      /* Prevent repeat of this error message.  */
02355                      fragP->fr_symbol = 0;
02356                     }
02357                   else if (amount < 0)
02358                     {
02359                      /* Don't error on first few frag relax passes.
02360                         See rs_org comment for a longer explanation.  */
02361                      if (pass < 2)
02362                        {
02363                          ret = 1;
02364                          break;
02365                        }
02366 
02367                      as_warn_where (fragP->fr_file, fragP->fr_line,
02368                                    _(".space or .fill with negative value, ignored"));
02369                      fragP->fr_symbol = 0;
02370                     }
02371                   else
02372                     growth = (was_address + fragP->fr_fix + amount
02373                             - fragP->fr_next->fr_address);
02374                 }
02375               break;
02376 
02377              case rs_machine_dependent:
02378 #ifdef md_relax_frag
02379               growth = md_relax_frag (segment, fragP, stretch);
02380 #else
02381 #ifdef TC_GENERIC_RELAX_TABLE
02382               /* The default way to relax a frag is to look through
02383                  TC_GENERIC_RELAX_TABLE.  */
02384               growth = relax_frag (segment, fragP, stretch);
02385 #endif /* TC_GENERIC_RELAX_TABLE  */
02386 #endif
02387               break;
02388 
02389              case rs_leb128:
02390               {
02391                 valueT value;
02392                 offsetT size;
02393 
02394                 value = resolve_symbol_value (fragP->fr_symbol);
02395                 size = sizeof_leb128 (value, fragP->fr_subtype);
02396                 growth = size - fragP->fr_offset;
02397                 fragP->fr_offset = size;
02398               }
02399               break;
02400 
02401              case rs_cfa:
02402               growth = eh_frame_relax_frag (fragP);
02403               break;
02404 
02405              case rs_dwarf2dbg:
02406               growth = dwarf2dbg_relax_frag (fragP);
02407               break;
02408 
02409              default:
02410               BAD_CASE (fragP->fr_type);
02411               break;
02412              }
02413            if (growth)
02414              {
02415               stretch += growth;
02416               stretched = 1;
02417               if (fragP->fr_type == rs_leb128)
02418                 rs_leb128_fudge += 16;
02419               else if (fragP->fr_type == rs_align
02420                       && (rs_leb128_fudge & 16) != 0
02421                       && stretch == 0)
02422                 rs_leb128_fudge += 16;
02423               else
02424                 rs_leb128_fudge = 0;
02425              }
02426          }
02427 
02428        if (stretch == 0
02429            && (rs_leb128_fudge & 16) == 0
02430            && (rs_leb128_fudge & -16) != 0)
02431          rs_leb128_fudge += 1;
02432        else
02433          rs_leb128_fudge = 0;
02434       }
02435     /* Until nothing further to relax.  */
02436     while (stretched && -- max_iterations);
02437 
02438     if (stretched)
02439       as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
02440               segment_name (segment));
02441   }
02442 
02443   for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
02444     if (fragP->last_fr_address != fragP->fr_address)
02445       {
02446        fragP->last_fr_address = fragP->fr_address;
02447        ret = 1;
02448       }
02449   return ret;
02450 }
02451 
02452 void
02453 number_to_chars_bigendian (char *buf, valueT val, int n)
02454 {
02455   if (n <= 0)
02456     abort ();
02457   while (n--)
02458     {
02459       buf[n] = val & 0xff;
02460       val >>= 8;
02461     }
02462 }
02463 
02464 void
02465 number_to_chars_littleendian (char *buf, valueT val, int n)
02466 {
02467   if (n <= 0)
02468     abort ();
02469   while (n--)
02470     {
02471       *buf++ = val & 0xff;
02472       val >>= 8;
02473     }
02474 }
02475 
02476 void
02477 write_print_statistics (FILE *file)
02478 {
02479   fprintf (file, "fixups: %d\n", n_fixups);
02480 }
02481 
02482 /* For debugging.  */
02483 extern int indent_level;
02484 
02485 void
02486 print_fixup (fixS *fixp)
02487 {
02488   indent_level = 1;
02489   fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
02490   if (fixp->fx_pcrel)
02491     fprintf (stderr, " pcrel");
02492   if (fixp->fx_pcrel_adjust)
02493     fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
02494   if (fixp->fx_im_disp)
02495     {
02496 #ifdef TC_NS32K
02497       fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
02498 #else
02499       fprintf (stderr, " im_disp");
02500 #endif
02501     }
02502   if (fixp->fx_tcbit)
02503     fprintf (stderr, " tcbit");
02504   if (fixp->fx_done)
02505     fprintf (stderr, " done");
02506   fprintf (stderr, "\n    size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
02507           fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
02508           (long) fixp->fx_offset, (long) fixp->fx_addnumber);
02509   fprintf (stderr, "\n    %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
02510           fixp->fx_r_type);
02511   if (fixp->fx_addsy)
02512     {
02513       fprintf (stderr, "\n   +<");
02514       print_symbol_value_1 (stderr, fixp->fx_addsy);
02515       fprintf (stderr, ">");
02516     }
02517   if (fixp->fx_subsy)
02518     {
02519       fprintf (stderr, "\n   -<");
02520       print_symbol_value_1 (stderr, fixp->fx_subsy);
02521       fprintf (stderr, ">");
02522     }
02523   fprintf (stderr, "\n");
02524 #ifdef TC_FIX_DATA_PRINT
02525   TC_FIX_DATA_PRINT (stderr, fixp);
02526 #endif
02527 }