home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / gas / write.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  73KB  |  2,668 lines

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