Broken PIC support in OS X

Manuel M T Chakravarty chak at
Fri Aug 5 07:05:18 CEST 2011

GMP version 5.0.2
OS/arch: OS X 10.7 (Lion), x86_64
Compiler: gcc 4.2 (Xcode 4.1)

When compiling GMP 5.0.2 on the specified system, assembly code (such as 'mpn/x86_64/bdiv_q_1.asm') is incorrectly compiled without support for PIC.  According to Apple documentation, the x86_64 code model for Mach-O always requires PIC:

As a result PIC is the default and doesn't need to be specified when compiling or linking.  Assembly code that doesn't adhere to this convention leads to a link time warning of the form

  ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE

The linker will still produce a binary, but it is not position independent.

To test, simply pre-process an assembly file, e.g.,

  m4  -DHAVE_CONFIG_H -D__GMP_WITHIN_GMP -DOPERATION_bdiv_q_1 bdiv_q_1.asm >tmp-bdiv_q_1.s

The resulting assembly code will load a global table address with

  movabs  $binvert_limb_table, %rdx

instead of with

  mov     binvert_limb_table at GOTPCREL(%rip), %rdx

I append a patch that fixes the problem.


PS: I tested this on OS X 10.7, but believe the same issues arises on 10.6 for x86_64 and recent versions of Xcode.

-=- Patch to ensure PIC support is used on OS X/x64_64 -=-

--- gmp-5.0.2.orig/	2011-08-04 16:35:01.000000000 +1000
+++ gmp-5.0.2/	2011-08-04 16:39:03.000000000 +1000
@@ -3178,6 +3178,7 @@
 	  case $host in
+	      GMP_DEFINE_RAW(["define(<PIC_ALWAYS>,<yes>)"])
 	      GMP_INCLUDE_MPN(x86_64/darwin.m4) ;;
--- gmp-5.0.2.orig/mpn/asm-defs.m4	2011-08-04 23:11:19.000000000 +1000
+++ gmp-5.0.2/mpn/asm-defs.m4	2011-08-04 23:11:59.000000000 +1000
@@ -1041,7 +1041,7 @@
 dnl  systems which are always PIC.  PIC_ALWAYS established in config.m4
 dnl  identifies these for us.
 dnl  Various possible defines passed from the Makefile that are to be tested

More information about the gmp-bugs mailing list