1 /* $NetBSD: gelf_symshndx.c,v 1.7 2026/05/17 21:40:50 jkoshy Exp $ */ 2 3 /*- 4 * Copyright (c) 2006,2008,2020 Joseph Koshy 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #if HAVE_NBTOOL_CONFIG_H 30 # include "nbtool_config.h" 31 #endif 32 33 #include <sys/cdefs.h> 34 35 #include <assert.h> 36 #include <gelf.h> 37 38 #include "_libelf.h" 39 40 ELFTC_VCSID("Id: gelf_symshndx.c 4074 2025-01-07 15:34:21Z jkoshy"); 41 42 __RCSID("$NetBSD: gelf_symshndx.c,v 1.7 2026/05/17 21:40:50 jkoshy Exp $"); 43 44 GElf_Sym * 45 gelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst, 46 Elf32_Word *shindex) 47 { 48 Elf *e; 49 size_t msz; 50 Elf_Scn *scn; 51 unsigned int ec; 52 uint32_t sh_type; 53 struct _Libelf_Data *ld, *lid; 54 55 ld = (struct _Libelf_Data *) d; 56 lid = (struct _Libelf_Data *) id; 57 58 if (gelf_getsym(d, ndx, dst) == 0) 59 return (NULL); 60 61 if (shindex == NULL) 62 return (dst); 63 64 if (lid == NULL) { 65 *shindex = 0; 66 return (dst); 67 } 68 69 if ((scn = lid->d_scn) == NULL || 70 (e = scn->s_elf) == NULL || (e != ld->d_scn->s_elf)) { 71 LIBELF_SET_ERROR(ARGUMENT, 0); 72 return (NULL); 73 } 74 75 ec = e->e_class; 76 assert(ec == ELFCLASS32 || ec == ELFCLASS64); 77 78 if (ec == ELFCLASS32) 79 sh_type = scn->s_shdr.s_shdr32.sh_type; 80 else 81 sh_type = scn->s_shdr.s_shdr64.sh_type; 82 83 if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD || 84 id->d_type != ELF_T_WORD) { 85 LIBELF_SET_ERROR(ARGUMENT, 0); 86 return (NULL); 87 } 88 89 if ((msz = _libelf_msize(ELF_T_WORD, ec, e->e_version)) == 0) 90 return (NULL); 91 92 assert(ndx >= 0); 93 94 if (msz * (size_t) ndx >= id->d_size) { 95 LIBELF_SET_ERROR(ARGUMENT, 0); 96 return (NULL); 97 } 98 99 *shindex = ((Elf32_Word *) id->d_buf)[ndx]; 100 101 return (dst); 102 } 103 104 int 105 gelf_update_symshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *gs, 106 Elf32_Word xindex) 107 { 108 Elf *e; 109 size_t msz; 110 Elf_Scn *scn; 111 unsigned int ec; 112 uint32_t sh_type; 113 struct _Libelf_Data *ld, *lid; 114 115 ld = (struct _Libelf_Data *) d; 116 lid = (struct _Libelf_Data *) id; 117 118 if (gelf_update_sym(d, ndx, gs) == 0) 119 return (0); 120 121 if (lid == NULL || (scn = lid->d_scn) == NULL || 122 (e = scn->s_elf) == NULL || (e != ld->d_scn->s_elf)) { 123 LIBELF_SET_ERROR(ARGUMENT, 0); 124 return (0); 125 } 126 127 ec = e->e_class; 128 assert(ec == ELFCLASS32 || ec == ELFCLASS64); 129 130 if (ec == ELFCLASS32) 131 sh_type = scn->s_shdr.s_shdr32.sh_type; 132 else 133 sh_type = scn->s_shdr.s_shdr64.sh_type; 134 135 if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD || 136 d->d_type != ELF_T_WORD) { 137 LIBELF_SET_ERROR(ARGUMENT, 0); 138 return (0); 139 } 140 141 if ((msz = _libelf_msize(ELF_T_WORD, ec, e->e_version)) == 0) 142 return (0); 143 144 assert(ndx >= 0); 145 146 if (msz * (size_t) ndx >= id->d_size) { 147 LIBELF_SET_ERROR(ARGUMENT, 0); 148 return (0); 149 } 150 151 *(((Elf32_Word *) id->d_buf) + ndx) = xindex; 152 153 return (1); 154 } 155