mkfs: drop support for zone != block
[minix.git] / libexec / ld.elf_so / arch / hppa / hppa_reloc.c
blobef1cdf1fbc6fd679de4b6a56979fa67bdc18cbf8
1 /* $NetBSD: hppa_reloc.c,v 1.34 2010/09/24 11:41:46 skrll Exp $ */
3 /*-
4 * Copyright (c) 2002, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Fredette and Nick Hudson.
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.
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.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: hppa_reloc.c,v 1.34 2010/09/24 11:41:46 skrll Exp $");
35 #endif /* not lint */
37 #include <stdlib.h>
38 #include <sys/types.h>
39 #include <sys/queue.h>
41 #include <string.h>
43 #include "rtld.h"
44 #include "debug.h"
46 #ifdef RTLD_DEBUG_HPPA
47 #define hdbg(x) xprintf x
48 #else
49 #define hdbg(x) /* nothing */
50 #endif
52 caddr_t _rtld_bind(const Obj_Entry *, const Elf_Addr);
53 void _rtld_bind_start(void);
54 void __rtld_setup_hppa_pltgot(const Obj_Entry *, Elf_Addr *);
57 * It is possible for the compiler to emit relocations for unaligned data.
58 * We handle this situation with these inlines.
60 #define RELOC_ALIGNED_P(x) \
61 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
63 static inline Elf_Addr
64 load_ptr(void *where)
66 if (__predict_true(RELOC_ALIGNED_P(where)))
67 return *(Elf_Addr *)where;
68 else {
69 Elf_Addr res;
71 (void)memcpy(&res, where, sizeof(res));
72 return res;
76 static inline void
77 store_ptr(void *where, Elf_Addr val)
79 if (__predict_true(RELOC_ALIGNED_P(where)))
80 *(Elf_Addr *)where = val;
81 else
82 (void)memcpy(where, &val, sizeof(val));
86 * In the runtime architecture (ABI), PLABEL function pointers are
87 * distinguished from normal function pointers by having the next-least-
88 * significant bit set. (This bit is referred to as the L field in HP
89 * documentation). The $$dyncall millicode is aware of this.
91 #define RTLD_MAKE_PLABEL(plabel) (((Elf_Addr)(plabel)) | (1 << 1))
92 #define RTLD_IS_PLABEL(addr) (((Elf_Addr)(addr)) & (1 << 1))
93 #define RTLD_GET_PLABEL(addr) ((hppa_plabel *) (((Elf_Addr)addr) & ~3))
96 * This is the PLABEL structure. The function PC and
97 * shared linkage members must come first, as they are
98 * the actual PLABEL.
100 typedef struct _hppa_plabel {
101 Elf_Addr hppa_plabel_pc;
102 Elf_Addr hppa_plabel_sl;
103 SLIST_ENTRY(_hppa_plabel) hppa_plabel_next;
104 } hppa_plabel;
107 * For now allocated PLABEL structures are tracked on a
108 * singly linked list. This maybe should be revisited.
110 static SLIST_HEAD(hppa_plabel_head, _hppa_plabel) hppa_plabel_list
111 = SLIST_HEAD_INITIALIZER(hppa_plabel_list);
114 * Because I'm hesitant to use NEW while relocating self,
115 * this is a small pool of preallocated PLABELs.
117 #define HPPA_PLABEL_PRE (14)
118 static hppa_plabel hppa_plabel_pre[HPPA_PLABEL_PRE];
119 static int hppa_plabel_pre_next = 0;
121 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
122 int _rtld_relocate_plt_objects(const Obj_Entry *);
123 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
124 const Elf_Rela *, Elf_Addr *);
127 * This bootstraps the dynamic linker by relocating its GOT.
128 * On the hppa, unlike on other architectures, static strings
129 * are found through the GOT. Static strings are essential
130 * for RTLD_DEBUG, and I suspect they're used early even when
131 * !defined(RTLD_DEBUG), making relocating the GOT essential.
133 * It gets worse. Relocating the GOT doesn't mean just walking
134 * it and adding the relocbase to all of the entries. You must
135 * find and use the GOT relocations, since those RELA relocations
136 * have the necessary addends - the GOT comes initialized as
137 * zeroes.
139 void
140 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
142 const Elf_Rela *relafirst, *rela, *relalim;
143 Elf_Addr relasz;
144 void *where;
145 Elf_Addr *pltgot;
146 const Elf_Rela *plabel_relocs[HPPA_PLABEL_PRE];
147 int nplabel_relocs = 0;
148 int i;
149 const Elf_Sym *symtab, *sym;
150 unsigned long symnum;
151 hppa_plabel *plabel;
154 * Process the DYNAMIC section, looking for the non-PLT relocations.
156 relafirst = NULL;
157 relasz = 0;
158 symtab = NULL;
159 pltgot = NULL;
160 for (; dynp->d_tag != DT_NULL; ++dynp) {
161 switch (dynp->d_tag) {
163 case DT_RELA:
164 relafirst = (const Elf_Rela *)
165 (relocbase + dynp->d_un.d_ptr);
166 break;
168 case DT_RELASZ:
169 relasz = dynp->d_un.d_val;
170 break;
172 case DT_SYMTAB:
173 symtab = (const Elf_Sym *)
174 (relocbase + dynp->d_un.d_ptr);
175 break;
177 case DT_PLTGOT:
178 pltgot = (Elf_Addr *)
179 (relocbase + dynp->d_un.d_ptr);
180 break;
183 relalim = (const Elf_Rela *)((const char *)relafirst + relasz);
185 for (rela = relafirst; rela < relalim; rela++) {
186 symnum = ELF_R_SYM(rela->r_info);
187 where = (void *)(relocbase + rela->r_offset);
189 switch (ELF_R_TYPE(rela->r_info)) {
190 case R_TYPE(DIR32):
191 if (symnum == 0)
192 store_ptr(where,
193 relocbase + rela->r_addend);
194 else {
195 sym = symtab + symnum;
196 store_ptr(where,
197 relocbase + rela->r_addend + sym->st_value);
199 break;
201 case R_TYPE(PLABEL32):
203 * PLABEL32 relocation processing is done in two phases
205 * i) local function relocations (symbol number == 0)
206 * can be resolved immediately.
208 * ii) external function relocations are deferred until
209 * we finish all other relocations so that global
210 * data isn't accessed until all other non-PLT
211 * relocations have been done.
213 if (symnum == 0)
214 *((Elf_Addr *)where) =
215 relocbase + rela->r_addend;
216 else
217 plabel_relocs[nplabel_relocs++] = rela;
218 break;
220 default:
221 break;
225 assert(nplabel_relocs < HPPA_PLABEL_PRE);
226 for (i = 0; i < nplabel_relocs; i++) {
227 rela = plabel_relocs[i];
228 where = (void *)(relocbase + rela->r_offset);
229 sym = symtab + ELF_R_SYM(rela->r_info);
231 plabel = &hppa_plabel_pre[hppa_plabel_pre_next++];
233 plabel->hppa_plabel_pc = (Elf_Addr)
234 (relocbase + sym->st_value + rela->r_addend);
235 plabel->hppa_plabel_sl = (Elf_Addr)pltgot;
237 SLIST_INSERT_HEAD(&hppa_plabel_list, plabel, hppa_plabel_next);
238 *((Elf_Addr *)where) = (Elf_Addr)(RTLD_MAKE_PLABEL(plabel));
241 #if defined(RTLD_DEBUG_HPPA)
242 for (rela = relafirst; rela < relalim; rela++) {
243 where = (void *)(relocbase + rela->r_offset);
245 switch (ELF_R_TYPE(rela->r_info)) {
246 case R_TYPE(DIR32):
247 hdbg(("DIR32 rela @%p(%p) -> %p(%p)\n",
248 (void *)rela->r_offset,
249 (void *)where,
250 (void *)rela->r_addend,
251 (void *)*((Elf_Addr *)where) ));
252 break;
254 case R_TYPE(PLABEL32):
255 symnum = ELF_R_SYM(rela->r_info);
256 if (symnum == 0) {
257 hdbg(("PLABEL 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 } else {
263 sym = symtab + symnum;
265 hdbg(("PLABEL32 rela @%p(%p), symnum=%ld(%p) -> %p(%p)\n",
266 (void *)rela->r_offset,
267 (void *)where,
268 symnum,
269 (void *)sym->st_value,
270 (void *)rela->r_addend,
271 (void *)*((Elf_Addr *)where) ));
273 break;
274 default:
275 hdbg(("rela XXX reloc\n"));
276 break;
279 #endif /* RTLD_DEBUG_HPPA */
283 * This allocates a PLABEL. If called with a non-NULL def, the
284 * plabel is for the function associated with that definition
285 * in the defining object defobj, plus the given addend. If
286 * called with a NULL def, the plabel is for the function at
287 * the (unrelocated) address in addend in the object defobj.
289 Elf_Addr
290 _rtld_function_descriptor_alloc(const Obj_Entry *defobj, const Elf_Sym *def,
291 Elf_Addr addend)
293 Elf_Addr func_pc, func_sl;
294 hppa_plabel *plabel;
296 if (def != NULL) {
299 * We assume that symbols of type STT_NOTYPE
300 * are undefined. Return NULL for these.
302 if (ELF_ST_TYPE(def->st_info) == STT_NOTYPE)
303 return (Elf_Addr)NULL;
305 /* Otherwise assert that this symbol must be a function. */
306 assert(ELF_ST_TYPE(def->st_info) == STT_FUNC);
308 func_pc = (Elf_Addr)(defobj->relocbase + def->st_value +
309 addend);
310 } else
311 func_pc = (Elf_Addr)(defobj->relocbase + addend);
314 * Search the existing PLABELs for one matching
315 * this function. If there is one, return it.
317 func_sl = (Elf_Addr)(defobj->pltgot);
318 SLIST_FOREACH(plabel, &hppa_plabel_list, hppa_plabel_next)
319 if (plabel->hppa_plabel_pc == func_pc &&
320 plabel->hppa_plabel_sl == func_sl)
321 return RTLD_MAKE_PLABEL(plabel);
324 * Once we've used up the preallocated set, we start
325 * using NEW to allocate plabels.
327 if (hppa_plabel_pre_next < HPPA_PLABEL_PRE)
328 plabel = &hppa_plabel_pre[hppa_plabel_pre_next++];
329 else {
330 plabel = NEW(hppa_plabel);
331 if (plabel == NULL)
332 return (Elf_Addr)-1;
335 /* Fill the new entry and insert it on the list. */
336 plabel->hppa_plabel_pc = func_pc;
337 plabel->hppa_plabel_sl = func_sl;
338 SLIST_INSERT_HEAD(&hppa_plabel_list, plabel, hppa_plabel_next);
340 return RTLD_MAKE_PLABEL(plabel);
344 * If a pointer is a PLABEL, this unwraps it.
346 const void *
347 _rtld_function_descriptor_function(const void *addr)
349 return (RTLD_IS_PLABEL(addr) ?
350 (const void *) RTLD_GET_PLABEL(addr)->hppa_plabel_pc :
351 addr);
354 /* This sets up an object's GOT. */
355 void
356 _rtld_setup_pltgot(const Obj_Entry *obj)
358 __rtld_setup_hppa_pltgot(obj, obj->pltgot);
362 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
364 const Elf_Rela *rela;
366 for (rela = obj->rela; rela < obj->relalim; rela++) {
367 Elf_Addr *where;
368 const Elf_Sym *def;
369 const Obj_Entry *defobj;
370 Elf_Addr tmp;
371 unsigned long symnum;
373 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
374 symnum = ELF_R_SYM(rela->r_info);
376 switch (ELF_R_TYPE(rela->r_info)) {
377 case R_TYPE(NONE):
378 break;
380 case R_TYPE(DIR32):
381 if (symnum) {
383 * This is either a DIR32 against a symbol
384 * (def->st_name != 0), or against a local
385 * section (def->st_name == 0).
387 def = obj->symtab + symnum;
388 defobj = obj;
389 if (def->st_name != 0)
390 def = _rtld_find_symdef(symnum, obj,
391 &defobj, false);
392 if (def == NULL)
393 return -1;
395 tmp = (Elf_Addr)(defobj->relocbase +
396 def->st_value + rela->r_addend);
398 if (load_ptr(where) != tmp)
399 store_ptr(where, tmp);
400 rdbg(("DIR32 %s in %s --> %p in %s",
401 obj->strtab + obj->symtab[symnum].st_name,
402 obj->path, (void *)load_ptr(where),
403 defobj->path));
404 } else {
405 tmp = (Elf_Addr)(obj->relocbase +
406 rela->r_addend);
408 if (load_ptr(where) != tmp)
409 store_ptr(where, tmp);
410 rdbg(("DIR32 in %s --> %p", obj->path,
411 (void *)load_ptr(where)));
413 break;
415 case R_TYPE(PLABEL32):
416 if (symnum) {
417 def = _rtld_find_symdef(symnum, obj, &defobj,
418 false);
419 if (def == NULL)
420 return -1;
422 tmp = _rtld_function_descriptor_alloc(defobj,
423 def, rela->r_addend);
424 if (tmp == (Elf_Addr)-1)
425 return -1;
427 if (*where != tmp)
428 *where = tmp;
429 rdbg(("PLABEL32 %s in %s --> %p in %s",
430 obj->strtab + obj->symtab[symnum].st_name,
431 obj->path, (void *)*where, defobj->path));
432 } else {
434 * This is a PLABEL for a static function, and
435 * the dynamic linker has both allocated a PLT
436 * entry for this function and told us where it
437 * is. We can safely use the PLT entry as the
438 * PLABEL because there should be no other
439 * PLABEL reloc referencing this function.
440 * This object should also have an IPLT
441 * relocation to initialize the PLT entry.
443 * The dynamic linker should also have ensured
444 * that the addend has the
445 * next-least-significant bit set; the
446 * $$dyncall millicode uses this to distinguish
447 * a PLABEL pointer from a plain function
448 * pointer.
450 tmp = (Elf_Addr)
451 (obj->relocbase + rela->r_addend);
453 if (*where != tmp)
454 *where = tmp;
455 rdbg(("PLABEL32 in %s --> %p", obj->path,
456 (void *)*where));
458 break;
460 case R_TYPE(COPY):
462 * These are deferred until all other relocations have
463 * been done. All we do here is make sure that the
464 * COPY relocation is not in a shared library. They
465 * are allowed only in executable files.
467 if (obj->isdynamic) {
468 _rtld_error(
469 "%s: Unexpected R_COPY relocation in shared library",
470 obj->path);
471 return -1;
473 rdbg(("COPY (avoid in main)"));
474 break;
476 default:
477 rdbg(("sym = %lu, type = %lu, offset = %p, "
478 "addend = %p, contents = %p, symbol = %s",
479 symnum, (u_long)ELF_R_TYPE(rela->r_info),
480 (void *)rela->r_offset, (void *)rela->r_addend,
481 (void *)load_ptr(where),
482 obj->strtab + obj->symtab[symnum].st_name));
483 _rtld_error("%s: Unsupported relocation type %ld "
484 "in non-PLT relocations",
485 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
486 return -1;
489 return 0;
493 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
495 const Elf_Rela *rela;
497 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
498 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
499 Elf_Addr func_pc, func_sl;
501 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(IPLT));
504 * If this is an IPLT reloc for a static function,
505 * fully resolve the PLT entry now.
507 if (ELF_R_SYM(rela->r_info) == 0) {
508 func_pc = (Elf_Addr)(obj->relocbase + rela->r_addend);
509 func_sl = (Elf_Addr)(obj->pltgot);
513 * Otherwise set up for lazy binding.
515 else {
517 * This function pointer points to the PLT
518 * stub added by the linker, and instead of
519 * a shared linkage value, we stash this
520 * relocation's offset. The PLT stub has
521 * already been set up to transfer to
522 * _rtld_bind_start.
524 func_pc = ((Elf_Addr)(obj->pltgot)) - 16;
525 func_sl = (Elf_Addr)
526 ((const char *)rela - (const char *)(obj->pltrela));
528 rdbg(("lazy bind %s(%p) --> old=(%p,%p) new=(%p,%p)",
529 obj->path,
530 (void *)where,
531 (void *)where[0], (void *)where[1],
532 (void *)func_pc, (void *)func_sl));
535 * Fill this PLT entry and return.
537 where[0] = func_pc;
538 where[1] = func_sl;
540 return 0;
543 static inline int
544 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
545 Elf_Addr *tp)
547 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
548 const Elf_Sym *def;
549 const Obj_Entry *defobj;
550 Elf_Addr func_pc, func_sl;
551 unsigned long info = rela->r_info;
553 assert(ELF_R_TYPE(info) == R_TYPE(IPLT));
555 if (ELF_R_SYM(info) == 0) {
556 func_pc = (Elf_Addr)(obj->relocbase + rela->r_addend);
557 func_sl = (Elf_Addr)(obj->pltgot);
558 } else {
559 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj,
560 tp != NULL);
561 if (__predict_false(def == NULL))
562 return -1;
563 if (__predict_false(def == &_rtld_sym_zero))
564 return 0;
566 func_pc = (Elf_Addr)(defobj->relocbase + def->st_value +
567 rela->r_addend);
568 func_sl = (Elf_Addr)(defobj->pltgot);
570 rdbg(("bind now/fixup in %s --> old=(%p,%p) new=(%p,%p)",
571 defobj->strtab + def->st_name,
572 (void *)where[0], (void *)where[1],
573 (void *)func_pc, (void *)func_sl));
576 * Fill this PLT entry and return.
578 if (where[0] != func_pc)
579 where[0] = func_pc;
580 if (where[1] != func_sl)
581 where[1] = func_sl;
583 if (tp)
584 *tp = (Elf_Addr)where;
586 return 0;
589 caddr_t
590 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
592 const Elf_Rela *rela;
593 Elf_Addr new_value = 0; /* XXX gcc */
594 int err;
596 rela = (const Elf_Rela *)((const char *)obj->pltrela + reloff);
598 assert(ELF_R_SYM(rela->r_info) != 0);
600 err = _rtld_relocate_plt_object(obj, rela, &new_value);
601 if (err)
602 _rtld_die();
604 return (caddr_t)new_value;
608 _rtld_relocate_plt_objects(const Obj_Entry *obj)
610 const Elf_Rela *rela = obj->pltrela;
612 for (; rela < obj->pltrelalim; rela++) {
613 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
614 return -1;
616 return 0;