12de3b87aSKai Wang /*-
22de3b87aSKai Wang * Copyright (c) 2006,2008 Joseph Koshy
32de3b87aSKai Wang * All rights reserved.
42de3b87aSKai Wang *
52de3b87aSKai Wang * Redistribution and use in source and binary forms, with or without
62de3b87aSKai Wang * modification, are permitted provided that the following conditions
72de3b87aSKai Wang * are met:
82de3b87aSKai Wang * 1. Redistributions of source code must retain the above copyright
92de3b87aSKai Wang * notice, this list of conditions and the following disclaimer.
102de3b87aSKai Wang * 2. Redistributions in binary form must reproduce the above copyright
112de3b87aSKai Wang * notice, this list of conditions and the following disclaimer in the
122de3b87aSKai Wang * documentation and/or other materials provided with the distribution.
132de3b87aSKai Wang *
142de3b87aSKai Wang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152de3b87aSKai Wang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162de3b87aSKai Wang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172de3b87aSKai Wang * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182de3b87aSKai Wang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192de3b87aSKai Wang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202de3b87aSKai Wang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212de3b87aSKai Wang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222de3b87aSKai Wang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232de3b87aSKai Wang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242de3b87aSKai Wang * SUCH DAMAGE.
252de3b87aSKai Wang */
262de3b87aSKai Wang
272de3b87aSKai Wang #include <assert.h>
282de3b87aSKai Wang #include <gelf.h>
292de3b87aSKai Wang
302de3b87aSKai Wang #include "_libelf.h"
312de3b87aSKai Wang
32*d003e0d7SEd Maste ELFTC_VCSID("$Id: gelf_symshndx.c 3732 2019-04-22 11:08:38Z jkoshy $");
332de3b87aSKai Wang
342de3b87aSKai Wang GElf_Sym *
gelf_getsymshndx(Elf_Data * d,Elf_Data * id,int ndx,GElf_Sym * dst,Elf32_Word * shindex)352de3b87aSKai Wang gelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst,
362de3b87aSKai Wang Elf32_Word *shindex)
372de3b87aSKai Wang {
382de3b87aSKai Wang int ec;
392de3b87aSKai Wang Elf *e;
402de3b87aSKai Wang size_t msz;
412de3b87aSKai Wang Elf_Scn *scn;
422de3b87aSKai Wang uint32_t sh_type;
432de3b87aSKai Wang struct _Libelf_Data *ld, *lid;
442de3b87aSKai Wang
452de3b87aSKai Wang ld = (struct _Libelf_Data *) d;
462de3b87aSKai Wang lid = (struct _Libelf_Data *) id;
472de3b87aSKai Wang
482de3b87aSKai Wang if (gelf_getsym(d, ndx, dst) == 0)
492de3b87aSKai Wang return (NULL);
502de3b87aSKai Wang
512de3b87aSKai Wang if (lid == NULL || (scn = lid->d_scn) == NULL ||
522de3b87aSKai Wang (e = scn->s_elf) == NULL || (e != ld->d_scn->s_elf) ||
532de3b87aSKai Wang shindex == NULL) {
542de3b87aSKai Wang LIBELF_SET_ERROR(ARGUMENT, 0);
552de3b87aSKai Wang return (NULL);
562de3b87aSKai Wang }
572de3b87aSKai Wang
582de3b87aSKai Wang ec = e->e_class;
592de3b87aSKai Wang assert(ec == ELFCLASS32 || ec == ELFCLASS64);
602de3b87aSKai Wang
612de3b87aSKai Wang if (ec == ELFCLASS32)
622de3b87aSKai Wang sh_type = scn->s_shdr.s_shdr32.sh_type;
632de3b87aSKai Wang else
642de3b87aSKai Wang sh_type = scn->s_shdr.s_shdr64.sh_type;
652de3b87aSKai Wang
662de3b87aSKai Wang if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
672de3b87aSKai Wang id->d_type != ELF_T_WORD) {
682de3b87aSKai Wang LIBELF_SET_ERROR(ARGUMENT, 0);
692de3b87aSKai Wang return (NULL);
702de3b87aSKai Wang }
712de3b87aSKai Wang
72*d003e0d7SEd Maste if ((msz = _libelf_msize(ELF_T_WORD, ec, e->e_version)) == 0)
73*d003e0d7SEd Maste return (NULL);
742de3b87aSKai Wang
75cf781b2eSEd Maste assert(ndx >= 0);
762de3b87aSKai Wang
77cf781b2eSEd Maste if (msz * (size_t) ndx >= id->d_size) {
782de3b87aSKai Wang LIBELF_SET_ERROR(ARGUMENT, 0);
792de3b87aSKai Wang return (NULL);
802de3b87aSKai Wang }
812de3b87aSKai Wang
822de3b87aSKai Wang *shindex = ((Elf32_Word *) id->d_buf)[ndx];
832de3b87aSKai Wang
842de3b87aSKai Wang return (dst);
852de3b87aSKai Wang }
862de3b87aSKai Wang
872de3b87aSKai Wang int
gelf_update_symshndx(Elf_Data * d,Elf_Data * id,int ndx,GElf_Sym * gs,Elf32_Word xindex)882de3b87aSKai Wang gelf_update_symshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *gs,
892de3b87aSKai Wang Elf32_Word xindex)
902de3b87aSKai Wang {
912de3b87aSKai Wang int ec;
922de3b87aSKai Wang Elf *e;
932de3b87aSKai Wang size_t msz;
942de3b87aSKai Wang Elf_Scn *scn;
952de3b87aSKai Wang uint32_t sh_type;
962de3b87aSKai Wang struct _Libelf_Data *ld, *lid;
972de3b87aSKai Wang
982de3b87aSKai Wang ld = (struct _Libelf_Data *) d;
992de3b87aSKai Wang lid = (struct _Libelf_Data *) id;
1002de3b87aSKai Wang
1012de3b87aSKai Wang if (gelf_update_sym(d, ndx, gs) == 0)
1022de3b87aSKai Wang return (0);
1032de3b87aSKai Wang
1042de3b87aSKai Wang if (lid == NULL || (scn = lid->d_scn) == NULL ||
1052de3b87aSKai Wang (e = scn->s_elf) == NULL || (e != ld->d_scn->s_elf)) {
1062de3b87aSKai Wang LIBELF_SET_ERROR(ARGUMENT, 0);
1072de3b87aSKai Wang return (0);
1082de3b87aSKai Wang }
1092de3b87aSKai Wang
1102de3b87aSKai Wang ec = e->e_class;
1112de3b87aSKai Wang assert(ec == ELFCLASS32 || ec == ELFCLASS64);
1122de3b87aSKai Wang
1132de3b87aSKai Wang if (ec == ELFCLASS32)
1142de3b87aSKai Wang sh_type = scn->s_shdr.s_shdr32.sh_type;
1152de3b87aSKai Wang else
1162de3b87aSKai Wang sh_type = scn->s_shdr.s_shdr64.sh_type;
1172de3b87aSKai Wang
1182de3b87aSKai Wang if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
1192de3b87aSKai Wang d->d_type != ELF_T_WORD) {
1202de3b87aSKai Wang LIBELF_SET_ERROR(ARGUMENT, 0);
1212de3b87aSKai Wang return (0);
1222de3b87aSKai Wang }
1232de3b87aSKai Wang
124*d003e0d7SEd Maste if ((msz = _libelf_msize(ELF_T_WORD, ec, e->e_version)) == 0)
125*d003e0d7SEd Maste return (0);
1262de3b87aSKai Wang
127cf781b2eSEd Maste assert(ndx >= 0);
128cf781b2eSEd Maste
129cf781b2eSEd Maste if (msz * (size_t) ndx >= id->d_size) {
1302de3b87aSKai Wang LIBELF_SET_ERROR(ARGUMENT, 0);
1312de3b87aSKai Wang return (0);
1322de3b87aSKai Wang }
1332de3b87aSKai Wang
1342de3b87aSKai Wang *(((Elf32_Word *) id->d_buf) + ndx) = xindex;
1352de3b87aSKai Wang
1362de3b87aSKai Wang return (1);
1372de3b87aSKai Wang }
138