thumb-2 and crt0.S init_hooks

Bill Tompkins bill+newlib@key-square.com
Thu Oct 2 16:10:00 GMT 2008


Hi Nick,

I get a fault on a Cortex-M3 when I implement either a  
hardware_init_hook() or software_init_hook() function.  The problem,  
as I understand it, is that the calls in crt0.S are of the form

   ldr     r3, .Lhwinit
       ...
   mov     lr, pc
   mov     pc, r3

The LR then contains a word-aligned address, rather than having bit 0  
set to indicate thumb mode.
My hardware_init_hook() gets compiled (by GCC 3.4.2) to something  
ending with:

   bx lr

Since the LR doesn't have the 0 bit set, that implies that it should  
transition out of thumb mode, and on the M3 that faults.

I haven't tested the following on anything other than the M3, but it  
seems like it should do the trick:

--- libgloss/arm/crt0.S	25 Apr 2008 09:02:40 -0000	1.7
+++ libgloss/arm/crt0.S	1 Oct 2008 12:54:56 -0000
@@ -167,14 +167,23 @@
  	ldr	r3, .Lhwinit
  	cmp	r3, #0
  	beq	.LC24
+#if defined(__thumb__) || defined(__thumb2__)
+	blx   r3
+#else
  	mov	lr, pc
  	mov	pc, r3
+#endif
  .LC24:	
  	ldr	r3, .Lswinit
  	cmp	r3, #0
  	beq	.LC25
+#if defined(__thumb__) || defined(__thumb2__)
+	blx   r3
+#else
  	mov	lr, pc
  	mov	pc, r3
+#endif
+
  .LC25:	
  	mov	r0, #0		/*  no arguments  */
  	mov	r1, #0		/*  no argv either */

-Bill



More information about the Newlib mailing list