Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* subsegs.c - subsegments -
  2.    Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
  3.    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009
  4.    Free Software Foundation, Inc.
  5.  
  6.    This file is part of GAS, the GNU Assembler.
  7.  
  8.    GAS is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 3, or (at your option)
  11.    any later version.
  12.  
  13.    GAS is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with GAS; see the file COPYING.  If not, write to the Free
  20.    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  21.    02110-1301, USA.  */
  22.  
  23. /* Segments & sub-segments.  */
  24.  
  25. #include "as.h"
  26.  
  27. #include "subsegs.h"
  28. #include "obstack.h"
  29.  
  30. frchainS *frchain_now;
  31.  
  32. static struct obstack frchains;
  33.  
  34. static fragS dummy_frag;
  35.  
  36. void
  37. subsegs_begin (void)
  38. {
  39.   obstack_begin (&frchains, chunksize);
  40. #if __GNUC__ >= 2
  41.   obstack_alignment_mask (&frchains) = __alignof__ (frchainS) - 1;
  42. #endif
  43.  
  44.   frchain_now = NULL;           /* Warn new_subseg() that we are booting.  */
  45.   frag_now = &dummy_frag;
  46. }
  47. /*
  48.  *                      subseg_change()
  49.  *
  50.  * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the
  51.  * subsegment. If we are already in the correct subsegment, change nothing.
  52.  * This is used eg as a worker for subseg_set [which does make a new frag_now]
  53.  * and for changing segments after we have read the source. We construct eg
  54.  * fixSs even after the source file is read, so we do have to keep the
  55.  * segment context correct.
  56.  */
  57. void
  58. subseg_change (register segT seg, register int subseg)
  59. {
  60.   segment_info_type *seginfo = seg_info (seg);
  61.   now_seg = seg;
  62.   now_subseg = subseg;
  63.  
  64.   if (! seginfo)
  65.     {
  66.       seginfo = (segment_info_type *) xcalloc (1, sizeof (*seginfo));
  67.       seginfo->bfd_section = seg;
  68.       bfd_set_section_userdata (stdoutput, seg, seginfo);
  69.     }
  70. }
  71. static void
  72. subseg_set_rest (segT seg, subsegT subseg)
  73. {
  74.   frchainS *frcP;               /* crawl frchain chain */
  75.   frchainS **lastPP;            /* address of last pointer */
  76.   frchainS *newP;               /* address of new frchain */
  77.   segment_info_type *seginfo;
  78.  
  79.   mri_common_symbol = NULL;
  80.  
  81.   if (frag_now && frchain_now)
  82.     frchain_now->frch_frag_now = frag_now;
  83.  
  84.   gas_assert (frchain_now == 0
  85.           || frchain_now->frch_last == frag_now);
  86.  
  87.   subseg_change (seg, (int) subseg);
  88.  
  89.   seginfo = seg_info (seg);
  90.  
  91.   /* Attempt to find or make a frchain for that subsection.
  92.      We keep the list sorted by subsection number.  */
  93.   for (frcP = *(lastPP = &seginfo->frchainP);
  94.        frcP != NULL;
  95.        frcP = *(lastPP = &frcP->frch_next))
  96.     if (frcP->frch_subseg >= subseg)
  97.       break;
  98.  
  99.   if (frcP == NULL || frcP->frch_subseg != subseg)
  100.     {
  101.       /* This should be the only code that creates a frchainS.  */
  102.  
  103.       newP = (frchainS *) obstack_alloc (&frchains, sizeof (frchainS));
  104.       newP->frch_subseg = subseg;
  105.       newP->fix_root = NULL;
  106.       newP->fix_tail = NULL;
  107.       obstack_begin (&newP->frch_obstack, chunksize);
  108. #if __GNUC__ >= 2
  109.       obstack_alignment_mask (&newP->frch_obstack) = __alignof__ (fragS) - 1;
  110. #endif
  111.       newP->frch_frag_now = frag_alloc (&newP->frch_obstack);
  112.       newP->frch_frag_now->fr_type = rs_fill;
  113.       newP->frch_cfi_data = NULL;
  114.  
  115.       newP->frch_root = newP->frch_last = newP->frch_frag_now;
  116.  
  117.       *lastPP = newP;
  118.       newP->frch_next = frcP;
  119.       frcP = newP;
  120.     }
  121.  
  122.   frchain_now = frcP;
  123.   frag_now = frcP->frch_frag_now;
  124.  
  125.   gas_assert (frchain_now->frch_last == frag_now);
  126. }
  127.  
  128. /*
  129.  *                      subseg_set(segT, subsegT)
  130.  *
  131.  * If you attempt to change to the current subsegment, nothing happens.
  132.  *
  133.  * In:  segT, subsegT code for new subsegment.
  134.  *      frag_now -> incomplete frag for current subsegment.
  135.  *      If frag_now==NULL, then there is no old, incomplete frag, so
  136.  *      the old frag is not closed off.
  137.  *
  138.  * Out: now_subseg, now_seg updated.
  139.  *      Frchain_now points to the (possibly new) struct frchain for this
  140.  *      sub-segment.
  141.  */
  142.  
  143. segT
  144. subseg_get (const char *segname, int force_new)
  145. {
  146.   segT secptr;
  147.   segment_info_type *seginfo;
  148.   const char *now_seg_name = (now_seg
  149.                               ? bfd_get_section_name (stdoutput, now_seg)
  150.                               : 0);
  151.  
  152.   if (!force_new
  153.       && now_seg_name
  154.       && (now_seg_name == segname
  155.           || !strcmp (now_seg_name, segname)))
  156.     return now_seg;
  157.  
  158.   if (!force_new)
  159.     secptr = bfd_make_section_old_way (stdoutput, segname);
  160.   else
  161.     secptr = bfd_make_section_anyway (stdoutput, segname);
  162.  
  163.   seginfo = seg_info (secptr);
  164.   if (! seginfo)
  165.     {
  166.       secptr->output_section = secptr;
  167.       seginfo = (segment_info_type *) xcalloc (1, sizeof (*seginfo));
  168.       seginfo->bfd_section = secptr;
  169.       bfd_set_section_userdata (stdoutput, secptr, seginfo);
  170.     }
  171.   return secptr;
  172. }
  173.  
  174. segT
  175. subseg_new (const char *segname, subsegT subseg)
  176. {
  177.   segT secptr;
  178.  
  179.   secptr = subseg_get (segname, 0);
  180.   subseg_set_rest (secptr, subseg);
  181.   return secptr;
  182. }
  183.  
  184. /* Like subseg_new, except a new section is always created, even if
  185.    a section with that name already exists.  */
  186. segT
  187. subseg_force_new (const char *segname, subsegT subseg)
  188. {
  189.   segT secptr;
  190.  
  191.   secptr = subseg_get (segname, 1);
  192.   subseg_set_rest (secptr, subseg);
  193.   return secptr;
  194. }
  195.  
  196. void
  197. subseg_set (segT secptr, subsegT subseg)
  198. {
  199.   if (! (secptr == now_seg && subseg == now_subseg))
  200.     subseg_set_rest (secptr, subseg);
  201.   mri_common_symbol = NULL;
  202. }
  203.  
  204. #ifndef obj_sec_sym_ok_for_reloc
  205. #define obj_sec_sym_ok_for_reloc(SEC)   0
  206. #endif
  207.  
  208. symbolS *
  209. section_symbol (segT sec)
  210. {
  211.   segment_info_type *seginfo = seg_info (sec);
  212.   symbolS *s;
  213.  
  214.   if (seginfo == 0)
  215.     abort ();
  216.   if (seginfo->sym)
  217.     return seginfo->sym;
  218.  
  219. #ifndef EMIT_SECTION_SYMBOLS
  220. #define EMIT_SECTION_SYMBOLS 1
  221. #endif
  222.  
  223.   if (! EMIT_SECTION_SYMBOLS || symbol_table_frozen)
  224.     {
  225.       /* Here we know it won't be going into the symbol table.  */
  226.       s = symbol_create (sec->symbol->name, sec, 0, &zero_address_frag);
  227.     }
  228.   else
  229.     {
  230.       segT seg;
  231.       s = symbol_find (sec->symbol->name);
  232.       /* We have to make sure it is the right symbol when we
  233.          have multiple sections with the same section name.  */
  234.       if (s == NULL
  235.           || ((seg = S_GET_SEGMENT (s)) != sec
  236.               && seg != undefined_section))
  237.         s = symbol_new (sec->symbol->name, sec, 0, &zero_address_frag);
  238.       else if (seg == undefined_section)
  239.         {
  240.           S_SET_SEGMENT (s, sec);
  241.           symbol_set_frag (s, &zero_address_frag);
  242.         }
  243.     }
  244.  
  245.   S_CLEAR_EXTERNAL (s);
  246.  
  247.   /* Use the BFD section symbol, if possible.  */
  248.   if (obj_sec_sym_ok_for_reloc (sec))
  249.     symbol_set_bfdsym (s, sec->symbol);
  250.   else
  251.     symbol_get_bfdsym (s)->flags |= BSF_SECTION_SYM;
  252.  
  253.   seginfo->sym = s;
  254.   return s;
  255. }
  256.  
  257. /* Return whether the specified segment is thought to hold text.  */
  258.  
  259. int
  260. subseg_text_p (segT sec)
  261. {
  262.   return (bfd_get_section_flags (stdoutput, sec) & SEC_CODE) != 0;
  263. }
  264.  
  265. /* Return non zero if SEC has at least one byte of data.  It is
  266.    possible that we'll return zero even on a non-empty section because
  267.    we don't know all the fragment types, and it is possible that an
  268.    fr_fix == 0 one still contributes data.  Think of this as
  269.    seg_definitely_not_empty_p.  */
  270.  
  271. int
  272. seg_not_empty_p (segT sec ATTRIBUTE_UNUSED)
  273. {
  274.   segment_info_type *seginfo = seg_info (sec);
  275.   frchainS *chain;
  276.   fragS *frag;
  277.  
  278.   if (!seginfo)
  279.     return 0;
  280.  
  281.   for (chain = seginfo->frchainP; chain; chain = chain->frch_next)
  282.     {
  283.       for (frag = chain->frch_root; frag; frag = frag->fr_next)
  284.         if (frag->fr_fix)
  285.           return 1;
  286.       if (obstack_next_free (&chain->frch_obstack)
  287.           != chain->frch_last->fr_literal)
  288.         return 1;
  289.     }
  290.   return 0;
  291. }
  292.  
  293. void
  294. subsegs_print_statistics (FILE *file)
  295. {
  296.   frchainS *frchp;
  297.   asection *s;
  298.  
  299.   fprintf (file, "frag chains:\n");
  300.   for (s = stdoutput->sections; s; s = s->next)
  301.     {
  302.       segment_info_type *seginfo;
  303.  
  304.       /* Skip gas-internal sections.  */
  305.       if (segment_name (s)[0] == '*')
  306.         continue;
  307.  
  308.       seginfo = seg_info (s);
  309.       if (!seginfo)
  310.         continue;
  311.  
  312.       for (frchp = seginfo->frchainP; frchp; frchp = frchp->frch_next)
  313.         {
  314.           int count = 0;
  315.           fragS *fragp;
  316.  
  317.           for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
  318.             count++;
  319.  
  320.           fprintf (file, "\n");
  321.           fprintf (file, "\t%p %-10s\t%10d frags\n", (void *) frchp,
  322.                    segment_name (s), count);
  323.         }
  324.     }
  325. }
  326.  
  327. /* end of subsegs.c */
  328.