1 
2 /*--------------------------------------------------------------------*/
3 /*--- Startup: the real stuff                             m_main.c ---*/
4 /*--------------------------------------------------------------------*/
5 
6 /*
7    This file is part of Valgrind, a dynamic binary instrumentation
8    framework.
9 
10    Copyright (C) 2000-2017 Julian Seward
11       jseward@acm.org
12 
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of the
16    License, or (at your option) any later version.
17 
18    This program is distributed in the hope that it will be useful, but
19    WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    General Public License for more details.
22 
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26    02111-1307, USA.
27 
28    The GNU General Public License is contained in the file COPYING.
29 */
30 
31 #include "vgversion.h"
32 #include "pub_core_basics.h"
33 #include "pub_core_vki.h"
34 #include "pub_core_threadstate.h"
35 #include "pub_core_xarray.h"
36 #include "pub_core_clientstate.h"
37 #include "pub_core_aspacemgr.h"
38 #include "pub_core_aspacehl.h"
39 #include "pub_core_clreq.h"
40 #include "pub_core_commandline.h"
41 #include "pub_core_debuglog.h"
42 #include "pub_core_errormgr.h"
43 #include "pub_core_execontext.h"
44 #include "pub_core_gdbserver.h"
45 #include "pub_core_initimg.h"
46 #include "pub_core_libcbase.h"
47 #include "pub_core_libcassert.h"
48 #include "pub_core_libcfile.h"
49 #include "pub_core_libcprint.h"
50 #include "pub_core_libcproc.h"
51 #include "pub_core_libcsignal.h"
52 #include "pub_core_sbprofile.h"
53 #include "pub_core_mach.h"
54 #include "pub_core_machine.h"
55 #include "pub_core_mallocfree.h"
56 #include "pub_core_options.h"
57 #include "pub_core_debuginfo.h"
58 #include "pub_core_redir.h"
59 #include "pub_core_scheduler.h"
60 #include "pub_core_seqmatch.h"      // For VG_(string_match)
61 #include "pub_core_signals.h"
62 #include "pub_core_stacks.h"        // For VG_(register_stack)
63 #include "pub_core_syswrap.h"
64 #include "pub_core_tooliface.h"
65 #include "pub_core_translate.h"     // For VG_(translate)
66 #include "pub_core_trampoline.h"
67 #include "pub_core_transtab.h"
68 #include "pub_core_inner.h"
69 #if defined(ENABLE_INNER_CLIENT_REQUEST)
70 #include "pub_core_clreq.h"
71 #endif
72 
73 
74 /*====================================================================*/
75 /*=== Command-line: variables, processing, etc                     ===*/
76 /*====================================================================*/
77 
78 // See pub_{core,tool}_options.h for explanations of all these.
79 
usage_NORETURN(Bool debug_help)80 static void usage_NORETURN ( Bool debug_help )
81 {
82    /* 'usage1' contains a %s
83       - for the name of the GDB executable
84       - for the name of vgdb's path prefix
85       which must be supplied when they are VG_(printf)'d. */
86    const HChar usage1[] =
87 "usage: valgrind [options] prog-and-args\n"
88 "\n"
89 "  tool-selection option, with default in [ ]:\n"
90 "    --tool=<name>             use the Valgrind tool named <name> [memcheck]\n"
91 "\n"
92 "  basic user options for all Valgrind tools, with defaults in [ ]:\n"
93 "    -h --help                 show this message\n"
94 "    --help-debug              show this message, plus debugging options\n"
95 "    --version                 show version\n"
96 "    -q --quiet                run silently; only print error msgs\n"
97 "    -v --verbose              be more verbose -- show misc extra info\n"
98 "    --trace-children=no|yes   Valgrind-ise child processes (follow execve)? [no]\n"
99 "    --trace-children-skip=patt1,patt2,...    specifies a list of executables\n"
100 "                              that --trace-children=yes should not trace into\n"
101 "    --trace-children-skip-by-arg=patt1,patt2,...   same as --trace-children-skip=\n"
102 "                              but check the argv[] entries for children, rather\n"
103 "                              than the exe name, to make a follow/no-follow decision\n"
104 "    --child-silent-after-fork=no|yes omit child output between fork & exec? [no]\n"
105 "    --vgdb=no|yes|full        activate gdbserver? [yes]\n"
106 "                              full is slower but provides precise watchpoint/step\n"
107 "    --vgdb-error=<number>     invoke gdbserver after <number> errors [%d]\n"
108 "                              to get started quickly, use --vgdb-error=0\n"
109 "                              and follow the on-screen directions\n"
110 "    --vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none]\n"
111 "         where event is one of:\n"
112 "           startup exit valgrindabexit all none\n"
113 "    --track-fds=no|yes        track open file descriptors? [no]\n"
114 "    --time-stamp=no|yes       add timestamps to log messages? [no]\n"
115 "    --log-fd=<number>         log messages to file descriptor [2=stderr]\n"
116 "    --log-file=<file>         log messages to <file>\n"
117 "    --log-socket=ipaddr:port  log messages to socket ipaddr:port\n"
118 "\n"
119 "  user options for Valgrind tools that report errors:\n"
120 "    --xml=yes                 emit error output in XML (some tools only)\n"
121 "    --xml-fd=<number>         XML output to file descriptor\n"
122 "    --xml-file=<file>         XML output to <file>\n"
123 "    --xml-socket=ipaddr:port  XML output to socket ipaddr:port\n"
124 "    --xml-user-comment=STR    copy STR verbatim into XML output\n"
125 "    --demangle=no|yes         automatically demangle C++ names? [yes]\n"
126 "    --num-callers=<number>    show <number> callers in stack traces [12]\n"
127 "    --error-limit=no|yes      stop showing new errors if too many? [yes]\n"
128 "    --exit-on-first-error=no|yes exit code on the first error found? [no]\n"
129 "    --error-exitcode=<number> exit code to return if errors found [0=disable]\n"
130 "    --error-markers=<begin>,<end> add lines with begin/end markers before/after\n"
131 "                              each error output in plain text mode [none]\n"
132 "    --show-error-list=no|yes  show detected errors list and\n"
133 "                              suppression counts at exit [no]\n"
134 "    -s                        same as --show-error-list=yes\n"
135 "    --keep-debuginfo=no|yes   Keep symbols etc for unloaded code [no]\n"
136 "                              This allows saved stack traces (e.g. memory leaks)\n"
137 "                              to include file/line info for code that has been\n"
138 "                              dlclose'd (or similar)\n"
139 "    --show-below-main=no|yes  continue stack traces below main() [no]\n"
140 "    --default-suppressions=yes|no\n"
141 "                              load default suppressions [yes]\n"
142 "    --suppressions=<filename> suppress errors described in <filename>\n"
143 "    --gen-suppressions=no|yes|all    print suppressions for errors? [no]\n"
144 "    --input-fd=<number>       file descriptor for input [0=stdin]\n"
145 "    --dsymutil=no|yes         run dsymutil on Mac OS X when helpful? [yes]\n"
146 "    --max-stackframe=<number> assume stack switch for SP changes larger\n"
147 "                              than <number> bytes [2000000]\n"
148 "    --main-stacksize=<number> set size of main thread's stack (in bytes)\n"
149 "                              [min(max(current 'ulimit' value,1MB),16MB)]\n"
150 "\n"
151 "  user options for Valgrind tools that replace malloc:\n"
152 "    --alignment=<number>      set minimum alignment of heap allocations [%s]\n"
153 "    --redzone-size=<number>   set minimum size of redzones added before/after\n"
154 "                              heap blocks (in bytes). [%s]\n"
155 "    --xtree-memory=none|allocs|full   profile heap memory in an xtree [none]\n"
156 "                              and produces a report at the end of the execution\n"
157 "                              none: no profiling, allocs: current allocated\n"
158 "                              size/blocks, full: profile current and cumulative\n"
159 "                              allocated size/blocks and freed size/blocks.\n"
160 "    --xtree-memory-file=<file>   xtree memory report file [xtmemory.kcg.%%p]\n"
161 "\n"
162 "  uncommon user options for all Valgrind tools:\n"
163 "    --fullpath-after=         (with nothing after the '=')\n"
164 "                              show full source paths in call stacks\n"
165 "    --fullpath-after=string   like --fullpath-after=, but only show the\n"
166 "                              part of the path after 'string'.  Allows removal\n"
167 "                              of path prefixes.  Use this flag multiple times\n"
168 "                              to specify a set of prefixes to remove.\n"
169 "    --extra-debuginfo-path=path    absolute path to search for additional\n"
170 "                              debug symbols, in addition to existing default\n"
171 "                              well known search paths.\n"
172 "    --debuginfo-server=ipaddr:port    also query this server\n"
173 "                              (valgrind-di-server) for debug symbols\n"
174 "    --allow-mismatched-debuginfo=no|yes  [no]\n"
175 "                              for the above two flags only, accept debuginfo\n"
176 "                              objects that don't \"match\" the main object\n"
177 "    --smc-check=none|stack|all|all-non-file [all-non-file]\n"
178 "                              checks for self-modifying code: none, only for\n"
179 "                              code found in stacks, for all code, or for all\n"
180 "                              code except that from file-backed mappings\n"
181 "    --read-inline-info=yes|no read debug info about inlined function calls\n"
182 "                              and use it to do better stack traces.\n"
183 "                              [yes] on Linux/Android/Solaris for the tools\n"
184 "                              Memcheck/Massif/Helgrind/DRD only.\n"
185 "                              [no] for all other tools and platforms.\n"
186 "    --read-var-info=yes|no    read debug info on stack and global variables\n"
187 "                              and use it to print better error messages in\n"
188 "                              tools that make use of it (Memcheck, Helgrind,\n"
189 "                              DRD) [no]\n"
190 "    --vgdb-poll=<number>      gdbserver poll max every <number> basic blocks [%d] \n"
191 "    --vgdb-shadow-registers=no|yes   let gdb see the shadow registers [no]\n"
192 "    --vgdb-prefix=<prefix>    prefix for vgdb FIFOs [%s]\n"
193 "    --run-libc-freeres=no|yes free up glibc memory at exit on Linux? [yes]\n"
194 "    --run-cxx-freeres=no|yes  free up libstdc++ memory at exit on Linux\n"
195 "                              and Solaris? [yes]\n"
196 "    --sim-hints=hint1,hint2,...  activate unusual sim behaviours [none] \n"
197 "         where hint is one of:\n"
198 "           lax-ioctls lax-doors fuse-compatible enable-outer\n"
199 "           no-inner-prefix no-nptl-pthread-stackcache fallback-llsc none\n"
200 "    --fair-sched=no|yes|try   schedule threads fairly on multicore systems [no]\n"
201 "    --kernel-variant=variant1,variant2,...\n"
202 "         handle non-standard kernel variants [none]\n"
203 "         where variant is one of:\n"
204 "           bproc android-no-hw-tls\n"
205 "           android-gpu-sgx5xx android-gpu-adreno3xx none\n"
206 "    --merge-recursive-frames=<number>  merge frames between identical\n"
207 "           program counters in max <number> frames) [0]\n"
208 "    --num-transtab-sectors=<number> size of translated code cache [%d]\n"
209 "           more sectors may increase performance, but use more memory.\n"
210 "    --avg-transtab-entry-size=<number> avg size in bytes of a translated\n"
211 "           basic block [0, meaning use tool provided default]\n"
212 "    --aspace-minaddr=0xPP     avoid mapping memory below 0xPP [guessed]\n"
213 "    --valgrind-stacksize=<number> size of valgrind (host) thread's stack\n"
214 "                               (in bytes) ["
215                                 VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)
216                                                 "]\n"
217 "    --show-emwarns=no|yes     show warnings about emulation limits? [no]\n"
218 "    --require-text-symbol=:sonamepattern:symbolpattern    abort run if the\n"
219 "                              stated shared object doesn't have the stated\n"
220 "                              text symbol.  Patterns can contain ? and *.\n"
221 "    --soname-synonyms=syn1=pattern1,syn2=pattern2,... synonym soname\n"
222 "              specify patterns for function wrapping or replacement.\n"
223 "              To use a non-libc malloc library that is\n"
224 "                  in the main exe:  --soname-synonyms=somalloc=NONE\n"
225 "                  in libxyzzy.so:   --soname-synonyms=somalloc=libxyzzy.so\n"
226 "    --sigill-diagnostics=yes|no  warn about illegal instructions? [yes]\n"
227 "    --unw-stack-scan-thresh=<number>   Enable stack-scan unwind if fewer\n"
228 "                  than <number> good frames found  [0, meaning \"disabled\"]\n"
229 "                  NOTE: stack scanning is only available on arm-linux.\n"
230 "    --unw-stack-scan-frames=<number>   Max number of frames that can be\n"
231 "                  recovered by stack scanning [5]\n"
232 "    --resync-filter=no|yes|verbose [yes on MacOS, no on other OSes]\n"
233 "              attempt to avoid expensive address-space-resync operations\n"
234 "    --max-threads=<number>    maximum number of threads that valgrind can\n"
235 "                              handle [%d]\n"
236 "\n";
237 
238    const HChar usage2[] =
239 "\n"
240 "  debugging options for all Valgrind tools:\n"
241 "    -d                        show verbose debugging output\n"
242 "    --stats=no|yes            show tool and core statistics [no]\n"
243 "    --sanity-level=<number>   level of sanity checking to do [1]\n"
244 "    --trace-flags=<XXXXXXXX>   show generated code? (X = 0|1) [00000000]\n"
245 "    --profile-flags=<XXXXXXXX> ditto, but for profiling (X = 0|1) [00000000]\n"
246 "    --profile-interval=<number> show profile every <number> event checks\n"
247 "                                [0, meaning only at the end of the run]\n"
248 "    --trace-notbelow=<number> only show BBs above <number> [999999999]\n"
249 "    --trace-notabove=<number> only show BBs below <number> [0]\n"
250 "    --trace-syscalls=no|yes   show all system calls? [no]\n"
251 "    --trace-signals=no|yes    show signal handling details? [no]\n"
252 "    --trace-symtab=no|yes     show symbol table details? [no]\n"
253 "    --trace-symtab-patt=<patt> limit debuginfo tracing to obj name <patt>\n"
254 "    --trace-cfi=no|yes        show call-frame-info details? [no]\n"
255 "    --debug-dump=syms         mimic /usr/bin/readelf --syms\n"
256 "    --debug-dump=line         mimic /usr/bin/readelf --debug-dump=line\n"
257 "    --debug-dump=frames       mimic /usr/bin/readelf --debug-dump=frames\n"
258 "    --trace-redir=no|yes      show redirection details? [no]\n"
259 "    --trace-sched=no|yes      show thread scheduler details? [no]\n"
260 "    --profile-heap=no|yes     profile Valgrind's own space use\n"
261 "    --core-redzone-size=<number>  set minimum size of redzones added before/after\n"
262 "                              heap blocks allocated for Valgrind internal use (in bytes) [4]\n"
263 "    --wait-for-gdb=yes|no     pause on startup to wait for gdb attach\n"
264 "    --sym-offsets=yes|no      show syms in form 'name+offset'? [no]\n"
265 "    --progress-interval=<number>  report progress every <number>\n"
266 "                                  CPU seconds [0, meaning disabled]\n"
267 "    --command-line-only=no|yes  only use command line options [no]\n"
268 "\n"
269 "  Vex options for all Valgrind tools:\n"
270 "    --vex-iropt-verbosity=<0..9>           [0]\n"
271 "    --vex-iropt-level=<0..2>               [2]\n"
272 "    --vex-iropt-unroll-thresh=<0..400>     [120]\n"
273 "    --vex-guest-max-insns=<1..100>         [50]\n"
274 "    --vex-guest-chase-thresh=<0..99>       [10]\n"
275 "    --vex-guest-chase-cond=no|yes          [no]\n"
276 "    Precise exception control.  Possible values for 'mode' are as follows\n"
277 "      and specify the minimum set of registers guaranteed to be correct\n"
278 "      immediately prior to memory access instructions:\n"
279 "         sp-at-mem-access          stack pointer only\n"
280 "         unwindregs-at-mem-access  registers needed for stack unwinding\n"
281 "         allregs-at-mem-access     all registers\n"
282 "         allregs-at-each-insn      all registers are always correct\n"
283 "      Default value for all 3 following flags is [unwindregs-at-mem-access].\n"
284 "      --vex-iropt-register-updates=mode   setting to use by default\n"
285 "      --px-default=mode      synonym for --vex-iropt-register-updates\n"
286 "      --px-file-backed=mode  optional setting for file-backed (non-JIT) code\n"
287 "    Tracing and profile control:\n"
288 "      --trace-flags and --profile-flags values (omit the middle space):\n"
289 "         1000 0000   show conversion into IR\n"
290 "         0100 0000   show after initial opt\n"
291 "         0010 0000   show after instrumentation\n"
292 "         0001 0000   show after second opt\n"
293 "         0000 1000   show after tree building\n"
294 "         0000 0100   show selecting insns\n"
295 "         0000 0010   show after reg-alloc\n"
296 "         0000 0001   show final assembly\n"
297 "         0000 0000   show summary profile only\n"
298 "        (Nb: you need --trace-notbelow and/or --trace-notabove\n"
299 "             with --trace-flags for full details)\n"
300 "    --vex-regalloc-version=2|3             [3]\n"
301 "\n"
302 "  debugging options for Valgrind tools that report errors\n"
303 "    --dump-error=<number>     show translation for basic block associated\n"
304 "                              with <number>'th error context [0=show none]\n"
305 "\n"
306 "  debugging options for Valgrind tools that replace malloc:\n"
307 "    --trace-malloc=no|yes     show client malloc details? [no]\n"
308 "    --xtree-compress-strings=no|yes   compress strings in xtree callgrind format [yes]\n"
309 "\n";
310 
311    const HChar usage3[] =
312 "\n"
313 "  Extra options read from ~/.valgrindrc, $VALGRIND_OPTS, ./.valgrindrc\n"
314 "\n"
315 "  %s is %s\n"
316 "  Valgrind is Copyright (C) 2000-2017, and GNU GPL'd, by Julian Seward et al.\n"
317 "  LibVEX is Copyright (C) 2004-2017, and GNU GPL'd, by OpenWorks LLP et al.\n"
318 "\n"
319 "  Bug reports, feedback, admiration, abuse, etc, to: %s.\n"
320 "\n";
321 
322    HChar default_alignment[30];      // large enough
323    HChar default_redzone_size[30];   // large enough
324 
325    // Ensure the message goes to stdout
326    VG_(log_output_sink).fd = 1;
327    VG_(log_output_sink).type = VgLogTo_Fd;
328 
329    if (VG_(needs).malloc_replacement) {
330       VG_(sprintf)(default_alignment,    "%d",  VG_MIN_MALLOC_SZB);
331       VG_(sprintf)(default_redzone_size, "%lu", VG_(tdict).tool_client_redzone_szB);
332    } else {
333       VG_(strcpy)(default_alignment,    "not used by this tool");
334       VG_(strcpy)(default_redzone_size, "not used by this tool");
335    }
336    /* 'usage1' a type as described after each arg. */
337    VG_(printf)(usage1,
338                VG_(clo_vgdb_error)        /* int */,
339                default_alignment          /* char* */,
340                default_redzone_size       /* char* */,
341                VG_(clo_vgdb_poll)         /* int */,
342                VG_(vgdb_prefix_default)() /* char* */,
343                N_SECTORS_DEFAULT          /* int */,
344                MAX_THREADS_DEFAULT        /* int */
345                );
346    if (VG_(details).name) {
347       VG_(printf)("  user options for %s:\n", VG_(details).name);
348       if (VG_(needs).command_line_options)
349 	 VG_TDICT_CALL(tool_print_usage);
350       else
351 	 VG_(printf)("    (none)\n");
352    }
353    if (debug_help) {
354       VG_(printf)("%s", usage2);
355 
356       if (VG_(details).name) {
357          VG_(printf)("  debugging options for %s:\n", VG_(details).name);
358 
359          if (VG_(needs).command_line_options)
360             VG_TDICT_CALL(tool_print_debug_usage);
361          else
362             VG_(printf)("    (none)\n");
363       }
364    }
365    VG_(printf)(usage3, VG_(details).name, VG_(details).copyright_author,
366                VG_BUGS_TO);
367    VG_(exit)(0);
368 }
369 
370 
371 /* Peer at previously set up VG_(args_for_valgrind) and do some
372    minimal command line processing that must happen early on:
373 
374    - show the version string, if requested (-v)
375    - extract any request for help (--help, -h, --help-debug)
376    - set VG_(toolname) (--tool=)
377    - set VG_(clo_max_stackframe) (--max-stackframe=)
378    - set VG_(clo_main_stacksize) (--main-stacksize=)
379    - set VG_(clo_sim_hints) (--sim-hints=)
380    - set VG_(clo_max_threads) (--max-threads)
381 
382    That's all it does.  The main command line processing is done below
383    by main_process_cmd_line_options.  Note that
384    main_process_cmd_line_options has to handle but ignore the ones we
385    have handled here.
386 */
early_process_cmd_line_options(Int * need_help)387 static void early_process_cmd_line_options ( /*OUT*/Int* need_help )
388 {
389    UInt   i;
390    HChar* str;
391    Int need_version = 0;
392 
393    vg_assert( VG_(args_for_valgrind) );
394 
395    /* parse the options we have (only the options we care about now) */
396    for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) {
397 
398       str = * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i );
399       vg_assert(str);
400 
401       if VG_XACT_CLO(str, "--version", need_version, 1) {}
402       else if (VG_STREQ(str, "-v") ||
403                VG_STREQ(str, "--verbose"))
404          VG_(clo_verbosity)++;
405       else if (VG_STREQ(str, "-q") ||
406                VG_STREQ(str, "--quiet"))
407          VG_(clo_verbosity)--;
408       else if VG_XACT_CLO(str, "--help", *need_help, *need_help+1) {}
409       else if VG_XACT_CLO(str, "-h",     *need_help, *need_help+1) {}
410 
411       else if VG_XACT_CLO(str, "--help-debug", *need_help, *need_help+2) {}
412 
413       // The tool has already been determined, but we need to know the name
414       // here.
415       else if VG_STR_CLO(str, "--tool", VG_(clo_toolname)) {}
416 
417       // Set up VG_(clo_max_stackframe) and VG_(clo_main_stacksize).
418       // These are needed by VG_(ii_create_image), which happens
419       // before main_process_cmd_line_options().
420       else if VG_INT_CLO(str, "--max-stackframe", VG_(clo_max_stackframe)) {}
421       else if VG_INT_CLO(str, "--main-stacksize", VG_(clo_main_stacksize)) {}
422 
423       // Set up VG_(clo_max_threads); needed for VG_(tl_pre_clo_init)
424       else if VG_INT_CLO(str, "--max-threads", VG_(clo_max_threads)) {}
425 
426       // Set up VG_(clo_sim_hints). This is needed a.o. for an inner
427       // running in an outer, to have "no-inner-prefix" enabled
428       // as early as possible.
429       else if VG_USETX_CLO (str, "--sim-hints",
430                             "lax-ioctls,lax-doors,fuse-compatible,"
431                             "enable-outer,no-inner-prefix,"
432                             "no-nptl-pthread-stackcache,fallback-llsc",
433                             VG_(clo_sim_hints)) {}
434    }
435 
436    if (need_version) {
437       // Nb: the version string goes to stdout.
438       VG_(log_output_sink).fd = 1;
439       VG_(log_output_sink).type = VgLogTo_Fd;
440       if (VG_(clo_verbosity) <= 1)
441          VG_(printf)("valgrind-" VERSION "\n");
442       else
443          VG_(printf)("valgrind-" VERSION "-" VGGIT "\n");
444       VG_(exit)(0);
445    }
446 
447    /* For convenience */
448    VG_N_THREADS = VG_(clo_max_threads);
449 
450 #  if defined(VGO_solaris) || defined(VGO_darwin)
451    /* Sim hint no-nptl-pthread-stackcache should be ignored. */
452    VG_(clo_sim_hints) &= ~SimHint2S(SimHint_no_nptl_pthread_stackcache);
453 #  endif
454 }
455 
456 /* The main processing for command line options.  See comments above
457    on early_process_cmd_line_options. */
458 static
main_process_cmd_line_options(void)459 void main_process_cmd_line_options( void )
460 {
461    Int   i;
462    Int   toolname_len = VG_(strlen)(VG_(clo_toolname));
463    const HChar* tmp_str;         // Used in a couple of places.
464 
465    /* Whether the user has explicitly provided --sigill-diagnostics
466       or --show-error-list.
467       If not explicitly given depends on general verbosity setting. */
468    Bool sigill_diag_set = False;
469    Bool show_error_list_set = False;
470 
471    /* Log to stderr by default, but usage message goes to stdout.  XML
472       output is initially disabled. */
473    VgLogTo log_to = VgLogTo_Fd;  // Where is logging output to be sent?
474    VgLogTo xml_to = VgLogTo_Fd;  // Where is XML output to be sent?
475    Int tmp_log_fd = 2;
476    Int tmp_xml_fd = -1;
477 
478    /* Check for sane path in ./configure --prefix=... */
479    if (VG_LIBDIR[0] != '/')
480       VG_(err_config_error)("Please use absolute paths in "
481                             "./configure --prefix=... or --libdir=...\n");
482 
483    vg_assert( VG_(args_for_valgrind) );
484 
485    VG_(clo_suppressions) = VG_(newXA)(VG_(malloc), "main.mpclo.4",
486                                       VG_(free), sizeof(HChar *));
487    VG_(clo_fullpath_after) = VG_(newXA)(VG_(malloc), "main.mpclo.5",
488                                         VG_(free), sizeof(HChar *));
489    VG_(clo_req_tsyms) = VG_(newXA)(VG_(malloc), "main.mpclo.6",
490                                    VG_(free), sizeof(HChar *));
491 
492    /* Constants for parsing PX control flags. */
493    const HChar* pxStrings[5]
494       = { "sp-at-mem-access",      "unwindregs-at-mem-access",
495           "allregs-at-mem-access", "allregs-at-each-insn", NULL };
496    const VexRegisterUpdates pxVals[5]
497       = { VexRegUpdSpAtMemAccess,      VexRegUpdUnwindregsAtMemAccess,
498           VexRegUpdAllregsAtMemAccess, VexRegUpdAllregsAtEachInsn, 0/*inval*/ };
499 
500    /* BEGIN command-line processing loop */
501 
502    for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) {
503 
504       HChar* arg   = * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i );
505       HChar* colon = arg;
506       UInt   ix    = 0;
507 
508       // Look for a colon in the option name.
509       while (*colon && *colon != ':' && *colon != '=')
510          colon++;
511 
512       // Does it have the form "--toolname:foo"?  We have to do it at the start
513       // in case someone has combined a prefix with a core-specific option,
514       // eg.  "--memcheck:verbose".
515       if (*colon == ':') {
516          if (VG_STREQN(2,            arg,                "--") &&
517              VG_STREQN(toolname_len, arg+2,              VG_(clo_toolname)) &&
518              VG_STREQN(1,            arg+2+toolname_len, ":"))
519          {
520             // Prefix matches, convert "--toolname:foo" to "--foo".
521             // Two things to note:
522             // - We cannot modify the option in-place.  If we did, and then
523             //   a child was spawned with --trace-children=yes, the
524             //   now-non-prefixed option would be passed and could screw up
525             //   the child.
526             // - We create copies, and never free them.  Why?  Non-prefixed
527             //   options hang around forever, so tools need not make copies
528             //   of strings within them.  We need to have the same behaviour
529             //   for prefixed options.  The pointer to the copy will be lost
530             //   once we leave this function (although a tool may keep a
531             //   pointer into it), but the space wasted is insignificant.
532             //   (In bug #142197, the copies were being freed, which caused
533             //   problems for tools that reasonably assumed that arguments
534             //   wouldn't disappear on them.)
535             if (0)
536                VG_(printf)("tool-specific arg: %s\n", arg);
537             arg = VG_(strdup)("main.mpclo.1", arg + toolname_len + 1);
538             arg[0] = '-';
539             arg[1] = '-';
540 
541          } else {
542             // prefix doesn't match, skip to next arg
543             continue;
544          }
545       }
546 
547       /* Ignore these options - they've already been handled */
548       if      VG_STREQN( 7, arg, "--tool=")              {}
549       else if VG_STREQN(20, arg, "--command-line-only=") {}
550       else if VG_STREQ(     arg, "--")                   {}
551       else if VG_STREQ(     arg, "-d")                   {}
552       else if VG_STREQ(     arg, "-q")                   {}
553       else if VG_STREQ(     arg, "--quiet")              {}
554       else if VG_STREQ(     arg, "-v")                   {}
555       else if VG_STREQ(     arg, "--verbose")            {}
556       else if VG_STREQN(17, arg, "--max-stackframe=")    {}
557       else if VG_STREQN(17, arg, "--main-stacksize=")    {}
558       else if VG_STREQN(14, arg, "--max-threads=")       {}
559       else if VG_STREQN(12, arg, "--sim-hints=")         {}
560       else if VG_STREQN(15, arg, "--profile-heap=")      {}
561       else if VG_STREQN(20, arg, "--core-redzone-size=") {}
562       else if VG_STREQN(15, arg, "--redzone-size=")      {}
563       else if VG_STREQN(17, arg, "--aspace-minaddr=")    {}
564 
565       else if VG_BINT_CLO(arg, "--valgrind-stacksize",
566                           VG_(clo_valgrind_stacksize),
567                           2*VKI_PAGE_SIZE, 10*VG_DEFAULT_STACK_ACTIVE_SZB)
568                             {VG_(clo_valgrind_stacksize)
569                                   = VG_PGROUNDUP(VG_(clo_valgrind_stacksize));}
570 
571       /* Obsolete options. Report an error and exit */
572       else if VG_STREQN(34, arg, "--vex-iropt-precise-memory-exns=no") {
573          VG_(fmsg_bad_option)
574             (arg,
575              "--vex-iropt-precise-memory-exns is obsolete\n"
576              "Use --vex-iropt-register-updates=unwindregs-at-mem-access instead\n");
577       }
578       else if VG_STREQN(35, arg, "--vex-iropt-precise-memory-exns=yes") {
579          VG_(fmsg_bad_option)
580             (arg,
581              "--vex-iropt-precise-memory-exns is obsolete\n"
582              "Use --vex-iropt-register-updates=allregs-at-mem-access instead\n"
583              " (or --vex-iropt-register-updates=allregs-at-each-insn)\n");
584       }
585 
586       /* These options are new, not yet handled by
587          early_process_cmd_line_options. */
588       else if VG_BOOL_CLO(arg, "--sigill-diagnostics", VG_(clo_sigill_diag))
589          sigill_diag_set = True;
590 
591       else if VG_BOOL_CLO(arg, "--stats",          VG_(clo_stats)) {}
592       else if VG_BOOL_CLO(arg, "--xml",            VG_(clo_xml))
593          VG_(debugLog_setXml)(VG_(clo_xml));
594 
595       else if VG_XACT_CLO(arg, "--vgdb=no",        VG_(clo_vgdb), Vg_VgdbNo) {}
596       else if VG_XACT_CLO(arg, "--vgdb=yes",       VG_(clo_vgdb), Vg_VgdbYes) {}
597       else if VG_XACT_CLO(arg, "--vgdb=full",      VG_(clo_vgdb), Vg_VgdbFull) {
598          /* automatically updates register values at each insn
599             with --vgdb=full */
600          VG_(clo_vex_control).iropt_register_updates_default
601             = VG_(clo_px_file_backed)
602             = VexRegUpdAllregsAtEachInsn;
603       }
604       else if VG_INT_CLO (arg, "--vgdb-poll",      VG_(clo_vgdb_poll)) {}
605       else if VG_INT_CLO (arg, "--vgdb-error",     VG_(clo_vgdb_error)) {}
606       else if VG_USET_CLO (arg, "--vgdb-stop-at",
607                            "startup,exit,valgrindabexit",
608                            VG_(clo_vgdb_stop_at)) {}
609       else if VG_STR_CLO (arg, "--vgdb-prefix",    VG_(clo_vgdb_prefix)) {
610          VG_(arg_vgdb_prefix) = arg;
611       }
612       else if VG_BOOL_CLO(arg, "--vgdb-shadow-registers",
613                             VG_(clo_vgdb_shadow_registers)) {}
614       else if VG_BOOL_CLO(arg, "--demangle",       VG_(clo_demangle)) {}
615       else if VG_STR_CLO (arg, "--soname-synonyms",VG_(clo_soname_synonyms)) {}
616       else if VG_BOOL_CLO(arg, "--error-limit",    VG_(clo_error_limit)) {}
617       else if VG_BOOL_CLO(arg, "--exit-on-first-error", VG_(clo_exit_on_first_error)) {}
618       else if VG_INT_CLO (arg, "--error-exitcode", VG_(clo_error_exitcode)) {}
619       else if VG_STR_CLO (arg, "--error-markers",  tmp_str) {
620          Int m;
621          const HChar *startpos = tmp_str;
622          const HChar *nextpos;
623          for (m = 0;
624               m < sizeof(VG_(clo_error_markers))
625                  /sizeof(VG_(clo_error_markers)[0]);
626               m++) {
627             /* Release previous value if clo given multiple times. */
628             VG_(free)(VG_(clo_error_markers)[m]);
629             VG_(clo_error_markers)[m] = NULL;
630 
631             nextpos = VG_(strchr)(startpos, ',');
632             if (!nextpos)
633                nextpos = startpos + VG_(strlen)(startpos);
634             if (startpos != nextpos) {
635                VG_(clo_error_markers)[m]
636                   = VG_(malloc)("main.mpclo.2", nextpos - startpos + 1);
637                VG_(memcpy)(VG_(clo_error_markers)[m], startpos,
638                            nextpos - startpos);
639                VG_(clo_error_markers)[m][nextpos - startpos] = '\0';
640             }
641             startpos = *nextpos ? nextpos + 1 : nextpos;
642          }
643       }
644       else if VG_BOOL_CLO(arg, "--show-error-list", VG_(clo_show_error_list)) {
645             show_error_list_set = True; }
646       else if (VG_STREQ(arg, "-s")) {
647          VG_(clo_show_error_list) = True;
648          show_error_list_set = True;
649       }
650       else if VG_BOOL_CLO(arg, "--show-emwarns",   VG_(clo_show_emwarns)) {}
651 
652       else if VG_BOOL_CLO(arg, "--run-libc-freeres", VG_(clo_run_libc_freeres)) {}
653       else if VG_BOOL_CLO(arg, "--run-cxx-freeres",  VG_(clo_run_cxx_freeres)) {}
654       else if VG_BOOL_CLO(arg, "--show-below-main",  VG_(clo_show_below_main)) {}
655       else if VG_BOOL_CLO(arg, "--keep-debuginfo",   VG_(clo_keep_debuginfo)) {}
656       else if VG_BOOL_CLO(arg, "--time-stamp",       VG_(clo_time_stamp)) {}
657       else if VG_BOOL_CLO(arg, "--track-fds",        VG_(clo_track_fds)) {}
658       else if VG_BOOL_CLO(arg, "--trace-children",   VG_(clo_trace_children)) {}
659       else if VG_BOOL_CLO(arg, "--child-silent-after-fork",
660                             VG_(clo_child_silent_after_fork)) {}
661       else if VG_STR_CLO(arg, "--fair-sched",        tmp_str) {
662          if (VG_(strcmp)(tmp_str, "yes") == 0)
663             VG_(clo_fair_sched) = enable_fair_sched;
664          else if (VG_(strcmp)(tmp_str, "try") == 0)
665             VG_(clo_fair_sched) = try_fair_sched;
666          else if (VG_(strcmp)(tmp_str, "no") == 0)
667             VG_(clo_fair_sched) = disable_fair_sched;
668          else
669             VG_(fmsg_bad_option)(arg,
670                "Bad argument, should be 'yes', 'try' or 'no'\n");
671       }
672       else if VG_BOOL_CLO(arg, "--trace-sched",      VG_(clo_trace_sched)) {}
673       else if VG_BOOL_CLO(arg, "--trace-signals",    VG_(clo_trace_signals)) {}
674       else if VG_BOOL_CLO(arg, "--trace-symtab",     VG_(clo_trace_symtab)) {}
675       else if VG_STR_CLO (arg, "--trace-symtab-patt", VG_(clo_trace_symtab_patt)) {}
676       else if VG_BOOL_CLO(arg, "--trace-cfi",        VG_(clo_trace_cfi)) {}
677       else if VG_XACT_CLO(arg, "--debug-dump=syms",  VG_(clo_debug_dump_syms),
678                                                      True) {}
679       else if VG_XACT_CLO(arg, "--debug-dump=line",  VG_(clo_debug_dump_line),
680                                                      True) {}
681       else if VG_XACT_CLO(arg, "--debug-dump=frames",
682                                VG_(clo_debug_dump_frames), True) {}
683       else if VG_BOOL_CLO(arg, "--trace-redir",      VG_(clo_trace_redir)) {}
684 
685       else if VG_BOOL_CLO(arg, "--trace-syscalls",   VG_(clo_trace_syscalls)) {}
686       else if VG_BOOL_CLO(arg, "--wait-for-gdb",     VG_(clo_wait_for_gdb)) {}
687       else if VG_BOOL_CLO(arg, "--sym-offsets",      VG_(clo_sym_offsets)) {}
688       else if VG_BINT_CLO(arg, "--progress-interval",
689                                VG_(clo_progress_interval), 0, 3600) {}
690       else if VG_BOOL_CLO(arg, "--read-inline-info", VG_(clo_read_inline_info)) {}
691       else if VG_BOOL_CLO(arg, "--read-var-info",    VG_(clo_read_var_info)) {}
692 
693       else if VG_INT_CLO (arg, "--dump-error",       VG_(clo_dump_error))   {}
694       else if VG_INT_CLO (arg, "--input-fd",         VG_(clo_input_fd))     {}
695       else if VG_INT_CLO (arg, "--sanity-level",     VG_(clo_sanity_level)) {}
696       else if VG_BINT_CLO(arg, "--num-callers",      VG_(clo_backtrace_size), 1,
697                                                      VG_DEEPEST_BACKTRACE) {}
698       else if VG_BINT_CLO(arg, "--num-transtab-sectors",
699                                VG_(clo_num_transtab_sectors),
700                                MIN_N_SECTORS, MAX_N_SECTORS) {}
701       else if VG_BINT_CLO(arg, "--avg-transtab-entry-size",
702                                VG_(clo_avg_transtab_entry_size),
703                                50, 5000) {}
704       else if VG_BINT_CLO(arg, "--merge-recursive-frames",
705                                VG_(clo_merge_recursive_frames), 0,
706                                VG_DEEPEST_BACKTRACE) {}
707 
708       else if VG_XACT_CLO(arg, "--smc-check=none",
709                           VG_(clo_smc_check), Vg_SmcNone) {}
710       else if VG_XACT_CLO(arg, "--smc-check=stack",
711                           VG_(clo_smc_check), Vg_SmcStack) {}
712       else if VG_XACT_CLO(arg, "--smc-check=all",
713                           VG_(clo_smc_check), Vg_SmcAll) {}
714       else if VG_XACT_CLO(arg, "--smc-check=all-non-file",
715                           VG_(clo_smc_check), Vg_SmcAllNonFile) {}
716 
717       else if VG_USETX_CLO (arg, "--kernel-variant",
718                             "bproc,"
719                             "android-no-hw-tls,"
720                             "android-gpu-sgx5xx,"
721                             "android-gpu-adreno3xx",
722                             VG_(clo_kernel_variant)) {}
723 
724       else if VG_BOOL_CLO(arg, "--dsymutil",        VG_(clo_dsymutil)) {}
725 
726       else if VG_STR_CLO (arg, "--trace-children-skip",
727                                VG_(clo_trace_children_skip)) {}
728       else if VG_STR_CLO (arg, "--trace-children-skip-by-arg",
729                                VG_(clo_trace_children_skip_by_arg)) {}
730 
731       else if VG_BINT_CLO(arg, "--vex-iropt-verbosity",
732                        VG_(clo_vex_control).iropt_verbosity, 0, 10) {}
733       else if VG_BINT_CLO(arg, "--vex-iropt-level",
734                        VG_(clo_vex_control).iropt_level, 0, 2) {}
735       else if VG_BINT_CLO(arg, "--vex-regalloc-version",
736                        VG_(clo_vex_control).regalloc_version, 2, 3) {}
737 
738       else if VG_STRINDEX_CLO(arg, "--vex-iropt-register-updates",
739                                    pxStrings, ix) {
740          vg_assert(ix < 4);
741          vg_assert(pxVals[ix] >= VexRegUpdSpAtMemAccess);
742          vg_assert(pxVals[ix] <= VexRegUpdAllregsAtEachInsn);
743          VG_(clo_vex_control).iropt_register_updates_default = pxVals[ix];
744       }
745       else if VG_STRINDEX_CLO(arg, "--px-default", pxStrings, ix) {
746          // NB: --px-default is an alias for the hard-to-remember
747          // --vex-iropt-register-updates, hence the same logic.
748          vg_assert(ix < 4);
749          vg_assert(pxVals[ix] >= VexRegUpdSpAtMemAccess);
750          vg_assert(pxVals[ix] <= VexRegUpdAllregsAtEachInsn);
751          VG_(clo_vex_control).iropt_register_updates_default = pxVals[ix];
752       }
753       else if VG_STRINDEX_CLO(arg, "--px-file-backed", pxStrings, ix) {
754          // Whereas --px-file-backed isn't
755          // the same flag as --vex-iropt-register-updates.
756          vg_assert(ix < 4);
757          vg_assert(pxVals[ix] >= VexRegUpdSpAtMemAccess);
758          vg_assert(pxVals[ix] <= VexRegUpdAllregsAtEachInsn);
759          VG_(clo_px_file_backed) = pxVals[ix];
760       }
761 
762       else if VG_BINT_CLO(arg, "--vex-iropt-unroll-thresh",
763                        VG_(clo_vex_control).iropt_unroll_thresh, 0, 400) {}
764       else if VG_BINT_CLO(arg, "--vex-guest-max-insns",
765                        VG_(clo_vex_control).guest_max_insns, 1, 100) {}
766       else if VG_BINT_CLO(arg, "--vex-guest-chase-thresh",
767                        VG_(clo_vex_control).guest_chase_thresh, 0, 99) {}
768       else if VG_BOOL_CLO(arg, "--vex-guest-chase-cond",
769                        VG_(clo_vex_control).guest_chase_cond) {}
770 
771       else if VG_INT_CLO(arg, "--log-fd", tmp_log_fd) {
772          log_to = VgLogTo_Fd;
773          VG_(clo_log_fname_unexpanded) = NULL;
774       }
775       else if VG_INT_CLO(arg, "--xml-fd", tmp_xml_fd) {
776          xml_to = VgLogTo_Fd;
777          VG_(clo_xml_fname_unexpanded) = NULL;
778       }
779 
780       else if VG_STR_CLO(arg, "--log-file", VG_(clo_log_fname_unexpanded)) {
781          log_to = VgLogTo_File;
782       }
783       else if VG_STR_CLO(arg, "--xml-file", VG_(clo_xml_fname_unexpanded)) {
784          xml_to = VgLogTo_File;
785       }
786 
787       else if VG_STR_CLO(arg, "--log-socket", VG_(clo_log_fname_unexpanded)) {
788          log_to = VgLogTo_Socket;
789       }
790       else if VG_STR_CLO(arg, "--xml-socket", VG_(clo_xml_fname_unexpanded)) {
791          xml_to = VgLogTo_Socket;
792       }
793 
794       else if VG_STR_CLO(arg, "--debuginfo-server",
795                               VG_(clo_debuginfo_server)) {}
796 
797       else if VG_BOOL_CLO(arg, "--allow-mismatched-debuginfo",
798                                VG_(clo_allow_mismatched_debuginfo)) {}
799 
800       else if VG_STR_CLO(arg, "--xml-user-comment",
801                               VG_(clo_xml_user_comment)) {}
802 
803       else if VG_BOOL_CLO(arg, "--default-suppressions",
804                           VG_(clo_default_supp)) {}
805 
806       else if VG_STR_CLO(arg, "--suppressions", tmp_str) {
807          VG_(addToXA)(VG_(clo_suppressions), &tmp_str);
808       }
809 
810       else if VG_STR_CLO (arg, "--fullpath-after", tmp_str) {
811          VG_(addToXA)(VG_(clo_fullpath_after), &tmp_str);
812       }
813 
814       else if VG_STR_CLO (arg, "--extra-debuginfo-path",
815                       VG_(clo_extra_debuginfo_path)) {}
816 
817       else if VG_STR_CLO(arg, "--require-text-symbol", tmp_str) {
818          /* String needs to be of the form C?*C?*, where C is any
819             character, but is the same both times.  Having it in this
820             form facilitates finding the boundary between the sopatt
821             and the fnpatt just by looking for the second occurrence
822             of C, without hardwiring any assumption about what C
823             is. */
824          HChar patt[7];
825          Bool ok = True;
826          ok = tmp_str && VG_(strlen)(tmp_str) > 0;
827          if (ok) {
828            patt[0] = patt[3] = tmp_str[0];
829            patt[1] = patt[4] = '?';
830            patt[2] = patt[5] = '*';
831            patt[6] = 0;
832            ok = VG_(string_match)(patt, tmp_str);
833          }
834          if (!ok) {
835             VG_(fmsg_bad_option)(arg,
836                "Invalid --require-text-symbol= specification.\n");
837          }
838          VG_(addToXA)(VG_(clo_req_tsyms), &tmp_str);
839       }
840 
841       /* "stuvwxyz" --> stuvwxyz (binary) */
842       else if VG_STR_CLO(arg, "--trace-flags", tmp_str) {
843          Int j;
844          if (8 != VG_(strlen)(tmp_str)) {
845             VG_(fmsg_bad_option)(arg,
846                "--trace-flags argument must have 8 digits\n");
847          }
848          for (j = 0; j < 8; j++) {
849             if      ('0' == tmp_str[j]) { /* do nothing */ }
850             else if ('1' == tmp_str[j]) VG_(clo_trace_flags) |= (1 << (7-j));
851             else {
852                VG_(fmsg_bad_option)(arg,
853                   "--trace-flags argument can only contain 0s and 1s\n");
854             }
855          }
856       }
857 
858       else if VG_INT_CLO (arg, "--trace-notbelow", VG_(clo_trace_notbelow)) {}
859 
860       else if VG_INT_CLO (arg, "--trace-notabove", VG_(clo_trace_notabove)) {}
861 
862       /* "stuvwxyz" --> stuvwxyz (binary) */
863       else if VG_STR_CLO(arg, "--profile-flags", tmp_str) {
864          Int j;
865          if (8 != VG_(strlen)(tmp_str)) {
866             VG_(fmsg_bad_option)(arg,
867                "--profile-flags argument must have 8 digits\n");
868          }
869          for (j = 0; j < 8; j++) {
870             if      ('0' == tmp_str[j]) { /* do nothing */ }
871             else if ('1' == tmp_str[j]) VG_(clo_profyle_flags) |= (1 << (7-j));
872             else {
873                VG_(fmsg_bad_option)(arg,
874                   "--profile-flags argument can only contain 0s and 1s\n");
875             }
876          }
877          VG_(clo_profyle_sbs) = True;
878       }
879 
880       else if VG_INT_CLO (arg, "--profile-interval",
881                           VG_(clo_profyle_interval)) {}
882 
883       else if VG_XACT_CLO(arg, "--gen-suppressions=no",
884                                VG_(clo_gen_suppressions), 0) {}
885       else if VG_XACT_CLO(arg, "--gen-suppressions=yes",
886                                VG_(clo_gen_suppressions), 1) {}
887       else if VG_XACT_CLO(arg, "--gen-suppressions=all",
888                                VG_(clo_gen_suppressions), 2) {}
889 
890       else if VG_BINT_CLO(arg, "--unw-stack-scan-thresh",
891                           VG_(clo_unw_stack_scan_thresh), 0, 100) {}
892       else if VG_BINT_CLO(arg, "--unw-stack-scan-frames",
893                           VG_(clo_unw_stack_scan_frames), 0, 32) {}
894 
895       else if VG_XACT_CLO(arg, "--resync-filter=no",
896                                VG_(clo_resync_filter), 0) {}
897       else if VG_XACT_CLO(arg, "--resync-filter=yes",
898                                VG_(clo_resync_filter), 1) {}
899       else if VG_XACT_CLO(arg, "--resync-filter=verbose",
900                                VG_(clo_resync_filter), 2) {}
901 
902       else if ( ! VG_(needs).command_line_options
903              || ! VG_TDICT_CALL(tool_process_cmd_line_option, arg) ) {
904          VG_(fmsg_unknown_option)(arg);
905       }
906    }
907 
908    /* END command-line processing loop */
909 
910    /* Notify about deprecated features here. */
911 
912    /* Determine the path prefix for vgdb */
913    if (VG_(clo_vgdb_prefix) == NULL)
914      VG_(clo_vgdb_prefix) = VG_(vgdb_prefix_default)();
915 
916    /* Make VEX control parameters sane */
917 
918    if (VG_(clo_vex_control).guest_chase_thresh
919        >= VG_(clo_vex_control).guest_max_insns)
920       VG_(clo_vex_control).guest_chase_thresh
921          = VG_(clo_vex_control).guest_max_insns - 1;
922 
923    if (VG_(clo_vex_control).guest_chase_thresh < 0)
924       VG_(clo_vex_control).guest_chase_thresh = 0;
925 
926    /* Check various option values */
927 
928    if (VG_(clo_verbosity) < 0)
929       VG_(clo_verbosity) = 0;
930 
931    if (!sigill_diag_set)
932       VG_(clo_sigill_diag) = (VG_(clo_verbosity) > 0);
933 
934    if (!show_error_list_set) {
935       if (VG_(clo_xml))
936          VG_(clo_show_error_list) = VG_(clo_verbosity) >= 1;
937       else
938          VG_(clo_show_error_list) = VG_(clo_verbosity) >= 2;
939    }
940 
941    if (VG_(clo_trace_notbelow) == -1) {
942      if (VG_(clo_trace_notabove) == -1) {
943        /* [] */
944        VG_(clo_trace_notbelow) = 2147483647;
945        VG_(clo_trace_notabove) = 0;
946      } else {
947        /* [0 .. notabove] */
948        VG_(clo_trace_notbelow) = 0;
949      }
950    } else {
951      if (VG_(clo_trace_notabove) == -1) {
952        /* [notbelow .. ]  */
953        VG_(clo_trace_notabove) = 2147483647;
954      } else {
955        /* [notbelow .. notabove]  */
956      }
957    }
958 
959    VG_(dyn_vgdb_error) = VG_(clo_vgdb_error);
960 
961    if (VG_(clo_gen_suppressions) > 0 &&
962        !VG_(needs).core_errors && !VG_(needs).tool_errors) {
963       VG_(fmsg_bad_option)("--gen-suppressions=yes",
964          "Can't use --gen-suppressions= with %s\n"
965          "because it doesn't generate errors.\n", VG_(details).name);
966    }
967    if ((VG_(clo_exit_on_first_error)) &&
968        (VG_(clo_error_exitcode)==0)) {
969       VG_(fmsg_bad_option)("--exit-on-first-error=yes",
970          "You must define a non nul exit error code, with --error-exitcode=...\n");
971    }
972 
973 #  if !defined(VGO_darwin)
974    if (VG_(clo_resync_filter) != 0) {
975       VG_(fmsg_bad_option)("--resync-filter=yes or =verbose",
976                            "--resync-filter= is only available on MacOS X.\n");
977       /*NOTREACHED*/
978    }
979 #  endif
980 
981    /* If XML output is requested, check that the tool actually
982       supports it. */
983    if (VG_(clo_xml) && !VG_(needs).xml_output) {
984       VG_(clo_xml) = False;
985       VG_(fmsg_bad_option)("--xml=yes",
986          "%s does not support XML output.\n", VG_(details).name);
987       /*NOTREACHED*/
988    }
989 
990    vg_assert( VG_(clo_gen_suppressions) >= 0 );
991    vg_assert( VG_(clo_gen_suppressions) <= 2 );
992 
993    /* If we've been asked to emit XML, mash around various other
994       options so as to constrain the output somewhat, and to remove
995       any need for user input during the run.
996    */
997    if (VG_(clo_xml)) {
998 
999       /* We can't allow --gen-suppressions=yes, since that requires us
1000          to print the error and then ask the user if she wants a
1001          suppression for it, but in XML mode we won't print it until
1002          we know whether we also need to print a suppression.  Hence a
1003          circular dependency.  So disallow this.
1004          (--gen-suppressions=all is still OK since we don't need any
1005          user interaction in this case.) */
1006       if (VG_(clo_gen_suppressions) == 1) {
1007          VG_(fmsg_bad_option)(
1008             "--xml=yes together with --gen-suppressions=yes",
1009             "When --xml=yes is specified, --gen-suppressions=no\n"
1010             "or --gen-suppressions=all is allowed, but not "
1011             "--gen-suppressions=yes.\n");
1012       }
1013 
1014       /* Disallow dump_error in XML mode; sounds like a recipe for
1015          chaos.  No big deal; dump_error is a flag for debugging V
1016          itself. */
1017       if (VG_(clo_dump_error) > 0) {
1018          VG_(fmsg_bad_option)("--xml=yes",
1019             "Cannot be used together with --dump-error");
1020       }
1021 
1022       /* Disable error limits (this might be a bad idea!) */
1023       VG_(clo_error_limit) = False;
1024       /* Disable emulation warnings */
1025 
1026       /* Also, we want to set options for the leak checker, but that
1027          will have to be done in Memcheck's flag-handling code, not
1028          here. */
1029    }
1030 
1031    /* All non-logging-related options have been checked.  If the logging
1032       option specified is ok, we can switch to it, as we know we won't
1033       have to generate any other command-line-related error messages.
1034       (So far we should be still attached to stderr, so we can show on
1035       the terminal any problems to do with processing command line
1036       opts.) */
1037    VG_(init_log_xml_sinks)(log_to, xml_to, tmp_log_fd, tmp_xml_fd);
1038 
1039    /* Register child at-fork handler which will take care of handling
1040       --child-silent-after-fork clo and also reopening output sinks for forked
1041       children, if requested via --log|xml-file= options. */
1042    VG_(atfork)(NULL, NULL, VG_(logging_atfork_child));
1043 
1044    // Suppressions related stuff
1045 
1046    if (VG_(clo_default_supp) &&
1047        (VG_(needs).core_errors || VG_(needs).tool_errors)) {
1048       /* If we haven't reached the max number of suppressions, load
1049          the default one. */
1050       static const HChar default_supp[] = "default.supp";
1051       Int len = VG_(strlen)(VG_(libdir)) + 1 + sizeof(default_supp);
1052       HChar *buf = VG_(malloc)("main.mpclo.3", len);
1053       VG_(sprintf)(buf, "%s/%s", VG_(libdir), default_supp);
1054       VG_(addToXA)(VG_(clo_suppressions), &buf);
1055    }
1056 }
1057 
1058 
1059 /*====================================================================*/
1060 /*=== File descriptor setup                                        ===*/
1061 /*====================================================================*/
1062 
1063 /* Number of file descriptors that Valgrind tries to reserve for
1064    its own use - just a small constant. */
1065 #if defined(VGO_dragonfly)
1066 #define N_RESERVED_FDS (20)
1067 #else
1068 #define N_RESERVED_FDS (12)
1069 #endif
1070 
setup_file_descriptors(void)1071 static void setup_file_descriptors(void)
1072 {
1073    struct vki_rlimit rl;
1074    Bool show = False;
1075 
1076    /* Get the current file descriptor limits. */
1077    if (VG_(getrlimit)(VKI_RLIMIT_NOFILE, &rl) < 0) {
1078       rl.rlim_cur = 1024;
1079       rl.rlim_max = 1024;
1080    }
1081 
1082 #  if defined(VGO_darwin)
1083    /* Darwin lies. It reports file max as RLIM_INFINITY but
1084       silently disallows anything bigger than 10240. */
1085    if (rl.rlim_cur >= 10240  &&  rl.rlim_max == 0x7fffffffffffffffULL) {
1086       rl.rlim_max = 10240;
1087    }
1088 #  endif
1089 
1090    if (show)
1091       VG_(printf)("fd limits: host, before: cur %llu max %llu\n",
1092                   (ULong)rl.rlim_cur, (ULong)rl.rlim_max);
1093 
1094    /* Work out where to move the soft limit to. */
1095    if (rl.rlim_cur + N_RESERVED_FDS <= rl.rlim_max) {
1096       rl.rlim_cur = rl.rlim_cur + N_RESERVED_FDS;
1097    } else {
1098       rl.rlim_cur = rl.rlim_max;
1099    }
1100 
1101    /* Reserve some file descriptors for our use. */
1102    VG_(fd_soft_limit) = rl.rlim_cur - N_RESERVED_FDS;
1103    VG_(fd_hard_limit) = rl.rlim_cur - N_RESERVED_FDS;
1104 
1105    /* Update the soft limit. */
1106    VG_(setrlimit)(VKI_RLIMIT_NOFILE, &rl);
1107 
1108    if (show) {
1109       VG_(printf)("fd limits: host,  after: cur %lu max %lu\n",
1110                   (UWord)rl.rlim_cur, (UWord)rl.rlim_max);
1111       VG_(printf)("fd limits: guest       : cur %d max %d\n",
1112                   VG_(fd_soft_limit), VG_(fd_hard_limit));
1113    }
1114 
1115    if (VG_(cl_exec_fd) != -1)
1116       VG_(cl_exec_fd) = VG_(safe_fd)( VG_(cl_exec_fd) );
1117 }
1118 
1119 
1120 /*====================================================================*/
1121 /*=== main()                                                       ===*/
1122 /*====================================================================*/
1123 
1124 /* When main() is entered, we should be on the following stack, not
1125    the one the kernel gave us.  We will run on this stack until
1126    simulation of the root thread is started, at which point a transfer
1127    is made to a dynamically allocated stack.  This is for the sake of
1128    uniform overflow detection for all Valgrind threads.  This is
1129    marked global even though it isn't, because assembly code below
1130    needs to reference the name. */
1131 
1132 /*static*/ struct {
1133    HChar bytes [VG_STACK_GUARD_SZB + VG_DEFAULT_STACK_ACTIVE_SZB + VG_STACK_GUARD_SZB];
1134 } VG_(interim_stack);
1135 
1136 /* These are the structures used to hold info for creating the initial
1137    client image.
1138 
1139    'iicii' mostly holds important register state present at system
1140    startup (_start_valgrind).  valgrind_main() then fills in the rest
1141    of it and passes it to VG_(ii_create_image)().  That produces
1142    'iifii', which is later handed to VG_(ii_finalise_image). */
1143 
1144 /* In all OS-instantiations, the_iicii has a field .sp_at_startup.
1145    This should get some address inside the stack on which we gained
1146    control (eg, it could be the SP at startup).  It doesn't matter
1147    exactly where in the stack it is.  This value is passed to the
1148    address space manager at startup.  On Linux, aspacem then uses it
1149    to identify the initial stack segment and hence the upper end of
1150    the usable address space. */
1151 
1152 static IICreateImageInfo   the_iicii;
1153 static IIFinaliseImageInfo the_iifii;
1154 
1155 
1156 /* A simple pair structure, used for conveying debuginfo handles to
1157    calls to VG_TRACK(new_mem_startup, ...). */
1158 typedef  struct { Addr a; ULong ull; }  Addr_n_ULong;
1159 
1160 
1161 /* --- Forwards decls to do with shutdown --- */
1162 
1163 static void final_tidyup(ThreadId tid);
1164 
1165 /* Do everything which needs doing when the last thread exits */
1166 static
1167 void shutdown_actions_NORETURN( ThreadId tid,
1168                                 VgSchedReturnCode tids_schedretcode );
1169 
1170 /* --- end of Forwards decls to do with shutdown --- */
1171 
1172 
1173 /* By the time we get to valgrind_main, the_iicii should already have
1174    been filled in with any important details as required by whatever
1175    OS we have been built for.
1176 */
1177 static
valgrind_main(Int argc,HChar ** argv,HChar ** envp)1178 Int valgrind_main ( Int argc, HChar **argv, HChar **envp )
1179 {
1180    Int     need_help          = 0; // 0 = no, 1 = --help, 2 = --help-debug
1181    ThreadId tid_main          = VG_INVALID_THREADID;
1182    Int     loglevel, i;
1183    XArray* addr2dihandle = NULL;
1184 
1185    //============================================================
1186    //
1187    // Nb: startup is complex.  Prerequisites are shown at every step.
1188    // *** Be very careful when messing with the order ***
1189    //
1190    // The first order of business is to get debug logging, the address
1191    // space manager and the dynamic memory manager up and running.
1192    // Once that's done, we can relax a bit.
1193    //
1194    //============================================================
1195 
1196    /* This is needed to make VG_(getenv) usable early. */
1197    VG_(client_envp) = (HChar**)envp;
1198 
1199    //--------------------------------------------------------------
1200    // Start up Mach kernel interface, if any
1201    //   p: none
1202    //--------------------------------------------------------------
1203 #  if defined(VGO_darwin)
1204    VG_(mach_init)();
1205 #  endif
1206 
1207    //--------------------------------------------------------------
1208    // Start up the logging mechanism
1209    //   p: none
1210    //--------------------------------------------------------------
1211    /* Start the debugging-log system ASAP.  First find out how many
1212       "-d"s were specified.  This is a pre-scan of the command line.  Also
1213       get --profile-heap=yes, --core-redzone-size, --redzone-size
1214       --aspace-minaddr which are needed by the time we start up dynamic
1215       memory management.  */
1216    loglevel = 0;
1217    for (i = 1; i < argc; i++) {
1218       const HChar* tmp_str;
1219       if (argv[i][0] != '-') break;
1220       if VG_STREQ(argv[i], "--") break;
1221       if VG_STREQ(argv[i], "-d") loglevel++;
1222       if VG_BOOL_CLO(argv[i], "--profile-heap", VG_(clo_profile_heap)) {}
1223       if VG_BINT_CLO(argv[i], "--core-redzone-size", VG_(clo_core_redzone_size),
1224                      0, MAX_CLO_REDZONE_SZB) {}
1225       if VG_BINT_CLO(argv[i], "--redzone-size", VG_(clo_redzone_size),
1226                      0, MAX_CLO_REDZONE_SZB) {}
1227       if VG_STR_CLO(argv[i], "--aspace-minaddr", tmp_str) {
1228          Bool ok = VG_(parse_Addr) (&tmp_str, &VG_(clo_aspacem_minAddr));
1229          if (!ok)
1230             VG_(fmsg_bad_option)(argv[i], "Invalid address\n");
1231          const HChar *errmsg;
1232          if (!VG_(am_is_valid_for_aspacem_minAddr)(VG_(clo_aspacem_minAddr),
1233                                                    &errmsg))
1234             VG_(fmsg_bad_option)(argv[i], "%s\n", errmsg);
1235       }
1236    }
1237 
1238    /* ... and start the debug logger.  Now we can safely emit logging
1239       messages all through startup. */
1240    VG_(debugLog_startup)(loglevel, "Stage 2 (main)");
1241    VG_(debugLog)(1, "main", "Welcome to Valgrind version "
1242                             VERSION " debug logging\n");
1243 
1244    //--------------------------------------------------------------
1245    // Ensure we're on a plausible stack.
1246    //   p: logging
1247    //--------------------------------------------------------------
1248    VG_(debugLog)(1, "main", "Checking current stack is plausible\n");
1249    { HChar* limLo  = (HChar*)(&VG_(interim_stack).bytes[0]);
1250      HChar* limHi  = limLo + sizeof(VG_(interim_stack));
1251      HChar* volatile
1252             aLocal = (HChar*)&limLo; /* any auto local will do */
1253      /* Re "volatile": Apple clang version 4.0
1254         (tags/Apple/clang-421.0.57) (based on LLVM 3.1svn)" appeared
1255         to miscompile the following check, causing run to abort at
1256         this point (in 64-bit mode) even though aLocal is within limLo
1257         .. limHi.  But in fact clang is within its rights to do
1258         strange things here.  "The reason is that the comparisons
1259         aLocal < limLo and aLocal >= limHi cause undefined behaviour
1260         (according to c99 6.5.8) because they compare pointers that do
1261         not point into the same aggregate."  Adding "volatile" appears
1262         to fix it because "The compiler would have to prove that there
1263         is undefined behavior in order to exploit it.  But as a
1264         volatile variable can change its value in ways invisible to
1265         the compiler, the compiler must make the conservative
1266         assumption that it points into the same aggregate as the other
1267         pointer its compared against.  I.e. the behaviour is possibly
1268         defined." (Analysis by Florian Krohm). */
1269      if (aLocal < limLo || aLocal >= limHi) {
1270         /* something's wrong.  Stop. */
1271         VG_(debugLog)(0, "main", "Root stack %p to %p, a local %p\n",
1272                           limLo, limHi, aLocal );
1273         VG_(debugLog)(0, "main", "Valgrind: FATAL: "
1274                                  "Initial stack switched failed.\n");
1275         VG_(debugLog)(0, "main", "   Cannot continue.  Sorry.\n");
1276         VG_(exit)(1);
1277      }
1278    }
1279 
1280    //--------------------------------------------------------------
1281    // Ensure we have a plausible pointer to the stack on which
1282    // we gained control (not the current stack!)
1283    //   p: logging
1284    //--------------------------------------------------------------
1285    VG_(debugLog)(1, "main", "Checking initial stack was noted\n");
1286    if (the_iicii.sp_at_startup == 0) {
1287       VG_(debugLog)(0, "main", "Valgrind: FATAL: "
1288                                "Initial stack was not noted.\n");
1289       VG_(debugLog)(0, "main", "   Cannot continue.  Sorry.\n");
1290       VG_(exit)(1);
1291    }
1292 
1293    //--------------------------------------------------------------
1294    // Start up the address space manager, and determine the
1295    // approximate location of the client's stack
1296    //   p: logging, plausible-stack
1297    //--------------------------------------------------------------
1298    VG_(debugLog)(1, "main", "Starting the address space manager\n");
1299    vg_assert(VKI_PAGE_SIZE    == 4096  || VKI_PAGE_SIZE == 8192
1300              || VKI_PAGE_SIZE == 16384 || VKI_PAGE_SIZE == 32768
1301              || VKI_PAGE_SIZE == 65536);
1302    vg_assert(VKI_MAX_PAGE_SIZE    == 4096  || VKI_MAX_PAGE_SIZE == 8192
1303              || VKI_MAX_PAGE_SIZE == 16384 || VKI_MAX_PAGE_SIZE == 32768
1304              || VKI_MAX_PAGE_SIZE == 65536);
1305    vg_assert(VKI_PAGE_SIZE <= VKI_MAX_PAGE_SIZE);
1306    vg_assert(VKI_PAGE_SIZE     == (1 << VKI_PAGE_SHIFT));
1307    vg_assert(VKI_MAX_PAGE_SIZE == (1 << VKI_MAX_PAGE_SHIFT));
1308    the_iicii.clstack_end = VG_(am_startup)( the_iicii.sp_at_startup );
1309    VG_(debugLog)(1, "main", "Address space manager is running\n");
1310 
1311    //--------------------------------------------------------------
1312    // Start up the dynamic memory manager
1313    //   p: address space management
1314    //   p: getting --profile-heap,--core-redzone-size,--redzone-size
1315    //   In fact m_mallocfree is self-initialising, so there's no
1316    //   initialisation call to do.  Instead, try a simple malloc/
1317    //   free pair right now to check that nothing is broken.
1318    //--------------------------------------------------------------
1319    VG_(debugLog)(1, "main", "Starting the dynamic memory manager\n");
1320    { void* p = VG_(malloc)( "main.vm.1", 12345 );
1321      VG_(free)( p );
1322    }
1323    VG_(debugLog)(1, "main", "Dynamic memory manager is running\n");
1324 
1325    //============================================================
1326    //
1327    // Dynamic memory management is now available.
1328    //
1329    //============================================================
1330 
1331    //--------------------------------------------------------------
1332    // Initialise m_debuginfo
1333    //  p: dynamic memory allocation
1334    VG_(debugLog)(1, "main", "Initialise m_debuginfo\n");
1335    VG_(di_initialise)();
1336 
1337    //--------------------------------------------------------------
1338    // Look for alternative libdir
1339    { HChar *cp = VG_(getenv)(VALGRIND_LIB);
1340      if (cp != NULL)
1341         VG_(libdir) = cp;
1342      VG_(debugLog)(1, "main", "VG_(libdir) = %s\n", VG_(libdir));
1343    }
1344 
1345    //--------------------------------------------------------------
1346    // Extract the launcher name from the environment.
1347    VG_(debugLog)(1, "main", "Getting launcher's name ...\n");
1348    VG_(name_of_launcher) = VG_(getenv)(VALGRIND_LAUNCHER);
1349    if (VG_(name_of_launcher) == NULL) {
1350       VG_(printf)("valgrind: You cannot run '%s' directly.\n", argv[0]);
1351       VG_(printf)("valgrind: You should use $prefix/bin/valgrind.\n");
1352       VG_(exit)(1);
1353    }
1354    VG_(debugLog)(1, "main", "... %s\n", VG_(name_of_launcher));
1355 
1356    //--------------------------------------------------------------
1357    // We used to set the process datasize rlimit to zero to prevent
1358    // any internal use of brk() from having any effect. But later
1359    // linux kernels redefine RLIMIT_DATA as the size of any data
1360    // areas, including some dynamic mmap memory allocations.
1361    // See bug #357833 for the commit that went into linux 4.5
1362    // changing the definition of RLIMIT_DATA. So don't mess with
1363    // RLIMIT_DATA here now anymore. Just remember it for use in
1364    // the syscall wrappers.
1365    VG_(getrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data));
1366 
1367    // Get the current process stack rlimit.
1368    VG_(getrlimit)(VKI_RLIMIT_STACK, &VG_(client_rlimit_stack));
1369 
1370    //--------------------------------------------------------------
1371    // Figure out what sort of CPU we're on, and whether it is
1372    // able to run V.
1373    /* The vex_archinfo structure is passed down later to the client
1374     * to verify the HW info settings are consistent.
1375     */
1376    VexArchInfo vex_archinfo;
1377    VG_(debugLog)(1, "main", "Get hardware capabilities ...\n");
1378    { VexArch     vex_arch;
1379      Bool ok = VG_(machine_get_hwcaps)();
1380      if (!ok) {
1381         VG_(printf)("\n");
1382         VG_(printf)("valgrind: fatal error: unsupported CPU.\n");
1383         VG_(printf)("   Supported CPUs are:\n");
1384         VG_(printf)("   * x86 (practically any; Pentium-I or above), "
1385                     "AMD Athlon or above)\n");
1386         VG_(printf)("   * AMD Athlon64/Opteron\n");
1387         VG_(printf)("   * ARM (armv7)\n");
1388         VG_(printf)("   * MIPS (mips32 and above; mips64 and above)\n");
1389         VG_(printf)("   * PowerPC (most; ppc405 and above)\n");
1390         VG_(printf)("   * System z (64bit only - s390x; z990 and above)\n");
1391         VG_(printf)("\n");
1392         VG_(exit)(1);
1393      }
1394      VG_(machine_get_VexArchInfo)( &vex_arch, &vex_archinfo );
1395      VG_(debugLog)(
1396         1, "main", "... arch = %s, hwcaps = %s\n",
1397            LibVEX_ppVexArch   ( vex_arch ),
1398            LibVEX_ppVexHwCaps ( vex_arch, vex_archinfo.hwcaps )
1399      );
1400    }
1401 
1402    //--------------------------------------------------------------
1403    // Record the working directory at startup
1404    //   p: none
1405    VG_(debugLog)(1, "main", "Getting the working directory at startup\n");
1406    VG_(record_startup_wd)();
1407    const HChar *wd = VG_(get_startup_wd)();
1408    VG_(debugLog)(1, "main", "... %s\n", wd != NULL ? wd : "<NO CWD>" );
1409 
1410    //============================================================
1411    // Command line argument handling order:
1412    // * If --help/--help-debug are present, show usage message
1413    //   (including the tool-specific usage)
1414    // * (If no --tool option given, default to Memcheck)
1415    // * Then, if client is missing, abort with error msg
1416    // * Then, if any cmdline args are bad, abort with error msg
1417    //============================================================
1418 
1419    //--------------------------------------------------------------
1420    // Split up argv into: C args, V args, V extra args, and exename.
1421    //   p: dynamic memory allocation
1422    //--------------------------------------------------------------
1423    VG_(debugLog)(1, "main", "Split up command line\n");
1424    VG_(split_up_argv)( argc, argv );
1425    vg_assert( VG_(args_for_valgrind) );
1426    vg_assert( VG_(args_for_client) );
1427    if (0) {
1428       for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++)
1429          VG_(printf)(
1430             "varg %s\n",
1431             * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i )
1432          );
1433       VG_(printf)(" exe %s\n", VG_(args_the_exename));
1434       for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++)
1435          VG_(printf)(
1436             "carg %s\n",
1437             * (HChar**) VG_(indexXA)( VG_(args_for_client), i )
1438          );
1439    }
1440 
1441    //--------------------------------------------------------------
1442    // Extract tool name and whether help has been requested.
1443    // Note we can't print the help message yet, even if requested,
1444    // because the tool has not been initialised.
1445    //   p: split_up_argv [for VG_(args_for_valgrind)]
1446    //--------------------------------------------------------------
1447    VG_(debugLog)(1, "main",
1448                     "(early_) Process Valgrind's command line options\n");
1449    early_process_cmd_line_options(&need_help);
1450 
1451    // BEGIN HACK
1452    // When changing the logic for the VG_(clo_read_inline_info) default,
1453    // the manual and --help output have to be changed accordingly.
1454    vg_assert(VG_(clo_toolname) != NULL);
1455    vg_assert(VG_(clo_read_inline_info) == False);
1456 #  if !defined(VGO_darwin)
1457    if (0 == VG_(strcmp)(VG_(clo_toolname), "memcheck")
1458        || 0 == VG_(strcmp)(VG_(clo_toolname), "helgrind")
1459        || 0 == VG_(strcmp)(VG_(clo_toolname), "drd")
1460        || 0 == VG_(strcmp)(VG_(clo_toolname), "massif")
1461        || 0 == VG_(strcmp)(VG_(clo_toolname), "exp-dhat")) {
1462       /* Change the default setting.  Later on (just below)
1463          main_process_cmd_line_options should pick up any
1464          user-supplied setting for it and will override the default
1465          set here. */
1466       VG_(clo_read_inline_info) = True;
1467    }
1468 #  endif
1469    // END HACK
1470 
1471    // Set default vex control params.
1472    LibVEX_default_VexControl(& VG_(clo_vex_control));
1473 
1474    //--------------------------------------------------------------
1475    // Load client executable, finding in $PATH if necessary
1476    //   p: early_process_cmd_line_options()  [for 'exec', 'need_help',
1477    //                                         clo_max_stackframe,
1478    //                                         clo_main_stacksize]
1479    //   p: layout_remaining_space            [so there's space]
1480    //
1481    // Set up client's environment
1482    //   p: set-libdir                     [for VG_(libdir)]
1483    //   p: early_process_cmd_line_options [for VG_(clo_toolname)]
1484    //
1485    // Setup client stack, eip, and VG_(client_arg[cv])
1486    //   p: load_client()     [for 'info']
1487    //   p: fix_environment() [for 'env']
1488    //
1489    // Setup client data (brk) segment.  Initially a 1-page segment
1490    // which abuts a shrinkable reservation.
1491    //     p: load_client()     [for 'info' and hence VG_(brk_base)]
1492    //
1493    // p: _start_in_C (for zeroing out the_iicii and putting some
1494    //    initial values into it)
1495    //--------------------------------------------------------------
1496    if (!need_help) {
1497       VG_(debugLog)(1, "main", "Create initial image\n");
1498 
1499 #     if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris) || defined(VGO_dragonfly)
1500       the_iicii.argv              = argv;
1501       the_iicii.envp              = envp;
1502       the_iicii.toolname          = VG_(clo_toolname);
1503 #     else
1504 #       error "Unknown platform"
1505 #     endif
1506 
1507       /* NOTE: this call reads VG_(clo_main_stacksize). */
1508       the_iifii = VG_(ii_create_image)( the_iicii, &vex_archinfo );
1509    }
1510 
1511    //==============================================================
1512    //
1513    // Finished loading/setting up the client address space.
1514    //
1515    //==============================================================
1516 
1517    //--------------------------------------------------------------
1518    // setup file descriptors
1519    //   p: n/a
1520    //--------------------------------------------------------------
1521    VG_(debugLog)(1, "main", "Setup file descriptors\n");
1522    setup_file_descriptors();
1523 
1524    //--------------------------------------------------------------
1525    // create fake /proc/<pid>/cmdline and /proc/<pid>/auxv files
1526    // and then unlink them, but hold onto the fds, so we can handr
1527    // them out to the client when it tries to open
1528    // /proc/<pid>/cmdline or /proc/<pid>/auxv for itself.
1529    //   p: setup file descriptors
1530    //   p: ii_create_image for VG_(client_auxv) setup.
1531    //--------------------------------------------------------------
1532    VG_(cl_cmdline_fd) = -1;
1533    VG_(cl_auxv_fd) = -1;
1534 #if defined(VGO_solaris)
1535    VG_(cl_psinfo_fd) = -1;
1536 #endif
1537 
1538 #if defined(VGO_linux) || defined(VGO_solaris)
1539    if (!need_help) {
1540       HChar  buf[50];   // large enough
1541       HChar  buf2[VG_(mkstemp_fullname_bufsz)(sizeof buf - 1)];
1542       Int    fd, r;
1543 
1544 #if defined(VGO_linux) || defined(SOLARIS_PROC_CMDLINE)
1545       /* Fake /proc/<pid>/cmdline only on Linux and Solaris if supported. */
1546       HChar  nul[1];
1547       const HChar* exename;
1548 
1549       VG_(debugLog)(1, "main", "Create fake /proc/<pid>/cmdline\n");
1550 
1551       VG_(sprintf)(buf, "proc_%d_cmdline", VG_(getpid)());
1552       fd = VG_(mkstemp)( buf, buf2 );
1553       if (fd == -1)
1554          VG_(err_config_error)("Can't create client cmdline file in %s\n", buf2);
1555 
1556       nul[0] = 0;
1557       exename = VG_(args_the_exename);
1558       VG_(write)(fd, exename, VG_(strlen)( exename ));
1559       VG_(write)(fd, nul, 1);
1560 
1561       for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
1562          HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i );
1563          VG_(write)(fd, arg, VG_(strlen)( arg ));
1564          VG_(write)(fd, nul, 1);
1565       }
1566 
1567       /* Don't bother to seek the file back to the start; instead do
1568 	 it every time a copy of it is given out (by PRE(sys_open) or
1569 	 PRE(sys_openat)). That is probably more robust across fork() etc. */
1570 
1571       /* Now delete it, but hang on to the fd. */
1572       r = VG_(unlink)( buf2 );
1573       if (r)
1574          VG_(err_config_error)("Can't delete client cmdline file in %s\n", buf2);
1575 
1576       VG_(cl_cmdline_fd) = fd;
1577 #endif // defined(VGO_linux) || defined(SOLARIS_PROC_CMDLINE)
1578 
1579       /* Fake /proc/<pid>/auxv on both Linux and Solaris. */
1580       VG_(debugLog)(1, "main", "Create fake /proc/<pid>/auxv\n");
1581 
1582       VG_(sprintf)(buf, "proc_%d_auxv", VG_(getpid)());
1583       fd = VG_(mkstemp)( buf, buf2 );
1584       if (fd == -1)
1585          VG_(err_config_error)("Can't create client auxv file in %s\n", buf2);
1586 
1587       UWord *client_auxv = VG_(client_auxv);
1588       unsigned int client_auxv_len = 0;
1589       while (*client_auxv != 0) {
1590          client_auxv++;
1591          client_auxv++;
1592          client_auxv_len += 2 * sizeof(UWord);
1593       }
1594       client_auxv_len += 2 * sizeof(UWord);
1595 
1596       VG_(write)(fd, VG_(client_auxv), client_auxv_len);
1597 
1598       /* Don't bother to seek the file back to the start; instead do
1599 	 it every time a copy of it is given out (by PRE(sys_open)).
1600 	 That is probably more robust across fork() etc. */
1601 
1602       /* Now delete it, but hang on to the fd. */
1603       r = VG_(unlink)( buf2 );
1604       if (r)
1605          VG_(err_config_error)("Can't delete client auxv file in %s\n", buf2);
1606 
1607       VG_(cl_auxv_fd) = fd;
1608 
1609 #if defined(VGO_solaris)
1610       /* Fake /proc/<pid>/psinfo on Solaris.
1611        * Contents will be fetched and partially faked later on the fly. */
1612       VG_(debugLog)(1, "main", "Create fake /proc/<pid>/psinfo\n");
1613 
1614       VG_(sprintf)(buf, "proc_%d_psinfo", VG_(getpid)());
1615       fd = VG_(mkstemp)( buf, buf2 );
1616       if (fd == -1)
1617          VG_(err_config_error)("Can't create client psinfo file in %s\n", buf2);
1618 
1619       /* Now delete it, but hang on to the fd. */
1620       r = VG_(unlink)( buf2 );
1621       if (r)
1622          VG_(err_config_error)("Can't delete client psinfo file in %s\n", buf2);
1623 
1624       VG_(cl_psinfo_fd) = fd;
1625 #endif /* VGO_solaris */
1626    }
1627 #endif
1628 
1629    //--------------------------------------------------------------
1630    // Init tool part 1: pre_clo_init
1631    //   p: setup_client_stack()      [for 'VG_(client_arg[cv]']
1632    //   p: setup_file_descriptors()  [for 'VG_(fd_xxx_limit)']
1633    //--------------------------------------------------------------
1634    VG_(debugLog)(1, "main", "Initialise the tool part 1 (pre_clo_init)\n");
1635    VG_(tl_pre_clo_init)();
1636    // Activate var info readers, if the tool asked for it:
1637    if (VG_(needs).var_info)
1638       VG_(clo_read_var_info) = True;
1639 
1640    //--------------------------------------------------------------
1641    // If --tool and --help/--help-debug was given, now give the core+tool
1642    // help message
1643    //   p: early_process_cmd_line_options() [for 'need_help']
1644    //   p: tl_pre_clo_init                  [for 'VG_(tdict).usage']
1645    //--------------------------------------------------------------
1646    VG_(debugLog)(1, "main", "Print help and quit, if requested\n");
1647    if (need_help) {
1648       usage_NORETURN(/*--help-debug?*/need_help >= 2);
1649    }
1650 
1651    //--------------------------------------------------------------
1652    // Process command line options to Valgrind + tool
1653    //   p: setup_client_stack()      [for 'VG_(client_arg[cv]']
1654    //   p: setup_file_descriptors()  [for 'VG_(fd_xxx_limit)']
1655    //--------------------------------------------------------------
1656    VG_(debugLog)(1, "main",
1657                     "(main_) Process Valgrind's command line options, "
1658                     "setup logging\n");
1659    main_process_cmd_line_options();
1660 
1661    //--------------------------------------------------------------
1662    // Zeroise the millisecond counter by doing a first read of it.
1663    //   p: none
1664    //--------------------------------------------------------------
1665    (void) VG_(read_millisecond_timer)();
1666 
1667    //--------------------------------------------------------------
1668    // Print the preamble
1669    //   p: tl_pre_clo_init            [for 'VG_(details).name' and friends]
1670    //   p: main_process_cmd_line_options()
1671    //         [for VG_(clo_verbosity), VG_(clo_xml)]
1672    //--------------------------------------------------------------
1673    VG_(debugLog)(1, "main", "Print the preamble...\n");
1674    VG_(print_preamble)(VG_(log_output_sink).type != VgLogTo_File);
1675    VG_(debugLog)(1, "main", "...finished the preamble\n");
1676 
1677    //--------------------------------------------------------------
1678    // Init tool part 2: post_clo_init
1679    //   p: setup_client_stack()      [for 'VG_(client_arg[cv]']
1680    //   p: setup_file_descriptors()  [for 'VG_(fd_xxx_limit)']
1681    //   p: print_preamble()          [so any warnings printed in post_clo_init
1682    //                                 are shown after the preamble]
1683    //--------------------------------------------------------------
1684    VG_(debugLog)(1, "main", "Initialise the tool part 2 (post_clo_init)\n");
1685    VG_TDICT_CALL(tool_post_clo_init);
1686    {
1687       /* The tool's "needs" will by now be finalised, since it has no
1688          further opportunity to specify them.  So now sanity check
1689          and finish initialising the needs. */
1690       const HChar* s;
1691       Bool  ok;
1692       ok = VG_(finish_needs_init)( &s );
1693       if (!ok) {
1694          VG_(core_panic)(s);
1695       }
1696    }
1697 
1698    //--------------------------------------------------------------
1699    // Initialise translation table and translation cache
1700    //   p: aspacem         [??]
1701    //   p: tl_pre_clo_init [for 'VG_(details).avg_translation_sizeB']
1702    //--------------------------------------------------------------
1703    VG_(debugLog)(1, "main", "Initialise TT/TC\n");
1704    VG_(init_tt_tc)();
1705 
1706    //--------------------------------------------------------------
1707    // Initialise the redirect table.
1708    //   p: init_tt_tc [so it can call VG_(search_transtab) safely]
1709    //   p: aspacem [so can change ownership of sysinfo pages]
1710    //--------------------------------------------------------------
1711    VG_(debugLog)(1, "main", "Initialise redirects\n");
1712    VG_(redir_initialise)();
1713 
1714    //--------------------------------------------------------------
1715    // Allow GDB attach
1716    //   p: main_process_cmd_line_options()  [for VG_(clo_wait_for_gdb)]
1717    //--------------------------------------------------------------
1718    /* Hook to delay things long enough so we can get the pid and
1719       attach GDB in another shell. */
1720    if (VG_(clo_wait_for_gdb)) {
1721       const int ms = 8000; // milliseconds
1722       VG_(debugLog)(1, "main", "Wait for GDB during %d ms\n", ms);
1723       VG_(printf)("pid=%d, entering delay %d ms loop\n", VG_(getpid)(), ms);
1724       VG_(poll)(NULL, 0, ms);
1725    }
1726 
1727    //--------------------------------------------------------------
1728    // Search for file descriptors that are inherited from our parent
1729    //   p: main_process_cmd_line_options  [for VG_(clo_track_fds)]
1730    //--------------------------------------------------------------
1731    if (VG_(clo_track_fds)) {
1732       VG_(debugLog)(1, "main", "Init preopened fds\n");
1733       VG_(init_preopened_fds)();
1734    }
1735 
1736 #if defined(VGO_solaris)
1737    VG_(syswrap_init)();
1738 #endif
1739 
1740    //--------------------------------------------------------------
1741    // Load debug info for the existing segments.
1742    //   p: setup_code_redirect_table [so that redirs can be recorded]
1743    //   p: mallocfree
1744    //   p: probably: setup fds and process CLOs, so that logging works
1745    //   p: initialise m_debuginfo
1746    //
1747    // While doing this, make a note of the debuginfo-handles that
1748    // come back from VG_(di_notify_mmap).
1749    // Later, in "Tell the tool about the initial client memory permissions"
1750    // (just below) we can then hand these handles off to the tool in
1751    // calls to VG_TRACK(new_mem_startup, ...).  This gives the tool the
1752    // opportunity to make further queries to m_debuginfo before the
1753    // client is started, if it wants.  We put this information into an
1754    // XArray, each handle along with the associated segment start address,
1755    // and search the XArray for the handles later, when calling
1756    // VG_TRACK(new_mem_startup, ...).
1757    //--------------------------------------------------------------
1758    VG_(debugLog)(1, "main", "Load initial debug info\n");
1759 
1760    vg_assert(!addr2dihandle);
1761    addr2dihandle = VG_(newXA)( VG_(malloc), "main.vm.2",
1762                                VG_(free), sizeof(Addr_n_ULong) );
1763 
1764 #  if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_dragonfly)
1765    { Addr* seg_starts;
1766      Int   n_seg_starts;
1767      Addr_n_ULong anu;
1768 
1769      seg_starts = VG_(get_segment_starts)( SkFileC | SkFileV, &n_seg_starts );
1770      vg_assert(seg_starts && n_seg_starts >= 0);
1771 
1772      /* show them all to the debug info reader.  allow_SkFileV has to
1773         be True here so that we read info from the valgrind executable
1774         itself. */
1775      for (i = 0; i < n_seg_starts; i++) {
1776         anu.ull = VG_(di_notify_mmap)( seg_starts[i], True/*allow_SkFileV*/,
1777                                        -1/*Don't use_fd*/);
1778         /* anu.ull holds the debuginfo handle returned by di_notify_mmap,
1779            if any. */
1780         if (anu.ull > 0) {
1781            anu.a = seg_starts[i];
1782            VG_(addToXA)( addr2dihandle, &anu );
1783         }
1784      }
1785 
1786      VG_(free)( seg_starts );
1787    }
1788 #  elif defined(VGO_darwin)
1789    { Addr* seg_starts;
1790      Int   n_seg_starts;
1791      seg_starts = VG_(get_segment_starts)( SkFileC, &n_seg_starts );
1792      vg_assert(seg_starts && n_seg_starts >= 0);
1793 
1794      /* show them all to the debug info reader.
1795         Don't read from V segments (unlike Linux) */
1796      // GrP fixme really?
1797      for (i = 0; i < n_seg_starts; i++) {
1798         VG_(di_notify_mmap)( seg_starts[i], False/*don't allow_SkFileV*/,
1799                              -1/*don't use_fd*/);
1800      }
1801 
1802      VG_(free)( seg_starts );
1803    }
1804 #  else
1805 #    error Unknown OS
1806 #  endif
1807 
1808    //--------------------------------------------------------------
1809    // Tell aspacem of ownership change of the asm helpers, so that
1810    // m_translate allows them to be translated.  However, only do this
1811    // after the initial debug info read, since making a hole in the
1812    // address range for the stage2 binary confuses the debug info reader.
1813    //   p: aspacem
1814    //--------------------------------------------------------------
1815    { Bool change_ownership_v_c_OK;
1816      Addr co_start   = VG_PGROUNDDN( (Addr)&VG_(trampoline_stuff_start) );
1817      Addr co_endPlus = VG_PGROUNDUP( (Addr)&VG_(trampoline_stuff_end) );
1818      VG_(debugLog)(1,"redir",
1819                      "transfer ownership V -> C of 0x%llx .. 0x%llx\n",
1820                      (ULong)co_start, (ULong)co_endPlus-1 );
1821 
1822      change_ownership_v_c_OK
1823         = VG_(am_change_ownership_v_to_c)( co_start, co_endPlus - co_start );
1824      vg_assert(change_ownership_v_c_OK);
1825    }
1826 
1827    if (VG_(clo_xml)) {
1828       HChar buf[50];    // large enough
1829       VG_(elapsed_wallclock_time)(buf, sizeof buf);
1830       VG_(printf_xml)( "<status>\n"
1831                        "  <state>RUNNING</state>\n"
1832                        "  <time>%pS</time>\n"
1833                        "</status>\n",
1834                        buf );
1835       VG_(printf_xml)( "\n" );
1836    }
1837 
1838    VG_(init_Threads)();
1839 
1840    //--------------------------------------------------------------
1841    // Initialise the scheduler (phase 1) [generates tid_main]
1842    //   p: none, afaics
1843    //--------------------------------------------------------------
1844    VG_(debugLog)(1, "main", "Initialise scheduler (phase 1)\n");
1845    tid_main = VG_(scheduler_init_phase1)();
1846    vg_assert(tid_main >= 0 && tid_main < VG_N_THREADS
1847              && tid_main != VG_INVALID_THREADID);
1848    /* Tell the tool about tid_main */
1849    VG_TRACK( pre_thread_ll_create, VG_INVALID_THREADID, tid_main );
1850 
1851    //--------------------------------------------------------------
1852    // Tell the tool about the initial client memory permissions
1853    //   p: aspacem
1854    //   p: mallocfree
1855    //   p: setup_client_stack
1856    //   p: setup_client_dataseg
1857    //
1858    // For each segment we tell the client about, look up in
1859    // addr2dihandle as created above, to see if there's a debuginfo
1860    // handle associated with the segment, that we can hand along
1861    // to the tool, to be helpful.
1862    //--------------------------------------------------------------
1863    VG_(debugLog)(1, "main", "Tell tool about initial permissions\n");
1864    { Addr*     seg_starts;
1865      Int       n_seg_starts;
1866 
1867      vg_assert(addr2dihandle);
1868 
1869      /* Mark the main thread as running while we tell the tool about
1870         the client memory so that the tool can associate that memory
1871         with the main thread. */
1872      vg_assert(VG_(running_tid) == VG_INVALID_THREADID);
1873      VG_(running_tid) = tid_main;
1874 
1875      seg_starts = VG_(get_segment_starts)( SkFileC | SkAnonC | SkShmC,
1876                                            &n_seg_starts );
1877      vg_assert(seg_starts && n_seg_starts >= 0);
1878 
1879      /* Show client segments to the tool */
1880      for (i = 0; i < n_seg_starts; i++) {
1881         Word j, n;
1882         NSegment const* seg
1883            = VG_(am_find_nsegment)( seg_starts[i] );
1884         vg_assert(seg);
1885         vg_assert(seg->kind == SkFileC || seg->kind == SkAnonC ||
1886                   seg->kind == SkShmC);
1887         vg_assert(seg->start == seg_starts[i]);
1888         {
1889            VG_(debugLog)(2, "main",
1890                             "tell tool about %010lx-%010lx %c%c%c\n",
1891                              seg->start, seg->end,
1892                              seg->hasR ? 'r' : '-',
1893                              seg->hasW ? 'w' : '-',
1894                              seg->hasX ? 'x' : '-' );
1895            /* search addr2dihandle to see if we have an entry
1896               matching seg->start. */
1897            n = VG_(sizeXA)( addr2dihandle );
1898            for (j = 0; j < n; j++) {
1899               Addr_n_ULong* anl = VG_(indexXA)( addr2dihandle, j );
1900               if (anl->a == seg->start) {
1901                   vg_assert(anl->ull > 0); /* check it's a valid handle */
1902                   break;
1903               }
1904            }
1905            vg_assert(j >= 0 && j <= n);
1906            VG_TRACK( new_mem_startup, seg->start, seg->end+1-seg->start,
1907                      seg->hasR, seg->hasW, seg->hasX,
1908                      /* and the retrieved debuginfo handle, if any */
1909                      j < n
1910                      ? ((Addr_n_ULong*)VG_(indexXA)( addr2dihandle, j ))->ull
1911                         : 0 );
1912         }
1913      }
1914 
1915      VG_(free)( seg_starts );
1916      VG_(deleteXA)( addr2dihandle );
1917 
1918      /* Also do the initial stack permissions. */
1919      {
1920        SSizeT inaccessible_len;
1921        NSegment const* seg
1922           = VG_(am_find_nsegment)( the_iifii.initial_client_SP );
1923        vg_assert(seg);
1924        vg_assert(seg->kind == SkAnonC);
1925        vg_assert(the_iifii.initial_client_SP >= seg->start);
1926        vg_assert(the_iifii.initial_client_SP <= seg->end);
1927 
1928        /* Stuff below the initial SP is unaddressable.  Take into
1929 	  account any ABI-mandated space below the stack pointer that
1930 	  is required (VG_STACK_REDZONE_SZB).  setup_client_stack()
1931 	  will have allocated an extra page if a red zone is required,
1932 	  to be on the safe side. */
1933        inaccessible_len = the_iifii.initial_client_SP - VG_STACK_REDZONE_SZB
1934                           - seg->start;
1935        vg_assert(inaccessible_len >= 0);
1936        if (inaccessible_len > 0)
1937           VG_TRACK( die_mem_stack,
1938                     seg->start,
1939                     inaccessible_len );
1940        VG_(debugLog)(2, "main", "mark stack inaccessible %010lx-%010lx\n",
1941                         seg->start,
1942                         the_iifii.initial_client_SP-1 - VG_STACK_REDZONE_SZB);
1943      }
1944 
1945      /* Also the assembly helpers. */
1946      VG_TRACK( new_mem_startup,
1947                (Addr)&VG_(trampoline_stuff_start),
1948                (Addr)&VG_(trampoline_stuff_end)
1949                   - (Addr)&VG_(trampoline_stuff_start),
1950                False, /* readable? */
1951                False, /* writable? */
1952                True   /* executable? */,
1953                0 /* di_handle: no associated debug info */ );
1954 
1955      /* Clear the running thread indicator */
1956      VG_(running_tid) = VG_INVALID_THREADID;
1957      vg_assert(VG_(running_tid) == VG_INVALID_THREADID);
1958 
1959      /* Darwin only: tell the tools where the client's kernel commpage
1960         is.  It would be better to do this by telling aspacemgr about
1961         it -- see the now disused record_system_memory() in
1962         initimg-darwin.c -- but that causes the sync checker to fail,
1963         since the mapping doesn't appear in the kernel-supplied
1964         process map.  So do it here instead. */
1965 #    if defined(VGP_amd64_darwin)
1966      VG_TRACK( new_mem_startup,
1967                0x7fffffe00000, 0x7ffffffff000-0x7fffffe00000,
1968                True, False, True, /* r-x */
1969                0 /* di_handle: no associated debug info */ );
1970 #    elif defined(VGP_x86_darwin)
1971      VG_TRACK( new_mem_startup,
1972                0xfffec000, 0xfffff000-0xfffec000,
1973                True, False, True, /* r-x */
1974                0 /* di_handle: no associated debug info */ );
1975 #    endif
1976    }
1977 
1978    //--------------------------------------------------------------
1979    // Initialise the scheduler (phase 2)
1980    //   p: Initialise the scheduler (phase 1) [for tid_main]
1981    //   p: setup_file_descriptors() [else VG_(safe_fd)() breaks]
1982    //   p: setup_client_stack
1983    //--------------------------------------------------------------
1984    VG_(debugLog)(1, "main", "Initialise scheduler (phase 2)\n");
1985    { NSegment const* seg
1986         = VG_(am_find_nsegment)( the_iifii.initial_client_SP );
1987      vg_assert(seg);
1988      vg_assert(seg->kind == SkAnonC);
1989      vg_assert(the_iifii.initial_client_SP >= seg->start);
1990      vg_assert(the_iifii.initial_client_SP <= seg->end);
1991      VG_(scheduler_init_phase2)( tid_main,
1992                                  seg->end, the_iifii.clstack_max_size );
1993    }
1994 
1995    //--------------------------------------------------------------
1996    // Set up state for the root thread
1997    //   p: ?
1998    //      setup_scheduler()      [for sched-specific thread 1 stuff]
1999    //      VG_(ii_create_image)   [for 'the_iicii' initial info]
2000    //--------------------------------------------------------------
2001    VG_(debugLog)(1, "main", "Finalise initial image\n");
2002    { /* Mark the main thread as running while we tell the tool about
2003         the client memory which could be tracked during initial image
2004         finalisation. So the tool can associate that memory with the
2005         main thread. */
2006      vg_assert(VG_(running_tid) == VG_INVALID_THREADID);
2007      VG_(running_tid) = tid_main;
2008 
2009      VG_(ii_finalise_image)( the_iifii );
2010 
2011      /* Clear the running thread indicator */
2012      VG_(running_tid) = VG_INVALID_THREADID;
2013      vg_assert(VG_(running_tid) == VG_INVALID_THREADID);
2014    }
2015 
2016    //--------------------------------------------------------------
2017    // Initialise the signal handling subsystem
2018    //   p: n/a
2019    //--------------------------------------------------------------
2020    // Nb: temporarily parks the saved blocking-mask in saved_sigmask.
2021    VG_(debugLog)(1, "main", "Initialise signal management\n");
2022    /* Check that the kernel-interface signal definitions look sane */
2023    VG_(vki_do_initial_consistency_checks)();
2024    /* .. and go on to use them. */
2025    VG_(sigstartup_actions)();
2026 
2027    //--------------------------------------------------------------
2028    // Read suppression file
2029    //   p: main_process_cmd_line_options()  [for VG_(clo_suppressions)]
2030    //--------------------------------------------------------------
2031    if (VG_(needs).core_errors || VG_(needs).tool_errors) {
2032       VG_(debugLog)(1, "main", "Load suppressions\n");
2033       VG_(load_suppressions)();
2034    }
2035 
2036    //--------------------------------------------------------------
2037    // register client stack
2038    //--------------------------------------------------------------
2039    VG_(clstk_id) = VG_(register_stack)(VG_(clstk_start_base), VG_(clstk_end));
2040 
2041    //--------------------------------------------------------------
2042    // Show the address space state so far
2043    //--------------------------------------------------------------
2044    VG_(debugLog)(1, "main", "\n");
2045    VG_(debugLog)(1, "main", "\n");
2046    VG_(am_show_nsegments)(1,"Memory layout at client startup");
2047    VG_(debugLog)(1, "main", "\n");
2048    VG_(debugLog)(1, "main", "\n");
2049 
2050    //--------------------------------------------------------------
2051    // Run!
2052    //--------------------------------------------------------------
2053    VG_(debugLog)(1, "main", "Running thread 1\n");
2054 
2055    /* As a result of the following call, the last thread standing
2056       eventually winds up running shutdown_actions_NORETURN
2057       just below.  Unfortunately, simply exporting said function
2058       causes m_main to be part of a module cycle, which is pretty
2059       nonsensical.  So instead of doing that, the address of said
2060       function is stored in a global variable 'owned' by m_syswrap,
2061       and it uses that function pointer to get back here when it needs
2062       to. */
2063 
2064    /* Set continuation address. */
2065    VG_(address_of_m_main_shutdown_actions_NORETURN)
2066       = & shutdown_actions_NORETURN;
2067 
2068    /* Run the first thread, eventually ending up at the continuation
2069       address. */
2070    VG_(main_thread_wrapper_NORETURN)(1);
2071 
2072    /*NOTREACHED*/
2073    vg_assert(0);
2074 }
2075 
2076 /* Do everything which needs doing when the last thread exits or when
2077    a thread exits requesting a complete process exit.
2078 
2079    We enter here holding The Lock.  For the case VgSrc_ExitProcess we
2080    must never release it, because to do so would allow other threads
2081    to continue after the system is ostensibly shut down.  So we must
2082    go to our grave, so to speak, holding the lock.
2083 
2084    In fact, there is never any point in releasing the lock at this
2085    point - we have it, we're shutting down the entire system, and
2086    for the case VgSrc_ExitProcess doing so positively causes trouble.
2087    So don't.
2088 
2089    The final_tidyup call makes a bit of a nonsense of the ExitProcess
2090    case, since it will run __gnu_cxx::__freeres and libc_freeres functions,
2091    thus allowing other lurking threads to run again.  Hmm. */
2092 
2093 static
shutdown_actions_NORETURN(ThreadId tid,VgSchedReturnCode tids_schedretcode)2094 void shutdown_actions_NORETURN( ThreadId tid,
2095                                 VgSchedReturnCode tids_schedretcode )
2096 {
2097    VG_(debugLog)(1, "main", "entering VG_(shutdown_actions_NORETURN)\n");
2098    VG_(am_show_nsegments)(1,"Memory layout at client shutdown");
2099 
2100    vg_assert(VG_(is_running_thread)(tid));
2101    vg_assert(tids_schedretcode == VgSrc_ExitThread
2102 	     || tids_schedretcode == VgSrc_ExitProcess
2103              || tids_schedretcode == VgSrc_FatalSig );
2104 
2105    if (tids_schedretcode == VgSrc_ExitThread) {
2106 
2107       // We are the last surviving thread.  Right?
2108       vg_assert( VG_(count_living_threads)() == 1 );
2109 
2110       // Wait for all other threads to exit.
2111       // jrs: Huh?  but they surely are already gone
2112       VG_(reap_threads)(tid);
2113 
2114       // Clean the client up before the final report.
2115       // This causes __gnu_cxx::__freeres and libc_freeres functions to run.
2116       final_tidyup(tid);
2117 
2118       /* be paranoid */
2119       vg_assert(VG_(is_running_thread)(tid));
2120       vg_assert(VG_(count_living_threads)() == 1);
2121 
2122    } else {
2123 
2124       // We may not be the last surviving thread.  However, we
2125       // want to shut down the entire process.  We hold the lock
2126       // and we need to keep hold of it all the way out, in order
2127       // that none of the other threads ever run again.
2128       vg_assert( VG_(count_living_threads)() >= 1 );
2129 
2130       // Clean the client up before the final report.
2131       // This causes __gnu_cxx::__freeres and libc_freeres functions to run.
2132       // Perhaps this is unsafe, as per comment above.
2133       final_tidyup(tid);
2134 
2135       /* be paranoid */
2136       vg_assert(VG_(is_running_thread)(tid));
2137       vg_assert(VG_(count_living_threads)() >= 1);
2138    }
2139 
2140    /* Final call to gdbserver, if requested. */
2141    if (VG_(gdbserver_stop_at) (VgdbStopAt_Exit)) {
2142       VG_(umsg)("(action at exit) vgdb me ... \n");
2143       VG_(gdbserver) (tid);
2144    }
2145    VG_(threads)[tid].status = VgTs_Empty;
2146 
2147    //--------------------------------------------------------------
2148    // Finalisation: cleanup, messages, etc.  Order not so important, only
2149    // affects what order the messages come.
2150    //--------------------------------------------------------------
2151    // First thing in the post-amble is a blank line.
2152    if (VG_(clo_xml))
2153       VG_(printf_xml)("\n");
2154    else if (VG_(clo_verbosity) > 0)
2155       VG_(message)(Vg_UserMsg, "\n");
2156 
2157    if (VG_(clo_xml)) {
2158       HChar buf[50];    // large enough
2159       VG_(elapsed_wallclock_time)(buf, sizeof buf);
2160       VG_(printf_xml)( "<status>\n"
2161                               "  <state>FINISHED</state>\n"
2162                               "  <time>%pS</time>\n"
2163                               "</status>\n"
2164                               "\n",
2165                               buf);
2166    }
2167 
2168    /* Print out file descriptor summary and stats. */
2169    if (VG_(clo_track_fds))
2170       VG_(show_open_fds)("at exit");
2171 
2172    /* Call the tool's finalisation function.  This makes Memcheck's
2173       leak checker run, and possibly chuck a bunch of leak errors into
2174       the error management machinery. */
2175    VG_TDICT_CALL(tool_fini, 0/*exitcode*/);
2176 
2177    if (VG_(needs).core_errors || VG_(needs).tool_errors) {
2178       if (VG_(clo_verbosity) == 1
2179           && !VG_(clo_xml)
2180           && !VG_(clo_show_error_list))
2181          VG_(message)(Vg_UserMsg,
2182                       "For lists of detected and suppressed errors,"
2183                       " rerun with: -s\n");
2184 
2185       /* Show the error counts. */
2186       if (VG_(clo_xml)) {
2187          VG_(show_error_counts_as_XML)();
2188       }
2189 
2190       /* In XML mode, this merely prints the used suppressions. */
2191       VG_(show_all_errors)(VG_(clo_verbosity), VG_(clo_xml));
2192    }
2193 
2194    if (VG_(clo_xml)) {
2195       VG_(printf_xml)("\n");
2196       VG_(printf_xml)("</valgrindoutput>\n");
2197       VG_(printf_xml)("\n");
2198    }
2199 
2200    VG_(sanity_check_general)( True /*include expensive checks*/ );
2201 
2202    if (VG_(clo_stats))
2203       VG_(print_all_stats)(VG_(clo_verbosity) >= 1, /* Memory stats */
2204                            False /* tool prints stats in the tool fini */);
2205 
2206    /* Show a profile of the heap(s) at shutdown.  Optionally, first
2207       throw away all the debug info, as that makes it easy to spot
2208       leaks in the debuginfo reader. */
2209    if (VG_(clo_profile_heap)) {
2210       if (0) VG_(di_discard_ALL_debuginfo)();
2211       VG_(print_arena_cc_analysis)();
2212    }
2213 
2214    /* If profiling has been requested, but with zero interval, it
2215       means "profile at the end of the run only".  In which case we
2216       need to dump the profile now. */
2217    if (VG_(clo_profyle_sbs) && VG_(clo_profyle_interval) == 0) {
2218       VG_(get_and_show_SB_profile)(0/*denoting end-of-run*/);
2219    }
2220 
2221    /* Print Vex storage stats */
2222    if (0)
2223        LibVEX_ShowAllocStats();
2224 
2225    /* Flush any output cached by previous calls to VG_(message). */
2226    VG_(message_flush)();
2227 
2228    /* Terminate gdbserver if ever it was started. We terminate it here
2229       so that it get the output above if output was redirected to
2230       gdb */
2231    VG_(gdbserver_exit) (tid, tids_schedretcode);
2232 
2233    /* Ok, finally exit in the os-specific way, according to the scheduler's
2234       return code.  In short, if the (last) thread exited by calling
2235       sys_exit, do likewise; if the (last) thread stopped due to a fatal
2236       signal, terminate the entire system with that same fatal signal. */
2237    VG_(debugLog)(1, "core_os",
2238                  "VG_(terminate_NORETURN)(tid=%u) schedretcode %s"
2239                  " os_state.exit_code %ld fatalsig %d\n",
2240                  tid, VG_(name_of_VgSchedReturnCode)(tids_schedretcode),
2241                  VG_(threads)[tid].os_state.exitcode,
2242                  VG_(threads)[tid].os_state.fatalsig);
2243 
2244    switch (tids_schedretcode) {
2245    case VgSrc_ExitThread:  /* the normal way out (Linux, Solaris) */
2246    case VgSrc_ExitProcess: /* the normal way out (Darwin) */
2247       /* Change the application return code to user's return code,
2248          if an error was found */
2249       if (VG_(clo_error_exitcode) > 0
2250           && VG_(get_n_errs_found)() > 0) {
2251          VG_(client_exit)( VG_(clo_error_exitcode) );
2252       } else {
2253          /* otherwise, return the client's exit code, in the normal
2254             way. */
2255          VG_(client_exit)( VG_(threads)[tid].os_state.exitcode );
2256       }
2257       /* NOT ALIVE HERE! */
2258       VG_(core_panic)("entered the afterlife in main() -- ExitT/P");
2259       break; /* what the hell :) */
2260 
2261    case VgSrc_FatalSig:
2262       /* We were killed by a fatal signal, so replicate the effect */
2263       vg_assert(VG_(threads)[tid].os_state.fatalsig != 0);
2264       VG_(kill_self)(VG_(threads)[tid].os_state.fatalsig);
2265       /* we shouldn't be alive at this point.  But VG_(kill_self)
2266          sometimes fails with EPERM on Darwin, for unclear reasons. */
2267 #     if defined(VGO_darwin)
2268       VG_(debugLog)(0, "main", "VG_(kill_self) failed.  Exiting normally.\n");
2269       VG_(exit)(0); /* bogus, but we really need to exit now */
2270       /* fall through .. */
2271 #     endif
2272       VG_(core_panic)("main(): signal was supposed to be fatal");
2273       break;
2274 
2275    default:
2276       VG_(core_panic)("main(): unexpected scheduler return code");
2277    }
2278 }
2279 
2280 /* -------------------- */
2281 
2282 /* Final clean-up before terminating the process.
2283    Clean up the client by calling __gnu_cxx::__freeres() (if requested)
2284    and __libc_freeres() (if requested).
2285 */
final_tidyup(ThreadId tid)2286 static void final_tidyup(ThreadId tid)
2287 {
2288 #if defined(VGO_linux) || defined(VGO_solaris)
2289    Addr freeres_wrapper = VG_(client_freeres_wrapper);
2290 
2291    vg_assert(VG_(is_running_thread)(tid));
2292 
2293    if (freeres_wrapper == 0) {
2294       return; /* can't do it */
2295    }
2296 
2297    Vg_FreeresToRun to_run = 0;
2298    if (VG_(needs).cxx_freeres && VG_(clo_run_cxx_freeres)) {
2299       to_run |= VG_RUN__GNU_CXX__FREERES;
2300    }
2301 
2302    if (VG_(needs).libc_freeres && VG_(clo_run_libc_freeres)) {
2303       to_run |= VG_RUN__LIBC_FREERES;
2304    }
2305 
2306    if (to_run == 0) {
2307       return; /* won't do it */
2308    }
2309 
2310 #  if defined(VGP_ppc64be_linux)
2311    Addr r2 = VG_(get_tocptr)(VG_(current_DiEpoch)(),
2312                              freeres_wrapper);
2313    if (r2 == 0) {
2314       VG_(message)(Vg_UserMsg,
2315                    "Caught __NR_exit, but can't run __gnu_cxx::__freeres()\n");
2316       VG_(message)(Vg_UserMsg,
2317                    "   or __libc_freeres() since cannot establish TOC pointer "
2318                    "for it.\n");
2319       return;
2320    }
2321 #  endif
2322 
2323    if (VG_(clo_verbosity) > 2  ||
2324        VG_(clo_trace_syscalls) ||
2325        VG_(clo_trace_sched)) {
2326 
2327       vg_assert(to_run > 0);
2328       vg_assert(to_run <= (VG_RUN__GNU_CXX__FREERES | VG_RUN__LIBC_FREERES));
2329 
2330       const HChar *msgs[] = {"__gnu_cxx::__freeres()", "__libc_freeres()",
2331                              "__gnu_cxx::__freeres and __libc_freeres()"};
2332       VG_(message)(Vg_DebugMsg,
2333                    "Caught __NR_exit; running %s wrapper\n", msgs[to_run - 1]);
2334    }
2335 
2336    /* Set thread context to point to freeres_wrapper.
2337       ppc64be-linux note: freeres_wrapper gives us the real
2338       function entry point, not a fn descriptor, so can use it
2339       directly.  However, we need to set R2 (the toc pointer)
2340       appropriately. */
2341    VG_(set_IP)(tid, freeres_wrapper);
2342 
2343 #  if defined(VGP_ppc64be_linux)
2344    VG_(threads)[tid].arch.vex.guest_GPR2 = r2;
2345    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2346             offsetof(VexGuestPPC64State, guest_GPR2),
2347             sizeof(VG_(threads)[tid].arch.vex.guest_GPR2));
2348 #  elif  defined(VGP_ppc64le_linux)
2349    /* setting GPR2 but not really needed, GPR12 is needed */
2350    VG_(threads)[tid].arch.vex.guest_GPR2  = freeres_wrapper;
2351    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2352             offsetof(VexGuestPPC64State, guest_GPR2),
2353             sizeof(VG_(threads)[tid].arch.vex.guest_GPR2));
2354    VG_(threads)[tid].arch.vex.guest_GPR12 = freeres_wrapper;
2355    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2356             offsetof(VexGuestPPC64State, guest_GPR12),
2357             sizeof(VG_(threads)[tid].arch.vex.guest_GPR12));
2358 #  endif
2359    /* mips-linux note: we need to set t9 */
2360 #  if defined(VGP_mips32_linux)
2361    VG_(threads)[tid].arch.vex.guest_r25 = freeres_wrapper;
2362    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2363             offsetof(VexGuestMIPS32State, guest_r25),
2364             sizeof(VG_(threads)[tid].arch.vex.guest_r25));
2365 #  elif defined(VGP_mips64_linux)
2366    VG_(threads)[tid].arch.vex.guest_r25 = freeres_wrapper;
2367    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2368             offsetof(VexGuestMIPS64State, guest_r25),
2369             sizeof(VG_(threads)[tid].arch.vex.guest_r25));
2370 #  endif
2371 
2372    /* Pass a parameter to freeres_wrapper(). */
2373 #  if defined(VGA_x86)
2374    Addr sp = VG_(threads)[tid].arch.vex.guest_ESP;
2375    *((UWord *) sp) = to_run;
2376    VG_TRACK(post_mem_write, Vg_CoreClientReq, tid, sp, sizeof(UWord));
2377    sp = sp - sizeof(UWord);
2378    VG_(threads)[tid].arch.vex.guest_ESP = sp;
2379    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2380             offsetof(VexGuestX86State, guest_ESP),
2381             sizeof(VG_(threads)[tid].arch.vex.guest_ESP));
2382 #  elif defined(VGA_amd64)
2383    VG_(threads)[tid].arch.vex.guest_RDI = to_run;
2384    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2385             offsetof(VexGuestAMD64State, guest_RDI),
2386             sizeof(VG_(threads)[tid].arch.vex.guest_RDI));
2387 #   elif defined(VGA_arm)
2388    VG_(threads)[tid].arch.vex.guest_R0 = to_run;
2389    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2390             offsetof(VexGuestARMState, guest_R0),
2391             sizeof(VG_(threads)[tid].arch.vex.guest_R0));
2392 #  elif defined(VGA_arm64)
2393    VG_(threads)[tid].arch.vex.guest_X0 = to_run;
2394    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2395             offsetof(VexGuestARM64State, guest_X0),
2396             sizeof(VG_(threads)[tid].arch.vex.guest_X0));
2397 #  elif defined(VGA_mips32)
2398    VG_(threads)[tid].arch.vex.guest_r4 = to_run;
2399    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2400             offsetof(VexGuestMIPS32State, guest_r4),
2401             sizeof(VG_(threads)[tid].arch.vex.guest_r4));
2402 #  elif defined(VGA_mips64)
2403    VG_(threads)[tid].arch.vex.guest_r4 = to_run;
2404    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2405             offsetof(VexGuestMIPS64State, guest_r4),
2406             sizeof(VG_(threads)[tid].arch.vex.guest_r4));
2407 #  elif defined(VGA_ppc32)
2408    VG_(threads)[tid].arch.vex.guest_GPR3 = to_run;
2409    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2410             offsetof(VexGuestPPC32State, guest_GPR3),
2411             sizeof(VG_(threads)[tid].arch.vex.guest_GPR3));
2412 #  elif defined(VGA_ppc64be) || defined(VGA_ppc64le)
2413    VG_(threads)[tid].arch.vex.guest_GPR3 = to_run;
2414    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2415             offsetof(VexGuestPPC64State, guest_GPR3),
2416             sizeof(VG_(threads)[tid].arch.vex.guest_GPR3));
2417 #  elif defined(VGA_s390x)
2418    VG_(threads)[tid].arch.vex.guest_r2 = to_run;
2419    VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2420             offsetof(VexGuestS390XState, guest_r2),
2421             sizeof(VG_(threads)[tid].arch.vex.guest_r2));
2422 #else
2423    I_die_here : architecture missing in m_main.c
2424 #endif
2425 
2426    /* Block all blockable signals by copying the real block state into
2427       the thread's block state */
2428    VG_(sigprocmask)(VKI_SIG_BLOCK, NULL, &VG_(threads)[tid].sig_mask);
2429    VG_(threads)[tid].tmp_sig_mask = VG_(threads)[tid].sig_mask;
2430 
2431    /* and restore handlers to default. */
2432    VG_(set_default_handler)(VKI_SIGSEGV);
2433    VG_(set_default_handler)(VKI_SIGBUS);
2434    VG_(set_default_handler)(VKI_SIGILL);
2435    VG_(set_default_handler)(VKI_SIGFPE);
2436 
2437    // We were exiting, so assert that...
2438    vg_assert(VG_(is_exiting)(tid));
2439    // ...but now we're not again.
2440    VG_(threads)[tid].exitreason = VgSrc_None;
2441 
2442    // Run until client thread exits - ideally with FREERES_DONE,
2443    // but exit/exitgroup/signal will do.
2444    VG_(scheduler)(tid);
2445 
2446    vg_assert(VG_(is_exiting)(tid));
2447 #endif
2448 }
2449 
2450 
2451 /*====================================================================*/
2452 /*=== Getting to main() alive: LINUX                               ===*/
2453 /*====================================================================*/
2454 
2455 #if defined(VGO_linux) || defined(VGO_dragonfly)
2456 
2457 /* If linking of the final executables is done with glibc present,
2458    then Valgrind starts at main() above as usual, and all of the
2459    following code is irrelevant.
2460 
2461    However, this is not the intended mode of use.  The plan is to
2462    avoid linking against glibc, by giving gcc the flags
2463    -nodefaultlibs -lgcc -nostartfiles at startup.
2464 
2465    From this derive two requirements:
2466 
2467    1. gcc may emit calls to memcpy, memmove and memset to deal with
2468       structure assignments etc.  Since we have chosen to ignore all the
2469       "normal" supporting libraries, we have to provide our own
2470       implementations of them.  No problem.
2471 
2472    2. We have to provide a symbol "_start", to which the kernel
2473       hands control at startup.  Hence the code below.
2474 */
2475 
2476 /* ---------------- Requirement 1 ---------------- */
2477 
2478 void* memcpy(void *dest, const void *src, SizeT n);
memcpy(void * dest,const void * src,SizeT n)2479 void* memcpy(void *dest, const void *src, SizeT n) {
2480    return VG_(memcpy)(dest,src,n);
2481 }
2482 void* memmove(void *dest, const void *src, SizeT n);
memmove(void * dest,const void * src,SizeT n)2483 void* memmove(void *dest, const void *src, SizeT n) {
2484    return VG_(memmove)(dest,src,n);
2485 }
2486 void* memset(void *s, int c, SizeT n);
memset(void * s,int c,SizeT n)2487 void* memset(void *s, int c, SizeT n) {
2488   return VG_(memset)(s,c,n);
2489 }
2490 
2491 /* BVA: abort() for those platforms that need it (PPC and ARM). */
2492 void abort(void);
abort(void)2493 void abort(void){
2494    VG_(printf)("Something called raise().\n");
2495    vg_assert(0);
2496 }
2497 
2498 /* EAZG: ARM's EABI will call floating point exception handlers in
2499    libgcc which boil down to an abort or raise, that's usually defined
2500    in libc. Instead, define them here. */
2501 #if defined(VGP_arm_linux)
2502 
2503 void raise(void);
raise(void)2504 void raise(void){
2505    VG_(printf)("Something called raise().\n");
2506    vg_assert(0);
2507 }
2508 
2509 void __aeabi_unwind_cpp_pr0(void);
__aeabi_unwind_cpp_pr0(void)2510 void __aeabi_unwind_cpp_pr0(void){
2511    VG_(printf)("Something called __aeabi_unwind_cpp_pr0()\n");
2512    vg_assert(0);
2513 }
2514 
2515 void __aeabi_unwind_cpp_pr1(void);
__aeabi_unwind_cpp_pr1(void)2516 void __aeabi_unwind_cpp_pr1(void){
2517    VG_(printf)("Something called __aeabi_unwind_cpp_pr1()\n");
2518    vg_assert(0);
2519 }
2520 
2521 #endif /* defined(VGP_arm_linux) */
2522 
2523 /* Some Android helpers.  See bug 368529. */
2524 #if defined(__clang__) \
2525     && (defined(VGPV_arm_linux_android) \
2526         || defined(VGPV_x86_linux_android) \
2527         || defined(VGPV_mips32_linux_android) \
2528         || defined(VGPV_arm64_linux_android))
2529 
2530 /* Replace __aeabi_memcpy* functions with vgPlain_memcpy. */
2531 void *__aeabi_memcpy(void *dest, const void *src, SizeT n);
__aeabi_memcpy(void * dest,const void * src,SizeT n)2532 void *__aeabi_memcpy(void *dest, const void *src, SizeT n)
2533 {
2534     return VG_(memcpy)(dest, src, n);
2535 }
2536 
2537 void *__aeabi_memcpy4(void *dest, const void *src, SizeT n);
__aeabi_memcpy4(void * dest,const void * src,SizeT n)2538 void *__aeabi_memcpy4(void *dest, const void *src, SizeT n)
2539 {
2540     return VG_(memcpy)(dest, src, n);
2541 }
2542 
2543 void *__aeabi_memcpy8(void *dest, const void *src, SizeT n);
__aeabi_memcpy8(void * dest,const void * src,SizeT n)2544 void *__aeabi_memcpy8(void *dest, const void *src, SizeT n)
2545 {
2546     return VG_(memcpy)(dest, src, n);
2547 }
2548 
2549 /* Replace __aeabi_memclr* functions with vgPlain_memset. */
2550 void *__aeabi_memclr(void *dest, SizeT n);
__aeabi_memclr(void * dest,SizeT n)2551 void *__aeabi_memclr(void *dest, SizeT n)
2552 {
2553     return VG_(memset)(dest, 0, n);
2554 }
2555 
2556 void *__aeabi_memclr4(void *dest, SizeT n);
__aeabi_memclr4(void * dest,SizeT n)2557 void *__aeabi_memclr4(void *dest, SizeT n)
2558 {
2559     return VG_(memset)(dest, 0, n);
2560 }
2561 
2562 void *__aeabi_memclr8(void *dest, SizeT n);
__aeabi_memclr8(void * dest,SizeT n)2563 void *__aeabi_memclr8(void *dest, SizeT n)
2564 {
2565     return VG_(memset)(dest, 0, n);
2566 }
2567 #endif /* clang and android, basically */
2568 
2569 /* ---------------- Requirement 2 ---------------- */
2570 
2571 /* Glibc's sysdeps/i386/elf/start.S has the following gem of a
2572    comment, which explains how the stack looks right at process start
2573    (when _start is jumped to).  Hence _start passes %esp to
2574    _start_in_C_linux, which extracts argc/argv/envp and starts up
2575    correctly. */
2576 
2577 /* This is the canonical entry point, usually the first thing in the text
2578    segment.  The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
2579    point runs, most registers' values are unspecified, except for:
2580 
2581    %edx         Contains a function pointer to be registered with `atexit'.
2582                 This is how the dynamic linker arranges to have DT_FINI
2583                 functions called for shared libraries that have been loaded
2584                 before this code runs.
2585 
2586    %esp         The stack contains the arguments and environment:
2587                 0(%esp)                 argc
2588                 4(%esp)                 argv[0]
2589                 ...
2590                 (4*argc)(%esp)          NULL
2591                 (4*(argc+1))(%esp)      envp[0]
2592                 ...
2593                                         NULL
2594 */
2595 
2596 /* The kernel hands control to _start, which extracts the initial
2597    stack pointer and calls onwards to _start_in_C_linux.  This also switches
2598    the new stack.  */
2599 #if defined(VGP_x86_linux)
2600 asm("\n"
2601     ".text\n"
2602     "\t.globl _start\n"
2603     "\t.type _start,@function\n"
2604     "_start:\n"
2605     /* set up the new stack in %eax */
2606     "\tmovl  $vgPlain_interim_stack, %eax\n"
2607     "\taddl  $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n"
2608     "\taddl  $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %eax\n"
2609     /* allocate at least 16 bytes on the new stack, and aligned */
2610     "\tsubl  $16, %eax\n"
2611     "\tandl  $~15, %eax\n"
2612     /* install it, and collect the original one */
2613     "\txchgl %eax, %esp\n"
2614     /* call _start_in_C_linux, passing it the startup %esp */
2615     "\tmovl  %eax, (%esp)\n"
2616     "\tcall  _start_in_C_linux\n"
2617     "\thlt\n"
2618     ".previous\n"
2619 );
2620 #elif defined(VGP_amd64_linux)
2621 asm("\n"
2622     ".text\n"
2623     "\t.globl _start\n"
2624     "\t.type _start,@function\n"
2625     "_start:\n"
2626     /* set up the new stack in %rdi */
2627     "\tmovq  $vgPlain_interim_stack, %rdi\n"
2628     "\taddq  $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rdi\n"
2629     "\taddq  $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %rdi\n"
2630     "\tandq  $~15, %rdi\n"
2631     /* install it, and collect the original one */
2632     "\txchgq %rdi, %rsp\n"
2633     /* call _start_in_C_linux, passing it the startup %rsp */
2634     "\tcall  _start_in_C_linux\n"
2635     "\thlt\n"
2636     ".previous\n"
2637 );
2638 #elif defined(VGP_ppc32_linux)
2639 asm("\n"
2640     ".text\n"
2641     "\t.globl _start\n"
2642     "\t.type _start,@function\n"
2643     "_start:\n"
2644     /* set up the new stack in r16 */
2645     "\tlis 16,vgPlain_interim_stack@ha\n"
2646     "\tla  16,vgPlain_interim_stack@l(16)\n"
2647     "\tlis    17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" >> 16)\n"
2648     "\tori 17,17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" & 0xFFFF)\n"
2649     "\tlis    18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" >> 16)\n"
2650     "\tori 18,18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" & 0xFFFF)\n"
2651     "\tadd 16,17,16\n"
2652     "\tadd 16,18,16\n"
2653     "\trlwinm 16,16,0,0,27\n"
2654     /* now r16 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
2655        VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
2656        boundary.  And r1 is the original SP.  Set the SP to r16 and
2657        call _start_in_C_linux, passing it the initial SP. */
2658     "\tmr 3,1\n"
2659     "\tmr 1,16\n"
2660     "\tbl _start_in_C_linux\n"
2661     "\ttrap\n"
2662     ".previous\n"
2663 );
2664 #elif defined(VGP_ppc64be_linux)
2665 asm("\n"
2666     /* PPC64 ELF ABI says '_start' points to a function descriptor.
2667        So we must have one, and that is what goes into the .opd section. */
2668     "\t.align 2\n"
2669     "\t.global _start\n"
2670     "\t.section \".opd\",\"aw\"\n"
2671     "\t.align 3\n"
2672     "_start:\n"
2673     "\t.quad ._start,.TOC.@tocbase,0\n"
2674     "\t.previous\n"
2675     "\t.type ._start,@function\n"
2676     "\t.global  ._start\n"
2677     "._start:\n"
2678     /* set up the new stack in r16 */
2679     "\tlis  16,   vgPlain_interim_stack@highest\n"
2680     "\tori  16,16,vgPlain_interim_stack@higher\n"
2681     "\tsldi 16,16,32\n"
2682     "\toris 16,16,vgPlain_interim_stack@h\n"
2683     "\tori  16,16,vgPlain_interim_stack@l\n"
2684     "\txor  17,17,17\n"
2685     "\tlis    17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" >> 16)\n"
2686     "\tori 17,17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" & 0xFFFF)\n"
2687     "\txor 18,18,18\n"
2688     "\tlis    18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" >> 16)\n"
2689     "\tori 18,18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" & 0xFFFF)\n"
2690     "\tadd 16,17,16\n"
2691     "\tadd 16,18,16\n"
2692     "\trldicr 16,16,0,59\n"
2693     /* now r16 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
2694        VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
2695        boundary.  And r1 is the original SP.  Set the SP to r16 and
2696        call _start_in_C_linux, passing it the initial SP. */
2697     "\tmr 3,1\n"
2698     "\tmr 1,16\n"
2699     "\tlis  14,   _start_in_C_linux@highest\n"
2700     "\tori  14,14,_start_in_C_linux@higher\n"
2701     "\tsldi 14,14,32\n"
2702     "\toris 14,14,_start_in_C_linux@h\n"
2703     "\tori  14,14,_start_in_C_linux@l\n"
2704     "\tld 14,0(14)\n"
2705     "\tmtctr 14\n"
2706     "\tbctrl\n"
2707     "\tnop\n"
2708     "\ttrap\n"
2709 );
2710 #elif defined(VGP_ppc64le_linux)
2711 /* Little Endian uses ELF version 2 but in the future may also
2712  * support other ELF versions.
2713  */
2714 asm("\n"
2715     "\t.align 2\n"
2716     "\t.global _start\n"
2717     "\t.type _start,@function\n"
2718     "_start:\n"
2719     "#if _CALL_ELF == 2    \n"
2720     "0:  addis        2,12,.TOC.-0b@ha\n"
2721     "    addi         2,2,.TOC.-0b@l\n"
2722     "    .localentry  _start, .-_start\n"
2723     "#endif \n"
2724     /* set up the new stack in r16 */
2725     "\tlis  16,   vgPlain_interim_stack@highest\n"
2726     "\tori  16,16,vgPlain_interim_stack@higher\n"
2727     "\tsldi 16,16,32\n"
2728     "\toris 16,16,vgPlain_interim_stack@h\n"
2729     "\tori  16,16,vgPlain_interim_stack@l\n"
2730     "\txor  17,17,17\n"
2731     "\tlis    17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" >> 16)\n"
2732     "\tori 17,17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" & 0xFFFF)\n"
2733     "\txor 18,18,18\n"
2734     "\tlis    18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" >> 16)\n"
2735     "\tori 18,18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" & 0xFFFF)\n"
2736     "\tadd 16,17,16\n"
2737     "\tadd 16,18,16\n"
2738     "\trldicr 16,16,0,59\n"
2739     /* now r16 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
2740        VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
2741        boundary.  And r1 is the original SP.  Set the SP to r16 and
2742        call _start_in_C_linux, passing it the initial SP. */
2743     "\tmr 3,1\n"
2744     "\tmr 1,16\n"
2745     "\tlis  14,   _start_in_C_linux@highest\n"
2746     "\tori  14,14,_start_in_C_linux@higher\n"
2747     "\tsldi 14,14,32\n"
2748     "\toris 14,14,_start_in_C_linux@h\n"
2749     "\tori  14,14,_start_in_C_linux@l\n"
2750     "\tmtctr 14\n"
2751     "\tbctrl\n"
2752     "\tnop\n"
2753     "\ttrap\n"
2754 );
2755 #elif defined(VGP_s390x_linux)
2756 /*
2757     This is the canonical entry point, usually the first thing in the text
2758     segment. Most registers' values are unspecified, except for:
2759 
2760     %r14         Contains a function pointer to be registered with `atexit'.
2761                  This is how the dynamic linker arranges to have DT_FINI
2762                  functions called for shared libraries that have been loaded
2763                  before this code runs.
2764 
2765     %r15         The stack contains the arguments and environment:
2766                  0(%r15)              argc
2767                  8(%r15)              argv[0]
2768                  ...
2769                  (8*argc)(%r15)       NULL
2770                  (8*(argc+1))(%r15)   envp[0]
2771                  ...
2772                                       NULL
2773 */
2774 asm("\n\t"
2775     ".text\n\t"
2776     ".globl _start\n\t"
2777     ".type  _start,@function\n\t"
2778     "_start:\n\t"
2779     /* set up the new stack in %r1 */
2780     "larl   %r1,  vgPlain_interim_stack\n\t"
2781     "larl   %r5,  1f\n\t"
2782     "ag     %r1,  0(%r5)\n\t"
2783     "ag     %r1,  2f-1f(%r5)\n\t"
2784     "nill   %r1,  0xFFF0\n\t"
2785     /* install it, and collect the original one */
2786     "lgr    %r2,  %r15\n\t"
2787     "lgr    %r15, %r1\n\t"
2788     /* call _start_in_C_linux, passing it the startup %r15 */
2789     "brasl  %r14, _start_in_C_linux\n\t"
2790     /* trigger execution of an invalid opcode -> halt machine */
2791     "j      .+2\n\t"
2792     "1:   .quad "VG_STRINGIFY(VG_STACK_GUARD_SZB)"\n\t"
2793     "2:   .quad "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n\t"
2794     ".previous\n"
2795 );
2796 #elif defined(VGP_arm_linux)
2797 asm("\n"
2798     "\t.text\n"
2799     "\t.align 4\n"
2800     "\t.type _start,#function\n"
2801     "\t.global _start\n"
2802     "_start:\n"
2803     "\tldr  r0, [pc, #36]\n"
2804     "\tldr  r1, [pc, #36]\n"
2805     "\tadd  r0, r1, r0\n"
2806     "\tldr  r1, [pc, #32]\n"
2807     "\tadd  r0, r1, r0\n"
2808     "\tmvn  r1, #15\n"
2809     "\tand  r0, r0, r1\n"
2810     "\tmov  r1, sp\n"
2811     "\tmov  sp, r0\n"
2812     "\tmov  r0, r1\n"
2813     "\tb _start_in_C_linux\n"
2814     "\t.word vgPlain_interim_stack\n"
2815     "\t.word "VG_STRINGIFY(VG_STACK_GUARD_SZB)"\n"
2816     "\t.word "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n"
2817 );
2818 #elif defined(VGP_arm64_linux)
2819 asm("\n"
2820     "\t.text\n"
2821     "\t.align 2\n"
2822     "\t.type _start,#function\n"
2823     "\t.global _start\n"
2824     "_start:\n"
2825     "\tadrp x0, vgPlain_interim_stack\n"
2826     "\tadd  x0, x0, :lo12:vgPlain_interim_stack\n"
2827     // The next 2 assume that VG_STACK_GUARD_SZB fits in 32 bits
2828     "\tmov  x1, (("VG_STRINGIFY(VG_STACK_GUARD_SZB)") >> 0) & 0xFFFF\n"
2829     "\tmovk x1, (("VG_STRINGIFY(VG_STACK_GUARD_SZB)") >> 16) & 0xFFFF,"
2830                 " lsl 16\n"
2831     "\tadd  x0, x0, x1\n"
2832     // The next 2 assume that VG_DEFAULT_STACK_ACTIVE_SZB fits in 32 bits
2833     "\tmov  x1, (("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)") >> 0) & 0xFFFF\n"
2834     "\tmovk x1, (("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)") >> 16) & 0xFFFF,"
2835                 " lsl 16\n"
2836     "\tadd  x0, x0, x1\n"
2837     "\tand  x0, x0, -16\n"
2838     "\tmov  x1, sp\n"
2839     "\tmov  sp, x0\n"
2840     "\tmov  x0, x1\n"
2841     "\tb _start_in_C_linux\n"
2842 );
2843 #elif defined(VGP_mips32_linux)
2844 asm("\n"
2845     "\t.type _gp_disp,@object\n"
2846     ".text\n"
2847     "\t.globl __start\n"
2848     "\t.type __start,@function\n"
2849     "__start:\n"
2850 
2851     "\tbal 1f\n"
2852     "\tnop\n"
2853 
2854     "1:\n"
2855 
2856     "\tlui      $28, %hi(_gp_disp)\n"
2857     "\taddiu    $28, $28, %lo(_gp_disp)\n"
2858     "\taddu     $28, $28, $31\n"
2859     /* t1/$9 <- Addr(interim_stack) */
2860     "\tlui      $9, %hi(vgPlain_interim_stack)\n"
2861     /* t1/$9 <- Addr(interim_stack) */
2862     "\taddiu    $9, %lo(vgPlain_interim_stack)\n"
2863 
2864 
2865     "\tli    $10, "VG_STRINGIFY(VG_STACK_GUARD_SZB)"\n"
2866     "\tli    $11, "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n"
2867 
2868     "\taddu     $9, $9, $10\n"
2869     "\taddu     $9, $9, $11\n"
2870     "\tli       $12, 0xFFFFFFF0\n"
2871     "\tand      $9, $9, $12\n"
2872     /* now t1/$9 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
2873        VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
2874        boundary.  And $29 is the original SP.  Set the SP to t1 and
2875        call _start_in_C, passing it the initial SP. */
2876 
2877     "\tmove    $4, $29\n"     // a0 <- $sp (_start_in_C first arg)
2878     "\tmove    $29, $9\n"     // $sp <- t1 (new sp)
2879 
2880     "\tlui     $25, %hi(_start_in_C_linux)\n"
2881     "\taddiu   $25, %lo(_start_in_C_linux)\n"
2882 
2883     "\tbal  _start_in_C_linux\n"
2884     "\tbreak  0x7\n"
2885     ".previous\n"
2886 );
2887 #elif defined(VGP_x86_dragonfly)
2888 asm("\n"
2889     ".text\n"
2890     "\t.globl _start\n"
2891     "\t.type _start,@function\n"
2892     "_start:\n"
2893     /* set up the new stack in %eax */
2894     "\tmovl  $vgPlain_interim_stack, %eax\n"
2895     "\taddl  $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n"
2896     "\taddl  $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %eax\n"
2897     "\tsubl  $16, %eax\n"
2898     "\tandl  $~15, %eax\n"
2899     /* install it, and collect the original one */
2900     "\txchgl %eax, %esp\n"
2901     /* call _start_in_C_linux, passing it the startup %esp */
2902     "\tpushl %eax\n"
2903     "\tcall  _start_in_C_linux\n"
2904     "\thlt\n"
2905     ".previous\n"
2906 );
2907 #elif defined(VGP_amd64_dragonfly)
2908 asm("\n"
2909     ".text\n"
2910     "\t.globl _start\n"
2911     "\t.type _start,@function\n"
2912     "_start:\n"
2913     /* set up the new stack in %rsi */
2914     "\tmovq  $vgPlain_interim_stack, %rsi\n"
2915     "\taddq  $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rsi\n"
2916     "\taddq  $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %rsi\n"
2917     "\tandq  $~15, %rsi\n"
2918     /* install it, and collect the original one */
2919     "\txchgq %rsi, %rsp\n"
2920     /* call _start_in_C_amd64_dragonfly, passing it the startup %rsp */
2921     "\tcall  _start_in_C_amd64_dragonfly\n"
2922     "\thlt\n"
2923     ".previous\n"
2924 );
2925 #elif defined(VGP_mips64_linux)
2926 asm(
2927 ".text\n"
2928 ".globl __start\n"
2929 ".type __start,@function\n"
2930 "__start:\n"
2931     "\t.set noreorder\n"
2932     "\t.cpload $25\n"
2933     "\t.set reorder\n"
2934     "\t.cprestore 16\n"
2935     "\tlui    $9, %hi(vgPlain_interim_stack)\n"
2936     /* t1/$9 <- Addr(interim_stack) */
2937     "\tdaddiu $9, %lo(vgPlain_interim_stack)\n"
2938 
2939     "\tli     $10, "VG_STRINGIFY(VG_STACK_GUARD_SZB)"\n"
2940     "\tli     $11, "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n"
2941 
2942     "\tdaddu  $9, $9, $10\n"
2943     "\tdaddu  $9, $9, $11\n"
2944     "\tli     $12, 0xFFFFFF00\n"
2945     "\tand    $9, $9, $12\n"
2946     /* now t1/$9 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
2947        VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
2948        boundary.  And $29 is the original SP.  Set the SP to t1 and
2949        call _start_in_C, passing it the initial SP. */
2950 
2951     "\tmove   $4, $29\n"     // a0 <- $sp (_start_in_C first arg)
2952     "\tmove   $29, $9\n"     // $sp <- t1 (new sp)
2953 
2954     "\tlui    $9, %highest(_start_in_C_linux)\n"
2955     "\tori    $9, %higher(_start_in_C_linux)\n"
2956     "\tdsll32 $9, $9, 0x0\n"
2957     "\tlui    $10, %hi(_start_in_C_linux)\n"
2958     "\tdaddiu $10, %lo(_start_in_C_linux)\n"
2959     "\tdaddu  $25, $9, $10\n"
2960     "\tjalr   $25\n"
2961     "\tnop\n"
2962 ".previous\n"
2963 );
2964 #else
2965 #  error "Unknown platform"
2966 #endif
2967 
2968 /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */
2969 #define _GNU_SOURCE
2970 #define _FILE_OFFSET_BITS 64
2971 /* This is in order to get AT_NULL and AT_PAGESIZE. */
2972 #include <elf.h>
2973 /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */
2974 
2975 #if defined(VGP_amd64_dragonfly)
2976 void _start_in_C_amd64_dragonfly ( UWord* pArgc, UWord *initial_sp );
_start_in_C_amd64_dragonfly(UWord * pArgc,UWord * initial_sp)2977 void _start_in_C_amd64_dragonfly ( UWord* pArgc, UWord *initial_sp )
2978 {
2979    Int     r;
2980    Word    argc = pArgc[0];
2981    HChar** argv = (HChar**)&pArgc[1];
2982    HChar** envp = (HChar**)&pArgc[1+argc+1];
2983 
2984    VG_(memset)( &the_iicii, 0, sizeof(the_iicii) );
2985    VG_(memset)( &the_iifii, 0, sizeof(the_iifii) );
2986 
2987    the_iicii.sp_at_startup = (Addr)initial_sp;
2988 
2989    r = valgrind_main( (Int)argc, argv, envp );
2990    /* NOTREACHED */
2991    VG_(exit)(r);
2992 }
2993 
2994 #else
2995 /* Avoid compiler warnings: this fn _is_ used, but labelling it
2996    'static' causes gcc to complain it isn't.
2997    attribute 'used' also ensures the code is not eliminated at link
2998    time */
2999 __attribute__ ((used))
3000 void _start_in_C_linux ( UWord* pArgc );
3001 __attribute__ ((used))
_start_in_C_linux(UWord * pArgc)3002 void _start_in_C_linux ( UWord* pArgc )
3003 {
3004    Int     r;
3005    Word    argc = pArgc[0];
3006    HChar** argv = (HChar**)&pArgc[1];
3007    HChar** envp = (HChar**)&pArgc[1+argc+1];
3008 
3009    // For an inner Valgrind, register the interim stack asap.
3010    // This is needed to allow the outer valgrind to do stacktraces during init.
3011    // Note that this stack is not unregistered when the main thread
3012    // is switching to the (real) stack. Unregistering this would imply
3013    // to save the stack id in a global variable, and have a "if"
3014    // in run_a_thread_NORETURN to do the unregistration only for the
3015    // main thread. This unregistration is not worth this complexity.
3016    INNER_REQUEST
3017       ((void) VALGRIND_STACK_REGISTER
3018        (&VG_(interim_stack).bytes[0],
3019         &VG_(interim_stack).bytes[0] + sizeof(VG_(interim_stack))));
3020 
3021    VG_(memset)( &the_iicii, 0, sizeof(the_iicii) );
3022    VG_(memset)( &the_iifii, 0, sizeof(the_iifii) );
3023 
3024    the_iicii.sp_at_startup = (Addr)pArgc;
3025 
3026 #  if defined(VGP_ppc32_linux) || defined(VGP_ppc64be_linux) \
3027       || defined(VGP_ppc64le_linux) || defined(VGP_arm64_linux) \
3028       || defined(VGP_mips32_linux)  || defined(VGP_mips64_linux)
3029    {
3030       /* ppc32/ppc64, arm64, mips32/64 can be configured with different
3031          page sizes. Determine this early. This is an ugly hack and really
3032          should be moved into valgrind_main. */
3033       UWord *sp = &pArgc[1+argc+1];
3034       while (*sp++ != 0)
3035          ;
3036       for (; *sp != AT_NULL && *sp != AT_PAGESZ; sp += 2);
3037       if (*sp == AT_PAGESZ) {
3038          VKI_PAGE_SIZE = sp[1];
3039          for (VKI_PAGE_SHIFT = 12;
3040               VKI_PAGE_SHIFT <= VKI_MAX_PAGE_SHIFT; VKI_PAGE_SHIFT++)
3041             if (VKI_PAGE_SIZE == (1UL << VKI_PAGE_SHIFT))
3042          break;
3043       }
3044    }
3045 #  endif
3046 
3047    r = valgrind_main( (Int)argc, argv, envp );
3048    /* NOTREACHED */
3049    VG_(exit)(r);
3050 }
3051 #endif
3052 
3053 
3054 /*====================================================================*/
3055 /*=== Getting to main() alive: darwin                              ===*/
3056 /*====================================================================*/
3057 
3058 #elif defined(VGO_darwin)
3059 
3060 /*
3061    Memory layout established by kernel:
3062 
3063    0(%esp)   argc
3064    4(%esp)   argv[0]
3065              ...
3066              argv[argc-1]
3067              NULL
3068              envp[0]
3069              ...
3070              envp[n]
3071              NULL
3072              executable name (presumably, a pointer to it)
3073              NULL
3074 
3075    Ditto in the 64-bit case, except all offsets from SP are obviously
3076    twice as large.
3077 */
3078 
3079 /* The kernel hands control to _start, which extracts the initial
3080    stack pointer and calls onwards to _start_in_C_darwin.  This also
3081    switches to the new stack.  */
3082 #if defined(VGP_x86_darwin)
3083 asm("\n"
3084     ".text\n"
3085     ".align 2,0x90\n"
3086     "\t.globl __start\n"
3087     "__start:\n"
3088     /* set up the new stack in %eax */
3089     "\tmovl  $_vgPlain_interim_stack, %eax\n"
3090     "\taddl  $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n"
3091     "\taddl  $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %eax\n"
3092     "\tsubl  $16, %eax\n"
3093     "\tandl  $~15, %eax\n"
3094     /* install it, and collect the original one */
3095     "\txchgl %eax, %esp\n"
3096     "\tsubl  $12, %esp\n"  // keep stack 16 aligned; see #295428
3097     /* call _start_in_C_darwin, passing it the startup %esp */
3098     "\tpushl %eax\n"
3099     "\tcall  __start_in_C_darwin\n"
3100     "\tint $3\n"
3101     "\tint $3\n"
3102 );
3103 #elif defined(VGP_amd64_darwin)
3104 asm("\n"
3105     ".text\n"
3106     "\t.globl __start\n"
3107     ".align 3,0x90\n"
3108     "__start:\n"
3109     /* set up the new stack in %rdi */
3110     "\tmovabsq $_vgPlain_interim_stack, %rdi\n"
3111     "\taddq    $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rdi\n"
3112     "\taddq    $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %rdi\n"
3113     "\tandq    $~15, %rdi\n"
3114     /* install it, and collect the original one */
3115     "\txchgq %rdi, %rsp\n"
3116     /* call _start_in_C_darwin, passing it the startup %rsp */
3117     "\tcall  __start_in_C_darwin\n"
3118     "\tint $3\n"
3119     "\tint $3\n"
3120 );
3121 #endif
3122 
3123 void* __memcpy_chk(void *dest, const void *src, SizeT n, SizeT n2);
__memcpy_chk(void * dest,const void * src,SizeT n,SizeT n2)3124 void* __memcpy_chk(void *dest, const void *src, SizeT n, SizeT n2) {
3125     // skip check
3126    return VG_(memcpy)(dest,src,n);
3127 }
3128 void* __memset_chk(void *s, int c, SizeT n, SizeT n2);
__memset_chk(void * s,int c,SizeT n,SizeT n2)3129 void* __memset_chk(void *s, int c, SizeT n, SizeT n2) {
3130     // skip check
3131   return VG_(memset)(s,c,n);
3132 }
3133 void __bzero(void* s, UWord n);
__bzero(void * s,UWord n)3134 void __bzero(void* s, UWord n) {
3135     (void)VG_(memset)(s,0,n);
3136 }
3137 void bzero(void *s, SizeT n);
bzero(void * s,SizeT n)3138 void bzero(void *s, SizeT n) {
3139     VG_(memset)(s,0,n);
3140 }
3141 
3142 void* memcpy(void *dest, const void *src, SizeT n);
memcpy(void * dest,const void * src,SizeT n)3143 void* memcpy(void *dest, const void *src, SizeT n) {
3144    return VG_(memcpy)(dest,src,n);
3145 }
3146 void* memset(void *s, int c, SizeT n);
memset(void * s,int c,SizeT n)3147 void* memset(void *s, int c, SizeT n) {
3148   return VG_(memset)(s,c,n);
3149 }
3150 
3151 /* Avoid compiler warnings: this fn _is_ used, but labelling it
3152    'static' causes gcc to complain it isn't. */
3153 void _start_in_C_darwin ( UWord* pArgc );
_start_in_C_darwin(UWord * pArgc)3154 void _start_in_C_darwin ( UWord* pArgc )
3155 {
3156    Int     r;
3157    Int     argc = *(Int *)pArgc;  // not pArgc[0] on LP64
3158    HChar** argv = (HChar**)&pArgc[1];
3159    HChar** envp = (HChar**)&pArgc[1+argc+1];
3160 
3161    // See _start_in_C_linux
3162    INNER_REQUEST
3163       ((void) VALGRIND_STACK_REGISTER
3164        (&VG_(interim_stack).bytes[0],
3165         &VG_(interim_stack).bytes[0] + sizeof(VG_(interim_stack))));
3166 
3167    VG_(memset)( &the_iicii, 0, sizeof(the_iicii) );
3168    VG_(memset)( &the_iifii, 0, sizeof(the_iifii) );
3169 
3170    the_iicii.sp_at_startup = (Addr)pArgc;
3171 
3172    r = valgrind_main( (Int)argc, argv, envp );
3173    /* NOTREACHED */
3174    VG_(exit)(r);
3175 }
3176 
3177 /*====================================================================*/
3178 /*=== Getting to main() alive: Solaris                             ===*/
3179 /*====================================================================*/
3180 #elif defined(VGO_solaris)
3181 #if defined(VGP_x86_solaris)
3182 /* The kernel hands control to _start, which extracts the initial stack
3183    pointer and calls onwards to _start_in_C_solaris.  This also switches to
3184    the new stack. */
3185 asm("\n"
3186     "\t.text\n"
3187     "\t.globl _start\n"
3188     "\t.type _start, @function\n"
3189     "_start:\n"
3190     /* Set up the new stack in %eax. */
3191     "\tmovl  $vgPlain_interim_stack, %eax\n"
3192     "\taddl  $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n"
3193     "\taddl  $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %eax\n"
3194     "\tandl  $~15, %eax\n"
3195     /* Install it, and collect the original one. */
3196     "\txchgl %eax, %esp\n"
3197     "\tsubl  $12, %esp\n"  /* Keep stack 16-byte aligned. */
3198     /* Call _start_in_C_solaris, passing it the startup %esp. */
3199     "\tpushl %eax\n"
3200     "\tcall  _start_in_C_solaris\n"
3201     /* NOTREACHED */
3202     "\thlt\n"
3203     "\t.previous\n"
3204 );
3205 #elif defined(VGP_amd64_solaris)
3206 asm("\n"
3207     ".text\n"
3208     "\t.globl _start\n"
3209     "\t.type _start, @function\n"
3210     "_start:\n"
3211     /* Set up the new stack in %rdi. */
3212     "\tmovq  $vgPlain_interim_stack, %rdi\n"
3213     "\taddq  $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rdi\n"
3214     "\taddq  $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %rdi\n"
3215     "\tandq  $~15, %rdi\n"
3216     /* Install it, and collect the original one. */
3217     "\txchgq %rdi, %rsp\n"
3218     /* Call _start_in_C_solaris, passing it the startup %rsp. */
3219     "\tcall  _start_in_C_solaris\n"
3220     /* NOTREACHED */
3221     "\thlt\n"
3222     ".previous\n"
3223 );
3224 #else
3225 #  error "Unknown Solaris platform"
3226 #endif
3227 
3228 void *memcpy(void *dest, const void *src, size_t n);
memcpy(void * dest,const void * src,size_t n)3229 void *memcpy(void *dest, const void *src, size_t n) {
3230    return VG_(memcpy)(dest, src, n);
3231 }
3232 
3233 __attribute__ ((used))
3234 void _start_in_C_solaris ( UWord* pArgc );
3235 __attribute__ ((used))
_start_in_C_solaris(UWord * pArgc)3236 void _start_in_C_solaris ( UWord* pArgc )
3237 {
3238    Int     r;
3239    Word    argc = pArgc[0];
3240    HChar** argv = (HChar**)&pArgc[1];
3241    HChar** envp = (HChar**)&pArgc[1 + argc + 1];
3242 
3243    VG_(memset)( &the_iicii, 0, sizeof(the_iicii) );
3244    VG_(memset)( &the_iifii, 0, sizeof(the_iifii) );
3245 
3246    the_iicii.sp_at_startup = (Addr)pArgc;
3247 
3248    r = valgrind_main((Int)argc, argv, envp);
3249    /* NOTREACHED */
3250    VG_(exit)(r);
3251 }
3252 
3253 #else
3254 #  error "Unknown OS"
3255 #endif
3256 
3257 
VG_(get_initial_client_SP)3258 Addr VG_(get_initial_client_SP)( void )
3259 {
3260    return the_iifii.initial_client_SP;
3261 }
3262 
3263 /*====================================================================*/
3264 /*=== {u,}{div,mod}di3 replacements                                ===*/
3265 /*====================================================================*/
3266 
3267 /* For static linking on x86-darwin, we need to supply our own 64-bit
3268    integer division code, else the link dies thusly:
3269 
3270    ld_classic: Undefined symbols:
3271      ___udivdi3
3272      ___umoddi3
3273 */
3274 #if defined(VGP_x86_darwin)
3275 
3276 /* Routines for doing signed/unsigned 64 x 64 ==> 64 div and mod
3277    (udivdi3, umoddi3, divdi3, moddi3) using only 32 x 32 ==> 32
3278    division.  Cobbled together from
3279 
3280    http://www.hackersdelight.org/HDcode/divlu.c
3281    http://www.hackersdelight.org/HDcode/divls.c
3282    http://www.hackersdelight.org/HDcode/newCode/divDouble.c
3283 
3284    The code from those three files is covered by the following license,
3285    as it appears at:
3286 
3287    http://www.hackersdelight.org/permissions.htm
3288 
3289       You are free to use, copy, and distribute any of the code on
3290       this web site, whether modified by you or not. You need not give
3291       attribution. This includes the algorithms (some of which appear
3292       in Hacker's Delight), the Hacker's Assistant, and any code
3293       submitted by readers. Submitters implicitly agree to this.
3294 */
3295 
3296 /* Long division, unsigned (64/32 ==> 32).
3297    This procedure performs unsigned "long division" i.e., division of a
3298 64-bit unsigned dividend by a 32-bit unsigned divisor, producing a
3299 32-bit quotient.  In the overflow cases (divide by 0, or quotient
3300 exceeds 32 bits), it returns a remainder of 0xFFFFFFFF (an impossible
3301 value).
3302    The dividend is u1 and u0, with u1 being the most significant word.
3303 The divisor is parameter v. The value returned is the quotient.
3304    Max line length is 57, to fit in hacker.book. */
3305 
nlz32(UInt x)3306 static Int nlz32(UInt x)
3307 {
3308    Int n;
3309    if (x == 0) return(32);
3310    n = 0;
3311    if (x <= 0x0000FFFF) {n = n +16; x = x <<16;}
3312    if (x <= 0x00FFFFFF) {n = n + 8; x = x << 8;}
3313    if (x <= 0x0FFFFFFF) {n = n + 4; x = x << 4;}
3314    if (x <= 0x3FFFFFFF) {n = n + 2; x = x << 2;}
3315    if (x <= 0x7FFFFFFF) {n = n + 1;}
3316    return n;
3317 }
3318 
3319 /* 64 x 32 ==> 32 unsigned division, using only 32 x 32 ==> 32
3320    division as a primitive. */
divlu2(UInt u1,UInt u0,UInt v,UInt * r)3321 static UInt divlu2(UInt u1, UInt u0, UInt v, UInt *r)
3322 {
3323    const UInt b = 65536;     // Number base (16 bits).
3324    UInt un1, un0,            // Norm. dividend LSD's.
3325         vn1, vn0,            // Norm. divisor digits.
3326         q1, q0,              // Quotient digits.
3327         un32, un21, un10,    // Dividend digit pairs.
3328         rhat;                // A remainder.
3329    Int s;                    // Shift amount for norm.
3330 
3331    if (u1 >= v) {            // If overflow, set rem.
3332       if (r != NULL)         // to an impossible value,
3333          *r = 0xFFFFFFFF;    // and return the largest
3334       return 0xFFFFFFFF;}    // possible quotient.
3335 
3336    s = nlz32(v);             // 0 <= s <= 31.
3337    v = v << s;               // Normalize divisor.
3338    vn1 = v >> 16;            // Break divisor up into
3339    vn0 = v & 0xFFFF;         // two 16-bit digits.
3340 
3341    un32 = (u1 << s) | ((u0 >> (32 - s)) & (-s >> 31));
3342    un10 = u0 << s;           // Shift dividend left.
3343 
3344    un1 = un10 >> 16;         // Break right half of
3345    un0 = un10 & 0xFFFF;      // dividend into two digits.
3346 
3347    q1 = un32/vn1;            // Compute the first
3348    rhat = un32 - q1*vn1;     // quotient digit, q1.
3349  again1:
3350    if (q1 >= b || q1*vn0 > b*rhat + un1) {
3351      q1 = q1 - 1;
3352      rhat = rhat + vn1;
3353      if (rhat < b) goto again1;}
3354 
3355    un21 = un32*b + un1 - q1*v;  // Multiply and subtract.
3356 
3357    q0 = un21/vn1;            // Compute the second
3358    rhat = un21 - q0*vn1;     // quotient digit, q0.
3359  again2:
3360    if (q0 >= b || q0*vn0 > b*rhat + un0) {
3361      q0 = q0 - 1;
3362      rhat = rhat + vn1;
3363      if (rhat < b) goto again2;}
3364 
3365    if (r != NULL)            // If remainder is wanted,
3366       *r = (un21*b + un0 - q0*v) >> s;     // return it.
3367    return q1*b + q0;
3368 }
3369 
3370 
3371 /* 64 x 32 ==> 32 signed division, using only 32 x 32 ==> 32 division
3372    as a primitive. */
divls(Int u1,UInt u0,Int v,Int * r)3373 static Int divls(Int u1, UInt u0, Int v, Int *r)
3374 {
3375    Int q, uneg, vneg, diff, borrow;
3376 
3377    uneg = u1 >> 31;          // -1 if u < 0.
3378    if (uneg) {               // Compute the absolute
3379       u0 = -u0;              // value of the dividend u.
3380       borrow = (u0 != 0);
3381       u1 = -u1 - borrow;}
3382 
3383    vneg = v >> 31;           // -1 if v < 0.
3384    v = (v ^ vneg) - vneg;    // Absolute value of v.
3385 
3386    if ((UInt)u1 >= (UInt)v) goto overflow;
3387 
3388    q = divlu2(u1, u0, v, (UInt *)r);
3389 
3390    diff = uneg ^ vneg;       // Negate q if signs of
3391    q = (q ^ diff) - diff;    // u and v differed.
3392    if (uneg && r != NULL)
3393       *r = -*r;
3394 
3395    if ((diff ^ q) < 0 && q != 0) {  // If overflow,
3396  overflow:                    // set remainder
3397       if (r != NULL)         // to an impossible value,
3398          *r = 0x80000000;    // and return the largest
3399       q = 0x80000000;}       // possible neg. quotient.
3400    return q;
3401 }
3402 
3403 
3404 
3405 /* This file contains a program for doing 64/64 ==> 64 division, on a
3406 machine that does not have that instruction but that does have
3407 instructions for "long division" (64/32 ==> 32). Code for unsigned
3408 division is given first, followed by a simple program for doing the
3409 signed version by using the unsigned version.
3410    These programs are useful in implementing "long long" (64-bit)
3411 arithmetic on a machine that has the long division instruction. It will
3412 work on 64- and 32-bit machines, provided the compiler implements long
3413 long's (64-bit integers). It is desirable that the machine have the
3414 Count Leading Zeros instruction.
3415    In the GNU world, these programs are known as __divdi3 and __udivdi3,
3416 and similar names are used here.
3417    This material is not in HD, but may be in a future edition.
3418 Max line length is 57, to fit in hacker.book. */
3419 
3420 
nlz64(ULong x)3421 static Int nlz64(ULong x)
3422 {
3423    Int n;
3424    if (x == 0) return(64);
3425    n = 0;
3426    if (x <= 0x00000000FFFFFFFFULL) {n = n + 32; x = x << 32;}
3427    if (x <= 0x0000FFFFFFFFFFFFULL) {n = n + 16; x = x << 16;}
3428    if (x <= 0x00FFFFFFFFFFFFFFULL) {n = n +  8; x = x <<  8;}
3429    if (x <= 0x0FFFFFFFFFFFFFFFULL) {n = n +  4; x = x <<  4;}
3430    if (x <= 0x3FFFFFFFFFFFFFFFULL) {n = n +  2; x = x <<  2;}
3431    if (x <= 0x7FFFFFFFFFFFFFFFULL) {n = n +  1;}
3432    return n;
3433 }
3434 
3435 // ---------------------------- udivdi3 --------------------------------
3436 
3437    /* The variables u0, u1, etc. take on only 32-bit values, but they
3438    are declared long long to avoid some compiler warning messages and to
3439    avoid some unnecessary EXTRs that the compiler would put in, to
3440    convert long longs to ints.
3441 
3442    First the procedure takes care of the case in which the divisor is a
3443    32-bit quantity. There are two subcases: (1) If the left half of the
3444    dividend is less than the divisor, one execution of DIVU is all that
3445    is required (overflow is not possible). (2) Otherwise it does two
3446    divisions, using the grade school method, with variables used as
3447    suggested below.
3448 
3449        q1 q0
3450     ________
3451    v)  u1 u0
3452      q1*v
3453      ____
3454         k u0   */
3455 
3456 /* These macros must be used with arguments of the appropriate type
3457 (unsigned long long for DIVU and long long for DIVS. They are
3458 simulations of the presumed machines ops. I.e., they look at only the
3459 low-order 32 bits of the divisor, they return garbage if the division
3460 overflows, and they return garbage in the high-order half of the
3461 quotient doubleword.
3462    In practice, these would be replaced with uses of the machine's DIVU
3463 and DIVS instructions (e.g., by using the GNU "asm" facility). */
3464 
DIVU(ULong u,UInt v)3465 static UInt DIVU ( ULong u, UInt v )
3466 {
3467   UInt uHi = (UInt)(u >> 32);
3468   UInt uLo = (UInt)u;
3469   return divlu2(uHi, uLo, v, NULL);
3470 }
3471 
DIVS(Long u,Int v)3472 static Int DIVS ( Long u, Int v )
3473 {
3474   Int  uHi = (Int)(u >> 32);
3475   UInt uLo = (UInt)u;
3476   return divls(uHi, uLo, v, NULL);
3477 }
3478 
3479 /* 64 x 64 ==> 64 unsigned division, using only 32 x 32 ==> 32
3480    division as a primitive. */
udivdi3(ULong u,ULong v)3481 static ULong udivdi3(ULong u, ULong v)
3482 {
3483    ULong u0, u1, v1, q0, q1, k, n;
3484 
3485    if (v >> 32 == 0) {          // If v < 2**32:
3486       if (u >> 32 < v)          // If u/v cannot overflow,
3487          return DIVU(u, v)      // just do one division.
3488             & 0xFFFFFFFF;
3489       else {                    // If u/v would overflow:
3490          u1 = u >> 32;          // Break u up into two
3491          u0 = u & 0xFFFFFFFF;   // halves.
3492          q1 = DIVU(u1, v)       // First quotient digit.
3493             & 0xFFFFFFFF;
3494          k = u1 - q1*v;         // First remainder, < v.
3495          q0 = DIVU((k << 32) + u0, v) // 2nd quot. digit.
3496             & 0xFFFFFFFF;
3497          return (q1 << 32) + q0;
3498       }
3499    }
3500                                 // Here v >= 2**32.
3501    n = nlz64(v);                // 0 <= n <= 31.
3502    v1 = (v << n) >> 32;         // Normalize the divisor
3503                                 // so its MSB is 1.
3504    u1 = u >> 1;                 // To ensure no overflow.
3505    q1 = DIVU(u1, v1)            // Get quotient from
3506        & 0xFFFFFFFF;            // divide unsigned insn.
3507    q0 = (q1 << n) >> 31;        // Undo normalization and
3508                                 // division of u by 2.
3509    if (q0 != 0)                 // Make q0 correct or
3510       q0 = q0 - 1;              // too small by 1.
3511    if ((u - q0*v) >= v)
3512       q0 = q0 + 1;              // Now q0 is correct.
3513    return q0;
3514 }
3515 
3516 
3517 // ----------------------------- divdi3 --------------------------------
3518 
3519 /* This routine presumes that smallish cases (those which can be done in
3520 one execution of DIVS) are common. If this is not the case, the test for
3521 this case should be deleted.
3522    Note that the test for when DIVS can be used is not entirely
3523 accurate. For example, DIVS is not used if v = 0xFFFFFFFF8000000,
3524 whereas if could be (if u is sufficiently small in magnitude). */
3525 
3526 // ------------------------------ cut ----------------------------------
3527 
my_llabs(Long x)3528 static ULong my_llabs ( Long x )
3529 {
3530    ULong t = x >> 63;
3531    return (x ^ t) - t;
3532 }
3533 
3534 /* 64 x 64 ==> 64 signed division, using only 32 x 32 ==> 32 division
3535    as a primitive. */
divdi3(Long u,Long v)3536 static Long divdi3(Long u, Long v)
3537 {
3538    ULong au, av;
3539    Long q, t;
3540    au = my_llabs(u);
3541    av = my_llabs(v);
3542    if (av >> 31 == 0) {         // If |v| < 2**31 and
3543    // if (v << 32 >> 32 == v) { // If v is in range and
3544       if (au < av << 31) {      // |u|/|v| cannot
3545          q = DIVS(u, v);        // overflow, use DIVS.
3546          return (q << 32) >> 32;
3547       }
3548    }
3549    q = udivdi3(au,av);          // Invoke udivdi3.
3550    t = (u ^ v) >> 63;           // If u, v have different
3551    return (q ^ t) - t;          // signs, negate q.
3552 }
3553 
3554 // ---------------------------- end cut --------------------------------
3555 
3556 ULong __udivdi3 (ULong u, ULong v);
__udivdi3(ULong u,ULong v)3557 ULong __udivdi3 (ULong u, ULong v)
3558 {
3559   return udivdi3(u,v);
3560 }
3561 
3562 Long __divdi3 (Long u, Long v);
__divdi3(Long u,Long v)3563 Long __divdi3 (Long u, Long v)
3564 {
3565   return divdi3(u,v);
3566 }
3567 
3568 ULong __umoddi3 (ULong u, ULong v);
__umoddi3(ULong u,ULong v)3569 ULong __umoddi3 (ULong u, ULong v)
3570 {
3571   ULong q = __udivdi3(u, v);
3572   ULong r = u - q * v;
3573   return r;
3574 }
3575 
3576 Long __moddi3 (Long u, Long v);
__moddi3(Long u,Long v)3577 Long __moddi3 (Long u, Long v)
3578 {
3579   Long q = __divdi3(u, v);
3580   Long r = u - q * v;
3581   return r;
3582 }
3583 
3584 /* ------------------------------------------------
3585    ld_classic: Undefined symbols:
3586       ___fixunsdfdi
3587    ------------------------------------------------
3588 */
3589 
3590 /* ===-- fixunsdfdi.c - Implement __fixunsdfdi -----------------------------===
3591  *
3592  *                     The LLVM Compiler Infrastructure
3593  *
3594  * This file is dual licensed under the MIT and the University of Illinois Open
3595  * Source Licenses. See LICENSE.TXT for details.
3596  *
3597  * ===----------------------------------------------------------------------===
3598  *
3599  * This file implements __fixunsdfdi for the compiler_rt library.
3600  *
3601  * ===----------------------------------------------------------------------===
3602  */
3603 
3604 /* As per http://www.gnu.org/licenses/license-list.html#GPLCompatibleLicenses,
3605 
3606    the "NCSA/University of Illinois Open Source License" is compatible
3607    with the GPL (both version 2 and 3).  What is claimed to be
3608    compatible is this
3609 
3610    http://www.opensource.org/licenses/UoI-NCSA.php
3611 
3612    and the LLVM documentation at
3613 
3614    http://www.llvm.org/docs/DeveloperPolicy.html#license
3615 
3616    says all the code in LLVM is available under the University of
3617    Illinois/NCSA Open Source License, at this URL
3618 
3619    http://www.opensource.org/licenses/UoI-NCSA.php
3620 
3621    viz, the same one that the FSF pages claim is compatible.  So I
3622    think it's OK to include it.
3623 */
3624 
3625 /* Returns: convert a to a unsigned long long, rounding toward zero.
3626  *          Negative values all become zero.
3627  */
3628 
3629 /* Assumption: double is a IEEE 64 bit floating point type
3630  *             du_int is a 64 bit integral type
3631  *             value in double is representable in du_int or is negative
3632  *                 (no range checking performed)
3633  */
3634 
3635 /* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
3636 
3637 typedef unsigned long long du_int;
3638 typedef unsigned su_int;
3639 
3640 typedef union
3641 {
3642     du_int all;
3643     struct
3644     {
3645 #if VG_LITTLEENDIAN
3646         su_int low;
3647         su_int high;
3648 #else
3649         su_int high;
3650         su_int low;
3651 #endif /* VG_LITTLEENDIAN */
3652     }s;
3653 } udwords;
3654 
3655 typedef union
3656 {
3657     udwords u;
3658     double  f;
3659 } double_bits;
3660 
3661 du_int __fixunsdfdi(double a);
3662 
3663 du_int
__fixunsdfdi(double a)3664 __fixunsdfdi(double a)
3665 {
3666     double_bits fb;
3667     fb.f = a;
3668     int e = ((fb.u.s.high & 0x7FF00000) >> 20) - 1023;
3669     if (e < 0 || (fb.u.s.high & 0x80000000))
3670         return 0;
3671     udwords r;
3672     r.s.high = (fb.u.s.high & 0x000FFFFF) | 0x00100000;
3673     r.s.low = fb.u.s.low;
3674     if (e > 52)
3675         r.all <<= (e - 52);
3676     else
3677         r.all >>= (52 - e);
3678     return r.all;
3679 }
3680 
3681 
3682 #endif
3683 
3684 
3685 /*====================================================================*/
3686 /*=== Dummy _voucher_mach_msg_set for OSX 10.10                    ===*/
3687 /*====================================================================*/
3688 
3689 #if defined(VGO_darwin) && DARWIN_VERS >= DARWIN_10_10
3690 
3691 /* Builds on MacOSX 10.10+ seem to need this for some reason. */
3692 /* extern boolean_t voucher_mach_msg_set(mach_msg_header_t *msg)
3693                     __attribute__((weak_import));
3694    I haven't a clue what the return value means, so just return 0.
3695    Looks like none of the generated uses in the tree look at the
3696    return value anyway.
3697 */
3698 UWord voucher_mach_msg_set ( UWord arg1 );
voucher_mach_msg_set(UWord arg1)3699 UWord voucher_mach_msg_set ( UWord arg1 )
3700 {
3701    return 0;
3702 }
3703 
3704 #endif
3705 
3706 
3707 /*--------------------------------------------------------------------*/
3708 /*--- end                                                          ---*/
3709 /*--------------------------------------------------------------------*/
3710