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 | |
njn | 9f20746 | 2009-03-10 22:02:09 +0000 | [diff] [blame^] | 10 | Copyright (C) 2000-2009 Julian Seward |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 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) |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 139 | @ --> ZA (at) |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 140 | Z --> ZZ (Z) |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 141 | ( --> ZL (left) |
| 142 | ) --> ZR (right) |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 143 | |
| 144 | Everything else is left unchanged. |
| 145 | */ |
| 146 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 147 | /* If you change these, the code in VG_(maybe_Z_demangle) needs to be |
| 148 | changed accordingly. NOTE: duplicates |
| 149 | I_{WRAP,REPLACE}_SONAME_FNNAME_Z{U,Z} in valgrind.h. */ |
| 150 | |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 151 | /* Use an extra level of macroisation so as to ensure the soname/fnname |
| 152 | args are fully macro-expanded before pasting them together. */ |
| 153 | #define VG_CONCAT4(_aa,_bb,_cc,_dd) _aa##_bb##_cc##_dd |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 154 | |
sewardj | 6b9cc87 | 2006-10-17 01:39:30 +0000 | [diff] [blame] | 155 | #define VG_REPLACE_FUNCTION_ZU(soname,fnname) VG_CONCAT4(_vgrZU_,soname,_,fnname) |
| 156 | #define VG_REPLACE_FUNCTION_ZZ(soname,fnname) VG_CONCAT4(_vgrZZ_,soname,_,fnname) |
| 157 | |
| 158 | #define VG_WRAP_FUNCTION_ZU(soname,fnname) VG_CONCAT4(_vgwZU_,soname,_,fnname) |
| 159 | #define VG_WRAP_FUNCTION_ZZ(soname,fnname) VG_CONCAT4(_vgwZZ_,soname,_,fnname) |
| 160 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 161 | /* --------- Some handy Z-encoded names. --------- */ |
| 162 | |
| 163 | /* --- Soname of the standard C library. --- */ |
| 164 | |
| 165 | #if defined(VGO_linux) |
| 166 | # define VG_Z_LIBC_SONAME libcZdsoZa // libc.so* |
| 167 | #elif defined(VGP_ppc32_aix5) |
| 168 | /* AIX has both /usr/lib/libc.a and /usr/lib/libc_r.a. */ |
| 169 | # define VG_Z_LIBC_SONAME libcZaZdaZLshrZdoZR // libc*.a(shr.o) |
| 170 | #elif defined(VGP_ppc64_aix5) |
| 171 | # define VG_Z_LIBC_SONAME libcZaZdaZLshrZu64ZdoZR // libc*.a(shr_64.o) |
| 172 | #else |
| 173 | # error "Unknown platform" |
| 174 | #endif |
| 175 | |
| 176 | /* --- Soname of the GNU C++ library. --- */ |
| 177 | |
| 178 | // DDD: this one and those below should probably be conditionally compiled, |
| 179 | // as should all the redirects in the tools that use them. |
| 180 | #define VG_Z_LIBSTDCXX_SONAME libstdcZpZpZa // libstdc++* |
| 181 | |
| 182 | /* --- Soname of XLC's C++ library. --- */ |
| 183 | |
| 184 | /* AIX: xlC's C++ runtime library is called libC.a, and the |
| 185 | interesting symbols appear to be in ansicore_32.o or ansicore_64.o |
| 186 | respectively. */ |
| 187 | #if defined(VGP_ppc32_aix5) |
| 188 | # define VG_Z_LIBC_DOT_A libCZdaZLansicoreZu32ZdoZR // libC.a(ansicore_32.o) |
| 189 | #elif defined(VGP_ppc64_aix5) |
| 190 | # define VG_Z_LIBC_DOT_A libCZdaZLansicoreZu64ZdoZR // libC.a(ansicore_64.o) |
| 191 | #endif |
| 192 | |
| 193 | /* --- Sonames for Linux ELF linkers. --- */ |
| 194 | |
| 195 | #define VG_Z_LD_LINUX_SO_2 ldZhlinuxZdsoZd2 // ld-linux.so.2 |
| 196 | #define VG_Z_LD_LINUX_X86_64_SO_2 ldZhlinuxZhx86Zh64ZdsoZd2 // ld-linux-x86-64.so.2 |
| 197 | #define VG_Z_LD64_SO_1 ld64ZdsoZd1 // ld64.so.1 |
| 198 | #define VG_Z_LD_SO_1 ldZdsoZd1 // ld.so.1 |
| 199 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 200 | |
njn | 4a164d0 | 2005-06-18 18:49:40 +0000 | [diff] [blame] | 201 | #endif // __PUB_TOOL_REDIR_H |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 202 | |
| 203 | /*--------------------------------------------------------------------*/ |
| 204 | /*--- end ---*/ |
| 205 | /*--------------------------------------------------------------------*/ |