mdreloc.c revision 1.8 1 /* $NetBSD: mdreloc.c,v 1.8 2018/07/16 00:29:37 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 *
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.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: mdreloc.c,v 1.8 2018/07/16 00:29:37 christos Exp $");
35 #endif /* not lint */
36
37 #include <sys/types.h>
38 #include <string.h>
39
40 #include "debug.h"
41 #include "rtld.h"
42
43 void _rtld_bind_start(void);
44 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
45 Elf_Addr _rtld_bind(const Obj_Entry *, Elf_Word);
46 void *_rtld_tlsdesc(void *);
47
48 /*
49 * AARCH64 PLT looks like this;
50 *
51 * PLT HEADER <8 instructions>
52 * PLT ENTRY #0 <4 instructions>
53 * PLT ENTRY #1 <4 instructions>
54 * .
55 * .
56 * PLT ENTRY #n <4 instructions>
57 *
58 * PLT HEADER
59 * stp x16, x30, [sp, #-16]!
60 * adrp x16, (GOT+16)
61 * ldr x17, [x16, #PLT_GOT+0x10]
62 * add x16, x16, #PLT_GOT+0x10
63 * br x17
64 * nop
65 * nop
66 * nop
67 *
68 * PLT ENTRY #n
69 * adrp x16, PLTGOT + n * 8
70 * ldr x17, [x16, PLTGOT + n * 8]
71 * add x16, x16, :lo12:PLTGOT + n * 8
72 * br x17
73 */
74 void
75 _rtld_setup_pltgot(const Obj_Entry *obj)
76 {
77
78 obj->pltgot[1] = (Elf_Addr) obj;
79 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
80 }
81
82 void
83 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
84 {
85 const Elf_Rela *rela = 0, *relalim;
86 Elf_Addr relasz = 0;
87 Elf_Addr *where;
88
89 for (; dynp->d_tag != DT_NULL; dynp++) {
90 switch (dynp->d_tag) {
91 case DT_RELA:
92 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
93 break;
94 case DT_RELASZ:
95 relasz = dynp->d_un.d_val;
96 break;
97 }
98 }
99 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
100 for (; rela < relalim; rela++) {
101 where = (Elf_Addr *)(relocbase + rela->r_offset);
102 *where += (Elf_Addr)relocbase;
103 }
104 }
105
106 int
107 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
108 {
109 const Elf_Sym *def = NULL;
110 const Obj_Entry *defobj = NULL;
111 unsigned long last_symnum = ULONG_MAX;
112
113 for (const Elf_Rela *rela = obj->rela; rela < obj->relalim; rela++) {
114 Elf_Addr *where;
115 Elf_Addr tmp;
116 unsigned long symnum;
117
118 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
119
120 switch (ELF_R_TYPE(rela->r_info)) {
121 case R_TYPE(ABS64): /* word S + A */
122 case R_TYPE(GLOB_DAT): /* word S + A */
123 case R_TLS_TYPE(TLS_DTPREL):
124 case R_TLS_TYPE(TLS_DTPMOD):
125 case R_TLS_TYPE(TLS_TPREL):
126 symnum = ELF_R_SYM(rela->r_info);
127 if (last_symnum != symnum) {
128 last_symnum = symnum;
129 def = _rtld_find_symdef(symnum, obj, &defobj,
130 false);
131 if (def == NULL)
132 return -1;
133 }
134
135 default:
136 break;
137 }
138
139 switch (ELF_R_TYPE(rela->r_info)) {
140 case R_TYPE(NONE):
141 break;
142
143 case R_TYPE(ABS64): /* word S + A */
144 case R_TYPE(GLOB_DAT): /* word S + A */
145 tmp = (Elf_Addr)defobj->relocbase + def->st_value +
146 rela->r_addend;
147 if (*where != tmp)
148 *where = tmp;
149 rdbg(("ABS64/GLOB_DAT %s in %s --> %p @ %p in %s",
150 obj->strtab + obj->symtab[symnum].st_name,
151 obj->path, (void *)tmp, where, defobj->path));
152 break;
153
154 case R_TYPE(RELATIVE): /* word B + A */
155 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
156 rdbg(("RELATIVE in %s --> %p", obj->path,
157 (void *)*where));
158 break;
159
160 case R_TYPE(COPY):
161 /*
162 * These are deferred until all other relocations have
163 * been done. All we do here is make sure that the
164 * COPY relocation is not in a shared library. They
165 * are allowed only in executable files.
166 */
167 if (obj->isdynamic) {
168 _rtld_error(
169 "%s: Unexpected R_COPY relocation in shared library",
170 obj->path);
171 return -1;
172 }
173 rdbg(("COPY (avoid in main)"));
174 break;
175
176 case R_TLS_TYPE(TLS_DTPREL):
177 *where = (Elf_Addr)(def->st_value + rela->r_addend);
178
179 rdbg(("TLS_DTPREL %s in %s --> %p",
180 obj->strtab + obj->symtab[symnum].st_name,
181 obj->path, (void *)*where));
182
183 break;
184 case R_TLS_TYPE(TLS_DTPMOD):
185 *where = (Elf_Addr)(defobj->tlsindex);
186
187 rdbg(("TLS_DTPMOD %s in %s --> %p",
188 obj->strtab + obj->symtab[symnum].st_name,
189 obj->path, (void *)*where));
190
191 break;
192
193 case R_TLS_TYPE(TLS_TPREL):
194 if (!defobj->tls_done &&
195 _rtld_tls_offset_allocate(obj))
196 return -1;
197
198 *where = (Elf_Addr)def->st_value + defobj->tlsoffset +
199 sizeof(struct tls_tcb);
200 rdbg(("TLS_TPREL %s in %s --> %p in %s",
201 obj->strtab + obj->symtab[symnum].st_name,
202 obj->path, (void *)*where, defobj->path));
203 break;
204
205 default:
206 rdbg(("sym = %lu, type = %lu, offset = %p, "
207 "addend = %p, contents = %p, symbol = %s",
208 (u_long)ELF_R_SYM(rela->r_info),
209 (u_long)ELF_R_TYPE(rela->r_info),
210 (void *)rela->r_offset, (void *)rela->r_addend,
211 (void *)*where,
212 obj->strtab + obj->symtab[symnum].st_name));
213 _rtld_error("%s: Unsupported relocation type %ld "
214 "in non-PLT relocations",
215 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
216 return -1;
217 }
218 }
219 return 0;
220 }
221
222 int
223 _rtld_relocate_plt_lazy(Obj_Entry *obj)
224 {
225
226 if (!obj->relocbase)
227 return 0;
228
229 for (const Elf_Rela *rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
230 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
231
232 assert((ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT)) ||
233 (ELF_R_TYPE(rela->r_info) == R_TYPE(TLSDESC)));
234
235 switch (ELF_R_TYPE(rela->r_info)) {
236 case R_TYPE(JUMP_SLOT):
237 /* Just relocate the GOT slots pointing into the PLT */
238 *where += (Elf_Addr)obj->relocbase;
239 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
240 break;
241 case R_TYPE(TLSDESC):
242 assert(ELF_R_SYM(rela->r_info) == 0); /* XXX */
243 if (ELF_R_SYM(rela->r_info) == 0) {
244 where[0] = (Elf_Addr)_rtld_tlsdesc;
245 where[1] = obj->tlsoffset + rela->r_addend + sizeof(struct tls_tcb);
246 }
247 break;
248 }
249 }
250
251 return 0;
252 }
253
254 void
255 _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
256 {
257 const Elf_Rela *rela;
258 Elf_Addr *where, target;
259
260 while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
261 rela = obj->pltrelalim - obj->ifunc_remaining;
262 --obj->ifunc_remaining;
263 if (ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE)) {
264 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
265 target = (Elf_Addr)(obj->relocbase + rela->r_addend);
266 _rtld_exclusive_exit(mask);
267 target = _rtld_resolve_ifunc2(obj, target);
268 _rtld_exclusive_enter(mask);
269 if (*where != target)
270 *where = target;
271 }
272 }
273 }
274
275 static int
276 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
277 Elf_Addr *tp)
278 {
279 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
280 Elf_Addr new_value;
281 const Elf_Sym *def;
282 const Obj_Entry *defobj;
283 unsigned long info = rela->r_info;
284
285 assert(ELF_R_TYPE(info) == R_TYPE(JUMP_SLOT));
286
287 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
288 if (__predict_false(def == NULL))
289 return -1;
290 if (__predict_false(def == &_rtld_sym_zero))
291 return 0;
292
293 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
294 if (tp == NULL)
295 return 0;
296 new_value = _rtld_resolve_ifunc(defobj, def);
297 } else {
298 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
299 }
300 rdbg(("bind now/fixup in %s --> old=%p new=%p",
301 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
302 if (*where != new_value)
303 *where = new_value;
304 if (tp)
305 *tp = new_value;
306
307 return 0;
308 }
309
310 Elf_Addr
311 _rtld_bind(const Obj_Entry *obj, Elf_Word relaidx)
312 {
313 const Elf_Rela *rela = obj->pltrela + relaidx;
314 Elf_Addr new_value = 0;
315
316 _rtld_shared_enter();
317 int err = _rtld_relocate_plt_object(obj, rela, &new_value);
318 if (err)
319 _rtld_die();
320 _rtld_shared_exit();
321
322 return new_value;
323 }
324 int
325 _rtld_relocate_plt_objects(const Obj_Entry *obj)
326 {
327 const Elf_Rela *rela;
328 int err = 0;
329
330 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
331 err = _rtld_relocate_plt_object(obj, rela, NULL);
332 if (err)
333 break;
334 }
335
336 return err;
337 }
338