Branch data Line data Source code
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.
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 <stddef.h>
35 : : #include <stdlib.h>
36 : :
37 : : #include "libelfP.h"
38 : :
39 : :
40 : : Elf_Data *
41 : 199287 : elf_newdata (Elf_Scn *scn)
42 : : {
43 : 199287 : Elf_Data_List *result = NULL;
44 : :
45 [ + - ]: 199287 : if (scn == NULL)
46 : : return NULL;
47 : :
48 [ - + ]: 199287 : if (unlikely (scn->index == 0))
49 : : {
50 : : /* It is not allowed to add something to the 0th section. */
51 : 0 : __libelf_seterrno (ELF_E_NOT_NUL_SECTION);
52 : 0 : return NULL;
53 : : }
54 : :
55 [ - + ]: 199287 : if (scn->elf->class == ELFCLASS32
56 : : || (offsetof (struct Elf, state.elf32.ehdr)
57 : : == offsetof (struct Elf, state.elf64.ehdr))
58 : 199287 : ? scn->elf->state.elf32.ehdr == NULL
59 : : : scn->elf->state.elf64.ehdr == NULL)
60 : : {
61 : 0 : __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
62 : 0 : return NULL;
63 : : }
64 : :
65 : : rwlock_wrlock (scn->elf->lock);
66 : :
67 [ + - ][ + + ]: 199287 : if (scn->data_read && scn->data_list_rear == NULL)
68 : : {
69 : : /* This means the section was created by the user and this is the
70 : : first data. */
71 : 199285 : result = &scn->data_list;
72 : 199285 : result->flags = ELF_F_DIRTY;
73 : : }
74 : : else
75 : : {
76 : : /* Create a new, empty data descriptor. */
77 : 2 : result = (Elf_Data_List *) calloc (1, sizeof (Elf_Data_List));
78 [ - + ]: 2 : if (result == NULL)
79 : : {
80 : 0 : __libelf_seterrno (ELF_E_NOMEM);
81 : 0 : goto out;
82 : : }
83 : :
84 : 2 : result->flags = ELF_F_DIRTY | ELF_F_MALLOCED;
85 : :
86 [ - + ]: 2 : 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 : 0 : scn->data_list_rear = &scn->data_list;
90 : : }
91 : :
92 : : /* Set the predefined values. */
93 : 199287 : result->data.d.d_version = __libelf_version;
94 : :
95 : 199287 : result->data.s = scn;
96 : :
97 : : /* Add to the end of the list. */
98 [ + + ]: 199287 : if (scn->data_list_rear != NULL)
99 : 2 : scn->data_list_rear->next = result;
100 : :
101 : 199287 : scn->data_list_rear = result;
102 : :
103 : : out:
104 : : rwlock_unlock (scn->elf->lock);
105 : :
106 : : /* Please note that the following is thread safe and is also defined
107 : : for RESULT == NULL since it still return NULL. */
108 : 199287 : return &result->data.d;
109 : : }
|