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 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 *
gelf_getsymshndx(symdata,shndxdata,ndx,dst,dstshndx)43 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   Elf_Data_Scn *symdata_scn = (Elf_Data_Scn *) symdata;
51   Elf_Data_Scn *shndxdata_scn = (Elf_Data_Scn *) shndxdata;
52   GElf_Sym *result = NULL;
53   Elf32_Word shndx = 0;
54 
55   if (symdata == NULL)
56     return NULL;
57 
58   if (unlikely (symdata->d_type != ELF_T_SYM)
59       || (likely (shndxdata_scn != NULL)
60 	  && unlikely (shndxdata->d_type != ELF_T_WORD)))
61     {
62       __libelf_seterrno (ELF_E_INVALID_HANDLE);
63       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   if (likely (shndxdata_scn != NULL))
71     {
72       if (INVALID_NDX (ndx, Elf32_Word, &shndxdata_scn->d))
73 	{
74 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
75 	  goto out;
76 	}
77 
78       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   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       if (INVALID_NDX (ndx, Elf32_Sym, symdata))
93 	{
94 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
95 	  goto out;
96 	}
97 
98       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       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       COPY (st_info);
109       COPY (st_other);
110       COPY (st_shndx);
111       COPY (st_value);
112       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       if (INVALID_NDX (ndx, GElf_Sym, symdata))
122 	{
123 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
124 	  goto out;
125 	}
126 
127       *dst = ((GElf_Sym *) symdata->d_buf)[ndx];
128     }
129 
130   /* Now we can store the section index.  */
131   if (dstshndx != NULL)
132     *dstshndx = shndx;
133 
134   result = dst;
135 
136  out:
137   rwlock_unlock (symdata_scn->s->elf->lock);
138 
139   return result;
140 }
141