reloc.c revision 228435
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: head/libexec/rtld-elf/amd64/reloc.c 228435 2011-12-12 11:03:14Z kib $
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/sysarch.h>
37
38#include <dlfcn.h>
39#include <err.h>
40#include <errno.h>
41#include <fcntl.h>
42#include <stdarg.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48#include "debug.h"
49#include "rtld.h"
50
51/*
52 * Process the special R_X86_64_COPY relocations in the main program.  These
53 * copy data from a shared object into a region in the main program's BSS
54 * segment.
55 *
56 * Returns 0 on success, -1 on failure.
57 */
58int
59do_copy_relocations(Obj_Entry *dstobj)
60{
61    const Elf_Rela *relalim;
62    const Elf_Rela *rela;
63
64    assert(dstobj->mainprog);	/* COPY relocations are invalid elsewhere */
65
66    relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela + dstobj->relasize);
67    for (rela = dstobj->rela;  rela < relalim;  rela++) {
68	if (ELF_R_TYPE(rela->r_info) == R_X86_64_COPY) {
69	    void *dstaddr;
70	    const Elf_Sym *dstsym;
71	    const char *name;
72	    size_t size;
73	    const void *srcaddr;
74	    const Elf_Sym *srcsym;
75	    const Obj_Entry *srcobj, *defobj;
76	    SymLook req;
77	    int res;
78
79	    dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
80	    dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
81	    name = dstobj->strtab + dstsym->st_name;
82	    size = dstsym->st_size;
83	    symlook_init(&req, name);
84	    req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
85
86	    for (srcobj = dstobj->next;  srcobj != NULL;  srcobj = srcobj->next) {
87		res = symlook_obj(&req, srcobj);
88		if (res == 0) {
89		    srcsym = req.sym_out;
90		    defobj = req.defobj_out;
91		    break;
92		}
93	    }
94
95	    if (srcobj == NULL) {
96		_rtld_error("Undefined symbol \"%s\" referenced from COPY"
97		  " relocation in %s", name, dstobj->path);
98		return -1;
99	    }
100
101	    srcaddr = (const void *) (defobj->relocbase + srcsym->st_value);
102	    memcpy(dstaddr, srcaddr, size);
103	}
104    }
105
106    return 0;
107}
108
109/* Initialize the special GOT entries. */
110void
111init_pltgot(Obj_Entry *obj)
112{
113    if (obj->pltgot != NULL) {
114	obj->pltgot[1] = (Elf_Addr) obj;
115	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
116    }
117}
118
119/* Process the non-PLT relocations. */
120int
121reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, RtldLockState *lockstate)
122{
123	const Elf_Rela *relalim;
124	const Elf_Rela *rela;
125	SymCache *cache;
126	int r = -1;
127
128	/*
129	 * The dynamic loader may be called from a thread, we have
130	 * limited amounts of stack available so we cannot use alloca().
131	 */
132	if (obj != obj_rtld) {
133	    cache = calloc(obj->nchains, sizeof(SymCache));
134	    /* No need to check for NULL here */
135	} else
136	    cache = NULL;
137
138	relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize);
139	for (rela = obj->rela;  rela < relalim;  rela++) {
140	    Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
141	    Elf32_Addr *where32 = (Elf32_Addr *)where;
142
143	    switch (ELF_R_TYPE(rela->r_info)) {
144
145	    case R_X86_64_NONE:
146		break;
147
148	    case R_X86_64_64:
149		{
150		    const Elf_Sym *def;
151		    const Obj_Entry *defobj;
152
153		    def = find_symdef(ELF_R_SYM(rela->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 + rela->r_addend);
159		}
160		break;
161
162	    case R_X86_64_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(rela->r_info), obj, &defobj,
173		      false, cache, lockstate);
174		    if (def == NULL)
175			goto done;
176
177		    *where32 = (Elf32_Addr) (unsigned long) (defobj->relocbase +
178		        def->st_value + rela->r_addend - (Elf_Addr) where);
179		}
180		break;
181	/* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
182
183	    case R_X86_64_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_X86_64_COPY relocation"
192		      " in shared library", obj->path);
193		    goto done;
194		}
195		break;
196
197	    case R_X86_64_GLOB_DAT:
198		{
199		    const Elf_Sym *def;
200		    const Obj_Entry *defobj;
201
202		    def = find_symdef(ELF_R_SYM(rela->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_X86_64_TPOFF64:
212		{
213		    const Elf_Sym *def;
214		    const Obj_Entry *defobj;
215
216		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
217		      false, cache, lockstate);
218		    if (def == NULL)
219			goto done;
220
221		    /*
222		     * We lazily allocate offsets for static TLS as we
223		     * see the first relocation that references the
224		     * TLS block. This allows us to support (small
225		     * amounts of) static TLS in dynamically loaded
226		     * modules. If we run out of space, we generate an
227		     * error.
228		     */
229		    if (!defobj->tls_done) {
230			if (!allocate_tls_offset((Obj_Entry*) defobj)) {
231			    _rtld_error("%s: No space available for static "
232					"Thread Local Storage", obj->path);
233			    goto done;
234			}
235		    }
236
237		    *where = (Elf_Addr) (def->st_value - defobj->tlsoffset +
238					 rela->r_addend);
239		}
240		break;
241
242	    case R_X86_64_TPOFF32:
243		{
244		    const Elf_Sym *def;
245		    const Obj_Entry *defobj;
246
247		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
248		      false, cache, lockstate);
249		    if (def == NULL)
250			goto done;
251
252		    /*
253		     * We lazily allocate offsets for static TLS as we
254		     * see the first relocation that references the
255		     * TLS block. This allows us to support (small
256		     * amounts of) static TLS in dynamically loaded
257		     * modules. If we run out of space, we generate an
258		     * error.
259		     */
260		    if (!defobj->tls_done) {
261			if (!allocate_tls_offset((Obj_Entry*) defobj)) {
262			    _rtld_error("%s: No space available for static "
263					"Thread Local Storage", obj->path);
264			    goto done;
265			}
266		    }
267
268		    *where32 = (Elf32_Addr) (def->st_value -
269					     defobj->tlsoffset +
270					     rela->r_addend);
271		}
272		break;
273
274	    case R_X86_64_DTPMOD64:
275		{
276		    const Elf_Sym *def;
277		    const Obj_Entry *defobj;
278
279		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
280		      false, cache, lockstate);
281		    if (def == NULL)
282			goto done;
283
284		    *where += (Elf_Addr) defobj->tlsindex;
285		}
286		break;
287
288	    case R_X86_64_DTPOFF64:
289		{
290		    const Elf_Sym *def;
291		    const Obj_Entry *defobj;
292
293		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
294		      false, cache, lockstate);
295		    if (def == NULL)
296			goto done;
297
298		    *where += (Elf_Addr) (def->st_value + rela->r_addend);
299		}
300		break;
301
302	    case R_X86_64_DTPOFF32:
303		{
304		    const Elf_Sym *def;
305		    const Obj_Entry *defobj;
306
307		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
308		      false, cache, lockstate);
309		    if (def == NULL)
310			goto done;
311
312		    *where32 += (Elf32_Addr) (def->st_value + rela->r_addend);
313		}
314		break;
315
316	    case R_X86_64_RELATIVE:
317		*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
318		break;
319
320	/* missing: R_X86_64_GOTPCREL, R_X86_64_32, R_X86_64_32S, R_X86_64_16, R_X86_64_PC16, R_X86_64_8, R_X86_64_PC8 */
321
322	    default:
323		_rtld_error("%s: Unsupported relocation type %u"
324		  " in non-PLT relocations\n", obj->path,
325		  (unsigned int)ELF_R_TYPE(rela->r_info));
326		goto done;
327	    }
328	}
329	r = 0;
330done:
331	if (cache != NULL)
332	    free(cache);
333	return(r);
334}
335
336/* Process the PLT relocations. */
337int
338reloc_plt(Obj_Entry *obj)
339{
340    const Elf_Rela *relalim;
341    const Elf_Rela *rela;
342
343    relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
344    for (rela = obj->pltrela;  rela < relalim;  rela++) {
345	Elf_Addr *where;
346
347	switch(ELF_R_TYPE(rela->r_info)) {
348	case R_X86_64_JMP_SLOT:
349	  /* Relocate the GOT slot pointing into the PLT. */
350	  where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
351	  *where += (Elf_Addr)obj->relocbase;
352	  break;
353
354	case R_X86_64_IRELATIVE:
355	  obj->irelative = true;
356	  break;
357
358	default:
359	  _rtld_error("Unknown relocation type %x in PLT",
360	    (unsigned int)ELF_R_TYPE(rela->r_info));
361	  return (-1);
362	}
363    }
364    return 0;
365}
366
367/* Relocate the jump slots in an object. */
368int
369reloc_jmpslots(Obj_Entry *obj, RtldLockState *lockstate)
370{
371    const Elf_Rela *relalim;
372    const Elf_Rela *rela;
373
374    if (obj->jmpslots_done)
375	return 0;
376    relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
377    for (rela = obj->pltrela;  rela < relalim;  rela++) {
378	Elf_Addr *where, target;
379	const Elf_Sym *def;
380	const Obj_Entry *defobj;
381
382	switch (ELF_R_TYPE(rela->r_info)) {
383	case R_X86_64_JMP_SLOT:
384	  where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
385	  def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true, NULL,
386	      lockstate);
387	  if (def == NULL)
388	      return (-1);
389	  if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
390	      obj->gnu_ifunc = true;
391	      continue;
392	  }
393	  target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
394	  reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
395	  break;
396
397	case R_X86_64_IRELATIVE:
398	  break;
399
400	default:
401	  _rtld_error("Unknown relocation type %x in PLT",
402	    (unsigned int)ELF_R_TYPE(rela->r_info));
403	  return (-1);
404	}
405    }
406    obj->jmpslots_done = true;
407    return 0;
408}
409
410int
411reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
412{
413    const Elf_Rela *relalim;
414    const Elf_Rela *rela;
415
416    relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
417    for (rela = obj->pltrela;  rela < relalim;  rela++) {
418	Elf_Addr *where, target, *ptr;
419
420	switch (ELF_R_TYPE(rela->r_info)) {
421	case R_X86_64_JMP_SLOT:
422	  break;
423
424	case R_X86_64_IRELATIVE:
425	  ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend);
426	  where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
427	  target = ((Elf_Addr (*)(void))ptr)();
428	  *where = target;
429	  break;
430	}
431    }
432    return (0);
433}
434
435int
436reloc_gnu_ifunc(Obj_Entry *obj, RtldLockState *lockstate)
437{
438    const Elf_Rela *relalim;
439    const Elf_Rela *rela;
440
441    if (!obj->gnu_ifunc)
442	return (0);
443    relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
444    for (rela = obj->pltrela;  rela < relalim;  rela++) {
445	Elf_Addr *where, target;
446	const Elf_Sym *def;
447	const Obj_Entry *defobj;
448
449	switch (ELF_R_TYPE(rela->r_info)) {
450	case R_X86_64_JMP_SLOT:
451	  where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
452	  def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true, NULL,
453	      lockstate);
454	  if (def == NULL)
455	      return (-1);
456	  if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
457	      continue;
458	  target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
459	  reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
460	  break;
461	}
462    }
463    obj->gnu_ifunc = false;
464    return 0;
465}
466
467void
468allocate_initial_tls(Obj_Entry *objs)
469{
470    /*
471     * Fix the size of the static TLS block by using the maximum
472     * offset allocated so far and adding a bit for dynamic modules to
473     * use.
474     */
475    tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
476    amd64_set_fsbase(allocate_tls(objs, 0,
477				  3*sizeof(Elf_Addr), sizeof(Elf_Addr)));
478}
479
480void *__tls_get_addr(tls_index *ti)
481{
482    Elf_Addr** segbase;
483    Elf_Addr* dtv;
484
485    __asm __volatile("movq %%fs:0, %0" : "=r" (segbase));
486    dtv = segbase[1];
487
488    return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);
489}
490