xref: /freebsd/libexec/rtld-elf/amd64/reloc.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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/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  */
58 int
59 do_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 	    unsigned long hash;
73 	    size_t size;
74 	    const void *srcaddr;
75 	    const Elf_Sym *srcsym;
76 	    Obj_Entry *srcobj;
77 
78 	    dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
79 	    dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
80 	    name = dstobj->strtab + dstsym->st_name;
81 	    hash = elf_hash(name);
82 	    size = dstsym->st_size;
83 
84 	    for (srcobj = dstobj->next;  srcobj != NULL;  srcobj = srcobj->next)
85 		if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL)
86 		    break;
87 
88 	    if (srcobj == NULL) {
89 		_rtld_error("Undefined symbol \"%s\" referenced from COPY"
90 		  " relocation in %s", name, dstobj->path);
91 		return -1;
92 	    }
93 
94 	    srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
95 	    memcpy(dstaddr, srcaddr, size);
96 	}
97     }
98 
99     return 0;
100 }
101 
102 /* Initialize the special GOT entries. */
103 void
104 init_pltgot(Obj_Entry *obj)
105 {
106     if (obj->pltgot != NULL) {
107 	obj->pltgot[1] = (Elf_Addr) obj;
108 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
109     }
110 }
111 
112 /* Process the non-PLT relocations. */
113 int
114 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
115 {
116 	const Elf_Rela *relalim;
117 	const Elf_Rela *rela;
118 	SymCache *cache;
119 	int bytes = obj->nchains * sizeof(SymCache);
120 	int r = -1;
121 
122 	/*
123 	 * The dynamic loader may be called from a thread, we have
124 	 * limited amounts of stack available so we cannot use alloca().
125 	 */
126 	cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
127 	if (cache == MAP_FAILED)
128 	    cache = NULL;
129 
130 	relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize);
131 	for (rela = obj->rela;  rela < relalim;  rela++) {
132 	    Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
133 	    Elf32_Addr *where32 = (Elf32_Addr *)where;
134 
135 	    switch (ELF_R_TYPE(rela->r_info)) {
136 
137 	    case R_X86_64_NONE:
138 		break;
139 
140 	    case R_X86_64_64:
141 		{
142 		    const Elf_Sym *def;
143 		    const Obj_Entry *defobj;
144 
145 		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
146 		      false, cache);
147 		    if (def == NULL)
148 			goto done;
149 
150 		    *where = (Elf_Addr) (defobj->relocbase + def->st_value + rela->r_addend);
151 		}
152 		break;
153 
154 	    case R_X86_64_PC32:
155 		/*
156 		 * I don't think the dynamic linker should ever see this
157 		 * type of relocation.  But the binutils-2.6 tools sometimes
158 		 * generate it.
159 		 */
160 		{
161 		    const Elf_Sym *def;
162 		    const Obj_Entry *defobj;
163 
164 		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
165 		      false, cache);
166 		    if (def == NULL)
167 			goto done;
168 
169 		    *where32 = (Elf32_Addr) (unsigned long) (defobj->relocbase +
170 		        def->st_value + rela->r_addend - (Elf_Addr) where);
171 		}
172 		break;
173 	/* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
174 
175 	    case R_X86_64_COPY:
176 		/*
177 		 * These are deferred until all other relocations have
178 		 * been done.  All we do here is make sure that the COPY
179 		 * relocation is not in a shared library.  They are allowed
180 		 * only in executable files.
181 		 */
182 		if (!obj->mainprog) {
183 		    _rtld_error("%s: Unexpected R_X86_64_COPY relocation"
184 		      " in shared library", obj->path);
185 		    goto done;
186 		}
187 		break;
188 
189 	    case R_X86_64_GLOB_DAT:
190 		{
191 		    const Elf_Sym *def;
192 		    const Obj_Entry *defobj;
193 
194 		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
195 		      false, cache);
196 		    if (def == NULL)
197 			goto done;
198 
199 		    *where = (Elf_Addr) (defobj->relocbase + def->st_value);
200 		}
201 		break;
202 
203 	    case R_X86_64_TPOFF64:
204 		{
205 		    const Elf_Sym *def;
206 		    const Obj_Entry *defobj;
207 
208 		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
209 		      false, cache);
210 		    if (def == NULL)
211 			goto done;
212 
213 		    /*
214 		     * We lazily allocate offsets for static TLS as we
215 		     * see the first relocation that references the
216 		     * TLS block. This allows us to support (small
217 		     * amounts of) static TLS in dynamically loaded
218 		     * modules. If we run out of space, we generate an
219 		     * error.
220 		     */
221 		    if (!defobj->tls_done) {
222 			if (!allocate_tls_offset((Obj_Entry*) defobj)) {
223 			    _rtld_error("%s: No space available for static "
224 					"Thread Local Storage", obj->path);
225 			    goto done;
226 			}
227 		    }
228 
229 		    *where = (Elf_Addr) (def->st_value - defobj->tlsoffset +
230 					 rela->r_addend);
231 		}
232 		break;
233 
234 	    case R_X86_64_TPOFF32:
235 		{
236 		    const Elf_Sym *def;
237 		    const Obj_Entry *defobj;
238 
239 		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
240 		      false, cache);
241 		    if (def == NULL)
242 			goto done;
243 
244 		    /*
245 		     * We lazily allocate offsets for static TLS as we
246 		     * see the first relocation that references the
247 		     * TLS block. This allows us to support (small
248 		     * amounts of) static TLS in dynamically loaded
249 		     * modules. If we run out of space, we generate an
250 		     * error.
251 		     */
252 		    if (!defobj->tls_done) {
253 			if (!allocate_tls_offset((Obj_Entry*) defobj)) {
254 			    _rtld_error("%s: No space available for static "
255 					"Thread Local Storage", obj->path);
256 			    goto done;
257 			}
258 		    }
259 
260 		    *where32 = (Elf32_Addr) (def->st_value -
261 					     defobj->tlsoffset +
262 					     rela->r_addend);
263 		}
264 		break;
265 
266 	    case R_X86_64_DTPMOD64:
267 		{
268 		    const Elf_Sym *def;
269 		    const Obj_Entry *defobj;
270 
271 		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
272 		      false, cache);
273 		    if (def == NULL)
274 			goto done;
275 
276 		    *where += (Elf_Addr) defobj->tlsindex;
277 		}
278 		break;
279 
280 	    case R_X86_64_DTPOFF64:
281 		{
282 		    const Elf_Sym *def;
283 		    const Obj_Entry *defobj;
284 
285 		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
286 		      false, cache);
287 		    if (def == NULL)
288 			goto done;
289 
290 		    *where += (Elf_Addr) (def->st_value + rela->r_addend);
291 		}
292 		break;
293 
294 	    case R_X86_64_DTPOFF32:
295 		{
296 		    const Elf_Sym *def;
297 		    const Obj_Entry *defobj;
298 
299 		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
300 		      false, cache);
301 		    if (def == NULL)
302 			goto done;
303 
304 		    *where32 += (Elf32_Addr) (def->st_value + rela->r_addend);
305 		}
306 		break;
307 
308 	    case R_X86_64_RELATIVE:
309 		*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
310 		break;
311 
312 	/* 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 */
313 
314 	    default:
315 		_rtld_error("%s: Unsupported relocation type %d"
316 		  " in non-PLT relocations\n", obj->path,
317 		  ELF_R_TYPE(rela->r_info));
318 		goto done;
319 	    }
320 	}
321 	r = 0;
322 done:
323 	if (cache)
324 	    munmap(cache, bytes);
325 	return(r);
326 }
327 
328 /* Process the PLT relocations. */
329 int
330 reloc_plt(Obj_Entry *obj)
331 {
332     const Elf_Rela *relalim;
333     const Elf_Rela *rela;
334 
335     relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
336     for (rela = obj->pltrela;  rela < relalim;  rela++) {
337 	Elf_Addr *where;
338 
339 	assert(ELF_R_TYPE(rela->r_info) == R_X86_64_JMP_SLOT);
340 
341 	/* Relocate the GOT slot pointing into the PLT. */
342 	where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
343 	*where += (Elf_Addr)obj->relocbase;
344     }
345     return 0;
346 }
347 
348 /* Relocate the jump slots in an object. */
349 int
350 reloc_jmpslots(Obj_Entry *obj)
351 {
352     const Elf_Rela *relalim;
353     const Elf_Rela *rela;
354 
355     if (obj->jmpslots_done)
356 	return 0;
357     relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
358     for (rela = obj->pltrela;  rela < relalim;  rela++) {
359 	Elf_Addr *where, target;
360 	const Elf_Sym *def;
361 	const Obj_Entry *defobj;
362 
363 	assert(ELF_R_TYPE(rela->r_info) == R_X86_64_JMP_SLOT);
364 	where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
365 	def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true, NULL);
366 	if (def == NULL)
367 	    return -1;
368 	target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
369 	reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
370     }
371     obj->jmpslots_done = true;
372     return 0;
373 }
374 
375 void
376 allocate_initial_tls(Obj_Entry *objs)
377 {
378     /*
379      * Fix the size of the static TLS block by using the maximum
380      * offset allocated so far and adding a bit for dynamic modules to
381      * use.
382      */
383     tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
384     amd64_set_fsbase(allocate_tls(objs, 0,
385 				  2*sizeof(Elf_Addr), sizeof(Elf_Addr)));
386 }
387 
388 void *__tls_get_addr(tls_index *ti)
389 {
390     Elf_Addr** segbase;
391     Elf_Addr* dtv;
392 
393     __asm __volatile("movq %%fs:0, %0" : "=r" (segbase));
394     dtv = segbase[1];
395 
396     return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);
397 }
398