blob: 1539980034021776f78dc11ec75d0d3a92890737 [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
njn53612422005-03-12 16:22:54 +000010 Copyright (C) 2000-2005 Julian Seward
sewardjcbdddcf2005-03-10 23:23:45 +000011 jseward@acm.org
12 Copyright (C) 2003-2005 Jeremy Fitzhardinge
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*/
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"
njnaf1d7df2005-06-11 01:31:52 +000039#include "pub_core_mallocfree.h"
njn20242342005-05-16 23:31:24 +000040#include "pub_core_options.h"
njn8a96ec52005-10-15 15:48:52 +000041#include "pub_core_oset.h"
njnd1af0032005-05-29 17:01:48 +000042#include "pub_core_redir.h"
njna7598f62005-06-18 03:27:58 +000043#include "pub_core_trampoline.h"
njn8bddf582005-05-13 23:40:55 +000044#include "pub_core_transtab.h"
sewardj461c69d2005-11-17 20:15:04 +000045#include "pub_core_tooliface.h" // VG_(needs).malloc_replacement
46
sewardj55f9d1a2005-04-25 11:11:44 +000047
sewardjcbdddcf2005-03-10 23:23:45 +000048/*------------------------------------------------------------*/
49/*--- General purpose redirection. ---*/
50/*------------------------------------------------------------*/
51
njnd9109c62005-06-26 04:49:25 +000052#define TRACE_REDIR(format, args...) \
53 if (VG_(clo_trace_redir)) { VG_(message)(Vg_DebugMsg, format, ## args); }
54
sewardjcbdddcf2005-03-10 23:23:45 +000055/*
56 wraps and redirections, indexed by from_addr
57
58 Redirection and wrapping are two distinct mechanisms which Valgrind
59 can use to change the client's control flow.
60
61 Redirection intercepts a call to a client function, and re-points it
62 to a new piece of code (presumably functionally equivalent). The
63 original code is never run.
64
65 Wrapping does call the client's original code, but calls "before"
66 and "after" functions which can inspect (and perhaps modify) the
67 function's arguments and return value.
68 */
69struct _CodeRedirect {
njn8a96ec52005-10-15 15:48:52 +000070 Addr from_addr; /* old addr -- MUST BE THE FIRST WORD! */
71
sewardjcbdddcf2005-03-10 23:23:45 +000072 enum redir_type {
73 R_REDIRECT, /* plain redirection */
74 R_WRAPPER, /* wrap with valgrind-internal code */
75 R_CLIENT_WRAPPER, /* wrap with client-side code */
76 } type;
77
78 const Char *from_lib; /* library qualifier pattern */
79 const Char *from_sym; /* symbol */
sewardjcbdddcf2005-03-10 23:23:45 +000080
njnd9109c62005-06-26 04:49:25 +000081 Addr to_addr; /* used for redirection -- new addr */
82 const FuncWrapper *wrapper; /* used for wrapping */
sewardjcbdddcf2005-03-10 23:23:45 +000083
njnd9109c62005-06-26 04:49:25 +000084 CodeRedirect *next; /* next pointer on unresolved list */
sewardjcbdddcf2005-03-10 23:23:45 +000085};
86
njn8a96ec52005-10-15 15:48:52 +000087static OSet* resolved_redirs;
sewardjcbdddcf2005-03-10 23:23:45 +000088
njn8a96ec52005-10-15 15:48:52 +000089// We use a linked list here rather than an OSet, because we want to
90// traverse it and possibly remove elements as we look at them. OSet
91// doesn't support this very well.
njnd9109c62005-06-26 04:49:25 +000092static CodeRedirect *unresolved_redirs = NULL;
sewardjcbdddcf2005-03-10 23:23:45 +000093
njn4f612c22005-06-25 20:22:43 +000094static Bool soname_matches(const Char *pattern, const Char* soname)
sewardjcbdddcf2005-03-10 23:23:45 +000095{
njn41f8e4a2005-06-25 20:13:05 +000096 // pattern must start with "soname:"
97 vg_assert(NULL != pattern);
98 vg_assert(0 == VG_(strncmp)(pattern, "soname:", 7));
sewardjcbdddcf2005-03-10 23:23:45 +000099
njn4f612c22005-06-25 20:22:43 +0000100 if (NULL == soname)
sewardjcbdddcf2005-03-10 23:23:45 +0000101 return False;
102
njn4f612c22005-06-25 20:22:43 +0000103 return VG_(string_match)(pattern + 7, soname);
sewardjcbdddcf2005-03-10 23:23:45 +0000104}
105
njnd9109c62005-06-26 04:49:25 +0000106Bool VG_(is_resolved)(const CodeRedirect *redir)
sewardjcbdddcf2005-03-10 23:23:45 +0000107{
108 return redir->from_addr != 0;
109}
110
njnd9109c62005-06-26 04:49:25 +0000111// Prepends redir to the unresolved list.
112static void add_redir_to_unresolved_list(CodeRedirect *redir)
sewardjcbdddcf2005-03-10 23:23:45 +0000113{
njnd9109c62005-06-26 04:49:25 +0000114 redir->next = unresolved_redirs;
115 unresolved_redirs = redir;
sewardjcbdddcf2005-03-10 23:23:45 +0000116}
117
sewardjf9a82d12005-07-23 09:52:21 +0000118static void add_redir_to_resolved_list(CodeRedirect *redir, Bool need_discard)
tom748a1312005-04-02 15:53:01 +0000119{
njnd9109c62005-06-26 04:49:25 +0000120 vg_assert(redir->from_addr);
121
122 switch (redir->type) {
123 case R_REDIRECT: {
njnd9109c62005-06-26 04:49:25 +0000124 TRACE_REDIR(" redir resolved (%s:%s=%p -> %p)",
125 redir->from_lib, redir->from_sym, redir->from_addr,
126 redir->to_addr);
127
128 vg_assert(redir->to_addr != 0);
tom748a1312005-04-02 15:53:01 +0000129
sewardjf9a82d12005-07-23 09:52:21 +0000130 if (need_discard) {
tom748a1312005-04-02 15:53:01 +0000131 /* For some given (from, to) redir, the "from" function got
sewardjf9a82d12005-07-23 09:52:21 +0000132 loaded before the .so containing "to" became available so
133 we need to discard any existing translations involving
134 the "from" function.
tom748a1312005-04-02 15:53:01 +0000135
136 Note, we only really need to discard the first bb of the
137 old entry point, and so we avoid the problem of having to
138 figure out how big that bb was -- since it is at least 1
139 byte of original code, we can just pass 1 as the original
140 size to invalidate_translations() and it will indeed get
141 rid of the translation.
142
143 Note, this is potentially expensive -- discarding
sewardjeccb2d82005-07-23 11:36:03 +0000144 translations requires a complete search through all of
145 them.
tom748a1312005-04-02 15:53:01 +0000146 */
sewardjf9a82d12005-07-23 09:52:21 +0000147 TRACE_REDIR("Discarding translation due to redirect of already loaded function" );
njnd9109c62005-06-26 04:49:25 +0000148 TRACE_REDIR(" %s:%s(%p) -> %p)", redir->from_lib, redir->from_sym,
149 redir->from_addr, redir->to_addr );
sewardj45f4e7c2005-09-27 19:20:21 +0000150 VG_(discard_translations)((Addr64)redir->from_addr, 1,
151 "add_redir_to_resolved_list");
tom748a1312005-04-02 15:53:01 +0000152 }
153
njn8a96ec52005-10-15 15:48:52 +0000154 // This entails a possible double OSet lookup -- one for Contains(),
155 // one for Insert(). If we had OSet_InsertIfNonDup() we could do it
156 // with one lookup.
157 if ( ! VG_(OSet_Contains)(resolved_redirs, &redir->from_addr) ) {
158 VG_(OSet_Insert)(resolved_redirs, redir);
njnd9109c62005-06-26 04:49:25 +0000159 } else {
njnd9109c62005-06-26 04:49:25 +0000160 TRACE_REDIR(" redir %s:%s:%p->%p duplicated\n",
161 redir->from_lib, redir->from_sym, redir->from_addr,
162 redir->to_addr);
sewardj6a443b22005-11-20 19:37:54 +0000163 // jrs 20 Nov 05: causes this: m_mallocfree.c:170
164 // (mk_plain_bszB): Assertion 'bszB != 0' failed.
165 // Perhaps it is an invalid free? Disable for now
166 // XXX leak?
167 //VG_(arena_free)(VG_AR_SYMTAB, redir);
tom748a1312005-04-02 15:53:01 +0000168 }
169 break;
njnd9109c62005-06-26 04:49:25 +0000170 }
tom748a1312005-04-02 15:53:01 +0000171
172 case R_WRAPPER:
njnd9109c62005-06-26 04:49:25 +0000173 TRACE_REDIR(" wrapper resolved (%s:%s=%p -> wrapper)",
174 redir->from_lib, redir->from_sym, redir->from_addr);
175
176 vg_assert(redir->wrapper);
tom748a1312005-04-02 15:53:01 +0000177
178 /* XXX redir leaked */
179 //VG_(wrap_function)(redir->from_addr, redir->wrapper);
180 break;
181
182 case R_CLIENT_WRAPPER:
njnd9109c62005-06-26 04:49:25 +0000183 vg_assert(redir->wrapper);
tom748a1312005-04-02 15:53:01 +0000184 VG_(core_panic)("not implemented");
185 break;
186 }
187}
188
njnd9109c62005-06-26 04:49:25 +0000189// Resolve a redir using si if possible. Returns True if it succeeded.
190static Bool resolve_redir_with_seginfo(CodeRedirect *redir, const SegInfo *si)
sewardjcbdddcf2005-03-10 23:23:45 +0000191{
njnd9109c62005-06-26 04:49:25 +0000192 Bool ok;
sewardjcbdddcf2005-03-10 23:23:45 +0000193
194 vg_assert(si != NULL);
njnd9109c62005-06-26 04:49:25 +0000195 vg_assert(redir->from_addr == 0 );
196 vg_assert(redir->from_sym != NULL);
sewardjcbdddcf2005-03-10 23:23:45 +0000197
njnd9109c62005-06-26 04:49:25 +0000198 // Resolved if the soname matches and we find the symbol.
199 ok = soname_matches(redir->from_lib, VG_(seginfo_soname)(si));
200 if (ok) {
njn748ace42005-06-21 03:36:01 +0000201 redir->from_addr = VG_(reverse_search_one_symtab)(si, redir->from_sym);
njnd9109c62005-06-26 04:49:25 +0000202 ok = ( redir->from_addr == 0 ? False : True );
sewardjcbdddcf2005-03-10 23:23:45 +0000203 }
njnd9109c62005-06-26 04:49:25 +0000204 return ok;
sewardjcbdddcf2005-03-10 23:23:45 +0000205}
206
njn2025cf92005-06-26 20:44:48 +0000207// Resolve a redir using any SegInfo if possible. This is called whenever
208// a new sym-to-addr redir is created. It covers the case where a
209// replacement function is loaded after its replacee.
njnd9109c62005-06-26 04:49:25 +0000210static Bool resolve_redir_with_existing_seginfos(CodeRedirect *redir)
njnbf7ca332005-05-14 17:11:06 +0000211{
212 const SegInfo *si;
213
njnd9109c62005-06-26 04:49:25 +0000214 for (si = VG_(next_seginfo)(NULL);
215 si != NULL;
216 si = VG_(next_seginfo)(si))
njnbf7ca332005-05-14 17:11:06 +0000217 {
njnd9109c62005-06-26 04:49:25 +0000218 if (resolve_redir_with_seginfo(redir, si))
njnbf7ca332005-05-14 17:11:06 +0000219 return True;
220 }
221 return False;
222}
223
njnd9109c62005-06-26 04:49:25 +0000224// Resolve as many unresolved redirs as possible with this SegInfo. This
njn2025cf92005-06-26 20:44:48 +0000225// should be called when a new SegInfo symtab is loaded. It covers the case
226// where a replacee function is loaded after its replacement function.
njnd9109c62005-06-26 04:49:25 +0000227void VG_(resolve_existing_redirs_with_seginfo)(SegInfo *si)
sewardjcbdddcf2005-03-10 23:23:45 +0000228{
njnd9109c62005-06-26 04:49:25 +0000229 CodeRedirect **prevp = &unresolved_redirs;
sewardjcbdddcf2005-03-10 23:23:45 +0000230 CodeRedirect *redir, *next;
231
njnd9109c62005-06-26 04:49:25 +0000232 TRACE_REDIR("Just loaded %s (soname=%s),",
233 VG_(seginfo_filename)(si), VG_(seginfo_soname)(si));
234 TRACE_REDIR(" resolving any unresolved redirs with it");
sewardjcbdddcf2005-03-10 23:23:45 +0000235
njnd9109c62005-06-26 04:49:25 +0000236 // Visit each unresolved redir - if it becomes resolved, then
237 // move it from the unresolved list to the resolved list.
238 for (redir = unresolved_redirs; redir != NULL; redir = next) {
sewardjcbdddcf2005-03-10 23:23:45 +0000239 next = redir->next;
240
njnd9109c62005-06-26 04:49:25 +0000241 if (resolve_redir_with_seginfo(redir, si)) {
sewardjcbdddcf2005-03-10 23:23:45 +0000242 *prevp = next;
243 redir->next = NULL;
sewardjf9a82d12005-07-23 09:52:21 +0000244 add_redir_to_resolved_list(redir, False);
sewardjcbdddcf2005-03-10 23:23:45 +0000245 } else
246 prevp = &redir->next;
247 }
sewardjcbdddcf2005-03-10 23:23:45 +0000248
njnd9109c62005-06-26 04:49:25 +0000249 TRACE_REDIR(" Finished resolving");
sewardjcbdddcf2005-03-10 23:23:45 +0000250}
251
tom748a1312005-04-02 15:53:01 +0000252/* Redirect a function at from_addr to a function at to_addr */
njn16eeb4e2005-06-16 03:56:58 +0000253__attribute__((unused)) // It is used, but not on all platforms...
njnd9109c62005-06-26 04:49:25 +0000254static void add_redirect_addr_to_addr( Addr from_addr, Addr to_addr )
tom748a1312005-04-02 15:53:01 +0000255{
njn8a96ec52005-10-15 15:48:52 +0000256 CodeRedirect* redir = VG_(OSet_AllocNode)(resolved_redirs,
257 sizeof(CodeRedirect));
njnd9109c62005-06-26 04:49:25 +0000258 vg_assert(0 != from_addr && 0 != to_addr);
259
260 redir->type = R_REDIRECT;
261
262 redir->from_lib = NULL;
263 redir->from_sym = NULL;
264 redir->from_addr = from_addr;
265
266 redir->to_addr = to_addr;
267 redir->wrapper = 0;
268
269 TRACE_REDIR("REDIRECT addr to addr: %p to %p", from_addr, to_addr);
270
271 // This redirection is already resolved, put it straight in the list.
sewardjf9a82d12005-07-23 09:52:21 +0000272 add_redir_to_resolved_list(redir, True);
njnd9109c62005-06-26 04:49:25 +0000273}
274
275/* Redirect a lib/symbol reference to a function at addr */
276static void add_redirect_sym_to_addr(
277 const Char *from_lib, const Char *from_sym, Addr to_addr
278)
279{
njn8a96ec52005-10-15 15:48:52 +0000280 CodeRedirect* redir = VG_(OSet_AllocNode)(resolved_redirs,
281 sizeof(CodeRedirect));
njnd9109c62005-06-26 04:49:25 +0000282 vg_assert(from_lib && from_sym && 0 != to_addr);
283
284 redir->type = R_REDIRECT;
285 redir->from_lib = VG_(arena_strdup)(VG_AR_SYMTAB, from_lib);
286 redir->from_sym = VG_(arena_strdup)(VG_AR_SYMTAB, from_sym);
287 redir->from_addr = 0;
288 redir->to_addr = to_addr;
289 redir->wrapper = 0;
290
291 TRACE_REDIR("REDIR sym to addr: %s:%s to %p", from_lib, from_sym, to_addr);
292
293 // Check against all existing segments to see if this redirection
njn2025cf92005-06-26 20:44:48 +0000294 // can be resolved immediately (as will be the case when the replacement
295 // function is loaded after the replacee). Then add it to the
296 // appropriate list.
njnd9109c62005-06-26 04:49:25 +0000297 if (resolve_redir_with_existing_seginfos(redir)) {
sewardjf9a82d12005-07-23 09:52:21 +0000298 add_redir_to_resolved_list(redir, True);
njnd9109c62005-06-26 04:49:25 +0000299 } else {
300 add_redir_to_unresolved_list(redir);
301 }
tom748a1312005-04-02 15:53:01 +0000302}
303
sewardjcbdddcf2005-03-10 23:23:45 +0000304CodeRedirect *VG_(add_wrapper)(const Char *from_lib, const Char *from_sym,
305 const FuncWrapper *wrapper)
306{
njn8a96ec52005-10-15 15:48:52 +0000307 CodeRedirect* redir = VG_(OSet_AllocNode)(resolved_redirs,
308 sizeof(CodeRedirect));
njnd9109c62005-06-26 04:49:25 +0000309 redir->type = R_WRAPPER;
njn136b83b2005-06-19 05:14:03 +0000310 redir->from_lib = VG_(arena_strdup)(VG_AR_SYMTAB, from_lib);
311 redir->from_sym = VG_(arena_strdup)(VG_AR_SYMTAB, from_sym);
sewardjcbdddcf2005-03-10 23:23:45 +0000312 redir->from_addr = 0;
njn136b83b2005-06-19 05:14:03 +0000313 redir->to_addr = 0;
314 redir->wrapper = wrapper;
sewardjcbdddcf2005-03-10 23:23:45 +0000315
njnd9109c62005-06-26 04:49:25 +0000316 TRACE_REDIR("REDIR sym to wrapper: %s:%s to (%p,%p)",
317 from_lib, from_sym, wrapper->before, wrapper->after);
318
319 // Check against all existing segments to see if this redirection
320 // can be resolved immediately. Then add it to the appropriate list.
321 if (resolve_redir_with_existing_seginfos(redir)) {
sewardjf9a82d12005-07-23 09:52:21 +0000322 add_redir_to_resolved_list(redir, True);
njnd9109c62005-06-26 04:49:25 +0000323 } else {
324 add_redir_to_unresolved_list(redir);
sewardjcbdddcf2005-03-10 23:23:45 +0000325 }
326
327 return redir;
328}
329
sewardjcbdddcf2005-03-10 23:23:45 +0000330/* If address 'a' is being redirected, return the redirected-to
331 address. */
332Addr VG_(code_redirect)(Addr a)
333{
njn8a96ec52005-10-15 15:48:52 +0000334 CodeRedirect* r = VG_(OSet_Lookup)(resolved_redirs, &a);
sewardjcbdddcf2005-03-10 23:23:45 +0000335 if (r == NULL)
336 return a;
337
338 vg_assert(r->to_addr != 0);
339
340 return r->to_addr;
341}
342
njn8a96ec52005-10-15 15:48:52 +0000343static void* symtab_alloc(SizeT n)
344{
345 return VG_(arena_malloc)(VG_AR_SYMTAB, n);
346}
347
348static void symtab_free(void* p)
349{
350 return VG_(arena_free)(VG_AR_SYMTAB, p);
351}
352
sewardjcbdddcf2005-03-10 23:23:45 +0000353void VG_(setup_code_redirect_table) ( void )
354{
njn8a96ec52005-10-15 15:48:52 +0000355 // Initialise resolved_redirs list.
356 resolved_redirs = VG_(OSet_Create)(offsetof(CodeRedirect, from_addr),
357 NULL, // Use fast comparison
358 symtab_alloc,
359 symtab_free);
360
njnd1af0032005-05-29 17:01:48 +0000361#if defined(VGP_x86_linux)
362 /* Redirect _dl_sysinfo_int80, which is glibc's default system call
sewardjb9bce632005-06-21 01:41:34 +0000363 routine, to our copy so that the special sysinfo unwind hack in
364 m_stacktrace.c will kick in. */
365 add_redirect_sym_to_addr(
366 "soname:ld-linux.so.2", "_dl_sysinfo_int80",
367 (Addr)&VG_(x86_linux_REDIR_FOR__dl_sysinfo_int80)
368 );
sewardj6a443b22005-11-20 19:37:54 +0000369 /* If we're using memcheck, use this intercept right from the
370 start, otherwise ld.so (glibc-2.3.5) makes a lot of noise. */
371 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
372 add_redirect_sym_to_addr(
373 "soname:ld-linux.so.2", "index",
374 (Addr)&VG_(x86_linux_REDIR_FOR_index)
375 );
376 }
sewardjb9bce632005-06-21 01:41:34 +0000377
njnd1af0032005-05-29 17:01:48 +0000378#elif defined(VGP_amd64_linux)
sewardjb9bce632005-06-21 01:41:34 +0000379
njnd1af0032005-05-29 17:01:48 +0000380 /* Redirect vsyscalls to local versions */
sewardjb9bce632005-06-21 01:41:34 +0000381 add_redirect_addr_to_addr(
382 0xFFFFFFFFFF600000ULL,
383 (Addr)&VG_(amd64_linux_REDIR_FOR_vgettimeofday)
384 );
sewardjb9bce632005-06-21 01:41:34 +0000385 add_redirect_addr_to_addr(
tomf5db3b62005-06-21 13:26:17 +0000386 0xFFFFFFFFFF600400ULL,
sewardjb9bce632005-06-21 01:41:34 +0000387 (Addr)&VG_(amd64_linux_REDIR_FOR_vtime)
388 );
389
cerion85665ca2005-06-20 15:51:07 +0000390#elif defined(VGP_ppc32_linux)
sewardjb9bce632005-06-21 01:41:34 +0000391
sewardj96fcfc82005-11-18 21:12:52 +0000392 /* If we're using memcheck, use these intercepts right from
393 the start, otherwise ld.so makes a lot of noise. */
394 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
395
396 add_redirect_sym_to_addr(
397 "soname:ld.so.1", "strlen",
398 (Addr)&VG_(ppc32_linux_REDIR_FOR_strlen)
399 );
400 add_redirect_sym_to_addr(
401 "soname:ld.so.1", "strcmp",
402 (Addr)&VG_(ppc32_linux_REDIR_FOR_strcmp)
403 );
404
405 }
sewardjb9bce632005-06-21 01:41:34 +0000406
sewardj2c48c7b2005-11-29 13:05:56 +0000407#elif defined(VGP_ppc64_linux)
408
409 // we'll have to stick some godawful hacks in here, no doubt
410
njnd1af0032005-05-29 17:01:48 +0000411#else
412# error Unknown platform
413#endif
414}
sewardjcbdddcf2005-03-10 23:23:45 +0000415
njn16eeb4e2005-06-16 03:56:58 +0000416/* Z-decode a symbol into library:func form, eg
417
418 _vgi_libcZdsoZd6__ZdlPv --> libc.so.6:_ZdlPv
419
420 Uses the Z-encoding scheme described in pub_core_redir.h.
421 Returns True if demangle OK, False otherwise.
422*/
423static Bool Z_decode(const Char* symbol, Char* result, Int nbytes)
424{
425# define EMIT(ch) \
426 do { \
427 if (j >= nbytes) \
428 result[j-1] = 0; \
429 else \
430 result[j++] = ch; \
431 } while (0)
432
433 Bool error = False;
434 Int i, j = 0;
435 Int len = VG_(strlen)(symbol);
436 if (0) VG_(printf)("idm: %s\n", symbol);
437
438 i = VG_REPLACE_FUNCTION_PREFIX_LEN;
439
440 /* Chew though the Z-encoded soname part. */
441 while (True) {
442
443 if (i >= len)
444 break;
445
446 if (symbol[i] == '_')
447 /* We found the underscore following the Z-encoded soname.
448 Just copy the rest literally. */
449 break;
450
451 if (symbol[i] != 'Z') {
452 EMIT(symbol[i]);
453 i++;
454 continue;
455 }
456
457 /* We've got a Z-escape. Act accordingly. */
458 i++;
459 if (i >= len) {
460 /* Hmm, Z right at the end. Something's wrong. */
461 error = True;
462 EMIT('Z');
463 break;
464 }
465 switch (symbol[i]) {
466 case 'a': EMIT('*'); break;
467 case 'p': EMIT('+'); break;
468 case 'c': EMIT(':'); break;
469 case 'd': EMIT('.'); break;
470 case 'u': EMIT('_'); break;
471 case 'h': EMIT('-'); break;
472 case 's': EMIT(' '); break;
473 case 'Z': EMIT('Z'); break;
474 default: error = True; EMIT('Z'); EMIT(symbol[i]); break;
475 }
476 i++;
477 }
478
479 if (error || i >= len || symbol[i] != '_') {
480 /* Something's wrong. Give up. */
481 VG_(message)(Vg_UserMsg, "intercept: error demangling: %s", symbol);
482 EMIT(0);
483 return False;
484 }
485
486 /* Copy the rest of the string verbatim. */
487 i++;
488 EMIT(':');
489 while (True) {
490 if (i >= len)
491 break;
492 EMIT(symbol[i]);
493 i++;
494 }
495
496 EMIT(0);
497 if (0) VG_(printf)("%s\n", result);
498 return True;
499
500# undef EMIT
501}
502
503// Nb: this can change the string pointed to by 'symbol'.
504static void handle_replacement_function( Char* symbol, Addr addr )
505{
506 Bool ok;
507 Int len = VG_(strlen)(symbol) + 1 - VG_REPLACE_FUNCTION_PREFIX_LEN;
508 Char *lib = VG_(arena_malloc)(VG_AR_SYMTAB, len+8);
509 Char *func;
510
511 // Put "soname:" at the start of lib
512 VG_(strcpy)(lib, "soname:");
513
514 ok = Z_decode(symbol, lib+7, len);
515 if (ok) {
516 // lib is "soname:<libname>:<fnname>". Split the string at the 2nd ':'.
517 func = lib + VG_(strlen)(lib)-1;
518 while(*func != ':') func--;
519 *func = '\0';
520 func++; // Move past the '\0'
521
522 // Now lib is "soname:<libname>" and func is "<fnname>".
523 if (0) VG_(printf)("lib A%sZ, func A%sZ\n", lib, func);
524 add_redirect_sym_to_addr(lib, func, addr);
525
526 // Overwrite the given Z-encoded name with just the fnname.
527 VG_(strcpy)(symbol, func);
528 }
529
530 VG_(arena_free)(VG_AR_SYMTAB, lib);
531}
532
njnbc6d84d2005-06-19 18:58:03 +0000533static Addr __libc_freeres_wrapper = 0;
534
535Addr VG_(get_libc_freeres_wrapper)(void)
536{
537 return __libc_freeres_wrapper;
538}
539
njn16eeb4e2005-06-16 03:56:58 +0000540// This is specifically for stringifying VG_(x) function names. We
541// need to do two macroexpansions to get the VG_ macro expanded before
542// stringifying.
543#define _STR(x) #x
544#define STR(x) _STR(x)
545
546static void handle_load_notifier( Char* symbol, Addr addr )
547{
548 if (VG_(strcmp)(symbol, STR(VG_NOTIFY_ON_LOAD(freeres))) == 0)
njnbc6d84d2005-06-19 18:58:03 +0000549 __libc_freeres_wrapper = addr;
njn16eeb4e2005-06-16 03:56:58 +0000550// else if (VG_(strcmp)(symbol, STR(VG_WRAPPER(pthread_startfunc_wrapper))) == 0)
551// VG_(pthread_startfunc_wrapper)((Addr)(si->offset + sym->st_value));
552 else
553 vg_assert2(0, "unrecognised load notification function: %s", symbol);
554}
555
556static Bool is_replacement_function(Char* s)
557{
558 return (0 == VG_(strncmp)(s,
559 VG_REPLACE_FUNCTION_PREFIX,
560 VG_REPLACE_FUNCTION_PREFIX_LEN));
561}
562
563static Bool is_load_notifier(Char* s)
564{
565 return (0 == VG_(strncmp)(s,
566 VG_NOTIFY_ON_LOAD_PREFIX,
567 VG_NOTIFY_ON_LOAD_PREFIX_LEN));
568}
569
570// Call this for each symbol loaded. It determines if we need to do
571// anything special with it. It can modify 'symbol' in-place.
572void VG_(maybe_redir_or_notify) ( Char* symbol, Addr addr )
573{
574 if (is_replacement_function(symbol))
575 handle_replacement_function(symbol, addr);
576 else
577 if (is_load_notifier(symbol))
578 handle_load_notifier(symbol, addr);
579}
580
581
sewardjcbdddcf2005-03-10 23:23:45 +0000582//:: /*------------------------------------------------------------*/
583//:: /*--- General function wrapping. ---*/
584//:: /*------------------------------------------------------------*/
585//::
586//:: /*
587//:: TODO:
588//:: - hook into the symtab machinery
589//:: - client-side wrappers?
590//:: - better interfaces for before() functions to get to arguments
591//:: - handle munmap of code (dlclose())
592//:: - handle thread exit
593//:: - handle longjmp
594//:: */
595//:: struct callkey {
596//:: ThreadId tid; /* calling thread */
597//:: Addr esp; /* address of args on stack */
598//:: Addr eip; /* return address */
599//:: };
600//::
601//:: struct call_instance {
602//:: struct callkey key;
603//::
604//:: const FuncWrapper *wrapper;
605//:: void *nonce;
606//:: };
607//::
608//:: static inline Addr addrcmp(Addr a, Addr b)
609//:: {
610//:: if (a < b)
611//:: return -1;
612//:: else if (a > b)
613//:: return 1;
614//:: else
615//:: return 0;
616//:: }
617//::
618//:: static inline Int cmp(UInt a, UInt b)
619//:: {
620//:: if (a < b)
621//:: return -1;
622//:: else if (a > b)
623//:: return 1;
624//:: else
625//:: return 0;
626//:: }
627//::
628//:: static Int keycmp(const void *pa, const void *pb)
629//:: {
630//:: const struct callkey *a = (const struct callkey *)pa;
631//:: const struct callkey *b = (const struct callkey *)pb;
632//:: Int ret;
633//::
634//:: if ((ret = cmp(a->tid, b->tid)))
635//:: return ret;
636//::
637//:: if ((ret = addrcmp(a->esp, b->esp)))
638//:: return ret;
639//::
640//:: return addrcmp(a->eip, b->eip);
641//:: }
642//::
643//:: /* List of wrapped call invocations which are currently active */
njnbe91aae2005-03-27 01:42:41 +0000644//:: static SkipList wrapped_frames = VG_SKIPLIST_INIT(struct call_instance, key, keycmp,
sewardjcbdddcf2005-03-10 23:23:45 +0000645//:: NULL, VG_AR_SYMTAB);
646//::
647//:: static struct call_instance *find_call(Addr retaddr, Addr argsp, ThreadId tid)
648//:: {
649//:: struct callkey key = { tid, argsp, retaddr };
650//::
651//:: return VG_(SkipList_Find_Exact)(&wrapped_frames, &key);
652//:: }
653//::
654//:: static void wrapper_return(Addr retaddr);
655//::
656//:: /* Called from generated code via helper */
657//:: void VG_(wrap_before)(ThreadState *tst, const FuncWrapper *wrapper)
658//:: {
njnaf839f52005-06-23 03:27:57 +0000659//:: Addr retaddr = VG_RETADDR(tst->arch);
660//:: Addr argp = (Addr)&VG_FUNC_ARG(tst->arch, 0);
sewardjcbdddcf2005-03-10 23:23:45 +0000661//:: void *nonce = NULL;
662//:: Bool mf = VG_(my_fault);
663//:: VG_(my_fault) = True;
664//::
665//:: if (wrapper->before) {
njnaf839f52005-06-23 03:27:57 +0000666//:: va_list args = VG_VA_LIST(tst->arch);
sewardjcbdddcf2005-03-10 23:23:45 +0000667//:: nonce = (*wrapper->before)(args);
668//:: }
669//::
670//:: if (wrapper->after) {
671//:: /* If there's an after function, make sure it gets called */
672//:: struct call_instance *call;
673//::
674//:: call = find_call(retaddr, argp, tst->tid);
675//::
676//:: if (call != NULL) {
677//:: /* Found a stale outstanding call; clean it up and recycle
678//:: the structure */
679//:: if (call->wrapper->after)
680//:: (*call->wrapper->after)(call->nonce, RT_LONGJMP, 0);
681//:: } else {
682//:: call = VG_(SkipNode_Alloc)(&wrapped_frames);
683//::
684//:: call->key.tid = tst->tid;
685//:: call->key.esp = argp;
686//:: call->key.eip = retaddr;
687//::
688//:: VG_(SkipList_Insert)(&wrapped_frames, call);
689//::
690//:: wrapper_return(retaddr);
691//:: }
692//::
693//:: call->wrapper = wrapper;
694//:: call->nonce = nonce;
695//:: } else
696//:: vg_assert(nonce == NULL);
697//::
698//:: VG_(my_fault) = mf;
699//:: }
700//::
701//:: /* Called from generated code via helper */
702//:: void VG_(wrap_after)(ThreadState *tst)
703//:: {
njnaf839f52005-06-23 03:27:57 +0000704//:: Addr EIP = VG_INSTR_PTR(tst->arch); /* instruction after call */
705//:: Addr ESP = VG_STACK_PTR(tst->arch); /* pointer to args */
706//:: Word ret = VG_RETVAL(tst->arch); /* return value */
sewardjcbdddcf2005-03-10 23:23:45 +0000707//:: struct call_instance *call;
708//:: Bool mf = VG_(my_fault);
709//::
710//:: VG_(my_fault) = True;
711//:: call = find_call(EIP, ESP, tst->tid);
712//::
713//:: if (0)
714//:: VG_(printf)("wrap_after(%p,%p,%d) -> %p\n", EIP, ESP, tst->tid, call);
715//::
716//:: if (call != NULL) {
717//:: if (call->wrapper->after)
718//:: (*call->wrapper->after)(call->nonce, RT_RETURN, ret);
719//::
720//:: VG_(SkipList_Remove)(&wrapped_frames, &call->key);
721//:: VG_(SkipNode_Free)(&wrapped_frames, call);
722//:: }
723//:: VG_(my_fault) = mf;
724//:: }
725//::
726//::
727//:: struct wrapped_function {
728//:: Addr eip; /* eip of function entrypoint */
729//:: const FuncWrapper *wrapper;
730//:: };
731//::
732//:: struct wrapper_return {
733//:: Addr eip; /* return address */
734//:: };
735//::
736//:: /* A mapping from eip of wrapped function entrypoints to actual wrappers */
njnbe91aae2005-03-27 01:42:41 +0000737//:: static SkipList wrapped_functions = VG_SKIPLIST_INIT(struct wrapped_function, eip, VG_(cmp_Addr),
sewardjcbdddcf2005-03-10 23:23:45 +0000738//:: NULL, VG_AR_SYMTAB);
739//::
740//:: /* A set of EIPs which are return addresses for wrapped functions */
njnbe91aae2005-03-27 01:42:41 +0000741//:: static SkipList wrapper_returns = VG_SKIPLIST_INIT(struct wrapper_return, eip, VG_(cmp_Addr),
sewardjcbdddcf2005-03-10 23:23:45 +0000742//:: NULL, VG_AR_SYMTAB);
743//::
744//:: /* Wrap function starting at eip */
745//:: void VG_(wrap_function)(Addr eip, const FuncWrapper *wrapper)
746//:: {
747//:: struct wrapped_function *func;
748//::
749//:: if (0)
750//:: VG_(printf)("wrapping %p with (%p,%p)\n", eip, wrapper->before, wrapper->after);
751//::
752//:: func = VG_(SkipList_Find_Exact)(&wrapped_functions, &eip);
753//::
754//:: if (func == NULL) {
755//:: func = VG_(SkipNode_Alloc)(&wrapped_functions);
756//:: VG_(invalidate_translations)(eip, 1, True);
757//::
758//:: func->eip = eip;
759//:: VG_(SkipList_Insert)(&wrapped_functions, func);
760//:: }
761//::
762//:: func->wrapper = wrapper;
763//:: }
764//::
765//:: const FuncWrapper *VG_(is_wrapped)(Addr eip)
766//:: {
767//:: struct wrapped_function *func = VG_(SkipList_Find_Exact)(&wrapped_functions, &eip);
768//::
769//:: if (func)
770//:: return func->wrapper;
771//:: return NULL;
772//:: }
773//::
774//:: Bool VG_(is_wrapper_return)(Addr eip)
775//:: {
776//:: struct wrapper_return *ret = VG_(SkipList_Find_Exact)(&wrapper_returns, &eip);
777//::
778//:: return ret != NULL;
779//:: }
780//::
781//:: /* Mark eip as being the return address of a wrapper, so that the
782//:: codegen will generate the appropriate call. */
783//:: void wrapper_return(Addr eip)
784//:: {
785//:: struct wrapper_return *ret;
786//::
787//:: if (VG_(is_wrapper_return)(eip))
788//:: return;
789//::
790//:: VG_(invalidate_translations)(eip, 1, True);
791//::
792//:: ret = VG_(SkipNode_Alloc)(&wrapper_returns);
793//:: ret->eip = eip;
794//::
795//:: VG_(SkipList_Insert)(&wrapper_returns, ret);
796//:: }
njnc0ae7052005-08-25 22:55:19 +0000797
798/*--------------------------------------------------------------------*/
799/*--- end ---*/
800/*--------------------------------------------------------------------*/