Bug in __register_exitproc() in __atexit.c

Freddie Chopin freddie_chopin@op.pl
Mon Dec 21 10:26:00 GMT 2015

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. When 
either on_exit() or __cxa_atexit() are used, then the static instance is 
linked into the executable.

The static instance of _on_exit_args struct is used when "small reent" 
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 all 

I attach two patches that implement relevant changes - the first one actually 
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 mostly 
identical to what I sent previously, but with changed commit message (there's 
no problem with _on_exit_args after the first patch is applied).

I've verified that all 8 combinations of relevant options (reent small, 
dynamic alloc in atexit, global atexit) can be compiled.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Add-static-instance-of-_on_exit_args-for-_REENT_SMAL.patch
Type: text/x-patch
Size: 10628 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/newlib/attachments/20151221/efff70fb/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-Fix-for-_ATEXIT_DYNAMIC_ALLOC-in-__register_exitproc.patch
Type: text/x-patch
Size: 1387 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/newlib/attachments/20151221/efff70fb/attachment-0001.bin>

More information about the Newlib mailing list