A cleanup of the redirection stuff.

- Renamed VG_INTERCEPT as VG_REPLACE_FUNCTION to make its purpose
  clearer.

- Renamed VG_WRAPPER as VG_NOTIFY_ON_LOAD to make its purpose cleare.
  Started calling that stuff "load notification".

- Moved various things into m_redir.c, a much more sensible place for
  them.  This reduced the number of exported functions overall.  Renamed
  intercept_demangle() as Z_decode() as part of this.

- Improved the documentation of this stuff, especially in
  pub_core_redir.c.

- Got --run-libc-freeres=yes working again.  It was doing nothing.

- Renamed vg_inject.so as vg_preload_core.so to match
  vg_preload_<tool>.so

- Renamed vg_intercept.c as vg_preloaded.c.  (I kept the "vg_" prefix
  because this filename can appear in stack traces, so the "vg_" is a
  useful hint for users that it belongs to Valgrind.)

- Removed all the Memcheck-specific calls to add_redirect_sym_to_sym()
  from VG_(setup_redirect_table)(), instead using VG_REPLACE_FUNCTION in
  mac_replace_strmem.c, just like vg_replace_malloc.c.  This is the
  right way to do it.  This required moving some of
  coregrind/pub_core_redir.h into the newly added
  include/pub_tool_redir.h.  add_redirect_sym_to_sym() is no longer
  used...

- Now only handing off symbols to m_redir for inspection/decoding after
  they have been deemed to be interesting by the symbol table reader.

- Factored out commonality between the add_redirect_*_to_* functions
  into add_redirect_X_to_X().

- Added "Zh", meaning '-' ('h' for "hyphen"), to the Z-decoding scheme,
  to handle sonames like "ld-linux-x86-64.so.2".

- Added a FAQ explaining the newly found issue of glibc aliasing 
  sometimes causing the wrong function name to appear in stack traces.

- Added a new regtest strchr.c.  It's possible this will fail on some
  platforms.  If so, an alternative output file can be provided, but
  I'd like to see it in practice first.

It's possible that there will be minor breakage on other
platforms/setups, but it should be minimal and easily fixable.

Plus some ordinary cleanups in symtab.c:

- Removed the old optimisation from VG_(addStr)() whereby it kept track
  of the previous 5 added strings and avoiding duplicating any of them.
  Turns out it was barely having any effect any more, and just
  complicated things.

- Made read_symtab() more readable, by introducing a new variable
  "sym_name" and introducing the auxiliary function
  is_symbol_interesting().

- renamed the module variable 'segInfo' as 'segInfo_list' to make it
  more obvious it's a module variable and not just some ordinary local
  variable (which was an easy mistake to make).

-----------------------------------------------------------------------------

XXX: [later] remove add_redirect_sym_to_sym, and everything related to
     X_to_sym?  (ie. only need X_to_addr)

XXX: better function names?  all those 'resolved' names...
     [later...]




git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3916 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/include/pub_tool_redir.h b/include/pub_tool_redir.h
new file mode 100644
index 0000000..76125dd
--- /dev/null
+++ b/include/pub_tool_redir.h
@@ -0,0 +1,97 @@
+
+/*--------------------------------------------------------------------*/
+/*--- Redirections, etc.                          pub_tool_redir.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2005 Julian Seward
+      jseward@acm.org
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#ifndef __PUB_TOOL_REDIR
+#define __PUB_TOOL_REDIR
+
+/* The following macros facilitate function replacement, which is one form
+   of code replacement.
+
+   The general idea is: you can write a function like this:
+
+      ret_type VG_REPLACE_FUNCTION(zEncodedSoname, fnname) ( ... args ... )
+      {
+         ... body ...
+      }
+
+   zEncodedSoname should be a Z-encoded soname (see below for Z-encoding
+   details) and fnname should be an unencoded fn name.  The resulting name is
+
+      _vgi_zEncodedSoname_fnname
+
+   The "_vgi_" is a prefix that gets discarded upon decoding.
+   
+   When it sees this name, the core's symbol-table reading machinery
+   and redirection machinery will conspire to cause calls to the function
+   'fnname' in object with soname 'zEncodedSoname' to actually be routed to
+   the function written here.  We use this below to define dozens of
+   replacements of malloc, free, etc.
+
+   The soname must be a Z-encoded bit of text because sonames can
+   contain dots etc which are not valid symbol names.  But don't Z-encode
+   the function name, since it will already be a valid symbol name, and the
+   Z-encoding might screw up the C++ demangling.
+
+   Note that the soname can contain '*' as a wildcard meaning "match
+   anything".
+
+   Note also that the replacement function should probably (must be?) in
+   client space, so it runs on the simulated CPU.  So it must be in
+   either vg_preload_<tool>.so or vg_preload_core.so.
+   
+   It is important that the Z-encoded soname contains no unencoded 
+   underscores, since the intercept-handlers in vg_symtab2.c detect
+   the end of the soname by looking for the first trailing underscore.
+
+   Z-encoding details:  the scheme is like GHC's.  It is just about
+   readable enough to make a preprocessor unnecessary.  First the "_vgi_"
+   prefix is added, and then the following characters are transformed.
+
+     *         -->  Za    ('a' for "asterisk")
+     +         -->  Zp
+     :         -->  Zc
+     .         -->  Zd
+     _         -->  Zu
+     -         -->  Zh    ('h' for "hyphen")
+     (space)   -->  Zs
+     Z         -->  ZZ
+
+   Everything else is left unchanged.
+*/
+
+#define VG_REPLACE_FUNCTION(soname, fnname)  _vgi_##soname##_##fnname
+#define VG_REPLACE_FUNCTION_PREFIX           "_vgi_"
+#define VG_REPLACE_FUNCTION_PREFIX_LEN       5
+
+#endif   // __PUB_TOOL_REDIR
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/