blob: 3ae8c6222ed0b88c6e5f2d0a3b82c1d6601d3f04 [file] [log] [blame]
sewardj024598e2008-09-18 14:43:05 +00001
2/*--------------------------------------------------------------------*/
3/*--- Ptrcheck: a pointer-use checker. pc_intercepts.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Ptrcheck, a Valgrind tool for checking pointer
8 use in programs.
9
10 Copyright (C) 2003-2008 Nicholas Nethercote
11 njn@valgrind.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/* Nothing actually in here. However it appears this file is needed
32 to make malloc intercepting work. (jrs, 2 july 08 -- not sure about
33 that).
34*/
35
36#include "pub_tool_basics.h"
37#include "pub_tool_hashtable.h"
38#include "pub_tool_redir.h"
39#include "pub_tool_tooliface.h"
40#include "valgrind.h"
41
42
43/* The following intercepts are copied verbatim from
sewardjdda0df82008-10-21 23:11:38 +000044 memcheck/mc_replace_strmem.c. If you copy more in, please keep
45 them in the same order as in mc_replace_strmem.c. */
sewardj024598e2008-09-18 14:43:05 +000046
47/* --------- Some handy Z-encoded names. --------- */
48
49/* --- Soname of the standard C library. --- */
50
51#if defined(VGO_linux)
52# define m_libc_soname libcZdsoZa // libc.so*
53#elif defined(VGP_ppc32_aix5)
54 /* AIX has both /usr/lib/libc.a and /usr/lib/libc_r.a. */
55# define m_libc_soname libcZaZdaZLshrZdoZR // libc*.a(shr.o)
56#elif defined(VGP_ppc64_aix5)
57# define m_libc_soname libcZaZdaZLshrZu64ZdoZR // libc*.a(shr_64.o)
58#else
59# error "Unknown platform"
60#endif
61
62/* --- Sonames for Linux ELF linkers. --- */
63
64#define m_ld_linux_so_2 ldZhlinuxZdsoZd2 // ld-linux.so.2
65#define m_ld_linux_x86_64_so_2 ldZhlinuxZhx86Zh64ZdsoZd2 // ld-linux-x86-64.so.2
66#define m_ld64_so_1 ld64ZdsoZd1 // ld64.so.1
67#define m_ld_so_1 ldZdsoZd1 // ld.so.1
68
69
70
71
sewardj7e98c032009-01-26 00:06:43 +000072#define STRNLEN(soname, fnname) \
73 SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* str, SizeT n ); \
74 SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* str, SizeT n ) \
75 { \
76 SizeT i = 0; \
77 while (i < n && str[i] != 0) i++; \
78 return i; \
79 }
80
81STRNLEN(m_libc_soname, strnlen)
82
83
sewardjdda0df82008-10-21 23:11:38 +000084// Note that this replacement often doesn't get used because gcc inlines
85// calls to strlen() with its own built-in version. This can be very
86// confusing if you aren't expecting it. Other small functions in this file
87// may also be inline by gcc.
88#define STRLEN(soname, fnname) \
89 SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* str ); \
90 SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* str ) \
91 { \
92 SizeT i = 0; \
93 while (str[i] != 0) i++; \
94 return i; \
95 }
96
97STRLEN(m_libc_soname, strlen)
98STRLEN(m_ld_linux_so_2, strlen)
99STRLEN(m_ld_linux_x86_64_so_2, strlen)
100STRLEN(m_ld_so_1, strlen)
101
102
sewardj024598e2008-09-18 14:43:05 +0000103#define STRCMP(soname, fnname) \
104 int VG_REPLACE_FUNCTION_ZU(soname,fnname) \
105 ( const char* s1, const char* s2 ); \
106 int VG_REPLACE_FUNCTION_ZU(soname,fnname) \
107 ( const char* s1, const char* s2 ) \
108 { \
109 register unsigned char c1; \
110 register unsigned char c2; \
111 while (True) { \
112 c1 = *(unsigned char *)s1; \
113 c2 = *(unsigned char *)s2; \
114 if (c1 != c2) break; \
115 if (c1 == 0) break; \
116 s1++; s2++; \
117 } \
118 if ((unsigned char)c1 < (unsigned char)c2) return -1; \
119 if ((unsigned char)c1 > (unsigned char)c2) return 1; \
120 return 0; \
121 }
122
123STRCMP(m_libc_soname, strcmp)
124STRCMP(m_ld_linux_x86_64_so_2, strcmp)
125STRCMP(m_ld64_so_1, strcmp)
126
127
sewardj024598e2008-09-18 14:43:05 +0000128#define MEMCPY(soname, fnname) \
129 void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \
130 ( void *dst, const void *src, SizeT sz ); \
131 void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \
132 ( void *dest, const void *src, SizeT sz ) \
133 { \
134 const UChar* s = (const UChar*)src; \
135 UChar* d = (UChar*)dest; \
136 const UWord* sW = (const UWord*)src; \
137 UWord* dW = (UWord*)dest; \
138 const UWord al = sizeof(UWord)-1; \
139 \
140 if (0 == (((UWord)dW) & al) && 0 == (((UWord)sW) & al)) { \
141 while (sz >= 4 * sizeof(UWord)) { \
142 dW[0] = sW[0]; \
143 dW[1] = sW[1]; \
144 dW[2] = sW[2]; \
145 dW[3] = sW[3]; \
146 sz -= 4 * sizeof(UWord); \
147 dW += 4; \
148 sW += 4; \
149 } \
150 if (sz == 0) \
151 return dest; \
152 while (sz >= 1 * sizeof(UWord)) { \
153 dW[0] = sW[0]; \
154 sz -= 1 * sizeof(UWord); \
155 dW += 1; \
156 sW += 1; \
157 } \
158 if (sz == 0) \
159 return dest; \
160 s = (const UChar*)sW; \
161 d = (UChar*)dW; \
162 } \
163 \
164 while (sz--) \
165 *d++ = *s++; \
166 \
167 return dest; \
168 }
169
170MEMCPY(m_libc_soname, memcpy)
171MEMCPY(m_ld_so_1, memcpy) /* ld.so.1 */
172MEMCPY(m_ld64_so_1, memcpy) /* ld64.so.1 */
173
174
sewardjdda0df82008-10-21 23:11:38 +0000175/* Copy SRC to DEST, returning the address of the terminating '\0' in
176 DEST. (minor variant of strcpy) */
177#define STPCPY(soname, fnname) \
178 char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ); \
179 char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ) \
180 { \
181 while (*src) *dst++ = *src++; \
182 *dst = 0; \
183 \
184 return dst; \
185 }
186
187STPCPY(m_libc_soname, stpcpy)
188STPCPY(m_ld_linux_so_2, stpcpy)
189STPCPY(m_ld_linux_x86_64_so_2, stpcpy)
190
191
sewardj024598e2008-09-18 14:43:05 +0000192/*--------------------------------------------------------------------*/
193/*--- end pc_intercepts.c ---*/
194/*--------------------------------------------------------------------*/