mdreloc.c revision 1.3 1 /* $NetBSD: mdreloc.c,v 1.3 2017/06/19 11:57:01 joerg 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.3 2017/06/19 11:57:01 joerg 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 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
46
47 void
48 _rtld_setup_pltgot(const Obj_Entry *obj)
49 {
50 obj->pltgot[1] = (Elf_Addr) obj;
51 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
52 }
53
54 void
55 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
56 {
57 const Elf_Rel *rel = 0, *rellim;
58 Elf_Addr relsz = 0;
59 Elf_Addr *where;
60
61 for (; dynp->d_tag != DT_NULL; dynp++) {
62 switch (dynp->d_tag) {
63 case DT_REL:
64 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
65 break;
66 case DT_RELSZ:
67 relsz = dynp->d_un.d_val;
68 break;
69 }
70 }
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;
75 }
76 }
77
78 int
79 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
80 {
81 const Elf_Sym *def = NULL;
82 const Obj_Entry *defobj = NULL;
83 unsigned long last_symnum = ULONG_MAX;
84
85 for (const Elf_Rela *rela = obj->rela; rela < obj->relalim; rela++) {
86 Elf_Addr *where;
87 unsigned long symnum;
88 Elf_Addr addend;
89
90 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
91 addend = rela->r_addend;
92
93 switch (ELF_R_TYPE(rela->r_info)) {
94 case R_TYPE(ABS64): /* word B + S + A */
95 case R_TYPE(GLOB_DAT): /* word B + S */
96 case R_TLS_TYPE(TLS_DTPREL):
97 case R_TLS_TYPE(TLS_DTPMOD):
98 case R_TLS_TYPE(TLS_TPREL):
99 symnum = ELF_R_SYM(rela->r_info);
100 if (last_symnum != symnum) {
101 last_symnum = symnum;
102 def = _rtld_find_symdef(symnum, obj, &defobj,
103 false);
104 if (def == NULL)
105 return -1;
106 }
107
108 default:
109 break;
110 }
111
112 switch (ELF_R_TYPE(rela->r_info)) {
113 case R_TYPE(NONE):
114 break;
115
116 case R_TYPE(ABS64): /* word B + S + A */
117 case R_TYPE(GLOB_DAT): /* word B + S */
118 *where = addend + (Elf_Addr)defobj->relocbase +
119 def->st_value;
120 rdbg(("ABS64/GLOB_DAT %s in %s --> %p @ %p in %s",
121 obj->strtab + obj->symtab[symnum].st_name,
122 obj->path, (void *)tmp, where, defobj->path));
123 break;
124
125 case R_TYPE(RELATIVE): /* word B + A */
126 *where = addend + (Elf_Addr)obj->relocbase;
127 rdbg(("RELATIVE in %s --> %p", obj->path,
128 (void *)tmp));
129 break;
130
131 case R_TYPE(COPY):
132 /*
133 * These are deferred until all other relocations have
134 * been done. All we do here is make sure that the
135 * COPY relocation is not in a shared library. They
136 * are allowed only in executable files.
137 */
138 if (obj->isdynamic) {
139 _rtld_error(
140 "%s: Unexpected R_COPY relocation in shared library",
141 obj->path);
142 return -1;
143 }
144 rdbg(("COPY (avoid in main)"));
145 break;
146
147 case R_TLS_TYPE(TLS_DTPREL):
148 *where = addend + (Elf_Addr)(def->st_value);
149
150 rdbg(("TLS_DTPOFF32 %s in %s --> %p",
151 obj->strtab + obj->symtab[symnum].st_name,
152 obj->path, (void *)tmp));
153
154 break;
155 case R_TLS_TYPE(TLS_DTPMOD):
156 *where = (Elf_Addr)(defobj->tlsindex);
157
158 rdbg(("TLS_DTPMOD %s in %s --> %p",
159 obj->strtab + obj->symtab[symnum].st_name,
160 obj->path, (void *)tmp));
161
162 break;
163
164 case R_TLS_TYPE(TLS_TPREL):
165 if (!defobj->tls_done &&
166 _rtld_tls_offset_allocate(obj))
167 return -1;
168
169 *where = (Elf_Addr)def->st_value + defobj->tlsoffset +
170 sizeof(struct tls_tcb);
171 rdbg(("TLS_TPOFF32 %s in %s --> %p",
172 obj->strtab + obj->symtab[symnum].st_name,
173 obj->path, (void *)tmp));
174 break;
175
176 default:
177 rdbg(("sym = %lu, type = %lu, offset = %p, "
178 "contents = %p, symbol = %s",
179 (u_long)ELF_R_SYM(rela->r_info),
180 (u_long)ELF_R_TYPE(rela->r_info),
181 (void *)rela->r_offset, *where,
182 obj->strtab + obj->symtab[symnum].st_name));
183 _rtld_error("%s: Unsupported relocation type %ld "
184 "in non-PLT relocations",
185 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
186 return -1;
187 }
188 }
189 return 0;
190 }
191
192 int
193 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
194 {
195
196 if (!obj->relocbase)
197 return 0;
198
199 for (const Elf_Rel *rel = obj->pltrel; rel < obj->pltrellim; rel++) {
200 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
201
202 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
203
204 /* Just relocate the GOT slots pointing into the PLT */
205 *where += (Elf_Addr)obj->relocbase;
206 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
207 }
208
209 return 0;
210 }
211
212 static int
213 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
214 Elf_Addr *tp)
215 {
216 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
217 Elf_Addr new_value;
218 const Elf_Sym *def;
219 const Obj_Entry *defobj;
220 unsigned long info = rel->r_info;
221
222 assert(ELF_R_TYPE(info) == R_TYPE(JUMP_SLOT));
223
224 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
225 if (__predict_false(def == NULL))
226 return -1;
227 if (__predict_false(def == &_rtld_sym_zero))
228 return 0;
229
230 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
231 if (tp == NULL)
232 return 0;
233 new_value = _rtld_resolve_ifunc(defobj, def);
234 } else {
235 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
236 }
237 rdbg(("bind now/fixup in %s --> old=%p new=%p",
238 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
239 if (*where != new_value)
240 *where = new_value;
241 if (tp)
242 *tp = new_value;
243
244 return 0;
245 }
246
247 caddr_t
248 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
249 {
250 const Elf_Rel *rel = obj->pltrel + reloff;
251 Elf_Addr new_value = 0; /* XXX gcc */
252
253 _rtld_shared_enter();
254 int err = _rtld_relocate_plt_object(obj, rel, &new_value);
255 if (err)
256 _rtld_die();
257 _rtld_shared_exit();
258
259 return (caddr_t)new_value;
260 }
261 int
262 _rtld_relocate_plt_objects(const Obj_Entry *obj)
263 {
264 const Elf_Rel *rel;
265 int err = 0;
266
267 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
268 err = _rtld_relocate_plt_object(obj, rel, NULL);
269 if (err)
270 break;
271 }
272
273 return err;
274 }
275