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 #include <limits.h>
30*f8fb3368SJohn Marino #include <stdint.h>
31*f8fb3368SJohn Marino
32*f8fb3368SJohn Marino #include "_libelf.h"
33*f8fb3368SJohn Marino
34*f8fb3368SJohn Marino ELFTC_VCSID("$Id: gelf_sym.c 3177 2015-03-30 18:19:41Z emaste $");
35*f8fb3368SJohn Marino
36*f8fb3368SJohn Marino GElf_Sym *
gelf_getsym(Elf_Data * ed,int ndx,GElf_Sym * dst)37*f8fb3368SJohn Marino gelf_getsym(Elf_Data *ed, int ndx, GElf_Sym *dst)
38*f8fb3368SJohn Marino {
39*f8fb3368SJohn Marino int ec;
40*f8fb3368SJohn Marino Elf *e;
41*f8fb3368SJohn Marino size_t msz;
42*f8fb3368SJohn Marino Elf_Scn *scn;
43*f8fb3368SJohn Marino uint32_t sh_type;
44*f8fb3368SJohn Marino Elf32_Sym *sym32;
45*f8fb3368SJohn Marino Elf64_Sym *sym64;
46*f8fb3368SJohn Marino struct _Libelf_Data *d;
47*f8fb3368SJohn Marino
48*f8fb3368SJohn Marino d = (struct _Libelf_Data *) ed;
49*f8fb3368SJohn Marino
50*f8fb3368SJohn Marino if (d == NULL || ndx < 0 || dst == NULL ||
51*f8fb3368SJohn Marino (scn = d->d_scn) == NULL ||
52*f8fb3368SJohn Marino (e = scn->s_elf) == NULL) {
53*f8fb3368SJohn Marino LIBELF_SET_ERROR(ARGUMENT, 0);
54*f8fb3368SJohn Marino return (NULL);
55*f8fb3368SJohn Marino }
56*f8fb3368SJohn Marino
57*f8fb3368SJohn Marino ec = e->e_class;
58*f8fb3368SJohn Marino assert(ec == ELFCLASS32 || ec == ELFCLASS64);
59*f8fb3368SJohn Marino
60*f8fb3368SJohn Marino if (ec == ELFCLASS32)
61*f8fb3368SJohn Marino sh_type = scn->s_shdr.s_shdr32.sh_type;
62*f8fb3368SJohn Marino else
63*f8fb3368SJohn Marino sh_type = scn->s_shdr.s_shdr64.sh_type;
64*f8fb3368SJohn Marino
65*f8fb3368SJohn Marino if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) {
66*f8fb3368SJohn Marino LIBELF_SET_ERROR(ARGUMENT, 0);
67*f8fb3368SJohn Marino return (NULL);
68*f8fb3368SJohn Marino }
69*f8fb3368SJohn Marino
70*f8fb3368SJohn Marino msz = _libelf_msize(ELF_T_SYM, ec, e->e_version);
71*f8fb3368SJohn Marino
72*f8fb3368SJohn Marino assert(msz > 0);
73*f8fb3368SJohn Marino assert(ndx >= 0);
74*f8fb3368SJohn Marino
75*f8fb3368SJohn Marino if (msz * (size_t) ndx >= d->d_data.d_size) {
76*f8fb3368SJohn Marino LIBELF_SET_ERROR(ARGUMENT, 0);
77*f8fb3368SJohn Marino return (NULL);
78*f8fb3368SJohn Marino }
79*f8fb3368SJohn Marino
80*f8fb3368SJohn Marino if (ec == ELFCLASS32) {
81*f8fb3368SJohn Marino sym32 = (Elf32_Sym *) d->d_data.d_buf + ndx;
82*f8fb3368SJohn Marino
83*f8fb3368SJohn Marino dst->st_name = sym32->st_name;
84*f8fb3368SJohn Marino dst->st_value = (Elf64_Addr) sym32->st_value;
85*f8fb3368SJohn Marino dst->st_size = (Elf64_Xword) sym32->st_size;
86*f8fb3368SJohn Marino dst->st_info = sym32->st_info;
87*f8fb3368SJohn Marino dst->st_other = sym32->st_other;
88*f8fb3368SJohn Marino dst->st_shndx = sym32->st_shndx;
89*f8fb3368SJohn Marino } else {
90*f8fb3368SJohn Marino sym64 = (Elf64_Sym *) d->d_data.d_buf + ndx;
91*f8fb3368SJohn Marino
92*f8fb3368SJohn Marino *dst = *sym64;
93*f8fb3368SJohn Marino }
94*f8fb3368SJohn Marino
95*f8fb3368SJohn Marino return (dst);
96*f8fb3368SJohn Marino }
97*f8fb3368SJohn Marino
98*f8fb3368SJohn Marino int
gelf_update_sym(Elf_Data * ed,int ndx,GElf_Sym * gs)99*f8fb3368SJohn Marino gelf_update_sym(Elf_Data *ed, int ndx, GElf_Sym *gs)
100*f8fb3368SJohn Marino {
101*f8fb3368SJohn Marino int ec;
102*f8fb3368SJohn Marino Elf *e;
103*f8fb3368SJohn Marino size_t msz;
104*f8fb3368SJohn Marino Elf_Scn *scn;
105*f8fb3368SJohn Marino uint32_t sh_type;
106*f8fb3368SJohn Marino Elf32_Sym *sym32;
107*f8fb3368SJohn Marino Elf64_Sym *sym64;
108*f8fb3368SJohn Marino struct _Libelf_Data *d;
109*f8fb3368SJohn Marino
110*f8fb3368SJohn Marino d = (struct _Libelf_Data *) ed;
111*f8fb3368SJohn Marino
112*f8fb3368SJohn Marino if (d == NULL || ndx < 0 || gs == NULL ||
113*f8fb3368SJohn Marino (scn = d->d_scn) == NULL ||
114*f8fb3368SJohn Marino (e = scn->s_elf) == NULL) {
115*f8fb3368SJohn Marino LIBELF_SET_ERROR(ARGUMENT, 0);
116*f8fb3368SJohn Marino return (0);
117*f8fb3368SJohn Marino }
118*f8fb3368SJohn Marino
119*f8fb3368SJohn Marino ec = e->e_class;
120*f8fb3368SJohn Marino assert(ec == ELFCLASS32 || ec == ELFCLASS64);
121*f8fb3368SJohn Marino
122*f8fb3368SJohn Marino if (ec == ELFCLASS32)
123*f8fb3368SJohn Marino sh_type = scn->s_shdr.s_shdr32.sh_type;
124*f8fb3368SJohn Marino else
125*f8fb3368SJohn Marino sh_type = scn->s_shdr.s_shdr64.sh_type;
126*f8fb3368SJohn Marino
127*f8fb3368SJohn Marino if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) {
128*f8fb3368SJohn Marino LIBELF_SET_ERROR(ARGUMENT, 0);
129*f8fb3368SJohn Marino return (0);
130*f8fb3368SJohn Marino }
131*f8fb3368SJohn Marino
132*f8fb3368SJohn Marino msz = _libelf_msize(ELF_T_SYM, ec, e->e_version);
133*f8fb3368SJohn Marino
134*f8fb3368SJohn Marino assert(msz > 0);
135*f8fb3368SJohn Marino assert(ndx >= 0);
136*f8fb3368SJohn Marino
137*f8fb3368SJohn Marino if (msz * (size_t) ndx >= d->d_data.d_size) {
138*f8fb3368SJohn Marino LIBELF_SET_ERROR(ARGUMENT, 0);
139*f8fb3368SJohn Marino return (0);
140*f8fb3368SJohn Marino }
141*f8fb3368SJohn Marino
142*f8fb3368SJohn Marino if (ec == ELFCLASS32) {
143*f8fb3368SJohn Marino sym32 = (Elf32_Sym *) d->d_data.d_buf + ndx;
144*f8fb3368SJohn Marino
145*f8fb3368SJohn Marino sym32->st_name = gs->st_name;
146*f8fb3368SJohn Marino sym32->st_info = gs->st_info;
147*f8fb3368SJohn Marino sym32->st_other = gs->st_other;
148*f8fb3368SJohn Marino sym32->st_shndx = gs->st_shndx;
149*f8fb3368SJohn Marino
150*f8fb3368SJohn Marino LIBELF_COPY_U32(sym32, gs, st_value);
151*f8fb3368SJohn Marino LIBELF_COPY_U32(sym32, gs, st_size);
152*f8fb3368SJohn Marino } else {
153*f8fb3368SJohn Marino sym64 = (Elf64_Sym *) d->d_data.d_buf + ndx;
154*f8fb3368SJohn Marino
155*f8fb3368SJohn Marino *sym64 = *gs;
156*f8fb3368SJohn Marino }
157*f8fb3368SJohn Marino
158*f8fb3368SJohn Marino return (1);
159*f8fb3368SJohn Marino }
160