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