]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - libexec/rtld-elf/amd64/reloc.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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$
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/sysarch.h>
37
38 #include <dlfcn.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdarg.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #include "debug.h"
49 #include "rtld.h"
50
51 /*
52  * Process the special R_X86_64_COPY relocations in the main program.  These
53  * copy data from a shared object into a region in the main program's BSS
54  * segment.
55  *
56  * Returns 0 on success, -1 on failure.
57  */
58 int
59 do_copy_relocations(Obj_Entry *dstobj)
60 {
61     const Elf_Rela *relalim;
62     const Elf_Rela *rela;
63
64     assert(dstobj->mainprog);   /* COPY relocations are invalid elsewhere */
65
66     relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela + dstobj->relasize);
67     for (rela = dstobj->rela;  rela < relalim;  rela++) {
68         if (ELF_R_TYPE(rela->r_info) == R_X86_64_COPY) {
69             void *dstaddr;
70             const Elf_Sym *dstsym;
71             const char *name;
72             unsigned long hash;
73             size_t size;
74             const void *srcaddr;
75             const Elf_Sym *srcsym;
76             Obj_Entry *srcobj;
77             const Ver_Entry *ve;
78
79             dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
80             dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
81             name = dstobj->strtab + dstsym->st_name;
82             hash = elf_hash(name);
83             size = dstsym->st_size;
84             ve = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
85
86             for (srcobj = dstobj->next;  srcobj != NULL;  srcobj = srcobj->next)
87                 if ((srcsym = symlook_obj(name, hash, srcobj, ve, 0)) != NULL)
88                     break;
89
90             if (srcobj == NULL) {
91                 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
92                   " relocation in %s", name, dstobj->path);
93                 return -1;
94             }
95
96             srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
97             memcpy(dstaddr, srcaddr, size);
98         }
99     }
100
101     return 0;
102 }
103
104 /* Initialize the special GOT entries. */
105 void
106 init_pltgot(Obj_Entry *obj)
107 {
108     if (obj->pltgot != NULL) {
109         obj->pltgot[1] = (Elf_Addr) obj;
110         obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
111     }
112 }
113
114 /* Process the non-PLT relocations. */
115 int
116 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
117 {
118         const Elf_Rela *relalim;
119         const Elf_Rela *rela;
120         SymCache *cache;
121         int bytes = obj->nchains * sizeof(SymCache);
122         int r = -1;
123
124         /*
125          * The dynamic loader may be called from a thread, we have
126          * limited amounts of stack available so we cannot use alloca().
127          */
128         cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
129         if (cache == MAP_FAILED)
130             cache = NULL;
131
132         relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize);
133         for (rela = obj->rela;  rela < relalim;  rela++) {
134             Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
135             Elf32_Addr *where32 = (Elf32_Addr *)where;
136
137             switch (ELF_R_TYPE(rela->r_info)) {
138
139             case R_X86_64_NONE:
140                 break;
141
142             case R_X86_64_64:
143                 {
144                     const Elf_Sym *def;
145                     const Obj_Entry *defobj;
146
147                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
148                       false, cache);
149                     if (def == NULL)
150                         goto done;
151
152                     *where = (Elf_Addr) (defobj->relocbase + def->st_value + rela->r_addend);
153                 }
154                 break;
155
156             case R_X86_64_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(rela->r_info), obj, &defobj,
167                       false, cache);
168                     if (def == NULL)
169                         goto done;
170
171                     *where32 = (Elf32_Addr) (unsigned long) (defobj->relocbase +
172                         def->st_value + rela->r_addend - (Elf_Addr) where);
173                 }
174                 break;
175         /* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
176
177             case R_X86_64_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_X86_64_COPY relocation"
186                       " in shared library", obj->path);
187                     goto done;
188                 }
189                 break;
190
191             case R_X86_64_GLOB_DAT:
192                 {
193                     const Elf_Sym *def;
194                     const Obj_Entry *defobj;
195
196                     def = find_symdef(ELF_R_SYM(rela->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_X86_64_TPOFF64:
206                 {
207                     const Elf_Sym *def;
208                     const Obj_Entry *defobj;
209
210                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
211                       false, cache);
212                     if (def == NULL)
213                         goto done;
214
215                     /*
216                      * We lazily allocate offsets for static TLS as we
217                      * see the first relocation that references the
218                      * TLS block. This allows us to support (small
219                      * amounts of) static TLS in dynamically loaded
220                      * modules. If we run out of space, we generate an
221                      * error.
222                      */
223                     if (!defobj->tls_done) {
224                         if (!allocate_tls_offset((Obj_Entry*) defobj)) {
225                             _rtld_error("%s: No space available for static "
226                                         "Thread Local Storage", obj->path);
227                             goto done;
228                         }
229                     }
230
231                     *where = (Elf_Addr) (def->st_value - defobj->tlsoffset +
232                                          rela->r_addend);
233                 }
234                 break;
235
236             case R_X86_64_TPOFF32:
237                 {
238                     const Elf_Sym *def;
239                     const Obj_Entry *defobj;
240
241                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
242                       false, cache);
243                     if (def == NULL)
244                         goto done;
245
246                     /*
247                      * We lazily allocate offsets for static TLS as we
248                      * see the first relocation that references the
249                      * TLS block. This allows us to support (small
250                      * amounts of) static TLS in dynamically loaded
251                      * modules. If we run out of space, we generate an
252                      * error.
253                      */
254                     if (!defobj->tls_done) {
255                         if (!allocate_tls_offset((Obj_Entry*) defobj)) {
256                             _rtld_error("%s: No space available for static "
257                                         "Thread Local Storage", obj->path);
258                             goto done;
259                         }
260                     }
261
262                     *where32 = (Elf32_Addr) (def->st_value -
263                                              defobj->tlsoffset +
264                                              rela->r_addend);
265                 }
266                 break;
267
268             case R_X86_64_DTPMOD64:
269                 {
270                     const Elf_Sym *def;
271                     const Obj_Entry *defobj;
272
273                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
274                       false, cache);
275                     if (def == NULL)
276                         goto done;
277
278                     *where += (Elf_Addr) defobj->tlsindex;
279                 }
280                 break;
281
282             case R_X86_64_DTPOFF64:
283                 {
284                     const Elf_Sym *def;
285                     const Obj_Entry *defobj;
286
287                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
288                       false, cache);
289                     if (def == NULL)
290                         goto done;
291
292                     *where += (Elf_Addr) (def->st_value + rela->r_addend);
293                 }
294                 break;
295
296             case R_X86_64_DTPOFF32:
297                 {
298                     const Elf_Sym *def;
299                     const Obj_Entry *defobj;
300
301                     def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
302                       false, cache);
303                     if (def == NULL)
304                         goto done;
305
306                     *where32 += (Elf32_Addr) (def->st_value + rela->r_addend);
307                 }
308                 break;
309
310             case R_X86_64_RELATIVE:
311                 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
312                 break;
313
314         /* 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 */
315
316             default:
317                 _rtld_error("%s: Unsupported relocation type %u"
318                   " in non-PLT relocations\n", obj->path,
319                   (unsigned int)ELF_R_TYPE(rela->r_info));
320                 goto done;
321             }
322         }
323         r = 0;
324 done:
325         if (cache)
326             munmap(cache, bytes);
327         return(r);
328 }
329
330 /* Process the PLT relocations. */
331 int
332 reloc_plt(Obj_Entry *obj)
333 {
334     const Elf_Rela *relalim;
335     const Elf_Rela *rela;
336
337     relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
338     for (rela = obj->pltrela;  rela < relalim;  rela++) {
339         Elf_Addr *where;
340
341         assert(ELF_R_TYPE(rela->r_info) == R_X86_64_JMP_SLOT);
342
343         /* Relocate the GOT slot pointing into the PLT. */
344         where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
345         *where += (Elf_Addr)obj->relocbase;
346     }
347     return 0;
348 }
349
350 /* Relocate the jump slots in an object. */
351 int
352 reloc_jmpslots(Obj_Entry *obj)
353 {
354     const Elf_Rela *relalim;
355     const Elf_Rela *rela;
356
357     if (obj->jmpslots_done)
358         return 0;
359     relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
360     for (rela = obj->pltrela;  rela < relalim;  rela++) {
361         Elf_Addr *where, target;
362         const Elf_Sym *def;
363         const Obj_Entry *defobj;
364
365         assert(ELF_R_TYPE(rela->r_info) == R_X86_64_JMP_SLOT);
366         where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
367         def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true, NULL);
368         if (def == NULL)
369             return -1;
370         target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
371         reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
372     }
373     obj->jmpslots_done = true;
374     return 0;
375 }
376
377 void
378 allocate_initial_tls(Obj_Entry *objs)
379 {
380     /*
381      * Fix the size of the static TLS block by using the maximum
382      * offset allocated so far and adding a bit for dynamic modules to
383      * use.
384      */
385     tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
386     amd64_set_fsbase(allocate_tls(objs, 0,
387                                   3*sizeof(Elf_Addr), sizeof(Elf_Addr)));
388 }
389
390 void *__tls_get_addr(tls_index *ti)
391 {
392     Elf_Addr** segbase;
393     Elf_Addr* dtv;
394
395     __asm __volatile("movq %%fs:0, %0" : "=r" (segbase));
396     dtv = segbase[1];
397
398     return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);
399 }