I'm developing an embedded application using GCC/G++ compiled for arm-eabi. Due to resource constraints, I'm trying to disable the standard C++ exception handling. I'm compiling the code with "-fno-exceptions -nostartfiles -ffreestanding".
When a global instance of a class exists, and that class contains an instance of another class as a member, then a lot of exception handling code is being linked in. This wouldn't be so bad, except that it's also bringing in lots of stdio stuff, like printf, fopen, fclose and other FILE functions. This application has no filesystem, and even if it did, these functions waste too much code space.
I understand that even with -fno-exceptions, G++ links in an operator new that uses exceptions, because the library doesn't have a non-exception-using operator new (except new(nothrow)). I created replacements for operator new and delete, and these are linked into the output as well as the unwanted standard-library functions.
What puzzles me is that I'm not calling new anywhere. It's only when a global object contains another object that all this code is linked in.
For example:
class UartA {
...
private:
Ringbuffer* rxbuf;
};
class UartB {
...
private:
Ringbuffer rxbuf;
};
If a global instance of UartA is created, the exception handling, operator new, and stdio stuff are not linked in. This is what I want.
If a global instance of UartB is created (where rxbuf is an instance instead of a pointer), the unwanted code is linked in.
Neither UartA nor UartB use operator new, exceptions or stdio. They differ only by the type of rxbuf.
Can you suggest how to prevent linking the extra code? Also, why is this being linked in for UartB, but not UartA?
since you are basically doing the things that an OS developer would do to get a standalone c or c++ environment. You may want to look into just using a custom linker script. You just need to be careful because things like global constructors no longer happen automatically..but you will also not get anything you didn't explicitly ask for (and it isn't hard to write the code to call the global constructors). Here's the linker script from my OS.
OUTPUT_FORMAT("elf32-i386")
ENTRY(start)
virt = 0xc0100000; /* 3.1 gig */
phys = 0x00100000; /* 1 meg */
SECTIONS
{
.text virt : AT(phys)
{
code = .; _code = .; __code = .;
*(.text)
*(.gnu.linkonce.t*)
. = ALIGN(4096);
}
.rodata : AT(phys + (rodata - code))
{
rodata = .; _rodata = .; __rodata = .;
*(.rodata*)
*(.gnu.linkonce.r*)
__CTOR_LIST__ = .;
LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
*(.ctors)
LONG(0)
__CTOR_END__ = .;
__DTOR_LIST__ = .;
LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
*(.dtors)
LONG(0)
__DTOR_END__ = .;
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .; _data = .; __data = .;
*(.data)
*(.gnu.linkonce.d*)
. = ALIGN(4096);
}
.tbss : AT(phys + (tbss - code))
{
tbss = .; _tbss = .; __tbss = .;
*(.tbss)
*(.tbss.*)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .; _bss = .; __bss = .;
*(.bss)
*(.bss.*)
*(COMMON)
*(.gnu.linkonce.b*)
. = ALIGN(4096);
}
end = .; _end = .; __end = .;
}
It probably does more than you need (aligns sections to 4k boundaries, has all symbols at > 3GB mark) but is a good starting place.
You can use it like this:
ld -T link_script.ld *.o -lc -o appname
the "-lc" should link in libc as well if that's what you want.
I think the closest you can get is compiling and linking with -fno-exceptions and -fno-rtti. If there's a better way to get rid of the rest, I'd be glad to hear it myself.
As far as getting rid of new, try -nostdlib.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With