GCC supports two pseudo variables to get the function name, __FUNCTION__
[dragonfly.git] / sys / emulation / ibcs2 / coff / imgact_coff.c
1 /*-
2  * Copyright (c) 1994 Sean Eric Fagan
3  * Copyright (c) 1994 Søren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software withough specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/i386/ibcs2/imgact_coff.c,v 1.40 1999/12/15 23:01:47 eivind Exp $
30  * $DragonFly: src/sys/emulation/ibcs2/coff/Attic/imgact_coff.c,v 1.14 2005/02/17 13:59:36 joerg Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/mman.h>
36 #include <sys/imgact.h>
37 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
39 #include <sys/malloc.h>
40 #include <sys/mount.h>
41 #include <sys/proc.h>
42 #include <sys/nlookup.h>
43 #include <sys/vnode.h>
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_zone.h>
51
52 #include "../i386/coff.h"
53 #include "../i386/ibcs2_util.h"
54
55 extern struct sysentvec ibcs2_svr3_sysvec;
56
57 static int coff_load_file (struct thread *td, char *name);
58 static int exec_coff_imgact (struct image_params *imgp);
59
60 static int load_coff_section (struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot);
61
62 static int
63 load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset,
64                   caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
65 {
66         size_t map_len;
67         vm_offset_t map_offset;
68         vm_offset_t map_addr;
69         int error;
70         unsigned char *data_buf = 0;
71         size_t copy_len;
72
73         map_offset = trunc_page(offset);
74         map_addr = trunc_page((vm_offset_t)vmaddr);
75
76         if (memsz > filsz) {
77                 /*
78                  * We have the stupid situation that
79                  * the section is longer than it is on file,
80                  * which means it has zero-filled areas, and
81                  * we have to work for it.  Stupid iBCS!
82                  */
83                 map_len = trunc_page(offset + filsz) - trunc_page(map_offset);
84         } else {
85                 /*
86                  * The only stuff we care about is on disk, and we
87                  * don't care if we map in more than is really there.
88                  */
89                 map_len = round_page(offset + filsz) - trunc_page(map_offset);
90         }
91
92         DPRINTF(("%s(%d):  vm_mmap(&vmspace->vm_map, &0x%08lx, 0x%x, 0x%x, "
93                 "VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, vp, 0x%x)\n",
94                 __FILE__, __LINE__, map_addr, map_len, prot, map_offset));
95
96         if ((error = vm_mmap(&vmspace->vm_map,
97                              &map_addr,
98                              map_len,
99                              prot,
100                              VM_PROT_ALL,
101                              MAP_PRIVATE | MAP_FIXED,
102                              (caddr_t) vp,
103                              map_offset)) != 0)
104                 return error;
105
106         if (memsz == filsz) {
107                 /* We're done! */
108                 return 0;
109         }
110
111         /*
112          * Now we have screwball stuff, to accomodate stupid COFF.
113          * We have to map the remaining bit of the file into the kernel's
114          * memory map, allocate some anonymous memory, copy that last
115          * bit into it, and then we're done. *sigh*
116          * For clean-up reasons, we actally map in the file last.
117          */
118
119         copy_len = (offset + filsz) - trunc_page(offset + filsz);
120         map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
121         map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
122
123         DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx,0x%x, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n", __FILE__, __LINE__, map_addr, map_len));
124
125         if (map_len != 0) {
126                 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
127                                     map_len, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
128                 if (error)
129                         return error;
130         }
131
132         if ((error = vm_mmap(kernel_map,
133                             (vm_offset_t *) &data_buf,
134                             PAGE_SIZE,
135                             VM_PROT_READ,
136                             VM_PROT_READ,
137                             0,
138                             (caddr_t) vp,
139                             trunc_page(offset + filsz))) != 0)
140                 return error;
141
142         error = copyout(data_buf, (caddr_t) map_addr, copy_len);
143
144         if (vm_map_remove(kernel_map,
145                           (vm_offset_t) data_buf,
146                           (vm_offset_t) data_buf + PAGE_SIZE))
147                 panic("load_coff_section vm_map_remove failed");
148
149         return error;
150 }
151
152 static int
153 coff_load_file(struct thread *td, char *name)
154 {
155         struct vmspace *vmspace;
156         int error;
157         struct nlookupdata nd;
158         struct vnode *vp;
159         struct vattr attr;
160         struct filehdr *fhdr;
161         struct aouthdr *ahdr;
162         struct scnhdr *scns;
163         char *ptr = 0;
164         int nscns;
165         unsigned long text_offset = 0, text_address = 0, text_size = 0;
166         unsigned long data_offset = 0, data_address = 0, data_size = 0;
167         unsigned long bss_size = 0;
168         struct ucred *cred;
169         int i;
170
171         KKASSERT(td->td_proc);
172         cred = td->td_proc->p_ucred;
173         vmspace = td->td_proc->p_vmspace;
174
175         /* XXX use of 'curthread' should be 'td'?*/
176         error = nlookup_init(&nd, name, UIO_SYSSPACE, NLC_FOLLOW);
177         if (error == 0)
178                 error = nlookup(&nd);
179         if (error == 0) {
180                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
181                 if (error)
182                         error = ENOEXEC;
183         }
184         if (error)
185                 goto done;
186
187         if (vp->v_writecount) {
188                 error = ETXTBSY;
189                 goto fail;
190         }
191
192         if ((error = VOP_GETATTR(vp, &attr, td)) != 0)
193                 goto fail;
194
195         if ((vp->v_mount->mnt_flag & MNT_NOEXEC)
196             || ((attr.va_mode & 0111) == 0)
197             || (attr.va_type != VREG))
198                 goto fail;
199
200         if (attr.va_size == 0) {
201                 error = ENOEXEC;
202                 goto fail;
203         }
204
205         if ((error = VOP_ACCESS(vp, VEXEC, cred, td)) != 0)
206                 goto fail;
207
208         if ((error = VOP_OPEN(vp, FREAD, cred, NULL, td)) != 0)
209                 goto fail;
210
211         /*
212          * Lose the lock on the vnode. It's no longer needed, and must not
213          * exist for the pagefault paging to work below.
214          */
215         VOP_UNLOCK(vp, 0, td);
216
217         if ((error = vm_mmap(kernel_map,
218                             (vm_offset_t *) &ptr,
219                             PAGE_SIZE,
220                             VM_PROT_READ,
221                             VM_PROT_READ,
222                             0,
223                             (caddr_t) vp,
224                             0)) != 0)
225                 goto unlocked_fail;
226
227         fhdr = (struct filehdr *)ptr;
228
229         if (fhdr->f_magic != I386_COFF) {
230                 error = ENOEXEC;
231                 goto dealloc_and_fail;
232         }
233
234         nscns = fhdr->f_nscns;
235
236         if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
237                 /*
238                  * XXX -- just fail.  I'm so lazy.
239                  */
240                 error = ENOEXEC;
241                 goto dealloc_and_fail;
242         }
243
244         ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
245
246         scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
247                           + sizeof(struct aouthdr));
248
249         for (i = 0; i < nscns; i++) {
250                 if (scns[i].s_flags & STYP_NOLOAD)
251                         continue;
252                 else if (scns[i].s_flags & STYP_TEXT) {
253                         text_address = scns[i].s_vaddr;
254                         text_size = scns[i].s_size;
255                         text_offset = scns[i].s_scnptr;
256                 }
257                 else if (scns[i].s_flags & STYP_DATA) {
258                         data_address = scns[i].s_vaddr;
259                         data_size = scns[i].s_size;
260                         data_offset = scns[i].s_scnptr;
261                 } else if (scns[i].s_flags & STYP_BSS) {
262                         bss_size = scns[i].s_size;
263                 }
264         }
265
266         if ((error = load_coff_section(vmspace, vp, text_offset,
267                                       (caddr_t)(void *)(uintptr_t)text_address,
268                                       text_size, text_size,
269                                       VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
270                 goto dealloc_and_fail;
271         }
272         if ((error = load_coff_section(vmspace, vp, data_offset,
273                                       (caddr_t)(void *)(uintptr_t)data_address,
274                                       data_size + bss_size, data_size,
275                                       VM_PROT_ALL)) != 0) {
276                 goto dealloc_and_fail;
277         }
278
279         error = 0;
280
281  dealloc_and_fail:
282         if (vm_map_remove(kernel_map,
283                           (vm_offset_t) ptr,
284                           (vm_offset_t) ptr + PAGE_SIZE))
285                 panic("%s: vm_map_remove failed", __func__);
286
287  fail:
288         VOP_UNLOCK(vp, 0, td);
289  unlocked_fail:
290         vrele(vp);
291 done:
292         nlookup_done(&nd);
293         return error;
294 }
295
296 static int
297 exec_coff_imgact(imgp)
298         struct image_params *imgp;
299 {
300         const struct filehdr *fhdr = (const struct filehdr*)imgp->image_header;
301         const struct aouthdr *ahdr;
302         const struct scnhdr *scns;
303         int i;
304         struct vmspace *vmspace;
305         int nscns;
306         int error;
307         unsigned long text_offset = 0, text_address = 0, text_size = 0;
308         unsigned long data_offset = 0, data_address = 0, data_size = 0;
309         unsigned long bss_size = 0;
310         caddr_t hole;
311
312         if (fhdr->f_magic != I386_COFF ||
313             !(fhdr->f_flags & F_EXEC)) {
314
315                  DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
316                  return -1;
317         }
318
319         nscns = fhdr->f_nscns;
320         if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
321                 /*
322                  * For now, return an error -- need to be able to
323                  * read in all of the section structures.
324                  */
325
326                 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
327                 return -1;
328         }
329
330         ahdr = (const struct aouthdr*)
331                ((const char*)(imgp->image_header) + sizeof(struct filehdr));
332         imgp->entry_addr = ahdr->entry;
333
334         scns = (const struct scnhdr*)
335                ((const char*)(imgp->image_header) + sizeof(struct filehdr) +
336                 sizeof(struct aouthdr));
337
338         exec_new_vmspace(imgp, NULL);
339         vmspace = imgp->proc->p_vmspace;
340
341         for (i = 0; i < nscns; i++) {
342
343           DPRINTF(("i = %d, scns[i].s_name = %s, scns[i].s_vaddr = %08lx, "
344                    "scns[i].s_scnptr = %d\n", i, scns[i].s_name,
345                    scns[i].s_vaddr, scns[i].s_scnptr));
346           if (scns[i].s_flags & STYP_NOLOAD) {
347                 /*
348                  * A section that is not loaded, for whatever
349                  * reason.  It takes precedance over other flag
350                  * bits...
351                  */
352                 continue;
353           } else if (scns[i].s_flags & STYP_TEXT) {
354                 text_address = scns[i].s_vaddr;
355                 text_size = scns[i].s_size;
356                 text_offset = scns[i].s_scnptr;
357           } else if (scns[i].s_flags & STYP_DATA) {
358                 /* .data section */
359                 data_address = scns[i].s_vaddr;
360                 data_size = scns[i].s_size;
361                 data_offset = scns[i].s_scnptr;
362           } else if (scns[i].s_flags & STYP_BSS) {
363                 /* .bss section */
364                 bss_size = scns[i].s_size;
365           } else if (scns[i].s_flags & STYP_LIB) {
366                 char *buf = 0;
367                 int foff = trunc_page(scns[i].s_scnptr);
368                 int off = scns[i].s_scnptr - foff;
369                 int len = round_page(scns[i].s_size + PAGE_SIZE);
370                 int j;
371
372                 if ((error = vm_mmap(kernel_map,
373                                     (vm_offset_t *) &buf,
374                                     len,
375                                     VM_PROT_READ,
376                                     VM_PROT_READ,
377                                     0,
378                                     (caddr_t) imgp->vp,
379                                     foff)) != 0) {
380                         return ENOEXEC;
381                 }
382                 if(scns[i].s_size) {
383                         char *libbuf;
384                         int emul_path_len = strlen(ibcs2_emul_path);
385
386                         libbuf = malloc(MAXPATHLEN + emul_path_len,
387                                         M_TEMP, M_WAITOK);
388                         strcpy(libbuf, ibcs2_emul_path);
389
390                         for (j = off; j < scns[i].s_size + off; j++) {
391                                 char *libname;
392
393                                 libname = buf + j + 4 * *(long*)(buf + j + 4);
394                                 j += 4* *(long*)(buf + j);
395
396                                 DPRINTF(("%s(%d):  shared library %s\n",
397                                          __FILE__, __LINE__, libname));
398                                 strcpy(&libbuf[emul_path_len], libname);
399                                 error = coff_load_file(imgp->proc->p_thread, libbuf);
400                                 if (error)
401                                         error = coff_load_file(
402                                                     imgp->proc->p_thread,
403                                                     libname);
404                                 if (error)
405                                         break;
406                         }
407                         free(libbuf, M_TEMP);
408                 }
409                 if (vm_map_remove(kernel_map,
410                                   (vm_offset_t) buf,
411                                   (vm_offset_t) buf + len))
412                         panic("exec_coff_imgact vm_map_remove failed");
413                 if (error)
414                         return error;
415                 }
416         }
417         /*
418          * Map in .text now
419          */
420
421         DPRINTF(("%s(%d):  load_coff_section(vmspace, "
422                 "imgp->vp, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n",
423                 __FILE__, __LINE__, text_offset, text_address,
424                 text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE));
425         if ((error = load_coff_section(vmspace, imgp->vp,
426                                       text_offset,
427                                       (caddr_t)(void *)(uintptr_t)text_address,
428                                       text_size, text_size,
429                                       VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
430                 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
431                 return error;
432         }
433         /*
434          * Map in .data and .bss now
435          */
436
437
438         DPRINTF(("%s(%d): load_coff_section(vmspace, "
439                 "imgp->vp, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n",
440                 __FILE__, __LINE__, data_offset, data_address,
441                 data_size + bss_size, data_size, VM_PROT_ALL));
442         if ((error = load_coff_section(vmspace, imgp->vp,
443                                       data_offset,
444                                       (caddr_t)(void *)(uintptr_t)data_address,
445                                       data_size + bss_size, data_size,
446                                       VM_PROT_ALL)) != 0) {
447
448                 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
449                 return error;
450         }
451
452         imgp->interpreted = 0;
453         imgp->proc->p_sysent = &ibcs2_svr3_sysvec;
454
455         vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
456         vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
457         vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)text_address;
458         vmspace->vm_daddr = (caddr_t)(void *)(uintptr_t)data_address;
459
460         hole = (caddr_t)trunc_page((vm_offset_t)vmspace->vm_daddr) + ctob(vmspace->vm_dsize);
461
462
463         DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n",
464                 __FILE__, __LINE__, hole));
465         DPRINTF(("imgact: error = %d\n", error));
466
467         error = vm_map_find(&vmspace->vm_map, NULL, 0,
468                             (vm_offset_t *) &hole, PAGE_SIZE, FALSE,
469                                 VM_PROT_ALL, VM_PROT_ALL, 0);
470
471         DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n",
472                 ctob(vmspace->vm_dsize), vmspace->vm_daddr,
473                 ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
474         DPRINTF(("%s(%d):  returning successfully!\n", __FILE__, __LINE__));
475
476         /* Indicate that this file should not be modified */
477         imgp->vp->v_flag |= VTEXT;
478         return 0;
479 }
480
481 /*
482  * Tell kern_execve.c about it, with a little help from the linker.
483  */
484 static struct execsw coff_execsw = { exec_coff_imgact, "coff" };
485 EXEC_SET(coff, coff_execsw);