blob: 9172cc26985b2dfcca3c3a051d8f3783dcb788e4 [file] [log] [blame]
njnc0ae7052005-08-25 22:55:19 +00001
sewardjcbdddcf2005-03-10 23:23:45 +00002/*--------------------------------------------------------------------*/
njnc0ae7052005-08-25 22:55:19 +00003/*--- Function replacement and wrapping. m_redir.c ---*/
sewardjcbdddcf2005-03-10 23:23:45 +00004/*--------------------------------------------------------------------*/
5
6/*
njnc0ae7052005-08-25 22:55:19 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardjcbdddcf2005-03-10 23:23:45 +00009
sewardj9eecbbb2010-05-03 21:37:12 +000010 Copyright (C) 2000-2010 Julian Seward
sewardjcbdddcf2005-03-10 23:23:45 +000011 jseward@acm.org
sewardj9eecbbb2010-05-03 21:37:12 +000012 Copyright (C) 2003-2010 Jeremy Fitzhardinge
sewardjcbdddcf2005-03-10 23:23:45 +000013 jeremy@goop.org
14
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.
19
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.
24
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.
29
30 The GNU General Public License is contained in the file COPYING.
31*/
sewardjcbdddcf2005-03-10 23:23:45 +000032
njnc7561b92005-06-19 01:24:32 +000033#include "pub_core_basics.h"
sewardj45f4e7c2005-09-27 19:20:21 +000034#include "pub_core_debuglog.h"
njn88c51482005-06-25 20:49:33 +000035#include "pub_core_debuginfo.h"
njn97405b22005-06-02 03:39:33 +000036#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000037#include "pub_core_libcassert.h"
njn36a20fa2005-06-03 03:08:39 +000038#include "pub_core_libcprint.h"
sewardjd7a02db2008-12-12 08:07:49 +000039#include "pub_core_seqmatch.h"
njnaf1d7df2005-06-11 01:31:52 +000040#include "pub_core_mallocfree.h"
njn20242342005-05-16 23:31:24 +000041#include "pub_core_options.h"
njn8a96ec52005-10-15 15:48:52 +000042#include "pub_core_oset.h"
njnd1af0032005-05-29 17:01:48 +000043#include "pub_core_redir.h"
njna7598f62005-06-18 03:27:58 +000044#include "pub_core_trampoline.h"
njn8bddf582005-05-13 23:40:55 +000045#include "pub_core_transtab.h"
sewardj461c69d2005-11-17 20:15:04 +000046#include "pub_core_tooliface.h" // VG_(needs).malloc_replacement
sewardj6b9cc872006-10-17 01:39:30 +000047#include "pub_core_machine.h" // VG_(fnptr_to_fnentry)
sewardj0ec07f32006-01-12 12:32:32 +000048#include "pub_core_aspacemgr.h" // VG_(am_find_nsegment)
sewardj14c7cc52007-02-25 15:08:24 +000049#include "pub_core_xarray.h"
sewardj0ec07f32006-01-12 12:32:32 +000050#include "pub_core_clientstate.h" // VG_(client___libc_freeres_wrapper)
51#include "pub_core_demangle.h" // VG_(maybe_Z_demangle)
sewardj461c69d2005-11-17 20:15:04 +000052
bart257b91b2009-12-31 13:31:11 +000053#include "config.h" /* GLIBC_2_* */
54
sewardj55f9d1a2005-04-25 11:11:44 +000055
sewardj0ec07f32006-01-12 12:32:32 +000056/* This module is a critical part of the redirection/intercept system.
57 It keeps track of the current intercept state, cleans up the
58 translation caches when that state changes, and finally, answers
59 queries about the whether an address is currently redirected or
60 not. It doesn't do any of the control-flow trickery needed to put
61 the redirections into practice. That is the job of m_translate,
62 which calls here to find out which translations need to be
63 redirected.
64
65 The interface is simple. VG_(redir_initialise) initialises and
66 loads some hardwired redirects which never disappear; this is
67 platform-specific.
68
69 The module is notified of redirection state changes by m_debuginfo.
sewardjf9ebc392010-05-09 22:30:43 +000070 That calls VG_(redir_notify_new_DebugInfo) when a new DebugInfo
71 (shared object symbol table, basically) appears. Appearance of new
72 symbols can cause new (active) redirections to appear for two
73 reasons: the symbols in the new table may match existing
74 redirection specifications (see comments below), and because the
75 symbols in the new table may themselves supply new redirect
76 specifications which match existing symbols (or ones in the new
77 table).
sewardj0ec07f32006-01-12 12:32:32 +000078
79 Redirect specifications are really symbols with "funny" prefixes
80 (_vgrZU_ and _vgrZZ_). These names tell m_redir that the
81 associated code should replace the standard entry point for some
82 set of functions. The set of functions is specified by a (soname
83 pattern, function name pattern) pair which is encoded in the symbol
84 name following the prefix. The names use a Z-encoding scheme so
85 that they may contain punctuation characters and wildcards (*).
86 The encoding scheme is described in pub_tool_redir.h and is decoded
87 by VG_(maybe_Z_demangle).
88
89 When a shared object is unloaded, this module learns of it via a
sewardjf9ebc392010-05-09 22:30:43 +000090 call to VG_(redir_notify_delete_DebugInfo). It then removes from
91 its tables all active redirections in any way associated with that
sewardj0ec07f32006-01-12 12:32:32 +000092 object, and tidies up the translation caches accordingly.
93
94 That takes care of tracking the redirection state. When a
95 translation is actually to be made, m_translate calls to
96 VG_(redir_do_lookup) in this module to find out if the
97 translation's address should be redirected.
98*/
99
sewardjcbdddcf2005-03-10 23:23:45 +0000100/*------------------------------------------------------------*/
sewardj0ec07f32006-01-12 12:32:32 +0000101/*--- Semantics ---*/
sewardjcbdddcf2005-03-10 23:23:45 +0000102/*------------------------------------------------------------*/
103
sewardj0ec07f32006-01-12 12:32:32 +0000104/* The redirector holds two pieces of state:
njnd9109c62005-06-26 04:49:25 +0000105
sewardj0ec07f32006-01-12 12:32:32 +0000106 Specs - a set of (soname pattern, fnname pattern) -> redir addr
107 Active - a set of orig addr -> (bool, redir addr)
sewardjcbdddcf2005-03-10 23:23:45 +0000108
sewardj0ec07f32006-01-12 12:32:32 +0000109 Active is the currently active set of bindings that the translator
110 consults. Specs is the current set of specifications as harvested
111 from reading symbol tables of the currently loaded objects.
sewardjcbdddcf2005-03-10 23:23:45 +0000112
sewardj0ec07f32006-01-12 12:32:32 +0000113 Active is a pure function of Specs and the current symbol table
114 state (maintained by m_debuginfo). Call the latter SyminfoState.
sewardjcbdddcf2005-03-10 23:23:45 +0000115
sewardj0ec07f32006-01-12 12:32:32 +0000116 Therefore whenever either Specs or SyminfoState changes, Active
117 must be recomputed. [Inefficient if done naively, but this is a
118 spec].
njn8a96ec52005-10-15 15:48:52 +0000119
sewardj0ec07f32006-01-12 12:32:32 +0000120 Active is computed as follows:
sewardjcbdddcf2005-03-10 23:23:45 +0000121
sewardj0ec07f32006-01-12 12:32:32 +0000122 Active = empty
123 for spec in Specs {
124 sopatt = spec.soname pattern
125 fnpatt = spec.fnname pattern
126 redir = spec.redir addr
127 for so matching sopatt in SyminfoState {
128 for fn matching fnpatt in fnnames_of(so) {
129 &fn -> redir is added to Active
130 }
131 }
tom748a1312005-04-02 15:53:01 +0000132 }
133
sewardj0ec07f32006-01-12 12:32:32 +0000134 [as an implementation detail, when a binding (orig -> redir) is
135 deleted from Active as a result of recomputing it, then all
136 translations intersecting redir must be deleted. However, this is
137 not part of the spec].
138
139 [Active also depends on where the aspacemgr has decided to put all
140 the pieces of code -- that affects the "orig addr" and "redir addr"
141 values.]
142
143 ---------------------
144
145 That completes the spec, apart from one difficult issue: duplicates.
146
147 Clearly we must impose the requirement that domain(Active) contains
148 no duplicates. The difficulty is how to constrain Specs enough to
149 avoid getting into that situation. It's easy to write specs which
150 could cause conflicting bindings in Active, eg:
151
152 (libpthread.so, pthread_mutex_lock) -> a1
153 (libpthread.so, pthread_*) -> a2
154
155 for a1 != a2. Or even hairier:
156
157 (libpthread.so, pthread_mutex_*) -> a1
158 (libpthread.so, pthread_*_lock) -> a2
159
160 I can't think of any sane way of detecting when an addition to
161 Specs would generate conflicts. However, considering we don't
162 actually want to have a system that allows this, I propose this:
163 all changes to Specs are acceptable. But, when recomputing Active
164 following the change, if the same orig is bound to more than one
165 redir, then the first binding for orig is retained, and all the
166 rest ignored.
167
168 ===========================================================
169 ===========================================================
170 Incremental implementation:
171
sewardjf9ebc392010-05-09 22:30:43 +0000172 When a new DebugInfo appears:
sewardj0ec07f32006-01-12 12:32:32 +0000173 - it may be the source of new specs
174 - it may be the source of new matches for existing specs
175 Therefore:
176
sewardjf9ebc392010-05-09 22:30:43 +0000177 - (new Specs x existing DebugInfos): scan all symbols in the new
178 DebugInfo to find new specs. Each of these needs to be compared
179 against all symbols in all the existing DebugInfos to generate
sewardj0ec07f32006-01-12 12:32:32 +0000180 new actives.
181
sewardjf9ebc392010-05-09 22:30:43 +0000182 - (existing Specs x new DebugInfo): scan all symbols in the
183 DebugInfo, trying to match them to any existing specs, also
184 generating new actives.
sewardj0ec07f32006-01-12 12:32:32 +0000185
sewardjf9ebc392010-05-09 22:30:43 +0000186 - (new Specs x new DebugInfo): scan all symbols in the new
187 DebugInfo, trying to match them against the new specs, to
188 generate new actives.
sewardj0ec07f32006-01-12 12:32:32 +0000189
190 - Finally, add new new specs to the current set of specs.
191
192 When adding a new active (s,d) to the Actives:
193 lookup s in Actives
194 if already bound to d, ignore
195 if already bound to something other than d, complain loudly and ignore
196 else add (s,d) to Actives
197 and discard (s,1) and (d,1) (maybe overly conservative)
198
sewardjf9ebc392010-05-09 22:30:43 +0000199 When a DebugInfo disappears:
sewardj0ec07f32006-01-12 12:32:32 +0000200 - delete all specs acquired from the seginfo
201 - delete all actives derived from the just-deleted specs
202 - if each active (s,d) deleted, discard (s,1) and (d,1)
203*/
204
205
206/*------------------------------------------------------------*/
207/*--- REDIRECTION SPECIFICATIONS ---*/
208/*------------------------------------------------------------*/
209
210/* A specification of a redirection we want to do. Note that because
211 both the "from" soname and function name may contain wildcards, the
sewardj5092a972007-11-16 18:45:40 +0000212 spec can match an arbitrary number of times.
213
214 16 Nov 2007: Comments re .mandatory field: The initial motivation
215 for this is making Memcheck work sanely on glibc-2.6.X ppc32-linux.
216 We really need to intercept 'strlen' in ld.so right from startup.
217 If ld.so does not have a visible 'strlen' symbol, Memcheck
218 generates an impossible number of errors resulting from highly
219 tuned strlen implementation in ld.so, and is completely unusable
220 -- the resulting undefinedness eventually seeps everywhere. */
sewardj0ec07f32006-01-12 12:32:32 +0000221typedef
222 struct _Spec {
223 struct _Spec* next; /* linked list */
sewardjdf24cf12007-11-16 18:32:40 +0000224 /* FIXED PARTS -- set when created and not changed */
sewardj0ec07f32006-01-12 12:32:32 +0000225 HChar* from_sopatt; /* from soname pattern */
226 HChar* from_fnpatt; /* from fnname pattern */
227 Addr to_addr; /* where redirecting to */
228 Bool isWrap; /* wrap or replacement? */
sewardjf499c7d2009-08-10 19:39:02 +0000229 const HChar** mandatory; /* non-NULL ==> abort V and print the
230 strings if from_sopatt is loaded but
231 from_fnpatt cannot be found */
sewardjdf24cf12007-11-16 18:32:40 +0000232 /* VARIABLE PARTS -- used transiently whilst processing redirections */
233 Bool mark; /* set if spec requires further processing */
234 Bool done; /* set if spec was successfully matched */
sewardj0ec07f32006-01-12 12:32:32 +0000235 }
236 Spec;
237
sewardjf9ebc392010-05-09 22:30:43 +0000238/* Top-level data structure. It contains a pointer to a DebugInfo and
239 also a list of the specs harvested from that DebugInfo. Note that
sewardj0ec07f32006-01-12 12:32:32 +0000240 seginfo is allowed to be NULL, meaning that the specs are
241 pre-loaded ones at startup and are not associated with any
242 particular seginfo. */
243typedef
244 struct _TopSpec {
245 struct _TopSpec* next; /* linked list */
sewardjb8b79ad2008-03-03 01:35:41 +0000246 DebugInfo* seginfo; /* symbols etc */
247 Spec* specs; /* specs pulled out of seginfo */
248 Bool mark; /* transient temporary used during deletion */
sewardj0ec07f32006-01-12 12:32:32 +0000249 }
250 TopSpec;
251
252/* This is the top level list of redirections. m_debuginfo maintains
sewardjf9ebc392010-05-09 22:30:43 +0000253 a list of DebugInfos, and the idea here is to maintain a list with
sewardj0ec07f32006-01-12 12:32:32 +0000254 the same number of elements (in fact, with one more element, so as
255 to record abovementioned preloaded specifications.) */
256static TopSpec* topSpecs = NULL;
257
258
259/*------------------------------------------------------------*/
260/*--- CURRENTLY ACTIVE REDIRECTIONS ---*/
261/*------------------------------------------------------------*/
262
263/* Represents a currently active binding. If either parent_spec or
264 parent_sym is NULL, then this binding was hardwired at startup and
265 should not be deleted. Same is true if either parent's seginfo
266 field is NULL. */
267typedef
268 struct {
269 Addr from_addr; /* old addr -- MUST BE THE FIRST WORD! */
270 Addr to_addr; /* where redirecting to */
271 TopSpec* parent_spec; /* the TopSpec which supplied the Spec */
272 TopSpec* parent_sym; /* the TopSpec which supplied the symbol */
273 Bool isWrap; /* wrap or replacement? */
tomd2645142009-10-29 09:27:11 +0000274 Bool isIFunc; /* indirect function? */
sewardj0ec07f32006-01-12 12:32:32 +0000275 }
276 Active;
277
278/* The active set is a fast lookup table */
279static OSet* activeSet = NULL;
280
tomd2645142009-10-29 09:27:11 +0000281/* Wrapper routine for indirect functions */
282static Addr iFuncWrapper;
sewardj0ec07f32006-01-12 12:32:32 +0000283
284/*------------------------------------------------------------*/
285/*--- FWDses ---*/
286/*------------------------------------------------------------*/
287
288static void maybe_add_active ( Active /*by value; callee copies*/ );
289
sewardj9c606bd2008-09-18 18:12:50 +0000290static void* dinfo_zalloc(HChar* ec, SizeT);
sewardjb8b79ad2008-03-03 01:35:41 +0000291static void dinfo_free(void*);
sewardj9c606bd2008-09-18 18:12:50 +0000292static HChar* dinfo_strdup(HChar* ec, HChar*);
sewardj0ec07f32006-01-12 12:32:32 +0000293static Bool is_plausible_guest_addr(Addr);
sewardj6b9cc872006-10-17 01:39:30 +0000294static Bool is_aix5_glink_idiom(Addr);
sewardj0ec07f32006-01-12 12:32:32 +0000295
296static void show_redir_state ( HChar* who );
297static void show_active ( HChar* left, Active* act );
298
sewardja672ea32006-04-29 18:03:14 +0000299static void handle_maybe_load_notifier( const UChar* soname,
300 HChar* symbol, Addr addr );
sewardj0ec07f32006-01-12 12:32:32 +0000301
sewardjf9ebc392010-05-09 22:30:43 +0000302static void handle_require_text_symbols ( DebugInfo* );
sewardj0ec07f32006-01-12 12:32:32 +0000303
304/*------------------------------------------------------------*/
305/*--- NOTIFICATIONS ---*/
306/*------------------------------------------------------------*/
307
308static
309void generate_and_add_actives (
310 /* spec list and the owning TopSpec */
311 Spec* specs,
312 TopSpec* parent_spec,
sewardjb8b79ad2008-03-03 01:35:41 +0000313 /* debuginfo and the owning TopSpec */
314 DebugInfo* di,
sewardj0ec07f32006-01-12 12:32:32 +0000315 TopSpec* parent_sym
316 );
317
sewardjb8b79ad2008-03-03 01:35:41 +0000318/* Notify m_redir of the arrival of a new DebugInfo. This is fairly
sewardj0ec07f32006-01-12 12:32:32 +0000319 complex, but the net effect is to (1) add a new entry to the
320 topspecs list, and (2) figure out what new binding are now active,
321 and, as a result, add them to the actives mapping. */
322
323#define N_DEMANGLED 256
324
sewardjb8b79ad2008-03-03 01:35:41 +0000325void VG_(redir_notify_new_DebugInfo)( DebugInfo* newsi )
sewardj0ec07f32006-01-12 12:32:32 +0000326{
sewardj6b9cc872006-10-17 01:39:30 +0000327 Bool ok, isWrap;
328 Int i, nsyms;
329 Spec* specList;
330 Spec* spec;
331 TopSpec* ts;
332 TopSpec* newts;
333 HChar* sym_name;
334 Addr sym_addr, sym_toc;
335 HChar demangled_sopatt[N_DEMANGLED];
336 HChar demangled_fnpatt[N_DEMANGLED];
337 Bool check_ppcTOCs = False;
sewardjb8b79ad2008-03-03 01:35:41 +0000338 Bool isText;
sewardja672ea32006-04-29 18:03:14 +0000339 const UChar* newsi_soname;
340
sewardj6b9cc872006-10-17 01:39:30 +0000341# if defined(VG_PLAT_USES_PPCTOC)
342 check_ppcTOCs = True;
343# endif
344
sewardj0ec07f32006-01-12 12:32:32 +0000345 vg_assert(newsi);
sewardje3f1e592009-07-31 09:41:29 +0000346 newsi_soname = VG_(DebugInfo_get_soname)(newsi);
sewardja672ea32006-04-29 18:03:14 +0000347 vg_assert(newsi_soname != NULL);
sewardj0ec07f32006-01-12 12:32:32 +0000348
349 /* stay sane: we don't already have this. */
350 for (ts = topSpecs; ts; ts = ts->next)
351 vg_assert(ts->seginfo != newsi);
352
sewardjb8b79ad2008-03-03 01:35:41 +0000353 /* scan this DebugInfo's symbol table, pulling out and demangling
sewardj0ec07f32006-01-12 12:32:32 +0000354 any specs found */
355
356 specList = NULL; /* the spec list we're building up */
357
sewardje3f1e592009-07-31 09:41:29 +0000358 nsyms = VG_(DebugInfo_syms_howmany)( newsi );
sewardj0ec07f32006-01-12 12:32:32 +0000359 for (i = 0; i < nsyms; i++) {
tomd2645142009-10-29 09:27:11 +0000360 VG_(DebugInfo_syms_getidx)( newsi, i, &sym_addr, &sym_toc,
361 NULL, &sym_name, &isText, NULL );
sewardj0ec07f32006-01-12 12:32:32 +0000362 ok = VG_(maybe_Z_demangle)( sym_name, demangled_sopatt, N_DEMANGLED,
sewardj6b9cc872006-10-17 01:39:30 +0000363 demangled_fnpatt, N_DEMANGLED, &isWrap );
sewardjb8b79ad2008-03-03 01:35:41 +0000364 /* ignore data symbols */
365 if (!isText)
366 continue;
sewardj0ec07f32006-01-12 12:32:32 +0000367 if (!ok) {
368 /* It's not a full-scale redirect, but perhaps it is a load-notify
369 fn? Let the load-notify department see it. */
sewardja672ea32006-04-29 18:03:14 +0000370 handle_maybe_load_notifier( newsi_soname, sym_name, sym_addr );
sewardj0ec07f32006-01-12 12:32:32 +0000371 continue;
372 }
sewardj6b9cc872006-10-17 01:39:30 +0000373 if (check_ppcTOCs && sym_toc == 0) {
374 /* This platform uses toc pointers, but none could be found
375 for this symbol, so we can't safely redirect/wrap to it.
376 Just skip it; we'll make a second pass over the symbols in
377 the following loop, and complain at that point. */
378 continue;
379 }
sewardj9c606bd2008-09-18 18:12:50 +0000380 spec = dinfo_zalloc("redir.rnnD.1", sizeof(Spec));
sewardj0ec07f32006-01-12 12:32:32 +0000381 vg_assert(spec);
sewardj9c606bd2008-09-18 18:12:50 +0000382 spec->from_sopatt = dinfo_strdup("redir.rnnD.2", demangled_sopatt);
383 spec->from_fnpatt = dinfo_strdup("redir.rnnD.3", demangled_fnpatt);
sewardj0ec07f32006-01-12 12:32:32 +0000384 vg_assert(spec->from_sopatt);
385 vg_assert(spec->from_fnpatt);
386 spec->to_addr = sym_addr;
387 spec->isWrap = isWrap;
388 /* check we're not adding manifestly stupid destinations */
389 vg_assert(is_plausible_guest_addr(sym_addr));
390 spec->next = specList;
391 spec->mark = False; /* not significant */
sewardjdf24cf12007-11-16 18:32:40 +0000392 spec->done = False; /* not significant */
sewardj0ec07f32006-01-12 12:32:32 +0000393 specList = spec;
394 }
395
sewardj6b9cc872006-10-17 01:39:30 +0000396 if (check_ppcTOCs) {
397 for (i = 0; i < nsyms; i++) {
tomd2645142009-10-29 09:27:11 +0000398 VG_(DebugInfo_syms_getidx)( newsi, i, &sym_addr, &sym_toc,
399 NULL, &sym_name, &isText, NULL );
sewardjb8b79ad2008-03-03 01:35:41 +0000400 ok = isText
401 && VG_(maybe_Z_demangle)(
402 sym_name, demangled_sopatt, N_DEMANGLED,
403 demangled_fnpatt, N_DEMANGLED, &isWrap );
sewardj6b9cc872006-10-17 01:39:30 +0000404 if (!ok)
405 /* not a redirect. Ignore. */
406 continue;
407 if (sym_toc != 0)
408 /* has a valid toc pointer. Ignore. */
409 continue;
410
411 for (spec = specList; spec; spec = spec->next)
412 if (0 == VG_(strcmp)(spec->from_sopatt, demangled_sopatt)
413 && 0 == VG_(strcmp)(spec->from_fnpatt, demangled_fnpatt))
414 break;
415 if (spec)
sewardjdf24cf12007-11-16 18:32:40 +0000416 /* a redirect to some other copy of that symbol, which
417 does have a TOC value, already exists */
418 continue;
sewardj6b9cc872006-10-17 01:39:30 +0000419
420 /* Complain */
421 VG_(message)(Vg_DebugMsg,
sewardj738856f2009-07-15 14:48:32 +0000422 "WARNING: no TOC ptr for redir/wrap to %s %s\n",
sewardj6b9cc872006-10-17 01:39:30 +0000423 demangled_sopatt, demangled_fnpatt);
424 }
425 }
426
sewardjf9ebc392010-05-09 22:30:43 +0000427 /* Ok. Now specList holds the list of specs from the DebugInfo.
sewardj0ec07f32006-01-12 12:32:32 +0000428 Build a new TopSpec, but don't add it to topSpecs yet. */
sewardj9c606bd2008-09-18 18:12:50 +0000429 newts = dinfo_zalloc("redir.rnnD.4", sizeof(TopSpec));
sewardj0ec07f32006-01-12 12:32:32 +0000430 vg_assert(newts);
431 newts->next = NULL; /* not significant */
432 newts->seginfo = newsi;
433 newts->specs = specList;
434 newts->mark = False; /* not significant */
435
436 /* We now need to augment the active set with the following partial
437 cross product:
438
439 (1) actives formed by matching the new specs in specList against
440 all symbols currently listed in topSpecs
441
442 (2) actives formed by matching the new symbols in newsi against
443 all specs currently listed in topSpecs
444
445 (3) actives formed by matching the new symbols in newsi against
446 the new specs in specList
447
448 This is necessary in order to maintain the invariant that
449 Actives contains all bindings generated by matching ALL specs in
450 topSpecs against ALL symbols in topSpecs (that is, a cross
451 product of ALL known specs against ALL known symbols).
452 */
453 /* Case (1) */
454 for (ts = topSpecs; ts; ts = ts->next) {
455 if (ts->seginfo)
456 generate_and_add_actives( specList, newts,
457 ts->seginfo, ts );
458 }
sewardj6b9cc872006-10-17 01:39:30 +0000459
sewardj0ec07f32006-01-12 12:32:32 +0000460 /* Case (2) */
461 for (ts = topSpecs; ts; ts = ts->next) {
462 generate_and_add_actives( ts->specs, ts,
463 newsi, newts );
464 }
465
466 /* Case (3) */
467 generate_and_add_actives( specList, newts,
468 newsi, newts );
469
470 /* Finally, add the new TopSpec. */
471 newts->next = topSpecs;
472 topSpecs = newts;
473
474 if (VG_(clo_trace_redir))
sewardjb8b79ad2008-03-03 01:35:41 +0000475 show_redir_state("after VG_(redir_notify_new_DebugInfo)");
sewardjf9ebc392010-05-09 22:30:43 +0000476
477 /* Really finally (quite unrelated to all the above) check the
478 names in the module against any --require-text-symbol=
479 specifications we might have. */
480 handle_require_text_symbols(newsi);
sewardj0ec07f32006-01-12 12:32:32 +0000481}
482
483#undef N_DEMANGLED
484
tomd2645142009-10-29 09:27:11 +0000485/* Add a new target for an indirect function. Adds a new redirection
486 for the indirection function with address old_from that redirects
487 the ordinary function with address new_from to the target address
488 of the original redirection. */
489
490void VG_(redir_add_ifunc_target)( Addr old_from, Addr new_from )
491{
492 Active *old, new;
493
494 old = VG_(OSetGen_Lookup)(activeSet, &old_from);
495 vg_assert(old);
496 vg_assert(old->isIFunc);
497
498 new = *old;
499 new.from_addr = new_from;
500 new.isIFunc = False;
501 maybe_add_active (new);
502
503 if (VG_(clo_trace_redir)) {
504 VG_(message)( Vg_DebugMsg,
505 "Adding redirect for indirect function 0x%llx from 0x%llx -> 0x%llx\n",
506 (ULong)old_from, (ULong)new_from, (ULong)new.to_addr );
507 }
508}
sewardj0ec07f32006-01-12 12:32:32 +0000509
510/* Do one element of the basic cross product: add to the active set,
511 all matches resulting from comparing all the given specs against
512 all the symbols in the given seginfo. If a conflicting binding
513 would thereby arise, don't add it, but do complain. */
514
515static
516void generate_and_add_actives (
517 /* spec list and the owning TopSpec */
518 Spec* specs,
519 TopSpec* parent_spec,
520 /* seginfo and the owning TopSpec */
sewardjb8b79ad2008-03-03 01:35:41 +0000521 DebugInfo* di,
sewardj0ec07f32006-01-12 12:32:32 +0000522 TopSpec* parent_sym
523 )
524{
525 Spec* sp;
tomd2645142009-10-29 09:27:11 +0000526 Bool anyMark, isText, isIFunc;
sewardj0ec07f32006-01-12 12:32:32 +0000527 Active act;
528 Int nsyms, i;
529 Addr sym_addr;
530 HChar* sym_name;
531
sewardjdf24cf12007-11-16 18:32:40 +0000532 /* First figure out which of the specs match the seginfo's soname.
533 Also clear the 'done' bits, so that after the main loop below
534 tell which of the Specs really did get done. */
sewardj0ec07f32006-01-12 12:32:32 +0000535 anyMark = False;
536 for (sp = specs; sp; sp = sp->next) {
sewardjdf24cf12007-11-16 18:32:40 +0000537 sp->done = False;
sewardj0ec07f32006-01-12 12:32:32 +0000538 sp->mark = VG_(string_match)( sp->from_sopatt,
sewardje3f1e592009-07-31 09:41:29 +0000539 VG_(DebugInfo_get_soname)(di) );
sewardj0ec07f32006-01-12 12:32:32 +0000540 anyMark = anyMark || sp->mark;
541 }
542
543 /* shortcut: if none of the sonames match, there will be no bindings. */
544 if (!anyMark)
545 return;
546
547 /* Iterate outermost over the symbols in the seginfo, in the hope
548 of trashing the caches less. */
sewardje3f1e592009-07-31 09:41:29 +0000549 nsyms = VG_(DebugInfo_syms_howmany)( di );
sewardj0ec07f32006-01-12 12:32:32 +0000550 for (i = 0; i < nsyms; i++) {
sewardje3f1e592009-07-31 09:41:29 +0000551 VG_(DebugInfo_syms_getidx)( di, i, &sym_addr, NULL, NULL,
tomd2645142009-10-29 09:27:11 +0000552 &sym_name, &isText, &isIFunc );
sewardjb8b79ad2008-03-03 01:35:41 +0000553
554 /* ignore data symbols */
555 if (!isText)
556 continue;
sewardj6b9cc872006-10-17 01:39:30 +0000557
558 /* On AIX, we cannot redirect calls to a so-called glink
559 function for reasons which are not obvious - something to do
560 with saving r2 across the call. Not a problem, as we don't
561 want to anyway; presumably it is the target of the glink we
562 need to redirect. Hence just spot them and ignore them.
563 They are always of a very specific (more or less
564 ABI-mandated) form. */
565 if (is_aix5_glink_idiom(sym_addr))
566 continue;
567
sewardj0ec07f32006-01-12 12:32:32 +0000568 for (sp = specs; sp; sp = sp->next) {
569 if (!sp->mark)
570 continue; /* soname doesn't match */
571 if (VG_(string_match)( sp->from_fnpatt, sym_name )) {
572 /* got a new binding. Add to collection. */
573 act.from_addr = sym_addr;
574 act.to_addr = sp->to_addr;
575 act.parent_spec = parent_spec;
576 act.parent_sym = parent_sym;
577 act.isWrap = sp->isWrap;
tomd2645142009-10-29 09:27:11 +0000578 act.isIFunc = isIFunc;
sewardjdf24cf12007-11-16 18:32:40 +0000579 sp->done = True;
sewardj0ec07f32006-01-12 12:32:32 +0000580 maybe_add_active( act );
581 }
sewardjdf24cf12007-11-16 18:32:40 +0000582 } /* for (sp = specs; sp; sp = sp->next) */
583 } /* for (i = 0; i < nsyms; i++) */
584
585 /* Now, finally, look for Specs which were marked to be done, but
586 didn't get matched. If any such are mandatory we must abort the
587 system at this point. */
588 for (sp = specs; sp; sp = sp->next) {
589 if (!sp->mark)
590 continue;
591 if (sp->mark && (!sp->done) && sp->mandatory)
592 break;
593 }
594 if (sp) {
sewardjf499c7d2009-08-10 19:39:02 +0000595 const HChar** strp;
sewardjdf24cf12007-11-16 18:32:40 +0000596 HChar* v = "valgrind: ";
597 vg_assert(sp->mark);
598 vg_assert(!sp->done);
599 vg_assert(sp->mandatory);
600 VG_(printf)("\n");
601 VG_(printf)(
602 "%sFatal error at startup: a function redirection\n", v);
603 VG_(printf)(
604 "%swhich is mandatory for this platform-tool combination\n", v);
605 VG_(printf)(
606 "%scannot be set up. Details of the redirection are:\n", v);
607 VG_(printf)(
608 "%s\n", v);
609 VG_(printf)(
610 "%sA must-be-redirected function\n", v);
611 VG_(printf)(
612 "%swhose name matches the pattern: %s\n", v, sp->from_fnpatt);
613 VG_(printf)(
614 "%sin an object with soname matching: %s\n", v, sp->from_sopatt);
615 VG_(printf)(
616 "%swas not found whilst processing\n", v);
617 VG_(printf)(
sewardje3f1e592009-07-31 09:41:29 +0000618 "%ssymbols from the object with soname: %s\n",
619 v, VG_(DebugInfo_get_soname)(di));
sewardjdf24cf12007-11-16 18:32:40 +0000620 VG_(printf)(
621 "%s\n", v);
sewardjb57e6932009-08-02 12:21:31 +0000622
623 for (strp = sp->mandatory; *strp; strp++)
624 VG_(printf)(
625 "%s%s\n", v, *strp);
626
sewardjdf24cf12007-11-16 18:32:40 +0000627 VG_(printf)(
628 "%s\n", v);
629 VG_(printf)(
630 "%sCannot continue -- exiting now. Sorry.\n", v);
631 VG_(printf)("\n");
632 VG_(exit)(1);
sewardj0ec07f32006-01-12 12:32:32 +0000633 }
634}
635
636
637/* Add an act (passed by value; is copied here) and deal with
638 conflicting bindings. */
639static void maybe_add_active ( Active act )
640{
641 HChar* what = NULL;
642 Active* old;
643
644 /* Complain and ignore manifestly bogus 'from' addresses.
645
646 Kludge: because this can get called befor the trampoline area (a
647 bunch of magic 'to' addresses) has its ownership changed from V
648 to C, we can't check the 'to' address similarly. Sigh.
649
650 amd64-linux hack: the vsysinfo pages appear to have no
651 permissions
652 ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0
653 so skip the check for them. */
654 if (!is_plausible_guest_addr(act.from_addr)
655# if defined(VGP_amd64_linux)
656 && act.from_addr != 0xFFFFFFFFFF600000ULL
657 && act.from_addr != 0xFFFFFFFFFF600400ULL
658# endif
659 ) {
660 what = "redirection from-address is in non-executable area";
661 goto bad;
662 }
663
njne2a9ad32007-09-17 05:30:48 +0000664 old = VG_(OSetGen_Lookup)( activeSet, &act.from_addr );
sewardj0ec07f32006-01-12 12:32:32 +0000665 if (old) {
666 /* Dodgy. Conflicting binding. */
667 vg_assert(old->from_addr == act.from_addr);
668 if (old->to_addr != act.to_addr) {
669 /* we have to ignore it -- otherwise activeSet would contain
670 conflicting bindings. */
671 what = "new redirection conflicts with existing -- ignoring it";
672 goto bad;
njnd9109c62005-06-26 04:49:25 +0000673 } else {
sewardj0ec07f32006-01-12 12:32:32 +0000674 /* This appears to be a duplicate of an existing binding.
675 Safe(ish) -- ignore. */
676 /* XXXXXXXXXXX COMPLAIN if new and old parents differ */
tom748a1312005-04-02 15:53:01 +0000677 }
njnd9109c62005-06-26 04:49:25 +0000678 } else {
njne2a9ad32007-09-17 05:30:48 +0000679 Active* a = VG_(OSetGen_AllocNode)(activeSet, sizeof(Active));
sewardj0ec07f32006-01-12 12:32:32 +0000680 vg_assert(a);
681 *a = act;
njne2a9ad32007-09-17 05:30:48 +0000682 VG_(OSetGen_Insert)(activeSet, a);
sewardj0ec07f32006-01-12 12:32:32 +0000683 /* Now that a new from->to redirection is in force, we need to
684 get rid of any translations intersecting 'from' in order that
685 they get redirected to 'to'. So discard them. Just for
686 paranoia (but, I believe, unnecessarily), discard 'to' as
687 well. */
688 VG_(discard_translations)( (Addr64)act.from_addr, 1,
sewardjb8b79ad2008-03-03 01:35:41 +0000689 "redir_new_DebugInfo(from_addr)");
sewardj0ec07f32006-01-12 12:32:32 +0000690 VG_(discard_translations)( (Addr64)act.to_addr, 1,
sewardjb8b79ad2008-03-03 01:35:41 +0000691 "redir_new_DebugInfo(to_addr)");
sewardj0ec07f32006-01-12 12:32:32 +0000692 }
693 return;
694
695 bad:
696 vg_assert(what);
697 if (VG_(clo_verbosity) > 1) {
sewardj738856f2009-07-15 14:48:32 +0000698 VG_(message)(Vg_UserMsg, "WARNING: %s\n", what);
sewardj0ec07f32006-01-12 12:32:32 +0000699 show_active( " new: ", &act);
njnd9109c62005-06-26 04:49:25 +0000700 }
tom748a1312005-04-02 15:53:01 +0000701}
702
njnd9109c62005-06-26 04:49:25 +0000703
sewardjb8b79ad2008-03-03 01:35:41 +0000704/* Notify m_redir of the deletion of a DebugInfo. This is relatively
sewardj0ec07f32006-01-12 12:32:32 +0000705 simple -- just get rid of all actives derived from it, and free up
706 the associated list elements. */
707
sewardjb8b79ad2008-03-03 01:35:41 +0000708void VG_(redir_notify_delete_DebugInfo)( DebugInfo* delsi )
sewardj0ec07f32006-01-12 12:32:32 +0000709{
710 TopSpec* ts;
711 TopSpec* tsPrev;
712 Spec* sp;
713 Spec* sp_next;
714 OSet* tmpSet;
715 Active* act;
716 Bool delMe;
njne2a9ad32007-09-17 05:30:48 +0000717 Addr addr;
sewardj0ec07f32006-01-12 12:32:32 +0000718
719 vg_assert(delsi);
720
721 /* Search for it, and make tsPrev point to the previous entry, if
722 any. */
723 tsPrev = NULL;
724 ts = topSpecs;
725 while (True) {
726 if (ts == NULL) break;
727 if (ts->seginfo == delsi) break;
728 tsPrev = ts;
729 ts = ts->next;
730 }
731
sewardjb8b79ad2008-03-03 01:35:41 +0000732 vg_assert(ts); /* else we don't have the deleted DebugInfo */
sewardj0ec07f32006-01-12 12:32:32 +0000733 vg_assert(ts->seginfo == delsi);
734
735 /* Traverse the actives, copying the addresses of those we intend
736 to delete into tmpSet. */
sewardj9c606bd2008-09-18 18:12:50 +0000737 tmpSet = VG_(OSetWord_Create)(dinfo_zalloc, "redir.rndD.1", dinfo_free);
sewardj0ec07f32006-01-12 12:32:32 +0000738
739 ts->mark = True;
740
njne2a9ad32007-09-17 05:30:48 +0000741 VG_(OSetGen_ResetIter)( activeSet );
742 while ( (act = VG_(OSetGen_Next)(activeSet)) ) {
sewardj0ec07f32006-01-12 12:32:32 +0000743 delMe = act->parent_spec != NULL
744 && act->parent_sym != NULL
745 && act->parent_spec->seginfo != NULL
746 && act->parent_sym->seginfo != NULL
747 && (act->parent_spec->mark || act->parent_sym->mark);
748
749 /* While we're at it, a bit of paranoia: delete any actives
sewardje6647f42006-03-12 00:39:19 +0000750 which don't have both feet in valid client executable areas.
751 But don't delete hardwired-at-startup ones; these are denoted
752 by having parent_spec or parent_sym being NULL. */
753 if ( (!delMe)
754 && act->parent_spec != NULL
755 && act->parent_sym != NULL ) {
756 if (!is_plausible_guest_addr(act->from_addr))
757 delMe = True;
758 if (!is_plausible_guest_addr(act->to_addr))
759 delMe = True;
sewardj0ec07f32006-01-12 12:32:32 +0000760 }
761
762 if (delMe) {
njne2a9ad32007-09-17 05:30:48 +0000763 VG_(OSetWord_Insert)( tmpSet, act->from_addr );
sewardj0ec07f32006-01-12 12:32:32 +0000764 /* While we have our hands on both the 'from' and 'to'
765 of this Active, do paranoid stuff with tt/tc. */
766 VG_(discard_translations)( (Addr64)act->from_addr, 1,
sewardjb8b79ad2008-03-03 01:35:41 +0000767 "redir_del_DebugInfo(from_addr)");
sewardj0ec07f32006-01-12 12:32:32 +0000768 VG_(discard_translations)( (Addr64)act->to_addr, 1,
sewardjb8b79ad2008-03-03 01:35:41 +0000769 "redir_del_DebugInfo(to_addr)");
sewardj0ec07f32006-01-12 12:32:32 +0000770 }
771 }
772
njne2a9ad32007-09-17 05:30:48 +0000773 /* Now traverse tmpSet, deleting corresponding elements in activeSet. */
774 VG_(OSetWord_ResetIter)( tmpSet );
775 while ( VG_(OSetWord_Next)(tmpSet, &addr) ) {
776 act = VG_(OSetGen_Remove)( activeSet, &addr );
sewardj0ec07f32006-01-12 12:32:32 +0000777 vg_assert(act);
njne2a9ad32007-09-17 05:30:48 +0000778 VG_(OSetGen_FreeNode)( activeSet, act );
sewardj0ec07f32006-01-12 12:32:32 +0000779 }
780
njne2a9ad32007-09-17 05:30:48 +0000781 VG_(OSetWord_Destroy)( tmpSet );
sewardj0ec07f32006-01-12 12:32:32 +0000782
783 /* The Actives set is now cleaned up. Free up this TopSpec and
784 everything hanging off it. */
785 for (sp = ts->specs; sp; sp = sp_next) {
sewardjb8b79ad2008-03-03 01:35:41 +0000786 if (sp->from_sopatt) dinfo_free(sp->from_sopatt);
787 if (sp->from_fnpatt) dinfo_free(sp->from_fnpatt);
sewardj0ec07f32006-01-12 12:32:32 +0000788 sp_next = sp->next;
sewardjb8b79ad2008-03-03 01:35:41 +0000789 dinfo_free(sp);
sewardj0ec07f32006-01-12 12:32:32 +0000790 }
791
792 if (tsPrev == NULL) {
793 /* first in list */
794 topSpecs = ts->next;
njnd9109c62005-06-26 04:49:25 +0000795 } else {
sewardj0ec07f32006-01-12 12:32:32 +0000796 tsPrev->next = ts->next;
sewardjcbdddcf2005-03-10 23:23:45 +0000797 }
sewardjb8b79ad2008-03-03 01:35:41 +0000798 dinfo_free(ts);
sewardjcbdddcf2005-03-10 23:23:45 +0000799
sewardj0ec07f32006-01-12 12:32:32 +0000800 if (VG_(clo_trace_redir))
sewardjb8b79ad2008-03-03 01:35:41 +0000801 show_redir_state("after VG_(redir_notify_delete_DebugInfo)");
sewardjcbdddcf2005-03-10 23:23:45 +0000802}
803
sewardj0ec07f32006-01-12 12:32:32 +0000804
805/*------------------------------------------------------------*/
806/*--- QUERIES (really the whole point of this module) ---*/
807/*------------------------------------------------------------*/
808
809/* This is the crucial redirection function. It answers the question:
810 should this code address be redirected somewhere else? It's used
811 just before translating a basic block. */
812Addr VG_(redir_do_lookup) ( Addr orig, Bool* isWrap )
sewardjcbdddcf2005-03-10 23:23:45 +0000813{
njne2a9ad32007-09-17 05:30:48 +0000814 Active* r = VG_(OSetGen_Lookup)(activeSet, &orig);
sewardjcbdddcf2005-03-10 23:23:45 +0000815 if (r == NULL)
sewardj0ec07f32006-01-12 12:32:32 +0000816 return orig;
sewardjcbdddcf2005-03-10 23:23:45 +0000817
818 vg_assert(r->to_addr != 0);
sewardj0ec07f32006-01-12 12:32:32 +0000819 if (isWrap)
tomd2645142009-10-29 09:27:11 +0000820 *isWrap = r->isWrap || r->isIFunc;
tom3b42e162009-11-16 09:06:09 +0000821 if (r->isIFunc) {
822 vg_assert(iFuncWrapper);
tomd2645142009-10-29 09:27:11 +0000823 return iFuncWrapper;
tom3b42e162009-11-16 09:06:09 +0000824 }
sewardjcbdddcf2005-03-10 23:23:45 +0000825 return r->to_addr;
826}
827
sewardj0ec07f32006-01-12 12:32:32 +0000828
829/*------------------------------------------------------------*/
830/*--- INITIALISATION ---*/
831/*------------------------------------------------------------*/
832
833/* Add a never-delete-me Active. */
834
835__attribute__((unused)) /* only used on amd64 */
836static void add_hardwired_active ( Addr from, Addr to )
njn8a96ec52005-10-15 15:48:52 +0000837{
sewardj0ec07f32006-01-12 12:32:32 +0000838 Active act;
839 act.from_addr = from;
840 act.to_addr = to;
841 act.parent_spec = NULL;
842 act.parent_sym = NULL;
843 act.isWrap = False;
tomb6fd3652009-11-01 16:37:33 +0000844 act.isIFunc = False;
sewardj0ec07f32006-01-12 12:32:32 +0000845 maybe_add_active( act );
njn8a96ec52005-10-15 15:48:52 +0000846}
847
sewardj0ec07f32006-01-12 12:32:32 +0000848
849/* Add a never-delete-me Spec. This is a bit of a kludge. On the
850 assumption that this is called only at startup, only handle the
851 case where topSpecs is completely empty, or if it isn't, it has
852 just one entry and that is the one with NULL seginfo -- that is the
853 entry that holds these initial specs. */
854
855__attribute__((unused)) /* not used on all platforms */
sewardjdf24cf12007-11-16 18:32:40 +0000856static void add_hardwired_spec ( HChar* sopatt, HChar* fnpatt,
857 Addr to_addr,
sewardjf499c7d2009-08-10 19:39:02 +0000858 const HChar** mandatory )
njn8a96ec52005-10-15 15:48:52 +0000859{
sewardj9c606bd2008-09-18 18:12:50 +0000860 Spec* spec = dinfo_zalloc("redir.ahs.1", sizeof(Spec));
sewardj0ec07f32006-01-12 12:32:32 +0000861 vg_assert(spec);
862
863 if (topSpecs == NULL) {
sewardj9c606bd2008-09-18 18:12:50 +0000864 topSpecs = dinfo_zalloc("redir.ahs.2", sizeof(TopSpec));
sewardj0ec07f32006-01-12 12:32:32 +0000865 vg_assert(topSpecs);
sewardjdf24cf12007-11-16 18:32:40 +0000866 /* symtab_zalloc sets all fields to zero */
sewardj0ec07f32006-01-12 12:32:32 +0000867 }
868
869 vg_assert(topSpecs != NULL);
870 vg_assert(topSpecs->next == NULL);
871 vg_assert(topSpecs->seginfo == NULL);
sewardjdf24cf12007-11-16 18:32:40 +0000872 /* FIXED PARTS */
sewardj0ec07f32006-01-12 12:32:32 +0000873 spec->from_sopatt = sopatt;
874 spec->from_fnpatt = fnpatt;
875 spec->to_addr = to_addr;
876 spec->isWrap = False;
sewardjdf24cf12007-11-16 18:32:40 +0000877 spec->mandatory = mandatory;
878 /* VARIABLE PARTS */
sewardj0ec07f32006-01-12 12:32:32 +0000879 spec->mark = False; /* not significant */
sewardjdf24cf12007-11-16 18:32:40 +0000880 spec->done = False; /* not significant */
sewardj0ec07f32006-01-12 12:32:32 +0000881
882 spec->next = topSpecs->specs;
883 topSpecs->specs = spec;
njn8a96ec52005-10-15 15:48:52 +0000884}
885
njn8a96ec52005-10-15 15:48:52 +0000886
sewardjb57e6932009-08-02 12:21:31 +0000887__attribute__((unused)) /* not used on all platforms */
888static const HChar* complain_about_stripped_glibc_ldso[]
889= { "Possible fixes: (1, short term): install glibc's debuginfo",
890 "package on this machine. (2, longer term): ask the packagers",
891 "for your Linux distribution to please in future ship a non-",
892 "stripped ld.so (or whatever the dynamic linker .so is called)",
893 "that exports the above-named function using the standard",
894 "calling conventions for this platform.",
895 NULL
896 };
897
898
sewardj0ec07f32006-01-12 12:32:32 +0000899/* Initialise the redir system, and create the initial Spec list and
900 for amd64-linux a couple of permanent active mappings. The initial
901 Specs are not converted into Actives yet, on the (checked)
sewardjb8b79ad2008-03-03 01:35:41 +0000902 assumption that no DebugInfos have so far been created, and so when
sewardj0ec07f32006-01-12 12:32:32 +0000903 they are created, that will happen. */
904
905void VG_(redir_initialise) ( void )
906{
sewardjb8b79ad2008-03-03 01:35:41 +0000907 // Assert that there are no DebugInfos so far
sewardje3f1e592009-07-31 09:41:29 +0000908 vg_assert( VG_(next_DebugInfo)(NULL) == NULL );
sewardj0ec07f32006-01-12 12:32:32 +0000909
910 // Initialise active mapping.
njne2a9ad32007-09-17 05:30:48 +0000911 activeSet = VG_(OSetGen_Create)(offsetof(Active, from_addr),
912 NULL, // Use fast comparison
sewardjb8b79ad2008-03-03 01:35:41 +0000913 dinfo_zalloc,
sewardj9c606bd2008-09-18 18:12:50 +0000914 "redir.ri.1",
sewardjb8b79ad2008-03-03 01:35:41 +0000915 dinfo_free);
sewardj0ec07f32006-01-12 12:32:32 +0000916
917 // The rest of this function just adds initial Specs.
918
919# if defined(VGP_x86_linux)
sewardj6a443b22005-11-20 19:37:54 +0000920 /* If we're using memcheck, use this intercept right from the
921 start, otherwise ld.so (glibc-2.3.5) makes a lot of noise. */
922 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
sewardj0ec07f32006-01-12 12:32:32 +0000923 add_hardwired_spec(
924 "ld-linux.so.2", "index",
sewardjdf24cf12007-11-16 18:32:40 +0000925 (Addr)&VG_(x86_linux_REDIR_FOR_index),
926 NULL
sewardj0ec07f32006-01-12 12:32:32 +0000927 );
sewardj6a443b22005-11-20 19:37:54 +0000928 }
sewardjb9bce632005-06-21 01:41:34 +0000929
sewardj0ec07f32006-01-12 12:32:32 +0000930# elif defined(VGP_amd64_linux)
njnd1af0032005-05-29 17:01:48 +0000931 /* Redirect vsyscalls to local versions */
sewardj0ec07f32006-01-12 12:32:32 +0000932 add_hardwired_active(
sewardjb9bce632005-06-21 01:41:34 +0000933 0xFFFFFFFFFF600000ULL,
934 (Addr)&VG_(amd64_linux_REDIR_FOR_vgettimeofday)
935 );
sewardj0ec07f32006-01-12 12:32:32 +0000936 add_hardwired_active(
tomf5db3b62005-06-21 13:26:17 +0000937 0xFFFFFFFFFF600400ULL,
sewardjb9bce632005-06-21 01:41:34 +0000938 (Addr)&VG_(amd64_linux_REDIR_FOR_vtime)
939 );
940
sewardjb57e6932009-08-02 12:21:31 +0000941 /* If we're using memcheck, use these intercepts right from
942 the start, otherwise ld.so makes a lot of noise. */
943 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
bart2ebe4112008-06-01 16:33:37 +0000944
sewardjb57e6932009-08-02 12:21:31 +0000945 add_hardwired_spec(
946 "ld-linux-x86-64.so.2", "strlen",
947 (Addr)&VG_(amd64_linux_REDIR_FOR_strlen),
948# if defined(GLIBC_2_2) || defined(GLIBC_2_3) || defined(GLIBC_2_4) \
949 || defined(GLIBC_2_5) || defined(GLIBC_2_6) || defined(GLIBC_2_7) \
950 || defined(GLIBC_2_8) || defined(GLIBC_2_9)
951 NULL
952# else
953 /* for glibc-2.10 and later, this is mandatory - can't sanely
954 continue without it */
955 complain_about_stripped_glibc_ldso
956# endif
957 );
958 }
959
960# elif defined(VGP_ppc32_linux)
sewardj96fcfc82005-11-18 21:12:52 +0000961 /* If we're using memcheck, use these intercepts right from
962 the start, otherwise ld.so makes a lot of noise. */
963 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
sewardj0fd30492007-11-16 19:39:49 +0000964
sewardj5092a972007-11-16 18:45:40 +0000965 /* this is mandatory - can't sanely continue without it */
sewardj0ec07f32006-01-12 12:32:32 +0000966 add_hardwired_spec(
967 "ld.so.1", "strlen",
sewardjdf24cf12007-11-16 18:32:40 +0000968 (Addr)&VG_(ppc32_linux_REDIR_FOR_strlen),
sewardjb57e6932009-08-02 12:21:31 +0000969 complain_about_stripped_glibc_ldso
sewardj96fcfc82005-11-18 21:12:52 +0000970 );
sewardj0ec07f32006-01-12 12:32:32 +0000971 add_hardwired_spec(
sewardjd5a30a42006-02-09 10:29:43 +0000972 "ld.so.1", "strcmp",
sewardjdf24cf12007-11-16 18:32:40 +0000973 (Addr)&VG_(ppc32_linux_REDIR_FOR_strcmp),
sewardj5092a972007-11-16 18:45:40 +0000974 NULL /* not mandatory - so why bother at all? */
975 /* glibc-2.6.1 (openSUSE 10.3, ppc32) seems fine without it */
sewardj96fcfc82005-11-18 21:12:52 +0000976 );
sewardjcd8c95a2006-08-25 11:48:38 +0000977 add_hardwired_spec(
978 "ld.so.1", "index",
sewardjdf24cf12007-11-16 18:32:40 +0000979 (Addr)&VG_(ppc32_linux_REDIR_FOR_strchr),
sewardj5092a972007-11-16 18:45:40 +0000980 NULL /* not mandatory - so why bother at all? */
981 /* glibc-2.6.1 (openSUSE 10.3, ppc32) seems fine without it */
sewardjcd8c95a2006-08-25 11:48:38 +0000982 );
bart2ebe4112008-06-01 16:33:37 +0000983 }
sewardjb9bce632005-06-21 01:41:34 +0000984
sewardj0ec07f32006-01-12 12:32:32 +0000985# elif defined(VGP_ppc64_linux)
sewardjf0915fc2006-01-05 14:07:04 +0000986 /* If we're using memcheck, use these intercepts right from
987 the start, otherwise ld.so makes a lot of noise. */
988 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
989
sewardj0fd30492007-11-16 19:39:49 +0000990 /* this is mandatory - can't sanely continue without it */
sewardj1a85f4f2006-01-12 21:15:35 +0000991 add_hardwired_spec(
992 "ld64.so.1", "strlen",
sewardjdf24cf12007-11-16 18:32:40 +0000993 (Addr)VG_(fnptr_to_fnentry)( &VG_(ppc64_linux_REDIR_FOR_strlen) ),
sewardjb57e6932009-08-02 12:21:31 +0000994 complain_about_stripped_glibc_ldso
sewardjdf24cf12007-11-16 18:32:40 +0000995 );
sewardjf0915fc2006-01-05 14:07:04 +0000996
sewardj1a85f4f2006-01-12 21:15:35 +0000997 add_hardwired_spec(
998 "ld64.so.1", "index",
sewardjdf24cf12007-11-16 18:32:40 +0000999 (Addr)VG_(fnptr_to_fnentry)( &VG_(ppc64_linux_REDIR_FOR_strchr) ),
sewardj0fd30492007-11-16 19:39:49 +00001000 NULL /* not mandatory - so why bother at all? */
1001 /* glibc-2.5 (FC6, ppc64) seems fine without it */
sewardjdf24cf12007-11-16 18:32:40 +00001002 );
bart2ebe4112008-06-01 16:33:37 +00001003 }
sewardj2c48c7b2005-11-29 13:05:56 +00001004
sewardj59570ff2010-01-01 11:59:33 +00001005# elif defined(VGP_arm_linux)
1006 /* If we're using memcheck, use these intercepts right from
1007 the start, otherwise ld.so makes a lot of noise. */
1008 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1009 add_hardwired_spec(
1010 "ld-linux.so.3", "strlen",
1011 (Addr)&VG_(arm_linux_REDIR_FOR_strlen),
sewardjd69cee82010-08-22 12:13:35 +00001012 complain_about_stripped_glibc_ldso
sewardj59570ff2010-01-01 11:59:33 +00001013 );
1014 //add_hardwired_spec(
1015 // "ld-linux.so.3", "index",
1016 // (Addr)&VG_(arm_linux_REDIR_FOR_index),
1017 // NULL
1018 //);
1019 add_hardwired_spec(
1020 "ld-linux.so.3", "memcpy",
1021 (Addr)&VG_(arm_linux_REDIR_FOR_memcpy),
sewardjd69cee82010-08-22 12:13:35 +00001022 complain_about_stripped_glibc_ldso
sewardj59570ff2010-01-01 11:59:33 +00001023 );
1024 }
1025 /* nothing so far */
1026
sewardj6b9cc872006-10-17 01:39:30 +00001027# elif defined(VGP_ppc32_aix5)
1028 /* nothing so far */
1029
1030# elif defined(VGP_ppc64_aix5)
1031 /* nothing so far */
1032
njnea2d6fd2010-07-01 00:20:20 +00001033# elif defined(VGP_x86_darwin)
njnf76d27a2009-05-28 01:53:07 +00001034 /* If we're using memcheck, use these intercepts right from
1035 the start, otherwise dyld makes a lot of noise. */
1036 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1037 add_hardwired_spec("dyld", "strcmp",
njnea2d6fd2010-07-01 00:20:20 +00001038 (Addr)&VG_(x86_darwin_REDIR_FOR_strcmp), NULL);
njnf76d27a2009-05-28 01:53:07 +00001039 add_hardwired_spec("dyld", "strlen",
njnea2d6fd2010-07-01 00:20:20 +00001040 (Addr)&VG_(x86_darwin_REDIR_FOR_strlen), NULL);
njnf76d27a2009-05-28 01:53:07 +00001041 add_hardwired_spec("dyld", "strcat",
njnea2d6fd2010-07-01 00:20:20 +00001042 (Addr)&VG_(x86_darwin_REDIR_FOR_strcat), NULL);
njnf76d27a2009-05-28 01:53:07 +00001043 add_hardwired_spec("dyld", "strcpy",
njnea2d6fd2010-07-01 00:20:20 +00001044 (Addr)&VG_(x86_darwin_REDIR_FOR_strcpy), NULL);
njnf76d27a2009-05-28 01:53:07 +00001045 add_hardwired_spec("dyld", "strlcat",
njnea2d6fd2010-07-01 00:20:20 +00001046 (Addr)&VG_(x86_darwin_REDIR_FOR_strlcat), NULL);
1047 }
1048
1049# elif defined(VGP_amd64_darwin)
1050 /* If we're using memcheck, use these intercepts right from
1051 the start, otherwise dyld makes a lot of noise. */
1052 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1053 add_hardwired_spec("dyld", "strcmp",
1054 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcmp), NULL);
1055 add_hardwired_spec("dyld", "strlen",
1056 (Addr)&VG_(amd64_darwin_REDIR_FOR_strlen), NULL);
1057 add_hardwired_spec("dyld", "strcat",
1058 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcat), NULL);
1059 add_hardwired_spec("dyld", "strcpy",
1060 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcpy), NULL);
1061 add_hardwired_spec("dyld", "strlcat",
1062 (Addr)&VG_(amd64_darwin_REDIR_FOR_strlcat), NULL);
njnf76d27a2009-05-28 01:53:07 +00001063 // DDD: #warning fixme rdar://6166275
1064 add_hardwired_spec("dyld", "arc4random",
njnea2d6fd2010-07-01 00:20:20 +00001065 (Addr)&VG_(amd64_darwin_REDIR_FOR_arc4random), NULL);
njnf76d27a2009-05-28 01:53:07 +00001066 }
1067
sewardj0ec07f32006-01-12 12:32:32 +00001068# else
1069# error Unknown platform
1070# endif
1071
1072 if (VG_(clo_trace_redir))
1073 show_redir_state("after VG_(redir_initialise)");
njnd1af0032005-05-29 17:01:48 +00001074}
sewardjcbdddcf2005-03-10 23:23:45 +00001075
njn16eeb4e2005-06-16 03:56:58 +00001076
sewardj0ec07f32006-01-12 12:32:32 +00001077/*------------------------------------------------------------*/
1078/*--- MISC HELPERS ---*/
1079/*------------------------------------------------------------*/
1080
sewardj9c606bd2008-09-18 18:12:50 +00001081static void* dinfo_zalloc(HChar* ec, SizeT n) {
sewardjdf24cf12007-11-16 18:32:40 +00001082 void* p;
1083 vg_assert(n > 0);
sewardj9c606bd2008-09-18 18:12:50 +00001084 p = VG_(arena_malloc)(VG_AR_DINFO, ec, n);
sewardjdf24cf12007-11-16 18:32:40 +00001085 tl_assert(p);
1086 VG_(memset)(p, 0, n);
1087 return p;
njn16eeb4e2005-06-16 03:56:58 +00001088}
1089
sewardjb8b79ad2008-03-03 01:35:41 +00001090static void dinfo_free(void* p) {
sewardjdf24cf12007-11-16 18:32:40 +00001091 tl_assert(p);
sewardjb8b79ad2008-03-03 01:35:41 +00001092 return VG_(arena_free)(VG_AR_DINFO, p);
njn16eeb4e2005-06-16 03:56:58 +00001093}
1094
sewardj9c606bd2008-09-18 18:12:50 +00001095static HChar* dinfo_strdup(HChar* ec, HChar* str)
njnbc6d84d2005-06-19 18:58:03 +00001096{
sewardj9c606bd2008-09-18 18:12:50 +00001097 return VG_(arena_strdup)(VG_AR_DINFO, ec, str);
njnbc6d84d2005-06-19 18:58:03 +00001098}
1099
sewardj0ec07f32006-01-12 12:32:32 +00001100/* Really this should be merged with translations_allowable_from_seg
1101 in m_translate. */
1102static Bool is_plausible_guest_addr(Addr a)
njn16eeb4e2005-06-16 03:56:58 +00001103{
sewardj6b9cc872006-10-17 01:39:30 +00001104 NSegment const* seg = VG_(am_find_nsegment)(a);
sewardj0ec07f32006-01-12 12:32:32 +00001105 return seg != NULL
1106 && (seg->kind == SkAnonC || seg->kind == SkFileC)
1107 && (seg->hasX || seg->hasR); /* crude x86-specific hack */
1108}
1109
sewardj6b9cc872006-10-17 01:39:30 +00001110/* A function which spots AIX 'glink' functions. A 'glink' function
1111 is a stub function which has something to do with AIX-style dynamic
1112 linking, and jumps to the real target (with which it typically
1113 shares the same name). See also comment where this function is
1114 used (above). */
1115static Bool is_aix5_glink_idiom ( Addr sym_addr )
1116{
1117# if defined(VGP_ppc32_aix5)
1118 UInt* w = (UInt*)sym_addr;
1119 if (VG_IS_4_ALIGNED(w)
1120 && is_plausible_guest_addr((Addr)(w+0))
1121 && is_plausible_guest_addr((Addr)(w+6))
1122 && (w[0] & 0xFFFF0000) == 0x81820000 /* lwz r12,func@toc(r2) */
1123 && w[1] == 0x90410014 /* stw r2,20(r1) */
1124 && w[2] == 0x800c0000 /* lwz r0,0(r12) */
1125 && w[3] == 0x804c0004 /* lwz r2,4(r12) */
1126 && w[4] == 0x7c0903a6 /* mtctr r0 */
1127 && w[5] == 0x4e800420 /* bctr */
1128 && w[6] == 0x00000000 /* illegal */)
1129 return True;
1130# elif defined(VGP_ppc64_aix5)
1131 UInt* w = (UInt*)sym_addr;
1132 if (VG_IS_4_ALIGNED(w)
1133 && is_plausible_guest_addr((Addr)(w+0))
1134 && is_plausible_guest_addr((Addr)(w+6))
1135 && (w[0] & 0xFFFF0000) == 0xE9820000 /* ld r12,func@toc(r2) */
1136 && w[1] == 0xF8410028 /* std r2,40(r1) */
1137 && w[2] == 0xE80C0000 /* ld r0,0(r12) */
1138 && w[3] == 0xE84C0008 /* ld r2,8(r12) */
1139 && w[4] == 0x7c0903a6 /* mtctr r0 */
1140 && w[5] == 0x4e800420 /* bctr */
1141 && w[6] == 0x00000000 /* illegal */)
1142 return True;
1143# endif
1144 return False;
1145}
sewardj0ec07f32006-01-12 12:32:32 +00001146
sewardjf9ebc392010-05-09 22:30:43 +00001147
sewardj0ec07f32006-01-12 12:32:32 +00001148/*------------------------------------------------------------*/
1149/*--- NOTIFY-ON-LOAD FUNCTIONS ---*/
1150/*------------------------------------------------------------*/
1151
sewardja672ea32006-04-29 18:03:14 +00001152static
1153void handle_maybe_load_notifier( const UChar* soname,
1154 HChar* symbol, Addr addr )
sewardj0ec07f32006-01-12 12:32:32 +00001155{
sewardja672ea32006-04-29 18:03:14 +00001156# if defined(VGP_x86_linux)
1157 /* x86-linux only: if we see _dl_sysinfo_int80, note its address.
1158 See comment on declaration of VG_(client__dl_sysinfo_int80) for
1159 the reason. As far as I can tell, the relevant symbol is always
1160 in object with soname "ld-linux.so.2". */
1161 if (symbol && symbol[0] == '_'
1162 && 0 == VG_(strcmp)(symbol, "_dl_sysinfo_int80")
1163 && 0 == VG_(strcmp)(soname, "ld-linux.so.2")) {
1164 if (VG_(client__dl_sysinfo_int80) == 0)
1165 VG_(client__dl_sysinfo_int80) = addr;
1166 }
1167# endif
1168
1169 /* Normal load-notifier handling after here. First, ignore all
1170 symbols lacking the right prefix. */
sewardj0ec07f32006-01-12 12:32:32 +00001171 if (0 != VG_(strncmp)(symbol, VG_NOTIFY_ON_LOAD_PREFIX,
1172 VG_NOTIFY_ON_LOAD_PREFIX_LEN))
1173 /* Doesn't have the right prefix */
1174 return;
1175
1176 if (VG_(strcmp)(symbol, VG_STRINGIFY(VG_NOTIFY_ON_LOAD(freeres))) == 0)
1177 VG_(client___libc_freeres_wrapper) = addr;
tomd2645142009-10-29 09:27:11 +00001178 else if (VG_(strcmp)(symbol, VG_STRINGIFY(VG_NOTIFY_ON_LOAD(ifunc_wrapper))) == 0)
1179 iFuncWrapper = addr;
njn16eeb4e2005-06-16 03:56:58 +00001180 else
1181 vg_assert2(0, "unrecognised load notification function: %s", symbol);
1182}
1183
sewardj0ec07f32006-01-12 12:32:32 +00001184
1185/*------------------------------------------------------------*/
sewardjf9ebc392010-05-09 22:30:43 +00001186/*--- REQUIRE-TEXT-SYMBOL HANDLING ---*/
1187/*------------------------------------------------------------*/
1188
1189/* In short: check that the currently-being-loaded object has text
1190 symbols that satisfy any --require-text-symbol= specifications that
1191 apply to it, and abort the run with an error message if not.
1192*/
1193static void handle_require_text_symbols ( DebugInfo* di )
1194{
1195 /* First thing to do is figure out which, if any,
1196 --require-text-symbol specification strings apply to this
1197 object. Most likely none do, since it is not expected to
1198 frequently be used. Work through the list of specs and
1199 accumulate in fnpatts[] the fn patterns that pertain to this
1200 object. */
1201 HChar* fnpatts[VG_CLO_MAX_REQ_TSYMS];
1202 Int fnpatts_used = 0;
1203 Int i, j;
1204 const HChar* di_soname = VG_(DebugInfo_get_soname)(di);
1205 vg_assert(di_soname); // must be present
1206
1207 VG_(memset)(&fnpatts, 0, sizeof(fnpatts));
1208
1209 vg_assert(VG_(clo_n_req_tsyms) >= 0);
1210 vg_assert(VG_(clo_n_req_tsyms) <= VG_CLO_MAX_REQ_TSYMS);
1211 for (i = 0; i < VG_(clo_n_req_tsyms); i++) {
1212 HChar* spec = VG_(clo_req_tsyms)[i];
1213 vg_assert(spec && VG_(strlen)(spec) >= 4);
1214 // clone the spec, so we can stick a zero at the end of the sopatt
1215 spec = VG_(strdup)("m_redir.hrts.1", spec);
1216 HChar sep = spec[0];
1217 HChar* sopatt = &spec[1];
1218 HChar* fnpatt = VG_(strchr)(sopatt, sep);
1219 // the initial check at clo processing in time in m_main
1220 // should ensure this.
1221 vg_assert(fnpatt && *fnpatt == sep);
1222 *fnpatt = 0;
1223 fnpatt++;
1224 if (VG_(string_match)(sopatt, di_soname))
1225 fnpatts[fnpatts_used++]
1226 = VG_(strdup)("m_redir.hrts.2", fnpatt);
1227 VG_(free)(spec);
1228 }
1229
1230 if (fnpatts_used == 0)
1231 return; /* no applicable spec strings */
1232
1233 /* So finally, fnpatts[0 .. fnpatts_used - 1] contains the set of
1234 (patterns for) text symbol names that must be found in this
1235 object, in order to continue. That is, we must find at least
1236 one text symbol name that matches each pattern, else we must
1237 abort the run. */
1238
1239 if (0) VG_(printf)("for %s\n", di_soname);
1240 for (i = 0; i < fnpatts_used; i++)
1241 if (0) VG_(printf)(" fnpatt: %s\n", fnpatts[i]);
1242
1243 /* For each spec, look through the syms to find one that matches.
1244 This isn't terribly efficient but it happens rarely, so no big
1245 deal. */
1246 for (i = 0; i < fnpatts_used; i++) {
1247 Bool found = False;
1248 HChar* fnpatt = fnpatts[i];
1249 Int nsyms = VG_(DebugInfo_syms_howmany)(di);
1250 for (j = 0; j < nsyms; j++) {
1251 Bool isText = False;
1252 HChar* sym_name = NULL;
1253 VG_(DebugInfo_syms_getidx)( di, j, NULL, NULL,
1254 NULL, &sym_name, &isText, NULL );
1255 /* ignore data symbols */
1256 if (0) VG_(printf)("QQQ %s\n", sym_name);
1257 vg_assert(sym_name);
1258 if (!isText)
1259 continue;
1260 if (VG_(string_match)(fnpatt, sym_name)) {
1261 found = True;
1262 break;
1263 }
1264 }
1265
1266 if (!found) {
1267 HChar* v = "valgrind: ";
1268 VG_(printf)("\n");
1269 VG_(printf)(
1270 "%sFatal error at when loading library with soname\n", v);
1271 VG_(printf)(
1272 "%s %s\n", v, di_soname);
1273 VG_(printf)(
1274 "%sCannot find any text symbol with a name "
1275 "that matches the pattern\n", v);
1276 VG_(printf)("%s %s\n", v, fnpatt);
1277 VG_(printf)("%sas required by a --require-text-symbol= "
1278 "specification.\n", v);
1279 VG_(printf)("\n");
1280 VG_(printf)(
1281 "%sCannot continue -- exiting now.\n", v);
1282 VG_(printf)("\n");
1283 VG_(exit)(1);
1284 }
1285 }
1286
1287 /* All required specs were found. Just free memory and return. */
1288 for (i = 0; i < fnpatts_used; i++)
1289 VG_(free)(fnpatts[i]);
1290}
1291
1292
1293/*------------------------------------------------------------*/
sewardj0ec07f32006-01-12 12:32:32 +00001294/*--- SANITY/DEBUG ---*/
1295/*------------------------------------------------------------*/
1296
1297static void show_spec ( HChar* left, Spec* spec )
njn16eeb4e2005-06-16 03:56:58 +00001298{
sewardj738856f2009-07-15 14:48:32 +00001299 VG_(message)( Vg_DebugMsg,
1300 "%s%25s %30s %s-> 0x%08llx\n",
1301 left,
1302 spec->from_sopatt, spec->from_fnpatt,
1303 spec->isWrap ? "W" : "R",
1304 (ULong)spec->to_addr );
njn16eeb4e2005-06-16 03:56:58 +00001305}
1306
sewardj0ec07f32006-01-12 12:32:32 +00001307static void show_active ( HChar* left, Active* act )
njn16eeb4e2005-06-16 03:56:58 +00001308{
sewardj0ec07f32006-01-12 12:32:32 +00001309 Bool ok;
1310 HChar name1[64] = "";
1311 HChar name2[64] = "";
1312 name1[0] = name2[0] = 0;
1313 ok = VG_(get_fnname_w_offset)(act->from_addr, name1, 64);
1314 if (!ok) VG_(strcpy)(name1, "???");
1315 ok = VG_(get_fnname_w_offset)(act->to_addr, name2, 64);
1316 if (!ok) VG_(strcpy)(name2, "???");
1317
sewardj738856f2009-07-15 14:48:32 +00001318 VG_(message)(Vg_DebugMsg, "%s0x%08llx (%20s) %s-> 0x%08llx %s\n",
sewardj0ec07f32006-01-12 12:32:32 +00001319 left,
1320 (ULong)act->from_addr, name1,
1321 act->isWrap ? "W" : "R",
1322 (ULong)act->to_addr, name2 );
njn16eeb4e2005-06-16 03:56:58 +00001323}
1324
sewardj0ec07f32006-01-12 12:32:32 +00001325static void show_redir_state ( HChar* who )
njn16eeb4e2005-06-16 03:56:58 +00001326{
sewardj0ec07f32006-01-12 12:32:32 +00001327 TopSpec* ts;
1328 Spec* sp;
1329 Active* act;
sewardj738856f2009-07-15 14:48:32 +00001330 VG_(message)(Vg_DebugMsg, "<<\n");
1331 VG_(message)(Vg_DebugMsg, " ------ REDIR STATE %s ------\n", who);
sewardj0ec07f32006-01-12 12:32:32 +00001332 for (ts = topSpecs; ts; ts = ts->next) {
1333 VG_(message)(Vg_DebugMsg,
sewardj738856f2009-07-15 14:48:32 +00001334 " TOPSPECS of soname %s\n",
sewardje3f1e592009-07-31 09:41:29 +00001335 ts->seginfo
1336 ? (HChar*)VG_(DebugInfo_get_soname)(ts->seginfo)
1337 : "(hardwired)" );
sewardj0ec07f32006-01-12 12:32:32 +00001338 for (sp = ts->specs; sp; sp = sp->next)
1339 show_spec(" ", sp);
1340 }
sewardj738856f2009-07-15 14:48:32 +00001341 VG_(message)(Vg_DebugMsg, " ------ ACTIVE ------\n");
njne2a9ad32007-09-17 05:30:48 +00001342 VG_(OSetGen_ResetIter)( activeSet );
1343 while ( (act = VG_(OSetGen_Next)(activeSet)) ) {
sewardj0ec07f32006-01-12 12:32:32 +00001344 show_active(" ", act);
1345 }
1346
sewardj738856f2009-07-15 14:48:32 +00001347 VG_(message)(Vg_DebugMsg, ">>\n");
njn16eeb4e2005-06-16 03:56:58 +00001348}
1349
njnc0ae7052005-08-25 22:55:19 +00001350/*--------------------------------------------------------------------*/
1351/*--- end ---*/
1352/*--------------------------------------------------------------------*/