xref: /freebsd/libexec/rtld-elf/i386/reloc.c (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
1 /*-
2  * Copyright 1996, 1997, 1998, 1999 John D. Polstra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 /*
29  * Dynamic linker for ELF.
30  *
31  * John Polstra <jdp@polstra.com>.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/mman.h>
36 #include <machine/segments.h>
37 #include <machine/sysarch.h>
38 
39 #include <dlfcn.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include "debug.h"
50 #include "rtld.h"
51 
52 /*
53  * Process the special R_386_COPY relocations in the main program.  These
54  * copy data from a shared object into a region in the main program's BSS
55  * segment.
56  *
57  * Returns 0 on success, -1 on failure.
58  */
59 int
60 do_copy_relocations(Obj_Entry *dstobj)
61 {
62     const Elf_Rel *rellim;
63     const Elf_Rel *rel;
64 
65     assert(dstobj->mainprog);	/* COPY relocations are invalid elsewhere */
66 
67     rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
68     for (rel = dstobj->rel;  rel < rellim;  rel++) {
69 	if (ELF_R_TYPE(rel->r_info) == R_386_COPY) {
70 	    void *dstaddr;
71 	    const Elf_Sym *dstsym;
72 	    const char *name;
73 	    size_t size;
74 	    const void *srcaddr;
75 	    const Elf_Sym *srcsym;
76 	    const Obj_Entry *srcobj, *defobj;
77 	    SymLook req;
78 	    int res;
79 
80 	    dstaddr = (void *) (dstobj->relocbase + rel->r_offset);
81 	    dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
82 	    name = dstobj->strtab + dstsym->st_name;
83 	    size = dstsym->st_size;
84 	    symlook_init(&req, name);
85 	    req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info));
86 
87 	    for (srcobj = dstobj->next;  srcobj != NULL;  srcobj = srcobj->next) {
88 		res = symlook_obj(&req, srcobj);
89 		if (res == 0) {
90 		    srcsym = req.sym_out;
91 		    defobj = req.defobj_out;
92 		    break;
93 		}
94 	    }
95 
96 	    if (srcobj == NULL) {
97 		_rtld_error("Undefined symbol \"%s\" referenced from COPY"
98 		  " relocation in %s", name, dstobj->path);
99 		return -1;
100 	    }
101 
102 	    srcaddr = (const void *) (defobj->relocbase + srcsym->st_value);
103 	    memcpy(dstaddr, srcaddr, size);
104 	}
105     }
106 
107     return 0;
108 }
109 
110 /* Initialize the special GOT entries. */
111 void
112 init_pltgot(Obj_Entry *obj)
113 {
114     if (obj->pltgot != NULL) {
115 	obj->pltgot[1] = (Elf_Addr) obj;
116 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
117     }
118 }
119 
120 /* Process the non-PLT relocations. */
121 int
122 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, RtldLockState *lockstate)
123 {
124 	const Elf_Rel *rellim;
125 	const Elf_Rel *rel;
126 	SymCache *cache;
127 	int r = -1;
128 
129 	/*
130 	 * The dynamic loader may be called from a thread, we have
131 	 * limited amounts of stack available so we cannot use alloca().
132 	 */
133 	if (obj != obj_rtld) {
134 	    cache = calloc(obj->nchains, sizeof(SymCache));
135 	    /* No need to check for NULL here */
136 	} else
137 	    cache = NULL;
138 
139 	rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
140 	for (rel = obj->rel;  rel < rellim;  rel++) {
141 	    Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
142 
143 	    switch (ELF_R_TYPE(rel->r_info)) {
144 
145 	    case R_386_NONE:
146 		break;
147 
148 	    case R_386_32:
149 		{
150 		    const Elf_Sym *def;
151 		    const Obj_Entry *defobj;
152 
153 		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
154 		      false, cache, lockstate);
155 		    if (def == NULL)
156 			goto done;
157 
158 		    *where += (Elf_Addr) (defobj->relocbase + def->st_value);
159 		}
160 		break;
161 
162 	    case R_386_PC32:
163 		/*
164 		 * I don't think the dynamic linker should ever see this
165 		 * type of relocation.  But the binutils-2.6 tools sometimes
166 		 * generate it.
167 		 */
168 		{
169 		    const Elf_Sym *def;
170 		    const Obj_Entry *defobj;
171 
172 		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
173 		      false, cache, lockstate);
174 		    if (def == NULL)
175 			goto done;
176 
177 		    *where +=
178 		      (Elf_Addr) (defobj->relocbase + def->st_value) -
179 		      (Elf_Addr) where;
180 		}
181 		break;
182 
183 	    case R_386_COPY:
184 		/*
185 		 * These are deferred until all other relocations have
186 		 * been done.  All we do here is make sure that the COPY
187 		 * relocation is not in a shared library.  They are allowed
188 		 * only in executable files.
189 		 */
190 		if (!obj->mainprog) {
191 		    _rtld_error("%s: Unexpected R_386_COPY relocation"
192 		      " in shared library", obj->path);
193 		    goto done;
194 		}
195 		break;
196 
197 	    case R_386_GLOB_DAT:
198 		{
199 		    const Elf_Sym *def;
200 		    const Obj_Entry *defobj;
201 
202 		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
203 		      false, cache, lockstate);
204 		    if (def == NULL)
205 			goto done;
206 
207 		    *where = (Elf_Addr) (defobj->relocbase + def->st_value);
208 		}
209 		break;
210 
211 	    case R_386_RELATIVE:
212 		*where += (Elf_Addr) obj->relocbase;
213 		break;
214 
215 	    case R_386_TLS_TPOFF:
216 	    case R_386_TLS_TPOFF32:
217 		{
218 		    const Elf_Sym *def;
219 		    const Obj_Entry *defobj;
220 		    Elf_Addr add;
221 
222 		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
223 		      false, cache, lockstate);
224 		    if (def == NULL)
225 			goto done;
226 
227 		    /*
228 		     * We lazily allocate offsets for static TLS as we
229 		     * see the first relocation that references the
230 		     * TLS block. This allows us to support (small
231 		     * amounts of) static TLS in dynamically loaded
232 		     * modules. If we run out of space, we generate an
233 		     * error.
234 		     */
235 		    if (!defobj->tls_done) {
236 			if (!allocate_tls_offset((Obj_Entry*) defobj)) {
237 			    _rtld_error("%s: No space available for static "
238 					"Thread Local Storage", obj->path);
239 			    goto done;
240 			}
241 		    }
242 		    add = (Elf_Addr) (def->st_value - defobj->tlsoffset);
243 		    if (ELF_R_TYPE(rel->r_info) == R_386_TLS_TPOFF)
244 			*where += add;
245 		    else
246 			*where -= add;
247 		}
248 		break;
249 
250 	    case R_386_TLS_DTPMOD32:
251 		{
252 		    const Elf_Sym *def;
253 		    const Obj_Entry *defobj;
254 
255 		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
256 		      false, cache, lockstate);
257 		    if (def == NULL)
258 			goto done;
259 
260 		    *where += (Elf_Addr) defobj->tlsindex;
261 		}
262 		break;
263 
264 	    case R_386_TLS_DTPOFF32:
265 		{
266 		    const Elf_Sym *def;
267 		    const Obj_Entry *defobj;
268 
269 		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
270 		      false, cache, lockstate);
271 		    if (def == NULL)
272 			goto done;
273 
274 		    *where += (Elf_Addr) def->st_value;
275 		}
276 		break;
277 
278 	    default:
279 		_rtld_error("%s: Unsupported relocation type %d"
280 		  " in non-PLT relocations\n", obj->path,
281 		  ELF_R_TYPE(rel->r_info));
282 		goto done;
283 	    }
284 	}
285 	r = 0;
286 done:
287 	if (cache != NULL)
288 	    free(cache);
289 	return(r);
290 }
291 
292 /* Process the PLT relocations. */
293 int
294 reloc_plt(Obj_Entry *obj)
295 {
296     const Elf_Rel *rellim;
297     const Elf_Rel *rel;
298 
299     rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
300     for (rel = obj->pltrel;  rel < rellim;  rel++) {
301 	Elf_Addr *where/*, val*/;
302 
303 	switch (ELF_R_TYPE(rel->r_info)) {
304 	case R_386_JMP_SLOT:
305 	  /* Relocate the GOT slot pointing into the PLT. */
306 	  where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
307 	  *where += (Elf_Addr)obj->relocbase;
308 	  break;
309 
310 	case R_386_IRELATIVE:
311 	  obj->irelative = true;
312 	  break;
313 
314 	default:
315 	  _rtld_error("Unknown relocation type %x in PLT",
316 	    ELF_R_TYPE(rel->r_info));
317 	  return (-1);
318 	}
319     }
320     return 0;
321 }
322 
323 /* Relocate the jump slots in an object. */
324 int
325 reloc_jmpslots(Obj_Entry *obj, RtldLockState *lockstate)
326 {
327     const Elf_Rel *rellim;
328     const Elf_Rel *rel;
329 
330     if (obj->jmpslots_done)
331 	return 0;
332     rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
333     for (rel = obj->pltrel;  rel < rellim;  rel++) {
334 	Elf_Addr *where, target;
335 	const Elf_Sym *def;
336 	const Obj_Entry *defobj;
337 
338 	switch (ELF_R_TYPE(rel->r_info)) {
339 	case R_386_JMP_SLOT:
340 	  where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
341 	  def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL,
342 	      lockstate);
343 	  if (def == NULL)
344 	      return (-1);
345 	  if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
346 	      obj->gnu_ifunc = true;
347 	      continue;
348 	  }
349 	  target = (Elf_Addr)(defobj->relocbase + def->st_value);
350 	  reloc_jmpslot(where, target, defobj, obj, rel);
351 	  break;
352 
353 	case R_386_IRELATIVE:
354 	  break;
355 
356 	default:
357 	  _rtld_error("Unknown relocation type %x in PLT",
358 	    ELF_R_TYPE(rel->r_info));
359 	  return (-1);
360 	}
361     }
362 
363     obj->jmpslots_done = true;
364     return 0;
365 }
366 
367 int
368 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
369 {
370     const Elf_Rel *rellim;
371     const Elf_Rel *rel;
372     Elf_Addr *where, target;
373 
374     if (!obj->irelative)
375 	return (0);
376     rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
377     for (rel = obj->pltrel;  rel < rellim;  rel++) {
378 	switch (ELF_R_TYPE(rel->r_info)) {
379 	case R_386_IRELATIVE:
380 	  where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
381 	  lock_release(rtld_bind_lock, lockstate);
382 	  target = ((Elf_Addr (*)(void))(obj->relocbase + *where))();
383 	  wlock_acquire(rtld_bind_lock, lockstate);
384 	  *where = target;
385 	  break;
386 	}
387     }
388     obj->irelative = false;
389     return (0);
390 }
391 
392 int
393 reloc_gnu_ifunc(Obj_Entry *obj, RtldLockState *lockstate)
394 {
395     const Elf_Rel *rellim;
396     const Elf_Rel *rel;
397 
398     if (!obj->gnu_ifunc)
399 	return (0);
400     rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
401     for (rel = obj->pltrel;  rel < rellim;  rel++) {
402 	Elf_Addr *where, target;
403 	const Elf_Sym *def;
404 	const Obj_Entry *defobj;
405 
406 	switch (ELF_R_TYPE(rel->r_info)) {
407 	case R_386_JMP_SLOT:
408 	  where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
409 	  def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL,
410 	      lockstate);
411 	  if (def == NULL)
412 	      return (-1);
413 	  if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
414 	      continue;
415 	  lock_release(rtld_bind_lock, lockstate);
416 	  target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
417 	  wlock_acquire(rtld_bind_lock, lockstate);
418 	  reloc_jmpslot(where, target, defobj, obj, rel);
419 	  break;
420 	}
421     }
422 
423     obj->gnu_ifunc = false;
424     return (0);
425 }
426 
427 void
428 allocate_initial_tls(Obj_Entry *objs)
429 {
430     void* tls;
431 
432     /*
433      * Fix the size of the static TLS block by using the maximum
434      * offset allocated so far and adding a bit for dynamic modules to
435      * use.
436      */
437     tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
438     tls = allocate_tls(objs, NULL, 3*sizeof(Elf_Addr), sizeof(Elf_Addr));
439     i386_set_gsbase(tls);
440 }
441 
442 /* GNU ABI */
443 __attribute__((__regparm__(1)))
444 void *___tls_get_addr(tls_index *ti)
445 {
446     Elf_Addr** segbase;
447     Elf_Addr* dtv;
448 
449     __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
450     dtv = segbase[1];
451 
452     return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);
453 }
454 
455 /* Sun ABI */
456 void *__tls_get_addr(tls_index *ti)
457 {
458     Elf_Addr** segbase;
459     Elf_Addr* dtv;
460 
461     __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
462     dtv = segbase[1];
463 
464     return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);
465 }
466