hppa_reloc.c revision 1.33 1 1.33 joerg /* $NetBSD: hppa_reloc.c,v 1.33 2010/08/06 16:33:17 joerg Exp $ */
2 1.1 fredette
3 1.1 fredette /*-
4 1.21 skrll * Copyright (c) 2002, 2004 The NetBSD Foundation, Inc.
5 1.1 fredette * All rights reserved.
6 1.1 fredette *
7 1.1 fredette * This code is derived from software contributed to The NetBSD Foundation
8 1.21 skrll * by Matt Fredette and Nick Hudson.
9 1.1 fredette *
10 1.1 fredette * Redistribution and use in source and binary forms, with or without
11 1.1 fredette * modification, are permitted provided that the following conditions
12 1.1 fredette * are met:
13 1.1 fredette * 1. Redistributions of source code must retain the above copyright
14 1.1 fredette * notice, this list of conditions and the following disclaimer.
15 1.1 fredette * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 fredette * notice, this list of conditions and the following disclaimer in the
17 1.1 fredette * documentation and/or other materials provided with the distribution.
18 1.1 fredette *
19 1.1 fredette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 fredette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 fredette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 fredette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 fredette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 fredette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 fredette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 fredette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 fredette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 fredette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 fredette * POSSIBILITY OF SUCH DAMAGE.
30 1.1 fredette */
31 1.1 fredette
32 1.23 skrll #include <sys/cdefs.h>
33 1.23 skrll #ifndef lint
34 1.33 joerg __RCSID("$NetBSD: hppa_reloc.c,v 1.33 2010/08/06 16:33:17 joerg Exp $");
35 1.23 skrll #endif /* not lint */
36 1.23 skrll
37 1.1 fredette #include <stdlib.h>
38 1.1 fredette #include <sys/types.h>
39 1.1 fredette #include <sys/queue.h>
40 1.1 fredette
41 1.24 skrll #include <string.h>
42 1.24 skrll
43 1.1 fredette #include "rtld.h"
44 1.1 fredette #include "debug.h"
45 1.1 fredette
46 1.1 fredette #ifdef RTLD_DEBUG_HPPA
47 1.14 mycroft #define hdbg(x) xprintf x
48 1.1 fredette #else
49 1.1 fredette #define hdbg(x) /* nothing */
50 1.1 fredette #endif
51 1.12 mycroft
52 1.20 skrll caddr_t _rtld_bind(const Obj_Entry *, const Elf_Addr);
53 1.12 mycroft void _rtld_bind_start(void);
54 1.13 mycroft void __rtld_setup_hppa_pltgot(const Obj_Entry *, Elf_Addr *);
55 1.1 fredette
56 1.1 fredette /*
57 1.24 skrll * It is possible for the compiler to emit relocations for unaligned data.
58 1.24 skrll * We handle this situation with these inlines.
59 1.24 skrll */
60 1.24 skrll #define RELOC_ALIGNED_P(x) \
61 1.24 skrll (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
62 1.24 skrll
63 1.24 skrll static inline Elf_Addr
64 1.24 skrll load_ptr(void *where)
65 1.24 skrll {
66 1.24 skrll if (__predict_true(RELOC_ALIGNED_P(where)))
67 1.24 skrll return *(Elf_Addr *)where;
68 1.24 skrll else {
69 1.24 skrll Elf_Addr res;
70 1.24 skrll
71 1.24 skrll (void)memcpy(&res, where, sizeof(res));
72 1.24 skrll return res;
73 1.24 skrll }
74 1.24 skrll }
75 1.24 skrll
76 1.24 skrll static inline void
77 1.24 skrll store_ptr(void *where, Elf_Addr val)
78 1.24 skrll {
79 1.24 skrll if (__predict_true(RELOC_ALIGNED_P(where)))
80 1.24 skrll *(Elf_Addr *)where = val;
81 1.24 skrll else
82 1.24 skrll (void)memcpy(where, &val, sizeof(val));
83 1.24 skrll }
84 1.24 skrll
85 1.24 skrll /*
86 1.1 fredette * In the runtime architecture (ABI), PLABEL function
87 1.1 fredette * pointers are distinguished from normal function
88 1.1 fredette * pointers by having the next-least-significant bit
89 1.1 fredette * set. (This bit is referred to as the L field in
90 1.1 fredette * HP documentation). The $$dyncall millicode is
91 1.1 fredette * aware of this.
92 1.1 fredette */
93 1.1 fredette #define RTLD_MAKE_PLABEL(plabel) (((Elf_Addr)(plabel)) | (1 << 1))
94 1.1 fredette #define RTLD_IS_PLABEL(addr) (((Elf_Addr)(addr)) & (1 << 1))
95 1.1 fredette #define RTLD_GET_PLABEL(addr) ((hppa_plabel *) (((Elf_Addr)addr) & ~3))
96 1.1 fredette
97 1.1 fredette /*
98 1.1 fredette * This is the PLABEL structure. The function PC and
99 1.1 fredette * shared linkage members must come first, as they are
100 1.1 fredette * the actual PLABEL.
101 1.1 fredette */
102 1.1 fredette typedef struct _hppa_plabel {
103 1.1 fredette Elf_Addr hppa_plabel_pc;
104 1.1 fredette Elf_Addr hppa_plabel_sl;
105 1.1 fredette SLIST_ENTRY(_hppa_plabel) hppa_plabel_next;
106 1.1 fredette } hppa_plabel;
107 1.1 fredette
108 1.1 fredette /*
109 1.1 fredette * For now allocated PLABEL structures are tracked on a
110 1.1 fredette * singly linked list. This maybe should be revisited.
111 1.1 fredette */
112 1.1 fredette static SLIST_HEAD(hppa_plabel_head, _hppa_plabel) hppa_plabel_list
113 1.1 fredette = SLIST_HEAD_INITIALIZER(hppa_plabel_list);
114 1.1 fredette
115 1.1 fredette /*
116 1.1 fredette * Because I'm hesitant to use NEW while relocating self,
117 1.1 fredette * this is a small pool of preallocated PLABELs.
118 1.1 fredette */
119 1.30 skrll #define HPPA_PLABEL_PRE (14)
120 1.1 fredette static hppa_plabel hppa_plabel_pre[HPPA_PLABEL_PRE];
121 1.1 fredette static int hppa_plabel_pre_next = 0;
122 1.1 fredette
123 1.20 skrll void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
124 1.20 skrll int _rtld_relocate_plt_objects(const Obj_Entry *);
125 1.25 skrll static inline int _rtld_relocate_plt_object(const Obj_Entry *,
126 1.25 skrll const Elf_Rela *, Elf_Addr *);
127 1.25 skrll
128 1.1 fredette /*
129 1.1 fredette * This bootstraps the dynamic linker by relocating its GOT.
130 1.1 fredette * On the hppa, unlike on other architectures, static strings
131 1.1 fredette * are found through the GOT. Static strings are essential
132 1.1 fredette * for RTLD_DEBUG, and I suspect they're used early even when
133 1.1 fredette * !defined(RTLD_DEBUG), making relocating the GOT essential.
134 1.1 fredette *
135 1.1 fredette * It gets worse. Relocating the GOT doesn't mean just walking
136 1.1 fredette * it and adding the relocbase to all of the entries. You must
137 1.1 fredette * find and use the GOT relocations, since those RELA relocations
138 1.1 fredette * have the necessary addends - the GOT comes initialized as
139 1.1 fredette * zeroes.
140 1.1 fredette */
141 1.1 fredette void
142 1.20 skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
143 1.1 fredette {
144 1.1 fredette const Elf_Rela *relafirst, *rela, *relalim;
145 1.22 chs Elf_Addr relasz;
146 1.24 skrll void *where;
147 1.20 skrll Elf_Addr *pltgot;
148 1.20 skrll const Elf_Rela *plabel_relocs[HPPA_PLABEL_PRE];
149 1.20 skrll int nplabel_relocs = 0;
150 1.20 skrll int i;
151 1.20 skrll const Elf_Sym *symtab, *sym;
152 1.20 skrll unsigned long symnum;
153 1.20 skrll hppa_plabel *plabel;
154 1.1 fredette
155 1.22 chs /*
156 1.22 chs * Process the DYNAMIC section, looking for the non-PLT relocations.
157 1.1 fredette */
158 1.1 fredette relafirst = NULL;
159 1.22 chs relasz = 0;
160 1.22 chs symtab = NULL;
161 1.22 chs pltgot = NULL;
162 1.1 fredette for (; dynp->d_tag != DT_NULL; ++dynp) {
163 1.1 fredette switch (dynp->d_tag) {
164 1.1 fredette
165 1.1 fredette case DT_RELA:
166 1.1 fredette relafirst = (const Elf_Rela *)
167 1.1 fredette (relocbase + dynp->d_un.d_ptr);
168 1.1 fredette break;
169 1.1 fredette
170 1.1 fredette case DT_RELASZ:
171 1.1 fredette relasz = dynp->d_un.d_val;
172 1.1 fredette break;
173 1.20 skrll
174 1.20 skrll case DT_SYMTAB:
175 1.20 skrll symtab = (const Elf_Sym *)
176 1.20 skrll (relocbase + dynp->d_un.d_ptr);
177 1.20 skrll break;
178 1.20 skrll
179 1.20 skrll case DT_PLTGOT:
180 1.20 skrll pltgot = (Elf_Addr *)
181 1.20 skrll (relocbase + dynp->d_un.d_ptr);
182 1.20 skrll break;
183 1.1 fredette }
184 1.1 fredette }
185 1.28 mjf relalim = (const Elf_Rela *)((const char *)relafirst + relasz);
186 1.1 fredette
187 1.18 skrll for (rela = relafirst; rela < relalim; rela++) {
188 1.20 skrll symnum = ELF_R_SYM(rela->r_info);
189 1.24 skrll where = (void *)(relocbase + rela->r_offset);
190 1.20 skrll
191 1.20 skrll switch (ELF_R_TYPE(rela->r_info)) {
192 1.20 skrll case R_TYPE(DIR32):
193 1.20 skrll if (symnum == 0)
194 1.24 skrll store_ptr(where,
195 1.24 skrll relocbase + rela->r_addend);
196 1.20 skrll else {
197 1.20 skrll sym = symtab + symnum;
198 1.24 skrll store_ptr(where,
199 1.24 skrll relocbase + rela->r_addend + sym->st_value);
200 1.20 skrll }
201 1.20 skrll break;
202 1.20 skrll
203 1.20 skrll case R_TYPE(PLABEL32):
204 1.20 skrll /*
205 1.20 skrll * PLABEL32 relocation processing is done in two phases
206 1.20 skrll *
207 1.20 skrll * i) local function relocations (symbol number == 0)
208 1.20 skrll * can be resolved immediately.
209 1.20 skrll *
210 1.20 skrll * ii) external function relocations are deferred until
211 1.20 skrll * we finish all other relocations so that global
212 1.20 skrll * data isn't accessed until all other non-PLT
213 1.20 skrll * relocations have been done.
214 1.20 skrll */
215 1.20 skrll if (symnum == 0)
216 1.20 skrll *((Elf_Addr *)where) =
217 1.20 skrll relocbase + rela->r_addend;
218 1.20 skrll else
219 1.20 skrll plabel_relocs[nplabel_relocs++] = rela;
220 1.20 skrll break;
221 1.20 skrll
222 1.20 skrll default:
223 1.20 skrll break;
224 1.20 skrll }
225 1.1 fredette }
226 1.1 fredette
227 1.20 skrll assert(nplabel_relocs < HPPA_PLABEL_PRE);
228 1.20 skrll for (i = 0; i < nplabel_relocs; i++) {
229 1.20 skrll rela = plabel_relocs[i];
230 1.24 skrll where = (void *)(relocbase + rela->r_offset);
231 1.20 skrll sym = symtab + ELF_R_SYM(rela->r_info);
232 1.20 skrll
233 1.20 skrll plabel = &hppa_plabel_pre[hppa_plabel_pre_next++];
234 1.20 skrll
235 1.20 skrll plabel->hppa_plabel_pc = (Elf_Addr)
236 1.20 skrll (relocbase + sym->st_value + rela->r_addend);
237 1.20 skrll plabel->hppa_plabel_sl = (Elf_Addr)pltgot;
238 1.20 skrll
239 1.20 skrll SLIST_INSERT_HEAD(&hppa_plabel_list, plabel, hppa_plabel_next);
240 1.20 skrll *((Elf_Addr *)where) = (Elf_Addr)(RTLD_MAKE_PLABEL(plabel));
241 1.20 skrll }
242 1.20 skrll
243 1.1 fredette #if defined(RTLD_DEBUG_HPPA)
244 1.18 skrll for (rela = relafirst; rela < relalim; rela++) {
245 1.24 skrll where = (void *)(relocbase + rela->r_offset);
246 1.20 skrll
247 1.20 skrll switch (ELF_R_TYPE(rela->r_info)) {
248 1.20 skrll case R_TYPE(DIR32):
249 1.20 skrll hdbg(("DIR32 rela @%p(%p) -> %p(%p)\n",
250 1.1 fredette (void *)rela->r_offset,
251 1.1 fredette (void *)where,
252 1.1 fredette (void *)rela->r_addend,
253 1.20 skrll (void *)*((Elf_Addr *)where) ));
254 1.20 skrll break;
255 1.20 skrll
256 1.20 skrll case R_TYPE(PLABEL32):
257 1.20 skrll symnum = ELF_R_SYM(rela->r_info);
258 1.20 skrll if (symnum == 0) {
259 1.20 skrll hdbg(("PLABEL rela @%p(%p) -> %p(%p)\n",
260 1.20 skrll (void *)rela->r_offset,
261 1.20 skrll (void *)where,
262 1.20 skrll (void *)rela->r_addend,
263 1.20 skrll (void *)*((Elf_Addr *)where) ));
264 1.20 skrll } else {
265 1.20 skrll sym = symtab + symnum;
266 1.20 skrll
267 1.20 skrll hdbg(("PLABEL32 rela @%p(%p), symnum=%ld(%p) -> %p(%p)\n",
268 1.20 skrll (void *)rela->r_offset,
269 1.20 skrll (void *)where,
270 1.20 skrll symnum,
271 1.20 skrll (void *)sym->st_value,
272 1.20 skrll (void *)rela->r_addend,
273 1.20 skrll (void *)*((Elf_Addr *)where) ));
274 1.20 skrll }
275 1.20 skrll break;
276 1.20 skrll default:
277 1.20 skrll hdbg(("rela XXX reloc\n"));
278 1.20 skrll break;
279 1.20 skrll }
280 1.1 fredette }
281 1.1 fredette #endif /* RTLD_DEBUG_HPPA */
282 1.1 fredette }
283 1.1 fredette
284 1.1 fredette /*
285 1.1 fredette * This allocates a PLABEL. If called with a non-NULL def, the
286 1.1 fredette * plabel is for the function associated with that definition
287 1.1 fredette * in the defining object defobj, plus the given addend. If
288 1.1 fredette * called with a NULL def, the plabel is for the function at
289 1.1 fredette * the (unrelocated) address in addend in the object defobj.
290 1.1 fredette */
291 1.1 fredette Elf_Addr
292 1.1 fredette _rtld_function_descriptor_alloc(const Obj_Entry *defobj, const Elf_Sym *def,
293 1.1 fredette Elf_Addr addend)
294 1.1 fredette {
295 1.1 fredette Elf_Addr func_pc, func_sl;
296 1.1 fredette hppa_plabel *plabel;
297 1.1 fredette
298 1.1 fredette if (def != NULL) {
299 1.1 fredette
300 1.1 fredette /*
301 1.1 fredette * We assume that symbols of type STT_NOTYPE
302 1.1 fredette * are undefined. Return NULL for these.
303 1.1 fredette */
304 1.1 fredette if (ELF_ST_TYPE(def->st_info) == STT_NOTYPE)
305 1.1 fredette return (Elf_Addr)NULL;
306 1.1 fredette
307 1.1 fredette /* Otherwise assert that this symbol must be a function. */
308 1.1 fredette assert(ELF_ST_TYPE(def->st_info) == STT_FUNC);
309 1.1 fredette
310 1.1 fredette func_pc = (Elf_Addr)(defobj->relocbase + def->st_value +
311 1.1 fredette addend);
312 1.1 fredette } else
313 1.1 fredette func_pc = (Elf_Addr)(defobj->relocbase + addend);
314 1.1 fredette
315 1.1 fredette /*
316 1.1 fredette * Search the existing PLABELs for one matching
317 1.1 fredette * this function. If there is one, return it.
318 1.1 fredette */
319 1.20 skrll func_sl = (Elf_Addr)(defobj->pltgot);
320 1.1 fredette SLIST_FOREACH(plabel, &hppa_plabel_list, hppa_plabel_next)
321 1.1 fredette if (plabel->hppa_plabel_pc == func_pc &&
322 1.1 fredette plabel->hppa_plabel_sl == func_sl)
323 1.1 fredette return RTLD_MAKE_PLABEL(plabel);
324 1.1 fredette
325 1.1 fredette /*
326 1.1 fredette * Once we've used up the preallocated set, we start
327 1.1 fredette * using NEW to allocate plabels.
328 1.1 fredette */
329 1.1 fredette if (hppa_plabel_pre_next < HPPA_PLABEL_PRE)
330 1.1 fredette plabel = &hppa_plabel_pre[hppa_plabel_pre_next++];
331 1.1 fredette else {
332 1.1 fredette plabel = NEW(hppa_plabel);
333 1.1 fredette if (plabel == NULL)
334 1.1 fredette return (Elf_Addr)-1;
335 1.1 fredette }
336 1.1 fredette
337 1.1 fredette /* Fill the new entry and insert it on the list. */
338 1.1 fredette plabel->hppa_plabel_pc = func_pc;
339 1.1 fredette plabel->hppa_plabel_sl = func_sl;
340 1.1 fredette SLIST_INSERT_HEAD(&hppa_plabel_list, plabel, hppa_plabel_next);
341 1.1 fredette
342 1.1 fredette return RTLD_MAKE_PLABEL(plabel);
343 1.1 fredette }
344 1.1 fredette
345 1.1 fredette /*
346 1.1 fredette * If a pointer is a PLABEL, this unwraps it.
347 1.1 fredette */
348 1.1 fredette const void *
349 1.1 fredette _rtld_function_descriptor_function(const void *addr)
350 1.1 fredette {
351 1.1 fredette return (RTLD_IS_PLABEL(addr) ?
352 1.1 fredette (const void *) RTLD_GET_PLABEL(addr)->hppa_plabel_pc :
353 1.1 fredette addr);
354 1.1 fredette }
355 1.1 fredette
356 1.2 mycroft /* This sets up an object's GOT. */
357 1.2 mycroft void
358 1.2 mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
359 1.2 mycroft {
360 1.20 skrll __rtld_setup_hppa_pltgot(obj, obj->pltgot);
361 1.4 mycroft }
362 1.4 mycroft
363 1.4 mycroft int
364 1.33 joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
365 1.4 mycroft {
366 1.5 mycroft const Elf_Rela *rela;
367 1.5 mycroft
368 1.5 mycroft for (rela = obj->rela; rela < obj->relalim; rela++) {
369 1.5 mycroft Elf_Addr *where;
370 1.5 mycroft const Elf_Sym *def;
371 1.5 mycroft const Obj_Entry *defobj;
372 1.5 mycroft Elf_Addr tmp;
373 1.6 mycroft unsigned long symnum;
374 1.5 mycroft
375 1.5 mycroft where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
376 1.6 mycroft symnum = ELF_R_SYM(rela->r_info);
377 1.5 mycroft
378 1.5 mycroft switch (ELF_R_TYPE(rela->r_info)) {
379 1.5 mycroft case R_TYPE(NONE):
380 1.5 mycroft break;
381 1.4 mycroft
382 1.5 mycroft case R_TYPE(DIR32):
383 1.6 mycroft if (symnum) {
384 1.5 mycroft /*
385 1.5 mycroft * This is either a DIR32 against a symbol
386 1.5 mycroft * (def->st_name != 0), or against a local
387 1.5 mycroft * section (def->st_name == 0).
388 1.5 mycroft */
389 1.6 mycroft def = obj->symtab + symnum;
390 1.5 mycroft defobj = obj;
391 1.5 mycroft if (def->st_name != 0)
392 1.6 mycroft def = _rtld_find_symdef(symnum, obj,
393 1.6 mycroft &defobj, false);
394 1.5 mycroft if (def == NULL)
395 1.5 mycroft return -1;
396 1.4 mycroft
397 1.5 mycroft tmp = (Elf_Addr)(defobj->relocbase +
398 1.5 mycroft def->st_value + rela->r_addend);
399 1.4 mycroft
400 1.24 skrll if (load_ptr(where) != tmp)
401 1.24 skrll store_ptr(where, tmp);
402 1.14 mycroft rdbg(("DIR32 %s in %s --> %p in %s",
403 1.7 mycroft obj->strtab + obj->symtab[symnum].st_name,
404 1.24 skrll obj->path, (void *)load_ptr(where), defobj->path));
405 1.5 mycroft } else {
406 1.5 mycroft tmp = (Elf_Addr)(obj->relocbase +
407 1.5 mycroft rela->r_addend);
408 1.5 mycroft
409 1.24 skrll if (load_ptr(where) != tmp)
410 1.24 skrll store_ptr(where, tmp);
411 1.20 skrll rdbg(("DIR32 in %s --> %p", obj->path,
412 1.24 skrll (void *)load_ptr(where)));
413 1.5 mycroft }
414 1.5 mycroft break;
415 1.5 mycroft
416 1.5 mycroft case R_TYPE(PLABEL32):
417 1.6 mycroft if (symnum) {
418 1.6 mycroft def = _rtld_find_symdef(symnum, obj, &defobj,
419 1.6 mycroft false);
420 1.5 mycroft if (def == NULL)
421 1.5 mycroft return -1;
422 1.4 mycroft
423 1.19 skrll tmp = _rtld_function_descriptor_alloc(defobj,
424 1.19 skrll def, rela->r_addend);
425 1.5 mycroft if (tmp == (Elf_Addr)-1)
426 1.5 mycroft return -1;
427 1.4 mycroft
428 1.4 mycroft if (*where != tmp)
429 1.4 mycroft *where = tmp;
430 1.14 mycroft rdbg(("PLABEL32 %s in %s --> %p in %s",
431 1.7 mycroft obj->strtab + obj->symtab[symnum].st_name,
432 1.7 mycroft obj->path, (void *)*where, defobj->path));
433 1.5 mycroft } else {
434 1.5 mycroft /*
435 1.5 mycroft * This is a PLABEL for a static function, and
436 1.5 mycroft * the dynamic linker has both allocated a PLT
437 1.5 mycroft * entry for this function and told us where it
438 1.5 mycroft * is. We can safely use the PLT entry as the
439 1.5 mycroft * PLABEL because there should be no other
440 1.5 mycroft * PLABEL reloc referencing this function.
441 1.5 mycroft * This object should also have an IPLT
442 1.5 mycroft * relocation to initialize the PLT entry.
443 1.5 mycroft *
444 1.5 mycroft * The dynamic linker should also have ensured
445 1.5 mycroft * that the addend has the
446 1.5 mycroft * next-least-significant bit set; the
447 1.5 mycroft * $$dyncall millicode uses this to distinguish
448 1.5 mycroft * a PLABEL pointer from a plain function
449 1.5 mycroft * pointer.
450 1.5 mycroft */
451 1.19 skrll tmp = (Elf_Addr)
452 1.19 skrll (obj->relocbase + rela->r_addend);
453 1.5 mycroft
454 1.5 mycroft if (*where != tmp)
455 1.5 mycroft *where = tmp;
456 1.14 mycroft rdbg(("PLABEL32 in %s --> %p", obj->path,
457 1.14 mycroft (void *)*where));
458 1.5 mycroft }
459 1.5 mycroft break;
460 1.4 mycroft
461 1.5 mycroft case R_TYPE(COPY):
462 1.4 mycroft /*
463 1.5 mycroft * These are deferred until all other relocations have
464 1.5 mycroft * been done. All we do here is make sure that the
465 1.5 mycroft * COPY relocation is not in a shared library. They
466 1.5 mycroft * are allowed only in executable files.
467 1.4 mycroft */
468 1.10 mycroft if (obj->isdynamic) {
469 1.5 mycroft _rtld_error(
470 1.5 mycroft "%s: Unexpected R_COPY relocation in shared library",
471 1.5 mycroft obj->path);
472 1.4 mycroft return -1;
473 1.5 mycroft }
474 1.14 mycroft rdbg(("COPY (avoid in main)"));
475 1.5 mycroft break;
476 1.4 mycroft
477 1.5 mycroft default:
478 1.14 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
479 1.5 mycroft "addend = %p, contents = %p, symbol = %s",
480 1.6 mycroft symnum, (u_long)ELF_R_TYPE(rela->r_info),
481 1.5 mycroft (void *)rela->r_offset, (void *)rela->r_addend,
482 1.24 skrll (void *)load_ptr(where),
483 1.6 mycroft obj->strtab + obj->symtab[symnum].st_name));
484 1.5 mycroft _rtld_error("%s: Unsupported relocation type %ld "
485 1.29 jmmv "in non-PLT relocations",
486 1.5 mycroft obj->path, (u_long) ELF_R_TYPE(rela->r_info));
487 1.4 mycroft return -1;
488 1.4 mycroft }
489 1.8 mycroft }
490 1.8 mycroft return 0;
491 1.8 mycroft }
492 1.8 mycroft
493 1.8 mycroft int
494 1.16 skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
495 1.8 mycroft {
496 1.8 mycroft const Elf_Rela *rela;
497 1.8 mycroft
498 1.8 mycroft for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
499 1.8 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
500 1.8 mycroft Elf_Addr func_pc, func_sl;
501 1.8 mycroft
502 1.8 mycroft assert(ELF_R_TYPE(rela->r_info) == R_TYPE(IPLT));
503 1.8 mycroft
504 1.8 mycroft /*
505 1.8 mycroft * If this is an IPLT reloc for a static function,
506 1.8 mycroft * fully resolve the PLT entry now.
507 1.8 mycroft */
508 1.8 mycroft if (ELF_R_SYM(rela->r_info) == 0) {
509 1.8 mycroft func_pc = (Elf_Addr)(obj->relocbase + rela->r_addend);
510 1.20 skrll func_sl = (Elf_Addr)(obj->pltgot);
511 1.8 mycroft }
512 1.8 mycroft
513 1.8 mycroft /*
514 1.8 mycroft * Otherwise set up for lazy binding.
515 1.8 mycroft */
516 1.8 mycroft else {
517 1.8 mycroft /*
518 1.8 mycroft * This function pointer points to the PLT
519 1.8 mycroft * stub added by the linker, and instead of
520 1.8 mycroft * a shared linkage value, we stash this
521 1.8 mycroft * relocation's offset. The PLT stub has
522 1.8 mycroft * already been set up to transfer to
523 1.8 mycroft * _rtld_bind_start.
524 1.8 mycroft */
525 1.20 skrll func_pc = ((Elf_Addr)(obj->pltgot)) - 16;
526 1.19 skrll func_sl = (Elf_Addr)
527 1.28 mjf ((const char *)rela - (const char *)(obj->pltrela));
528 1.8 mycroft }
529 1.20 skrll rdbg(("lazy bind %s(%p) --> old=(%p,%p) new=(%p,%p)",
530 1.20 skrll obj->path,
531 1.20 skrll (void *)where,
532 1.20 skrll (void *)where[0], (void *)where[1],
533 1.20 skrll (void *)func_pc, (void *)func_sl));
534 1.8 mycroft
535 1.8 mycroft /*
536 1.8 mycroft * Fill this PLT entry and return.
537 1.8 mycroft */
538 1.8 mycroft where[0] = func_pc;
539 1.8 mycroft where[1] = func_sl;
540 1.4 mycroft }
541 1.4 mycroft return 0;
542 1.1 fredette }
543 1.20 skrll
544 1.25 skrll static inline int
545 1.25 skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
546 1.20 skrll {
547 1.20 skrll Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
548 1.20 skrll const Elf_Sym *def;
549 1.20 skrll const Obj_Entry *defobj;
550 1.20 skrll Elf_Addr func_pc, func_sl;
551 1.31 christos unsigned long info = rela->r_info;
552 1.20 skrll
553 1.31 christos assert(ELF_R_TYPE(info) == R_TYPE(IPLT));
554 1.20 skrll
555 1.31 christos if (ELF_R_SYM(info) == 0) {
556 1.25 skrll func_pc = (Elf_Addr)(obj->relocbase + rela->r_addend);
557 1.25 skrll func_sl = (Elf_Addr)(obj->pltgot);
558 1.25 skrll } else {
559 1.31 christos def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj,
560 1.31 christos tp != NULL);
561 1.31 christos if (__predict_false(def == NULL))
562 1.25 skrll return -1;
563 1.31 christos if (__predict_false(def == &_rtld_sym_zero))
564 1.31 christos return 0;
565 1.20 skrll
566 1.25 skrll func_pc = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
567 1.25 skrll func_sl = (Elf_Addr)(defobj->pltgot);
568 1.20 skrll
569 1.25 skrll rdbg(("bind now/fixup in %s --> old=(%p,%p) new=(%p,%p)",
570 1.25 skrll defobj->strtab + def->st_name,
571 1.25 skrll (void *)where[0], (void *)where[1],
572 1.25 skrll (void *)func_pc, (void *)func_sl));
573 1.25 skrll }
574 1.20 skrll /*
575 1.20 skrll * Fill this PLT entry and return.
576 1.20 skrll */
577 1.20 skrll if (where[0] != func_pc)
578 1.20 skrll where[0] = func_pc;
579 1.20 skrll if (where[1] != func_sl)
580 1.20 skrll where[1] = func_sl;
581 1.20 skrll
582 1.25 skrll if (tp)
583 1.25 skrll *tp = (Elf_Addr)where;
584 1.25 skrll
585 1.25 skrll return 0;
586 1.25 skrll }
587 1.25 skrll
588 1.25 skrll caddr_t
589 1.25 skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
590 1.25 skrll {
591 1.28 mjf const Elf_Rela *rela;
592 1.32 skrll Elf_Addr new_value = 0; /* XXX gcc */
593 1.25 skrll int err;
594 1.25 skrll
595 1.28 mjf rela = (const Elf_Rela *)((const char *)obj->pltrela + reloff);
596 1.28 mjf
597 1.25 skrll assert(ELF_R_SYM(rela->r_info) != 0);
598 1.25 skrll
599 1.25 skrll err = _rtld_relocate_plt_object(obj, rela, &new_value);
600 1.31 christos if (err)
601 1.25 skrll _rtld_die();
602 1.25 skrll
603 1.25 skrll return (caddr_t)new_value;
604 1.20 skrll }
605 1.20 skrll
606 1.20 skrll int
607 1.20 skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
608 1.20 skrll {
609 1.25 skrll const Elf_Rela *rela = obj->pltrela;
610 1.20 skrll
611 1.25 skrll for (; rela < obj->pltrelalim; rela++) {
612 1.25 skrll if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
613 1.25 skrll return -1;
614 1.20 skrll }
615 1.20 skrll return 0;
616 1.20 skrll }
617