ld: pru: Place exception-handling sections correctly

Message ID 20230503171124.6710-1-dimitar@dinux.eu
State Accepted
Headers
Series ld: pru: Place exception-handling sections correctly |

Checks

Context Check Description
snail/binutils-gdb-check success Github commit url

Commit Message

Dimitar Dimitrov May 3, 2023, 5:11 p.m. UTC
  A recent GCC commit [1] exposed a latent bug in the PRU default linker
script. The exception-handling sections were not described in the linker
script. When GCC started outputting read-only EH sections, their
contents would erroneously be placed in the instruction memory (IMEM),
which in turn is not readable at runtime. Thus any access to the
read-only exception-handling data results in a runtime crash.

Fix by declaring the EH output sections. Do not split in read-only and
read-write sections because PRU has no true support for rodata. It has
only one memory for data (DMEM).

Tested pru-unknown-elf and found no regressions in Binutils. All GCC
failures caused by [1] are now fixed.

[1] https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=2744dbb9ecf104a113da3a0f39115da4653bb676

ld/ChangeLog:

	* scripttempl/pru.sc (OUTPUT_SECTION_ALIGN): New helper variable
	to place at end of DMEM output sections.
	(.eh_frame): New output section.
	(.gnu_extab): Ditto.
	(.gcc_except_table): Ditto.

Signed-off-by: Dimitar Dimitrov <dimitar@dinux.eu>
---
 ld/scripttempl/pru.sc | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)
  

Comments

Nick Clifton May 4, 2023, 11:43 a.m. UTC | #1
Hi Dimitar,

> A recent GCC commit [1] exposed a latent bug in the PRU default linker
> script. The exception-handling sections were not described in the linker
> script. When GCC started outputting read-only EH sections, their
> contents would erroneously be placed in the instruction memory (IMEM),
> which in turn is not readable at runtime. Thus any access to the
> read-only exception-handling data results in a runtime crash.
> 
> Fix by declaring the EH output sections. Do not split in read-only and
> read-write sections because PRU has no true support for rodata. It has
> only one memory for data (DMEM).

Patch approved and applied.

Note - I extended the ChangeLog entry to also mention the updated to the
.data and .resource_table sections.

Cheers
   Nick
  

Patch

diff --git a/ld/scripttempl/pru.sc b/ld/scripttempl/pru.sc
index 8531cb8d5be..3ff86bc61c7 100644
--- a/ld/scripttempl/pru.sc
+++ b/ld/scripttempl/pru.sc
@@ -24,6 +24,12 @@  ENTRY (_start)
 
 EOF
 
+OUTPUT_SECTION_ALIGN="
+    ${RELOCATING+/* In case this is the last input section,
+      align to keep the loadable segment size a multiple of the common page size.
+      Some SoCs have stricter memory size requirements than others.  */
+    . = ALIGN (CONSTANT (COMMONPAGESIZE));}"
+
 cat <<EOF
 SECTIONS
 {
@@ -163,10 +169,25 @@  SECTIONS
     ${RELOCATING+*(COMMON)}
     ${RELOCATING+ PROVIDE (_bss_end = .) ; }
 
-    ${RELOCATING+/* In case this is the last input section, align to
-      keep the loadable segment size a multiple of the common page size.
-      Some SoCs have stricter memory size requirements than others.  */
-    . = ALIGN (CONSTANT (COMMONPAGESIZE));}
+    ${OUTPUT_SECTION_ALIGN}
+  } ${RELOCATING+ > dmem}
+
+  .eh_frame ${RELOCATING-0} :
+  {
+    KEEP (*(.eh_frame))${RELOCATING+ *(.eh_frame.*)}
+    ${OUTPUT_SECTION_ALIGN}
+  } ${RELOCATING+ > dmem}
+
+  .gnu_extab ${RELOCATING-0} :
+  {
+    *(.gnu_extab)
+    ${OUTPUT_SECTION_ALIGN}
+  } ${RELOCATING+ > dmem}
+
+  .gcc_except_table ${RELOCATING-0} :
+  {
+    *(.gcc_except_table${RELOCATING+ .gcc_except_table.*})
+    ${OUTPUT_SECTION_ALIGN}
   } ${RELOCATING+ > dmem}
 
   /* Linux remoteproc loader requires the resource_table section
@@ -175,10 +196,7 @@  SECTIONS
   .resource_table ${RELOCATING-0} ${RELOCATING+ ALIGN (CONSTANT (MAXPAGESIZE))} :
   {
     KEEP (*(.resource_table))
-    ${RELOCATING+/* In case this is the last input section, align to
-      keep the loadable segment size a multiple of the common page size.
-      Some SoCs have stricter memory size requirements than others.  */
-    . = ALIGN (CONSTANT (COMMONPAGESIZE));}
+    ${OUTPUT_SECTION_ALIGN}
   } ${RELOCATING+ > dmem}
 
   /* Global data not cleared after reset.  */