From nobody Thu Jul 23 21:53:25 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1783435418; cv=none; d=zohomail.com; s=zohoarc; b=Fxcjo78ubYDMY2mNf2b8VrdOAW+AaaGwMi4fbd/aUvFn7g31HjlFKMPxL64I8cwhpj3R8/J+mtcZU80P3vJrG6NY8Is30A8tNrcNu6Q8n5ugs/9aWIG4sjTNURladnpNHICgQW0/Fio2gH7sLCSPUowE8TAQ55HGSniJWc6oimA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783435418; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=zWdYCF/A+zQ3wz2P11T6f9u/k5R4lW8TcTPhxiBHdB8=; b=Xfv7MVS7lfXh+GzdSpFXLFqelp7/IYD755yyEyASM55t+C3hlcVaaiM5lGJ11TaxfSct0Fg2npP9XGN1n87eUA9Iy4WY1TlUgRKwY/8oHJK6s5/XcBMiqB32Bp10EaVrIO0C0DVRfAA9pRPUyrqq0BCT6nhzH+XKvHeBVT3HLuY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1783435418661518.0919252587596; Tue, 7 Jul 2026 07:43:38 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1356286.1610917 (Exim 4.92) (envelope-from ) id 1wh718-0000h6-98; Tue, 07 Jul 2026 14:43:22 +0000 Received: by outflank-mailman (output) from mailman id 1356286.1610917; Tue, 07 Jul 2026 14:43:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wh718-0000gz-6A; Tue, 07 Jul 2026 14:43:22 +0000 Received: by outflank-mailman (input) for mailman id 1356286; Tue, 07 Jul 2026 14:43:21 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wh717-0000gr-4L for xen-devel@lists.xenproject.org; Tue, 07 Jul 2026 14:43:21 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wh716-0057RY-HK for xen-devel@lists.xenproject.org; Tue, 07 Jul 2026 16:43:20 +0200 Received: from [10.42.69.3] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4d107a-2eae-0a2a0a5409dd-0a2a4503bde8-40 for ; Tue, 07 Jul 2026 16:43:20 +0200 Received: from [209.85.221.45] (helo=mail-wr1-f45.google.com) by tlsNG-33051d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4d1088-ec1a-0a2a45030019-d155dd2de4f0-3 for ; Tue, 07 Jul 2026 16:43:20 +0200 Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-470174001a0so2357785f8f.0 for ; Tue, 07 Jul 2026 07:43:20 -0700 (PDT) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47aa039b0cesm34734408f8f.22.2026.07.07.07.43.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Jul 2026 07:43:19 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:Content-Type:In-Reply-To:Autocrypt:Content-Language:References:Cc:To:From:Subject:User-Agent:MIME-Version:Date:Message-ID" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1783435400; x=1784040200; darn=lists.xenproject.org; h=content-transfer-encoding:content-type:in-reply-to:autocrypt :content-language:references:cc:to:from:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to:content-type; bh=zWdYCF/A+zQ3wz2P11T6f9u/k5R4lW8TcTPhxiBHdB8=; b=DbiuuC/b6rbH7agNGrR8tAaXm+f0VhVW4u6bNxJujt6Z+zzLt+cCwnoXr+iEXDyxcN LnAaFsTiyBcDoKtB02ahkpcAXv2YHoNtCrcLX886zN7qEPSN48DPBcGko2nI7x22y1VZ Z7kJVTt91tq7gP0L47UutmiRvEZekwIw23UXzsRu7SedIbsPj8PaYzKD3QLx1JrlHkpA JO+kYrJN1RfoFR22++aA4ru+0TyQcORKk59sjXIKe2VwKa8YoHsr0GnzqcBeoq2nHyS1 cbo7h1EoRWWrK/UTnYlRKSr1FM2F9jOy8kQgLK9icMxDrtZLSloIuh4bR9QF3DnqRM5j QLRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783435400; x=1784040200; h=content-transfer-encoding:content-type:in-reply-to:autocrypt :content-language:references:cc:to:from:subject:user-agent :mime-version:date:message-id:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=zWdYCF/A+zQ3wz2P11T6f9u/k5R4lW8TcTPhxiBHdB8=; b=O+gqbV6qA7Kkq/ibnSOmRrZSwKjs9v4M3GjdxCp3p5dQxpX92xUmY543uuZJA9MfSh Lz3epVXWgakatO1ciSTmEwL/Skusuh2ZAF75vlRiWfm4K2sx1rQt+LJVNkxeCcun2O8X tK23bhrJ+dC478KsLINYt/Z4opCX+nKEbuGPtq0aezN765R2hudRI+EHnbhbFIG1IQHi R9o4mUGKZ62z+rG7TD4pgyhwTgR9F+foTgtGoF1ow19VUhwHj1e2ilmhf69s3YE8gmO2 PGcfgMaM0km8OoFKK3wx9brJpqLYJbVRoqwfwi33pK72RU3iESYr/tibiB6T3hjaDGTR 1jwA== X-Gm-Message-State: AOJu0YwI6nC6qx/QcYwvT/jnR7DR/TqCOIiILCR+SA+Pw12VkXZcJDTl 4ydSerc9t0l+y/CnMf0JWyvW+iLTGJ8omrQRjfHY9eb7buNezhDiJzsQ8G/spYFWUkuUhbVg24f oE0TtUw== X-Gm-Gg: AfdE7cm+z90Fbm3glWMI+Jtw0vTV9YXjBuOS6cvo7jONK5VuINS58/JKDUuIcyRxnF/ Oc8FKEvzQRSiK+sgfUhVApPlz+46/h9w1chvmvT4frzM0sKmeu0CLogieYd7XDwVJbTm6jBNptp vfN6t6yLt1GFqHDi3Gcg2cVwNZJzBycuJc4Vt5dc4EtgKHspKf6mAHldN2vj5ZQprD890J6mm3k B3L7vKPcv1NA35NzOYx9I97bOgjlOh2UihxzKF0umoW7a3vZToWs0uUWXcU/JJcxbXPCKlcRDVw VCZM+O/r8V5VPwYicBYurF/9CzDP+aybuPzwdXuozWdSFjwvQjMzUvzYI8/05QalqYK3RnNuLZO 8SXz/kr19yJhehq23MQTwVJFY8c8t1483Z1FaJ9lDiCVrqR0fQzKCKBSbz0qTTSVSh1x/66XKz+ FbNXJqEuanG4zP3O5NL2U1AhLXMAzWsy2kwHIeiShTbJJ/J+9SPVq184uoOpzpyROfAy4dkPn1o l/M X-Received: by 2002:a05:6000:d4b:b0:477:71ef:5d81 with SMTP id ffacd0b85a97d-47de66c7134mr5022894f8f.6.1783435399757; Tue, 07 Jul 2026 07:43:19 -0700 (PDT) Message-ID: Date: Tue, 7 Jul 2026 16:43:18 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v2 1/3] x86/mkelf32: tidy what is written out From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= , Teddy Astie References: <7a495793-53b1-4681-9c74-fa7e901ccf59@suse.com> Content-Language: en-US Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: <7a495793-53b1-4681-9c74-fa7e901ccf59@suse.com> Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-33051d/1783435400-B652F5D1-332A220F/0/0 X-purgate-type: clean X-purgate-size: 2283 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1783435421018158500 Content-Type: text/plain; charset="utf-8" First, replace an open-coded literal number by an expression. Then adjust tail padding: Don't write up to 4 bytes (and at least one), but truly only pad to the next 4-byte boundary. And pad with zeroes, not with whatever is left in buffer[]. Finally drop the explicit nul character in out_shstrtab_extra[] - the one which is being appended anyway will do fine. Signed-off-by: Jan Beulich --- v2: New. --- a/xen/arch/x86/boot/mkelf32.c +++ b/xen/arch/x86/boot/mkelf32.c @@ -68,7 +68,7 @@ static Elf32_Phdr note_phdr =3D { =20 static uint8_t out_shstrtab[] =3D "\0.text\0.shstrtab"; /* If num_phdrs >=3D 2, we need to tack the .note. */ -static uint8_t out_shstrtab_extra[] =3D ".note\0"; +static uint8_t out_shstrtab_extra[] =3D ".note"; =20 static Elf32_Shdr out_shdr[] =3D { { 0 }, @@ -97,11 +97,11 @@ static Elf32_Shdr out_shdr[] =3D { }; =20 /* - * The 17 points to the '.note' in the out_shstrtab and out_shstrtab_extra + * .sh_name points to the '.note' in the out_shstrtab and out_shstrtab_ext= ra * laid out in the file. */ static Elf32_Shdr out_shdr_note =3D { - 17, /* sh_name */ + sizeof(out_shstrtab), /* sh_name */ SHT_NOTE, /* sh_type */ 0, /* sh_flags */ DYNAMICALLY_FILLED, /* sh_addr */ @@ -468,13 +468,21 @@ int main(int argc, char **argv) do_write(outfd, out_shstrtab, sizeof(out_shstrtab)); /* Our .note */ do_write(outfd, out_shstrtab_extra, sizeof(out_shstrtab_extra)); - do_write(outfd, buffer, 4-((sizeof(out_shstrtab)+sizeof(out_shstrt= ab_extra)+dat_siz)&3)); + bytes =3D sizeof(out_shstrtab) + sizeof(out_shstrtab_extra) + dat_= siz; } else { do_write(outfd, out_shstrtab, sizeof(out_shstrtab)); - do_write(outfd, buffer, 4-((sizeof(out_shstrtab)+dat_siz)&3)); + bytes =3D sizeof(out_shstrtab) + dat_siz; } + + /* Pad to a multiple of 4 bytes, whatever that's good for. */ + if ( bytes & 3 ) + { + memset(buffer, 0, 4); + do_write(outfd, buffer, 4 - (bytes & 3)); + } + close(infd); close(outfd); From nobody Thu Jul 23 21:53:25 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1783435478; cv=none; d=zohomail.com; s=zohoarc; b=NLS489uJQDxG2RdPDnAbK2yM2d4yXcRMLQiuI1zV0+thW7mx0AxBrG5uSSQGCnH48bCy6z0MZnXb/AlMhML6sWZ4y9Zg7Vo/HBRbojXLgp1vwUjSUVbQUeF/DRtcwbOuKDjhY6iZikyqadS07DdAf6bMIYx1Ws/Ojg9R7Ijacis= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783435478; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=u3hyLhlP5/kin8s0XlLRomZ02/iENCnGZeQ9jDs63Ng=; b=YslX9RqbNKi/ieJKZc89TibEnQmqlWjN/PYUWYxUn1Sg4MfwQqQaIFZxCFxHkAc3tlbt8WXXwJqkF1qsdVt0hL5Lg3PDNYgq4K3yn8ui3Kd3iDildZus6uOg6PLfRyaT/eR9njyaBkqj0a+ETmqkdg/fieytmSZgOcgrpMcpW1s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1783435477974563.709380912679; Tue, 7 Jul 2026 07:44:37 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1356293.1610927 (Exim 4.92) (envelope-from ) id 1wh727-0001Bz-Hf; Tue, 07 Jul 2026 14:44:23 +0000 Received: by outflank-mailman (output) from mailman id 1356293.1610927; Tue, 07 Jul 2026 14:44:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wh727-0001Bs-Ek; Tue, 07 Jul 2026 14:44:23 +0000 Received: by outflank-mailman (input) for mailman id 1356293; Tue, 07 Jul 2026 14:44:22 +0000 Received: from mx.expurgate.net ([194.145.224.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wh726-0001Bh-8Y for xen-devel@lists.xenproject.org; Tue, 07 Jul 2026 14:44:22 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wh725-00EAaw-AH for xen-devel@lists.xenproject.org; Tue, 07 Jul 2026 16:44:21 +0200 Received: from [10.42.69.12] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4d10c1-bab6-0a2a0a5309dd-0a2a450c8e28-2 for ; Tue, 07 Jul 2026 16:44:21 +0200 Received: from [209.85.128.49] (helo=mail-wm1-f49.google.com) by tlsNG-d25034.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4d10c5-f399-0a2a450c0019-d1558031b9b5-3 for ; Tue, 07 Jul 2026 16:44:21 +0200 Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-493c52cde9eso42331845e9.3 for ; Tue, 07 Jul 2026 07:44:21 -0700 (PDT) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493e01c9faesm43411395e9.1.2026.07.07.07.44.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Jul 2026 07:44:20 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:In-Reply-To:Autocrypt:Content-Language:References:Cc:To:From:Subject:User-Agent:MIME-Version:Date:Message-ID" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1783435460; x=1784040260; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :references:cc:to:from:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=u3hyLhlP5/kin8s0XlLRomZ02/iENCnGZeQ9jDs63Ng=; b=gx2EyMmx0iS0lASa5sQF1hBDmVvXBBAu1xoRabd72VNz5OvPP/s69Xnh3PhH0GZU2k pN+2M6z/UTkKEWWFxUqQCtsQxDM/QCFQdnUgN6VxPhfSQSIEQpk0Z0rCvDOotBfiHV2X JpGxSpmhHdnSkp2k2lD5ui8Bn+HX1Gn0GA5nD5bZXigx1LTvQrJYLULzDXn08EAwFncz gYqEM6kzgzR5/vW2mLXew/i67ds40tdz0VwigFkMmuhy88ZSGEztkO+8mnmygyv9bjki Nt6DKZZOlpRDjziuLSuO+YSgZlmYoMuH/sBp4jmNetazPmedeqxy0oOuKYolBhGCRXlR BgOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783435460; x=1784040260; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :references:cc:to:from:subject:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=u3hyLhlP5/kin8s0XlLRomZ02/iENCnGZeQ9jDs63Ng=; b=rkgKV8VzBfn1/PTI0OV9Vx4W9MQgqv7rYAT6+f23h/76OIAUHDPzIvNkiIkGg2kdzd HRpOkG4GbfeM2fBz0C2cAtOGgyKw3F7cJ5+ZmPIWXo/aDxsqZvoBcAnlHPclmX2DtlZG zQsnOfuHPk7PaqQ9R9OS2wPk4sZVMdR0FtFzZfsA6b8Oy1rMQeEHtyjtkOWJ+30tQPbD gtJCIPlBUhr5K7Za4MNO32n28QHUbb9E+C2ua2a38OK/XyrkoQ6T11KPY39AXB8b+NOF wNse14SviiMQrSbMxl80OisMvNrgVKGCrZ+142m23mSzH52bCNFZVn3b+b2bOLrwPvNn bgeg== X-Gm-Message-State: AOJu0Yx4Fa1Lavxn8TKDJlt6Jx78bMFiId3p/qFz2j5n0hj+dIAeHpbo XgJsdR3o5k+iSkqV53awNM3EHCKH/wEBlz6Bp5hHMfH6ifotjhbthor3yDDKz6ESNRws7Vq1VB1 7qophRw== X-Gm-Gg: AfdE7cmApKhzc0jGCarePy5kmLUIonpFljKFstKu5d/fDsz84eFPzXffMeCAfKw7gzH c0T+/M7vEjoVb/xVkfw2BjJekAxTN0T/pi4eLCD0mgzz2uAQrLs2a+t6i1npEOVkA4qEESoOsbO p+aUbh6a2mg20uTHkniWu7lhVIX+5K0Y148NKH9qVmt91bzXqayvkeQDZY1CFivyPQQCAnxDSt6 UDudPgiXXLOLOCz7oU1A91l7HHhO2tuhYr+O/ec7DiTRzpw3zPC/k2/lGnncrIgjRC0BFdxYIjT zJC8yc3OPbBZM9+D9dPI+OO4qp3Kk/JEADuwtqUwo2Rf6zRQnG5NArMud0chcEuvG1YHEbAP63R Eux01HHap3wbV/Ia3kUmyu/xesTgFbnbLiOoTk1SxXCLgkZ7dph9OKB/fhnBvZDzgFpqWwOtwzm tl4g6B4kp5STs1ZocTnkxsM9A3jl1clwKyZG71utq+bMSiuONR9lbea3AZZxPjdqMEjMu/zHXNM zUb X-Received: by 2002:a05:600c:154d:b0:493:b8d9:f28b with SMTP id 5b1f17b1804b1-493df09313bmr68573075e9.23.1783435460526; Tue, 07 Jul 2026 07:44:20 -0700 (PDT) Message-ID: <28550fc7-eb6d-4d4b-ad6e-077dceb2a4f8@suse.com> Date: Tue, 7 Jul 2026 16:44:19 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v2 2/3] ELF: correct .note.* alignment handling From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Julien Grall , Stefano Stabellini , Anthony PERARD , Michal Orzel , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= , Teddy Astie References: <7a495793-53b1-4681-9c74-fa7e901ccf59@suse.com> Content-Language: en-US Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: <7a495793-53b1-4681-9c74-fa7e901ccf59@suse.com> Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-d25034/1783435461-93B35D51-1BE06C69/0/0 X-purgate-type: clean X-purgate-size: 7337 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1783435479979158500 Content-Type: text/plain; charset="utf-8" The present uniform 4-byte alignment hasn't been right for, I think, a very long time (albeit not forever). As per e.g. [1], 8-byte alignment is required in 64-bit ELF containers (and assembler-generated .note.gnu.property, for example, is 8-byte aligned, while - oddly - linker-generated .note.gnu.build-id is only 4-byte aligned [2]). Sadly libelf is also affected, and hence going strictly by the spec would break kernels also getting it wrong (e.g. Linux). Apply the same heuristic as GNU readelf does: If section alignment is 4 or less, assume only 4-byte padding. [1] https://refspecs.linuxfoundation.org/elf/gabi4+/ch5.pheader.html#note_s= ection [2] https://sourceware.org/bugzilla/show_bug.cgi?id=3D33259 Signed-off-by: Jan Beulich --- Arguably the spec text is ambiguous as to the width of namesz, descsz, and type: They could well be meant to be 8-byte quantities in 64-bit ELF as per "each entry is an array of 8-byte words". Yet with everyone using 4-byte fields, that's the de-facto standard now anyway. --- a/xen/arch/x86/include/asm/asm_defns.h +++ b/xen/arch/x86/include/asm/asm_defns.h @@ -398,14 +398,14 @@ static always_inline void stac(void) =20 #define ELFNOTE(name, type, desc) \ .pushsection .note.name, "a", @note ; \ - .p2align 2 ; \ + .balign BYTES_PER_LONG ; \ .long 2f - 1f /* namesz */ ; \ .long 4f - 3f /* descsz */ ; \ .long type /* type */ ; \ 1: .asciz #name /* name */ ; \ -2: .p2align 2 ; \ +2: .balign BYTES_PER_LONG ; \ 3: desc /* desc */ ; \ -4: .p2align 2 ; \ +4: .balign BYTES_PER_LONG ; \ .popsection =20 #define ASM_CONSTANT(name, value) \ --- a/xen/common/libelf/libelf-dominfo.c +++ b/xen/common/libelf/libelf-dominfo.c @@ -583,6 +583,8 @@ elf_errorstatus elf_xen_parse(struct elf count =3D elf_phdr_count(elf); for ( i =3D 0; i < count; i++ ) { + unsigned orig_align =3D elf->note_align; + phdr =3D elf_phdr_by_index(elf, i); if ( !elf_access_ok(elf, ELF_HANDLE_PTRVAL(phdr), 1) ) /* input has an insane program header count field */ @@ -597,10 +599,15 @@ elf_errorstatus elf_xen_parse(struct elf if (elf_uval(elf, phdr, p_offset) =3D=3D 0) continue; =20 + elf->note_align =3D elf_uval(elf, phdr, p_align); + more_notes =3D elf_xen_parse_notes(elf, parms, elf_segment_start(elf, phdr), elf_segment_end(elf, phdr), &total_note_count); + + elf->note_align =3D orig_align; + if ( more_notes =3D=3D ELF_NOTE_INVALID ) return -1; =20 @@ -616,6 +623,8 @@ elf_errorstatus elf_xen_parse(struct elf count =3D elf_shdr_count(elf); for ( i =3D 1; i < count; i++ ) { + unsigned orig_align =3D elf->note_align; + shdr =3D elf_shdr_by_index(elf, i); if ( !elf_access_ok(elf, ELF_HANDLE_PTRVAL(shdr), 1) ) /* input has an insane section header count field */ @@ -624,11 +633,15 @@ elf_errorstatus elf_xen_parse(struct elf if ( elf_uval(elf, shdr, sh_type) !=3D SHT_NOTE ) continue; =20 + elf->note_align =3D elf_uval(elf, shdr, sh_addralign); + more_notes =3D elf_xen_parse_notes(elf, parms, elf_section_start(elf, shdr), elf_section_end(elf, shdr), &total_note_count); =20 + elf->note_align =3D orig_align; + if ( more_notes =3D=3D ELF_NOTE_INVALID ) return -1; =20 --- a/xen/common/libelf/libelf-loader.c +++ b/xen/common/libelf/libelf-loader.c @@ -72,6 +72,9 @@ elf_errorstatus elf_init(struct elf_bina return -1; } =20 + /* Record default note alignment, as per EI_CLASS. */ + elf->note_align =3D elf_64bit(elf) ? 8 : 4; + /* Find section string table. */ section =3D elf_uval(elf, elf->ehdr, e_shstrndx); shdr =3D elf_shdr_by_index(elf, section); --- a/xen/common/libelf/libelf-tools.c +++ b/xen/common/libelf/libelf-tools.c @@ -288,6 +288,20 @@ ELF_HANDLE_DECL(elf_sym) elf_sym_by_inde return sym; } =20 +/* + * Notes are special: Formally for a long time the spec has demanded that + * 64-bit ELF would have 8-byte padding at respective places. However, ma= ny + * producers were never updated, so apply a heuristic GNU readelf also app= lies: + * Take section (or segment) alignment into consideration. When alignment= is 4 + * or less, assume only 4-byte padding. + */ +static unsigned elf_note_round_up(const struct elf_binary *elf, unsigned p= os) +{ + unsigned align =3D elf_32bit(elf) || elf->note_align <=3D 4 ? 4 : 8; + + return (pos + align - 1) & ~(align - 1); +} + const char *elf_note_name(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note= ) note) { return elf_strval(elf, ELF_HANDLE_PTRVAL(note) + elf_size(elf, note)); @@ -295,9 +309,9 @@ const char *elf_note_name(struct elf_bin =20 elf_ptrval elf_note_desc(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note)= note) { - unsigned namesz =3D (elf_uval(elf, note, namesz) + 3) & ~3; - - return ELF_HANDLE_PTRVAL(note) + elf_size(elf, note) + namesz; + return ELF_HANDLE_PTRVAL(note) + + elf_note_round_up(elf, + elf_size(elf, note) + elf_uval(elf, note, nam= esz)); } =20 uint64_t elf_note_numeric(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note= ) note) @@ -339,11 +353,9 @@ uint64_t elf_note_numeric_array(struct e =20 ELF_HANDLE_DECL(elf_note) elf_note_next(struct elf_binary *elf, ELF_HANDLE= _DECL(elf_note) note) { - unsigned namesz =3D (elf_uval(elf, note, namesz) + 3) & ~3; - unsigned descsz =3D (elf_uval(elf, note, descsz) + 3) & ~3; - - elf_ptrval ptrval =3D ELF_HANDLE_PTRVAL(note) - + elf_size(elf, note) + namesz + descsz; + elf_ptrval ptrval =3D + elf_note_desc(elf, note) + + elf_note_round_up(elf, elf_uval(elf, note, descsz)); =20 if ( ( ptrval <=3D ELF_HANDLE_PTRVAL(note) || /* wrapped or stuck */ !elf_access_ok(elf, ELF_HANDLE_PTRVAL(note), 1) ) ) --- a/xen/include/xen/elf.h +++ b/xen/include/xen/elf.h @@ -29,7 +29,7 @@ =20 #include =20 -#define ELFNOTE_ALIGN(_n_) (((_n_)+3)&~3) +#define ELFNOTE_ALIGN(_n_) ROUNDUP(_n_, BYTES_PER_LONG) #define ELFNOTE_NAME(_n_) ((char*)(_n_) + sizeof(*(_n_))) #define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + ELFNOTE_ALIGN((_n_)->namesz= )) #define ELFNOTE_NEXT(_n_) ((Elf_Note *)(ELFNOTE_DESC(_n_) + ELFNOTE_ALIGN(= (_n_)->descsz))) --- a/xen/include/xen/libelf.h +++ b/xen/include/xen/libelf.h @@ -181,6 +181,12 @@ struct elf_binary { char class; char data; =20 + /* + * Note alignment is defaulted from EI_CLASS, but overridden by + * segment / section alignment. + */ + unsigned note_align; + ELF_HANDLE_DECL(elf_ehdr) ehdr; elf_ptrval sec_strtab; ELF_HANDLE_DECL(elf_shdr) sym_tab; From nobody Thu Jul 23 21:53:25 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1783435511; cv=none; d=zohomail.com; s=zohoarc; b=gdsnwUpyLhe6fmoRb3mk5cGii7zYiR0uJisaNpAkagxo/V8P6oLfv0kARBPQh+vlb2JA2LOMwNtKaFe0/jJML6+mJrOA6Q2sVTngo2zegyQ0ArA8dHFs+VXReyjw1NqecbZ9VmcrlTbh+BQjyJ6EW0n7jP6eZhjrG1VOjsA3U9Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783435511; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Uco6iUozAyjagFtzC9lAXRJPgK3e6gqt9d8xgjkfa5M=; b=mx4cJ3S3EbeIyC+btXRZbDRuSfy6wrM3jNiPSilitPmhkTho81l+xZnJa5uUhtD0lJlOykaYct5Rbnky6y/VB+8OGjfLmpC2Qrx0A2BZQ2USVhGbE81BJgngd+9t/DFU5zaWGkrLXUhqujFy3CrJ57bougDYIolKh597i+gjK1w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1783435511250563.7307622180639; Tue, 7 Jul 2026 07:45:11 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1356300.1610935 (Exim 4.92) (envelope-from ) id 1wh72c-0001es-Ss; Tue, 07 Jul 2026 14:44:54 +0000 Received: by outflank-mailman (output) from mailman id 1356300.1610935; Tue, 07 Jul 2026 14:44:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wh72c-0001el-QH; Tue, 07 Jul 2026 14:44:54 +0000 Received: by outflank-mailman (input) for mailman id 1356300; Tue, 07 Jul 2026 14:44:53 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wh72a-0001eQ-To for xen-devel@lists.xenproject.org; Tue, 07 Jul 2026 14:44:53 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wh72a-006gde-Aj for xen-devel@lists.xenproject.org; Tue, 07 Jul 2026 16:44:52 +0200 Received: from [10.42.69.9] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4d10d4-e002-0a2a0a5209dd-0a2a4509ad3a-8 for ; Tue, 07 Jul 2026 16:44:52 +0200 Received: from [209.85.221.54] (helo=mail-wr1-f54.google.com) by tlsNG-bad1c0.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4d10e4-97e6-0a2a45090019-d155dd36ed98-3 for ; Tue, 07 Jul 2026 16:44:52 +0200 Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-47ddf7b09e5so2240558f8f.1 for ; Tue, 07 Jul 2026 07:44:52 -0700 (PDT) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47a9e4d780csm35456052f8f.11.2026.07.07.07.44.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Jul 2026 07:44:51 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:Content-Type:In-Reply-To:Autocrypt:Content-Language:References:Cc:To:From:Subject:User-Agent:MIME-Version:Date:Message-ID" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1783435492; x=1784040292; darn=lists.xenproject.org; h=content-transfer-encoding:content-type:in-reply-to:autocrypt :content-language:references:cc:to:from:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to:content-type; bh=Uco6iUozAyjagFtzC9lAXRJPgK3e6gqt9d8xgjkfa5M=; b=Hp6kxM4Rj8dk5RsnCdci7ZVH1TjG+VpwvP+7hMxyhU6+Y+C+G8WSpUhqbEwuPJCUkg M18SKTM/vpYq00yUK5K1ncYjuhk0VmzXZwqjYW+oXsBLAYOIFk2icKSlAAVRFJETCI/v P8teO+Ph74N6EznLosPq4ENFE5RggnZ8/uOU0a2EDtcMLo0Gtm/ovdp9mvmfTNkkgMZW zkZJ3xcxzTgtZtb9/kJgaYOIXABQsPnkzMihwKJSPbsIdYkrDSTc+FzEWl4+9DZWRsQa SY2V9nzhJ4CJECASQevd7wUnIj3Ys5+6+aN4bWpqFd5cXXmfzU15hH00A8dp7BMpLvGY brxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783435492; x=1784040292; h=content-transfer-encoding:content-type:in-reply-to:autocrypt :content-language:references:cc:to:from:subject:user-agent :mime-version:date:message-id:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=Uco6iUozAyjagFtzC9lAXRJPgK3e6gqt9d8xgjkfa5M=; b=LKguKx0a2XhUERmpTsgjm95ByY1KLHdnzLLwC4r+C0dA1Mx2O8QQifsScn9bwoc4qG 2/HlfzFBxB9+lB/dehwteyxbM3QNGnviIZLAlUyHsRwVwND5EZIae0WG9MmULvbjkkPa VNfbn7AA6o8AFNYiN5fGkfSASsFEGaOJthR3EQaRX2RouNXzN+3TVWMerv+QMEMDYude ST8mPTQVkDPFQHkWRtGgyGZ9qP4uuiOGsZAR8Louulr4tux55XZp9ABEjJYqJ2stw+Bw gwbILVoplddSQB9ojrJYRXlvXhIJ9RdYc39f0+d9GTLM6s0W9AGnflVhfeE0enkYEvdH ViJA== X-Gm-Message-State: AOJu0YwmuKHvUyw4YgrDkkbCKSiT/gKq6DM+8FJ99lGIGTU0EEEScOdl aQ28RvjzMRa60RYUy5fykm6EVOyA9nS7tpePFX9feJUj3yxsIJspOwXPIdCYjGX/WGyVcM4fl8c jMRZHLA== X-Gm-Gg: AfdE7cm67W9SGhgdDbriUfYKj2eNSb9uFN0HuK2uRvRBXzoBynETInw/X+MMGdV/vHB JkH2e7KLMCZ9dCUJSEVB/ckCozjejBVVT045pI+oY1Q1o2M6ESqK8E+/jFaW4+DzOyXaVdmGA/B O2HRw+u3JmErJE/vMMyrE9Y5JqZUiv5OnDu1pEh3gQBG8myB+LM4ubDea9vB58dmfK/jqqeCpUG sBRk+ObcuatkffACkzylqEbTPh1la3FYQcq07g9JiBTXOxNzqjh1QICg01DjhrVADdCMJMQLdoq nVVRX0jN1mNP3PyBF1oEYpU72qRm91T160REq4wOHgQnKXeGPy9dqgYXJ6BAFBY+8M1RKXe+3Jo tRqcbwCKjZQlP55ZuWR5VaicxprnbEfPG4jqKU06SoC/KqQ5hpG+Z0JAjvFGby3RVmgnAngJGOP 7T7iyOsDmCeLxF9u8Z1hgyQEgrk1J35p0VI94SRz71ngn+o7MZbNVKeHGsaH0PLyC90t4zo5UvQ Wyr X-Received: by 2002:a5d:56c2:0:b0:473:1706:7efe with SMTP id ffacd0b85a97d-47de66b489amr4385109f8f.24.1783435491686; Tue, 07 Jul 2026 07:44:51 -0700 (PDT) Message-ID: <6657fbca-d261-439f-a40d-77b7c1c93696@suse.com> Date: Tue, 7 Jul 2026 16:44:50 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v2 3/3] x86/mkelf32: re-write ELF notes From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= , Teddy Astie References: <7a495793-53b1-4681-9c74-fa7e901ccf59@suse.com> Content-Language: en-US Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: <7a495793-53b1-4681-9c74-fa7e901ccf59@suse.com> Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-bad1c0/1783435492-571AA986-E6A19B44/0/0 X-purgate-type: clean X-purgate-size: 5769 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1783435512078158500 Content-Type: text/plain; charset="utf-8" 64-bit ELF notes generally (sadly with exceptions) are padded to 8-byte boundaries, whereas 32-bit ELF notes are padded to 4-byte ones. The excess padding makes it impossible for tools like objdump to properly deal with these notes. Signed-off-by: Jan Beulich --- Is there a need to further generalize or tighten anything? --- v2: New. --- a/xen/arch/x86/boot/mkelf32.c +++ b/xen/arch/x86/boot/mkelf32.c @@ -8,6 +8,7 @@ */ =20 #include +#include #include #include #include @@ -256,6 +257,8 @@ int main(int argc, char **argv) char buffer[1024] =3D {}; int bytes, todo, i =3D 1; int num_phdrs =3D 1; + void *notes32 =3D NULL; + unsigned int note_sz32 =3D 0; =20 Elf32_Ehdr in32_ehdr; =20 @@ -359,8 +362,6 @@ int main(int argc, char **argv) do_read(infd, &in64_phdr, sizeof(in64_phdr)); endianadjust_phdr64(&in64_phdr); =20 - (void)lseek(infd, offset, SEEK_SET); - note_sz =3D in64_phdr.p_memsz; note_base =3D in64_phdr.p_vaddr - note_base; =20 @@ -373,6 +374,84 @@ int main(int argc, char **argv) offset, offset + dat_siz); return 1; } + + note_sz32 =3D note_sz; + + /* Convert 8-byte padded notes to 4-byte padded ones. */ + if ( in64_phdr.p_align =3D=3D 8 ) + { + Elf64_Note *notes64 =3D malloc(note_sz); + + notes32 =3D malloc(note_sz); + if ( notes64 && notes32 ) + { + unsigned int left =3D note_sz; + void *ptr32 =3D notes32; + + lseek(infd, in64_phdr.p_offset, SEEK_SET); + do_read(infd, notes64, note_sz); + + for ( bool start =3D true; left > sizeof(*notes64); ) + { + unsigned int size; + + if ( start && !notes64->namesz ) + { + /* Padding. */ + notes64 =3D (void *)(¬es64->namesz + 1); + left -=3D sizeof(notes64->namesz); + start =3D false; + continue; + } + + /* + * The note descriptor may start at either the next 4-= or + * 8-byte boundary. See e.g. + * https://sourceware.org/bugzilla/show_bug.cgi?id=3D3= 3259. + * For the notes we have actively in use at the time of + * writing, rounding to the next 4-byte boundary is en= ough + * (and in fact is already excessive: "GNU" and "Xen" = as + * note names both satisfy the 8-byte alignment without + * extra effort). + * + * The similar ->descsz aspect is covered by the check + * above. + */ + size =3D ROUNDUP(sizeof(*notes64) + notes64->namesz, 4= ) + + ROUNDUP(notes64->descsz, 4); + if ( left < size ) + { + fprintf(stderr, + "Warning: ELF note overrunning segment (%u= bytes left, %u bytes claimed)\n", + left, size); + left =3D 0; + break; + } + + /* Leverage Elf{32,64}_Note actually having same layou= t. */ + memcpy(ptr32, notes64, size); + + notes64 =3D (void *)notes64 + size; + ptr32 +=3D size; + left -=3D size; + start =3D true; + } + + if ( left ) + fprintf(stderr, "Warning: %u trailing bytes of ELF not= es\n", + left); + + note_sz32 =3D ptr32 - notes32; + memset(ptr32, 0, note_sz - note_sz32); + } + else + fprintf(stderr, + "Warning: Not enough memory to re-write %"PRIu32" = bytes of ELF notes\n", + note_sz); + } + + (void)lseek(infd, offset, SEEK_SET); + /* Gets us the absolute offset within the .text section. */ offset =3D in64_phdr.p_offset - offset; } @@ -405,8 +484,8 @@ int main(int argc, char **argv) /* Fill out the PT_NOTE program header. */ note_phdr.p_vaddr =3D note_base; note_phdr.p_paddr =3D note_base; - note_phdr.p_filesz =3D note_sz; - note_phdr.p_memsz =3D note_sz; + note_phdr.p_filesz =3D note_sz32; + note_phdr.p_memsz =3D note_sz32; note_phdr.p_offset =3D RAW_OFFSET + offset; =20 /* Tack on the .note\0 */ @@ -415,7 +494,7 @@ int main(int argc, char **argv) out_shdr[2].sh_offset +=3D sizeof(out_shdr_note); =20 /* Fill out the .note section. */ - out_shdr_note.sh_size =3D note_sz; + out_shdr_note.sh_size =3D note_sz32; out_shdr_note.sh_addr =3D note_base; out_shdr_note.sh_offset =3D RAW_OFFSET + offset; } @@ -483,6 +562,17 @@ int main(int argc, char **argv) do_write(outfd, buffer, 4 - (bytes & 3)); } =20 + if ( notes32 ) + { + lseek(outfd, note_phdr.p_offset, SEEK_SET); + + /* + * While we use note_sz32 above, overwrite the full original conte= nts, + * to not leave confusing rubbish there. + */ + do_write(outfd, notes32, note_sz); + } + close(infd); close(outfd);