Branch data Line data Source code
1 : : /* Update symbol information and section index in symbol table at the 2 : : given index. 3 : : Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014, 2015 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 <gelf.h> 36 : : #include <stdlib.h> 37 : : #include <string.h> 38 : : 39 : : #include "libelfP.h" 40 : : 41 : : 42 : : int 43 : 7432 : gelf_update_symshndx (Elf_Data *symdata, Elf_Data *shndxdata, int ndx, 44 : : GElf_Sym *src, Elf32_Word srcshndx) 45 : : { 46 : 7432 : Elf_Data_Scn *symdata_scn = (Elf_Data_Scn *) symdata; 47 : 7432 : Elf_Data_Scn *shndxdata_scn = (Elf_Data_Scn *) shndxdata; 48 : 7432 : Elf_Scn *scn; 49 : 7432 : Elf32_Word *shndx = NULL; 50 : 7432 : int result = 0; 51 : : 52 [ - + ]: 7432 : if (symdata == NULL) 53 : : return 0; 54 : : 55 [ - + ]: 7432 : if (unlikely (symdata_scn->d.d_type != ELF_T_SYM)) 56 : : { 57 : : /* The type of the data better should match. */ 58 : 0 : __libelf_seterrno (ELF_E_DATA_MISMATCH); 59 : 0 : return 0; 60 : : } 61 : : 62 : 7432 : scn = symdata_scn->s; 63 : : /* We simply have to believe the user that the two sections belong to 64 : : the same ELF file. */ 65 : 7432 : rwlock_wrlock (scn->elf->lock); 66 : : 67 : : /* The user is not required to pass a data descriptor for an extended 68 : : section index table. */ 69 [ - + ]: 7432 : if (shndxdata_scn != NULL) 70 : : { 71 [ # # ]: 0 : if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size)) 72 : : { 73 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX); 74 : 0 : goto out; 75 : : } 76 : : 77 : 0 : shndx = &((Elf32_Word *) shndxdata_scn->d.d_buf)[ndx]; 78 : : } 79 : : /* But if s/he does not the extended sectio index must be zero. */ 80 [ - + ]: 7432 : else if (unlikely (srcshndx != 0)) 81 : : { 82 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX); 83 : 0 : goto out; 84 : : } 85 : : 86 [ + + ]: 7432 : if (scn->elf->class == ELFCLASS32) 87 : : { 88 : 2534 : Elf32_Sym *sym; 89 : : 90 : : /* There is the possibility that the values in the input are 91 : : too large. */ 92 [ + - ]: 2534 : if (unlikely (src->st_value > 0xffffffffull) 93 [ - + ]: 2534 : || unlikely (src->st_size > 0xffffffffull)) 94 : : { 95 : 0 : __libelf_seterrno (ELF_E_INVALID_DATA); 96 : 0 : goto out; 97 : : } 98 : : 99 : : /* Check whether we have to resize the data buffer. */ 100 [ - + ]: 2534 : if (INVALID_NDX (ndx, Elf32_Sym, &symdata_scn->d)) 101 : : { 102 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX); 103 : 0 : goto out; 104 : : } 105 : : 106 : 2534 : sym = &((Elf32_Sym *) symdata_scn->d.d_buf)[ndx]; 107 : : 108 : : #define COPY(name) \ 109 : : sym->name = src->name 110 : 2534 : COPY (st_name); 111 : 2534 : COPY (st_value); 112 : 2534 : COPY (st_size); 113 : : /* Please note that we can simply copy the `st_info' element since 114 : : the definitions of ELFxx_ST_BIND and ELFxx_ST_TYPE are the same 115 : : for the 64 bit variant. */ 116 : 2534 : COPY (st_info); 117 : 2534 : COPY (st_other); 118 : 2534 : COPY (st_shndx); 119 : : } 120 : : else 121 : : { 122 : : /* Check whether we have to resize the data buffer. */ 123 [ - + ]: 4898 : if (INVALID_NDX (ndx, Elf64_Sym, &symdata_scn->d)) 124 : : { 125 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX); 126 : 0 : goto out; 127 : : } 128 : : 129 : 4898 : ((Elf64_Sym *) symdata_scn->d.d_buf)[ndx] = *src; 130 : : } 131 : : 132 : : /* Now we can store the section index. */ 133 [ - + ]: 7432 : if (shndx != NULL) 134 : 0 : *shndx = srcshndx; 135 : : 136 : 7432 : result = 1; 137 : : 138 : : /* Mark the section as modified. */ 139 : 7432 : scn->flags |= ELF_F_DIRTY; 140 : : 141 : 7432 : out: 142 : 7432 : rwlock_unlock (scn->elf->lock); 143 : : 144 : 7432 : return result; 145 : : }