1/* Update information in dynamic table at the given index.
2   Copyright (C) 2007, 2015 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
39int
40gelf_update_auxv (Elf_Data *data, int ndx, GElf_auxv_t *src)
41{
42  Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
43  Elf_Scn *scn;
44  int result = 0;
45
46  if (data == NULL)
47    return 0;
48
49  if (unlikely (ndx < 0))
50    {
51      __libelf_seterrno (ELF_E_INVALID_INDEX);
52      return 0;
53    }
54
55  if (unlikely (data_scn->d.d_type != ELF_T_AUXV))
56    {
57      /* The type of the data better should match.  */
58      __libelf_seterrno (ELF_E_DATA_MISMATCH);
59      return 0;
60    }
61
62  scn = data_scn->s;
63  rwlock_wrlock (scn->elf->lock);
64
65  if (scn->elf->class == ELFCLASS32)
66    {
67      Elf32_auxv_t *auxv;
68
69      /* There is the possibility that the values in the input are
70	 too large.  */
71      if (unlikely (src->a_type > 0xffffffffll)
72	  || unlikely (src->a_un.a_val > 0xffffffffull))
73	{
74	  __libelf_seterrno (ELF_E_INVALID_DATA);
75	  goto out;
76	}
77
78      /* Check whether we have to resize the data buffer.  */
79      if (unlikely ((ndx + 1) * sizeof (Elf32_auxv_t) > data_scn->d.d_size))
80	{
81	  __libelf_seterrno (ELF_E_INVALID_INDEX);
82	  goto out;
83	}
84
85      auxv = &((Elf32_auxv_t *) data_scn->d.d_buf)[ndx];
86
87      auxv->a_type = src->a_type;
88      auxv->a_un.a_val = src->a_un.a_val;
89    }
90  else
91    {
92      /* Check whether we have to resize the data buffer.  */
93      if (unlikely ((ndx + 1) * sizeof (Elf64_auxv_t) > data_scn->d.d_size))
94	{
95	  __libelf_seterrno (ELF_E_INVALID_INDEX);
96	  goto out;
97	}
98
99      ((Elf64_auxv_t *) data_scn->d.d_buf)[ndx] = *src;
100    }
101
102  result = 1;
103
104  /* Mark the section as modified.  */
105  scn->flags |= ELF_F_DIRTY;
106
107 out:
108  rwlock_unlock (scn->elf->lock);
109
110  return result;
111}
112