1 /* $NetBSD: mdreloc.c,v 1.23 2009/03/16 02:46:47 lukem Exp $ */
5 __RCSID("$NetBSD: mdreloc.c,v 1.23 2009/03/16 02:46:47 lukem Exp $");
10 __RCSID("$NetBSD: mdreloc.c,v 1.23 2009/03/16 02:46:47 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
*);
27 _rtld_setup_pltgot(const Obj_Entry
*obj
)
29 obj
->pltgot
[1] = (Elf_Addr
) obj
;
30 obj
->pltgot
[2] = (Elf_Addr
) &_rtld_bind_start
;
34 _rtld_relocate_nonplt_self(Elf_Dyn
*dynp
, Elf_Addr relocbase
)
36 const Elf_Rela
*rela
= 0, *relalim
;
40 for (; dynp
->d_tag
!= DT_NULL
; dynp
++) {
41 switch (dynp
->d_tag
) {
43 rela
= (const Elf_Rela
*)(relocbase
+ dynp
->d_un
.d_ptr
);
46 relasz
= dynp
->d_un
.d_val
;
50 relalim
= (const Elf_Rela
*)((const uint8_t *)rela
+ relasz
);
51 for (; rela
< relalim
; rela
++) {
52 where
= (Elf_Addr
*)(relocbase
+ rela
->r_offset
);
53 *where
+= (Elf_Addr
)relocbase
;
58 _rtld_relocate_nonplt_objects(const Obj_Entry
*obj
)
62 for (rela
= obj
->rela
; rela
< obj
->relalim
; rela
++) {
65 const Obj_Entry
*defobj
;
69 where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
70 symnum
= ELF_R_SYM(rela
->r_info
);
72 switch (ELF_R_TYPE(rela
->r_info
)) {
76 #if 1 /* XXX should not occur */
78 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
82 tmp
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
83 rela
->r_addend
) - (Elf_Addr
)where
;
86 rdbg(("PC32 %s in %s --> %p in %s",
87 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
88 obj
->path
, (void *)*where
, defobj
->path
));
94 case R_TYPE(GLOB_DAT
):
95 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
99 tmp
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
103 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
104 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
105 obj
->path
, (void *)*where
, defobj
->path
));
108 case R_TYPE(RELATIVE
):
109 *where
+= (Elf_Addr
)obj
->relocbase
;
110 rdbg(("RELATIVE in %s --> %p", obj
->path
,
116 * These are deferred until all other relocations have
117 * been done. All we do here is make sure that the
118 * COPY relocation is not in a shared library. They
119 * are allowed only in executable files.
121 if (obj
->isdynamic
) {
123 "%s: Unexpected R_COPY relocation in shared library",
127 rdbg(("COPY (avoid in main)"));
131 rdbg(("sym = %lu, type = %lu, offset = %p, "
132 "addend = %p, contents = %p, symbol = %s",
133 symnum
, (u_long
)ELF_R_TYPE(rela
->r_info
),
134 (void *)rela
->r_offset
, (void *)rela
->r_addend
,
136 obj
->strtab
+ obj
->symtab
[symnum
].st_name
));
137 _rtld_error("%s: Unsupported relocation type %ld "
138 "in non-PLT relocations",
139 obj
->path
, (u_long
) ELF_R_TYPE(rela
->r_info
));
147 _rtld_relocate_plt_lazy(const Obj_Entry
*obj
)
149 const Elf_Rela
*rela
;
154 for (rela
= obj
->pltrela
; rela
< obj
->pltrelalim
; rela
++) {
155 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
157 assert(ELF_R_TYPE(rela
->r_info
) == R_TYPE(JMP_SLOT
));
159 /* Just relocate the GOT slots pointing into the PLT */
160 *where
+= (Elf_Addr
)obj
->relocbase
;
161 rdbg(("fixup !main in %s --> %p", obj
->path
, (void *)*where
));
168 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rela
*rela
, Elf_Addr
*tp
)
170 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
173 const Obj_Entry
*defobj
;
175 assert(ELF_R_TYPE(rela
->r_info
) == R_TYPE(JMP_SLOT
));
177 def
= _rtld_find_symdef(ELF_R_SYM(rela
->r_info
), obj
, &defobj
, true);
181 assert(rela
->r_addend
== 0);
182 new_value
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
184 rdbg(("bind now/fixup in %s --> old=%p new=%p",
185 defobj
->strtab
+ def
->st_name
, (void *)*where
, (void *)new_value
));
186 if (*where
!= new_value
)
190 *tp
= new_value
- rela
->r_addend
;
196 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
198 const Elf_Rela
*rela
= (const Elf_Rela
*)((const uint8_t *)obj
->pltrela
+ reloff
);
202 result
= 0; /* XXX gcc */
204 err
= _rtld_relocate_plt_object(obj
, rela
, &result
);
205 if (err
|| result
== 0)
208 return (caddr_t
)result
;
212 _rtld_relocate_plt_objects(const Obj_Entry
*obj
)
214 const Elf_Rela
*rela
;
216 for (rela
= obj
->pltrela
; rela
< obj
->pltrelalim
; rela
++)
217 if (_rtld_relocate_plt_object(obj
, rela
, NULL
) < 0)