Bug in __register_exitproc() in __atexit.c

Joel Sherrill joel.sherrill@oarcorp.com
Tue Dec 22 02:53:00 GMT 2015

Enjoy your vacation Jeff. It has been a busy year for newlib. 

Personally I am very happy to see Cygwin and RTEMS share more in newlib as well as the FreeBSD alignment. It helps increase the number of configurations using the same lines of code. And that has positive impacts.


On December 21, 2015 10:55:50 AM CST, Jeff Johnston <jjohnstn@redhat.com> wrote:
>Thanks Freddie.  Patches applied.  I hope to cut the yearly snapshot
>either today
>or tomorrow before  I leave on vacation.
>-- Jeff J.
>----- Original Message -----
>> On Wednesday 16 of December 2015 17:25:23 Jeff Johnston wrote:
>> > 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 flags.
>> > 
>> > 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;
>> > #endif
>> > 
>> > 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.
>> OK, I've done it as described by Jeff and it works (; When only
>atexit() is
>> used, then the static instance of _on_exit_args struct is not linked.
>> either on_exit() or __cxa_atexit() are used, then the static instance
>> linked into the executable.
>> The static instance of _on_exit_args struct is used when "small
>> configuration is used. In theory it could be limited to "reent small"
>and "no
>> dynamic allocation in atexit()", but the approach I took seemed more
>> appropriate. on_exit()'s description in newlib's docs says that there
>are 32
>> statically allocated slots, so with this fix this is really true for
>> configurations.
>> I attach two patches that implement relevant changes - the first one
>> adds the static instance of _on_exit_args struct, while the second
>one is a
>> fix for malloc() usage in __register_exitproc(). The second patch is
>> identical to what I sent previously, but with changed commit message
>> no problem with _on_exit_args after the first patch is applied).
>> I've verified that all 8 combinations of relevant options (reent
>> dynamic alloc in atexit, global atexit) can be compiled.
>> Regards,
>> FCh


More information about the Newlib mailing list