]>
git.ipfire.org Git - thirdparty/elfutils.git/blob - libelf/elf_newdata.c
1 /* Create new, empty section data.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2015 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 /* data_read is set when data has been read from the ELF image or
68 when a new section has been created by elf_newscn. If data has
69 been read from the ELF image, then rawdata_base will point to raw
70 data. If data_read has been set by elf_newscn, then rawdata_base
71 will be NULL. data_list_rear will be set by elf_getdata if the
72 data has been converted, or by this function, elf_newdata, when
73 new data has been added.
75 Currently elf_getdata and elf_update rely on the fact that when
76 data_list_read is not NULL all they have to do is walk the data
77 list. They will ignore any (unread) raw data in that case.
79 So we need to make sure the data list is setup if there is
80 already data available. */
82 && scn
->rawdata_base
!= NULL
83 && scn
->data_list_rear
== NULL
)
84 __libelf_set_data_list_rdlock (scn
, 1);
86 if (scn
->data_read
&& scn
->data_list_rear
== NULL
)
88 /* This means the section was created by the user and this is the
90 result
= &scn
->data_list
;
91 result
->flags
= ELF_F_DIRTY
;
95 /* It would be more efficient to create new data without
96 reading/converting the data from the file. But then we
97 have to remember this. Currently elf_getdata and
98 elf_update rely on the fact that they don't have to
99 load/convert any data if data_list_rear is set. */
100 if (scn
->data_read
== 0)
102 if (__libelf_set_rawdata_wrlock (scn
) != 0)
103 /* Something went wrong. The error value is already set. */
105 __libelf_set_data_list_rdlock (scn
, 1);
108 /* Create a new, empty data descriptor. */
109 result
= calloc (1, sizeof (Elf_Data_List
));
112 __libelf_seterrno (ELF_E_NOMEM
);
116 result
->flags
= ELF_F_DIRTY
| ELF_F_MALLOCED
;
119 /* Set the predefined values. */
120 result
->data
.d
.d_version
= EV_CURRENT
;
122 result
->data
.s
= scn
;
124 /* Add to the end of the list. */
125 if (scn
->data_list_rear
!= NULL
)
126 scn
->data_list_rear
->next
= result
;
128 scn
->data_list_rear
= result
;
131 rwlock_unlock (scn
->elf
->lock
);
133 /* Please note that the following is thread safe and is also defined
134 for RESULT == NULL since it still return NULL. */
135 return &result
->data
.d
;