]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/amd64/amd64/elf_machdep.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / 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/signalvar.h>
39 #include <sys/vnode.h>
40
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 #include <vm/vm_param.h>
44
45 #include <machine/elf.h>
46 #include <machine/md_var.h>
47
48 struct sysentvec elf64_freebsd_sysvec = {
49         SYS_MAXSYSCALL,
50         sysent,
51         0,
52         0,
53         NULL,
54         0,
55         NULL,
56         NULL,
57         __elfN(freebsd_fixup),
58         sendsig,
59         sigcode,
60         &szsigcode,
61         NULL,
62         "FreeBSD ELF64",
63         __elfN(coredump),
64         NULL,
65         MINSIGSTKSZ,
66         PAGE_SIZE,
67         VM_MIN_ADDRESS,
68         VM_MAXUSER_ADDRESS,
69         USRSTACK,
70         PS_STRINGS,
71         VM_PROT_ALL,
72         exec_copyout_strings,
73         exec_setregs,
74         NULL
75 };
76
77 static Elf64_Brandinfo freebsd_brand_info = {
78                                                 ELFOSABI_FREEBSD,
79                                                 EM_X86_64,
80                                                 "FreeBSD",
81                                                 NULL,
82                                                 "/libexec/ld-elf.so.1",
83                                                 &elf64_freebsd_sysvec,
84                                                 NULL,
85                                           };
86
87 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY,
88         (sysinit_cfunc_t) elf64_insert_brand_entry,
89         &freebsd_brand_info);
90
91 static Elf64_Brandinfo freebsd_brand_oinfo = {
92                                                 ELFOSABI_FREEBSD,
93                                                 EM_X86_64,
94                                                 "FreeBSD",
95                                                 NULL,
96                                                 "/usr/libexec/ld-elf.so.1",
97                                                 &elf64_freebsd_sysvec,
98                                                 NULL,
99                                           };
100
101 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
102         (sysinit_cfunc_t) elf64_insert_brand_entry,
103         &freebsd_brand_oinfo);
104
105
106 void
107 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
108     size_t *off __unused)
109 {
110 }
111
112
113 /* Process one elf relocation with addend. */
114 static int
115 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
116     int type, int local, elf_lookup_fn lookup)
117 {
118         Elf64_Addr *where, val;
119         Elf32_Addr *where32, val32;
120         Elf_Addr addr;
121         Elf_Addr addend;
122         Elf_Size rtype, symidx;
123         const Elf_Rel *rel;
124         const Elf_Rela *rela;
125
126         switch (type) {
127         case ELF_RELOC_REL:
128                 rel = (const Elf_Rel *)data;
129                 where = (Elf_Addr *) (relocbase + rel->r_offset);
130                 rtype = ELF_R_TYPE(rel->r_info);
131                 symidx = ELF_R_SYM(rel->r_info);
132                 /* Addend is 32 bit on 32 bit relocs */
133                 switch (rtype) {
134                 case R_X86_64_PC32:
135                 case R_X86_64_32S:
136                         addend = *(Elf32_Addr *)where;
137                         break;
138                 default:
139                         addend = *where;
140                         break;
141                 }
142                 break;
143         case ELF_RELOC_RELA:
144                 rela = (const Elf_Rela *)data;
145                 where = (Elf_Addr *) (relocbase + rela->r_offset);
146                 addend = rela->r_addend;
147                 rtype = ELF_R_TYPE(rela->r_info);
148                 symidx = ELF_R_SYM(rela->r_info);
149                 break;
150         default:
151                 panic("unknown reloc type %d\n", type);
152         }
153
154         switch (rtype) {
155
156                 case R_X86_64_NONE:     /* none */
157                         break;
158
159                 case R_X86_64_64:               /* S + A */
160                         addr = lookup(lf, symidx, 1);
161                         val = addr + addend;
162                         if (addr == 0)
163                                 return -1;
164                         if (*where != val)
165                                 *where = val;
166                         break;
167
168                 case R_X86_64_PC32:     /* S + A - P */
169                         addr = lookup(lf, symidx, 1);
170                         where32 = (Elf32_Addr *)where;
171                         val32 = (Elf32_Addr)(addr + addend - (Elf_Addr)where);
172                         if (addr == 0)
173                                 return -1;
174                         if (*where32 != val32)
175                                 *where32 = val32;
176                         break;
177
178                 case R_X86_64_32S:      /* S + A sign extend */
179                         addr = lookup(lf, symidx, 1);
180                         val32 = (Elf32_Addr)(addr + addend);
181                         where32 = (Elf32_Addr *)where;
182                         if (addr == 0)
183                                 return -1;
184                         if (*where32 != val32)
185                                 *where32 = val32;
186                         break;
187
188                 case R_X86_64_COPY:     /* none */
189                         /*
190                          * There shouldn't be copy relocations in kernel
191                          * objects.
192                          */
193                         printf("kldload: unexpected R_COPY relocation\n");
194                         return -1;
195                         break;
196
197                 case R_X86_64_GLOB_DAT: /* S */
198                         addr = lookup(lf, symidx, 1);
199                         if (addr == 0)
200                                 return -1;
201                         if (*where != addr)
202                                 *where = addr;
203                         break;
204
205                 case R_X86_64_RELATIVE: /* B + A */
206                         addr = relocbase + addend;
207                         val = addr;
208                         if (*where != val)
209                                 *where = val;
210                         break;
211
212                 default:
213                         printf("kldload: unexpected relocation type %ld\n",
214                                rtype);
215                         return -1;
216         }
217         return(0);
218 }
219
220 int
221 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
222     elf_lookup_fn lookup)
223 {
224
225         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
226 }
227
228 int
229 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
230     int type, elf_lookup_fn lookup)
231 {
232
233         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
234 }
235
236 int
237 elf_cpu_load_file(linker_file_t lf __unused)
238 {
239
240         return (0);
241 }
242
243 int
244 elf_cpu_unload_file(linker_file_t lf __unused)
245 {
246
247         return (0);
248 }