Skip to content

Conversation

@LucasSte
Copy link
Collaborator

@LucasSte LucasSte commented Jan 9, 2026

Problem

When linking LLD calls finalizeSections, where it assigns the size of the program header table based on the number of program headers:

ctx.out.programHeaders->size =
sizeof(Elf_Phdr) * ctx.mainPart->phdrs.size();

But after that, it removes empty PT_LOAD headers:

// Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
// 0 sized region. This has to be done late since only after assignAddresses
// we know the size of the sections.
for (Partition &part : ctx.partitions)
removeEmptyPTLoad(ctx, part.phdrs);

So effectively the number of headers changes, but the size of the table does not.

Solution

Recalculate the size of the program header table after removing empty headers.

Copy link
Collaborator

@nagisa nagisa left a comment

Choose a reason for hiding this comment

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

This makes sense to me, although it probably does not hurt anything if the section is allocated a little too large.

@LucasSte
Copy link
Collaborator Author

This makes sense to me, although it probably does not hurt anything if the section is allocated a little too large.

The sbpf ELF loader requires the first section to be adjacent to the program header table.

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