Use of malloc primitives in Genode

Norman Feske norman.feske at ...1...
Tue Mar 15 21:30:29 CET 2011


Hi Amadeo,

> When I try to build the app below in Genode, the following linking error
> happens:
> 
> …/main.cc:6: undefined reference to `malloc(unsigned int)'
> …/main.cc:8: undefined reference to `free(void*)'
> 
> What am I missing here? Thanks!

because you are using 'malloc' from within a C++ source file and the
function in mini_c's 'stdlib.h' is not declared as 'extern "C"', the
compiler generates a mangled C++ symbol. You can see this when looking
at the object file for 'main.cc':

> nm test/amadeo/main.o

           U _Z4freePv
           U _Z6mallocj
           U __gxx_personality_v0
  00000000 T main

Simple fix: Just put the the '#include <stdlib.h>' in an 'extern "C"' block:

  extern "C" {
  #include <stdlib.h>
  }

In general, I would not recommend to use 'mini_c' because this library
is just meant as a support library for our demo applications (it is
actually part of the 'demo' repository). Hence, 'mini_c' is only tested
for our particular demos.

I would try to avoid libc functionality altogether, e.g., by allocation
memory via Genode's heap:

  #include <base/env.h>

  ...

  int *v = (int *)Genode::env()->heap()->alloc(N*sizeof(int));

Alternatively, if you really desire libc functionality, I'd recommend
using our C runtime. For this, you will need to add 'libc' to your
'LIBS' declaration in our 'target.mk' file. Optionally, you may also
want to add 'libc_log' if you like to use libc's fully-fledged 'printf'
to operate with Genode's LOG service. Also make sure that you have
listed the libc repository the 'REPOSITORIES' declaration in your
'<build-dir>/etc/build.conf' file. When booting, do not forget to load
'ldso', 'libc.lib.so', and 'libc_log.lib.so' as boot modules.

Cheers
Norman

-- 
Dr.-Ing. Norman Feske
Genode Labs

http://www.genode-labs.com · http://genode.org

Genode Labs GmbH · Amtsgericht Dresden · HRB 28424 · Sitz Dresden
Geschäftsführer: Dr.-Ing. Norman Feske, Christian Helmuth




More information about the users mailing list