2

I'm porting real-time kernel TNeoKernel to the Cortex-Mx processors, it works when compiled with ARMCC, but doesn't work when compiled with GCC: just after calling SVC instruction, PC is updated to the SVC_Handler, and at the next (any) instruction, UsageFault exception happens.

Examining of CFSR shows that INVSTATE bit is set: i.e. the processor is in ARM state, which is illegal on Cortex-M CPUs. I've checked xPSR: yes, T bit is cleared.

Then I've examined Vector table: yes, at the offset 0x2c I have the address of my SVC_Handler, but LSB is cleared: 0x8013c40. So, there's no surprise that CPU is put to the ARM state. The surprise is why do I have LSB cleared?

I re-checked address of SVC_Handler when compiled with ARMCC, and yes, LSB is set there: actual address of SVC_Handler is 0x0800297a, but Vector table contains 0x0800297b.

So, GCC generates wrong Vector table.

Currently I use CooCox IDE and arm-none-eabi-gcc version 4.8.4; options that are given to GCC are:

-mthumb -mcpu=cortex-m4

It seems pretty enough to make GCC know that the CPU does not support ARM state, so, is it a bug in GCC? Or, should I give some more keys to it, to make it generate correct Vector table?

UPD: here is the source file tn_arch_cortex_m.S on bitbucket. I have .thumb directive there; please note that this file is intended to work with GCC as well as ARMCC, so there are some macros to cope for both tools.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114
  • 2
    It is always helpful to [point people to the source](https://bitbucket.org/dfrank/tneokernel/src/d1c6620848f6b71759676d79ea99ced5b0e9a8da/src/arch/cortex_m/tn_arch_cortex_m.S?at=default#cl-263). My guess is that GNU **as** (assembler) has different pseudo-ops and they are not set for the `SVC_Handler`. Specifically, you need `.thumb` and `.thumb_func` to do this. See: [recent U-boot and Thumb2](http://lists.denx.de/pipermail/u-boot/2014-November/195889.html). – artless noise Nov 17 '14 at 16:50
  • @artlessnoise Sorry for not mentioning the source; I've updated my question. I do use `.thumb` although don't use `.thumb_func`, let me try... – Dmitry Frank Nov 17 '14 at 16:58
  • @artlessnoise Thank you so much, `.thumb_func` does the trick! Holy cow, I've spent so much time. Could you please teach me your gcc-fu a bit: how have you found this information so quickly? I've read The Definitive Guide to ARM Cortex-M3/M4 (before implementing Cortex port), I googled a lot before asking, and I failed to find this. Now that I know about `thumb_func`, I've just re-checked latest pdf from https://gcc.gnu.org/onlinedocs/ , there's nothing about `.thumb_func`! So, how have you found it? – Dmitry Frank Nov 17 '14 at 17:14
  • 1
    @artlessnoise please write your comment as an answer, I'll accept it. – Dmitry Frank Nov 17 '14 at 17:54
  • 2
    possible duplicate of [Mixing ARM and THUMB instructions](http://stackoverflow.com/questions/11526062/mixing-arm-and-thumb-instructions) and also [GAS ARM directives](https://sourceware.org/binutils/docs/as/ARM-Directives.html) documentation. – artless noise Nov 17 '14 at 18:05
  • 3
    I have up voted your question. It is easy to find the answer when you know it :) However, I think linking it as a duplicate is better for Stack Overflow than answering it for points. – artless noise Nov 17 '14 at 18:09

0 Answers0