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