Merge branch 'vendor/GDB'
[dragonfly.git] / libexec / rtld-elf / i386 / reloc.c
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 <sys/tls.h>
37
38 #include <machine/tls.h>
39
40 #include <dlfcn.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 #include "debug.h"
51 #include "rtld.h"
52
53 /*
54  * Process the special R_386_COPY relocations in the main program.  These
55  * copy data from a shared object into a region in the main program's BSS
56  * segment.
57  *
58  * Returns 0 on success, -1 on failure.
59  */
60 int
61 do_copy_relocations(Obj_Entry *dstobj)
62 {
63     const Elf_Rel *rellim;
64     const Elf_Rel *rel;
65
66     assert(dstobj->mainprog);   /* COPY relocations are invalid elsewhere */
67
68     rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
69     for (rel = dstobj->rel;  rel < rellim;  rel++) {
70         if (ELF_R_TYPE(rel->r_info) == R_386_COPY) {
71             void *dstaddr;
72             const Elf_Sym *dstsym;
73             const char *name;
74             size_t size;
75             const void *srcaddr;
76             const Elf_Sym *srcsym;
77             const Obj_Entry *srcobj, *defobj;
78             SymLook req;
79             int res;
80
81             dstaddr = (void *) (dstobj->relocbase + rel->r_offset);
82             dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
83             name = dstobj->strtab + dstsym->st_name;
84             size = dstsym->st_size;
85             symlook_init(&req, name);
86             req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info));
87             req.flags = SYMLOOK_EARLY;
88
89             for (srcobj = dstobj->next;  srcobj != NULL;  srcobj = srcobj->next) {
90                 res = symlook_obj(&req, srcobj);
91                 if (res == 0) {
92                     srcsym = req.sym_out;
93                     defobj = req.defobj_out;
94                     break;
95                 }
96             }
97
98             if (srcobj == NULL) {
99                 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
100                   " relocation in %s", name, dstobj->path);
101                 return -1;
102             }
103
104             srcaddr = (const void *) (defobj->relocbase + srcsym->st_value);
105             memcpy(dstaddr, srcaddr, size);
106         }
107     }
108
109     return 0;
110 }
111
112 /* Initialize the special GOT entries. */
113 void
114 init_pltgot(Obj_Entry *obj)
115 {
116     if (obj->pltgot != NULL) {
117         obj->pltgot[1] = (Elf_Addr) obj;
118         obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
119     }
120 }
121
122 /* Process the non-PLT relocations. */
123 int
124 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
125     RtldLockState *lockstate)
126 {
127         const Elf_Rel *rellim;
128         const Elf_Rel *rel;
129         SymCache *cache;
130         int r = -1;
131
132         /*
133          * The dynamic loader may be called from a thread, we have
134          * limited amounts of stack available so we cannot use alloca().
135          */
136         if (obj != obj_rtld) {
137             cache = calloc(obj->dynsymcount, sizeof(SymCache));
138             /* No need to check for NULL here */
139         } else
140             cache = NULL;
141
142         rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
143         for (rel = obj->rel;  rel < rellim;  rel++) {
144             Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
145
146             switch (ELF_R_TYPE(rel->r_info)) {
147
148             case R_386_NONE:
149                 break;
150
151             case R_386_32:
152                 {
153                     const Elf_Sym *def;
154                     const Obj_Entry *defobj;
155
156                     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
157                       flags, cache, lockstate);
158                     if (def == NULL)
159                         goto done;
160
161                     *where += (Elf_Addr) (defobj->relocbase + def->st_value);
162                 }
163                 break;
164
165             case R_386_PC32:
166                 /*
167                  * I don't think the dynamic linker should ever see this
168                  * type of relocation.  But the binutils-2.6 tools sometimes
169                  * generate it.
170                  */
171                 {
172                     const Elf_Sym *def;
173                     const Obj_Entry *defobj;
174
175                     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
176                       flags, cache, lockstate);
177                     if (def == NULL)
178                         goto done;
179
180                     *where +=
181                       (Elf_Addr) (defobj->relocbase + def->st_value) -
182                       (Elf_Addr) where;
183                 }
184                 break;
185
186             case R_386_COPY:
187                 /*
188                  * These are deferred until all other relocations have
189                  * been done.  All we do here is make sure that the COPY
190                  * relocation is not in a shared library.  They are allowed
191                  * only in executable files.
192                  */
193                 if (!obj->mainprog) {
194                     _rtld_error("%s: Unexpected R_386_COPY relocation"
195                       " in shared library", obj->path);
196                     goto done;
197                 }
198                 break;
199
200             case R_386_GLOB_DAT:
201                 {
202                     const Elf_Sym *def;
203                     const Obj_Entry *defobj;
204
205                     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
206                       flags, cache, lockstate);
207                     if (def == NULL)
208                         goto done;
209
210                     *where = (Elf_Addr) (defobj->relocbase + def->st_value);
211                 }
212                 break;
213
214             case R_386_RELATIVE:
215                 *where += (Elf_Addr) obj->relocbase;
216                 break;
217
218             case R_386_TLS_TPOFF:
219             case R_386_TLS_TPOFF32:
220                 {
221                     const Elf_Sym *def;
222                     const Obj_Entry *defobj;
223                     Elf_Addr add;
224
225                     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
226                       flags, cache, lockstate);
227                     if (def == NULL)
228                         goto done;
229
230                     /*
231                      * We lazily allocate offsets for static TLS as we
232                      * see the first relocation that references the
233                      * TLS block. This allows us to support (small
234                      * amounts of) static TLS in dynamically loaded
235                      * modules. If we run out of space, we generate an
236                      * error.
237                      */
238                     if (!defobj->tls_done) {
239                         if (!allocate_tls_offset((Obj_Entry*) defobj)) {
240                             _rtld_error("%s: No space available for static "
241                                         "Thread Local Storage", obj->path);
242                             goto done;
243                         }
244                     }
245                     add = (Elf_Addr) (def->st_value - defobj->tlsoffset);
246                     if (ELF_R_TYPE(rel->r_info) == R_386_TLS_TPOFF)
247                         *where += add;
248                     else
249                         *where -= add;
250                 }
251                 break;
252
253             case R_386_TLS_DTPMOD32:
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                       flags, cache, lockstate);
260                     if (def == NULL)
261                         goto done;
262
263                     *where += (Elf_Addr) defobj->tlsindex;
264                 }
265                 break;
266
267             case R_386_TLS_DTPOFF32:
268                 {
269                     const Elf_Sym *def;
270                     const Obj_Entry *defobj;
271
272                     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
273                       flags, cache, lockstate);
274                     if (def == NULL)
275                         goto done;
276
277                     *where += (Elf_Addr) def->st_value;
278                 }
279                 break;
280
281             default:
282                 _rtld_error("%s: Unsupported relocation type %d"
283                   " in non-PLT relocations\n", obj->path,
284                   ELF_R_TYPE(rel->r_info));
285                 goto done;
286             }
287         }
288         r = 0;
289 done:
290         if (cache != NULL)
291             free(cache);
292         return (r);
293 }
294
295 /* Process the PLT relocations. */
296 int
297 reloc_plt(Obj_Entry *obj)
298 {
299     const Elf_Rel *rellim;
300     const Elf_Rel *rel;
301
302     rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
303     for (rel = obj->pltrel;  rel < rellim;  rel++) {
304         Elf_Addr *where;
305
306         switch (ELF_R_TYPE(rel->r_info)) {
307         case R_386_JMP_SLOT:
308           /* Relocate the GOT slot pointing into the PLT. */
309           where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
310           *where += (Elf_Addr)obj->relocbase;
311           break;
312
313         case R_386_IRELATIVE:
314           obj->irelative = true;
315           break;
316
317         default:
318           _rtld_error("Unknown relocation type %x in PLT",
319             ELF_R_TYPE(rel->r_info));
320           return (-1);
321         }
322     }
323     return 0;
324 }
325
326 /* Relocate the jump slots in an object. */
327 int
328 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
329 {
330     const Elf_Rel *rellim;
331     const Elf_Rel *rel;
332
333     if (obj->jmpslots_done)
334         return 0;
335     rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
336     for (rel = obj->pltrel;  rel < rellim;  rel++) {
337         Elf_Addr *where, target;
338         const Elf_Sym *def;
339         const Obj_Entry *defobj;
340
341         switch (ELF_R_TYPE(rel->r_info)) {
342         case R_386_JMP_SLOT:
343           where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
344           def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
345                 SYMLOOK_IN_PLT | flags, NULL, lockstate);
346           if (def == NULL)
347               return (-1);
348           if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
349               obj->gnu_ifunc = true;
350               continue;
351           }
352           target = (Elf_Addr)(defobj->relocbase + def->st_value);
353           reloc_jmpslot(where, target, defobj, obj, rel);
354           break;
355
356         case R_386_IRELATIVE:
357           break;
358
359         default:
360           _rtld_error("Unknown relocation type %x in PLT",
361             ELF_R_TYPE(rel->r_info));
362           return (-1);
363         }
364     }
365
366     obj->jmpslots_done = true;
367     return 0;
368 }
369
370 int
371 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
372 {
373     const Elf_Rel *rellim;
374     const Elf_Rel *rel;
375     Elf_Addr *where, target;
376
377     if (!obj->irelative)
378         return (0);
379     rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
380     for (rel = obj->pltrel;  rel < rellim;  rel++) {
381         switch (ELF_R_TYPE(rel->r_info)) {
382         case R_386_IRELATIVE:
383           where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
384           lock_release(rtld_bind_lock, lockstate);
385           target = ((Elf_Addr (*)(void))(obj->relocbase + *where))();
386           wlock_acquire(rtld_bind_lock, lockstate);
387           *where = target;
388           break;
389         }
390     }
391     obj->irelative = false;
392     return (0);
393 }
394
395 int
396 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
397 {
398     const Elf_Rel *rellim;
399     const Elf_Rel *rel;
400
401     if (!obj->gnu_ifunc)
402         return (0);
403     rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
404     for (rel = obj->pltrel;  rel < rellim;  rel++) {
405         Elf_Addr *where, target;
406         const Elf_Sym *def;
407         const Obj_Entry *defobj;
408
409         switch (ELF_R_TYPE(rel->r_info)) {
410         case R_386_JMP_SLOT:
411           where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
412           def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
413                 SYMLOOK_IN_PLT | flags, NULL, lockstate);
414           if (def == NULL)
415               return (-1);
416           if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
417               continue;
418           lock_release(rtld_bind_lock, lockstate);
419           target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
420           wlock_acquire(rtld_bind_lock, lockstate);
421           reloc_jmpslot(where, target, defobj, obj, rel);
422           break;
423         }
424     }
425
426     obj->gnu_ifunc = false;
427     return (0);
428 }
429
430 /* GNU ABI */
431 __attribute__((__regparm__(1)))
432 void *
433 ___tls_get_addr(tls_index *ti)
434 {
435     struct tls_tcb *tcb;
436
437     tcb = tls_get_tcb();
438     return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
439 }
440
441 /* Sun ABI */
442 void *
443 __tls_get_addr(tls_index *ti)
444 {
445     struct tls_tcb *tcb;
446
447     tcb = tls_get_tcb();
448     return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
449 }
450
451 /* Sun ABI */
452 void *
453 __tls_get_addr_tcb(struct tls_tcb *tcb, tls_index *ti)
454 {
455     return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
456 }