2 Copyright (C) 1998,1999,2000,2001,2002,2005,2009,2014,2015 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
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
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
19 or both in parallel, as here.
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.
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/>. */
46 Elf_Scn *result = NULL;
52 /* We rely on the prefix of the `elf', `elf32', and `elf64' element
54 assert (offsetof (Elf, state.elf.scns_last)
55 == offsetof (Elf, state.elf32.scns_last));
56 assert (offsetof (Elf, state.elf.scns_last)
57 == offsetof (Elf, state.elf64.scns_last));
58 assert (offsetof (Elf, state.elf32.scns)
59 == offsetof (Elf, state.elf64.scns));
61 rwlock_wrlock (elf->lock);
64 if (elf->state.elf.scns_last->cnt < elf->state.elf.scns_last->max)
66 result = &elf->state.elf.scns_last->data[elf->state.elf.scns_last->cnt];
68 if (++elf->state.elf.scns_last->cnt == 1
69 && (elf->state.elf.scns_last
70 == (elf->class == ELFCLASS32
71 || (offsetof (Elf, state.elf32.scns)
72 == offsetof (Elf, state.elf64.scns))
73 ? &elf->state.elf32.scns : &elf->state.elf64.scns)))
74 /* This is zeroth section. */
78 assert (elf->state.elf.scns_last->cnt > 1);
79 result->index = result[-1].index + 1;
84 /* We must allocate a new element. */
85 Elf_ScnList *newp = NULL;
87 assert (elf->state.elf.scnincr > 0);
90 #if SIZE_MAX <= 4294967295U
91 likely (elf->state.elf.scnincr
92 < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
97 newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
98 + ((elf->state.elf.scnincr *= 2)
99 * sizeof (Elf_Scn)), 1);
102 __libelf_seterrno (ELF_E_NOMEM);
106 result = &newp->data[0];
108 /* One section used. */
111 /* This is the number of sections we allocated. */
112 newp->max = elf->state.elf.scnincr;
114 /* Remember the index for the first section in this block. */
116 = 1 + elf->state.elf.scns_last->data[elf->state.elf.scns_last->max - 1].index;
118 /* Enqueue the new list element. */
119 elf->state.elf.scns_last = elf->state.elf.scns_last->next = newp;
122 /* Create a section header for this section. */
123 if (elf->class == ELFCLASS32)
125 result->shdr.e32 = (Elf32_Shdr *) calloc (1, sizeof (Elf32_Shdr));
126 if (result->shdr.e32 == NULL)
128 __libelf_seterrno (ELF_E_NOMEM);
134 result->shdr.e64 = (Elf64_Shdr *) calloc (1, sizeof (Elf64_Shdr));
135 if (result->shdr.e64 == NULL)
137 __libelf_seterrno (ELF_E_NOMEM);
143 result->shdr_flags = ELF_F_DIRTY | ELF_F_MALLOCED;
144 result->list = elf->state.elf.scns_last;
146 /* Initialize the data part. */
147 result->data_read = 1;
148 if (unlikely (first))
150 /* For the first section we mark the data as already available. */
151 //result->data_list_rear = &result->data_list;
156 result->flags |= ELF_F_DIRTY;
159 rwlock_unlock (elf->lock);