]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
x86-64: Disallow PC reloc against weak undefined symbols in PIE
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 8 Jul 2021 21:03:53 +0000 (14:03 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 8 Jul 2021 21:04:40 +0000 (14:04 -0700)
Disallow PC relocations against weak undefined symbols in PIE since they
can lead to non-zero address at run-time.

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.

(cherry picked from commit 661b504df9bfb8d7c5d669091720e1dc0079c05e)

bfd/ChangeLog
bfd/elf64-x86-64.c
ld/ChangeLog
ld/testsuite/ld-x86-64/pie3.d

index 14d159b3b6593db11759fe55177859dd8c6dd84a..9566e418d7bbcb74c13fb56760bb94be91686ee9 100644 (file)
@@ -1,3 +1,9 @@
+2021-07-08  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/21782
+       * elf64-x86-64.c (elf_x86_64_relocate_section): Disallow PC
+       relocations against weak undefined symbols in PIE.
+
 2021-07-08  Nick Clifton  <nickc@redhat.com>
 
        * po/pt.po: Updated Portuguese translation.
index 98fb88113c0d257056c08caa7b4e3797815bb1d8..4b6489b9648297332d5819c1fabd0ab2e152f744 100644 (file)
@@ -3160,6 +3160,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;
@@ -3173,8 +3175,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))
index fa8cd35d22f9f02c40596d0ae2dddfb9e4ada3c5..40b4e584aa09e9186cef8196bb74c56f7e9690e9 100644 (file)
@@ -1,3 +1,8 @@
+2021-07-08  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/21782
+       * testsuite/ld-x86-64/pie3.d: Expect linker error.
+
 2021-07-07  Michael Matz  <matz@suse.de>
 
        PR ld/28021
index b943837bf6424699b3d6d560545a3ca1b856a6fe..a20029c63ba5365c681132d0f113d1fc72c23aac 100644 (file)
@@ -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