f0b78426f00cde087e7072da55ea46d0a22aa01b
[dragonfly.git] / libexec / rtld-elf / amd64 / 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: src/libexec/rtld-elf/amd64/reloc.c,v 1.18 2006/03/28 06:09:24 davidxu Exp $
26  * $DragonFly: src/libexec/rtld-elf/amd64/reloc.c,v 1.1 2006/07/27 00:40:35 corecode Exp $
27  */
28
29 /*
30  * Dynamic linker for ELF.
31  *
32  * John Polstra <jdp@polstra.com>.
33  */
34
35 #include <sys/param.h>
36 #include <sys/mman.h>
37 #include <sys/tls.h>
38
39 #include <machine/sysarch.h>
40 #include <machine/tls.h>
41
42 #include <dlfcn.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include "debug.h"
53 #include "rtld.h"
54
55 /*
56  * Process the special R_X86_64_COPY relocations in the main program.  These
57  * copy data from a shared object into a region in the main program's BSS
58  * segment.
59  *
60  * Returns 0 on success, -1 on failure.
61  */
62 int
63 do_copy_relocations(Obj_Entry *dstobj)
64 {
65     const Elf_Rela *relalim;
66     const Elf_Rela *rela;
67
68     assert(dstobj->mainprog);   /* COPY relocations are invalid elsewhere */
69
70     relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela + dstobj->relasize);
71     for (rela = dstobj->rela;  rela < relalim;  rela++) {
72         if (ELF_R_TYPE(rela->r_info) == R_X86_64_COPY) {
73             void *dstaddr;
74             const Elf_Sym *dstsym;
75             const char *name;
76             unsigned long hash;
77             size_t size;
78             const void *srcaddr;
79             const Elf_Sym *srcsym;
80             Obj_Entry *srcobj;
81
82             dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
83             dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
84             name = dstobj->strtab + dstsym->st_name;
85             hash = elf_hash(name);
86             size = dstsym->st_size;
87
88             for (srcobj = dstobj->next;  srcobj != NULL;  srcobj = srcobj->next)
89                 if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL)
90                     break;
91
92             if (srcobj == NULL) {
93                 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
94                   " relocation in %s", name, dstobj->path);
95                 return -1;
96             }
97
98             srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
99             memcpy(dstaddr, srcaddr, size);
100         }
101     }
102
103     return 0;
104 }
105
106 /* Initialize the special GOT entries. */
107 void
108 init_pltgot(Obj_Entry *obj)
109 {
110     if (obj->pltgot != NULL) {
111         obj->pltgot[1] = (Elf_Addr) obj;
112         obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
113     }
114 }
115
116 /* Process the non-PLT relocations. */
117 int
118 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
119 {
120         const Elf_Rela *relalim;
121         const Elf_Rela *rela;
122         SymCache *cache;
123         int bytes = obj->nchains * sizeof(SymCache);
124         int r = -1;
125
126         /*
127          * The dynamic loader may be called from a thread, we have
128          * limited amounts of stack available so we cannot use alloca().
129          */
130         cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
131         if (cache == MAP_FAILED)
132             cache = NULL;
133
134         relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize);
135         for (rela = obj->rela;  rela < relalim;  rela++) {
136             Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
137             Elf32_Addr *where32 = (Elf32_Addr *)where;
138
139             switch (ELF_R_TYPE(rela->r_info)) {
140
141             case R_X86_64_NONE:
142                 break;
143
144             case R_X86_64_64:
145                 {
146                     const Elf_Sym *def;
147                     const Obj_Entry *defobj;
148
149                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
150                       false, cache);
151                     if (def == NULL)
152                         goto done;
153
154                     *where = (Elf_Addr) (defobj->relocbase + def->st_value + rela->r_addend);
155                 }
156                 break;
157
158             case R_X86_64_PC32:
159                 /*
160                  * I don't think the dynamic linker should ever see this
161                  * type of relocation.  But the binutils-2.6 tools sometimes
162                  * generate it.
163                  */
164                 {
165                     const Elf_Sym *def;
166                     const Obj_Entry *defobj;
167
168                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
169                       false, cache);
170                     if (def == NULL)
171                         goto done;
172
173                     *where32 = (Elf32_Addr) (unsigned long) (defobj->relocbase +
174                         def->st_value + rela->r_addend - (Elf_Addr) where);
175                 }
176                 break;
177         /* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
178
179             case R_X86_64_COPY:
180                 /*
181                  * These are deferred until all other relocations have
182                  * been done.  All we do here is make sure that the COPY
183                  * relocation is not in a shared library.  They are allowed
184                  * only in executable files.
185                  */
186                 if (!obj->mainprog) {
187                     _rtld_error("%s: Unexpected R_X86_64_COPY relocation"
188                       " in shared library", obj->path);
189                     goto done;
190                 }
191                 break;
192
193             case R_X86_64_GLOB_DAT:
194                 {
195                     const Elf_Sym *def;
196                     const Obj_Entry *defobj;
197
198                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
199                       false, cache);
200                     if (def == NULL)
201                         goto done;
202
203                     *where = (Elf_Addr) (defobj->relocbase + def->st_value);
204                 }
205                 break;
206
207             case R_X86_64_TPOFF64:
208                 {
209                     const Elf_Sym *def;
210                     const Obj_Entry *defobj;
211
212                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
213                       false, cache);
214                     if (def == NULL)
215                         goto done;
216
217                     /*
218                      * We lazily allocate offsets for static TLS as we
219                      * see the first relocation that references the
220                      * TLS block. This allows us to support (small
221                      * amounts of) static TLS in dynamically loaded
222                      * modules. If we run out of space, we generate an
223                      * error.
224                      */
225                     if (!defobj->tls_done) {
226                         if (!allocate_tls_offset((Obj_Entry*) defobj)) {
227                             _rtld_error("%s: No space available for static "
228                                         "Thread Local Storage", obj->path);
229                             goto done;
230                         }
231                     }
232
233                     *where = (Elf_Addr) (def->st_value - defobj->tlsoffset +
234                                          rela->r_addend);
235                 }
236                 break;
237
238             case R_X86_64_TPOFF32:
239                 {
240                     const Elf_Sym *def;
241                     const Obj_Entry *defobj;
242
243                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
244                       false, cache);
245                     if (def == NULL)
246                         goto done;
247
248                     /*
249                      * We lazily allocate offsets for static TLS as we
250                      * see the first relocation that references the
251                      * TLS block. This allows us to support (small
252                      * amounts of) static TLS in dynamically loaded
253                      * modules. If we run out of space, we generate an
254                      * error.
255                      */
256                     if (!defobj->tls_done) {
257                         if (!allocate_tls_offset((Obj_Entry*) defobj)) {
258                             _rtld_error("%s: No space available for static "
259                                         "Thread Local Storage", obj->path);
260                             goto done;
261                         }
262                     }
263
264                     *where32 = (Elf32_Addr) (def->st_value -
265                                              defobj->tlsoffset +
266                                              rela->r_addend);
267                 }
268                 break;
269
270             case R_X86_64_DTPMOD64:
271                 {
272                     const Elf_Sym *def;
273                     const Obj_Entry *defobj;
274
275                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
276                       false, cache);
277                     if (def == NULL)
278                         goto done;
279
280                     *where += (Elf_Addr) defobj->tlsindex;
281                 }
282                 break;
283
284             case R_X86_64_DTPOFF64:
285                 {
286                     const Elf_Sym *def;
287                     const Obj_Entry *defobj;
288
289                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
290                       false, cache);
291                     if (def == NULL)
292                         goto done;
293
294                     *where += (Elf_Addr) (def->st_value + rela->r_addend);
295                 }
296                 break;
297
298             case R_X86_64_DTPOFF32:
299                 {
300                     const Elf_Sym *def;
301                     const Obj_Entry *defobj;
302
303                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
304                       false, cache);
305                     if (def == NULL)
306                         goto done;
307
308                     *where32 += (Elf32_Addr) (def->st_value + rela->r_addend);
309                 }
310                 break;
311
312             case R_X86_64_RELATIVE:
313                 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
314                 break;
315
316         /* 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 */
317
318             default:
319                 _rtld_error("%s: Unsupported relocation type %u"
320                   " in non-PLT relocations\n", obj->path,
321                   (unsigned int)ELF_R_TYPE(rela->r_info));
322                 goto done;
323             }
324         }
325         r = 0;
326 done:
327         if (cache)
328             munmap(cache, bytes);
329         return(r);
330 }
331
332 /* Process the PLT relocations. */
333 int
334 reloc_plt(Obj_Entry *obj)
335 {
336     const Elf_Rela *relalim;
337     const Elf_Rela *rela;
338
339     relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
340     for (rela = obj->pltrela;  rela < relalim;  rela++) {
341         Elf_Addr *where;
342
343         assert(ELF_R_TYPE(rela->r_info) == R_X86_64_JMP_SLOT);
344
345         /* Relocate the GOT slot pointing into the PLT. */
346         where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
347         *where += (Elf_Addr)obj->relocbase;
348     }
349     return 0;
350 }
351
352 /* Relocate the jump slots in an object. */
353 int
354 reloc_jmpslots(Obj_Entry *obj)
355 {
356     const Elf_Rela *relalim;
357     const Elf_Rela *rela;
358
359     if (obj->jmpslots_done)
360         return 0;
361     relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
362     for (rela = obj->pltrela;  rela < relalim;  rela++) {
363         Elf_Addr *where, target;
364         const Elf_Sym *def;
365         const Obj_Entry *defobj;
366
367         assert(ELF_R_TYPE(rela->r_info) == R_X86_64_JMP_SLOT);
368         where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
369         def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true, NULL);
370         if (def == NULL)
371             return -1;
372         target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
373         reloc_jmpslot(where, target);
374     }
375     obj->jmpslots_done = true;
376     return 0;
377 }
378
379 void *__tls_get_addr(tls_index *ti)
380 {
381     struct tls_tcb *tcb;
382
383     tcb = tls_get_tcb();
384     return tls_get_addr_common(&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
385 }
386
387 void *
388 __tls_get_addr_tcb(struct tls_tcb *tcb, tls_index *ti)
389 {
390     return tls_get_addr_common(&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
391 }