x86-64: Disallow PC reloc against weak undefined symbols in PIE

Message ID 20210708205625.602224-1-hjl.tools@gmail.com
State New
Headers show
Series
  • x86-64: Disallow PC reloc against weak undefined symbols in PIE
Related show

Commit Message

Luis Machado via Binutils July 8, 2021, 8:56 p.m.
Disallow PC relocations against weak undefined symbols in PIE since they
can lead to non-zero address at run-time.

Checking it into master branch and will backport to release branches.

bfd/

	PR ld/21782
	* elf64-x86-64.c (elf_x86_64_relocate_section): Disallow PC
	relocations against weak undefined symbols in PIE.

ld/

	PR ld/21782
	* testsuite/ld-x86-64/pie3.d: Expect linker error.
---
 bfd/elf64-x86-64.c            |  7 +++++--
 ld/testsuite/ld-x86-64/pie3.d | 11 +----------
 2 files changed, 6 insertions(+), 12 deletions(-)

-- 
2.31.1

Patch

diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 4c55c5999c3..8deb62dba5c 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -3161,6 +3161,8 @@  elf_x86_64_relocate_section (bfd *output_bfd,
 		       || (no_copyreloc_p
 			   && h->def_dynamic
 			   && !(h->root.u.def.section->flags & SEC_CODE))))
+		  || (bfd_link_pie (info)
+		      && h->root.type == bfd_link_hash_undefweak)
 		  || bfd_link_dll (info)))
 	    {
 	      bool fail = false;
@@ -3174,8 +3176,9 @@  elf_x86_64_relocate_section (bfd *output_bfd,
 		{
 		  /* We can only use PC-relative relocations in PIE
 		     from non-code sections.  */
-		  if (h->type == STT_FUNC
-		      && (sec->flags & SEC_CODE) != 0)
+		  if (h->root.type == bfd_link_hash_undefweak
+		      || (h->type == STT_FUNC
+			  && (sec->flags & SEC_CODE) != 0))
 		    fail = true;
 		}
 	      else if (no_copyreloc_p || bfd_link_dll (info))
diff --git a/ld/testsuite/ld-x86-64/pie3.d b/ld/testsuite/ld-x86-64/pie3.d
index b943837bf64..a20029c63ba 100644
--- a/ld/testsuite/ld-x86-64/pie3.d
+++ b/ld/testsuite/ld-x86-64/pie3.d
@@ -1,12 +1,3 @@ 
 #as: --64
 #ld: -pie -melf_x86_64 --hash-style=sysv -z max-page-size=0x200000 -z noseparate-code
-#objdump: -dw
-
-.*: +file format .*
-
-
-Disassembly of section .text:
-
-0+191 <_start>:
- +191:	48 8d 05 68 fe ff ff 	lea    -0x198\(%rip\),%rax        # 0 <_start-0x191>
-#pass
+#error: .*relocation R_X86_64_PC32 against undefined symbol `foo' can not be used when making a PIE object; recompile with -fPIE