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 | /* |
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 | |
| 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 | |
nethercote | 969ecf1 | 2004-10-13 17:29:01 +0000 | [diff] [blame] | 33 | #include <errno.h> |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 35 | #include <string.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <assert.h> |
| 38 | #include <signal.h> |
| 39 | #include <fcntl.h> |
nethercote | 969ecf1 | 2004-10-13 17:29:01 +0000 | [diff] [blame] | 40 | #include <sys/mman.h> |
fitzhardinge | 716c2c1 | 2004-03-09 00:57:45 +0000 | [diff] [blame] | 41 | #include <sys/resource.h> |
nethercote | 969ecf1 | 2004-10-13 17:29:01 +0000 | [diff] [blame] | 42 | #include <unistd.h> |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 43 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 44 | #include "core.h" |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 45 | #include "ume.h" |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 46 | |
| 47 | static int stack[SIGSTKSZ*4]; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 48 | |
nethercote | c25c449 | 2004-10-18 11:52:17 +0000 | [diff] [blame] | 49 | // Initial stack pointer, which points to argc. |
| 50 | static void* init_sp; |
| 51 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 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 | |
nethercote | 969ecf1 | 2004-10-13 17:29:01 +0000 | [diff] [blame] | 58 | /*------------------------------------------------------------*/ |
| 59 | /*--- Auxv modification ---*/ |
| 60 | /*------------------------------------------------------------*/ |
| 61 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 62 | /* Modify the auxv the kernel gave us to make it look like we were |
| 63 | execed as the shared object. |
| 64 | |
| 65 | This also inserts a new entry into the auxv table so we can |
| 66 | communicate some extra information to stage2 (namely, the fd of the |
| 67 | padding file, so it can identiry and remove the padding later). |
| 68 | */ |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 69 | static void *fix_auxv(void *v_init_esp, const struct exeinfo *info, |
| 70 | int padfile) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 71 | { |
| 72 | struct ume_auxv *auxv; |
| 73 | int *newesp; |
| 74 | int seen; |
| 75 | int delta; |
| 76 | int i; |
| 77 | static const int new_entries = 2; |
| 78 | |
| 79 | /* make sure we're running on the private stack */ |
| 80 | assert(&delta >= stack && &delta < &stack[sizeof(stack)/sizeof(*stack)]); |
| 81 | |
| 82 | /* find the beginning of the AUXV table */ |
| 83 | auxv = find_auxv(v_init_esp); |
| 84 | |
| 85 | /* Work out how we should move things to make space for the new |
| 86 | auxv entry. It seems that ld.so wants a 16-byte aligned stack on |
| 87 | entry, so make sure that's the case. */ |
| 88 | newesp = (int *)(((unsigned long)v_init_esp - new_entries * sizeof(*auxv)) & ~0xf); |
| 89 | delta = (char *)v_init_esp - (char *)newesp; |
| 90 | |
| 91 | memmove(newesp, v_init_esp, (char *)auxv - (char *)v_init_esp); |
| 92 | |
| 93 | v_init_esp = (void *)newesp; |
| 94 | auxv -= delta/sizeof(*auxv); |
| 95 | |
| 96 | /* stage2 needs this so it can clean up the padding we leave in |
| 97 | place when we start it */ |
| 98 | auxv[0].a_type = AT_UME_PADFD; |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 99 | auxv[0].u.a_val = padfile; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 100 | |
| 101 | /* This will be needed by valgrind itself so that it can |
| 102 | subsequently execve() children. This needs to be done here |
| 103 | because /proc/self/exe will go away once we unmap stage1. */ |
| 104 | auxv[1].a_type = AT_UME_EXECFD; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 105 | auxv[1].u.a_val = open("/proc/self/exe", O_RDONLY); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 106 | |
| 107 | /* make sure the rest are sane */ |
| 108 | for(i = new_entries; i < delta/sizeof(*auxv); i++) { |
| 109 | auxv[i].a_type = AT_IGNORE; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 110 | auxv[i].u.a_val = 0; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* OK, go through and patch up the auxv entries to match the new |
| 114 | executable */ |
| 115 | seen = 0; |
| 116 | for(; auxv->a_type != AT_NULL; auxv++) { |
| 117 | if (0) |
nethercote | 545fe67 | 2004-11-01 16:52:43 +0000 | [diff] [blame] | 118 | printf("doing auxv %p %4lld: %lld %p\n", |
| 119 | auxv, (ULong)auxv->a_type, (ULong)auxv->u.a_val, auxv->u.a_ptr); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 120 | |
| 121 | switch(auxv->a_type) { |
| 122 | case AT_PHDR: |
| 123 | seen |= 1; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 124 | auxv->u.a_val = info->phdr; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 125 | break; |
| 126 | |
| 127 | case AT_PHNUM: |
| 128 | seen |= 2; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 129 | auxv->u.a_val = info->phnum; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 130 | break; |
| 131 | |
| 132 | case AT_BASE: |
| 133 | seen |= 4; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 134 | auxv->u.a_val = info->interp_base; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 135 | break; |
| 136 | |
| 137 | case AT_ENTRY: |
| 138 | seen |= 8; |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 139 | auxv->u.a_val = info->entry; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 140 | break; |
fitzhardinge | a1d37c0 | 2004-06-14 02:36:09 +0000 | [diff] [blame] | 141 | |
nethercote | eda6b28 | 2004-06-15 08:34:10 +0000 | [diff] [blame] | 142 | #if (defined(AT_SYSINFO) || defined(AT_SYSINFO_EHDR)) |
| 143 | #ifdef AT_SYSINFO |
fitzhardinge | a1d37c0 | 2004-06-14 02:36:09 +0000 | [diff] [blame] | 144 | case AT_SYSINFO: |
nethercote | eda6b28 | 2004-06-15 08:34:10 +0000 | [diff] [blame] | 145 | #endif |
nethercote | 079acf6 | 2004-06-14 11:56:59 +0000 | [diff] [blame] | 146 | #ifdef AT_SYSINFO_EHDR |
fitzhardinge | a1d37c0 | 2004-06-14 02:36:09 +0000 | [diff] [blame] | 147 | case AT_SYSINFO_EHDR: |
nethercote | 079acf6 | 2004-06-14 11:56:59 +0000 | [diff] [blame] | 148 | #endif |
fitzhardinge | a1d37c0 | 2004-06-14 02:36:09 +0000 | [diff] [blame] | 149 | auxv->a_type = AT_IGNORE; |
| 150 | break; |
nethercote | eda6b28 | 2004-06-15 08:34:10 +0000 | [diff] [blame] | 151 | #endif |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | /* If we didn't see all the entries we need to fix up, then we |
| 156 | can't make the new executable viable. */ |
| 157 | if (seen != 0xf) { |
nethercote | 2e1cb4c | 2004-08-05 12:16:13 +0000 | [diff] [blame] | 158 | fprintf(stderr, "valgrind: we didn't see enough auxv entries (seen=%x)\n", seen); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 159 | exit(1); |
| 160 | } |
| 161 | |
| 162 | return v_init_esp; |
| 163 | } |
| 164 | |
nethercote | 969ecf1 | 2004-10-13 17:29:01 +0000 | [diff] [blame] | 165 | |
| 166 | /*------------------------------------------------------------*/ |
| 167 | /*--- Address space padding ---*/ |
| 168 | /*------------------------------------------------------------*/ |
| 169 | |
| 170 | static void check_mmap(void* res, void* base, int len) |
| 171 | { |
| 172 | if ((void*)-1 == res) { |
| 173 | fprintf(stderr, "valgrind: padding mmap(%p, %d) failed during startup.\n" |
| 174 | "valgrind: is there a hard virtual memory limit set?\n", |
| 175 | base, len); |
| 176 | exit(1); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | typedef struct { |
| 181 | char* fillgap_start; |
| 182 | char* fillgap_end; |
| 183 | int fillgap_padfile; |
| 184 | } fillgap_extra; |
| 185 | |
| 186 | static int fillgap(char *segstart, char *segend, const char *perm, off_t off, |
| 187 | int maj, int min, int ino, void* e) |
| 188 | { |
| 189 | fillgap_extra* extra = e; |
| 190 | |
| 191 | if (segstart >= extra->fillgap_end) |
| 192 | return 0; |
| 193 | |
| 194 | if (segstart > extra->fillgap_start) { |
| 195 | void* res = mmap(extra->fillgap_start, segstart - extra->fillgap_start, |
| 196 | PROT_NONE, MAP_FIXED|MAP_PRIVATE, |
| 197 | extra->fillgap_padfile, 0); |
| 198 | check_mmap(res, extra->fillgap_start, segstart - extra->fillgap_start); |
| 199 | } |
| 200 | extra->fillgap_start = segend; |
| 201 | |
| 202 | return 1; |
| 203 | } |
| 204 | |
| 205 | // Choose a name for the padfile, open it. |
| 206 | int as_openpadfile(void) |
| 207 | { |
| 208 | char buf[256]; |
| 209 | int padfile; |
| 210 | int seq = 1; |
| 211 | do { |
| 212 | snprintf(buf, 256, "/tmp/.pad.%d.%d", getpid(), seq++); |
| 213 | padfile = open(buf, O_RDWR|O_CREAT|O_EXCL, 0); |
| 214 | unlink(buf); |
| 215 | if (padfile == -1 && errno != EEXIST) { |
| 216 | fprintf(stderr, "valgrind: couldn't open padfile\n"); |
| 217 | exit(44); |
| 218 | } |
| 219 | } while(padfile == -1); |
| 220 | |
| 221 | return padfile; |
| 222 | } |
| 223 | |
| 224 | // Pad all the empty spaces in a range of address space to stop interlopers. |
| 225 | void as_pad(void *start, void *end, int padfile) |
| 226 | { |
| 227 | fillgap_extra extra; |
| 228 | extra.fillgap_start = start; |
| 229 | extra.fillgap_end = end; |
| 230 | extra.fillgap_padfile = padfile; |
| 231 | |
| 232 | foreach_map(fillgap, &extra); |
| 233 | |
| 234 | if (extra.fillgap_start < extra.fillgap_end) { |
| 235 | void* res = mmap(extra.fillgap_start, |
| 236 | extra.fillgap_end - extra.fillgap_start, |
| 237 | PROT_NONE, MAP_FIXED|MAP_PRIVATE, padfile, 0); |
| 238 | check_mmap(res, extra.fillgap_start, |
| 239 | extra.fillgap_end - extra.fillgap_start); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /*------------------------------------------------------------*/ |
| 245 | /*--- main() and related pieces ---*/ |
| 246 | /*------------------------------------------------------------*/ |
| 247 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 248 | static int prmap(char *start, char *end, const char *perm, off_t off, int maj, |
| 249 | int min, int ino, void* dummy) { |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 250 | printf("mapping %10p-%10p %s %02x:%02x %d\n", |
| 251 | start, end, perm, maj, min, ino); |
| 252 | return 1; |
| 253 | } |
| 254 | |
nethercote | 9d07242 | 2004-10-13 17:58:36 +0000 | [diff] [blame] | 255 | static void main2(void) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 256 | { |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 257 | int err, padfile; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 258 | struct exeinfo info; |
| 259 | extern char _end; |
| 260 | int *esp; |
| 261 | char buf[strlen(valgrind_lib) + sizeof(stage2) + 16]; |
| 262 | |
nethercote | 5462ef7 | 2004-10-26 17:03:54 +0000 | [diff] [blame] | 263 | info.exe_end = PGROUNDDN(init_sp); |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 264 | #ifdef HAVE_PIE |
| 265 | info.exe_base = ROUNDDN(info.exe_end - 0x02000000, 0x10000000); |
| 266 | assert(info.exe_base >= PGROUNDUP(&_end)); |
| 267 | info.map_base = info.exe_base + 0x01000000; |
| 268 | #else |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 269 | // If this system doesn't have PIE (position-independent executables), |
| 270 | // we have to choose a hardwired location for stage2. |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 271 | info.exe_base = PGROUNDUP(&_end); |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 272 | info.map_base = KICKSTART_BASE + 0x01000000; |
| 273 | #endif |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 274 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 275 | info.argv = NULL; |
| 276 | |
fitzhardinge | a49f9b5 | 2003-12-16 22:26:45 +0000 | [diff] [blame] | 277 | snprintf(buf, sizeof(buf), "%s/%s", valgrind_lib, stage2); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 278 | |
| 279 | err = do_exec(buf, &info); |
| 280 | |
| 281 | if (err != 0) { |
nethercote | 2e1cb4c | 2004-08-05 12:16:13 +0000 | [diff] [blame] | 282 | fprintf(stderr, "valgrind: failed to load %s: %s\n", |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 283 | buf, strerror(err)); |
| 284 | exit(1); |
| 285 | } |
| 286 | |
| 287 | /* Make sure stage2's dynamic linker can't tromp on the lower part |
| 288 | of the address space. */ |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 289 | padfile = as_openpadfile(); |
| 290 | as_pad(0, (void *)info.map_base, padfile); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 291 | |
nethercote | c25c449 | 2004-10-18 11:52:17 +0000 | [diff] [blame] | 292 | esp = fix_auxv(init_sp, &info, padfile); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 293 | |
| 294 | if (0) { |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 295 | printf("---------- launch stage 2 ----------\n"); |
| 296 | printf("eip=%p esp=%p\n", (void *)info.init_eip, esp); |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 297 | foreach_map(prmap, /*dummy*/NULL); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 298 | } |
| 299 | |
nethercote | a3c3cf2 | 2004-11-01 18:38:00 +0000 | [diff] [blame] | 300 | jmp_with_stack(info.init_eip, (Addr)esp); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 301 | } |
| 302 | |
nethercote | c25c449 | 2004-10-18 11:52:17 +0000 | [diff] [blame] | 303 | int main(int argc, char** argv) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 304 | { |
fitzhardinge | 716c2c1 | 2004-03-09 00:57:45 +0000 | [diff] [blame] | 305 | struct rlimit rlim; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 306 | const char *cp = getenv(VALGRINDLIB); |
| 307 | |
| 308 | if (cp != NULL) |
| 309 | valgrind_lib = cp; |
| 310 | |
nethercote | c25c449 | 2004-10-18 11:52:17 +0000 | [diff] [blame] | 311 | // Initial stack pointer is to argc, which is immediately before argv[0] |
nethercote | 41c75da | 2004-10-18 15:34:14 +0000 | [diff] [blame] | 312 | // on the stack. Nb: Assumes argc is word-aligned. |
nethercote | c25c449 | 2004-10-18 11:52:17 +0000 | [diff] [blame] | 313 | init_sp = argv - 1; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 314 | |
fitzhardinge | 716c2c1 | 2004-03-09 00:57:45 +0000 | [diff] [blame] | 315 | /* Set the address space limit as high as it will go, since we make |
| 316 | a lot of very large mappings. */ |
| 317 | getrlimit(RLIMIT_AS, &rlim); |
| 318 | rlim.rlim_cur = rlim.rlim_max; |
| 319 | setrlimit(RLIMIT_AS, &rlim); |
| 320 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 321 | /* move onto another stack so we can play with the main one */ |
nethercote | a3c3cf2 | 2004-11-01 18:38:00 +0000 | [diff] [blame] | 322 | jmp_with_stack((Addr)main2, (Addr)stack + sizeof(stack)); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 323 | } |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 324 | |
| 325 | /*--------------------------------------------------------------------*/ |
| 326 | /*--- end stage1.c ---*/ |
| 327 | /*--------------------------------------------------------------------*/ |