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