Don't complain about -fPIC for undefined symbol
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 15 Jan 2015 19:06:33 +0000 (11:06 -0800)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 15 Jan 2015 19:08:59 +0000 (11:08 -0800)
When building executable, undefined symbol is a fatal error.  We don't
complain about -fPIC if the symbol is undefined.

bfd/

PR ld/17847
* elf64-x86-64.c (elf_x86_64_relocate_section): Don't complain
about -fPIC if the symbol is undefined when building executable.

ld/testsuite/

PR ld/17847
* ld-x86-64/pie1.d: New file.
* ld-x86-64/pie1.s: Likwise.
* ld-x86-64/x86-64.exp: Run pie1.

bfd/ChangeLog
bfd/elf64-x86-64.c
ld/testsuite/ChangeLog
ld/testsuite/ld-x86-64/pie1.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pie1.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/x86-64.exp

index 8c376e25be2944b6eed21480ece5847574e03cbd..9558b5c3d39e98786d1ad20bdad8d70192067896 100644 (file)
@@ -1,3 +1,9 @@
+2015-01-15  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/17847
+       * elf64-x86-64.c (elf_x86_64_relocate_section): Don't complain
+       about -fPIC if the symbol is undefined when building executable.
+
 2015-01-15  Nick Clifton  <nickc@redhat.com>
 
        PR binutils/17512
index 6b7d3c9a56846bf4c622609b7ce77afbaeab94e3..bf7d386bd9a133d9b749cd460a04b3e9fa4f4cc6 100644 (file)
@@ -4063,10 +4063,14 @@ elf_x86_64_relocate_section (bfd *output_bfd,
        case R_X86_64_PC16:
        case R_X86_64_PC32:
        case R_X86_64_PC32_BND:
+         /* Don't complain about -fPIC if the symbol is undefined when
+            building executable.  */
          if (info->shared
              && (input_section->flags & SEC_ALLOC) != 0
              && (input_section->flags & SEC_READONLY) != 0
-             && h != NULL)
+             && h != NULL
+             && !(info->executable
+                  && h->root.type == bfd_link_hash_undefined))
            {
              bfd_boolean fail = FALSE;
              bfd_boolean branch
@@ -4140,11 +4144,14 @@ direct:
            break;
 
           /* Don't copy a pc-relative relocation into the output file
-             if the symbol needs copy reloc.  */
+             if the symbol needs copy reloc or the symbol is undefined
+             when building executable.  */
          if ((info->shared
               && !(info->executable
                    && h != NULL
-                   && (h->needs_copy || eh->needs_copy)
+                   && (h->needs_copy
+                       || eh->needs_copy
+                       || h->root.type == bfd_link_hash_undefined)
                    && IS_X86_64_PCREL_TYPE (r_type))
               && (h == NULL
                   || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
index 3ab61c775663bc7472e74dbfe988956df3aed615..58e5595e1e4a4d12e4be070fc385b9925a99f1f4 100644 (file)
@@ -1,3 +1,10 @@
+2015-01-15  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/17847
+       * ld-x86-64/pie1.d: New file.
+       * ld-x86-64/pie1.s: Likwise.
+       * ld-x86-64/x86-64.exp: Run pie1.
+
 2015-01-13  Thomas Preud'homme <thomas.preudhomme@arm.com>
 
        * ld-aarch64/aarch64-elf.exp: Added relocs-257-symbolic-func test.
diff --git a/ld/testsuite/ld-x86-64/pie1.d b/ld/testsuite/ld-x86-64/pie1.d
new file mode 100644 (file)
index 0000000..06d2562
--- /dev/null
@@ -0,0 +1,9 @@
+#name: PIE with undefined symbol
+#as: --64
+#ld: -pie -melf_x86_64 --noinhibit-exec
+#readelf: -s --wide
+#warning: \A[^\n]*\.o[^\n]*In function `_start':\n[^\n]*: undefined reference to `foo'\Z
+
+#...
+ +[0-9]+: +[0-9a-f]+ +0 +NOTYPE +GLOBAL +DEFAULT +UND foo
+#pass
diff --git a/ld/testsuite/ld-x86-64/pie1.s b/ld/testsuite/ld-x86-64/pie1.s
new file mode 100644 (file)
index 0000000..94c59cb
--- /dev/null
@@ -0,0 +1,4 @@
+       .text
+       .global _start
+_start:
+       movl    foo(%rip), %eax
index 5b49bffe6603f58e88799a99c1e623c9cff86c98..bfa89c3ce52d307d6cab4a5fda817f47e67a5f1e 100644 (file)
@@ -228,6 +228,7 @@ run_dump_test "pr12570b"
 run_dump_test "pr14215"
 run_dump_test "pr14207"
 run_dump_test "gotplt1"
+run_dump_test "pie1"
 
 if { ![istarget "x86_64-*-linux*"] && ![istarget "x86_64-*-nacl*"]} {
     return