Variables assigned to ram are increasing elf file size (=> occupying flash)

David Brown
Wed Sep 2 15:19:28 GMT 2020

(This is for the OP's benefit - I suspect that you, Dan, know this already.)

It's also important to remember that the elf file is not stored in flash
(assuming we are talking about a small embedded system here, rather than
an embedded Linux system or something).  The elf file typically contains
a huge amount of extra information such as debugging data in addition to
the flash image.  It's only the .text and .data sections (and perhaps
some others such as vectors or other flash sections, depending on the
linker setup) that actually take space in the flash.  The .bss section
takes no space in flash.


On 01/09/2020 17:41, Dan Kegel wrote:
> Unless they're initialized, the arrays should be in BSS, which shouldn't be
> in the elf nor stored to flash (since it's all zero, and should be cleared
> on startup).
> Since you didn't include a test case, here's a tiny example:
> dank@thinky:~$ cat foo.c
> #include <stdio.h>
> #include <stdlib.h>
> int blarg[500000];
> int main(int argc, char **argv)
> {
>    int i = atoi(argv[1]);
>    int j = atoi(argv[2]);
>    blarg[i] = 7;
>    printf("blarg[%d] == %d\n", j, j);
>    return 0;
> }
> dank@thinky:~$ gcc -O2 foo.c
> dank@thinky:~$ size a.out
>    text   data    bss    dec    hex filename
>    1783    608 2000032 2002423 1e8df7 a.out
> dank@thinky:~$ ls -l a.out
> -rwxrwxr-x 1 dank dank 16768 Sep  1 08:39 a.out
> Note the small size of a.out.
> - Dan
> On Tue, Sep 1, 2020 at 3:21 AM Rachel Sapir <> wrote:
>> Hello,
>> I noticed that when I increase array sizes (I have some very big arrays)
>> the .elf file is increased by the same size. Since the elf file is saved to
>> the flash, it occupies flash area unnecessarily.
>> Is there a way to prevent this from happening?
>> Best regards,
>> Rachel

More information about the Gcc-help mailing list