Using the GNU Linker Script to know the FLASH and RAM Areas in the Application

Sometimes it is handy to know in the running application the start address, end address and the size of a linked section, e.g. to know the boundaries of RAM or FLASH areas. This means that from the application code I can get access to knowledge of the GNU linker:

Information about Linker Sections

Information about Linker Sections

To access the section information from the GNU linker script is pretty easy and simple. For example in my GNU linker file I have the following memory mapping:

MEMORY {
 m_interrupts (RX) : ORIGIN = 0x00000000, LENGTH = 0x00000198
 m_text (RX) : ORIGIN = 0x00000410, LENGTH = 0x000FFBF0
 m_data_1FFF0000 (RW) : ORIGIN = 0x1FFF0000, LENGTH = 0x00010000
 m_data (RW) : ORIGIN = 0x20000000, LENGTH = 0x00030000
 m_cfmprotrom (RX) : ORIGIN = 0x00000400, LENGTH = 0x00000010
}

I can define additional symbols inside the SECTIONS part of the linker script like this to know the boundaries and size of the m_text MEMORY area:

SECTIONS {
  /* other section placement here. */

  __m_text_start = ORIGIN(m_text);
  __m_text_end = ORIGIN(m_text)+ LENGTH(m_text);
  __m_text_size = LENGTH(m_text);
}

ORIGIN() will return the start address of the section and LENGTH() the size of the section, and I can do basic arithmetic too. The linker generated symbols will have an address.

To use them from the application code, I have to declare them like this:

/* the following symbols are defined in the GNU linker script */
extern int *__m_text_start;
extern int *__m_text_end;
extern int *__m_text_size;

Taking the address of these symbols will return the address information. To build a string with the address information I can use snprintf() or xsnprintf() (see XFormat, a Lightweight printf() and sprintf() Alternative):

res = XF1_xsnprintf(buffer, sizeof(buffer), "Start Addr: 0x%X, End Addr: 0x%X, Size: 0x%X\r\n", &__m_text_start, &__m_text_end, &__m_text_size);

With this I get the start and end address plus the section size I can use in my application:

Information about Linker Sections

Information about Linker Sections

Happy Linking 🙂

Links

Advertisements

3 thoughts on “Using the GNU Linker Script to know the FLASH and RAM Areas in the Application

    • Hi Juan,
      it really does not matter: it is a linker symbol (from which I can take the address) but the symbol itself has no type or size. So it could be a char, enum, struct or whatever. I usually declare it as a pointer or as a int type.

      Like

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s