binutils: backport an upstream patch to fix gold link crash

This backports the upstream patch
From 635aa30e3ae9735e362cfd1cda2be9f7b65b32a2 Mon Sep 17 00:00:00 2001
From: Cary Coutant <ccoutant@google.com>
Date: Tue, 23 Sep 2014 23:18:19 -0700
Subject: [PATCH] Fix race condition causing assert in
 Eh_frame_hdr::do_sized_write().

BUG=chromium:456287
TEST=cbuildbot chromiumos-sdk lumpy-release daisy-release
     build chrome with thread-enabled binutils.

Change-Id: Icbc4a603808612d9bae6c52d47e72d0a3d720eab
Reviewed-on: https://chromium-review.googlesource.com/247370
Reviewed-by: Luis Lozano <llozano@chromium.org>
Commit-Queue: Yunlian Jiang <yunlian@gmail.com>
Tested-by: Yunlian Jiang <yunlian@gmail.com>
diff --git a/gold/gold.cc b/gold/gold.cc
index 4de9289..7c72465 100644
--- a/gold/gold.cc
+++ b/gold/gold.cc
@@ -809,6 +809,8 @@
   if (!any_postprocessing_sections)
     {
       input_sections_blocker = new Task_token(true);
+      // Write_symbols_task, Relocate_tasks.
+      input_sections_blocker->add_blocker();
       input_sections_blocker->add_blockers(input_objects->number_of_relobjs());
     }
 
@@ -837,6 +839,7 @@
 
   // Queue a task to write out the output sections.
   workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker,
+					   input_sections_blocker,
 					   final_blocker));
 
   // Queue a task to write out everything else.
diff --git a/gold/layout.cc b/gold/layout.cc
index 6c2e0af..295bc9b 100644
--- a/gold/layout.cc
+++ b/gold/layout.cc
@@ -5521,6 +5521,8 @@
 Write_sections_task::locks(Task_locker* tl)
 {
   tl->add(this, this->output_sections_blocker_);
+  if (this->input_sections_blocker_ != NULL)
+    tl->add(this, this->input_sections_blocker_);
   tl->add(this, this->final_blocker_);
 }
 
diff --git a/gold/layout.h b/gold/layout.h
index 792b748..a8d1adf 100644
--- a/gold/layout.h
+++ b/gold/layout.h
@@ -1455,9 +1455,11 @@
  public:
   Write_sections_task(const Layout* layout, Output_file* of,
 		      Task_token* output_sections_blocker,
+		      Task_token* input_sections_blocker,
 		      Task_token* final_blocker)
     : layout_(layout), of_(of),
       output_sections_blocker_(output_sections_blocker),
+      input_sections_blocker_(input_sections_blocker),
       final_blocker_(final_blocker)
   { }
 
@@ -1482,6 +1484,7 @@
   const Layout* layout_;
   Output_file* of_;
   Task_token* output_sections_blocker_;
+  Task_token* input_sections_blocker_;
   Task_token* final_blocker_;
 };
 
diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am
index 4aac2e9..a507467 100644
--- a/gold/testsuite/Makefile.am
+++ b/gold/testsuite/Makefile.am
@@ -2314,10 +2314,9 @@
 if DEFAULT_TARGET_X86_64
 check_PROGRAMS += exception_x86_64_bnd_test
 exception_x86_64_bnd_test_SOURCES = exception_test_main.cc
-exception_x86_64_bnd_test_DEPENDENCIES = exception_x86_64_bnd_1.o \
-					 exception_x86_64_bnd_2.o
+exception_x86_64_bnd_test_DEPENDENCIES = gcctestdir/ld exception_x86_64_bnd_1.o exception_x86_64_bnd_2.o
 exception_x86_64_bnd_test_LDFLAGS = $(exception_test_LDFLAGS)
-exception_x86_64_bnd_test_LDADD = $(exception_x86_64_bnd_test_DEPENDENCIES)
+exception_x86_64_bnd_test_LDADD = exception_x86_64_bnd_1.o exception_x86_64_bnd_2.o
 exception_x86_64_bnd_1.o: exception_test_1.cc gcctestdir/as
 	$(CXXCOMPILE) -c -fpic -Bgcctestdir/ -Wa,-madd-bnd-prefix -o $@ $<
 exception_x86_64_bnd_2.o: exception_test_2.cc gcctestdir/as
diff --git a/gold/testsuite/Makefile.in b/gold/testsuite/Makefile.in
index 59cb746..610c1e0 100644
--- a/gold/testsuite/Makefile.in
+++ b/gold/testsuite/Makefile.in
@@ -2637,11 +2637,10 @@
 @GCC_TRUE@@IFUNC_TRUE@@TEST_AS_NATIVE_LINKER_TRUE@ifuncvar_LDFLAGS = -Bgcctestdir/ -Wl,-R,.
 @GCC_TRUE@@IFUNC_TRUE@@TEST_AS_NATIVE_LINKER_TRUE@ifuncvar_LDADD = ifuncvar.so
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@TEST_AS_NATIVE_LINKER_TRUE@exception_x86_64_bnd_test_SOURCES = exception_test_main.cc
-@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@TEST_AS_NATIVE_LINKER_TRUE@exception_x86_64_bnd_test_DEPENDENCIES = exception_x86_64_bnd_1.o \
-@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@TEST_AS_NATIVE_LINKER_TRUE@					 exception_x86_64_bnd_2.o
 
+@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@TEST_AS_NATIVE_LINKER_TRUE@exception_x86_64_bnd_test_DEPENDENCIES = gcctestdir/ld exception_x86_64_bnd_1.o exception_x86_64_bnd_2.o
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@TEST_AS_NATIVE_LINKER_TRUE@exception_x86_64_bnd_test_LDFLAGS = $(exception_test_LDFLAGS)
-@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@TEST_AS_NATIVE_LINKER_TRUE@exception_x86_64_bnd_test_LDADD = $(exception_x86_64_bnd_test_DEPENDENCIES)
+@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@TEST_AS_NATIVE_LINKER_TRUE@exception_x86_64_bnd_test_LDADD = exception_x86_64_bnd_1.o exception_x86_64_bnd_2.o
 @DEFAULT_TARGET_I386_TRUE@@NATIVE_OR_CROSS_LINKER_TRUE@SPLIT_DEFSYMS = --defsym __morestack=0x100 --defsym __morestack_non_split=0x200
 @DEFAULT_TARGET_X86_64_TRUE@@NATIVE_OR_CROSS_LINKER_TRUE@SPLIT_DEFSYMS = --defsym __morestack=0x100 --defsym __morestack_non_split=0x200
 all: $(BUILT_SOURCES)