From 64a0765807ad0cbd178b5b1aade1ab1f20e2be32 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Mon, 29 Jan 2018 13:07:34 -0800 Subject: [PATCH] ELF: Always make __start and __stop symbols dynamic This patch always makes __start and __stop symbols dynamic to support dlsym on __start and __stop symbols when dynamic sections are created. NB: --gc-sections will remove __start and __stop symbols in the executable if they aren't referenced within the executable. bfd/ PR ld/21964 * elflink.c (bfd_elf_define_start_stop): Always make __start and __stop symbols dynamic when dynamic sections are created. ld/ PR ld/21964 * testsuite/ld-elf/pr21964-3.c: New file. * testsuite/ld-elf/shared.exp: Run pr21964-3 test on Linux. * testsuite/ld-x86-64/pr18176.d: Updated. --- bfd/elflink.c | 28 +++++++++++++++++++++++++--- ld/testsuite/ld-elf/pr21964-3.c | 23 +++++++++++++++++++++++ ld/testsuite/ld-elf/shared.exp | 12 ++++++++++++ ld/testsuite/ld-x86-64/pr18176.d | 2 +- 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 ld/testsuite/ld-elf/pr21964-3.c diff --git a/bfd/elflink.c b/bfd/elflink.c index e81f6c6d82..0ece18094f 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -14332,13 +14332,32 @@ bfd_elf_define_start_stop (struct bfd_link_info *info, const char *symbol, asection *sec) { struct elf_link_hash_entry *h; + struct elf_link_hash_table *htab = elf_hash_table (info); + /* Always make __start and __stop symbols dynamic to support dlsym + on __start and __stop symbols when dynamic sections are created. + + NB: --gc-sections will remove __start and __stop symbols in the + executable if they aren't referenced within the executable. */ + bfd_boolean create_p + = (htab != NULL + && htab->dynamic_sections_created + && elf_section_data (sec)->this_hdr.bfd_section != NULL + && symbol[0] != '.'); h = elf_link_hash_lookup (elf_hash_table (info), symbol, FALSE, FALSE, TRUE); + if (h == NULL && create_p) + { + /* lang_init_start_stop will free SYMBOL later. */ + symbol = xstrdup (symbol); + h = elf_link_hash_lookup (elf_hash_table (info), symbol, + TRUE, FALSE, TRUE); + } if (h != NULL - && (h->root.type == bfd_link_hash_undefined + && (create_p + || h->root.type == bfd_link_hash_undefined || h->root.type == bfd_link_hash_undefweak - || ((h->ref_regular || h->def_dynamic) && !h->def_regular))) + || (h->ref_regular && !h->def_regular))) { h->root.type = bfd_link_hash_defined; h->root.u.def.section = sec; @@ -14355,7 +14374,10 @@ bfd_elf_define_start_stop (struct bfd_link_info *info, (*bed->elf_backend_hide_symbol) (info, h, TRUE); } else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) - h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED; + { + bfd_elf_link_record_dynamic_symbol (info, h); + h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED; + } return &h->root; } return NULL; diff --git a/ld/testsuite/ld-elf/pr21964-3.c b/ld/testsuite/ld-elf/pr21964-3.c new file mode 100644 index 0000000000..424c3e50ea --- /dev/null +++ b/ld/testsuite/ld-elf/pr21964-3.c @@ -0,0 +1,23 @@ +#define _GNU_SOURCE +#include +#include + +extern int __start___verbose[]; +extern int __stop___verbose[]; +int bar (void) +{ + static int my_var __attribute__ ((section("__verbose"), used)) = 6; + int *ptr; + ptr = (int*) dlsym (RTLD_DEFAULT, "__start___verbose"); + if (!ptr || *ptr != 6) + return -1; + return 0; +} + +int +main () +{ + if (bar () == 0) + printf ("PASS\n"); + return 0; +} diff --git a/ld/testsuite/ld-elf/shared.exp b/ld/testsuite/ld-elf/shared.exp index 5c4d78aa2d..3eafd9846e 100644 --- a/ld/testsuite/ld-elf/shared.exp +++ b/ld/testsuite/ld-elf/shared.exp @@ -1077,6 +1077,18 @@ if { [istarget *-*-linux*] "pr22393-2-static" \ "pass.out" \ ] \ + [list \ + "Run pr21964-3" \ + "" \ + "" \ + {pr21964-3.c} \ + "pr21964-3" \ + "pass.out" \ + "" \ + "" \ + "" \ + "-ldl" \ + ] \ ] } diff --git a/ld/testsuite/ld-x86-64/pr18176.d b/ld/testsuite/ld-x86-64/pr18176.d index 4e3ad9ff08..4ff07bc23e 100644 --- a/ld/testsuite/ld-x86-64/pr18176.d +++ b/ld/testsuite/ld-x86-64/pr18176.d @@ -5,5 +5,5 @@ #target: x86_64-*-linux* #... - GNU_RELRO 0x04bd17 0x000000000024bd17 0x000000000024bd17 0x0022e9 0x0022e9 R 0x1 + GNU_RELRO 0x04cd17 0x000000000024cd17 0x000000000024cd17 0x0022e9 0x0022e9 R 0x1 #pass -- 2.14.3