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