ARM Technical Support Knowledge Articles

LOCATING A TABLE OF POINTERS AT A SPECIFIC ADDRESS

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

I need to locate an array of pointers at a specific address. However, I'm having the following problems.

How can I locate the array at a fixed address?

ANSWER

To accomplish what you want, you must create a file containing only the array. The objects pointed to from the array must be defined in a separate source file. Then, you may use the linker controls to locate the constant segment for the array file.

Fortunately, you can use sophisticated macros and a header file to make management of this process easy.

  1. Create the following TABLE.H header file and add the strings you will use.
    /*===============================================
    Always undefine the table definition macros at
    the top of this file.
    ===============================================*/
    #undef TABLE_DEF
    #undef TABLE_END
    #undef TABLE_MSG
    
    /*===============================================
    Change the following definitions to specify the
    memory space to use for the table and for the
    strings.
    ===============================================*/
    #define TABLE_MSPACE    code
    #define STRING_MSPACE   code
    
    /*===============================================
    Do not change the following.
    ===============================================*/
    #if   defined(BUILD_TABLE)
    
    #define TABLE_DEF(name)   char STRING_MSPACE * TABLE_MSPACE name [] = {
    #define TABLE_END    };
    #define TABLE_MSG(name,str)   &name,
    #elif defined(BUILD_STRINGS)
    
    #define TABLE_DEF(name)
    #define TABLE_END
    #define TABLE_MSG(name,str)    char STRING_MSPACE name [] = str;
    
    #elif defined(BUILD_EXTERNS)
    #define TABLE_DEF(name)
    #define TABLE_END
    #define TABLE_MSG(name,str)   extern char STRING_MSPACE name [];
    
    #endif
    
    /*===============================================
    Create your table as follows.  Add more
    TABLE_MSG macros to add more strings.
    ===============================================*/
    TABLE_DEF(table)
      TABLE_MSG (s001, "String1")
      TABLE_MSG (s002, "String2")
      TABLE_MSG (s003, "String3")
      TABLE_MSG (s004, "String4")
    TABLE_END
    
    /*===============================================
    ===============================================*/
    
  2. Create the following STRINGS.C file which includes the header file and declares the strings.
    #define BUILD_STRINGS 1
    #include "table.h"
    
  3. Create the following TABLES.C file which includes the header file to create extern definitions for the strings and then includes the header file a second time to create the table.
    #define BUILD_EXTERNS 1
    #include "table.h"
    #undef BUILD_EXTERNS
    
    #define BUILD_TABLE 1
    #include "table.h"
    
  4. Use the linker CODE control to locate the ?CO?TABLE segment at the address you want. For example, CODE (?CO?TABLE(0x4000)) locates the table at 0x4000 in the CODE space. 5. You may wish to create preprocessor output files (*.I) to view the results of the preprocessor. For example:


    TABLE.I

    #line 1 "table.c" /0
    #line 1 "table.h" /0
    #line 19 "table.h" /1
    #line 31 "table.h" /0
      extern char code s001 [];
      extern char code s002 [];
      extern char code s003 [];
      extern char code s004 [];
    #line 2 "table.c" /0
    #line 1 "table.h" /0
    


    STRINGS.I

    #line 1 "strings.c" /0
    #line 1 "table.h" /0
    #line 19 "table.h" /1
    #line 25 "table.h" /0
    #line 31 "table.h" /1
    #line 37 "table.h" /0
      char code s001 [] = "String1";
      char code s002 [] = "String2";
      char code s003 [] = "String3";
      char code s004 [] = "String4";
    #line 2 "strings.c" /0
    

    Note that declaring the strings outside the table and then referencing the address of the string in the table uses no additional code or data space.

Article last edited on: 2005-07-09 12:43:23

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