[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [MiNT] C++ Stuff



Miro Kropacek wrote:
    - The -ffunction-sections GCC option. With that, the linker is able
    to get rid of unused functions found in .o files.

Wow, are you kidding, right? (OK, I know you aren't :) How this can be possible?

The trick is that the compiler puts every function in a separate section in the .o file. Then the linker gets rid of the unused sections. That's all ;-) It is impossible to do the same thing with a.out object files, because the can contain only one .text section. ELF objects can contain any number of section, with arbitrary names. So we have to change our simple linker script for taking care of such cases.

For me, the size of final binaries always looked quite big but I thought there is a lot of function dependencies in mintlib and that's the reason why even simple hello world takes more than 100 KB.

This is the main reason. Just for precision: currently, the size of a Hello World built with GCC 4.3.3 is 52 Ko. It is still too much. The startup code contains support for filling the argv and envp arrays, even if the program doesn't care about the command line and the environment. Some compilers has an option for ignoring the command line (easy : just use an alternate startup code).

Some people say that using function removal does not bring much improvement on the final size, because most libraries are designed with only one extern function by source file.

Just a word about a very common and evil function : printf
It is nice, because it can display anything.
So if I call printf for displaying a string, I will embed the whole printf function in my executable. But printf can also display floats, so float support is also embedded, even if I never use floats on my programs. Same with the fependencies of GCC float support, etc... For solving this issue, some libraries provide a function called iprintf. It has the same functionality as printf, but without float support. Si if I don' use floats, and if I call iprintf instead of printf, I will not embed float support in my executable.

And finally, a nice feature. If I write printf("Hello, World!\n"), GCC will examine the code and say :
- Oho, a call to printf. The programmer wants to display a string.
- The string does not contain any % character, so the formatting capabilites of printf are useless here.
- The string is ended by \n
- So I can replace the original code by puts("Hello, World!"); (Note the \n has disappeared, because it is automatically appended by puts).
GCC has become clever ;-)

If I replace my printf line with the following:
printf("Hello, %s!\n", "World");
GCC can't use puts, so it must use the real printf implementation.
And the size of my Hello World bumps to 103 Ko :-(

--
Vincent Rivière