1 /* $NetBSD: hppa_reloc.c,v 1.34 2010/09/24 11:41:46 skrll Exp $ */
4 * Copyright (c) 2002, 2004 The NetBSD Foundation, Inc.
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
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>
34 __RCSID("$NetBSD: hppa_reloc.c,v 1.34 2010/09/24 11:41:46 skrll Exp $");
38 #include <sys/types.h>
39 #include <sys/queue.h>
46 #ifdef RTLD_DEBUG_HPPA
47 #define hdbg(x) xprintf x
49 #define hdbg(x) /* nothing */
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
66 if (__predict_true(RELOC_ALIGNED_P(where
)))
67 return *(Elf_Addr
*)where
;
71 (void)memcpy(&res
, where
, sizeof(res
));
77 store_ptr(void *where
, Elf_Addr val
)
79 if (__predict_true(RELOC_ALIGNED_P(where
)))
80 *(Elf_Addr
*)where
= val
;
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
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
;
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
140 _rtld_relocate_nonplt_self(Elf_Dyn
*dynp
, Elf_Addr relocbase
)
142 const Elf_Rela
*relafirst
, *rela
, *relalim
;
146 const Elf_Rela
*plabel_relocs
[HPPA_PLABEL_PRE
];
147 int nplabel_relocs
= 0;
149 const Elf_Sym
*symtab
, *sym
;
150 unsigned long symnum
;
154 * Process the DYNAMIC section, looking for the non-PLT relocations.
160 for (; dynp
->d_tag
!= DT_NULL
; ++dynp
) {
161 switch (dynp
->d_tag
) {
164 relafirst
= (const Elf_Rela
*)
165 (relocbase
+ dynp
->d_un
.d_ptr
);
169 relasz
= dynp
->d_un
.d_val
;
173 symtab
= (const Elf_Sym
*)
174 (relocbase
+ dynp
->d_un
.d_ptr
);
178 pltgot
= (Elf_Addr
*)
179 (relocbase
+ dynp
->d_un
.d_ptr
);
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
)) {
193 relocbase
+ rela
->r_addend
);
195 sym
= symtab
+ symnum
;
197 relocbase
+ rela
->r_addend
+ sym
->st_value
);
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.
214 *((Elf_Addr
*)where
) =
215 relocbase
+ rela
->r_addend
;
217 plabel_relocs
[nplabel_relocs
++] = rela
;
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
)) {
247 hdbg(("DIR32 rela @%p(%p) -> %p(%p)\n",
248 (void *)rela
->r_offset
,
250 (void *)rela
->r_addend
,
251 (void *)*((Elf_Addr
*)where
) ));
254 case R_TYPE(PLABEL32
):
255 symnum
= ELF_R_SYM(rela
->r_info
);
257 hdbg(("PLABEL rela @%p(%p) -> %p(%p)\n",
258 (void *)rela
->r_offset
,
260 (void *)rela
->r_addend
,
261 (void *)*((Elf_Addr
*)where
) ));
263 sym
= symtab
+ symnum
;
265 hdbg(("PLABEL32 rela @%p(%p), symnum=%ld(%p) -> %p(%p)\n",
266 (void *)rela
->r_offset
,
269 (void *)sym
->st_value
,
270 (void *)rela
->r_addend
,
271 (void *)*((Elf_Addr
*)where
) ));
275 hdbg(("rela XXX reloc\n"));
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.
290 _rtld_function_descriptor_alloc(const Obj_Entry
*defobj
, const Elf_Sym
*def
,
293 Elf_Addr func_pc
, func_sl
;
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
+
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
++];
330 plabel
= NEW(hppa_plabel
);
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.
347 _rtld_function_descriptor_function(const void *addr
)
349 return (RTLD_IS_PLABEL(addr
) ?
350 (const void *) RTLD_GET_PLABEL(addr
)->hppa_plabel_pc
:
354 /* This sets up an object's GOT. */
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
++) {
369 const Obj_Entry
*defobj
;
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
)) {
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
;
389 if (def
->st_name
!= 0)
390 def
= _rtld_find_symdef(symnum
, obj
,
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
),
405 tmp
= (Elf_Addr
)(obj
->relocbase
+
408 if (load_ptr(where
) != tmp
)
409 store_ptr(where
, tmp
);
410 rdbg(("DIR32 in %s --> %p", obj
->path
,
411 (void *)load_ptr(where
)));
415 case R_TYPE(PLABEL32
):
417 def
= _rtld_find_symdef(symnum
, obj
, &defobj
,
422 tmp
= _rtld_function_descriptor_alloc(defobj
,
423 def
, rela
->r_addend
);
424 if (tmp
== (Elf_Addr
)-1)
429 rdbg(("PLABEL32 %s in %s --> %p in %s",
430 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
431 obj
->path
, (void *)*where
, defobj
->path
));
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
451 (obj
->relocbase
+ rela
->r_addend
);
455 rdbg(("PLABEL32 in %s --> %p", obj
->path
,
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
) {
469 "%s: Unexpected R_COPY relocation in shared library",
473 rdbg(("COPY (avoid in main)"));
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
));
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.
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
524 func_pc
= ((Elf_Addr
)(obj
->pltgot
)) - 16;
526 ((const char *)rela
- (const char *)(obj
->pltrela
));
528 rdbg(("lazy bind %s(%p) --> old=(%p,%p) new=(%p,%p)",
531 (void *)where
[0], (void *)where
[1],
532 (void *)func_pc
, (void *)func_sl
));
535 * Fill this PLT entry and return.
544 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rela
*rela
,
547 Elf_Word
*where
= (Elf_Word
*)(obj
->relocbase
+ rela
->r_offset
);
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
);
559 def
= _rtld_find_plt_symdef(ELF_R_SYM(info
), obj
, &defobj
,
561 if (__predict_false(def
== NULL
))
563 if (__predict_false(def
== &_rtld_sym_zero
))
566 func_pc
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
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
)
580 if (where
[1] != func_sl
)
584 *tp
= (Elf_Addr
)where
;
590 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
592 const Elf_Rela
*rela
;
593 Elf_Addr new_value
= 0; /* XXX gcc */
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
);
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)