PATCH: Fix BZ/186: Ld failed to issue an error on non-PIC code when building shared library

H. J. Lu hjl@lucon.org
Fri May 28 21:19:00 GMT 2004


On Fri, May 28, 2004 at 11:20:29AM -0700, H. J. Lu wrote:
> PIC is required on x86_64 for shared library. But the linker fails to
> do so under certain circumstances, which leads to the run-time error.
> This patch catches those errors. I also updated the testsuite.
> 
> 

The patch is wrong. I should check those relocations in
elf64_x86_64_relocate_section and use SYMBOL_REFERENCES_LOCAL. Here is
the updated one.

BTW, the bug is at

http://sources.redhat.com/bugzilla/show_bug.cgi?id=186


H.J.
-------------- next part --------------
2004-05-28  H.J. Lu  <hongjiu.lu@intel.com>

	* elf64-x86-64.c (elf64_x86_64_check_relocs): Warn overflow
	relocation symbol.
	(elf64_x86_64_relocate_section): Issue an error for
	R_X86_64_PC8, R_X86_64_PC16 and R_X86_64_PC32 relocations
	against global symbols when building shared library.

--- bfd/elf64-x86-64.c.shared	2004-05-11 13:34:02.000000000 -0700
+++ bfd/elf64-x86-64.c	2004-05-28 12:15:14.356262453 -0700
@@ -663,9 +663,10 @@ elf64_x86_64_check_relocs (bfd *abfd, st
 	  if (info->shared)
 	    {
 	      (*_bfd_error_handler)
-		(_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
+		(_("%s: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
 		 bfd_archive_filename (abfd),
-		 x86_64_elf_howto_table[r_type].name);
+		 x86_64_elf_howto_table[r_type].name,
+		 (h) ? h->root.root.string : "a local symbol");
 	      bfd_set_error (bfd_error_bad_value);
 	      return FALSE;
 	    }
@@ -788,9 +789,10 @@ elf64_x86_64_check_relocs (bfd *abfd, st
 	      && (sec->flags & SEC_READONLY) != 0)
 	    {
 	      (*_bfd_error_handler)
-		(_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
+		(_("%s: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
 		 bfd_archive_filename (abfd),
-		 x86_64_elf_howto_table[r_type].name);
+		 x86_64_elf_howto_table[r_type].name,
+		 (h) ? h->root.root.string : "a local symbol");
 	      bfd_set_error (bfd_error_bad_value);
 	      return FALSE;
 	    }
@@ -1946,6 +1948,21 @@ elf64_x86_64_relocate_section (bfd *outp
 	case R_X86_64_PC8:
 	case R_X86_64_PC16:
 	case R_X86_64_PC32:
+	  if (info->shared
+	      && !SYMBOL_REFERENCES_LOCAL (info, h)
+	      && (sec->flags & SEC_ALLOC) != 0
+	      && (sec->flags & SEC_READONLY) != 0)
+	    {
+	      (*_bfd_error_handler)
+		(_("%s: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
+		 bfd_archive_filename (input_bfd),
+		 x86_64_elf_howto_table[r_type].name,
+		 (h) ? h->root.root.string : "a local symbol");
+	      bfd_set_error (bfd_error_bad_value);
+	      return FALSE;
+	    }
+	  /* Fall through.  */
+
 	case R_X86_64_8:
 	case R_X86_64_16:
 	case R_X86_64_32:


More information about the Binutils mailing list