jseward | 2886b0e | 2004-01-04 03:46:11 +0000 | [diff] [blame] | 1 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 3 | /*--- Startup: preliminaries stage1.c ---*/ |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
jseward | 2886b0e | 2004-01-04 03:46:11 +0000 | [diff] [blame] | 6 | /* |
| 7 | This file is part of Valgrind, an extensible x86 protected-mode |
| 8 | emulator for monitoring program execution on x86-Unixes. |
| 9 | |
| 10 | Copyright (C) 2000-2004 Julian Seward |
| 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 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 31 | #define _FILE_OFFSET_BITS 64 |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <elf.h> |
| 35 | #include <string.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <assert.h> |
| 38 | #include <signal.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <errno.h> |
fitzhardinge | 716c2c1 | 2004-03-09 00:57:45 +0000 | [diff] [blame] | 41 | #include <sys/resource.h> |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 42 | |
| 43 | #include "vg_include.h" |
| 44 | |
| 45 | #include "ume.h" |
| 46 | #include "ume_arch.h" |
| 47 | #include "ume_archdefs.h" |
| 48 | |
| 49 | static int stack[SIGSTKSZ*4]; |
| 50 | static int our_argc; |
| 51 | |
| 52 | /* Where we expect to find all our aux files (namely, stage2) */ |
| 53 | static const char *valgrind_lib = VG_LIBDIR; |
| 54 | |
| 55 | /* stage2's name */ |
| 56 | static const char stage2[] = "stage2"; |
| 57 | |
| 58 | /* Modify the auxv the kernel gave us to make it look like we were |
| 59 | execed as the shared object. |
| 60 | |
| 61 | This also inserts a new entry into the auxv table so we can |
| 62 | communicate some extra information to stage2 (namely, the fd of the |
| 63 | padding file, so it can identiry and remove the padding later). |
| 64 | */ |
| 65 | static void *fix_auxv(void *v_init_esp, const struct exeinfo *info) |
| 66 | { |
| 67 | struct ume_auxv *auxv; |
| 68 | int *newesp; |
| 69 | int seen; |
| 70 | int delta; |
| 71 | int i; |
| 72 | static const int new_entries = 2; |
| 73 | |
| 74 | /* make sure we're running on the private stack */ |
| 75 | assert(&delta >= stack && &delta < &stack[sizeof(stack)/sizeof(*stack)]); |
| 76 | |
| 77 | /* find the beginning of the AUXV table */ |
| 78 | auxv = find_auxv(v_init_esp); |
| 79 | |
| 80 | /* Work out how we should move things to make space for the new |
| 81 | auxv entry. It seems that ld.so wants a 16-byte aligned stack on |
| 82 | entry, so make sure that's the case. */ |
| 83 | newesp = (int *)(((unsigned long)v_init_esp - new_entries * sizeof(*auxv)) & ~0xf); |
| 84 | delta = (char *)v_init_esp - (char *)newesp; |
| 85 | |
| 86 | memmove(newesp, v_init_esp, (char *)auxv - (char *)v_init_esp); |
| 87 | |
| 88 | v_init_esp = (void *)newesp; |
| 89 | auxv -= delta/sizeof(*auxv); |
| 90 | |
| 91 | /* stage2 needs this so it can clean up the padding we leave in |
| 92 | place when we start it */ |
| 93 | auxv[0].a_type = AT_UME_PADFD; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 94 | auxv[0].u.a_val = as_getpadfd(); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 95 | |
| 96 | /* This will be needed by valgrind itself so that it can |
| 97 | subsequently execve() children. This needs to be done here |
| 98 | because /proc/self/exe will go away once we unmap stage1. */ |
| 99 | auxv[1].a_type = AT_UME_EXECFD; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 100 | auxv[1].u.a_val = open("/proc/self/exe", O_RDONLY); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 101 | |
| 102 | /* make sure the rest are sane */ |
| 103 | for(i = new_entries; i < delta/sizeof(*auxv); i++) { |
| 104 | auxv[i].a_type = AT_IGNORE; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 105 | auxv[i].u.a_val = 0; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* OK, go through and patch up the auxv entries to match the new |
| 109 | executable */ |
| 110 | seen = 0; |
| 111 | for(; auxv->a_type != AT_NULL; auxv++) { |
| 112 | if (0) |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 113 | printf("doing auxv %p %4x: %d %p\n", auxv, auxv->a_type, auxv->u.a_val, auxv->u.a_ptr); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 114 | |
| 115 | switch(auxv->a_type) { |
| 116 | case AT_PHDR: |
| 117 | seen |= 1; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 118 | auxv->u.a_val = info->phdr; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 119 | break; |
| 120 | |
| 121 | case AT_PHNUM: |
| 122 | seen |= 2; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 123 | auxv->u.a_val = info->phnum; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 124 | break; |
| 125 | |
| 126 | case AT_BASE: |
| 127 | seen |= 4; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 128 | auxv->u.a_val = info->interp_base; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 129 | break; |
| 130 | |
| 131 | case AT_ENTRY: |
| 132 | seen |= 8; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 133 | auxv->u.a_val = info->entry; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 134 | break; |
fitzhardinge | a1d37c0 | 2004-06-14 02:36:09 +0000 | [diff] [blame] | 135 | |
| 136 | case AT_SYSINFO: |
nethercote | 079acf6 | 2004-06-14 11:56:59 +0000 | [diff] [blame] | 137 | #ifdef AT_SYSINFO_EHDR |
fitzhardinge | a1d37c0 | 2004-06-14 02:36:09 +0000 | [diff] [blame] | 138 | case AT_SYSINFO_EHDR: |
nethercote | 079acf6 | 2004-06-14 11:56:59 +0000 | [diff] [blame] | 139 | #endif |
fitzhardinge | a1d37c0 | 2004-06-14 02:36:09 +0000 | [diff] [blame] | 140 | auxv->a_type = AT_IGNORE; |
| 141 | break; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | /* If we didn't see all the entries we need to fix up, then we |
| 146 | can't make the new executable viable. */ |
| 147 | if (seen != 0xf) { |
| 148 | fprintf(stderr, "fix_auxv: we didn't see enough auxv entries (seen=%x)\n", seen); |
| 149 | exit(1); |
| 150 | } |
| 151 | |
| 152 | return v_init_esp; |
| 153 | } |
| 154 | |
| 155 | static void hoops(void) |
| 156 | { |
| 157 | int err; |
| 158 | struct exeinfo info; |
| 159 | extern char _end; |
| 160 | int *esp; |
| 161 | char buf[strlen(valgrind_lib) + sizeof(stage2) + 16]; |
| 162 | |
| 163 | info.exe_base = PGROUNDUP(&_end); |
| 164 | info.exe_end = PGROUNDDN(ume_exec_esp); |
| 165 | |
| 166 | /* XXX FIXME: how can stage1 know where stage2 wants things placed? |
| 167 | Options: |
| 168 | - we could look for a symbol |
| 169 | - it could have a special PHDR (v. ELF specific) |
| 170 | - something else? |
| 171 | */ |
| 172 | info.map_base = 0xb0000000; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 173 | info.argv = NULL; |
| 174 | |
fitzhardinge | a49f9b5 | 2003-12-16 22:26:45 +0000 | [diff] [blame] | 175 | snprintf(buf, sizeof(buf), "%s/%s", valgrind_lib, stage2); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 176 | |
| 177 | err = do_exec(buf, &info); |
| 178 | |
| 179 | if (err != 0) { |
| 180 | fprintf(stderr, "failed to load %s: %s\n", |
| 181 | buf, strerror(err)); |
| 182 | exit(1); |
| 183 | } |
| 184 | |
| 185 | /* Make sure stage2's dynamic linker can't tromp on the lower part |
| 186 | of the address space. */ |
| 187 | as_pad(0, (void *)info.map_base); |
| 188 | |
| 189 | esp = fix_auxv(ume_exec_esp, &info); |
| 190 | |
| 191 | if (0) { |
| 192 | int prmap(void *start, void *end, const char *perm, off_t off, int maj, int min, int ino) { |
| 193 | printf("mapping %10p-%10p %s %02x:%02x %d\n", |
| 194 | start, end, perm, maj, min, ino); |
| 195 | return 1; |
| 196 | } |
| 197 | printf("---------- launch stage 2 ----------\n"); |
| 198 | printf("eip=%p esp=%p\n", (void *)info.init_eip, esp); |
| 199 | foreach_map(prmap); |
| 200 | } |
| 201 | |
| 202 | ume_go(info.init_eip, (addr_t)esp); |
| 203 | } |
| 204 | |
| 205 | int main(int argc, char **argv) |
| 206 | { |
fitzhardinge | 716c2c1 | 2004-03-09 00:57:45 +0000 | [diff] [blame] | 207 | struct rlimit rlim; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 208 | const char *cp = getenv(VALGRINDLIB); |
| 209 | |
| 210 | if (cp != NULL) |
| 211 | valgrind_lib = cp; |
| 212 | |
| 213 | assert(ume_exec_esp != NULL); |
| 214 | |
| 215 | our_argc = argc; |
| 216 | |
fitzhardinge | 716c2c1 | 2004-03-09 00:57:45 +0000 | [diff] [blame] | 217 | /* Set the address space limit as high as it will go, since we make |
| 218 | a lot of very large mappings. */ |
| 219 | getrlimit(RLIMIT_AS, &rlim); |
| 220 | rlim.rlim_cur = rlim.rlim_max; |
| 221 | setrlimit(RLIMIT_AS, &rlim); |
| 222 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 223 | /* move onto another stack so we can play with the main one */ |
| 224 | ume_go((addr_t)hoops, (addr_t)stack + sizeof(stack)); |
| 225 | } |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 226 | |
| 227 | /*--------------------------------------------------------------------*/ |
| 228 | /*--- end stage1.c ---*/ |
| 229 | /*--------------------------------------------------------------------*/ |