configure.ac: Generalize glibc version check
[valgrind.git] / coregrind / m_redir.c
blob9aee707f1845bccec751bcaad7f9811a9c3ff4c1
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-2013 Julian Seward
11 jseward@acm.org
12 Copyright (C) 2003-2013 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, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 02111-1307, USA.
30 The GNU General Public License is contained in the file COPYING.
33 #include "pub_core_basics.h"
34 #include "pub_core_debuglog.h"
35 #include "pub_core_debuginfo.h"
36 #include "pub_core_libcbase.h"
37 #include "pub_core_libcassert.h"
38 #include "pub_core_libcprint.h"
39 #include "pub_core_vki.h"
40 #include "pub_core_libcfile.h"
41 #include "pub_core_seqmatch.h"
42 #include "pub_core_mallocfree.h"
43 #include "pub_core_options.h"
44 #include "pub_core_oset.h"
45 #include "pub_core_redir.h"
46 #include "pub_core_trampoline.h"
47 #include "pub_core_transtab.h"
48 #include "pub_core_tooliface.h" // VG_(needs).malloc_replacement
49 #include "pub_core_machine.h" // VG_(fnptr_to_fnentry)
50 #include "pub_core_aspacemgr.h" // VG_(am_find_nsegment)
51 #include "pub_core_xarray.h"
52 #include "pub_core_clientstate.h" // VG_(client___libc_freeres_wrapper)
53 #include "pub_core_demangle.h" // VG_(maybe_Z_demangle)
54 #include "pub_core_libcproc.h" // VG_(libdir)
56 #include "config.h" /* GLIBC_MANDATORY_*_REDIRECT */
59 /* This module is a critical part of the redirection/intercept system.
60 It keeps track of the current intercept state, cleans up the
61 translation caches when that state changes, and finally, answers
62 queries about the whether an address is currently redirected or
63 not. It doesn't do any of the control-flow trickery needed to put
64 the redirections into practice. That is the job of m_translate,
65 which calls here to find out which translations need to be
66 redirected.
68 The interface is simple. VG_(redir_initialise) initialises and
69 loads some hardwired redirects which never disappear; this is
70 platform-specific.
72 The module is notified of redirection state changes by m_debuginfo.
73 That calls VG_(redir_notify_new_DebugInfo) when a new DebugInfo
74 (shared object symbol table, basically) appears. Appearance of new
75 symbols can cause new (active) redirections to appear for two
76 reasons: the symbols in the new table may match existing
77 redirection specifications (see comments below), and because the
78 symbols in the new table may themselves supply new redirect
79 specifications which match existing symbols (or ones in the new
80 table).
82 Redirect specifications are really symbols with "funny" prefixes
83 (_vgrNNNNZU_ and _vgrNNNNZZ_). These names tell m_redir that the
84 associated code should replace the standard entry point for some
85 set of functions. The set of functions is specified by a (soname
86 pattern, function name pattern) pair which is encoded in the symbol
87 name following the prefix. The names use a Z-encoding scheme so
88 that they may contain punctuation characters and wildcards (*).
89 The encoding scheme is described in pub_tool_redir.h and is decoded
90 by VG_(maybe_Z_demangle). The NNNN are behavioural equivalence
91 class tags, and are used to by code in this module to resolve
92 situations where one address appears to be redirected to more than
93 one replacement/wrapper. This is also described in
94 pub_tool_redir.h.
96 When a shared object is unloaded, this module learns of it via a
97 call to VG_(redir_notify_delete_DebugInfo). It then removes from
98 its tables all active redirections in any way associated with that
99 object, and tidies up the translation caches accordingly.
101 That takes care of tracking the redirection state. When a
102 translation is actually to be made, m_translate calls to
103 VG_(redir_do_lookup) in this module to find out if the
104 translation's address should be redirected.
107 /*------------------------------------------------------------*/
108 /*--- Semantics ---*/
109 /*------------------------------------------------------------*/
111 /* The redirector holds two pieces of state:
113 Specs - a set of (soname pattern, fnname pattern) -> redir addr
114 Active - a set of orig addr -> (bool, redir addr)
116 Active is the currently active set of bindings that the translator
117 consults. Specs is the current set of specifications as harvested
118 from reading symbol tables of the currently loaded objects.
120 Active is a pure function of Specs and the current symbol table
121 state (maintained by m_debuginfo). Call the latter SyminfoState.
123 Therefore whenever either Specs or SyminfoState changes, Active
124 must be recomputed. [Inefficient if done naively, but this is a
125 spec].
127 Active is computed as follows:
129 Active = empty
130 for spec in Specs {
131 sopatt = spec.soname pattern
132 fnpatt = spec.fnname pattern
133 redir = spec.redir addr
134 for so matching sopatt in SyminfoState {
135 for fn matching fnpatt in fnnames_of(so) {
136 &fn -> redir is added to Active
141 [as an implementation detail, when a binding (orig -> redir) is
142 deleted from Active as a result of recomputing it, then all
143 translations intersecting redir must be deleted. However, this is
144 not part of the spec].
146 [Active also depends on where the aspacemgr has decided to put all
147 the pieces of code -- that affects the "orig addr" and "redir addr"
148 values.]
150 ---------------------
152 That completes the spec, apart from one difficult issue: duplicates.
154 Clearly we must impose the requirement that domain(Active) contains
155 no duplicates. The difficulty is how to constrain Specs enough to
156 avoid getting into that situation. It's easy to write specs which
157 could cause conflicting bindings in Active, eg:
159 (libpthread.so, pthread_mutex_lock) -> a1
160 (libpthread.so, pthread_*) -> a2
162 for a1 != a2. Or even hairier:
164 (libpthread.so, pthread_mutex_*) -> a1
165 (libpthread.so, pthread_*_lock) -> a2
167 I can't think of any sane way of detecting when an addition to
168 Specs would generate conflicts. However, considering we don't
169 actually want to have a system that allows this, I propose this:
170 all changes to Specs are acceptable. But, when recomputing Active
171 following the change, if the same orig is bound to more than one
172 redir, then the first binding for orig is retained, and all the
173 rest ignored.
175 ===========================================================
176 ===========================================================
177 Incremental implementation:
179 When a new DebugInfo appears:
180 - it may be the source of new specs
181 - it may be the source of new matches for existing specs
182 Therefore:
184 - (new Specs x existing DebugInfos): scan all symbols in the new
185 DebugInfo to find new specs. Each of these needs to be compared
186 against all symbols in all the existing DebugInfos to generate
187 new actives.
189 - (existing Specs x new DebugInfo): scan all symbols in the
190 DebugInfo, trying to match them to any existing specs, also
191 generating new actives.
193 - (new Specs x new DebugInfo): scan all symbols in the new
194 DebugInfo, trying to match them against the new specs, to
195 generate new actives.
197 - Finally, add new new specs to the current set of specs.
199 When adding a new active (s,d) to the Actives:
200 lookup s in Actives
201 if already bound to d, ignore
202 if already bound to something other than d, complain loudly and ignore
203 else add (s,d) to Actives
204 and discard (s,1) and (d,1) (maybe overly conservative)
206 When a DebugInfo disappears:
207 - delete all specs acquired from the seginfo
208 - delete all actives derived from the just-deleted specs
209 - if each active (s,d) deleted, discard (s,1) and (d,1)
213 /*------------------------------------------------------------*/
214 /*--- REDIRECTION SPECIFICATIONS ---*/
215 /*------------------------------------------------------------*/
217 /* A specification of a redirection we want to do. Note that because
218 both the "from" soname and function name may contain wildcards, the
219 spec can match an arbitrary number of times.
221 16 Nov 2007: Comments re .mandatory field: The initial motivation
222 for this is making Memcheck work sanely on glibc-2.6.X ppc32-linux.
223 We really need to intercept 'strlen' in ld.so right from startup.
224 If ld.so does not have a visible 'strlen' symbol, Memcheck
225 generates an impossible number of errors resulting from highly
226 tuned strlen implementation in ld.so, and is completely unusable
227 -- the resulting undefinedness eventually seeps everywhere. */
228 typedef
229 struct _Spec {
230 struct _Spec* next; /* linked list */
231 /* FIXED PARTS -- set when created and not changed */
232 HChar* from_sopatt; /* from soname pattern */
233 HChar* from_fnpatt; /* from fnname pattern */
234 Addr to_addr; /* where redirecting to */
235 Bool isWrap; /* wrap or replacement? */
236 Int becTag; /* 0 through 9999. Behavioural equivalance class tag.
237 If two wrappers have the same (non-zero) tag, they
238 are promising that they behave identically. */
239 Int becPrio; /* 0 through 9. Behavioural equivalence class prio.
240 Used to choose between competing wrappers with
241 the same (non-zero) tag. */
242 const HChar** mandatory; /* non-NULL ==> abort V and print the
243 strings if from_sopatt is loaded but
244 from_fnpatt cannot be found */
245 /* VARIABLE PARTS -- used transiently whilst processing redirections */
246 Bool mark; /* set if spec requires further processing */
247 Bool done; /* set if spec was successfully matched */
249 Spec;
251 /* Top-level data structure. It contains a pointer to a DebugInfo and
252 also a list of the specs harvested from that DebugInfo. Note that
253 seginfo is allowed to be NULL, meaning that the specs are
254 pre-loaded ones at startup and are not associated with any
255 particular seginfo. */
256 typedef
257 struct _TopSpec {
258 struct _TopSpec* next; /* linked list */
259 DebugInfo* seginfo; /* symbols etc */
260 Spec* specs; /* specs pulled out of seginfo */
261 Bool mark; /* transient temporary used during deletion */
263 TopSpec;
265 /* This is the top level list of redirections. m_debuginfo maintains
266 a list of DebugInfos, and the idea here is to maintain a list with
267 the same number of elements (in fact, with one more element, so as
268 to record abovementioned preloaded specifications.) */
269 static TopSpec* topSpecs = NULL;
272 /*------------------------------------------------------------*/
273 /*--- CURRENTLY ACTIVE REDIRECTIONS ---*/
274 /*------------------------------------------------------------*/
276 /* Represents a currently active binding. If either parent_spec or
277 parent_sym is NULL, then this binding was hardwired at startup and
278 should not be deleted. Same is true if either parent's seginfo
279 field is NULL. */
280 typedef
281 struct {
282 Addr from_addr; /* old addr -- MUST BE THE FIRST WORD! */
283 Addr to_addr; /* where redirecting to */
284 TopSpec* parent_spec; /* the TopSpec which supplied the Spec */
285 TopSpec* parent_sym; /* the TopSpec which supplied the symbol */
286 Int becTag; /* behavioural eclass tag for ::to_addr */
287 Int becPrio; /* and its priority */
288 Bool isWrap; /* wrap or replacement? */
289 Bool isIFunc; /* indirect function? */
291 Active;
293 /* The active set is a fast lookup table */
294 static OSet* activeSet = NULL;
296 /* Wrapper routine for indirect functions */
297 static Addr iFuncWrapper;
299 /*------------------------------------------------------------*/
300 /*--- FWDses ---*/
301 /*------------------------------------------------------------*/
303 static void maybe_add_active ( Active /*by value; callee copies*/ );
305 static void* dinfo_zalloc(const HChar* ec, SizeT);
306 static void dinfo_free(void*);
307 static HChar* dinfo_strdup(const HChar* ec, const HChar*);
308 static Bool is_plausible_guest_addr(Addr);
310 static void show_redir_state ( const HChar* who );
311 static void show_active ( const HChar* left, Active* act );
313 static void handle_maybe_load_notifier( const HChar* soname,
314 HChar* symbol, Addr addr );
316 static void handle_require_text_symbols ( DebugInfo* );
318 /*------------------------------------------------------------*/
319 /*--- NOTIFICATIONS ---*/
320 /*------------------------------------------------------------*/
322 static
323 void generate_and_add_actives (
324 /* spec list and the owning TopSpec */
325 Spec* specs,
326 TopSpec* parent_spec,
327 /* debuginfo and the owning TopSpec */
328 DebugInfo* di,
329 TopSpec* parent_sym
333 /* Copy all the names from a given symbol into an AR_DINFO allocated,
334 NULL terminated array, for easy iteration. Caller must pass also
335 the address of a 2-entry array which can be used in the common case
336 to avoid dynamic allocation. */
337 static HChar** alloc_symname_array ( HChar* pri_name, HChar** sec_names,
338 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 HChar** pp = sec_names;
350 while (*pp) { n_req++; pp++; }
351 /* .. allocate and copy in. */
352 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 tl_assert(i == n_req);
358 tl_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 ( HChar** names, 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 #define N_DEMANGLED 256
390 void VG_(redir_notify_new_DebugInfo)( DebugInfo* newdi )
392 Bool ok, isWrap;
393 Int i, nsyms, becTag, becPrio;
394 Spec* specList;
395 Spec* spec;
396 TopSpec* ts;
397 TopSpec* newts;
398 HChar* sym_name_pri;
399 HChar** sym_names_sec;
400 SymAVMAs sym_avmas;
401 HChar demangled_sopatt[N_DEMANGLED];
402 HChar demangled_fnpatt[N_DEMANGLED];
403 Bool check_ppcTOCs = False;
404 Bool isText;
405 const HChar* newdi_soname;
406 Bool dehacktivate_pthread_stack_cache_var_search = False;
407 const HChar* const pthread_soname = "libpthread.so.0";
408 const HChar* const pthread_stack_cache_actsize_varname
409 = "stack_cache_actsize";
411 # if defined(VG_PLAT_USES_PPCTOC)
412 check_ppcTOCs = True;
413 # endif
415 vg_assert(newdi);
416 newdi_soname = VG_(DebugInfo_get_soname)(newdi);
417 vg_assert(newdi_soname != NULL);
419 #ifdef ENABLE_INNER
421 /* When an outer Valgrind is executing an inner Valgrind, the
422 inner "sees" in its address space the mmap-ed vgpreload files
423 of the outer. The inner must avoid interpreting the
424 redirections given in the outer vgpreload mmap-ed files.
425 Otherwise, some tool combinations badly fail.
427 Example: outer memcheck tool executing an inner none tool.
429 If inner none interprets the outer malloc redirection, the
430 inner will redirect malloc to a memcheck function it does not
431 have (as the redirection target is from the outer). With
432 such a failed redirection, a call to malloc inside the inner
433 will then result in a "no-operation" (and so no memory will
434 be allocated).
436 When running as an inner, no redirection will be done
437 for a vgpreload file if this file is not located in the
438 inner VALGRIND_LIB directory.
440 Recognising a vgpreload file based on a filename pattern
441 is a kludge. An alternate solution would be to change
442 the _vgr prefix according to outer/inner/client.
444 const HChar* newdi_filename = VG_(DebugInfo_get_filename)(newdi);
445 const HChar* newdi_basename = VG_(basename) (newdi_filename);
446 if (VG_(strncmp) (newdi_basename, "vgpreload_", 10) == 0) {
447 /* This looks like a vgpreload file => check if this file
448 is from the inner VALGRIND_LIB.
449 We do this check using VG_(stat) + dev/inode comparison
450 as vg-in-place defines a VALGRIND_LIB with symlinks
451 pointing to files inside the valgrind build directories. */
452 struct vg_stat newdi_stat;
453 SysRes newdi_res;
454 HChar in_vglib_filename[VKI_PATH_MAX];
455 struct vg_stat in_vglib_stat;
456 SysRes in_vglib_res;
458 newdi_res = VG_(stat)(newdi_filename, &newdi_stat);
460 VG_(strncpy) (in_vglib_filename, VG_(libdir), VKI_PATH_MAX);
461 VG_(strncat) (in_vglib_filename, "/", VKI_PATH_MAX);
462 VG_(strncat) (in_vglib_filename, newdi_basename, VKI_PATH_MAX);
463 in_vglib_res = VG_(stat)(in_vglib_filename, &in_vglib_stat);
465 /* If we find newdi_basename in inner VALGRIND_LIB
466 but newdi_filename is not the same file, then we do
467 not execute the redirection. */
468 if (!sr_isError(in_vglib_res)
469 && !sr_isError(newdi_res)
470 && (newdi_stat.dev != in_vglib_stat.dev
471 || newdi_stat.ino != in_vglib_stat.ino)) {
472 /* <inner VALGRIND_LIB>/newdi_basename is an existing file
473 and is different of newdi_filename.
474 So, we do not execute newdi_filename redirection. */
475 if ( VG_(clo_verbosity) > 1 ) {
476 VG_(message)( Vg_DebugMsg,
477 "Skipping vgpreload redir in %s"
478 " (not from VALGRIND_LIB_INNER)\n",
479 newdi_filename);
481 return;
482 } else {
483 if ( VG_(clo_verbosity) > 1 ) {
484 VG_(message)( Vg_DebugMsg,
485 "Executing vgpreload redir in %s"
486 " (from VALGRIND_LIB_INNER)\n",
487 newdi_filename);
492 #endif
495 /* stay sane: we don't already have this. */
496 for (ts = topSpecs; ts; ts = ts->next)
497 vg_assert(ts->seginfo != newdi);
499 /* scan this DebugInfo's symbol table, pulling out and demangling
500 any specs found */
502 specList = NULL; /* the spec list we're building up */
504 dehacktivate_pthread_stack_cache_var_search =
505 SimHintiS(SimHint_no_nptl_pthread_stackcache, VG_(clo_sim_hints))
506 && 0 == VG_(strcmp)(newdi_soname, pthread_soname);
508 nsyms = VG_(DebugInfo_syms_howmany)( newdi );
509 for (i = 0; i < nsyms; i++) {
510 VG_(DebugInfo_syms_getidx)( newdi, i, &sym_avmas,
511 NULL, &sym_name_pri, &sym_names_sec,
512 &isText, NULL );
513 /* Set up to conveniently iterate over all names for this symbol. */
514 HChar* twoslots[2];
515 HChar** names_init = alloc_symname_array(sym_name_pri, sym_names_sec,
516 &twoslots[0]);
517 HChar** names;
518 for (names = names_init; *names; names++) {
519 ok = VG_(maybe_Z_demangle)( *names,
520 demangled_sopatt, N_DEMANGLED,
521 demangled_fnpatt, N_DEMANGLED,
522 &isWrap, &becTag, &becPrio );
523 /* ignore data symbols */
524 if (!isText) {
525 /* But search for dehacktivate stack cache var if needed. */
526 if (dehacktivate_pthread_stack_cache_var_search
527 && 0 == VG_(strcmp)(*names,
528 pthread_stack_cache_actsize_varname)) {
529 if ( VG_(clo_verbosity) > 1 ) {
530 VG_(message)( Vg_DebugMsg,
531 "deactivate nptl pthread stackcache via kludge:"
532 " found symbol %s at addr %p\n",
533 *names, (void*) sym_avmas.main);
535 VG_(client__stack_cache_actsize__addr) = (SizeT*) sym_avmas.main;
536 dehacktivate_pthread_stack_cache_var_search = False;
538 continue;
540 if (!ok) {
541 /* It's not a full-scale redirect, but perhaps it is a load-notify
542 fn? Let the load-notify department see it. */
543 handle_maybe_load_notifier( newdi_soname, *names, sym_avmas.main );
544 continue;
546 if (check_ppcTOCs && GET_TOCPTR_AVMA(sym_avmas) == 0) {
547 /* This platform uses toc pointers, but none could be found
548 for this symbol, so we can't safely redirect/wrap to it.
549 Just skip it; we'll make a second pass over the symbols in
550 the following loop, and complain at that point. */
551 continue;
554 if (0 == VG_(strncmp) (demangled_sopatt,
555 VG_SO_SYN_PREFIX, VG_SO_SYN_PREFIX_LEN)) {
556 /* This is a redirection for handling lib so synonyms. If we
557 have a matching lib synonym, then replace the sopatt.
558 Otherwise, just ignore this redirection spec. */
560 if (!VG_(clo_soname_synonyms))
561 continue; // No synonyms => skip the redir.
563 /* Search for a matching synonym=newname*/
564 SizeT const sopatt_syn_len
565 = VG_(strlen)(demangled_sopatt+VG_SO_SYN_PREFIX_LEN);
566 HChar const* last = VG_(clo_soname_synonyms);
568 while (*last) {
569 HChar const* first = last;
570 last = advance_to_equal(first);
572 if ((last - first) == sopatt_syn_len
573 && 0 == VG_(strncmp)(demangled_sopatt+VG_SO_SYN_PREFIX_LEN,
574 first,
575 sopatt_syn_len)) {
576 // Found the demangle_sopatt synonym => replace it
577 first = last + 1;
578 last = advance_to_comma(first);
579 VG_(strncpy)(demangled_sopatt, first, last - first);
580 demangled_sopatt[last - first] = '\0';
581 break;
584 last = advance_to_comma(last);
585 if (*last == ',')
586 last++;
589 // If we have not replaced the sopatt, then skip the redir.
590 if (0 == VG_(strncmp) (demangled_sopatt,
591 VG_SO_SYN_PREFIX, VG_SO_SYN_PREFIX_LEN))
592 continue;
595 spec = dinfo_zalloc("redir.rnnD.1", sizeof(Spec));
596 vg_assert(spec);
597 spec->from_sopatt = dinfo_strdup("redir.rnnD.2", demangled_sopatt);
598 spec->from_fnpatt = dinfo_strdup("redir.rnnD.3", demangled_fnpatt);
599 vg_assert(spec->from_sopatt);
600 vg_assert(spec->from_fnpatt);
601 spec->to_addr = sym_avmas.main;
602 spec->isWrap = isWrap;
603 spec->becTag = becTag;
604 spec->becPrio = becPrio;
605 /* check we're not adding manifestly stupid destinations */
606 vg_assert(is_plausible_guest_addr(sym_avmas.main));
607 spec->next = specList;
608 spec->mark = False; /* not significant */
609 spec->done = False; /* not significant */
610 specList = spec;
612 free_symname_array(names_init, &twoslots[0]);
614 if (dehacktivate_pthread_stack_cache_var_search) {
615 VG_(message)(Vg_DebugMsg,
616 "WARNING: could not find symbol for var %s in %s\n",
617 pthread_stack_cache_actsize_varname, pthread_soname);
618 VG_(message)(Vg_DebugMsg,
619 "=> pthread stack cache cannot be disabled!\n");
622 if (check_ppcTOCs) {
623 for (i = 0; i < nsyms; i++) {
624 VG_(DebugInfo_syms_getidx)( newdi, i, &sym_avmas,
625 NULL, &sym_name_pri, &sym_names_sec,
626 &isText, NULL );
627 HChar* twoslots[2];
628 HChar** names_init = alloc_symname_array(sym_name_pri, sym_names_sec,
629 &twoslots[0]);
630 HChar** names;
631 for (names = names_init; *names; names++) {
632 ok = isText
633 && VG_(maybe_Z_demangle)(
634 *names, demangled_sopatt, N_DEMANGLED,
635 demangled_fnpatt, N_DEMANGLED, &isWrap, NULL, NULL );
636 if (!ok)
637 /* not a redirect. Ignore. */
638 continue;
639 if (GET_TOCPTR_AVMA(sym_avmas) != 0)
640 /* has a valid toc pointer. Ignore. */
641 continue;
643 for (spec = specList; spec; spec = spec->next)
644 if (0 == VG_(strcmp)(spec->from_sopatt, demangled_sopatt)
645 && 0 == VG_(strcmp)(spec->from_fnpatt, demangled_fnpatt))
646 break;
647 if (spec)
648 /* a redirect to some other copy of that symbol, which
649 does have a TOC value, already exists */
650 continue;
652 /* Complain */
653 VG_(message)(Vg_DebugMsg,
654 "WARNING: no TOC ptr for redir/wrap to %s %s\n",
655 demangled_sopatt, demangled_fnpatt);
657 free_symname_array(names_init, &twoslots[0]);
661 /* Ok. Now specList holds the list of specs from the DebugInfo.
662 Build a new TopSpec, but don't add it to topSpecs yet. */
663 newts = dinfo_zalloc("redir.rnnD.4", sizeof(TopSpec));
664 vg_assert(newts);
665 newts->next = NULL; /* not significant */
666 newts->seginfo = newdi;
667 newts->specs = specList;
668 newts->mark = False; /* not significant */
670 /* We now need to augment the active set with the following partial
671 cross product:
673 (1) actives formed by matching the new specs in specList against
674 all symbols currently listed in topSpecs
676 (2) actives formed by matching the new symbols in newdi against
677 all specs currently listed in topSpecs
679 (3) actives formed by matching the new symbols in newdi against
680 the new specs in specList
682 This is necessary in order to maintain the invariant that
683 Actives contains all bindings generated by matching ALL specs in
684 topSpecs against ALL symbols in topSpecs (that is, a cross
685 product of ALL known specs against ALL known symbols).
687 /* Case (1) */
688 for (ts = topSpecs; ts; ts = ts->next) {
689 if (ts->seginfo)
690 generate_and_add_actives( specList, newts,
691 ts->seginfo, ts );
694 /* Case (2) */
695 for (ts = topSpecs; ts; ts = ts->next) {
696 generate_and_add_actives( ts->specs, ts,
697 newdi, newts );
700 /* Case (3) */
701 generate_and_add_actives( specList, newts,
702 newdi, newts );
704 /* Finally, add the new TopSpec. */
705 newts->next = topSpecs;
706 topSpecs = newts;
708 if (VG_(clo_trace_redir))
709 show_redir_state("after VG_(redir_notify_new_DebugInfo)");
711 /* Really finally (quite unrelated to all the above) check the
712 names in the module against any --require-text-symbol=
713 specifications we might have. */
714 handle_require_text_symbols(newdi);
717 #undef N_DEMANGLED
719 /* Add a new target for an indirect function. Adds a new redirection
720 for the indirection function with address old_from that redirects
721 the ordinary function with address new_from to the target address
722 of the original redirection. */
724 void VG_(redir_add_ifunc_target)( Addr old_from, Addr new_from )
726 Active *old, new;
728 old = VG_(OSetGen_Lookup)(activeSet, &old_from);
729 vg_assert(old);
730 vg_assert(old->isIFunc);
732 new = *old;
733 new.from_addr = new_from;
734 new.isIFunc = False;
735 maybe_add_active (new);
737 if (VG_(clo_trace_redir)) {
738 VG_(message)( Vg_DebugMsg,
739 "Adding redirect for indirect function "
740 "0x%llx from 0x%llx -> 0x%llx\n",
741 (ULong)old_from, (ULong)new_from, (ULong)new.to_addr );
745 /* Do one element of the basic cross product: add to the active set,
746 all matches resulting from comparing all the given specs against
747 all the symbols in the given seginfo. If a conflicting binding
748 would thereby arise, don't add it, but do complain. */
750 static
751 void generate_and_add_actives (
752 /* spec list and the owning TopSpec */
753 Spec* specs,
754 TopSpec* parent_spec,
755 /* seginfo and the owning TopSpec */
756 DebugInfo* di,
757 TopSpec* parent_sym
760 Spec* sp;
761 Bool anyMark, isText, isIFunc;
762 Active act;
763 Int nsyms, i;
764 SymAVMAs sym_avmas;
765 HChar* sym_name_pri;
766 HChar** sym_names_sec;
768 /* First figure out which of the specs match the seginfo's soname.
769 Also clear the 'done' bits, so that after the main loop below
770 tell which of the Specs really did get done. */
771 anyMark = False;
772 for (sp = specs; sp; sp = sp->next) {
773 sp->done = False;
774 sp->mark = VG_(string_match)( sp->from_sopatt,
775 VG_(DebugInfo_get_soname)(di) );
776 anyMark = anyMark || sp->mark;
779 /* shortcut: if none of the sonames match, there will be no bindings. */
780 if (!anyMark)
781 return;
783 /* Iterate outermost over the symbols in the seginfo, in the hope
784 of trashing the caches less. */
785 nsyms = VG_(DebugInfo_syms_howmany)( di );
786 for (i = 0; i < nsyms; i++) {
787 VG_(DebugInfo_syms_getidx)( di, i, &sym_avmas,
788 NULL, &sym_name_pri, &sym_names_sec,
789 &isText, &isIFunc );
790 HChar* twoslots[2];
791 HChar** names_init = alloc_symname_array(sym_name_pri, sym_names_sec,
792 &twoslots[0]);
793 HChar** names;
794 for (names = names_init; *names; names++) {
796 /* ignore data symbols */
797 if (!isText)
798 continue;
800 for (sp = specs; sp; sp = sp->next) {
801 if (!sp->mark)
802 continue; /* soname doesn't match */
803 if (VG_(string_match)( sp->from_fnpatt, *names )) {
804 /* got a new binding. Add to collection. */
805 act.from_addr = sym_avmas.main;
806 act.to_addr = sp->to_addr;
807 act.parent_spec = parent_spec;
808 act.parent_sym = parent_sym;
809 act.becTag = sp->becTag;
810 act.becPrio = sp->becPrio;
811 act.isWrap = sp->isWrap;
812 act.isIFunc = isIFunc;
813 sp->done = True;
814 maybe_add_active( act );
816 /* If the function being wrapped has a local entry point
817 * redirect it to the global entry point. The redirection
818 * must save and setup r2 then setup r12 for the new function.
819 * On return, r2 must be restored. Local entry points used
820 * in PPC64 Little Endian.
822 if (GET_LOCAL_EP_AVMA(sym_avmas) != 0) {
823 act.from_addr = GET_LOCAL_EP_AVMA(sym_avmas);
824 maybe_add_active( act );
828 } /* for (sp = specs; sp; sp = sp->next) */
830 } /* iterating over names[] */
831 free_symname_array(names_init, &twoslots[0]);
832 } /* for (i = 0; i < nsyms; i++) */
834 /* Now, finally, look for Specs which were marked to be done, but
835 didn't get matched. If any such are mandatory we must abort the
836 system at this point. */
837 for (sp = specs; sp; sp = sp->next) {
838 if (!sp->mark)
839 continue;
840 if (sp->mark && (!sp->done) && sp->mandatory)
841 break;
843 if (sp) {
844 const HChar** strp;
845 const HChar* v = "valgrind: ";
846 vg_assert(sp->mark);
847 vg_assert(!sp->done);
848 vg_assert(sp->mandatory);
849 VG_(printf)("\n");
850 VG_(printf)(
851 "%sFatal error at startup: a function redirection\n", v);
852 VG_(printf)(
853 "%swhich is mandatory for this platform-tool combination\n", v);
854 VG_(printf)(
855 "%scannot be set up. Details of the redirection are:\n", v);
856 VG_(printf)(
857 "%s\n", v);
858 VG_(printf)(
859 "%sA must-be-redirected function\n", v);
860 VG_(printf)(
861 "%swhose name matches the pattern: %s\n", v, sp->from_fnpatt);
862 VG_(printf)(
863 "%sin an object with soname matching: %s\n", v, sp->from_sopatt);
864 VG_(printf)(
865 "%swas not found whilst processing\n", v);
866 VG_(printf)(
867 "%ssymbols from the object with soname: %s\n",
868 v, VG_(DebugInfo_get_soname)(di));
869 VG_(printf)(
870 "%s\n", v);
872 for (strp = sp->mandatory; *strp; strp++)
873 VG_(printf)(
874 "%s%s\n", v, *strp);
876 VG_(printf)(
877 "%s\n", v);
878 VG_(printf)(
879 "%sCannot continue -- exiting now. Sorry.\n", v);
880 VG_(printf)("\n");
881 VG_(exit)(1);
886 /* Add an act (passed by value; is copied here) and deal with
887 conflicting bindings. */
888 static void maybe_add_active ( Active act )
890 const HChar* what = NULL;
891 Active* old = NULL;
892 Bool add_act = False;
894 /* Complain and ignore manifestly bogus 'from' addresses.
896 Kludge: because this can get called befor the trampoline area (a
897 bunch of magic 'to' addresses) has its ownership changed from V
898 to C, we can't check the 'to' address similarly. Sigh.
900 amd64-linux hack: the vsysinfo pages appear to have no
901 permissions
902 ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0
903 so skip the check for them. */
904 if (!is_plausible_guest_addr(act.from_addr)
905 # if defined(VGP_amd64_linux)
906 && act.from_addr != 0xFFFFFFFFFF600000ULL
907 && act.from_addr != 0xFFFFFFFFFF600400ULL
908 && act.from_addr != 0xFFFFFFFFFF600800ULL
909 # endif
911 what = "redirection from-address is in non-executable area";
912 goto bad;
915 old = VG_(OSetGen_Lookup)( activeSet, &act.from_addr );
916 if (old) {
917 /* Dodgy. Conflicting binding. */
918 vg_assert(old->from_addr == act.from_addr);
919 if (old->to_addr != act.to_addr) {
920 /* We've got a conflicting binding -- that is, from_addr is
921 specified to redirect to two different destinations,
922 old->to_addr and act.to_addr. If we can prove that they
923 are behaviourally equivalent then that's no problem. So
924 we can look at the behavioural eclass tags for both
925 functions to see if that's so. If they are equal, and
926 nonzero, then that's fine. But if not, we can't show they
927 are equivalent, so we have to complain, and ignore the new
928 binding. */
929 vg_assert(old->becTag >= 0 && old->becTag <= 9999);
930 vg_assert(old->becPrio >= 0 && old->becPrio <= 9);
931 vg_assert(act.becTag >= 0 && act.becTag <= 9999);
932 vg_assert(act.becPrio >= 0 && act.becPrio <= 9);
933 if (old->becTag == 0)
934 vg_assert(old->becPrio == 0);
935 if (act.becTag == 0)
936 vg_assert(act.becPrio == 0);
938 if (old->becTag == 0 || act.becTag == 0 || old->becTag != act.becTag) {
939 /* We can't show that they are equivalent. Complain and
940 ignore. */
941 what = "new redirection conflicts with existing -- ignoring it";
942 goto bad;
944 /* They have the same eclass tag. Use the priorities to
945 resolve the ambiguity. */
946 if (act.becPrio <= old->becPrio) {
947 /* The new one doesn't have a higher priority, so just
948 ignore it. */
949 if (VG_(clo_verbosity) > 2) {
950 VG_(message)(Vg_UserMsg, "Ignoring %s redirection:\n",
951 act.becPrio < old->becPrio ? "lower priority"
952 : "duplicate");
953 show_active( " old: ", old);
954 show_active( " new: ", &act);
956 } else {
957 /* The tricky case. The new one has a higher priority, so
958 we need to get the old one out of the OSet and install
959 this one in its place. */
960 if (VG_(clo_verbosity) > 1) {
961 VG_(message)(Vg_UserMsg,
962 "Preferring higher priority redirection:\n");
963 show_active( " old: ", old);
964 show_active( " new: ", &act);
966 add_act = True;
967 void* oldNd = VG_(OSetGen_Remove)( activeSet, &act.from_addr );
968 vg_assert(oldNd == old);
969 VG_(OSetGen_FreeNode)( activeSet, old );
970 old = NULL;
972 } else {
973 /* This appears to be a duplicate of an existing binding.
974 Safe(ish) -- ignore. */
975 /* XXXXXXXXXXX COMPLAIN if new and old parents differ */
978 } else {
979 /* There's no previous binding for this from_addr, so we must
980 add 'act' to the active set. */
981 add_act = True;
984 /* So, finally, actually add it. */
985 if (add_act) {
986 Active* a = VG_(OSetGen_AllocNode)(activeSet, sizeof(Active));
987 vg_assert(a);
988 *a = act;
989 VG_(OSetGen_Insert)(activeSet, a);
990 /* Now that a new from->to redirection is in force, we need to
991 get rid of any translations intersecting 'from' in order that
992 they get redirected to 'to'. So discard them. Just for
993 paranoia (but, I believe, unnecessarily), discard 'to' as
994 well. */
995 VG_(discard_translations)( (Addr64)act.from_addr, 1,
996 "redir_new_DebugInfo(from_addr)");
997 VG_(discard_translations)( (Addr64)act.to_addr, 1,
998 "redir_new_DebugInfo(to_addr)");
999 if (VG_(clo_verbosity) > 2) {
1000 VG_(message)(Vg_UserMsg, "Adding active redirection:\n");
1001 show_active( " new: ", &act);
1004 return;
1006 bad:
1007 vg_assert(what);
1008 vg_assert(!add_act);
1009 if (VG_(clo_verbosity) > 1) {
1010 VG_(message)(Vg_UserMsg, "WARNING: %s\n", what);
1011 if (old) {
1012 show_active( " old: ", old);
1014 show_active( " new: ", &act);
1019 /* Notify m_redir of the deletion of a DebugInfo. This is relatively
1020 simple -- just get rid of all actives derived from it, and free up
1021 the associated list elements. */
1023 void VG_(redir_notify_delete_DebugInfo)( DebugInfo* delsi )
1025 TopSpec* ts;
1026 TopSpec* tsPrev;
1027 Spec* sp;
1028 Spec* sp_next;
1029 OSet* tmpSet;
1030 Active* act;
1031 Bool delMe;
1032 Addr addr;
1034 vg_assert(delsi);
1036 /* Search for it, and make tsPrev point to the previous entry, if
1037 any. */
1038 tsPrev = NULL;
1039 ts = topSpecs;
1040 while (True) {
1041 if (ts == NULL) break;
1042 if (ts->seginfo == delsi) break;
1043 tsPrev = ts;
1044 ts = ts->next;
1047 vg_assert(ts); /* else we don't have the deleted DebugInfo */
1048 vg_assert(ts->seginfo == delsi);
1050 /* Traverse the actives, copying the addresses of those we intend
1051 to delete into tmpSet. */
1052 tmpSet = VG_(OSetWord_Create)(dinfo_zalloc, "redir.rndD.1", dinfo_free);
1054 ts->mark = True;
1056 VG_(OSetGen_ResetIter)( activeSet );
1057 while ( (act = VG_(OSetGen_Next)(activeSet)) ) {
1058 delMe = act->parent_spec != NULL
1059 && act->parent_sym != NULL
1060 && act->parent_spec->seginfo != NULL
1061 && act->parent_sym->seginfo != NULL
1062 && (act->parent_spec->mark || act->parent_sym->mark);
1064 /* While we're at it, a bit of paranoia: delete any actives
1065 which don't have both feet in valid client executable areas.
1066 But don't delete hardwired-at-startup ones; these are denoted
1067 by having parent_spec or parent_sym being NULL. */
1068 if ( (!delMe)
1069 && act->parent_spec != NULL
1070 && act->parent_sym != NULL ) {
1071 if (!is_plausible_guest_addr(act->from_addr))
1072 delMe = True;
1073 if (!is_plausible_guest_addr(act->to_addr))
1074 delMe = True;
1077 if (delMe) {
1078 VG_(OSetWord_Insert)( tmpSet, act->from_addr );
1079 /* While we have our hands on both the 'from' and 'to'
1080 of this Active, do paranoid stuff with tt/tc. */
1081 VG_(discard_translations)( (Addr64)act->from_addr, 1,
1082 "redir_del_DebugInfo(from_addr)");
1083 VG_(discard_translations)( (Addr64)act->to_addr, 1,
1084 "redir_del_DebugInfo(to_addr)");
1088 /* Now traverse tmpSet, deleting corresponding elements in activeSet. */
1089 VG_(OSetWord_ResetIter)( tmpSet );
1090 while ( VG_(OSetWord_Next)(tmpSet, &addr) ) {
1091 act = VG_(OSetGen_Remove)( activeSet, &addr );
1092 vg_assert(act);
1093 VG_(OSetGen_FreeNode)( activeSet, act );
1096 VG_(OSetWord_Destroy)( tmpSet );
1098 /* The Actives set is now cleaned up. Free up this TopSpec and
1099 everything hanging off it. */
1100 for (sp = ts->specs; sp; sp = sp_next) {
1101 if (sp->from_sopatt) dinfo_free(sp->from_sopatt);
1102 if (sp->from_fnpatt) dinfo_free(sp->from_fnpatt);
1103 sp_next = sp->next;
1104 dinfo_free(sp);
1107 if (tsPrev == NULL) {
1108 /* first in list */
1109 topSpecs = ts->next;
1110 } else {
1111 tsPrev->next = ts->next;
1113 dinfo_free(ts);
1115 if (VG_(clo_trace_redir))
1116 show_redir_state("after VG_(redir_notify_delete_DebugInfo)");
1120 /*------------------------------------------------------------*/
1121 /*--- QUERIES (really the whole point of this module) ---*/
1122 /*------------------------------------------------------------*/
1124 /* This is the crucial redirection function. It answers the question:
1125 should this code address be redirected somewhere else? It's used
1126 just before translating a basic block. */
1127 Addr VG_(redir_do_lookup) ( Addr orig, Bool* isWrap )
1129 Active* r = VG_(OSetGen_Lookup)(activeSet, &orig);
1130 if (r == NULL)
1131 return orig;
1133 vg_assert(r->to_addr != 0);
1134 if (isWrap)
1135 *isWrap = r->isWrap || r->isIFunc;
1136 if (r->isIFunc) {
1137 vg_assert(iFuncWrapper);
1138 return iFuncWrapper;
1140 return r->to_addr;
1144 /*------------------------------------------------------------*/
1145 /*--- INITIALISATION ---*/
1146 /*------------------------------------------------------------*/
1148 /* Add a never-delete-me Active. */
1150 __attribute__((unused)) /* only used on amd64 */
1151 static void add_hardwired_active ( Addr from, Addr to )
1153 Active act;
1154 act.from_addr = from;
1155 act.to_addr = to;
1156 act.parent_spec = NULL;
1157 act.parent_sym = NULL;
1158 act.becTag = 0; /* "not equivalent to any other fn" */
1159 act.becPrio = 0; /* mandatory when becTag == 0 */
1160 act.isWrap = False;
1161 act.isIFunc = False;
1162 maybe_add_active( act );
1166 /* Add a never-delete-me Spec. This is a bit of a kludge. On the
1167 assumption that this is called only at startup, only handle the
1168 case where topSpecs is completely empty, or if it isn't, it has
1169 just one entry and that is the one with NULL seginfo -- that is the
1170 entry that holds these initial specs. */
1172 __attribute__((unused)) /* not used on all platforms */
1173 static void add_hardwired_spec (const HChar* sopatt, const HChar* fnpatt,
1174 Addr to_addr,
1175 const HChar** mandatory )
1177 Spec* spec = dinfo_zalloc("redir.ahs.1", sizeof(Spec));
1178 vg_assert(spec);
1180 if (topSpecs == NULL) {
1181 topSpecs = dinfo_zalloc("redir.ahs.2", sizeof(TopSpec));
1182 vg_assert(topSpecs);
1183 /* symtab_zalloc sets all fields to zero */
1186 vg_assert(topSpecs != NULL);
1187 vg_assert(topSpecs->next == NULL);
1188 vg_assert(topSpecs->seginfo == NULL);
1189 /* FIXED PARTS */
1190 spec->from_sopatt = (HChar *)sopatt;
1191 spec->from_fnpatt = (HChar *)fnpatt;
1192 spec->to_addr = to_addr;
1193 spec->isWrap = False;
1194 spec->mandatory = mandatory;
1195 /* VARIABLE PARTS */
1196 spec->mark = False; /* not significant */
1197 spec->done = False; /* not significant */
1199 spec->next = topSpecs->specs;
1200 topSpecs->specs = spec;
1204 __attribute__((unused)) /* not used on all platforms */
1205 static const HChar* complain_about_stripped_glibc_ldso[]
1206 = { "Possible fixes: (1, short term): install glibc's debuginfo",
1207 "package on this machine. (2, longer term): ask the packagers",
1208 "for your Linux distribution to please in future ship a non-",
1209 "stripped ld.so (or whatever the dynamic linker .so is called)",
1210 "that exports the above-named function using the standard",
1211 "calling conventions for this platform. The package you need",
1212 "to install for fix (1) is called",
1214 " On Debian, Ubuntu: libc6-dbg",
1215 " On SuSE, openSuSE, Fedora, RHEL: glibc-debuginfo",
1216 NULL
1220 /* Initialise the redir system, and create the initial Spec list and
1221 for amd64-linux a couple of permanent active mappings. The initial
1222 Specs are not converted into Actives yet, on the (checked)
1223 assumption that no DebugInfos have so far been created, and so when
1224 they are created, that will happen. */
1226 void VG_(redir_initialise) ( void )
1228 // Assert that there are no DebugInfos so far
1229 vg_assert( VG_(next_DebugInfo)(NULL) == NULL );
1231 // Initialise active mapping.
1232 activeSet = VG_(OSetGen_Create)(offsetof(Active, from_addr),
1233 NULL, // Use fast comparison
1234 dinfo_zalloc,
1235 "redir.ri.1",
1236 dinfo_free);
1238 // The rest of this function just adds initial Specs.
1240 # if defined(VGP_x86_linux)
1241 /* If we're using memcheck, use this intercept right from the
1242 start, otherwise ld.so (glibc-2.3.5) makes a lot of noise. */
1243 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1244 const HChar** mandatory;
1245 # ifndef GLIBC_MANDATORY_INDEX_AND_STRLEN_REDIRECT
1246 mandatory = NULL;
1247 # else
1248 /* for glibc-2.12 and later, this is mandatory - can't sanely
1249 continue without it */
1250 mandatory = complain_about_stripped_glibc_ldso;
1251 # endif
1252 add_hardwired_spec(
1253 "ld-linux.so.2", "index",
1254 (Addr)&VG_(x86_linux_REDIR_FOR_index), mandatory);
1255 add_hardwired_spec(
1256 "ld-linux.so.2", "strlen",
1257 (Addr)&VG_(x86_linux_REDIR_FOR_strlen), mandatory);
1260 # elif defined(VGP_amd64_linux)
1261 /* Redirect vsyscalls to local versions */
1262 add_hardwired_active(
1263 0xFFFFFFFFFF600000ULL,
1264 (Addr)&VG_(amd64_linux_REDIR_FOR_vgettimeofday)
1266 add_hardwired_active(
1267 0xFFFFFFFFFF600400ULL,
1268 (Addr)&VG_(amd64_linux_REDIR_FOR_vtime)
1270 add_hardwired_active(
1271 0xFFFFFFFFFF600800ULL,
1272 (Addr)&VG_(amd64_linux_REDIR_FOR_vgetcpu)
1275 /* If we're using memcheck, use these intercepts right from
1276 the start, otherwise ld.so makes a lot of noise. */
1277 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1279 add_hardwired_spec(
1280 "ld-linux-x86-64.so.2", "strlen",
1281 (Addr)&VG_(amd64_linux_REDIR_FOR_strlen),
1282 # ifndef GLIBC_MANDATORY_STRLEN_REDIRECT
1283 NULL
1284 # else
1285 /* for glibc-2.10 and later, this is mandatory - can't sanely
1286 continue without it */
1287 complain_about_stripped_glibc_ldso
1288 # endif
1292 # elif defined(VGP_ppc32_linux)
1293 /* If we're using memcheck, use these intercepts right from
1294 the start, otherwise ld.so makes a lot of noise. */
1295 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1297 /* this is mandatory - can't sanely continue without it */
1298 add_hardwired_spec(
1299 "ld.so.1", "strlen",
1300 (Addr)&VG_(ppc32_linux_REDIR_FOR_strlen),
1301 complain_about_stripped_glibc_ldso
1303 add_hardwired_spec(
1304 "ld.so.1", "strcmp",
1305 (Addr)&VG_(ppc32_linux_REDIR_FOR_strcmp),
1306 NULL /* not mandatory - so why bother at all? */
1307 /* glibc-2.6.1 (openSUSE 10.3, ppc32) seems fine without it */
1309 add_hardwired_spec(
1310 "ld.so.1", "index",
1311 (Addr)&VG_(ppc32_linux_REDIR_FOR_strchr),
1312 NULL /* not mandatory - so why bother at all? */
1313 /* glibc-2.6.1 (openSUSE 10.3, ppc32) seems fine without it */
1317 # elif defined(VGP_ppc64be_linux)
1318 /* If we're using memcheck, use these intercepts right from
1319 the start, otherwise ld.so makes a lot of noise. */
1320 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1322 /* this is mandatory - can't sanely continue without it */
1323 add_hardwired_spec(
1324 "ld64.so.1", "strlen",
1325 (Addr)VG_(fnptr_to_fnentry)( &VG_(ppc64_linux_REDIR_FOR_strlen) ),
1326 complain_about_stripped_glibc_ldso
1329 add_hardwired_spec(
1330 "ld64.so.1", "index",
1331 (Addr)VG_(fnptr_to_fnentry)( &VG_(ppc64_linux_REDIR_FOR_strchr) ),
1332 NULL /* not mandatory - so why bother at all? */
1333 /* glibc-2.5 (FC6, ppc64) seems fine without it */
1337 # elif defined(VGP_ppc64le_linux)
1338 /* If we're using memcheck, use these intercepts right from
1339 * the start, otherwise ld.so makes a lot of noise.
1341 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1343 /* this is mandatory - can't sanely continue without it */
1344 add_hardwired_spec(
1345 "ld64.so.2", "strlen",
1346 (Addr)&VG_(ppc64_linux_REDIR_FOR_strlen),
1347 complain_about_stripped_glibc_ldso
1350 add_hardwired_spec(
1351 "ld64.so.2", "index",
1352 (Addr)&VG_(ppc64_linux_REDIR_FOR_strchr),
1353 NULL /* not mandatory - so why bother at all? */
1354 /* glibc-2.5 (FC6, ppc64) seems fine without it */
1358 # elif defined(VGP_arm_linux)
1359 /* If we're using memcheck, use these intercepts right from the
1360 start, otherwise ld.so makes a lot of noise. In most ARM-linux
1361 distros, ld.so's soname is ld-linux.so.3, but Ubuntu 14.04 on
1362 Odroid uses ld-linux-armhf.so.3 for some reason. */
1363 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1364 /* strlen */
1365 add_hardwired_spec(
1366 "ld-linux.so.3", "strlen",
1367 (Addr)&VG_(arm_linux_REDIR_FOR_strlen),
1368 complain_about_stripped_glibc_ldso
1370 add_hardwired_spec(
1371 "ld-linux-armhf.so.3", "strlen",
1372 (Addr)&VG_(arm_linux_REDIR_FOR_strlen),
1373 complain_about_stripped_glibc_ldso
1375 /* memcpy */
1376 add_hardwired_spec(
1377 "ld-linux.so.3", "memcpy",
1378 (Addr)&VG_(arm_linux_REDIR_FOR_memcpy),
1379 complain_about_stripped_glibc_ldso
1381 add_hardwired_spec(
1382 "ld-linux-armhf.so.3", "memcpy",
1383 (Addr)&VG_(arm_linux_REDIR_FOR_memcpy),
1384 complain_about_stripped_glibc_ldso
1386 /* strcmp */
1387 add_hardwired_spec(
1388 "ld-linux.so.3", "strcmp",
1389 (Addr)&VG_(arm_linux_REDIR_FOR_strcmp),
1390 complain_about_stripped_glibc_ldso
1392 add_hardwired_spec(
1393 "ld-linux-armhf.so.3", "strcmp",
1394 (Addr)&VG_(arm_linux_REDIR_FOR_strcmp),
1395 complain_about_stripped_glibc_ldso
1399 # elif defined(VGP_arm64_linux)
1400 /* If we're using memcheck, use these intercepts right from
1401 the start, otherwise ld.so makes a lot of noise. */
1402 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1403 add_hardwired_spec(
1404 "ld-linux-aarch64.so.1", "strlen",
1405 (Addr)&VG_(arm64_linux_REDIR_FOR_strlen),
1406 complain_about_stripped_glibc_ldso
1408 add_hardwired_spec(
1409 "ld-linux-aarch64.so.1", "index",
1410 (Addr)&VG_(arm64_linux_REDIR_FOR_index),
1411 NULL
1413 add_hardwired_spec(
1414 "ld-linux-aarch64.so.1", "strcmp",
1415 (Addr)&VG_(arm64_linux_REDIR_FOR_strcmp),
1416 NULL
1418 //add_hardwired_spec(
1419 // "ld-linux.so.3", "memcpy",
1420 // (Addr)&VG_(arm_linux_REDIR_FOR_memcpy),
1421 // complain_about_stripped_glibc_ldso
1422 //);
1425 # elif defined(VGP_x86_darwin)
1426 /* If we're using memcheck, use these intercepts right from
1427 the start, otherwise dyld makes a lot of noise. */
1428 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1429 add_hardwired_spec("dyld", "strcmp",
1430 (Addr)&VG_(x86_darwin_REDIR_FOR_strcmp), NULL);
1431 add_hardwired_spec("dyld", "strlen",
1432 (Addr)&VG_(x86_darwin_REDIR_FOR_strlen), NULL);
1433 add_hardwired_spec("dyld", "strcat",
1434 (Addr)&VG_(x86_darwin_REDIR_FOR_strcat), NULL);
1435 add_hardwired_spec("dyld", "strcpy",
1436 (Addr)&VG_(x86_darwin_REDIR_FOR_strcpy), NULL);
1437 add_hardwired_spec("dyld", "strlcat",
1438 (Addr)&VG_(x86_darwin_REDIR_FOR_strlcat), NULL);
1441 # elif defined(VGP_amd64_darwin)
1442 /* If we're using memcheck, use these intercepts right from
1443 the start, otherwise dyld makes a lot of noise. */
1444 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1445 add_hardwired_spec("dyld", "strcmp",
1446 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcmp), NULL);
1447 add_hardwired_spec("dyld", "strlen",
1448 (Addr)&VG_(amd64_darwin_REDIR_FOR_strlen), NULL);
1449 add_hardwired_spec("dyld", "strcat",
1450 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcat), NULL);
1451 add_hardwired_spec("dyld", "strcpy",
1452 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcpy), NULL);
1453 add_hardwired_spec("dyld", "strlcat",
1454 (Addr)&VG_(amd64_darwin_REDIR_FOR_strlcat), NULL);
1455 // DDD: #warning fixme rdar://6166275
1456 add_hardwired_spec("dyld", "arc4random",
1457 (Addr)&VG_(amd64_darwin_REDIR_FOR_arc4random), NULL);
1458 # if DARWIN_VERS == DARWIN_10_9
1459 add_hardwired_spec("dyld", "strchr",
1460 (Addr)&VG_(amd64_darwin_REDIR_FOR_strchr), NULL);
1461 # endif
1464 # elif defined(VGP_s390x_linux)
1465 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1466 // added in rsponse to BZ 327943
1467 add_hardwired_spec("ld64.so.1", "index",
1468 (Addr)&VG_(s390x_linux_REDIR_FOR_index),
1469 complain_about_stripped_glibc_ldso);
1472 # elif defined(VGP_mips32_linux)
1473 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1475 /* this is mandatory - can't sanely continue without it */
1476 add_hardwired_spec(
1477 "ld.so.3", "strlen",
1478 (Addr)&VG_(mips32_linux_REDIR_FOR_strlen),
1479 complain_about_stripped_glibc_ldso
1483 # elif defined(VGP_mips64_linux)
1484 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1486 /* this is mandatory - can't sanely continue without it */
1487 add_hardwired_spec(
1488 "ld.so.3", "strlen",
1489 (Addr)&VG_(mips64_linux_REDIR_FOR_strlen),
1490 complain_about_stripped_glibc_ldso
1494 # else
1495 # error Unknown platform
1496 # endif
1498 if (VG_(clo_trace_redir))
1499 show_redir_state("after VG_(redir_initialise)");
1503 /*------------------------------------------------------------*/
1504 /*--- MISC HELPERS ---*/
1505 /*------------------------------------------------------------*/
1507 static void* dinfo_zalloc(const HChar* ec, SizeT n) {
1508 void* p;
1509 vg_assert(n > 0);
1510 p = VG_(arena_malloc)(VG_AR_DINFO, ec, n);
1511 tl_assert(p);
1512 VG_(memset)(p, 0, n);
1513 return p;
1516 static void dinfo_free(void* p) {
1517 tl_assert(p);
1518 return VG_(arena_free)(VG_AR_DINFO, p);
1521 static HChar* dinfo_strdup(const HChar* ec, const HChar* str)
1523 return VG_(arena_strdup)(VG_AR_DINFO, ec, str);
1526 /* Really this should be merged with translations_allowable_from_seg
1527 in m_translate. */
1528 static Bool is_plausible_guest_addr(Addr a)
1530 NSegment const* seg = VG_(am_find_nsegment)(a);
1531 return seg != NULL
1532 && (seg->kind == SkAnonC || seg->kind == SkFileC)
1533 && (seg->hasX || seg->hasR); /* crude x86-specific hack */
1537 /*------------------------------------------------------------*/
1538 /*--- NOTIFY-ON-LOAD FUNCTIONS ---*/
1539 /*------------------------------------------------------------*/
1541 static
1542 void handle_maybe_load_notifier( const HChar* soname,
1543 HChar* symbol, Addr addr )
1545 # if defined(VGP_x86_linux)
1546 /* x86-linux only: if we see _dl_sysinfo_int80, note its address.
1547 See comment on declaration of VG_(client__dl_sysinfo_int80) for
1548 the reason. As far as I can tell, the relevant symbol is always
1549 in object with soname "ld-linux.so.2". */
1550 if (symbol && symbol[0] == '_'
1551 && 0 == VG_(strcmp)(symbol, "_dl_sysinfo_int80")
1552 && 0 == VG_(strcmp)(soname, "ld-linux.so.2")) {
1553 if (VG_(client__dl_sysinfo_int80) == 0)
1554 VG_(client__dl_sysinfo_int80) = addr;
1556 # endif
1558 /* Normal load-notifier handling after here. First, ignore all
1559 symbols lacking the right prefix. */
1560 vg_assert(symbol); // assert rather than segfault if it is NULL
1561 if (0 != VG_(strncmp)(symbol, VG_NOTIFY_ON_LOAD_PREFIX,
1562 VG_NOTIFY_ON_LOAD_PREFIX_LEN))
1563 /* Doesn't have the right prefix */
1564 return;
1566 if (VG_(strcmp)(symbol, VG_STRINGIFY(VG_NOTIFY_ON_LOAD(freeres))) == 0)
1567 VG_(client___libc_freeres_wrapper) = addr;
1568 else
1569 if (VG_(strcmp)(symbol, VG_STRINGIFY(VG_NOTIFY_ON_LOAD(ifunc_wrapper))) == 0)
1570 iFuncWrapper = addr;
1571 else
1572 vg_assert2(0, "unrecognised load notification function: %s", symbol);
1576 /*------------------------------------------------------------*/
1577 /*--- REQUIRE-TEXT-SYMBOL HANDLING ---*/
1578 /*------------------------------------------------------------*/
1580 /* In short: check that the currently-being-loaded object has text
1581 symbols that satisfy any --require-text-symbol= specifications that
1582 apply to it, and abort the run with an error message if not.
1584 static void handle_require_text_symbols ( DebugInfo* di )
1586 /* First thing to do is figure out which, if any,
1587 --require-text-symbol specification strings apply to this
1588 object. Most likely none do, since it is not expected to
1589 frequently be used. Work through the list of specs and
1590 accumulate in fnpatts[] the fn patterns that pertain to this
1591 object. */
1592 HChar* fnpatts[VG_CLO_MAX_REQ_TSYMS];
1593 Int fnpatts_used = 0;
1594 Int i, j;
1595 const HChar* di_soname = VG_(DebugInfo_get_soname)(di);
1596 vg_assert(di_soname); // must be present
1598 VG_(memset)(&fnpatts, 0, sizeof(fnpatts));
1600 vg_assert(VG_(clo_n_req_tsyms) >= 0);
1601 vg_assert(VG_(clo_n_req_tsyms) <= VG_CLO_MAX_REQ_TSYMS);
1602 for (i = 0; i < VG_(clo_n_req_tsyms); i++) {
1603 const HChar* clo_spec = VG_(clo_req_tsyms)[i];
1604 vg_assert(clo_spec && VG_(strlen)(clo_spec) >= 4);
1605 // clone the spec, so we can stick a zero at the end of the sopatt
1606 HChar *spec = VG_(strdup)("m_redir.hrts.1", clo_spec);
1607 HChar sep = spec[0];
1608 HChar* sopatt = &spec[1];
1609 HChar* fnpatt = VG_(strchr)(sopatt, sep);
1610 // the initial check at clo processing in time in m_main
1611 // should ensure this.
1612 vg_assert(fnpatt && *fnpatt == sep);
1613 *fnpatt = 0;
1614 fnpatt++;
1615 if (VG_(string_match)(sopatt, di_soname))
1616 fnpatts[fnpatts_used++]
1617 = VG_(strdup)("m_redir.hrts.2", fnpatt);
1618 VG_(free)(spec);
1621 if (fnpatts_used == 0)
1622 return; /* no applicable spec strings */
1624 /* So finally, fnpatts[0 .. fnpatts_used - 1] contains the set of
1625 (patterns for) text symbol names that must be found in this
1626 object, in order to continue. That is, we must find at least
1627 one text symbol name that matches each pattern, else we must
1628 abort the run. */
1630 if (0) VG_(printf)("for %s\n", di_soname);
1631 for (i = 0; i < fnpatts_used; i++)
1632 if (0) VG_(printf)(" fnpatt: %s\n", fnpatts[i]);
1634 /* For each spec, look through the syms to find one that matches.
1635 This isn't terribly efficient but it happens rarely, so no big
1636 deal. */
1637 for (i = 0; i < fnpatts_used; i++) {
1638 Bool found = False;
1639 HChar* fnpatt = fnpatts[i];
1640 Int nsyms = VG_(DebugInfo_syms_howmany)(di);
1641 for (j = 0; j < nsyms; j++) {
1642 Bool isText = False;
1643 HChar* sym_name_pri = NULL;
1644 HChar** sym_names_sec = NULL;
1645 VG_(DebugInfo_syms_getidx)( di, j, NULL,
1646 NULL, &sym_name_pri, &sym_names_sec,
1647 &isText, NULL );
1648 HChar* twoslots[2];
1649 HChar** names_init = alloc_symname_array(sym_name_pri, sym_names_sec,
1650 &twoslots[0]);
1651 HChar** names;
1652 for (names = names_init; *names; names++) {
1653 /* ignore data symbols */
1654 if (0) VG_(printf)("QQQ %s\n", *names);
1655 vg_assert(sym_name_pri);
1656 if (!isText)
1657 continue;
1658 if (VG_(string_match)(fnpatt, *names)) {
1659 found = True;
1660 break;
1663 free_symname_array(names_init, &twoslots[0]);
1664 if (found)
1665 break;
1668 if (!found) {
1669 const HChar* v = "valgrind: ";
1670 VG_(printf)("\n");
1671 VG_(printf)(
1672 "%sFatal error at when loading library with soname\n", v);
1673 VG_(printf)(
1674 "%s %s\n", v, di_soname);
1675 VG_(printf)(
1676 "%sCannot find any text symbol with a name "
1677 "that matches the pattern\n", v);
1678 VG_(printf)("%s %s\n", v, fnpatt);
1679 VG_(printf)("%sas required by a --require-text-symbol= "
1680 "specification.\n", v);
1681 VG_(printf)("\n");
1682 VG_(printf)(
1683 "%sCannot continue -- exiting now.\n", v);
1684 VG_(printf)("\n");
1685 VG_(exit)(1);
1689 /* All required specs were found. Just free memory and return. */
1690 for (i = 0; i < fnpatts_used; i++)
1691 VG_(free)(fnpatts[i]);
1695 /*------------------------------------------------------------*/
1696 /*--- SANITY/DEBUG ---*/
1697 /*------------------------------------------------------------*/
1699 static void show_spec ( const HChar* left, Spec* spec )
1701 VG_(message)( Vg_DebugMsg,
1702 "%s%25s %30s %s-> (%04d.%d) 0x%08llx\n",
1703 left,
1704 spec->from_sopatt, spec->from_fnpatt,
1705 spec->isWrap ? "W" : "R",
1706 spec->becTag, spec->becPrio,
1707 (ULong)spec->to_addr );
1710 static void show_active ( const HChar* left, Active* act )
1712 Bool ok;
1713 HChar name1[64] = "";
1714 HChar name2[64] = "";
1715 name1[0] = name2[0] = 0;
1716 ok = VG_(get_fnname_w_offset)(act->from_addr, name1, 64);
1717 if (!ok) VG_(strcpy)(name1, "???");
1718 ok = VG_(get_fnname_w_offset)(act->to_addr, name2, 64);
1719 if (!ok) VG_(strcpy)(name2, "???");
1721 VG_(message)(Vg_DebugMsg, "%s0x%08llx (%20s) %s-> (%04d.%d) 0x%08llx %s\n",
1722 left,
1723 (ULong)act->from_addr, name1,
1724 act->isWrap ? "W" : "R",
1725 act->becTag, act->becPrio,
1726 (ULong)act->to_addr, name2 );
1729 static void show_redir_state ( const HChar* who )
1731 TopSpec* ts;
1732 Spec* sp;
1733 Active* act;
1734 VG_(message)(Vg_DebugMsg, "<<\n");
1735 VG_(message)(Vg_DebugMsg, " ------ REDIR STATE %s ------\n", who);
1736 for (ts = topSpecs; ts; ts = ts->next) {
1737 if (ts->seginfo)
1738 VG_(message)(Vg_DebugMsg,
1739 " TOPSPECS of soname %s filename %s\n",
1740 VG_(DebugInfo_get_soname)(ts->seginfo),
1741 VG_(DebugInfo_get_filename)(ts->seginfo));
1742 else
1743 VG_(message)(Vg_DebugMsg,
1744 " TOPSPECS of soname (hardwired)\n");
1746 for (sp = ts->specs; sp; sp = sp->next)
1747 show_spec(" ", sp);
1749 VG_(message)(Vg_DebugMsg, " ------ ACTIVE ------\n");
1750 VG_(OSetGen_ResetIter)( activeSet );
1751 while ( (act = VG_(OSetGen_Next)(activeSet)) ) {
1752 show_active(" ", act);
1755 VG_(message)(Vg_DebugMsg, ">>\n");
1758 /*--------------------------------------------------------------------*/
1759 /*--- end ---*/
1760 /*--------------------------------------------------------------------*/