home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Source / GNU / cctools / as / write.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-13  |  41.4 KB  |  1,374 lines

  1. /* write.c - emit .o file - Copyright(C)1986 Free Software Foundation, Inc.
  2.    Copyright (C) 1986,1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* 
  21.  
  22.    Umm, with real good luck, this thing should be set up to do byteordering
  23.    correctly, but I may have managed to miss a place or two.  Treat a.out
  24.    very carefully until you're SURE that it works. . .
  25.  
  26.    In order to cross-assemble the target machine must have an a.out header
  27.    similar to the one in a.out.h on THIS machine.  Byteorder doesn't matter;
  28.    we take special care of it, but the numbers must be the same SIZE (# of
  29.    bytes) and in the same PLACE.  If this is not true, you will have some
  30.    trouble.
  31.  */
  32.  
  33. #include "as.h"
  34. #include "md.h"
  35. #include "subsegs.h"
  36. #include "obstack.h"
  37. #include "struc-symbol.h"
  38. #include "write.h"
  39. #include "symbols.h"
  40.  
  41. #ifdef SPARC
  42. #include "sparc.h"
  43. #endif
  44.  
  45. #ifdef M88K
  46. #include "m88k.h"
  47. #endif
  48.  
  49. #ifdef M98K
  50. #include "m98k.h"
  51. #endif
  52.  
  53.  
  54. #ifdef I860
  55. #include "i860.h"
  56. #endif I860
  57.  
  58. void    append();
  59.  
  60. #ifdef hpux
  61. #define EXEC_MACHINE_TYPE HP9000S200_ID
  62. #endif
  63.  
  64. /*
  65.  * In: length of relocation (or of address) in chars: 1, 2 or 4.
  66.  * Out: GNU LD relocation length code: 0, 1, or 2.
  67.  */
  68.  
  69. static unsigned char
  70.  
  71. nbytes_r_length [] = {
  72.   42, 0, 1, 42, 2
  73.   };
  74.  
  75.  
  76. static struct frag *    text_frag_root;
  77. static struct frag *    data_frag_root;
  78.  
  79. static struct frag *    text_last_frag;    /* Last frag in segment. */
  80. static struct frag *    data_last_frag;    /* Last frag in segment. */
  81.  
  82. static struct exec    the_exec;
  83.  
  84. #ifdef Mach_O
  85. static long int symbol_number = 0;
  86. #endif Mach_O
  87.  
  88. static long int string_byte_count;
  89.  
  90. static char *        the_object_file;
  91.  
  92. #ifndef RISC
  93. static
  94. #endif
  95. char *        next_object_file_charP;    /* Tracks object file bytes. */
  96.  
  97. static long int        size_of_the_object_file; /* # bytes in object file. */
  98.  
  99. /* static long int        length; JF unused */    /* String length, including trailing '\0'. */
  100.  
  101. static void    relax_segment();
  102. void        emit_segment();
  103. static relax_addressT    relax_align();
  104. static long int    fixup_segment();
  105. #ifndef RISC
  106. static void        emit_relocations();
  107. #endif
  108. /*
  109.  *            fix_new()
  110.  *
  111.  * Create a fixS in obstack 'notes'.
  112.  */
  113. void
  114. #ifdef RISC
  115. fix_new (frag, where, size, add_symbol, sub_symbol, offset, pcrel, r_type)
  116. #else
  117. fix_new (frag, where, size, add_symbol, sub_symbol, offset, pcrel)
  118. #endif
  119.      fragS *    frag;        /* Which frag? */
  120.      int    where;        /* Where in that frag? */
  121.      short int    size;        /* 1, 2  or 4 usually. */
  122.      symbolS *    add_symbol;    /* X_add_symbol. */
  123.      symbolS *    sub_symbol;    /* X_subtract_symbol. */
  124.      long int    offset;        /* X_add_number. */
  125.      int    pcrel;        /* TRUE if PC-relative relocation. */
  126. #ifdef RISC
  127.     int        r_type;
  128. #endif
  129. {
  130.   register fixS *    fixP;
  131.  
  132.   fixP = (fixS *)obstack_alloc(¬es,sizeof(fixS));
  133.  
  134.   fixP -> fx_frag    = frag;
  135.   fixP -> fx_where    = where;
  136.   fixP -> fx_size    = size;
  137.   fixP -> fx_addsy    = add_symbol;
  138.   fixP -> fx_subsy    = sub_symbol;
  139.   fixP -> fx_offset    = offset;
  140.   fixP -> fx_pcrel    = pcrel;
  141.   fixP -> fx_next    = * seg_fix_rootP;
  142.  
  143.   /* JF these 'cuz of the NS32K stuff */
  144.   fixP -> fx_im_disp    = 0;
  145.   fixP -> fx_pcrel_adjust = 0;
  146.   fixP -> fx_bsr    = 0;
  147.   fixP ->fx_bit_fixP    = 0;
  148.  
  149. #ifdef RISC
  150.   fixP->fx_r_type = r_type;
  151. #endif
  152.  
  153.   * seg_fix_rootP = fixP;
  154. }
  155.  
  156. void
  157. write_object_file()
  158. {
  159.   register struct frchain *    frchainP; /* Track along all frchains. */
  160.   register fragS *        fragP;    /* Track along all frags. */
  161.   register struct frchain *    next_frchainP;
  162.   register fragS * *        prev_fragPP;
  163.   register char *        name;
  164.   register symbolS *        symbolP;
  165.   register symbolS **        symbolPP;
  166.   /* register fixS *        fixP; JF unused */
  167.   unsigned
  168.       text_siz,
  169.     data_siz,
  170.     syms_siz,
  171.     tr_siz,
  172.     dr_siz;
  173.   void output_file_create();
  174.   void output_file_append();
  175.   void output_file_close();
  176. #ifdef DONTDEF
  177.   void gdb_emit();
  178.   void gdb_end();
  179. #endif
  180.   extern long omagic;        /* JF magic # to write out.  Is different for
  181.                    Suns and Vaxen and other boxes */
  182.  
  183. #ifdef    VMS
  184.   /*
  185.    *    Under VMS we try to be compatible with VAX-11 "C".  Thus, we
  186.    *    call a routine to check for the definition of the procedure
  187.    *    "_main", and if so -- fix it up so that it can be program
  188.    *    entry point.
  189.    */
  190.   VMS_Check_For_Main();
  191. #endif /* VMS */
  192.   /*
  193.    * After every sub-segment, we fake an ".align ...". This conforms to BSD4.2
  194.    * brane-damage. We then fake ".fill 0" because that is the kind of frag
  195.    * that requires least thought. ".align" frags like to have a following
  196.    * frag since that makes calculating their intended length trivial.
  197.    */
  198.  
  199. #ifdef RISC
  200. #define SUB_SEGMENT_ALIGN (3)
  201. #else
  202. #define SUB_SEGMENT_ALIGN (2)
  203. #endif
  204.  
  205.   for ( frchainP=frchain_root; frchainP; frchainP=frchainP->frch_next )
  206.     {
  207. #ifdef    VMS
  208.       /*
  209.        *    Under VAX/VMS, the linker (and PSECT specifications)
  210.        *    take care of correctly aligning the segments.
  211.        *    Doing the alignment here (on initialized data) can
  212.        *    mess up the calculation of global data PSECT sizes.
  213.        */
  214. #undef    SUB_SEGMENT_ALIGN
  215. #define    SUB_SEGMENT_ALIGN ((frchainP->frch_seg != SEG_DATA) ? 2 : 0)
  216. #endif    /* VMS */
  217.       subseg_new (frchainP -> frch_seg, frchainP -> frch_subseg);
  218. #if defined(M68K) && defined(Mach_O)
  219.       /*
  220.        * To get only instructions in the (__TEXT,__text) section the section
  221.        * must not be padded with something that is not an instruction (or a
  222.        * full instruction as .word 0 is not a full instruction but rather half
  223.        * of an "orbi #0x??,d0".  So what is done here is to first align to a
  224.        * 2 byte boundary (that will not do anything if only text is in the text
  225.        * section) then align to a 4 byte boundary saving a handle to the frag
  226.        * to be used in Mach-O.c which will put out a nop for it in place of
  227.        * two zeros if it has size of 2 (not 0).
  228.        */
  229.       if(frchainP -> frch_seg == SEG_TEXT &&
  230.          frchainP -> frch_subseg == 0){
  231.       extern struct frag *frag_nop;
  232.  
  233.       frag_align (1, 0);
  234.       frag_nop = frag_now;
  235.       frag_align (2, 0);
  236.       }
  237. #endif defined(M68K) && defined(Mach_O)
  238.  
  239. #if defined(I386) && defined(Mach_O)
  240.       /*
  241.        * To get only instructions in the (__TEXT,__text) section the section
  242.        * must not be padded with something that is not an instruction.  For the
  243.        * I386 we pad with nop's (0x90).
  244.        */
  245.       if(frchainP -> frch_seg == SEG_TEXT &&
  246.          frchainP -> frch_subseg == 0){
  247.       frag_align (2, 0x90);
  248.       }
  249. #endif defined(I386) && defined(Mach_O)
  250.  
  251. #ifdef NeXT
  252.       /*
  253.        * The objective-C sections are aligned to only 4 bytes 2^2.
  254.        * Because of the fact that literal pointer sections need to have only
  255.        * literal pointers (and not padded bytes of zeros) no zero bytes can be
  256.        * added to them.  And because some sections (__protocol and
  257.        * _string_object) have items that are not a multiple of 8 but a multiple
  258.        * of 4 and the objective-C runtime gets to as an array of structs and
  259.        * must not have any padding between them.
  260.        */
  261.       if(frchainP -> frch_seg == SEG_DATA && frchainP -> frch_subseg >= 2){
  262.       frag_align (2, 0);
  263.       }
  264.       else{
  265.       /*
  266.        * To cause the next sub segment to have the correct alignment we
  267.        * need to pad (this is what frag_align() does) the previous sub
  268.        * segment to the next sub segment's alignment.
  269.        */
  270.       if(frchainP->frch_next){
  271.           if(frchainP->frch_next->frch_subseg_align > SUB_SEGMENT_ALIGN){
  272.           frag_align (frchainP->frch_next->frch_subseg_align, 0);
  273.           }
  274.           else{
  275.           frag_align (SUB_SEGMENT_ALIGN, 0);
  276.           }
  277.       }
  278.       }
  279. #else /* !defined(NeXT) */
  280.       frag_align (SUB_SEGMENT_ALIGN, 0);
  281.                 /* frag_align will have left a new frag. */
  282.                 /* Use this last frag for an empty ".fill". */
  283. #endif /* NeXT */
  284.       /*
  285.        * For this segment ...
  286.        * Create a last frag. Do not leave a "being filled in frag".
  287.        */
  288.       frag_wane (frag_now);
  289.       frag_now -> fr_fix    = 0;
  290.       know( frag_now -> fr_next == NULL );
  291.       /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
  292.       /* Above shows we haven't left a half-completed object on obstack. */
  293.     }
  294.  
  295.   /*
  296.    * From now on, we don't care about sub-segments.
  297.    * Build one frag chain for each segment. Linked thru fr_next.
  298.    * We know that there is at least 1 text frchain & at least 1 data frchain.
  299.    */
  300.   prev_fragPP = &text_frag_root;
  301.   for ( frchainP=frchain_root; frchainP; frchainP=next_frchainP )
  302.     {
  303.       know( frchainP -> frch_root );
  304.       * prev_fragPP = frchainP -> frch_root;
  305.       prev_fragPP = & frchainP -> frch_last -> fr_next;
  306.       if (   ((next_frchainP = frchainP->frch_next) == NULL)
  307.       || next_frchainP == data0_frchainP)
  308.     {
  309.       prev_fragPP = & data_frag_root;
  310.       if ( next_frchainP )
  311.         {
  312.           text_last_frag = frchainP -> frch_last;
  313.         }
  314.       else
  315.         {
  316.           data_last_frag = frchainP -> frch_last;
  317.         }
  318.     }
  319.     }                /* for(each struct frchain) */
  320.  
  321.   /*
  322.    * We have two segments. If user gave -R flag, then we must put the
  323.    * data frags into the text segment. Do this before relaxing so
  324.    * we know to take advantage of -R and make shorter addresses.
  325.    */
  326.   if ( flagseen [ 'R' ] )
  327.     {
  328.       fixS *tmp;
  329.  
  330.       text_last_frag -> fr_next = data_frag_root;
  331.       text_last_frag = data_last_frag;
  332.       data_last_frag = NULL;
  333.       data_frag_root = NULL;
  334.       if(text_fix_root) {
  335.     for(tmp=text_fix_root;tmp->fx_next;tmp=tmp->fx_next)
  336.       ;
  337.     tmp->fx_next=data_fix_root;
  338.       } else
  339.         text_fix_root=data_fix_root;
  340.       data_fix_root=NULL;
  341.     }
  342.  
  343.   relax_segment (text_frag_root, SEG_TEXT);
  344.   relax_segment (data_frag_root, SEG_DATA);
  345.   /*
  346.    * Now the addresses of frags are correct within the segment.
  347.    */
  348.  
  349.   know(   text_last_frag -> fr_type   == rs_fill && text_last_frag -> fr_offset == 0 );
  350.   text_siz=text_last_frag->fr_address;
  351. #ifdef RISC
  352. #ifdef I860
  353.   /* Check/force alignment here! */
  354.   text_siz = (text_siz + 0x1F) & (~0x1F);/* Keep 32 byte alignment (most
  355.                         restrictive) */
  356.   text_last_frag->fr_address = text_siz; /* and pad the last fragment. */
  357. #endif I860
  358.   text_siz= (text_siz+7)&(~7);
  359.   text_last_frag->fr_address=text_siz;
  360. #endif
  361.   md_number_to_chars((char *)&the_exec.a_text,text_siz, sizeof(the_exec.a_text));
  362.   /* the_exec . a_text = text_last_frag -> fr_address; */
  363.  
  364.   /*
  365.    * Join the 2 segments into 1 huge segment.
  366.    * To do this, re-compute every rn_address in the SEG_DATA frags.
  367.    * Then join the data frags after the text frags.
  368.    *
  369.    * Determine a_data [length of data segment].
  370.    */
  371.   if (data_frag_root)
  372.     {
  373.       register relax_addressT    slide;
  374.  
  375.       know(   text_last_frag -> fr_type   == rs_fill && text_last_frag -> fr_offset == 0 );
  376.       data_siz=data_last_frag->fr_address;
  377. #ifdef RISC
  378. #ifdef I860
  379.       data_siz += (16 - (data_siz % 16)) % 16; /* Pad data seg to preserve
  380.                           alignment */
  381.       data_last_frag->fr_address = data_siz;   /* to quad-word boundries */
  382. #endif I860
  383.       data_siz += (8 - (data_siz % 8)) % 8;
  384.       data_last_frag->fr_address = data_siz;
  385. #endif
  386.       md_number_to_chars((char *)&the_exec.a_data,data_siz,sizeof(the_exec.a_data));
  387.       /* the_exec . a_data = data_last_frag -> fr_address; */
  388.       slide = text_siz; /* Address in file of the data segment. */
  389.       for (fragP = data_frag_root;
  390.        fragP;
  391.        fragP = fragP -> fr_next)
  392.     {
  393.       fragP -> fr_address += slide;
  394.     }
  395.       know( text_last_frag );
  396.       text_last_frag -> fr_next = data_frag_root;
  397.     }
  398.   else {
  399.       md_number_to_chars((char *)&the_exec.a_data,0,sizeof(the_exec.a_data));
  400.       data_siz = 0;
  401.   }
  402.  
  403.   bss_address_frag . fr_address = text_siz + data_siz;
  404. #ifdef RISC
  405. #ifdef I860
  406.   local_bss_counter=(local_bss_counter+0xF)&(~0xF); /* Pad BSS to preserve
  407.                                alignment */
  408. #endif I860
  409.   local_bss_counter=(local_bss_counter+7)&(~7);
  410. #endif
  411.   md_number_to_chars((char *)&the_exec.a_bss,local_bss_counter,sizeof(the_exec.a_bss));
  412.  
  413.           
  414.   /*
  415.    *
  416.    * Crawl the symbol chain.
  417.    *
  418.    * For each symbol whose value depends on a frag, take the address of
  419.    * that frag and subsume it into the value of the symbol.
  420.    * After this, there is just one way to lookup a symbol value.
  421.    * Values are left in their final state for object file emission.
  422.    * We adjust the values of 'L' local symbols, even if we do
  423.    * not intend to emit them to the object file, because their values
  424.    * are needed for fix-ups.
  425.    *
  426.    * Unless we saw a -L flag, remove all symbols that begin with 'L'
  427.    * from the symbol chain.
  428.    *
  429.    * Count the (length of the nlists of the) (remaining) symbols.
  430.    * Assign a symbol number to each symbol.
  431.    * Count the number of string-table chars we will emit.
  432.    *
  433.    */
  434.   know( zero_address_frag . fr_address == 0 );
  435.   string_byte_count = sizeof( string_byte_count );
  436.  
  437.   /* JF deal with forward references first. . . */
  438.   for(symbolP=symbol_rootP;symbolP;symbolP=symbolP->sy_next) {
  439.       if(symbolP->sy_forward) {
  440. #ifdef Mach_O
  441.         if(symbolP->sy_nlist.n_type & N_STAB)
  442.             symbolP->sy_other = symbolP->sy_forward->sy_other;
  443. #endif Mach_O
  444.         symbolP->sy_value+=symbolP->sy_forward->sy_value+symbolP->sy_forward->sy_frag->fr_address;
  445.         symbolP->sy_forward=0;
  446.     }
  447.   }
  448.   symbolPP = & symbol_rootP;    /* -> last symbol chain link. */
  449.   {
  450. #ifndef Mach_O
  451.     register long int        symbol_number;
  452.  
  453.     symbol_number = 0;
  454. #endif !defined(Mach_O)
  455.     while (symbolP  = * symbolPP)
  456.       {
  457.     name = symbolP -> sy_name;
  458.     if(flagseen['R'] && (symbolP->sy_nlist.n_type&N_DATA)) {
  459.       symbolP->sy_nlist.n_type&= ~N_DATA;
  460.       symbolP->sy_nlist.n_type|= N_TEXT;
  461.     }
  462.     /* if(symbolP->sy_forward) {
  463.       symbolP->sy_value += symbolP->sy_forward->sy_value + symbolP->sy_forward->sy_frag->fr_address;
  464.     } */
  465.     
  466.     symbolP -> sy_value += symbolP -> sy_frag -> fr_address;
  467.         /* JF the 128 bit is a hack so stabs like
  468.            "LET_STMT:23. . ."  don't go away */
  469.     /* CPH: 128 bit hack is moby loser.  N_SO for file "Lower.c"
  470.        fell through the cracks.  I think that N_STAB should be
  471.        used instead of 128. */
  472.         /* JF the \001 bit is to make sure that local labels
  473.            ( 1: - 9: don't make it into the symtable either */
  474. #ifndef    VMS    /* Under VMS we need to keep local symbols */
  475.     if ( !name || (symbolP->sy_nlist.n_type&N_STAB)
  476. #ifdef NeXT
  477.         /* bug fix for undefined local symbols */
  478.         || (name [0] == 'L' &&(symbolP->sy_nlist.n_type & N_TYPE) == N_UNDF)
  479. #endif NeXT
  480.         || (name[0]!='\001' && (flagseen ['L'] || name [0] != 'L' )))
  481. #endif    /* not VMS */
  482.       {
  483.         symbolP -> sy_number = symbol_number ++;
  484. #ifndef    VMS
  485.         if (name)
  486.           {            /* Ordinary case. */
  487. #ifdef NeXT
  488.         /* bug fix for undefined local symbols */
  489.         if((symbolP->sy_nlist.n_type & N_STAB) == 0 &&
  490.            name [0] == 'L' && !flagseen['L'] &&
  491.            (symbolP->sy_nlist.n_type & N_TYPE) == N_UNDF)
  492.             if(name[1] != '\0' && name[2] == '\001'){
  493.             as_bad("Undefined local symbol %c (%cf or %cb)",
  494.                 name[1], name[1], name[1]);
  495.             name[0] = name[1];
  496.             name[1] = '\0';
  497.             }
  498.             else
  499.             as_bad("Undefined local symbol %s", name);
  500. #endif NeXT
  501.         symbolP -> sy_name_offset = string_byte_count;
  502.         string_byte_count += strlen (symbolP  -> sy_name) + 1;
  503.           }
  504.         else            /* .Stabd case. */
  505. #endif    /* not VMS */
  506.         symbolP -> sy_name_offset = 0;
  507.         symbolPP = & (symbolP -> sy_next);
  508.       }
  509. #ifndef    VMS
  510.     else
  511.         * symbolPP = symbolP -> sy_next;
  512. #endif    /* not VMS */
  513.       }                /* for each symbol */
  514.  
  515.     syms_siz = sizeof( struct nlist) * symbol_number;
  516.     md_number_to_chars((char *)&the_exec.a_syms,syms_siz,sizeof(the_exec.a_syms));
  517.     /* the_exec . a_syms = sizeof( struct nlist) * symbol_number; */
  518.   }
  519.  
  520.   /*
  521.    * Addresses of frags now reflect addresses we use in the object file.
  522.    * Symbol values are correct.
  523.    * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
  524.    * Also converting any machine-dependent frags using md_convert_frag();
  525.    */
  526.   subseg_change( SEG_TEXT, 0);
  527.  
  528.   for (fragP = text_frag_root;  fragP;  fragP = fragP -> fr_next)
  529.     {
  530.       switch (fragP -> fr_type)
  531.     {
  532.     case rs_align:
  533.     case rs_org:
  534.       fragP -> fr_type = rs_fill;
  535.       know( fragP -> fr_var == 1 );
  536.       know( fragP -> fr_next );
  537.       fragP -> fr_offset
  538.         =     fragP -> fr_next -> fr_address
  539.           -   fragP -> fr_address
  540.         - fragP -> fr_fix;
  541.       break;
  542.  
  543.     case rs_fill:
  544.       break;
  545.  
  546.     case rs_machine_dependent:
  547.       md_convert_frag (fragP);
  548.       /*
  549.        * After md_convert_frag, we make the frag into a ".space 0".
  550.        * Md_convert_frag() should set up any fixSs and constants
  551.        * required.
  552.        */
  553.       frag_wane (fragP);
  554.       break;
  555.  
  556. #ifndef WORKING_DOT_WORD
  557.     case rs_broken_word:
  558.       {
  559.         struct broken_word *lie;
  560.         extern md_short_jump_size;
  561.         extern md_long_jump_size;
  562.  
  563.         if(fragP->fr_subtype) {
  564.           fragP->fr_fix+=md_short_jump_size;
  565.           for(lie=(struct broken_word *)(fragP->fr_symbol);lie && lie->dispfrag==fragP;lie=lie->next_broken_word)
  566.         if(lie->added==1)
  567.           fragP->fr_fix+=md_long_jump_size;
  568.         }
  569.         frag_wane(fragP);
  570.       }
  571.       break;
  572. #endif
  573.  
  574.     default:
  575.       BAD_CASE( fragP -> fr_type );
  576.       break;
  577.     }            /* switch (fr_type) */
  578.     }                /* for each frag. */
  579.  
  580. #ifndef WORKING_DOT_WORD
  581.     {
  582.       struct broken_word *lie;
  583.       struct broken_word **prevP;
  584.  
  585.       prevP= &broken_words;
  586.       for(lie=broken_words; lie; lie=lie->next_broken_word)
  587.     if(!lie->added) {
  588. #ifdef RISC
  589.       fix_new(    lie->frag,  lie->word_goes_here - lie->frag->fr_literal,
  590.               2,  lie->add,
  591.             lie->sub,  lie->addnum,
  592.             0,  NO_RELOC);
  593. #endif
  594. #ifdef NS32K
  595.       fix_new_ns32k(lie->frag,
  596.               lie->word_goes_here - lie->frag->fr_literal,
  597.             2,
  598.             lie->add,
  599.             lie->sub,
  600.             lie->addnum,
  601.             0, 0, 2, 0, 0);
  602. #endif
  603. #if !defined(RISC) && !defined(NS32K)
  604.       fix_new(    lie->frag,  lie->word_goes_here - lie->frag->fr_literal,
  605.               2,  lie->add,
  606.             lie->sub,  lie->addnum,
  607.             0);
  608. #endif
  609.       /* md_number_to_chars(lie->word_goes_here,
  610.                    lie->add->sy_value
  611.                    + lie->addnum
  612.                    - (lie->sub->sy_value),
  613.                  2); */
  614.       *prevP=lie->next_broken_word;
  615.     } else
  616.       prevP= &(lie->next_broken_word);
  617.  
  618.       for(lie=broken_words;lie;) {
  619.     struct broken_word *untruth;
  620.     char    *table_ptr;
  621.     long    table_addr;
  622.     long    from_addr,
  623.         to_addr;
  624.     int    n,
  625.         m;
  626.  
  627.     extern    md_short_jump_size;
  628.     extern    md_long_jump_size;
  629.     void    md_create_short_jump();
  630.     void    md_create_long_jump();
  631.  
  632.  
  633.  
  634.     fragP=lie->dispfrag;
  635.  
  636.     /* Find out how many broken_words go here */
  637.     n=0;
  638.     for(untruth=lie;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word)
  639.       if(untruth->added==1)
  640.         n++;
  641.  
  642.     table_ptr=lie->dispfrag->fr_opcode;
  643.     table_addr=lie->dispfrag->fr_address+(table_ptr - lie->dispfrag->fr_literal);
  644.     /* Create the jump around the long jumps */
  645.     /* This is a short jump from table_ptr+0 to table_ptr+n*long_jump_size */
  646.     from_addr=table_addr;
  647.     to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
  648.     md_create_short_jump(table_ptr,from_addr,to_addr,lie->dispfrag,lie->add);
  649.     table_ptr+=md_short_jump_size;
  650.     table_addr+=md_short_jump_size;
  651.  
  652.     for(m=0;lie && lie->dispfrag==fragP;m++,lie=lie->next_broken_word) {
  653.       if(lie->added==2)
  654.         continue;
  655.       /* Patch the jump table */
  656.       /* This is the offset from ??? to table_ptr+0 */
  657.       to_addr =   table_addr
  658.                 - (lie->sub->sy_value);
  659.       md_number_to_chars(lie->word_goes_here,to_addr,2);
  660.       for(untruth=lie->next_broken_word;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word) {
  661.         if(untruth->use_jump==lie)
  662.           md_number_to_chars(untruth->word_goes_here,to_addr,2);
  663.       }
  664.  
  665.       /* Install the long jump */
  666.       /* this is a long jump from table_ptr+0 to the final target */
  667.       from_addr=table_addr;
  668.       to_addr=lie->add->sy_value+lie->addnum;
  669.       md_create_long_jump(table_ptr,from_addr,to_addr,lie->dispfrag,lie->add);
  670.       table_ptr+=md_long_jump_size;
  671.       table_addr+=md_long_jump_size;
  672.     }
  673.       }
  674.     }
  675. #endif
  676. #ifdef Mach_O
  677.  
  678.   (void)fixup_segment (text_fix_root, N_TEXT);
  679.   (void)fixup_segment (data_fix_root, N_DATA);
  680.   write_Mach_O (string_byte_count, symbol_number);
  681.  
  682. #else !defined(Mach_O)
  683. #ifndef    VMS
  684.   /*
  685.    * Scan every FixS performing fixups. We had to wait until now to do
  686.    * this because md_convert_frag() may have made some fixSs.
  687.    */
  688.   /* the_exec . a_trsize
  689.     = sizeof(struct relocation_info) * fixup_segment (text_fix_root, N_TEXT);
  690.   the_exec . a_drsize
  691.     = sizeof(struct relocation_info) * fixup_segment (data_fix_root, N_DATA); */
  692.  
  693.   tr_siz=sizeof(struct relocation_info) * fixup_segment (text_fix_root, N_TEXT);
  694.   md_number_to_chars((char *)&the_exec.a_trsize, tr_siz ,sizeof(the_exec.a_trsize));
  695.   dr_siz=sizeof(struct relocation_info) * fixup_segment (data_fix_root, N_DATA);
  696.   md_number_to_chars((char *)&the_exec.a_drsize, dr_siz, sizeof(the_exec.a_drsize));
  697.   md_number_to_chars((char *)&the_exec.a_magic,omagic,sizeof(the_exec.a_magic));
  698.   md_number_to_chars((char *)&the_exec.a_entry,0,sizeof(the_exec.a_entry));
  699.  
  700. #ifdef EXEC_MACHINE_TYPE
  701.   md_number_to_chars((char *)&the_exec.a_machtype, EXEC_MACHINE_TYPE, sizeof(the_exec.a_machtype));
  702. #endif
  703. #ifdef EXEC_VERSION
  704.   md_number_to_chars((char *)&the_exec.a_version,EXEC_VERSION,sizeof(the_exec.a_version));
  705. #endif
  706.   
  707.   /* the_exec . a_entry = 0; */
  708.  
  709.   size_of_the_object_file =
  710.     sizeof( the_exec ) +
  711.       text_siz +
  712.         data_siz +
  713.       syms_siz +
  714.         tr_siz +
  715.           dr_siz +
  716.         string_byte_count;
  717.     
  718.   next_object_file_charP
  719.     = the_object_file
  720.       = xmalloc ( size_of_the_object_file );
  721.  
  722.   output_file_create (out_file_name);
  723.  
  724.   append (& next_object_file_charP, (char *)(&the_exec), (unsigned long)sizeof(the_exec));
  725.  
  726.   /*
  727.    * Emit code.
  728.    */
  729.   for (fragP = text_frag_root;  fragP;  fragP = fragP -> fr_next)
  730.     {
  731.       register long int        count;
  732.       register char *        fill_literal;
  733.       register long int        fill_size;
  734.  
  735.       know( fragP -> fr_type == rs_fill );
  736.       append (& next_object_file_charP, fragP -> fr_literal, (unsigned long)fragP -> fr_fix);
  737.       fill_literal= fragP -> fr_literal + fragP -> fr_fix;
  738.       fill_size   = fragP -> fr_var;
  739.       know( fragP -> fr_offset >= 0 );
  740.       for (count = fragP -> fr_offset;  count;  count --)
  741.       append (& next_object_file_charP, fill_literal, (unsigned long)fill_size);
  742.     }                /* for each code frag. */
  743.  
  744.   /*
  745.    * Emit relocations.
  746.    */
  747.   emit_relocations (text_fix_root, (relax_addressT)0);
  748.   emit_relocations (data_fix_root, text_last_frag -> fr_address);
  749.   /*
  750.    * Emit all symbols left in the symbol chain.
  751.    * Any symbol still undefined is made N_EXT.
  752.    */
  753.   for (   symbolP = symbol_rootP;   symbolP;   symbolP = symbolP -> sy_next   )
  754.     {
  755.       register char *    temp;
  756.  
  757.       temp = symbolP -> sy_nlist . n_un . n_name;
  758.       /* JF fix the numbers up. Call by value RULES! */
  759.       md_number_to_chars((char *)&(symbolP -> sy_nlist  . n_un . n_strx ),symbolP -> sy_name_offset,sizeof(symbolP -> sy_nlist  . n_un . n_strx ));
  760.       md_number_to_chars((char *)&(symbolP->sy_nlist.n_desc),symbolP->sy_nlist.n_desc,sizeof(symbolP -> sy_nlist  . n_desc));
  761.       md_number_to_chars((char *)&(symbolP->sy_nlist.n_value),symbolP->sy_nlist.n_value,sizeof(symbolP->sy_nlist.n_value));
  762.       /* symbolP -> sy_nlist  . n_un . n_strx = symbolP -> sy_name_offset; JF replaced by md above */
  763.       if (symbolP -> sy_type == N_UNDF)
  764.       symbolP -> sy_type |= N_EXT; /* Any undefined symbols become N_EXT. */
  765.       append (& next_object_file_charP, (char *)(& symbolP -> sy_nlist),
  766.           (unsigned long)sizeof(struct nlist));
  767.       symbolP -> sy_nlist . n_un . n_name = temp;
  768.     }                /* for each symbol */
  769.  
  770.   /*
  771.    * next_object_file_charP -> slot for next object byte.
  772.    * Emit strings.
  773.    * Find strings by crawling along symbol table chain.
  774.    */
  775. /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
  776.   md_number_to_chars((char *)&string_byte_count, string_byte_count, sizeof(string_byte_count));
  777.  
  778.   append (& next_object_file_charP, (char *)&string_byte_count, (unsigned long)sizeof(string_byte_count));
  779.   for (   symbolP = symbol_rootP;   symbolP;   symbolP = symbolP -> sy_next   )
  780.     {
  781.       if (symbolP -> sy_name)
  782.     {            /* Ordinary case: not .stabd. */
  783.       append (& next_object_file_charP, symbolP -> sy_name,
  784.           (unsigned long)(strlen (symbolP -> sy_name) + 1));
  785.     }
  786.     }                /* for each symbol */
  787.  
  788.   know( next_object_file_charP == the_object_file + size_of_the_object_file );
  789.  
  790.   output_file_append (the_object_file, size_of_the_object_file, out_file_name);
  791.  
  792. #ifdef DONTDEF
  793.   if (flagseen['G'])        /* GDB symbol file to be appended? */
  794.     {
  795.       gdb_emit (out_file_name);
  796.       gdb_end ();
  797.     }
  798. #endif
  799.  
  800.   output_file_close (out_file_name);
  801. #else    /* VMS */
  802.   /*
  803.    *    Now do the VMS-dependent part of writing the object file
  804.    */
  805.   VMS_write_object_file(text_siz, data_siz, text_frag_root, data_frag_root);
  806. #endif    /* VMS */
  807. #endif Mach_O
  808. }                /* write_object_file() */
  809.  
  810. /*
  811.  *            relax_segment()
  812.  *
  813.  * Now we have a segment, not a crowd of sub-segments, we can make fr_address
  814.  * values.
  815.  *
  816.  * Relax the frags.
  817.  *
  818.  * After this, all frags in this segment have addresses that are correct
  819.  * within the segment. Since segments live in different file addresses,
  820.  * these frag addresses may not be the same as final object-file addresses.
  821.  */
  822. #ifndef    VMS
  823. static
  824. #endif    /* not VMS */
  825. void
  826. relax_segment (segment_frag_root, segment_type)
  827.      struct frag *    segment_frag_root;
  828.      segT        segment_type; /* N_DATA or N_TEXT */
  829. {
  830.   register struct frag *    fragP;
  831.   register relax_addressT    address;
  832.   /* register relax_addressT    old_address; JF unused */
  833.   /* register relax_addressT    new_address; JF unused */
  834.  
  835.   know( segment_type == SEG_DATA || segment_type == SEG_TEXT );
  836.  
  837.   /* In case md_estimate_size_before_relax() wants to make fixSs. */
  838.   subseg_change (segment_type, 0);
  839.  
  840.   /*
  841.    * For each frag in segment: count and store  (a 1st guess of) fr_address.
  842.    */
  843.   address = 0;
  844.   for ( fragP = segment_frag_root;   fragP;   fragP = fragP -> fr_next )
  845.     {
  846.       fragP -> fr_address = address;
  847.       address += fragP -> fr_fix;
  848.       switch (fragP -> fr_type)
  849.     {
  850.     case rs_fill:
  851.       address += fragP -> fr_offset * fragP -> fr_var;
  852.       break;
  853.  
  854.     case rs_align:
  855.       address += relax_align (address, fragP -> fr_offset);
  856.       break;
  857.  
  858.     case rs_org:
  859.       /*
  860.        * Assume .org is nugatory. It will grow with 1st relax.
  861.        */
  862.       break;
  863.  
  864.     case rs_machine_dependent:
  865.       address += md_estimate_size_before_relax
  866.         (fragP, seg_N_TYPE [(int) segment_type]);
  867.       break;
  868.  
  869. #ifndef WORKING_DOT_WORD
  870.         /* Broken words don't concern us yet */
  871.     case rs_broken_word:
  872.         break;
  873. #endif
  874.  
  875.     default:
  876.       BAD_CASE( fragP -> fr_type );
  877.       break;
  878.     }            /* switch(fr_type) */
  879.     }                /* for each frag in the segment */
  880.  
  881.   /*
  882.    * Do relax().
  883.    */
  884.   {
  885.     register long int    stretch; /* May be any size, 0 or negative. */
  886.                 /* Cumulative number of addresses we have */
  887.                 /* relaxed this pass. */
  888.                 /* We may have relaxed more than one address. */
  889.     register long int stretched;  /* Have we stretched on this pass? */
  890.                   /* This is 'cuz stretch may be zero, when,
  891.                      in fact some piece of code grew, and
  892.                      another shrank.  If a branch instruction
  893.                      doesn't fit anymore, we could be scrod */
  894.  
  895.     do
  896.       {
  897.     stretch = stretched = 0;
  898.     for (fragP = segment_frag_root;  fragP;  fragP = fragP -> fr_next)
  899.       {
  900.         register long int    growth;
  901.         register long int    was_address;
  902.         /* register long int    var; */
  903.         register long int    offset;
  904.         register symbolS *    symbolP;
  905.         register long int    target;
  906.         register long int    after;
  907.         register long int    aim;
  908.  
  909.         was_address = fragP -> fr_address;
  910.         address = fragP -> fr_address += stretch;
  911.         symbolP = fragP -> fr_symbol;
  912.         offset = fragP -> fr_offset;
  913.         /* var = fragP -> fr_var; */
  914.         switch (fragP -> fr_type)
  915.           {
  916.           case rs_fill:    /* .fill never relaxes. */
  917.         growth = 0;
  918.         break;
  919.  
  920. #ifndef WORKING_DOT_WORD
  921.         /* JF:  This is RMS's idea.  I do *NOT* want to be blamed
  922.            for it I do not want to write it.  I do not want to have
  923.            anything to do with it.  This is not the proper way to
  924.            implement this misfeature. */
  925.           case rs_broken_word:
  926.             {
  927.         struct broken_word *lie;
  928.         struct broken_word *untruth;
  929.         extern int md_short_jump_size;
  930.         extern int md_long_jump_size;
  931.  
  932.             /* Yes this is ugly (storing the broken_word pointer
  933.                in the symbol slot).  Still, this whole chunk of
  934.                code is ugly, and I don't feel like doing anything
  935.                about it.  Think of it as stubbornness in action */
  936.         growth=0;
  937.             for(lie=(struct broken_word *)(fragP->fr_symbol);
  938.             lie && lie->dispfrag==fragP;
  939.             lie=lie->next_broken_word) {
  940.  
  941.             if(lie->added)
  942.                 continue;
  943.             offset=  lie->add->sy_frag->fr_address+lie->add->sy_value + lie->addnum -
  944.                 (lie->sub->sy_frag->fr_address+lie->sub->sy_value);
  945.             if(offset<=-32768 || offset>=32767) {
  946.                 if(flagseen['k'])
  947.                     as_warn(".word %s-%s+%ld didn't fit",lie->add->sy_name,lie->sub->sy_name,lie->addnum);
  948.                 lie->added=1;
  949.                 if(fragP->fr_subtype==0) {
  950.                     fragP->fr_subtype++;
  951.                     growth+=md_short_jump_size;
  952.                 }
  953.                 for(untruth=lie->next_broken_word;untruth && untruth->dispfrag==lie->dispfrag;untruth=untruth->next_broken_word)
  954.                     if(untruth->add->sy_frag==lie->add->sy_frag && untruth->add->sy_value==lie->add->sy_value) {
  955.                         untruth->added=2;
  956.                         untruth->use_jump=lie;
  957.                     }
  958.                 growth+=md_long_jump_size;
  959.             }
  960.             }
  961.         }
  962.         break;
  963. #endif
  964.           case rs_align:
  965.         growth = relax_align ((relax_addressT)(address + fragP -> fr_fix), offset)
  966.           - relax_align ((relax_addressT)(was_address +  fragP -> fr_fix), offset);
  967.         break;
  968.  
  969.           case rs_org:
  970.         target = offset;
  971.         if (symbolP)
  972.           {
  973.             know(   ((symbolP -> sy_type & N_TYPE) == N_ABS) || ((symbolP -> sy_type & N_TYPE) == N_DATA) || ((symbolP -> sy_type & N_TYPE) == N_TEXT));
  974.             know( symbolP -> sy_frag );
  975.             know( (symbolP->sy_type&N_TYPE)!=N_ABS || symbolP->sy_frag==&zero_address_frag );
  976.             target +=
  977.               symbolP -> sy_value
  978.             + symbolP -> sy_frag -> fr_address;
  979.           }
  980.         know( fragP -> fr_next );
  981.         after = fragP -> fr_next -> fr_address;
  982.         growth = ((target - after ) > 0) ? (target - after) : 0;
  983.                 /* Growth may be -ve, but variable part */
  984.                 /* of frag cannot have < 0 chars. */
  985.                 /* That is, we can't .org backwards. */
  986.  
  987.         growth -= stretch;    /* This is an absolute growth factor */
  988.         break;
  989.  
  990.           case rs_machine_dependent:
  991.         {
  992.           register const relax_typeS *    this_type;
  993.           register const relax_typeS *    start_type;
  994.           register relax_substateT    next_state;
  995.           register relax_substateT    this_state;
  996.  
  997.           start_type = this_type
  998.             = md_relax_table + (this_state = fragP -> fr_subtype);
  999.         target = offset;
  1000.         if (symbolP)
  1001.           {
  1002.  know(   ((symbolP -> sy_type & N_TYPE) == N_ABS) || ((symbolP -> sy_type &
  1003.  N_TYPE) == N_DATA) || ((symbolP -> sy_type & N_TYPE) == N_TEXT));
  1004.             know( symbolP -> sy_frag );
  1005.             know( (symbolP->sy_type&N_TYPE)!=N_ABS || symbolP->sy_frag==&zero_address_frag );
  1006.             target +=
  1007.               symbolP -> sy_value
  1008.             + symbolP -> sy_frag -> fr_address;
  1009.  
  1010.             /* If frag has yet to be reached on this pass,
  1011.                assume it will move by STRETCH just as we did.
  1012.                If this is not so, it will be because some frag
  1013.                between grows, and that will force another pass.  */
  1014.  
  1015.                    /* JF was just address */
  1016.                 /* JF also added is_dnrange hack */
  1017.                 /* There's gotta be a better/faster/etc way
  1018.                    to do this. . . */
  1019.             /* gnu@cygnus.com:  I changed this from > to >=
  1020.                because I ran into a zero-length frag (fr_fix=0)
  1021.                which was created when the obstack needed a new
  1022.                chunk JUST AFTER the opcode of a branch.  Since
  1023.                fr_fix is zero, fr_address of this frag is the same
  1024.                as fr_address of the next frag.  This
  1025.                zero-length frag was variable and jumped to .+2
  1026.                (in the next frag), but since the > comparison
  1027.                below failed (the two were =, not >), "stretch"
  1028.                was not added to the target.  Stretch was 178, so
  1029.                the offset appeared to be .-176 instead, which did
  1030.                not fit into a byte branch, so the assembler
  1031.                relaxed the branch to a word.  This didn't compare
  1032.                with what happened when the same source file was
  1033.                assembled on other machines, which is how I found it.
  1034.                You might want to think about what other places have
  1035.                trouble with zero length frags... */
  1036.  
  1037.             if (symbolP->sy_frag->fr_address >= was_address && is_dnrange(fragP,symbolP->sy_frag))
  1038.               target += stretch;
  1039.  
  1040.           }
  1041.           aim = target - address - fragP -> fr_fix;
  1042.           if (aim < 0)
  1043.             {
  1044.               /* Look backwards. */
  1045.               for (next_state = this_type -> rlx_more;  next_state;  )
  1046.             {
  1047.               if (aim >= this_type -> rlx_backward)
  1048.                   next_state = 0;
  1049.               else
  1050.                 {    /* Grow to next state. */
  1051.                   this_type = md_relax_table + (this_state = next_state);
  1052.                   next_state = this_type -> rlx_more;
  1053.                 }
  1054.             }
  1055.             }
  1056.           else
  1057.             {
  1058. #ifdef DONTDEF
  1059. /* JF these next few lines of code are for the mc68020 which can't handle short
  1060.    offsets of zero in branch instructions.  What a kludge! */
  1061.  if(aim==0 && this_state==(1<<2+0)) {    /* FOO hard encoded from m.c */
  1062.     aim=this_type->rlx_forward+1;    /* Force relaxation into word mode */
  1063.  }
  1064. #endif
  1065. /* JF end of 68020 code */
  1066.               /* Look forwards. */
  1067.               for (next_state = this_type -> rlx_more;  next_state;  )
  1068.             {
  1069.               if (aim <= this_type -> rlx_forward)
  1070.                   next_state = 0;
  1071.               else
  1072.                 {    /* Grow to next state. */
  1073.                   this_type = md_relax_table + (this_state = next_state);
  1074.                   next_state = this_type -> rlx_more;
  1075.                 }
  1076.             }
  1077.             }
  1078.           if (growth = this_type -> rlx_length - start_type -> rlx_length)
  1079.               fragP -> fr_subtype = this_state;
  1080.         }
  1081.         break;
  1082.  
  1083.           default:
  1084.         BAD_CASE( fragP -> fr_type );
  1085.         break;
  1086.           }
  1087.         if(growth) {
  1088.           stretch += growth;
  1089.           stretched++;
  1090.         }
  1091.       }            /* For each frag in the segment. */
  1092.       } while (stretched);    /* Until nothing further to relax. */
  1093.   }
  1094.  
  1095.   /*
  1096.    * We now have valid fr_address'es for each frag.
  1097.    */
  1098.  
  1099.   /*
  1100.    * All fr_address's are correct, relative to their own segment.
  1101.    * We have made all the fixS we will ever make.
  1102.    */
  1103. }                /* relax_segment() */
  1104.  
  1105. /*
  1106.  * Relax_align. Advance location counter to next address that has 'alignment'
  1107.  * lowest order bits all 0s.
  1108.  */
  1109.  
  1110. static relax_addressT        /* How many addresses does the .align take? */
  1111. relax_align (address, alignment)
  1112.      register relax_addressT    address; /* Address now. */
  1113.      register long int        alignment; /* Alignment (binary). */
  1114. {
  1115.   relax_addressT    mask;
  1116.   relax_addressT    new_address;
  1117.  
  1118.   mask = ~ ( (~0) << alignment );
  1119.   new_address = (address + mask) & (~ mask);
  1120.   return (new_address - address);
  1121. }
  1122.  
  1123. /*
  1124.  *            fixup_segment()
  1125.  */
  1126. static long int
  1127. fixup_segment (fixP, this_segment_type)
  1128.      register fixS *    fixP;
  1129.      int        this_segment_type; /* N_TYPE bits for segment. */
  1130. {
  1131.   register long int        seg_reloc_count;
  1132.         /* JF these all used to be local to the for loop, but GDB doesn't seem to be able to deal with them there, so I moved them here for a bit. */
  1133.       register symbolS *    add_symbolP;
  1134.       register symbolS *    sub_symbolP;
  1135.       register long int        add_number;
  1136.       register int        size;
  1137.       register char *        place;
  1138.       register long int        where;
  1139.       register char        pcrel;
  1140.       register fragS *        fragP;
  1141.       register int        add_symbol_N_TYPE;
  1142.  
  1143.  
  1144.   seg_reloc_count = 0;
  1145.   for ( ;  fixP;  fixP = fixP -> fx_next)
  1146.     {
  1147.       fragP       = fixP  -> fx_frag;
  1148.       know( fragP );
  1149.       where      = fixP  -> fx_where;
  1150.       place       = fragP -> fr_literal + where;
  1151.       size      = fixP  -> fx_size;
  1152.       add_symbolP = fixP  -> fx_addsy;
  1153.       sub_symbolP = fixP  -> fx_subsy;
  1154.       add_number  = fixP  -> fx_offset;
  1155.       pcrel      = fixP  -> fx_pcrel;
  1156.       if(add_symbolP)
  1157.     add_symbol_N_TYPE = add_symbolP -> sy_type & N_TYPE;
  1158.       if (sub_symbolP)
  1159.     {
  1160.       if(!add_symbolP)    /* Its just -sym */
  1161.         {
  1162.           if(sub_symbolP->sy_type!=N_ABS)
  1163.             as_warn("Negative of non-absolute symbol %s", sub_symbolP->sy_name);
  1164.           add_number-=sub_symbolP->sy_value;
  1165.         }
  1166.       else if (   ((sub_symbolP -> sy_type ^ add_symbol_N_TYPE) & N_TYPE) == 0
  1167.           && (   add_symbol_N_TYPE == N_DATA
  1168.           || add_symbol_N_TYPE == N_TEXT
  1169.           || add_symbol_N_TYPE == N_BSS
  1170.           || add_symbol_N_TYPE == N_ABS))
  1171.         {
  1172.           /* Difference of 2 symbols from same segment. */
  1173.           /* Can't make difference of 2 undefineds: 'value' means */
  1174.           /* something different for N_UNDF. */
  1175.           add_number += add_symbolP -> sy_value - sub_symbolP -> sy_value;
  1176.           add_symbolP = NULL;
  1177.           fixP -> fx_addsy = NULL;
  1178.         }
  1179.       else
  1180.         {
  1181.           /* Different segments in subtraction. */
  1182.           know( sub_symbolP -> sy_type != (N_ABS | N_EXT))
  1183.         if (sub_symbolP -> sy_type == N_ABS)
  1184.             add_number -= sub_symbolP -> sy_value;
  1185.         else
  1186.           {
  1187.                as_warn("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
  1188.                     seg_name[(int)N_TYPE_seg[sub_symbolP->sy_type&N_TYPE]],
  1189.                     sub_symbolP -> sy_name, fragP -> fr_address + where);
  1190.           }
  1191.         }
  1192.     }
  1193.       if (add_symbolP)
  1194.     {
  1195. #ifdef NeXT
  1196.         /*
  1197.          * Bit number 1 (i.e. the value 2) is used in fx_pcrel to indicate
  1198.          * a variation of pc relative fix-ups that force a relocation entry 
  1199.          * to be generated.  This is allows the NeXT linker to do scattered
  1200.          * relocation.
  1201.          */
  1202.       if (add_symbol_N_TYPE == this_segment_type && pcrel && !(pcrel & 2))
  1203. #else !defined(NeXT)
  1204.       if (add_symbol_N_TYPE == this_segment_type && pcrel)
  1205. #endif NeXT
  1206.         {
  1207.           /*
  1208.            * This fixup was made when the symbol's segment was
  1209.            * SEG_UNKNOWN, but it is now in the local segment.
  1210.            * So we know how to do the address without relocation.
  1211.            */
  1212.           add_number += add_symbolP -> sy_value;
  1213.           add_number -= size + where + fragP -> fr_address;
  1214.           pcrel = 0;    /* Lie. Don't want further pcrel processing. */
  1215.           fixP -> fx_addsy = NULL; /* No relocations please. */
  1216.           /*
  1217.            * It would be nice to check that the address does not overflow.
  1218.            * I didn't do this check because:
  1219.            * +  It is machine dependent in the general case (eg 32032)
  1220.            * +  Compiler output will never need this checking, so why
  1221.            *    slow down the usual case?
  1222.            */
  1223.         }
  1224.       else
  1225.         {
  1226.           switch (add_symbol_N_TYPE)
  1227.         {
  1228.         case N_ABS:
  1229.           add_number += add_symbolP -> sy_value;
  1230.           fixP -> fx_addsy = NULL;
  1231.           add_symbolP = NULL;
  1232.           break;
  1233.           
  1234.         case N_BSS:
  1235.         case N_DATA:
  1236.         case N_TEXT:
  1237.           seg_reloc_count ++;
  1238.           add_number += add_symbolP -> sy_value;
  1239.           break;
  1240.           
  1241.         case N_UNDF:
  1242.           seg_reloc_count ++;
  1243.           break;
  1244.           
  1245.         default:
  1246.           BAD_CASE( add_symbol_N_TYPE );
  1247.           break;
  1248.         }        /* switch on symbol seg */
  1249.         }            /* if not in local seg */
  1250.     }            /* if there was a + symbol */
  1251.       if (pcrel)
  1252.     {
  1253.       add_number -= size + where + fragP -> fr_address;
  1254.       if (add_symbolP == 0)
  1255.         {
  1256.           fixP -> fx_addsy = & abs_symbol;
  1257.           seg_reloc_count ++;
  1258.         }
  1259.     }
  1260.       /* OVE added fx_im_disp for ns32k and others */
  1261.       if (!fixP->fx_bit_fixP) {
  1262.     /* JF I hope this works . . . */
  1263.     if((size==1 && (add_number& ~0xFF)   && (add_number&~0xFF!=(-1&~0xFF))) ||
  1264.        (size==2 && (add_number& ~0xFFFF) && (add_number&~0xFFFF!=(-1&~0xFFFF))))
  1265.       as_warn("Fixup of %d too large for field width of %d",add_number, size);
  1266.  
  1267.     switch (fixP->fx_im_disp) {
  1268.     case 0:
  1269. #ifdef RISC
  1270.       fixP->fx_addnumber = add_number;
  1271.       md_number_to_imm(place, add_number, size, fixP, this_segment_type);
  1272. #else
  1273.       md_number_to_imm (place, add_number, size);
  1274.       /* OVE: the immediates, like disps, have lsb at lowest address */
  1275. #endif
  1276.       break;
  1277.     case 1:
  1278.       md_number_to_disp (place,
  1279.                  fixP->fx_pcrel ? add_number+fixP->fx_pcrel_adjust:add_number,
  1280.                  size);
  1281.       break;
  1282.     case 2: /* fix requested for .long .word etc */
  1283.       md_number_to_chars (place, add_number, size);
  1284.       break;
  1285.     default:
  1286.       as_fatal("Internal error in write.c in fixup_segment");
  1287.     } /* OVE: maybe one ought to put _imm _disp _chars in one md-func */
  1288.       } else {
  1289.     md_number_to_field (place, add_number, fixP->fx_bit_fixP);
  1290.       }
  1291.     }                /* For each fixS in this segment. */
  1292.   return (seg_reloc_count);
  1293. }                /* fixup_segment() */
  1294.  
  1295.  
  1296. /* Relocation entries for Mach-O files get emitted in write_Mach_O() */
  1297. #ifndef Mach_O
  1298. /* The sparc needs its own emit_relocations() */
  1299. #ifndef RISC
  1300. /*
  1301.  *        emit_relocations()
  1302.  *
  1303.  * Crawl along a fixS chain. Emit the segment's relocations.
  1304.  */
  1305. static void
  1306. emit_relocations (fixP, segment_address_in_file)
  1307.      register fixS *    fixP;    /* Fixup chain for this segment. */
  1308.      relax_addressT    segment_address_in_file;
  1309. {
  1310.   struct relocation_info    ri;
  1311.   register symbolS *        symbolP;
  1312.  
  1313.     /* JF this is for paranoia */
  1314.   bzero((char *)&ri,sizeof(ri));
  1315.   for ( ;  fixP;  fixP = fixP -> fx_next)
  1316.     {
  1317.       if (symbolP = fixP -> fx_addsy)
  1318.     {
  1319.  
  1320. #ifdef M88K
  1321.       ri.r_type = fixP->fx_r_type;
  1322. #endif
  1323. #ifndef hpux
  1324.         /* These two 'cuz of NS32K */
  1325.       ri . r_bsr        = fixP -> fx_bsr;
  1326.       ri . r_disp        = fixP -> fx_im_disp;
  1327. #endif
  1328.       ri . r_length        = nbytes_r_length [fixP -> fx_size];
  1329.       ri . r_pcrel        = fixP -> fx_pcrel;
  1330.       ri . r_address    = fixP -> fx_frag -> fr_address
  1331.         +   fixP -> fx_where
  1332.           - segment_address_in_file;
  1333.       if ((symbolP -> sy_type & N_TYPE) == N_UNDF)
  1334.         {
  1335.           ri . r_extern    = 1;
  1336.           ri . r_symbolnum    = symbolP -> sy_number;
  1337.         }
  1338.       else
  1339.         {
  1340.           ri . r_extern    = 0;
  1341.           ri . r_symbolnum    = symbolP -> sy_type & N_TYPE;
  1342.         }
  1343.  
  1344.       /* 
  1345.         The 68k machines assign bit-fields from higher bits to 
  1346.         lower bits ("left-to-right") within the int.  VAXen assign 
  1347.         bit-fields from lower bits to higher bits ("right-to-left").
  1348.         Both handle multi-byte numbers in their usual fashion
  1349.         (Big-endian and little-endian stuff).
  1350.         Thus we need a machine dependent routine to make
  1351.         sure the structure is written out correctly.  FUN!
  1352.        */
  1353.       md_ri_to_chars((char *) &ri, ri); 
  1354.       append (&next_object_file_charP, (char *)& ri, (unsigned long)sizeof(ri));
  1355.     }
  1356.     }
  1357.  
  1358. }
  1359. #endif
  1360. #endif !defined(Mach_O)
  1361.  
  1362. int
  1363. is_dnrange(f1,f2)
  1364. struct frag *f1,*f2;
  1365. {
  1366.     while(f1) {
  1367.         if(f1->fr_next==f2)
  1368.             return 1;
  1369.         f1=f1->fr_next;
  1370.     }
  1371.     return 0;
  1372. }
  1373. /* End: as-write.c */
  1374.