This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: Define __start/__stop symbols when there is only a dynamic def
- From: "H.J. Lu" <hjl dot tools at gmail dot com>
- To: Michael Matz <matz at suse dot de>
- Cc: Alan Modra <amodra at gmail dot com>, Nick Clifton <nickc at redhat dot com>, Binutils <binutils at sourceware dot org>
- Date: Mon, 29 Jan 2018 13:16:21 -0800
- Subject: Re: Define __start/__stop symbols when there is only a dynamic def
- Authentication-results: sourceware.org; auth=none
On Mon, Jan 29, 2018 at 10:28 AM, Michael Matz <matz@suse.de> wrote:
> Hi,
>
> On Mon, 29 Jan 2018, H.J. Lu wrote:
>
>> > % cat app.c
>> > #define _GNU_SOURCE
>> > #include <stdlib.h>
>> > #include <stdio.h>
>> > #include <dlfcn.h>
>> >
>> > extern int __start___verbose[];
>> > extern int __stop___verbose[];
>> > int bar (void)
>> > {
>> > static int my_var __attribute__((section("__verbose"))) = 6;
>> > int *ptr;
>> > ptr = (int*) dlsym(RTLD_DEFAULT, "__start___verbose");
>> > if (!ptr || *ptr != 6)
>> > return -1;
>> > return 0;
>> > }
>> >
>> > int main()
>> > {
>> > if (bar () != 0)
>> > {
>> > printf("main: wrong __start___verbose\n");
>> > exit(2);
>> > }
>> > return 0;
>> > }
>>
>> Just to verify. This is the correct testcase:
>>
>> [hjl@gnu-6 orphan-4]$ cat app.c
>> #define _GNU_SOURCE
>> #include <stdio.h>
>> #include <dlfcn.h>
>>
>> extern int __start___verbose[];
>> extern int __stop___verbose[];
>> int bar (void)
>> {
>> static int my_var __attribute__((section("__verbose"))) = 6;
>> int *ptr;
>> ptr = (int*) dlsym(RTLD_DEFAULT, "__start___verbose");
>> if (!ptr || *ptr != 6)
>> return -1;
>> return 0;
>> }
>>
>> int main()
>> {
>> if (bar () != 0)
>> {
>> printf("main: wrong __start___verbose\n");
>> return 2;
>> }
>> return 0;
>> }
>> [hjl@gnu-6 orphan-4]$ make
>> cc -g -c -o app.o app.c
>> cc -B./ -o app app.o -ldl -Wl,-R,.
>> ./app
>> main: wrong __start___verbose
>> make: *** [Makefile:6: all] Error 2
>> [hjl@gnu-6 orphan-4]$
>
> Yes, that's it. (the associated lib.c needs to have a regular ref symbol
> to __start___symbol and that lib needs to have something else than "6" in
> its __verbose section, in order to test the exact situation of pacemaker).
> With the patch from Alan ./app will have __start___verbose in its symbol
> table, but not yet in the dynamic symbol table. So with only Alans patch
> -E would be necessary. With the addition of the
> bfd_elf_link_record_dynamic_symbol() call to bfd_elf_define_start_stop the
> above testcase will start working as is even without -E.
I don't think Alan's patch nor yours are correct. Here is mine with your
testcase. OK for master?
Thanks.
--
H.J.
From 64a0765807ad0cbd178b5b1aade1ab1f20e2be32 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
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 <dlfcn.h>
+#include <stdio.h>
+
+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