EmbDev.net

Forum: ARM programming with GCC/GNU tools what does (size before relaxing) in a GCC .map file mean and how does it work?


Author: (size before relaxing) GCC (Guest)
Posted on:

Rate this post
0 useful
not useful
Hello,

Please help me understand how the compiler does the flowing 
optimization:


I have an array of strings (pointers to char) declared this way:

const char* const STR_ARRAY[] =
{
"first",
"second",
"third",
"fourth",
"fifth",
... and so on ..

"last"
}

After compiling, I fond in the .map file the memory zone where the 
string where saved and I was surprised to see that some optimization was 
made.

.rodata.str1.1
                0x01055586     0x1af3 .myStrArray.o
                               0x2060 (size before relaxing)


The actual size of my strings is 0x2061 (all the letters + the null 
terminator for each string), but only 0x1af3 bytes used..


Thank you,
Andrew

Author: A. K. (prx)
Posted on:

Rate this post
0 useful
not useful
Are there duplicate strings, or strings where one is identical to the 
ending of another?

Author: (size before relaxing) GCC (Guest)
Posted on:

Rate this post
0 useful
not useful
Thank you for the reply A.K.,

There are no duplicate strings, but indeed there are strings where one 
is identical to the ending of another (like: "first" and "the first").

More there are situations where the beginning of one sting is identical 
to another string (like: "first" and "first string") or the beginnings 
of two strings are identical (like: "the first" and "the second").

Is this how the optimization is made?

Thanks again,
Andrew

Author: A. K. (prx)
Posted on:

Rate this post
0 useful
not useful
It was just a guess, but I do know that compilers such as GCC are able 
to share identical strings. If some string is idential to the trailing 
part of another, it can be omitted entirely as the corresponding part of 
the longer one can be used instead. It won't work with shared prefixes 
though, since they miss the \0 in the proper place.

Author: A. K. (prx)
Posted on:

Rate this post
0 useful
not useful
Since both sizes are shown in the map file, it likely is caused by some 
optimization done by the linker though.

Author: (size before relaxing) GCC (Guest)
Posted on:

Rate this post
0 useful
not useful
I spent some time analyzing the memory and I observed that you were 
right.

For two strings, when one is identical to the ending of the other, for 
example "first\0" and "the first\0" only the longer string, in this case 
"the first" is stored in memory.

I have not investigated if the optimization is made by the linker or the 
compiler.

Thanks,
Andrei

Author: Johann L. (gjlayde)
Posted on:

Rate this post
0 useful
not useful
(size before relaxing) GCC wrote:

> There are no duplicate strings, but indeed there are strings where one
> is identical to the ending of another (like: "first" and "the first").

It's calles string merging.  The trigger is the right sections flags set 
by the compiler, which then are avaluated by the linker in order to 
merge the strings.  Look at the generated assembler code.  GCC puts the 
strings into sections with flags like "aMS".  "M" allows merging and "S" 
marks it as a section containing strings.

Author: pooter (Guest)
Posted on:

Rate this post
0 useful
not useful
the size after relaxing should be always smaller than before

Author: Johann L. (gjlayde)
Posted on:

Rate this post
0 useful
not useful
pooter wrote:
> the size after relaxing should be always smaller than before

No, not necessarily.  It can be the same size.  And depending on what 
relaxations are performed, code sie can even increase.

An example for increasing code size is when a jump / call is out of 
reach and a jump pad just after the function is added that forwards to 
the the  jump target, and the original jump jumps to the pad.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.