1 /* $NetBSD: mdreloc.c,v 1.29 2009/03/16 02:46:47 lukem Exp $ */
5 __RCSID("$NetBSD: mdreloc.c,v 1.29 2009/03/16 02:46:47 lukem Exp $");
16 void _rtld_bind_start(void);
17 void _rtld_relocate_nonplt_self(Elf_Dyn
*, Elf_Addr
);
18 caddr_t
_rtld_bind(const Obj_Entry
*, Elf_Word
);
21 _rtld_setup_pltgot(const Obj_Entry
*obj
)
23 obj
->pltgot
[1] = (Elf_Addr
) obj
;
24 obj
->pltgot
[2] = (Elf_Addr
) &_rtld_bind_start
;
28 _rtld_relocate_nonplt_self(Elf_Dyn
*dynp
, Elf_Addr relocbase
)
30 const Elf_Rel
*rel
= 0, *rellim
;
34 for (; dynp
->d_tag
!= DT_NULL
; dynp
++) {
35 switch (dynp
->d_tag
) {
37 rel
= (const Elf_Rel
*)(relocbase
+ dynp
->d_un
.d_ptr
);
40 relsz
= dynp
->d_un
.d_val
;
44 rellim
= (const Elf_Rel
*)((const uint8_t *)rel
+ relsz
);
45 for (; rel
< rellim
; rel
++) {
46 where
= (Elf_Addr
*)(relocbase
+ rel
->r_offset
);
47 *where
+= (Elf_Addr
)relocbase
;
52 * It is possible for the compiler to emit relocations for unaligned data.
53 * We handle this situation with these inlines.
55 #define RELOC_ALIGNED_P(x) \
56 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
58 static inline Elf_Addr
63 memcpy(&res
, where
, sizeof(res
));
69 store_ptr(void *where
, Elf_Addr val
)
72 memcpy(where
, &val
, sizeof(val
));
76 _rtld_relocate_nonplt_objects(const Obj_Entry
*obj
)
80 for (rel
= obj
->rel
; rel
< obj
->rellim
; rel
++) {
83 const Obj_Entry
*defobj
;
87 where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
88 symnum
= ELF_R_SYM(rel
->r_info
);
90 switch (ELF_R_TYPE(rel
->r_info
)) {
94 #if 1 /* XXX should not occur */
95 case R_TYPE(PC24
): { /* word32 S - P + A */
99 * Extract addend and sign-extend if needed.
102 if (addend
& 0x00800000)
103 addend
|= 0xff000000;
105 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
108 tmp
= (Elf_Addr
)obj
->relocbase
+ def
->st_value
109 - (Elf_Addr
)where
+ (addend
<< 2);
110 if ((tmp
& 0xfe000000) != 0xfe000000 &&
111 (tmp
& 0xfe000000) != 0) {
113 "%s: R_ARM_PC24 relocation @ %p to %s failed "
114 "(displacement %ld (%#lx) out of range)",
116 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
117 (long) tmp
, (long) tmp
);
121 *where
= (*where
& 0xff000000) | (tmp
& 0x00ffffff);
122 rdbg(("PC24 %s in %s --> %p @ %p in %s",
123 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
124 obj
->path
, (void *)*where
, where
, defobj
->path
));
129 case R_TYPE(ABS32
): /* word32 B + S + A */
130 case R_TYPE(GLOB_DAT
): /* word32 B + S */
131 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
134 if (__predict_true(RELOC_ALIGNED_P(where
))) {
135 tmp
= *where
+ (Elf_Addr
)defobj
->relocbase
+
137 /* Set the Thumb bit, if needed. */
138 if (ELF_ST_TYPE(def
->st_info
) == STT_ARM_TFUNC
)
142 tmp
= load_ptr(where
) +
143 (Elf_Addr
)defobj
->relocbase
+
145 /* Set the Thumb bit, if needed. */
146 if (ELF_ST_TYPE(def
->st_info
) == STT_ARM_TFUNC
)
148 store_ptr(where
, tmp
);
150 rdbg(("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
151 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
152 obj
->path
, (void *)tmp
, where
, defobj
->path
));
155 case R_TYPE(RELATIVE
): /* word32 B + A */
156 if (__predict_true(RELOC_ALIGNED_P(where
))) {
157 tmp
= *where
+ (Elf_Addr
)obj
->relocbase
;
160 tmp
= load_ptr(where
) +
161 (Elf_Addr
)obj
->relocbase
;
162 store_ptr(where
, tmp
);
164 rdbg(("RELATIVE in %s --> %p", obj
->path
,
170 * These are deferred until all other relocations have
171 * been done. All we do here is make sure that the
172 * COPY relocation is not in a shared library. They
173 * are allowed only in executable files.
175 if (obj
->isdynamic
) {
177 "%s: Unexpected R_COPY relocation in shared library",
181 rdbg(("COPY (avoid in main)"));
185 rdbg(("sym = %lu, type = %lu, offset = %p, "
186 "contents = %p, symbol = %s",
187 symnum
, (u_long
)ELF_R_TYPE(rel
->r_info
),
188 (void *)rel
->r_offset
, (void *)load_ptr(where
),
189 obj
->strtab
+ obj
->symtab
[symnum
].st_name
));
190 _rtld_error("%s: Unsupported relocation type %ld "
191 "in non-PLT relocations",
192 obj
->path
, (u_long
) ELF_R_TYPE(rel
->r_info
));
200 _rtld_relocate_plt_lazy(const Obj_Entry
*obj
)
207 for (rel
= obj
->pltrel
; rel
< obj
->pltrellim
; rel
++) {
208 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
210 assert(ELF_R_TYPE(rel
->r_info
) == R_TYPE(JUMP_SLOT
));
212 /* Just relocate the GOT slots pointing into the PLT */
213 *where
+= (Elf_Addr
)obj
->relocbase
;
214 rdbg(("fixup !main in %s --> %p", obj
->path
, (void *)*where
));
221 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rel
*rel
,
224 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
227 const Obj_Entry
*defobj
;
229 assert(ELF_R_TYPE(rel
->r_info
) == R_TYPE(JUMP_SLOT
));
231 def
= _rtld_find_symdef(ELF_R_SYM(rel
->r_info
), obj
, &defobj
, true);
235 new_value
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
236 /* Set the Thumb bit, if needed. */
237 if (ELF_ST_TYPE(def
->st_info
) == STT_ARM_TFUNC
)
239 rdbg(("bind now/fixup in %s --> old=%p new=%p",
240 defobj
->strtab
+ def
->st_name
, (void *)*where
, (void *)new_value
));
241 if (*where
!= new_value
)
250 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
252 const Elf_Rel
*rel
= (const Elf_Rel
*)((const uint8_t *)obj
->pltrel
+ reloff
);
256 err
= _rtld_relocate_plt_object(obj
, rel
, &new_value
);
257 if (err
|| new_value
== 0)
260 return (caddr_t
)new_value
;
263 _rtld_relocate_plt_objects(const Obj_Entry
*obj
)
268 for (rel
= obj
->pltrel
; rel
< obj
->pltrellim
; rel
++) {
269 err
= _rtld_relocate_plt_object(obj
, rel
, NULL
);