undefined reference to `memset'

Peter Jeremy PeterJeremy at optushome.com.au
Thu Mar 24 14:46:09 PST 2005

On Thu, 2005-Mar-24 12:03:19 -0800, Vinod Kashyap wrote:
[  char x[100] = { 0 };  ]
>A statement like this (auto and not static)

I'd point out that this is the first time that you've mentioned that
the variable is auto.  Leaving out critical information will not
encourage people to help you.

> is necessary if you
>are dealing with re-entrancy.

This isn't completely true.  The preferred approach is:
	char	*x;
	x = malloc(100, MEM_POOL_xxx, M_ZERO | M_WAITOK);
(with a matching free() later).

>  Whatever the issues with wastage or
>bad performance, a programmer should definitely be able to do it,
>if he so desires.

Again, untrue.  The FreeBSD kernel is not a standard C environment.
Kernel stack is a relatively small, fixed size and using excessive
kernel stack will lead to panics.  Increasing the kernel stack size is
undesirable because it's expensive in RAM consumption.

>How is it then, that an explicit call to memset (like in my example) works?

The code
	auto char	x[100] = {0};
is equivalent to
	auto char	x[100];
	memset(x, 0, sizeof(x));
but memset only exists as a static inline function (defined in libkern.h).
If an explicit call to memset works then the problem would appear to be
that the compiler's implicit expansion is failing to detect the static
inline definition, and generating an external reference which can't be
satisfied.  This would seem to be a gcc bug.

>2. I should have mentioned that I don't see the problem if I am
>   building only the kernel module.  It happens only when I am building
>   the kernel integrating the module containing the example code.

This is the opposite of what you implied previously.  There are some
differences in how kernel modules are built so this 

How about posting a (short) compilable piece of C code that shows the
problem.  I would expect that an "nm" of the resultant object would
show "U memset" when the code was compiled for linking into the kernel
and "<some_address> t memset" or not reference memset at all when
compiled as a module.

Peter Jeremy

More information about the freebsd-stable mailing list