Skip to content

feat: generate a .symtab_shndx section when shnum > 65k#1783

Merged
davidlattimore merged 10 commits into
wild-linker:mainfrom
vishruth-thimmaiah:symtab_shndx
Apr 5, 2026
Merged

feat: generate a .symtab_shndx section when shnum > 65k#1783
davidlattimore merged 10 commits into
wild-linker:mainfrom
vishruth-thimmaiah:symtab_shndx

Conversation

@vishruth-thimmaiah

Copy link
Copy Markdown
Member

This PR adds support for generating a .symtab_shndx when the number of sections crosses SHN_LORESERVE. Additionaly we also set the header's shnum to 0 and shstrndx to SHN_XINDEX, using the first section's sh_size and sh_link fields respectively instead.

@vishruth-thimmaiah vishruth-thimmaiah marked this pull request as draft March 31, 2026 20:01

@davidlattimore davidlattimore left a comment

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.

Thanks for working on this. I haven't properly reviewed this yet, since I see there's still some work to do getting tests to pass. If you need any guidance on the tests feel free to ask.

Comment thread libwild/src/elf.rs Outdated
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
@vishruth-thimmaiah vishruth-thimmaiah marked this pull request as ready for review April 4, 2026 20:00
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
Comment thread libwild/src/layout.rs Outdated
total_sizes: &mut OutputSectionPartMap<u64>,
num_sections: u32,
) {
if num_sections < u32::from(object::elf::SHN_LORESERVE) {

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.

We want to avoid references to object::elf from platform-generic code. Probably the whole shndx mechanism is elf-specific, so maybe this function should be moved to elf.rs, most likely by adding a new method to the Platform trait.

Comment thread libwild/src/layout.rs Outdated

let num_sections = keep_sections.values_iter().filter(|p| **p).count();
let mut num_sections = keep_sections.values_iter().filter(|p| **p).count();
if num_sections >= object::elf::SHN_LORESERVE as usize {

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.

Can this bit of elf-specific code be moved to elf.rs by adding a new method to the Platform trait?

Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
@davidlattimore davidlattimore merged commit e6e8f98 into wild-linker:main Apr 5, 2026
24 checks passed
marxin pushed a commit to marxin/wild that referenced this pull request Apr 5, 2026
Comment thread libwild/src/elf_writer.rs
format!("Expected .symtab_shndx section when writing symbol {} with shndx set to SHN_XINDEX.", String::from_utf8_lossy(name))
})?;
object::elf::SHN_XINDEX
};

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.

I'm not sure that this logic is correct (and I'm not sure how to go about testing the u16 section overflow to verify). The problem is that we're using a u32 to encode two different kinds of values: a section index (which can logically be any value in the u32 range), and reserved index values such as SHN_ABS which have values in the 0xfff0-0xffff range, and these value ranges overlap. So if we happened to have over 0xfff0 sections, we would wrongly encode one of them as SHN_ABS instead of SHN_XINDEX.

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.

Thanks for pointing that out. You're right, both that this isn't right as it currently stands and that testing it is likely to be hard. From a testing perspective, we probably need some way to define tests that compile generated code, which we don't currently support. That way, we can generate an input file with a sufficiently large number of sections. I guess the other problem is that the sections aren't especially used at runtime, so there's not much we can do at runtime to check that they're right. Probably we need to rely on test assertions to verify that it's correct.

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 can probably use macros in c to generate >65k sections. To verify the symbols' section indexes, we can probably use the ExpectSym? I can take a look at this and submit a PR.

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.

That sounds like it would work. Thanks!

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.

3 participants