From patchwork Thu Nov 17 12:43:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 60748 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A76C439960D0 for ; Thu, 17 Nov 2022 12:43:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A76C439960D0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668689026; bh=i8Vhg8mgIdPd9diMXToRl2IUsLtW8Oz+rDh+b3ls5Hs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=pq7t2RlR9ngsDwZM6TYDd59VFgSO5ds0Y2zWS0nE0d6GlDX8AWZEAY4DeqBM6ResM zaB2sE2zYYX1w1URT3+/+wwae0ZRDoKL12XJA5/lBGBpvvs6Mqfp7q0JCGho/xCnDJ keBVlexi0uuZA4iz0Kxb1xqXuAd71ey6E3GeZVL4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2f.google.com (mail-oa1-x2f.google.com [IPv6:2001:4860:4864:20::2f]) by sourceware.org (Postfix) with ESMTPS id DE745398AC39 for ; Thu, 17 Nov 2022 12:43:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DE745398AC39 Received: by mail-oa1-x2f.google.com with SMTP id 586e51a60fabf-1322d768ba7so1990736fac.5 for ; Thu, 17 Nov 2022 04:43:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=i8Vhg8mgIdPd9diMXToRl2IUsLtW8Oz+rDh+b3ls5Hs=; b=ksUaBEB4B7103KdDcZjjGOreQRAXB2yAea8cyMNOzXxxCFiXdkIDY5B3e2u98Lvc31 0wLMss/ew/ZSNvob3KRgr2I3lQAJr+oAw+6muupnKmmG+e1wPtwAxbMNLYLnXKNOrreo +jOlqAwp2h8s7Oa3vfKb11+QJ/9qwJUp7A8vGLOH48zQ45ISFN1ZbDdwKo/83RjVD67A wXaC+oFM0JfXeBd330FSKQbM7xnMi1Fzuk98hXYP1U91xYS1NnnJ05xDIdTU4ID6KDEw DH3HN9Jnd6IdTY50KLoBVTm3XFyx7qrDNILJDF8NIUVVkRKaNCuKyFoy5CPQQ3LV2r1V 2hfw== X-Gm-Message-State: ANoB5pmFzKeEv/HGYEUgdOCCDZgD54Ax9Wpa7+ytqsJ8g9iWqBZZDtij ETW6J/Ut7yOawyndHoUudWLZubqFFvfQI7yI X-Google-Smtp-Source: AA0mqf6l/vJEYcAjRiJEFThGMOiM+f7pWMJKdwb8eyIFU4oHbVolmC/Y+kCDAyhraSthlSaw67pIZQ== X-Received: by 2002:a05:6870:428a:b0:132:4f85:df90 with SMTP id y10-20020a056870428a00b001324f85df90mr4144135oah.288.1668689001821; Thu, 17 Nov 2022 04:43:21 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c1:84f3:2ecb:33cc:fbb6:e247]) by smtp.gmail.com with ESMTPSA id d9-20020a4ad349000000b0049f3bdd791esm269903oos.26.2022.11.17.04.43.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 04:43:21 -0800 (PST) To: libc-alpha@sourceware.org, Fangrui Song , "H . J . Lu" Subject: [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation Date: Thu, 17 Nov 2022 09:43:17 -0300 Message-Id: <20221117124317.2816607-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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 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: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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 +#endif END (_start) /* To fulfill the System V/i386 ABI we need this symbol. Yuck, it's so