blob: 76125dd866a3d714b1c024cd8108079350e1805f [file] [log] [blame]
njn16eeb4e2005-06-16 03:56:58 +00001
2/*--------------------------------------------------------------------*/
3/*--- Redirections, etc. pub_tool_redir.h ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2000-2005 Julian Seward
11 jseward@acm.org
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
31#ifndef __PUB_TOOL_REDIR
32#define __PUB_TOOL_REDIR
33
34/* The following macros facilitate function replacement, which is one form
35 of code replacement.
36
37 The general idea is: you can write a function like this:
38
39 ret_type VG_REPLACE_FUNCTION(zEncodedSoname, fnname) ( ... args ... )
40 {
41 ... body ...
42 }
43
44 zEncodedSoname should be a Z-encoded soname (see below for Z-encoding
45 details) and fnname should be an unencoded fn name. The resulting name is
46
47 _vgi_zEncodedSoname_fnname
48
49 The "_vgi_" is a prefix that gets discarded upon decoding.
50
51 When it sees this name, the core's symbol-table reading machinery
52 and redirection machinery will conspire to cause calls to the function
53 'fnname' in object with soname 'zEncodedSoname' to actually be routed to
54 the function written here. We use this below to define dozens of
55 replacements of malloc, free, etc.
56
57 The soname must be a Z-encoded bit of text because sonames can
58 contain dots etc which are not valid symbol names. But don't Z-encode
59 the function name, since it will already be a valid symbol name, and the
60 Z-encoding might screw up the C++ demangling.
61
62 Note that the soname can contain '*' as a wildcard meaning "match
63 anything".
64
65 Note also that the replacement function should probably (must be?) in
66 client space, so it runs on the simulated CPU. So it must be in
67 either vg_preload_<tool>.so or vg_preload_core.so.
68
69 It is important that the Z-encoded soname contains no unencoded
70 underscores, since the intercept-handlers in vg_symtab2.c detect
71 the end of the soname by looking for the first trailing underscore.
72
73 Z-encoding details: the scheme is like GHC's. It is just about
74 readable enough to make a preprocessor unnecessary. First the "_vgi_"
75 prefix is added, and then the following characters are transformed.
76
77 * --> Za ('a' for "asterisk")
78 + --> Zp
79 : --> Zc
80 . --> Zd
81 _ --> Zu
82 - --> Zh ('h' for "hyphen")
83 (space) --> Zs
84 Z --> ZZ
85
86 Everything else is left unchanged.
87*/
88
89#define VG_REPLACE_FUNCTION(soname, fnname) _vgi_##soname##_##fnname
90#define VG_REPLACE_FUNCTION_PREFIX "_vgi_"
91#define VG_REPLACE_FUNCTION_PREFIX_LEN 5
92
93#endif // __PUB_TOOL_REDIR
94
95/*--------------------------------------------------------------------*/
96/*--- end ---*/
97/*--------------------------------------------------------------------*/