1 /* $NetBSD: mdreloc.c,v 1.34 2010/08/06 16:33:17 joerg Exp $ */
5 __RCSID("$NetBSD: mdreloc.c,v 1.34 2010/08/06 16:33:17 joerg Exp $");
14 void _rtld_bind_start(void);
15 void _rtld_relocate_nonplt_self(Elf_Dyn
*, Elf_Addr
);
16 caddr_t
_rtld_bind(const Obj_Entry
*, Elf_Word
);
19 _rtld_setup_pltgot(const Obj_Entry
*obj
)
21 obj
->pltgot
[1] = (Elf_Addr
) obj
;
22 obj
->pltgot
[2] = (Elf_Addr
) &_rtld_bind_start
;
26 _rtld_relocate_nonplt_self(Elf_Dyn
*dynp
, Elf_Addr relocbase
)
28 const Elf_Rel
*rel
= 0, *rellim
;
32 for (; dynp
->d_tag
!= DT_NULL
; dynp
++) {
33 switch (dynp
->d_tag
) {
35 rel
= (const Elf_Rel
*)(relocbase
+ dynp
->d_un
.d_ptr
);
38 relsz
= dynp
->d_un
.d_val
;
42 rellim
= (const Elf_Rel
*)((const uint8_t *)rel
+ relsz
);
43 for (; rel
< rellim
; rel
++) {
44 where
= (Elf_Addr
*)(relocbase
+ rel
->r_offset
);
45 *where
+= (Elf_Addr
)relocbase
;
50 * It is possible for the compiler to emit relocations for unaligned data.
51 * We handle this situation with these inlines.
53 #define RELOC_ALIGNED_P(x) \
54 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
56 static inline Elf_Addr
61 memcpy(&res
, where
, sizeof(res
));
67 store_ptr(void *where
, Elf_Addr val
)
70 memcpy(where
, &val
, sizeof(val
));
74 _rtld_relocate_nonplt_objects(Obj_Entry
*obj
)
78 for (rel
= obj
->rel
; rel
< obj
->rellim
; rel
++) {
81 const Obj_Entry
*defobj
;
85 where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
86 symnum
= ELF_R_SYM(rel
->r_info
);
88 switch (ELF_R_TYPE(rel
->r_info
)) {
92 #if 1 /* XXX should not occur */
93 case R_TYPE(PC24
): { /* word32 S - P + A */
97 * Extract addend and sign-extend if needed.
100 if (addend
& 0x00800000)
101 addend
|= 0xff000000;
103 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
106 tmp
= (Elf_Addr
)obj
->relocbase
+ def
->st_value
107 - (Elf_Addr
)where
+ (addend
<< 2);
108 if ((tmp
& 0xfe000000) != 0xfe000000 &&
109 (tmp
& 0xfe000000) != 0) {
111 "%s: R_ARM_PC24 relocation @ %p to %s failed "
112 "(displacement %ld (%#lx) out of range)",
114 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
115 (long) tmp
, (long) tmp
);
119 *where
= (*where
& 0xff000000) | (tmp
& 0x00ffffff);
120 rdbg(("PC24 %s in %s --> %p @ %p in %s",
121 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
122 obj
->path
, (void *)*where
, where
, defobj
->path
));
127 case R_TYPE(ABS32
): /* word32 B + S + A */
128 case R_TYPE(GLOB_DAT
): /* word32 B + S */
129 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
132 if (__predict_true(RELOC_ALIGNED_P(where
))) {
133 tmp
= *where
+ (Elf_Addr
)defobj
->relocbase
+
135 /* Set the Thumb bit, if needed. */
136 if (ELF_ST_TYPE(def
->st_info
) == STT_ARM_TFUNC
)
140 tmp
= load_ptr(where
) +
141 (Elf_Addr
)defobj
->relocbase
+
143 /* Set the Thumb bit, if needed. */
144 if (ELF_ST_TYPE(def
->st_info
) == STT_ARM_TFUNC
)
146 store_ptr(where
, tmp
);
148 rdbg(("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
149 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
150 obj
->path
, (void *)tmp
, where
, defobj
->path
));
153 case R_TYPE(RELATIVE
): /* word32 B + A */
154 if (__predict_true(RELOC_ALIGNED_P(where
))) {
155 tmp
= *where
+ (Elf_Addr
)obj
->relocbase
;
158 tmp
= load_ptr(where
) +
159 (Elf_Addr
)obj
->relocbase
;
160 store_ptr(where
, tmp
);
162 rdbg(("RELATIVE in %s --> %p", obj
->path
,
168 * These are deferred until all other relocations have
169 * been done. All we do here is make sure that the
170 * COPY relocation is not in a shared library. They
171 * are allowed only in executable files.
173 if (obj
->isdynamic
) {
175 "%s: Unexpected R_COPY relocation in shared library",
179 rdbg(("COPY (avoid in main)"));
183 rdbg(("sym = %lu, type = %lu, offset = %p, "
184 "contents = %p, symbol = %s",
185 symnum
, (u_long
)ELF_R_TYPE(rel
->r_info
),
186 (void *)rel
->r_offset
, (void *)load_ptr(where
),
187 obj
->strtab
+ obj
->symtab
[symnum
].st_name
));
188 _rtld_error("%s: Unsupported relocation type %ld "
189 "in non-PLT relocations",
190 obj
->path
, (u_long
) ELF_R_TYPE(rel
->r_info
));
198 _rtld_relocate_plt_lazy(const Obj_Entry
*obj
)
205 for (rel
= obj
->pltrel
; rel
< obj
->pltrellim
; rel
++) {
206 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
208 assert(ELF_R_TYPE(rel
->r_info
) == R_TYPE(JUMP_SLOT
));
210 /* Just relocate the GOT slots pointing into the PLT */
211 *where
+= (Elf_Addr
)obj
->relocbase
;
212 rdbg(("fixup !main in %s --> %p", obj
->path
, (void *)*where
));
219 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rel
*rel
,
222 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
225 const Obj_Entry
*defobj
;
226 unsigned long info
= rel
->r_info
;
228 assert(ELF_R_TYPE(info
) == R_TYPE(JUMP_SLOT
));
230 def
= _rtld_find_plt_symdef(ELF_R_SYM(info
), obj
, &defobj
, tp
!= NULL
);
231 if (__predict_false(def
== NULL
))
233 if (__predict_false(def
== &_rtld_sym_zero
))
236 new_value
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
237 /* Set the Thumb bit, if needed. */
238 if (ELF_ST_TYPE(def
->st_info
) == STT_ARM_TFUNC
)
240 rdbg(("bind now/fixup in %s --> old=%p new=%p",
241 defobj
->strtab
+ def
->st_name
, (void *)*where
, (void *)new_value
));
242 if (*where
!= new_value
)
251 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
253 const Elf_Rel
*rel
= (const Elf_Rel
*)((const uint8_t *)obj
->pltrel
+ reloff
);
254 Elf_Addr new_value
= 0; /* XXX gcc */
257 err
= _rtld_relocate_plt_object(obj
, rel
, &new_value
);
261 return (caddr_t
)new_value
;
264 _rtld_relocate_plt_objects(const Obj_Entry
*obj
)
269 for (rel
= obj
->pltrel
; rel
< obj
->pltrellim
; rel
++) {
270 err
= _rtld_relocate_plt_object(obj
, rel
, NULL
);