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