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, 2005, 2009, 2014, 2015 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 <gelf.h>
36 : : #include <string.h>
37 : :
38 : : #include "libelfP.h"
39 : :
40 : :
41 : : GElf_Sym *
42 : 46753640 : gelf_getsymshndx (Elf_Data *symdata, Elf_Data *shndxdata, int ndx,
43 : : GElf_Sym *dst, Elf32_Word *dstshndx)
44 : : {
45 : 46753640 : Elf_Data_Scn *symdata_scn = (Elf_Data_Scn *) symdata;
46 : 46753640 : Elf_Data_Scn *shndxdata_scn = (Elf_Data_Scn *) shndxdata;
47 : 46753640 : GElf_Sym *result = NULL;
48 : 46753640 : Elf32_Word shndx = 0;
49 : :
50 [ - + ]: 46753640 : if (symdata == NULL)
51 : : return NULL;
52 : :
53 [ + - ]: 46753640 : if (unlikely (symdata->d_type != ELF_T_SYM)
54 [ + + ]: 46753640 : || (likely (shndxdata_scn != NULL)
55 [ - + ]: 1748892 : && unlikely (shndxdata->d_type != ELF_T_WORD)))
56 : : {
57 : 0 : __libelf_seterrno (ELF_E_INVALID_HANDLE);
58 : 0 : return NULL;
59 : : }
60 : :
61 : 46753640 : rwlock_rdlock (symdata_scn->s->elf->lock);
62 : :
63 : : /* The user is not required to pass a data descriptor for an extended
64 : : section index table. */
65 [ + + ]: 46753640 : if (likely (shndxdata_scn != NULL))
66 : : {
67 [ - + ]: 1748892 : if (INVALID_NDX (ndx, Elf32_Word, &shndxdata_scn->d))
68 : : {
69 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX);
70 : 0 : goto out;
71 : : }
72 : :
73 : 1748892 : shndx = ((Elf32_Word *) shndxdata_scn->d.d_buf)[ndx];
74 : : }
75 : :
76 : : /* This is the one place where we have to take advantage of the fact
77 : : that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'.
78 : : The interface is broken so that it requires this hack. */
79 [ + + ]: 46753640 : if (symdata_scn->s->elf->class == ELFCLASS32)
80 : : {
81 : 564450 : Elf32_Sym *src;
82 : :
83 : : /* Here it gets a bit more complicated. The format of the symbol
84 : : table entries has to be adopted. The user better has provided
85 : : a buffer where we can store the information. While copying the
86 : : data we are converting the format. */
87 [ - + ]: 564450 : if (INVALID_NDX (ndx, Elf32_Sym, symdata))
88 : : {
89 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX);
90 : 0 : goto out;
91 : : }
92 : :
93 : 564450 : src = &((Elf32_Sym *) symdata->d_buf)[ndx];
94 : :
95 : : /* This might look like a simple copy operation but it's
96 : : not. There are zero- and sign-extensions going on. */
97 : : #define COPY(name) \
98 : : dst->name = src->name
99 : 564450 : COPY (st_name);
100 : : /* Please note that we can simply copy the `st_info' element since
101 : : the definitions of ELFxx_ST_BIND and ELFxx_ST_TYPE are the same
102 : : for the 64 bit variant. */
103 : 564450 : COPY (st_info);
104 : 564450 : COPY (st_other);
105 : 564450 : COPY (st_shndx);
106 : 564450 : COPY (st_value);
107 : 564450 : COPY (st_size);
108 : : }
109 : : else
110 : : {
111 : : /* If this is a 64 bit object it's easy. */
112 : 46189190 : eu_static_assert (sizeof (GElf_Sym) == sizeof (Elf64_Sym));
113 : :
114 : : /* The data is already in the correct form. Just make sure the
115 : : index is OK. */
116 [ - + ]: 46189190 : if (INVALID_NDX (ndx, GElf_Sym, symdata))
117 : : {
118 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX);
119 : 0 : goto out;
120 : : }
121 : :
122 : 46189190 : *dst = ((GElf_Sym *) symdata->d_buf)[ndx];
123 : : }
124 : :
125 : : /* Now we can store the section index. */
126 [ + - ]: 46753640 : if (dstshndx != NULL)
127 : 46753640 : *dstshndx = shndx;
128 : :
129 : : result = dst;
130 : :
131 : 46753640 : out:
132 : 46753640 : rwlock_unlock (symdata_scn->s->elf->lock);
133 : :
134 : 46753640 : return result;
135 : : }
|