njn | c0ae705 | 2005-08-25 22:55:19 +0000 | [diff] [blame] | 1 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | c0ae705 | 2005-08-25 22:55:19 +0000 | [diff] [blame] | 3 | /*--- Function replacement and wrapping. m_redir.c ---*/ |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | c0ae705 | 2005-08-25 22:55:19 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 9 | |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2010 Julian Seward |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 11 | jseward@acm.org |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 12 | Copyright (C) 2003-2010 Jeremy Fitzhardinge |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 13 | 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 | */ |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 32 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 33 | #include "pub_core_basics.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 34 | #include "pub_core_debuglog.h" |
njn | 88c5148 | 2005-06-25 20:49:33 +0000 | [diff] [blame] | 35 | #include "pub_core_debuginfo.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 36 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 37 | #include "pub_core_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 38 | #include "pub_core_libcprint.h" |
sewardj | d7a02db | 2008-12-12 08:07:49 +0000 | [diff] [blame] | 39 | #include "pub_core_seqmatch.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 40 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 41 | #include "pub_core_options.h" |
njn | 8a96ec5 | 2005-10-15 15:48:52 +0000 | [diff] [blame] | 42 | #include "pub_core_oset.h" |
njn | d1af003 | 2005-05-29 17:01:48 +0000 | [diff] [blame] | 43 | #include "pub_core_redir.h" |
njn | a7598f6 | 2005-06-18 03:27:58 +0000 | [diff] [blame] | 44 | #include "pub_core_trampoline.h" |
njn | 8bddf58 | 2005-05-13 23:40:55 +0000 | [diff] [blame] | 45 | #include "pub_core_transtab.h" |
sewardj | 461c69d | 2005-11-17 20:15:04 +0000 | [diff] [blame] | 46 | #include "pub_core_tooliface.h" // VG_(needs).malloc_replacement |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 47 | #include "pub_core_machine.h" // VG_(fnptr_to_fnentry) |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 48 | #include "pub_core_aspacemgr.h" // VG_(am_find_nsegment) |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 49 | #include "pub_core_xarray.h" |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 50 | #include "pub_core_clientstate.h" // VG_(client___libc_freeres_wrapper) |
| 51 | #include "pub_core_demangle.h" // VG_(maybe_Z_demangle) |
sewardj | 461c69d | 2005-11-17 20:15:04 +0000 | [diff] [blame] | 52 | |
bart | 257b91b | 2009-12-31 13:31:11 +0000 | [diff] [blame] | 53 | #include "config.h" /* GLIBC_2_* */ |
| 54 | |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 55 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 56 | /* 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. |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 70 | 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). |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 78 | |
| 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 |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 90 | call to VG_(redir_notify_delete_DebugInfo). It then removes from |
| 91 | its tables all active redirections in any way associated with that |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 92 | 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 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 100 | /*------------------------------------------------------------*/ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 101 | /*--- Semantics ---*/ |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 102 | /*------------------------------------------------------------*/ |
| 103 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 104 | /* The redirector holds two pieces of state: |
njn | d9109c6 | 2005-06-26 04:49:25 +0000 | [diff] [blame] | 105 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 106 | Specs - a set of (soname pattern, fnname pattern) -> redir addr |
| 107 | Active - a set of orig addr -> (bool, redir addr) |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 108 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 109 | 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. |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 112 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 113 | Active is a pure function of Specs and the current symbol table |
| 114 | state (maintained by m_debuginfo). Call the latter SyminfoState. |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 115 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 116 | Therefore whenever either Specs or SyminfoState changes, Active |
| 117 | must be recomputed. [Inefficient if done naively, but this is a |
| 118 | spec]. |
njn | 8a96ec5 | 2005-10-15 15:48:52 +0000 | [diff] [blame] | 119 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 120 | Active is computed as follows: |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 121 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 122 | 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 | } |
tom | 748a131 | 2005-04-02 15:53:01 +0000 | [diff] [blame] | 132 | } |
| 133 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 134 | [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 | |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 172 | When a new DebugInfo appears: |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 173 | - it may be the source of new specs |
| 174 | - it may be the source of new matches for existing specs |
| 175 | Therefore: |
| 176 | |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 177 | - (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 |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 180 | new actives. |
| 181 | |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 182 | - (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. |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 185 | |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 186 | - (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. |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 189 | |
| 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 | |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 199 | When a DebugInfo disappears: |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 200 | - 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 |
sewardj | 5092a97 | 2007-11-16 18:45:40 +0000 | [diff] [blame] | 212 | 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. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 221 | typedef |
| 222 | struct _Spec { |
| 223 | struct _Spec* next; /* linked list */ |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 224 | /* FIXED PARTS -- set when created and not changed */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 225 | 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? */ |
sewardj | f499c7d | 2009-08-10 19:39:02 +0000 | [diff] [blame] | 229 | const HChar** mandatory; /* non-NULL ==> abort V and print the |
| 230 | strings if from_sopatt is loaded but |
| 231 | from_fnpatt cannot be found */ |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 232 | /* 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 */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 235 | } |
| 236 | Spec; |
| 237 | |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 238 | /* 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 |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 240 | 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. */ |
| 243 | typedef |
| 244 | struct _TopSpec { |
| 245 | struct _TopSpec* next; /* linked list */ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 246 | DebugInfo* seginfo; /* symbols etc */ |
| 247 | Spec* specs; /* specs pulled out of seginfo */ |
| 248 | Bool mark; /* transient temporary used during deletion */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 249 | } |
| 250 | TopSpec; |
| 251 | |
| 252 | /* This is the top level list of redirections. m_debuginfo maintains |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 253 | a list of DebugInfos, and the idea here is to maintain a list with |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 254 | the same number of elements (in fact, with one more element, so as |
| 255 | to record abovementioned preloaded specifications.) */ |
| 256 | static 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. */ |
| 267 | typedef |
| 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? */ |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 274 | Bool isIFunc; /* indirect function? */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 275 | } |
| 276 | Active; |
| 277 | |
| 278 | /* The active set is a fast lookup table */ |
| 279 | static OSet* activeSet = NULL; |
| 280 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 281 | /* Wrapper routine for indirect functions */ |
| 282 | static Addr iFuncWrapper; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 283 | |
| 284 | /*------------------------------------------------------------*/ |
| 285 | /*--- FWDses ---*/ |
| 286 | /*------------------------------------------------------------*/ |
| 287 | |
| 288 | static void maybe_add_active ( Active /*by value; callee copies*/ ); |
| 289 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 290 | static void* dinfo_zalloc(HChar* ec, SizeT); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 291 | static void dinfo_free(void*); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 292 | static HChar* dinfo_strdup(HChar* ec, HChar*); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 293 | static Bool is_plausible_guest_addr(Addr); |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 294 | static Bool is_aix5_glink_idiom(Addr); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 295 | |
| 296 | static void show_redir_state ( HChar* who ); |
| 297 | static void show_active ( HChar* left, Active* act ); |
| 298 | |
sewardj | a672ea3 | 2006-04-29 18:03:14 +0000 | [diff] [blame] | 299 | static void handle_maybe_load_notifier( const UChar* soname, |
| 300 | HChar* symbol, Addr addr ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 301 | |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 302 | static void handle_require_text_symbols ( DebugInfo* ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 303 | |
| 304 | /*------------------------------------------------------------*/ |
| 305 | /*--- NOTIFICATIONS ---*/ |
| 306 | /*------------------------------------------------------------*/ |
| 307 | |
| 308 | static |
| 309 | void generate_and_add_actives ( |
| 310 | /* spec list and the owning TopSpec */ |
| 311 | Spec* specs, |
| 312 | TopSpec* parent_spec, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 313 | /* debuginfo and the owning TopSpec */ |
| 314 | DebugInfo* di, |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 315 | TopSpec* parent_sym |
| 316 | ); |
| 317 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 318 | /* Notify m_redir of the arrival of a new DebugInfo. This is fairly |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 319 | 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 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 325 | void VG_(redir_notify_new_DebugInfo)( DebugInfo* newsi ) |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 326 | { |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 327 | 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; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 338 | Bool isText; |
sewardj | a672ea3 | 2006-04-29 18:03:14 +0000 | [diff] [blame] | 339 | const UChar* newsi_soname; |
| 340 | |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 341 | # if defined(VG_PLAT_USES_PPCTOC) |
| 342 | check_ppcTOCs = True; |
| 343 | # endif |
| 344 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 345 | vg_assert(newsi); |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 346 | newsi_soname = VG_(DebugInfo_get_soname)(newsi); |
sewardj | a672ea3 | 2006-04-29 18:03:14 +0000 | [diff] [blame] | 347 | vg_assert(newsi_soname != NULL); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 348 | |
| 349 | /* stay sane: we don't already have this. */ |
| 350 | for (ts = topSpecs; ts; ts = ts->next) |
| 351 | vg_assert(ts->seginfo != newsi); |
| 352 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 353 | /* scan this DebugInfo's symbol table, pulling out and demangling |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 354 | any specs found */ |
| 355 | |
| 356 | specList = NULL; /* the spec list we're building up */ |
| 357 | |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 358 | nsyms = VG_(DebugInfo_syms_howmany)( newsi ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 359 | for (i = 0; i < nsyms; i++) { |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 360 | VG_(DebugInfo_syms_getidx)( newsi, i, &sym_addr, &sym_toc, |
| 361 | NULL, &sym_name, &isText, NULL ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 362 | ok = VG_(maybe_Z_demangle)( sym_name, demangled_sopatt, N_DEMANGLED, |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 363 | demangled_fnpatt, N_DEMANGLED, &isWrap ); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 364 | /* ignore data symbols */ |
| 365 | if (!isText) |
| 366 | continue; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 367 | 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. */ |
sewardj | a672ea3 | 2006-04-29 18:03:14 +0000 | [diff] [blame] | 370 | handle_maybe_load_notifier( newsi_soname, sym_name, sym_addr ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 371 | continue; |
| 372 | } |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 373 | 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 | } |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 380 | spec = dinfo_zalloc("redir.rnnD.1", sizeof(Spec)); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 381 | vg_assert(spec); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 382 | spec->from_sopatt = dinfo_strdup("redir.rnnD.2", demangled_sopatt); |
| 383 | spec->from_fnpatt = dinfo_strdup("redir.rnnD.3", demangled_fnpatt); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 384 | 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 */ |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 392 | spec->done = False; /* not significant */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 393 | specList = spec; |
| 394 | } |
| 395 | |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 396 | if (check_ppcTOCs) { |
| 397 | for (i = 0; i < nsyms; i++) { |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 398 | VG_(DebugInfo_syms_getidx)( newsi, i, &sym_addr, &sym_toc, |
| 399 | NULL, &sym_name, &isText, NULL ); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 400 | ok = isText |
| 401 | && VG_(maybe_Z_demangle)( |
| 402 | sym_name, demangled_sopatt, N_DEMANGLED, |
| 403 | demangled_fnpatt, N_DEMANGLED, &isWrap ); |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 404 | 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) |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 416 | /* a redirect to some other copy of that symbol, which |
| 417 | does have a TOC value, already exists */ |
| 418 | continue; |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 419 | |
| 420 | /* Complain */ |
| 421 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 422 | "WARNING: no TOC ptr for redir/wrap to %s %s\n", |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 423 | demangled_sopatt, demangled_fnpatt); |
| 424 | } |
| 425 | } |
| 426 | |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 427 | /* Ok. Now specList holds the list of specs from the DebugInfo. |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 428 | Build a new TopSpec, but don't add it to topSpecs yet. */ |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 429 | newts = dinfo_zalloc("redir.rnnD.4", sizeof(TopSpec)); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 430 | 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 | } |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 459 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 460 | /* 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)) |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 475 | show_redir_state("after VG_(redir_notify_new_DebugInfo)"); |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 476 | |
| 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); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | #undef N_DEMANGLED |
| 484 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 485 | /* 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 | |
| 490 | void 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 | } |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 509 | |
| 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 | |
| 515 | static |
| 516 | void generate_and_add_actives ( |
| 517 | /* spec list and the owning TopSpec */ |
| 518 | Spec* specs, |
| 519 | TopSpec* parent_spec, |
| 520 | /* seginfo and the owning TopSpec */ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 521 | DebugInfo* di, |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 522 | TopSpec* parent_sym |
| 523 | ) |
| 524 | { |
| 525 | Spec* sp; |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 526 | Bool anyMark, isText, isIFunc; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 527 | Active act; |
| 528 | Int nsyms, i; |
| 529 | Addr sym_addr; |
| 530 | HChar* sym_name; |
| 531 | |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 532 | /* 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. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 535 | anyMark = False; |
| 536 | for (sp = specs; sp; sp = sp->next) { |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 537 | sp->done = False; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 538 | sp->mark = VG_(string_match)( sp->from_sopatt, |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 539 | VG_(DebugInfo_get_soname)(di) ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 540 | 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. */ |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 549 | nsyms = VG_(DebugInfo_syms_howmany)( di ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 550 | for (i = 0; i < nsyms; i++) { |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 551 | VG_(DebugInfo_syms_getidx)( di, i, &sym_addr, NULL, NULL, |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 552 | &sym_name, &isText, &isIFunc ); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 553 | |
| 554 | /* ignore data symbols */ |
| 555 | if (!isText) |
| 556 | continue; |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 557 | |
| 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 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 568 | 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; |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 578 | act.isIFunc = isIFunc; |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 579 | sp->done = True; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 580 | maybe_add_active( act ); |
| 581 | } |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 582 | } /* 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) { |
sewardj | f499c7d | 2009-08-10 19:39:02 +0000 | [diff] [blame] | 595 | const HChar** strp; |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 596 | 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)( |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 618 | "%ssymbols from the object with soname: %s\n", |
| 619 | v, VG_(DebugInfo_get_soname)(di)); |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 620 | VG_(printf)( |
| 621 | "%s\n", v); |
sewardj | b57e693 | 2009-08-02 12:21:31 +0000 | [diff] [blame] | 622 | |
| 623 | for (strp = sp->mandatory; *strp; strp++) |
| 624 | VG_(printf)( |
| 625 | "%s%s\n", v, *strp); |
| 626 | |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 627 | 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); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
| 636 | |
| 637 | /* Add an act (passed by value; is copied here) and deal with |
| 638 | conflicting bindings. */ |
| 639 | static 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 | |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 664 | old = VG_(OSetGen_Lookup)( activeSet, &act.from_addr ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 665 | 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; |
njn | d9109c6 | 2005-06-26 04:49:25 +0000 | [diff] [blame] | 673 | } else { |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 674 | /* This appears to be a duplicate of an existing binding. |
| 675 | Safe(ish) -- ignore. */ |
| 676 | /* XXXXXXXXXXX COMPLAIN if new and old parents differ */ |
tom | 748a131 | 2005-04-02 15:53:01 +0000 | [diff] [blame] | 677 | } |
njn | d9109c6 | 2005-06-26 04:49:25 +0000 | [diff] [blame] | 678 | } else { |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 679 | Active* a = VG_(OSetGen_AllocNode)(activeSet, sizeof(Active)); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 680 | vg_assert(a); |
| 681 | *a = act; |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 682 | VG_(OSetGen_Insert)(activeSet, a); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 683 | /* 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, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 689 | "redir_new_DebugInfo(from_addr)"); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 690 | VG_(discard_translations)( (Addr64)act.to_addr, 1, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 691 | "redir_new_DebugInfo(to_addr)"); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 692 | } |
| 693 | return; |
| 694 | |
| 695 | bad: |
| 696 | vg_assert(what); |
| 697 | if (VG_(clo_verbosity) > 1) { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 698 | VG_(message)(Vg_UserMsg, "WARNING: %s\n", what); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 699 | show_active( " new: ", &act); |
njn | d9109c6 | 2005-06-26 04:49:25 +0000 | [diff] [blame] | 700 | } |
tom | 748a131 | 2005-04-02 15:53:01 +0000 | [diff] [blame] | 701 | } |
| 702 | |
njn | d9109c6 | 2005-06-26 04:49:25 +0000 | [diff] [blame] | 703 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 704 | /* Notify m_redir of the deletion of a DebugInfo. This is relatively |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 705 | simple -- just get rid of all actives derived from it, and free up |
| 706 | the associated list elements. */ |
| 707 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 708 | void VG_(redir_notify_delete_DebugInfo)( DebugInfo* delsi ) |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 709 | { |
| 710 | TopSpec* ts; |
| 711 | TopSpec* tsPrev; |
| 712 | Spec* sp; |
| 713 | Spec* sp_next; |
| 714 | OSet* tmpSet; |
| 715 | Active* act; |
| 716 | Bool delMe; |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 717 | Addr addr; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 718 | |
| 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 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 732 | vg_assert(ts); /* else we don't have the deleted DebugInfo */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 733 | vg_assert(ts->seginfo == delsi); |
| 734 | |
| 735 | /* Traverse the actives, copying the addresses of those we intend |
| 736 | to delete into tmpSet. */ |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 737 | tmpSet = VG_(OSetWord_Create)(dinfo_zalloc, "redir.rndD.1", dinfo_free); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 738 | |
| 739 | ts->mark = True; |
| 740 | |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 741 | VG_(OSetGen_ResetIter)( activeSet ); |
| 742 | while ( (act = VG_(OSetGen_Next)(activeSet)) ) { |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 743 | 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 |
sewardj | e6647f4 | 2006-03-12 00:39:19 +0000 | [diff] [blame] | 750 | 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; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | if (delMe) { |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 763 | VG_(OSetWord_Insert)( tmpSet, act->from_addr ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 764 | /* 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, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 767 | "redir_del_DebugInfo(from_addr)"); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 768 | VG_(discard_translations)( (Addr64)act->to_addr, 1, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 769 | "redir_del_DebugInfo(to_addr)"); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 773 | /* 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 ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 777 | vg_assert(act); |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 778 | VG_(OSetGen_FreeNode)( activeSet, act ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 779 | } |
| 780 | |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 781 | VG_(OSetWord_Destroy)( tmpSet ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 782 | |
| 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) { |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 786 | if (sp->from_sopatt) dinfo_free(sp->from_sopatt); |
| 787 | if (sp->from_fnpatt) dinfo_free(sp->from_fnpatt); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 788 | sp_next = sp->next; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 789 | dinfo_free(sp); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | if (tsPrev == NULL) { |
| 793 | /* first in list */ |
| 794 | topSpecs = ts->next; |
njn | d9109c6 | 2005-06-26 04:49:25 +0000 | [diff] [blame] | 795 | } else { |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 796 | tsPrev->next = ts->next; |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 797 | } |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 798 | dinfo_free(ts); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 799 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 800 | if (VG_(clo_trace_redir)) |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 801 | show_redir_state("after VG_(redir_notify_delete_DebugInfo)"); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 802 | } |
| 803 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 804 | |
| 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. */ |
| 812 | Addr VG_(redir_do_lookup) ( Addr orig, Bool* isWrap ) |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 813 | { |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 814 | Active* r = VG_(OSetGen_Lookup)(activeSet, &orig); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 815 | if (r == NULL) |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 816 | return orig; |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 817 | |
| 818 | vg_assert(r->to_addr != 0); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 819 | if (isWrap) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 820 | *isWrap = r->isWrap || r->isIFunc; |
tom | 3b42e16 | 2009-11-16 09:06:09 +0000 | [diff] [blame] | 821 | if (r->isIFunc) { |
| 822 | vg_assert(iFuncWrapper); |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 823 | return iFuncWrapper; |
tom | 3b42e16 | 2009-11-16 09:06:09 +0000 | [diff] [blame] | 824 | } |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 825 | return r->to_addr; |
| 826 | } |
| 827 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 828 | |
| 829 | /*------------------------------------------------------------*/ |
| 830 | /*--- INITIALISATION ---*/ |
| 831 | /*------------------------------------------------------------*/ |
| 832 | |
| 833 | /* Add a never-delete-me Active. */ |
| 834 | |
| 835 | __attribute__((unused)) /* only used on amd64 */ |
| 836 | static void add_hardwired_active ( Addr from, Addr to ) |
njn | 8a96ec5 | 2005-10-15 15:48:52 +0000 | [diff] [blame] | 837 | { |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 838 | 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; |
tom | b6fd365 | 2009-11-01 16:37:33 +0000 | [diff] [blame] | 844 | act.isIFunc = False; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 845 | maybe_add_active( act ); |
njn | 8a96ec5 | 2005-10-15 15:48:52 +0000 | [diff] [blame] | 846 | } |
| 847 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 848 | |
| 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 */ |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 856 | static void add_hardwired_spec ( HChar* sopatt, HChar* fnpatt, |
| 857 | Addr to_addr, |
sewardj | f499c7d | 2009-08-10 19:39:02 +0000 | [diff] [blame] | 858 | const HChar** mandatory ) |
njn | 8a96ec5 | 2005-10-15 15:48:52 +0000 | [diff] [blame] | 859 | { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 860 | Spec* spec = dinfo_zalloc("redir.ahs.1", sizeof(Spec)); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 861 | vg_assert(spec); |
| 862 | |
| 863 | if (topSpecs == NULL) { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 864 | topSpecs = dinfo_zalloc("redir.ahs.2", sizeof(TopSpec)); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 865 | vg_assert(topSpecs); |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 866 | /* symtab_zalloc sets all fields to zero */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | vg_assert(topSpecs != NULL); |
| 870 | vg_assert(topSpecs->next == NULL); |
| 871 | vg_assert(topSpecs->seginfo == NULL); |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 872 | /* FIXED PARTS */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 873 | spec->from_sopatt = sopatt; |
| 874 | spec->from_fnpatt = fnpatt; |
| 875 | spec->to_addr = to_addr; |
| 876 | spec->isWrap = False; |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 877 | spec->mandatory = mandatory; |
| 878 | /* VARIABLE PARTS */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 879 | spec->mark = False; /* not significant */ |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 880 | spec->done = False; /* not significant */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 881 | |
| 882 | spec->next = topSpecs->specs; |
| 883 | topSpecs->specs = spec; |
njn | 8a96ec5 | 2005-10-15 15:48:52 +0000 | [diff] [blame] | 884 | } |
| 885 | |
njn | 8a96ec5 | 2005-10-15 15:48:52 +0000 | [diff] [blame] | 886 | |
sewardj | b57e693 | 2009-08-02 12:21:31 +0000 | [diff] [blame] | 887 | __attribute__((unused)) /* not used on all platforms */ |
| 888 | static 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", |
sewardj | d85ee0c | 2011-02-11 16:47:03 +0000 | [diff] [blame] | 894 | "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", |
sewardj | b57e693 | 2009-08-02 12:21:31 +0000 | [diff] [blame] | 899 | NULL |
| 900 | }; |
| 901 | |
| 902 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 903 | /* 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) |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 906 | assumption that no DebugInfos have so far been created, and so when |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 907 | they are created, that will happen. */ |
| 908 | |
| 909 | void VG_(redir_initialise) ( void ) |
| 910 | { |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 911 | // Assert that there are no DebugInfos so far |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 912 | vg_assert( VG_(next_DebugInfo)(NULL) == NULL ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 913 | |
| 914 | // Initialise active mapping. |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 915 | activeSet = VG_(OSetGen_Create)(offsetof(Active, from_addr), |
| 916 | NULL, // Use fast comparison |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 917 | dinfo_zalloc, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 918 | "redir.ri.1", |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 919 | dinfo_free); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 920 | |
| 921 | // The rest of this function just adds initial Specs. |
| 922 | |
| 923 | # if defined(VGP_x86_linux) |
sewardj | 6a443b2 | 2005-11-20 19:37:54 +0000 | [diff] [blame] | 924 | /* 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)) { |
sewardj | d28cc96 | 2011-03-28 08:22:55 +0000 | [diff] [blame] | 927 | 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 |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 938 | add_hardwired_spec( |
| 939 | "ld-linux.so.2", "index", |
sewardj | d28cc96 | 2011-03-28 08:22:55 +0000 | [diff] [blame] | 940 | (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); |
sewardj | 6a443b2 | 2005-11-20 19:37:54 +0000 | [diff] [blame] | 944 | } |
sewardj | b9bce63 | 2005-06-21 01:41:34 +0000 | [diff] [blame] | 945 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 946 | # elif defined(VGP_amd64_linux) |
njn | d1af003 | 2005-05-29 17:01:48 +0000 | [diff] [blame] | 947 | /* Redirect vsyscalls to local versions */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 948 | add_hardwired_active( |
sewardj | b9bce63 | 2005-06-21 01:41:34 +0000 | [diff] [blame] | 949 | 0xFFFFFFFFFF600000ULL, |
| 950 | (Addr)&VG_(amd64_linux_REDIR_FOR_vgettimeofday) |
| 951 | ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 952 | add_hardwired_active( |
tom | f5db3b6 | 2005-06-21 13:26:17 +0000 | [diff] [blame] | 953 | 0xFFFFFFFFFF600400ULL, |
sewardj | b9bce63 | 2005-06-21 01:41:34 +0000 | [diff] [blame] | 954 | (Addr)&VG_(amd64_linux_REDIR_FOR_vtime) |
| 955 | ); |
| 956 | |
sewardj | b57e693 | 2009-08-02 12:21:31 +0000 | [diff] [blame] | 957 | /* 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)) { |
bart | 2ebe411 | 2008-06-01 16:33:37 +0000 | [diff] [blame] | 960 | |
sewardj | b57e693 | 2009-08-02 12:21:31 +0000 | [diff] [blame] | 961 | 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) |
sewardj | 96fcfc8 | 2005-11-18 21:12:52 +0000 | [diff] [blame] | 977 | /* 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)) { |
sewardj | 0fd3049 | 2007-11-16 19:39:49 +0000 | [diff] [blame] | 980 | |
sewardj | 5092a97 | 2007-11-16 18:45:40 +0000 | [diff] [blame] | 981 | /* this is mandatory - can't sanely continue without it */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 982 | add_hardwired_spec( |
| 983 | "ld.so.1", "strlen", |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 984 | (Addr)&VG_(ppc32_linux_REDIR_FOR_strlen), |
sewardj | b57e693 | 2009-08-02 12:21:31 +0000 | [diff] [blame] | 985 | complain_about_stripped_glibc_ldso |
sewardj | 96fcfc8 | 2005-11-18 21:12:52 +0000 | [diff] [blame] | 986 | ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 987 | add_hardwired_spec( |
sewardj | d5a30a4 | 2006-02-09 10:29:43 +0000 | [diff] [blame] | 988 | "ld.so.1", "strcmp", |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 989 | (Addr)&VG_(ppc32_linux_REDIR_FOR_strcmp), |
sewardj | 5092a97 | 2007-11-16 18:45:40 +0000 | [diff] [blame] | 990 | NULL /* not mandatory - so why bother at all? */ |
| 991 | /* glibc-2.6.1 (openSUSE 10.3, ppc32) seems fine without it */ |
sewardj | 96fcfc8 | 2005-11-18 21:12:52 +0000 | [diff] [blame] | 992 | ); |
sewardj | cd8c95a | 2006-08-25 11:48:38 +0000 | [diff] [blame] | 993 | add_hardwired_spec( |
| 994 | "ld.so.1", "index", |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 995 | (Addr)&VG_(ppc32_linux_REDIR_FOR_strchr), |
sewardj | 5092a97 | 2007-11-16 18:45:40 +0000 | [diff] [blame] | 996 | NULL /* not mandatory - so why bother at all? */ |
| 997 | /* glibc-2.6.1 (openSUSE 10.3, ppc32) seems fine without it */ |
sewardj | cd8c95a | 2006-08-25 11:48:38 +0000 | [diff] [blame] | 998 | ); |
bart | 2ebe411 | 2008-06-01 16:33:37 +0000 | [diff] [blame] | 999 | } |
sewardj | b9bce63 | 2005-06-21 01:41:34 +0000 | [diff] [blame] | 1000 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1001 | # elif defined(VGP_ppc64_linux) |
sewardj | f0915fc | 2006-01-05 14:07:04 +0000 | [diff] [blame] | 1002 | /* 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 | |
sewardj | 0fd3049 | 2007-11-16 19:39:49 +0000 | [diff] [blame] | 1006 | /* this is mandatory - can't sanely continue without it */ |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 1007 | add_hardwired_spec( |
| 1008 | "ld64.so.1", "strlen", |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 1009 | (Addr)VG_(fnptr_to_fnentry)( &VG_(ppc64_linux_REDIR_FOR_strlen) ), |
sewardj | b57e693 | 2009-08-02 12:21:31 +0000 | [diff] [blame] | 1010 | complain_about_stripped_glibc_ldso |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 1011 | ); |
sewardj | f0915fc | 2006-01-05 14:07:04 +0000 | [diff] [blame] | 1012 | |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 1013 | add_hardwired_spec( |
| 1014 | "ld64.so.1", "index", |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 1015 | (Addr)VG_(fnptr_to_fnentry)( &VG_(ppc64_linux_REDIR_FOR_strchr) ), |
sewardj | 0fd3049 | 2007-11-16 19:39:49 +0000 | [diff] [blame] | 1016 | NULL /* not mandatory - so why bother at all? */ |
| 1017 | /* glibc-2.5 (FC6, ppc64) seems fine without it */ |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 1018 | ); |
bart | 2ebe411 | 2008-06-01 16:33:37 +0000 | [diff] [blame] | 1019 | } |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 1020 | |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 1021 | # 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), |
sewardj | d69cee8 | 2010-08-22 12:13:35 +0000 | [diff] [blame] | 1028 | complain_about_stripped_glibc_ldso |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 1029 | ); |
| 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), |
sewardj | d69cee8 | 2010-08-22 12:13:35 +0000 | [diff] [blame] | 1038 | complain_about_stripped_glibc_ldso |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 1039 | ); |
| 1040 | } |
| 1041 | /* nothing so far */ |
| 1042 | |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 1043 | # elif defined(VGP_ppc32_aix5) |
| 1044 | /* nothing so far */ |
| 1045 | |
| 1046 | # elif defined(VGP_ppc64_aix5) |
| 1047 | /* nothing so far */ |
| 1048 | |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 1049 | # elif defined(VGP_x86_darwin) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 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", |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 1054 | (Addr)&VG_(x86_darwin_REDIR_FOR_strcmp), NULL); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1055 | add_hardwired_spec("dyld", "strlen", |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 1056 | (Addr)&VG_(x86_darwin_REDIR_FOR_strlen), NULL); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1057 | add_hardwired_spec("dyld", "strcat", |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 1058 | (Addr)&VG_(x86_darwin_REDIR_FOR_strcat), NULL); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1059 | add_hardwired_spec("dyld", "strcpy", |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 1060 | (Addr)&VG_(x86_darwin_REDIR_FOR_strcpy), NULL); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1061 | add_hardwired_spec("dyld", "strlcat", |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 1062 | (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); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1079 | // DDD: #warning fixme rdar://6166275 |
| 1080 | add_hardwired_spec("dyld", "arc4random", |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 1081 | (Addr)&VG_(amd64_darwin_REDIR_FOR_arc4random), NULL); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1084 | # elif defined(VGP_s390x_linux) |
| 1085 | /* nothing so far */ |
| 1086 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1087 | # else |
| 1088 | # error Unknown platform |
| 1089 | # endif |
| 1090 | |
| 1091 | if (VG_(clo_trace_redir)) |
| 1092 | show_redir_state("after VG_(redir_initialise)"); |
njn | d1af003 | 2005-05-29 17:01:48 +0000 | [diff] [blame] | 1093 | } |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 1094 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1095 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1096 | /*------------------------------------------------------------*/ |
| 1097 | /*--- MISC HELPERS ---*/ |
| 1098 | /*------------------------------------------------------------*/ |
| 1099 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1100 | static void* dinfo_zalloc(HChar* ec, SizeT n) { |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 1101 | void* p; |
| 1102 | vg_assert(n > 0); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1103 | p = VG_(arena_malloc)(VG_AR_DINFO, ec, n); |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 1104 | tl_assert(p); |
| 1105 | VG_(memset)(p, 0, n); |
| 1106 | return p; |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 1109 | static void dinfo_free(void* p) { |
sewardj | df24cf1 | 2007-11-16 18:32:40 +0000 | [diff] [blame] | 1110 | tl_assert(p); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 1111 | return VG_(arena_free)(VG_AR_DINFO, p); |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1114 | static HChar* dinfo_strdup(HChar* ec, HChar* str) |
njn | bc6d84d | 2005-06-19 18:58:03 +0000 | [diff] [blame] | 1115 | { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1116 | return VG_(arena_strdup)(VG_AR_DINFO, ec, str); |
njn | bc6d84d | 2005-06-19 18:58:03 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1119 | /* Really this should be merged with translations_allowable_from_seg |
| 1120 | in m_translate. */ |
| 1121 | static Bool is_plausible_guest_addr(Addr a) |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1122 | { |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 1123 | NSegment const* seg = VG_(am_find_nsegment)(a); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1124 | return seg != NULL |
| 1125 | && (seg->kind == SkAnonC || seg->kind == SkFileC) |
| 1126 | && (seg->hasX || seg->hasR); /* crude x86-specific hack */ |
| 1127 | } |
| 1128 | |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 1129 | /* 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). */ |
| 1134 | static 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 | } |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1165 | |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 1166 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1167 | /*------------------------------------------------------------*/ |
| 1168 | /*--- NOTIFY-ON-LOAD FUNCTIONS ---*/ |
| 1169 | /*------------------------------------------------------------*/ |
| 1170 | |
sewardj | a672ea3 | 2006-04-29 18:03:14 +0000 | [diff] [blame] | 1171 | static |
| 1172 | void handle_maybe_load_notifier( const UChar* soname, |
| 1173 | HChar* symbol, Addr addr ) |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1174 | { |
sewardj | a672ea3 | 2006-04-29 18:03:14 +0000 | [diff] [blame] | 1175 | # 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. */ |
sewardj | dd9508c | 2011-05-04 09:03:41 +0000 | [diff] [blame] | 1190 | vg_assert(symbol); // assert rather than segfault if it is NULL |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1191 | 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; |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 1198 | else if (VG_(strcmp)(symbol, VG_STRINGIFY(VG_NOTIFY_ON_LOAD(ifunc_wrapper))) == 0) |
| 1199 | iFuncWrapper = addr; |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1200 | else |
| 1201 | vg_assert2(0, "unrecognised load notification function: %s", symbol); |
| 1202 | } |
| 1203 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1204 | |
| 1205 | /*------------------------------------------------------------*/ |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 1206 | /*--- 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 | */ |
| 1213 | static 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 | /*------------------------------------------------------------*/ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1314 | /*--- SANITY/DEBUG ---*/ |
| 1315 | /*------------------------------------------------------------*/ |
| 1316 | |
| 1317 | static void show_spec ( HChar* left, Spec* spec ) |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1318 | { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1319 | 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 ); |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1327 | static void show_active ( HChar* left, Active* act ) |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1328 | { |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1329 | 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 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1338 | VG_(message)(Vg_DebugMsg, "%s0x%08llx (%20s) %s-> 0x%08llx %s\n", |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1339 | left, |
| 1340 | (ULong)act->from_addr, name1, |
| 1341 | act->isWrap ? "W" : "R", |
| 1342 | (ULong)act->to_addr, name2 ); |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1345 | static void show_redir_state ( HChar* who ) |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1346 | { |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1347 | TopSpec* ts; |
| 1348 | Spec* sp; |
| 1349 | Active* act; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1350 | VG_(message)(Vg_DebugMsg, "<<\n"); |
| 1351 | VG_(message)(Vg_DebugMsg, " ------ REDIR STATE %s ------\n", who); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1352 | for (ts = topSpecs; ts; ts = ts->next) { |
| 1353 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1354 | " TOPSPECS of soname %s\n", |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 1355 | ts->seginfo |
| 1356 | ? (HChar*)VG_(DebugInfo_get_soname)(ts->seginfo) |
| 1357 | : "(hardwired)" ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1358 | for (sp = ts->specs; sp; sp = sp->next) |
| 1359 | show_spec(" ", sp); |
| 1360 | } |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1361 | VG_(message)(Vg_DebugMsg, " ------ ACTIVE ------\n"); |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 1362 | VG_(OSetGen_ResetIter)( activeSet ); |
| 1363 | while ( (act = VG_(OSetGen_Next)(activeSet)) ) { |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1364 | show_active(" ", act); |
| 1365 | } |
| 1366 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1367 | VG_(message)(Vg_DebugMsg, ">>\n"); |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
njn | c0ae705 | 2005-08-25 22:55:19 +0000 | [diff] [blame] | 1370 | /*--------------------------------------------------------------------*/ |
| 1371 | /*--- end ---*/ |
| 1372 | /*--------------------------------------------------------------------*/ |