jseward | 2886b0e | 2004-01-04 03:46:11 +0000 | [diff] [blame] | 1 | |
nethercote | 1fe5450 | 2004-07-26 15:28:33 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | 08a2e17 | 2005-06-21 22:47:54 +0000 | [diff] [blame] | 3 | /*--- User-mode execve. pub_core_ume.h ---*/ |
nethercote | 1fe5450 | 2004-07-26 15:28:33 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
jseward | 2886b0e | 2004-01-04 03:46:11 +0000 | [diff] [blame] | 6 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
jseward | 2886b0e | 2004-01-04 03:46:11 +0000 | [diff] [blame] | 9 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2005 Julian Seward |
jseward | 2886b0e | 2004-01-04 03:46:11 +0000 | [diff] [blame] | 11 | jseward@acm.org |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
njn | 08a2e17 | 2005-06-21 22:47:54 +0000 | [diff] [blame] | 31 | #ifndef __PUB_CORE_UME_H |
| 32 | #define __PUB_CORE_UME_H |
| 33 | |
| 34 | //-------------------------------------------------------------------- |
| 35 | // PURPOSE: This module implements user-mode execve, ie. program loading |
| 36 | // and exec'ing. It is shared between stage1 and stage2. |
| 37 | //-------------------------------------------------------------------- |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 38 | |
| 39 | #include <elf.h> |
| 40 | #include <sys/types.h> |
| 41 | |
nethercote | 1fe5450 | 2004-07-26 15:28:33 +0000 | [diff] [blame] | 42 | /*------------------------------------------------------------*/ |
| 43 | /*--- General stuff ---*/ |
| 44 | /*------------------------------------------------------------*/ |
| 45 | |
njn | 08a2e17 | 2005-06-21 22:47:54 +0000 | [diff] [blame] | 46 | /* This is only here so it can be shared between stage1 and stage2 */ |
nethercote | 969ecf1 | 2004-10-13 17:29:01 +0000 | [diff] [blame] | 47 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 48 | /* JRS 9 Aug 05: both of these are apparently unused, except by |
| 49 | memcheck/tests/vgtest_ume.c. */ |
| 50 | //zz extern |
| 51 | //zz void VG_(foreach_map)(int (*fn)(char *start, char *end, |
| 52 | //zz const char *perm, off_t offset, |
| 53 | //zz int maj, int min, int ino, void* extra), |
| 54 | //zz void* extra); |
| 55 | //zz |
| 56 | //zz /* Jump to 'dst', but first set the stack pointer to 'stack'. Also, |
| 57 | //zz clear all the integer registers before entering 'dst'. It's |
| 58 | //zz important that the stack pointer is set to exactly 'stack' and not |
| 59 | //zz (eg) stack - apparently_harmless_looking_small_offset. Basically |
| 60 | //zz because the code at 'dst' might be wanting to scan the area above |
| 61 | //zz 'stack' (viz, the auxv array), and putting spurious words on the |
| 62 | //zz stack confuses it. |
| 63 | //zz |
| 64 | //zz This is only exported so that vgtest_ume.c can use it. |
| 65 | //zz */ |
| 66 | //zz extern |
| 67 | //zz __attribute__((noreturn)) |
| 68 | //zz void VG_(jump_and_switch_stacks) ( Addr stack, Addr dst ); |
nethercote | 107e1c0 | 2004-10-13 17:55:31 +0000 | [diff] [blame] | 69 | |
sewardj | 0c1a596 | 2005-03-22 00:19:55 +0000 | [diff] [blame] | 70 | |
nethercote | 107e1c0 | 2004-10-13 17:55:31 +0000 | [diff] [blame] | 71 | /*------------------------------------------------------------*/ |
| 72 | /*--- Loading ELF files ---*/ |
| 73 | /*------------------------------------------------------------*/ |
| 74 | |
nethercote | ea147e7 | 2004-07-26 15:43:57 +0000 | [diff] [blame] | 75 | // Info needed to load and run a program. IN/INOUT/OUT refers to the |
| 76 | // inputs/outputs of do_exec(). |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 77 | struct exeinfo |
| 78 | { |
nethercote | ea147e7 | 2004-07-26 15:43:57 +0000 | [diff] [blame] | 79 | char** argv; // IN: the original argv |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 80 | |
nethercote | a3c3cf2 | 2004-11-01 18:38:00 +0000 | [diff] [blame] | 81 | Addr exe_base; // INOUT: lowest (allowed) address of exe |
| 82 | Addr exe_end; // INOUT: highest (allowed) address |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 83 | |
nethercote | a3c3cf2 | 2004-11-01 18:38:00 +0000 | [diff] [blame] | 84 | Addr phdr; // OUT: address phdr was mapped at |
| 85 | int phnum; // OUT: number of phdrs |
| 86 | Addr interp_base; // OUT: where interpreter (ld.so) was mapped |
| 87 | Addr entry; // OUT: entrypoint in main executable |
| 88 | Addr init_eip; // OUT: initial eip |
| 89 | Addr brkbase; // OUT: base address of brk segment |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 90 | |
nethercote | 1fe5450 | 2004-07-26 15:28:33 +0000 | [diff] [blame] | 91 | // These are the extra args added by #! scripts |
nethercote | ea147e7 | 2004-07-26 15:43:57 +0000 | [diff] [blame] | 92 | char* interp_name; // OUT: the interpreter name |
| 93 | char* interp_args; // OUT: the args for the interpreter |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 96 | // Do a number of appropriate checks to see if the file looks executable by |
| 97 | // the kernel: ie. it's a file, it's readable and executable, and it's in |
| 98 | // either ELF or "#!" format. On success, 'out_fd' gets the fd of the file |
| 99 | // if it's non-NULL. Otherwise the fd is closed. |
| 100 | extern SysRes VG_(pre_exec_check)(const Char* exe_name, Int* out_fd); |
| 101 | |
nethercote | ea147e7 | 2004-07-26 15:43:57 +0000 | [diff] [blame] | 102 | // Does everything short of actually running 'exe': finds the file, |
| 103 | // checks execute permissions, sets up interpreter if program is a script, |
| 104 | // reads headers, maps file into memory, and returns important info about |
| 105 | // the program. |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 106 | extern Int VG_(do_exec)(const char *exe, struct exeinfo *info); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 107 | |
nethercote | 1fe5450 | 2004-07-26 15:28:33 +0000 | [diff] [blame] | 108 | /*------------------------------------------------------------*/ |
nethercote | 1fe5450 | 2004-07-26 15:28:33 +0000 | [diff] [blame] | 109 | /*--- Finding and dealing with auxv ---*/ |
| 110 | /*------------------------------------------------------------*/ |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 111 | |
| 112 | struct ume_auxv |
| 113 | { |
sewardj | b9bca7c | 2005-03-02 14:04:19 +0000 | [diff] [blame] | 114 | Word a_type; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 115 | union { |
| 116 | void *a_ptr; |
sewardj | b9bca7c | 2005-03-02 14:04:19 +0000 | [diff] [blame] | 117 | Word a_val; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 118 | } u; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
njn | 62ff0f2 | 2005-06-21 23:03:36 +0000 | [diff] [blame] | 121 | extern struct ume_auxv *VG_(find_auxv)(UWord* orig_esp); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 122 | |
njn | 08a2e17 | 2005-06-21 22:47:54 +0000 | [diff] [blame] | 123 | #endif /* __PUB_CORE_UME_H */ |
nethercote | 1fe5450 | 2004-07-26 15:28:33 +0000 | [diff] [blame] | 124 | |
| 125 | /*--------------------------------------------------------------------*/ |
njn | 08a2e17 | 2005-06-21 22:47:54 +0000 | [diff] [blame] | 126 | /*--- end ---*/ |
nethercote | 1fe5450 | 2004-07-26 15:28:33 +0000 | [diff] [blame] | 127 | /*--------------------------------------------------------------------*/ |