1*7304104dSAndroid Build Coastguard Worker /* Create new, empty section data.
2*7304104dSAndroid Build Coastguard Worker Copyright (C) 1998, 1999, 2000, 2001, 2002, 2015 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker Contributed by Ulrich Drepper <[email protected]>, 1998.
5*7304104dSAndroid Build Coastguard Worker
6*7304104dSAndroid Build Coastguard Worker This file is free software; you can redistribute it and/or modify
7*7304104dSAndroid Build Coastguard Worker it under the terms of either
8*7304104dSAndroid Build Coastguard Worker
9*7304104dSAndroid Build Coastguard Worker * the GNU Lesser General Public License as published by the Free
10*7304104dSAndroid Build Coastguard Worker Software Foundation; either version 3 of the License, or (at
11*7304104dSAndroid Build Coastguard Worker your option) any later version
12*7304104dSAndroid Build Coastguard Worker
13*7304104dSAndroid Build Coastguard Worker or
14*7304104dSAndroid Build Coastguard Worker
15*7304104dSAndroid Build Coastguard Worker * the GNU General Public License as published by the Free
16*7304104dSAndroid Build Coastguard Worker Software Foundation; either version 2 of the License, or (at
17*7304104dSAndroid Build Coastguard Worker your option) any later version
18*7304104dSAndroid Build Coastguard Worker
19*7304104dSAndroid Build Coastguard Worker or both in parallel, as here.
20*7304104dSAndroid Build Coastguard Worker
21*7304104dSAndroid Build Coastguard Worker elfutils is distributed in the hope that it will be useful, but
22*7304104dSAndroid Build Coastguard Worker WITHOUT ANY WARRANTY; without even the implied warranty of
23*7304104dSAndroid Build Coastguard Worker MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24*7304104dSAndroid Build Coastguard Worker General Public License for more details.
25*7304104dSAndroid Build Coastguard Worker
26*7304104dSAndroid Build Coastguard Worker You should have received copies of the GNU General Public License and
27*7304104dSAndroid Build Coastguard Worker the GNU Lesser General Public License along with this program. If
28*7304104dSAndroid Build Coastguard Worker not, see <http://www.gnu.org/licenses/>. */
29*7304104dSAndroid Build Coastguard Worker
30*7304104dSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
31*7304104dSAndroid Build Coastguard Worker # include <config.h>
32*7304104dSAndroid Build Coastguard Worker #endif
33*7304104dSAndroid Build Coastguard Worker
34*7304104dSAndroid Build Coastguard Worker #include <stddef.h>
35*7304104dSAndroid Build Coastguard Worker #include <stdlib.h>
36*7304104dSAndroid Build Coastguard Worker
37*7304104dSAndroid Build Coastguard Worker #include "libelfP.h"
38*7304104dSAndroid Build Coastguard Worker
39*7304104dSAndroid Build Coastguard Worker
40*7304104dSAndroid Build Coastguard Worker Elf_Data *
elf_newdata(Elf_Scn * scn)41*7304104dSAndroid Build Coastguard Worker elf_newdata (Elf_Scn *scn)
42*7304104dSAndroid Build Coastguard Worker {
43*7304104dSAndroid Build Coastguard Worker Elf_Data_List *result = NULL;
44*7304104dSAndroid Build Coastguard Worker
45*7304104dSAndroid Build Coastguard Worker if (scn == NULL)
46*7304104dSAndroid Build Coastguard Worker return NULL;
47*7304104dSAndroid Build Coastguard Worker
48*7304104dSAndroid Build Coastguard Worker if (unlikely (scn->index == 0))
49*7304104dSAndroid Build Coastguard Worker {
50*7304104dSAndroid Build Coastguard Worker /* It is not allowed to add something to the 0th section. */
51*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_NOT_NUL_SECTION);
52*7304104dSAndroid Build Coastguard Worker return NULL;
53*7304104dSAndroid Build Coastguard Worker }
54*7304104dSAndroid Build Coastguard Worker
55*7304104dSAndroid Build Coastguard Worker if (scn->elf->class == ELFCLASS32
56*7304104dSAndroid Build Coastguard Worker || (offsetof (struct Elf, state.elf32.ehdr)
57*7304104dSAndroid Build Coastguard Worker == offsetof (struct Elf, state.elf64.ehdr))
58*7304104dSAndroid Build Coastguard Worker ? scn->elf->state.elf32.ehdr == NULL
59*7304104dSAndroid Build Coastguard Worker : scn->elf->state.elf64.ehdr == NULL)
60*7304104dSAndroid Build Coastguard Worker {
61*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
62*7304104dSAndroid Build Coastguard Worker return NULL;
63*7304104dSAndroid Build Coastguard Worker }
64*7304104dSAndroid Build Coastguard Worker
65*7304104dSAndroid Build Coastguard Worker rwlock_wrlock (scn->elf->lock);
66*7304104dSAndroid Build Coastguard Worker
67*7304104dSAndroid Build Coastguard Worker /* data_read is set when data has been read from the ELF image or
68*7304104dSAndroid Build Coastguard Worker when a new section has been created by elf_newscn. If data has
69*7304104dSAndroid Build Coastguard Worker been read from the ELF image, then rawdata_base will point to raw
70*7304104dSAndroid Build Coastguard Worker data. If data_read has been set by elf_newscn, then rawdata_base
71*7304104dSAndroid Build Coastguard Worker will be NULL. data_list_rear will be set by elf_getdata if the
72*7304104dSAndroid Build Coastguard Worker data has been converted, or by this function, elf_newdata, when
73*7304104dSAndroid Build Coastguard Worker new data has been added.
74*7304104dSAndroid Build Coastguard Worker
75*7304104dSAndroid Build Coastguard Worker Currently elf_getdata and elf_update rely on the fact that when
76*7304104dSAndroid Build Coastguard Worker data_list_read is not NULL all they have to do is walk the data
77*7304104dSAndroid Build Coastguard Worker list. They will ignore any (unread) raw data in that case.
78*7304104dSAndroid Build Coastguard Worker
79*7304104dSAndroid Build Coastguard Worker So we need to make sure the data list is setup if there is
80*7304104dSAndroid Build Coastguard Worker already data available. */
81*7304104dSAndroid Build Coastguard Worker if (scn->data_read
82*7304104dSAndroid Build Coastguard Worker && scn->rawdata_base != NULL
83*7304104dSAndroid Build Coastguard Worker && scn->data_list_rear == NULL)
84*7304104dSAndroid Build Coastguard Worker __libelf_set_data_list_rdlock (scn, 1);
85*7304104dSAndroid Build Coastguard Worker
86*7304104dSAndroid Build Coastguard Worker if (scn->data_read && scn->data_list_rear == NULL)
87*7304104dSAndroid Build Coastguard Worker {
88*7304104dSAndroid Build Coastguard Worker /* This means the section was created by the user and this is the
89*7304104dSAndroid Build Coastguard Worker first data. */
90*7304104dSAndroid Build Coastguard Worker result = &scn->data_list;
91*7304104dSAndroid Build Coastguard Worker result->flags = ELF_F_DIRTY;
92*7304104dSAndroid Build Coastguard Worker }
93*7304104dSAndroid Build Coastguard Worker else
94*7304104dSAndroid Build Coastguard Worker {
95*7304104dSAndroid Build Coastguard Worker /* It would be more efficient to create new data without
96*7304104dSAndroid Build Coastguard Worker reading/converting the data from the file. But then we
97*7304104dSAndroid Build Coastguard Worker have to remember this. Currently elf_getdata and
98*7304104dSAndroid Build Coastguard Worker elf_update rely on the fact that they don't have to
99*7304104dSAndroid Build Coastguard Worker load/convert any data if data_list_rear is set. */
100*7304104dSAndroid Build Coastguard Worker if (scn->data_read == 0)
101*7304104dSAndroid Build Coastguard Worker {
102*7304104dSAndroid Build Coastguard Worker if (__libelf_set_rawdata_wrlock (scn) != 0)
103*7304104dSAndroid Build Coastguard Worker /* Something went wrong. The error value is already set. */
104*7304104dSAndroid Build Coastguard Worker goto out;
105*7304104dSAndroid Build Coastguard Worker __libelf_set_data_list_rdlock (scn, 1);
106*7304104dSAndroid Build Coastguard Worker }
107*7304104dSAndroid Build Coastguard Worker
108*7304104dSAndroid Build Coastguard Worker /* Create a new, empty data descriptor. */
109*7304104dSAndroid Build Coastguard Worker result = calloc (1, sizeof (Elf_Data_List));
110*7304104dSAndroid Build Coastguard Worker if (result == NULL)
111*7304104dSAndroid Build Coastguard Worker {
112*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_NOMEM);
113*7304104dSAndroid Build Coastguard Worker goto out;
114*7304104dSAndroid Build Coastguard Worker }
115*7304104dSAndroid Build Coastguard Worker
116*7304104dSAndroid Build Coastguard Worker result->flags = ELF_F_DIRTY | ELF_F_MALLOCED;
117*7304104dSAndroid Build Coastguard Worker }
118*7304104dSAndroid Build Coastguard Worker
119*7304104dSAndroid Build Coastguard Worker /* Set the predefined values. */
120*7304104dSAndroid Build Coastguard Worker result->data.d.d_version = EV_CURRENT;
121*7304104dSAndroid Build Coastguard Worker
122*7304104dSAndroid Build Coastguard Worker result->data.s = scn;
123*7304104dSAndroid Build Coastguard Worker
124*7304104dSAndroid Build Coastguard Worker /* Add to the end of the list. */
125*7304104dSAndroid Build Coastguard Worker if (scn->data_list_rear != NULL)
126*7304104dSAndroid Build Coastguard Worker scn->data_list_rear->next = result;
127*7304104dSAndroid Build Coastguard Worker
128*7304104dSAndroid Build Coastguard Worker scn->data_list_rear = result;
129*7304104dSAndroid Build Coastguard Worker
130*7304104dSAndroid Build Coastguard Worker out:
131*7304104dSAndroid Build Coastguard Worker rwlock_unlock (scn->elf->lock);
132*7304104dSAndroid Build Coastguard Worker
133*7304104dSAndroid Build Coastguard Worker /* Please note that the following is thread safe and is also defined
134*7304104dSAndroid Build Coastguard Worker for RESULT == NULL since it still return NULL. */
135*7304104dSAndroid Build Coastguard Worker return &result->data.d;
136*7304104dSAndroid Build Coastguard Worker }
137