Set EI_OSABI to ELFOSABI_GNU for local IFUNC symbols
authorH.J. Lu <hjl.tools@gmail.com>
Wed, 12 Aug 2015 20:31:29 +0000 (13:31 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Wed, 12 Aug 2015 20:47:41 +0000 (13:47 -0700)
Since the backend elf_add_symbol_hook isn't called on local symbols,
the EI_OSABI field isn't to ELFOSABI_GNU where are local IFUNC symbols.
This patch changes the x86 backends to set has_gnu_symbols if there are
relocations against IFUNC symbols.  Other backends with IFUNC support
may need a similar change.

This patch also changes the type of has_gnu_symbols from bfd_boolean to
enum elf_gnu_symbols.

bfd/

PR ld/18815
* elf-bfd.h (elf_gnu_symbols): New enum.
(elf_obj_tdata): Use elf_gnu_symbols on has_gnu_symbols.
* elf-s390-common.c (elf_s390_add_symbol_hook): Set
has_gnu_symbols to elf_gnu_symbol_any.
* elf32-arm.c (elf32_arm_add_symbol_hook): Likewise.
* elf32-m68k.c (elf_m68k_add_symbol_hook): Likewise.
* elf32-ppc.c (ppc_elf_add_symbol_hook): Likewise.
* elf32-sparc.c (elf32_sparc_add_symbol_hook): Likewise.
* elf64-ppc.c (ppc64_elf_add_symbol_hook): Likewise.
* elf64-sparc.c (elf64_sparc_add_symbol_hook): Likewise.
* lfxx-aarch64.c (_bfd_aarch64_elf_add_symbol_hook): Likewise.
* elf32-i386.c (elf_i386_check_relocs): Update has_gnu_symbols
if there are relocations against IFUNC symbols.
(elf_i386_add_symbol_hook): Don't check STT_GNU_IFUNC here.
* elf64-x86-64. (elf_x86_64_check_relocs): Update has_gnu_symbols
if there are relocations against IFUNC symbols.
(elf_x86_64_add_symbol_hook): Don't check STT_GNU_IFUNC here.

ld/testsuite/

PR ld/18815
* ld-i386/i386.exp: Run pr18815.
* ld-x86-64/x86-64.exp: Likewise.
* ld-i386/pr18815.d: New file.
* ld-i386/pr18815.s: Likewise.
* ld-x86-64/pr18815.d: Likewise.
* ld-x86-64/pr18815.s: Likewise.

17 files changed:
bfd/elf-bfd.h
bfd/elf-s390-common.c
bfd/elf32-arm.c
bfd/elf32-i386.c
bfd/elf32-m68k.c
bfd/elf32-ppc.c
bfd/elf32-sparc.c
bfd/elf64-ppc.c
bfd/elf64-sparc.c
bfd/elf64-x86-64.c
bfd/elfxx-aarch64.c
ld/testsuite/ld-i386/i386.exp
ld/testsuite/ld-i386/pr18815.d [new file with mode: 0644]
ld/testsuite/ld-i386/pr18815.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr18815.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr18815.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/x86-64.exp

index c92671a61012767b604e5f7ef320a13c0819ba81..d8d11b7771b56a427f606fb809a6829e0734753b 100644 (file)
@@ -1641,6 +1641,18 @@ struct output_elf_obj_tdata
   bfd_boolean flags_init;
 };
 
+/* Indicate if the bfd contains symbols that have the STT_GNU_IFUNC
+   symbol type or STB_GNU_UNIQUE binding.  Used to set the osabi
+   field in the ELF header structure.  */
+enum elf_gnu_symbols
+  {
+    elf_gnu_symbol_none = 0,
+    elf_gnu_symbol_any = 1 << 0,
+    elf_gnu_symbol_ifunc = (elf_gnu_symbol_any | 1 << 1),
+    elf_gnu_symbol_unique = (elf_gnu_symbol_any | 1 << 2),
+    elf_gnu_symbol_all = (elf_gnu_symbol_ifunc | elf_gnu_symbol_unique)
+  };
+
 /* Some private data is stashed away for future use using the tdata pointer
    in the bfd structure.  */
 
@@ -1751,10 +1763,7 @@ struct elf_obj_tdata
      symbols.  */
   bfd_boolean bad_symtab;
 
-  /* True if the bfd contains symbols that have the STT_GNU_IFUNC
-     symbol type or STB_GNU_UNIQUE binding.  Used to set the osabi
-     field in the ELF header structure.  */
-  bfd_boolean has_gnu_symbols;
+  enum elf_gnu_symbols has_gnu_symbols;
 
   /* Information grabbed from an elf core file.  */
   struct core_elf_obj_tdata *core;
index dc6f55b0938357910440fcdb3d0acb6b0797581b..09d4e5c3414b73278c02d5716af6a199f6ddf934 100644 (file)
@@ -238,7 +238,7 @@ elf_s390_add_symbol_hook (bfd *abfd,
        || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
       && (abfd->flags & DYNAMIC) == 0
       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
-    elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
+    elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
 
   return TRUE;
 }
index ff69728729765f0f64446b3c8fd8d13b8ae5b3c2..d313de41d3257bb0fc9b410feb68a8debad4a753 100644 (file)
@@ -15928,7 +15928,7 @@ elf32_arm_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
        || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
       && (abfd->flags & DYNAMIC) == 0
       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
-    elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
+    elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
 
   if (elf32_arm_hash_table (info) == NULL)
     return FALSE;
index 98902ac61864fe31096772e207fa51c379dbc528..3063bed7d1d468932e8a375c3aebb88bc4a996ba 100644 (file)
@@ -1576,6 +1576,10 @@ elf_i386_check_relocs (bfd *abfd,
          /* It is referenced by a non-shared object. */
          h->ref_regular = 1;
          h->root.non_ir_ref = 1;
+
+         if (h->type == STT_GNU_IFUNC)
+           elf_tdata (info->output_bfd)->has_gnu_symbols
+             |= elf_gnu_symbol_ifunc;
        }
 
       if (! elf_i386_tls_transition (info, abfd, sec, NULL,
@@ -5330,11 +5334,11 @@ elf_i386_add_symbol_hook (bfd * abfd,
                          asection ** secp ATTRIBUTE_UNUSED,
                          bfd_vma * valp ATTRIBUTE_UNUSED)
 {
-  if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
-       || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
+  if (ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE
       && (abfd->flags & DYNAMIC) == 0
       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
-    elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
+    elf_tdata (info->output_bfd)->has_gnu_symbols
+      |= elf_gnu_symbol_unique;
 
   return TRUE;
 }
index db0d0da6ec0805f3844a9b09d28b33cb21c57dcb..07211b57aec1be50e8b7a1e7b6e5b0cc0dcee9b1 100644 (file)
@@ -4846,7 +4846,7 @@ elf_m68k_add_symbol_hook (bfd *abfd,
        || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
       && (abfd->flags & DYNAMIC) == 0
       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
-    elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
+    elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
 
   return TRUE;
 }
index fc1a85447455142e3a637edf0242c20a98b3185d..91942ec297fb20488698ae78f6aae79af2022123 100644 (file)
@@ -3653,7 +3653,7 @@ ppc_elf_add_symbol_hook (bfd *abfd,
        || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
       && (abfd->flags & DYNAMIC) == 0
       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
-    elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
+    elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
 
   return TRUE;
 }
index 88efe9e4cc3ba1d2541c34f56fd4cd7ab7bd175c..8b8b601f35381b302fe6e23fb6cf3cdadf7e1bcf 100644 (file)
@@ -184,7 +184,7 @@ elf32_sparc_add_symbol_hook (bfd * abfd,
        || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
       && (abfd->flags & DYNAMIC) == 0
       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
-    elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
+    elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
   return TRUE;
 }
 
index ef081642ac674da0318818b9bb62b1ce177b611d..e153ee484b6544fdf79c5ad9c774db3cd9f77cab 100644 (file)
@@ -4821,7 +4821,7 @@ ppc64_elf_add_symbol_hook (bfd *ibfd,
        || ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
       && (ibfd->flags & DYNAMIC) == 0
       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
-    elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
+    elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
 
   if (*sec != NULL
       && strcmp ((*sec)->name, ".opd") == 0)
index 26e664efc7f49db1599c01a2e861fde895527723..5413891c4d6d50b339013407de7a786d40e49143 100644 (file)
@@ -430,7 +430,7 @@ elf64_sparc_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
        || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
       && (abfd->flags & DYNAMIC) == 0
       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
-    elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
+    elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
 
   if (ELF_ST_TYPE (sym->st_info) == STT_REGISTER)
     {
index b3c85221afc06ddd4e042b0c3386f87c245b8f94..2d3c55ea81efeb45c15e5e5bcc5b5318087f4dbe 100644 (file)
@@ -1723,6 +1723,10 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
          /* It is referenced by a non-shared object. */
          h->ref_regular = 1;
          h->root.non_ir_ref = 1;
+
+         if (h->type == STT_GNU_IFUNC)
+           elf_tdata (info->output_bfd)->has_gnu_symbols
+             |= elf_gnu_symbol_ifunc;
        }
 
       if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
@@ -5873,11 +5877,11 @@ elf_x86_64_add_symbol_hook (bfd *abfd,
       return TRUE;
     }
 
-  if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
-       || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
+  if (ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE
       && (abfd->flags & DYNAMIC) == 0
       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
-    elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
+    elf_tdata (info->output_bfd)->has_gnu_symbols
+      |= elf_gnu_symbol_unique;
 
   return TRUE;
 }
index a8617739c0d025ac7985ad06cc69ea8474347196..69dac6db3413031b3b9b78def07802ceae95e5ae 100644 (file)
@@ -494,7 +494,7 @@ _bfd_aarch64_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
        || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
       && (abfd->flags & DYNAMIC) == 0
       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
-    elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
+    elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
 
   return TRUE;
 }
index 0dbdd1e06e054f635920ac0bbec11d5b5d46ca67..0a718cfb888cc7db9e152f99f227326f7689842c 100644 (file)
@@ -309,6 +309,7 @@ run_dump_test "pr14215"
 run_dump_test "pr17057"
 run_dump_test "pr17935-1"
 run_dump_test "pr17935-2"
+run_dump_test "pr18815"
 
 # Add $PLT_CFLAGS if PLT is expected.
 global PLT_CFLAGS
diff --git a/ld/testsuite/ld-i386/pr18815.d b/ld/testsuite/ld-i386/pr18815.d
new file mode 100644 (file)
index 0000000..f49ecf7
--- /dev/null
@@ -0,0 +1,9 @@
+#name: PR ld/18815
+#as: --32
+#ld: -melf_i386
+#readelf: -h
+
+ELF Header:
+#...
+  OS/ABI:                            UNIX - GNU
+#pass
diff --git a/ld/testsuite/ld-i386/pr18815.s b/ld/testsuite/ld-i386/pr18815.s
new file mode 100644 (file)
index 0000000..ac3377d
--- /dev/null
@@ -0,0 +1,15 @@
+       .text
+       .type   selector, %function
+foo:
+       movl    $0, %eax
+       ret
+selector:
+       mov     $foo, %eax
+       ret
+       .type   selector, %gnu_indirect_function
+       .globl  _start
+_start:
+       mov     $selector, %eax
+       call    *%eax
+       ret
+       .section        .note.GNU-stack,"",@progbits
diff --git a/ld/testsuite/ld-x86-64/pr18815.d b/ld/testsuite/ld-x86-64/pr18815.d
new file mode 100644 (file)
index 0000000..dd95b2a
--- /dev/null
@@ -0,0 +1,9 @@
+#name: PR ld/18815
+#as: --64
+#ld: -melf_x86_64
+#readelf: -h
+
+ELF Header:
+#...
+  OS/ABI:                            UNIX - GNU
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr18815.s b/ld/testsuite/ld-x86-64/pr18815.s
new file mode 100644 (file)
index 0000000..6ec79ba
--- /dev/null
@@ -0,0 +1,15 @@
+       .text
+       .type   selector, %function
+foo:
+       movl    $0, %eax
+       ret
+selector:
+       mov     $foo, %eax
+       ret
+       .type   selector, %gnu_indirect_function
+       .globl  _start
+_start:
+       mov     $selector, %rax
+       call    *%rax
+       ret
+       .section        .note.GNU-stack,"",@progbits
index d67ff2a346fcd543efeea6c300607c450495490d..bd48cd3c6e702165ecbc978cdcdafb66b882fd5c 100644 (file)
@@ -325,6 +325,7 @@ run_dump_test "pr17935-1"
 run_dump_test "pr17935-2"
 run_dump_test "pr18160"
 run_dump_test "pr18176"
+run_dump_test "pr18815"
 
 # Add $PLT_CFLAGS if PLT is expected.
 global PLT_CFLAGS