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