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