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