home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / cctools / as / layout.c < prev    next >
C/C++ Source or Header  |  1995-07-20  |  21KB  |  711 lines

  1. /* layout.c (was part of write.c in original GAS version)
  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. #include <stdlib.h>
  21. #include <string.h>
  22. #include "stuff/round.h"
  23. #include "as.h"
  24. #include "sections.h"
  25. #include "frags.h"
  26. #include "symbols.h"
  27. #include "fixes.h"
  28. #include "messages.h"
  29. #include "expr.h"
  30. #include "md.h"
  31. #include "obstack.h"
  32.  
  33. #ifdef SPARC
  34. /* internal relocation types not to be emitted */
  35. #define SPARC_RELOC_13 (127)
  36. #define SPARC_RELOC_22 (126)
  37. #endif
  38.  
  39. static void fixup_section(
  40.     fixS *fixP,
  41.     int nsect);
  42. static void relax_section(
  43.     struct frag *section_frag_root,
  44.     int nsect);
  45. static relax_addressT relax_align(
  46.     relax_addressT address,
  47.     long alignment);
  48. static int is_down_range(
  49.     struct frag *f1,
  50.     struct frag *f2);
  51.  
  52. /*
  53.  * layout_addresses() is called after all the assembly code has been read and
  54.  * fragments, symbols and fixups have been created.  This routine sets the
  55.  * address of the fragments and symbols.  Then it does the fixups of the frags
  56.  * and prepares the fixes so relocation entries can be created from them.
  57.  */
  58. void
  59. layout_addresses(
  60. void)
  61. {
  62.     struct frchain *frchainP;
  63.     fragS *fragP;
  64.     relax_addressT slide, tmp;
  65.     symbolS *symbolP;
  66.  
  67.     if(frchain_root == NULL)
  68.         return;
  69.  
  70.     /*
  71.      * If there is any current frag close it off.
  72.      */
  73.     if(frag_now != NULL && frag_now->fr_fix == 0){
  74.         frag_now->fr_fix = obstack_next_free(&frags) -
  75.                    frag_now->fr_literal;
  76.         frag_wane(frag_now);
  77.     }
  78.  
  79.     /*
  80.      * For every section, add a last ".fill 0" frag that will later be used
  81.      * as the ending address of that section.
  82.      */
  83.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  84.         /*
  85.          * We must do the obstack_finish(), so the next object we put on
  86.          * obstack frags will not appear to start at the fr_literal of the
  87.          * current frag.  Also, it ensures that the next object will begin
  88.          * on a address that is aligned correctly for the engine that runs
  89.          * the assembler.
  90.          */
  91.         obstack_finish(&frags);
  92.  
  93.         /*
  94.          * Make a fresh frag for the last frag.
  95.          */
  96.         frag_now = (fragS *)obstack_alloc(&frags, SIZEOF_STRUCT_FRAG);
  97.         memset(frag_now, '\0', SIZEOF_STRUCT_FRAG);
  98.         frag_now->fr_next = NULL;
  99.         obstack_finish(&frags);
  100.  
  101.         /*
  102.          * Append the new frag to current frchain.
  103.          */
  104.         frchainP->frch_last->fr_next = frag_now;
  105.         frchainP->frch_last = frag_now;
  106.         frag_wane(frag_now);
  107.  
  108.     }
  109.  
  110.     /*
  111.      * Now set the relitive addresses of frags within the section by
  112.      * relaxing each section.  That is all sections will start at address
  113.      * zero and addresses of the frags in that section will increase from
  114.      * there.
  115.      */
  116.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  117.         if((frchainP->frch_section.flags & SECTION_TYPE) == S_ZEROFILL)
  118.         continue;
  119.         /*
  120.          * This is done so in case md_estimate_size_before_relax() (called
  121.          * by relax_section) wants to make fixSs they are for this
  122.          * section.
  123.          */
  124.         frchain_now = frchainP;
  125.  
  126.         relax_section(frchainP->frch_root, frchainP->frch_nsect);
  127.     }
  128.  
  129.     /*
  130.      * Now set the absolute addresses of all frags by sliding the frags in
  131.      * each non-zerofill section by the address ranges taken up by the
  132.      * sections before it.
  133.      */ 
  134.     slide = 0;
  135.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  136.         if((frchainP->frch_section.flags & SECTION_TYPE) == S_ZEROFILL)
  137.         continue;
  138.         slide = round(slide, 1 << frchainP->frch_section.align);
  139.         tmp = frchainP->frch_last->fr_address;
  140.         if(slide != 0){
  141.         for(fragP = frchainP->frch_root; fragP; fragP = fragP->fr_next){
  142.             fragP->fr_address += slide;
  143.         }
  144.         }
  145.         slide += tmp;
  146.     }
  147.     /*
  148.      * Now with the non-zerofill section addresses set set all of the
  149.      * addresses of the zerofill sections.  Comming in the fr_address is
  150.      * the size of the section and going out it is the start address.  This
  151.      * will make layout_symbols() work out naturally.  The only funky thing
  152.      * is that section numbers do not end up in address order.
  153.      */
  154.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  155.         if((frchainP->frch_section.flags & SECTION_TYPE) != S_ZEROFILL)
  156.         continue;
  157.         slide = round(slide, 1 << frchainP->frch_section.align);
  158.  
  159.         tmp = frchainP->frch_root->fr_address;
  160.         frchainP->frch_root->fr_address = slide;
  161.         frchainP->frch_last->fr_address = tmp + slide;
  162.         slide += tmp;
  163.     }
  164.  
  165.     /*
  166.      * Set the symbol addresses based on there frag's address.
  167.      * First forward references are handled.
  168.      */
  169.     for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
  170.         if(symbolP->sy_forward != NULL){
  171.         if(symbolP->sy_nlist.n_type & N_STAB)
  172.             symbolP->sy_other = symbolP->sy_forward->sy_other;
  173.         symbolP->sy_value += symbolP->sy_forward->sy_value +
  174.                      symbolP->sy_forward->sy_frag->fr_address;
  175.         symbolP->sy_forward = 0;
  176.         }
  177.     }
  178.     for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
  179.         symbolP->sy_value += symbolP->sy_frag->fr_address;
  180.     }
  181.  
  182.     /*
  183.      * At this point the addresses of frags now reflect addresses we use in 
  184.      * the object file and the symbol values are correct.
  185.      * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
  186.      * Also converting any machine-dependent frags using md_convert_frag();
  187.      */
  188.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  189.         /*
  190.          * This is done so any fixes created by md_convert_frag() are for
  191.          * this section.
  192.          */
  193.         frchain_now = frchainP;
  194.  
  195.         for(fragP = frchainP->frch_root; fragP; fragP = fragP->fr_next){
  196.         switch(fragP->fr_type){
  197.         case rs_align:
  198.         case rs_org:
  199.             fragP->fr_type = rs_fill;
  200.             know(fragP->fr_var == 1);
  201.             know(fragP->fr_next != NULL);
  202.             fragP->fr_offset = fragP->fr_next->fr_address -
  203.                        fragP->fr_address -
  204.                        fragP->fr_fix;
  205.             break;
  206.  
  207.         case rs_fill:
  208.             break;
  209.  
  210.         case rs_machine_dependent:
  211.             md_convert_frag(fragP);
  212.             /*
  213.              * After md_convert_frag, we make the frag into a ".fill 0"
  214.              * md_convert_frag() should set up any fixSs and constants
  215.              * required.
  216.              */
  217.             frag_wane(fragP);
  218.             break;
  219.  
  220.         default:
  221.             BAD_CASE(fragP->fr_type);
  222.             break;
  223.         }
  224.         }
  225.     }
  226.  
  227.     /*
  228.      * For each section do the fixups for the frags.
  229.      */
  230.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  231.         fixup_section(frchainP->frch_fix_root, frchainP->frch_nsect);
  232.     }
  233. }
  234.  
  235. /*
  236.  * fixup_section() does the fixups of the frags and prepares the fixes so
  237.  * relocation entries can be created from them.  The fixups cause the contents
  238.  * of the frag to have the value for the fixup expression.  A fix structure that
  239.  * ends up with a non NULL fx_addsy will have a relocation entry created for it.
  240.  */
  241. static
  242. void
  243. fixup_section(
  244. fixS *fixP,
  245. int nsect)
  246. {
  247.     symbolS *add_symbolP;
  248.     symbolS *sub_symbolP;
  249.     long value;
  250.     int size;
  251.     char *place;
  252.     long where;
  253.     char pcrel;
  254.     fragS *fragP;
  255.     int    add_symbol_N_TYPE;
  256.     int    add_symbol_nsect;
  257.  
  258.     /*
  259.      * The general fix expression is "fx_addsy - fx_subsy + fx_offset".
  260.      * The goal is to put the result of this expression into the frag at
  261.      * "place" for size "size".  The value of the expression is calculated
  262.      * in the variable "value" and starts with just the fx_offset.
  263.      */
  264.     for( ; fixP != NULL; fixP = fixP->fx_next){
  265.         fragP       = fixP->fx_frag;
  266.         know(fragP);
  267.         where    = fixP->fx_where;
  268.         place       = fragP->fr_literal + where;
  269.         size    = fixP->fx_size;
  270.         add_symbolP = fixP->fx_addsy;
  271.         sub_symbolP = fixP->fx_subsy;
  272.         value      = fixP->fx_offset;
  273.         pcrel       = fixP->fx_pcrel;
  274.  
  275.         add_symbol_N_TYPE = 0;
  276.         add_symbol_nsect = 0;
  277.  
  278.         if(add_symbolP != NULL){
  279.         add_symbol_N_TYPE = add_symbolP->sy_type & N_TYPE;
  280.         if(add_symbol_N_TYPE == N_SECT)
  281.             add_symbol_nsect = add_symbolP->sy_other;
  282.         }
  283.  
  284.         /*
  285.          * Is there a subtract symbol?
  286.          */
  287.         if(sub_symbolP){
  288.         /* is it just -sym ? */
  289.         if(add_symbolP == NULL){
  290.             if(sub_symbolP->sy_type != N_ABS)
  291.             as_warn("Negative of non-absolute symbol %s",
  292.                 sub_symbolP->sy_name);
  293.             value -= sub_symbolP->sy_value;
  294.             fixP->fx_subsy = NULL;
  295.         }
  296.         /*
  297.          * There are both an add symbol and a subtract symbol at this
  298.          * point.
  299.          *
  300.          * If both symbols are absolute then just calculate the
  301.          * value of the fix expression and no relocation entry will be
  302.          * needed.
  303.          */
  304.         else if((sub_symbolP->sy_type & N_TYPE) == N_ABS &&
  305.                 (add_symbolP->sy_type & N_TYPE) == N_ABS){
  306.             value += add_symbolP->sy_value - sub_symbolP->sy_value;
  307.             add_symbolP = NULL;
  308.             fixP->fx_addsy = NULL; /* no relocation entry */
  309.             fixP->fx_subsy = NULL;
  310.         }
  311.         /*
  312.          * If both symbols are defined in a section then calculate the
  313.          * value of the fix expression and let a section difference
  314.          * relocation entry be created.
  315.          */
  316.         else if((sub_symbolP->sy_type & N_TYPE) == N_SECT &&
  317.                 (add_symbolP->sy_type & N_TYPE) == N_SECT){
  318.             /*
  319.              * If we are allowed to use the new features that are
  320.              * incompatible with 3.2 then just calculate the value and
  321.              * let this create a SECTDIFF relocation type.
  322.              */
  323.             if(flagseen['k']){
  324. #ifdef SPARC
  325.             // special case dealing with assembler internal relocation entries
  326.             // SPARC_RELOC_13 and RELOC_22. The can not be output and must
  327.             // be resolved
  328.             if ((fixP->fx_r_type == SPARC_RELOC_13) ||
  329.                     (fixP->fx_r_type == SPARC_RELOC_22)) {
  330.                 if (sub_symbolP->sy_other == add_symbolP->sy_other) {
  331.                     value += add_symbolP->sy_value -
  332.                     sub_symbolP->sy_value;
  333.                     add_symbolP = NULL;
  334.                     fixP->fx_addsy = NULL; /* no relocation entry */
  335.                     fixP->fx_subsy = NULL;
  336.                 } else {
  337.                     as_warn("Can't emit reloc type %u {-symbol \"%s\"} @ file "
  338.                     "address %ld (mode?).", fixP->fx_r_type, 
  339.                     sub_symbolP->sy_name, fragP->fr_address + where);
  340.                 }
  341.             } else
  342.                 value += add_symbolP->sy_value - sub_symbolP->sy_value;
  343. #else
  344.             value += add_symbolP->sy_value - sub_symbolP->sy_value;
  345. #endif
  346.             goto down;
  347.             }
  348.             else{
  349.             /*
  350.              * This logic works only if the two symbols can't later
  351.              * be separated by scattered loading.  To make sure that
  352.              * this can't happen we would have to make sure all
  353.              * symbols associated with addresses between these two
  354.              * symbols including the add symbol are of the Lx form
  355.              * and the -L flag is not see so they will not appear
  356.              * in the output (if they are not in the output then the
  357.              * link editor can't separate the chain of frags by
  358.              * scattered loading).  Since this code does not make
  359.              * sure of this it is broken.  But this is a known bug
  360.              * in the NeXT 3.2 and earilier releases so this code is
  361.              * if'ed !flagseen['k'] and will make it compatable with
  362.              * 3.2 and previous releases.
  363.              */
  364.             if(sub_symbolP->sy_other == add_symbolP->sy_other){
  365.                 value += add_symbolP->sy_value -
  366.                      sub_symbolP->sy_value;
  367.                 add_symbolP = NULL;
  368.                 fixP->fx_addsy = NULL; /* no relocation entry */
  369.                 fixP->fx_subsy = NULL;
  370.             }
  371.             else{
  372.                 as_warn("Can't emit reloc {- symbol \"%s\"} @ file "
  373.                     "address %ld (mode?).", sub_symbolP->sy_name,
  374.                     fragP->fr_address + where);
  375.             }
  376.             }
  377.         }
  378.         /*
  379.          * If the subtract symbol is absolute subtract it's value from
  380.          * the fix expression and let a relocation entry get created
  381.          * that is not a section difference type.
  382.          */
  383.         else if(sub_symbolP->sy_type == N_ABS){
  384.             value -= sub_symbolP->sy_value;
  385.             fixP->fx_subsy = NULL; /* no SECTDIFF relocation entry */
  386.         }
  387.         /*
  388.          * At this point we have something we can't generate a
  389.          * relocation entry for (two undefined symbols, etc.).
  390.          */
  391.             else{
  392.              as_warn("Can't emit reloc {- symbol \"%s\"} @ file "
  393.                  "address %ld.", sub_symbolP->sy_name,
  394.                  fragP->fr_address + where);
  395.         }
  396.         }
  397.  
  398.         /*
  399.          * If a there is an add symbol in the fixup expression then add
  400.          * the symbol value into the fixup expression's value.
  401.          */
  402.         if(add_symbolP){
  403.         /*
  404.          * If this symbol is in this section and is pc-relative and we
  405.          * do not want to force a pc-relative relocation entry (to
  406.          * support scattered loading) then just calculate the value.
  407.          */
  408.         if(add_symbol_nsect == nsect &&
  409.            pcrel && !(fixP->fx_pcrel_reloc)){
  410.             /*
  411.              * This fixup was made when the symbol's section was
  412.              * unknown, but it is now in this section. So we know how
  413.              * to do the address without relocation.
  414.              */
  415.             value += add_symbolP->sy_value;
  416.             value -= size + where + fragP->fr_address;
  417.             pcrel = 0;    /* Lie. Don't want further pcrel processing. */
  418.             fixP->fx_addsy = NULL; /* No relocations please. */
  419.             /*
  420.              * It would be nice to check that the address does not
  421.              * overflow.
  422.              * I didn't do this check because:
  423.              * +  It is machine dependent in the general case (eg 32032)
  424.              * +  Compiler output will never need this checking, so why
  425.              *    slow down the usual case?
  426.              */
  427.         }
  428.         else{
  429.             switch(add_symbol_N_TYPE){
  430.             case N_ABS:
  431.             value += add_symbolP->sy_value;
  432.             fixP->fx_addsy = NULL; /* no relocation entry */
  433.             add_symbolP = NULL;
  434.             break;
  435.             
  436.             case N_SECT:
  437.             value += add_symbolP->sy_value;
  438.             break;
  439.             
  440.             case N_UNDF:
  441.             break;
  442.             
  443.             default:
  444.             BAD_CASE(add_symbol_N_TYPE);
  445.             break;
  446.             }
  447.         }
  448.         }
  449. down:
  450.         /*
  451.          * If the fixup expression is pc-relative then the value of the pc
  452.          * will be added to the expression when the machine executes the
  453.          * the instruction so we adjust the fixup expression's value by
  454.          * subtracting off the pc value (where) and adjust for insn size.
  455.          */
  456.         if(pcrel){
  457.         value -= size + where + fragP->fr_address;
  458.         if(add_symbolP == NULL){
  459.             fixP->fx_addsy = &abs_symbol; /* force relocation entry */
  460.         }
  461.         }
  462.  
  463.         if((size == 1 && (value & 0xffffff00) &&
  464.                 ((value & 0xffffff80) != 0xffffff80)) ||
  465.            (size == 2 && (value & 0xffff8000) &&
  466.                 ((value & 0xffff8000) != 0xffff8000)))
  467.         as_warn("Fixup of %ld too large for field width of %d",
  468.             value, size);
  469.  
  470.         /*
  471.          * Now place the fix expression's value in the place for the size.
  472.          * And save the fix expression's value to be used when creating
  473.          * a relocation entry if required.
  474.          */
  475.         md_number_to_imm(place, value, size, fixP, nsect);
  476.         fixP->fx_value = value;
  477.     }
  478. }
  479.  
  480.  
  481. /*
  482.  * relax_section() here we set the fr_address values in the frags.
  483.  * After this, all frags in this segment have addresses that are correct
  484.  * relative to the section (that is the section starts at address zero).
  485.  * After all of the sections have been processed by this call and their sizes
  486.  * are know then they can be slid to their final address.
  487.  */
  488. static
  489. void
  490. relax_section(
  491. struct frag *frag_root,
  492. int nsect)
  493. {
  494.     struct frag *fragP;
  495.     relax_addressT address;
  496.  
  497.     long stretch; /* May be any size, 0 or negative. */
  498.           /* Cumulative number of addresses we have */
  499.           /* relaxed this pass. */
  500.           /* We may have relaxed more than one address. */
  501.     long stretched;  /* Have we stretched on this pass? */
  502.             /* This is 'cuz stretch may be zero, when,
  503.                in fact some piece of code grew, and
  504.                another shrank.  If a branch instruction
  505.                doesn't fit anymore, we need another pass */
  506.  
  507.     const relax_typeS *this_type;
  508.     const relax_typeS *start_type;
  509.     relax_substateT next_state;
  510.     relax_substateT this_state;
  511.  
  512.     long growth;
  513.     long was_address;
  514.     long offset;
  515.     symbolS *symbolP;
  516.     long target;
  517.     long after;
  518.     long aim;
  519.  
  520.     growth = 0;
  521.  
  522.     /*
  523.      * For each frag in segment count and store (a 1st guess of) fr_address.
  524.      */
  525.     address = 0;
  526.     for(fragP = frag_root; fragP != NULL; fragP = fragP->fr_next){
  527.         fragP->fr_address = address;
  528.         address += fragP->fr_fix;
  529.         switch(fragP->fr_type){
  530.         case rs_fill:
  531.         address += fragP->fr_offset * fragP->fr_var;
  532.         break;
  533.  
  534.         case rs_align:
  535.         address += relax_align(address, fragP->fr_offset);
  536.         break;
  537.  
  538.         case rs_org:
  539.         /*
  540.          * Assume .org is nugatory. It will grow with 1st relax.
  541.          */
  542.         break;
  543.  
  544.         case rs_machine_dependent:
  545.         address += md_estimate_size_before_relax(fragP, nsect);
  546.         break;
  547.  
  548.         default:
  549.         BAD_CASE(fragP->fr_type);
  550.         break;
  551.         }
  552.     }
  553.  
  554.     /*
  555.      * Do relax().
  556.      * Make repeated passes over the chain of frags allowing each frag to
  557.      * grow if needed.  On each pass each frag's address is incremented by
  558.      * the accumulated growth, kept in stretched.  Passes are continued 
  559.      * until there is no stretch on the previous pass.
  560.      */
  561.     do{
  562.         stretch = 0;
  563.         stretched = 0;
  564.         for(fragP = frag_root; fragP != NULL; fragP = fragP->fr_next){
  565.         was_address = fragP->fr_address;
  566.         fragP->fr_address += stretch;
  567.         address = fragP->fr_address;
  568.         symbolP = fragP->fr_symbol;
  569.         offset = fragP->fr_offset;
  570.         switch(fragP->fr_type){
  571.         case rs_fill:    /* .fill never relaxes. */
  572.             growth = 0;
  573.             break;
  574.  
  575.         case rs_align:
  576.             growth = relax_align((relax_addressT)
  577.                      (address + fragP->fr_fix), offset) -
  578.                  relax_align((relax_addressT)
  579.                      (was_address + fragP->fr_fix), offset);
  580.             break;
  581.  
  582.         case rs_org:
  583.             target = offset;
  584.             if(symbolP != NULL){
  585.             know(((symbolP->sy_type & N_TYPE) == N_ABS) ||
  586.                  ((symbolP->sy_type & N_TYPE) == N_SECT));
  587.             know(symbolP->sy_frag);
  588.             know((symbolP->sy_type & N_TYPE) != N_ABS ||
  589.                  symbolP->sy_frag == &zero_address_frag );
  590.             target += symbolP->sy_value +
  591.                   symbolP->sy_frag->fr_address;
  592.             }
  593.             know(fragP->fr_next);
  594.             after = fragP->fr_next->fr_address;
  595.             /*
  596.              * Growth may be negative, but variable part of frag cannot
  597.              * have < 0 chars. That is, we can't .org backwards.
  598.              */
  599.             growth = ((target - after ) > 0) ? (target - after) : 0;
  600.  
  601.             growth -= stretch;    /* This is an absolute growth factor */
  602.             break;
  603.  
  604.         case rs_machine_dependent:
  605.             this_state = fragP->fr_subtype;
  606.             this_type = md_relax_table + this_state;
  607.             start_type = this_type;
  608.  
  609.             target = offset;
  610.             if(symbolP){
  611.             know(((symbolP->sy_type & N_TYPE) == N_ABS) ||
  612.                  ((symbolP->sy_type & N_TYPE) == N_SECT));
  613.             know(symbolP->sy_frag);
  614.             know((symbolP->sy_type & N_TYPE) != N_ABS ||
  615.                  symbolP->sy_frag == &zero_address_frag);
  616.  
  617.             target += symbolP->sy_value +
  618.                   symbolP->sy_frag->fr_address;
  619.             /*
  620.              * If frag has yet to be reached on this pass,
  621.              * assume it will move by STRETCH just as we did.
  622.              * If this is not so, it will be because some frag
  623.              * between grows, and that will force another pass.
  624.              */
  625.             if(symbolP->sy_frag->fr_address >= was_address &&
  626.                is_down_range(fragP, symbolP->sy_frag))
  627.                 target += stretch;
  628.             }
  629.             aim = target - address - fragP->fr_fix;
  630.             if(aim < 0){
  631.             /* Look backwards. */
  632.             for(next_state = this_type->rlx_more; next_state; ){
  633.                 if(aim >= this_type->rlx_backward)
  634.                 next_state = 0;
  635.                 else{    /* Grow to next state. */
  636.                 this_state = next_state;
  637.                 this_type = md_relax_table + this_state;
  638.                 next_state = this_type->rlx_more;
  639.                 }
  640.             }
  641.             }
  642.             else{
  643.             /* Look forwards. */
  644.             for(next_state = this_type->rlx_more; next_state; ){
  645.                 if(aim <= this_type->rlx_forward)
  646.                 next_state = 0;
  647.                 else{    /* Grow to next state. */
  648.                 this_state = next_state;
  649.                 this_type = md_relax_table + this_state;
  650.                 next_state = this_type->rlx_more;
  651.                 }
  652.             }
  653.             }
  654.             if((growth = this_type->rlx_length -start_type->rlx_length))
  655.               fragP->fr_subtype = this_state;
  656.             break;
  657.  
  658.           default:
  659.               BAD_CASE(fragP->fr_type);
  660.               break;
  661.         }
  662.         if(growth) {
  663.             stretch += growth;
  664.             stretched++;
  665.         }
  666.         }            /* For each frag in the segment. */
  667.     }while(stretched);    /* Until nothing further to relax. */
  668.  
  669.     /*
  670.      * We now have valid fr_address'es for each frag.  All fr_address's
  671.      * are correct, relative to their own section.  We have made all the
  672.      * fixS for this section that will be made.
  673.      */
  674. }
  675.  
  676. /*
  677.  * Relax_align. Advance location counter to next address that has 'alignment'
  678.  * lowest order bits all 0s.
  679.  */
  680. static
  681. relax_addressT        /* How many addresses does the .align take? */
  682. relax_align(
  683. relax_addressT address, /* Address now. */
  684. long alignment)        /* Alignment (binary). */
  685. {
  686.     relax_addressT mask;
  687.     relax_addressT new_address;
  688.  
  689.     mask = ~ ( (~0) << alignment );
  690.     new_address = (address + mask) & (~ mask);
  691.     return(new_address - address);
  692. }
  693.  
  694. /*
  695.  * is_down_range() is used in relax_section() to determine it one fragment is
  696.  * after another to know if it will also be moved if the first is moved.
  697.  */
  698. static
  699. int
  700. is_down_range(
  701. struct frag *f1,
  702. struct frag *f2)
  703. {
  704.     while(f1){
  705.         if(f1->fr_next == f2)
  706.         return(1);
  707.         f1 = f1->fr_next;
  708.     }
  709.     return(0);
  710. }
  711.