close
Skip to content

[memory-region] Error when section VMA does not fit in region#1102

Open
deepakshirkem wants to merge 1 commit intoqualcomm:mainfrom
deepakshirkem:fix/memory-region-vma-check-327
Open

[memory-region] Error when section VMA does not fit in region#1102
deepakshirkem wants to merge 1 commit intoqualcomm:mainfrom
deepakshirkem:fix/memory-region-vma-check-327

Conversation

@deepakshirkem
Copy link
Copy Markdown
Contributor

Problem

Fixes #327

ELD was silently linking when a section's VMA was placedoutside its assigned memory region bounds:

MEMORY {
RAM : ORIGIN = 0x1000, LENGTH = 0x1000
}
SECTIONS {
.foo (0x2500) : { *(.text.foo) } >RAM  ← outside RAM!
}

Fix

Added check in postLayout() to verify each section's VMA fits within its assigned memory region bounds, matching LLD
behavior exactly:

Error: section '.foo' will not fit in region 'RAM':overflowed by 1288 bytes

Testing

Added test MemoryRegionVMACheck that verifies ELD emits a clear error when a section VMA does not fit in its
assigned memory region.

cc @parth-07 @quic-seaswara

#START_TEST
RUN: %clang %clangg0opts -o %t1.1.o %p/Inputs/1.c -c -ffunction-sections
RUN: %not %link %linkopts -o %t1.1.out %t1.1.o -T %p/Inputs/script.t 2>&1 | %filecheck %s
#CHECK: section '.foo' will not fit in region 'RAM'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There is no error with bfd linker.

Comment thread lib/Target/GNULDBackend.cpp Outdated
<< sec->name() << region.getName() << (secEnd - regionEnd);
}
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With bfd, any explicit address specified, is not assigned to the memory region.

Probably the user needs to be notified, that this output section is not part of the memory region as a note, and also a linker script warning using -Wlinker-script that this section is outside the memory region bounds.

In any case these might be needed to be moved to ScriptMemoryRegion to flag.

Copy link
Copy Markdown
Contributor Author

@deepakshirkem deepakshirkem Apr 27, 2026

Choose a reason for hiding this comment

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

Hi @quic-seaswara, My initial thought was also to add this check in ScriptMemoryRegion or verifyMemoryRegions(), but during debugging I found that final section addresses are not yet assigned at that point (they are still 0x0).That's why I moved the check to postLayout() where addresses are fully assigned. That's why I moved the check to postLayout() where addresses are fully assigned.

I will address suggested comments those are making more sense.

Thank You ::((

@deepakshirkem deepakshirkem force-pushed the fix/memory-region-vma-check-327 branch from 1fd139a to c171680 Compare April 27, 2026 20:55
ELD was silently linking when a section's VMA was placed
outside its assigned memory region bounds.

Added warning in ScriptMemoryRegion::verifyMemoryUsage()
under -Wlinker-script flag, matching BFD behavior.
Fixes qualcomm#327

Signed-off-by: deepakshirkem <deepakshirke509@gmail.com>
@deepakshirkem deepakshirkem force-pushed the fix/memory-region-vma-check-327 branch from c171680 to 87bc13a Compare April 27, 2026 21:01
@deepakshirkem
Copy link
Copy Markdown
Contributor Author

Hi @quic-seaswara,
Updated the fix as per your feedback.Moved check to ScriptMemoryRegion::verifyMemoryUsage() as you suggested Changed from error to warning under -Wlinker-script matching BFD behavior Warning runs only once after layout is complete. Please review when you get a chance.

Thank you ::((

@deepakshirkem
Copy link
Copy Markdown
Contributor Author

Hi @quic-seaswara / @quic-areg, Please take another look at these changes when you get a chance?

uint64_t vma = out->prolog().vma().result();
if (!containsVMA(vma))
Config.raise(Diag::warn_section_not_in_region)
<< out->name() << getName() << utility::toHex(vma);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Need the linker script file also in the warning message.

if (&out->epilog().region() != this)
continue;
if (!out->prolog().hasVMA())
continue;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You might want to move this condition to line 51 ?

for (auto *out : SectMap) {
if (!out->epilog().hasRegion())
continue;
if (&out->epilog().region() != this)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Instead of passing the sectionMap can verifyMemoryRegions run on all output sections that have memory region and call verifyMemoryUsage ? The code can be greatly simplified I think.

Note : the original loop will not change,

#START_TEST
RUN: %clang %clangg0opts -o %t1.1.o %p/Inputs/1.c -c -ffunction-sections
RUN: %link %linkopts -o %t1.1.out %t1.1.o -T %p/Inputs/script.t -Wlinker-script 2>&1 | %filecheck %s
RUN: %link %linkopts -o %t1.2.out %t1.1.o -T %p/Inputs/script.t 2>&1 | %filecheck %s --check-prefix=NOWARN --allow-empty
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why --allow-empty ?

#---MemoryRegionVMACheck.test----------------------- Executable -----------------#
#BEGIN_COMMENT
# Test that ELD emits a warning when a section VMA does not fit
# in its assigned memory region, matching BFD behavior.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does bfd emit a warning by default ?

@deepakshirkem
Copy link
Copy Markdown
Contributor Author

Hi @quic-seaswara, I apologize for the misunderstanding. I have not addressed these comments carefully enough.

There is no error with bfd linker.

With bfd, any explicit address specified, is not assigned to the memory region.

Probably the user needs to be notified, that this output section is not part of the memory region as a note, and also a linker script warning using -Wlinker-script that this section is outside the memory region bounds.

In any case these might be needed to be moved to ScriptMemoryRegion to flag.

I have re-verified the behavior both BFD and LLD error out for this test case. If you give me the go-ahead, I will implement this as an error to match BFD and LLD behavior.

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.

eld does not give error if the specified vma/lma does not fit into the specified memory regions

2 participants