Bug in __register_exitproc() in __atexit.c

Jeff Johnston jjohnstn@redhat.com
Thu Dec 17 21:24:00 GMT 2015

----- Original Message -----
> On Monday 14 of December 2015 16:23:21 Jeff Johnston wrote:
> > I think I like the weak reference idea.  Have register_exitproc make a weak
> > reference and have on_exit and __cxa_atexit make a strong reference.  If
> > someone uses the configuration flags "and" uses on_exit or __cxa_atexit,
> > then they pull in the additional static storage and that is the cost of
> > using them.
> OK, I know how to make the _on_exit_args static instance weak in
> __register_exitproc(), but how do I make force it to be linked in when
> on_exit() or __cxa_atexit() is used? In other words - how do I make it non-
> weak when specific functions - that don't use this variable directly - are
> linked?

Use the same tactic that is used in __atexit.c.

Declare a static structure in a new .c file and create a const pointer to it
called __on_exit_args.  (see libc/reent/impure.c for an example of how this is done
for the reent struct).

In __register_exitproc, declare it as weak and refer to it when needed due to the

In on_exit and __cxa_exit, use the flags to determine when to declare a dummy global variable
such that you have:

#if defined(FLAGS_NEEDED...)
const void *__some_dummy_var = &__on_exit_args;

Applications that link in on_exit or __cxa_exit will drag in the struct if configured with the
flags.  Other applications will not get the added bloat of the struct.

I have a simple example library if it is still unclear.

-- Jeff J.  

> Thanks for any help!
> Regards,
> FCh

More information about the Newlib mailing list