From 059fc6d823a81602d951275ba99430550b84f8a3 Mon Sep 17 00:00:00 2001
From: Mahesh Bodapati <mbodapat@xilinx.com>
Date: Tue, 26 Feb 2019 17:46:36 +0530
Subject: [PATCH 27/31] [Patch,Microblaze] : "PR22471, undefined reference to
 linker-defined symbols" is causing runtime loops.reverting for now.

Signed-off-by: Mahesh Bodapati <mbodapat@xilinx.com>
Signed-off-by: Nagaraju Mekala <nagaraju.mekala@xilinx.com>
---
 bfd/elflink.c                             |  7 +---
 bfd/linker.c                              |  9 +----
 ld/ldexp.c                                | 64 +++++++++++++++++++------------
 ld/ldlang.c                               |  6 +--
 ld/testsuite/ld-mips-elf/tlsdyn-o32-1.d   |  4 +-
 ld/testsuite/ld-mips-elf/tlsdyn-o32-1.got |  2 +-
 ld/testsuite/ld-mips-elf/tlsdyn-o32-2.d   |  4 +-
 ld/testsuite/ld-mips-elf/tlsdyn-o32-2.got |  2 +-
 ld/testsuite/ld-mips-elf/tlsdyn-o32-3.d   |  4 +-
 ld/testsuite/ld-mips-elf/tlsdyn-o32-3.got |  2 +-
 10 files changed, 53 insertions(+), 51 deletions(-)

diff --git a/bfd/elflink.c b/bfd/elflink.c
index b316149..3860891 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -1496,15 +1496,10 @@ _bfd_elf_merge_symbol (bfd *abfd,
      treated as strong if the new symbol is from a dynamic library.
      This reflects the way glibc's ld.so works.
 
-     Also allow a weak symbol to override a linker script symbol
-     defined by an early pass over the script.  This is done so the
-     linker knows the symbol is defined in an object file, for the
-     DEFINED script function.
-
      Do this before setting *type_change_ok or *size_change_ok so that
      we warn properly when dynamic library symbols are overridden.  */
 
-  if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
+  if (newdef && !newdyn && olddyn)
     newweak = FALSE;
   if (olddef && newdyn)
     oldweak = FALSE;
diff --git a/bfd/linker.c b/bfd/linker.c
index 6b4c8e5..8d51c2a 100644
--- a/bfd/linker.c
+++ b/bfd/linker.c
@@ -1449,14 +1449,9 @@ _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
   do
     {
       enum link_action action;
-      int prev;
 
-      prev = h->type;
-      /* Treat symbols defined by early linker script pass as undefined.  */
-      if (h->ldscript_def)
-	prev = bfd_link_hash_undefined;
       cycle = FALSE;
-      action = link_action[(int) row][prev];
+      action = link_action[(int) row][(int) h->type];
       switch (action)
 	{
 	case FAIL:
@@ -1500,7 +1495,6 @@ _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
 	    h->u.def.section = section;
 	    h->u.def.value = value;
 	    h->linker_def = 0;
-	    h->ldscript_def = 0;
 
 	    /* If we have been asked to, we act like collect2 and
 	       identify all functions that might be global
@@ -1600,7 +1594,6 @@ _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
 	  else
 	    h->u.c.p->section = section;
 	  h->linker_def = 0;
-	  h->ldscript_def = 0;
 	  break;
 
 	case REF:
diff --git a/ld/ldexp.c b/ld/ldexp.c
index 6fa251e..d46a06f 100644
--- a/ld/ldexp.c
+++ b/ld/ldexp.c
@@ -60,12 +60,15 @@ struct definedness_hash_entry
      section statement, the section we'd like it relative to.  */
   asection *final_sec;
 
-  /* Low bits of iteration count.  Symbols with matching iteration have
-     been defined in this pass over the script.  */
-  unsigned int iteration : 8;
-
   /* Symbol was defined by an object file.  */
   unsigned int by_object : 1;
+
+  /* Symbols was defined by a script.  */
+  unsigned int by_script : 1;
+
+  /* Low bit of iteration count.  Symbols with matching iteration have
+     been defined in this pass over the script.  */
+  unsigned int iteration : 1;
 };
 
 static struct bfd_hash_table definedness_table;
@@ -283,6 +286,7 @@ definedness_newfunc (struct bfd_hash_entry *entry,
     einfo (_("%F%P: bfd_hash_allocate failed creating symbol %s\n"), name);
 
   ret->by_object = 0;
+  ret->by_script = 0;
   ret->iteration = 0;
   return &ret->root;
 }
@@ -316,7 +320,7 @@ update_definedness (const char *name, struct bfd_link_hash_entry *h)
   /* If the symbol was already defined, and not by a script, then it
      must be defined by an object file or by the linker target code.  */
   ret = TRUE;
-  if (!h->ldscript_def
+  if (!defentry->by_script
       && (h->type == bfd_link_hash_defined
 	  || h->type == bfd_link_hash_defweak
 	  || h->type == bfd_link_hash_common))
@@ -328,6 +332,7 @@ update_definedness (const char *name, struct bfd_link_hash_entry *h)
 	ret = FALSE;
     }
 
+  defentry->by_script = 1;
   defentry->iteration = lang_statement_iteration;
   defentry->final_sec = bfd_abs_section_ptr;
   if (expld.phase == lang_final_phase_enum
@@ -681,9 +686,6 @@ fold_trinary (etree_type *tree)
 static void
 fold_name (etree_type *tree)
 {
-  struct bfd_link_hash_entry *h;
-  struct definedness_hash_entry *def;
-
   memset (&expld.result, 0, sizeof (expld.result));
 
   switch (tree->type.node_code)
@@ -701,18 +703,23 @@ fold_name (etree_type *tree)
       break;
 
     case DEFINED:
-      h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
-					&link_info,
-					tree->name.name,
-					FALSE, FALSE, TRUE);
-      new_number (h != NULL
-		  && (h->type == bfd_link_hash_defined
-		      || h->type == bfd_link_hash_defweak
-		      || h->type == bfd_link_hash_common)
-		  && (!h->ldscript_def
-		      || (def = symbol_defined (tree->name.name)) == NULL
-		      || def->by_object
-		      || def->iteration == (lang_statement_iteration & 255)));
+      if (expld.phase != lang_first_phase_enum)
+	{
+	  struct bfd_link_hash_entry *h;
+	  struct definedness_hash_entry *def;
+
+	  h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
+					    &link_info,
+					    tree->name.name,
+					    FALSE, FALSE, TRUE);
+	  new_number (h != NULL
+		      && (h->type == bfd_link_hash_defined
+			  || h->type == bfd_link_hash_defweak
+			  || h->type == bfd_link_hash_common)
+		      && ((def = symbol_defined (tree->name.name)) == NULL
+			  || def->by_object
+			  || def->iteration == (lang_statement_iteration & 1)));
+	}
       break;
 
     case NAME:
@@ -721,6 +728,9 @@ fold_name (etree_type *tree)
 	{
 	  /* Self-assignment is only allowed for absolute symbols
 	     defined in a linker script.  */
+	  struct bfd_link_hash_entry *h;
+	  struct definedness_hash_entry *def;
+
 	  h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
 					    &link_info,
 					    tree->name.name,
@@ -730,13 +740,17 @@ fold_name (etree_type *tree)
 		    || h->type == bfd_link_hash_defweak)
 		&& h->u.def.section == bfd_abs_section_ptr
 		&& (def = symbol_defined (tree->name.name)) != NULL
-		&& def->iteration == (lang_statement_iteration & 255)))
+		&& def->iteration == (lang_statement_iteration & 1)))
 	    expld.assign_name = NULL;
 	}
-      if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
+      if (expld.phase == lang_first_phase_enum)
+	;
+      else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
 	new_rel_from_abs (expld.dot);
       else
 	{
+	  struct bfd_link_hash_entry *h;
+
 	  h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
 					    &link_info,
 					    tree->name.name,
@@ -751,7 +765,7 @@ fold_name (etree_type *tree)
 	      output_section = h->u.def.section->output_section;
 	      if (output_section == NULL)
 		{
-		  if (expld.phase <= lang_mark_phase_enum)
+		  if (expld.phase == lang_mark_phase_enum)
 		    new_rel (h->u.def.value, h->u.def.section);
 		  else
 		    einfo (_("%X%P:%pS: unresolvable symbol `%s'"
@@ -943,12 +957,12 @@ is_sym_value (const etree_type *tree, bfd_vma val)
   return (tree->type.node_class == etree_name
 	  && tree->type.node_code == NAME
 	  && (def = symbol_defined (tree->name.name)) != NULL
-	  && def->iteration == (lang_statement_iteration & 255)
+	  && def->by_script
+	  && def->iteration == (lang_statement_iteration & 1)
 	  && (h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
 						&link_info,
 						tree->name.name,
 						FALSE, FALSE, TRUE)) != NULL
-	  && h->ldscript_def
 	  && h->type == bfd_link_hash_defined
 	  && h->u.def.section == bfd_abs_section_ptr
 	  && h->u.def.value == val);
diff --git a/ld/ldlang.c b/ld/ldlang.c
index 350baf2..7e12480 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -3360,7 +3360,9 @@ open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode)
 #endif
 	  break;
 	case lang_assignment_statement_enum:
-	  if (s->assignment_statement.exp->type.node_class != etree_assert)
+	  if (s->assignment_statement.exp->type.node_class != etree_assert
+	      && s->assignment_statement.exp->assign.defsym)
+	    /* This is from a --defsym on the command line.  */
 	    exp_fold_tree_no_dot (s->assignment_statement.exp);
 	  break;
 	default:
@@ -7176,7 +7178,6 @@ lang_process (void)
 
   /* Create a bfd for each input file.  */
   current_target = default_target;
-  lang_statement_iteration++;
   open_input_bfds (statement_list.head, OPEN_BFD_NORMAL);
 
 #ifdef ENABLE_PLUGINS
@@ -7232,7 +7233,6 @@ lang_process (void)
 
 	  /* Rescan archives in case new undefined symbols have appeared.  */
 	  files = file_chain;
-	  lang_statement_iteration++;
 	  open_input_bfds (statement_list.head, OPEN_BFD_RESCAN);
 	  lang_list_remove_tail (&file_chain, &files);
 	  while (files.head != NULL)
diff --git a/ld/testsuite/ld-mips-elf/tlsdyn-o32-1.d b/ld/testsuite/ld-mips-elf/tlsdyn-o32-1.d
index 17e42d0..011df6c 100644
--- a/ld/testsuite/ld-mips-elf/tlsdyn-o32-1.d
+++ b/ld/testsuite/ld-mips-elf/tlsdyn-o32-1.d
@@ -5,7 +5,7 @@ Disassembly of section .text:
 
 .* <__start>:
   .*:	3c1c0fc0 	lui	gp,0xfc0
-  .*:	279c7b80 	addiu	gp,gp,31616
+  .*:	279c7c30 	addiu	gp,gp,31792
   .*:	0399e021 	addu	gp,gp,t9
   .*:	27bdfff0 	addiu	sp,sp,-16
   .*:	afbe0008 	sw	s8,8\(sp\)
@@ -55,7 +55,7 @@ Disassembly of section .text:
 
 .* <other>:
   .*:	3c1c0fc0 	lui	gp,0xfc0
-  .*:	279c7ac0 	addiu	gp,gp,31424
+  .*:	279c7b70 	addiu	gp,gp,31600
   .*:	0399e021 	addu	gp,gp,t9
   .*:	27bdfff0 	addiu	sp,sp,-16
   .*:	afbe0008 	sw	s8,8\(sp\)
diff --git a/ld/testsuite/ld-mips-elf/tlsdyn-o32-1.got b/ld/testsuite/ld-mips-elf/tlsdyn-o32-1.got
index 508fed2..1dbcab4 100644
--- a/ld/testsuite/ld-mips-elf/tlsdyn-o32-1.got
+++ b/ld/testsuite/ld-mips-elf/tlsdyn-o32-1.got
@@ -13,6 +13,6 @@ OFFSET   TYPE              VALUE
 
 
 Contents of section .got:
- 10000020 00000000 80000000 0040053c 00000000  .........@......
+ 10000020 00000000 80000000 0040048c 00000000  .........@......
  10000030 00000000 00000000 00000000 00000000  ................
  10000040 00000000 00000001 00000000           ............    
diff --git a/ld/testsuite/ld-mips-elf/tlsdyn-o32-2.d b/ld/testsuite/ld-mips-elf/tlsdyn-o32-2.d
index 17e42d0..011df6c 100644
--- a/ld/testsuite/ld-mips-elf/tlsdyn-o32-2.d
+++ b/ld/testsuite/ld-mips-elf/tlsdyn-o32-2.d
@@ -5,7 +5,7 @@ Disassembly of section .text:
 
 .* <__start>:
   .*:	3c1c0fc0 	lui	gp,0xfc0
-  .*:	279c7b80 	addiu	gp,gp,31616
+  .*:	279c7c30 	addiu	gp,gp,31792
   .*:	0399e021 	addu	gp,gp,t9
   .*:	27bdfff0 	addiu	sp,sp,-16
   .*:	afbe0008 	sw	s8,8\(sp\)
@@ -55,7 +55,7 @@ Disassembly of section .text:
 
 .* <other>:
   .*:	3c1c0fc0 	lui	gp,0xfc0
-  .*:	279c7ac0 	addiu	gp,gp,31424
+  .*:	279c7b70 	addiu	gp,gp,31600
   .*:	0399e021 	addu	gp,gp,t9
   .*:	27bdfff0 	addiu	sp,sp,-16
   .*:	afbe0008 	sw	s8,8\(sp\)
diff --git a/ld/testsuite/ld-mips-elf/tlsdyn-o32-2.got b/ld/testsuite/ld-mips-elf/tlsdyn-o32-2.got
index 4a97099..fb50635 100644
--- a/ld/testsuite/ld-mips-elf/tlsdyn-o32-2.got
+++ b/ld/testsuite/ld-mips-elf/tlsdyn-o32-2.got
@@ -13,6 +13,6 @@ OFFSET   TYPE              VALUE
 
 
 Contents of section .got:
- 10000020 00000000 80000000 0040053c 00000000  .*
+ 10000020 00000000 80000000 0040048c 00000000  .*
  10000030 00000000 00000000 00000000 00000000  .*
  10000040 00000000 00000001 00000000           .*
diff --git a/ld/testsuite/ld-mips-elf/tlsdyn-o32-3.d b/ld/testsuite/ld-mips-elf/tlsdyn-o32-3.d
index fb3750a..3828aca 100644
--- a/ld/testsuite/ld-mips-elf/tlsdyn-o32-3.d
+++ b/ld/testsuite/ld-mips-elf/tlsdyn-o32-3.d
@@ -5,7 +5,7 @@ Disassembly of section .text:
 
 .* <other>:
   .*:	3c1c0fc0 	lui	gp,0xfc0
-  .*:	279c7b80 	addiu	gp,gp,31616
+  .*:	279c7c30 	addiu	gp,gp,31792
   .*:	0399e021 	addu	gp,gp,t9
   .*:	27bdfff0 	addiu	sp,sp,-16
   .*:	afbe0008 	sw	s8,8\(sp\)
@@ -51,7 +51,7 @@ Disassembly of section .text:
 
 .* <__start>:
   .*:	3c1c0fc0 	lui	gp,0xfc0
-  .*:	279c7ad0 	addiu	gp,gp,31440
+  .*:	279c7b80 	addiu	gp,gp,31616
   .*:	0399e021 	addu	gp,gp,t9
   .*:	27bdfff0 	addiu	sp,sp,-16
   .*:	afbe0008 	sw	s8,8\(sp\)
diff --git a/ld/testsuite/ld-mips-elf/tlsdyn-o32-3.got b/ld/testsuite/ld-mips-elf/tlsdyn-o32-3.got
index d96375c..4a97099 100644
--- a/ld/testsuite/ld-mips-elf/tlsdyn-o32-3.got
+++ b/ld/testsuite/ld-mips-elf/tlsdyn-o32-3.got
@@ -13,6 +13,6 @@ OFFSET   TYPE              VALUE
 
 
 Contents of section .got:
- 10000020 00000000 80000000 004005ec 00000000  .*
+ 10000020 00000000 80000000 0040053c 00000000  .*
  10000030 00000000 00000000 00000000 00000000  .*
  10000040 00000000 00000001 00000000           .*
-- 
2.7.4

