MinGW gcc malloc issue with -fno-builtin

I am having a strange problem with malloc and free in MinGW gcc

It can best be illustrated with the following program(notice no external headers)

void free(void* p)
{
    write(1,"called free\n",12);
}
int main()
{
}

I am compiling with the following command: gcc -g -fno-builtin test.c

Running this program, I expect to get no output, yet when I run the program I get the following output:

called free
called free
called free
called free

Using gdb, I found free being called in mingwrt-4.0.3-1-mingw32-src\mingwrt-4.0.3-1-mingw32-src\src\libcrt\misc\glob.c

Is there anyway to turn this off? I would have thought that specifying -fno-builtin would have made my program not expect to be able to call things like free

EDIT: I should clarify that I have written my own memory library with malloc and free and the issue is that I do not want MinGW to call those functions. I also do not want to use any external libraries like stdio or stdlib.

I have currently implemented a simple fix of renaming my malloc and free but ideally I would be able to name them malloc and free and not have to worry about any external code calling them. If someone could also explain why MinGW even needs to malloc memory in the simple program I wrote above, I would appreciate it as well.

Answers


The fact that you are using the -fno-builtin flag does not mean that the compiler will not call built-in functions.

According to the documentation, it means that the compiler will generate a full function call for the built-in functions, instead of using specials optimizations like generating code for the function in place.

From the GCC man page:

Don't recognize built-in functions that do not begin with _builtin as prefix.

GCC normally generates special code to handle certain built-in functions more efficiently; for instance, calls to "alloca" may become single instructions which adjust the stack directly, and calls to "memcpy" may become inline copy loops. The resulting code is often both smaller and faster, but since the function calls no longer appear as such, you cannot set a breakpoint on those calls, nor can you change the behavior of the functions by linking with a different library. In addition, when a function is recognized as a built-in function, GCC may use information about that function to warn about problems with calls to that function, or to generate more efficient code, even if the resulting code still contains calls to that function. For example, warnings are given with -Wformat for bad calls to "printf", when "printf" is built in, and "strlen" is known not to modify global memory.

Despite being able to override the implementation of malloc and free, I believe the only way to remove the calls that the compiler add to these functions to initialize the program environment is only possible to remove with the argument -nostartfiles. The only problem with this, is that you will be responsible to start up the environment, since the compiler will not provide it anymore for you.

This article is a bit off topic, but I believe you can find more answers and details over there:

A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux


Need Your Help

LargeInteger's equivalent of BigInteger's and?

java binary biginteger jscience

LargeInteger doesn't appear to have an equivalent function to BigInteger's and.

embed codebehind results in javascript mvc3

javascript asp.net-mvc-3

I have a mvc3 view (map.cshtml) that has a javascript section in it.