Imported Upstream version 0.155
[platform/upstream/elfutils.git] / libelf / gelf_update_auxv.c
1 /* Update information in dynamic table at the given index.
2    Copyright (C) 2007 Red Hat, Inc.
3    This file is part of elfutils.
4
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of either
7
8      * the GNU Lesser General Public License as published by the Free
9        Software Foundation; either version 3 of the License, or (at
10        your option) any later version
11
12    or
13
14      * the GNU General Public License as published by the Free
15        Software Foundation; either version 2 of the License, or (at
16        your option) any later version
17
18    or both in parallel, as here.
19
20    elfutils is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24
25    You should have received copies of the GNU General Public License and
26    the GNU Lesser General Public License along with this program.  If
27    not, see <http://www.gnu.org/licenses/>.  */
28
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include <gelf.h>
34 #include <string.h>
35
36 #include "libelfP.h"
37
38
39 int
40 gelf_update_auxv (data, ndx, src)
41      Elf_Data *data;
42      int ndx;
43      GElf_auxv_t *src;
44 {
45   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
46   Elf_Scn *scn;
47   int result = 0;
48
49   if (data == NULL)
50     return 0;
51
52   if (unlikely (ndx < 0))
53     {
54       __libelf_seterrno (ELF_E_INVALID_INDEX);
55       return 0;
56     }
57
58   if (unlikely (data_scn->d.d_type != ELF_T_AUXV))
59     {
60       /* The type of the data better should match.  */
61       __libelf_seterrno (ELF_E_DATA_MISMATCH);
62       return 0;
63     }
64
65   scn = data_scn->s;
66   rwlock_wrlock (scn->elf->lock);
67
68   if (scn->elf->class == ELFCLASS32)
69     {
70       Elf32_auxv_t *auxv;
71
72       /* There is the possibility that the values in the input are
73          too large.  */
74       if (unlikely (src->a_type > 0xffffffffll)
75           || unlikely (src->a_un.a_val > 0xffffffffull))
76         {
77           __libelf_seterrno (ELF_E_INVALID_DATA);
78           goto out;
79         }
80
81       /* Check whether we have to resize the data buffer.  */
82       if (unlikely ((ndx + 1) * sizeof (Elf32_auxv_t) > data_scn->d.d_size))
83         {
84           __libelf_seterrno (ELF_E_INVALID_INDEX);
85           goto out;
86         }
87
88       auxv = &((Elf32_auxv_t *) data_scn->d.d_buf)[ndx];
89
90       auxv->a_type = src->a_type;
91       auxv->a_un.a_val = src->a_un.a_val;
92     }
93   else
94     {
95       /* Check whether we have to resize the data buffer.  */
96       if (unlikely ((ndx + 1) * sizeof (Elf64_auxv_t) > data_scn->d.d_size))
97         {
98           __libelf_seterrno (ELF_E_INVALID_INDEX);
99           goto out;
100         }
101
102       ((Elf64_auxv_t *) data_scn->d.d_buf)[ndx] = *src;
103     }
104
105   result = 1;
106
107   /* Mark the section as modified.  */
108   scn->flags |= ELF_F_DIRTY;
109
110  out:
111   rwlock_unlock (scn->elf->lock);
112
113   return result;
114 }