[newlib][PATCH] Newlib build is broken if configured with nano-malloc and non-reentrant system calls

Игорь Веневцев igor.venevtsev@gmail.com
Fri Jan 29 15:20:00 GMT 2016


Non-reentrant system calls version implies both MISSING_SYSCALL_NAMES
and REENTRANT_SYSCALL_PROVIDED macros to be defined.
Being coupled with --enable-newlib-nano-malloc knob it breaks the build:

bash-4.3$ ../newlib-2.3.0.20160104/configure CC_FOR_TARGET=gcc
AR_FOR_TARGET=ar RANLIB_FOR_TARGET=ranlib CFLAGS_FOR_TARGET="-m32
-DMISSING_SYSCALL_NAMES -DREENTRANT_SYSCALLS_PROVIDED"
--target=i386-elf --enable-newlib-nano-malloc && make

<...omitted output...>

../../../../../../newlib-2.3.0.20160104/newlib/libc/stdlib/nano-mallocr.c:
In function ‘_mallinfo_r’:
../../../../../../newlib-2.3.0.20160104/newlib/libc/stdlib/nano-mallocr.c:489:35:
error: macro "_sbrk_r" requires 2 arguments, but only 1 given
         sbrk_now = _sbrk_r(RCALL 0);
                                   ^
../../../../../../newlib-2.3.0.20160104/newlib/libc/stdlib/nano-mallocr.c:489:20:
error: ‘_sbrk_r’ undeclared (first use in this function)
         sbrk_now = _sbrk_r(RCALL 0);
                    ^
../../../../../../newlib-2.3.0.20160104/newlib/libc/stdlib/nano-mallocr.c:489:20:
note: each undeclared identifier is reported only once for each
function it appears in
Makefile:1512: recipe for target 'lib_a-nano-mallinfor.o' failed
make[8]: *** [lib_a-nano-mallinfor.o] Error 1

In case of non-reentrant system calls _sbrk_r became a macro with TWO
args (defined in reent.h):

#define _sbrk_r(__reent, __incr)  sbrk(__incr)

But in our case only one argument is present. (RCALL 0) is considered
as a single argument despite RCALL itself is a macro:)

So intermediate one-arg macro will be enough to expand args before
final _sbrk_r expansion:

#define _SBRK_R(X) _sbrk_r(X)

Here is a patch:

---
 newlib/libc/stdlib/nano-mallocr.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/newlib/libc/stdlib/nano-mallocr.c
b/newlib/libc/stdlib/nano-mallocr.c
index 8d6ca5c..4a2f468 100644
--- a/newlib/libc/stdlib/nano-mallocr.c
+++ b/newlib/libc/stdlib/nano-mallocr.c
@@ -46,6 +46,8 @@
 #define MAX(a,b) ((a) >= (b) ? (a) : (b))
 #endif

+#define _SBRK_R(X) _sbrk_r(X)
+
 #ifdef INTERNAL_NEWLIB

 #include <sys/config.h>
@@ -209,9 +211,9 @@ static void* sbrk_aligned(RARG malloc_size_t s)
 {
     char *p, *align_p;

-    if (sbrk_start == NULL) sbrk_start = _sbrk_r(RCALL 0);
+    if (sbrk_start == NULL) sbrk_start = _SBRK_R(RCALL 0);

-    p = _sbrk_r(RCALL s);
+    p = _SBRK_R(RCALL s);

     /* sbrk returns -1 if fail to allocate */
     if (p == (void *)-1)
@@ -222,7 +224,7 @@ static void* sbrk_aligned(RARG malloc_size_t s)
     {
         /* p is not aligned, ask for a few more bytes so that we have s
          * bytes reserved from align_p. */
-        p = _sbrk_r(RCALL align_p - p);
+        p = _SBRK_R(RCALL align_p - p);
         if (p == (void *)-1)
             return p;
     }
@@ -486,7 +488,7 @@ struct mallinfo nano_mallinfo(RONEARG)

     if (sbrk_start == NULL) total_size = 0;
     else {
-        sbrk_now = _sbrk_r(RCALL 0);
+        sbrk_now = _SBRK_R(RCALL 0);

         if (sbrk_now == (void *)-1)
             total_size = (size_t)-1;
--
2.1.0

Regards, Igor



More information about the Newlib mailing list