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