From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-x62c.google.com with SMTP id i10so7052574ejg.6 for ; Thu, 17 Nov 2022 10:02:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=ivk6J6Maw10v2kv7fnDHdixPmOyavnnfrKbT0BPXW+g=; b=Ac5RJpxEvJhUVw3EejyOWZBnm2vQnz3+cucpqDKq3dw5kldeNH4eXmgwcWniXO+MsA Lf80E7LCBRUh0kIGmASS8MpS/C/aMz4kit8Wf0BD5b604aHPk4rNHTUMaVNWtp6Fph1n qbtqyQCe41wDDgFZDbEfi1SD69BoyZc6zZdYsYwHWJT7nGpM8/MYgXr0pYUJCCFu1lMq szJYrsbWftcf77s38NC9m/OsNmygxCGa9vWXj+5e1pZAXgqHfjzZi1e4PqoBJ4/2tyIT 1JkjN01Tmd5++HSsfWjI53S05qdW47izJxs2qGTzUaVW7DTs8Excia43o4G/zcpPsO7a G2Vg== 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> From: "H.J. Lu" 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-Spam-Status: No, score=-3023.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 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.