From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-4.6 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id C72761F910 for ; Thu, 17 Nov 2022 17:28:38 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="PDIg2Xva"; dkim-atps=neutral Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C02B3385454E for ; Thu, 17 Nov 2022 17:28:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C02B3385454E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668706117; bh=QDrNGvwcWS2h1skN6Yx9a0dgCd/4LZeX8250LeooXCc=; h=Date:Subject:To:Cc:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=PDIg2Xvae1v6Mm54pc33PwMhUmCh3uTpheTO0cPMesTONUk74BKyHBpxmFDZ/rRwh EkVT3KrDziPCU9cfNTJ/PY01z3pZLyNiPx7y6MbVw1BFmdBoBPVrg92vCwTZnctie5 zzXKEoszaHaOMfTA2Q0VjqJyKvtOS8/u1+UH/OGI= Received: from mail-oa1-x35.google.com (mail-oa1-x35.google.com [IPv6:2001:4860:4864:20::35]) by sourceware.org (Postfix) with ESMTPS id 36150385454C for ; Thu, 17 Nov 2022 17:28:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 36150385454C Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-13ae8117023so2919419fac.9 for ; Thu, 17 Nov 2022 09:28:16 -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:organization: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=QDrNGvwcWS2h1skN6Yx9a0dgCd/4LZeX8250LeooXCc=; b=zICxOBKEZtVdmkiEjPVHqZjnbxuXdYy3bGReBFHZme1xyd7xLxjAZCjBviNnLOx6LE A1Uw8gpgsKvZU9Mv9vJ0yEsXvRvAEGNqwuum9Xz+vOJj+2bgCQA0BzQvz1CZ152vdeYb /oNy2MFGi+NxI9fbIYABpeXu/xorFzbK0a0oKrMfDFO4GbLQJSOxdhR5Slz6aRLb2Tzy pRKnayo35sFigandHMP9yFkHbhomvl6yogAJt1CekS/q4uIolvYOhYCYko1d+JBCVMNu aQRdZNR4HIIH09t2XApHgw47G2UtYlR7T3q4cqKOmNcZPJsQAPahY/stjiYAQsYqfvKl syhA== X-Gm-Message-State: ANoB5plEIKAIZs/hWQSJShUkpv5pFA08BtrzbhoQvgdZMkI+9UWitujB GITC0KRVqRHkGlJfb9IieXO0kUC1s3NP89ur X-Google-Smtp-Source: AA0mqf7FfCYIIbuaxkjrHsKM+tAxVLJ4iORUVJ6dRZj9RkciRmdBpfhogrS5i5w1idRDI9RD6rXxxA== X-Received: by 2002:a05:6870:631a:b0:136:6a3f:1fb1 with SMTP id s26-20020a056870631a00b001366a3f1fb1mr4974812oao.287.1668706095427; Thu, 17 Nov 2022 09:28:15 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c1:84f3:2833:5b94:1e27:c35b? ([2804:1b3:a7c1:84f3:2833:5b94:1e27:c35b]) by smtp.gmail.com with ESMTPSA id t12-20020a9d66cc000000b0066c34486aa7sm543260otm.73.2022.11.17.09.28.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Nov 2022 09:28:14 -0800 (PST) Message-ID: <22e87e66-8090-162b-3b56-0c812fd23da4@linaro.org> Date: Thu, 17 Nov 2022 14:28:12 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation Content-Language: en-US To: "H.J. Lu" Cc: libc-alpha@sourceware.org, Fangrui Song References: <20221117124317.2816607-1-adhemerval.zanella@linaro.org> <70206245-247a-009b-cd39-a010e86af5b2@linaro.org> Organization: Linaro In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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: , From: Adhemerval Zanella Netto via Libc-alpha Reply-To: Adhemerval Zanella Netto Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" On 17/11/22 13:52, H.J. Lu wrote: > On Thu, Nov 17, 2022 at 8:42 AM Adhemerval Zanella Netto > wrote: >> >> >> >> On 17/11/22 13:07, H.J. Lu wrote: >>> On Thu, Nov 17, 2022 at 4:43 AM Adhemerval Zanella >>> wrote: >>>> >>>> lld does not implement all the linker optimization to avoid the GOT >>>> relocation as done by binutils (bfd/elf32-i386.c:elf_i386_convert_load_reloc). >>>> The current 'movl main@GOT(%ebx), %eax' will then create a GOT >>>> relocation when building with lld, which make static-pie status to >>>> not being able to start the provided main function. >>>> >>>> The change uses a __wrap_main local symbol, which in turn calls main >>>> (similar as used by aarch64 and s390x). >>>> >>>> Checked on i686-linux-gnu with binutils and lld. >>>> --- >>>> sysdeps/i386/start.S | 13 +++++++++---- >>>> 1 file changed, 9 insertions(+), 4 deletions(-) >>>> >>>> diff --git a/sysdeps/i386/start.S b/sysdeps/i386/start.S >>>> index 4ec04bdfd7..d593c4de00 100644 >>>> --- a/sysdeps/i386/start.S >>>> +++ b/sysdeps/i386/start.S >>>> @@ -98,11 +98,10 @@ ENTRY (_start) >>>> pushl main@GOT(%ebx) >>>> # else >>>> /* Avoid relocation in static PIE since _start is called before >>>> - it is relocated. Don't use "leal main@GOTOFF(%ebx), %eax" >>>> - since main may be in a shared object. Linker will convert >>>> - "movl main@GOT(%ebx), %eax" to "leal main@GOTOFF(%ebx), %eax" >>>> + it is relocated. This also avoid rely on linker optimization to >>>> + transform 'movl main@GOT(%ebx), %eax' to 'leal main@GOTOFF(%ebx)' >>>> if main is defined locally. */ >>>> - movl main@GOT(%ebx), %eax >>>> + 955774751b71c4bc94029dd541ad9d34634ec995 __wrap_main@GOTOFF(%ebx), %eax >>>> pushl %eax >>>> # endif >>>> >>>> @@ -130,6 +129,12 @@ ENTRY (_start) >>>> 1: movl (%esp), %ebx >>>> ret >>>> #endif >>>> + >>>> +#if defined PIC && !defined SHARED >>>> +__wrap_main: >>>> + _CET_ENDBR >>>> + jmp main >>> >>> Shouldn't it be "jmp main@PLT"? >> >> My understanding is for static build we can handle main as hidden, so there >> is no need use a PLT relocation here. > > The original comments have > > it is relocated. Don't use "leal main@GOTOFF(%ebx), %eax" > since main may be in a shared object. Linker will convert > > If this is only used for static build, we can use "leal > main@GOTOFF(%ebx), %eax". > It does not work with binutils (and that's why you installed 955774751b71c4): $ make test t=elf/tst-main1 [...] csu/crt1.o(.text+0x1e): unresolvable R_386_GOTOFF relocation against symbol `main' collect2: error: ld returned 1 exit status The start.o (PIC and !SHARED) is used for crt1.o. With 'main' requirement, leal does work for building tst-main1, so maybe one option could to add a .global main to pull the symbol.