1 /* $NetBSD: mdreloc.c,v 1.32 2010/08/06 16:33:18 joerg Exp $ */
5 __RCSID("$NetBSD: mdreloc.c,v 1.32 2010/08/06 16:33:18 joerg Exp $");
13 void _rtld_bind_start(void);
14 void _rtld_relocate_nonplt_self(Elf_Dyn
*, Elf_Addr
);
15 caddr_t
_rtld_bind(const Obj_Entry
*, Elf_Word
);
18 _rtld_setup_pltgot(const Obj_Entry
*obj
)
20 obj
->pltgot
[1] = (Elf_Addr
) obj
;
21 obj
->pltgot
[2] = (Elf_Addr
) &_rtld_bind_start
;
25 _rtld_relocate_nonplt_self(Elf_Dyn
*dynp
, Elf_Addr relocbase
)
27 const Elf_Rel
*rel
= 0, *rellim
;
31 for (; dynp
->d_tag
!= DT_NULL
; dynp
++) {
32 switch (dynp
->d_tag
) {
34 rel
= (const Elf_Rel
*)(relocbase
+ dynp
->d_un
.d_ptr
);
37 relsz
= dynp
->d_un
.d_val
;
41 if (rel
== 0 || relsz
== 0)
43 rellim
= (const Elf_Rel
*)((const uint8_t *)rel
+ relsz
);
44 for (; rel
< rellim
; rel
++) {
45 where
= (Elf_Addr
*)(relocbase
+ rel
->r_offset
);
46 *where
+= (Elf_Addr
)relocbase
;
51 _rtld_relocate_nonplt_objects(Obj_Entry
*obj
)
56 for (rel
= obj
->rel
; rel
< obj
->rellim
; rel
++) {
59 const Obj_Entry
*defobj
;
63 where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
64 symnum
= ELF_R_SYM(rel
->r_info
);
66 switch (ELF_R_TYPE(rel
->r_info
)) {
70 #if 1 /* XXX should not occur */
72 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
75 target
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
77 *where
+= target
- (Elf_Addr
)where
;
78 rdbg(("PC32 %s in %s --> %p in %s",
79 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
80 obj
->path
, (void *)*where
, defobj
->path
));
86 case R_TYPE(GLOB_DAT
):
87 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
90 target
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
92 tmp
= target
+ *where
;
95 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
96 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
97 obj
->path
, (void *)*where
, defobj
->path
));
100 case R_TYPE(RELATIVE
):
101 *where
+= (Elf_Addr
)obj
->relocbase
;
102 rdbg(("RELATIVE in %s --> %p", obj
->path
,
108 * These are deferred until all other relocations have
109 * been done. All we do here is make sure that the
110 * COPY relocation is not in a shared library. They
111 * are allowed only in executable files.
113 if (obj
->isdynamic
) {
115 "%s: Unexpected R_COPY relocation in shared library",
119 rdbg(("COPY (avoid in main)"));
123 rdbg(("sym = %lu, type = %lu, offset = %p, "
124 "contents = %p, symbol = %s",
125 symnum
, (u_long
)ELF_R_TYPE(rel
->r_info
),
126 (void *)rel
->r_offset
, (void *)*where
,
127 obj
->strtab
+ obj
->symtab
[symnum
].st_name
));
128 _rtld_error("%s: Unsupported relocation type %ld "
129 "in non-PLT relocations",
130 obj
->path
, (u_long
) ELF_R_TYPE(rel
->r_info
));
138 _rtld_relocate_plt_lazy(const Obj_Entry
*obj
)
145 for (rel
= obj
->pltrel
; rel
< obj
->pltrellim
; rel
++) {
146 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
148 assert(ELF_R_TYPE(rel
->r_info
) == R_TYPE(JMP_SLOT
));
150 /* Just relocate the GOT slots pointing into the PLT */
151 *where
+= (Elf_Addr
)obj
->relocbase
;
152 rdbg(("fixup !main in %s --> %p", obj
->path
, (void *)*where
));
159 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rel
*rel
,
162 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
165 const Obj_Entry
*defobj
;
166 unsigned long info
= rel
->r_info
;
168 assert(ELF_R_TYPE(info
) == R_TYPE(JMP_SLOT
));
170 def
= _rtld_find_plt_symdef(ELF_R_SYM(info
), obj
, &defobj
, tp
!= NULL
);
171 if (__predict_false(def
== NULL
))
173 if (__predict_false(def
== &_rtld_sym_zero
))
176 target
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
177 rdbg(("bind now/fixup in %s --> old=%p new=%p",
178 defobj
->strtab
+ def
->st_name
, (void *)*where
,
180 if (*where
!= target
)
188 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
190 const Elf_Rel
*rel
= (const Elf_Rel
*)((const uint8_t *)obj
->pltrel
195 new_value
= 0; /* XXX gcc */
197 err
= _rtld_relocate_plt_object(obj
, rel
, &new_value
);
201 return (caddr_t
)new_value
;
205 _rtld_relocate_plt_objects(const Obj_Entry
*obj
)
210 for (rel
= obj
->pltrel
; rel
< obj
->pltrellim
; rel
++) {
211 err
= _rtld_relocate_plt_object(obj
, rel
, NULL
);