mdreloc.c revision 1.38 1 1.38 joerg /* $NetBSD: mdreloc.c,v 1.38 2017/06/19 11:57:01 joerg Exp $ */
2 1.21 skrll
3 1.21 skrll #include <sys/cdefs.h>
4 1.21 skrll #ifndef lint
5 1.38 joerg __RCSID("$NetBSD: mdreloc.c,v 1.38 2017/06/19 11:57:01 joerg Exp $");
6 1.21 skrll #endif /* not lint */
7 1.11 junyoung
8 1.1 mycroft #include <sys/types.h>
9 1.33 joerg #include <sys/ucontext.h>
10 1.1 mycroft
11 1.1 mycroft #include "debug.h"
12 1.1 mycroft #include "rtld.h"
13 1.1 mycroft
14 1.13 mycroft void _rtld_bind_start(void);
15 1.12 mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
16 1.19 skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
17 1.12 mycroft
18 1.1 mycroft void
19 1.1 mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
20 1.1 mycroft {
21 1.1 mycroft obj->pltgot[1] = (Elf_Addr) obj;
22 1.1 mycroft obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
23 1.1 mycroft }
24 1.2 mycroft
25 1.12 mycroft void
26 1.19 skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
27 1.12 mycroft {
28 1.12 mycroft const Elf_Rel *rel = 0, *rellim;
29 1.12 mycroft Elf_Addr relsz = 0;
30 1.12 mycroft Elf_Addr *where;
31 1.12 mycroft
32 1.12 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
33 1.12 mycroft switch (dynp->d_tag) {
34 1.12 mycroft case DT_REL:
35 1.12 mycroft rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
36 1.12 mycroft break;
37 1.12 mycroft case DT_RELSZ:
38 1.12 mycroft relsz = dynp->d_un.d_val;
39 1.12 mycroft break;
40 1.12 mycroft }
41 1.12 mycroft }
42 1.22 christos if (rel == 0 || relsz == 0)
43 1.22 christos return;
44 1.27 lukem rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz);
45 1.12 mycroft for (; rel < rellim; rel++) {
46 1.12 mycroft where = (Elf_Addr *)(relocbase + rel->r_offset);
47 1.12 mycroft *where += (Elf_Addr)relocbase;
48 1.12 mycroft }
49 1.12 mycroft }
50 1.12 mycroft
51 1.2 mycroft int
52 1.32 joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
53 1.2 mycroft {
54 1.3 mycroft const Elf_Rel *rel;
55 1.20 lukem Elf_Addr target = 0;
56 1.38 joerg const Elf_Sym *def = NULL;
57 1.38 joerg const Obj_Entry *defobj = NULL;
58 1.38 joerg unsigned long last_symnum = ULONG_MAX;
59 1.12 mycroft
60 1.3 mycroft for (rel = obj->rel; rel < obj->rellim; rel++) {
61 1.3 mycroft Elf_Addr *where;
62 1.3 mycroft Elf_Addr tmp;
63 1.4 mycroft unsigned long symnum;
64 1.3 mycroft
65 1.3 mycroft where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
66 1.38 joerg
67 1.38 joerg switch (ELF_R_TYPE(rel->r_info)) {
68 1.38 joerg case R_TYPE(PC32):
69 1.38 joerg case R_TYPE(GOT32):
70 1.38 joerg case R_TYPE(32):
71 1.38 joerg case R_TYPE(GLOB_DAT):
72 1.38 joerg case R_TYPE(TLS_TPOFF):
73 1.38 joerg case R_TYPE(TLS_TPOFF32):
74 1.38 joerg case R_TYPE(TLS_DTPMOD32):
75 1.38 joerg case R_TYPE(TLS_DTPOFF32):
76 1.38 joerg symnum = ELF_R_SYM(rel->r_info);
77 1.38 joerg if (symnum != last_symnum) {
78 1.38 joerg last_symnum = symnum;
79 1.38 joerg def = _rtld_find_symdef(symnum, obj, &defobj,
80 1.38 joerg false);
81 1.38 joerg if (def == NULL)
82 1.38 joerg return -1;
83 1.38 joerg }
84 1.38 joerg break;
85 1.38 joerg default:
86 1.38 joerg break;
87 1.38 joerg }
88 1.38 joerg
89 1.3 mycroft
90 1.3 mycroft switch (ELF_R_TYPE(rel->r_info)) {
91 1.3 mycroft case R_TYPE(NONE):
92 1.3 mycroft break;
93 1.2 mycroft
94 1.2 mycroft #if 1 /* XXX should not occur */
95 1.3 mycroft case R_TYPE(PC32):
96 1.23 matt target = (Elf_Addr)(defobj->relocbase + def->st_value);
97 1.3 mycroft
98 1.4 mycroft *where += target - (Elf_Addr)where;
99 1.14 mycroft rdbg(("PC32 %s in %s --> %p in %s",
100 1.5 mycroft obj->strtab + obj->symtab[symnum].st_name,
101 1.5 mycroft obj->path, (void *)*where, defobj->path));
102 1.3 mycroft break;
103 1.2 mycroft
104 1.3 mycroft case R_TYPE(GOT32):
105 1.2 mycroft #endif
106 1.3 mycroft case R_TYPE(32):
107 1.3 mycroft case R_TYPE(GLOB_DAT):
108 1.23 matt target = (Elf_Addr)(defobj->relocbase + def->st_value);
109 1.3 mycroft
110 1.4 mycroft tmp = target + *where;
111 1.3 mycroft if (*where != tmp)
112 1.3 mycroft *where = tmp;
113 1.14 mycroft rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
114 1.5 mycroft obj->strtab + obj->symtab[symnum].st_name,
115 1.5 mycroft obj->path, (void *)*where, defobj->path));
116 1.3 mycroft break;
117 1.3 mycroft
118 1.3 mycroft case R_TYPE(RELATIVE):
119 1.12 mycroft *where += (Elf_Addr)obj->relocbase;
120 1.14 mycroft rdbg(("RELATIVE in %s --> %p", obj->path,
121 1.12 mycroft (void *)*where));
122 1.3 mycroft break;
123 1.3 mycroft
124 1.3 mycroft case R_TYPE(COPY):
125 1.3 mycroft /*
126 1.3 mycroft * These are deferred until all other relocations have
127 1.3 mycroft * been done. All we do here is make sure that the
128 1.3 mycroft * COPY relocation is not in a shared library. They
129 1.3 mycroft * are allowed only in executable files.
130 1.3 mycroft */
131 1.8 mycroft if (obj->isdynamic) {
132 1.3 mycroft _rtld_error(
133 1.2 mycroft "%s: Unexpected R_COPY relocation in shared library",
134 1.3 mycroft obj->path);
135 1.3 mycroft return -1;
136 1.3 mycroft }
137 1.14 mycroft rdbg(("COPY (avoid in main)"));
138 1.3 mycroft break;
139 1.3 mycroft
140 1.33 joerg case R_TYPE(TLS_TPOFF):
141 1.33 joerg if (!defobj->tls_done &&
142 1.33 joerg _rtld_tls_offset_allocate(obj))
143 1.33 joerg return -1;
144 1.33 joerg
145 1.35 apb *where += (Elf_Addr)(def->st_value - defobj->tlsoffset);
146 1.33 joerg
147 1.33 joerg rdbg(("TLS_TPOFF %s in %s --> %p",
148 1.33 joerg obj->strtab + obj->symtab[symnum].st_name,
149 1.33 joerg obj->path, (void *)*where));
150 1.33 joerg break;
151 1.33 joerg
152 1.35 apb case R_TYPE(TLS_TPOFF32):
153 1.35 apb if (!defobj->tls_done &&
154 1.35 apb _rtld_tls_offset_allocate(obj))
155 1.35 apb return -1;
156 1.35 apb
157 1.35 apb *where += (Elf_Addr)(defobj->tlsoffset - def->st_value);
158 1.35 apb rdbg(("TLS_TPOFF32 %s in %s --> %p",
159 1.35 apb obj->strtab + obj->symtab[symnum].st_name,
160 1.35 apb obj->path, (void *)*where));
161 1.35 apb break;
162 1.35 apb
163 1.33 joerg case R_TYPE(TLS_DTPMOD32):
164 1.33 joerg *where = (Elf_Addr)(defobj->tlsindex);
165 1.33 joerg
166 1.33 joerg rdbg(("TLS_DTPMOD32 %s in %s --> %p",
167 1.33 joerg obj->strtab + obj->symtab[symnum].st_name,
168 1.33 joerg obj->path, (void *)*where));
169 1.33 joerg break;
170 1.33 joerg
171 1.33 joerg case R_TYPE(TLS_DTPOFF32):
172 1.33 joerg *where = (Elf_Addr)(def->st_value);
173 1.33 joerg
174 1.33 joerg rdbg(("TLS_DTPOFF32 %s in %s --> %p",
175 1.33 joerg obj->strtab + obj->symtab[symnum].st_name,
176 1.33 joerg obj->path, (void *)*where));
177 1.33 joerg
178 1.33 joerg break;
179 1.33 joerg
180 1.3 mycroft default:
181 1.14 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
182 1.3 mycroft "contents = %p, symbol = %s",
183 1.38 joerg (u_long)ELF_R_SYM(rel->r_info),
184 1.38 joerg (u_long)ELF_R_TYPE(rel->r_info),
185 1.3 mycroft (void *)rel->r_offset, (void *)*where,
186 1.4 mycroft obj->strtab + obj->symtab[symnum].st_name));
187 1.3 mycroft _rtld_error("%s: Unsupported relocation type %ld "
188 1.28 jmmv "in non-PLT relocations",
189 1.3 mycroft obj->path, (u_long) ELF_R_TYPE(rel->r_info));
190 1.2 mycroft return -1;
191 1.2 mycroft }
192 1.2 mycroft }
193 1.6 mycroft return 0;
194 1.6 mycroft }
195 1.6 mycroft
196 1.6 mycroft int
197 1.19 skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
198 1.6 mycroft {
199 1.6 mycroft const Elf_Rel *rel;
200 1.6 mycroft
201 1.18 mycroft if (!obj->relocbase)
202 1.6 mycroft return 0;
203 1.6 mycroft
204 1.6 mycroft for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
205 1.6 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
206 1.6 mycroft
207 1.6 mycroft assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
208 1.6 mycroft
209 1.6 mycroft /* Just relocate the GOT slots pointing into the PLT */
210 1.6 mycroft *where += (Elf_Addr)obj->relocbase;
211 1.14 mycroft rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
212 1.6 mycroft }
213 1.6 mycroft
214 1.6 mycroft return 0;
215 1.6 mycroft }
216 1.6 mycroft
217 1.26 matt static inline int
218 1.26 matt _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
219 1.26 matt Elf_Addr *tp)
220 1.6 mycroft {
221 1.16 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
222 1.26 matt Elf_Addr target;
223 1.26 matt const Elf_Sym *def;
224 1.6 mycroft const Obj_Entry *defobj;
225 1.29 christos unsigned long info = rel->r_info;
226 1.6 mycroft
227 1.29 christos assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
228 1.6 mycroft
229 1.29 christos def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
230 1.29 christos if (__predict_false(def == NULL))
231 1.26 matt return -1;
232 1.29 christos if (__predict_false(def == &_rtld_sym_zero))
233 1.29 christos return 0;
234 1.29 christos
235 1.36 joerg if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
236 1.36 joerg if (tp == NULL)
237 1.36 joerg return 0;
238 1.36 joerg target = _rtld_resolve_ifunc(defobj, def);
239 1.36 joerg } else {
240 1.36 joerg target = (Elf_Addr)(defobj->relocbase + def->st_value);
241 1.36 joerg }
242 1.36 joerg
243 1.26 matt rdbg(("bind now/fixup in %s --> old=%p new=%p",
244 1.26 matt defobj->strtab + def->st_name, (void *)*where,
245 1.26 matt (void *)target));
246 1.26 matt if (*where != target)
247 1.26 matt *where = target;
248 1.26 matt if (tp)
249 1.26 matt *tp = target;
250 1.26 matt return 0;
251 1.26 matt }
252 1.26 matt
253 1.26 matt caddr_t
254 1.26 matt _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
255 1.26 matt {
256 1.31 skrll const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel
257 1.31 skrll + reloff);
258 1.26 matt Elf_Addr new_value;
259 1.26 matt int err;
260 1.26 matt
261 1.30 skrll new_value = 0; /* XXX gcc */
262 1.30 skrll
263 1.34 joerg _rtld_shared_enter();
264 1.26 matt err = _rtld_relocate_plt_object(obj, rel, &new_value);
265 1.29 christos if (err)
266 1.16 mycroft _rtld_die();
267 1.34 joerg _rtld_shared_exit();
268 1.6 mycroft
269 1.16 mycroft return (caddr_t)new_value;
270 1.15 junyoung }
271 1.15 junyoung
272 1.15 junyoung int
273 1.15 junyoung _rtld_relocate_plt_objects(const Obj_Entry *obj)
274 1.15 junyoung {
275 1.15 junyoung const Elf_Rel *rel;
276 1.26 matt int err = 0;
277 1.15 junyoung
278 1.15 junyoung for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
279 1.26 matt err = _rtld_relocate_plt_object(obj, rel, NULL);
280 1.26 matt if (err)
281 1.26 matt break;
282 1.15 junyoung }
283 1.26 matt return err;
284 1.2 mycroft }
285 1.33 joerg
286 1.33 joerg /*
287 1.33 joerg * i386 specific GNU variant of __tls_get_addr using register based
288 1.33 joerg * argument passing.
289 1.33 joerg */
290 1.33 joerg #define DTV_MAX_INDEX(dtv) ((size_t)((dtv)[-1]))
291 1.33 joerg
292 1.33 joerg __dso_public __attribute__((__regparm__(1))) void *
293 1.33 joerg ___tls_get_addr(void *arg_)
294 1.33 joerg {
295 1.33 joerg size_t *arg = (size_t *)arg_;
296 1.33 joerg void **dtv;
297 1.33 joerg struct tls_tcb *tcb = __lwp_getprivate_fast();
298 1.33 joerg size_t idx = arg[0], offset = arg[1];
299 1.33 joerg
300 1.33 joerg dtv = tcb->tcb_dtv;
301 1.33 joerg
302 1.33 joerg if (__predict_true(idx < DTV_MAX_INDEX(dtv) && dtv[idx] != NULL))
303 1.33 joerg return (uint8_t *)dtv[idx] + offset;
304 1.33 joerg
305 1.33 joerg return _rtld_tls_get_addr(tcb, idx, offset);
306 1.33 joerg }
307