From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by sourceware.org (Postfix) with ESMTPS id 7F3E0385802D; Wed, 5 May 2021 20:31:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7F3E0385802D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=maskray.me Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=emacsray@gmail.com Received: by mail-pg1-f180.google.com with SMTP id m190so2652003pga.2; Wed, 05 May 2021 13:31:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=Hm+Aa3OF2bUoqHq2iqJhAAed+jrYtsq/3mfcw17RnQI=; b=ThSQ91zsnBnE4VIdkaHRPy8d2zgFJfUWN/0P87PdBpXvGTWhgOB9ECMH4uxajJY3DE HBWYHzuRJ94MZjW7Pf9H8vFuioP79zNHjLXbKm1yOnYR6PVTKPMiT4olcH/Skkb+V54A nQ+dfBUDr9CQbA7hEjxfmcy+K/VYxY1Olm+9dAsGUXbiVEyZmAjUoeZmQRy7Noe0sSCX CWXgrF0NCZgp6IG2OxjBggh06LsCPYiwGw7hv+VQbIsSueMLc4Jcd/yGiVyiqbZvQu6x o/bu2p4ASexRllmJFJfwZWFsuuw9ICOmACq18I2Cuz1oQoxvdAFdJi6KUNytzOAHtUTq bPoA== X-Gm-Message-State: AOAM533vuc5ggW4OtepXRWThxkLS9K2HYoUcMHk+r/pa0gEwcUONCV7U jIJL3QxU3lFclARESbNhRLk= X-Google-Smtp-Source: ABdhPJxwfIXZ8FAteW6M1t512IdyfWSIQIPSZEwHRPhzA6EXNYRjlJol1PKSjUFvj3XRPDrNPz5Ltg== X-Received: by 2002:a63:ab05:: with SMTP id p5mr675574pgf.149.1620246666379; Wed, 05 May 2021 13:31:06 -0700 (PDT) Received: from localhost ([2601:647:4b01:ae80:f317:4f22:3cdf:e37e]) by smtp.gmail.com with ESMTPSA id j27sm83620pgb.54.2021.05.05.13.31.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 May 2021 13:31:05 -0700 (PDT) Date: Wed, 5 May 2021 13:31:04 -0700 From: Fangrui Song To: "H.J. Lu" Cc: Bruno Haible , Florian Weimer , GNU C Library , Andreas Schwab , bug-gnulib@gnu.org, Binutils Subject: Re: Undefined use of weak symbols in gnulib Message-ID: <20210505203104.qnomt5jypiyrz4yw@gmail.com> References: <87o8e0p92r.fsf@oldenburg.str.redhat.com> <87fszc8a1z.fsf@igel.home> <87eeewnfzw.fsf@oldenburg.str.redhat.com> <1680226.UWtE2gOZdF@omega> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, KAM_ASCII_DIVIDERS, KAM_DMARC_STATUS, KAM_INFOUSMEBIZ, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 May 2021 20:31:09 -0000 On 2021-04-27, H.J. Lu via Binutils wrote: >On Tue, Apr 27, 2021 at 7:10 PM H.J. Lu wrote: >> >> On Tue, Apr 27, 2021 at 6:57 PM Bruno Haible wrote: >> > >> > Hi Florian, >> > >> > > Here's a fairly representative test case, I think. >> > > >> > > #include >> > > #include >> > > >> > > extern __typeof (pthread_key_create) __pthread_key_create __attribute__ ((weak)); >> > > extern __typeof (pthread_once) pthread_once __attribute__ ((weak)); >> > > >> > > void >> > > f1 (void) >> > > { >> > > puts ("f1 called"); >> > > } >> > > >> > > pthread_once_t once_var; >> > > >> > > void __attribute__ ((weak)) >> > > f2 (void) >> > > { >> > > if (__pthread_key_create != NULL) >> > > pthread_once (&once_var, f1); >> > > } >> > > >> > > int >> > > main (void) >> > > { >> > > f2 (); >> > > } >> > > >> > > Building it with “gcc -O2 -fpie -pie” and linking with binutils 2.30 >> > > does not result in a crash with LD_PRELOAD=libpthread.so.0. >> > >> > Thank you for the test case. It helps the understanding. >> > >> > But I don't understand >> > - why anyone would redeclare 'pthread_once', when it's a standard POSIX >> > function, >> > - why f2 is declared weak, >> > - why the program skips its initializations in single-threaded mode, >> > - why libpthread would be loaded through LD_PRELOAD or dlopen, given >> > that the long-term statement has been that declaring a symbol weak >> > has no effect on the dynamic linker [1][2][3][4]? >> > >> > How about the following test case instead? >> > >> > ===================================================================== >> > #include >> > #include >> > >> > #pragma weak pthread_key_create >> > #pragma weak pthread_once >> > >> > void >> > do_init (void) >> > { >> > puts ("initialization code"); >> > } >> > >> > pthread_once_t once_var; >> > >> > void >> > init (void) >> > { >> > if (pthread_key_create != NULL) >> > { >> > puts ("multi-threaded initialization"); >> > pthread_once (&once_var, do_init); >> > } >> > else >> > do_init (); >> > } >> > >> > int >> > main (void) >> > { >> > init (); >> > } >> > ===================================================================== >> > >> > $ gcc -Wall -fpie -pie foo.c ; ./a.out >> > initialization code >> > >> > $ gcc -Wall -fpie -pie foo.c -Wl,--no-as-needed -lpthread ; ./a.out >> > multi-threaded initialization >> > initialization code >> > >> > What will change for this program with glibc 2.34? >> > >> > Bruno >> > >> > [1] https://sourceware.org/legacy-ml/libc-hacker/2000-06/msg00029.html >> > [2] https://www.akkadia.org/drepper/dsohowto.pdf page 6 >> > [3] https://stackoverflow.com/questions/21092601/is-pthread-in-glibc-so-implemented-by-weak-symbol-to-provide-pthread-stub-functi/21103255 >> > [4] https://stackoverflow.com/questions/20658809/dynamic-loading-and-weak-symbol-resolution >> > >> >> Does x86 show the same issue? I fixed several undefined weak symbol >> bugs on x86: >> >> https://sourceware.org/bugzilla/show_bug.cgi?id=19636 >> https://sourceware.org/bugzilla/show_bug.cgi?id=19704 >> https://sourceware.org/bugzilla/show_bug.cgi?id=19719 I don't consider the first two bugs. Whether a dynamic relocation is emitted depends on (1) whether .dynsym exists (2) architecture (3) relocation type (4) -no-pie/-pie/-shared (5) -z {,no}dynamic-undefined-weak. It is unlikely a user can summarize rules which can be relied upon. We can step back and look at these from a different perspective: figure out what should be defined, then everything else has no hard rule and we can choose whatever to simplify rules. * absolute relocation resolves to 0. There may or may not be dynamic relocations. * PC-relative relocation doesn't make sense. I changed LLD to use a simple rule: * -no-pie and -pie: no dynamic relocation * -shared: dynamic relocation https://maskray.me/blog/2021-04-25-weak-symbol >> with a linker option: >> >> 'dynamic-undefined-weak' >> 'nodynamic-undefined-weak' >> Make undefined weak symbols dynamic when building a dynamic >> object, if they are referenced from a regular object file and >> not forced local by symbol visibility or versioning. Do not >> make them dynamic if 'nodynamic-undefined-weak'. If neither >> option is given, a target may default to either option being >> in force, or make some other selection of undefined weak >> symbols dynamic. Not all targets support these options. >> >> Alan extended the fix to PPC: >> >> commit 954b63d4c8645f86e40c7ef6c6d60acd2bf019de >> Author: Alan Modra >> Date: Wed Apr 19 01:26:57 2017 +0930 >> >> Implement -z dynamic-undefined-weak >> >> -z nodynamic-undefined-weak is only implemented for x86. (The sparc >> backend has some support code but doesn't enable the option by >> including ld/emulparams/dynamic_undefined_weak.sh, and since the >> support looks like it may be broken I haven't enabled it.) This patch >> adds the complementary -z dynamic-undefined-weak, extends both options >> to affect building of shared libraries as well as executables, and >> adds support for the option on powerpc. >> > >Another undefined weak symbol linker bug: > >https://sourceware.org/bugzilla/show_bug.cgi?id=22269