1164190Sjkoshy/*-
2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy
3164190Sjkoshy * All rights reserved.
4164190Sjkoshy *
5164190Sjkoshy * Redistribution and use in source and binary forms, with or without
6164190Sjkoshy * modification, are permitted provided that the following conditions
7164190Sjkoshy * are met:
8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
9164190Sjkoshy *    notice, this list of conditions and the following disclaimer.
10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12164190Sjkoshy *    documentation and/or other materials provided with the distribution.
13164190Sjkoshy *
14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17164190Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24164190Sjkoshy * SUCH DAMAGE.
25164190Sjkoshy */
26164190Sjkoshy
27164190Sjkoshy#include <sys/cdefs.h>
28164190Sjkoshy__FBSDID("$FreeBSD: releng/10.3/lib/libelf/gelf_symshndx.c 164190 2006-11-11 17:16:35Z jkoshy $");
29164190Sjkoshy
30164190Sjkoshy#include <sys/limits.h>
31164190Sjkoshy
32164190Sjkoshy#include <assert.h>
33164190Sjkoshy#include <gelf.h>
34164190Sjkoshy
35164190Sjkoshy#include "_libelf.h"
36164190Sjkoshy
37164190SjkoshyGElf_Sym *
38164190Sjkoshygelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst,
39164190Sjkoshy    Elf32_Word *shindex)
40164190Sjkoshy{
41164190Sjkoshy	int ec;
42164190Sjkoshy	Elf *e;
43164190Sjkoshy	Elf_Scn *scn;
44164190Sjkoshy	size_t msz;
45164190Sjkoshy	uint32_t sh_type;
46164190Sjkoshy
47164190Sjkoshy	if (gelf_getsym(d, ndx, dst) == 0)
48164190Sjkoshy		return (NULL);
49164190Sjkoshy
50164190Sjkoshy	if (id == NULL || (scn = id->d_scn) == NULL ||
51164190Sjkoshy	    (e = scn->s_elf) == NULL || (e != d->d_scn->s_elf) ||
52164190Sjkoshy	    shindex == NULL) {
53164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
54164190Sjkoshy		return (NULL);
55164190Sjkoshy	}
56164190Sjkoshy
57164190Sjkoshy	ec = e->e_class;
58164190Sjkoshy	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
59164190Sjkoshy
60164190Sjkoshy	if (ec == ELFCLASS32)
61164190Sjkoshy		sh_type = scn->s_shdr.s_shdr32.sh_type;
62164190Sjkoshy	else
63164190Sjkoshy		sh_type = scn->s_shdr.s_shdr64.sh_type;
64164190Sjkoshy
65164190Sjkoshy	if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
66164190Sjkoshy	   id->d_type != ELF_T_WORD) {
67164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
68164190Sjkoshy		return (NULL);
69164190Sjkoshy	}
70164190Sjkoshy
71164190Sjkoshy	msz = _libelf_msize(ELF_T_WORD, ec, e->e_version);
72164190Sjkoshy
73164190Sjkoshy	assert(msz > 0);
74164190Sjkoshy
75164190Sjkoshy	if (msz * ndx >= id->d_size) {
76164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
77164190Sjkoshy		return (NULL);
78164190Sjkoshy	}
79164190Sjkoshy
80164190Sjkoshy	*shindex = ((Elf32_Word *) id->d_buf)[ndx];
81164190Sjkoshy
82164190Sjkoshy	return (dst);
83164190Sjkoshy}
84164190Sjkoshy
85164190Sjkoshyint
86164190Sjkoshygelf_update_symshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *gs,
87164190Sjkoshy    Elf32_Word xindex)
88164190Sjkoshy{
89164190Sjkoshy	int ec;
90164190Sjkoshy	Elf *e;
91164190Sjkoshy	Elf_Scn *scn;
92164190Sjkoshy	size_t msz;
93164190Sjkoshy	uint32_t sh_type;
94164190Sjkoshy
95164190Sjkoshy	if (gelf_update_sym(d, ndx, gs) == 0)
96164190Sjkoshy		return (0);
97164190Sjkoshy
98164190Sjkoshy	if (id == NULL || (scn = id->d_scn) == NULL ||
99164190Sjkoshy	    (e = scn->s_elf) == NULL || (e != d->d_scn->s_elf)) {
100164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
101164190Sjkoshy		return (0);
102164190Sjkoshy	}
103164190Sjkoshy
104164190Sjkoshy	ec = e->e_class;
105164190Sjkoshy	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
106164190Sjkoshy
107164190Sjkoshy	if (ec == ELFCLASS32)
108164190Sjkoshy		sh_type = scn->s_shdr.s_shdr32.sh_type;
109164190Sjkoshy	else
110164190Sjkoshy		sh_type = scn->s_shdr.s_shdr64.sh_type;
111164190Sjkoshy
112164190Sjkoshy	if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
113164190Sjkoshy	    d->d_type != ELF_T_WORD) {
114164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
115164190Sjkoshy		return (0);
116164190Sjkoshy	}
117164190Sjkoshy
118164190Sjkoshy	msz = _libelf_msize(ELF_T_WORD, ec, e->e_version);
119164190Sjkoshy	assert(msz > 0);
120164190Sjkoshy
121164190Sjkoshy	if (msz * ndx >= id->d_size) {
122164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
123164190Sjkoshy		return (0);
124164190Sjkoshy	}
125164190Sjkoshy
126164190Sjkoshy	*(((Elf32_Word *) id->d_buf) + ndx) = xindex;
127164190Sjkoshy
128164190Sjkoshy	return (1);
129164190Sjkoshy}
130