1 /* $NetBSD: mdreloc.c,v 1.27 2010/08/06 16:33:18 joerg Exp $ */
5 __RCSID("$NetBSD: mdreloc.c,v 1.27 2010/08/06 16:33:18 joerg Exp $");
10 __RCSID("$NetBSD: mdreloc.c,v 1.27 2010/08/06 16:33:18 joerg Exp $");
13 #include <sys/types.h>
18 void _rtld_bind_start(void);
19 void _rtld_relocate_nonplt_self(Elf_Dyn
*, Elf_Addr
);
20 caddr_t
_rtld_bind(const Obj_Entry
*, Elf_Word
);
21 static inline int _rtld_relocate_plt_object(const Obj_Entry
*,
22 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
;
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
+
82 rela
->r_addend
) - (Elf_Addr
)where
;
85 rdbg(("PC32 %s in %s --> %p in %s",
86 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
87 obj
->path
, (void *)*where
, defobj
->path
));
93 case R_TYPE(GLOB_DAT
):
94 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
98 tmp
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
102 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
103 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
104 obj
->path
, (void *)*where
, defobj
->path
));
107 case R_TYPE(RELATIVE
):
108 *where
+= (Elf_Addr
)obj
->relocbase
;
109 rdbg(("RELATIVE in %s --> %p", obj
->path
,
115 * These are deferred until all other relocations have
116 * been done. All we do here is make sure that the
117 * COPY relocation is not in a shared library. They
118 * are allowed only in executable files.
120 if (obj
->isdynamic
) {
122 "%s: Unexpected R_COPY relocation in shared library",
126 rdbg(("COPY (avoid in main)"));
130 rdbg(("sym = %lu, type = %lu, offset = %p, "
131 "addend = %p, contents = %p, symbol = %s",
132 symnum
, (u_long
)ELF_R_TYPE(rela
->r_info
),
133 (void *)rela
->r_offset
, (void *)rela
->r_addend
,
135 obj
->strtab
+ obj
->symtab
[symnum
].st_name
));
136 _rtld_error("%s: Unsupported relocation type %ld "
137 "in non-PLT relocations",
138 obj
->path
, (u_long
) ELF_R_TYPE(rela
->r_info
));
146 _rtld_relocate_plt_lazy(const Obj_Entry
*obj
)
148 const Elf_Rela
*rela
;
153 for (rela
= obj
->pltrela
; rela
< obj
->pltrelalim
; rela
++) {
154 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
156 assert(ELF_R_TYPE(rela
->r_info
) == R_TYPE(JMP_SLOT
));
158 /* Just relocate the GOT slots pointing into the PLT */
159 *where
+= (Elf_Addr
)obj
->relocbase
;
160 rdbg(("fixup !main in %s --> %p", obj
->path
, (void *)*where
));
167 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rela
*rela
,
170 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
173 const Obj_Entry
*defobj
;
174 unsigned long info
= rela
->r_info
;
176 assert(ELF_R_TYPE(info
) == R_TYPE(JMP_SLOT
));
178 def
= _rtld_find_plt_symdef(ELF_R_SYM(info
), obj
, &defobj
, tp
!= NULL
);
179 if (__predict_false(def
== NULL
))
181 if (__predict_false(def
== &_rtld_sym_zero
))
184 assert(rela
->r_addend
== 0);
185 new_value
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
187 rdbg(("bind now/fixup in %s --> old=%p new=%p",
188 defobj
->strtab
+ def
->st_name
, (void *)*where
, (void *)new_value
));
189 if (*where
!= new_value
)
193 *tp
= new_value
- rela
->r_addend
;
199 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
201 const Elf_Rela
*rela
= (const Elf_Rela
*)((const uint8_t *)obj
->pltrela
+ reloff
);
205 result
= 0; /* XXX gcc */
207 err
= _rtld_relocate_plt_object(obj
, rela
, &result
);
211 return (caddr_t
)result
;
215 _rtld_relocate_plt_objects(const Obj_Entry
*obj
)
217 const Elf_Rela
*rela
;
219 for (rela
= obj
->pltrela
; rela
< obj
->pltrelalim
; rela
++)
220 if (_rtld_relocate_plt_object(obj
, rela
, NULL
) < 0)