diff mbox

Fix stpcpy / mempcpy namespace (bug 17573)

Message ID alpine.DEB.2.10.1411101817450.6767@digraph.polyomino.org.uk
State New
Headers show

Commit Message

Joseph Myers Nov. 10, 2014, 6:18 p.m. UTC
Various glibc functions call __stpcpy and __mempcpy for namespace
reasons instead of plain stpcpy and mempcpy.  But __stpcpy and
__mempcpy are macros that call __builtin_stpcpy and __builtin_mempcpy,
and unless GCC optimizes the calls, they end up calling the C
functions stpcpy and mempcpy.

For calls from within shared libc, libc_hidden_builtin_proto ensures
that calls to those C functions are in turn mapped to call __GI_stpcpy
and __GI_mempcpy.  However, for static libc, and for calls from shared
libraries other than libc, the ELF symbols stpcpy and mempcpy end up
getting called, breaking the ISO C namespace (in the case of stpcpy)
or glibc conventions about not relying on the "future library
directions" reservations (in the case of mempcpy).

This patch fixes this by adding declarations of these functions to
include/string.h, under an appropriate condition, with __asm__ used to
change the assembler name used for calls; this fixes one of the bugs
shown in the sample output of my proposed tests for such namespace
issues <https://sourceware.org/ml/libc-alpha/2014-11/msg00157.html>
(the mempcpy case was previously discussed, and the approach for the
fix is as I suggested in
<https://sourceware.org/ml/libc-alpha/2013-02/msg00063.html>).
(Siddhesh, the new NOT_IN_libc conditional will I expect need updating
as part of your overhaul of such conditionals.)

Tested for x86_64 with the testsuite; also checked that dcigettext.o
(an example previously noted of undesired calls to stpcpy and mempcpy)
now calls __stpcpy and __mempcpy instead, as do non-libc shared
libraries (__stpcpy and __mempcpy were already exported from shared
libc).  Disassembly of installed shared libraries isn't easy to
compare because of reordered PLT entries resulting from the change in
functions called (libnsl, libnss_compat, libnss_dns, libnss_files,
libnss_hesiod, libnss_nis, libnss_nisplus, libpthread, librt all have
such changes).

2014-11-10  Joseph Myers  <joseph@codesourcery.com>

	[BZ #17573]
	* include/string.h [NOT_IN_libc || !SHARED] (mempcpy): Declare
	with asm name __mempcpy.
	[NOT_IN_libc || !SHARED] (stpcpy): Declare with asm name __stpcpy.

Comments

Roland McGrath Nov. 12, 2014, 9:44 p.m. UTC | #1
Looks OK.
Andreas Schwab Nov. 13, 2014, 10:22 a.m. UTC | #2
Joseph Myers <joseph@codesourcery.com> writes:

> 	[BZ #17573]
> 	* include/string.h [NOT_IN_libc || !SHARED] (mempcpy): Declare
> 	with asm name __mempcpy.
> 	[NOT_IN_libc || !SHARED] (stpcpy): Declare with asm name __stpcpy.

gcc mempcpy.c -c -std=gnu99 -fgnu89-inline  -D_FORTIFY_SOURCE=2 -O2 -U_FORTIFY_SOURCE -Wall -Winline -Wundef -Wwrite-strings -fasynchronous-unwind-tables -fmerge-all-constants -fmessage-length=0 -frounding-math -funwind-tables -g -grecord-gcc-switches -mtune=power3 -mhard-float -Wstrict-prototypes -Werror=implicit-function-declaration -mlong-double-128         -I../include -I/home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string  -I/home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base  -I../sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu  -I../sysdeps/unix/sysv/linux/powerpc/powerpc32  -I../sysdeps/unix/sysv/linux/powerpc  -I../sysdeps/powerpc/nptl  -I../sysdeps/unix/sysv/linux/include -I../sysdeps/unix/sysv/linux  -I../sysdeps/nptl  -I../sysdeps/pthread  -I../sysdeps/gnu  -I../sysdeps/unix/inet  -I../sysdeps/unix/sysv  -I../sysdeps/unix/powerpc  -I../sysdeps/unix  -I../sysdeps/posix  -I../sysdeps/powerpc/powerpc32/fpu  -I../sysdeps/powerpc/powerpc32  -I../sysdeps/wordsize-32  -I../sysdeps/powerpc/fpu  -I../sysdeps/powerpc  -I../sysdeps/ieee754/ldbl-128ibm  -I../sysdeps/ieee754/ldbl-opt  -I../sysdeps/ieee754/dbl-64  -I../sysdeps/ieee754/flt-32  -I../sysdeps/ieee754  -I../sysdeps/generic  -I.. -I../libio -I.   -D_LIBC_REENTRANT -include ../include/libc-symbols.h       -o /home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string/mempcpy.o -MD -MP -MF /home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string/mempcpy.o.dt -MT /home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string/mempcpy.o
/tmp/ccXDS5DL.s: Assembler messages:
/tmp/ccXDS5DL.s:40: Error: symbol `__mempcpy' is already defined

Andreas.
Adhemerval Zanella Nov. 13, 2014, 11:06 a.m. UTC | #3
On 13-11-2014 08:22, Andreas Schwab wrote:
> Joseph Myers <joseph@codesourcery.com> writes:
>
>> 	[BZ #17573]
>> 	* include/string.h [NOT_IN_libc || !SHARED] (mempcpy): Declare
>> 	with asm name __mempcpy.
>> 	[NOT_IN_libc || !SHARED] (stpcpy): Declare with asm name __stpcpy.
> gcc mempcpy.c -c -std=gnu99 -fgnu89-inline  -D_FORTIFY_SOURCE=2 -O2 -U_FORTIFY_SOURCE -Wall -Winline -Wundef -Wwrite-strings -fasynchronous-unwind-tables -fmerge-all-constants -fmessage-length=0 -frounding-math -funwind-tables -g -grecord-gcc-switches -mtune=power3 -mhard-float -Wstrict-prototypes -Werror=implicit-function-declaration -mlong-double-128         -I../include -I/home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string  -I/home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base  -I../sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu  -I../sysdeps/unix/sysv/linux/powerpc/powerpc32  -I../sysdeps/unix/sysv/linux/powerpc  -I../sysdeps/powerpc/nptl  -I../sysdeps/unix/sysv/linux/include -I../sysdeps/unix/sysv/linux  -I../sysdeps/nptl  -I../sysdeps/pthread  -I../sysdeps/gnu  -I../sysdeps/unix/inet  -I../sysdeps/unix/sysv  -I../sysdeps/unix/powerpc  -I../sysdeps/unix  -I../sysdeps/posix  -I../sysdeps/powerpc/powerpc32/fpu  -I../sysdeps/powerpc/powerpc32  -I../sysdeps/wordsize-32  -I../sysdeps/powerpc/fpu  -I../sysdeps/powerpc  -I../sysdeps/ieee754/ldbl-128ibm  -I../sysdeps/ieee754/ldbl-opt  -I../sysdeps/ieee754/dbl-64  -I../sysdeps/ieee754/flt-32  -I../sysdeps/ieee754  -I../sysdeps/generic  -I.. -I../libio -I.   -D_LIBC_REENTRANT -include ../include/libc-symbols.h       -o /home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string/mempcpy.o -MD -MP -MF /home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string/mempcpy.o.dt -MT /home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string/mempcpy.o
> /tmp/ccXDS5DL.s: Assembler messages:
> /tmp/ccXDS5DL.s:40: Error: symbol `__mempcpy' is already defined
>
> Andreas.
>
This also breaks powerpc64 build with a similar issue.  I have tried to disable
multiarch for statics builds, as x86_64 custom implementation does, but it also
breaks.
Stefan Liebler Nov. 13, 2014, 11:37 a.m. UTC | #4
On 11/13/2014 11:22 AM, Andreas Schwab wrote:
> Joseph Myers <joseph@codesourcery.com> writes:
>
>> 	[BZ #17573]
>> 	* include/string.h [NOT_IN_libc || !SHARED] (mempcpy): Declare
>> 	with asm name __mempcpy.
>> 	[NOT_IN_libc || !SHARED] (stpcpy): Declare with asm name __stpcpy.
>
> gcc mempcpy.c -c -std=gnu99 -fgnu89-inline  -D_FORTIFY_SOURCE=2 -O2 -U_FORTIFY_SOURCE -Wall -Winline -Wundef -Wwrite-strings -fasynchronous-unwind-tables -fmerge-all-constants -fmessage-length=0 -frounding-math -funwind-tables -g -grecord-gcc-switches -mtune=power3 -mhard-float -Wstrict-prototypes -Werror=implicit-function-declaration -mlong-double-128         -I../include -I/home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string  -I/home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base  -I../sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu  -I../sysdeps/unix/sysv/linux/powerpc/powerpc32  -I../sysdeps/unix/sysv/linux/powerpc  -I../sysdeps/powerpc/nptl  -I../sysdeps/unix/sysv/linux/include -I../sysdeps/unix/sysv/linux  -I../sysdeps/nptl  -I../sysdeps/pthread  -I../sysdeps/gnu  -I../sysdeps/unix/inet  -I../sysdeps/unix/sysv  -I../sysdeps/unix/powerpc  -I../sysdeps/unix  -I../sysdeps/posix  -I../sysdeps/powerpc/powerpc32/fpu  -I../sysdeps/powerpc/powerpc32  -I../sysdeps/wordsize-32  -I../sysd
eps/powerpc/fpu  -I../sysdeps/powerpc  -I../sysdeps/ieee754/ldbl-128ibm  -I../sysdeps/ieee754/ldbl-opt  -I../sysdeps/ieee754/dbl-64  -I../sysdeps/ieee754/flt-32  -I../sysdeps/ieee754  -I../sysdeps/generic  -I.. -I../libio -I.   -D_LIBC_REENTRANT -include ../include/libc-symbols.h       -o /home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string/mempcpy.o -MD -MP -MF /home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string/mempcpy.o.dt -MT /home/abuild/rpmbuild/BUILD/glibc-2.20.90/cc-base/string/mempcpy.o
> /tmp/ccXDS5DL.s: Assembler messages:
> /tmp/ccXDS5DL.s:40: Error: symbol `__mempcpy' is already defined
>
> Andreas.
>

Hi,

I get the same error message on s390.
When building with multiple jobs, i get the following error in addition:
/tmp/ccRwSbmU.s:23: Error: symbol `__stpcpy' is already defined

Bye
Stefan.
Jiong Wang Nov. 13, 2014, 3:45 p.m. UTC | #5
On 13/11/14 11:37, Stefan Liebler wrote:
>
> Hi,
>
> I get the same error message on s390.
> When building with multiple jobs, i get the following error in addition:
> /tmp/ccRwSbmU.s:23: Error: symbol `__stpcpy' is already defined

  + aarch64

>
> Bye
> Stefan.
>
>
>
>
diff mbox

Patch

diff --git a/include/string.h b/include/string.h
index 8323412..2603e9c 100644
--- a/include/string.h
+++ b/include/string.h
@@ -113,6 +113,13 @@  libc_hidden_builtin_proto (strspn)
 libc_hidden_builtin_proto (strstr)
 libc_hidden_builtin_proto (ffs)
 
+#if defined NOT_IN_libc || !defined SHARED
+/* Redirect calls to __builtin_mempcpy and __builtin_stpcpy to call
+   __mempcpy and __stpcpy if not inlined.  */
+extern __typeof (mempcpy) mempcpy __asm__ ("__mempcpy");
+extern __typeof (stpcpy) stpcpy __asm__ ("__stpcpy");
+#endif
+
 # ifndef _ISOMAC
 #  ifndef index
 #   define index(s, c)	(strchr ((s), (c)))