Home | History | Annotate | Download | only in m_initimg
      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-2012 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_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 
     70 static 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 */
    127 static 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 */
    291 static 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 
    351 struct auxv
    352 {
    353    Word a_type;
    354    union {
    355       void *a_ptr;
    356       Word a_val;
    357    } u;
    358 };
    359 
    360 static
    361 struct 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 
    383 static
    384 Addr 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                However, ignoring AT_BASE makes V crash on Android 4.1.
    673                So, keep the AT_BASE on android for now.
    674                ??? Need to dig in depth about AT_BASE/GDB interaction */
    675 #           if !defined(VGPV_arm_linux_android)
    676             auxv->a_type = AT_IGNORE;
    677 #           endif
    678             auxv->u.a_val = info->interp_base;
    679             break;
    680 
    681          case AT_PLATFORM:
    682          case AT_BASE_PLATFORM:
    683             /* points to a platform description string */
    684             auxv->u.a_ptr = copy_str(&strtab, orig_auxv->u.a_ptr);
    685             break;
    686 
    687          case AT_ENTRY:
    688             auxv->u.a_val = info->entry;
    689             break;
    690 
    691          case AT_HWCAP:
    692 #           if defined(VGP_arm_linux)
    693             { Bool has_neon = (auxv->u.a_val & VKI_HWCAP_NEON) > 0;
    694               VG_(debugLog)(2, "initimg",
    695                                "ARM has-neon from-auxv: %s\n",
    696                                has_neon ? "YES" : "NO");
    697               VG_(machine_arm_set_has_NEON)( has_neon );
    698               #define VKI_HWCAP_TLS 32768
    699               Bool has_tls = (auxv->u.a_val & VKI_HWCAP_TLS) > 0;
    700               VG_(debugLog)(2, "initimg",
    701                                "ARM has-tls from-auxv: %s\n",
    702                                has_tls ? "YES" : "NO");
    703               /* If real hw sets properly HWCAP_TLS, we might
    704                  use this info to decide to really execute set_tls syscall
    705                  in syswrap-arm-linux.c rather than to base this on
    706                  conditional compilation. */
    707             }
    708 #           endif
    709             break;
    710 
    711          case AT_DCACHEBSIZE:
    712          case AT_ICACHEBSIZE:
    713          case AT_UCACHEBSIZE:
    714 #           if defined(VGP_ppc32_linux)
    715             /* acquire cache info */
    716             if (auxv->u.a_val > 0) {
    717                VG_(machine_ppc32_set_clszB)( auxv->u.a_val );
    718                VG_(debugLog)(2, "initimg",
    719                                 "PPC32 cache line size %u (type %u)\n",
    720                                 (UInt)auxv->u.a_val, (UInt)auxv->a_type );
    721             }
    722 #           elif defined(VGP_ppc64_linux)
    723             /* acquire cache info */
    724             if (auxv->u.a_val > 0) {
    725                VG_(machine_ppc64_set_clszB)( auxv->u.a_val );
    726                VG_(debugLog)(2, "initimg",
    727                                 "PPC64 cache line size %u (type %u)\n",
    728                                 (UInt)auxv->u.a_val, (UInt)auxv->a_type );
    729             }
    730 #           endif
    731             break;
    732 
    733 #        if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
    734          case AT_IGNOREPPC:
    735             break;
    736 #        endif
    737 
    738          case AT_SECURE:
    739             /* If this is 1, then it means that this program is
    740                running suid, and therefore the dynamic linker should
    741                be careful about LD_PRELOAD, etc.  However, since
    742                stage1 (the thing the kernel actually execve's) should
    743                never be SUID, and we need LD_PRELOAD to work for the
    744                client, we set AT_SECURE to 0. */
    745             auxv->u.a_val = 0;
    746             break;
    747 
    748          case AT_SYSINFO:
    749             /* Trash this, because we don't reproduce it */
    750             auxv->a_type = AT_IGNORE;
    751             break;
    752 
    753 #        if !defined(VGP_ppc32_linux) && !defined(VGP_ppc64_linux)
    754          case AT_SYSINFO_EHDR: {
    755             /* Trash this, because we don't reproduce it */
    756             const NSegment* ehdrseg = VG_(am_find_nsegment)((Addr)auxv->u.a_ptr);
    757             vg_assert(ehdrseg);
    758             VG_(am_munmap_valgrind)(ehdrseg->start, ehdrseg->end - ehdrseg->start);
    759             auxv->a_type = AT_IGNORE;
    760             break;
    761          }
    762 #        endif
    763 
    764          case AT_RANDOM:
    765             /* points to 16 random bytes - we need to ensure this is
    766                propagated to the client as glibc will assume it is
    767                present if it is built for kernel 2.6.29 or later */
    768             auxv->u.a_ptr = strtab;
    769             VG_(memcpy)(strtab, orig_auxv->u.a_ptr, 16);
    770             strtab += 16;
    771             break;
    772 
    773          case AT_EXECFN:
    774             /* points to the executable filename */
    775             auxv->u.a_ptr = copy_str(&strtab, VG_(args_the_exename));
    776             break;
    777 
    778          default:
    779             /* stomp out anything we don't know about */
    780             VG_(debugLog)(2, "initimg",
    781                              "stomping auxv entry %lld\n",
    782                              (ULong)auxv->a_type);
    783             auxv->a_type = AT_IGNORE;
    784             break;
    785       }
    786    }
    787    *auxv = *orig_auxv;
    788    vg_assert(auxv->a_type == AT_NULL);
    789 
    790    vg_assert((strtab-stringbase) == stringsize);
    791 
    792    /* client_SP is pointing at client's argc/argv */
    793 
    794    if (0) VG_(printf)("startup SP = %#lx\n", client_SP);
    795    return client_SP;
    796 }
    797 
    798 
    799 /* Allocate the client data segment.  It is an expandable anonymous
    800    mapping abutting a shrinkable reservation of size max_dseg_size.
    801    The data segment starts at VG_(brk_base), which is page-aligned,
    802    and runs up to VG_(brk_limit), which isn't. */
    803 
    804 static void setup_client_dataseg ( SizeT max_size )
    805 {
    806    Bool   ok;
    807    SysRes sres;
    808    Addr   anon_start  = VG_(brk_base);
    809    SizeT  anon_size   = VKI_PAGE_SIZE;
    810    Addr   resvn_start = anon_start + anon_size;
    811    SizeT  resvn_size  = max_size - anon_size;
    812 
    813    vg_assert(VG_IS_PAGE_ALIGNED(anon_size));
    814    vg_assert(VG_IS_PAGE_ALIGNED(resvn_size));
    815    vg_assert(VG_IS_PAGE_ALIGNED(anon_start));
    816    vg_assert(VG_IS_PAGE_ALIGNED(resvn_start));
    817 
    818    /* Because there's been no brk activity yet: */
    819    vg_assert(VG_(brk_base) == VG_(brk_limit));
    820 
    821    /* Try to create the data seg and associated reservation where
    822       VG_(brk_base) says. */
    823    ok = VG_(am_create_reservation)(
    824            resvn_start,
    825            resvn_size,
    826            SmLower,
    827            anon_size
    828         );
    829 
    830    if (!ok) {
    831       /* Hmm, that didn't work.  Well, let aspacem suggest an address
    832          it likes better, and try again with that. */
    833       anon_start = VG_(am_get_advisory_client_simple)
    834                       ( 0/*floating*/, anon_size+resvn_size, &ok );
    835       if (ok) {
    836          resvn_start = anon_start + anon_size;
    837          ok = VG_(am_create_reservation)(
    838                  resvn_start,
    839                  resvn_size,
    840                  SmLower,
    841                  anon_size
    842               );
    843          if (ok)
    844             VG_(brk_base) = VG_(brk_limit) = anon_start;
    845       }
    846       /* that too might have failed, but if it has, we're hosed: there
    847          is no Plan C. */
    848    }
    849    vg_assert(ok);
    850 
    851    /* We make the data segment (heap) executable because LinuxThreads on
    852       ppc32 creates trampolines in this area.  Also, on x86/Linux the data
    853       segment is RWX natively, at least according to /proc/self/maps.
    854       Also, having a non-executable data seg would kill any program which
    855       tried to create code in the data seg and then run it. */
    856    sres = VG_(am_mmap_anon_fixed_client)(
    857              anon_start,
    858              anon_size,
    859              VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC
    860           );
    861    vg_assert(!sr_isError(sres));
    862    vg_assert(sr_Res(sres) == anon_start);
    863 }
    864 
    865 
    866 /*====================================================================*/
    867 /*=== TOP-LEVEL: VG_(setup_client_initial_image)                   ===*/
    868 /*====================================================================*/
    869 
    870 /* Create the client's initial memory image. */
    871 IIFinaliseImageInfo VG_(ii_create_image)( IICreateImageInfo iicii )
    872 {
    873    ExeInfo info;
    874    HChar** env = NULL;
    875 
    876    IIFinaliseImageInfo iifii;
    877    VG_(memset)( &iifii, 0, sizeof(iifii) );
    878 
    879    //--------------------------------------------------------------
    880    // Load client executable, finding in $PATH if necessary
    881    //   p: get_helprequest_and_toolname()  [for 'exec', 'need_help']
    882    //   p: layout_remaining_space          [so there's space]
    883    //--------------------------------------------------------------
    884    VG_(debugLog)(1, "initimg", "Loading client\n");
    885 
    886    if (VG_(args_the_exename) == NULL)
    887       VG_(err_missing_prog)();
    888 
    889    load_client(&info, &iifii.initial_client_IP, &iifii.initial_client_TOC);
    890 
    891    //--------------------------------------------------------------
    892    // Set up client's environment
    893    //   p: set-libdir                   [for VG_(libdir)]
    894    //   p: get_helprequest_and_toolname [for toolname]
    895    //--------------------------------------------------------------
    896    VG_(debugLog)(1, "initimg", "Setup client env\n");
    897    env = setup_client_env(iicii.envp, iicii.toolname);
    898 
    899    //--------------------------------------------------------------
    900    // Setup client stack, eip, and VG_(client_arg[cv])
    901    //   p: load_client()     [for 'info']
    902    //   p: fix_environment() [for 'env']
    903    //--------------------------------------------------------------
    904    {
    905       /* When allocating space for the client stack on Linux, take
    906          notice of the --main-stacksize value.  This makes it possible
    907          to run programs with very large (primary) stack requirements
    908          simply by specifying --main-stacksize. */
    909       /* Logic is as follows:
    910          - by default, use the client's current stack rlimit
    911          - if that exceeds 16M, clamp to 16M
    912          - if a larger --main-stacksize value is specified, use that instead
    913          - in all situations, the minimum allowed stack size is 1M
    914       */
    915       void* init_sp = iicii.argv - 1;
    916       SizeT m1  = 1024 * 1024;
    917       SizeT m16 = 16 * m1;
    918       SizeT szB = (SizeT)VG_(client_rlimit_stack).rlim_cur;
    919       if (szB < m1) szB = m1;
    920       if (szB > m16) szB = m16;
    921       if (VG_(clo_main_stacksize) > 0) szB = VG_(clo_main_stacksize);
    922       if (szB < m1) szB = m1;
    923       szB = VG_PGROUNDUP(szB);
    924       VG_(debugLog)(1, "initimg",
    925                        "Setup client stack: size will be %ld\n", szB);
    926 
    927       iifii.clstack_max_size = szB;
    928 
    929       iifii.initial_client_SP
    930          = setup_client_stack( init_sp, env,
    931                                &info, &iifii.client_auxv,
    932                                iicii.clstack_top, iifii.clstack_max_size );
    933 
    934       VG_(free)(env);
    935 
    936       VG_(debugLog)(2, "initimg",
    937                        "Client info: "
    938                        "initial_IP=%p initial_TOC=%p brk_base=%p\n",
    939                        (void*)(iifii.initial_client_IP),
    940                        (void*)(iifii.initial_client_TOC),
    941                        (void*)VG_(brk_base) );
    942       VG_(debugLog)(2, "initimg",
    943                        "Client info: "
    944                        "initial_SP=%p max_stack_size=%ld\n",
    945                        (void*)(iifii.initial_client_SP),
    946                        (SizeT)iifii.clstack_max_size );
    947    }
    948 
    949    //--------------------------------------------------------------
    950    // Setup client data (brk) segment.  Initially a 1-page segment
    951    // which abuts a shrinkable reservation.
    952    //     p: load_client()     [for 'info' and hence VG_(brk_base)]
    953    //--------------------------------------------------------------
    954    {
    955       SizeT m1 = 1024 * 1024;
    956       SizeT m8 = 8 * m1;
    957       SizeT dseg_max_size = (SizeT)VG_(client_rlimit_data).rlim_cur;
    958       VG_(debugLog)(1, "initimg", "Setup client data (brk) segment\n");
    959       if (dseg_max_size < m1) dseg_max_size = m1;
    960       if (dseg_max_size > m8) dseg_max_size = m8;
    961       dseg_max_size = VG_PGROUNDUP(dseg_max_size);
    962 
    963       setup_client_dataseg( dseg_max_size );
    964    }
    965 
    966    return iifii;
    967 }
    968 
    969 
    970 /*====================================================================*/
    971 /*=== TOP-LEVEL: VG_(finalise_thread1state)                        ===*/
    972 /*====================================================================*/
    973 
    974 /* Just before starting the client, we may need to make final
    975    adjustments to its initial image.  Also we need to set up the VEX
    976    guest state for thread 1 (the root thread) and copy in essential
    977    starting values.  This is handed the IIFinaliseImageInfo created by
    978    VG_(ii_create_image).
    979 */
    980 void VG_(ii_finalise_image)( IIFinaliseImageInfo iifii )
    981 {
    982    ThreadArchState* arch = &VG_(threads)[1].arch;
    983 
    984    /* On Linux we get client_{ip/sp/toc}, and start the client with
    985       all other registers zeroed. */
    986 
    987 #  if defined(VGP_x86_linux)
    988    vg_assert(0 == sizeof(VexGuestX86State) % 16);
    989 
    990    /* Zero out the initial state, and set up the simulated FPU in a
    991       sane way. */
    992    LibVEX_GuestX86_initialise(&arch->vex);
    993 
    994    /* Zero out the shadow areas. */
    995    VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestX86State));
    996    VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestX86State));
    997 
    998    /* Put essential stuff into the new state. */
    999    arch->vex.guest_ESP = iifii.initial_client_SP;
   1000    arch->vex.guest_EIP = iifii.initial_client_IP;
   1001 
   1002    /* initialise %cs, %ds and %ss to point at the operating systems
   1003       default code, data and stack segments.  Also %es (see #291253). */
   1004    asm volatile("movw %%cs, %0" : : "m" (arch->vex.guest_CS));
   1005    asm volatile("movw %%ds, %0" : : "m" (arch->vex.guest_DS));
   1006    asm volatile("movw %%ss, %0" : : "m" (arch->vex.guest_SS));
   1007    asm volatile("movw %%es, %0" : : "m" (arch->vex.guest_ES));
   1008 
   1009 #  elif defined(VGP_amd64_linux)
   1010    vg_assert(0 == sizeof(VexGuestAMD64State) % 16);
   1011 
   1012    /* Zero out the initial state, and set up the simulated FPU in a
   1013       sane way. */
   1014    LibVEX_GuestAMD64_initialise(&arch->vex);
   1015 
   1016    /* Zero out the shadow areas. */
   1017    VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestAMD64State));
   1018    VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestAMD64State));
   1019 
   1020    /* Put essential stuff into the new state. */
   1021    arch->vex.guest_RSP = iifii.initial_client_SP;
   1022    arch->vex.guest_RIP = iifii.initial_client_IP;
   1023 
   1024 #  elif defined(VGP_ppc32_linux)
   1025    vg_assert(0 == sizeof(VexGuestPPC32State) % 16);
   1026 
   1027    /* Zero out the initial state, and set up the simulated FPU in a
   1028       sane way. */
   1029    LibVEX_GuestPPC32_initialise(&arch->vex);
   1030 
   1031    /* Zero out the shadow areas. */
   1032    VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestPPC32State));
   1033    VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestPPC32State));
   1034 
   1035    /* Put essential stuff into the new state. */
   1036    arch->vex.guest_GPR1 = iifii.initial_client_SP;
   1037    arch->vex.guest_CIA  = iifii.initial_client_IP;
   1038 
   1039 #  elif defined(VGP_ppc64_linux)
   1040    vg_assert(0 == sizeof(VexGuestPPC64State) % 16);
   1041 
   1042    /* Zero out the initial state, and set up the simulated FPU in a
   1043       sane way. */
   1044    LibVEX_GuestPPC64_initialise(&arch->vex);
   1045 
   1046    /* Zero out the shadow areas. */
   1047    VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestPPC64State));
   1048    VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestPPC64State));
   1049 
   1050    /* Put essential stuff into the new state. */
   1051    arch->vex.guest_GPR1 = iifii.initial_client_SP;
   1052    arch->vex.guest_GPR2 = iifii.initial_client_TOC;
   1053    arch->vex.guest_CIA  = iifii.initial_client_IP;
   1054 
   1055 #   elif defined(VGP_arm_linux)
   1056    /* Zero out the initial state, and set up the simulated FPU in a
   1057       sane way. */
   1058    LibVEX_GuestARM_initialise(&arch->vex);
   1059 
   1060    /* Zero out the shadow areas. */
   1061    VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestARMState));
   1062    VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestARMState));
   1063 
   1064    arch->vex.guest_R13  = iifii.initial_client_SP;
   1065    arch->vex.guest_R15T = iifii.initial_client_IP;
   1066 
   1067    /* This is just EABI stuff. */
   1068    // FIXME jrs: what's this for?
   1069    arch->vex.guest_R1 =  iifii.initial_client_SP;
   1070 
   1071 #  elif defined(VGP_s390x_linux)
   1072    vg_assert(0 == sizeof(VexGuestS390XState) % 16);
   1073 
   1074    /* Zero out the initial state. This also sets the guest_fpc to 0, which
   1075       is also done by the kernel for the fpc during execve. */
   1076    LibVEX_GuestS390X_initialise(&arch->vex);
   1077 
   1078    /* Mark all registers as undefined ... */
   1079    VG_(memset)(&arch->vex_shadow1, 0xFF, sizeof(VexGuestS390XState));
   1080    VG_(memset)(&arch->vex_shadow2, 0x00, sizeof(VexGuestS390XState));
   1081    /* ... except SP, FPC, and IA */
   1082    VG_(memset)((UChar *)&arch->vex_shadow1 + VG_O_STACK_PTR, 0x00, 8);
   1083    VG_(memset)((UChar *)&arch->vex_shadow1 + VG_O_FPC_REG,   0x00, 4);
   1084    VG_(memset)((UChar *)&arch->vex_shadow1 + VG_O_INSTR_PTR, 0x00, 8);
   1085 
   1086    /* Put essential stuff into the new state. */
   1087    arch->vex.guest_SP = iifii.initial_client_SP;
   1088    arch->vex.guest_IA = iifii.initial_client_IP;
   1089    /* See sys_execve in <linux>/arch/s390/kernel/process.c */
   1090    arch->vex.guest_fpc = 0;
   1091 
   1092    /* Tell the tool about the registers we just wrote */
   1093    VG_TRACK(post_reg_write, Vg_CoreStartup, /*tid*/1, VG_O_STACK_PTR, 8);
   1094    VG_TRACK(post_reg_write, Vg_CoreStartup, /*tid*/1, VG_O_FPC_REG,   4);
   1095    VG_TRACK(post_reg_write, Vg_CoreStartup, /*tid*/1, VG_O_INSTR_PTR, 8);
   1096    return;
   1097 
   1098 #  elif defined(VGP_mips32_linux)
   1099    vg_assert(0 == sizeof(VexGuestMIPS32State) % 16);
   1100    /* Zero out the initial state, and set up the simulated FPU in a
   1101       sane way. */
   1102    LibVEX_GuestMIPS32_initialise(&arch->vex);
   1103 
   1104    /* Zero out the shadow areas. */
   1105    VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestMIPS32State));
   1106    VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestMIPS32State));
   1107 
   1108    arch->vex.guest_r29 = iifii.initial_client_SP;
   1109    arch->vex.guest_PC = iifii.initial_client_IP;
   1110    arch->vex.guest_r31 = iifii.initial_client_SP;
   1111 
   1112 #  else
   1113 #    error Unknown platform
   1114 #  endif
   1115 
   1116    /* Tell the tool that we just wrote to the registers. */
   1117    VG_TRACK( post_reg_write, Vg_CoreStartup, /*tid*/1, /*offset*/0,
   1118              sizeof(VexGuestArchState));
   1119 }
   1120 
   1121 #endif // defined(VGO_linux)
   1122 
   1123 /*--------------------------------------------------------------------*/
   1124 /*---                                                              ---*/
   1125 /*--------------------------------------------------------------------*/
   1126