Cache the max alignment of output sections instead of scanning all
output sections for each input section, which can take a very long
time if there are millions of input/output sections.
	PR ld/22274
	* elfnn-riscv.c (riscv_elf_link_hash_table): Add max_alignment.
	(riscv_elf_link_hash_table_create): Initialize max_alignment to
	(bfd_vma) -1.
	(_bfd_riscv_relax_section): Cache the max alignment of output
	sections if possible.
+2017-10-09  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/22274
+       * elfnn-riscv.c (riscv_elf_link_hash_table): Add max_alignment.
+       (riscv_elf_link_hash_table_create): Initialize max_alignment to
+       (bfd_vma) -1.
+       (_bfd_riscv_relax_section): Cache the max alignment of output
+       sections if possible.
+
 2017-10-10  Alan Modra  <amodra@gmail.com>
 
        * elf64-ppc.c (ppc64_elf_before_check_relocs): Set sec_type for
 
 
   /* Small local sym to section mapping cache.  */
   struct sym_cache sym_cache;
+
+  /* The max alignment of output sections.  */
+  bfd_vma max_alignment;
 };
 
 
       return NULL;
     }
 
+  ret->max_alignment = (bfd_vma) -1;
   return &ret->elf.root;
 }
 
                                                 info->keep_memory)))
     goto fail;
 
-  max_alignment = _bfd_riscv_get_max_alignment (sec);
+  if (htab)
+    {
+      max_alignment = htab->max_alignment;
+      if (max_alignment == (bfd_vma) -1)
+       {
+         max_alignment = _bfd_riscv_get_max_alignment (sec);
+         htab->max_alignment = max_alignment;
+       }
+    }
+  else
+    max_alignment = _bfd_riscv_get_max_alignment (sec);
 
   /* Examine and consider relaxing each reloc.  */
   for (i = 0; i < sec->reloc_count; i++)