From patchwork Thu Nov 17 18:13:08 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: 60790 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 85C80385457A for ; Thu, 17 Nov 2022 18:13:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85C80385457A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668708814; bh=jesFHX6D+XuTYok+15afRrv/x9bPyuNa8nUmfhXv0Mc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=FQ5ZhiaF1CR9VqwDWy6anMwZwhTJoF8rd7vwJCwaSaMmjBb5jm1cLHKRj618bYjEt Xsk5RyCJj31JBx+oGIbVTbZAMCkKqCdP0rWqgkCgM/6lhmeMDWxD2yalaAoXF2qXwQ /BHJ7tkMdlbxSorXmp/gVY7asGb9M/wLvdZ2Ndjo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22f.google.com (mail-oi1-x22f.google.com [IPv6:2607:f8b0:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id E80D83854571 for ; Thu, 17 Nov 2022 18:13:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E80D83854571 Received: by mail-oi1-x22f.google.com with SMTP id h132so2768514oif.2 for ; Thu, 17 Nov 2022 10:13:12 -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=jesFHX6D+XuTYok+15afRrv/x9bPyuNa8nUmfhXv0Mc=; b=UrV7K0PJRxCdsh9b+q5a3PBoMNmNUZLwiSCm0ByMRnPj3nFTSJ506c2PfQ/Yx1pnON OMHSEW6Qgs/WVRGRU9Qx4cUjLz9zC3WbBs88OrEs2uwnOiKZWbpeCT1zLxRZWem9JSBD B4JAuhgfOYSo+Pl2tdiYp4+trjK04mJ6sT7i752zSjkubdd9GNGx9PhrQveBYJDQALBR S0NenYl1vvRZoQUGcVMri2yUHEHy/Pfn1plBVmrNWQCB+ccd9hA/tqsFRkHeHsbEu9HS NUaKoCSJtQT3vCdXZEs1iKed7USrF9BTnwLb0gAHMUilDoI/1YbA3n0SXCt6x+eZS9KG U+Og== X-Gm-Message-State: ANoB5pnLeylr1Fy7OVLWvIiZodORD5YKSxaDZoN2g2hyXtQsF2a8kaxv /acU7kpM8C5hE2WkVnxAJksdP2bYjsawclyl X-Google-Smtp-Source: AA0mqf60eMp7z5JTEPJAt0YUFsThXwsALuqJChIlAxPzOJcG7nZK8gp8Nl4tQDmetH3QodItadNgEg== X-Received: by 2002:aca:f156:0:b0:35a:6bf8:e331 with SMTP id p83-20020acaf156000000b0035a6bf8e331mr1742686oih.125.1668708792141; Thu, 17 Nov 2022 10:13:12 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c1:84f3:2ecb:33cc:fbb6:e247]) by smtp.gmail.com with ESMTPSA id n9-20020a056870558900b0013669485016sm747538oao.37.2022.11.17.10.13.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 10:13:11 -0800 (PST) To: libc-alpha@sourceware.org, Fangrui Song , "H . J . Lu" Subject: [PATCH v2] i386: Avoid avoid rely on linker optimization to avoid relocation Date: Thu, 17 Nov 2022 15:13:08 -0300 Message-Id: <20221117181308.1761316-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. Reviewed-by: Fangrui Song --- 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..23e4f2b012 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@PLT +#endif END (_start) /* To fulfill the System V/i386 ABI we need this symbol. Yuck, it's so