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