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