[PATCH 07/20] Use gcc's __asm__ extension to rename symbols

Richard Henderson rth at twiddle.net
Mon Mar 4 19:41:37 CET 2013


When using gcc, we can alter the source/link symbol mapping.
This flows all the way through to the dwarf2 output, e.g.

: Abbrev Number: 13 (DW_TAG_subprogram)
   DW_AT_external    : 1
   DW_AT_name        : mpn_get_d
   DW_AT_decl_file   : 1
   DW_AT_decl_line   : 122
   DW_AT_linkage_name: __gmpn_get_d

so the debugger can know the linkage symbol at which to install
a breakpoint given a source name.
---
 gmp-h.in   | 34 +++++++++++++++++++++++-----------
 gmp-impl.h | 22 +++++++++++++++-------
 2 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/gmp-h.in b/gmp-h.in
index 157b3d8..1b27998 100644
--- a/gmp-h.in
+++ b/gmp-h.in
@@ -250,16 +250,31 @@ typedef __mpq_struct *mpq_ptr;
      __GMP_PUBLIC_NT_A	- for "no-throw" functions with attributes
      __GMP_PUBLIC_DATA	- for declaring data variables
      __GMP_PUBLIC_ALIAS	- for re-declaring symbols with another name
-*/
+
+   When using GCC, we'll use __asm__ to give the ABI symbol the linkage
+   name corresponding to the ABI name.  Similarly to define aliases.
+
+   When not using GCC, we will have built <gmp-renames.h> to perform
+   the renaming at the preprocessor level.  */
+
+#define __GMP_STR1(X)     #X
+#define __GMP_STR(X)      __GMP_STR1(X)
+#define __GMP_GLUE1(X, Y)  X ## Y
+#define __GMP_GLUE(X, Y)  __GMP_GLUE1(X, Y)
+
 #if !defined(__GMP_WITHIN_CONFIGURE) || !defined(__GMP_PUBLIC_FULL)
-#define __GMP_PUBLIC_FULL(TYPE, API_NAME, ARGS, NT, ATTRS, ABI_NAME) \
-  __GMP_DECLSPEC TYPE API_NAME ARGS NT ATTRS
-#define __GMP_PUBLIC_ALIAS_FULL(NEW_NAME, OLD_NAME, ABI_NAME)
+# ifdef __GNUC__
+#  define __GMP_PUBLIC_FULL(TYPE, API_NAME, ARGS, NT, ATTRS, ABI_NAME) \
+    __GMP_DECLSPEC TYPE API_NAME ARGS NT __asm__(__GMP_STR(ABI_NAME)) ATTRS
+#  define __GMP_PUBLIC_ALIAS_FULL(NEW_NAME, OLD_NAME, ABI_NAME) \
+    __typeof(OLD_NAME) NEW_NAME __asm__(__GMP_STR(ABI_NAME));
+# else
+#  define __GMP_PUBLIC_FULL(TYPE, API_NAME, ARGS, NT, ATTRS, ABI_NAME) \
+    __GMP_DECLSPEC TYPE API_NAME ARGS NT ATTRS
+#  define __GMP_PUBLIC_ALIAS_FULL(NEW_NAME, OLD_NAME, ABI_NAME)
+# endif
 #endif
 
-#define __GMP_GLU1(X, Y)  X ## Y
-#define __GMP_GLUE(X, Y)  __GMP_GLU1(X, Y)
-
 #define __GMP_PUBLIC_NT_A(TYPE, API_NAME, ARGS, ATTRS)			\
   __GMP_PUBLIC_FULL(TYPE, API_NAME, ARGS, __GMP_NOTHROW, ATTRS,		\
 		    __GMP_GLUE(__GMP_ABI_PREFIX, API_NAME))
@@ -279,10 +294,7 @@ typedef __mpq_struct *mpq_ptr;
   __GMP_PUBLIC_ALIAS_FULL(NEW_NAME, OLD_NAME,				\
 			  __GMP_GLUE(__GMP_ABI_PREFIX, OLD_NAME))
 
-/* When using GCC, we'll adjust the API symbol to point to the ABI symbol
-   automatically.  Otherwise, we'll have generated and installed a set of
-   defines that will perform the remapping at the preprocessor level.  */
-#if !defined(__GMP_WITHIN_CONFIGURE)
+#if !defined(__GMP_WITHIN_CONFIGURE) && !defined(__GNUC__)
 # include <gmp-rename.h>
 #endif
 
diff --git a/gmp-impl.h b/gmp-impl.h
index fda8bca..7ebfe4d 100644
--- a/gmp-impl.h
+++ b/gmp-impl.h
@@ -63,10 +63,21 @@ along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
      __GMP_INTERN_NT	- for "no-throw" functions
      __GMP_INTERN_NT_A	- for "no-throw" functions with attributes
      __GMP_INTERN_DATA	- for declaring data variables
-*/
+
+   When using GCC, we'll use __asm__ to give the ABI symbol the linkage
+   name corresponding to the ABI name.  Similarly to define aliases.
+
+   When not using GCC, we will have built "gmp-renamesi.h" to perform
+   the renaming at the preprocessor level.  */
+
 #if !defined(__GMP_WITHIN_CONFIGURE) || !defined(__GMP_INTERN_FULL)
-#define __GMP_INTERN_FULL(TYPE, API_NAME, ARGS, NT, ATTRS, ABI_NAME) \
-  __GMP_DECLSPEC TYPE API_NAME ARGS NT ATTRS
+# ifdef __GNUC__
+#  define __GMP_INTERN_FULL(TYPE, API_NAME, ARGS, NT, ATTRS, ABI_NAME) \
+    __GMP_DECLSPEC TYPE API_NAME ARGS NT __asm__(__GMP_STR(ABI_NAME)) ATTRS
+# else
+#  define __GMP_INTERN_FULL(TYPE, API_NAME, ARGS, NT, ATTRS, ABI_NAME) \
+    __GMP_DECLSPEC TYPE API_NAME ARGS NT ATTRS
+# endif
 #endif
 
 #define __GMP_INTERN_NT_A(TYPE, API_NAME, ARGS, ATTRS)			\
@@ -84,10 +95,7 @@ along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
   __GMP_INTERN_FULL(extern TYPE, API_NAME, , , ,			\
 		    __GMP_GLUE(__GMP_ABI_PREFIX, API_NAME))
 
-/* When using GCC, we'll adjust the API symbol to point to the ABI symbol
-   automatically.  Otherwise, we'll have generated and installed a set of
-   defines that will perform the remapping at the preprocessor level.  */
-#if !defined(__GMP_WITHIN_CONFIGURE)
+#if !defined(__GMP_WITHIN_CONFIGURE) && !defined(__GNUC__)
 # include "gmp-renamei.h"
 #endif
 
-- 
1.8.1.2



More information about the gmp-devel mailing list