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