Branch data Line data Source code
1 : : /* Get symbol information and separate section index from symbol table
2 : : at the given index.
3 : : Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
4 : : This file is part of elfutils.
5 : : Written by Ulrich Drepper <drepper@redhat.com>, 2000.
6 : :
7 : : This file is free software; you can redistribute it and/or modify
8 : : it under the terms of either
9 : :
10 : : * the GNU Lesser General Public License as published by the Free
11 : : Software Foundation; either version 3 of the License, or (at
12 : : your option) any later version
13 : :
14 : : or
15 : :
16 : : * the GNU General Public License as published by the Free
17 : : Software Foundation; either version 2 of the License, or (at
18 : : your option) any later version
19 : :
20 : : or both in parallel, as here.
21 : :
22 : : elfutils is distributed in the hope that it will be useful, but
23 : : WITHOUT ANY WARRANTY; without even the implied warranty of
24 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 : : General Public License for more details.
26 : :
27 : : You should have received copies of the GNU General Public License and
28 : : the GNU Lesser General Public License along with this program. If
29 : : not, see <http://www.gnu.org/licenses/>. */
30 : :
31 : : #ifdef HAVE_CONFIG_H
32 : : # include <config.h>
33 : : #endif
34 : :
35 : : #include <assert.h>
36 : : #include <gelf.h>
37 : : #include <string.h>
38 : :
39 : : #include "libelfP.h"
40 : :
41 : :
42 : : GElf_Sym *
43 : 463950248 : gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx)
44 : : Elf_Data *symdata;
45 : : Elf_Data *shndxdata;
46 : : int ndx;
47 : : GElf_Sym *dst;
48 : : Elf32_Word *dstshndx;
49 : : {
50 : 463950248 : Elf_Data_Scn *symdata_scn = (Elf_Data_Scn *) symdata;
51 : 463950248 : Elf_Data_Scn *shndxdata_scn = (Elf_Data_Scn *) shndxdata;
52 : 463950248 : GElf_Sym *result = NULL;
53 : 463950248 : Elf32_Word shndx = 0;
54 : :
55 [ + - ]: 463950248 : if (symdata == NULL)
56 : : return NULL;
57 : :
58 [ + - ]: 463950248 : if (unlikely (symdata->d_type != ELF_T_SYM)
59 [ + + ]: 463950248 : || (likely (shndxdata_scn != NULL)
60 [ - + ]: 88002 : && unlikely (shndxdata->d_type != ELF_T_WORD)))
61 : : {
62 : 0 : __libelf_seterrno (ELF_E_INVALID_HANDLE);
63 : 0 : return NULL;
64 : : }
65 : :
66 : : rwlock_rdlock (symdata_scn->s->elf->lock);
67 : :
68 : : /* The user is not required to pass a data descriptor for an extended
69 : : section index table. */
70 [ + + ]: 463950248 : if (likely (shndxdata_scn != NULL))
71 : : {
72 [ - + ]: 88002 : if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
73 : : {
74 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX);
75 : 0 : goto out;
76 : : }
77 : :
78 : 88002 : shndx = ((Elf32_Word *) shndxdata_scn->d.d_buf)[ndx];
79 : : }
80 : :
81 : : /* This is the one place where we have to take advantage of the fact
82 : : that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'.
83 : : The interface is broken so that it requires this hack. */
84 [ + + ]: 463950248 : if (symdata_scn->s->elf->class == ELFCLASS32)
85 : : {
86 : : Elf32_Sym *src;
87 : :
88 : : /* Here it gets a bit more complicated. The format of the symbol
89 : : table entries has to be adopted. The user better has provided
90 : : a buffer where we can store the information. While copying the
91 : : data we are converting the format. */
92 [ - + ]: 98557 : if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
93 : : {
94 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX);
95 : 0 : goto out;
96 : : }
97 : :
98 : 98557 : src = &((Elf32_Sym *) symdata->d_buf)[ndx];
99 : :
100 : : /* This might look like a simple copy operation but it's
101 : : not. There are zero- and sign-extensions going on. */
102 : : #define COPY(name) \
103 : : dst->name = src->name
104 : 98557 : COPY (st_name);
105 : : /* Please note that we can simply copy the `st_info' element since
106 : : the definitions of ELFxx_ST_BIND and ELFxx_ST_TYPE are the same
107 : : for the 64 bit variant. */
108 : 98557 : COPY (st_info);
109 : 98557 : COPY (st_other);
110 : 98557 : COPY (st_shndx);
111 : 98557 : COPY (st_value);
112 : 98557 : COPY (st_size);
113 : : }
114 : : else
115 : : {
116 : : /* If this is a 64 bit object it's easy. */
117 : : assert (sizeof (GElf_Sym) == sizeof (Elf64_Sym));
118 : :
119 : : /* The data is already in the correct form. Just make sure the
120 : : index is OK. */
121 [ - + ]: 463851691 : if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
122 : : {
123 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX);
124 : 0 : goto out;
125 : : }
126 : :
127 : 463851691 : *dst = ((GElf_Sym *) symdata->d_buf)[ndx];
128 : : }
129 : :
130 : : /* Now we can store the section index. */
131 [ + - ]: 463950248 : if (dstshndx != NULL)
132 : 463950248 : *dstshndx = shndx;
133 : :
134 : 463950248 : result = dst;
135 : :
136 : : out:
137 : : rwlock_unlock (symdata_scn->s->elf->lock);
138 : :
139 : 463950248 : return result;
140 : : }
|