From patchwork Thu Aug 18 20:33:35 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waldemar Brodkorb X-Patchwork-Id: 660533 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from helium.openadk.org (helium.openadk.org [IPv6:2a00:1828:2000:679::23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3sFd9C1sssz9sCY for ; Fri, 19 Aug 2016 06:33:47 +1000 (AEST) Received: from helium.openadk.org (localhost [IPv6:::1]) by helium.openadk.org (Postfix) with ESMTP id CDFCC10136; Thu, 18 Aug 2016 22:33:41 +0200 (CEST) X-Original-To: devel@uclibc-ng.org Delivered-To: devel@helium.openadk.org Received: by helium.openadk.org (Postfix, from userid 1000) id 1D56B10136; Thu, 18 Aug 2016 22:33:36 +0200 (CEST) Date: Thu, 18 Aug 2016 22:33:35 +0200 From: Waldemar Brodkorb To: Thomas Petazzoni Message-ID: <20160818203335.GL22524@waldemar-brodkorb.de> References: <20160811233327.4016a9af@free-electrons.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20160811233327.4016a9af@free-electrons.com> X-Operating-System: Linux 3.16.0-4-amd64 x86_64 User-Agent: Mutt/1.5.23 (2014-03-12) Cc: devel@uclibc-ng.org, "Yann E. MORIN" Subject: Re: [uclibc-ng-devel] pthread_mutex_*() functions, dynamic vs. static linking X-BeenThere: devel@uclibc-ng.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: uClibc-ng Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: devel-bounces@uclibc-ng.org Sender: "devel" H Thomas, Thomas Petazzoni wrote, > Hello, > > Today I was investigating a link issue that occurs in Buildroot with > the libarchive package. It turns out that the code of this package > uses pthread_mutex_lock/pthread_mutex_unlock in some places to be > thread-safe, but does *not* link with the libpthread library. > > I was originally surprised that this could even work, but I discovered > that the libc intentionally provides a stub implementation of > pthread_mutex_lock/unlock. The idea behind this is that a library can > use pthread_mutex_lock/unlock without linking to libpthread. This way, > if the application using the library is single-threaded and doesn't > link with libpthread, pthread_mutex_lock/unlock are no-ops. On the > other hand, if the application using the library is multi-threaded, it > will link with libpthread, and therefore the libpthread versions of > pthread_mutex_lock/unlock will be used. > > This all seems good, until you get to static linking. Indeed with > uClibc-ng, the following program: > > #include > > int main(void) > { > pthread_mutex_t lock; > pthread_mutex_lock(&lock); > return 0; > } > > will link perfectly fine with dynamic linking: > > $ ./host/usr/bin/arm-linux-gcc -o foo foo.c > $ ./host/usr/bin/arm-linux-readelf -d foo | grep NEEDED > 0x00000001 (NEEDED) Shared library: [libc.so.0] > > but will fail to build with static linking: > > $ ./host/usr/bin/arm-linux-gcc -o foo foo.c -static > /tmp/ccda8vkc.o: In function `main': > foo.c:(.text+0x14): undefined reference to `pthread_mutex_lock' > collect2: error: ld returned 1 exit status > > And this explains the build failures like > http://autobuild.buildroot.net/results/01b/01b7088a06e7310c8773e78e8be4f6e88da67357/build-end.log > that we are seeing in Buildroot. > > It is worth mentioning that using the musl C library, this test case > works fine in both dynamic linking and static linking cases (and the > libarchive error also does not occur). What about following patch, which creates dummies for pthread_mutex_* functions in the !SHARED case: From 8d11aa1b9a983e0422dffa84eb1a7b71c616a096 Mon Sep 17 00:00:00 2001 From: Waldemar Brodkorb Date: Thu, 18 Aug 2016 08:17:36 +0200 Subject: [PATCH] add dummies Signed-off-by: Waldemar Brodkorb --- libc/misc/internals/__uClibc_main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libc/misc/internals/__uClibc_main.c b/libc/misc/internals/__uClibc_main.c index 9bb81fc..9320039 100644 --- a/libc/misc/internals/__uClibc_main.c +++ b/libc/misc/internals/__uClibc_main.c @@ -81,6 +81,9 @@ static int __pthread_return_0 (pthread_mutex_t *unused) { return 0; } weak_alias (__pthread_return_0, __pthread_mutex_lock) weak_alias (__pthread_return_0, __pthread_mutex_trylock) weak_alias (__pthread_return_0, __pthread_mutex_unlock) +weak_alias (__pthread_return_0, pthread_mutex_lock) +weak_alias (__pthread_return_0, pthread_mutex_trylock) +weak_alias (__pthread_return_0, pthread_mutex_unlock) int weak_function __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)