From patchwork Wed Apr 16 10:36:01 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vineet Gupta X-Patchwork-Id: 339545 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from whitealder.osuosl.org (whitealder.osuosl.org [140.211.166.138]) by ozlabs.org (Postfix) with ESMTP id C4BE61400D5 for ; Wed, 16 Apr 2014 20:37:25 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 3537E8C031; Wed, 16 Apr 2014 10:37:24 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id rUnuUkQgIdpk; Wed, 16 Apr 2014 10:37:22 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by whitealder.osuosl.org (Postfix) with ESMTP id 7A4B88BF5D; Wed, 16 Apr 2014 10:37:22 +0000 (UTC) X-Original-To: uclibc@lists.busybox.net Delivered-To: uclibc@osuosl.org Received: from hemlock.osuosl.org (hemlock.osuosl.org [140.211.166.133]) by ash.osuosl.org (Postfix) with ESMTP id 07DC31BF9D5 for ; Wed, 16 Apr 2014 10:37:22 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id 00FB489713 for ; Wed, 16 Apr 2014 10:37:22 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7GTXR58GtRXj for ; Wed, 16 Apr 2014 10:37:21 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from smtprelay.synopsys.com (us01smtprelay-2.synopsys.com [198.182.60.111]) by hemlock.osuosl.org (Postfix) with ESMTPS id 367B18960C for ; Wed, 16 Apr 2014 10:37:20 +0000 (UTC) Received: from us02secmta2.synopsys.com (us02secmta2.synopsys.com [10.12.235.98]) by smtprelay.synopsys.com (Postfix) with ESMTP id EA4B310C1221; Wed, 16 Apr 2014 03:37:18 -0700 (PDT) Received: from us02secmta2.internal.synopsys.com (us02secmta2.internal.synopsys.com [127.0.0.1]) by us02secmta2.internal.synopsys.com (Service) with ESMTP id DDCD355F13; Wed, 16 Apr 2014 03:37:18 -0700 (PDT) Received: from mailhost.synopsys.com (mailhost2.synopsys.com [10.9.202.240]) by us02secmta2.internal.synopsys.com (Service) with ESMTP id 22B2155F02; Wed, 16 Apr 2014 03:37:18 -0700 (PDT) Received: from mailhost.synopsys.com (localhost [127.0.0.1]) by mailhost.synopsys.com (Postfix) with ESMTP id 0EC90F09; Wed, 16 Apr 2014 03:37:18 -0700 (PDT) Received: from us01wehtc1.internal.synopsys.com (us01wehtc1.internal.synopsys.com [10.12.239.235]) by mailhost.synopsys.com (Postfix) with ESMTP id 01E53F08; Wed, 16 Apr 2014 03:37:18 -0700 (PDT) Received: from IN01WEHTCB.internal.synopsys.com (10.144.199.105) by us01wehtc1.internal.synopsys.com (10.12.239.235) with Microsoft SMTP Server (TLS) id 14.3.158.1; Wed, 16 Apr 2014 03:37:17 -0700 Received: from vg-E6520.internal.synopsys.com (10.12.196.93) by IN01WEHTCB.internal.synopsys.com (10.144.199.243) with Microsoft SMTP Server (TLS) id 14.3.158.1; Wed, 16 Apr 2014 16:07:15 +0530 From: Vineet Gupta To: Subject: [PATCH] weak symbols need to be "defined" weak but "declared" strong Date: Wed, 16 Apr 2014 16:06:01 +0530 Message-ID: <1397644605-13526-1-git-send-email-vgupta@synopsys.com> X-Mailer: git-send-email 1.8.3.2 MIME-Version: 1.0 X-Originating-IP: [10.12.196.93] Cc: Joern Rennecke , Christian Ruppert , Francois Bedard , Jeremy Bennett , Vineet Gupta , Anton Kolesov X-BeenThere: uclibc@uclibc.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Discussion and development of uClibc \(the embedded C library\)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: uclibc-bounces@uclibc.org Sender: uclibc-bounces@uclibc.org Patch "LT.old: Make __errno_location/__h_errno_location thread safe" uncovered yet another bug with static linking and errno (hopefully this is last of them all). Currently, __errno_location is declared weak but is defined strong. While this provides with the desired weak semantics in dso, it is subtly broken in static links. Quoting Joern Rennecke (ARC gcc expert): | I think the issue is that you declare the function as weak in the | header file. That is a rare instance where you want the reference | use declaration that differs a bit from the definition. | If the reference uses a weakly declared function, that creates a | weakref, i.e. the linker won't bother to look for this symbol at | all - if it gets linked in for some other reason, fine, | otherwise, it stays zero. So the solution to declare strong, define weak. Supporting data ----------------- orig code: ARM mmap wrapper (LT.old build + my prev patch for errno) _mmap: @ args = 8, pretend = 0, frame = 0 @ frame_needed = 0, uses_anonymous_args = 0 stmfd sp!, {r4, r5, r7, lr} ldr r5, [sp, #20] movs ip, r5, asl #20 beq .L2 bl __errno_location(PLT) mov r3, #22 str r3, [r0, #0] mvn r0, #0 ... ... .weak __errno_location A statically linked hello world program which uses mmap too. As we can see__errno_location is completely gone - which is semantically wrong - we need functional errno. 00008274 <__GI_mmap>: 8274: e92d40b0 push {r4, r5, r7, lr} 8278: e59d5014 ldr r5, [sp, #20] 827c: e1b0ca05 lsls ip, r5, #20 8280: 0a000004 beq 8298 8284: e320f000 nop {0} ^^^^^^^^^^ 8288: e3a03016 mov r3, #22 828c: e5803000 str r3, [r0] 8290: e3e00000 mvn r0, #0 This in turn is due to a fixup in ARM ld which transforms branch-to-null into a nop. It is better than crashing but still wrong since errno handling is removed. With the patch, errno_location is restored back in test program. 00008274 <__GI_mmap>: 8274: e92d40b0 push {r4, r5, r7, lr} 8278: e59d5014 ldr r5, [sp, #20] 827c: e1b0ca05 lsls ip, r5, #20 8280: 0a000004 beq 8298 <__GI_mmap+0x24> 8284: eb000010 bl 82cc <__errno_location> 8288: e3a03016 mov r3, #22 828c: e5803000 str r3, [r0] Cc: Christian Ruppert CC: Francois Bedard Cc: Anton Kolesov Cc: Joern Rennecke Cc: Jeremy Bennett Cc: Thomas Petazzoni Cc: Peter Korsgaard Cc: Khem Raj --- include/netdb.h | 3 --- libc/misc/internals/__errno_location.c | 2 +- libc/misc/internals/__h_errno_location.c | 2 +- libc/sysdeps/linux/common/bits/errno.h | 3 --- 4 files changed, 2 insertions(+), 8 deletions(-) diff --git a/include/netdb.h b/include/netdb.h index 0f361bb6f662..7ce01c24d8b4 100644 --- a/include/netdb.h +++ b/include/netdb.h @@ -58,9 +58,6 @@ __BEGIN_DECLS /* Function to get address of global `h_errno' variable. */ extern int *__h_errno_location (void) __THROW __attribute__ ((__const__)); -#ifdef _LIBC -extern int weak_const_function *__h_errno_location(void); -#endif /* Macros for accessing h_errno from inside libc. */ #ifdef _LIBC diff --git a/libc/misc/internals/__errno_location.c b/libc/misc/internals/__errno_location.c index 9bbc2d779653..6c359f933d16 100644 --- a/libc/misc/internals/__errno_location.c +++ b/libc/misc/internals/__errno_location.c @@ -12,7 +12,7 @@ extern int errno; #endif -int *__errno_location(void) +int weak_const_function *__errno_location(void) { return &errno; } diff --git a/libc/misc/internals/__h_errno_location.c b/libc/misc/internals/__h_errno_location.c index b30859e81f19..c510c8143eae 100644 --- a/libc/misc/internals/__h_errno_location.c +++ b/libc/misc/internals/__h_errno_location.c @@ -12,7 +12,7 @@ extern int h_errno; #endif -int *__h_errno_location(void) +int weak_const_function *__h_errno_location(void) { return &h_errno; } diff --git a/libc/sysdeps/linux/common/bits/errno.h b/libc/sysdeps/linux/common/bits/errno.h index 611b8359001a..7c0aeb179a75 100644 --- a/libc/sysdeps/linux/common/bits/errno.h +++ b/libc/sysdeps/linux/common/bits/errno.h @@ -42,9 +42,6 @@ # ifndef __ASSEMBLER__ /* Function to get address of global `errno' variable. */ extern int *__errno_location (void) __THROW __attribute__ ((__const__)); -# ifdef _LIBC -extern int weak_const_function *__errno_location(void); -# endif # ifdef __UCLIBC_HAS_THREADS__ /* When using threads, errno is a per-thread value. */