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.1 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI,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 01ACD1F910 for ; Thu, 17 Nov 2022 16:57:33 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="IVutFbSI"; dkim-atps=neutral Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 29F7138376A8 for ; Thu, 17 Nov 2022 16:57:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 29F7138376A8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668704252; bh=9ebY9EbnNkr292OyAZgLBlVlxjeCN2lIqyoBYsbbJeU=; h=References:In-Reply-To:Date:Subject:To:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=IVutFbSIz9g1TfZfufHO27eJ9pFbvRxdaNZ3jpnWEIs+CJQVyTU0FHmcmpJEraITe z4X8wWr0U2XLoHaeIShnkDd+UN+jA1lMiTqlQT9HcEfv8pph5rLCdJXn3Ujbe1G7Li mSCqp3n3LU4sHr3jJJV/wmea3zsB4S2UFxsK8xKU= Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id A84D83906BFB for ; Thu, 17 Nov 2022 16:53:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A84D83906BFB Received: by mail-ej1-x634.google.com with SMTP id n21so6513840ejb.9 for ; Thu, 17 Nov 2022 08:53:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9ebY9EbnNkr292OyAZgLBlVlxjeCN2lIqyoBYsbbJeU=; b=JshBgb7JOfEvxr+DXh/lOi9SSKiwXQUbl9YK7oQ3HHp3NRfoox9vPh8lsdgodXcOsU 4lUvA9urcm+BLnaaYTAs6Z/UCxrWMMJ8cq1dMcGzfM4vy3pQ/qV/O3UtFOErXk3s49j5 0ApzF8LLrW6tRYEXfGk6g59nqI6xOXv7RKNp9gNRcc0JaON3jdoVsux0SNiQOJvm5q52 5a8IO44QHRlHUnbkn89e2iZqmO6Q2TuSVCJSFRyyplmfGktOedpjiXBbFebW3v73ZWCU 5/P2oqPXd+WwTUJ/SZlrkawped0+6iM0l5NTC6OxvntLTqMeKFARyTmBMXKu2wiCLdNH 2p7A== X-Gm-Message-State: ANoB5pm+ApONhD/J6AwnnHICr8h0M2yU+cE3CTggaYEp2tQ4g3hs3NyB 3ISqdN30ts8lIgG/F8nWpiS9NzlyPIQkPxNwX+7vEy70sNo= X-Google-Smtp-Source: AA0mqf4ZUlnb1KaVJX1Gf+Gzz09uNr4mHPbob7a6bwfDH9vLXy0BSJsUZcOT/lFIvSI8tllPE1WfIcFmtQb+AGMQps0= X-Received: by 2002:a17:906:6b95:b0:781:e01e:951f with SMTP id l21-20020a1709066b9500b00781e01e951fmr2966065ejr.283.1668703981364; Thu, 17 Nov 2022 08:53:01 -0800 (PST) MIME-Version: 1.0 References: <20221117124317.2816607-1-adhemerval.zanella@linaro.org> <70206245-247a-009b-cd39-a010e86af5b2@linaro.org> In-Reply-To: <70206245-247a-009b-cd39-a010e86af5b2@linaro.org> Date: Thu, 17 Nov 2022 08:52:23 -0800 Message-ID: Subject: Re: [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation To: Adhemerval Zanella Netto Cc: libc-alpha@sourceware.org, Fangrui Song Content-Type: text/plain; charset="UTF-8" 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: "H.J. Lu via Libc-alpha" Reply-To: "H.J. Lu" Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" 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 > >> + leal __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". -- H.J.