1 /* Get information from dynamic table at the given index. 2 Copyright (C) 2000, 2001, 2002 Red Hat, Inc. 3 Written by Ulrich Drepper <drepper (at) redhat.com>, 2000. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, version 2. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program; if not, write to the Free Software Foundation, 16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 17 18 #ifdef HAVE_CONFIG_H 19 # include <config.h> 20 #endif 21 22 #include <assert.h> 23 #include <gelf.h> 24 #include <string.h> 25 26 #include "libelfP.h" 27 28 29 GElf_Dyn * 30 gelf_getdyn (data, ndx, dst) 31 Elf_Data *data; 32 int ndx; 33 GElf_Dyn *dst; 34 { 35 Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data; 36 GElf_Dyn *result = NULL; 37 Elf *elf; 38 39 if (data_scn == NULL) 40 return NULL; 41 42 if (unlikely (data_scn->d.d_type != ELF_T_DYN)) 43 { 44 __libelf_seterrno (ELF_E_INVALID_HANDLE); 45 return NULL; 46 } 47 48 elf = data_scn->s->elf; 49 50 rwlock_rdlock (elf->lock); 51 52 /* This is the one place where we have to take advantage of the fact 53 that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'. 54 The interface is broken so that it requires this hack. */ 55 if (elf->class == ELFCLASS32) 56 { 57 Elf32_Dyn *src; 58 59 /* Here it gets a bit more complicated. The format of the symbol 60 table entries has to be adopted. The user better has provided 61 a buffer where we can store the information. While copying the 62 data we are converting the format. */ 63 if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size)) 64 { 65 __libelf_seterrno (ELF_E_INVALID_INDEX); 66 goto out; 67 } 68 69 src = &((Elf32_Dyn *) data_scn->d.d_buf)[ndx]; 70 71 /* This might look like a simple copy operation but it's 72 not. There are zero- and sign-extensions going on. */ 73 dst->d_tag = src->d_tag; 74 /* It OK to copy `d_val' since `d_ptr' has the same size. */ 75 dst->d_un.d_val = src->d_un.d_val; 76 } 77 else 78 { 79 /* If this is a 64 bit object it's easy. */ 80 assert (sizeof (GElf_Dyn) == sizeof (Elf64_Dyn)); 81 82 /* The data is already in the correct form. Just make sure the 83 index is OK. */ 84 if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size)) 85 { 86 __libelf_seterrno (ELF_E_INVALID_INDEX); 87 goto out; 88 } 89 90 *dst = ((GElf_Dyn *) data_scn->d.d_buf)[ndx]; 91 } 92 93 result = dst; 94 95 out: 96 rwlock_unlock (elf->lock); 97 98 return result; 99 } 100