Remove source file added to EXTRA_DIST in error
[valgrind.git] / coregrind / m_redir.c
blob37c67f4c13a96902ccb22c6c35af14d5b1a8cb6d
2 /*--------------------------------------------------------------------*/
3 /*--- Function replacement and wrapping. m_redir.c ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2000-2017 Julian Seward
11 jseward@acm.org
12 Copyright (C) 2003-2017 Jeremy Fitzhardinge
13 jeremy@goop.org
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of the
18 License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, see <http://www.gnu.org/licenses/>.
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics.h"
32 #include "pub_core_debuglog.h"
33 #include "pub_core_debuginfo.h"
34 #include "pub_core_libcbase.h"
35 #include "pub_core_libcassert.h"
36 #include "pub_core_libcprint.h"
37 #include "pub_core_vki.h"
38 #include "pub_core_libcfile.h"
39 #include "pub_core_seqmatch.h"
40 #include "pub_core_mallocfree.h"
41 #include "pub_core_options.h"
42 #include "pub_core_oset.h"
43 #include "pub_core_redir.h"
44 #include "pub_core_trampoline.h"
45 #include "pub_core_transtab.h"
46 #include "pub_core_tooliface.h" // VG_(needs).malloc_replacement
47 #include "pub_core_machine.h" // VG_(fnptr_to_fnentry)
48 #include "pub_core_aspacemgr.h" // VG_(am_find_nsegment)
49 #include "pub_core_xarray.h"
50 #include "pub_core_clientstate.h" // VG_(client_freeres_wrapper)
51 #include "pub_core_demangle.h" // VG_(maybe_Z_demangle)
52 #include "pub_core_libcproc.h" // VG_(libdir)
54 #include "config.h" /* GLIBC_MANDATORY_*_REDIRECT */
57 /* This module is a critical part of the redirection/intercept system.
58 It keeps track of the current intercept state, cleans up the
59 translation caches when that state changes, and finally, answers
60 queries about the whether an address is currently redirected or
61 not. It doesn't do any of the control-flow trickery needed to put
62 the redirections into practice. That is the job of m_translate,
63 which calls here to find out which translations need to be
64 redirected.
66 The interface is simple. VG_(redir_initialise) initialises and
67 loads some hardwired redirects which never disappear; this is
68 platform-specific.
70 The module is notified of redirection state changes by m_debuginfo.
71 That calls VG_(redir_notify_new_DebugInfo) when a new DebugInfo
72 (shared object symbol table, basically) appears. Appearance of new
73 symbols can cause new (active) redirections to appear for two
74 reasons: the symbols in the new table may match existing
75 redirection specifications (see comments below), and because the
76 symbols in the new table may themselves supply new redirect
77 specifications which match existing symbols (or ones in the new
78 table).
80 Redirect specifications are really symbols with "funny" prefixes
81 (_vgrNNNNZU_ and _vgrNNNNZZ_). These names tell m_redir that the
82 associated code should replace the standard entry point for some
83 set of functions. The set of functions is specified by a (soname
84 pattern, function name pattern) pair which is encoded in the symbol
85 name following the prefix. The names use a Z-encoding scheme so
86 that they may contain punctuation characters and wildcards (*).
87 The encoding scheme is described in pub_tool_redir.h and is decoded
88 by VG_(maybe_Z_demangle). The NNNN are behavioural equivalence
89 class tags, and are used to by code in this module to resolve
90 situations where one address appears to be redirected to more than
91 one replacement/wrapper. This is also described in
92 pub_tool_redir.h.
94 When a shared object is unloaded, this module learns of it via a
95 call to VG_(redir_notify_delete_DebugInfo). It then removes from
96 its tables all active redirections in any way associated with that
97 object, and tidies up the translation caches accordingly.
99 That takes care of tracking the redirection state. When a
100 translation is actually to be made, m_translate calls to
101 VG_(redir_do_lookup) in this module to find out if the
102 translation's address should be redirected.
105 /*------------------------------------------------------------*/
106 /*--- Semantics ---*/
107 /*------------------------------------------------------------*/
109 /* The redirector holds two pieces of state:
111 Specs - a set of (soname pattern, fnname pattern) -> redir addr
112 Active - a set of orig addr -> (bool, redir addr)
114 Active is the currently active set of bindings that the translator
115 consults. Specs is the current set of specifications as harvested
116 from reading symbol tables of the currently loaded objects.
118 Active is a pure function of Specs and the current symbol table
119 state (maintained by m_debuginfo). Call the latter SyminfoState.
121 Therefore whenever either Specs or SyminfoState changes, Active
122 must be recomputed. [Inefficient if done naively, but this is a
123 spec].
125 Active is computed as follows:
127 Active = empty
128 for spec in Specs {
129 sopatt = spec.soname pattern
130 fnpatt = spec.fnname pattern
131 redir = spec.redir addr
132 for so matching sopatt in SyminfoState {
133 for fn matching fnpatt in fnnames_of(so) {
134 &fn -> redir is added to Active
139 [as an implementation detail, when a binding (orig -> redir) is
140 deleted from Active as a result of recomputing it, then all
141 translations intersecting redir must be deleted. However, this is
142 not part of the spec].
144 [Active also depends on where the aspacemgr has decided to put all
145 the pieces of code -- that affects the "orig addr" and "redir addr"
146 values.]
148 ---------------------
150 That completes the spec, apart from one difficult issue: duplicates.
152 Clearly we must impose the requirement that domain(Active) contains
153 no duplicates. The difficulty is how to constrain Specs enough to
154 avoid getting into that situation. It's easy to write specs which
155 could cause conflicting bindings in Active, eg:
157 (libpthread.so, pthread_mutex_lock) -> a1
158 (libpthread.so, pthread_*) -> a2
160 for a1 != a2. Or even hairier:
162 (libpthread.so, pthread_mutex_*) -> a1
163 (libpthread.so, pthread_*_lock) -> a2
165 I can't think of any sane way of detecting when an addition to
166 Specs would generate conflicts. However, considering we don't
167 actually want to have a system that allows this, I propose this:
168 all changes to Specs are acceptable. But, when recomputing Active
169 following the change, if the same orig is bound to more than one
170 redir, then the first binding for orig is retained, and all the
171 rest ignored.
173 ===========================================================
174 ===========================================================
175 Incremental implementation:
177 When a new DebugInfo appears:
178 - it may be the source of new specs
179 - it may be the source of new matches for existing specs
180 Therefore:
182 - (new Specs x existing DebugInfos): scan all symbols in the new
183 DebugInfo to find new specs. Each of these needs to be compared
184 against all symbols in all the existing DebugInfos to generate
185 new actives.
187 - (existing Specs x new DebugInfo): scan all symbols in the
188 DebugInfo, trying to match them to any existing specs, also
189 generating new actives.
191 - (new Specs x new DebugInfo): scan all symbols in the new
192 DebugInfo, trying to match them against the new specs, to
193 generate new actives.
195 - Finally, add new new specs to the current set of specs.
197 When adding a new active (s,d) to the Actives:
198 lookup s in Actives
199 if already bound to d, ignore
200 if already bound to something other than d, complain loudly and ignore
201 else add (s,d) to Actives
202 and discard (s,1) and (d,1) (maybe overly conservative)
204 When a DebugInfo disappears:
205 - delete all specs acquired from the seginfo
206 - delete all actives derived from the just-deleted specs
207 - if each active (s,d) deleted, discard (s,1) and (d,1)
211 /*------------------------------------------------------------*/
212 /*--- REDIRECTION SPECIFICATIONS ---*/
213 /*------------------------------------------------------------*/
215 /* A specification of a redirection we want to do. Note that because
216 both the "from" soname and function name may contain wildcards, the
217 spec can match an arbitrary number of times.
219 16 Nov 2007: Comments re .mandatory field: The initial motivation
220 for this is making Memcheck work sanely on glibc-2.6.X ppc32-linux.
221 We really need to intercept 'strlen' in ld.so right from startup.
222 If ld.so does not have a visible 'strlen' symbol, Memcheck
223 generates an impossible number of errors resulting from highly
224 tuned strlen implementation in ld.so, and is completely unusable
225 -- the resulting undefinedness eventually seeps everywhere. */
226 typedef
227 struct _Spec {
228 struct _Spec* next; /* linked list */
229 /* FIXED PARTS -- set when created and not changed */
230 HChar* from_sopatt; /* from soname pattern */
231 HChar* from_fnpatt; /* from fnname pattern */
232 Addr to_addr; /* where redirecting to */
233 Bool isWrap; /* wrap or replacement? */
234 Bool isGlobal; /* must the symbol to replace be global? */
235 Int becTag; /* 0 through 9999. Behavioural equivalance class tag.
236 If two wrappers have the same (non-zero) tag, they
237 are promising that they behave identically. */
238 Int becPrio; /* 0 through 9. Behavioural equivalence class prio.
239 Used to choose between competing wrappers with
240 the same (non-zero) tag. */
241 const HChar** mandatory; /* non-NULL ==> abort V and print the
242 strings if from_sopatt is loaded but
243 from_fnpatt cannot be found */
244 /* VARIABLE PARTS -- used transiently whilst processing redirections */
245 Bool mark; /* set if spec requires further processing */
246 Bool done; /* set if spec was successfully matched */
248 Spec;
250 /* Top-level data structure. It contains a pointer to a DebugInfo and
251 also a list of the specs harvested from that DebugInfo. Note that
252 seginfo is allowed to be NULL, meaning that the specs are
253 pre-loaded ones at startup and are not associated with any
254 particular seginfo. */
255 typedef
256 struct _TopSpec {
257 struct _TopSpec* next; /* linked list */
258 const DebugInfo* seginfo; /* symbols etc */
259 Spec* specs; /* specs pulled out of seginfo */
260 Bool mark; /* transient temporary used during deletion */
262 TopSpec;
264 /* This is the top level list of redirections. m_debuginfo maintains
265 a list of DebugInfos, and the idea here is to maintain a list with
266 the same number of elements (in fact, with one more element, so as
267 to record abovementioned preloaded specifications.) */
268 static TopSpec* topSpecs = NULL;
271 /*------------------------------------------------------------*/
272 /*--- CURRENTLY ACTIVE REDIRECTIONS ---*/
273 /*------------------------------------------------------------*/
275 /* Represents a currently active binding. If either parent_spec or
276 parent_sym is NULL, then this binding was hardwired at startup and
277 should not be deleted. Same is true if either parent's seginfo
278 field is NULL. */
279 typedef
280 struct {
281 Addr from_addr; /* old addr -- MUST BE THE FIRST WORD! */
282 Addr to_addr; /* where redirecting to */
283 TopSpec* parent_spec; /* the TopSpec which supplied the Spec */
284 TopSpec* parent_sym; /* the TopSpec which supplied the symbol */
285 Int becTag; /* behavioural eclass tag for ::to_addr */
286 Int becPrio; /* and its priority */
287 Bool isWrap; /* wrap or replacement? */
288 Bool isIFunc; /* indirect function? */
290 Active;
292 /* The active set is a fast lookup table */
293 static OSet* activeSet = NULL;
295 /* Wrapper routine for indirect functions */
296 static Addr iFuncWrapper;
298 /*------------------------------------------------------------*/
299 /*--- FWDses ---*/
300 /*------------------------------------------------------------*/
302 static void maybe_add_active ( Active /*by value; callee copies*/ );
304 static void* dinfo_zalloc(const HChar* ec, SizeT);
305 static void dinfo_free(void*);
306 static HChar* dinfo_strdup(const HChar* ec, const HChar*);
307 static Bool is_plausible_guest_addr(Addr);
309 static void show_redir_state ( const HChar* who );
310 static void show_active ( const HChar* left, const Active* act );
312 static void handle_maybe_load_notifier( const HChar* soname,
313 const HChar* symbol, Addr addr );
315 static void handle_require_text_symbols ( const DebugInfo* );
317 /*------------------------------------------------------------*/
318 /*--- NOTIFICATIONS ---*/
319 /*------------------------------------------------------------*/
321 static
322 void generate_and_add_actives (
323 /* spec list and the owning TopSpec */
324 Spec* specs,
325 TopSpec* parent_spec,
326 /* debuginfo and the owning TopSpec */
327 const DebugInfo* di,
328 TopSpec* parent_sym
332 /* Copy all the names from a given symbol into an AR_DINFO allocated,
333 NULL terminated array, for easy iteration. Caller must pass also
334 the address of a 2-entry array which can be used in the common case
335 to avoid dynamic allocation. */
336 static const HChar** alloc_symname_array ( const HChar* pri_name,
337 const HChar** sec_names,
338 const HChar** twoslots )
340 /* Special-case the common case: only one name. We expect the
341 caller to supply a stack-allocated 2-entry array for this. */
342 if (sec_names == NULL) {
343 twoslots[0] = pri_name;
344 twoslots[1] = NULL;
345 return twoslots;
347 /* Else must use dynamic allocation. Figure out size .. */
348 Word n_req = 1;
349 const HChar** pp = sec_names;
350 while (*pp) { n_req++; pp++; }
351 /* .. allocate and copy in. */
352 const HChar** arr = dinfo_zalloc("redir.asa.1", (n_req+1) * sizeof(HChar*));
353 Word i = 0;
354 arr[i++] = pri_name;
355 pp = sec_names;
356 while (*pp) { arr[i++] = *pp; pp++; }
357 vg_assert(i == n_req);
358 vg_assert(arr[n_req] == NULL);
359 return arr;
363 /* Free the array allocated by alloc_symname_array, if any. */
364 static void free_symname_array ( const HChar** names, const HChar** twoslots )
366 if (names != twoslots)
367 dinfo_free(names);
370 static HChar const* advance_to_equal ( HChar const* c ) {
371 while (*c && *c != '=') {
372 ++c;
374 return c;
376 static HChar const* advance_to_comma ( HChar const* c ) {
377 while (*c && *c != ',') {
378 ++c;
380 return c;
383 /* Notify m_redir of the arrival of a new DebugInfo. This is fairly
384 complex, but the net effect is to (1) add a new entry to the
385 topspecs list, and (2) figure out what new binding are now active,
386 and, as a result, add them to the actives mapping. */
388 void VG_(redir_notify_new_DebugInfo)( const DebugInfo* newdi )
390 Bool ok, isWrap, isGlobal;
391 Int i, nsyms, becTag, becPrio;
392 Spec* specList;
393 Spec* spec;
394 TopSpec* ts;
395 TopSpec* newts;
396 const HChar* sym_name_pri;
397 const HChar** sym_names_sec;
398 SymAVMAs sym_avmas;
399 const HChar* demangled_sopatt;
400 const HChar* demangled_fnpatt;
401 Bool check_ppcTOCs = False;
402 Bool isText;
403 const HChar* newdi_soname;
404 Bool dehacktivate_pthread_stack_cache_var_search = False;
405 const HChar* const pthread_soname = "libpthread.so.0";
406 const HChar* const pthread_stack_cache_actsize_varname
407 = "stack_cache_actsize";
408 const HChar* const libc_soname = "libc.so.6";
409 const HChar* const libc_gnu_get_libc_version_funcname = "gnu_get_libc_version";
410 #if defined(VGO_solaris)
411 Bool vg_vfork_fildes_var_search = False;
412 const HChar* const vg_preload_core_soname = "vgpreload_core.so.0";
413 const HChar* const vg_vfork_fildes_varname = "vg_vfork_fildes";
414 #endif
416 # if defined(VG_PLAT_USES_PPCTOC)
417 check_ppcTOCs = True;
418 # endif
420 vg_assert(newdi);
421 newdi_soname = VG_(DebugInfo_get_soname)(newdi);
422 vg_assert(newdi_soname != NULL);
424 #ifdef ENABLE_INNER
426 /* When an outer Valgrind is executing an inner Valgrind, the
427 inner "sees" in its address space the mmap-ed vgpreload files
428 of the outer. The inner must avoid interpreting the
429 redirections given in the outer vgpreload mmap-ed files.
430 Otherwise, some tool combinations badly fail.
432 Example: outer memcheck tool executing an inner none tool.
434 If inner none interprets the outer malloc redirection, the
435 inner will redirect malloc to a memcheck function it does not
436 have (as the redirection target is from the outer). With
437 such a failed redirection, a call to malloc inside the inner
438 will then result in a "no-operation" (and so no memory will
439 be allocated).
441 When running as an inner, no redirection will be done
442 for a vgpreload file if this file is not located in the
443 inner VALGRIND_LIB directory.
445 Recognising a vgpreload file based on a filename pattern
446 is a kludge. An alternate solution would be to change
447 the _vgr prefix according to outer/inner/client.
449 const HChar* newdi_filename = VG_(DebugInfo_get_filename)(newdi);
450 const HChar* newdi_basename = VG_(basename) (newdi_filename);
451 if (VG_(strncmp) (newdi_basename, "vgpreload_", 10) == 0) {
452 /* This looks like a vgpreload file => check if this file
453 is from the inner VALGRIND_LIB.
454 We do this check using VG_(stat) + dev/inode comparison
455 as vg-in-place defines a VALGRIND_LIB with symlinks
456 pointing to files inside the valgrind build directories. */
457 struct vg_stat newdi_stat;
458 SysRes newdi_res;
459 struct vg_stat in_vglib_stat;
460 SysRes in_vglib_res;
462 newdi_res = VG_(stat)(newdi_filename, &newdi_stat);
464 HChar in_vglib_filename[VG_(strlen)(VG_(libdir)) + 1 +
465 VG_(strlen)(newdi_basename) + 1];
466 VG_(sprintf)(in_vglib_filename, "%s/%s", VG_(libdir), newdi_basename);
468 in_vglib_res = VG_(stat)(in_vglib_filename, &in_vglib_stat);
470 /* If we find newdi_basename in inner VALGRIND_LIB
471 but newdi_filename is not the same file, then we do
472 not execute the redirection. */
473 if (!sr_isError(in_vglib_res)
474 && !sr_isError(newdi_res)
475 && (newdi_stat.dev != in_vglib_stat.dev
476 || newdi_stat.ino != in_vglib_stat.ino)) {
477 /* <inner VALGRIND_LIB>/newdi_basename is an existing file
478 and is different of newdi_filename.
479 So, we do not execute newdi_filename redirection. */
480 if ( VG_(clo_verbosity) > 1 ) {
481 VG_(message)( Vg_DebugMsg,
482 "Skipping vgpreload redir in %s"
483 " (not from VALGRIND_LIB_INNER)\n",
484 newdi_filename);
486 return;
487 } else {
488 if ( VG_(clo_verbosity) > 1 ) {
489 VG_(message)( Vg_DebugMsg,
490 "Executing vgpreload redir in %s"
491 " (from VALGRIND_LIB_INNER)\n",
492 newdi_filename);
497 #endif
500 /* stay sane: we don't already have this. */
501 for (ts = topSpecs; ts; ts = ts->next)
502 vg_assert(ts->seginfo != newdi);
504 /* scan this DebugInfo's symbol table, pulling out and demangling
505 any specs found */
507 specList = NULL; /* the spec list we're building up */
509 dehacktivate_pthread_stack_cache_var_search =
510 SimHintiS(SimHint_no_nptl_pthread_stackcache, VG_(clo_sim_hints))
511 && (0 == VG_(strcmp)(newdi_soname, pthread_soname) ||
512 0 == VG_(strcmp)(newdi_soname, libc_soname));
514 #if defined(VGO_solaris)
515 vg_vfork_fildes_var_search =
516 0 == VG_(strcmp)(newdi_soname, vg_preload_core_soname);
517 #endif
519 nsyms = VG_(DebugInfo_syms_howmany)( newdi );
520 for (i = 0; i < nsyms; i++) {
521 VG_(DebugInfo_syms_getidx)( newdi, i, &sym_avmas,
522 NULL, &sym_name_pri, &sym_names_sec,
523 &isText, NULL, NULL );
524 /* Set up to conveniently iterate over all names for this symbol. */
525 const HChar* twoslots[2];
526 const HChar** names_init =
527 alloc_symname_array(sym_name_pri, sym_names_sec, &twoslots[0]);
528 const HChar** names;
529 for (names = names_init; *names; names++) {
530 isGlobal = False;
531 ok = VG_(maybe_Z_demangle)( *names,
532 &demangled_sopatt,
533 &demangled_fnpatt,
534 &isWrap, &becTag, &becPrio );
536 if (isText && dehacktivate_pthread_stack_cache_var_search) {
537 if (0 == VG_(strcmp)(*names, libc_gnu_get_libc_version_funcname)) {
538 if ( VG_(clo_verbosity) > 1 ) {
539 VG_(message)( Vg_DebugMsg,
540 "deactivate nptl pthread stackcache via tunable:"
541 " found symbol %s at addr %p\n",
542 *names, (void*) sym_avmas.main);
544 VG_(client__gnu_get_libc_version_addr) = (client__gnu_get_libc_version_type) sym_avmas.main;
545 dehacktivate_pthread_stack_cache_var_search = False;
549 /* ignore data symbols */
550 if (!isText) {
551 /* But search for dehacktivate stack cache var if needed. */
552 if (dehacktivate_pthread_stack_cache_var_search
553 && 0 == VG_(strcmp)(*names,
554 pthread_stack_cache_actsize_varname)) {
555 if ( VG_(clo_verbosity) > 1 ) {
556 VG_(message)( Vg_DebugMsg,
557 "deactivate nptl pthread stackcache via kludge:"
558 " found symbol %s at addr %p\n",
559 *names, (void*) sym_avmas.main);
561 VG_(client__stack_cache_actsize__addr) = (SizeT*) sym_avmas.main;
562 dehacktivate_pthread_stack_cache_var_search = False;
564 #if defined(VGO_solaris)
565 if (vg_vfork_fildes_var_search
566 && 0 == VG_(strcmp)(*names, vg_vfork_fildes_varname)) {
567 if ( VG_(clo_verbosity) > 1 ) {
568 VG_(message)( Vg_DebugMsg,
569 "vfork kludge: found symbol %s at addr %p\n",
570 *names, (void*) sym_avmas.main);
572 VG_(vfork_fildes_addr) = (Int*) sym_avmas.main;
573 vg_vfork_fildes_var_search = False;
575 #endif
576 continue;
578 if (!ok) {
579 /* It's not a full-scale redirect, but perhaps it is a load-notify
580 fn? Let the load-notify department see it. */
581 handle_maybe_load_notifier( newdi_soname, *names, sym_avmas.main );
582 continue;
584 if (check_ppcTOCs && GET_TOCPTR_AVMA(sym_avmas) == 0) {
585 /* This platform uses toc pointers, but none could be found
586 for this symbol, so we can't safely redirect/wrap to it.
587 Just skip it; we'll make a second pass over the symbols in
588 the following loop, and complain at that point. */
589 continue;
592 HChar *replaced_sopatt = NULL;
593 if (0 == VG_(strncmp) (demangled_sopatt,
594 VG_SO_SYN_PREFIX, VG_SO_SYN_PREFIX_LEN)) {
595 /* This is a redirection for handling lib so synonyms. If we
596 have a matching lib synonym, then replace the sopatt.
597 Otherwise, just ignore this redirection spec. */
599 /* Search for a matching synonym=newname*/
600 SizeT const sopatt_syn_len
601 = VG_(strlen)(demangled_sopatt+VG_SO_SYN_PREFIX_LEN);
602 HChar const* last = VG_(clo_soname_synonyms);
604 while (last != NULL && *last) {
605 HChar const* first = last;
606 last = advance_to_equal(first);
608 if ((last - first) == sopatt_syn_len
609 && 0 == VG_(strncmp)(demangled_sopatt+VG_SO_SYN_PREFIX_LEN,
610 first,
611 sopatt_syn_len)) {
612 // Found the demangle_sopatt synonym => replace it
613 first = last + 1;
614 last = advance_to_comma(first);
615 replaced_sopatt = dinfo_zalloc("redir.rnnD.5",
616 last - first + 1);
617 VG_(strncpy)(replaced_sopatt, first, last - first);
618 replaced_sopatt[last - first] = '\0';
619 demangled_sopatt = replaced_sopatt;
620 break;
623 last = advance_to_comma(last);
624 if (*last == ',')
625 last++;
628 // If the user didn't set it then somalloc is special. We
629 // want to match public/global symbols that match the
630 // fnpatt everywhere.
631 if (replaced_sopatt == NULL
632 && VG_(strcmp) ( demangled_sopatt, SO_SYN_MALLOC_NAME ) == 0)
634 replaced_sopatt = dinfo_strdup("m_redir.rnnD.1", "*");
635 demangled_sopatt = replaced_sopatt;
636 isGlobal = True;
639 // If we have not replaced the sopatt, then skip the redir.
640 if (replaced_sopatt == NULL)
641 continue;
644 spec = dinfo_zalloc("redir.rnnD.1", sizeof(Spec));
645 spec->from_sopatt = dinfo_strdup("redir.rnnD.2", demangled_sopatt);
646 spec->from_fnpatt = dinfo_strdup("redir.rnnD.3", demangled_fnpatt);
647 spec->to_addr = sym_avmas.main;
648 spec->isWrap = isWrap;
649 spec->isGlobal = isGlobal;
650 spec->becTag = becTag;
651 spec->becPrio = becPrio;
652 /* check we're not adding manifestly stupid destinations */
653 vg_assert(is_plausible_guest_addr(sym_avmas.main));
654 spec->next = specList;
655 spec->mark = False; /* not significant */
656 spec->done = False; /* not significant */
657 specList = spec;
658 /* The demangler is the owner of the demangled_sopatt memory,
659 unless it was replaced. In this case, we have to free the
660 replace_sopatt(==demangled_sopatt). We can free it,
661 because it was dinfo_strup-ed into spec->from_sopatt. */
662 if (replaced_sopatt != NULL) {
663 vg_assert(demangled_sopatt == replaced_sopatt);
664 dinfo_free(replaced_sopatt);
667 free_symname_array(names_init, &twoslots[0]);
669 if (dehacktivate_pthread_stack_cache_var_search) {
670 VG_(message)(Vg_DebugMsg,
671 "WARNING: could not find symbol for var %s in %s\n",
672 pthread_stack_cache_actsize_varname, pthread_soname);
673 VG_(message)(Vg_DebugMsg,
674 "=> pthread stack cache cannot be disabled!\n");
676 #if defined(VGO_solaris)
677 if (vg_vfork_fildes_var_search) {
678 VG_(message)(Vg_DebugMsg,
679 "WARNING: could not find symbol for var %s in %s\n",
680 vg_vfork_fildes_varname, vg_preload_core_soname);
681 VG_(message)(Vg_DebugMsg,
682 "=> posix_spawn() will not work correctly!\n");
684 #endif
686 if (check_ppcTOCs) {
687 for (i = 0; i < nsyms; i++) {
688 VG_(DebugInfo_syms_getidx)( newdi, i, &sym_avmas,
689 NULL, &sym_name_pri, &sym_names_sec,
690 &isText, NULL, NULL );
691 const HChar* twoslots[2];
692 const HChar** names_init =
693 alloc_symname_array(sym_name_pri, sym_names_sec, &twoslots[0]);
694 const HChar** names;
695 for (names = names_init; *names; names++) {
696 ok = isText
697 && VG_(maybe_Z_demangle)(
698 *names, &demangled_sopatt,
699 &demangled_fnpatt, &isWrap, NULL, NULL );
700 if (!ok)
701 /* not a redirect. Ignore. */
702 continue;
703 if (GET_TOCPTR_AVMA(sym_avmas) != 0)
704 /* has a valid toc pointer. Ignore. */
705 continue;
707 for (spec = specList; spec; spec = spec->next)
708 if (0 == VG_(strcmp)(spec->from_sopatt, demangled_sopatt)
709 && 0 == VG_(strcmp)(spec->from_fnpatt, demangled_fnpatt))
710 break;
711 if (spec)
712 /* a redirect to some other copy of that symbol, which
713 does have a TOC value, already exists */
714 continue;
716 /* Complain */
717 VG_(message)(Vg_DebugMsg,
718 "WARNING: no TOC ptr for redir/wrap to %s %s\n",
719 demangled_sopatt, demangled_fnpatt);
721 free_symname_array(names_init, &twoslots[0]);
725 /* Ok. Now specList holds the list of specs from the DebugInfo.
726 Build a new TopSpec, but don't add it to topSpecs yet. */
727 newts = dinfo_zalloc("redir.rnnD.4", sizeof(TopSpec));
728 newts->next = NULL; /* not significant */
729 newts->seginfo = newdi;
730 newts->specs = specList;
731 newts->mark = False; /* not significant */
733 /* We now need to augment the active set with the following partial
734 cross product:
736 (1) actives formed by matching the new specs in specList against
737 all symbols currently listed in topSpecs
739 (2) actives formed by matching the new symbols in newdi against
740 all specs currently listed in topSpecs
742 (3) actives formed by matching the new symbols in newdi against
743 the new specs in specList
745 This is necessary in order to maintain the invariant that
746 Actives contains all bindings generated by matching ALL specs in
747 topSpecs against ALL symbols in topSpecs (that is, a cross
748 product of ALL known specs against ALL known symbols).
750 /* Case (1) */
751 for (ts = topSpecs; ts; ts = ts->next) {
752 if (ts->seginfo)
753 generate_and_add_actives( specList, newts,
754 ts->seginfo, ts );
757 /* Case (2) */
758 for (ts = topSpecs; ts; ts = ts->next) {
759 generate_and_add_actives( ts->specs, ts,
760 newdi, newts );
763 /* Case (3) */
764 generate_and_add_actives( specList, newts,
765 newdi, newts );
767 /* Finally, add the new TopSpec. */
768 newts->next = topSpecs;
769 topSpecs = newts;
771 if (VG_(clo_trace_redir))
772 show_redir_state("after VG_(redir_notify_new_DebugInfo)");
774 /* Really finally (quite unrelated to all the above) check the
775 names in the module against any --require-text-symbol=
776 specifications we might have. */
777 handle_require_text_symbols(newdi);
780 /* Add a new target for an indirect function. Adds a new redirection
781 for the indirection function with address old_from that redirects
782 the ordinary function with address new_from to the target address
783 of the original redirection. */
785 void VG_(redir_add_ifunc_target)( Addr old_from, Addr new_from )
787 Active *old, new;
789 old = VG_(OSetGen_Lookup)(activeSet, &old_from);
790 vg_assert(old);
791 vg_assert(old->isIFunc);
793 new = *old;
794 new.from_addr = new_from;
795 new.isIFunc = False;
796 maybe_add_active (new);
798 if (VG_(clo_trace_redir)) {
799 VG_(message)( Vg_DebugMsg,
800 "Adding redirect for indirect function "
801 "0x%lx from 0x%lx -> 0x%lx\n",
802 old_from, new_from, new.to_addr );
806 /* Do one element of the basic cross product: add to the active set,
807 all matches resulting from comparing all the given specs against
808 all the symbols in the given seginfo. If a conflicting binding
809 would thereby arise, don't add it, but do complain. */
811 static
812 void generate_and_add_actives (
813 /* spec list and the owning TopSpec */
814 Spec* specs,
815 TopSpec* parent_spec,
816 /* seginfo and the owning TopSpec */
817 const DebugInfo* di,
818 TopSpec* parent_sym
821 Spec* sp;
822 Bool anyMark, isText, isIFunc, isGlobal;
823 Active act;
824 Int nsyms, i;
825 SymAVMAs sym_avmas;
826 const HChar* sym_name_pri;
827 const HChar** sym_names_sec;
829 /* First figure out which of the specs match the seginfo's soname.
830 Also clear the 'done' bits, so that after the main loop below
831 tell which of the Specs really did get done. */
832 anyMark = False;
833 for (sp = specs; sp; sp = sp->next) {
834 sp->done = False;
835 const HChar *soname = VG_(DebugInfo_get_soname)(di);
837 /* When searching for global public symbols (like for the somalloc
838 synonym symbols), exclude the dynamic (runtime) linker as it is very
839 special. See https://bugs.kde.org/show_bug.cgi?id=355454 */
840 if ((VG_(strcmp)(sp->from_sopatt, "*") == 0) &&
841 (sp->isGlobal == True) &&
842 VG_(is_soname_ld_so)(soname)) {
843 sp->mark = False;
844 continue;
847 sp->mark = VG_(string_match)( sp->from_sopatt, soname );
848 anyMark = anyMark || sp->mark;
851 /* shortcut: if none of the sonames match, there will be no bindings. */
852 if (!anyMark)
853 return;
855 /* Iterate outermost over the symbols in the seginfo, in the hope
856 of trashing the caches less. */
857 nsyms = VG_(DebugInfo_syms_howmany)( di );
858 for (i = 0; i < nsyms; i++) {
859 VG_(DebugInfo_syms_getidx)( di, i, &sym_avmas,
860 NULL, &sym_name_pri, &sym_names_sec,
861 &isText, &isIFunc, &isGlobal );
862 const HChar* twoslots[2];
863 const HChar** names_init =
864 alloc_symname_array(sym_name_pri, sym_names_sec, &twoslots[0]);
865 const HChar** names;
866 for (names = names_init; *names; names++) {
868 /* ignore data symbols */
869 if (!isText)
870 continue;
872 for (sp = specs; sp; sp = sp->next) {
873 if (!sp->mark)
874 continue; /* soname doesn't match */
875 if (VG_(string_match)( sp->from_fnpatt, *names )
876 && (sp->isGlobal == False || isGlobal == True)) {
877 /* got a new binding. Add to collection. */
878 act.from_addr = sym_avmas.main;
879 act.to_addr = sp->to_addr;
880 act.parent_spec = parent_spec;
881 act.parent_sym = parent_sym;
882 act.becTag = sp->becTag;
883 act.becPrio = sp->becPrio;
884 act.isWrap = sp->isWrap;
885 act.isIFunc = isIFunc;
886 sp->done = True;
887 maybe_add_active( act );
889 /* If the function being wrapped has a local entry point
890 * redirect it to the global entry point. The redirection
891 * must save and setup r2 then setup r12 for the new function.
892 * On return, r2 must be restored. Local entry points used
893 * in PPC64 Little Endian.
895 if (GET_LOCAL_EP_AVMA(sym_avmas) != 0) {
896 act.from_addr = GET_LOCAL_EP_AVMA(sym_avmas);
897 maybe_add_active( act );
901 } /* for (sp = specs; sp; sp = sp->next) */
903 } /* iterating over names[] */
904 free_symname_array(names_init, &twoslots[0]);
905 } /* for (i = 0; i < nsyms; i++) */
907 /* Now, finally, look for Specs which were marked to be done, but
908 didn't get matched. If any such are mandatory we must abort the
909 system at this point. */
910 for (sp = specs; sp; sp = sp->next) {
911 if (!sp->mark)
912 continue;
913 if (sp->mark && (!sp->done) && sp->mandatory)
914 break;
916 if (sp) {
917 const HChar** strp;
918 const HChar* v = "valgrind: ";
919 vg_assert(sp->mark);
920 vg_assert(!sp->done);
921 vg_assert(sp->mandatory);
922 VG_(printf)("\n");
923 VG_(printf)(
924 "%sFatal error at startup: a function redirection\n", v);
925 VG_(printf)(
926 "%swhich is mandatory for this platform-tool combination\n", v);
927 VG_(printf)(
928 "%scannot be set up. Details of the redirection are:\n", v);
929 VG_(printf)(
930 "%s\n", v);
931 VG_(printf)(
932 "%sA must-be-redirected function\n", v);
933 VG_(printf)(
934 "%swhose name matches the pattern: %s\n", v, sp->from_fnpatt);
935 VG_(printf)(
936 "%sin an object with soname matching: %s\n", v, sp->from_sopatt);
937 VG_(printf)(
938 "%swas not found whilst processing\n", v);
939 VG_(printf)(
940 "%ssymbols from the object with soname: %s\n",
941 v, VG_(DebugInfo_get_soname)(di));
942 VG_(printf)(
943 "%s\n", v);
945 for (strp = sp->mandatory; *strp; strp++)
946 VG_(printf)(
947 "%s%s\n", v, *strp);
949 VG_(printf)(
950 "%s\n", v);
951 VG_(printf)(
952 "%sCannot continue -- exiting now. Sorry.\n", v);
953 VG_(printf)("\n");
954 VG_(exit)(1);
959 /* Add an act (passed by value; is copied here) and deal with
960 conflicting bindings. */
961 static void maybe_add_active ( Active act )
963 const HChar* what = NULL;
964 Active* old = NULL;
965 Bool add_act = False;
967 /* Complain and ignore manifestly bogus 'from' addresses.
969 Kludge: because this can get called befor the trampoline area (a
970 bunch of magic 'to' addresses) has its ownership changed from V
971 to C, we can't check the 'to' address similarly. Sigh.
973 amd64-linux hack: the vsysinfo pages appear to have no
974 permissions
975 ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0
976 so skip the check for them. */
977 if (!is_plausible_guest_addr(act.from_addr)
978 # if defined(VGP_amd64_linux)
979 && act.from_addr != 0xFFFFFFFFFF600000ULL
980 && act.from_addr != 0xFFFFFFFFFF600400ULL
981 && act.from_addr != 0xFFFFFFFFFF600800ULL
982 # endif
984 what = "redirection from-address is in non-executable area";
985 goto bad;
988 old = VG_(OSetGen_Lookup)( activeSet, &act.from_addr );
989 if (old) {
990 /* Dodgy. Conflicting binding. */
991 vg_assert(old->from_addr == act.from_addr);
992 if (old->to_addr != act.to_addr) {
993 /* We've got a conflicting binding -- that is, from_addr is
994 specified to redirect to two different destinations,
995 old->to_addr and act.to_addr. If we can prove that they
996 are behaviourally equivalent then that's no problem. So
997 we can look at the behavioural eclass tags for both
998 functions to see if that's so. If they are equal, and
999 nonzero, then that's fine. But if not, we can't show they
1000 are equivalent, so we have to complain, and ignore the new
1001 binding. */
1002 vg_assert(old->becTag >= 0 && old->becTag <= 9999);
1003 vg_assert(old->becPrio >= 0 && old->becPrio <= 9);
1004 vg_assert(act.becTag >= 0 && act.becTag <= 9999);
1005 vg_assert(act.becPrio >= 0 && act.becPrio <= 9);
1006 if (old->becTag == 0)
1007 vg_assert(old->becPrio == 0);
1008 if (act.becTag == 0)
1009 vg_assert(act.becPrio == 0);
1011 if (old->becTag == 0 || act.becTag == 0 || old->becTag != act.becTag) {
1012 /* We can't show that they are equivalent. Complain and
1013 ignore. */
1014 what = "new redirection conflicts with existing -- ignoring it";
1015 goto bad;
1017 /* They have the same eclass tag. Use the priorities to
1018 resolve the ambiguity. */
1019 if (act.becPrio <= old->becPrio) {
1020 /* The new one doesn't have a higher priority, so just
1021 ignore it. */
1022 if (VG_(clo_verbosity) > 2) {
1023 VG_(message)(Vg_UserMsg, "Ignoring %s redirection:\n",
1024 act.becPrio < old->becPrio ? "lower priority"
1025 : "duplicate");
1026 show_active( " old: ", old);
1027 show_active( " new: ", &act);
1029 } else {
1030 /* The tricky case. The new one has a higher priority, so
1031 we need to get the old one out of the OSet and install
1032 this one in its place. */
1033 if (VG_(clo_verbosity) > 1) {
1034 VG_(message)(Vg_UserMsg,
1035 "Preferring higher priority redirection:\n");
1036 show_active( " old: ", old);
1037 show_active( " new: ", &act);
1039 add_act = True;
1040 void* oldNd = VG_(OSetGen_Remove)( activeSet, &act.from_addr );
1041 vg_assert(oldNd == old);
1042 VG_(OSetGen_FreeNode)( activeSet, old );
1043 old = NULL;
1045 } else {
1046 /* This appears to be a duplicate of an existing binding.
1047 Safe(ish) -- ignore. */
1048 /* XXXXXXXXXXX COMPLAIN if new and old parents differ */
1051 } else {
1052 /* There's no previous binding for this from_addr, so we must
1053 add 'act' to the active set. */
1054 add_act = True;
1057 /* So, finally, actually add it. */
1058 if (add_act) {
1059 Active* a = VG_(OSetGen_AllocNode)(activeSet, sizeof(Active));
1060 vg_assert(a);
1061 *a = act;
1062 VG_(OSetGen_Insert)(activeSet, a);
1063 /* Now that a new from->to redirection is in force, we need to
1064 get rid of any translations intersecting 'from' in order that
1065 they get redirected to 'to'. So discard them. Just for
1066 paranoia (but, I believe, unnecessarily), discard 'to' as
1067 well. */
1068 VG_(discard_translations)( act.from_addr, 1,
1069 "redir_new_DebugInfo(from_addr)");
1070 VG_(discard_translations)( act.to_addr, 1,
1071 "redir_new_DebugInfo(to_addr)");
1072 if (VG_(clo_verbosity) > 2) {
1073 VG_(message)(Vg_UserMsg, "Adding active redirection:\n");
1074 show_active( " new: ", &act);
1077 return;
1079 bad:
1080 vg_assert(what);
1081 vg_assert(!add_act);
1082 if (VG_(clo_verbosity) > 1) {
1083 VG_(message)(Vg_UserMsg, "WARNING: %s\n", what);
1084 if (old) {
1085 show_active( " old: ", old);
1087 show_active( " new: ", &act);
1092 /* Notify m_redir of the deletion of a DebugInfo. This is relatively
1093 simple -- just get rid of all actives derived from it, and free up
1094 the associated list elements. */
1096 void VG_(redir_notify_delete_DebugInfo)( const DebugInfo* delsi )
1098 TopSpec* ts;
1099 TopSpec* tsPrev;
1100 Spec* sp;
1101 Spec* sp_next;
1102 OSet* tmpSet;
1103 Active* act;
1104 Bool delMe;
1105 Addr addr;
1107 vg_assert(delsi);
1109 /* Search for it, and make tsPrev point to the previous entry, if
1110 any. */
1111 tsPrev = NULL;
1112 ts = topSpecs;
1113 while (True) {
1114 if (ts == NULL) break;
1115 if (ts->seginfo == delsi) break;
1116 tsPrev = ts;
1117 ts = ts->next;
1120 vg_assert(ts); /* else we don't have the deleted DebugInfo */
1121 vg_assert(ts->seginfo == delsi);
1123 /* Traverse the actives, copying the addresses of those we intend
1124 to delete into tmpSet. */
1125 tmpSet = VG_(OSetWord_Create)(dinfo_zalloc, "redir.rndD.1", dinfo_free);
1127 ts->mark = True;
1129 VG_(OSetGen_ResetIter)( activeSet );
1130 while ( (act = VG_(OSetGen_Next)(activeSet)) ) {
1131 delMe = act->parent_spec != NULL
1132 && act->parent_sym != NULL
1133 && act->parent_spec->seginfo != NULL
1134 && act->parent_sym->seginfo != NULL
1135 && (act->parent_spec->mark || act->parent_sym->mark);
1137 /* While we're at it, a bit of paranoia: delete any actives
1138 which don't have both feet in valid client executable areas.
1139 But don't delete hardwired-at-startup ones; these are denoted
1140 by having parent_spec or parent_sym being NULL. */
1141 if ( (!delMe)
1142 && act->parent_spec != NULL
1143 && act->parent_sym != NULL ) {
1144 if (!is_plausible_guest_addr(act->from_addr))
1145 delMe = True;
1146 if (!is_plausible_guest_addr(act->to_addr))
1147 delMe = True;
1150 if (delMe) {
1151 VG_(OSetWord_Insert)( tmpSet, act->from_addr );
1152 /* While we have our hands on both the 'from' and 'to'
1153 of this Active, do paranoid stuff with tt/tc. */
1154 VG_(discard_translations)( act->from_addr, 1,
1155 "redir_del_DebugInfo(from_addr)");
1156 VG_(discard_translations)( act->to_addr, 1,
1157 "redir_del_DebugInfo(to_addr)");
1161 /* Now traverse tmpSet, deleting corresponding elements in activeSet. */
1162 VG_(OSetWord_ResetIter)( tmpSet );
1163 while ( VG_(OSetWord_Next)(tmpSet, &addr) ) {
1164 act = VG_(OSetGen_Remove)( activeSet, &addr );
1165 vg_assert(act);
1166 VG_(OSetGen_FreeNode)( activeSet, act );
1169 VG_(OSetWord_Destroy)( tmpSet );
1171 /* The Actives set is now cleaned up. Free up this TopSpec and
1172 everything hanging off it. */
1173 for (sp = ts->specs; sp; sp = sp_next) {
1174 if (sp->from_sopatt) dinfo_free(sp->from_sopatt);
1175 if (sp->from_fnpatt) dinfo_free(sp->from_fnpatt);
1176 sp_next = sp->next;
1177 dinfo_free(sp);
1180 if (tsPrev == NULL) {
1181 /* first in list */
1182 topSpecs = ts->next;
1183 } else {
1184 tsPrev->next = ts->next;
1186 dinfo_free(ts);
1188 if (VG_(clo_trace_redir))
1189 show_redir_state("after VG_(redir_notify_delete_DebugInfo)");
1193 /*------------------------------------------------------------*/
1194 /*--- QUERIES (really the whole point of this module) ---*/
1195 /*------------------------------------------------------------*/
1197 /* This is the crucial redirection function. It answers the question:
1198 should this code address be redirected somewhere else? It's used
1199 just before translating a basic block. */
1200 Addr VG_(redir_do_lookup) ( Addr orig, Bool* isWrap )
1202 Active* r = VG_(OSetGen_Lookup)(activeSet, &orig);
1203 if (r == NULL) {
1204 if (isWrap) *isWrap = False;
1205 return orig;
1208 vg_assert(r->to_addr != 0);
1209 if (isWrap)
1210 *isWrap = r->isWrap || r->isIFunc;
1211 if (r->isIFunc) {
1212 vg_assert(iFuncWrapper);
1213 return iFuncWrapper;
1215 return r->to_addr;
1218 /* Does the soname represent a dynamic (runtime) linker?
1219 Considers various VG_U_LD* entries from pub_tool_redir.h. */
1220 Bool VG_(is_soname_ld_so) (const HChar *soname)
1222 # if defined(VGO_linux)
1223 if (VG_STREQ(soname, VG_U_LD_LINUX_SO_3)) return True;
1224 if (VG_STREQ(soname, VG_U_LD_LINUX_SO_2)) return True;
1225 if (VG_STREQ(soname, VG_U_LD_LINUX_X86_64_SO_2)) return True;
1226 if (VG_STREQ(soname, VG_U_LD64_SO_1)) return True;
1227 if (VG_STREQ(soname, VG_U_LD64_SO_2)) return True;
1228 if (VG_STREQ(soname, VG_U_LD_SO_1)) return True;
1229 if (VG_STREQ(soname, VG_U_LD_LINUX_AARCH64_SO_1)) return True;
1230 if (VG_STREQ(soname, VG_U_LD_LINUX_ARMHF_SO_3)) return True;
1231 if (VG_STREQ(soname, VG_U_LD_LINUX_MIPSN8_S0_1)) return True;
1232 # elif defined(VGO_freebsd)
1233 if (VG_STREQ(soname, VG_U_LD_ELF_SO_1)) return True;
1234 if (VG_STREQ(soname, VG_U_LD_ELF32_SO_1)) return True;
1235 # elif defined(VGO_darwin)
1236 if (VG_STREQ(soname, VG_U_DYLD)) return True;
1237 # elif defined(VGO_solaris)
1238 if (VG_STREQ(soname, VG_U_LD_SO_1)) return True;
1239 # else
1240 # error "Unsupported OS"
1241 # endif
1243 return False;
1246 /*------------------------------------------------------------*/
1247 /*--- INITIALISATION ---*/
1248 /*------------------------------------------------------------*/
1250 /* Add a never-delete-me Active. */
1252 __attribute__((unused)) /* only used on amd64 */
1253 static void add_hardwired_active ( Addr from, Addr to )
1255 Active act;
1256 act.from_addr = from;
1257 act.to_addr = to;
1258 act.parent_spec = NULL;
1259 act.parent_sym = NULL;
1260 act.becTag = 0; /* "not equivalent to any other fn" */
1261 act.becPrio = 0; /* mandatory when becTag == 0 */
1262 act.isWrap = False;
1263 act.isIFunc = False;
1264 maybe_add_active( act );
1268 /* Add a never-delete-me Spec. This is a bit of a kludge. On the
1269 assumption that this is called only at startup, only handle the
1270 case where topSpecs is completely empty, or if it isn't, it has
1271 just one entry and that is the one with NULL seginfo -- that is the
1272 entry that holds these initial specs. */
1274 __attribute__((unused)) /* not used on all platforms */
1275 static void add_hardwired_spec (const HChar* sopatt, const HChar* fnpatt,
1276 Addr to_addr,
1277 const HChar** mandatory )
1279 Spec* spec = dinfo_zalloc("redir.ahs.1", sizeof(Spec));
1281 if (topSpecs == NULL) {
1282 topSpecs = dinfo_zalloc("redir.ahs.2", sizeof(TopSpec));
1283 /* symtab_zalloc sets all fields to zero */
1286 vg_assert(topSpecs != NULL);
1287 vg_assert(topSpecs->next == NULL);
1288 vg_assert(topSpecs->seginfo == NULL);
1289 /* FIXED PARTS */
1290 /* Note, that these CONST_CAST will not cause a problem, in the sense
1291 that VG_(redir_notify_delete_DebugInfo) will delete them. The reason
1292 is that the TopSpec here has seginfo == NULL and such a TopSpec will
1293 never be freed. See the asserts at the beginning of said function. */
1294 spec->from_sopatt = CONST_CAST(HChar *,sopatt);
1295 spec->from_fnpatt = CONST_CAST(HChar *,fnpatt);
1296 spec->to_addr = to_addr;
1297 spec->isWrap = False;
1298 spec->isGlobal = False;
1299 spec->mandatory = mandatory;
1300 /* VARIABLE PARTS */
1301 spec->mark = False; /* not significant */
1302 spec->done = False; /* not significant */
1304 spec->next = topSpecs->specs;
1305 topSpecs->specs = spec;
1309 __attribute__((unused)) /* not used on all platforms */
1310 static const HChar* complain_about_stripped_glibc_ldso[]
1311 = { "Possible fixes: (1, short term): install glibc's debuginfo",
1312 "package on this machine. (2, longer term): ask the packagers",
1313 "for your Linux distribution to please in future ship a non-",
1314 "stripped ld.so (or whatever the dynamic linker .so is called)",
1315 "that exports the above-named function using the standard",
1316 "calling conventions for this platform. The package you need",
1317 "to install for fix (1) is called",
1319 " On Debian, Ubuntu: libc6-dbg",
1320 " On SuSE, openSuSE, Fedora, RHEL: glibc-debuginfo",
1322 "Note that if you are debugging a 32 bit process on a",
1323 "64 bit system, you will need a corresponding 32 bit debuginfo",
1324 "package (e.g. libc6-dbg:i386).",
1325 NULL
1329 /* Initialise the redir system, and create the initial Spec list and
1330 for amd64-linux a couple of permanent active mappings. The initial
1331 Specs are not converted into Actives yet, on the (checked)
1332 assumption that no DebugInfos have so far been created, and so when
1333 they are created, that will happen. */
1335 void VG_(redir_initialise) ( void )
1337 // Assert that there are no DebugInfos so far
1338 vg_assert( VG_(next_DebugInfo)(NULL) == NULL );
1340 // Initialise active mapping.
1341 activeSet = VG_(OSetGen_Create)(offsetof(Active, from_addr),
1342 NULL, // Use fast comparison
1343 dinfo_zalloc,
1344 "redir.ri.1",
1345 dinfo_free);
1347 // The rest of this function just adds initial Specs.
1349 # if defined(VGP_x86_linux)
1350 /* If we're using memcheck, use this intercept right from the
1351 start, otherwise ld.so (glibc-2.3.5) makes a lot of noise. */
1352 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1353 const HChar** mandatory;
1354 # ifndef GLIBC_MANDATORY_INDEX_AND_STRLEN_REDIRECT
1355 mandatory = NULL;
1356 # else
1357 /* for glibc-2.12 and later, this is mandatory - can't sanely
1358 continue without it */
1359 mandatory = complain_about_stripped_glibc_ldso;
1360 # endif
1361 add_hardwired_spec(
1362 "ld-linux.so.2", "index",
1363 (Addr)&VG_(x86_linux_REDIR_FOR_index), mandatory);
1364 add_hardwired_spec(
1365 "ld-linux.so.2", "strlen",
1366 (Addr)&VG_(x86_linux_REDIR_FOR_strlen), mandatory);
1369 # elif defined(VGP_amd64_linux)
1370 /* Redirect vsyscalls to local versions */
1371 add_hardwired_active(
1372 0xFFFFFFFFFF600000ULL,
1373 (Addr)&VG_(amd64_linux_REDIR_FOR_vgettimeofday)
1375 add_hardwired_active(
1376 0xFFFFFFFFFF600400ULL,
1377 (Addr)&VG_(amd64_linux_REDIR_FOR_vtime)
1379 add_hardwired_active(
1380 0xFFFFFFFFFF600800ULL,
1381 (Addr)&VG_(amd64_linux_REDIR_FOR_vgetcpu)
1384 /* If we're using memcheck, use these intercepts right from
1385 the start, otherwise ld.so makes a lot of noise. */
1386 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1388 add_hardwired_spec(
1389 "ld-linux-x86-64.so.2", "index",
1390 (Addr)&VG_(amd64_linux_REDIR_FOR_index), NULL);
1391 add_hardwired_spec(
1392 "ld-linux-x86-64.so.2", "strlen",
1393 (Addr)&VG_(amd64_linux_REDIR_FOR_strlen),
1394 # ifndef GLIBC_MANDATORY_STRLEN_REDIRECT
1395 NULL
1396 # else
1397 /* for glibc-2.10 and later, this is mandatory - can't sanely
1398 continue without it */
1399 complain_about_stripped_glibc_ldso
1400 # endif
1404 # elif defined(VGP_ppc32_linux)
1405 /* If we're using memcheck, use these intercepts right from
1406 the start, otherwise ld.so makes a lot of noise. */
1407 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1409 /* this is mandatory - can't sanely continue without it */
1410 add_hardwired_spec(
1411 "ld.so.1", "strlen",
1412 (Addr)&VG_(ppc32_linux_REDIR_FOR_strlen),
1413 complain_about_stripped_glibc_ldso
1415 add_hardwired_spec(
1416 "ld.so.1", "strcmp",
1417 (Addr)&VG_(ppc32_linux_REDIR_FOR_strcmp),
1418 NULL /* not mandatory - so why bother at all? */
1419 /* glibc-2.6.1 (openSUSE 10.3, ppc32) seems fine without it */
1421 add_hardwired_spec(
1422 "ld.so.1", "index",
1423 (Addr)&VG_(ppc32_linux_REDIR_FOR_strchr),
1424 NULL /* not mandatory - so why bother at all? */
1425 /* glibc-2.6.1 (openSUSE 10.3, ppc32) seems fine without it */
1429 # elif defined(VGP_ppc64be_linux)
1430 /* If we're using memcheck, use these intercepts right from
1431 the start, otherwise ld.so makes a lot of noise. */
1432 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1434 /* this is mandatory - can't sanely continue without it */
1435 add_hardwired_spec(
1436 "ld64.so.1", "strlen",
1437 (Addr)VG_(fnptr_to_fnentry)( &VG_(ppc64_linux_REDIR_FOR_strlen) ),
1438 complain_about_stripped_glibc_ldso
1441 add_hardwired_spec(
1442 "ld64.so.1", "index",
1443 (Addr)VG_(fnptr_to_fnentry)( &VG_(ppc64_linux_REDIR_FOR_strchr) ),
1444 NULL /* not mandatory - so why bother at all? */
1445 /* glibc-2.5 (FC6, ppc64) seems fine without it */
1449 # elif defined(VGP_ppc64le_linux)
1450 /* If we're using memcheck, use these intercepts right from
1451 * the start, otherwise ld.so makes a lot of noise.
1453 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1455 /* this is mandatory - can't sanely continue without it */
1456 add_hardwired_spec(
1457 "ld64.so.2", "strlen",
1458 (Addr)&VG_(ppc64_linux_REDIR_FOR_strlen),
1459 complain_about_stripped_glibc_ldso
1462 add_hardwired_spec(
1463 "ld64.so.2", "index",
1464 (Addr)&VG_(ppc64_linux_REDIR_FOR_strchr),
1465 NULL /* not mandatory - so why bother at all? */
1466 /* glibc-2.5 (FC6, ppc64) seems fine without it */
1470 # elif defined(VGP_arm_linux)
1471 /* If we're using memcheck, use these intercepts right from the
1472 start, otherwise ld.so makes a lot of noise. In most ARM-linux
1473 distros, ld.so's soname is ld-linux.so.3, but Ubuntu 14.04 on
1474 Odroid uses ld-linux-armhf.so.3 for some reason. */
1475 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1476 /* strlen */
1477 add_hardwired_spec(
1478 "ld-linux.so.3", "strlen",
1479 (Addr)&VG_(arm_linux_REDIR_FOR_strlen),
1480 complain_about_stripped_glibc_ldso
1482 add_hardwired_spec(
1483 "ld-linux-armhf.so.3", "strlen",
1484 (Addr)&VG_(arm_linux_REDIR_FOR_strlen),
1485 complain_about_stripped_glibc_ldso
1487 /* memcpy */
1488 add_hardwired_spec(
1489 "ld-linux.so.3", "memcpy",
1490 (Addr)&VG_(arm_linux_REDIR_FOR_memcpy),
1491 complain_about_stripped_glibc_ldso
1493 add_hardwired_spec(
1494 "ld-linux-armhf.so.3", "memcpy",
1495 (Addr)&VG_(arm_linux_REDIR_FOR_memcpy),
1496 complain_about_stripped_glibc_ldso
1498 /* strcmp */
1499 add_hardwired_spec(
1500 "ld-linux.so.3", "strcmp",
1501 (Addr)&VG_(arm_linux_REDIR_FOR_strcmp),
1502 complain_about_stripped_glibc_ldso
1504 add_hardwired_spec(
1505 "ld-linux-armhf.so.3", "strcmp",
1506 (Addr)&VG_(arm_linux_REDIR_FOR_strcmp),
1507 complain_about_stripped_glibc_ldso
1509 /* index */
1510 add_hardwired_spec(
1511 "ld-linux.so.3", "index",
1512 (Addr)&VG_(arm_linux_REDIR_FOR_index),
1513 complain_about_stripped_glibc_ldso
1515 add_hardwired_spec(
1516 "ld-linux-armhf.so.3", "index",
1517 (Addr)&VG_(arm_linux_REDIR_FOR_index),
1518 complain_about_stripped_glibc_ldso
1522 # elif defined(VGP_arm64_linux)
1523 /* If we're using memcheck, use these intercepts right from
1524 the start, otherwise ld.so makes a lot of noise. */
1525 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1526 add_hardwired_spec(
1527 "ld-linux-aarch64.so.1", "strlen",
1528 (Addr)&VG_(arm64_linux_REDIR_FOR_strlen),
1529 complain_about_stripped_glibc_ldso
1531 add_hardwired_spec(
1532 "ld-linux-aarch64.so.1", "index",
1533 (Addr)&VG_(arm64_linux_REDIR_FOR_index),
1534 NULL
1536 add_hardwired_spec(
1537 "ld-linux-aarch64.so.1", "strcmp",
1538 (Addr)&VG_(arm64_linux_REDIR_FOR_strcmp),
1539 NULL
1541 # if defined(VGPV_arm64_linux_android)
1542 add_hardwired_spec(
1543 "NONE", "__dl_strlen", // in /system/bin/linker64
1544 (Addr)&VG_(arm64_linux_REDIR_FOR_strlen),
1545 NULL
1547 # endif
1550 # elif defined(VGP_x86_freebsd) || defined(VGP_amd64_freebsd)
1551 /* XXX do something real if needed */
1552 # elif defined(VGP_x86_darwin)
1553 /* If we're using memcheck, use these intercepts right from
1554 the start, otherwise dyld makes a lot of noise. */
1555 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1556 add_hardwired_spec("dyld", "strcmp",
1557 (Addr)&VG_(x86_darwin_REDIR_FOR_strcmp), NULL);
1558 add_hardwired_spec("dyld", "strlen",
1559 (Addr)&VG_(x86_darwin_REDIR_FOR_strlen), NULL);
1560 add_hardwired_spec("dyld", "strcat",
1561 (Addr)&VG_(x86_darwin_REDIR_FOR_strcat), NULL);
1562 add_hardwired_spec("dyld", "strcpy",
1563 (Addr)&VG_(x86_darwin_REDIR_FOR_strcpy), NULL);
1564 add_hardwired_spec("dyld", "strlcat",
1565 (Addr)&VG_(x86_darwin_REDIR_FOR_strlcat), NULL);
1568 # elif defined(VGP_amd64_darwin)
1569 /* If we're using memcheck, use these intercepts right from
1570 the start, otherwise dyld makes a lot of noise. */
1571 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1572 add_hardwired_spec("dyld", "strcmp",
1573 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcmp), NULL);
1574 add_hardwired_spec("dyld", "strlen",
1575 (Addr)&VG_(amd64_darwin_REDIR_FOR_strlen), NULL);
1576 add_hardwired_spec("dyld", "strcat",
1577 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcat), NULL);
1578 add_hardwired_spec("dyld", "strcpy",
1579 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcpy), NULL);
1580 add_hardwired_spec("dyld", "strlcat",
1581 (Addr)&VG_(amd64_darwin_REDIR_FOR_strlcat), NULL);
1582 // DDD: #warning fixme rdar://6166275
1583 add_hardwired_spec("dyld", "arc4random",
1584 (Addr)&VG_(amd64_darwin_REDIR_FOR_arc4random), NULL);
1585 # if DARWIN_VERS == DARWIN_10_9
1586 add_hardwired_spec("dyld", "strchr",
1587 (Addr)&VG_(amd64_darwin_REDIR_FOR_strchr), NULL);
1588 # endif
1591 # elif defined(VGP_s390x_linux)
1592 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1593 // added in rsponse to BZ 327943
1594 add_hardwired_spec("ld64.so.1", "index",
1595 (Addr)&VG_(s390x_linux_REDIR_FOR_index),
1596 complain_about_stripped_glibc_ldso);
1599 # elif defined(VGP_mips32_linux)
1600 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1601 /* this is mandatory - can't sanely continue without it */
1602 add_hardwired_spec(
1603 "ld.so.1", "strlen",
1604 (Addr)&VG_(mips32_linux_REDIR_FOR_strlen),
1605 complain_about_stripped_glibc_ldso
1607 add_hardwired_spec(
1608 "ld.so.1", "index",
1609 (Addr)&VG_(mips32_linux_REDIR_FOR_index),
1610 complain_about_stripped_glibc_ldso
1612 # if defined(VGPV_mips32_linux_android)
1613 add_hardwired_spec(
1614 "NONE", "__dl_strlen",
1615 (Addr)&VG_(mips32_linux_REDIR_FOR_strlen),
1616 NULL
1618 # endif
1621 # elif defined(VGP_mips64_linux)
1622 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1623 /* this is mandatory - can't sanely continue without it */
1624 add_hardwired_spec(
1625 "ld.so.1", "strlen",
1626 (Addr)&VG_(mips64_linux_REDIR_FOR_strlen),
1627 complain_about_stripped_glibc_ldso
1629 add_hardwired_spec(
1630 "ld.so.1", "index",
1631 (Addr)&VG_(mips64_linux_REDIR_FOR_index),
1632 complain_about_stripped_glibc_ldso
1634 # if defined(VGABI_64)
1635 add_hardwired_spec(
1636 "ld-linux-mipsn8.so.1", "strlen",
1637 (Addr)&VG_(mips64_linux_REDIR_FOR_strlen),
1638 complain_about_stripped_glibc_ldso
1640 add_hardwired_spec(
1641 "ld-linux-mipsn8.so.1", "index",
1642 (Addr)&VG_(mips64_linux_REDIR_FOR_index),
1643 complain_about_stripped_glibc_ldso
1645 # elif defined(VGABI_N32)
1646 add_hardwired_spec(
1647 "ld.so.1", "strchr",
1648 (Addr)&VG_(mips64_linux_REDIR_FOR_index),
1649 complain_about_stripped_glibc_ldso
1651 # else
1652 # error unknown mips64 ABI
1653 # endif
1656 # elif defined(VGP_nanomips_linux)
1657 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1659 add_hardwired_spec(
1660 "ld.so.1", "strlen",
1661 (Addr)&VG_(nanomips_linux_REDIR_FOR_strlen),
1662 complain_about_stripped_glibc_ldso
1664 add_hardwired_spec(
1665 "ld.so.1", "index",
1666 (Addr)&VG_(nanomips_linux_REDIR_FOR_index),
1667 complain_about_stripped_glibc_ldso
1671 # elif defined(VGP_x86_solaris)
1672 /* If we're using memcheck, use these intercepts right from
1673 the start, otherwise ld.so makes a lot of noise. */
1674 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1675 add_hardwired_spec("/lib/ld.so.1", "strcmp",
1676 (Addr)&VG_(x86_solaris_REDIR_FOR_strcmp), NULL);
1678 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1679 add_hardwired_spec("/lib/ld.so.1", "strlen",
1680 (Addr)&VG_(x86_solaris_REDIR_FOR_strlen), NULL);
1683 # elif defined(VGP_amd64_solaris)
1684 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1685 add_hardwired_spec("/lib/amd64/ld.so.1", "strcpy",
1686 (Addr)&VG_(amd64_solaris_REDIR_FOR_strcpy), NULL);
1688 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1689 add_hardwired_spec("/lib/amd64/ld.so.1", "strncpy",
1690 (Addr)&VG_(amd64_solaris_REDIR_FOR_strncpy), NULL);
1692 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1693 add_hardwired_spec("/lib/amd64/ld.so.1", "strcmp",
1694 (Addr)&VG_(amd64_solaris_REDIR_FOR_strcmp), NULL);
1696 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1697 add_hardwired_spec("/lib/amd64/ld.so.1", "strcat",
1698 (Addr)&VG_(amd64_solaris_REDIR_FOR_strcat), NULL);
1700 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1701 add_hardwired_spec("/lib/amd64/ld.so.1", "strlen",
1702 (Addr)&VG_(amd64_solaris_REDIR_FOR_strlen), NULL);
1705 # else
1706 # error Unknown platform
1707 # endif
1709 if (VG_(clo_trace_redir))
1710 show_redir_state("after VG_(redir_initialise)");
1714 /*------------------------------------------------------------*/
1715 /*--- MISC HELPERS ---*/
1716 /*------------------------------------------------------------*/
1718 static void* dinfo_zalloc(const HChar* ec, SizeT n) {
1719 void* p;
1720 vg_assert(n > 0);
1721 p = VG_(arena_malloc)(VG_AR_DINFO, ec, n);
1722 VG_(memset)(p, 0, n);
1723 return p;
1726 static void dinfo_free(void* p) {
1727 vg_assert(p);
1728 return VG_(arena_free)(VG_AR_DINFO, p);
1731 static HChar* dinfo_strdup(const HChar* ec, const HChar* str)
1733 return VG_(arena_strdup)(VG_AR_DINFO, ec, str);
1736 /* Really this should be merged with translations_allowable_from_seg
1737 in m_translate. */
1738 static Bool is_plausible_guest_addr(Addr a)
1740 NSegment const* seg = VG_(am_find_nsegment)(a);
1741 return seg != NULL
1742 && (seg->kind == SkAnonC || seg->kind == SkFileC ||
1743 seg->kind == SkShmC)
1744 && (seg->hasX || seg->hasR); /* crude x86-specific hack */
1748 /*------------------------------------------------------------*/
1749 /*--- NOTIFY-ON-LOAD FUNCTIONS ---*/
1750 /*------------------------------------------------------------*/
1752 static
1753 void handle_maybe_load_notifier( const HChar* soname,
1754 const HChar* symbol, Addr addr )
1756 # if defined(VGP_x86_linux)
1757 /* x86-linux only: if we see _dl_sysinfo_int80, note its address.
1758 See comment on declaration of VG_(client__dl_sysinfo_int80) for
1759 the reason. As far as I can tell, the relevant symbol is always
1760 in object with soname "ld-linux.so.2". */
1761 if (symbol && symbol[0] == '_'
1762 && 0 == VG_(strcmp)(symbol, "_dl_sysinfo_int80")
1763 && 0 == VG_(strcmp)(soname, "ld-linux.so.2")) {
1764 if (VG_(client__dl_sysinfo_int80) == 0)
1765 VG_(client__dl_sysinfo_int80) = addr;
1767 # endif
1769 /* Normal load-notifier handling after here. First, ignore all
1770 symbols lacking the right prefix. */
1771 vg_assert(symbol); // assert rather than segfault if it is NULL
1772 if (0 != VG_(strncmp)(symbol, VG_NOTIFY_ON_LOAD_PREFIX,
1773 VG_NOTIFY_ON_LOAD_PREFIX_LEN))
1774 /* Doesn't have the right prefix */
1775 return;
1777 if (VG_(strcmp)(symbol, VG_STRINGIFY(VG_NOTIFY_ON_LOAD(freeres))) == 0)
1778 VG_(client_freeres_wrapper) = addr;
1779 else
1780 if (VG_(strcmp)(symbol, VG_STRINGIFY(VG_NOTIFY_ON_LOAD(ifunc_wrapper))) == 0)
1781 iFuncWrapper = addr;
1782 else
1783 vg_assert2(0, "unrecognised load notification function: %s", symbol);
1787 /*------------------------------------------------------------*/
1788 /*--- REQUIRE-TEXT-SYMBOL HANDLING ---*/
1789 /*------------------------------------------------------------*/
1791 /* In short: check that the currently-being-loaded object has text
1792 symbols that satisfy any --require-text-symbol= specifications that
1793 apply to it, and abort the run with an error message if not.
1795 static void handle_require_text_symbols ( const DebugInfo* di )
1797 /* First thing to do is figure out which, if any,
1798 --require-text-symbol specification strings apply to this
1799 object. Most likely none do, since it is not expected to
1800 frequently be used. Work through the list of specs and
1801 accumulate in fnpatts[] the fn patterns that pertain to this
1802 object. */
1803 XArray *fnpatts = VG_(newXA)( VG_(malloc), "m_redir.hrts.5",
1804 VG_(free), sizeof(HChar*) );
1806 Int i, j;
1807 const HChar* di_soname = VG_(DebugInfo_get_soname)(di);
1808 vg_assert(di_soname); // must be present
1810 for (i = 0; i < VG_(sizeXA)(VG_(clo_req_tsyms)); i++) {
1811 const HChar* clo_spec = *(HChar**) VG_(indexXA)(VG_(clo_req_tsyms), i);
1812 vg_assert(clo_spec && VG_(strlen)(clo_spec) >= 4);
1813 // clone the spec, so we can stick a zero at the end of the sopatt
1814 HChar *spec = VG_(strdup)("m_redir.hrts.1", clo_spec);
1815 HChar sep = spec[0];
1816 HChar* sopatt = &spec[1];
1817 HChar* fnpatt = VG_(strchr)(sopatt, sep);
1818 // the initial check at clo processing in time in m_main
1819 // should ensure this.
1820 vg_assert(fnpatt && *fnpatt == sep);
1821 *fnpatt = 0;
1822 fnpatt++;
1823 if (VG_(string_match)(sopatt, di_soname)) {
1824 HChar *pattern = VG_(strdup)("m_redir.hrts.2", fnpatt);
1825 VG_(addToXA)(fnpatts, &pattern);
1827 VG_(free)(spec);
1830 if (VG_(sizeXA)(fnpatts) == 0) {
1831 VG_(deleteXA)(fnpatts);
1832 return; /* no applicable spec strings */
1835 /* So finally, fnpatts contains the set of
1836 (patterns for) text symbol names that must be found in this
1837 object, in order to continue. That is, we must find at least
1838 one text symbol name that matches each pattern, else we must
1839 abort the run. */
1841 if (0) VG_(printf)("for %s\n", di_soname);
1842 for (i = 0; i < VG_(sizeXA)(fnpatts); i++)
1843 if (0) VG_(printf)(" fnpatt: %s\n",
1844 *(HChar**) VG_(indexXA)(fnpatts, i));
1846 /* For each spec, look through the syms to find one that matches.
1847 This isn't terribly efficient but it happens rarely, so no big
1848 deal. */
1849 for (i = 0; i < VG_(sizeXA)(fnpatts); i++) {
1850 Bool found = False;
1851 const HChar* fnpatt = *(HChar**) VG_(indexXA)(fnpatts, i);
1852 Int nsyms = VG_(DebugInfo_syms_howmany)(di);
1853 for (j = 0; j < nsyms; j++) {
1854 Bool isText = False;
1855 const HChar* sym_name_pri = NULL;
1856 const HChar** sym_names_sec = NULL;
1857 VG_(DebugInfo_syms_getidx)( di, j, NULL,
1858 NULL, &sym_name_pri, &sym_names_sec,
1859 &isText, NULL, NULL );
1860 const HChar* twoslots[2];
1861 const HChar** names_init =
1862 alloc_symname_array(sym_name_pri, sym_names_sec, &twoslots[0]);
1863 const HChar** names;
1864 for (names = names_init; *names; names++) {
1865 /* ignore data symbols */
1866 if (0) VG_(printf)("QQQ %s\n", *names);
1867 vg_assert(sym_name_pri);
1868 if (!isText)
1869 continue;
1870 if (VG_(string_match)(fnpatt, *names)) {
1871 found = True;
1872 break;
1875 free_symname_array(names_init, &twoslots[0]);
1876 if (found)
1877 break;
1880 if (!found) {
1881 const HChar* v = "valgrind: ";
1882 VG_(printf)("\n");
1883 VG_(printf)(
1884 "%sFatal error at when loading library with soname\n", v);
1885 VG_(printf)(
1886 "%s %s\n", v, di_soname);
1887 VG_(printf)(
1888 "%sCannot find any text symbol with a name "
1889 "that matches the pattern\n", v);
1890 VG_(printf)("%s %s\n", v, fnpatt);
1891 VG_(printf)("%sas required by a --require-text-symbol= "
1892 "specification.\n", v);
1893 VG_(printf)("\n");
1894 VG_(printf)(
1895 "%sCannot continue -- exiting now.\n", v);
1896 VG_(printf)("\n");
1897 VG_(exit)(1);
1901 /* All required specs were found. Just free memory and return. */
1902 for (i = 0; i < VG_(sizeXA)(fnpatts); i++)
1903 VG_(free)(*(HChar**) VG_(indexXA)(fnpatts, i));
1904 VG_(deleteXA)(fnpatts);
1908 /*------------------------------------------------------------*/
1909 /*--- SANITY/DEBUG ---*/
1910 /*------------------------------------------------------------*/
1912 static void show_spec ( const HChar* left, const Spec* spec )
1914 VG_(message)( Vg_DebugMsg,
1915 "%s%-25s %-30s %s%s-> (%04d.%d) 0x%08lx\n",
1916 left,
1917 spec->from_sopatt, spec->from_fnpatt,
1918 spec->isWrap ? "W" : "R",
1919 spec->isGlobal ? "G" : "L",
1920 spec->becTag, spec->becPrio,
1921 spec->to_addr );
1924 static void show_active ( const HChar* left, const Active* act )
1926 Bool ok;
1927 const HChar *buf;
1929 DiEpoch ep = VG_(current_DiEpoch)();
1930 ok = VG_(get_fnname_w_offset)(ep, act->from_addr, &buf);
1931 if (!ok) buf = "???";
1932 // Stash away name1
1933 HChar name1[VG_(strlen)(buf) + 1];
1934 VG_(strcpy)(name1, buf);
1936 const HChar *name2;
1937 ok = VG_(get_fnname_w_offset)(ep, act->to_addr, &name2);
1938 if (!ok) name2 = "???";
1940 VG_(message)(Vg_DebugMsg, "%s0x%08lx (%-20s) %s-> (%04d.%d) 0x%08lx %s\n",
1941 left,
1942 act->from_addr, name1,
1943 act->isWrap ? "W" : "R",
1944 act->becTag, act->becPrio,
1945 act->to_addr, name2 );
1948 static void show_redir_state ( const HChar* who )
1950 TopSpec* ts;
1951 Spec* sp;
1952 Active* act;
1953 VG_(message)(Vg_DebugMsg, "<<\n");
1954 VG_(message)(Vg_DebugMsg, " ------ REDIR STATE %s ------\n", who);
1955 for (ts = topSpecs; ts; ts = ts->next) {
1956 if (ts->seginfo)
1957 VG_(message)(Vg_DebugMsg,
1958 " TOPSPECS of soname %s filename %s\n",
1959 VG_(DebugInfo_get_soname)(ts->seginfo),
1960 VG_(DebugInfo_get_filename)(ts->seginfo));
1961 else
1962 VG_(message)(Vg_DebugMsg,
1963 " TOPSPECS of soname (hardwired)\n");
1965 for (sp = ts->specs; sp; sp = sp->next)
1966 show_spec(" ", sp);
1968 VG_(message)(Vg_DebugMsg, " ------ ACTIVE ------\n");
1969 VG_(OSetGen_ResetIter)( activeSet );
1970 while ( (act = VG_(OSetGen_Next)(activeSet)) ) {
1971 show_active(" ", act);
1974 VG_(message)(Vg_DebugMsg, ">>\n");
1977 /*--------------------------------------------------------------------*/
1978 /*--- end ---*/
1979 /*--------------------------------------------------------------------*/