blob: 4ca83583501b00c2f9759e9a6116ada46db68ef0 [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
sewardj9eecbbb2010-05-03 21:37:12 +000010 Copyright (C) 2000-2010 Julian Seward
njn16eeb4e2005-06-16 03:56:58 +000011 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
njn4a164d02005-06-18 18:49:40 +000031#ifndef __PUB_TOOL_REDIR_H
32#define __PUB_TOOL_REDIR_H
njn16eeb4e2005-06-16 03:56:58 +000033
sewardj0ec07f32006-01-12 12:32:32 +000034/* The following macros facilitate function replacement and wrapping.
njn16eeb4e2005-06-16 03:56:58 +000035
sewardj0ec07f32006-01-12 12:32:32 +000036 Function wrapping and function replacement are similar but not
37 identical.
njn16eeb4e2005-06-16 03:56:58 +000038
sewardj0ec07f32006-01-12 12:32:32 +000039 A replacement for some function F simply diverts all calls to F
40 to the stated replacement. There is no way to get back to F itself
41 from the replacement.
42
43 A wrapper for a function F causes all calls to F to instead go to
44 the wrapper. However, from inside the wrapper, it is possible
45 (with some difficulty) to get to F itself.
46
47 You may notice that replacement is a special case of wrapping, in
48 which the call to the original is omitted. For implementation
49 reasons, though, it is important to use the following macros
50 correctly: in particular, if you want to write a replacement, make
51 sure you use the VG_REPLACE_FN_ macros and not the VG_WRAP_FN_
52 macros.
53
sewardj96044842011-08-18 13:09:55 +000054 Finally there is the concept of prioritised behavioural equivalence
55 tags. A tag is a 5-digit decimal number (00000 to 99999) encoded
56 in the name. The top 4 digits are the equivalence class number,
57 and the last digit is a priority.
sewardj85cf9002011-08-16 09:54:00 +000058
sewardj96044842011-08-18 13:09:55 +000059 When processing redirections at library load time, if the set of
60 available specifications yields more than one replacement or
61 wrapper function for a given address, the system will try to
62 resolve the situation by examining the tags on the
63 replacements/wrappers.
64
65 If two replacement/wrapper functions have the same tag and
66 priority, then the redirection machinery will assume they have
67 identical behaviour and can choose between them arbitrarily. If
68 they have the same tag but different priorities, then the one with
69 higher priority will be chosen. If neither case holds, then the
70 redirection is ambiguous and the system will ignore one of them
71 arbitrarily, but print a warning when running at -v or above.
72
73 The tag is mandatory and must comprise 5 decimal digits. The tag
74 00000 is special and means "does not have behaviour identical to any
sewardj85cf9002011-08-16 09:54:00 +000075 other replacement/wrapper function". Hence if you wish to write a
76 wrap/replacement function that is not subject to the above
sewardj96044842011-08-18 13:09:55 +000077 resolution rules, use 00000 for the tag. Tags 00001 through 00009
78 may not be used for any purpose.
sewardj85cf9002011-08-16 09:54:00 +000079
80
sewardj0ec07f32006-01-12 12:32:32 +000081 Replacement
82 ~~~~~~~~~~~
83 To write a replacement function, do this:
84
85 ret_type
86 VG_REPLACE_FUNCTION_ZU(zEncodedSoname,fnname) ( .. args .. )
njn16eeb4e2005-06-16 03:56:58 +000087 {
88 ... body ...
89 }
90
sewardj85cf9002011-08-16 09:54:00 +000091 zEncodedSoname should be a Z-encoded soname (see below for
92 Z-encoding details) and fnname should be an unencoded fn name. A
sewardj96044842011-08-18 13:09:55 +000093 default-safe equivalence tag of 00000 is assumed (see comments
sewardj85cf9002011-08-16 09:54:00 +000094 above). The resulting name is
njn16eeb4e2005-06-16 03:56:58 +000095
sewardj96044842011-08-18 13:09:55 +000096 _vgr00000ZU_zEncodedSoname_fnname
njn16eeb4e2005-06-16 03:56:58 +000097
sewardj96044842011-08-18 13:09:55 +000098 The "_vgr00000ZU_" is a prefix that gets discarded upon decoding.
sewardj85cf9002011-08-16 09:54:00 +000099 It identifies this function as a replacement and specifies its
100 equivalence tag.
sewardj0ec07f32006-01-12 12:32:32 +0000101
102 It is also possible to write
103
104 ret_type
105 VG_REPLACE_FUNCTION_ZZ(zEncodedSoname,zEncodedFnname) ( .. args .. )
106 {
107 ... body ...
108 }
njn16eeb4e2005-06-16 03:56:58 +0000109
sewardj0ec07f32006-01-12 12:32:32 +0000110 which means precisely the same, but the function name is also
111 Z-encoded. This can sometimes be necessary. In this case the
112 resulting function name is
113
sewardj96044842011-08-18 13:09:55 +0000114 _vgr00000ZZ_zEncodedSoname_zEncodedFnname
sewardj0ec07f32006-01-12 12:32:32 +0000115
116 When it sees this either such name, the core's symbol-table reading
117 machinery and redirection machinery first Z-decode the soname and
118 if necessary the fnname. They are encoded so that they may include
119 arbitrary characters, and in particular they may contain '*', which
120 acts as a wildcard.
121
122 They then will conspire to cause calls to any function matching
123 'fnname' in any object whose soname matches 'soname' to actually be
124 routed to this function. This is used in Valgrind to define dozens
125 of replacements of malloc, free, etc.
njn16eeb4e2005-06-16 03:56:58 +0000126
127 The soname must be a Z-encoded bit of text because sonames can
sewardj0ec07f32006-01-12 12:32:32 +0000128 contain dots etc which are not valid symbol names. The function
129 name may or may not be Z-encoded: to include wildcards it has to be,
130 but Z-encoding C++ function names which are themselves already mangled
131 using Zs in some way is tedious and error prone, so the _ZU variant
132 allows them not to be Z-encoded.
njn16eeb4e2005-06-16 03:56:58 +0000133
sewardj0ec07f32006-01-12 12:32:32 +0000134 Note that the soname "NONE" is specially interpreted to match any
135 shared object which doesn't have a soname.
njn16eeb4e2005-06-16 03:56:58 +0000136
137 Note also that the replacement function should probably (must be?) in
138 client space, so it runs on the simulated CPU. So it must be in
njn7b4e5ba2005-08-25 22:53:57 +0000139 either vgpreload_<tool>.so or vgpreload_core.so. It also only works
140 with functions in shared objects, I think.
njn16eeb4e2005-06-16 03:56:58 +0000141
sewardj0ec07f32006-01-12 12:32:32 +0000142 It is important that the Z-encoded names contain no unencoded
143 underscores, since the intercept-handlers in m_redir.c detect the
144 end of the soname by looking for the first trailing underscore.
njn16eeb4e2005-06-16 03:56:58 +0000145
sewardj85cf9002011-08-16 09:54:00 +0000146 To write function names which explicitly state the equivalence class
147 tag, use
sewardj96044842011-08-18 13:09:55 +0000148 VG_REPLACE_FUNCTION_EZU(5-digit-tag,zEncodedSoname,fnname)
sewardj85cf9002011-08-16 09:54:00 +0000149 or
sewardj96044842011-08-18 13:09:55 +0000150 VG_REPLACE_FUNCTION_EZZ(5-digit-tag,zEncodedSoname,zEncodedFnname)
sewardj85cf9002011-08-16 09:54:00 +0000151
sewardj96044842011-08-18 13:09:55 +0000152 As per comments above, the tag must be a 5 digit decimal number,
153 padded with leading zeroes, in the range 00010 to 99999 inclusive.
sewardj85cf9002011-08-16 09:54:00 +0000154
155
sewardj0ec07f32006-01-12 12:32:32 +0000156 Wrapping
157 ~~~~~~~~
158 This is identical to replacement, except that you should use the
159 macro names
njn16eeb4e2005-06-16 03:56:58 +0000160
sewardj0ec07f32006-01-12 12:32:32 +0000161 VG_WRAP_FUNCTION_ZU
162 VG_WRAP_FUNCTION_ZZ
sewardj85cf9002011-08-16 09:54:00 +0000163 VG_WRAP_FUNCTION_EZU
164 VG_WRAP_FUNCTION_EZZ
sewardj0ec07f32006-01-12 12:32:32 +0000165
166 instead.
167
168 Z-encoding
169 ~~~~~~~~~~
170 Z-encoding details: the scheme is like GHC's. It is just about
171 readable enough to make a preprocessor unnecessary. First the
172 "_vgrZU_" or "_vgrZZ_" prefix is added, and then the following
173 characters are transformed.
174
175 * --> Za (asterisk)
sewardj0ec07f32006-01-12 12:32:32 +0000176 : --> Zc (colon)
177 . --> Zd (dot)
sewardj0ec07f32006-01-12 12:32:32 +0000178 - --> Zh (hyphen)
sewardj578b1712009-07-26 19:41:07 +0000179 + --> Zp (plus)
sewardj0ec07f32006-01-12 12:32:32 +0000180 (space) --> Zs (space)
sewardj578b1712009-07-26 19:41:07 +0000181 _ --> Zu (underscore)
sewardj6b9cc872006-10-17 01:39:30 +0000182 @ --> ZA (at)
sewardj578b1712009-07-26 19:41:07 +0000183 $ --> ZD (dollar)
sewardj6b9cc872006-10-17 01:39:30 +0000184 ( --> ZL (left)
185 ) --> ZR (right)
sewardj578b1712009-07-26 19:41:07 +0000186 Z --> ZZ (Z)
njn16eeb4e2005-06-16 03:56:58 +0000187
188 Everything else is left unchanged.
189*/
190
sewardj0ec07f32006-01-12 12:32:32 +0000191/* If you change these, the code in VG_(maybe_Z_demangle) needs to be
192 changed accordingly. NOTE: duplicates
193 I_{WRAP,REPLACE}_SONAME_FNNAME_Z{U,Z} in valgrind.h. */
194
sewardj6b9cc872006-10-17 01:39:30 +0000195/* Use an extra level of macroisation so as to ensure the soname/fnname
196 args are fully macro-expanded before pasting them together. */
197#define VG_CONCAT4(_aa,_bb,_cc,_dd) _aa##_bb##_cc##_dd
sewardj0ec07f32006-01-12 12:32:32 +0000198
sewardj85cf9002011-08-16 09:54:00 +0000199#define VG_CONCAT6(_aa,_bb,_cc,_dd,_ee,_ff) _aa##_bb##_cc##_dd##_ee##_ff
sewardj6b9cc872006-10-17 01:39:30 +0000200
sewardj85cf9002011-08-16 09:54:00 +0000201/* The 4 basic macros. */
202#define VG_REPLACE_FUNCTION_EZU(_eclasstag,_soname,_fnname) \
203 VG_CONCAT6(_vgr,_eclasstag,ZU_,_soname,_,_fnname)
204
205#define VG_REPLACE_FUNCTION_EZZ(_eclasstag,_soname,_fnname) \
206 VG_CONCAT6(_vgr,_eclasstag,ZZ_,_soname,_,_fnname)
207
208#define VG_WRAP_FUNCTION_EZU(_eclasstag,_soname,_fnname) \
209 VG_CONCAT6(_vgw,_eclasstag,ZU_,_soname,_,_fnname)
210
211#define VG_WRAP_FUNCTION_EZZ(_eclasstag,_soname,_fnname) \
212 VG_CONCAT6(_vgw,_eclasstag,ZZ_,_soname,_,_fnname)
213
214/* Convenience macros defined in terms of the above 4. */
215#define VG_REPLACE_FUNCTION_ZU(_soname,_fnname) \
sewardj96044842011-08-18 13:09:55 +0000216 VG_CONCAT6(_vgr,00000,ZU_,_soname,_,_fnname)
sewardj85cf9002011-08-16 09:54:00 +0000217
218#define VG_REPLACE_FUNCTION_ZZ(_soname,_fnname) \
sewardj96044842011-08-18 13:09:55 +0000219 VG_CONCAT6(_vgr,00000,ZZ_,_soname,_,_fnname)
sewardj85cf9002011-08-16 09:54:00 +0000220
221#define VG_WRAP_FUNCTION_ZU(_soname,_fnname) \
sewardj96044842011-08-18 13:09:55 +0000222 VG_CONCAT6(_vgw,00000,ZU_,_soname,_,_fnname)
sewardj85cf9002011-08-16 09:54:00 +0000223
224#define VG_WRAP_FUNCTION_ZZ(_soname,_fnname) \
sewardj96044842011-08-18 13:09:55 +0000225 VG_CONCAT6(_vgw,00000,ZZ_,_soname,_,_fnname)
sewardj85cf9002011-08-16 09:54:00 +0000226
sewardj6b9cc872006-10-17 01:39:30 +0000227
njne6154662009-02-10 04:23:41 +0000228/* --------- Some handy Z-encoded names. --------- */
229
njnb4cfbc42009-05-04 04:20:02 +0000230// Nb: ALL THESE NAMES MUST BEGIN WITH "VG_Z_". Why? If we applied
231// conditional compilation inconsistently we could accidentally use an
232// undefined constant like VG_Z_LIBC_DOT_A, resulting in a bogus Z-encoded
233// name like "_vgrZU_VG_Z_LIBC_DOT_A_foo". This can't be detected at
234// compile-time, because both the constant's name and its value are
235// identifiers. However, by always using "VG_Z_" as a prefix, we can do a
236// run-time check and abort if any name has "VG_Z_" in it, because that
237// indicates that the constant has been used without being defined.
238
njne6154662009-02-10 04:23:41 +0000239/* --- Soname of the standard C library. --- */
240
241#if defined(VGO_linux)
242# define VG_Z_LIBC_SONAME libcZdsoZa // libc.so*
njnf76d27a2009-05-28 01:53:07 +0000243#elif defined(VGO_darwin)
244# define VG_Z_LIBC_SONAME libSystemZdZaZddylib // libSystem.*.dylib
njne6154662009-02-10 04:23:41 +0000245#else
246# error "Unknown platform"
247#endif
248
249/* --- Soname of the GNU C++ library. --- */
250
njnb4cfbc42009-05-04 04:20:02 +0000251// Valid on all platforms(?)
njne6154662009-02-10 04:23:41 +0000252#define VG_Z_LIBSTDCXX_SONAME libstdcZpZpZa // libstdc++*
253
njn5f5ef2a2009-05-11 08:01:09 +0000254/* --- Soname of the pthreads library. --- */
255
sewardj6e9de462011-06-28 07:25:29 +0000256#if defined(VGO_linux)
njn5f5ef2a2009-05-11 08:01:09 +0000257# define VG_Z_LIBPTHREAD_SONAME libpthreadZdsoZd0 // libpthread.so.0
njnf76d27a2009-05-28 01:53:07 +0000258#elif defined(VGO_darwin)
259# define VG_Z_LIBPTHREAD_SONAME libSystemZdZaZddylib // libSystem.*.dylib
njn5f5ef2a2009-05-11 08:01:09 +0000260#else
261# error "Unknown platform"
262#endif
263
sewardja0eee322009-07-31 08:46:35 +0000264/* --- Sonames for Linux ELF linkers, plus unencoded versions. --- */
njne6154662009-02-10 04:23:41 +0000265
njnb4cfbc42009-05-04 04:20:02 +0000266#if defined(VGO_linux)
sewardja0eee322009-07-31 08:46:35 +0000267
sewardj651cfa42010-01-11 13:02:19 +0000268#define VG_Z_LD_LINUX_SO_3 ldZhlinuxZdsoZd3 // ld-linux.so.3
269#define VG_U_LD_LINUX_SO_3 "ld-linux.so.3"
270
njne6154662009-02-10 04:23:41 +0000271#define VG_Z_LD_LINUX_SO_2 ldZhlinuxZdsoZd2 // ld-linux.so.2
sewardja0eee322009-07-31 08:46:35 +0000272#define VG_U_LD_LINUX_SO_2 "ld-linux.so.2"
273
njne6154662009-02-10 04:23:41 +0000274#define VG_Z_LD_LINUX_X86_64_SO_2 ldZhlinuxZhx86Zh64ZdsoZd2 // ld-linux-x86-64.so.2
sewardja0eee322009-07-31 08:46:35 +0000275#define VG_U_LD_LINUX_X86_64_SO_2 "ld-linux-x86-64.so.2"
276
njne6154662009-02-10 04:23:41 +0000277#define VG_Z_LD64_SO_1 ld64ZdsoZd1 // ld64.so.1
sewardja0eee322009-07-31 08:46:35 +0000278#define VG_U_LD64_SO_1 "ld64.so.1"
279
njne6154662009-02-10 04:23:41 +0000280#define VG_Z_LD_SO_1 ldZdsoZd1 // ld.so.1
sewardja0eee322009-07-31 08:46:35 +0000281#define VG_U_LD_SO_1 "ld.so.1"
282
njnb4cfbc42009-05-04 04:20:02 +0000283#endif
njn16eeb4e2005-06-16 03:56:58 +0000284
njnf76d27a2009-05-28 01:53:07 +0000285/* --- Executable name for Darwin Mach-O linker. --- */
286
287#if defined(VGO_darwin)
sewardja0eee322009-07-31 08:46:35 +0000288
njnf76d27a2009-05-28 01:53:07 +0000289#define VG_Z_DYLD dyld // dyld
sewardja0eee322009-07-31 08:46:35 +0000290#define VG_U_DYLD "dyld"
291
njnf76d27a2009-05-28 01:53:07 +0000292#endif
293
294
njn4a164d02005-06-18 18:49:40 +0000295#endif // __PUB_TOOL_REDIR_H
njn16eeb4e2005-06-16 03:56:58 +0000296
297/*--------------------------------------------------------------------*/
298/*--- end ---*/
299/*--------------------------------------------------------------------*/