ARM Technical Support Knowledge Articles

Placing (constant) jump tables in ROM

Applies to: ARM Developer Suite (ADS), DS-5, RealView Development Suite (RVDS)

Answer

Scenario

Some users find their (constant) jump tables get placed in RAM (in .data) rather than ROM (.constdata), using code like this:

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

The ARM compiler will also issue a warning:

"jump.c", line 7: Warning:  #925-D: type qualifiers on function types are ignored
  const PFUNC *table[3] = {func0, func1, func2};
  ^

Answer

This type definition is incorrect for this purpose. It should read something like:

typedef void (*PFUNC)(void);

The PFUNC type is a pointer to a void function that has no parameters. This can then be used to create an array of constant function pointers as follows:

const PFUNC table[] = {func0, func1, func2};

The following example shows this in full:

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]();
}

If this is compiled with ARM Compiler toolchain v5.0 update 1:

armcc -c jump.c

... and then disassembled with:

fromelf -c -d jump.o

... the table[] is placed in a read only .constdata section, and therefore can then be placed in ROM as required.

** Section #1 '.text' (SHT_PROGBITS) [SHF_ALLOC + SHF_EXECINSTR]
    Size   : 24 bytes (alignment 4)
    Address: 0x00000000
    $a
    .text
    jump
        0x00000000:    e3500002    ..P.    CMP      r0,#2
        0x00000004:    812fff1e    ../.    BXHI     lr
        0x00000008:    e59f1004    ....    LDR      r1,[pc,#4] ; [0x14] = 0
        0x0000000c:    e7910100    ....    LDR      r0,[r1,r0,LSL #2]
        0x00000010:    e12fff10    ../.    BX       r0
    $d
        0x00000014:    00000000    ....    DCD    0

:
:

** Section #4 '.constdata' (SHT_PROGBITS) [SHF_ALLOC]
    Size   : 12 bytes (alignment 4)
    Address: 0x00000000
    0x000000:   00 00 00 00 00 00 00 00 00 00 00 00                ............

After linking, the literal pool that lives at address 0x00000014 in section 1 of ELF object jump.o, will contain the address of the table in ROM.

Article last edited on: 2011-09-19 15:27:17

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential