This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] ELF: Don't check DT_NEEDED for linker script defined symbols


Linker shouldn't use any shared objects, including those from DT_NEEDED,
to resolve references of symbols which will be defined by linker script.

OK for master?

H.J.
--
bfd/

	PR ld/22471
	* elflink.c (elf_link_add_object_symbols)): Don't check DT_NEEDED
	for linker script defined symbols.

include/

	PR ld/22471
	* bfdlink.h (bfd_link_info): Add builtin_linker_script.

ld/

	PR ld/22471
	* emultempl/elf32.em (gld${EMULATION_NAME}_after_open): Set
	ldscript_def on referenced symbols defined by builtin linker
	script.
	(gld${EMULATION_NAME}_get_script): Set builtin_linker_script
	when builtin linker script are used.
	* testsuite/ld-elf/pr22471.t: New file.
	* testsuite/ld-elf/pr22471a.s: Likewise.
	* testsuite/ld-elf/pr22471b.s: Likewise.
	* testsuite/ld-elf/shared.exp: Run PR ld/22471 tests.
---
 bfd/elflink.c                  |  1 +
 include/bfdlink.h              |  3 +++
 ld/emultempl/elf32.em          | 35 +++++++++++++++++++++++++++++++++++
 ld/testsuite/ld-elf/pr22471.t  |  1 +
 ld/testsuite/ld-elf/pr22471a.s |  1 +
 ld/testsuite/ld-elf/pr22471b.s | 13 +++++++++++++
 ld/testsuite/ld-elf/shared.exp | 30 ++++++++++++++++++++++++++++++
 7 files changed, 84 insertions(+)
 create mode 100644 ld/testsuite/ld-elf/pr22471.t
 create mode 100644 ld/testsuite/ld-elf/pr22471a.s
 create mode 100644 ld/testsuite/ld-elf/pr22471b.s

diff --git a/bfd/elflink.c b/bfd/elflink.c
index 72886d57e5..e43512f25b 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -4886,6 +4886,7 @@ error_free_dyn:
 	  if (!add_needed
 	      && matched
 	      && definition
+	      && !h->root.ldscript_def
 	      && ((dynsym
 		   && h->ref_regular_nonweak
 		   && (old_bfd == NULL
diff --git a/include/bfdlink.h b/include/bfdlink.h
index 2370c0d45a..8af39fe54e 100644
--- a/include/bfdlink.h
+++ b/include/bfdlink.h
@@ -492,6 +492,9 @@ struct bfd_link_info
   /* TRUE if common symbols should be treated as undefined.  */
   unsigned int inhibit_common_definition : 1;
 
+  /* TRUE if builtin linker script is used.  */
+  unsigned int builtin_linker_script : 1;
+
   /* The 1-byte NOP for x86 call instruction.  */
   char call_nop_byte;
 
diff --git a/ld/emultempl/elf32.em b/ld/emultempl/elf32.em
index edd8944f04..7d5d66dea3 100644
--- a/ld/emultempl/elf32.em
+++ b/ld/emultempl/elf32.em
@@ -1284,6 +1284,39 @@ gld${EMULATION_NAME}_after_open (void)
       return;
     }
 
+  if (link_info.builtin_linker_script)
+    {
+      struct bfd_link_hash_entry *h;
+      const char *symbols[] =
+	{
+	  "__${ETEXT_NAME}",
+	  "_${ETEXT_NAME}",
+	  "${ETEXT_NAME}",
+	  "${USER_LABEL_PREFIX}" "__bss_start",
+	  "${USER_LABEL_PREFIX}" "_edata",
+	  "${USER_LABEL_PREFIX}" "edata",
+	  "${USER_LABEL_PREFIX}" "_end",
+	  "${USER_LABEL_PREFIX}" "end",
+	  NULL
+	};
+      const char **p;
+
+      /* These symbols are defined by builtin linker scripts.  Set
+	 ldscript_def if they are referenced.  */
+      for (p = symbols; *p != NULL; p++)
+	{
+	  h = bfd_link_hash_lookup (link_info.hash, *p, FALSE, FALSE,
+				    FALSE);
+	  if (h != NULL
+	      && (h->type == bfd_link_hash_new
+		  || h->type == bfd_link_hash_undefined
+		  || h->type == bfd_link_hash_undefweak
+		  || h->type == bfd_link_hash_common))
+	    h->ldscript_def = 1;
+	}
+    }
+
+
   if (!link_info.traditional_format)
     {
       bfd *elfbfd = NULL;
@@ -2351,6 +2384,8 @@ fragment <<EOF
 {
   *isfile = 0;
 
+  link_info.builtin_linker_script = TRUE;
+
   if (bfd_link_relocatable (&link_info) && config.build_constructors)
     return
 EOF
diff --git a/ld/testsuite/ld-elf/pr22471.t b/ld/testsuite/ld-elf/pr22471.t
new file mode 100644
index 0000000000..8862dc291d
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr22471.t
@@ -0,0 +1 @@
+{ local: *; };
diff --git a/ld/testsuite/ld-elf/pr22471a.s b/ld/testsuite/ld-elf/pr22471a.s
new file mode 100644
index 0000000000..3bb6a92bdb
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr22471a.s
@@ -0,0 +1 @@
+# Empty input.
diff --git a/ld/testsuite/ld-elf/pr22471b.s b/ld/testsuite/ld-elf/pr22471b.s
new file mode 100644
index 0000000000..b59a8ddcb6
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr22471b.s
@@ -0,0 +1,13 @@
+	.type start,"function"
+	.global start
+start:
+	.type _start,"function"
+	.global _start
+_start:
+	.type __start,"function"
+	.global __start
+__start:
+	.type main,"function"
+	.global main
+main:
+	.dc.a __bss_start
diff --git a/ld/testsuite/ld-elf/shared.exp b/ld/testsuite/ld-elf/shared.exp
index a40f8e0f59..2bbc99b898 100644
--- a/ld/testsuite/ld-elf/shared.exp
+++ b/ld/testsuite/ld-elf/shared.exp
@@ -47,6 +47,36 @@ if [istarget "tic6x-*-*"] {
     append LFLAGS " -melf32_tic6x_le"
 }
 
+run_ld_link_tests [list \
+    [list \
+	"Build pr22471a.so" \
+	"$LFLAGS -shared" \
+	"" \
+	"$AFLAGS_PIC" \
+	{pr22471a.s} \
+	{} \
+	"pr22471a.so" \
+    ] \
+    [list \
+	"Build pr22471b.so" \
+	"$LFLAGS -shared --version-script pr22471.t" \
+	"tmpdir/pr22471a.so" \
+	"$AFLAGS_PIC" \
+	{pr22471a.s} \
+	{} \
+	"pr22471b.so" \
+    ] \
+    [list \
+	"Build pr22471" \
+	"$LFLAGS -rpath-link ." \
+	"tmpdir/pr22471b.so" \
+	"" \
+	{pr22471b.s} \
+	{} \
+	"pr22471" \
+    ] \
+]
+
 # PR ld/20828 check for correct dynamic symbol table entries where:
 # - symbols have been defined with a linker script,
 # - the same symbols have been seen in shared library used in the link,
-- 
2.14.3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]