Discussion:
Compiling with -fdata-sections doesn't put the constant in the section expected
Clément Péron
2014-10-01 09:30:44 UTC
Permalink
Hi,

In the manual, it says that : -fdata-sections
-ffunction-sections-fdata-sections Place each function or data item
into its own section in the output file if the target supports
arbitrary sections


I try to use it but the result is not the one as expected.

gcc --version
gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2

And i compile with these arguments :
gcc -c -o hello.o -ffunction-sections -fdata-sections hello.c

I can see with objdump that .text sections are good.

I have different .text.__function__

But i have only one .rodata instead of different .rodata.__function__

Thanks for your help,

Clement

/*------------ hello.c ---------------*/

#include <stdio.h>
#include <stdlib.h>

void used_function();

int main(){

printf("On-the fly string"); //goto .rodata

used_function();

return 0;

}

void unused_function(){
const char var9[] = "String const";
char var3[] = "String not const"; //goto .text.unused_function and
is removed by the linker
printf("I would like to optimize this string !"); //goto
.rodata.str1.8 and isn't removed by the linker
printf("%s %s", var9, var3);
}

void used_function(){
printf("A string used here with the same size 2"); //goto .rodata.str1.8
}
Andrew Haley
2014-10-01 09:42:48 UTC
Permalink
Post by Clément Péron
I have different .text.__function__
But i have only one .rodata instead of different .rodata.__function__
Thanks for your help,
Now turn on the optimizer. :-)

Andrew.
Clément Péron
2014-10-01 09:48:04 UTC
Permalink
Hi Andrew,

Thanks for your reply,

I try with

gcc -c -o hello.o -ffunction-sections -fdata-sections hello.c -Os

But same result the data are put in section depending on their length
and not on the function where they are declared (due to the
fmerge-constants optimization).
I got a .rodata.str1.1 section.

gcc -c -o hello.o -ffunction-sections -fdata-sections hello.c -Os
-fno-merge-constants
And same result as without -Os argument

After the link my string used will stay in my ELF :S

Regards,
Clement
Post by Andrew Haley
Post by Clément Péron
I have different .text.__function__
But i have only one .rodata instead of different .rodata.__function__
Thanks for your help,
Now turn on the optimizer. :-)
Andrew.
Andrew Haley
2014-10-01 10:02:28 UTC
Permalink
Post by Clément Péron
After the link my string used will stay in my ELF :S
I am quite baffled. Maybe there is some reason this doesn't work, but
right now I can't think of what it might be. Unused strings are removed
from static functions, as you'd expect.

I can't think of any reason that each string should not be put into a
unique section. It's need a unique name for each string, but that isn't
hard.

Andrew.
Clément Péron
2014-10-01 12:14:12 UTC
Permalink
Yes, functions declared as static and their local variables are
removed well by the compiler when optimization is enabled.
Post by Andrew Haley
I can't think of any reason that each string should not be put into a
unique section. It's need a unique name for each string, but that isn't
hard.
You think it's easy to patch because I'm trying to look sources to
understand but not sure i will suceed to patch it myself.

Do i need to report this somewhere else ? (I saw that the bug was
reported here but nobody confirmed it
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54303)

Clement
Clément Péron
2014-10-02 14:08:55 UTC
Permalink
Ok so i look into the sources and i think their is another other
feature which doesn't work as expected.

Line : 381 of varasm.c sprintf (name, ".rodata.cst%d", (int) (align / 8)).

As i understood it, i think when you use the -fmerge-constant
parameters the string and the constant are put into a specific section
(respectively .rodata.strX.X or .rodata.cstX) depending on their
alignment and LD compare those vars inside those sections and try to
merge when it's possible.

But i created different functions with different constants and none
are put into a section called .rodata.cstX ? is it only me ?

I'm thinking to patch my gcc and i'm thinking about the best way to
achieve both -fdata-sections and -fmerge-constant.

I suppose that the Garbage collector is before the merge of the
string/cst so i think call the section .rodata.csX.__function and
.rodata.strX.X.__function are the best way to achieve both garbage
collecting and merging.

What do you think ? Do you have the same issue when you try to merge constants ?

Thanks for your help,
Clement
Post by Clément Péron
Yes, functions declared as static and their local variables are
removed well by the compiler when optimization is enabled.
Post by Andrew Haley
I can't think of any reason that each string should not be put into a
unique section. It's need a unique name for each string, but that isn't
hard.
You think it's easy to patch because I'm trying to look sources to
understand but not sure i will suceed to patch it myself.
Do i need to report this somewhere else ? (I saw that the bug was
reported here but nobody confirmed it
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54303)
Clement
Clément Péron
2014-10-09 15:00:13 UTC
Permalink
Hi,

I've just add a small code to separate each mergeable string in
different section and i win 6ko on my elf. Which is quite nice when
you have only 200ko available. :)
Post by Clément Péron
Ok so i look into the sources and i think their is another other
feature which doesn't work as expected.
Line : 381 of varasm.c sprintf (name, ".rodata.cst%d", (int) (align / 8)).
As i understood it, i think when you use the -fmerge-constant
parameters the string and the constant are put into a specific section
(respectively .rodata.strX.X or .rodata.cstX) depending on their
alignment and LD compare those vars inside those sections and try to
merge when it's possible.
But i created different functions with different constants and none
are put into a section called .rodata.cstX ? is it only me ?
I'm thinking to patch my gcc and i'm thinking about the best way to
achieve both -fdata-sections and -fmerge-constant.
I suppose that the Garbage collector is before the merge of the
string/cst so i think call the section .rodata.csX.__function and
.rodata.strX.X.__function are the best way to achieve both garbage
collecting and merging.
What do you think ? Do you have the same issue when you try to merge constants ?
Thanks for your help,
Clement
Post by Clément Péron
Yes, functions declared as static and their local variables are
removed well by the compiler when optimization is enabled.
Post by Andrew Haley
I can't think of any reason that each string should not be put into a
unique section. It's need a unique name for each string, but that isn't
hard.
You think it's easy to patch because I'm trying to look sources to
understand but not sure i will suceed to patch it myself.
Do i need to report this somewhere else ? (I saw that the bug was
reported here but nobody confirmed it
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54303)
Clement
Loading...