From 7e4588997ebdddefc48d8be2d51a715822817c0e Mon Sep 17 00:00:00 2001
From: Claudiu Zissulescu <claziss@synopsys.com>
Date: Mon, 29 Feb 2016 16:07:48 +0100
Subject: [PATCH] [ARC] General fixes.

bfd/
2016-02-29  Cupertino Miranda <Cupertino.Miranda@synopsys.com>

	* elf32-arc.c (arc_elf_final_write_processing): Add condition to
	the flag change.
        (elf_arc_relocate_section): Fixes and conditions to support PIE.
	Assert for code sections dynamic relocs.

gas/
2016-02-29  Claudiu Zissulescu  <Claudiu.Zissulescu@synopsys.com>

	* config/tc-arc.c: Enable code density instructions for ARC EM.

ld/
2016-02-29  Cupertino Miranda  <Cupertino.Miranda@synopsys.com>

	* scripttempl/arclinux.sc: Force .tdata and .tbss to always be
	generated.
---
 bfd/ChangeLog              |  7 +++++++
 bfd/elf32-arc.c            | 28 ++++++++++++++++++++++------
 gas/ChangeLog              |  4 ++++
 gas/config/tc-arc.c        |  2 +-
 ld/ChangeLog               |  5 +++++
 ld/scripttempl/arclinux.sc |  4 ++--
 6 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 8339eae9b32..5d39624b7db 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,10 @@
+2016-02-29  Cupertino Miranda <Cupertino.Miranda@synopsys.com>
+
+	* elf32-arc.c (arc_elf_final_write_processing): Add condition to
+	the flag change.
+        (elf_arc_relocate_section): Fixes and conditions to support PIE.
+	Assert for code sections dynamic relocs.
+
 2016-02-26  Renlin Li  <renlin.li@arm.com>
 
 	* elfnn-aarch64.c (elfNN_aarch64_howto_table): Fix signed overflow
diff --git a/bfd/elf32-arc.c b/bfd/elf32-arc.c
index 8ffc23ad649..9993478742b 100644
--- a/bfd/elf32-arc.c
+++ b/bfd/elf32-arc.c
@@ -688,10 +688,11 @@ arc_elf_final_write_processing (bfd * abfd,
     default:
       abort ();
     }
+  if ((elf_elfheader (abfd)->e_flags & EF_ARC_MACH) == EF_ARC_CPU_GENERIC)
+    elf_elfheader (abfd)->e_flags |= val;
 
-  elf_elfheader (abfd)->e_flags &= ~EF_ARC_MACH;
-  elf_elfheader (abfd)->e_flags |= val;
   elf_elfheader (abfd)->e_machine = emf;
+
   /* Record whatever is the current syscall ABI version.  */
   elf_elfheader (abfd)->e_flags |= E_ARC_OSABI_CURRENT;
 }
@@ -1319,14 +1320,24 @@ elf_arc_relocate_section (bfd *		   output_bfd,
 		}
 	      else if (is_reloc_for_PLT (howto))
 		{
+		  /* Fail if it is linking for PIE and the symbol is
+		     undefined.  */
+		  if (bfd_link_executable (info)
+		      && !(*info->callbacks->undefined_symbol)
+		        (info, h->root.root.string, input_bfd, input_section,
+		         rel->r_offset, TRUE))
+		    {
+		      return FALSE;
+		    }
 		  reloc_data.sym_value = h->plt.offset;
 		  reloc_data.sym_section = htab->splt;
 
 		  reloc_data.should_relocate = TRUE;
 		}
-	      else if (!(*info->callbacks->undefined_symbol)
+	      else if (!bfd_link_pic (info)
+		       && !(*info->callbacks->undefined_symbol)
 		       (info, h->root.root.string, input_bfd, input_section,
-			rel->r_offset,!bfd_link_pic (info)))
+			rel->r_offset, TRUE))
 		{
 		  return FALSE;
 		}
@@ -1443,7 +1454,7 @@ elf_arc_relocate_section (bfd *		   output_bfd,
 	  case R_ARC_32_ME:
 	  case R_ARC_PC32:
 	  case R_ARC_32_PCREL:
-	    if (bfd_link_pic (info)
+	    if (bfd_link_pic (info) && !bfd_link_pie (info)
 		&& ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
 		    || (h != NULL
 			&& h->dynindx != -1
@@ -1509,6 +1520,11 @@ elf_arc_relocate_section (bfd *		   output_bfd,
 		    else
 		      {
 			BFD_ASSERT (h->dynindx != -1);
+
+			/* This type of dynamic relocation cannot be created
+			   for code sections.  */
+			BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
+
 			if ((input_section->flags & SEC_ALLOC) != 0)
 			  relocate = FALSE;
 			else
@@ -1715,7 +1731,7 @@ elf_arc_check_relocs (bfd *		         abfd,
 	    /* FALLTHROUGH */
 	  case R_ARC_PC32:
 	  case R_ARC_32_PCREL:
-	    if (bfd_link_pic (info)
+	    if (bfd_link_pic (info) && !bfd_link_pie (info)
 		&& ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
 		    || (h != NULL
 			&& h->dynindx != -1
diff --git a/gas/ChangeLog b/gas/ChangeLog
index 5faa925ca94..18156551833 100644
--- a/gas/ChangeLog
+++ b/gas/ChangeLog
@@ -1,3 +1,7 @@
+2016-02-29  Claudiu Zissulescu  <Claudiu.Zissulescu@synopsys.com>
+
+	* config/tc-arc.c: Enable code density instructions for ARC EM.
+
 2016-02-26  H.J. Lu  <hongjiu.lu@intel.com>
 
 	PR ld/19645
diff --git a/gas/config/tc-arc.c b/gas/config/tc-arc.c
index 82cff9aebd8..70a47f9d4b1 100644
--- a/gas/config/tc-arc.c
+++ b/gas/config/tc-arc.c
@@ -343,7 +343,7 @@ static const struct cpu_type
   { "arc700", ARC_OPCODE_ARC700,  bfd_mach_arc_arc700,
     E_ARC_MACH_ARC700,  0x00},
   { "arcem",  ARC_OPCODE_ARCv2EM, bfd_mach_arc_arcv2,
-    EF_ARC_CPU_ARCV2EM, 0x00},
+    EF_ARC_CPU_ARCV2EM, ARC_CD},
   { "archs",  ARC_OPCODE_ARCv2HS, bfd_mach_arc_arcv2,
     EF_ARC_CPU_ARCV2HS, ARC_CD},
   { "all",    ARC_OPCODE_BASE,    bfd_mach_arc_arcv2,
diff --git a/ld/ChangeLog b/ld/ChangeLog
index 24a913fd275..e24a457e49c 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,3 +1,8 @@
+2016-02-29  Cupertino Miranda  <Cupertino.Miranda@synopsys.com>
+
+	* scripttempl/arclinux.sc: Force .tdata and .tbss to always be
+	generated.
+
 2016-02-26  Renlin Li  <renlin.li@arm.com>
 
 	* testsuite/ld-aarch64/aarch64-elf.exp: Run new testcases.
diff --git a/ld/scripttempl/arclinux.sc b/ld/scripttempl/arclinux.sc
index 9d43ca31cbb..bef5336fb7e 100644
--- a/ld/scripttempl/arclinux.sc
+++ b/ld/scripttempl/arclinux.sc
@@ -326,8 +326,8 @@ cat <<EOF
   .data1        ${RELOCATING-0} : { *(.data1) }
   /* TLS local dynamic uses .tdata as a reference point.  */
   ${RELOCATING+${CREATE_SHLIB+PROVIDE_HIDDEN (.tdata = .);}}
-  .tdata	${RELOCATING-0} : { *(.tdata${RELOCATING+ .tdata.* .gnu.linkonce.td.*}) }
-  .tbss		${RELOCATING-0} : { *(.tbss${RELOCATING+ .tbss.* .gnu.linkonce.tb.*})${RELOCATING+ *(.tcommon)} }
+  .tdata	${RELOCATING-0} : { PROVIDE_HIDDEN(.tdata = .); *(.tdata${RELOCATING+ .tdata.* .gnu.linkonce.td.*}) }
+  .tbss		${RELOCATING-0} : { PROVIDE_HIDDEN(.tbss = .); *(.tbss${RELOCATING+ .tbss.* .gnu.linkonce.tb.*})${RELOCATING+ *(.tcommon)} }
   .eh_frame     ${RELOCATING-0} : { KEEP (*(.eh_frame)) }
   .gcc_except_table ${RELOCATING-0} : { *(.gcc_except_table) }
   ${WRITABLE_RODATA+${RODATA}}