2 /*--------------------------------------------------------------------*/
3 /*--- Startup: create initial process image on Linux ---*/
4 /*--- initimg-linux.c ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2000-2013 Julian Seward
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.
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.
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
29 The GNU General Public License is contained in the file COPYING.
32 #if defined(VGO_linux)
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 */
55 /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */
57 #define _FILE_OFFSET_BITS 64
58 /* This is for ELF types etc, and also the AT_ constants. */
60 /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */
63 /*====================================================================*/
64 /*=== Loading the client ===*/
65 /*====================================================================*/
67 /* Load the client whose name is VG_(argv_the_exename). */
69 static void load_client ( /*OUT*/ExeInfo
* info
,
70 /*OUT*/Addr
* client_ip
,
71 /*OUT*/Addr
* client_toc
)
73 const HChar
* exe_name
;
77 vg_assert( VG_(args_the_exename
) != NULL
);
78 exe_name
= ML_(find_executable
)( VG_(args_the_exename
) );
81 VG_(printf
)("valgrind: %s: command not found\n", VG_(args_the_exename
));
82 VG_(exit
)(127); // 127 is Posix NOTFOUND
85 VG_(memset
)(info
, 0, sizeof(*info
));
86 ret
= VG_(do_exec
)(exe_name
, info
);
88 VG_(printf
)("valgrind: could not execute '%s'\n", exe_name
);
92 // The client was successfully loaded! Continue.
94 /* Get hold of a file descriptor which refers to the client
95 executable. This is needed for attaching to GDB. */
96 res
= VG_(open
)(exe_name
, VKI_O_RDONLY
, VKI_S_IRUSR
);
98 VG_(cl_exec_fd
) = sr_Res(res
);
100 /* Copy necessary bits of 'info' that were filled in */
101 *client_ip
= info
->init_ip
;
102 *client_toc
= info
->init_toc
;
103 VG_(brk_base
) = VG_(brk_limit
) = VG_PGROUNDUP(info
->brkbase
);
107 /*====================================================================*/
108 /*=== Setting up the client's environment ===*/
109 /*====================================================================*/
111 /* Prepare the client's environment. This is basically a copy of our
114 LD_PRELOAD=$VALGRIND_LIB/vgpreload_core-PLATFORM.so:
115 ($VALGRIND_LIB/vgpreload_TOOL-PLATFORM.so:)?
118 If this is missing, then it is added.
120 Also, remove any binding for VALGRIND_LAUNCHER=. The client should
121 not be able to see this.
123 If this needs to handle any more variables it should be hacked
124 into something table driven. The copy is VG_(malloc)'d space.
126 static HChar
** setup_client_env ( HChar
** origenv
, const HChar
* toolname
)
131 const HChar
* preload_core
= "vgpreload_core";
132 const HChar
* ld_preload
= "LD_PRELOAD=";
133 const HChar
* v_launcher
= VALGRIND_LAUNCHER
"=";
134 Int ld_preload_len
= VG_(strlen
)( ld_preload
);
135 Int v_launcher_len
= VG_(strlen
)( v_launcher
);
136 Bool ld_preload_done
= False
;
137 Int vglib_len
= VG_(strlen
)(VG_(libdir
));
142 HChar
* preload_tool_path
;
145 /* Alloc space for the vgpreload_core.so path and vgpreload_<tool>.so
146 paths. We might not need the space for vgpreload_<tool>.so, but it
147 doesn't hurt to over-allocate briefly. The 16s are just cautious
149 Int preload_core_path_len
= vglib_len
+ sizeof(preload_core
)
150 + sizeof(VG_PLATFORM
) + 16;
151 Int preload_tool_path_len
= vglib_len
+ VG_(strlen
)(toolname
)
152 + sizeof(VG_PLATFORM
) + 16;
153 Int preload_string_len
= preload_core_path_len
+ preload_tool_path_len
;
154 HChar
* preload_string
= VG_(malloc
)("initimg-linux.sce.1",
156 /* Determine if there's a vgpreload_<tool>_<platform>.so file, and setup
158 preload_tool_path
= VG_(malloc
)("initimg-linux.sce.2", preload_tool_path_len
);
159 VG_(snprintf
)(preload_tool_path
, preload_tool_path_len
,
160 "%s/vgpreload_%s-%s.so", VG_(libdir
), toolname
, VG_PLATFORM
);
161 if (VG_(access
)(preload_tool_path
, True
/*r*/, False
/*w*/, False
/*x*/) == 0) {
162 VG_(snprintf
)(preload_string
, preload_string_len
, "%s/%s-%s.so:%s",
163 VG_(libdir
), preload_core
, VG_PLATFORM
, preload_tool_path
);
165 VG_(snprintf
)(preload_string
, preload_string_len
, "%s/%s-%s.so",
166 VG_(libdir
), preload_core
, VG_PLATFORM
);
168 VG_(free
)(preload_tool_path
);
170 VG_(debugLog
)(2, "initimg", "preload_string:\n");
171 VG_(debugLog
)(2, "initimg", " \"%s\"\n", preload_string
);
173 /* Count the original size of the env */
174 if (debug
) VG_(printf
)("\n\n");
176 for (cpp
= origenv
; cpp
&& *cpp
; cpp
++) {
178 if (debug
) VG_(printf
)("XXXXXXXXX: BEFORE %s\n", *cpp
);
181 /* Allocate a new space */
182 ret
= VG_(malloc
) ("initimg-linux.sce.3",
183 sizeof(HChar
*) * (envc
+1+1)); /* 1 new entry + NULL */
186 for (cpp
= ret
; *origenv
; ) {
187 if (debug
) VG_(printf
)("XXXXXXXXX: COPY %s\n", *origenv
);
192 vg_assert(envc
== (cpp
- ret
));
194 /* Walk over the new environment, mashing as we go */
195 for (cpp
= ret
; cpp
&& *cpp
; cpp
++) {
196 if (VG_(memcmp
)(*cpp
, ld_preload
, ld_preload_len
) == 0) {
197 Int len
= VG_(strlen
)(*cpp
) + preload_string_len
;
198 HChar
*cp
= VG_(malloc
)("initimg-linux.sce.4", len
);
200 VG_(snprintf
)(cp
, len
, "%s%s:%s",
201 ld_preload
, preload_string
, (*cpp
)+ld_preload_len
);
205 ld_preload_done
= True
;
207 if (debug
) VG_(printf
)("XXXXXXXXX: MASH %s\n", *cpp
);
210 /* Add the missing bits */
211 if (!ld_preload_done
) {
212 Int len
= ld_preload_len
+ preload_string_len
;
213 HChar
*cp
= VG_(malloc
) ("initimg-linux.sce.5", len
);
215 VG_(snprintf
)(cp
, len
, "%s%s", ld_preload
, preload_string
);
218 if (debug
) VG_(printf
)("XXXXXXXXX: ADD %s\n", cp
);
221 /* ret[0 .. envc-1] is live now. */
222 /* Find and remove a binding for VALGRIND_LAUNCHER. */
223 for (i
= 0; i
< envc
; i
++)
224 if (0 == VG_(memcmp(ret
[i
], v_launcher
, v_launcher_len
)))
228 for (; i
< envc
-1; i
++)
233 VG_(free
)(preload_string
);
236 for (i
= 0; i
< envc
; i
++) {
237 if (debug
) VG_(printf
)("XXXXXXXXX: FINAL %s\n", ret
[i
]);
244 /*====================================================================*/
245 /*=== Setting up the client's stack ===*/
246 /*====================================================================*/
248 #ifndef AT_DCACHEBSIZE
249 #define AT_DCACHEBSIZE 19
250 #endif /* AT_DCACHEBSIZE */
252 #ifndef AT_ICACHEBSIZE
253 #define AT_ICACHEBSIZE 20
254 #endif /* AT_ICACHEBSIZE */
256 #ifndef AT_UCACHEBSIZE
257 #define AT_UCACHEBSIZE 21
258 #endif /* AT_UCACHEBSIZE */
260 #ifndef AT_BASE_PLATFORM
261 #define AT_BASE_PLATFORM 24
262 #endif /* AT_BASE_PLATFORM */
266 #endif /* AT_RANDOM */
270 #endif /* AT_HWCAP2 */
274 #endif /* AT_EXECFN */
277 #define AT_SYSINFO 32
278 #endif /* AT_SYSINFO */
280 #ifndef AT_SYSINFO_EHDR
281 #define AT_SYSINFO_EHDR 33
282 #endif /* AT_SYSINFO_EHDR */
285 #define AT_SECURE 23 /* secure mode boolean */
286 #endif /* AT_SECURE */
288 /* Add a string onto the string table, and return its address */
289 static HChar
*copy_str(HChar
**tab
, const HChar
*str
)
299 VG_(printf
)("copied %p \"%s\" len %lld\n", orig
, orig
, (Long
)(cp
-orig
));
307 /* ----------------------------------------------------------------
309 This sets up the client's initial stack, containing the args,
310 environment and aux vector.
312 The format of the stack is:
314 higher address +-----------------+ <- clstack_end
332 lower address +-----------------+ <- sp
336 Allocate and create the initial client stack. It is allocated down
337 from clstack_end, which was previously determined by the address
338 space manager. The returned value is the SP value for the client.
340 The client's auxv is created by copying and modifying our own one.
341 As a side effect of scanning our own auxv, some important bits of
344 VG_(cache_line_size_ppc32) // ppc32 only -- cache line size
345 VG_(have_altivec_ppc32) // ppc32 only -- is Altivec supported?
347 ---------------------------------------------------------------- */
359 struct auxv
*find_auxv(UWord
* sp
)
361 sp
++; // skip argc (Nb: is word-sized, not int-sized!)
363 while (*sp
!= 0) // skip argv
367 while (*sp
!= 0) // skip env
371 #if defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
372 # if defined AT_IGNOREPPC
373 while (*sp
== AT_IGNOREPPC
) // skip AT_IGNOREPPC entries
378 return (struct auxv
*)sp
;
382 Addr
setup_client_stack( void* init_sp
,
387 SizeT clstack_max_size
,
388 const VexArchInfo
* vex_archinfo
)
390 /* The HW configuration setting (hwcaps) of the target can be
391 * checked against the Vex settings of the host platform as given
392 * by the values in vex_archinfo.
397 HChar
*strtab
; /* string table */
401 const struct auxv
*orig_auxv
;
402 const struct auxv
*cauxv
;
403 unsigned stringsize
; /* total size of strings in bytes */
404 unsigned auxsize
; /* total size of auxv in bytes */
405 Int argc
; /* total argc */
406 Int envc
; /* total number of env vars */
407 unsigned stacksize
; /* total client stack size */
408 Addr client_SP
; /* client stack base (initial SP) */
412 vg_assert(VG_IS_PAGE_ALIGNED(clstack_end
+1));
413 vg_assert( VG_(args_for_client
) );
415 /* use our own auxv as a prototype */
416 orig_auxv
= find_auxv(init_sp
);
418 /* ==================== compute sizes ==================== */
420 /* first of all, work out how big the client stack will be */
423 /* paste on the extra args if the loader needs them (ie, the #!
424 interpreter and its argument) */
426 if (info
->interp_name
!= NULL
) {
428 stringsize
+= VG_(strlen
)(info
->interp_name
) + 1;
430 if (info
->interp_args
!= NULL
) {
432 stringsize
+= VG_(strlen
)(info
->interp_args
) + 1;
435 /* now scan the args we're given... */
436 stringsize
+= VG_(strlen
)( VG_(args_the_exename
) ) + 1;
438 for (i
= 0; i
< VG_(sizeXA
)( VG_(args_for_client
) ); i
++) {
440 stringsize
+= VG_(strlen
)( * (HChar
**)
441 VG_(indexXA
)( VG_(args_for_client
), i
))
445 /* ...and the environment */
447 for (cpp
= orig_envp
; cpp
&& *cpp
; cpp
++) {
449 stringsize
+= VG_(strlen
)(*cpp
) + 1;
452 /* now, how big is the auxv? */
453 auxsize
= sizeof(*auxv
); /* there's always at least one entry: AT_NULL */
454 for (cauxv
= orig_auxv
; cauxv
->a_type
!= AT_NULL
; cauxv
++) {
455 if (cauxv
->a_type
== AT_PLATFORM
||
456 cauxv
->a_type
== AT_BASE_PLATFORM
)
457 stringsize
+= VG_(strlen
)(cauxv
->u
.a_ptr
) + 1;
458 else if (cauxv
->a_type
== AT_RANDOM
)
460 else if (cauxv
->a_type
== AT_EXECFN
)
461 stringsize
+= VG_(strlen
)(VG_(args_the_exename
)) + 1;
462 auxsize
+= sizeof(*cauxv
);
465 # if defined(VGP_ppc32_linux) || defined(VGP_ppc64be_linux) \
466 || defined(VGP_ppc64le_linux)
467 auxsize
+= 2 * sizeof(*cauxv
);
470 /* OK, now we know how big the client stack is */
472 sizeof(Word
) + /* argc */
473 sizeof(HChar
**) + /* argc[0] == exename */
474 sizeof(HChar
**)*argc
+ /* argv */
475 sizeof(HChar
**) + /* terminal NULL */
476 sizeof(HChar
**)*envc
+ /* envp */
477 sizeof(HChar
**) + /* terminal NULL */
479 VG_ROUNDUP(stringsize
, sizeof(Word
)); /* strings (aligned) */
481 if (0) VG_(printf
)("stacksize = %d\n", stacksize
);
483 /* client_SP is the client's stack pointer */
484 client_SP
= clstack_end
- stacksize
;
485 client_SP
= VG_ROUNDDN(client_SP
, 16); /* make stack 16 byte aligned */
487 /* base of the string table (aligned) */
488 stringbase
= strtab
= (HChar
*)clstack_end
489 - VG_ROUNDUP(stringsize
, sizeof(int));
491 clstack_start
= VG_PGROUNDDN(client_SP
);
493 /* The max stack size */
494 clstack_max_size
= VG_PGROUNDUP(clstack_max_size
);
497 VG_(printf
)("stringsize=%d auxsize=%d stacksize=%d maxsize=0x%x\n"
500 stringsize
, auxsize
, stacksize
, (Int
)clstack_max_size
,
501 (void*)clstack_start
, (void*)clstack_end
);
503 /* ==================== allocate space ==================== */
505 { SizeT anon_size
= clstack_end
- clstack_start
+ 1;
506 SizeT resvn_size
= clstack_max_size
- anon_size
;
507 Addr anon_start
= clstack_start
;
508 Addr resvn_start
= anon_start
- resvn_size
;
509 SizeT inner_HACK
= 0;
512 /* So far we've only accounted for space requirements down to the
513 stack pointer. If this target's ABI requires a redzone below
514 the stack pointer, we need to allocate an extra page, to
515 handle the worst case in which the stack pointer is almost at
516 the bottom of a page, and so there is insufficient room left
517 over to put the redzone in. In this case the simple thing to
518 do is allocate an extra page, by shrinking the reservation by
519 one page and growing the anonymous area by a corresponding
521 vg_assert(VG_STACK_REDZONE_SZB
>= 0);
522 vg_assert(VG_STACK_REDZONE_SZB
< VKI_PAGE_SIZE
);
523 if (VG_STACK_REDZONE_SZB
> 0) {
524 vg_assert(resvn_size
> VKI_PAGE_SIZE
);
525 resvn_size
-= VKI_PAGE_SIZE
;
526 anon_start
-= VKI_PAGE_SIZE
;
527 anon_size
+= VKI_PAGE_SIZE
;
530 vg_assert(VG_IS_PAGE_ALIGNED(anon_size
));
531 vg_assert(VG_IS_PAGE_ALIGNED(resvn_size
));
532 vg_assert(VG_IS_PAGE_ALIGNED(anon_start
));
533 vg_assert(VG_IS_PAGE_ALIGNED(resvn_start
));
534 vg_assert(resvn_start
== clstack_end
+ 1 - clstack_max_size
);
537 inner_HACK
= 1024*1024; // create 1M non-fault-extending stack
541 VG_(printf
)("%#lx 0x%lx %#lx 0x%lx\n",
542 resvn_start
, resvn_size
, anon_start
, anon_size
);
544 /* Create a shrinkable reservation followed by an anonymous
545 segment. Together these constitute a growdown stack. */
546 res
= VG_(mk_SysRes_Error
)(0);
547 ok
= VG_(am_create_reservation
)(
549 resvn_size
-inner_HACK
,
551 anon_size
+inner_HACK
554 /* allocate a stack - mmap enough space for the stack */
555 res
= VG_(am_mmap_anon_fixed_client
)(
556 anon_start
-inner_HACK
,
557 anon_size
+inner_HACK
,
561 if ((!ok
) || sr_isError(res
)) {
562 /* Allocation of the stack failed. We have to stop. */
563 VG_(printf
)("valgrind: "
564 "I failed to allocate space for the application's stack.\n");
565 VG_(printf
)("valgrind: "
566 "This may be the result of a very large --main-stacksize=\n");
567 VG_(printf
)("valgrind: setting. Cannot continue. Sorry.\n\n");
572 vg_assert(!sr_isError(res
));
574 /* Record stack extent -- needed for stack-change code. */
575 VG_(clstk_start_base
) = anon_start
-inner_HACK
;
576 VG_(clstk_end
) = VG_(clstk_start_base
) + anon_size
+inner_HACK
-1;
580 /* ==================== create client stack ==================== */
582 ptr
= (Addr
*)client_SP
;
584 /* --- client argc --- */
587 /* --- client argv --- */
588 if (info
->interp_name
) {
589 *ptr
++ = (Addr
)copy_str(&strtab
, info
->interp_name
);
590 VG_(free
)(info
->interp_name
);
592 if (info
->interp_args
) {
593 *ptr
++ = (Addr
)copy_str(&strtab
, info
->interp_args
);
594 VG_(free
)(info
->interp_args
);
597 *ptr
++ = (Addr
)copy_str(&strtab
, VG_(args_the_exename
));
599 for (i
= 0; i
< VG_(sizeXA
)( VG_(args_for_client
) ); i
++) {
600 *ptr
++ = (Addr
)copy_str(
602 * (HChar
**) VG_(indexXA
)( VG_(args_for_client
), i
)
608 VG_(client_envp
) = (HChar
**)ptr
;
609 for (cpp
= orig_envp
; cpp
&& *cpp
; ptr
++, cpp
++)
610 *ptr
= (Addr
)copy_str(&strtab
, *cpp
);
614 auxv
= (struct auxv
*)ptr
;
615 *client_auxv
= (UInt
*)auxv
;
616 VG_(client_auxv
) = (UWord
*)*client_auxv
;
617 // ??? According to 'man proc', auxv is a array of unsigned long
618 // terminated by two zeros. Why is valgrind working with UInt ?
619 // We do not take ULong* (as ULong 8 bytes on a 32 bits),
622 # if defined(VGP_ppc32_linux) || defined(VGP_ppc64be_linux) \
623 || defined(VGP_ppc64le_linux)
624 auxv
[0].a_type
= AT_IGNOREPPC
;
625 auxv
[0].u
.a_val
= AT_IGNOREPPC
;
626 auxv
[1].a_type
= AT_IGNOREPPC
;
627 auxv
[1].u
.a_val
= AT_IGNOREPPC
;
631 for (; orig_auxv
->a_type
!= AT_NULL
; auxv
++, orig_auxv
++) {
633 /* copy the entry... */
636 /* ...and fix up / examine the copy */
637 switch(auxv
->a_type
) {
649 # if !defined(VGPV_arm_linux_android) \
650 && !defined(VGPV_x86_linux_android) \
651 && !defined(VGPV_mips32_linux_android) \
652 && !defined(VGPV_arm64_linux_android)
653 case AT_FPUCW
: /* missing on android */
655 /* All these are pointerless, so we don't need to do
656 anything about them. */
661 auxv
->a_type
= AT_IGNORE
;
663 auxv
->u
.a_val
= info
->phdr
;
668 auxv
->a_type
= AT_IGNORE
;
670 auxv
->u
.a_val
= info
->phnum
;
674 auxv
->u
.a_val
= info
->interp_offset
;
678 case AT_BASE_PLATFORM
:
679 /* points to a platform description string */
680 auxv
->u
.a_ptr
= copy_str(&strtab
, orig_auxv
->u
.a_ptr
);
684 auxv
->u
.a_val
= info
->entry
;
688 # if defined(VGP_arm_linux)
689 { Bool has_neon
= (auxv
->u
.a_val
& VKI_HWCAP_NEON
) > 0;
690 VG_(debugLog
)(2, "initimg",
691 "ARM has-neon from-auxv: %s\n",
692 has_neon
? "YES" : "NO");
693 VG_(machine_arm_set_has_NEON
)( has_neon
);
694 #define VKI_HWCAP_TLS 32768
695 Bool has_tls
= (auxv
->u
.a_val
& VKI_HWCAP_TLS
) > 0;
696 VG_(debugLog
)(2, "initimg",
697 "ARM has-tls from-auxv: %s\n",
698 has_tls
? "YES" : "NO");
699 /* If real hw sets properly HWCAP_TLS, we might
700 use this info to decide to really execute set_tls syscall
701 in syswrap-arm-linux.c rather than to base this on
702 conditional compilation. */
706 # if defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
708 Bool auxv_2_07
, hw_caps_2_07
;
709 /* The HWCAP2 field may contain an arch_2_07 entry that indicates
710 * if the processor is compliant with the 2.07 ISA. (i.e. Power 8
711 * or beyond). The Valgrind vai.hwcaps value
712 * (coregrind/m_machine.c) has the VEX_HWCAPS_PPC64_ISA2_07
713 * flag set so Valgrind knows about Power8. Need to pass the
714 * HWCAP2 value along so the user level programs can detect that
715 * the processor supports ISA 2.07 and beyond.
717 /* Power Architecture 64-Bit ELF V2 ABI Specification
718 July 21, 2014, version 1.0, Page 124
719 www-03.ibm.com/technologyconnect/tgcm/TGCMServlet.wss?alias=OpenPOWER&linkid=1n0000
722 The a_val member of this entry is a bit map of hardware
723 capabilities. Some bit mask values include:
725 PPC_FEATURE2_ARCH_2_07 0x80000000
726 PPC_FEATURE2_HAS_HTM 0x40000000
727 PPC_FEATURE2_HAS_DSCR 0x20000000
728 PPC_FEATURE2_HAS_EBB 0x10000000
729 PPC_FEATURE2_HAS_ISEL 0x08000000
730 PPC_FEATURE2_HAS_TAR 0x04000000
731 PPC_FEATURE2_HAS_VCRYPTO 0x02000000
733 auxv_2_07
= (auxv
->u
.a_val
& 0x80000000ULL
) == 0x80000000ULL
;
734 hw_caps_2_07
= (vex_archinfo
->hwcaps
& VEX_HWCAPS_PPC64_ISA2_07
)
735 == VEX_HWCAPS_PPC64_ISA2_07
;
737 /* Verify the PPC_FEATURE2_ARCH_2_07 setting in HWCAP2
738 * matches the setting in VEX HWCAPS.
740 vg_assert(auxv_2_07
== hw_caps_2_07
);
749 # if defined(VGP_ppc32_linux)
750 /* acquire cache info */
751 if (auxv
->u
.a_val
> 0) {
752 VG_(machine_ppc32_set_clszB
)( auxv
->u
.a_val
);
753 VG_(debugLog
)(2, "initimg",
754 "PPC32 icache line size %u (type %u)\n",
755 (UInt
)auxv
->u
.a_val
, (UInt
)auxv
->a_type
);
757 # elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
758 /* acquire cache info */
759 if (auxv
->u
.a_val
> 0) {
760 VG_(machine_ppc64_set_clszB
)( auxv
->u
.a_val
);
761 VG_(debugLog
)(2, "initimg",
762 "PPC64 icache line size %u (type %u)\n",
763 (UInt
)auxv
->u
.a_val
, (UInt
)auxv
->a_type
);
768 # if defined(VGP_ppc32_linux) || defined(VGP_ppc64be_linux) \
769 || defined(VGP_ppc64le_linux)
775 /* If this is 1, then it means that this program is
776 running suid, and therefore the dynamic linker should
777 be careful about LD_PRELOAD, etc. However, since
778 stage1 (the thing the kernel actually execve's) should
779 never be SUID, and we need LD_PRELOAD to work for the
780 client, we set AT_SECURE to 0. */
785 /* Trash this, because we don't reproduce it */
786 auxv
->a_type
= AT_IGNORE
;
789 # if !defined(VGP_ppc32_linux) && !defined(VGP_ppc64be_linux) \
790 && !defined(VGP_ppc64le_linux)
791 case AT_SYSINFO_EHDR
: {
792 /* Trash this, because we don't reproduce it */
793 const NSegment
* ehdrseg
= VG_(am_find_nsegment
)((Addr
)auxv
->u
.a_ptr
);
795 VG_(am_munmap_valgrind
)(ehdrseg
->start
, ehdrseg
->end
- ehdrseg
->start
);
796 auxv
->a_type
= AT_IGNORE
;
802 /* points to 16 random bytes - we need to ensure this is
803 propagated to the client as glibc will assume it is
804 present if it is built for kernel 2.6.29 or later */
805 auxv
->u
.a_ptr
= strtab
;
806 VG_(memcpy
)(strtab
, orig_auxv
->u
.a_ptr
, 16);
811 /* points to the executable filename */
812 auxv
->u
.a_ptr
= copy_str(&strtab
, VG_(args_the_exename
));
816 /* stomp out anything we don't know about */
817 VG_(debugLog
)(2, "initimg",
818 "stomping auxv entry %lld\n",
819 (ULong
)auxv
->a_type
);
820 auxv
->a_type
= AT_IGNORE
;
825 vg_assert(auxv
->a_type
== AT_NULL
);
827 vg_assert((strtab
-stringbase
) == stringsize
);
829 /* client_SP is pointing at client's argc/argv */
831 if (0) VG_(printf
)("startup SP = %#lx\n", client_SP
);
836 /* Allocate the client data segment. It is an expandable anonymous
837 mapping abutting a shrinkable reservation of size max_dseg_size.
838 The data segment starts at VG_(brk_base), which is page-aligned,
839 and runs up to VG_(brk_limit), which isn't. */
841 static void setup_client_dataseg ( SizeT max_size
)
845 Addr anon_start
= VG_(brk_base
);
846 SizeT anon_size
= VKI_PAGE_SIZE
;
847 Addr resvn_start
= anon_start
+ anon_size
;
848 SizeT resvn_size
= max_size
- anon_size
;
850 vg_assert(VG_IS_PAGE_ALIGNED(anon_size
));
851 vg_assert(VG_IS_PAGE_ALIGNED(resvn_size
));
852 vg_assert(VG_IS_PAGE_ALIGNED(anon_start
));
853 vg_assert(VG_IS_PAGE_ALIGNED(resvn_start
));
855 /* Because there's been no brk activity yet: */
856 vg_assert(VG_(brk_base
) == VG_(brk_limit
));
858 /* Try to create the data seg and associated reservation where
859 VG_(brk_base) says. */
860 ok
= VG_(am_create_reservation
)(
868 /* Hmm, that didn't work. Well, let aspacem suggest an address
869 it likes better, and try again with that. */
870 anon_start
= VG_(am_get_advisory_client_simple
)
871 ( 0/*floating*/, anon_size
+resvn_size
, &ok
);
873 resvn_start
= anon_start
+ anon_size
;
874 ok
= VG_(am_create_reservation
)(
881 VG_(brk_base
) = VG_(brk_limit
) = anon_start
;
883 /* that too might have failed, but if it has, we're hosed: there
888 /* We make the data segment (heap) executable because LinuxThreads on
889 ppc32 creates trampolines in this area. Also, on x86/Linux the data
890 segment is RWX natively, at least according to /proc/self/maps.
891 Also, having a non-executable data seg would kill any program which
892 tried to create code in the data seg and then run it. */
893 sres
= VG_(am_mmap_anon_fixed_client
)(
896 VKI_PROT_READ
|VKI_PROT_WRITE
|VKI_PROT_EXEC
898 vg_assert(!sr_isError(sres
));
899 vg_assert(sr_Res(sres
) == anon_start
);
903 /*====================================================================*/
904 /*=== TOP-LEVEL: VG_(setup_client_initial_image) ===*/
905 /*====================================================================*/
907 /* Create the client's initial memory image. */
908 IIFinaliseImageInfo
VG_(ii_create_image
)( IICreateImageInfo iicii
,
909 const VexArchInfo
* vex_archinfo
)
914 IIFinaliseImageInfo iifii
;
915 VG_(memset
)( &iifii
, 0, sizeof(iifii
) );
917 //--------------------------------------------------------------
918 // Load client executable, finding in $PATH if necessary
919 // p: get_helprequest_and_toolname() [for 'exec', 'need_help']
920 // p: layout_remaining_space [so there's space]
921 //--------------------------------------------------------------
922 VG_(debugLog
)(1, "initimg", "Loading client\n");
924 if (VG_(args_the_exename
) == NULL
)
925 VG_(err_missing_prog
)();
927 load_client(&info
, &iifii
.initial_client_IP
, &iifii
.initial_client_TOC
);
929 //--------------------------------------------------------------
930 // Set up client's environment
931 // p: set-libdir [for VG_(libdir)]
932 // p: get_helprequest_and_toolname [for toolname]
933 //--------------------------------------------------------------
934 VG_(debugLog
)(1, "initimg", "Setup client env\n");
935 env
= setup_client_env(iicii
.envp
, iicii
.toolname
);
937 //--------------------------------------------------------------
938 // Setup client stack, eip, and VG_(client_arg[cv])
939 // p: load_client() [for 'info']
940 // p: fix_environment() [for 'env']
941 //--------------------------------------------------------------
943 /* When allocating space for the client stack on Linux, take
944 notice of the --main-stacksize value. This makes it possible
945 to run programs with very large (primary) stack requirements
946 simply by specifying --main-stacksize. */
947 /* Logic is as follows:
948 - by default, use the client's current stack rlimit
949 - if that exceeds 16M, clamp to 16M
950 - if a larger --main-stacksize value is specified, use that instead
951 - in all situations, the minimum allowed stack size is 1M
953 void* init_sp
= iicii
.argv
- 1;
954 SizeT m1
= 1024 * 1024;
956 SizeT szB
= (SizeT
)VG_(client_rlimit_stack
).rlim_cur
;
957 if (szB
< m1
) szB
= m1
;
958 if (szB
> m16
) szB
= m16
;
959 if (VG_(clo_main_stacksize
) > 0) szB
= VG_(clo_main_stacksize
);
960 if (szB
< m1
) szB
= m1
;
961 szB
= VG_PGROUNDUP(szB
);
962 VG_(debugLog
)(1, "initimg",
963 "Setup client stack: size will be %ld\n", szB
);
965 iifii
.clstack_max_size
= szB
;
967 iifii
.initial_client_SP
968 = setup_client_stack( init_sp
, env
,
969 &info
, &iifii
.client_auxv
,
970 iicii
.clstack_end
, iifii
.clstack_max_size
,
975 VG_(debugLog
)(2, "initimg",
977 "initial_IP=%p initial_TOC=%p brk_base=%p\n",
978 (void*)(iifii
.initial_client_IP
),
979 (void*)(iifii
.initial_client_TOC
),
980 (void*)VG_(brk_base
) );
981 VG_(debugLog
)(2, "initimg",
983 "initial_SP=%p max_stack_size=%ld\n",
984 (void*)(iifii
.initial_client_SP
),
985 (SizeT
)iifii
.clstack_max_size
);
988 //--------------------------------------------------------------
989 // Setup client data (brk) segment. Initially a 1-page segment
990 // which abuts a shrinkable reservation.
991 // p: load_client() [for 'info' and hence VG_(brk_base)]
992 //--------------------------------------------------------------
994 SizeT m1
= 1024 * 1024;
996 SizeT dseg_max_size
= (SizeT
)VG_(client_rlimit_data
).rlim_cur
;
997 VG_(debugLog
)(1, "initimg", "Setup client data (brk) segment\n");
998 if (dseg_max_size
< m1
) dseg_max_size
= m1
;
999 if (dseg_max_size
> m8
) dseg_max_size
= m8
;
1000 dseg_max_size
= VG_PGROUNDUP(dseg_max_size
);
1002 setup_client_dataseg( dseg_max_size
);
1009 /*====================================================================*/
1010 /*=== TOP-LEVEL: VG_(finalise_thread1state) ===*/
1011 /*====================================================================*/
1013 /* Just before starting the client, we may need to make final
1014 adjustments to its initial image. Also we need to set up the VEX
1015 guest state for thread 1 (the root thread) and copy in essential
1016 starting values. This is handed the IIFinaliseImageInfo created by
1017 VG_(ii_create_image).
1019 void VG_(ii_finalise_image
)( IIFinaliseImageInfo iifii
)
1021 ThreadArchState
* arch
= &VG_(threads
)[1].arch
;
1023 /* On Linux we get client_{ip/sp/toc}, and start the client with
1024 all other registers zeroed. */
1026 # if defined(VGP_x86_linux)
1027 vg_assert(0 == sizeof(VexGuestX86State
) % LibVEX_GUEST_STATE_ALIGN
);
1029 /* Zero out the initial state, and set up the simulated FPU in a
1031 LibVEX_GuestX86_initialise(&arch
->vex
);
1033 /* Zero out the shadow areas. */
1034 VG_(memset
)(&arch
->vex_shadow1
, 0, sizeof(VexGuestX86State
));
1035 VG_(memset
)(&arch
->vex_shadow2
, 0, sizeof(VexGuestX86State
));
1037 /* Put essential stuff into the new state. */
1038 arch
->vex
.guest_ESP
= iifii
.initial_client_SP
;
1039 arch
->vex
.guest_EIP
= iifii
.initial_client_IP
;
1041 /* initialise %cs, %ds and %ss to point at the operating systems
1042 default code, data and stack segments. Also %es (see #291253). */
1043 asm volatile("movw %%cs, %0" : : "m" (arch
->vex
.guest_CS
));
1044 asm volatile("movw %%ds, %0" : : "m" (arch
->vex
.guest_DS
));
1045 asm volatile("movw %%ss, %0" : : "m" (arch
->vex
.guest_SS
));
1046 asm volatile("movw %%es, %0" : : "m" (arch
->vex
.guest_ES
));
1048 # elif defined(VGP_amd64_linux)
1049 vg_assert(0 == sizeof(VexGuestAMD64State
) % LibVEX_GUEST_STATE_ALIGN
);
1051 /* Zero out the initial state, and set up the simulated FPU in a
1053 LibVEX_GuestAMD64_initialise(&arch
->vex
);
1055 /* Zero out the shadow areas. */
1056 VG_(memset
)(&arch
->vex_shadow1
, 0, sizeof(VexGuestAMD64State
));
1057 VG_(memset
)(&arch
->vex_shadow2
, 0, sizeof(VexGuestAMD64State
));
1059 /* Put essential stuff into the new state. */
1060 arch
->vex
.guest_RSP
= iifii
.initial_client_SP
;
1061 arch
->vex
.guest_RIP
= iifii
.initial_client_IP
;
1063 # elif defined(VGP_ppc32_linux)
1064 vg_assert(0 == sizeof(VexGuestPPC32State
) % LibVEX_GUEST_STATE_ALIGN
);
1066 /* Zero out the initial state, and set up the simulated FPU in a
1068 LibVEX_GuestPPC32_initialise(&arch
->vex
);
1070 /* Zero out the shadow areas. */
1071 VG_(memset
)(&arch
->vex_shadow1
, 0, sizeof(VexGuestPPC32State
));
1072 VG_(memset
)(&arch
->vex_shadow2
, 0, sizeof(VexGuestPPC32State
));
1074 /* Put essential stuff into the new state. */
1075 arch
->vex
.guest_GPR1
= iifii
.initial_client_SP
;
1076 arch
->vex
.guest_CIA
= iifii
.initial_client_IP
;
1078 # elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
1079 vg_assert(0 == sizeof(VexGuestPPC64State
) % LibVEX_GUEST_STATE_ALIGN
);
1081 /* Zero out the initial state, and set up the simulated FPU in a
1083 LibVEX_GuestPPC64_initialise(&arch
->vex
);
1085 /* Zero out the shadow areas. */
1086 VG_(memset
)(&arch
->vex_shadow1
, 0, sizeof(VexGuestPPC64State
));
1087 VG_(memset
)(&arch
->vex_shadow2
, 0, sizeof(VexGuestPPC64State
));
1089 /* Put essential stuff into the new state. */
1090 arch
->vex
.guest_GPR1
= iifii
.initial_client_SP
;
1091 arch
->vex
.guest_GPR2
= iifii
.initial_client_TOC
;
1092 arch
->vex
.guest_CIA
= iifii
.initial_client_IP
;
1093 #if defined(VGP_ppc64le_linux)
1094 arch
->vex
.guest_GPR12
= iifii
.initial_client_IP
;
1097 # elif defined(VGP_arm_linux)
1098 /* Zero out the initial state, and set up the simulated FPU in a
1100 LibVEX_GuestARM_initialise(&arch
->vex
);
1102 /* Zero out the shadow areas. */
1103 VG_(memset
)(&arch
->vex_shadow1
, 0, sizeof(VexGuestARMState
));
1104 VG_(memset
)(&arch
->vex_shadow2
, 0, sizeof(VexGuestARMState
));
1106 arch
->vex
.guest_R13
= iifii
.initial_client_SP
;
1107 arch
->vex
.guest_R15T
= iifii
.initial_client_IP
;
1109 /* This is just EABI stuff. */
1110 // FIXME jrs: what's this for?
1111 arch
->vex
.guest_R1
= iifii
.initial_client_SP
;
1113 # elif defined(VGP_arm64_linux)
1114 /* Zero out the initial state. */
1115 LibVEX_GuestARM64_initialise(&arch
->vex
);
1117 /* Zero out the shadow areas. */
1118 VG_(memset
)(&arch
->vex_shadow1
, 0, sizeof(VexGuestARM64State
));
1119 VG_(memset
)(&arch
->vex_shadow2
, 0, sizeof(VexGuestARM64State
));
1121 arch
->vex
.guest_XSP
= iifii
.initial_client_SP
;
1122 arch
->vex
.guest_PC
= iifii
.initial_client_IP
;
1124 # elif defined(VGP_s390x_linux)
1125 vg_assert(0 == sizeof(VexGuestS390XState
) % LibVEX_GUEST_STATE_ALIGN
);
1127 /* Zero out the initial state. This also sets the guest_fpc to 0, which
1128 is also done by the kernel for the fpc during execve. */
1129 LibVEX_GuestS390X_initialise(&arch
->vex
);
1131 /* Mark all registers as undefined ... */
1132 VG_(memset
)(&arch
->vex_shadow1
, 0xFF, sizeof(VexGuestS390XState
));
1133 VG_(memset
)(&arch
->vex_shadow2
, 0x00, sizeof(VexGuestS390XState
));
1134 /* ... except SP, FPC, and IA */
1135 arch
->vex_shadow1
.guest_SP
= 0;
1136 arch
->vex_shadow1
.guest_fpc
= 0;
1137 arch
->vex_shadow1
.guest_IA
= 0;
1139 /* Put essential stuff into the new state. */
1140 arch
->vex
.guest_SP
= iifii
.initial_client_SP
;
1141 arch
->vex
.guest_IA
= iifii
.initial_client_IP
;
1142 /* See sys_execve in <linux>/arch/s390/kernel/process.c */
1143 arch
->vex
.guest_fpc
= 0;
1145 /* Tell the tool about the registers we just wrote */
1146 VG_TRACK(post_reg_write
, Vg_CoreStartup
, /*tid*/1, VG_O_STACK_PTR
, 8);
1147 VG_TRACK(post_reg_write
, Vg_CoreStartup
, /*tid*/1, VG_O_FPC_REG
, 4);
1148 VG_TRACK(post_reg_write
, Vg_CoreStartup
, /*tid*/1, VG_O_INSTR_PTR
, 8);
1150 /* At the end of this function there is code to mark all guest state
1151 registers as defined. For s390 that would be wrong, because the ABI
1152 says that all registers except SP, IA, and FPC are undefined upon
1154 #define PRECISE_GUEST_REG_DEFINEDNESS_AT_STARTUP 1
1156 # elif defined(VGP_mips32_linux)
1157 vg_assert(0 == sizeof(VexGuestMIPS32State
) % LibVEX_GUEST_STATE_ALIGN
);
1158 /* Zero out the initial state, and set up the simulated FPU in a
1160 LibVEX_GuestMIPS32_initialise(&arch
->vex
);
1162 /* Zero out the shadow areas. */
1163 VG_(memset
)(&arch
->vex_shadow1
, 0, sizeof(VexGuestMIPS32State
));
1164 VG_(memset
)(&arch
->vex_shadow2
, 0, sizeof(VexGuestMIPS32State
));
1166 arch
->vex
.guest_r29
= iifii
.initial_client_SP
;
1167 arch
->vex
.guest_PC
= iifii
.initial_client_IP
;
1168 arch
->vex
.guest_r31
= iifii
.initial_client_SP
;
1170 # elif defined(VGP_mips64_linux)
1171 vg_assert(0 == sizeof(VexGuestMIPS64State
) % LibVEX_GUEST_STATE_ALIGN
);
1172 /* Zero out the initial state, and set up the simulated FPU in a
1174 LibVEX_GuestMIPS64_initialise(&arch
->vex
);
1176 /* Zero out the shadow areas. */
1177 VG_(memset
)(&arch
->vex_shadow1
, 0, sizeof(VexGuestMIPS64State
));
1178 VG_(memset
)(&arch
->vex_shadow2
, 0, sizeof(VexGuestMIPS64State
));
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
;
1184 # elif defined(VGP_tilegx_linux)
1185 vg_assert(0 == sizeof(VexGuestTILEGXState
) % LibVEX_GUEST_STATE_ALIGN
);
1187 /* Zero out the initial state. */
1188 LibVEX_GuestTILEGX_initialise(&arch
->vex
);
1190 /* Zero out the shadow areas. */
1191 VG_(memset
)(&arch
->vex_shadow1
, 0, sizeof(VexGuestTILEGXState
));
1192 VG_(memset
)(&arch
->vex_shadow2
, 0, sizeof(VexGuestTILEGXState
));
1194 /* Put essential stuff into the new state. */
1195 arch
->vex
.guest_r54
= iifii
.initial_client_SP
;
1196 arch
->vex
.guest_pc
= iifii
.initial_client_IP
;
1199 # error Unknown platform
1202 # if !defined(PRECISE_GUEST_REG_DEFINEDNESS_AT_STARTUP)
1203 /* Tell the tool that we just wrote to the registers. */
1204 VG_TRACK( post_reg_write
, Vg_CoreStartup
, /*tid*/1, /*offset*/0,
1205 sizeof(VexGuestArchState
));
1208 /* Tell the tool about the client data segment and then kill it which will
1209 make it inaccessible/unaddressable. */
1210 const NSegment
*seg
= VG_(am_find_nsegment
)(VG_(brk_base
));
1212 vg_assert(seg
->kind
== SkAnonC
);
1213 VG_TRACK(new_mem_brk
, VG_(brk_base
), seg
->end
+ 1 - VG_(brk_base
),
1215 VG_TRACK(die_mem_brk
, VG_(brk_base
), seg
->end
+ 1 - VG_(brk_base
));
1218 #endif // defined(VGO_linux)
1220 /*--------------------------------------------------------------------*/
1222 /*--------------------------------------------------------------------*/