diff --git a/gold/ChangeLog b/gold/ChangeLog
index 1883cc36c90113b18d916f7870a38225767392d6..d6bae2bd6a73d8f7996c1062dc95351159cecd03 100644
--- a/gold/ChangeLog
+++ b/gold/ChangeLog
@@ -1,3 +1,8 @@
+2016-06-28  Igor Kudrin  <ikudrin@accesssoftek.com>
+
+	* aarch64.cc (Target_aarch64::Scan::local): Move the call to got_section
+	from the top level to the places of its use.
+
 2016-06-28  Igor Kudrin  <ikudrin@accesssoftek.com>
 
 	PR gold/18098
diff --git a/gold/aarch64.cc b/gold/aarch64.cc
index 75e4177c7e935cdc1a4ea9d027e0874bb546ecc8..3d03c11ce5a568a4c640f7629bed47a38805a080 100644
--- a/gold/aarch64.cc
+++ b/gold/aarch64.cc
@@ -5958,8 +5958,6 @@ Target_aarch64<size, big_endian>::Scan::local(
 
   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
       Reloc_section;
-  Output_data_got_aarch64<size, big_endian>* got =
-      target->got_section(symtab, layout);
   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
 
   // A local STT_GNU_IFUNC symbol may require a PLT entry.
@@ -6004,6 +6002,8 @@ Target_aarch64<size, big_endian>::Scan::local(
     case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
       // The above relocations are used to access GOT entries.
       {
+	Output_data_got_aarch64<size, big_endian>* got =
+	    target->got_section(symtab, layout);
 	bool is_new = false;
 	// This symbol requires a GOT entry.
 	if (is_ifunc)
@@ -6056,6 +6056,8 @@ Target_aarch64<size, big_endian>::Scan::local(
 	// Create a GOT entry for the tp-relative offset.
 	if (!parameters->doing_static_link())
 	  {
+	    Output_data_got_aarch64<size, big_endian>* got =
+		target->got_section(symtab, layout);
 	    got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
 				    target->rela_dyn_section(layout),
 				    elfcpp::R_AARCH64_TLS_TPREL64);
@@ -6063,6 +6065,8 @@ Target_aarch64<size, big_endian>::Scan::local(
 	else if (!object->local_has_got_offset(r_sym,
 					       GOT_TYPE_TLS_OFFSET))
 	  {
+	    Output_data_got_aarch64<size, big_endian>* got =
+		target->got_section(symtab, layout);
 	    got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
 	    unsigned int got_offset =
 		object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
@@ -6086,6 +6090,8 @@ Target_aarch64<size, big_endian>::Scan::local(
 	  }
 	gold_assert(tlsopt == tls::TLSOPT_NONE);
 
+	Output_data_got_aarch64<size, big_endian>* got =
+	    target->got_section(symtab, layout);
 	got->add_local_pair_with_rel(object,r_sym, data_shndx,
 				     GOT_TYPE_TLS_PAIR,
 				     target->rela_dyn_section(layout),