1 /* $NetBSD: mdreloc.c,v 1.2 2015/03/27 23:14:53 matt 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 2015/03/27 23:14:53 matt Exp $");
37 #include <sys/types.h>
38 #include <sys/endian.h>
47 void _rtld_bind_start(void);
48 void _rtld_relocate_nonplt_self(Elf_Dyn
*, Elf_Addr
);
49 void *_rtld_bind(const Obj_Entry
*, Elf_Word
);
52 _rtld_setup_pltgot(const Obj_Entry
*obj
)
54 obj
->pltgot
[0] = (Elf_Addr
) &_rtld_bind_start
;
55 obj
->pltgot
[1] = (Elf_Addr
) obj
;
59 _rtld_relocate_nonplt_self(Elf_Dyn
*dynp
, Elf_Addr relocbase
)
61 const Elf_Rela
*rela
= NULL
, *relalim
;
64 for (; dynp
->d_tag
!= DT_NULL
; dynp
++) {
65 switch (dynp
->d_tag
) {
67 rela
= (const Elf_Rela
*)(relocbase
+ dynp
->d_un
.d_ptr
);
70 relasz
= dynp
->d_un
.d_val
;
75 relalim
= (const Elf_Rela
*)((uintptr_t)rela
+ relasz
);
76 for (; rela
< relalim
; rela
++) {
77 Elf_Word r_type
= ELF_R_TYPE(rela
->r_info
);
78 Elf_Addr
*where
= (Elf_Addr
*)(relocbase
+ rela
->r_offset
);
81 case R_TYPE(RELATIVE
): {
82 Elf_Addr val
= relocbase
+ rela
->r_addend
;
84 rdbg(("RELATIVE/L(%p) -> %p in <self>",
99 _rtld_relocate_nonplt_objects(Obj_Entry
*obj
)
101 const Elf_Rela
*rela
;
103 const Obj_Entry
*defobj
;
105 for (rela
= obj
->rela
; rela
< obj
->relalim
; rela
++) {
106 Elf_Addr
* const where
=
107 (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
108 const Elf_Word r_symndx
= ELF_R_SYM(rela
->r_info
);
109 const Elf_Word r_type
= ELF_R_TYPE(rela
->r_info
);
115 case R_TYPE(RELATIVE
): {
116 def
= obj
->symtab
+ r_symndx
;
118 Elf_Addr val
= (Elf_Addr
)obj
->relocbase
+ rela
->r_addend
;
120 rdbg(("RELATIVE(%p) -> %p (%s) in %s",
122 obj
->strtab
+ def
->st_name
, obj
->path
));
128 case R_TYPESZ(ADDR
): {
129 def
= _rtld_find_symdef(r_symndx
, obj
, &defobj
, false);
133 Elf_Addr val
= (Elf_Addr
)defobj
->relocbase
+ rela
->r_addend
;
136 rdbg(("ADDR %s in %s --> %p in %s",
137 obj
->strtab
+ obj
->symtab
[r_symndx
].st_name
,
138 obj
->path
, (void *)val
, defobj
->path
));
142 case R_TYPESZ(TLS_DTPMOD
): {
143 def
= _rtld_find_symdef(r_symndx
, obj
, &defobj
, false);
147 Elf_Addr val
= (Elf_Addr
)defobj
->tlsindex
+ rela
->r_addend
;
150 rdbg(("DTPMOD %s in %s --> %p in %s",
151 obj
->strtab
+ obj
->symtab
[r_symndx
].st_name
,
152 obj
->path
, (void *)val
, defobj
->path
));
156 case R_TYPESZ(TLS_DTPREL
): {
157 Elf_Addr old
= *where
;
160 def
= _rtld_find_symdef(r_symndx
, obj
, &defobj
, false);
164 if (!defobj
->tls_done
&& _rtld_tls_offset_allocate(obj
))
167 val
= (Elf_Addr
)def
->st_value
- TLS_DTV_OFFSET
;
170 rdbg(("DTPREL %s in %s --> %p in %s",
171 obj
->strtab
+ obj
->symtab
[r_symndx
].st_name
,
172 obj
->path
, (void *)val
, defobj
->path
));
177 rdbg(("sym = %lu, type = %lu, offset = %p, "
178 "addend = %p, contents = %p, symbol = %s",
179 (u_long
)r_symndx
, (u_long
)r_type
,
180 (void *)rela
->r_offset
, (void *)rela
->r_addend
,
181 (void *)load_ptr(where
, sizeof(Elf_Addr
)),
182 obj
->strtab
+ obj
->symtab
[r_symndx
].st_name
));
183 _rtld_error("%s: Unsupported relocation type %ld "
184 "in non-PLT relocations",
185 obj
->path
, (u_long
)r_type
);
194 _rtld_relocate_plt_lazy(const Obj_Entry
*obj
)
196 /* PLT fixups were done above in the GOT relocation. */
201 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rel
*rel
,
204 const Obj_Entry
*defobj
;
207 assert(ELF_R_TYPE(rel
->r_info
) == R_TYPE(JMP_SLOT
));
209 const Elf_Sym
*def
= _rtld_find_plt_symdef(ELF_R_SYM(rel
->r_info
),
210 obj
, &defobj
, tp
!= NULL
);
211 if (__predict_false(def
== NULL
))
213 if (__predict_false(def
== &_rtld_sym_zero
))
216 if (ELF_ST_TYPE(def
->st_info
) == STT_GNU_IFUNC
) {
219 new_value
= _rtld_resolve_ifunc(defobj
, def
);
221 new_value
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
223 rdbg(("bind now/fixup in %s --> new=%p",
224 defobj
->strtab
+ def
->st_name
, (void *)new_value
));
225 *(Elf_Addr
*)(obj
->relocbase
+ rel
->r_offset
) = new_value
;
233 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
235 const Elf_Rel
*pltrel
= (const Elf_Rel
*)(obj
->pltrel
+ reloff
);
239 _rtld_shared_enter();
240 err
= _rtld_relocate_plt_object(obj
, pltrel
, &new_value
);
245 return (caddr_t
)new_value
;
249 _rtld_relocate_plt_objects(const Obj_Entry
*obj
)
252 for (const Elf_Rel
*rel
= obj
->pltrel
; rel
< obj
->pltrellim
; rel
++) {
253 if (_rtld_relocate_plt_object(obj
, rel
, NULL
) < 0)