1*5ac3bc71Schristos /* $NetBSD: gelf_symshndx.c,v 1.5 2024/03/03 17:37:34 christos Exp $ */
2e81373b4Schristos
39dd9d0cfSchristos /*-
4*5ac3bc71Schristos * Copyright (c) 2006,2008,2020 Joseph Koshy
59dd9d0cfSchristos * All rights reserved.
69dd9d0cfSchristos *
79dd9d0cfSchristos * Redistribution and use in source and binary forms, with or without
89dd9d0cfSchristos * modification, are permitted provided that the following conditions
99dd9d0cfSchristos * are met:
109dd9d0cfSchristos * 1. Redistributions of source code must retain the above copyright
119dd9d0cfSchristos * notice, this list of conditions and the following disclaimer.
129dd9d0cfSchristos * 2. Redistributions in binary form must reproduce the above copyright
139dd9d0cfSchristos * notice, this list of conditions and the following disclaimer in the
149dd9d0cfSchristos * documentation and/or other materials provided with the distribution.
159dd9d0cfSchristos *
169dd9d0cfSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179dd9d0cfSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189dd9d0cfSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199dd9d0cfSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209dd9d0cfSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219dd9d0cfSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229dd9d0cfSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239dd9d0cfSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249dd9d0cfSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259dd9d0cfSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269dd9d0cfSchristos * SUCH DAMAGE.
279dd9d0cfSchristos */
289dd9d0cfSchristos
29e81373b4Schristos #if HAVE_NBTOOL_CONFIG_H
30e81373b4Schristos # include "nbtool_config.h"
31e81373b4Schristos #endif
32e81373b4Schristos
339dd9d0cfSchristos #include <sys/cdefs.h>
349dd9d0cfSchristos
359dd9d0cfSchristos #include <assert.h>
369dd9d0cfSchristos #include <gelf.h>
379dd9d0cfSchristos
389dd9d0cfSchristos #include "_libelf.h"
399dd9d0cfSchristos
40*5ac3bc71Schristos __RCSID("$NetBSD: gelf_symshndx.c,v 1.5 2024/03/03 17:37:34 christos Exp $");
41*5ac3bc71Schristos ELFTC_VCSID("Id: gelf_symshndx.c 3977 2022-05-01 06:45:34Z jkoshy");
429dd9d0cfSchristos
439dd9d0cfSchristos GElf_Sym *
gelf_getsymshndx(Elf_Data * d,Elf_Data * id,int ndx,GElf_Sym * dst,Elf32_Word * shindex)449dd9d0cfSchristos gelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst,
459dd9d0cfSchristos Elf32_Word *shindex)
469dd9d0cfSchristos {
479dd9d0cfSchristos int ec;
489dd9d0cfSchristos Elf *e;
499dd9d0cfSchristos size_t msz;
509dd9d0cfSchristos Elf_Scn *scn;
519dd9d0cfSchristos uint32_t sh_type;
529dd9d0cfSchristos struct _Libelf_Data *ld, *lid;
539dd9d0cfSchristos
549dd9d0cfSchristos ld = (struct _Libelf_Data *) d;
559dd9d0cfSchristos lid = (struct _Libelf_Data *) id;
569dd9d0cfSchristos
579dd9d0cfSchristos if (gelf_getsym(d, ndx, dst) == 0)
589dd9d0cfSchristos return (NULL);
599dd9d0cfSchristos
60*5ac3bc71Schristos if (shindex == NULL)
61*5ac3bc71Schristos return (dst);
62*5ac3bc71Schristos
63*5ac3bc71Schristos if (lid == NULL) {
64*5ac3bc71Schristos *shindex = 0;
65*5ac3bc71Schristos return (dst);
66*5ac3bc71Schristos }
67*5ac3bc71Schristos
68*5ac3bc71Schristos if ((scn = lid->d_scn) == NULL ||
69*5ac3bc71Schristos (e = scn->s_elf) == NULL || (e != ld->d_scn->s_elf)) {
709dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
719dd9d0cfSchristos return (NULL);
729dd9d0cfSchristos }
739dd9d0cfSchristos
749dd9d0cfSchristos ec = e->e_class;
759dd9d0cfSchristos assert(ec == ELFCLASS32 || ec == ELFCLASS64);
769dd9d0cfSchristos
779dd9d0cfSchristos if (ec == ELFCLASS32)
789dd9d0cfSchristos sh_type = scn->s_shdr.s_shdr32.sh_type;
799dd9d0cfSchristos else
809dd9d0cfSchristos sh_type = scn->s_shdr.s_shdr64.sh_type;
819dd9d0cfSchristos
829dd9d0cfSchristos if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
839dd9d0cfSchristos id->d_type != ELF_T_WORD) {
849dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
859dd9d0cfSchristos return (NULL);
869dd9d0cfSchristos }
879dd9d0cfSchristos
88*5ac3bc71Schristos if ((msz = _libelf_msize(ELF_T_WORD, ec, e->e_version)) == 0)
89*5ac3bc71Schristos return (NULL);
909dd9d0cfSchristos
9142bd3019Schristos assert(ndx >= 0);
929dd9d0cfSchristos
9342bd3019Schristos if (msz * (size_t) ndx >= id->d_size) {
949dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
959dd9d0cfSchristos return (NULL);
969dd9d0cfSchristos }
979dd9d0cfSchristos
989dd9d0cfSchristos *shindex = ((Elf32_Word *) id->d_buf)[ndx];
999dd9d0cfSchristos
1009dd9d0cfSchristos return (dst);
1019dd9d0cfSchristos }
1029dd9d0cfSchristos
1039dd9d0cfSchristos int
gelf_update_symshndx(Elf_Data * d,Elf_Data * id,int ndx,GElf_Sym * gs,Elf32_Word xindex)1049dd9d0cfSchristos gelf_update_symshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *gs,
1059dd9d0cfSchristos Elf32_Word xindex)
1069dd9d0cfSchristos {
1079dd9d0cfSchristos int ec;
1089dd9d0cfSchristos Elf *e;
1099dd9d0cfSchristos size_t msz;
1109dd9d0cfSchristos Elf_Scn *scn;
1119dd9d0cfSchristos uint32_t sh_type;
1129dd9d0cfSchristos struct _Libelf_Data *ld, *lid;
1139dd9d0cfSchristos
1149dd9d0cfSchristos ld = (struct _Libelf_Data *) d;
1159dd9d0cfSchristos lid = (struct _Libelf_Data *) id;
1169dd9d0cfSchristos
1179dd9d0cfSchristos if (gelf_update_sym(d, ndx, gs) == 0)
1189dd9d0cfSchristos return (0);
1199dd9d0cfSchristos
1209dd9d0cfSchristos if (lid == NULL || (scn = lid->d_scn) == NULL ||
1219dd9d0cfSchristos (e = scn->s_elf) == NULL || (e != ld->d_scn->s_elf)) {
1229dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
1239dd9d0cfSchristos return (0);
1249dd9d0cfSchristos }
1259dd9d0cfSchristos
1269dd9d0cfSchristos ec = e->e_class;
1279dd9d0cfSchristos assert(ec == ELFCLASS32 || ec == ELFCLASS64);
1289dd9d0cfSchristos
1299dd9d0cfSchristos if (ec == ELFCLASS32)
1309dd9d0cfSchristos sh_type = scn->s_shdr.s_shdr32.sh_type;
1319dd9d0cfSchristos else
1329dd9d0cfSchristos sh_type = scn->s_shdr.s_shdr64.sh_type;
1339dd9d0cfSchristos
1349dd9d0cfSchristos if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
1359dd9d0cfSchristos d->d_type != ELF_T_WORD) {
1369dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
1379dd9d0cfSchristos return (0);
1389dd9d0cfSchristos }
1399dd9d0cfSchristos
140*5ac3bc71Schristos if ((msz = _libelf_msize(ELF_T_WORD, ec, e->e_version)) == 0)
141*5ac3bc71Schristos return (0);
1429dd9d0cfSchristos
14342bd3019Schristos assert(ndx >= 0);
14442bd3019Schristos
14542bd3019Schristos if (msz * (size_t) ndx >= id->d_size) {
1469dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
1479dd9d0cfSchristos return (0);
1489dd9d0cfSchristos }
1499dd9d0cfSchristos
1509dd9d0cfSchristos *(((Elf32_Word *) id->d_buf) + ndx) = xindex;
1519dd9d0cfSchristos
1529dd9d0cfSchristos return (1);
1539dd9d0cfSchristos }
154