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