1 /* $NetBSD: mdreloc.c,v 1.27 2009/03/16 02:44:47 lukem Exp $ */
5 __RCSID("$NetBSD: mdreloc.c,v 1.27 2009/03/16 02:44:47 lukem Exp $");
14 void _rtld_bind_start(void);
15 void _rtld_relocate_nonplt_self(Elf_Dyn
*, Elf_Addr
);
16 caddr_t
_rtld_bind(const Obj_Entry
*, Elf_Word
);
19 _rtld_setup_pltgot(const Obj_Entry
*obj
)
21 obj
->pltgot
[1] = (Elf_Addr
) obj
;
22 obj
->pltgot
[2] = (Elf_Addr
) &_rtld_bind_start
;
26 _rtld_relocate_nonplt_self(Elf_Dyn
*dynp
, Elf_Addr relocbase
)
28 const Elf_Rel
*rel
= 0, *rellim
;
32 for (; dynp
->d_tag
!= DT_NULL
; dynp
++) {
33 switch (dynp
->d_tag
) {
35 rel
= (const Elf_Rel
*)(relocbase
+ dynp
->d_un
.d_ptr
);
38 relsz
= dynp
->d_un
.d_val
;
42 if (rel
== 0 || relsz
== 0)
44 rellim
= (const Elf_Rel
*)((const uint8_t *)rel
+ relsz
);
45 for (; rel
< rellim
; rel
++) {
46 where
= (Elf_Addr
*)(relocbase
+ rel
->r_offset
);
47 *where
+= (Elf_Addr
)relocbase
;
52 _rtld_relocate_nonplt_objects(const Obj_Entry
*obj
)
57 for (rel
= obj
->rel
; rel
< obj
->rellim
; rel
++) {
60 const Obj_Entry
*defobj
;
64 where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
65 symnum
= ELF_R_SYM(rel
->r_info
);
67 switch (ELF_R_TYPE(rel
->r_info
)) {
71 #if 1 /* XXX should not occur */
73 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
76 target
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
78 *where
+= target
- (Elf_Addr
)where
;
79 rdbg(("PC32 %s in %s --> %p in %s",
80 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
81 obj
->path
, (void *)*where
, defobj
->path
));
87 case R_TYPE(GLOB_DAT
):
88 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
91 target
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
93 tmp
= target
+ *where
;
96 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
97 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
98 obj
->path
, (void *)*where
, defobj
->path
));
101 case R_TYPE(RELATIVE
):
102 *where
+= (Elf_Addr
)obj
->relocbase
;
103 rdbg(("RELATIVE in %s --> %p", obj
->path
,
109 * These are deferred until all other relocations have
110 * been done. All we do here is make sure that the
111 * COPY relocation is not in a shared library. They
112 * are allowed only in executable files.
114 if (obj
->isdynamic
) {
116 "%s: Unexpected R_COPY relocation in shared library",
120 rdbg(("COPY (avoid in main)"));
124 rdbg(("sym = %lu, type = %lu, offset = %p, "
125 "contents = %p, symbol = %s",
126 symnum
, (u_long
)ELF_R_TYPE(rel
->r_info
),
127 (void *)rel
->r_offset
, (void *)*where
,
128 obj
->strtab
+ obj
->symtab
[symnum
].st_name
));
129 _rtld_error("%s: Unsupported relocation type %ld "
130 "in non-PLT relocations",
131 obj
->path
, (u_long
) ELF_R_TYPE(rel
->r_info
));
139 _rtld_relocate_plt_lazy(const Obj_Entry
*obj
)
146 for (rel
= obj
->pltrel
; rel
< obj
->pltrellim
; rel
++) {
147 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
149 assert(ELF_R_TYPE(rel
->r_info
) == R_TYPE(JMP_SLOT
));
151 /* Just relocate the GOT slots pointing into the PLT */
152 *where
+= (Elf_Addr
)obj
->relocbase
;
153 rdbg(("fixup !main in %s --> %p", obj
->path
, (void *)*where
));
160 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rel
*rel
,
163 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
166 const Obj_Entry
*defobj
;
168 assert(ELF_R_TYPE(rel
->r_info
) == R_TYPE(JMP_SLOT
));
170 def
= _rtld_find_symdef(ELF_R_SYM(rel
->r_info
), obj
, &defobj
, true);
173 target
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
174 rdbg(("bind now/fixup in %s --> old=%p new=%p",
175 defobj
->strtab
+ def
->st_name
, (void *)*where
,
177 if (*where
!= target
)
185 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
187 const Elf_Rel
*rel
= (const Elf_Rel
*)((const uint8_t *)obj
->pltrel
+ reloff
);
191 err
= _rtld_relocate_plt_object(obj
, rel
, &new_value
);
192 if (err
|| new_value
== 0)
195 return (caddr_t
)new_value
;
199 _rtld_relocate_plt_objects(const Obj_Entry
*obj
)
204 for (rel
= obj
->pltrel
; rel
< obj
->pltrellim
; rel
++) {
205 err
= _rtld_relocate_plt_object(obj
, rel
, NULL
);