From patchwork Tue Jan 12 17:23:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 1425350 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=sourceware.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=K05/KGPI; dkim-atps=neutral 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 ozlabs.org (Postfix) with ESMTPS id 4DFcqC4cJkz9svs for ; Wed, 13 Jan 2021 04:23:19 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 737B83896C3C; Tue, 12 Jan 2021 17:23:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 737B83896C3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1610472197; bh=Jwsr8wRZM3yHsHwZETSkkb4I8uvWlJ9bp9FCPl8TVvA=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=K05/KGPIYNkKAvJ6GYUoN/dtVR9OohuLXgq+vIMdqqrKQlaeS4WJFyeNfiv10lUKi B92Re/ImqDuahRBeyCeUH+rnNuYCnxgtpBAQWQYi9ojPyjqPQnmA++UuSjujTYyUBy 4cxmODkDOF9IeacpoBVBJIJ91/rM2xRZtMI/l0/g= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-eopbgr50055.outbound.protection.outlook.com [40.107.5.55]) by sourceware.org (Postfix) with ESMTPS id 99AA23896C38 for ; Tue, 12 Jan 2021 17:23:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 99AA23896C38 Received: from DB7PR05CA0048.eurprd05.prod.outlook.com (2603:10a6:10:2e::25) by DB7PR08MB3801.eurprd08.prod.outlook.com (2603:10a6:10:79::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6; Tue, 12 Jan 2021 17:23:12 +0000 Received: from DB5EUR03FT038.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:2e:cafe::6f) by DB7PR05CA0048.outlook.office365.com (2603:10a6:10:2e::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6 via Frontend Transport; Tue, 12 Jan 2021 17:23:12 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; sourceware.org; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;sourceware.org; dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by DB5EUR03FT038.mail.protection.outlook.com (10.152.21.84) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6 via Frontend Transport; Tue, 12 Jan 2021 17:23:12 +0000 Received: ("Tessian outbound 4d8113405d55:v71"); Tue, 12 Jan 2021 17:23:12 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: b576f3a3f5652ef2 X-CR-MTA-TID: 64aa7808 Received: from eb2735c627d1.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id A7239096-3C24-4AE5-A5CB-DBFD82218D85.1; Tue, 12 Jan 2021 17:23:07 +0000 Received: from EUR04-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id eb2735c627d1.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Tue, 12 Jan 2021 17:23:07 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fWTu4lpAvtslaMHs8TpramPVnjtqeCAXkWu/TPzxo6NgEVMyMxFF4nmgbN8N6oFmh2oGUc/btCWQBWnLhbRzkcj110N26eouHlzwtl04yR2haOy2qzUDdUK8PCczE/9/7x0QM6iR1TMMEgSJGXo3aBFnZZvWG11Pl95XUicL77h/+P8vlmnVk00J1NYdu42998OJ3BqEXB/92OOrcT7wjMqj5kJBJhMNqJJC7yPMF+Bfx+/iEaN3iCY/cWWhtrS5eEDsSqQUNI6jC+F+qJ8+MUDbtVdK4c05rox0W+aVwU4dopDz/FQyYtxiHgUySS8WpmQJnaFtxtQD6KSGD20ZPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Jwsr8wRZM3yHsHwZETSkkb4I8uvWlJ9bp9FCPl8TVvA=; b=JxlNsFBLMvyIY+z2GZuy9jsY3ai4H3V7ye2kCF8eFxnvP3kdSk5JEfwaBs5QP5lsOptsqADEkmRZxlUIuzjJC+Djb6rHoQJvR7MlNBo4v+9PdoDglp2bDT4d0nz/da5Aq5Ev2qr3AnNdfm1PYLrE+SLn9mG708kxAvSQ4gCpj8uXdbhkvfYDwJXyeeBuVl+OCdG643xRzgMPNM9MbA4gI6zjfQu4WJlIwpy2xB6oxvlux/RGyWIimpESQfu7mZup0mFq1Ajgs+7KjRKi54G+S0nO63cRsWtQkl/YsNi/TPO4+Gi3HKda5JpanHB0Yr2+FewCJWOYiX97/VGX3Sc7fA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none Authentication-Results-Original: sourceware.org; dkim=none (message not signed) header.d=none;sourceware.org; dmarc=none action=none header.from=arm.com; Received: from PA4PR08MB6320.eurprd08.prod.outlook.com (2603:10a6:102:e5::9) by PR3PR08MB5753.eurprd08.prod.outlook.com (2603:10a6:102:87::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6; Tue, 12 Jan 2021 17:23:06 +0000 Received: from PA4PR08MB6320.eurprd08.prod.outlook.com ([fe80::700f:ddbe:a347:ee4f]) by PA4PR08MB6320.eurprd08.prod.outlook.com ([fe80::700f:ddbe:a347:ee4f%6]) with mapi id 15.20.3742.012; Tue, 12 Jan 2021 17:23:06 +0000 To: libc-alpha@sourceware.org Subject: [PATCH v3 5/5] csu: Move static pie self relocation later [BZ #27072] Date: Tue, 12 Jan 2021 17:23:00 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [217.140.106.50] X-ClientProxiedBy: LO4P123CA0303.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:196::20) To PA4PR08MB6320.eurprd08.prod.outlook.com (2603:10a6:102:e5::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (217.140.106.50) by LO4P123CA0303.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:196::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6 via Frontend Transport; Tue, 12 Jan 2021 17:23:05 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: e2b71abf-d1a0-4f69-a6af-08d8b71ebd65 X-MS-TrafficTypeDiagnostic: PR3PR08MB5753:|DB7PR08MB3801: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:10000;OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: XXM/7a0HCwzDn2YQPVeJRs7oOji/OSnip6JFBOkqVdl4+EBCrUsVxYySHlUEGyOCzVduCgCbKVZc2+ZmuKaaC/xkPyYTsoCE+PkwBD3GedKFkionkEsvMhrNUZvhdbcSjQvbZjMcxzY7W/fiyr9nAxcTI8CohKBcnq4AH/P3xOxaXrIN2WUQI5DPVXLAkk0PNl3HoskaSHBdk9lNWZCagQ0XEHvSL6Lw2TxDurHfZaCCKDXy18Wv90fN49c3KrNmZPF1ly+9hHBsj8grRXzZNyAb3qvdDLlld4+bIjE88GFae5gN5IrcGONFsX28ozheDrgMryalugXhBSgL/bj3h3fwlfeyx+W59j9GicgkHc69wcOd4yOypZMjKMGfLOQSVaqCbRF2MJLtcl+ZwN+HfoWzi0a9EbnsiVsSN1nzqbWCnYqcdJnYqpx9IYcN0FvoPrK2fPSQpyvC3GBkWIcymQ== X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PA4PR08MB6320.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(376002)(136003)(39850400004)(346002)(366004)(396003)(16526019)(26005)(6916009)(86362001)(6506007)(956004)(36756003)(83380400001)(69590400011)(478600001)(2906002)(8936002)(186003)(66476007)(6512007)(2616005)(6486002)(316002)(5660300002)(66946007)(44832011)(6666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/fbH9Xye7cXXLON0m7pxbWD3 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR08MB5753 Original-Authentication-Results: sourceware.org; dkim=none (message not signed) header.d=none; sourceware.org; dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DB5EUR03FT038.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: 13d9da07-d237-4684-9a41-08d8b71eb98a X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GQZVBtkorcLaixDI4YRw+ZmAyl0wHw+rvFNaSS8cxkD02DusqrzxgtLC4j0tDBJOdHk5jYziWL/9jtuJdbkBlAe524P6DMk1SXDhhmpqYJ6F+5x/PJQw7Cxd4ZEneGS0PBmkOrTfnYo4PncrywxSVckr2OGrtJRKZ87tPA/4vuis1o2XJvd8DrUfmcLTCWJbS46adLyYaJtCV8EWjyz/ucM4v7qkD/wE58QlURZ9gv/atC3z2lh6SipmR1lZTyzhURHUvofmaX1DdYz3k8znkSqXP/XMnZ3Och/UHd1o4/HYXwDJ0Cd0cgSSKdS5kz3DJMrm+qvj/rzDDMH9764mKjryjwCa1Z1on3oFSQcTDfxE6izP+lMAQFht7cfSq7ApXlgxC4g3YPmFrKjia2Snwg565vvx95K7KTF2vTTTA2NnFZ7nMYXK1kGZQ0pKmQ12eMspFFDNJfplml0jJHqqKNug/4xMoAsRDufsGF50iycPH0ZzIztqz1mic13BKzVOan4ss4eESa4192IVjWaLBw== X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(4636009)(396003)(39850400004)(376002)(346002)(136003)(46966006)(6512007)(69590400011)(82310400003)(34020700004)(2906002)(5660300002)(356005)(36756003)(8676002)(44832011)(2616005)(956004)(82740400003)(6916009)(6666004)(478600001)(6506007)(81166007)(8936002)(70586007)(16526019)(70206006)(83380400001)(186003)(316002)(86362001)(336012)(6486002)(26005)(47076005); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jan 2021 17:23:12.7302 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e2b71abf-d1a0-4f69-a6af-08d8b71ebd65 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: DB5EUR03FT038.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB7PR08MB3801 X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Szabolcs Nagy via Libc-alpha From: Szabolcs Nagy Reply-To: Szabolcs Nagy Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" IFUNC resolvers may depend on tunables and cpu feature setup so move static pie self relocation after those. It is hard to guarantee that the ealy startup code does not rely on relocations so this is a bit fragile. It would be more robust to handle RELATIVE relocs early and only IRELATIVE relocs later, but the current relocation processing code cannot do that. The early startup code before relocation processing includes _dl_aux_init (auxvec); __libc_init_secure (); __tunables_init (__environ); ARCH_INIT_CPU_FEATURES (); These are simple enough that RELATIVE relocs can be avoided. __ehdr_start may require RELATIVE relocation so it was moved later, fortunately ehdr and phdr are not used in the early code. --- csu/libc-start.c | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/csu/libc-start.c b/csu/libc-start.c index db859c3bed..fb64cdb2c9 100644 --- a/csu/libc-start.c +++ b/csu/libc-start.c @@ -142,8 +142,6 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), int result; #ifndef SHARED - _dl_relocate_static_pie (); - char **ev = &argv[argc + 1]; __environ = ev; @@ -165,24 +163,7 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), } # endif _dl_aux_init (auxvec); - if (GL(dl_phdr) == NULL) # endif - { - /* Starting from binutils-2.23, the linker will define the - magic symbol __ehdr_start to point to our own ELF header - if it is visible in a segment that also includes the phdrs. - So we can set up _dl_phdr and _dl_phnum even without any - information from auxv. */ - - extern const ElfW(Ehdr) __ehdr_start - __attribute__ ((weak, visibility ("hidden"))); - if (&__ehdr_start != NULL) - { - assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr)); - GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff; - GL(dl_phnum) = __ehdr_start.e_phnum; - } - } /* Initialize very early so that tunables can use it. */ __libc_init_secure (); @@ -191,6 +172,11 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), ARCH_INIT_CPU_FEATURES (); + /* Do static pie self relocation after tunables and cpu features + are setup for ifunc resolvers. Before this point relocations + must be avoided. */ + _dl_relocate_static_pie (); + /* Perform IREL{,A} relocations. */ ARCH_SETUP_IREL (); @@ -202,6 +188,26 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), hwcap and platform fields available in the TCB. */ ARCH_APPLY_IREL (); +# ifdef HAVE_AUX_VECTOR + if (GL(dl_phdr) == NULL) +# endif + { + /* Starting from binutils-2.23, the linker will define the + magic symbol __ehdr_start to point to our own ELF header + if it is visible in a segment that also includes the phdrs. + So we can set up _dl_phdr and _dl_phnum even without any + information from auxv. */ + + extern const ElfW(Ehdr) __ehdr_start + __attribute__ ((weak, visibility ("hidden"))); + if (&__ehdr_start != NULL) + { + assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr)); + GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff; + GL(dl_phnum) = __ehdr_start.e_phnum; + } + } + /* Set up the stack checker's canary. */ uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random); # ifdef THREAD_SET_STACK_GUARD