mdreloc.c revision 1.2 1 1.2 joerg /* $NetBSD: mdreloc.c,v 1.2 2014/08/25 20:40:52 joerg Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by Matt Thomas of 3am Software Foundry.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #include <sys/cdefs.h>
33 1.1 matt #ifndef lint
34 1.2 joerg __RCSID("$NetBSD: mdreloc.c,v 1.2 2014/08/25 20:40:52 joerg Exp $");
35 1.1 matt #endif /* not lint */
36 1.1 matt
37 1.1 matt #include <sys/types.h>
38 1.1 matt #include <string.h>
39 1.1 matt
40 1.1 matt #include "debug.h"
41 1.1 matt #include "rtld.h"
42 1.1 matt
43 1.1 matt void _rtld_bind_start(void);
44 1.1 matt void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
45 1.1 matt caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
46 1.1 matt
47 1.1 matt void
48 1.1 matt _rtld_setup_pltgot(const Obj_Entry *obj)
49 1.1 matt {
50 1.1 matt obj->pltgot[1] = (Elf_Addr) obj;
51 1.1 matt obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
52 1.1 matt }
53 1.1 matt
54 1.1 matt void
55 1.1 matt _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
56 1.1 matt {
57 1.1 matt const Elf_Rel *rel = 0, *rellim;
58 1.1 matt Elf_Addr relsz = 0;
59 1.1 matt Elf_Addr *where;
60 1.1 matt
61 1.1 matt for (; dynp->d_tag != DT_NULL; dynp++) {
62 1.1 matt switch (dynp->d_tag) {
63 1.1 matt case DT_REL:
64 1.1 matt rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
65 1.1 matt break;
66 1.1 matt case DT_RELSZ:
67 1.1 matt relsz = dynp->d_un.d_val;
68 1.1 matt break;
69 1.1 matt }
70 1.1 matt }
71 1.1 matt rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz);
72 1.1 matt for (; rel < rellim; rel++) {
73 1.1 matt where = (Elf_Addr *)(relocbase + rel->r_offset);
74 1.1 matt *where += (Elf_Addr)relocbase;
75 1.1 matt }
76 1.1 matt }
77 1.1 matt
78 1.1 matt int
79 1.1 matt _rtld_relocate_nonplt_objects(Obj_Entry *obj)
80 1.1 matt {
81 1.1 matt
82 1.1 matt for (const Elf_Rela *rela = obj->rela; rela < obj->relalim; rela++) {
83 1.1 matt Elf_Addr *where;
84 1.1 matt const Elf_Sym *def;
85 1.1 matt const Obj_Entry *defobj;
86 1.1 matt unsigned long symnum;
87 1.1 matt Elf_Addr addend;
88 1.1 matt
89 1.1 matt where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
90 1.1 matt symnum = ELF_R_SYM(rela->r_info);
91 1.1 matt addend = rela->r_addend;
92 1.1 matt
93 1.1 matt switch (ELF_R_TYPE(rela->r_info)) {
94 1.1 matt case R_TYPE(NONE):
95 1.1 matt break;
96 1.1 matt
97 1.1 matt case R_TYPE(ABS64): /* word B + S + A */
98 1.1 matt case R_TYPE(GLOB_DAT): /* word B + S */
99 1.1 matt def = _rtld_find_symdef(symnum, obj, &defobj, false);
100 1.1 matt if (def == NULL)
101 1.1 matt return -1;
102 1.1 matt *where = addend + (Elf_Addr)defobj->relocbase +
103 1.1 matt def->st_value;
104 1.1 matt rdbg(("ABS64/GLOB_DAT %s in %s --> %p @ %p in %s",
105 1.1 matt obj->strtab + obj->symtab[symnum].st_name,
106 1.1 matt obj->path, (void *)tmp, where, defobj->path));
107 1.1 matt break;
108 1.1 matt
109 1.1 matt case R_TYPE(RELATIVE): /* word B + A */
110 1.1 matt *where = addend + (Elf_Addr)obj->relocbase;
111 1.1 matt rdbg(("RELATIVE in %s --> %p", obj->path,
112 1.1 matt (void *)tmp));
113 1.1 matt break;
114 1.1 matt
115 1.1 matt case R_TYPE(COPY):
116 1.1 matt /*
117 1.1 matt * These are deferred until all other relocations have
118 1.1 matt * been done. All we do here is make sure that the
119 1.1 matt * COPY relocation is not in a shared library. They
120 1.1 matt * are allowed only in executable files.
121 1.1 matt */
122 1.1 matt if (obj->isdynamic) {
123 1.1 matt _rtld_error(
124 1.1 matt "%s: Unexpected R_COPY relocation in shared library",
125 1.1 matt obj->path);
126 1.1 matt return -1;
127 1.1 matt }
128 1.1 matt rdbg(("COPY (avoid in main)"));
129 1.1 matt break;
130 1.1 matt
131 1.1 matt case R_TLS_TYPE(TLS_DTPREL):
132 1.1 matt def = _rtld_find_symdef(symnum, obj, &defobj, false);
133 1.1 matt if (def == NULL)
134 1.1 matt return -1;
135 1.1 matt
136 1.1 matt *where = addend + (Elf_Addr)(def->st_value);
137 1.1 matt
138 1.1 matt rdbg(("TLS_DTPOFF32 %s in %s --> %p",
139 1.1 matt obj->strtab + obj->symtab[symnum].st_name,
140 1.1 matt obj->path, (void *)tmp));
141 1.1 matt
142 1.1 matt break;
143 1.1 matt case R_TLS_TYPE(TLS_DTPMOD):
144 1.1 matt def = _rtld_find_symdef(symnum, obj, &defobj, false);
145 1.1 matt if (def == NULL)
146 1.1 matt return -1;
147 1.1 matt
148 1.1 matt *where = (Elf_Addr)(defobj->tlsindex);
149 1.1 matt
150 1.1 matt rdbg(("TLS_DTPMOD %s in %s --> %p",
151 1.1 matt obj->strtab + obj->symtab[symnum].st_name,
152 1.1 matt obj->path, (void *)tmp));
153 1.1 matt
154 1.1 matt break;
155 1.1 matt
156 1.1 matt case R_TLS_TYPE(TLS_TPREL):
157 1.1 matt def = _rtld_find_symdef(symnum, obj, &defobj, false);
158 1.1 matt if (def == NULL)
159 1.1 matt return -1;
160 1.1 matt
161 1.1 matt if (!defobj->tls_done &&
162 1.1 matt _rtld_tls_offset_allocate(obj))
163 1.1 matt return -1;
164 1.1 matt
165 1.1 matt *where = (Elf_Addr)def->st_value + defobj->tlsoffset +
166 1.1 matt sizeof(struct tls_tcb);
167 1.1 matt rdbg(("TLS_TPOFF32 %s in %s --> %p",
168 1.1 matt obj->strtab + obj->symtab[symnum].st_name,
169 1.1 matt obj->path, (void *)tmp));
170 1.1 matt break;
171 1.1 matt
172 1.1 matt default:
173 1.1 matt rdbg(("sym = %lu, type = %lu, offset = %p, "
174 1.1 matt "contents = %p, symbol = %s",
175 1.1 matt symnum, (u_long)ELF_R_TYPE(rela->r_info),
176 1.1 matt (void *)rela->r_offset, *where,
177 1.1 matt obj->strtab + obj->symtab[symnum].st_name));
178 1.1 matt _rtld_error("%s: Unsupported relocation type %ld "
179 1.1 matt "in non-PLT relocations",
180 1.1 matt obj->path, (u_long) ELF_R_TYPE(rela->r_info));
181 1.1 matt return -1;
182 1.1 matt }
183 1.1 matt }
184 1.1 matt return 0;
185 1.1 matt }
186 1.1 matt
187 1.1 matt int
188 1.1 matt _rtld_relocate_plt_lazy(const Obj_Entry *obj)
189 1.1 matt {
190 1.1 matt
191 1.1 matt if (!obj->relocbase)
192 1.1 matt return 0;
193 1.1 matt
194 1.1 matt for (const Elf_Rel *rel = obj->pltrel; rel < obj->pltrellim; rel++) {
195 1.1 matt Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
196 1.1 matt
197 1.1 matt assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
198 1.1 matt
199 1.1 matt /* Just relocate the GOT slots pointing into the PLT */
200 1.1 matt *where += (Elf_Addr)obj->relocbase;
201 1.1 matt rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
202 1.1 matt }
203 1.1 matt
204 1.1 matt return 0;
205 1.1 matt }
206 1.1 matt
207 1.1 matt static int
208 1.1 matt _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
209 1.1 matt Elf_Addr *tp)
210 1.1 matt {
211 1.1 matt Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
212 1.1 matt Elf_Addr new_value;
213 1.1 matt const Elf_Sym *def;
214 1.1 matt const Obj_Entry *defobj;
215 1.1 matt unsigned long info = rel->r_info;
216 1.1 matt
217 1.1 matt assert(ELF_R_TYPE(info) == R_TYPE(JUMP_SLOT));
218 1.1 matt
219 1.1 matt def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
220 1.1 matt if (__predict_false(def == NULL))
221 1.1 matt return -1;
222 1.1 matt if (__predict_false(def == &_rtld_sym_zero))
223 1.1 matt return 0;
224 1.1 matt
225 1.2 joerg if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
226 1.2 joerg if (tp == NULL)
227 1.2 joerg return 0;
228 1.2 joerg new_value = _rtld_resolve_ifunc(defobj, def);
229 1.2 joerg } else {
230 1.2 joerg new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
231 1.2 joerg }
232 1.1 matt rdbg(("bind now/fixup in %s --> old=%p new=%p",
233 1.1 matt defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
234 1.1 matt if (*where != new_value)
235 1.1 matt *where = new_value;
236 1.1 matt if (tp)
237 1.1 matt *tp = new_value;
238 1.1 matt
239 1.1 matt return 0;
240 1.1 matt }
241 1.1 matt
242 1.1 matt caddr_t
243 1.1 matt _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
244 1.1 matt {
245 1.1 matt const Elf_Rel *rel = obj->pltrel + reloff;
246 1.1 matt Elf_Addr new_value = 0; /* XXX gcc */
247 1.1 matt
248 1.1 matt _rtld_shared_enter();
249 1.1 matt int err = _rtld_relocate_plt_object(obj, rel, &new_value);
250 1.1 matt if (err)
251 1.1 matt _rtld_die();
252 1.1 matt _rtld_shared_exit();
253 1.1 matt
254 1.1 matt return (caddr_t)new_value;
255 1.1 matt }
256 1.1 matt int
257 1.1 matt _rtld_relocate_plt_objects(const Obj_Entry *obj)
258 1.1 matt {
259 1.1 matt const Elf_Rel *rel;
260 1.1 matt int err = 0;
261 1.1 matt
262 1.1 matt for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
263 1.1 matt err = _rtld_relocate_plt_object(obj, rel, NULL);
264 1.1 matt if (err)
265 1.1 matt break;
266 1.1 matt }
267 1.1 matt
268 1.1 matt return err;
269 1.1 matt }
270