From patchwork Sat Dec 31 20:55:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Harmstone X-Patchwork-Id: 37894 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp3503223wrt; Sat, 31 Dec 2022 12:55:58 -0800 (PST) X-Google-Smtp-Source: AMrXdXv+4czR88AM3C1RjIH9yOCWZRx+rGoSNBxXifuitUzeS5Rj8T9LAP1WU2p1gLExv3may48K X-Received: by 2002:a17:907:d045:b0:7c1:5464:3360 with SMTP id vb5-20020a170907d04500b007c154643360mr43496789ejc.65.1672520158517; Sat, 31 Dec 2022 12:55:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672520158; cv=none; d=google.com; s=arc-20160816; b=QekENUNPt+H+Mxd/hKFAzdCF/pGPzoJMHe0m7dw+2XKHekhOZTxss/yyRfEuCxkWij LETIx/a4YDwkdN3c7lA8p5vWlS9iu3TB/WK/anfnCZCw1EEdSthaEzcIq/sXgA1Yd4i3 Fm1hbpoefRnZ2F7f8FanAXAyxmWuBxN2DXmqZrXSuJaEWK7JXXlsJDGMR2B0mBlo2gt5 0n5PIbstN7pWXviskJ76NmNe1b8teRSIC3E4CfRRhpZglrnNyIVZ4jgWAhjdhDpckTae 0zzAj6CYiUtPi94u7cYDtw/L5+LyaUyQJQ6fwO+i50+xA7s+Kv6rUNk6SwxeWG0Lupmb sN1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :delivered-to; bh=1+O/uXl+74IxmdjCasfXF/BQycdNsRxlkH9ayTdMHMk=; b=IVBN76Ybd8gfo/X9JYb6VueearHVgklWeCtA5/4FqF48wannzoGFeDjCd3Ok8tuWWh zva6opPq6wOH6gqW347nkaG1ktaGBsmByXMNENP3sSVb6eDiiH/Egz2Na/TpKnZJDWv3 i3yqH/gyTtwLGbNb+M1w3bXWB6sKaqPAPCHPtUAoOLIbOrJA0Ogb5D7TFxXNw+X4ahgk 81dBAtjrT6nHz1jOTymA0IxA+gLpdWBlFSaPE5gPjSvLp/C1vz8HLFqHEfS2fnwEcJX+ A+P1/MkFJx4kuHGcI2e+gAaFQac3jlztJkoN6y4vLNRtyZyZ+w04tx3l+Tia1xDfFvcD zE6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20210112 header.b=O5JBMQqf; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id xf13-20020a17090731cd00b007c11812c621si17991613ejb.459.2022.12.31.12.55.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 31 Dec 2022 12:55:58 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20210112 header.b=O5JBMQqf; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 006FB3858002 for ; Sat, 31 Dec 2022 20:55:57 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 722BA3858D1E for ; Sat, 31 Dec 2022 20:55:51 +0000 (GMT) Received: by mail-wm1-x333.google.com with SMTP id ay40so17763894wmb.2 for ; Sat, 31 Dec 2022 12:55:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:from:to:cc:subject:date:message-id:reply-to; bh=1+O/uXl+74IxmdjCasfXF/BQycdNsRxlkH9ayTdMHMk=; b=O5JBMQqf5o1aDa03FXrj8tfyRYQZFV3xHAu1TFf+Kuew1Pe/RzkhAJyKwbVaKR0Kw6 YAlDzM+BoD5sXeDognEGZRFqSddYFDTLAlPZ1hDDHHv2TWVSKquBb0jJxBZez8tbq0vm nEOpxLQTuirXral/dOMH03Jbif/FIlB9a67Yg5e6bh0Gh7RPJx5pJMQxso8WXx2UBevQ W9LbtlcW/k34I3bnTX0wUWTigVkmeexUgGsRVqlbpRYVxPLO/4qqAPwy+R4t44uhcdYz s8KXLjgD9uTEfzvz+g/Z8NYdgsWA0xJmOTDUF2XPvSfHFcVFvLFNZk4hD6qGkOuwTObC p05g== 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:cc :to:from:sender:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=1+O/uXl+74IxmdjCasfXF/BQycdNsRxlkH9ayTdMHMk=; b=MgIA0gu0mERbXGcsFUS/boA0brG56yCj1ilYuGluHt6niKFJXkxGx6mRWvMsUDJFRr DeFTsLDtQbjH5ngTOvDqU99s/VnTfByBuiU95bbtACynbNLlkJbiCuvC06ncVQXJOG56 1eEic9P3C2gbcuKBFghDu3seTGBknT24cSpc+M7LUcWLqnjGvub3DHimHLLrzlz+rwAE 7hRrRRPdgCgaLKjrAmrHIsbX0X2grjuCh9DSpiKH4qwZUUnFTl7WNI84DicTIOisNrxm Z+2+RuWsZ13y66iz3u99HVVu7HScqWWWnYgESPdEbIj/XxbD3auPdtYqOQ1MDx4vfFwO e2Fg== X-Gm-Message-State: AFqh2kovzvWghVa+uh8hV3lJxWroqkn9CSXcmHrneg9XF1Dl7sDjeEPO EZLah/7BGQfb1tqAfs7UdLpsaoGhhgo= X-Received: by 2002:a05:600c:3d0e:b0:3d1:ee97:980 with SMTP id bh14-20020a05600c3d0e00b003d1ee970980mr31820665wmb.7.1672520149876; Sat, 31 Dec 2022 12:55:49 -0800 (PST) Received: from beren.harmstone.com ([2a02:8010:64ea:0:8eb8:7eff:fe53:9d5f]) by smtp.gmail.com with ESMTPSA id j6-20020a05600c1c0600b003b4ff30e566sm59524873wms.3.2022.12.31.12.55.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 31 Dec 2022 12:55:49 -0800 (PST) From: Mark Harmstone To: binutils@sourceware.org, zac.walker@linaro.org Cc: Mark Harmstone Subject: [PATCH] Avoid unaligned pointer reads in PEP .idata section Date: Sat, 31 Dec 2022 20:55:46 +0000 Message-Id: <20221231205546.14330-1-mark@harmstone.com> X-Mailer: git-send-email 2.37.4 MIME-Version: 1.0 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URI_TRY_3LD 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: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753764497661722002?= X-GMAIL-MSGID: =?utf-8?q?1753764497661722002?= This is something I discovered when working on aarch64, though it's relevant to x86_64 too. The PE32+ imports are located in the .idata section, which starts off with a 20-byte structure for each DLL, containing offsets into the rest of the section. This is the Import Directory Table in https://learn.microsoft.com/en-us/windows/win32/debug/pe-format, which is a concatenation of the .idata$2 sections. This is then followed by an 20 zero bytes generated by the linker script, which calls this .idata$3. After this comes the .idata$4 entries for each function, which the loader overwrites with the function pointers. Because there's no padding between .idata$3 and .idata$4, this means that if there's an even number of DLLs, the function pointers won't be aligned on an 8-byte boundary. Misaligned reads are slower on x86_64, but this is more important on aarch64, as the e.g. `ldr x0, [x0, :lo12:__imp__func]` the compiler might generate requires __imp__func (the .idata$4 entry) to be aligned to 8 bytes. Without this you get IMAGE_REL_ARM64_PAGEOFFSET_12L overflow errors. --- ld/scripttempl/pep.sc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ld/scripttempl/pep.sc b/ld/scripttempl/pep.sc index f8fa74364aa..2091e8df0fd 100644 --- a/ld/scripttempl/pep.sc +++ b/ld/scripttempl/pep.sc @@ -34,6 +34,7 @@ if test "${RELOCATING}"; then KEEP (SORT(*)(.idata$3)) /* These zeroes mark the end of the import list. */ LONG (0); LONG (0); LONG (0); LONG (0); LONG (0); + . = ALIGN(8); KEEP (SORT(*)(.idata$4))' R_IDATA5='SORT(*)(.idata$5)' R_IDATA67='