mdreloc.c revision 1.6 1 /* $NetBSD: mdreloc.c,v 1.6 2024/11/30 01:04:05 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: mdreloc.c,v 1.6 2024/11/30 01:04:05 christos Exp $");
35 #endif /* not lint */
36
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <machine/cpu.h>
43
44 #include "debug.h"
45 #include "rtld.h"
46
47 #include <machine/lwp_private.h>
48
49 void _rtld_bind_start(void);
50 Elf_Addr _rtld_bind(const Obj_Entry *, Elf_Word);
51 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
52 static int _rtld_relocate_plt_object(const Obj_Entry *,
53 const Elf_Rela *, int, Elf_Addr *);
54
55 /*
56 * The OR1k PLT format is simple.
57 */
58
59 void
60 _rtld_setup_pltgot(const Obj_Entry *obj)
61 {
62 obj->pltgot[1] = (Elf_Addr) obj;
63 obj->pltgot[2] = (Elf_Addr) _rtld_bind_start;
64
65 dbg(("obj %s plt pltgot=%p start=%p obj=%p",
66 obj->path, obj->pltgot,
67 (void *) obj->pltgot[1], (void *) obj->pltgot[2]));
68 }
69
70 void
71 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
72 {
73 const Elf_Rela *rela = 0, *relalim;
74 Elf_Addr relasz = 0;
75 Elf_Addr *where;
76
77 for (; dynp->d_tag != DT_NULL; dynp++) {
78 switch (dynp->d_tag) {
79 case DT_RELA:
80 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
81 break;
82 case DT_RELASZ:
83 relasz = dynp->d_un.d_val;
84 break;
85 }
86 }
87 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
88 for (; rela < relalim; rela++) {
89 where = (Elf_Addr *)(relocbase + rela->r_offset);
90 *where = (Elf_Addr)(relocbase + rela->r_addend);
91 }
92 }
93
94 int
95 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
96 {
97 const Elf_Rela *rela;
98 const Elf_Sym *def = NULL;
99 const Obj_Entry *defobj = NULL;
100 unsigned long last_symnum = ULONG_MAX;
101
102 for (rela = obj->rela; rela < obj->relalim; rela++) {
103 Elf_Addr *where;
104 Elf_Addr tmp;
105 unsigned long symnum;
106
107 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
108
109 switch (ELF_R_TYPE(rela->r_info)) {
110 case R_TYPE(32): /* <address> S + A */
111 case R_TYPE(GLOB_DAT): /* <address> S + A */
112 case R_TYPE(TLS_DTPMOD):
113 case R_TYPE(TLS_DTPOFF):
114 case R_TYPE(TLS_TPOFF):
115 symnum = ELF_R_SYM(rela->r_info);
116 if (last_symnum != symnum) {
117 last_symnum = symnum;
118 def = _rtld_find_symdef(symnum, obj, &defobj,
119 false);
120 if (def == NULL)
121 return -1;
122 }
123 break;
124 default:
125 break;
126 }
127
128 switch (ELF_R_TYPE(rela->r_info)) {
129 #if 1 /* XXX Should not be necessary. */
130 case R_TYPE(JMP_SLOT):
131 #endif
132 case R_TYPE(NONE):
133 break;
134
135 case R_TYPE(32): /* <address> S + A */
136 case R_TYPE(GLOB_DAT): /* <address> S + A */
137 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
138 rela->r_addend);
139 if (*where != tmp)
140 *where = tmp;
141 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
142 obj->strtab + obj->symtab[symnum].st_name,
143 obj->path, (void *)*where, defobj->path));
144 break;
145
146 case R_TYPE(RELATIVE): /* <address> B + A */
147 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
148 rdbg(("RELATIVE in %s --> %p", obj->path,
149 (void *)*where));
150 break;
151
152 case R_TYPE(COPY):
153 /*
154 * These are deferred until all other relocations have
155 * been done. All we do here is make sure that the
156 * COPY relocation is not in a shared library. They
157 * are allowed only in executable files.
158 */
159 if (obj->isdynamic) {
160 _rtld_error(
161 "%s: Unexpected R_COPY relocation in shared library",
162 obj->path);
163 return -1;
164 }
165 rdbg(("COPY (avoid in main)"));
166 break;
167
168 case R_TYPE(TLS_DTPMOD):
169 *where = (Elf_Addr)defobj->tlsindex;
170 rdbg(("DTPMOD32 %s in %s --> %p in %s",
171 obj->strtab + obj->symtab[symnum].st_name,
172 obj->path, (void *)*where, defobj->path));
173 break;
174
175 case R_TYPE(TLS_DTPOFF):
176 *where = (Elf_Addr)(def->st_value + rela->r_addend
177 - TLS_DTV_OFFSET);
178 rdbg(("DTPOFF %s in %s --> %p in %s",
179 obj->strtab + obj->symtab[symnum].st_name,
180 obj->path, (void *)*where, defobj->path));
181 break;
182
183 case R_TYPE(TLS_TPOFF):
184 if (!defobj->tls_static &&
185 _rtld_tls_offset_allocate(__UNCONST(defobj)))
186 return -1;
187
188 *where = (Elf_Addr)(def->st_value + rela->r_addend
189 + defobj->tlsoffset - TLS_TP_OFFSET);
190 rdbg(("TPREL %s in %s --> %p in %s",
191 obj->strtab + obj->symtab[symnum].st_name,
192 obj->path, (void *)*where, defobj->path));
193 break;
194
195 default:
196 rdbg(("sym = %lu, type = %lu, offset = %p, "
197 "addend = %p, contents = %p, symbol = %s",
198 (u_long)ELF_R_SYM(rela->r_info),
199 (u_long)ELF_R_TYPE(rela->r_info),
200 (void *)rela->r_offset, (void *)rela->r_addend,
201 (void *)*where,
202 obj->strtab + obj->symtab[symnum].st_name));
203 _rtld_error("%s: Unsupported relocation type %ld "
204 "in non-PLT relocations",
205 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
206 return -1;
207 }
208 }
209 return 0;
210 }
211
212 int
213 _rtld_relocate_plt_lazy(Obj_Entry *obj)
214 {
215 const Elf_Rela *rela;
216 int reloff;
217
218 for (rela = obj->pltrela, reloff = 0;
219 rela < obj->pltrelalim;
220 rela++, reloff++) {
221 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
222
223 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
224
225 /*
226 * For now, simply treat then as relative.
227 */
228 *where += (Elf_Addr)obj->relocbase;
229 }
230
231 return 0;
232 }
233
234 static int
235 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
236 {
237 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
238 Elf_Addr value;
239 const Elf_Sym *def;
240 const Obj_Entry *defobj;
241 unsigned long info = rela->r_info;
242
243 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
244
245 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
246 if (__predict_false(def == NULL))
247 return -1;
248 if (__predict_false(def == &_rtld_sym_zero))
249 return 0;
250
251 value = (Elf_Addr)(defobj->relocbase + def->st_value);
252 rdbg(("bind now/fixup in %s --> new=%p",
253 defobj->strtab + def->st_name, (void *)value));
254
255 /*
256 * Simply replace the entry in GOT with the
257 * address of the routine.
258 */
259 assert(where >= (Elf_Word *)obj->pltgot);
260 assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
261 *where = value;
262
263 if (tp)
264 *tp = value;
265 return 0;
266 }
267
268 Elf_Addr
269 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
270 {
271 const Elf_Rela *rela = obj->pltrela + reloff;
272 Elf_Addr new_value;
273 int err;
274
275 new_value = 0; /* XXX gcc */
276
277 _rtld_shared_enter();
278 err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
279 if (err)
280 _rtld_die();
281 _rtld_shared_exit();
282
283 return new_value;
284 }
285
286 int
287 _rtld_relocate_plt_objects(const Obj_Entry *obj)
288 {
289 const Elf_Rela *rela;
290 int reloff;
291
292 for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
293 if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
294 return -1;
295 }
296 return 0;
297 }
298