gold: Something special about malloc/calloc/realloc/free symbols?

Florian Weimer fweimer@redhat.com
Tue Jan 10 11:40:00 GMT 2017


On 01/08/2017 06:31 AM, Paul Smith wrote:
> Hi all.  I have a weird situation and I don't know what to try next.
>
> I'm creating a .so (on GNU/Linux using GCC 6.2 an binutils 2.27) and I
> want to export only a specific set of symbols.  To do this I create a
> linker map like this:
>
>   {
>       global:
>           symbol_one;
>           symbol_two;
>             ...
>       local:
>           *;
>   };
>
> and I add it to my link line with -Wl,version-script=...

You also need to export _IO_stdin_used if it is defined by 
libc_nonshared.a.  I have not yet found a good way to clean this up on 
the glibc side.

> It all works great, except for one thing.  In my shared library I'm also
> linking a memory management library (linked as a .a) which replaces all
> the standard memory functions: malloc, calloc, realloc, free.  I don't
> want these to be published by my .so because I want only my library to
> be able to access them, not a program that links my library.

This happens automatically to enable symbol interposition.

If you avoid symbol interposition, you will not be able to use quite a 
few glibc functions (strdup, getline, asprintf, realpath with a NULL 
argument, to name a few examples), and you must make sure that your 
library does not call realloc or free on a pointer supplied by the 
application (or the application does not call realloc or free on a 
pointer obtained from the library).

The best way to deal with this is to rename the allocator functions in 
the source code.  If that's not possible, maybe you can use a partial 
link (ld -r) with --wrap or some other ld functionality which renames 
the symbols.

Florian



More information about the Binutils mailing list