Skip to content

Fix SUBALIGN linker script feature#752

Open
parth-07 wants to merge 1 commit intoqualcomm:mainfrom
parth-07:SubAlign2
Open

Fix SUBALIGN linker script feature#752
parth-07 wants to merge 1 commit intoqualcomm:mainfrom
parth-07:SubAlign2

Conversation

@parth-07
Copy link
Contributor

This commit fixes the SUBALIGN linker script feature. SUBALIGN feature is used to modify the alignment of all input sections present in an output section. Until now, we were only changing the alignment of the first fragment of each rule of the output sections with SUBALIGN specified. This commit fixes the SUBALIGN behavior.

SUBALIGN affects the alignment of the first fragment as is seen by the linker when traversing the output sections for the input sections that may contain more than 1 fragment such as .eh_frame, PLT / GOT sections, and .comment section.

Resolves #343

ELFSection *InSect = R->getSection();
for (Fragment *F : InSect->getFragmentList()) {
ELFSection *owningSect = F->getOwningSection();
if (owningSect && seen.insert(owningSect).second) {
Copy link
Contributor

@quic-areg quic-areg Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

early return if either of these are false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no statement after this if-command, and thus, I am not sure if early return would be beneficial here.

@parth-07 parth-07 force-pushed the SubAlign2 branch 2 times, most recently from f2715a1 to 23fed03 Compare January 29, 2026 04:25
@parth-07 parth-07 requested a review from quic-areg January 30, 2026 16:21
Copy link
Contributor

@quic-seaswara quic-seaswara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed a bit thorougly.

if (owningSect && seen.insert(owningSect).second) {
// Warn if SUBALIGN is reducing the section alignment
if (ThisConfig.showLinkerScriptWarnings() && F->alignment() > subAlign) {
ThisConfig.raise(Diag::warn_subalign_less_than_section_alignment)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be an error ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, as per GNU ld docs (https://sourceware.org/binutils/docs/ld/Forced-Input-Alignment.html), SUBALIGN is allowed to reduce the input section alignment.

for (Fragment *F : inSect->getFragmentList()) {
ELFSection *owningSect = F->getOwningSection();
if (owningSect->getKind() == LDFileFormat::Kind::OutputSectData) {
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome!

Should we do this for internal sections ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need to take this as case-by-case basis. Most internal sections do indeed represent a section and thus SUBALIGN should affect them. OutputSectData is an exception because it is not a section at all as per GNU ld docs.

@parth-07 parth-07 force-pushed the SubAlign2 branch 2 times, most recently from a303120 to a8ac9c6 Compare February 11, 2026 17:23
@parth-07 parth-07 force-pushed the SubAlign2 branch 3 times, most recently from a8ac9c6 to 6dbdf27 Compare February 11, 2026 18:08
@parth-07
Copy link
Contributor Author

Ping @quic-seaswara

This commit fixes the SUBALIGN linker script feature. SUBALIGN feature
is used to modify the alignment of all input sections present in an
output section. Until now, we were only changing the alignment of the
first fragment of each rule of the output sections with SUBALIGN
specified. This commit fixes the SUBALIGN behavior.

SUBALIGN affects the alignment of the first fragment as is seen by the linker
when traversing the output sections for the input sections
that may contain more than 1 fragment such as .eh_frame, PLT / GOT sections,
and .comment section.

Resolves qualcomm#343

Signed-off-by: Parth Arora <partaror@qti.qualcomm.com>
<< utility::toHex(subAlign) << utility::toHex(F->alignment())
<< owningSect->getLocation(0, ThisConfig.options());
}
F->setAlignment(subAlign);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do

F->setOwningSection->setAlignment(subAlign) right ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Fragment::paddingSize() is the key function that determines a fragment's padding requirements and it uses Fragment's alignment instead of the owning input section alignment. In the current design, it seems that we prefer fragment's alignment over the input section's alignment in the layout phase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

linkerscript SUBALIGN applies to each input section

4 participants