From patchwork Sun Dec 14 17:54:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 901 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765734933; bh=2bCaL+JLMdntS+CFiXctTbJLkrbCEA8Jxk3KaNcA2U8=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=Dl6v4LQlvyF9RuOGgEbCGgoUuwoQXO1oCVZyxjR5U1Vymm1ns+MTgi9rYEwus9T8y kMAPuy2DviNCHDVTCwoeNdJr6sxYRAKs0hHtSPXfgSsTMAaMw8ots0cs/f7anWkjGj 7VGTgvVnhwMt+4asHcLRB3EDlTnqlcwCc9iEMzOUjp3tSTY89VkIAFa1r+mA43ophK 1GcbX00+ED/rIsJ2HQ9dd1FLEm6nNyIhcJa7/49jc+kN3DEiub38JFrzNCVrqQud5h eU04lrw3dPDcsHlxGafWTeFzGCsfbAJYhyV7/QW6V0Q05OZY0+Z0k2RAN17E3KHXrT z179nJ/bqrmbQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id C6C3668AEA for ; Sun, 14 Dec 2025 10:55:33 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id 2HnhTntHUg3u for ; Sun, 14 Dec 2025 10:55:33 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765734933; bh=2bCaL+JLMdntS+CFiXctTbJLkrbCEA8Jxk3KaNcA2U8=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=Dl6v4LQlvyF9RuOGgEbCGgoUuwoQXO1oCVZyxjR5U1Vymm1ns+MTgi9rYEwus9T8y kMAPuy2DviNCHDVTCwoeNdJr6sxYRAKs0hHtSPXfgSsTMAaMw8ots0cs/f7anWkjGj 7VGTgvVnhwMt+4asHcLRB3EDlTnqlcwCc9iEMzOUjp3tSTY89VkIAFa1r+mA43ophK 1GcbX00+ED/rIsJ2HQ9dd1FLEm6nNyIhcJa7/49jc+kN3DEiub38JFrzNCVrqQud5h eU04lrw3dPDcsHlxGafWTeFzGCsfbAJYhyV7/QW6V0Q05OZY0+Z0k2RAN17E3KHXrT z179nJ/bqrmbQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id B13D168ADF for ; Sun, 14 Dec 2025 10:55:33 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765734932; bh=/SISnDxF7nIGmBxwfViwjoMraCV88oJSn16j1wekX7U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Pd5rj3QM1cIbWohWn58QF6a/N4czRX5DybKX3vV4sAeYll/Ji4wWdY1aeFq82hpRp CH9TZXjosMCokXcDeNpeoSX7Dh0XPklNZFZP2af6AQ2TpjKpGKXuZHxNaNw6Q2ajNq BDNncEepwRHgKZaEt5tCQ3qiFQ7dhgoLQoCMVNt5+6Tubl4QFxyquZiTpPQp63OCEu Z0+I1ybWnflE4G+oFJh6caTmOf/+tH5NYMh3J+Md93WJ6uMxSOJy9OBgfk0PpNWc1n eVOC5QnZZ0e0h0X4PawTw+FIaD0HJpbty+jC/dWSnhjd3IiJu8dWDUIyXbcC7MC8Hj PteR0TbfH76kg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id A705968AC0; Sun, 14 Dec 2025 10:55:32 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id I3QyaYSEhZbF; Sun, 14 Dec 2025 10:55:32 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765734927; bh=V3NplywnDqo7EaJFHaX0uzj8DpTMRPlyW003+0TyzT0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DeJGvs2qjVnVQj0P4ex0+DRFP4nVJK/41EILPaTck/bDRxPE4/QaTHgEGu0g9t9j8 MPd6GgAs018q8xOUnQw3iPAor8i8AoPCgEGqG5EDxpOUTobHYSiGQ0LdVIfuzqTf2J dWPeix8KPpbaeqzOsU9/OHHHM+rI8rplqzklR9yyyAfMF89Z5scxkhcmZmJE64HdGt oHSKiJ3/JXFuqfUVvONhOks7DuDasKZM2RosvD1qUPBJvqXI36eCcz5c5TGvQnQIjO Idnvhw46SK6ydp1MOracWjfQMbQG451K6I5C8EZGrlHYCTDpzU0zZDY77+meCDJRDn g4OcS5XmP7iEg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 2559F68A84; Sun, 14 Dec 2025 10:55:27 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Sun, 14 Dec 2025 10:54:30 -0700 Message-ID: <20251214175449.3799539-9-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251214175449.3799539-1-sjg@u-boot.org> References: <20251214175449.3799539-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: 2RZZSAIJVWJQPUKBUV3EUKFVLH64E2E4 X-Message-ID-Hash: 2RZZSAIJVWJQPUKBUV3EUKFVLH64E2E4 X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 08/21] linker_lists: Fix end-marker alignment to prevent padding List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Change the alignment of end markers in ll_entry_end() and ll_end_decl() from __aligned(4) and __aligned(CONFIG_LINKER_LIST_ALIGN) respectively to __aligned(1). The linker places zero-size end markers at aligned boundaries based on what follows them. When the next list's start marker has a high alignment requirement (e.g., 32 bytes), padding gets inserted before the end marker. This causes the byte span (end - start) to not be an exact multiple of the struct size. The compiler optimizes pointer subtraction (end - start) using magic-number multiplication for division. This optimization only produces correct results when the byte span is an exact multiple of the struct size. With padding, the result is garbage (e.g., -858993444 instead of 15). By using __aligned(1), the end marker is placed immediately after the last entry with no padding, ensuring (end - start) equals exactly n * sizeof where n is the number of entries. This makes ll_entry_count() and direct pointer arithmetic work correctly. Fixes: 0b2fa98aa5e5 ("linker_lists: Fix alignment issue") Co-developed-by: Claude Signed-off-by: Simon Glass --- include/linker_lists.h | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/include/linker_lists.h b/include/linker_lists.h index 0f4a2d686e2..6a018f175ca 100644 --- a/include/linker_lists.h +++ b/include/linker_lists.h @@ -145,6 +145,20 @@ * Since this macro defines an array end symbol, its leftmost index * must be 2 and its rightmost index must be 3. * + * The end symbol uses __aligned(1) to ensure it is placed immediately after + * the last entry without any padding. This is critical for ll_entry_count() + * to work correctly. + * + * If the end marker had a higher alignment (e.g., 4 or 32 bytes), the linker + * might insert padding between the last entry and the end marker to satisfy + * alignment requirements of the following section. This would cause pointer + * subtraction (end - start) to produce incorrect results because the compiler + * optimizes pointer division using magic-number multiplication, which only + * works correctly when the byte span is an exact multiple of the struct size. + * + * With __aligned(1), the end marker is placed at exactly (start + n * sizeof) + * where n is the number of entries, ensuring correct pointer arithmetic. + * * Example: * * :: @@ -153,7 +167,7 @@ */ #define ll_entry_end(_type, _list) \ ({ \ - static char end[0] __aligned(4) __attribute__((unused)) \ + static char end[0] __aligned(1) __attribute__((unused)) \ __section("__u_boot_list_2_"#_list"_3"); \ _type * tmp = (_type *)&end; \ asm("":"+r"(tmp)); \ @@ -239,8 +253,12 @@ static _type _sym[0] __aligned(CONFIG_LINKER_LIST_ALIGN) \ __maybe_unused __section("__u_boot_list_2_" #_list "_1") +/* + * ll_end_decl uses __aligned(1) to avoid padding before the end marker. + * See the comment for ll_entry_end() for a full explanation. + */ #define ll_end_decl(_sym, _type, _list) \ - static _type _sym[0] __aligned(CONFIG_LINKER_LIST_ALIGN) \ + static _type _sym[0] __aligned(1) \ __maybe_unused __section("__u_boot_list_2_" #_list "_3") /**