Re: PR28827, assertion building LLVM 9 on powerpc64le-linux-gnu

In trying to find a testcase for PR28827, I managed to hit a linker
error in bfd_set_section_contents with a .branch_lt input section
being too large for the output .branch_lt.

bfd/
	PR 28827
	* elf64-ppc.c (ppc64_elf_size_stubs): Set section size to
	maxsize past STUB_SHRINK_ITER before laying out.  Remove now
	unnecessary conditional setting of maxsize at start of loop.
ld/
	* testsuite/ld-powerpc/pr28827-2.d,
	* testsuite/ld-powerpc/pr28827-2.lnk,
	* testsuite/ld-powerpc/pr28827-2.s: New test.
	* testsuite/ld-powerpc/powerpc.exp: Run it.
diff --git a/bfd/elf64-ppc.c b/bfd/elf64-ppc.c
index d53128d..b4fa4ae 100644
--- a/bfd/elf64-ppc.c
+++ b/bfd/elf64-ppc.c
@@ -14093,10 +14093,7 @@
 	    {
 	      asection *stub_sec = group->stub_sec;
 
-	      if (htab->stub_iteration <= STUB_SHRINK_ITER
-		  || stub_sec->rawsize < stub_sec->size)
-		/* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
-		stub_sec->rawsize = stub_sec->size;
+	      stub_sec->rawsize = stub_sec->size;
 	      stub_sec->size = 0;
 	      stub_sec->reloc_count = 0;
 	      stub_sec->flags &= ~SEC_RELOC;
@@ -14111,9 +14108,7 @@
 	  htab->tga_group->stub_sec->size = 24 * 4;
 	}
 
-      if (htab->stub_iteration <= STUB_SHRINK_ITER
-	  || htab->brlt->rawsize < htab->brlt->size)
-	htab->brlt->rawsize = htab->brlt->size;
+      htab->brlt->rawsize = htab->brlt->size;
       htab->brlt->size = 0;
       htab->brlt->reloc_count = 0;
       htab->brlt->flags &= ~SEC_RELOC;
@@ -14122,9 +14117,7 @@
 
       if (htab->elf.srelrdyn != NULL)
 	{
-	  if (htab->stub_iteration <= STUB_SHRINK_ITER
-	      || htab->elf.srelrdyn->rawsize < htab->elf.srelrdyn->size)
-	    htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
+	  htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
 	  htab->elf.srelrdyn->size = 0;
 	}
 
@@ -14240,6 +14233,21 @@
 	      || htab->stub_iteration > 1))
 	break;
 
+      if (htab->stub_iteration > STUB_SHRINK_ITER)
+	{
+	  for (group = htab->group; group != NULL; group = group->next)
+	    if (group->stub_sec != NULL
+		&& group->stub_sec->size < group->stub_sec->rawsize)
+	      group->stub_sec->size = group->stub_sec->rawsize;
+
+	  if (htab->brlt->size < htab->brlt->rawsize)
+	    htab->brlt->size = htab->brlt->rawsize;
+
+	  if (htab->elf.srelrdyn != NULL
+	      && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
+	    htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
+	}
+
       /* Ask the linker to do its stuff.  */
       (*htab->params->layout_sections_again) ();
     }
diff --git a/ld/testsuite/ld-powerpc/powerpc.exp b/ld/testsuite/ld-powerpc/powerpc.exp
index 3eb707f..3d738f5 100644
--- a/ld/testsuite/ld-powerpc/powerpc.exp
+++ b/ld/testsuite/ld-powerpc/powerpc.exp
@@ -445,6 +445,7 @@
     run_dump_test "tlsie"
     run_dump_test "non-contiguous-powerpc64"
     run_dump_test "tprel"
+    run_dump_test "pr28827-2"
 }
 
 run_dump_test "localgot"
diff --git a/ld/testsuite/ld-powerpc/pr28827-2.d b/ld/testsuite/ld-powerpc/pr28827-2.d
new file mode 100644
index 0000000..8da819d
--- /dev/null
+++ b/ld/testsuite/ld-powerpc/pr28827-2.d
@@ -0,0 +1,48 @@
+#as: -a64
+#ld: -melf64ppc --plt-align=0 -T pr28827-2.lnk
+#objdump: -dr
+
+.*:     file format .*
+
+Disassembly of section \.text:
+
+.*:
+.*:	(49 ff ff f0|f0 ff ff 49) 	b       .* <far1>
+	\.\.\.
+
+.* <.*\.plt_branch\..*>:
+.*:	(e9 82 80 28|28 80 82 e9) 	ld      r12,-32728\(r2\)
+.*:	(7d 89 03 a6|a6 03 89 7d) 	mtctr   r12
+.*:	(4e 80 04 20|20 04 80 4e) 	bctr
+
+.* <_start>:
+.*:	(49 ff ff d8|d8 ff ff 49) 	b       .* <far1>
+.*:	(4b ff ff f0|f0 ff ff 4b) 	b       .*
+
+Disassembly of section \.far1:
+
+.*:
+.*:	(4a 00 00 38|38 00 00 4a) 	b       .* <_start>
+
+.* <.*\.long_branch\..*>:
+.*:	(49 ff ff f4|f4 ff ff 49) 	b       .* <far2>
+	\.\.\.
+
+.* <far1>:
+.*:	(41 82 ff f4|f4 ff 82 41) 	beq     .*
+.*:	(4a 00 00 24|24 00 00 4a) 	b       .* <_start>
+
+Disassembly of section \.far2:
+
+.*:
+.*:	(e9 82 80 20|20 80 82 e9) 	ld      r12,-32736\(r2\)
+.*:	(7d 89 03 a6|a6 03 89 7d) 	mtctr   r12
+.*:	(4e 80 04 20|20 04 80 4e) 	bctr
+
+.*:
+.*:	(4a 00 00 24|24 00 00 4a) 	b       .* <far1>
+	\.\.\.
+
+.* <far2>:
+.*:	(40 82 ff f4|f4 ff 82 40) 	bne     .*
+.*:	(4b ff ff e4|e4 ff ff 4b) 	b       .*
diff --git a/ld/testsuite/ld-powerpc/pr28827-2.lnk b/ld/testsuite/ld-powerpc/pr28827-2.lnk
new file mode 100644
index 0000000..61a8ca2
--- /dev/null
+++ b/ld/testsuite/ld-powerpc/pr28827-2.lnk
@@ -0,0 +1,9 @@
+SECTIONS
+{
+  . = SIZEOF_HEADERS;
+  .text : { *(.text) }
+  . = . + 0x2000000 + 32 - 4 * SIZEOF (.text);
+  .far1 : { *(.far1) }
+  . = . + 0x2000000 + 32 - 4 * SIZEOF (.far1);
+  .far2 : { *(.far2) }
+}
diff --git a/ld/testsuite/ld-powerpc/pr28827-2.s b/ld/testsuite/ld-powerpc/pr28827-2.s
new file mode 100644
index 0000000..a628d6d
--- /dev/null
+++ b/ld/testsuite/ld-powerpc/pr28827-2.s
@@ -0,0 +1,15 @@
+ .globl _start
+ .text
+_start:
+ b far1
+ b far2
+
+ .section .far1,"ax",@progbits
+far1:
+ beq far2
+ b _start
+
+ .section .far2,"ax",@progbits
+far2:
+ bne far1
+ b _start