0

I have the following problem:

In some commercial project (ARM Cortex-M), we have pre-compiled libraries from a supplier.

The library contains code from different suppliers; some library functions use the standard malloc() function call, while others use the memory allocator of the operating system (in the example below: os_malloc()).

The malloc() function is taken from the libc.a standard library.

Both implementations seem to disturb each other. For this reason I'd like to replace the default malloc() (and free()) implementation by something like this:

void * malloc(uint32_t size)
{
    return os_malloc(size);
}

void free(void * ptr)
{
    os_free(ptr);
}

However, in this case I would have to remove the object file containing malloc() and free() from the libc.a library file - what I also don't want to do.

Is there a possibility to tell the GNU linker not to take some symbol from a library if it is already defined outside the library?

... or a possibility to tell the linker not to take a certain object file from a library?

... or a possibility to do something like PROVIDE(malloc = os_malloc); in the linker script although the symbol malloc is defined in some library?

Thank you very much.

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38

1 Answers1

1

However, in this case I would have to remove the object file containing malloc() and free() from the libc.a library file - what I also don't want to do.

That statement is false, and demonstrates that you do not understand how linkers work with archive libraries.

You do not in fact need to remove anything from libc.a (though the replacement may not work for other reasons).

Here is a good explainer.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Unfortunately, I have already worked with a linker (a commercial linker for TriCore or PowerPC - I don't remember) that refused linking with the message "duplicate symbol" if a library file contained a symbol that also occurs in an object file. This is why I asked the question. However, if it is guaranteed that GNU ld takes the symbol from the object file in such a case, I will simply implement the wrappers shown in my question ... – Martin Rosenau Nov 04 '22 at 09:24