g++keeps unused objects with virtual functions
Richard Biener
richard.guenther@gmail.com
Tue Apr 7 21:16:00 GMT 2015
On April 7, 2015 5:00:27 PM GMT+02:00, Stefan Ehrlich <stefan.ehrlich@ehrlich.eu> wrote:
>Hello GCC developer team,
>I hope I am right here to address my problem with memory usage and g++:
>
>I am writing C++ software for several very small embedded systems (8k
>and smaller) and a feature with the virtual tables and the linker does
>not make my life easy :-) I have a lot of objects with virtual
>functions, where not all of them are used in each application, but they
>remain existing in the source code.
>Until now I could not find a way to get rid of them in the output
>elf/hex file automatically (they are not removed by the linker).
>
>For better understanding an example:
>
>The program:
>int main()
>{
> for(;;)
> {
> // Nothing to do
> }
> // unreachable code
> //return 0;
>}
>
>uses 62 bytes of flash and 0 bytes of RAM on an atmega8 µC (compiled
>with gcc 4.9.2)
>
>When I add a not used object with virtual functions (in the below
>listed example named as Derived0):
>
>class CBase
>{
>public:
> virtual void virtFunction() = 0;
>};
>
>class CDerived : public CBase
>{
>public:
> virtual void virtFunction() { }
>};
>
>CDerived Derived0;
>
>int main()
>{
> for(;;)
> {
> // Nothing to do
> }
> // unreachable code
> //return 0;
>}
>
>the memory usage jumps up to 156 bytes flash and 8 bytes RAM usage
>(same compiler 4.9.2)
>
>compiler and linker options are:
>avr-g++.exe -c -Os -Wall -fdata-sections -ffunction-sections
>-fvisibility=hidden -fvisibility-inlines-hidden -fno-rtti -flto
>-fuse-linker-plugin -mmcu=atmega8 ...
>avr-gcc.exe -Wall -Os -Wl,-static -Wl,-flto -fuse-linker-plugin
>-Wl,--strip-all -Wl,-s -Wl,--gc-sections -mmcu=atmega8 ...
>
>The more not used objects I use the worse the problem gets.
>
>Is there any possibility to remove unused virtual functions or at least
>the objects, which are not used? I have not find any solution so far.
>If not, is there a plan to add this feature to the linker?
I think it should already work with LTO. Maybe there are some target specifics which make the vtables referenced?
Richard.
>greetings from Austria
>
>Stefan
>
More information about the Gcc
mailing list