yttyx
Posts: 9
Joined: Tue May 11, 2021 7:46 am

Read-only data being put into .data section, not .rodata

Sun May 16, 2021 2:09 pm

Hi. I have a large array (104968 entries) of read-only data in a .cpp file which I would like the linker to put into the .rodata section so it gets accessed from flash when the program executes (not loaded into RAM).

I'm defining it like this:

Code: Select all

static const uint32_t hash_table[] =
{
    0x00000a0c,
    0x0001a698,
    0x0000fdf2,
    0x00019922,
.
.
};

At link time I get the following error:

Code: Select all

dictionary-read-test.elf section `.data' will not fit in region `RAM'
.../arm-none-eabi/bin/ld: region RAM overflowed
.../arm-none-eabi/bin/ld: region `RAM' overflowed by 174176 bytes

Looking at the .map file I can see hash_table has been put into the .data section.

Code: Select all

.data._ZL10hash_table
                  0x000000002000116c    0x66820 CMakeFiles/dictionary-read-test.dir/dictionary_i.cpp.obj

By contrast, in the same .cpp file I have another large array, this time of string pointers, which is being put into .rodata:

Code: Select all

static const char * steno_dictionary[] =
{
    "#240", "240",
    "#A", "{&5}",
    "#A/W-B/THUZ", "5,000",
    "#F", "{&6}",
    "#F/W-B/THUZ", "6,000",
.
.
};

Code: Select all

.rodata._ZL16steno_dictionary
                0x00000000101d1220    0x75270 CMakeFiles/dictionary-read-test.dir/dictionary_i.cpp.obj

Why isn't hash_table also being put into .rodata, please, and what can I do to put it there?

carlk3
Posts: 57
Joined: Wed Feb 17, 2021 8:46 pm

Re: Read-only data being put into .data section, not .rodata

Mon May 31, 2021 3:33 pm

Just speculating: specify the section with __attribute__? E.g.:

Code: Select all

static const uint32_t hash_table[] =
{
    0x00000a0c,
    0x0001a698,
    0x0000fdf2,
    0x00019922,
.
.
} __attribute__((section(".rodata")));

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 617
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: Read-only data being put into .data section, not .rodata

Mon May 31, 2021 4:58 pm

Why isn't hash_table also being put into .rodata, please, and what can I do to put it there?
try constexpr instead of const perhaps?

yttyx
Posts: 9
Joined: Tue May 11, 2021 7:46 am

Re: Read-only data being put into .data section, not .rodata

Tue Jun 01, 2021 9:29 am

@carlk3 @kilograham Thank you for your suggestions, which I have noted. I tried again yesterday with a combination of strings and uint16_t, and this does get put into the .rodata section:

Code: Select all

struct dictionary_entry
{
    const char * const steno;
    const char * const text;
    const uint16_t     flags;
};

static const dictionary_entry steno_dictionary_hashed[] =
{
.
.
    { "SKEFL", "successful", 0x0000 },
    { "TEFT", "test", 0x0000 },
.
.
};

matherp
Posts: 41
Joined: Tue May 02, 2017 10:54 am

Re: Read-only data being put into .data section, not .rodata

Tue Jun 01, 2021 10:07 am

What happens if you remove STATIC in the original definition. I never use STATIC with CONST and as far as I see can see my const variables are going into RODATA

e.g.

Code: Select all

const int ErrorMap[] = {        0,                                  // 0
                                1,                                  // Assertion failed
                                2,                                  // Low level I/O error
                                3,                                  // No response from SDcard
                                4,                                  // Could not find the file
                                5,                                  // Could not find the path
                                6,                                  // The path name format is invalid
                                7,                                  // Prohibited access or directory full
                                8,                                  // Directory exists or path to it cannot be found
                                9,                                  // The file/directory object is invalid
                               10,                                  // SD card is write protected
                               11,                                  // The logical drive number is invalid
                               12,                                  // The volume has no work area
                               13,                                  // Not a FAT volume
                               14,                                  // Format aborted
                               15,                                  // Could not access volume
                               16,                                  // File sharing policy
                               17,                                  // Buffer could not be allocated
                               18,                                  // Too many open files
                               19,                                  // Parameter is invalid
							   20									// Not present
                            };
is definitely in RODATA

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 617
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: Read-only data being put into .data section, not .rodata

Tue Jun 01, 2021 12:55 pm

just to be clear this is a C++/gcc issue... which is why i suggested constexpr - compiler is not required to put const in rodata though I dont know what heuristic it actually uses. (I assume if this was a C file it would work)

yttyx
Posts: 9
Joined: Tue May 11, 2021 7:46 am

Re: Read-only data being put into .data section, not .rodata

Tue Jun 01, 2021 6:06 pm

I was not able to reproduce the original reported issue, which was odd. I tried a few variations of the array definition, following the suggestions above:

Code: Select all

// uint32_t hash_table[] =                                             // .data
// const uint32_t hash_table[] =                                       // .rodata
// static uint32_t hash_table[] =                                      // .rodata
// static const uint32_t hash_table[] =                                // .rodata
// constexpr uint32_t hash_table[] =                                   // .rodata
// uint32_t  __attribute__( ( section( ".rodata" ) ) ) hash_table[] =  // .rodata (plus warning: /tmp/ccERyrml.s:175: Warning: setting incorrect section attributes for .rodata)
{
    0x00000a0c,
    0x0001a698,
    0x0000fdf2,
    0x00019922,
    0x00000a0c,
    0x0001a698,
    0x0000fdf2,
    0x00019922,
};

Return to “SDK”