[ECOS] Re: uSTL hello world

Bart Veer bartv@ecoscentric.com
Tue Sep 29 14:28:00 GMT 2009

>>>>> "Jifl" == Jonathan Larmour <jifl@jifvik.org> writes:


    Jifl> Unfortunately we have another problem. I've looked at the
    Jifl> output of our recent compilers, and I see that
    Jifl> CYG_REFERENCE_OBJECT no longer appears to be effective :-(.
    Jifl> The reference gets removed by more aggressive compiler
    Jifl> optimisation at -O2. This is a much wider problem than just
    Jifl> stdin/out/err of course.

    Jifl> Solving that efficiently may be difficult (although I don't
    Jifl> consider the current implementation to necessarily be
    Jifl> efficient either, when it worked, anyway). Managing to get
    Jifl> the reference into .text/.rodata would probably be better
    Jifl> than .data/.bss. Even better would be if it were possible to
    Jifl> play tricks to get it into a non-loadable section, although
    Jifl> that starts to venture into the world of HAL-specific stuff.
    Jifl> I see a .comment section though. Something along those lines
    Jifl> might work (and by overridable by specific HALs if needed).

    Jifl> But I've experimented (admittedly briefly) and not got as
    Jifl> far as even making the reference. I've tried using various
    Jifl> attributes, nested functions, volatiles, embedding
    Jifl> asm(".word " #__object__ ";"), etc. It may be that we can
    Jifl> only do this properly using HAL-specific inline asm :-(. The
    Jifl> least worst idea I've had which actually creates a reference
    Jifl> is to have a real function cyg_reference_object(void*) and
    Jifl> call it with the address of the object to reference, but
    Jifl> which returns immediately.

    Jifl> Any other ideas? At least there are not many uses of

I think the following may do the trick:

// The unused attribute stops the compiler warning about the variable
// not being used.
// The used attribute prevents the compiler from optimizing it away.

#define NEW_CYG_REFERENCE_OBJECT(__object__)                            \
    CYG_MACRO_START                                                     \
    static const void*  __cygvar_discard_me__                           \
    __attribute__ ((unused, used)) = (const void*)&(__object__);        \

I have tried it in a simple testcase at -O2. The compiler does not
optimize away __cygvar_discard_me__ because the used attribute
prevents that. Hence the referenced object gets pulled in during the
link. Subsequently linker garbage collection eliminates
__cygvar_discard_me__. If the referenced object is a C++ object then
it will be preserved because of the KEEP(*.ctors) in the linker
script. So, even though it seems peculiar to have a variable both used
and unused, that combo appears to do what we want and should be fully

Looking at the gcc ChangeLog-2001, attribute(used) was added on
2001-10-18 so has probably been available since gcc 3.1 days. However
there is a comment in cyg_type.h associated with CYGBLD_ATTRIB_USED
claiming >= 3.3.2.

Let me know if it still does not solve the problem in al cases, and I
can investigate further.


Bart Veer                                   eCos Configuration Architect
eCosCentric Limited    The eCos experts      http://www.ecoscentric.com/
Barnwell House, Barnwell Drive, Cambridge, UK.      Tel: +44 1223 245571
Registered in England and Wales: Reg No 4422071.
       >>>> Visit us at ESC-UK  http://www.embedded.co.uk <<<<
       >>>> Oct 7-8 on Stand 433 at FIVE ISC, Farnborough <<<<

Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

More information about the Ecos-discuss mailing list