Autoconf and RPL MALLOC

From Buici

Jump to: navigation, search

When using Autoconf to cross compile packages, you may find that the link fails because of the absence of a function, rpl_malloc. First, I'll explain how this happens and the I'll explain what can be done about it.

AC_FUNC_MALLOC

Autoconf exists to check for standards compliance among toolchains and libraries. It does it's best to provide a uniform build environment for a piece of software given that the original author may not be familiar with the differences between target platforms.

The AC_FUNC_MALLOC macro makes sure that the malloc function, when passed a zero argument, returns a valid memory block instead of NULL. This behaviour conforms to the GNU C library. Normally, this is a reasonable test that autoconf makes at build-time. In the case of cross-compilation, however, autoconf cannot execute a program to verify proper behavior. It makes the conservative assumption that the target library will produce non-conforming code.

Failure of this test causes autconf to replace malloc() calls with rpl_malloc() calls. At link time, if there is no rpl_malloc() function, the linker will fail with an error describing the missing symbol. The autoconf documentation recommends adding this _harmless_ code to the application to implement the function.

#if HAVE_CONFIG_H
# include <config.h>
#endif
#undef malloc
     
#include <sys/types.h>

void *malloc ();

/* Allocate an N-byte block of memory from the heap.
   If N is zero, allocate a 1-byte block.  */
    
void* rpl_malloc (size_t n)
{
  if (n == 0)
    n = 1;
  return malloc (n);
}

This might be a feasible thing to do in another universe, but in the reality where cross-compiling packages is already a substantial amount of work, it is unreasonable to suggest that a package builder will patch code into an application being where the target will use a GNU C library.

Fixing Autoconf

The best solution would be to fix autoconf such that it detects target binaries being link with the GNU C library and assumes conforming behavior for malloc().

A lesser solution would be to add a switch to the configure script that informs it that the target links with the GNU C library.

Hacking Through the Problem

The only reasonable solution, at the time of autoconf-2.59, is to define an environment variable that forces the test to pass. Define it in the environment prior to calling ./configure and the script will act as if the AC_FUNC_MALLOC check has passed.

export ac_cv_func_malloc_0_nonnull=yes

--Elf 17:09, 6 April 2006 (PDT)

Personal tools