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