1 /* Get RELA relocation information at 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 <gelf.h> 23 #include <string.h> 24 25 #include "libelfP.h" 26 27 28 GElf_Rela * 29 gelf_getrela (data, ndx, dst) 30 Elf_Data *data; 31 int ndx; 32 GElf_Rela *dst; 33 { 34 Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data; 35 Elf_Scn *scn; 36 GElf_Rela *result; 37 38 if (data_scn == NULL) 39 return NULL; 40 41 if (unlikely (ndx < 0)) 42 { 43 __libelf_seterrno (ELF_E_INVALID_INDEX); 44 return NULL; 45 } 46 47 if (unlikely (data_scn->d.d_type != ELF_T_RELA)) 48 { 49 __libelf_seterrno (ELF_E_INVALID_HANDLE); 50 return NULL; 51 } 52 53 /* This is the one place where we have to take advantage of the fact 54 that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'. 55 The interface is broken so that it requires this hack. */ 56 scn = data_scn->s; 57 58 rwlock_rdlock (scn->elf->lock); 59 60 if (scn->elf->class == ELFCLASS32) 61 { 62 /* We have to convert the data. */ 63 if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size)) 64 { 65 __libelf_seterrno (ELF_E_INVALID_INDEX); 66 result = NULL; 67 } 68 else 69 { 70 Elf32_Rela *src = &((Elf32_Rela *) data_scn->d.d_buf)[ndx]; 71 72 dst->r_offset = src->r_offset; 73 dst->r_info = GELF_R_INFO (ELF32_R_SYM (src->r_info), 74 ELF32_R_TYPE (src->r_info)); 75 dst->r_addend = src->r_addend; 76 77 result = dst; 78 } 79 } 80 else 81 { 82 /* Simply copy the data after we made sure we are actually getting 83 correct data. */ 84 if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size)) 85 { 86 __libelf_seterrno (ELF_E_INVALID_INDEX); 87 result = NULL; 88 } 89 else 90 result = memcpy (dst, &((Elf64_Rela *) data_scn->d.d_buf)[ndx], 91 sizeof (Elf64_Rela)); 92 } 93 94 rwlock_unlock (scn->elf->lock); 95 96 return result; 97 } 98