1 /* Create new, empty section data.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
3 This file is part of elfutils.
4 Contributed 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/>. */
41 elf_newdata (Elf_Scn *scn)
43 Elf_Data_List *result = NULL;
48 if (unlikely (scn->index == 0))
50 /* It is not allowed to add something to the 0th section. */
51 __libelf_seterrno (ELF_E_NOT_NUL_SECTION);
55 if (scn->elf->class == ELFCLASS32
56 || (offsetof (struct Elf, state.elf32.ehdr)
57 == offsetof (struct Elf, state.elf64.ehdr))
58 ? scn->elf->state.elf32.ehdr == NULL
59 : scn->elf->state.elf64.ehdr == NULL)
61 __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
65 rwlock_wrlock (scn->elf->lock);
67 if (scn->data_read && scn->data_list_rear == NULL)
69 /* This means the section was created by the user and this is the
71 result = &scn->data_list;
72 result->flags = ELF_F_DIRTY;
76 /* Create a new, empty data descriptor. */
77 result = (Elf_Data_List *) calloc (1, sizeof (Elf_Data_List));
80 __libelf_seterrno (ELF_E_NOMEM);
84 result->flags = ELF_F_DIRTY | ELF_F_MALLOCED;
86 if (scn->data_list_rear == NULL)
87 /* We create new data without reading/converting the data from the
88 file. That is fine but we have to remember this. */
89 scn->data_list_rear = &scn->data_list;
92 /* Set the predefined values. */
93 result->data.d.d_version = __libelf_version;
97 /* Add to the end of the list. */
98 if (scn->data_list_rear != NULL)
99 scn->data_list_rear->next = result;
101 scn->data_list_rear = result;
104 rwlock_unlock (scn->elf->lock);
106 /* Please note that the following is thread safe and is also defined
107 for RESULT == NULL since it still return NULL. */
108 return &result->data.d;