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