1 /* $NetBSD: mdreloc.c,v 1.24 2009/03/16 02:46:48 lukem Exp $ */
5 __RCSID("$NetBSD: mdreloc.c,v 1.24 2009/03/16 02:46:48 lukem Exp $");
10 __RCSID("$NetBSD: mdreloc.c,v 1.24 2009/03/16 02:46:48 lukem 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(const 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 case R_TYPE(32): /* word32 S + A */
76 case R_TYPE(GLOB_DAT
): /* word32 S + A */
77 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
81 tmp
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
86 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
87 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
88 obj
->path
, (void *)*where
, defobj
->path
));
91 case R_TYPE(RELATIVE
): /* word32 B + A */
92 tmp
= (Elf_Addr
)(obj
->relocbase
+ rela
->r_addend
);
95 rdbg(("RELATIVE in %s --> %p", obj
->path
,
101 * These are deferred until all other relocations have
102 * been done. All we do here is make sure that the
103 * COPY relocation is not in a shared library. They
104 * are allowed only in executable files.
106 if (obj
->isdynamic
) {
108 "%s: Unexpected R_COPY relocation in shared library",
112 rdbg(("COPY (avoid in main)"));
116 rdbg(("sym = %lu, type = %lu, offset = %p, "
117 "addend = %p, contents = %p, symbol = %s",
118 symnum
, (u_long
)ELF_R_TYPE(rela
->r_info
),
119 (void *)rela
->r_offset
, (void *)rela
->r_addend
,
121 obj
->strtab
+ obj
->symtab
[symnum
].st_name
));
122 _rtld_error("%s: Unsupported relocation type %ld "
123 "in non-PLT relocations",
124 obj
->path
, (u_long
) ELF_R_TYPE(rela
->r_info
));
132 _rtld_relocate_plt_lazy(const Obj_Entry
*obj
)
134 const Elf_Rela
*rela
;
139 for (rela
= obj
->pltrela
; rela
< obj
->pltrelalim
; rela
++) {
140 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
142 assert(ELF_R_TYPE(rela
->r_info
) == R_TYPE(JMP_SLOT
));
144 /* Just relocate the GOT slots pointing into the PLT */
145 *where
+= (Elf_Addr
)obj
->relocbase
;
146 rdbg(("fixup !main in %s --> %p", obj
->path
, (void *)*where
));
153 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rela
*rela
, Elf_Addr
*tp
)
155 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
158 const Obj_Entry
*defobj
;
160 assert(ELF_R_TYPE(rela
->r_info
) == R_TYPE(JMP_SLOT
));
162 def
= _rtld_find_symdef(ELF_R_SYM(rela
->r_info
), obj
, &defobj
, true);
166 new_value
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
168 rdbg(("bind now/fixup in %s --> old=%p new=%p",
169 defobj
->strtab
+ def
->st_name
, (void *)*where
, (void *)new_value
));
170 if (*where
!= new_value
)
174 *tp
= new_value
- rela
->r_addend
;
180 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
182 const Elf_Rela
*rela
= (const Elf_Rela
*)((const uint8_t *)obj
->pltrela
+ reloff
);
186 result
= 0; /* XXX gcc */
188 err
= _rtld_relocate_plt_object(obj
, rela
, &result
);
189 if (err
|| result
== 0)
192 return (caddr_t
)result
;
196 _rtld_relocate_plt_objects(const Obj_Entry
*obj
)
198 const Elf_Rela
*rela
;
200 for (rela
= obj
->pltrela
; rela
< obj
->pltrelalim
; rela
++)
201 if (_rtld_relocate_plt_object(obj
, rela
, NULL
) < 0)