+2015-12-21 Freddie Chopin <freddie.chopin@gmail.com>
+
+ * libc/stdlib/on_exit_args.{c,h}: New files.
+ * libc/stdlib/Makefile.am: Add new source file.
+ * libc/stdlib/Makefile.in: Regenerate.
+ * libc/stdlib/__atexit.c (__register_exitproc): Initialize
+ _on_exit_args_ptr field of _GLOBAL_ATEXIT on first run.
+ * libc/stdlib/on_exit.c: Force linking of static instance of
+ _on_exit_args.
+ * libc/stdlib/cxa_atexit.c: Likewise.
+
2015-12-17 Anton Kolesov <Anton.Kolesov@synopsys.com>
* libc/machine/arc/asm.h: Define new GCC definition for old compiler.
* libc/machine/arc/memcmp.S: Likewise.
* libc/machine/arc/memcpy-archs.S: Likewise.
* libc/machine/arc/memcpy-bs.S: Likewise.
- * libc/machine/arc/memcpy.S: Likewise.
+ * libc/machine/arc/memcpy.S: Likewise. * libc/machine/arc/memset-archs.S: Likewise.
* libc/machine/arc/memset-archs.S: Likewise.
* libc/machine/arc/memset-bs.S: Likewise.
* libc/machine/arc/memset.S: Likewise.
lib_a-mbstowcs.$(OBJEXT) lib_a-mbstowcs_r.$(OBJEXT) \
lib_a-mbtowc.$(OBJEXT) lib_a-mbtowc_r.$(OBJEXT) \
lib_a-mlock.$(OBJEXT) lib_a-mprec.$(OBJEXT) \
- lib_a-mstats.$(OBJEXT) lib_a-quick_exit.$(OBJEXT) \
- lib_a-rand.$(OBJEXT) lib_a-rand_r.$(OBJEXT) \
- lib_a-realloc.$(OBJEXT) lib_a-reallocf.$(OBJEXT) \
- lib_a-sb_charsets.$(OBJEXT) lib_a-strtod.$(OBJEXT) \
- lib_a-strtodg.$(OBJEXT) lib_a-strtol.$(OBJEXT) \
- lib_a-strtorx.$(OBJEXT) lib_a-strtoul.$(OBJEXT) \
- lib_a-utoa.$(OBJEXT) lib_a-wcstod.$(OBJEXT) \
- lib_a-wcstol.$(OBJEXT) lib_a-wcstoul.$(OBJEXT) \
- lib_a-wcstombs.$(OBJEXT) lib_a-wcstombs_r.$(OBJEXT) \
- lib_a-wctomb.$(OBJEXT) lib_a-wctomb_r.$(OBJEXT) \
- $(am__objects_1)
+ lib_a-mstats.$(OBJEXT) lib_a-on_exit_args.$(OBJEXT) \
+ lib_a-quick_exit.$(OBJEXT) lib_a-rand.$(OBJEXT) \
+ lib_a-rand_r.$(OBJEXT) lib_a-realloc.$(OBJEXT) \
+ lib_a-reallocf.$(OBJEXT) lib_a-sb_charsets.$(OBJEXT) \
+ lib_a-strtod.$(OBJEXT) lib_a-strtodg.$(OBJEXT) \
+ lib_a-strtol.$(OBJEXT) lib_a-strtorx.$(OBJEXT) \
+ lib_a-strtoul.$(OBJEXT) lib_a-utoa.$(OBJEXT) \
+ lib_a-wcstod.$(OBJEXT) lib_a-wcstol.$(OBJEXT) \
+ lib_a-wcstoul.$(OBJEXT) lib_a-wcstombs.$(OBJEXT) \
+ lib_a-wcstombs_r.$(OBJEXT) lib_a-wctomb.$(OBJEXT) \
+ lib_a-wctomb_r.$(OBJEXT) $(am__objects_1)
am__objects_3 = lib_a-cxa_atexit.$(OBJEXT) \
lib_a-cxa_finalize.$(OBJEXT) lib_a-drand48.$(OBJEXT) \
lib_a-ecvtbuf.$(OBJEXT) lib_a-efgcvt.$(OBJEXT) \
exit.lo gdtoa-gethex.lo gdtoa-hexnan.lo getenv.lo getenv_r.lo \
itoa.lo labs.lo ldiv.lo ldtoa.lo malloc.lo mblen.lo mblen_r.lo \
mbstowcs.lo mbstowcs_r.lo mbtowc.lo mbtowc_r.lo mlock.lo \
- mprec.lo mstats.lo quick_exit.lo rand.lo rand_r.lo realloc.lo \
- reallocf.lo sb_charsets.lo strtod.lo strtodg.lo strtol.lo \
- strtorx.lo strtoul.lo utoa.lo wcstod.lo wcstol.lo wcstoul.lo \
- wcstombs.lo wcstombs_r.lo wctomb.lo wctomb_r.lo \
- $(am__objects_8)
+ mprec.lo mstats.lo on_exit_args.lo quick_exit.lo rand.lo \
+ rand_r.lo realloc.lo reallocf.lo sb_charsets.lo strtod.lo \
+ strtodg.lo strtol.lo strtorx.lo strtoul.lo utoa.lo wcstod.lo \
+ wcstol.lo wcstoul.lo wcstombs.lo wcstombs_r.lo wctomb.lo \
+ wctomb_r.lo $(am__objects_8)
am__objects_10 = cxa_atexit.lo cxa_finalize.lo drand48.lo ecvtbuf.lo \
efgcvt.lo erand48.lo jrand48.lo lcong48.lo lrand48.lo \
mrand48.lo msize.lo mtrim.lo nrand48.lo rand48.lo seed48.lo \
dtoastub.c environ.c envlock.c eprintf.c exit.c gdtoa-gethex.c \
gdtoa-hexnan.c getenv.c getenv_r.c itoa.c labs.c ldiv.c \
ldtoa.c malloc.c mblen.c mblen_r.c mbstowcs.c mbstowcs_r.c \
- mbtowc.c mbtowc_r.c mlock.c mprec.c mstats.c quick_exit.c \
- rand.c rand_r.c realloc.c reallocf.c sb_charsets.c strtod.c \
- strtodg.c strtol.c strtorx.c strtoul.c utoa.c wcstod.c \
- wcstol.c wcstoul.c wcstombs.c wcstombs_r.c wctomb.c wctomb_r.c \
- $(am__append_1)
+ mbtowc.c mbtowc_r.c mlock.c mprec.c mstats.c on_exit_args.c \
+ quick_exit.c rand.c rand_r.c realloc.c reallocf.c \
+ sb_charsets.c strtod.c strtodg.c strtol.c strtorx.c strtoul.c \
+ utoa.c wcstod.c wcstol.c wcstoul.c wcstombs.c wcstombs_r.c \
+ wctomb.c wctomb_r.c $(am__append_1)
@NEWLIB_NANO_MALLOC_FALSE@MALIGNR = malignr
@NEWLIB_NANO_MALLOC_TRUE@MALIGNR = nano-malignr
@NEWLIB_NANO_MALLOC_FALSE@MALLOPTR = malloptr
lib_a-mstats.obj: mstats.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-mstats.obj `if test -f 'mstats.c'; then $(CYGPATH_W) 'mstats.c'; else $(CYGPATH_W) '$(srcdir)/mstats.c'; fi`
+lib_a-on_exit_args.o: on_exit_args.c
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-on_exit_args.o `test -f 'on_exit_args.c' || echo '$(srcdir)/'`on_exit_args.c
+
+lib_a-on_exit_args.obj: on_exit_args.c
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-on_exit_args.obj `if test -f 'on_exit_args.c'; then $(CYGPATH_W) 'on_exit_args.c'; else $(CYGPATH_W) '$(srcdir)/on_exit_args.c'; fi`
+
lib_a-quick_exit.o: quick_exit.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-quick_exit.o `test -f 'quick_exit.c' || echo '$(srcdir)/'`quick_exit.c
--- /dev/null
+/*
+ * Static instance of _on_exit_args struct.
+ *
+ * When _REENT_SMALL is used, _atexit struct only contains a pointer to
+ * _on_exit_args struct, so this was always allocated with malloc() - even for
+ * the first 32 calls of atexit()-like functions, which are guaranteed to
+ * succeed, but could fail because of "out of memory" error. This is even worse
+ * when _ATEXIT_DYNAMIC_ALLOC is _NOT_ defined, in which case malloc() is not
+ * used by internals of atexit()-like functions. In such configuration all calls
+ * to the functions that need _on_exit_args struct (on_exit() and
+ * __cxa_atexit()) would fail.
+ *
+ * Thats why a static instance of _on_exit_args struct is provided for
+ * _REENT_SMALL configuration. This way the first 32 calls to atexit()-like
+ * functions don't need malloc() and will always succeed.
+ *
+ * Because this struct is not needed for "normal" atexit(), it is used as a weak
+ * reference in __register_exitproc(), but any use of on_exit() or
+ * __cxa_atexit() will force it to be linked.
+ */
+
+#include <reent.h>
+
+#ifdef _REENT_SMALL
+
+static struct _on_exit_args _on_exit_args_instance = {{_NULL}, {_NULL}, 0, 0};
+
+struct _on_exit_args * const __on_exit_args = &_on_exit_args_instance;
+
+#endif /* def _REENT_SMALL */