blob: 76186908c91f52200c680960fc44af1a2ec5e11d [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",
sewardjd85ee0c2011-02-11 16:47:03 +0000894 "calling conventions for this platform. The package you need",
895 "to install for fix (1) is called",
896 "",
897 " On Debian, Ubuntu: libc6-dbg",
898 " On SuSE, openSuSE, Fedora, RHEL: glibc-debuginfo",
sewardjb57e6932009-08-02 12:21:31 +0000899 NULL
900 };
901
902
sewardj0ec07f32006-01-12 12:32:32 +0000903/* Initialise the redir system, and create the initial Spec list and
904 for amd64-linux a couple of permanent active mappings. The initial
905 Specs are not converted into Actives yet, on the (checked)
sewardjb8b79ad2008-03-03 01:35:41 +0000906 assumption that no DebugInfos have so far been created, and so when
sewardj0ec07f32006-01-12 12:32:32 +0000907 they are created, that will happen. */
908
909void VG_(redir_initialise) ( void )
910{
sewardjb8b79ad2008-03-03 01:35:41 +0000911 // Assert that there are no DebugInfos so far
sewardje3f1e592009-07-31 09:41:29 +0000912 vg_assert( VG_(next_DebugInfo)(NULL) == NULL );
sewardj0ec07f32006-01-12 12:32:32 +0000913
914 // Initialise active mapping.
njne2a9ad32007-09-17 05:30:48 +0000915 activeSet = VG_(OSetGen_Create)(offsetof(Active, from_addr),
916 NULL, // Use fast comparison
sewardjb8b79ad2008-03-03 01:35:41 +0000917 dinfo_zalloc,
sewardj9c606bd2008-09-18 18:12:50 +0000918 "redir.ri.1",
sewardjb8b79ad2008-03-03 01:35:41 +0000919 dinfo_free);
sewardj0ec07f32006-01-12 12:32:32 +0000920
921 // The rest of this function just adds initial Specs.
922
923# if defined(VGP_x86_linux)
sewardj6a443b22005-11-20 19:37:54 +0000924 /* If we're using memcheck, use this intercept right from the
925 start, otherwise ld.so (glibc-2.3.5) makes a lot of noise. */
926 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
sewardjd28cc962011-03-28 08:22:55 +0000927 const HChar** mandatory;
928# if defined(GLIBC_2_2) || defined(GLIBC_2_3) || defined(GLIBC_2_4) \
929 || defined(GLIBC_2_5) || defined(GLIBC_2_6) || defined(GLIBC_2_7) \
930 || defined(GLIBC_2_8) || defined(GLIBC_2_9) \
931 || defined(GLIBC_2_10) || defined(GLIBC_2_11)
932 mandatory = NULL;
933# else
934 /* for glibc-2.12 and later, this is mandatory - can't sanely
935 continue without it */
936 mandatory = complain_about_stripped_glibc_ldso;
937# endif
sewardj0ec07f32006-01-12 12:32:32 +0000938 add_hardwired_spec(
939 "ld-linux.so.2", "index",
sewardjd28cc962011-03-28 08:22:55 +0000940 (Addr)&VG_(x86_linux_REDIR_FOR_index), mandatory);
941 add_hardwired_spec(
942 "ld-linux.so.2", "strlen",
943 (Addr)&VG_(x86_linux_REDIR_FOR_strlen), mandatory);
sewardj6a443b22005-11-20 19:37:54 +0000944 }
sewardjb9bce632005-06-21 01:41:34 +0000945
sewardj0ec07f32006-01-12 12:32:32 +0000946# elif defined(VGP_amd64_linux)
njnd1af0032005-05-29 17:01:48 +0000947 /* Redirect vsyscalls to local versions */
sewardj0ec07f32006-01-12 12:32:32 +0000948 add_hardwired_active(
sewardjb9bce632005-06-21 01:41:34 +0000949 0xFFFFFFFFFF600000ULL,
950 (Addr)&VG_(amd64_linux_REDIR_FOR_vgettimeofday)
951 );
sewardj0ec07f32006-01-12 12:32:32 +0000952 add_hardwired_active(
tomf5db3b62005-06-21 13:26:17 +0000953 0xFFFFFFFFFF600400ULL,
sewardjb9bce632005-06-21 01:41:34 +0000954 (Addr)&VG_(amd64_linux_REDIR_FOR_vtime)
955 );
956
sewardjb57e6932009-08-02 12:21:31 +0000957 /* If we're using memcheck, use these intercepts right from
958 the start, otherwise ld.so makes a lot of noise. */
959 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
bart2ebe4112008-06-01 16:33:37 +0000960
sewardjb57e6932009-08-02 12:21:31 +0000961 add_hardwired_spec(
962 "ld-linux-x86-64.so.2", "strlen",
963 (Addr)&VG_(amd64_linux_REDIR_FOR_strlen),
964# if defined(GLIBC_2_2) || defined(GLIBC_2_3) || defined(GLIBC_2_4) \
965 || defined(GLIBC_2_5) || defined(GLIBC_2_6) || defined(GLIBC_2_7) \
966 || defined(GLIBC_2_8) || defined(GLIBC_2_9)
967 NULL
968# else
969 /* for glibc-2.10 and later, this is mandatory - can't sanely
970 continue without it */
971 complain_about_stripped_glibc_ldso
972# endif
973 );
974 }
975
976# elif defined(VGP_ppc32_linux)
sewardj96fcfc82005-11-18 21:12:52 +0000977 /* If we're using memcheck, use these intercepts right from
978 the start, otherwise ld.so makes a lot of noise. */
979 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
sewardj0fd30492007-11-16 19:39:49 +0000980
sewardj5092a972007-11-16 18:45:40 +0000981 /* this is mandatory - can't sanely continue without it */
sewardj0ec07f32006-01-12 12:32:32 +0000982 add_hardwired_spec(
983 "ld.so.1", "strlen",
sewardjdf24cf12007-11-16 18:32:40 +0000984 (Addr)&VG_(ppc32_linux_REDIR_FOR_strlen),
sewardjb57e6932009-08-02 12:21:31 +0000985 complain_about_stripped_glibc_ldso
sewardj96fcfc82005-11-18 21:12:52 +0000986 );
sewardj0ec07f32006-01-12 12:32:32 +0000987 add_hardwired_spec(
sewardjd5a30a42006-02-09 10:29:43 +0000988 "ld.so.1", "strcmp",
sewardjdf24cf12007-11-16 18:32:40 +0000989 (Addr)&VG_(ppc32_linux_REDIR_FOR_strcmp),
sewardj5092a972007-11-16 18:45:40 +0000990 NULL /* not mandatory - so why bother at all? */
991 /* glibc-2.6.1 (openSUSE 10.3, ppc32) seems fine without it */
sewardj96fcfc82005-11-18 21:12:52 +0000992 );
sewardjcd8c95a2006-08-25 11:48:38 +0000993 add_hardwired_spec(
994 "ld.so.1", "index",
sewardjdf24cf12007-11-16 18:32:40 +0000995 (Addr)&VG_(ppc32_linux_REDIR_FOR_strchr),
sewardj5092a972007-11-16 18:45:40 +0000996 NULL /* not mandatory - so why bother at all? */
997 /* glibc-2.6.1 (openSUSE 10.3, ppc32) seems fine without it */
sewardjcd8c95a2006-08-25 11:48:38 +0000998 );
bart2ebe4112008-06-01 16:33:37 +0000999 }
sewardjb9bce632005-06-21 01:41:34 +00001000
sewardj0ec07f32006-01-12 12:32:32 +00001001# elif defined(VGP_ppc64_linux)
sewardjf0915fc2006-01-05 14:07:04 +00001002 /* If we're using memcheck, use these intercepts right from
1003 the start, otherwise ld.so makes a lot of noise. */
1004 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1005
sewardj0fd30492007-11-16 19:39:49 +00001006 /* this is mandatory - can't sanely continue without it */
sewardj1a85f4f2006-01-12 21:15:35 +00001007 add_hardwired_spec(
1008 "ld64.so.1", "strlen",
sewardjdf24cf12007-11-16 18:32:40 +00001009 (Addr)VG_(fnptr_to_fnentry)( &VG_(ppc64_linux_REDIR_FOR_strlen) ),
sewardjb57e6932009-08-02 12:21:31 +00001010 complain_about_stripped_glibc_ldso
sewardjdf24cf12007-11-16 18:32:40 +00001011 );
sewardjf0915fc2006-01-05 14:07:04 +00001012
sewardj1a85f4f2006-01-12 21:15:35 +00001013 add_hardwired_spec(
1014 "ld64.so.1", "index",
sewardjdf24cf12007-11-16 18:32:40 +00001015 (Addr)VG_(fnptr_to_fnentry)( &VG_(ppc64_linux_REDIR_FOR_strchr) ),
sewardj0fd30492007-11-16 19:39:49 +00001016 NULL /* not mandatory - so why bother at all? */
1017 /* glibc-2.5 (FC6, ppc64) seems fine without it */
sewardjdf24cf12007-11-16 18:32:40 +00001018 );
bart2ebe4112008-06-01 16:33:37 +00001019 }
sewardj2c48c7b2005-11-29 13:05:56 +00001020
sewardj59570ff2010-01-01 11:59:33 +00001021# elif defined(VGP_arm_linux)
1022 /* If we're using memcheck, use these intercepts right from
1023 the start, otherwise ld.so makes a lot of noise. */
1024 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1025 add_hardwired_spec(
1026 "ld-linux.so.3", "strlen",
1027 (Addr)&VG_(arm_linux_REDIR_FOR_strlen),
sewardjd69cee82010-08-22 12:13:35 +00001028 complain_about_stripped_glibc_ldso
sewardj59570ff2010-01-01 11:59:33 +00001029 );
1030 //add_hardwired_spec(
1031 // "ld-linux.so.3", "index",
1032 // (Addr)&VG_(arm_linux_REDIR_FOR_index),
1033 // NULL
1034 //);
1035 add_hardwired_spec(
1036 "ld-linux.so.3", "memcpy",
1037 (Addr)&VG_(arm_linux_REDIR_FOR_memcpy),
sewardjd69cee82010-08-22 12:13:35 +00001038 complain_about_stripped_glibc_ldso
sewardj59570ff2010-01-01 11:59:33 +00001039 );
1040 }
1041 /* nothing so far */
1042
sewardj6b9cc872006-10-17 01:39:30 +00001043# elif defined(VGP_ppc32_aix5)
1044 /* nothing so far */
1045
1046# elif defined(VGP_ppc64_aix5)
1047 /* nothing so far */
1048
njnea2d6fd2010-07-01 00:20:20 +00001049# elif defined(VGP_x86_darwin)
njnf76d27a2009-05-28 01:53:07 +00001050 /* 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",
njnea2d6fd2010-07-01 00:20:20 +00001054 (Addr)&VG_(x86_darwin_REDIR_FOR_strcmp), NULL);
njnf76d27a2009-05-28 01:53:07 +00001055 add_hardwired_spec("dyld", "strlen",
njnea2d6fd2010-07-01 00:20:20 +00001056 (Addr)&VG_(x86_darwin_REDIR_FOR_strlen), NULL);
njnf76d27a2009-05-28 01:53:07 +00001057 add_hardwired_spec("dyld", "strcat",
njnea2d6fd2010-07-01 00:20:20 +00001058 (Addr)&VG_(x86_darwin_REDIR_FOR_strcat), NULL);
njnf76d27a2009-05-28 01:53:07 +00001059 add_hardwired_spec("dyld", "strcpy",
njnea2d6fd2010-07-01 00:20:20 +00001060 (Addr)&VG_(x86_darwin_REDIR_FOR_strcpy), NULL);
njnf76d27a2009-05-28 01:53:07 +00001061 add_hardwired_spec("dyld", "strlcat",
njnea2d6fd2010-07-01 00:20:20 +00001062 (Addr)&VG_(x86_darwin_REDIR_FOR_strlcat), NULL);
1063 }
1064
1065# elif defined(VGP_amd64_darwin)
1066 /* If we're using memcheck, use these intercepts right from
1067 the start, otherwise dyld makes a lot of noise. */
1068 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
1069 add_hardwired_spec("dyld", "strcmp",
1070 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcmp), NULL);
1071 add_hardwired_spec("dyld", "strlen",
1072 (Addr)&VG_(amd64_darwin_REDIR_FOR_strlen), NULL);
1073 add_hardwired_spec("dyld", "strcat",
1074 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcat), NULL);
1075 add_hardwired_spec("dyld", "strcpy",
1076 (Addr)&VG_(amd64_darwin_REDIR_FOR_strcpy), NULL);
1077 add_hardwired_spec("dyld", "strlcat",
1078 (Addr)&VG_(amd64_darwin_REDIR_FOR_strlcat), NULL);
njnf76d27a2009-05-28 01:53:07 +00001079 // DDD: #warning fixme rdar://6166275
1080 add_hardwired_spec("dyld", "arc4random",
njnea2d6fd2010-07-01 00:20:20 +00001081 (Addr)&VG_(amd64_darwin_REDIR_FOR_arc4random), NULL);
njnf76d27a2009-05-28 01:53:07 +00001082 }
1083
sewardjb5b87402011-03-07 16:05:35 +00001084# elif defined(VGP_s390x_linux)
1085 /* nothing so far */
1086
sewardj0ec07f32006-01-12 12:32:32 +00001087# else
1088# error Unknown platform
1089# endif
1090
1091 if (VG_(clo_trace_redir))
1092 show_redir_state("after VG_(redir_initialise)");
njnd1af0032005-05-29 17:01:48 +00001093}
sewardjcbdddcf2005-03-10 23:23:45 +00001094
njn16eeb4e2005-06-16 03:56:58 +00001095
sewardj0ec07f32006-01-12 12:32:32 +00001096/*------------------------------------------------------------*/
1097/*--- MISC HELPERS ---*/
1098/*------------------------------------------------------------*/
1099
sewardj9c606bd2008-09-18 18:12:50 +00001100static void* dinfo_zalloc(HChar* ec, SizeT n) {
sewardjdf24cf12007-11-16 18:32:40 +00001101 void* p;
1102 vg_assert(n > 0);
sewardj9c606bd2008-09-18 18:12:50 +00001103 p = VG_(arena_malloc)(VG_AR_DINFO, ec, n);
sewardjdf24cf12007-11-16 18:32:40 +00001104 tl_assert(p);
1105 VG_(memset)(p, 0, n);
1106 return p;
njn16eeb4e2005-06-16 03:56:58 +00001107}
1108
sewardjb8b79ad2008-03-03 01:35:41 +00001109static void dinfo_free(void* p) {
sewardjdf24cf12007-11-16 18:32:40 +00001110 tl_assert(p);
sewardjb8b79ad2008-03-03 01:35:41 +00001111 return VG_(arena_free)(VG_AR_DINFO, p);
njn16eeb4e2005-06-16 03:56:58 +00001112}
1113
sewardj9c606bd2008-09-18 18:12:50 +00001114static HChar* dinfo_strdup(HChar* ec, HChar* str)
njnbc6d84d2005-06-19 18:58:03 +00001115{
sewardj9c606bd2008-09-18 18:12:50 +00001116 return VG_(arena_strdup)(VG_AR_DINFO, ec, str);
njnbc6d84d2005-06-19 18:58:03 +00001117}
1118
sewardj0ec07f32006-01-12 12:32:32 +00001119/* Really this should be merged with translations_allowable_from_seg
1120 in m_translate. */
1121static Bool is_plausible_guest_addr(Addr a)
njn16eeb4e2005-06-16 03:56:58 +00001122{
sewardj6b9cc872006-10-17 01:39:30 +00001123 NSegment const* seg = VG_(am_find_nsegment)(a);
sewardj0ec07f32006-01-12 12:32:32 +00001124 return seg != NULL
1125 && (seg->kind == SkAnonC || seg->kind == SkFileC)
1126 && (seg->hasX || seg->hasR); /* crude x86-specific hack */
1127}
1128
sewardj6b9cc872006-10-17 01:39:30 +00001129/* A function which spots AIX 'glink' functions. A 'glink' function
1130 is a stub function which has something to do with AIX-style dynamic
1131 linking, and jumps to the real target (with which it typically
1132 shares the same name). See also comment where this function is
1133 used (above). */
1134static Bool is_aix5_glink_idiom ( Addr sym_addr )
1135{
1136# if defined(VGP_ppc32_aix5)
1137 UInt* w = (UInt*)sym_addr;
1138 if (VG_IS_4_ALIGNED(w)
1139 && is_plausible_guest_addr((Addr)(w+0))
1140 && is_plausible_guest_addr((Addr)(w+6))
1141 && (w[0] & 0xFFFF0000) == 0x81820000 /* lwz r12,func@toc(r2) */
1142 && w[1] == 0x90410014 /* stw r2,20(r1) */
1143 && w[2] == 0x800c0000 /* lwz r0,0(r12) */
1144 && w[3] == 0x804c0004 /* lwz r2,4(r12) */
1145 && w[4] == 0x7c0903a6 /* mtctr r0 */
1146 && w[5] == 0x4e800420 /* bctr */
1147 && w[6] == 0x00000000 /* illegal */)
1148 return True;
1149# elif defined(VGP_ppc64_aix5)
1150 UInt* w = (UInt*)sym_addr;
1151 if (VG_IS_4_ALIGNED(w)
1152 && is_plausible_guest_addr((Addr)(w+0))
1153 && is_plausible_guest_addr((Addr)(w+6))
1154 && (w[0] & 0xFFFF0000) == 0xE9820000 /* ld r12,func@toc(r2) */
1155 && w[1] == 0xF8410028 /* std r2,40(r1) */
1156 && w[2] == 0xE80C0000 /* ld r0,0(r12) */
1157 && w[3] == 0xE84C0008 /* ld r2,8(r12) */
1158 && w[4] == 0x7c0903a6 /* mtctr r0 */
1159 && w[5] == 0x4e800420 /* bctr */
1160 && w[6] == 0x00000000 /* illegal */)
1161 return True;
1162# endif
1163 return False;
1164}
sewardj0ec07f32006-01-12 12:32:32 +00001165
sewardjf9ebc392010-05-09 22:30:43 +00001166
sewardj0ec07f32006-01-12 12:32:32 +00001167/*------------------------------------------------------------*/
1168/*--- NOTIFY-ON-LOAD FUNCTIONS ---*/
1169/*------------------------------------------------------------*/
1170
sewardja672ea32006-04-29 18:03:14 +00001171static
1172void handle_maybe_load_notifier( const UChar* soname,
1173 HChar* symbol, Addr addr )
sewardj0ec07f32006-01-12 12:32:32 +00001174{
sewardja672ea32006-04-29 18:03:14 +00001175# if defined(VGP_x86_linux)
1176 /* x86-linux only: if we see _dl_sysinfo_int80, note its address.
1177 See comment on declaration of VG_(client__dl_sysinfo_int80) for
1178 the reason. As far as I can tell, the relevant symbol is always
1179 in object with soname "ld-linux.so.2". */
1180 if (symbol && symbol[0] == '_'
1181 && 0 == VG_(strcmp)(symbol, "_dl_sysinfo_int80")
1182 && 0 == VG_(strcmp)(soname, "ld-linux.so.2")) {
1183 if (VG_(client__dl_sysinfo_int80) == 0)
1184 VG_(client__dl_sysinfo_int80) = addr;
1185 }
1186# endif
1187
1188 /* Normal load-notifier handling after here. First, ignore all
1189 symbols lacking the right prefix. */
sewardjdd9508c2011-05-04 09:03:41 +00001190 vg_assert(symbol); // assert rather than segfault if it is NULL
sewardj0ec07f32006-01-12 12:32:32 +00001191 if (0 != VG_(strncmp)(symbol, VG_NOTIFY_ON_LOAD_PREFIX,
1192 VG_NOTIFY_ON_LOAD_PREFIX_LEN))
1193 /* Doesn't have the right prefix */
1194 return;
1195
1196 if (VG_(strcmp)(symbol, VG_STRINGIFY(VG_NOTIFY_ON_LOAD(freeres))) == 0)
1197 VG_(client___libc_freeres_wrapper) = addr;
tomd2645142009-10-29 09:27:11 +00001198 else if (VG_(strcmp)(symbol, VG_STRINGIFY(VG_NOTIFY_ON_LOAD(ifunc_wrapper))) == 0)
1199 iFuncWrapper = addr;
njn16eeb4e2005-06-16 03:56:58 +00001200 else
1201 vg_assert2(0, "unrecognised load notification function: %s", symbol);
1202}
1203
sewardj0ec07f32006-01-12 12:32:32 +00001204
1205/*------------------------------------------------------------*/
sewardjf9ebc392010-05-09 22:30:43 +00001206/*--- REQUIRE-TEXT-SYMBOL HANDLING ---*/
1207/*------------------------------------------------------------*/
1208
1209/* In short: check that the currently-being-loaded object has text
1210 symbols that satisfy any --require-text-symbol= specifications that
1211 apply to it, and abort the run with an error message if not.
1212*/
1213static void handle_require_text_symbols ( DebugInfo* di )
1214{
1215 /* First thing to do is figure out which, if any,
1216 --require-text-symbol specification strings apply to this
1217 object. Most likely none do, since it is not expected to
1218 frequently be used. Work through the list of specs and
1219 accumulate in fnpatts[] the fn patterns that pertain to this
1220 object. */
1221 HChar* fnpatts[VG_CLO_MAX_REQ_TSYMS];
1222 Int fnpatts_used = 0;
1223 Int i, j;
1224 const HChar* di_soname = VG_(DebugInfo_get_soname)(di);
1225 vg_assert(di_soname); // must be present
1226
1227 VG_(memset)(&fnpatts, 0, sizeof(fnpatts));
1228
1229 vg_assert(VG_(clo_n_req_tsyms) >= 0);
1230 vg_assert(VG_(clo_n_req_tsyms) <= VG_CLO_MAX_REQ_TSYMS);
1231 for (i = 0; i < VG_(clo_n_req_tsyms); i++) {
1232 HChar* spec = VG_(clo_req_tsyms)[i];
1233 vg_assert(spec && VG_(strlen)(spec) >= 4);
1234 // clone the spec, so we can stick a zero at the end of the sopatt
1235 spec = VG_(strdup)("m_redir.hrts.1", spec);
1236 HChar sep = spec[0];
1237 HChar* sopatt = &spec[1];
1238 HChar* fnpatt = VG_(strchr)(sopatt, sep);
1239 // the initial check at clo processing in time in m_main
1240 // should ensure this.
1241 vg_assert(fnpatt && *fnpatt == sep);
1242 *fnpatt = 0;
1243 fnpatt++;
1244 if (VG_(string_match)(sopatt, di_soname))
1245 fnpatts[fnpatts_used++]
1246 = VG_(strdup)("m_redir.hrts.2", fnpatt);
1247 VG_(free)(spec);
1248 }
1249
1250 if (fnpatts_used == 0)
1251 return; /* no applicable spec strings */
1252
1253 /* So finally, fnpatts[0 .. fnpatts_used - 1] contains the set of
1254 (patterns for) text symbol names that must be found in this
1255 object, in order to continue. That is, we must find at least
1256 one text symbol name that matches each pattern, else we must
1257 abort the run. */
1258
1259 if (0) VG_(printf)("for %s\n", di_soname);
1260 for (i = 0; i < fnpatts_used; i++)
1261 if (0) VG_(printf)(" fnpatt: %s\n", fnpatts[i]);
1262
1263 /* For each spec, look through the syms to find one that matches.
1264 This isn't terribly efficient but it happens rarely, so no big
1265 deal. */
1266 for (i = 0; i < fnpatts_used; i++) {
1267 Bool found = False;
1268 HChar* fnpatt = fnpatts[i];
1269 Int nsyms = VG_(DebugInfo_syms_howmany)(di);
1270 for (j = 0; j < nsyms; j++) {
1271 Bool isText = False;
1272 HChar* sym_name = NULL;
1273 VG_(DebugInfo_syms_getidx)( di, j, NULL, NULL,
1274 NULL, &sym_name, &isText, NULL );
1275 /* ignore data symbols */
1276 if (0) VG_(printf)("QQQ %s\n", sym_name);
1277 vg_assert(sym_name);
1278 if (!isText)
1279 continue;
1280 if (VG_(string_match)(fnpatt, sym_name)) {
1281 found = True;
1282 break;
1283 }
1284 }
1285
1286 if (!found) {
1287 HChar* v = "valgrind: ";
1288 VG_(printf)("\n");
1289 VG_(printf)(
1290 "%sFatal error at when loading library with soname\n", v);
1291 VG_(printf)(
1292 "%s %s\n", v, di_soname);
1293 VG_(printf)(
1294 "%sCannot find any text symbol with a name "
1295 "that matches the pattern\n", v);
1296 VG_(printf)("%s %s\n", v, fnpatt);
1297 VG_(printf)("%sas required by a --require-text-symbol= "
1298 "specification.\n", v);
1299 VG_(printf)("\n");
1300 VG_(printf)(
1301 "%sCannot continue -- exiting now.\n", v);
1302 VG_(printf)("\n");
1303 VG_(exit)(1);
1304 }
1305 }
1306
1307 /* All required specs were found. Just free memory and return. */
1308 for (i = 0; i < fnpatts_used; i++)
1309 VG_(free)(fnpatts[i]);
1310}
1311
1312
1313/*------------------------------------------------------------*/
sewardj0ec07f32006-01-12 12:32:32 +00001314/*--- SANITY/DEBUG ---*/
1315/*------------------------------------------------------------*/
1316
1317static void show_spec ( HChar* left, Spec* spec )
njn16eeb4e2005-06-16 03:56:58 +00001318{
sewardj738856f2009-07-15 14:48:32 +00001319 VG_(message)( Vg_DebugMsg,
1320 "%s%25s %30s %s-> 0x%08llx\n",
1321 left,
1322 spec->from_sopatt, spec->from_fnpatt,
1323 spec->isWrap ? "W" : "R",
1324 (ULong)spec->to_addr );
njn16eeb4e2005-06-16 03:56:58 +00001325}
1326
sewardj0ec07f32006-01-12 12:32:32 +00001327static void show_active ( HChar* left, Active* act )
njn16eeb4e2005-06-16 03:56:58 +00001328{
sewardj0ec07f32006-01-12 12:32:32 +00001329 Bool ok;
1330 HChar name1[64] = "";
1331 HChar name2[64] = "";
1332 name1[0] = name2[0] = 0;
1333 ok = VG_(get_fnname_w_offset)(act->from_addr, name1, 64);
1334 if (!ok) VG_(strcpy)(name1, "???");
1335 ok = VG_(get_fnname_w_offset)(act->to_addr, name2, 64);
1336 if (!ok) VG_(strcpy)(name2, "???");
1337
sewardj738856f2009-07-15 14:48:32 +00001338 VG_(message)(Vg_DebugMsg, "%s0x%08llx (%20s) %s-> 0x%08llx %s\n",
sewardj0ec07f32006-01-12 12:32:32 +00001339 left,
1340 (ULong)act->from_addr, name1,
1341 act->isWrap ? "W" : "R",
1342 (ULong)act->to_addr, name2 );
njn16eeb4e2005-06-16 03:56:58 +00001343}
1344
sewardj0ec07f32006-01-12 12:32:32 +00001345static void show_redir_state ( HChar* who )
njn16eeb4e2005-06-16 03:56:58 +00001346{
sewardj0ec07f32006-01-12 12:32:32 +00001347 TopSpec* ts;
1348 Spec* sp;
1349 Active* act;
sewardj738856f2009-07-15 14:48:32 +00001350 VG_(message)(Vg_DebugMsg, "<<\n");
1351 VG_(message)(Vg_DebugMsg, " ------ REDIR STATE %s ------\n", who);
sewardj0ec07f32006-01-12 12:32:32 +00001352 for (ts = topSpecs; ts; ts = ts->next) {
1353 VG_(message)(Vg_DebugMsg,
sewardj738856f2009-07-15 14:48:32 +00001354 " TOPSPECS of soname %s\n",
sewardje3f1e592009-07-31 09:41:29 +00001355 ts->seginfo
1356 ? (HChar*)VG_(DebugInfo_get_soname)(ts->seginfo)
1357 : "(hardwired)" );
sewardj0ec07f32006-01-12 12:32:32 +00001358 for (sp = ts->specs; sp; sp = sp->next)
1359 show_spec(" ", sp);
1360 }
sewardj738856f2009-07-15 14:48:32 +00001361 VG_(message)(Vg_DebugMsg, " ------ ACTIVE ------\n");
njne2a9ad32007-09-17 05:30:48 +00001362 VG_(OSetGen_ResetIter)( activeSet );
1363 while ( (act = VG_(OSetGen_Next)(activeSet)) ) {
sewardj0ec07f32006-01-12 12:32:32 +00001364 show_active(" ", act);
1365 }
1366
sewardj738856f2009-07-15 14:48:32 +00001367 VG_(message)(Vg_DebugMsg, ">>\n");
njn16eeb4e2005-06-16 03:56:58 +00001368}
1369
njnc0ae7052005-08-25 22:55:19 +00001370/*--------------------------------------------------------------------*/
1371/*--- end ---*/
1372/*--------------------------------------------------------------------*/