Home | History | Annotate | Download | only in gas
      1 /* frags.c - manage frags -
      2    Copyright (C) 1987-2016 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 3, 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 the Free
     18    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
     19    02110-1301, USA.  */
     20 
     21 #include "as.h"
     22 #include "subsegs.h"
     23 #include "obstack.h"
     24 
     25 extern fragS zero_address_frag;
     26 extern fragS predefined_address_frag;
     27 
     28 static int totalfrags;
     29 
     30 int
     31 get_frag_count (void)
     32 {
     33   return totalfrags;
     34 }
     35 
     36 void
     37 clear_frag_count (void)
     38 {
     39   totalfrags = 0;
     40 }
     41 
     42 /* Initialization for frag routines.  */
     44 
     45 void
     46 frag_init (void)
     47 {
     48   zero_address_frag.fr_type = rs_fill;
     49   predefined_address_frag.fr_type = rs_fill;
     50 }
     51 
     52 /* Check that we're not trying to assemble into a section that can't
     54    allocate frags (currently, this is only possible in the absolute
     55    section), or into an mri common.  */
     56 
     57 static void
     58 frag_alloc_check (const struct obstack *ob)
     59 {
     60   if (ob->chunk_size == 0)
     61     {
     62       as_bad (_("attempt to allocate data in absolute section"));
     63       subseg_set (text_section, 0);
     64     }
     65 
     66   if (mri_common_symbol != NULL)
     67     {
     68       as_bad (_("attempt to allocate data in common section"));
     69       mri_common_symbol = NULL;
     70     }
     71 }
     72 
     73 /* Allocate a frag on the specified obstack.
     74    Call this routine from everywhere else, so that all the weird alignment
     75    hackery can be done in just one place.  */
     76 
     77 fragS *
     78 frag_alloc (struct obstack *ob)
     79 {
     80   fragS *ptr;
     81   int oalign;
     82 
     83   (void) obstack_alloc (ob, 0);
     84   oalign = obstack_alignment_mask (ob);
     85   obstack_alignment_mask (ob) = 0;
     86   ptr = (fragS *) obstack_alloc (ob, SIZEOF_STRUCT_FRAG);
     87   obstack_alignment_mask (ob) = oalign;
     88   memset (ptr, 0, SIZEOF_STRUCT_FRAG);
     89   totalfrags++;
     90   return ptr;
     91 }
     92 
     93 /* Try to augment current frag by nchars chars.
     95    If there is no room, close of the current frag with a ".fill 0"
     96    and begin a new frag. Unless the new frag has nchars chars available
     97    do not return. Do not set up any fields of *now_frag.  */
     98 
     99 void
    100 frag_grow (size_t nchars)
    101 {
    102   if (obstack_room (&frchain_now->frch_obstack) < nchars)
    103     {
    104       size_t oldc;
    105       size_t newc;
    106 
    107       /* Try to allocate a bit more than needed right now.  But don't do
    108          this if we would waste too much memory.  Especially necessary
    109          for extremely big (like 2GB initialized) frags.  */
    110       if (nchars < 0x10000)
    111         newc = 2 * nchars;
    112       else
    113         newc = nchars + 0x10000;
    114       newc += SIZEOF_STRUCT_FRAG;
    115 
    116       /* Check for possible overflow.  */
    117       if (newc < nchars)
    118         as_fatal (_("can't extend frag %lu chars"), (unsigned long) nchars);
    119 
    120       /* Force to allocate at least NEWC bytes, but not less than the
    121          default.  */
    122       oldc = obstack_chunk_size (&frchain_now->frch_obstack);
    123       if (newc > oldc)
    124 	obstack_chunk_size (&frchain_now->frch_obstack) = newc;
    125 
    126       while (obstack_room (&frchain_now->frch_obstack) < nchars)
    127         {
    128           /* Not enough room in this frag.  Close it and start a new one.
    129              This must be done in a loop because the created frag may not
    130              be big enough if the current obstack chunk is used.  */
    131           frag_wane (frag_now);
    132           frag_new (0);
    133         }
    134 
    135       /* Restore the old chunk size.  */
    136       obstack_chunk_size (&frchain_now->frch_obstack) = oldc;
    137     }
    138 }
    139 
    140 /* Call this to close off a completed frag, and start up a new (empty)
    142    frag, in the same subsegment as the old frag.
    143    [frchain_now remains the same but frag_now is updated.]
    144    Because this calculates the correct value of fr_fix by
    145    looking at the obstack 'frags', it needs to know how many
    146    characters at the end of the old frag belong to the maximal
    147    variable part;  The rest must belong to fr_fix.
    148    It doesn't actually set up the old frag's fr_var.  You may have
    149    set fr_var == 1, but allocated 10 chars to the end of the frag;
    150    In this case you pass old_frags_var_max_size == 10.
    151    In fact, you may use fr_var for something totally unrelated to the
    152    size of the variable part of the frag;  None of the generic frag
    153    handling code makes use of fr_var.
    154 
    155    Make a new frag, initialising some components. Link new frag at end
    156    of frchain_now.  */
    157 
    158 void
    159 frag_new (size_t old_frags_var_max_size
    160 	  /* Number of chars (already allocated on obstack frags) in
    161 	     variable_length part of frag.  */)
    162 {
    163   fragS *former_last_fragP;
    164   frchainS *frchP;
    165 
    166   gas_assert (frchain_now->frch_last == frag_now);
    167 
    168   /* Fix up old frag's fr_fix.  */
    169   frag_now->fr_fix = frag_now_fix_octets () - old_frags_var_max_size;
    170   /* Make sure its type is valid.  */
    171   gas_assert (frag_now->fr_type != 0);
    172 
    173   /* This will align the obstack so the next struct we allocate on it
    174      will begin at a correct boundary.  */
    175   obstack_finish (&frchain_now->frch_obstack);
    176   frchP = frchain_now;
    177   know (frchP);
    178   former_last_fragP = frchP->frch_last;
    179   gas_assert (former_last_fragP != 0);
    180   gas_assert (former_last_fragP == frag_now);
    181   frag_now = frag_alloc (&frchP->frch_obstack);
    182 
    183   frag_now->fr_file = as_where (&frag_now->fr_line);
    184 
    185   /* Generally, frag_now->points to an address rounded up to next
    186      alignment.  However, characters will add to obstack frags
    187      IMMEDIATELY after the struct frag, even if they are not starting
    188      at an alignment address.  */
    189   former_last_fragP->fr_next = frag_now;
    190   frchP->frch_last = frag_now;
    191 
    192 #ifndef NO_LISTING
    193   {
    194     extern struct list_info_struct *listing_tail;
    195     frag_now->line = listing_tail;
    196   }
    197 #endif
    198 
    199   gas_assert (frchain_now->frch_last == frag_now);
    200 
    201   frag_now->fr_next = NULL;
    202 }
    203 
    204 /* Start a new frag unless we have n more chars of room in the current frag.
    206    Close off the old frag with a .fill 0.
    207 
    208    Return the address of the 1st char to write into. Advance
    209    frag_now_growth past the new chars.  */
    210 
    211 char *
    212 frag_more (size_t nchars)
    213 {
    214   char *retval;
    215 
    216   frag_alloc_check (&frchain_now->frch_obstack);
    217   frag_grow (nchars);
    218   retval = obstack_next_free (&frchain_now->frch_obstack);
    219   obstack_blank_fast (&frchain_now->frch_obstack, nchars);
    220   return retval;
    221 }
    222 
    223 /* Close the current frag, setting its fields for a relaxable frag.  Start a
    225    new frag.  */
    226 
    227 static void
    228 frag_var_init (relax_stateT type, size_t max_chars, size_t var,
    229 	       relax_substateT subtype, symbolS *symbol, offsetT offset,
    230                char *opcode)
    231 {
    232   frag_now->fr_var = var;
    233   frag_now->fr_type = type;
    234   frag_now->fr_subtype = subtype;
    235   frag_now->fr_symbol = symbol;
    236   frag_now->fr_offset = offset;
    237   frag_now->fr_opcode = opcode;
    238 #ifdef USING_CGEN
    239   frag_now->fr_cgen.insn = 0;
    240   frag_now->fr_cgen.opindex = 0;
    241   frag_now->fr_cgen.opinfo = 0;
    242 #endif
    243 #ifdef TC_FRAG_INIT
    244   TC_FRAG_INIT (frag_now);
    245 #endif
    246   frag_now->fr_file = as_where (&frag_now->fr_line);
    247 
    248   frag_new (max_chars);
    249 }
    250 
    251 /* Start a new frag unless we have max_chars more chars of room in the
    252    current frag.  Close off the old frag with a .fill 0.
    253 
    254    Set up a machine_dependent relaxable frag, then start a new frag.
    255    Return the address of the 1st char of the var part of the old frag
    256    to write into.  */
    257 
    258 char *
    259 frag_var (relax_stateT type, size_t max_chars, size_t var,
    260 	  relax_substateT subtype, symbolS *symbol, offsetT offset,
    261 	  char *opcode)
    262 {
    263   char *retval;
    264 
    265   frag_grow (max_chars);
    266   retval = obstack_next_free (&frchain_now->frch_obstack);
    267   obstack_blank_fast (&frchain_now->frch_obstack, max_chars);
    268   frag_var_init (type, max_chars, var, subtype, symbol, offset, opcode);
    269   return retval;
    270 }
    271 
    272 /* OVE: This variant of frag_var assumes that space for the tail has been
    274 	allocated by caller.
    275 	No call to frag_grow is done.  */
    276 
    277 char *
    278 frag_variant (relax_stateT type, size_t max_chars, size_t var,
    279 	      relax_substateT subtype, symbolS *symbol, offsetT offset,
    280 	      char *opcode)
    281 {
    282   char *retval;
    283 
    284   retval = obstack_next_free (&frchain_now->frch_obstack);
    285   frag_var_init (type, max_chars, var, subtype, symbol, offset, opcode);
    286 
    287   return retval;
    288 }
    289 
    290 /* Reduce the variable end of a frag to a harmless state.  */
    292 
    293 void
    294 frag_wane (fragS *fragP)
    295 {
    296   fragP->fr_type = rs_fill;
    297   fragP->fr_offset = 0;
    298   fragP->fr_var = 0;
    299 }
    300 
    301 /* Return the number of bytes by which the current frag can be grown.  */
    303 
    304 size_t
    305 frag_room (void)
    306 {
    307   return obstack_room (&frchain_now->frch_obstack);
    308 }
    309 
    310 /* Make an alignment frag.  The size of this frag will be adjusted to
    312    force the next frag to have the appropriate alignment.  ALIGNMENT
    313    is the power of two to which to align.  FILL_CHARACTER is the
    314    character to use to fill in any bytes which are skipped.  MAX is
    315    the maximum number of characters to skip when doing the alignment,
    316    or 0 if there is no maximum.  */
    317 
    318 void
    319 frag_align (int alignment, int fill_character, int max)
    320 {
    321   if (now_seg == absolute_section)
    322     {
    323       addressT new_off;
    324       addressT mask;
    325 
    326       mask = (~(addressT) 0) << alignment;
    327       new_off = (abs_section_offset + ~mask) & mask;
    328       if (max == 0 || new_off - abs_section_offset <= (addressT) max)
    329 	abs_section_offset = new_off;
    330     }
    331   else
    332     {
    333       char *p;
    334 
    335       p = frag_var (rs_align, 1, 1, (relax_substateT) max,
    336 		    (symbolS *) 0, (offsetT) alignment, (char *) 0);
    337       *p = fill_character;
    338     }
    339 }
    340 
    341 /* Make an alignment frag like frag_align, but fill with a repeating
    342    pattern rather than a single byte.  ALIGNMENT is the power of two
    343    to which to align.  FILL_PATTERN is the fill pattern to repeat in
    344    the bytes which are skipped.  N_FILL is the number of bytes in
    345    FILL_PATTERN.  MAX is the maximum number of characters to skip when
    346    doing the alignment, or 0 if there is no maximum.  */
    347 
    348 void
    349 frag_align_pattern (int alignment, const char *fill_pattern,
    350 		    size_t n_fill, int max)
    351 {
    352   char *p;
    353 
    354   p = frag_var (rs_align, n_fill, n_fill, (relax_substateT) max,
    355 		(symbolS *) 0, (offsetT) alignment, (char *) 0);
    356   memcpy (p, fill_pattern, n_fill);
    357 }
    358 
    359 /* The NOP_OPCODE is for the alignment fill value.  Fill it with a nop
    360    instruction so that the disassembler does not choke on it.  */
    361 #ifndef NOP_OPCODE
    362 #define NOP_OPCODE 0x00
    363 #endif
    364 
    365 /* Use this to restrict the amount of memory allocated for representing
    366    the alignment code.  Needs to be large enough to hold any fixed sized
    367    prologue plus the replicating portion.  */
    368 #ifndef MAX_MEM_FOR_RS_ALIGN_CODE
    369   /* Assume that if HANDLE_ALIGN is not defined then no special action
    370      is required to code fill, which means that we get just repeat the
    371      one NOP_OPCODE byte.  */
    372 # ifndef HANDLE_ALIGN
    373 #  define MAX_MEM_FOR_RS_ALIGN_CODE  1
    374 # else
    375 #  define MAX_MEM_FOR_RS_ALIGN_CODE  ((1 << alignment) - 1)
    376 # endif
    377 #endif
    378 
    379 void
    380 frag_align_code (int alignment, int max)
    381 {
    382   char *p;
    383 
    384   p = frag_var (rs_align_code, MAX_MEM_FOR_RS_ALIGN_CODE, 1,
    385 		(relax_substateT) max, (symbolS *) 0,
    386 		(offsetT) alignment, (char *) 0);
    387   *p = NOP_OPCODE;
    388 }
    389 
    390 addressT
    391 frag_now_fix_octets (void)
    392 {
    393   if (now_seg == absolute_section)
    394     return abs_section_offset;
    395 
    396   return ((char *) obstack_next_free (&frchain_now->frch_obstack)
    397 	  - frag_now->fr_literal);
    398 }
    399 
    400 addressT
    401 frag_now_fix (void)
    402 {
    403   return frag_now_fix_octets () / OCTETS_PER_BYTE;
    404 }
    405 
    406 void
    407 frag_append_1_char (int datum)
    408 {
    409   frag_alloc_check (&frchain_now->frch_obstack);
    410   if (obstack_room (&frchain_now->frch_obstack) <= 1)
    411     {
    412       frag_wane (frag_now);
    413       frag_new (0);
    414     }
    415   obstack_1grow (&frchain_now->frch_obstack, datum);
    416 }
    417 
    418 /* Return TRUE if FRAG1 and FRAG2 have a fixed relationship between
    419    their start addresses.  Set OFFSET to the difference in address
    420    not already accounted for in the frag FR_ADDRESS.  */
    421 
    422 bfd_boolean
    423 frag_offset_fixed_p (const fragS *frag1, const fragS *frag2, offsetT *offset)
    424 {
    425   const fragS *frag;
    426   offsetT off;
    427 
    428   /* Start with offset initialised to difference between the two frags.
    429      Prior to assigning frag addresses this will be zero.  */
    430   off = frag1->fr_address - frag2->fr_address;
    431   if (frag1 == frag2)
    432     {
    433       *offset = off;
    434       return TRUE;
    435     }
    436 
    437   /* Maybe frag2 is after frag1.  */
    438   frag = frag1;
    439   while (frag->fr_type == rs_fill)
    440     {
    441       off += frag->fr_fix + frag->fr_offset * frag->fr_var;
    442       frag = frag->fr_next;
    443       if (frag == NULL)
    444 	break;
    445       if (frag == frag2)
    446 	{
    447 	  *offset = off;
    448 	  return TRUE;
    449 	}
    450     }
    451 
    452   /* Maybe frag1 is after frag2.  */
    453   off = frag1->fr_address - frag2->fr_address;
    454   frag = frag2;
    455   while (frag->fr_type == rs_fill)
    456     {
    457       off -= frag->fr_fix + frag->fr_offset * frag->fr_var;
    458       frag = frag->fr_next;
    459       if (frag == NULL)
    460 	break;
    461       if (frag == frag1)
    462 	{
    463 	  *offset = off;
    464 	  return TRUE;
    465 	}
    466     }
    467 
    468   return FALSE;
    469 }
    470