Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop g++ from linking unwanted exception-handling code?

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?

like image 397
Neil Avatar asked Jun 15 '09 20:06

Neil


2 Answers

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.

like image 124
Evan Teran Avatar answered Sep 21 '22 12:09

Evan Teran


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.

like image 43
Dan Olson Avatar answered Sep 21 '22 12:09

Dan Olson