Skip to content

feat: support using an expression when setting the location counter#2005

Open
vishruth-thimmaiah wants to merge 6 commits into
wild-linker:mainfrom
vishruth-thimmaiah:expr_loc
Open

feat: support using an expression when setting the location counter#2005
vishruth-thimmaiah wants to merge 6 commits into
wild-linker:mainfrom
vishruth-thimmaiah:expr_loc

Conversation

@vishruth-thimmaiah
Copy link
Copy Markdown
Member

This PR adds support for using expressions when setting the current location within SECTIONS.

This PR doesn't add support for using symbols when setting the location pointer.

Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
{
. = 0x600000;
. = 0x300000 * 2;
start_of_sections = .;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Setting the location before defining any sections sets the location for the first section in the section table, not the first section in the linker script. Hence the need for this change, as the first section might not be .text.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess possibly another way of looking at what you said is that when the linker reads the linker script, when it encounters the first section, it emits certain linker-generated sections before that first section. This alternative way of looking at it might suggest a slightly different implementation. e.g. perhaps when reading the linker script, those implicit sections should be inserted, almost as if they were in the file. Not sure if that would have any implications or how it would compare in terms of complexity. Don't feel that you need to change anything in this regard... just something to think about.

Also related... I assume the location counter can be set either before the first section or between sections. Are both tested?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Also related... I assume the location counter can be set either before the first section or between sections. Are both tested?

Yes, this is already tested in line 10.

@vishruth-thimmaiah vishruth-thimmaiah marked this pull request as draft May 31, 2026 13:23
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
@vishruth-thimmaiah vishruth-thimmaiah changed the title feat: support using an expression when setting a location with . feat: support using an expression when setting the location counter Jun 1, 2026
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
@vishruth-thimmaiah vishruth-thimmaiah marked this pull request as ready for review June 1, 2026 04:08
{
. = 0x600000;
. = 0x300000 * 2;
start_of_sections = .;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess possibly another way of looking at what you said is that when the linker reads the linker script, when it encounters the first section, it emits certain linker-generated sections before that first section. This alternative way of looking at it might suggest a slightly different implementation. e.g. perhaps when reading the linker script, those implicit sections should be inserted, almost as if they were in the file. Not sure if that would have any implications or how it would compare in terms of complexity. Don't feel that you need to change anything in this regard... just something to think about.

Also related... I assume the location counter can be set either before the first section or between sections. Are both tested?

Comment on lines +300 to +303
if current_section_id.is_none() {
output_sections.set_base_address(new_location.address.clone());
continue;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If I comment out this if-block, the tests still seem to still all pass. Is it possible to make the test exercise and verify the behaviour here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I suppose we could verify .text is not at the base address, since it is not the first section outputed.

Comment thread libwild/src/layout.rs
&symbol_db,
);

let memory_regions: Vec<crate::linker_script::MemoryRegion<'_>> = symbol_db
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If I replace this Vec with and empty one, the tests still pass. Is it practical to add a test that catches it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We don't fully support the syntax for defining MEMORY regions that ld requires for an executable, so writing a test for this might not be possible here. memory_regions here is used for evaluating ORIGIN and LENGTH functions in an expression.

Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
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.

2 participants