Merge branch 'vendor/DIFFUTILS'
[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: src/libexec/rtld-elf/i386/reloc.c,v 1.22 2010/12/25 08:51:20 kib Exp $
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 *) ((c_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             unsigned long hash;
75             size_t size;
76             const void *srcaddr;
77             const Elf_Sym *srcsym;
78             const Ver_Entry *ve;
79             Obj_Entry *srcobj;
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             hash = elf_hash(name);
85             size = dstsym->st_size;
86             ve = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info));
87
88             for (srcobj = dstobj->next;  srcobj != NULL;  srcobj = srcobj->next)
89                 if ((srcsym = symlook_obj(name, hash, srcobj, ve, 0)) != 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_Rel *rellim;
121         const Elf_Rel *rel;
122         SymCache *cache;
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         if (obj != obj_rtld) {
130             cache = calloc(obj->nchains, sizeof(SymCache));
131             /* No need to check for NULL here */
132         } else
133             cache = NULL;
134
135         rellim = (const Elf_Rel *) ((c_caddr_t) obj->rel + obj->relsize);
136         for (rel = obj->rel;  rel < rellim;  rel++) {
137             Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
138
139             switch (ELF_R_TYPE(rel->r_info)) {
140
141             case R_386_NONE:
142                 break;
143
144             case R_386_32:
145                 {
146                     const Elf_Sym *def;
147                     const Obj_Entry *defobj;
148
149                     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
150                       false, cache);
151                     if (def == NULL)
152                         goto done;
153
154                     *where += (Elf_Addr) (defobj->relocbase + def->st_value);
155                 }
156                 break;
157
158             case R_386_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(rel->r_info), obj, &defobj,
169                       false, cache);
170                     if (def == NULL)
171                         goto done;
172
173                     *where +=
174                       (Elf_Addr) (defobj->relocbase + def->st_value) -
175                       (Elf_Addr) where;
176                 }
177                 break;
178
179             case R_386_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_386_COPY relocation"
188                       " in shared library", obj->path);
189                     goto done;
190                 }
191                 break;
192
193             case R_386_GLOB_DAT:
194                 {
195                     const Elf_Sym *def;
196                     const Obj_Entry *defobj;
197
198                     def = find_symdef(ELF_R_SYM(rel->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_386_RELATIVE:
208                 *where += (Elf_Addr) obj->relocbase;
209                 break;
210
211             case R_386_TLS_TPOFF:
212                 {
213                     const Elf_Sym *def;
214                     const Obj_Entry *defobj;
215
216                     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
217                       false, cache);
218                     if (def == NULL)
219                         goto done;
220
221                     /*
222                      * We lazily allocate offsets for static TLS as we
223                      * see the first relocation that references the
224                      * TLS block. This allows us to support (small
225                      * amounts of) static TLS in dynamically loaded
226                      * modules. If we run out of space, we generate an
227                      * error.
228                      */
229                     if (!defobj->tls_done) {
230                         if (!allocate_tls_offset((Obj_Entry*) defobj)) {
231                             _rtld_error("%s: No space available for static "
232                                         "Thread Local Storage", obj->path);
233                             goto done;
234                         }
235                     }
236
237                     *where += (Elf_Addr) (def->st_value - defobj->tlsoffset);
238                 }
239                 break;
240
241             case R_386_TLS_TPOFF32:
242                 {
243                     const Elf_Sym *def;
244                     const Obj_Entry *defobj;
245
246                     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
247                       false, cache);
248                     if (def == NULL)
249                         goto done;
250
251                     /*
252                      * We lazily allocate offsets for static TLS as we
253                      * see the first relocation that references the
254                      * TLS block. This allows us to support (small
255                      * amounts of) static TLS in dynamically loaded
256                      * modules. If we run out of space, we generate an
257                      * error.
258                      */
259                     if (!defobj->tls_done) {
260                         if (!allocate_tls_offset((Obj_Entry*) defobj)) {
261                             _rtld_error("%s: No space available for static "
262                                         "Thread Local Storage", obj->path);
263                             goto done;
264                         }
265                     }
266
267                     *where += (Elf_Addr) (defobj->tlsoffset - def->st_value);
268                 }
269                 break;
270
271             case R_386_TLS_DTPMOD32:
272                 {
273                     const Elf_Sym *def;
274                     const Obj_Entry *defobj;
275
276                     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
277                       false, cache);
278                     if (def == NULL)
279                         goto done;
280
281                     *where += (Elf_Addr) defobj->tlsindex;
282                 }
283                 break;
284
285             case R_386_TLS_DTPOFF32:
286                 {
287                     const Elf_Sym *def;
288                     const Obj_Entry *defobj;
289
290                     def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
291                       false, cache);
292                     if (def == NULL)
293                         goto done;
294
295                     *where += (Elf_Addr) def->st_value;
296                 }
297                 break;
298
299             default:
300                 _rtld_error("%s: Unsupported relocation type %d"
301                   " in non-PLT relocations\n", obj->path,
302                   ELF_R_TYPE(rel->r_info));
303                 goto done;
304             }
305         }
306         r = 0;
307 done:
308         if (cache != NULL)
309             free(cache);
310         return(r);
311 }
312
313 /* Process the PLT relocations. */
314 int
315 reloc_plt(Obj_Entry *obj)
316 {
317     const Elf_Rel *rellim;
318     const Elf_Rel *rel;
319
320     rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
321     for (rel = obj->pltrel;  rel < rellim;  rel++) {
322         Elf_Addr *where;
323
324         assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
325
326         /* Relocate the GOT slot pointing into the PLT. */
327         where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
328         *where += (Elf_Addr)obj->relocbase;
329     }
330     return 0;
331 }
332
333 /* Relocate the jump slots in an object. */
334 int
335 reloc_jmpslots(Obj_Entry *obj)
336 {
337     const Elf_Rel *rellim;
338     const Elf_Rel *rel;
339
340     if (obj->jmpslots_done)
341         return 0;
342     rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
343     for (rel = obj->pltrel;  rel < rellim;  rel++) {
344         Elf_Addr *where, target;
345         const Elf_Sym *def;
346         const Obj_Entry *defobj;
347
348         assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
349         where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
350         def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
351         if (def == NULL)
352             return -1;
353         target = (Elf_Addr)(defobj->relocbase + def->st_value);
354         reloc_jmpslot(where, target, defobj, obj, rel);
355     }
356     obj->jmpslots_done = true;
357     return 0;
358 }
359
360 /* GNU ABI */
361 __attribute__((__regparm__(1)))
362 void *
363 ___tls_get_addr(tls_index *ti)
364 {
365     struct tls_tcb *tcb;
366
367     tcb = tls_get_tcb();
368     return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
369 }
370
371 /* Sun ABI */
372 void *
373 __tls_get_addr(tls_index *ti)
374 {
375     struct tls_tcb *tcb;
376
377     tcb = tls_get_tcb();
378     return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
379 }
380
381 /* Sun ABI */
382 void *
383 __tls_get_addr_tcb(struct tls_tcb *tcb, tls_index *ti)
384 {
385     return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
386 }
387