1 /* Get information from auxiliary vector 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 <assert.h>
34 #include <gelf.h>
35 #include <string.h>
36 
37 #include "libelfP.h"
38 
39 
40 GElf_auxv_t *
gelf_getauxv(Elf_Data * data,int ndx,GElf_auxv_t * dst)41 gelf_getauxv (Elf_Data *data, int ndx, GElf_auxv_t *dst)
42 {
43   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
44   GElf_auxv_t *result = NULL;
45   Elf *elf;
46 
47   if (data_scn == NULL)
48     return NULL;
49 
50   if (unlikely (data_scn->d.d_type != ELF_T_AUXV))
51     {
52       __libelf_seterrno (ELF_E_INVALID_HANDLE);
53       return NULL;
54     }
55 
56   elf = data_scn->s->elf;
57 
58   rwlock_rdlock (elf->lock);
59 
60   /* This is the one place where we have to take advantage of the fact
61      that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'.
62      The interface is broken so that it requires this hack.  */
63   if (elf->class == ELFCLASS32)
64     {
65       Elf32_auxv_t *src;
66 
67       /* Here it gets a bit more complicated.  The format of the vector
68 	 entries has to be converted.  The user better have provided a
69 	 buffer where we can store the information.  While copying the data
70 	 we convert the format.  */
71       if (unlikely ((ndx + 1) * sizeof (Elf32_auxv_t) > data_scn->d.d_size))
72 	{
73 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
74 	  goto out;
75 	}
76 
77       src = &((Elf32_auxv_t *) data_scn->d.d_buf)[ndx];
78 
79       /* This might look like a simple copy operation but it's
80 	 not.  There are zero- and sign-extensions going on.  */
81       dst->a_type = src->a_type;
82       dst->a_un.a_val = src->a_un.a_val;
83     }
84   else
85     {
86       /* If this is a 64 bit object it's easy.  */
87       assert (sizeof (GElf_auxv_t) == sizeof (Elf64_auxv_t));
88 
89       /* The data is already in the correct form.  Just make sure the
90 	 index is OK.  */
91       if (unlikely ((ndx + 1) * sizeof (GElf_auxv_t) > data_scn->d.d_size))
92 	{
93 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
94 	  goto out;
95 	}
96 
97       memcpy (dst, data_scn->d.d_buf + ndx * sizeof (GElf_auxv_t),
98 	      sizeof (GElf_auxv_t));
99     }
100 
101   result = dst;
102 
103  out:
104   rwlock_unlock (elf->lock);
105 
106   return result;
107 }
108