]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/ia64/ia64/elf_machdep.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / sys / ia64 / ia64 / 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  * $FreeBSD$
26  */
27
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
31 #include <sys/exec.h>
32 #include <sys/imgact.h>
33 #include <sys/malloc.h>
34 #include <sys/proc.h>
35 #include <sys/namei.h>
36 #include <sys/fcntl.h>
37 #include <sys/vnode.h>
38 #include <sys/linker.h>
39 #include <sys/sysent.h>
40 #include <sys/imgact_elf.h>
41 #include <sys/syscall.h>
42 #include <sys/signalvar.h>
43
44 #include <vm/vm.h>
45 #include <vm/vm_param.h>
46
47 #include <machine/elf.h>
48 #include <machine/frame.h>
49 #include <machine/md_var.h>
50 #include <machine/unwind.h>
51
52 Elf_Addr link_elf_get_gp(linker_file_t);
53
54 extern Elf_Addr fptr_storage[];
55
56 struct sysentvec elf64_freebsd_sysvec = {
57         SYS_MAXSYSCALL,
58         sysent,
59         0,
60         0,
61         NULL,
62         0,
63         NULL,
64         NULL,
65         __elfN(freebsd_fixup),
66         sendsig,
67         NULL,           /* sigcode */
68         NULL,           /* &szsigcode */
69         NULL,
70         "FreeBSD ELF64",
71         __elfN(coredump),
72         NULL,
73         MINSIGSTKSZ,
74         PAGE_SIZE,
75         VM_MIN_ADDRESS,
76         VM_MAXUSER_ADDRESS,
77         USRSTACK,
78         PS_STRINGS,
79         VM_PROT_READ|VM_PROT_WRITE,
80         exec_copyout_strings,
81         exec_setregs,
82         NULL
83 };
84
85 static Elf64_Brandinfo freebsd_brand_info = {
86         ELFOSABI_FREEBSD,
87         EM_IA_64,
88         "FreeBSD",
89         NULL,
90         "/libexec/ld-elf.so.1",
91         &elf64_freebsd_sysvec,
92         NULL,
93 };
94 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY,
95     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
96
97 static Elf64_Brandinfo freebsd_brand_oinfo = {
98         ELFOSABI_FREEBSD,
99         EM_IA_64,
100         "FreeBSD",
101         NULL,
102         "/usr/libexec/ld-elf.so.1",
103         &elf64_freebsd_sysvec,
104         NULL,
105 };
106 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
107     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_oinfo);
108
109
110 void
111 elf64_dump_thread(struct thread *td, void *dst, size_t *off __unused)
112 {
113
114         /* Flush the dirty registers onto the backingstore. */
115         if (dst == NULL)
116                 ia64_flush_dirty(td, &td->td_frame->tf_special);
117 }
118
119
120 static Elf_Addr
121 lookup_fdesc(linker_file_t lf, Elf_Size symidx, elf_lookup_fn lookup)
122 {
123         linker_file_t top;
124         Elf_Addr addr;
125         const char *symname;
126         int i;
127         static int eot = 0;
128
129         addr = lookup(lf, symidx, 0);
130         if (addr == 0) {
131                 top = lf;
132                 symname = elf_get_symname(top, symidx);
133                 for (i = 0; i < top->ndeps; i++) {
134                         lf = top->deps[i];
135                         addr = (Elf_Addr)linker_file_lookup_symbol(lf,
136                             symname, 0);
137                         if (addr != 0)
138                                 break;
139                 }
140                 if (addr == 0)
141                         return (0);
142         }
143
144         if (eot)
145                 return (0);
146
147         /*
148          * Lookup and/or construct OPD
149          */
150         for (i = 0; i < 8192; i += 2) {
151                 if (fptr_storage[i] == addr)
152                         return (Elf_Addr)(fptr_storage + i);
153
154                 if (fptr_storage[i] == 0) {
155                         fptr_storage[i] = addr;
156                         fptr_storage[i+1] = link_elf_get_gp(lf);
157                         return (Elf_Addr)(fptr_storage + i);
158                 }
159         }
160
161         printf("%s: fptr table full\n", __func__);
162         eot = 1;
163
164         return (0);
165 }
166
167 /* Process one elf relocation with addend. */
168 static int
169 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
170     int type, int local, elf_lookup_fn lookup)
171 {
172         Elf_Addr *where;
173         Elf_Addr addend, addr;
174         Elf_Size rtype, symidx;
175         const Elf_Rel *rel;
176         const Elf_Rela *rela;
177
178         switch (type) {
179         case ELF_RELOC_REL:
180                 rel = (const Elf_Rel *)data;
181                 where = (Elf_Addr *)(relocbase + rel->r_offset);
182                 rtype = ELF_R_TYPE(rel->r_info);
183                 symidx = ELF_R_SYM(rel->r_info);
184                 switch (rtype) {
185                 case R_IA_64_DIR64LSB:
186                 case R_IA_64_FPTR64LSB:
187                 case R_IA_64_REL64LSB:
188                         addend = *where;
189                         break;
190                 default:
191                         addend = 0;
192                         break;
193                 }
194                 break;
195         case ELF_RELOC_RELA:
196                 rela = (const Elf_Rela *)data;
197                 where = (Elf_Addr *)(relocbase + rela->r_offset);
198                 rtype = ELF_R_TYPE(rela->r_info);
199                 symidx = ELF_R_SYM(rela->r_info);
200                 addend = rela->r_addend;
201                 break;
202         default:
203                 panic("%s: invalid ELF relocation (0x%x)\n", __func__, type);
204         }
205
206         if (local) {
207                 if (rtype == R_IA_64_REL64LSB)
208                         *where = relocbase + addend;
209                 return (0);
210         }
211
212         switch (rtype) {
213         case R_IA_64_NONE:
214                 break;
215         case R_IA_64_DIR64LSB:  /* word64 LSB   S + A */
216                 addr = lookup(lf, symidx, 1);
217                 if (addr == 0)
218                         return (-1);
219                 *where = addr + addend;
220                 break;
221         case R_IA_64_FPTR64LSB: /* word64 LSB   @fptr(S + A) */
222                 if (addend != 0) {
223                         printf("%s: addend ignored for OPD relocation\n",
224                             __func__);
225                 }
226                 addr = lookup_fdesc(lf, symidx, lookup);
227                 if (addr == 0)
228                         return (-1);
229                 *where = addr;
230                 break;
231         case R_IA_64_REL64LSB:  /* word64 LSB   BD + A */
232                 break;
233         case R_IA_64_IPLTLSB:
234                 addr = lookup_fdesc(lf, symidx, lookup);
235                 if (addr == 0)
236                         return (-1);
237                 where[0] = *((Elf_Addr*)addr) + addend;
238                 where[1] = *((Elf_Addr*)addr + 1);
239                 break;
240         default:
241                 printf("%s: unknown relocation (0x%x)\n", __func__,
242                     (int)rtype);
243                 return -1;
244         }
245
246         return (0);
247 }
248
249 int
250 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
251     elf_lookup_fn lookup)
252 {
253
254         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
255 }
256
257 int
258 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
259     int type, elf_lookup_fn lookup)
260 {
261
262         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
263 }
264
265 int
266 elf_cpu_load_file(linker_file_t lf)
267 {
268         Elf_Ehdr *hdr;
269         Elf_Phdr *ph, *phlim;
270         Elf_Addr reloc, vaddr;
271
272         hdr = (Elf_Ehdr *)(lf->address);
273         if (!IS_ELF(*hdr)) {
274                 printf("Missing or corrupted ELF header at %p\n", hdr);
275                 return (EFTYPE);
276         }
277
278         /*
279          * Iterate over the segments and register the unwind table if
280          * we come across it.
281          */
282         ph = (Elf_Phdr *)(lf->address + hdr->e_phoff);
283         phlim = ph + hdr->e_phnum;
284         reloc = ~0ULL;
285         while (ph < phlim) {
286                 if (ph->p_type == PT_LOAD && reloc == ~0ULL)
287                         reloc = (Elf_Addr)lf->address - ph->p_vaddr;
288
289                 if (ph->p_type == PT_IA_64_UNWIND) {
290                         vaddr = ph->p_vaddr + reloc;
291                         unw_table_add((vm_offset_t)lf->address, vaddr,
292                             vaddr + ph->p_memsz);
293                 }
294                 ++ph;
295         }
296
297         return (0);
298 }
299
300 int
301 elf_cpu_unload_file(linker_file_t lf)
302 {
303
304         unw_table_remove((vm_offset_t)lf->address);
305         return (0);
306 }