njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1 | |
| 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 | |
njn | 4a164d0 | 2005-06-18 18:49:40 +0000 | [diff] [blame] | 31 | #ifndef __PUB_TOOL_REDIR_H |
| 32 | #define __PUB_TOOL_REDIR_H |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 33 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 34 | /* The following macros facilitate function replacement and wrapping. |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 35 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 36 | Function wrapping and function replacement are similar but not |
| 37 | identical. |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 38 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 39 | 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 | |
| 54 | Replacement |
| 55 | ~~~~~~~~~~~ |
| 56 | To write a replacement function, do this: |
| 57 | |
| 58 | ret_type |
| 59 | VG_REPLACE_FUNCTION_ZU(zEncodedSoname,fnname) ( .. args .. ) |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 60 | { |
| 61 | ... body ... |
| 62 | } |
| 63 | |
| 64 | zEncodedSoname should be a Z-encoded soname (see below for Z-encoding |
| 65 | details) and fnname should be an unencoded fn name. The resulting name is |
| 66 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 67 | _vgrZU_zEncodedSoname_fnname |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 68 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 69 | The "_vgrZU_" is a prefix that gets discarded upon decoding. |
| 70 | |
| 71 | It is also possible to write |
| 72 | |
| 73 | ret_type |
| 74 | VG_REPLACE_FUNCTION_ZZ(zEncodedSoname,zEncodedFnname) ( .. args .. ) |
| 75 | { |
| 76 | ... body ... |
| 77 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 78 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 79 | which means precisely the same, but the function name is also |
| 80 | Z-encoded. This can sometimes be necessary. In this case the |
| 81 | resulting function name is |
| 82 | |
| 83 | _vgrZZ_zEncodedSoname_zEncodedFnname |
| 84 | |
| 85 | When it sees this either such name, the core's symbol-table reading |
| 86 | machinery and redirection machinery first Z-decode the soname and |
| 87 | if necessary the fnname. They are encoded so that they may include |
| 88 | arbitrary characters, and in particular they may contain '*', which |
| 89 | acts as a wildcard. |
| 90 | |
| 91 | They then will conspire to cause calls to any function matching |
| 92 | 'fnname' in any object whose soname matches 'soname' to actually be |
| 93 | routed to this function. This is used in Valgrind to define dozens |
| 94 | of replacements of malloc, free, etc. |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 95 | |
| 96 | The soname must be a Z-encoded bit of text because sonames can |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 97 | contain dots etc which are not valid symbol names. The function |
| 98 | name may or may not be Z-encoded: to include wildcards it has to be, |
| 99 | but Z-encoding C++ function names which are themselves already mangled |
| 100 | using Zs in some way is tedious and error prone, so the _ZU variant |
| 101 | allows them not to be Z-encoded. |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 102 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 103 | Note that the soname "NONE" is specially interpreted to match any |
| 104 | shared object which doesn't have a soname. |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 105 | |
| 106 | Note also that the replacement function should probably (must be?) in |
| 107 | client space, so it runs on the simulated CPU. So it must be in |
njn | 7b4e5ba | 2005-08-25 22:53:57 +0000 | [diff] [blame] | 108 | either vgpreload_<tool>.so or vgpreload_core.so. It also only works |
| 109 | with functions in shared objects, I think. |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 110 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 111 | It is important that the Z-encoded names contain no unencoded |
| 112 | underscores, since the intercept-handlers in m_redir.c detect the |
| 113 | end of the soname by looking for the first trailing underscore. |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 114 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 115 | Wrapping |
| 116 | ~~~~~~~~ |
| 117 | This is identical to replacement, except that you should use the |
| 118 | macro names |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 119 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 120 | VG_WRAP_FUNCTION_ZU |
| 121 | VG_WRAP_FUNCTION_ZZ |
| 122 | |
| 123 | instead. |
| 124 | |
| 125 | Z-encoding |
| 126 | ~~~~~~~~~~ |
| 127 | Z-encoding details: the scheme is like GHC's. It is just about |
| 128 | readable enough to make a preprocessor unnecessary. First the |
| 129 | "_vgrZU_" or "_vgrZZ_" prefix is added, and then the following |
| 130 | characters are transformed. |
| 131 | |
| 132 | * --> Za (asterisk) |
| 133 | + --> Zp (plus) |
| 134 | : --> Zc (colon) |
| 135 | . --> Zd (dot) |
| 136 | _ --> Zu (underscore) |
| 137 | - --> Zh (hyphen) |
| 138 | (space) --> Zs (space) |
| 139 | @ -> ZA (at) |
| 140 | Z --> ZZ (Z) |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 141 | |
| 142 | Everything else is left unchanged. |
| 143 | */ |
| 144 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame^] | 145 | /* If you change these, the code in VG_(maybe_Z_demangle) needs to be |
| 146 | changed accordingly. NOTE: duplicates |
| 147 | I_{WRAP,REPLACE}_SONAME_FNNAME_Z{U,Z} in valgrind.h. */ |
| 148 | |
| 149 | #define VG_REPLACE_FUNCTION_ZU(soname,fnname) _vgrZU_##soname##_##fnname |
| 150 | #define VG_REPLACE_FUNCTION_ZZ(soname,fnname) _vgrZZ_##soname##_##fnname |
| 151 | |
| 152 | #define VG_WRAP_FUNCTION_ZU(soname,fnname) _vgwZU_##soname##_##fnname |
| 153 | #define VG_WRAP_FUNCTION_ZZ(soname,fnname) _vgwZZ_##soname##_##fnname |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 154 | |
njn | 4a164d0 | 2005-06-18 18:49:40 +0000 | [diff] [blame] | 155 | #endif // __PUB_TOOL_REDIR_H |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 156 | |
| 157 | /*--------------------------------------------------------------------*/ |
| 158 | /*--- end ---*/ |
| 159 | /*--------------------------------------------------------------------*/ |