From patchwork Fri Jan 8 16:20:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 1423868 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=8.43.85.97; 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=B7A/cftD; dkim-atps=neutral Received: from sourceware.org (server2.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 ozlabs.org (Postfix) with ESMTPS id 4DC7dB2S4Qz9sWx for ; Sat, 9 Jan 2021 03:21:02 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7F5403982434; Fri, 8 Jan 2021 16:21:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7F5403982434 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1610122860; 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=B7A/cftDC9uyGBXJL7aNPEyMpB573PPViWdm/92+kYi4ezV/miIEdDmXOvfCWyO8b SrOpcDTdUqITl1r0izp2ucD1d+Sb0cfGjwRFHJxSnwxsme9bosZ/pRdqFa4cPDc055 VDh5Rkb3GdbbWjiKSCqP6my54wZf0EKbxifNLh5s= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR01-VE1-obe.outbound.protection.outlook.com (mail-eopbgr140073.outbound.protection.outlook.com [40.107.14.73]) by sourceware.org (Postfix) with ESMTPS id 2F0763982430 for ; Fri, 8 Jan 2021 16:20:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2F0763982430 Received: from DB9PR05CA0021.eurprd05.prod.outlook.com (2603:10a6:10:1da::26) by AM0PR08MB4434.eurprd08.prod.outlook.com (2603:10a6:208:143::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6; Fri, 8 Jan 2021 16:20:55 +0000 Received: from DB5EUR03FT049.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:1da:cafe::ba) by DB9PR05CA0021.outlook.office365.com (2603:10a6:10:1da::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6 via Frontend Transport; Fri, 8 Jan 2021 16:20:55 +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 DB5EUR03FT049.mail.protection.outlook.com (10.152.20.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6 via Frontend Transport; Fri, 8 Jan 2021 16:20:55 +0000 Received: ("Tessian outbound 587c3d093005:v71"); Fri, 08 Jan 2021 16:20:55 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: ffd42821b0ba5526 X-CR-MTA-TID: 64aa7808 Received: from 1dbaa99d84b5.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 19073FB2-0A37-4835-BB34-2BB8E9615725.1; Fri, 08 Jan 2021 16:20:49 +0000 Received: from EUR01-VE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 1dbaa99d84b5.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 08 Jan 2021 16:20:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NdWmYEZPKnqXlibD7JH8mtMPFnjx7M88w1OCz0NAgyevJeCRnIhdI/8OmqHDJUMbMpWK6YG3xUugGCVrS2nleagW2ibeeHgUziCQjeCUfClBeuxSKLbGwJq9ku+eM4GSverTc0UxsBhZs06rcq5AYQ1J14DLUubCq4XVKOUZ9nMkZQ6WjeCynpgtMR5kSLEXgXdwXQ32wOAqQbdVcem3nt1fnj4Bml1bLxp7Y1lDZPpjzPxuGWrvIpPLksuJ0xJNR+778//HTjvdCNeFiU+3Vc9L86ETM0lMNepmdfYnI3jDUD+KmtQbJ0O7NqWmQYPSvV0wXPEaU5OIwQO08xOzqA== 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=fwPK3ut30AjrfkY+qjtwqLGKw1rB/5Y52c//gWf0336GLx9C1/qHgRTKpjLzxJnLydF4BSFrxLFFXLpP6g6Fv4H0RQRZ20PS2VQN+fIUttV5HpZbE6NEdPsEJcWRuY0u7R0bq4hFtiN9QLQC/4Ki0V/Tnff2150q+64MczSdPFdNVts6PBrcSMcdUSJw+y51rfoypl1Xt0jXIeyUA0voG2lecOwPLmJkWDQFRHR0sZj4Tss01uH7Qs02Veybvly8mqf8MERDUJw3lLUeXUqJhEZXZoG/tKxAquVHEh1kJl+g/u14kBjfqH6/MkXzGveaThQ6867LYoDf/s7Ahkw0/g== 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 PR3PR08MB5578.eurprd08.prod.outlook.com (2603:10a6:102:83::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6; Fri, 8 Jan 2021 16:20:48 +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.006; Fri, 8 Jan 2021 16:20:48 +0000 To: libc-alpha@sourceware.org Subject: [PATCH v2 4/4] csu: Move static pie self relocation later [BZ #27072] Date: Fri, 8 Jan 2021 16:20:42 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [217.140.106.51] X-ClientProxiedBy: LO4P123CA0006.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:150::11) 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.51) by LO4P123CA0006.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:150::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6 via Frontend Transport; Fri, 8 Jan 2021 16:20:48 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 3f3e1801-d9ce-4629-8f4e-08d8b3f15ffc X-MS-TrafficTypeDiagnostic: PR3PR08MB5578:|AM0PR08MB4434: 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: mv36xWAT9K6Xhi4VXK6XMTkrCH3e1NCaum05ytDupQL0ThmE9jmDr2aANCPxoNNkqLWpCZ+LhvnR4B+H9EWzj878fRo4c7g1MOrImQ6VN8kOZrKtV5PwcnQyCxk4d8a8Tqy6ZCso1eJ8x57ACx7VddJgnwCjcQ0XtoG5f4ov6XSymMgAmZ1G9qwaLFutOwoXBCL46kHDzvIXx75PYd8b3x9VvQLwgVH86OloyVv5BcLYROqBIVydJ5VoqD+jYC+F4gNzxicJZvJt1B3kBn7Mkor2uDmMIpRbYe01/IOPhS4mOC3SMzICOf6HRUq4gVwV912yp938u900pCpwDrn19UHPo+6Qtjq16o20qXbJ23VL0WoYunT1tZM1BGtPpjCHHHl927HTwuiSQQ4YBUaYUZaWUH2gGpw0D+f8VCljtrY1EIAoveN+sDmc8bUHy93tLQTvCwj2DTvyD4QtIj9z+A== 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)(346002)(376002)(136003)(396003)(39850400004)(366004)(36756003)(8936002)(52116002)(86362001)(69590400011)(16526019)(6506007)(26005)(6916009)(83380400001)(186003)(5660300002)(6512007)(6666004)(66946007)(66476007)(8676002)(316002)(66556008)(6486002)(44832011)(956004)(2616005)(2906002)(478600001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: wTBxtUH0PZJBpq0TW4j8mULxOFoQo99GZbZIY/biCApMMxHXKupxA/+87RJBMzvEjUUMLs/MYXPbmJtPCFWXZCez0AHLitI7vxeOVdH82hYp/wwBWKVXB3uU4fZJaqyJE8BfXrtJm3/F81AoWo4hA2ctnACtyT8+5ZHAEuqKl2ToVW5TT5INtwGYq55KiCano8FbVbSvm/pwMXrOEepU2vNuGTVT4NyCIZstXN90X6h2OOxnyEQLRzEqScgToCjTTFoz24kgweC7dZR/JyhRWROGOYkWzX237FTZCE1SD74HJGz7ao9553+Bmybb5cDd/5HP8AYYlfJffSMHTdE66L1kDJA0rbbaoHA4+ykCqloioVZNgDZVQsyW05DvaLXCGPvBNaJ+SnXrWZZbszTcMqm9XDWHszyTIIhNUwtP68bx63gG4Xl7WavQuo58Xyt8Kdv8BxUJgzPrHnqZvVVTOr0fszbuka1monOn/jJu0ImB4hNoOT1sGuz/yNpmPygbIm4BQKV5oTfaJfdTILgqrSY3W1vbgcWZxA4QcifHe31CMwHYDOVSz+dZujXX/FWsUAk4x+RGbCjDLZaBXiB/0Z7XRfn8ST56P8eEynmnQ7Yl8mVd/4A+2DdJne6uszKTzATbDe5HVjAl1VR6weufVurMal+4zFZoJDXJnio6K2AhVQiX4wMHmnRxw+l6Kpz2BJTtFr78ZH/X++x9EzNKiuEqV9AL9sGHhEWM0Dbwi0uOsC9ifBieFxBtae7YeURosINoO/eDabhWJBOgbdfmjXePbHb2Y3PmwX059Y28EOpn0x12qBwEHkNCm7vq1RyeHBMPIZ4khhwO4UGsvWJEJ093y4szN/cqNkyNWyvhZ5sKk/NQI5Ql5sl5Rv3q6TJoB63JMRZAQwK41zhLeFOKMoUKUNp7VtJ/HQqozeXIGJ5UTWtQCzvbmyz8E7qAjLiPJynlobEHBdjZOMQ3c2M+lT0iEs3jc6Y8O2+QTqRu83gGwWywntmhcybxNA7wpQue X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR08MB5578 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: DB5EUR03FT049.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: 5bc917d4-8d43-4264-fb19-08d8b3f15c03 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3PfLbF0zuv+bkH4If1c/oZ2VXRdrbXuDDxaE2ZrHVE9H7gzCG2gX++2WmaDajTRB+3gBvamR4lZP5UKqgLm2svUU3GX48DSiOwIfYaq3fJxKMTywWMyAW8/yjXTTG+LLIFtF8ysZRS+43/Yiv8+UChS12RpfmRlvlVjJNQ8Z7wXj/pyToPJ4JTRWv7c4NS258WPdRkwWEZSJBsXwRHtq3tkMfQ8sSE/IvxoX7NB6FHqbqmiExq2zMEzCaZnFILYfksQvi89DX/iGH1uwM3qZnQapVtE1w3lrDq7NRycPl7FBWBRyfG3eaNZcEZ4aZCrRl6fHI03Y/urC5yzrq5hbCuFjP3b7ZqO39mgxA24a3MgXzwvHr0XbSL9eadi8ECql7S4ZsNDU3prpwTFwY/hVryRIv7FAAnulKLLQUunotRMdI/4PXLxTE5b/ZI2DEnFHAghqfLgtS3ECWycDKVWAgDqgywU8do/PcvLte/aPQ+YTVSgTUrcub5f87odZwqStMe/PNGbHk8t1FXFb7YNFCw== 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)(136003)(39850400004)(346002)(376002)(396003)(46966006)(956004)(2616005)(34020700004)(6512007)(44832011)(6506007)(82740400003)(6916009)(82310400003)(70586007)(186003)(6486002)(36756003)(316002)(16526019)(70206006)(86362001)(2906002)(26005)(6666004)(336012)(5660300002)(8676002)(8936002)(478600001)(47076005)(83380400001)(69590400011)(81166007)(356005); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jan 2021 16:20:55.1742 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3f3e1801-d9ce-4629-8f4e-08d8b3f15ffc 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: DB5EUR03FT049.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR08MB4434 X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_LOW, 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