0

Toolchain: GCC for Renesas RX 14.2.0.202607-GNURX, the macOS package.

rx-elf-gcc (GCC_Build_482d13c99) 14.2.0.202607-GNURX 20240801
GNU ld (Renesas RX GNU Toolchain 14.2.0.202607 (d43638649)) 2.44

I only ran the macOS package. The code is in bfd/elf32-rx.c, so I expect the other hosts to do the same.

The problem

With -O2 or -O3 and -mrelax, the linker makes programs that are wrong, or it never finishes.

-O2 and -O3 align the targets of branches, so the code has padding in it. The relaxation in the linker does not handle that padding right. -Os has no padding and is fine.

There are three separate faults. Two small programs that show them, aligned.c and forever.c, are at the end of this post with the patches.

1. A branch goes to the wrong place

rx-elf-gcc -O2 -mrelax -msim aligned.c -o aligned.elf
rx-elf-objdump -d aligned.elf
01000163 <_pass_on>:
 1000163:  71 12 00    add    #0, r1, r2
 1000166:  18          bne.s  100016e <.L7+0x6>
 1000167:  02          rts

01000168 <.L7>:
 1000168:  7e 22       abs    r2
 100016a:  2e ee       bra.b  1000158 <_check>

The bne should go to .L7. It goes 6 bytes past it. The program never ends in the simulator. Without -mrelax it returns 0.

What happens: there are 6 bytes of padding between rts and .L7. The linker makes bne.b into bne.s, which reaches 3 to 10 bytes ahead. Then it takes the padding out, and .L7 is 2 bytes ahead, which bne.s cannot say. The R_RX_DIR3U_PCREL relocation is then skipped without an error, so the instruction keeps the distance it had.

elf32_rx_relax_section has a check for branches over an alignment, but its condition can never be true. It asks for a target before the previous alignment and after the next one. It should be one or the other.

2. A NOP in the padding is cut in half

gcc.c-torture/execute/bitops-1.c from the GCC test suite shows it.

rx-elf-gcc -O2 -mrelax -msim bitops-1.c -o bitops.elf
rx-elf-objdump -d bitops.elf
 100022b:  21 ad       bne.b  10001d8 <.L12>
 100022d:  Address 0x1000230 is out of bounds.

01000230 <.L13>:
 1000230:  66 01       mov.l  #0, r1

The bytes at 100022d are 77 10 01. That is the start of the 5 byte NOP 77 10 01 00 00 (mul #1, r0). The padding was 5 bytes, grew when the code before it got shorter, and then the linker took 8 bytes off the end of it. The 3 bytes that are left run together with the next 2 as mul #0x016601, r0. That changes the stack pointer and skips the mov.l #0, r1.

3. The link never finishes

rx-elf-gcc -O2 -mrelax -msim forever.c -o forever.elf

ld runs until it is killed. The function ends in for (;;) {} after a loop, so the compiler puts two .balign in a row:

        .balign 8,3,7
.L2:
        .balign 8,3,7
.L4:
        bra     .L4

Two things go wrong with two alignments at one address. reloc_bubblesort swaps the two end markers for ever. With that fixed, the walk in elf32_rx_relax_section goes past the last relocation, because next_alignment += 2 assumes one end marker after every start marker.

GCC test suite

These programs of gcc.c-torture/execute fail with the package as it is. All of them pass without -mrelax.

Wrong result with -O2 -mrelax:
20230509-1, bitops-1, builtin-bitops-1, pr53645-2, pr64255, pr84339, pr84478, pr87290, pr95731, strlen-5

Link never finishes with -O2 -mrelax:
pr59014, pr68185

Link never finishes with -O3 -mrelax:
pr59014, pr112581-1, pr77766, pr85582-1

The patch

rx-relax-alignment.patch, at the end of this post, is against bfd/elf32-rx.c of your binutils 2.44 source for 14.2.0.202607. The top of it says what each part does.

I built the toolchain from your source archives, with the patch in the linker, and ran all 1656 programs of gcc.c-torture/execute in the simulator.

  • -O2 -mrelax, -O3 -mrelax, and -O2 -mrelax -ffunction-sections -fdata-sections -Wl,--gc-sections: the same programs pass as without -mrelax. No link hangs.
  • -Os and -Oz: every program links to the same bytes with and without the patch.
  • The programs listed above also pass with the compiler and libraries of your package and the patched linker.

One more thing

rx-dir3u-range-check.patch, also below, is separate and optional. In rx_elf_relocate_section, R_RX_DIR3U_PCREL is only applied when the distance is 3 to 10, and nothing is reported when it is not. That is why fault 1 links without a word. I would make it an error again, so a branch that cannot reach fails the link.

With both patches, none of the 1656 programs hits that error at any of the option sets above.

Can you take these, or fix it another way, in the next release?

If the forum changes the tabs in the patches to spaces, patch -l -p1 still applies them.

aligned.c

#include <stdlib.h>

/* Each function has a branch over a return to a label that -O2 aligns. */
__attribute__((noinline)) void check(long a, unsigned long b)
{
    if (a != 1 || b != 1)
        abort();
}

__attribute__((noinline)) void pass_on(long a)
{
    if (!a)
        return;
    check(a, a >= 0 ? (unsigned long)a : -(unsigned long)a);
}

int main(void)
{
    pass_on(1);
    return 0;
}

forever.c

volatile int ready;
int limit = 3;

/* Waits for ready for a number of turns, and stops for good when it does not come. */
__attribute__((noinline)) int wait_for_ready(int turns)
{
    while (turns > 0) {
        if (ready)
            return turns;
        turns--;
    }
    for (;;) {
    }
}

int main(void)
{
    ready = 1;
    return wait_for_ready(limit) == limit ? 0 : 1;
}

rx-relax-alignment.patch

RX: linker relaxation of code that has alignment padding in it

Against bfd/elf32-rx.c of the binutils 2.44 source for GNURX 14.2.0.202607.

-O2 and -O3 align the targets of branches, so their code has padding in it. -Os does not.
With -mrelax the linker gets code with padding wrong in three ways.

1. A branch over padding was made into the one byte form (R_RX_DIR3U_PCREL), which
   reaches 3 to 10 bytes ahead. When the padding then shrank, the target was nearer than
   3 bytes and the branch was left going somewhere else. The check that keeps such a
   branch long asked for a target before the previous alignment and after the next one,
   which no target is. It now asks for one or the other, and counts a target right after
   the padding. The test for BEQ.B and BNE.B also had the glue on the wrong side.

2. Padding can be one NOP of several bytes. When the padding shrank, the end of it was
   cut off, and what was left ran as the start of a longer instruction that took the
   next one with it. What is left is now filled with one byte NOPs.

3. With two alignments at one address, as for a label that is the target of a jump and
   the start of a loop, the link never ended. reloc_bubblesort swapped the two markers
   for ever. After that, the walk over the alignments stepped from one to the next with
   "next_alignment += 2", which is wrong for two at one address, and went past the last
   reloc. The sort now leaves two of a kind alone, and the walk looks for the markers.

Code of -Os has no padding, and links to the same bytes with and without this patch.

--- a/bfd/elf32-rx.c
+++ b/bfd/elf32-rx.c
@@ -1756,11 +1756,13 @@
 	    swappit = false;
 	  else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
 		   && (r[i + 1].r_addend & RX_RELAXA_ALIGN))
-	    swappit = true;
+	    /* Two of these at one address must not swap, or this never ends.  */
+	    swappit = !(ELF32_R_TYPE (r[i].r_info) == R_RX_RH_RELAX
+			&& (r[i].r_addend & RX_RELAXA_ALIGN));
 	  else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
 		   && (r[i + 1].r_addend & RX_RELAXA_ELIGN)
 		   && !(ELF32_R_TYPE (r[i].r_info) == R_RX_RH_RELAX
-			&& (r[i].r_addend & RX_RELAXA_ALIGN)))
+			&& (r[i].r_addend & (RX_RELAXA_ALIGN | RX_RELAXA_ELIGN))))
 	    swappit = true;
 	  else
 	    swappit = false;
@@ -2193,12 +2195,41 @@
 	     displacements across an alignment boundary, just in case.
 	     Note that this only affects relocations to the same
 	     section.  */
-	  prev_alignment = next_alignment;
-	  next_alignment += 2;
-	  while (next_alignment < irelend
-		 && (ELF32_R_TYPE (next_alignment->r_info) != R_RX_RH_RELAX
-		     || !(next_alignment->r_addend & RX_RELAXA_ELIGN)))
-	    next_alignment ++;
+	  if (irel->r_addend & RX_RELAXA_ALIGN)
+	    {
+	      /* Two gaps can end at one address.  Then there is not one
+		 end marker for each start marker in turn, and stepping
+		 over two relocs goes too far, in the end past the last
+		 one.  Look for the markers instead.  This gap ends at the
+		 first end marker after its start.  */
+	      Elf_Internal_Rela *end = irel + 1;
+
+	      while (end < irelend
+		     && (ELF32_R_TYPE (end->r_info) != R_RX_RH_RELAX
+			 || !(end->r_addend & RX_RELAXA_ELIGN)))
+		end ++;
+	      prev_alignment = end < irelend ? end : NULL;
+
+	      /* The next alignment is the first one that ends further on.
+		 Bytes deleted from this gap are put back in front of that
+		 one, so one that ends where this gap ends would give them
+		 straight back and the relaxing would never be done.  */
+	      next_alignment = end;
+	      while (next_alignment < irelend
+		     && (ELF32_R_TYPE (next_alignment->r_info) != R_RX_RH_RELAX
+			 || !(next_alignment->r_addend & RX_RELAXA_ELIGN)
+			 || next_alignment->r_offset <= end->r_offset))
+		next_alignment ++;
+	    }
+	  else
+	    {
+	      prev_alignment = next_alignment;
+	      next_alignment += 2;
+	      while (next_alignment < irelend
+		     && (ELF32_R_TYPE (next_alignment->r_info) != R_RX_RH_RELAX
+			 || !(next_alignment->r_addend & RX_RELAXA_ELIGN)))
+		next_alignment ++;
+	    }
 	  if (next_alignment >= irelend || next_alignment->r_offset == 0)
 	    next_alignment = NULL;
 	}
@@ -2229,6 +2260,9 @@
 
 	  elf32_rx_relax_delete_bytes (abfd, sec, erel->r_offset-nbytes, nbytes, next_alignment,
 				       erel->r_offset == sec->size, internal_relocs);
+	  /* What is left of the gap can be the start of a NOP of several
+	     bytes that lost its end.  Make it one byte NOPs.  */
+	  memset (contents + irel->r_offset, 0x03, erel->r_offset - irel->r_offset);
 	  *again = true;
 
 	  continue;
@@ -2748,10 +2782,10 @@
 	     deleting bytes here makes the branch *further* away.  We
 	     can be agressive with branches within this alignment
 	     block, but not branches outside it.  */
-	  if ((prev_alignment == NULL
-	       || symval < (bfd_vma)(sec_start + prev_alignment->r_offset))
-	      && (next_alignment == NULL
-		  || symval > (bfd_vma)(sec_start + next_alignment->r_offset)))
+	  if ((prev_alignment != NULL
+	       && symval < (bfd_vma)(sec_start + prev_alignment->r_offset))
+	      || (next_alignment != NULL
+		  && symval >= (bfd_vma)(sec_start + next_alignment->r_offset)))
 	    alignment_glue = section_alignment_glue;
 
 	  if (ELF32_R_TYPE(srel[1].r_info) == R_RX_RH_RELAX
@@ -2830,7 +2864,7 @@
 	    case 0x20: /* BEQ.B pcdsp:8 */
 	    case 0x21: /* BNE.B pcdsp:8 */
 	      if (max_pcrel3 + alignment_glue <= pcrel
-		  && pcrel - alignment_glue <= 10
+		  && pcrel <= 10 - alignment_glue
 		  && allow_pcrel3)
 		{
 		  insn[0] = 0x10 | ((insn[0] & 1) << 3);

rx-dir3u-range-check.patch

RX: report a one byte branch that cannot reach its target

Against bfd/elf32-rx.c of the binutils 2.44 source for GNURX 14.2.0.202607.

R_RX_DIR3U_PCREL is only applied when the distance is 3 to 10. For any other distance
the instruction is left as it was and nothing is reported, so the program is linked with
a branch to the wrong place. This makes it an error again, as for the other relocations.

--- a/bfd/elf32-rx.c
+++ b/bfd/elf32-rx.c
@@ -873,12 +873,9 @@
 	  break;
 
 	case R_RX_DIR3U_PCREL:
-	if((relocation >= 3) && (relocation <=10))
-	{
 	  RANGE (3, 10);
 	  OP (0) &= 0xf8;
 	  OP (0) |= relocation & 0x07;
-	}
 	  break;
 
 	case R_RX_RH_24_NEG:
Open Source Tools for Renesas commented
    • Hello,

      Thank you for the detailed report and for providing the minimal reproducible examples. We have investigated the reported behavior and can confirm that this is a bug.

      We intend to address this issue in a future release. However, as no release date has been established at this time, we recommend not relying on the availability of a fix for your current development plans.

      We will keep you informed of any relevant developments.

      Best regards,
      The Open Source Tools Team