views:

615

answers:

3

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?

+2  A: 

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.

Dan Olson
Thanks Dan, -nostdlib does prevent the unwanted stuff, but it prevents all standard libraries from linking, including things like memcpy.I guess I could use -nostdlib, and create my own C library. It just seems like that shouldn't be necessary. Telling the compiler and linker that I don't want exceptions or start files, and that the application is freestanding (no OS) ought to be enough to convince it to do what I want.
Neil
I believe that you can use -nostdlib and link manually against the libraries you do use (avoiding, for example, linking against the math lib. That is, if you don't want to).
David Rodríguez - dribeas
Yeah, if you don't want libstdc++ you're free to use -nostdlib in combination with -lc -lm, etc.
Dan Olson
I think I've sorted this out. Global objects arrange for their destructors to be called using atexit(), which calls __register_exitproc, which in turn has a call to malloc. The key point is that global objects require malloc for destructors, even though storage for the actual object is allocated by the linker.So, changing the link command to include "-nostdlib /path/to/my/malloc.o -lc" forced my malloc to be linked before bringing in the standard C library. I assume I will need to add "-lstdc++" at some point, too.I was mistaken about exception handling being included.
Neil
A: 

You might try trapping new to see if it is really being called anyway.

New can occur implicitly under some circumstances, such as copy-construction.

You may be able to remove these by writing your code slightly differently.

http://www.linuxtopia.org/online_books/programming_books/thinking_in_c++/Chapter11_013.html

Alex Brown
+2  A: 

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.

Evan Teran