Home | History | Annotate | Download | only in libasm
      1 /* Create new subsection section in given section.
      2    Copyright (C) 2002 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      5 
      6    Red Hat elfutils is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by the
      8    Free Software Foundation; version 2 of the License.
      9 
     10    Red Hat elfutils is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License along
     16    with Red Hat elfutils; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     18 
     19    Red Hat elfutils is an included package of the Open Invention Network.
     20    An included package of the Open Invention Network is a package for which
     21    Open Invention Network licensees cross-license their patents.  No patent
     22    license is granted, either expressly or impliedly, by designation as an
     23    included package.  Should you wish to participate in the Open Invention
     24    Network licensing program, please visit www.openinventionnetwork.com
     25    <http://www.openinventionnetwork.com>.  */
     26 
     27 #ifdef HAVE_CONFIG_H
     28 # include <config.h>
     29 #endif
     30 
     31 #include <stdlib.h>
     32 
     33 #include <libasmP.h>
     34 #include <system.h>
     35 
     36 
     37 AsmScn_t *
     38 asm_newsubscn (asmscn, nr)
     39      AsmScn_t *asmscn;
     40      unsigned int nr;
     41 {
     42   AsmScn_t *runp;
     43   AsmScn_t *newp;
     44 
     45   /* Just return if no section is given.  The error must have been
     46      somewhere else.  */
     47   if (asmscn == NULL)
     48     return NULL;
     49 
     50   /* Determine whether there is already a subsection with this number.  */
     51   runp = asmscn->subsection_id == 0 ? asmscn : asmscn->data.up;
     52   while (1)
     53     {
     54       if (runp->subsection_id == nr)
     55 	/* Found it.  */
     56 	return runp;
     57 
     58       if (runp->subnext == NULL || runp->subnext->subsection_id > nr)
     59 	break;
     60 
     61       runp = runp->subnext;
     62     }
     63 
     64   newp = (AsmScn_t *) malloc (sizeof (AsmScn_t));
     65   if (newp == NULL)
     66     return NULL;
     67 
     68   /* Same assembler context than the original section.  */
     69   newp->ctx = runp->ctx;
     70 
     71   /* User provided the subsectio nID.  */
     72   newp->subsection_id = nr;
     73 
     74   /* Inherit the parent's type.  */
     75   newp->type = runp->type;
     76 
     77   /* Pointer to the zeroth subsection.  */
     78   newp->data.up = runp->subsection_id == 0 ? runp : runp->data.up;
     79 
     80   /* We start at offset zero.  */
     81   newp->offset = 0;
     82   /* And generic alignment.  */
     83   newp->max_align = 1;
     84 
     85   /* No output yet.  */
     86   newp->content = NULL;
     87 
     88   /* Inherit the fill pattern from the section this one is derived from.  */
     89   newp->pattern = asmscn->pattern;
     90 
     91   /* Enqueue at the right position in the list.  */
     92   newp->subnext = runp->subnext;
     93   runp->subnext = newp;
     94 
     95   return newp;
     96 }
     97