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: AS17314 8.43.84.0/22 X-Spam-Status: No, score=-3.6 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, PDS_RDNS_DYNAMIC_FP,RCVD_IN_DNSWL_MED,RDNS_DYNAMIC,SPF_HELO_PASS, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (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 27DB31F910 for ; Thu, 17 Nov 2022 18:02:47 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="pTGPyNAv"; dkim-atps=neutral Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DCDB7385456D for ; Thu, 17 Nov 2022 18:02:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DCDB7385456D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668708164; bh=ivk6J6Maw10v2kv7fnDHdixPmOyavnnfrKbT0BPXW+g=; 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=pTGPyNAv5fOmuy3svDPpFJO4ns3v4A0rCgLdZd/jUON+a9BRMei49aFGq2iJ/6+gD 6QPmXroFP8NIwx5IzTfkrCyPAGsNTTnpvlNpSp36Re8ET4/yBw9HbkQKn3u2jHvP4z 5oITsHgLQTVjcpCzelQrd29BvnGl1oCCvTSbz2A0= Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id 2CFE43854567 for ; Thu, 17 Nov 2022 18:02:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2CFE43854567 Received: by mail-ej1-x62c.google.com with SMTP id i10so7052574ejg.6 for ; Thu, 17 Nov 2022 10:02:23 -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=ivk6J6Maw10v2kv7fnDHdixPmOyavnnfrKbT0BPXW+g=; b=st7aD5VKOXFJhyjEqdRkcbpWWDl1GQDP5kYdfuCUHgJ35p4ZL4q+e1H8xlACLUhuWP 0Vafs1pINu52E5DMLMOGlVO0ltbdVAncl53Y3WcB62R/Y9FlNCh2v4BXUsRAC5PGlYxG 76btuL7YyYxULlXku1L1Ytw9W8fjqQUtBR4HCurWEDluGniC18Fss6elEhHvFlSzMmYj z0VL/hUi1wfmapx0oRJqjRQiGZ3AAj8Pk752K7KV6833NwRE65YI34it891e9UQaCuNh OG4tLaYNaAGGNyUihxBx2rs7Jqn1lcXdgErgzn9aDDH3oMMlrZtxpMTdBdbqbtPwoyd2 EaRQ== X-Gm-Message-State: ANoB5pnJRZezE/NAl1bph4/FKjmYkFEH5q53InyLsiCaMxoxP/1SKQh7 YrPGsD9v0veaiRsH405LZ/GE93NUgQwl51F1poY= X-Google-Smtp-Source: AA0mqf7TE4w2YWCM6MpVWm0BIgiPrDgoRC/k7bUrUtsUbESrS5TGMnpT/R00DUj691QhI0NYNyKNwgJfZZhHIcFkBbs= X-Received: by 2002:a17:906:6b95:b0:781:e01e:951f with SMTP id l21-20020a1709066b9500b00781e01e951fmr3207784ejr.283.1668708141680; Thu, 17 Nov 2022 10:02:21 -0800 (PST) MIME-Version: 1.0 References: <20221117124317.2816607-1-adhemerval.zanella@linaro.org> <70206245-247a-009b-cd39-a010e86af5b2@linaro.org> <22e87e66-8090-162b-3b56-0c812fd23da4@linaro.org> In-Reply-To: <22e87e66-8090-162b-3b56-0c812fd23da4@linaro.org> Date: Thu, 17 Nov 2022 10:01:43 -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 9:28 AM Adhemerval Zanella Netto wrote: > > > > 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. That is because it is also used for dynamic executables. I think PLT should be used. -- H.J.