1 /* $NetBSD: mdreloc.c,v 1.2 2014/08/25 20:40:52 joerg Exp $ */
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: mdreloc.c,v 1.2 2014/08/25 20:40:52 joerg Exp $");
37 #include <sys/types.h>
43 void _rtld_bind_start(void);
44 void _rtld_relocate_nonplt_self(Elf_Dyn
*, Elf_Addr
);
45 caddr_t
_rtld_bind(const Obj_Entry
*, Elf_Word
);
48 _rtld_setup_pltgot(const Obj_Entry
*obj
)
50 obj
->pltgot
[1] = (Elf_Addr
) obj
;
51 obj
->pltgot
[2] = (Elf_Addr
) &_rtld_bind_start
;
55 _rtld_relocate_nonplt_self(Elf_Dyn
*dynp
, Elf_Addr relocbase
)
57 const Elf_Rel
*rel
= 0, *rellim
;
61 for (; dynp
->d_tag
!= DT_NULL
; dynp
++) {
62 switch (dynp
->d_tag
) {
64 rel
= (const Elf_Rel
*)(relocbase
+ dynp
->d_un
.d_ptr
);
67 relsz
= dynp
->d_un
.d_val
;
71 rellim
= (const Elf_Rel
*)((const uint8_t *)rel
+ relsz
);
72 for (; rel
< rellim
; rel
++) {
73 where
= (Elf_Addr
*)(relocbase
+ rel
->r_offset
);
74 *where
+= (Elf_Addr
)relocbase
;
79 _rtld_relocate_nonplt_objects(Obj_Entry
*obj
)
82 for (const Elf_Rela
*rela
= obj
->rela
; rela
< obj
->relalim
; rela
++) {
85 const Obj_Entry
*defobj
;
89 where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
90 symnum
= ELF_R_SYM(rela
->r_info
);
91 addend
= rela
->r_addend
;
93 switch (ELF_R_TYPE(rela
->r_info
)) {
97 case R_TYPE(ABS64
): /* word B + S + A */
98 case R_TYPE(GLOB_DAT
): /* word B + S */
99 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
102 *where
= addend
+ (Elf_Addr
)defobj
->relocbase
+
104 rdbg(("ABS64/GLOB_DAT %s in %s --> %p @ %p in %s",
105 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
106 obj
->path
, (void *)tmp
, where
, defobj
->path
));
109 case R_TYPE(RELATIVE
): /* word B + A */
110 *where
= addend
+ (Elf_Addr
)obj
->relocbase
;
111 rdbg(("RELATIVE in %s --> %p", obj
->path
,
117 * These are deferred until all other relocations have
118 * been done. All we do here is make sure that the
119 * COPY relocation is not in a shared library. They
120 * are allowed only in executable files.
122 if (obj
->isdynamic
) {
124 "%s: Unexpected R_COPY relocation in shared library",
128 rdbg(("COPY (avoid in main)"));
131 case R_TLS_TYPE(TLS_DTPREL
):
132 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
136 *where
= addend
+ (Elf_Addr
)(def
->st_value
);
138 rdbg(("TLS_DTPOFF32 %s in %s --> %p",
139 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
140 obj
->path
, (void *)tmp
));
143 case R_TLS_TYPE(TLS_DTPMOD
):
144 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
148 *where
= (Elf_Addr
)(defobj
->tlsindex
);
150 rdbg(("TLS_DTPMOD %s in %s --> %p",
151 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
152 obj
->path
, (void *)tmp
));
156 case R_TLS_TYPE(TLS_TPREL
):
157 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
161 if (!defobj
->tls_done
&&
162 _rtld_tls_offset_allocate(obj
))
165 *where
= (Elf_Addr
)def
->st_value
+ defobj
->tlsoffset
+
166 sizeof(struct tls_tcb
);
167 rdbg(("TLS_TPOFF32 %s in %s --> %p",
168 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
169 obj
->path
, (void *)tmp
));
173 rdbg(("sym = %lu, type = %lu, offset = %p, "
174 "contents = %p, symbol = %s",
175 symnum
, (u_long
)ELF_R_TYPE(rela
->r_info
),
176 (void *)rela
->r_offset
, *where
,
177 obj
->strtab
+ obj
->symtab
[symnum
].st_name
));
178 _rtld_error("%s: Unsupported relocation type %ld "
179 "in non-PLT relocations",
180 obj
->path
, (u_long
) ELF_R_TYPE(rela
->r_info
));
188 _rtld_relocate_plt_lazy(const Obj_Entry
*obj
)
194 for (const Elf_Rel
*rel
= obj
->pltrel
; rel
< obj
->pltrellim
; rel
++) {
195 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
197 assert(ELF_R_TYPE(rel
->r_info
) == R_TYPE(JUMP_SLOT
));
199 /* Just relocate the GOT slots pointing into the PLT */
200 *where
+= (Elf_Addr
)obj
->relocbase
;
201 rdbg(("fixup !main in %s --> %p", obj
->path
, (void *)*where
));
208 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rel
*rel
,
211 Elf_Addr
*where
= (Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
);
214 const Obj_Entry
*defobj
;
215 unsigned long info
= rel
->r_info
;
217 assert(ELF_R_TYPE(info
) == R_TYPE(JUMP_SLOT
));
219 def
= _rtld_find_plt_symdef(ELF_R_SYM(info
), obj
, &defobj
, tp
!= NULL
);
220 if (__predict_false(def
== NULL
))
222 if (__predict_false(def
== &_rtld_sym_zero
))
225 if (ELF_ST_TYPE(def
->st_info
) == STT_GNU_IFUNC
) {
228 new_value
= _rtld_resolve_ifunc(defobj
, def
);
230 new_value
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
232 rdbg(("bind now/fixup in %s --> old=%p new=%p",
233 defobj
->strtab
+ def
->st_name
, (void *)*where
, (void *)new_value
));
234 if (*where
!= new_value
)
243 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
245 const Elf_Rel
*rel
= obj
->pltrel
+ reloff
;
246 Elf_Addr new_value
= 0; /* XXX gcc */
248 _rtld_shared_enter();
249 int err
= _rtld_relocate_plt_object(obj
, rel
, &new_value
);
254 return (caddr_t
)new_value
;
257 _rtld_relocate_plt_objects(const Obj_Entry
*obj
)
262 for (rel
= obj
->pltrel
; rel
< obj
->pltrellim
; rel
++) {
263 err
= _rtld_relocate_plt_object(obj
, rel
, NULL
);