1 2 /*--------------------------------------------------------------------*/ 3 /*--- Startup: create initial process image on Linux ---*/ 4 /*--- initimg-linux.c ---*/ 5 /*--------------------------------------------------------------------*/ 6 7 /* 8 This file is part of Valgrind, a dynamic binary instrumentation 9 framework. 10 11 Copyright (C) 2000-2010 Julian Seward 12 jseward (at) acm.org 13 14 This program is free software; you can redistribute it and/or 15 modify it under the terms of the GNU General Public License as 16 published by the Free Software Foundation; either version 2 of the 17 License, or (at your option) any later version. 18 19 This program is distributed in the hope that it will be useful, but 20 WITHOUT ANY WARRANTY; without even the implied warranty of 21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 General Public License for more details. 23 24 You should have received a copy of the GNU General Public License 25 along with this program; if not, write to the Free Software 26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 27 02111-1307, USA. 28 29 The GNU General Public License is contained in the file COPYING. 30 */ 31 32 #if defined(VGO_linux) 33 34 #include "pub_core_basics.h" 35 #include "pub_core_vki.h" 36 #include "pub_core_debuglog.h" 37 #include "pub_core_libcbase.h" 38 #include "pub_core_libcassert.h" 39 #include "pub_core_libcfile.h" 40 #include "pub_core_libcproc.h" 41 #include "pub_core_libcprint.h" 42 #include "pub_core_xarray.h" 43 #include "pub_core_clientstate.h" 44 #include "pub_core_aspacemgr.h" 45 #include "pub_core_mallocfree.h" 46 #include "pub_core_machine.h" 47 #include "pub_core_ume.h" 48 #include "pub_core_options.h" 49 #include "pub_core_syscall.h" 50 #include "pub_core_tooliface.h" /* VG_TRACK */ 51 #include "pub_core_threadstate.h" /* ThreadArchState */ 52 #include "priv_initimg_pathscan.h" 53 #include "pub_core_initimg.h" /* self */ 54 55 /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */ 56 #define _GNU_SOURCE 57 #define _FILE_OFFSET_BITS 64 58 /* This is for ELF types etc, and also the AT_ constants. */ 59 #ifdef ANDROID 60 #include <linux/elf.h> 61 #define AT_FPUCW 18 62 #else 63 #include <elf.h> 64 #endif 65 /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */ 66 67 68 /*====================================================================*/ 69 /*=== Loading the client ===*/ 70 /*====================================================================*/ 71 72 /* Load the client whose name is VG_(argv_the_exename). */ 73 74 static void load_client ( /*OUT*/ExeInfo* info, 75 /*OUT*/Addr* client_ip, 76 /*OUT*/Addr* client_toc) 77 { 78 HChar* exe_name; 79 Int ret; 80 SysRes res; 81 82 vg_assert( VG_(args_the_exename) != NULL); 83 exe_name = ML_(find_executable)( VG_(args_the_exename) ); 84 85 if (!exe_name) { 86 VG_(printf)("valgrind: %s: command not found\n", VG_(args_the_exename)); 87 VG_(exit)(127); // 127 is Posix NOTFOUND 88 } 89 90 VG_(memset)(info, 0, sizeof(*info)); 91 ret = VG_(do_exec)(exe_name, info); 92 if (ret < 0) { 93 VG_(printf)("valgrind: could not execute '%s'\n", exe_name); 94 VG_(exit)(1); 95 } 96 97 // The client was successfully loaded! Continue. 98 99 /* Get hold of a file descriptor which refers to the client 100 executable. This is needed for attaching to GDB. */ 101 res = VG_(open)(exe_name, VKI_O_RDONLY, VKI_S_IRUSR); 102 if (!sr_isError(res)) 103 VG_(cl_exec_fd) = sr_Res(res); 104 105 /* Copy necessary bits of 'info' that were filled in */ 106 *client_ip = info->init_ip; 107 *client_toc = info->init_toc; 108 VG_(brk_base) = VG_(brk_limit) = VG_PGROUNDUP(info->brkbase); 109 } 110 111 112 /*====================================================================*/ 113 /*=== Setting up the client's environment ===*/ 114 /*====================================================================*/ 115 116 /* Prepare the client's environment. This is basically a copy of our 117 environment, except: 118 119 LD_PRELOAD=$VALGRIND_LIB/vgpreload_core-PLATFORM.so: 120 ($VALGRIND_LIB/vgpreload_TOOL-PLATFORM.so:)? 121 $LD_PRELOAD 122 123 If this is missing, then it is added. 124 125 Also, remove any binding for VALGRIND_LAUNCHER=. The client should 126 not be able to see this. 127 128 If this needs to handle any more variables it should be hacked 129 into something table driven. The copy is VG_(malloc)'d space. 130 */ 131 static HChar** setup_client_env ( HChar** origenv, const HChar* toolname) 132 { 133 HChar* preload_core = "vgpreload_core"; 134 HChar* ld_preload = "LD_PRELOAD="; 135 HChar* v_launcher = VALGRIND_LAUNCHER "="; 136 Int ld_preload_len = VG_(strlen)( ld_preload ); 137 Int v_launcher_len = VG_(strlen)( v_launcher ); 138 Bool ld_preload_done = False; 139 Int vglib_len = VG_(strlen)(VG_(libdir)); 140 Bool debug = False; 141 142 HChar** cpp; 143 HChar** ret; 144 HChar* preload_tool_path; 145 Int envc, i; 146 147 /* Alloc space for the vgpreload_core.so path and vgpreload_<tool>.so 148 paths. We might not need the space for vgpreload_<tool>.so, but it 149 doesn't hurt to over-allocate briefly. The 16s are just cautious 150 slop. */ 151 Int preload_core_path_len = vglib_len + sizeof(preload_core) 152 + sizeof(VG_PLATFORM) + 16; 153 Int preload_tool_path_len = vglib_len + VG_(strlen)(toolname) 154 + sizeof(VG_PLATFORM) + 16; 155 Int preload_string_len = preload_core_path_len + preload_tool_path_len; 156 HChar* preload_string = VG_(malloc)("initimg-linux.sce.1", 157 preload_string_len); 158 vg_assert(origenv); 159 vg_assert(toolname); 160 vg_assert(preload_string); 161 162 /* Determine if there's a vgpreload_<tool>_<platform>.so file, and setup 163 preload_string. */ 164 preload_tool_path = VG_(malloc)("initimg-linux.sce.2", preload_tool_path_len); 165 vg_assert(preload_tool_path); 166 VG_(snprintf)(preload_tool_path, preload_tool_path_len, 167 "%s/vgpreload_%s-%s.so", VG_(libdir), toolname, VG_PLATFORM); 168 if (VG_(access)(preload_tool_path, True/*r*/, False/*w*/, False/*x*/) == 0) { 169 VG_(snprintf)(preload_string, preload_string_len, "%s/%s-%s.so:%s", 170 VG_(libdir), preload_core, VG_PLATFORM, preload_tool_path); 171 } else { 172 VG_(snprintf)(preload_string, preload_string_len, "%s/%s-%s.so", 173 VG_(libdir), preload_core, VG_PLATFORM); 174 } 175 VG_(free)(preload_tool_path); 176 177 VG_(debugLog)(2, "initimg", "preload_string:\n"); 178 VG_(debugLog)(2, "initimg", " \"%s\"\n", preload_string); 179 180 /* Count the original size of the env */ 181 if (debug) VG_(printf)("\n\n"); 182 envc = 0; 183 for (cpp = origenv; cpp && *cpp; cpp++) { 184 envc++; 185 if (debug) VG_(printf)("XXXXXXXXX: BEFORE %s\n", *cpp); 186 } 187 188 /* Allocate a new space */ 189 ret = VG_(malloc) ("initimg-linux.sce.3", 190 sizeof(HChar *) * (envc+1+1)); /* 1 new entry + NULL */ 191 vg_assert(ret); 192 193 /* copy it over */ 194 for (cpp = ret; *origenv; ) { 195 if (debug) VG_(printf)("XXXXXXXXX: COPY %s\n", *origenv); 196 *cpp++ = *origenv++; 197 } 198 *cpp = NULL; 199 200 vg_assert(envc == (cpp - ret)); 201 202 /* Walk over the new environment, mashing as we go */ 203 for (cpp = ret; cpp && *cpp; cpp++) { 204 if (VG_(memcmp)(*cpp, ld_preload, ld_preload_len) == 0) { 205 Int len = VG_(strlen)(*cpp) + preload_string_len; 206 HChar *cp = VG_(malloc)("initimg-linux.sce.4", len); 207 vg_assert(cp); 208 209 VG_(snprintf)(cp, len, "%s%s:%s", 210 ld_preload, preload_string, (*cpp)+ld_preload_len); 211 212 *cpp = cp; 213 214 ld_preload_done = True; 215 } 216 if (debug) VG_(printf)("XXXXXXXXX: MASH %s\n", *cpp); 217 } 218 219 /* Add the missing bits */ 220 if (!ld_preload_done) { 221 Int len = ld_preload_len + preload_string_len; 222 HChar *cp = VG_(malloc) ("initimg-linux.sce.5", len); 223 vg_assert(cp); 224 225 VG_(snprintf)(cp, len, "%s%s", ld_preload, preload_string); 226 227 ret[envc++] = cp; 228 if (debug) VG_(printf)("XXXXXXXXX: ADD %s\n", cp); 229 } 230 231 /* ret[0 .. envc-1] is live now. */ 232 /* Find and remove a binding for VALGRIND_LAUNCHER. */ 233 for (i = 0; i < envc; i++) 234 if (0 == VG_(memcmp(ret[i], v_launcher, v_launcher_len))) 235 break; 236 237 if (i < envc) { 238 for (; i < envc-1; i++) 239 ret[i] = ret[i+1]; 240 envc--; 241 } 242 243 VG_(free)(preload_string); 244 ret[envc] = NULL; 245 246 for (i = 0; i < envc; i++) { 247 if (debug) VG_(printf)("XXXXXXXXX: FINAL %s\n", ret[i]); 248 } 249 250 return ret; 251 } 252 253 254 /*====================================================================*/ 255 /*=== Setting up the client's stack ===*/ 256 /*====================================================================*/ 257 258 #ifndef AT_DCACHEBSIZE 259 #define AT_DCACHEBSIZE 19 260 #endif /* AT_DCACHEBSIZE */ 261 262 #ifndef AT_ICACHEBSIZE 263 #define AT_ICACHEBSIZE 20 264 #endif /* AT_ICACHEBSIZE */ 265 266 #ifndef AT_UCACHEBSIZE 267 #define AT_UCACHEBSIZE 21 268 #endif /* AT_UCACHEBSIZE */ 269 270 #ifndef AT_BASE_PLATFORM 271 #define AT_BASE_PLATFORM 24 272 #endif /* AT_BASE_PLATFORM */ 273 274 #ifndef AT_RANDOM 275 #define AT_RANDOM 25 276 #endif /* AT_RANDOM */ 277 278 #ifndef AT_EXECFN 279 #define AT_EXECFN 31 280 #endif /* AT_EXECFN */ 281 282 #ifndef AT_SYSINFO 283 #define AT_SYSINFO 32 284 #endif /* AT_SYSINFO */ 285 286 #ifndef AT_SYSINFO_EHDR 287 #define AT_SYSINFO_EHDR 33 288 #endif /* AT_SYSINFO_EHDR */ 289 290 #ifndef AT_SECURE 291 #define AT_SECURE 23 /* secure mode boolean */ 292 #endif /* AT_SECURE */ 293 294 /* Add a string onto the string table, and return its address */ 295 static char *copy_str(char **tab, const char *str) 296 { 297 char *cp = *tab; 298 char *orig = cp; 299 300 while(*str) 301 *cp++ = *str++; 302 *cp++ = '\0'; 303 304 if (0) 305 VG_(printf)("copied %p \"%s\" len %lld\n", orig, orig, (Long)(cp-orig)); 306 307 *tab = cp; 308 309 return orig; 310 } 311 312 313 /* ---------------------------------------------------------------- 314 315 This sets up the client's initial stack, containing the args, 316 environment and aux vector. 317 318 The format of the stack is: 319 320 higher address +-----------------+ <- clstack_end 321 | | 322 : string table : 323 | | 324 +-----------------+ 325 | AT_NULL | 326 - - 327 | auxv | 328 +-----------------+ 329 | NULL | 330 - - 331 | envp | 332 +-----------------+ 333 | NULL | 334 - - 335 | argv | 336 +-----------------+ 337 | argc | 338 lower address +-----------------+ <- sp 339 | undefined | 340 : : 341 342 Allocate and create the initial client stack. It is allocated down 343 from clstack_end, which was previously determined by the address 344 space manager. The returned value is the SP value for the client. 345 346 The client's auxv is created by copying and modifying our own one. 347 As a side effect of scanning our own auxv, some important bits of 348 info are collected: 349 350 VG_(cache_line_size_ppc32) // ppc32 only -- cache line size 351 VG_(have_altivec_ppc32) // ppc32 only -- is Altivec supported? 352 353 ---------------------------------------------------------------- */ 354 355 struct auxv 356 { 357 Word a_type; 358 union { 359 void *a_ptr; 360 Word a_val; 361 } u; 362 }; 363 364 static 365 struct auxv *find_auxv(UWord* sp) 366 { 367 sp++; // skip argc (Nb: is word-sized, not int-sized!) 368 369 while (*sp != 0) // skip argv 370 sp++; 371 sp++; 372 373 while (*sp != 0) // skip env 374 sp++; 375 sp++; 376 377 #if defined(VGA_ppc32) || defined(VGA_ppc64) 378 # if defined AT_IGNOREPPC 379 while (*sp == AT_IGNOREPPC) // skip AT_IGNOREPPC entries 380 sp += 2; 381 # endif 382 #endif 383 384 return (struct auxv *)sp; 385 } 386 387 static 388 Addr setup_client_stack( void* init_sp, 389 char** orig_envp, 390 const ExeInfo* info, 391 UInt** client_auxv, 392 Addr clstack_end, 393 SizeT clstack_max_size ) 394 { 395 SysRes res; 396 char **cpp; 397 char *strtab; /* string table */ 398 char *stringbase; 399 Addr *ptr; 400 struct auxv *auxv; 401 const struct auxv *orig_auxv; 402 const struct auxv *cauxv; 403 unsigned stringsize; /* total size of strings in bytes */ 404 unsigned auxsize; /* total size of auxv in bytes */ 405 Int argc; /* total argc */ 406 Int envc; /* total number of env vars */ 407 unsigned stacksize; /* total client stack size */ 408 Addr client_SP; /* client stack base (initial SP) */ 409 Addr clstack_start; 410 Int i; 411 Bool have_exename; 412 413 vg_assert(VG_IS_PAGE_ALIGNED(clstack_end+1)); 414 vg_assert( VG_(args_for_client) ); 415 416 /* use our own auxv as a prototype */ 417 orig_auxv = find_auxv(init_sp); 418 419 /* ==================== compute sizes ==================== */ 420 421 /* first of all, work out how big the client stack will be */ 422 stringsize = 0; 423 have_exename = VG_(args_the_exename) != NULL; 424 425 /* paste on the extra args if the loader needs them (ie, the #! 426 interpreter and its argument) */ 427 argc = 0; 428 if (info->interp_name != NULL) { 429 argc++; 430 stringsize += VG_(strlen)(info->interp_name) + 1; 431 } 432 if (info->interp_args != NULL) { 433 argc++; 434 stringsize += VG_(strlen)(info->interp_args) + 1; 435 } 436 437 /* now scan the args we're given... */ 438 if (have_exename) 439 stringsize += VG_(strlen)( VG_(args_the_exename) ) + 1; 440 441 for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) { 442 argc++; 443 stringsize += VG_(strlen)( * (HChar**) 444 VG_(indexXA)( VG_(args_for_client), i )) 445 + 1; 446 } 447 448 /* ...and the environment */ 449 envc = 0; 450 for (cpp = orig_envp; cpp && *cpp; cpp++) { 451 envc++; 452 stringsize += VG_(strlen)(*cpp) + 1; 453 } 454 455 /* now, how big is the auxv? */ 456 auxsize = sizeof(*auxv); /* there's always at least one entry: AT_NULL */ 457 for (cauxv = orig_auxv; cauxv->a_type != AT_NULL; cauxv++) { 458 if (cauxv->a_type == AT_PLATFORM || 459 cauxv->a_type == AT_BASE_PLATFORM) 460 stringsize += VG_(strlen)(cauxv->u.a_ptr) + 1; 461 else if (cauxv->a_type == AT_RANDOM) 462 stringsize += 16; 463 else if (cauxv->a_type == AT_EXECFN) 464 stringsize += VG_(strlen)(VG_(args_the_exename)) + 1; 465 auxsize += sizeof(*cauxv); 466 } 467 468 # if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) 469 auxsize += 2 * sizeof(*cauxv); 470 # endif 471 472 /* OK, now we know how big the client stack is */ 473 stacksize = 474 sizeof(Word) + /* argc */ 475 (have_exename ? sizeof(char **) : 0) + /* argc[0] == exename */ 476 sizeof(char **)*argc + /* argv */ 477 sizeof(char **) + /* terminal NULL */ 478 sizeof(char **)*envc + /* envp */ 479 sizeof(char **) + /* terminal NULL */ 480 auxsize + /* auxv */ 481 VG_ROUNDUP(stringsize, sizeof(Word)); /* strings (aligned) */ 482 483 if (0) VG_(printf)("stacksize = %d\n", stacksize); 484 485 /* client_SP is the client's stack pointer */ 486 client_SP = clstack_end - stacksize; 487 client_SP = VG_ROUNDDN(client_SP, 16); /* make stack 16 byte aligned */ 488 489 /* base of the string table (aligned) */ 490 stringbase = strtab = (char *)clstack_end 491 - VG_ROUNDUP(stringsize, sizeof(int)); 492 493 clstack_start = VG_PGROUNDDN(client_SP); 494 495 /* The max stack size */ 496 clstack_max_size = VG_PGROUNDUP(clstack_max_size); 497 498 /* Record stack extent -- needed for stack-change code. */ 499 VG_(clstk_base) = clstack_start; 500 VG_(clstk_end) = clstack_end; 501 502 if (0) 503 VG_(printf)("stringsize=%d auxsize=%d stacksize=%d maxsize=0x%x\n" 504 "clstack_start %p\n" 505 "clstack_end %p\n", 506 stringsize, auxsize, stacksize, (Int)clstack_max_size, 507 (void*)clstack_start, (void*)clstack_end); 508 509 /* ==================== allocate space ==================== */ 510 511 { SizeT anon_size = clstack_end - clstack_start + 1; 512 SizeT resvn_size = clstack_max_size - anon_size; 513 Addr anon_start = clstack_start; 514 Addr resvn_start = anon_start - resvn_size; 515 SizeT inner_HACK = 0; 516 Bool ok; 517 518 /* So far we've only accounted for space requirements down to the 519 stack pointer. If this target's ABI requires a redzone below 520 the stack pointer, we need to allocate an extra page, to 521 handle the worst case in which the stack pointer is almost at 522 the bottom of a page, and so there is insufficient room left 523 over to put the redzone in. In this case the simple thing to 524 do is allocate an extra page, by shrinking the reservation by 525 one page and growing the anonymous area by a corresponding 526 page. */ 527 vg_assert(VG_STACK_REDZONE_SZB >= 0); 528 vg_assert(VG_STACK_REDZONE_SZB < VKI_PAGE_SIZE); 529 if (VG_STACK_REDZONE_SZB > 0) { 530 vg_assert(resvn_size > VKI_PAGE_SIZE); 531 resvn_size -= VKI_PAGE_SIZE; 532 anon_start -= VKI_PAGE_SIZE; 533 anon_size += VKI_PAGE_SIZE; 534 } 535 536 vg_assert(VG_IS_PAGE_ALIGNED(anon_size)); 537 vg_assert(VG_IS_PAGE_ALIGNED(resvn_size)); 538 vg_assert(VG_IS_PAGE_ALIGNED(anon_start)); 539 vg_assert(VG_IS_PAGE_ALIGNED(resvn_start)); 540 vg_assert(resvn_start == clstack_end + 1 - clstack_max_size); 541 542 # ifdef ENABLE_INNER 543 inner_HACK = 1024*1024; // create 1M non-fault-extending stack 544 # endif 545 546 if (0) 547 VG_(printf)("%#lx 0x%lx %#lx 0x%lx\n", 548 resvn_start, resvn_size, anon_start, anon_size); 549 550 /* Create a shrinkable reservation followed by an anonymous 551 segment. Together these constitute a growdown stack. */ 552 res = VG_(mk_SysRes_Error)(0); 553 ok = VG_(am_create_reservation)( 554 resvn_start, 555 resvn_size -inner_HACK, 556 SmUpper, 557 anon_size +inner_HACK 558 ); 559 if (ok) { 560 /* allocate a stack - mmap enough space for the stack */ 561 res = VG_(am_mmap_anon_fixed_client)( 562 anon_start -inner_HACK, 563 anon_size +inner_HACK, 564 VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC 565 ); 566 } 567 if ((!ok) || sr_isError(res)) { 568 /* Allocation of the stack failed. We have to stop. */ 569 VG_(printf)("valgrind: " 570 "I failed to allocate space for the application's stack.\n"); 571 VG_(printf)("valgrind: " 572 "This may be the result of a very large --main-stacksize=\n"); 573 VG_(printf)("valgrind: setting. Cannot continue. Sorry.\n\n"); 574 VG_(exit)(0); 575 } 576 577 vg_assert(ok); 578 vg_assert(!sr_isError(res)); 579 } 580 581 /* ==================== create client stack ==================== */ 582 583 ptr = (Addr*)client_SP; 584 585 /* --- client argc --- */ 586 *ptr++ = argc + (have_exename ? 1 : 0); 587 588 /* --- client argv --- */ 589 if (info->interp_name) { 590 *ptr++ = (Addr)copy_str(&strtab, info->interp_name); 591 VG_(free)(info->interp_name); 592 } 593 if (info->interp_args) { 594 *ptr++ = (Addr)copy_str(&strtab, info->interp_args); 595 VG_(free)(info->interp_args); 596 } 597 598 if (have_exename) 599 *ptr++ = (Addr)copy_str(&strtab, VG_(args_the_exename)); 600 601 for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) { 602 *ptr++ = (Addr)copy_str( 603 &strtab, 604 * (HChar**) VG_(indexXA)( VG_(args_for_client), i ) 605 ); 606 } 607 *ptr++ = 0; 608 609 /* --- envp --- */ 610 VG_(client_envp) = (Char **)ptr; 611 for (cpp = orig_envp; cpp && *cpp; ptr++, cpp++) 612 *ptr = (Addr)copy_str(&strtab, *cpp); 613 *ptr++ = 0; 614 615 /* --- auxv --- */ 616 auxv = (struct auxv *)ptr; 617 *client_auxv = (UInt *)auxv; 618 619 # if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) 620 auxv[0].a_type = AT_IGNOREPPC; 621 auxv[0].u.a_val = AT_IGNOREPPC; 622 auxv[1].a_type = AT_IGNOREPPC; 623 auxv[1].u.a_val = AT_IGNOREPPC; 624 auxv += 2; 625 # endif 626 627 for (; orig_auxv->a_type != AT_NULL; auxv++, orig_auxv++) { 628 const NSegment *ehdrseg; 629 630 /* copy the entry... */ 631 *auxv = *orig_auxv; 632 633 /* ...and fix up / examine the copy */ 634 switch(auxv->a_type) { 635 636 case AT_IGNORE: 637 case AT_PHENT: 638 case AT_PAGESZ: 639 case AT_FLAGS: 640 case AT_NOTELF: 641 case AT_UID: 642 case AT_EUID: 643 case AT_GID: 644 case AT_EGID: 645 case AT_CLKTCK: 646 case AT_FPUCW: 647 /* All these are pointerless, so we don't need to do 648 anything about them. */ 649 break; 650 651 case AT_PHDR: 652 if (info->phdr == 0) 653 auxv->a_type = AT_IGNORE; 654 else 655 auxv->u.a_val = info->phdr; 656 break; 657 658 case AT_PHNUM: 659 if (info->phdr == 0) 660 auxv->a_type = AT_IGNORE; 661 else 662 auxv->u.a_val = info->phnum; 663 break; 664 665 case AT_BASE: 666 auxv->u.a_val = info->interp_base; 667 break; 668 669 case AT_PLATFORM: 670 case AT_BASE_PLATFORM: 671 /* points to a platform description string */ 672 auxv->u.a_ptr = copy_str(&strtab, orig_auxv->u.a_ptr); 673 break; 674 675 case AT_ENTRY: 676 auxv->u.a_val = info->entry; 677 break; 678 679 case AT_HWCAP: 680 # if defined(VGP_arm_linux) 681 { Bool has_neon = (auxv->u.a_val & VKI_HWCAP_NEON) > 0; 682 VG_(debugLog)(2, "initimg", 683 "ARM has-neon from-auxv: %s\n", 684 has_neon ? "YES" : "NO"); 685 VG_(machine_arm_set_has_NEON)( has_neon ); 686 } 687 # endif 688 break; 689 690 case AT_DCACHEBSIZE: 691 case AT_ICACHEBSIZE: 692 case AT_UCACHEBSIZE: 693 # if defined(VGP_ppc32_linux) 694 /* acquire cache info */ 695 if (auxv->u.a_val > 0) { 696 VG_(machine_ppc32_set_clszB)( auxv->u.a_val ); 697 VG_(debugLog)(2, "initimg", 698 "PPC32 cache line size %u (type %u)\n", 699 (UInt)auxv->u.a_val, (UInt)auxv->a_type ); 700 } 701 # elif defined(VGP_ppc64_linux) 702 /* acquire cache info */ 703 if (auxv->u.a_val > 0) { 704 VG_(machine_ppc64_set_clszB)( auxv->u.a_val ); 705 VG_(debugLog)(2, "initimg", 706 "PPC64 cache line size %u (type %u)\n", 707 (UInt)auxv->u.a_val, (UInt)auxv->a_type ); 708 } 709 # endif 710 break; 711 712 # if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) 713 case AT_IGNOREPPC: 714 break; 715 # endif 716 717 case AT_SECURE: 718 /* If this is 1, then it means that this program is 719 running suid, and therefore the dynamic linker should 720 be careful about LD_PRELOAD, etc. However, since 721 stage1 (the thing the kernel actually execve's) should 722 never be SUID, and we need LD_PRELOAD to work for the 723 client, we set AT_SECURE to 0. */ 724 auxv->u.a_val = 0; 725 break; 726 727 case AT_SYSINFO: 728 /* Trash this, because we don't reproduce it */ 729 auxv->a_type = AT_IGNORE; 730 break; 731 732 # if !defined(VGP_ppc32_linux) && !defined(VGP_ppc64_linux) 733 case AT_SYSINFO_EHDR: 734 /* Trash this, because we don't reproduce it */ 735 ehdrseg = VG_(am_find_nsegment)((Addr)auxv->u.a_ptr); 736 vg_assert(ehdrseg); 737 VG_(am_munmap_valgrind)(ehdrseg->start, ehdrseg->end - ehdrseg->start); 738 auxv->a_type = AT_IGNORE; 739 break; 740 # endif 741 742 case AT_RANDOM: 743 /* points to 16 random bytes - we need to ensure this is 744 propagated to the client as glibc will assume it is 745 present if it is built for kernel 2.6.29 or later */ 746 auxv->u.a_ptr = strtab; 747 VG_(memcpy)(strtab, orig_auxv->u.a_ptr, 16); 748 strtab += 16; 749 break; 750 751 case AT_EXECFN: 752 /* points to the executable filename */ 753 auxv->u.a_ptr = copy_str(&strtab, VG_(args_the_exename)); 754 break; 755 756 default: 757 /* stomp out anything we don't know about */ 758 VG_(debugLog)(2, "initimg", 759 "stomping auxv entry %lld\n", 760 (ULong)auxv->a_type); 761 auxv->a_type = AT_IGNORE; 762 break; 763 } 764 } 765 *auxv = *orig_auxv; 766 vg_assert(auxv->a_type == AT_NULL); 767 768 vg_assert((strtab-stringbase) == stringsize); 769 770 /* client_SP is pointing at client's argc/argv */ 771 772 if (0) VG_(printf)("startup SP = %#lx\n", client_SP); 773 return client_SP; 774 } 775 776 777 /* Allocate the client data segment. It is an expandable anonymous 778 mapping abutting a shrinkable reservation of size max_dseg_size. 779 The data segment starts at VG_(brk_base), which is page-aligned, 780 and runs up to VG_(brk_limit), which isn't. */ 781 782 static void setup_client_dataseg ( SizeT max_size ) 783 { 784 Bool ok; 785 SysRes sres; 786 Addr anon_start = VG_(brk_base); 787 SizeT anon_size = VKI_PAGE_SIZE; 788 Addr resvn_start = anon_start + anon_size; 789 SizeT resvn_size = max_size - anon_size; 790 791 vg_assert(VG_IS_PAGE_ALIGNED(anon_size)); 792 vg_assert(VG_IS_PAGE_ALIGNED(resvn_size)); 793 vg_assert(VG_IS_PAGE_ALIGNED(anon_start)); 794 vg_assert(VG_IS_PAGE_ALIGNED(resvn_start)); 795 796 /* Because there's been no brk activity yet: */ 797 vg_assert(VG_(brk_base) == VG_(brk_limit)); 798 799 /* Try to create the data seg and associated reservation where 800 VG_(brk_base) says. */ 801 ok = VG_(am_create_reservation)( 802 resvn_start, 803 resvn_size, 804 SmLower, 805 anon_size 806 ); 807 808 if (!ok) { 809 /* Hmm, that didn't work. Well, let aspacem suggest an address 810 it likes better, and try again with that. */ 811 anon_start = VG_(am_get_advisory_client_simple) 812 ( 0/*floating*/, anon_size+resvn_size, &ok ); 813 if (ok) { 814 resvn_start = anon_start + anon_size; 815 ok = VG_(am_create_reservation)( 816 resvn_start, 817 resvn_size, 818 SmLower, 819 anon_size 820 ); 821 if (ok) 822 VG_(brk_base) = VG_(brk_limit) = anon_start; 823 } 824 /* that too might have failed, but if it has, we're hosed: there 825 is no Plan C. */ 826 } 827 vg_assert(ok); 828 829 /* We make the data segment (heap) executable because LinuxThreads on 830 ppc32 creates trampolines in this area. Also, on x86/Linux the data 831 segment is RWX natively, at least according to /proc/self/maps. 832 Also, having a non-executable data seg would kill any program which 833 tried to create code in the data seg and then run it. */ 834 sres = VG_(am_mmap_anon_fixed_client)( 835 anon_start, 836 anon_size, 837 VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC 838 ); 839 vg_assert(!sr_isError(sres)); 840 vg_assert(sr_Res(sres) == anon_start); 841 } 842 843 844 /*====================================================================*/ 845 /*=== TOP-LEVEL: VG_(setup_client_initial_image) ===*/ 846 /*====================================================================*/ 847 848 /* Create the client's initial memory image. */ 849 IIFinaliseImageInfo VG_(ii_create_image)( IICreateImageInfo iicii ) 850 { 851 ExeInfo info; 852 HChar** env = NULL; 853 854 IIFinaliseImageInfo iifii; 855 VG_(memset)( &iifii, 0, sizeof(iifii) ); 856 857 //-------------------------------------------------------------- 858 // Load client executable, finding in $PATH if necessary 859 // p: get_helprequest_and_toolname() [for 'exec', 'need_help'] 860 // p: layout_remaining_space [so there's space] 861 //-------------------------------------------------------------- 862 VG_(debugLog)(1, "initimg", "Loading client\n"); 863 864 if (VG_(args_the_exename) == NULL) 865 VG_(err_missing_prog)(); 866 867 load_client(&info, &iifii.initial_client_IP, &iifii.initial_client_TOC); 868 869 //-------------------------------------------------------------- 870 // Set up client's environment 871 // p: set-libdir [for VG_(libdir)] 872 // p: get_helprequest_and_toolname [for toolname] 873 //-------------------------------------------------------------- 874 VG_(debugLog)(1, "initimg", "Setup client env\n"); 875 env = setup_client_env(iicii.envp, iicii.toolname); 876 877 //-------------------------------------------------------------- 878 // Setup client stack, eip, and VG_(client_arg[cv]) 879 // p: load_client() [for 'info'] 880 // p: fix_environment() [for 'env'] 881 //-------------------------------------------------------------- 882 { 883 /* When allocating space for the client stack on Linux, take 884 notice of the --main-stacksize value. This makes it possible 885 to run programs with very large (primary) stack requirements 886 simply by specifying --main-stacksize. */ 887 /* Logic is as follows: 888 - by default, use the client's current stack rlimit 889 - if that exceeds 16M, clamp to 16M 890 - if a larger --main-stacksize value is specified, use that instead 891 - in all situations, the minimum allowed stack size is 1M 892 */ 893 void* init_sp = iicii.argv - 1; 894 SizeT m1 = 1024 * 1024; 895 SizeT m16 = 16 * m1; 896 SizeT szB = (SizeT)VG_(client_rlimit_stack).rlim_cur; 897 if (szB < m1) szB = m1; 898 if (szB > m16) szB = m16; 899 if (VG_(clo_main_stacksize) > 0) szB = VG_(clo_main_stacksize); 900 if (szB < m1) szB = m1; 901 szB = VG_PGROUNDUP(szB); 902 VG_(debugLog)(1, "initimg", 903 "Setup client stack: size will be %ld\n", szB); 904 905 iifii.clstack_max_size = szB; 906 907 iifii.initial_client_SP 908 = setup_client_stack( init_sp, env, 909 &info, &iifii.client_auxv, 910 iicii.clstack_top, iifii.clstack_max_size ); 911 912 VG_(free)(env); 913 914 VG_(debugLog)(2, "initimg", 915 "Client info: " 916 "initial_IP=%p initial_TOC=%p brk_base=%p\n", 917 (void*)(iifii.initial_client_IP), 918 (void*)(iifii.initial_client_TOC), 919 (void*)VG_(brk_base) ); 920 VG_(debugLog)(2, "initimg", 921 "Client info: " 922 "initial_SP=%p max_stack_size=%ld\n", 923 (void*)(iifii.initial_client_SP), 924 (SizeT)iifii.clstack_max_size ); 925 } 926 927 //-------------------------------------------------------------- 928 // Setup client data (brk) segment. Initially a 1-page segment 929 // which abuts a shrinkable reservation. 930 // p: load_client() [for 'info' and hence VG_(brk_base)] 931 //-------------------------------------------------------------- 932 { 933 SizeT m1 = 1024 * 1024; 934 SizeT m8 = 8 * m1; 935 SizeT dseg_max_size = (SizeT)VG_(client_rlimit_data).rlim_cur; 936 VG_(debugLog)(1, "initimg", "Setup client data (brk) segment\n"); 937 if (dseg_max_size < m1) dseg_max_size = m1; 938 if (dseg_max_size > m8) dseg_max_size = m8; 939 dseg_max_size = VG_PGROUNDUP(dseg_max_size); 940 941 setup_client_dataseg( dseg_max_size ); 942 } 943 944 return iifii; 945 } 946 947 948 /*====================================================================*/ 949 /*=== TOP-LEVEL: VG_(finalise_thread1state) ===*/ 950 /*====================================================================*/ 951 952 /* Just before starting the client, we may need to make final 953 adjustments to its initial image. Also we need to set up the VEX 954 guest state for thread 1 (the root thread) and copy in essential 955 starting values. This is handed the IIFinaliseImageInfo created by 956 VG_(ii_create_image). 957 */ 958 void VG_(ii_finalise_image)( IIFinaliseImageInfo iifii ) 959 { 960 ThreadArchState* arch = &VG_(threads)[1].arch; 961 962 /* On Linux we get client_{ip/sp/toc}, and start the client with 963 all other registers zeroed. */ 964 965 # if defined(VGP_x86_linux) 966 vg_assert(0 == sizeof(VexGuestX86State) % 16); 967 968 /* Zero out the initial state, and set up the simulated FPU in a 969 sane way. */ 970 LibVEX_GuestX86_initialise(&arch->vex); 971 972 /* Zero out the shadow areas. */ 973 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestX86State)); 974 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestX86State)); 975 976 /* Put essential stuff into the new state. */ 977 arch->vex.guest_ESP = iifii.initial_client_SP; 978 arch->vex.guest_EIP = iifii.initial_client_IP; 979 980 /* initialise %cs, %ds and %ss to point at the operating systems 981 default code, data and stack segments */ 982 asm volatile("movw %%cs, %0" : : "m" (arch->vex.guest_CS)); 983 asm volatile("movw %%ds, %0" : : "m" (arch->vex.guest_DS)); 984 asm volatile("movw %%ss, %0" : : "m" (arch->vex.guest_SS)); 985 986 # elif defined(VGP_amd64_linux) 987 vg_assert(0 == sizeof(VexGuestAMD64State) % 16); 988 989 /* Zero out the initial state, and set up the simulated FPU in a 990 sane way. */ 991 LibVEX_GuestAMD64_initialise(&arch->vex); 992 993 /* Zero out the shadow areas. */ 994 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestAMD64State)); 995 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestAMD64State)); 996 997 /* Put essential stuff into the new state. */ 998 arch->vex.guest_RSP = iifii.initial_client_SP; 999 arch->vex.guest_RIP = iifii.initial_client_IP; 1000 1001 # elif defined(VGP_ppc32_linux) 1002 vg_assert(0 == sizeof(VexGuestPPC32State) % 16); 1003 1004 /* Zero out the initial state, and set up the simulated FPU in a 1005 sane way. */ 1006 LibVEX_GuestPPC32_initialise(&arch->vex); 1007 1008 /* Zero out the shadow areas. */ 1009 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestPPC32State)); 1010 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestPPC32State)); 1011 1012 /* Put essential stuff into the new state. */ 1013 arch->vex.guest_GPR1 = iifii.initial_client_SP; 1014 arch->vex.guest_CIA = iifii.initial_client_IP; 1015 1016 # elif defined(VGP_ppc64_linux) 1017 vg_assert(0 == sizeof(VexGuestPPC64State) % 16); 1018 1019 /* Zero out the initial state, and set up the simulated FPU in a 1020 sane way. */ 1021 LibVEX_GuestPPC64_initialise(&arch->vex); 1022 1023 /* Zero out the shadow areas. */ 1024 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestPPC64State)); 1025 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestPPC64State)); 1026 1027 /* Put essential stuff into the new state. */ 1028 arch->vex.guest_GPR1 = iifii.initial_client_SP; 1029 arch->vex.guest_GPR2 = iifii.initial_client_TOC; 1030 arch->vex.guest_CIA = iifii.initial_client_IP; 1031 1032 # elif defined(VGP_arm_linux) 1033 /* Zero out the initial state, and set up the simulated FPU in a 1034 sane way. */ 1035 LibVEX_GuestARM_initialise(&arch->vex); 1036 1037 /* Zero out the shadow areas. */ 1038 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestARMState)); 1039 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestARMState)); 1040 1041 arch->vex.guest_R13 = iifii.initial_client_SP; 1042 arch->vex.guest_R15T = iifii.initial_client_IP; 1043 1044 /* This is just EABI stuff. */ 1045 // FIXME jrs: what's this for? 1046 arch->vex.guest_R1 = iifii.initial_client_SP; 1047 1048 # else 1049 # error Unknown platform 1050 # endif 1051 1052 /* Tell the tool that we just wrote to the registers. */ 1053 VG_TRACK( post_reg_write, Vg_CoreStartup, /*tid*/1, /*offset*/0, 1054 sizeof(VexGuestArchState)); 1055 } 1056 1057 #endif // defined(VGO_linux) 1058 1059 /*--------------------------------------------------------------------*/ 1060 /*--- ---*/ 1061 /*--------------------------------------------------------------------*/ 1062