Over a million developers have joined DZone.

Using GNU Linker Scripts to Find an App's FLASH and RAM Areas

DZone's Guide to

Using GNU Linker Scripts to Find an App's FLASH and RAM Areas

Here's a simple trick to access knowledge about specific linked areas in your app, RAM and FLASH here, with GNU linker scripts.

· IoT Zone ·
Free Resource

Digi-Key Electronics’ Internet of Things (IoT) Resource Center Inspires the Future: Read More

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

Information about Linker Sections

Information about Linker Sections

Accessing 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:

    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:

    /* 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() will return 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!


Digi-Key’s IoT Component Selector is your one-stop-shop for the IoT

iot ,embedded development ,gnu linker ,script ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}