5.6.7 Placing jump tables in ROM

You might find that jump tables are placed in RAM rather than in ROM.

A jump table might be placed in a RAM .data section when you define it as follows:

typedef void PFUNC(void);
const PFUNC *table[3] = {func0, func1, func2};

The compiler also issues the warning:

jump.c:19:1: warning: 'const' qualifier on function type 'PFUNC'
      (aka 'void (void)') has unspecified behavior
const PFUNC *table[3] = {func0, func1, func2};
^~~~~~

The following procedure describes how to place the jump table in a ROM .rodata section.

Procedure

  1. Create a C file jump.c.

    Make the PFUNC type a pointer to a void function that has no parameters. You can then use PFUNC to create an array of constant function pointers.

    extern void func0(void);
    extern void func1(void);
    extern void func2(void);
    
    typedef void (*PFUNC)(void);
    
    const PFUNC table[] = {func0, func1, func2};
    
    void jump(unsigned i)
    {
      if (i<=2)
      table[i]();
    }
  2. Compile the file.
    armclang --target=arm-arm-eabi-none -mcpu=cortex-a9 jump.c -g -c -o jump.o
  3. Run fromelf on the image to view the contents of the output sections.
    fromelf -c -d jump.o
    The table is placed in the read-only section .rodata that you can place in ROM as required:
    ...
    ** Section #3 '.text.jump' (SHT_PROGBITS) [SHF_ALLOC + SHF_EXECINSTR]
        Size   : 64 bytes (alignment 4)
        Address: 0x00000000
    
        $a.0
        [Anonymous symbol #24]
        jump
            0x00000000:    e92d4800    .H-.    PUSH     {r11,lr}
            0x00000004:    e24dd008    ..M.    SUB      sp,sp,#8
            0x00000008:    e1a01000    ....    MOV      r1,r0
            0x0000000c:    e58d0004    ....    STR      r0,[sp,#4]
            0x00000010:    e3500002    ..P.    CMP      r0,#2
            0x00000014:    e58d1000    ....    STR      r1,[sp,#0]
            0x00000018:    8a000006    ....    BHI      {pc}+0x20 ; 0x38
            0x0000001c:    eaffffff    ....    B        {pc}+0x4 ; 0x20
            0x00000020:    e59d0004    ....    LDR      r0,[sp,#4]
            0x00000024:    e3001000    ....    MOVW     r1,#:LOWER16: table
            0x00000028:    e3401000    ..@.    MOVT     r1,#:UPPER16: table
            0x0000002c:    e7910100    ....    LDR      r0,[r1,r0,LSL #2]
            0x00000030:    e12fff30    0./.    BLX      r0
            0x00000034:    eaffffff    ....    B        {pc}+0x4 ; 0x38
            0x00000038:    e28dd008    ....    ADD      sp,sp,#8
            0x0000003c:    e8bd8800    ....    POP      {r11,pc}
    
    ...
    ** Section #7 '.rodata.table' (SHT_PROGBITS) [SHF_ALLOC]
        Size   : 12 bytes (alignment 4)
        Address: 0x00000000
    
        0x000000:   00 00 00 00 00 00 00 00 00 00 00 00                ............
    ...
Non-ConfidentialPDF file icon PDF versionARM 100748_0607_00_en
Copyright © 2016, 2017 ARM Limited or its affiliates. All rights reserved.