From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 0E7673858D35 for ; Wed, 4 Jan 2023 15:49:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0E7673858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672847369; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eRCRjTmfkGJCS77/8GgZvozTcEI7+e96ugaoPCPNtbc=; b=SGAbWpbeGZ3IjgjNx0k+c4omP47elFVZJmvsb1vDSXUlw/OLoAcc/2NmTqGYbHr/qcU+cX LrvAWRcf/i2zihWtTXYvdFEh6UOHdHxSP7toV2nnUlUmEJORa1IF8tVjuBGsdj0os8bHc/ uI/QP2kMr5S1Som5PHrEu6KWVsGRedQ= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-628-_4rHf7foNqS4-Fo0qa0Aww-1; Wed, 04 Jan 2023 10:49:28 -0500 X-MC-Unique: _4rHf7foNqS4-Fo0qa0Aww-1 Received: by mail-qt1-f200.google.com with SMTP id i3-20020ac87643000000b003a816421776so12088502qtr.22 for ; Wed, 04 Jan 2023 07:49:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eRCRjTmfkGJCS77/8GgZvozTcEI7+e96ugaoPCPNtbc=; b=0hZd6tMQGTt7EVbLOVZ9dYyReIEvBoQpGQvTWVZEHULUD1Wp4PYRauXKoKrLNmtcvx 6c5Y5FzQy/eGkHa2OzD780i664jjd/YLHhQtWb66sJ1wBe56rU10sCdFcblqbsoUBOLW Qo6Wf5Ol1EngFrrK+B00a/w3yPoyTvdcj67VsM7c7zj2Lc/nxaZGX1roRinnR04DyOi+ lOAWxmvnygwYQFpazfybdnTbAkjfb+SbT1EnNVPa029yHG1eGWNdJiD0ENcMkrG1tGnR UAuVO2u1mLnBR0ZVTeC62KYe9JNp9NYhrYholvqMhM6ogE3cWCvGD7siXZzpYFlWkKeE PA+w== X-Gm-Message-State: AFqh2koXoocCTYUxOU0YO81IWTUHBXsY2QPcVdvKUlr4lEP2g3MMraI1 7h+rOXrnvO9cRCRqzAwP5fwLbgh0GxLtsSQ+d4JvRjqMGXNNClRyW6jtCAxhdrL6ZKFtEv8OC7A Yjw3KPS4nMDzx7RnIzA== X-Received: by 2002:a05:622a:1e95:b0:3a7:f93a:df8 with SMTP id bz21-20020a05622a1e9500b003a7f93a0df8mr74570962qtb.61.1672847368040; Wed, 04 Jan 2023 07:49:28 -0800 (PST) X-Google-Smtp-Source: AMrXdXs1MR+Y0TP4dZPrLswZJtZaU0s1ejTz2tXITcFAECLbCab0w7StsvYMZnDSQPvfO52FDgdxpQ== X-Received: by 2002:a05:622a:1e95:b0:3a7:f93a:df8 with SMTP id bz21-20020a05622a1e9500b003a7f93a0df8mr74570921qtb.61.1672847367409; Wed, 04 Jan 2023 07:49:27 -0800 (PST) Received: from [192.168.1.108] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id bs23-20020ac86f17000000b003a689a5b177sm20783877qtb.8.2023.01.04.07.49.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Jan 2023 07:49:26 -0800 (PST) Message-ID: <6a6a5201-950c-818e-4443-83d89d86dd6a@redhat.com> Date: Wed, 4 Jan 2023 10:49:25 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH] c++, TLS: Support cross-tu static initialization for targets without alias support [PR106435]. To: Iain Sandoe Cc: GCC Patches References: <20221207153939.49157-1-iain@sandoe.co.uk> <5C16E9B3-E14E-4B65-A8DE-5CF670F6961B@sandoe.co.uk> <2de9a8cc-70f7-4417-1df8-f3cf4f25a7f6@redhat.com> <9BE68B6C-569F-487D-BE7C-26BBE611873C@sandoe.co.uk> From: Jason Merrill In-Reply-To: <9BE68B6C-569F-487D-BE7C-26BBE611873C@sandoe.co.uk> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 1/4/23 10:30, Iain Sandoe wrote: > > >> On 4 Jan 2023, at 15:03, Jason Merrill wrote: >> >> On 1/3/23 18:17, Iain Sandoe wrote: >>>> On 3 Jan 2023, at 22:22, Jason Merrill wrote: >>>> >>>> On 12/7/22 10:39, Iain Sandoe wrote: >>>>> This has been tested on x86_64 and arm64 Darwin and on x86_64 linux gnu. >>>>> The basic patch is live in the homebrew macOS support and so has had quite >>>>> wide coverage on non-trivial codebases. >>>>> OK for master? >>>>> Iain >>>>> Since this actually fixes wrong code, I wonder if we should also consider >>>>> back-porting. >>>>> --- >8 --- >>>>> The description below relates to the code path when TARGET_SUPPORTS_ALIASES is >>>>> false; current operation is maintained for targets with alias support and any >>>>> new support code should be DCEd in that case. >>>>> -- >>>>> Currently, cross-tu static initialisation is not supported for targets without >>>>> alias support. >>>>> The patch adds support by building a shim function in place of the alias for >>>>> these targets; the shim simply calls the generic initialiser. Although this is >>>>> slightly less efficient than the alias, in practice (for targets that allow >>>>> sibcalls) the penalty is a single jump when code is optimised. >>>>> From the perspective of a TU referencing an extern TLS variable, there is no >>>>> way to determine if it requires a guarded dynamic init. So, in the referencing >>>>> TU, we build a weak reference to the potential init and check at runtime if the >>>>> init is present before calling it. This strategy is fine for targets that have >>>>> ELF semantics, but fails at link time for Mach-O (which does not permit the >>>>> reference to be undefined in the static link). >>>>> The actual initialiser call is contained in a wrapper function, and to resolve >>>>> the Mach-O linker issue, in the TU that is referencing the var, we now generate >>>>> both the wrapper _and_ a weak definition of a dummy init function. In the case >>>>> that there _is_ a dynamic init (in a different TU), that version will be non-weak >>>>> and will be override the weak dummy one. >>>> >>>> IIUC, this isn't reliable in general; in specific, I believe that the glibc dynamic loader no longer prefers strong definitions to weak ones. >>> Neither does Darwin’s dynamic loader, this implemenation works there because the static linker _will_ override the weak def with a strong one. IIUC, binutils ld does this too. >>> If we need this to work between DSOs then that potentially presents a problem (for Darwin the DSO is identified so that the symbol will be found in the library that resolved it in the static link, [but that can be defeated by forcing “flat linking”]), I am not sure if glibc dynamic loader would do something similar (although this code path is not taken on ELF targets since they have the symbol aliases). >>>> Perhaps on targets that don't allow weakrefs to be unbound, >>> Darwin would allow it if we were able to tell the static linker that the symbol is permitted to be undefined - but since we don’t know the symbol’s name outside the FE, that is not going to fly. >> >> Can you elaborate on this? > > At runtime Mach-O is much the same as ELF w.r.t weak references, the difference comes at static link time when (by default) Darwin’s linker requires all symbols to have a definition. > > Darwin’s static linker has three mechanisms for allowing a weak reference (in each case, at runtime, the symbol reference will be NULL if no definition is present - so ELF-like at that point): > > 1. A definition is supplied on the link line (usually in a DSO) the DSO is defined as a weak library, which means it is permitted to be absent at runtime. [the usage we are thinking of here is not what this facility was designed for, but it can work]. > > 2. We put -Wl,-undefined,dynamic_lookup on the link line, that allows any symbol to be undefined - it is a massive sledgehammer and not at all recommended for general code (we have to use it for things like plugins that need to resolve many symbols at runtime from their host). NOTE that it also seems to be incompatible with some modern fixups on arm64 (i.e. it looks like Apple do not intend to guarantee it will work in the future). > > 3. An individual symbol maybe be specified as “allowed to be undefined” by passing -Wl,-U,_symbol > > It was the third case I was thinking of - but I cannot see how to obtain the symbols easily (If we can identify them, we could arrange to emit them into some special section and then fish them out using simple-object in collect2 and apply to the generated link line). However, this does not seem like a phase3/4 kind of change (and I do not currently have much^W any spare time either). Aha, thanks. We shouldn't need to build a list in a special section: collect2 could look for _ZTH* symbol references and add -U options for them. Jason