Bug 2765

Summary: malloc.c strong_alias for free, malloc, realloc should be weak_alias
Product: glibc Reporter: Mike Bland <mbland>
Component: mallocAssignee: Not yet assigned to anyone <unassigned>
Status: REOPENED ---    
Severity: normal CC: dank, geir, glibc-bugs, ian, mec, neleai
Priority: P2 Flags: fweimer: security-
Version: 2.3.5   
Target Milestone: ---   
Host: i686-pc-linux-gnu Target: i486-linux-gnu
Build: i486-linux-gnu Last reconfirmed:

Description Mike Bland 2006-06-14 07:52:42 UTC
Building a static libc using 2.3.5 causes symbols from malloc.c (namely free,
malloc, and realloc) to be exported as non-weak due to the use of strong_alias.
 This causes problems when trying to interpose these functions during a static
link with definitions from another object.  For example:

mbland:~/test/static_interpose$ cat > foo.c
#include <stddef.h>

void *
malloc (size_t unused)
{
  return 0;
}

int
main (void)
{
  (void) malloc (0);
  return 0;
}
mbland:~/test/static_interpose$ gcc foo.c
mbland:~/test/static_interpose$ gcc -static foo.c
/usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../lib/libc.a(malloc.o): In function
`malloc':
: multiple definition of `malloc'
/tmp/ccCJFGaW.o:foo.c:(.text+0x0): first defined here
/usr/bin/ld: Warning: size of symbol `malloc' changed from 10 in /tmp/ccCJFGaW.o
to 397 in /usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../lib/libc.a(malloc.o)
collect2: ld returned 1 exit status

This example works in both cases with glibc-2.3.2, as these symbols were defined
as weak in the 2.3.2 libc.a.  Though I'm currently using glibc-2.3.5, I've
noticed that the strong_alias instances still persist in the current CVS.

I've built a static glibc with all the strong_alias instances changed to
weak_alias, and successfully built a statically-linked executable with these
symbols interposed.

--- glibc-2.3.5-orig/malloc/malloc.c    2006-06-13 13:49:00.000000000 -0700
+++ glibc-2.3.5/malloc/malloc.c         2006-06-13 15:35:20.000000000 -0700
@@ -5566,11 +5566,11 @@ weak_alias (__posix_memalign, posix_mema

 strong_alias (__libc_calloc, __calloc) weak_alias (__libc_calloc, calloc)
 strong_alias (__libc_free, __cfree) weak_alias (__libc_free, cfree)
-strong_alias (__libc_free, __free) strong_alias (__libc_free, free)
-strong_alias (__libc_malloc, __malloc) strong_alias (__libc_malloc, malloc)
+strong_alias (__libc_free, __free) weak_alias (__libc_free, free)
+strong_alias (__libc_malloc, __malloc) weak_alias (__libc_malloc, malloc)
 strong_alias (__libc_memalign, __memalign)
 weak_alias (__libc_memalign, memalign)
-strong_alias (__libc_realloc, __realloc) strong_alias (__libc_realloc, realloc)
+strong_alias (__libc_realloc, __realloc) weak_alias (__libc_realloc, realloc)
 strong_alias (__libc_valloc, __valloc) weak_alias (__libc_valloc, valloc)
 strong_alias (__libc_pvalloc, __pvalloc) weak_alias (__libc_pvalloc, pvalloc)
 strong_alias (__libc_mallinfo, __mallinfo)
Comment 1 Ulrich Drepper 2006-06-17 16:56:04 UTC
No.  You cannot replace just parts of the implementation.
Comment 2 dank 2006-06-17 19:35:02 UTC
But what about the use case of hooking malloc?
That requires the requested behavior, and yet it is
not replacing part of the implementation.
Comment 3 Geir Johansen 2009-03-12 22:06:55 UTC
This behavior prevents the TotalView memory debugging feature from working with
statically link codes:

$ cat main.c
int main() { }
$ gcc -static -g -L/opt/toolworks/totalview.8.6.0/linux-x86-64/lib
-ltvheap_cnl_static -lpthread main.c
/usr/lib/../lib64/libc.a(malloc.o): In function `free':
(.text+0x1c90): multiple definition of `free'
/opt/toolworks/totalview.8.6.0/linux-x86-64/lib/libtvheap_cnl_static.a:(.text+0x60d5):
first defined here
/usr/lib/../lib64/libc.a(malloc.o): In function `malloc':
(.text+0x3fe0): multiple definition of `malloc'
/opt/toolworks/totalview.8.6.0/linux-x86-64/lib/libtvheap_cnl_static.a:(.text+0x60e3):
first defined here
/usr/lib/../lib64/libc.a(malloc.o): In function `realloc':
(.text+0x4470): multiple definition of `realloc'
/opt/toolworks/totalview.8.6.0/linux-x86-64/lib/libtvheap_cnl_static.a:(.text+0x610d):
first defined here
collect2: ld returned 1 exit status
$
Comment 4 Ondrej Bilka 2012-12-27 17:00:51 UTC
Any insight why is strong alias in malloc et. al. needed?