]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/ia64/ia64/elf_machdep.c
MFC ixgbe cummulative patch from stable/8
[FreeBSD/releng/8.2.git] / 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         .sv_size        = SYS_MAXSYSCALL,
58         .sv_table       = sysent,
59         .sv_mask        = 0,
60         .sv_sigsize     = 0,
61         .sv_sigtbl      = NULL,
62         .sv_errsize     = 0,
63         .sv_errtbl      = NULL,
64         .sv_transtrap   = NULL,
65         .sv_fixup       = __elfN(freebsd_fixup),
66         .sv_sendsig     = sendsig,
67         .sv_sigcode     = NULL,
68         .sv_szsigcode   = NULL,
69         .sv_prepsyscall = NULL,
70         .sv_name        = "FreeBSD ELF64",
71         .sv_coredump    = __elfN(coredump),
72         .sv_imgact_try  = NULL,
73         .sv_minsigstksz = MINSIGSTKSZ,
74         .sv_pagesize    = PAGE_SIZE,
75         .sv_minuser     = VM_MIN_ADDRESS,
76         .sv_maxuser     = VM_MAXUSER_ADDRESS,
77         .sv_usrstack    = USRSTACK,
78         .sv_psstrings   = PS_STRINGS,
79         .sv_stackprot   = VM_PROT_READ|VM_PROT_WRITE,
80         .sv_copyout_strings = exec_copyout_strings,
81         .sv_setregs     = exec_setregs,
82         .sv_fixlimit    = NULL,
83         .sv_maxssiz     = NULL,
84         .sv_flags       = SV_ABI_FREEBSD | SV_LP64,
85         .sv_set_syscall_retval = cpu_set_syscall_retval,
86         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
87         .sv_syscallnames = syscallnames,
88 };
89
90 static Elf64_Brandinfo freebsd_brand_info = {
91         .brand          = ELFOSABI_FREEBSD,
92         .machine        = EM_IA_64,
93         .compat_3_brand = "FreeBSD",
94         .emul_path      = NULL,
95         .interp_path    = "/libexec/ld-elf.so.1",
96         .sysvec         = &elf64_freebsd_sysvec,
97         .interp_newpath = NULL,
98         .brand_note     = &elf64_freebsd_brandnote,
99         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
100 };
101 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
102     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
103
104 static Elf64_Brandinfo freebsd_brand_oinfo = {
105         .brand          = ELFOSABI_FREEBSD,
106         .machine        = EM_IA_64,
107         .compat_3_brand = "FreeBSD",
108         .emul_path      = NULL,
109         .interp_path    = "/usr/libexec/ld-elf.so.1",
110         .sysvec         = &elf64_freebsd_sysvec,
111         .interp_newpath = NULL,
112         .brand_note     = &elf64_freebsd_brandnote,
113         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
114 };
115 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
116     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_oinfo);
117
118
119 void
120 elf64_dump_thread(struct thread *td, void *dst, size_t *off __unused)
121 {
122
123         /* Flush the dirty registers onto the backingstore. */
124         if (dst == NULL)
125                 ia64_flush_dirty(td, &td->td_frame->tf_special);
126 }
127
128
129 static Elf_Addr
130 lookup_fdesc(linker_file_t lf, Elf_Size symidx, elf_lookup_fn lookup)
131 {
132         linker_file_t top;
133         Elf_Addr addr;
134         const char *symname;
135         int i;
136         static int eot = 0;
137
138         addr = lookup(lf, symidx, 0);
139         if (addr == 0) {
140                 top = lf;
141                 symname = elf_get_symname(top, symidx);
142                 for (i = 0; i < top->ndeps; i++) {
143                         lf = top->deps[i];
144                         addr = (Elf_Addr)linker_file_lookup_symbol(lf,
145                             symname, 0);
146                         if (addr != 0)
147                                 break;
148                 }
149                 if (addr == 0)
150                         return (0);
151         }
152
153         if (eot)
154                 return (0);
155
156         /*
157          * Lookup and/or construct OPD
158          */
159         for (i = 0; i < 8192; i += 2) {
160                 if (fptr_storage[i] == addr)
161                         return (Elf_Addr)(fptr_storage + i);
162
163                 if (fptr_storage[i] == 0) {
164                         fptr_storage[i] = addr;
165                         fptr_storage[i+1] = link_elf_get_gp(lf);
166                         return (Elf_Addr)(fptr_storage + i);
167                 }
168         }
169
170         printf("%s: fptr table full\n", __func__);
171         eot = 1;
172
173         return (0);
174 }
175
176 /* Process one elf relocation with addend. */
177 static int
178 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
179     int type, int local, elf_lookup_fn lookup)
180 {
181         Elf_Addr *where;
182         Elf_Addr addend, addr;
183         Elf_Size rtype, symidx;
184         const Elf_Rel *rel;
185         const Elf_Rela *rela;
186
187         switch (type) {
188         case ELF_RELOC_REL:
189                 rel = (const Elf_Rel *)data;
190                 where = (Elf_Addr *)(relocbase + rel->r_offset);
191                 rtype = ELF_R_TYPE(rel->r_info);
192                 symidx = ELF_R_SYM(rel->r_info);
193                 switch (rtype) {
194                 case R_IA_64_DIR64LSB:
195                 case R_IA_64_FPTR64LSB:
196                 case R_IA_64_REL64LSB:
197                         addend = *where;
198                         break;
199                 default:
200                         addend = 0;
201                         break;
202                 }
203                 break;
204         case ELF_RELOC_RELA:
205                 rela = (const Elf_Rela *)data;
206                 where = (Elf_Addr *)(relocbase + rela->r_offset);
207                 rtype = ELF_R_TYPE(rela->r_info);
208                 symidx = ELF_R_SYM(rela->r_info);
209                 addend = rela->r_addend;
210                 break;
211         default:
212                 panic("%s: invalid ELF relocation (0x%x)\n", __func__, type);
213         }
214
215         if (local) {
216                 if (rtype == R_IA_64_REL64LSB)
217                         *where = elf_relocaddr(lf, relocbase + addend);
218                 return (0);
219         }
220
221         switch (rtype) {
222         case R_IA_64_NONE:
223                 break;
224         case R_IA_64_DIR64LSB:  /* word64 LSB   S + A */
225                 addr = lookup(lf, symidx, 1);
226                 if (addr == 0)
227                         return (-1);
228                 *where = addr + addend;
229                 break;
230         case R_IA_64_FPTR64LSB: /* word64 LSB   @fptr(S + A) */
231                 if (addend != 0) {
232                         printf("%s: addend ignored for OPD relocation\n",
233                             __func__);
234                 }
235                 addr = lookup_fdesc(lf, symidx, lookup);
236                 if (addr == 0)
237                         return (-1);
238                 *where = addr;
239                 break;
240         case R_IA_64_REL64LSB:  /* word64 LSB   BD + A */
241                 break;
242         case R_IA_64_IPLTLSB:
243                 addr = lookup_fdesc(lf, symidx, lookup);
244                 if (addr == 0)
245                         return (-1);
246                 where[0] = *((Elf_Addr*)addr) + addend;
247                 where[1] = *((Elf_Addr*)addr + 1);
248                 break;
249         default:
250                 printf("%s: unknown relocation (0x%x)\n", __func__,
251                     (int)rtype);
252                 return -1;
253         }
254
255         return (0);
256 }
257
258 int
259 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
260     elf_lookup_fn lookup)
261 {
262
263         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
264 }
265
266 int
267 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
268     int type, elf_lookup_fn lookup)
269 {
270
271         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
272 }
273
274 int
275 elf_cpu_load_file(linker_file_t lf)
276 {
277         Elf_Ehdr *hdr;
278         Elf_Phdr *ph, *phlim;
279         Elf_Addr reloc, vaddr;
280
281         hdr = (Elf_Ehdr *)(lf->address);
282         if (!IS_ELF(*hdr)) {
283                 printf("Missing or corrupted ELF header at %p\n", hdr);
284                 return (EFTYPE);
285         }
286
287         /*
288          * Iterate over the segments and register the unwind table if
289          * we come across it.
290          */
291         ph = (Elf_Phdr *)(lf->address + hdr->e_phoff);
292         phlim = ph + hdr->e_phnum;
293         reloc = ~0ULL;
294         while (ph < phlim) {
295                 if (ph->p_type == PT_LOAD && reloc == ~0ULL)
296                         reloc = (Elf_Addr)lf->address - ph->p_vaddr;
297
298                 if (ph->p_type == PT_IA_64_UNWIND) {
299                         vaddr = ph->p_vaddr + reloc;
300                         unw_table_add((vm_offset_t)lf->address, vaddr,
301                             vaddr + ph->p_memsz);
302                 }
303                 ++ph;
304         }
305
306         /*
307          * Make the I-cache coherent, but don't worry obout the kernel
308          * itself because the loader needs to do that.
309          */
310         if (lf->id != 1)
311                 ia64_sync_icache((uintptr_t)lf->address, lf->size);
312
313         return (0);
314 }
315
316 int
317 elf_cpu_unload_file(linker_file_t lf)
318 {
319
320         unw_table_remove((vm_offset_t)lf->address);
321         return (0);
322 }