From: Tom Rini <trini@konsulko.com>
To: Rasmus Villemoes <ravi@prevas.dk>, Simon Glass <sjg@chromium.org>
Cc: u-boot@lists.denx.de, Simon Glass <simon.glass@canonical.com>
Subject: Re: [PATCH 1/2] linker_lists: Fix end-marker alignment to prevent padding
Date: Mon, 23 Mar 2026 09:29:10 -0600 [thread overview]
Message-ID: <20260323152910.GI502704@bill-the-cat> (raw)
In-Reply-To: <87jyv2oon5.fsf@prevas.dk>
[-- Attachment #1: Type: text/plain, Size: 2991 bytes --]
On Mon, Mar 23, 2026 at 10:56:14AM +0100, Rasmus Villemoes wrote:
> On Sat, Mar 21 2026, Simon Glass <sjg@chromium.org> wrote:
>
> > From: Simon Glass <simon.glass@canonical.com>
> >
> > 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 optimises pointer subtraction (end - start) using
> > magic-number multiplication for division. This optimisation 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.
>
> So I'm wondering why that is guaranteed. I mean, the linker is placing
> these sections one after another in order
>
>
> 2_foo_2_last_foo size sizeof(struct foo), alignment max(4, alignof(struct foo))
> 2_foo_3 size 0, alignment 4 (1 with your patch)
> 2_bar_1 size 0, alignment CONFIG_LINKER_LIST_ALIGN
> 2_bar_2_first_bar size sizeof(struct bar), alignment max(4, alignof(struct bar))
>
> So clearly the end of last_foo does have 4-byte alignment, yet it is
> observed that the linker sometimes makes 2_foo_3's address coincide with
> 2_bar_1's address?
>
> What I don't understand is that it seems that the linker could place the
> zero-size object 2_foo_3 at any 4-byte aligned address between the end
> of 2_foo_2_last_foo and 2_bar_1. And the same seems to be true when one
> changes it to have even smaller alignment requirement.
>
> So why does an align(1) stop the linker from placing that 0-size section
> at the same address as 2_bar_1, or even force it (as we need) to put it
> at the first possible address, i.e. immediately after last_foo? Unless
> alignment 1 is somehow special-cased to mean "place as early as
> possible", I can't see how this should provide any better guarantees
> than what we already have.
>
> So I don't oppose the patch at all, but I'd really like to understand
> how it actually works.
And as was recently demonstrated in getting device trees to be 8 byte
aligned in linker scripts, you need to be making changes in the linker
script to ensure output alignment matches what you expect and also it's
not always obvious what actually will happen in all cases (when you
haven't been dealing with the nuances of linker scripts for days on
end).
--
Tom
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
next prev parent reply other threads:[~2026-03-23 15:29 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-21 13:46 [PATCH 0/2] A few linker-list fixes Simon Glass
2026-03-21 13:46 ` [PATCH 1/2] linker_lists: Fix end-marker alignment to prevent padding Simon Glass
2026-03-23 9:56 ` Rasmus Villemoes
2026-03-23 15:29 ` Tom Rini [this message]
2026-03-23 16:17 ` Simon Glass
2026-04-13 21:07 ` Rasmus Villemoes
2026-04-12 1:36 ` Simon Glass
2026-04-13 17:24 ` Tom Rini
2026-03-21 13:46 ` [PATCH 2/2] dm: Fix linker list alignment for ll_entry_get() Simon Glass
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260323152910.GI502704@bill-the-cat \
--to=trini@konsulko.com \
--cc=ravi@prevas.dk \
--cc=simon.glass@canonical.com \
--cc=sjg@chromium.org \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox