From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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-Google-Smtp-Source: AMrXdXsiMa2TeD0JpWm2PlC2eRY0Dm/QWUuKpCbfCRaDuEEDSK9dG81rPJoBqVE2f0KoJZ09M9L2/w== 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) Sender: Mark Harmstone 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 Content-Transfer-Encoding: 8bit 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 List-Id: 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=' -- 2.37.4