1 /* $NetBSD: mdreloc.c,v 1.31 2014/08/25 20:40:52 joerg Exp $ */
5 __RCSID("$NetBSD: mdreloc.c,v 1.31 2014/08/25 20:40:52 joerg Exp $");
10 __RCSID("$NetBSD: mdreloc.c,v 1.31 2014/08/25 20:40:52 joerg Exp $");
13 #include <sys/types.h>
19 void _rtld_bind_start(void);
20 void _rtld_relocate_nonplt_self(Elf_Dyn
*, Elf_Addr
);
21 caddr_t
_rtld_bind(const Obj_Entry
*, Elf_Word
);
22 static inline int _rtld_relocate_plt_object(const Obj_Entry
*,
23 const Elf_Rela
*, Elf_Addr
*);
26 _rtld_setup_pltgot(const Obj_Entry
*obj
)
28 obj
->pltgot
[1] = (Elf_Addr
) obj
;
29 obj
->pltgot
[2] = (Elf_Addr
) &_rtld_bind_start
;
33 _rtld_relocate_nonplt_self(Elf_Dyn
*dynp
, Elf_Addr relocbase
)
35 const Elf_Rela
*rela
= 0, *relalim
;
39 for (; dynp
->d_tag
!= DT_NULL
; dynp
++) {
40 switch (dynp
->d_tag
) {
42 rela
= (const Elf_Rela
*)(relocbase
+ dynp
->d_un
.d_ptr
);
45 relasz
= dynp
->d_un
.d_val
;
49 relalim
= (const Elf_Rela
*)((const uint8_t *)rela
+ relasz
);
50 for (; rela
< relalim
; rela
++) {
51 where
= (Elf_Addr
*)(relocbase
+ rela
->r_offset
);
52 *where
= (Elf_Addr
)(relocbase
+ rela
->r_addend
);
57 _rtld_relocate_nonplt_objects(Obj_Entry
*obj
)
61 for (rela
= obj
->rela
; rela
< obj
->relalim
; rela
++) {
64 const Obj_Entry
*defobj
;
68 where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
69 symnum
= ELF_R_SYM(rela
->r_info
);
71 switch (ELF_R_TYPE(rela
->r_info
)) {
75 #if 1 /* XXX should not occur */
77 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
81 tmp
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
85 rdbg(("GOT32 %s in %s --> %p in %s",
86 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
87 obj
->path
, (void *)*where
, defobj
->path
));
91 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
95 tmp
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
96 rela
->r_addend
) - (Elf_Addr
)where
;
99 rdbg(("PC32 %s in %s --> %p in %s",
100 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
101 obj
->path
, (void *)*where
, defobj
->path
));
106 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
110 tmp
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
114 rdbg(("32 %s in %s --> %p in %s",
115 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
116 obj
->path
, (void *)*where
, defobj
->path
));
119 case R_TYPE(GLOB_DAT
):
120 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
124 tmp
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
) +
128 rdbg(("GLOB_DAT %s in %s --> %p in %s",
129 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
130 obj
->path
, (void *)*where
, defobj
->path
));
133 case R_TYPE(RELATIVE
):
135 *where
= (Elf_Addr
)obj
->relocbase
+ rela
->r_addend
;
137 *where
+= (Elf_Addr
)obj
->relocbase
;
138 rdbg(("RELATIVE in %s --> %p", obj
->path
,
144 * These are deferred until all other relocations have
145 * been done. All we do here is make sure that the
146 * COPY relocation is not in a shared library. They
147 * are allowed only in executable files.
149 if (obj
->isdynamic
) {
151 "%s: Unexpected R_COPY relocation in shared library",
155 rdbg(("COPY (avoid in main)"));
158 case R_TYPE(TLS_DTPOFF32
):
159 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
163 *where
= (Elf_Addr
)(def
->st_value
);
165 rdbg(("TLS_DTPOFF32 %s in %s --> %p",
166 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
167 obj
->path
, (void *)*where
));
170 case R_TYPE(TLS_DTPMOD32
):
171 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
175 *where
= (Elf_Addr
)(defobj
->tlsindex
);
177 rdbg(("TLS_DTPMOD32 %s in %s --> %p",
178 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
179 obj
->path
, (void *)*where
));
183 case R_TYPE(TLS_TPOFF32
):
184 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
188 if (!defobj
->tls_done
&&
189 _rtld_tls_offset_allocate(obj
))
192 *where
= (Elf_Addr
)def
->st_value
+
193 rela
->r_addend
+ defobj
->tlsoffset
+
194 sizeof(struct tls_tcb
);
196 rdbg(("TLS_TPOFF32 %s in %s --> %p",
197 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
198 obj
->path
, (void *)*where
));
202 rdbg(("sym = %lu, type = %lu, offset = %p, "
203 "addend = %p, contents = %p, symbol = %s",
204 symnum
, (u_long
)ELF_R_TYPE(rela
->r_info
),
205 (void *)rela
->r_offset
, (void *)rela
->r_addend
,
207 obj
->strtab
+ obj
->symtab
[symnum
].st_name
));
208 _rtld_error("%s: Unsupported relocation type %ld "
209 "in non-PLT relocations",
210 obj
->path
, (u_long
) ELF_R_TYPE(rela
->r_info
));
218 _rtld_relocate_plt_lazy(const Obj_Entry
*obj
)
220 const Elf_Rela
*rela
;
225 for (rela
= obj
->pltrela
; rela
< obj
->pltrelalim
; rela
++) {
226 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
228 assert(ELF_R_TYPE(rela
->r_info
) == R_TYPE(JMP_SLOT
));
230 /* Just relocate the GOT slots pointing into the PLT */
231 *where
+= (Elf_Addr
)obj
->relocbase
;
232 rdbg(("fixup !main in %s --> %p", obj
->path
, (void *)*where
));
239 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
241 const Elf_Rela
*rela
= (const Elf_Rela
*)((const uint8_t *)obj
->pltrela
+ reloff
);
245 new_value
= 0; /* XXX gcc */
247 _rtld_shared_enter();
248 err
= _rtld_relocate_plt_object(obj
, rela
, &new_value
);
253 return (caddr_t
)new_value
;
257 _rtld_relocate_plt_objects(const Obj_Entry
*obj
)
259 const Elf_Rela
*rela
= obj
->pltrela
;
261 for (; rela
< obj
->pltrelalim
; rela
++)
262 if (_rtld_relocate_plt_object(obj
, rela
, NULL
) < 0)
269 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rela
*rela
, Elf_Addr
*tp
)
271 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
274 const Obj_Entry
*defobj
;
275 unsigned long info
= rela
->r_info
;
277 assert(ELF_R_TYPE(info
) == R_TYPE(JMP_SLOT
));
279 def
= _rtld_find_plt_symdef(ELF_R_SYM(info
), obj
, &defobj
, tp
!= NULL
);
280 if (__predict_false(def
== NULL
))
282 if (__predict_false(def
== &_rtld_sym_zero
))
285 if (ELF_ST_TYPE(def
->st_info
) == STT_GNU_IFUNC
) {
288 new_value
= _rtld_resolve_ifunc(defobj
, def
);
290 new_value
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
292 rdbg(("bind now/fixup in %s --> old=%p new=%p",
293 defobj
->strtab
+ def
->st_name
, (void *)*where
, (void *)new_value
));
294 if (*where
!= new_value
)