]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/amd64/amd64/elf_machdep.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / amd64 / amd64 / elf_machdep.c
1 /*-
2  * Copyright 1996-1998 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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/exec.h>
33 #include <sys/imgact.h>
34 #include <sys/linker.h>
35 #include <sys/sysent.h>
36 #include <sys/imgact_elf.h>
37 #include <sys/syscall.h>
38 #include <sys/sysent.h>
39 #include <sys/signalvar.h>
40 #include <sys/vnode.h>
41
42 #include <vm/vm.h>
43 #include <vm/pmap.h>
44 #include <vm/vm_param.h>
45
46 #include <machine/elf.h>
47 #include <machine/md_var.h>
48
49 struct sysentvec elf64_freebsd_sysvec = {
50         .sv_size        = SYS_MAXSYSCALL,
51         .sv_table       = sysent,
52         .sv_mask        = 0,
53         .sv_sigsize     = 0,
54         .sv_sigtbl      = NULL,
55         .sv_errsize     = 0,
56         .sv_errtbl      = NULL,
57         .sv_transtrap   = NULL,
58         .sv_fixup       = __elfN(freebsd_fixup),
59         .sv_sendsig     = sendsig,
60         .sv_sigcode     = sigcode,
61         .sv_szsigcode   = &szsigcode,
62         .sv_prepsyscall = NULL,
63         .sv_name        = "FreeBSD ELF64",
64         .sv_coredump    = __elfN(coredump),
65         .sv_imgact_try  = NULL,
66         .sv_minsigstksz = MINSIGSTKSZ,
67         .sv_pagesize    = PAGE_SIZE,
68         .sv_minuser     = VM_MIN_ADDRESS,
69         .sv_maxuser     = VM_MAXUSER_ADDRESS,
70         .sv_usrstack    = USRSTACK,
71         .sv_psstrings   = PS_STRINGS,
72         .sv_stackprot   = VM_PROT_ALL,
73         .sv_copyout_strings     = exec_copyout_strings,
74         .sv_setregs     = exec_setregs,
75         .sv_fixlimit    = NULL,
76         .sv_maxssiz     = NULL,
77         .sv_flags       = SV_ABI_FREEBSD | SV_LP64
78 };
79
80 static Elf64_Brandinfo freebsd_brand_info = {
81         .brand          = ELFOSABI_FREEBSD,
82         .machine        = EM_X86_64,
83         .compat_3_brand = "FreeBSD",
84         .emul_path      = NULL,
85         .interp_path    = "/libexec/ld-elf.so.1",
86         .sysvec         = &elf64_freebsd_sysvec,
87         .interp_newpath = NULL,
88         .brand_note     = &elf64_freebsd_brandnote,
89         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
90 };
91
92 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
93         (sysinit_cfunc_t) elf64_insert_brand_entry,
94         &freebsd_brand_info);
95
96 static Elf64_Brandinfo freebsd_brand_oinfo = {
97         .brand          = ELFOSABI_FREEBSD,
98         .machine        = EM_X86_64,
99         .compat_3_brand = "FreeBSD",
100         .emul_path      = NULL,
101         .interp_path    = "/usr/libexec/ld-elf.so.1",
102         .sysvec         = &elf64_freebsd_sysvec,
103         .interp_newpath = NULL,
104         .brand_note     = &elf64_freebsd_brandnote,
105         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
106 };
107
108 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
109         (sysinit_cfunc_t) elf64_insert_brand_entry,
110         &freebsd_brand_oinfo);
111
112 static Elf64_Brandinfo kfreebsd_brand_info = {
113         .brand          = ELFOSABI_FREEBSD,
114         .machine        = EM_X86_64,
115         .compat_3_brand = "FreeBSD",
116         .emul_path      = NULL,
117         .interp_path    = "/lib/ld-kfreebsd-x86-64.so.1",
118         .sysvec         = &elf64_freebsd_sysvec,
119         .interp_newpath = NULL,
120         .brand_note     = &elf64_kfreebsd_brandnote,
121         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
122 };
123
124 SYSINIT(kelf64, SI_SUB_EXEC, SI_ORDER_ANY,
125         (sysinit_cfunc_t) elf64_insert_brand_entry,
126         &kfreebsd_brand_info);
127
128
129 void
130 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
131     size_t *off __unused)
132 {
133 }
134
135
136 /* Process one elf relocation with addend. */
137 static int
138 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
139     int type, int local, elf_lookup_fn lookup)
140 {
141         Elf64_Addr *where, val;
142         Elf32_Addr *where32, val32;
143         Elf_Addr addr;
144         Elf_Addr addend;
145         Elf_Size rtype, symidx;
146         const Elf_Rel *rel;
147         const Elf_Rela *rela;
148
149         switch (type) {
150         case ELF_RELOC_REL:
151                 rel = (const Elf_Rel *)data;
152                 where = (Elf_Addr *) (relocbase + rel->r_offset);
153                 rtype = ELF_R_TYPE(rel->r_info);
154                 symidx = ELF_R_SYM(rel->r_info);
155                 /* Addend is 32 bit on 32 bit relocs */
156                 switch (rtype) {
157                 case R_X86_64_PC32:
158                 case R_X86_64_32S:
159                         addend = *(Elf32_Addr *)where;
160                         break;
161                 default:
162                         addend = *where;
163                         break;
164                 }
165                 break;
166         case ELF_RELOC_RELA:
167                 rela = (const Elf_Rela *)data;
168                 where = (Elf_Addr *) (relocbase + rela->r_offset);
169                 addend = rela->r_addend;
170                 rtype = ELF_R_TYPE(rela->r_info);
171                 symidx = ELF_R_SYM(rela->r_info);
172                 break;
173         default:
174                 panic("unknown reloc type %d\n", type);
175         }
176
177         switch (rtype) {
178
179                 case R_X86_64_NONE:     /* none */
180                         break;
181
182                 case R_X86_64_64:               /* S + A */
183                         addr = lookup(lf, symidx, 1);
184                         val = addr + addend;
185                         if (addr == 0)
186                                 return -1;
187                         if (*where != val)
188                                 *where = val;
189                         break;
190
191                 case R_X86_64_PC32:     /* S + A - P */
192                         addr = lookup(lf, symidx, 1);
193                         where32 = (Elf32_Addr *)where;
194                         val32 = (Elf32_Addr)(addr + addend - (Elf_Addr)where);
195                         if (addr == 0)
196                                 return -1;
197                         if (*where32 != val32)
198                                 *where32 = val32;
199                         break;
200
201                 case R_X86_64_32S:      /* S + A sign extend */
202                         addr = lookup(lf, symidx, 1);
203                         val32 = (Elf32_Addr)(addr + addend);
204                         where32 = (Elf32_Addr *)where;
205                         if (addr == 0)
206                                 return -1;
207                         if (*where32 != val32)
208                                 *where32 = val32;
209                         break;
210
211                 case R_X86_64_COPY:     /* none */
212                         /*
213                          * There shouldn't be copy relocations in kernel
214                          * objects.
215                          */
216                         printf("kldload: unexpected R_COPY relocation\n");
217                         return -1;
218                         break;
219
220                 case R_X86_64_GLOB_DAT: /* S */
221                 case R_X86_64_JMP_SLOT: /* XXX need addend + offset */
222                         addr = lookup(lf, symidx, 1);
223                         if (addr == 0)
224                                 return -1;
225                         if (*where != addr)
226                                 *where = addr;
227                         break;
228
229                 case R_X86_64_RELATIVE: /* B + A */
230                         addr = relocbase + addend;
231                         val = addr;
232                         if (*where != val)
233                                 *where = val;
234                         break;
235
236                 default:
237                         printf("kldload: unexpected relocation type %ld\n",
238                                rtype);
239                         return -1;
240         }
241         return(0);
242 }
243
244 int
245 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
246     elf_lookup_fn lookup)
247 {
248
249         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
250 }
251
252 int
253 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
254     int type, elf_lookup_fn lookup)
255 {
256
257         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
258 }
259
260 int
261 elf_cpu_load_file(linker_file_t lf __unused)
262 {
263
264         return (0);
265 }
266
267 int
268 elf_cpu_unload_file(linker_file_t lf __unused)
269 {
270
271         return (0);
272 }