Skip to content

6l: fix text PT_LOAD to use Plan9 ELF convention (off=HEADR, vaddr=IN…#146

Merged
staalmannen merged 1 commit into
mainfrom
claude/upgrade-ape-c-library-mmZGd
May 22, 2026
Merged

6l: fix text PT_LOAD to use Plan9 ELF convention (off=HEADR, vaddr=IN…#146
staalmannen merged 1 commit into
mainfrom
claude/upgrade-ape-c-library-mmZGd

Conversation

@staalmannen

Copy link
Copy Markdown
Owner

…ITTEXT)

Linux-style text PT_LOAD (off=0, vaddr=INITTEXT-HEADR=0x200000) causes Plan9's kernel ELF loader to map the binary incorrectly, producing "general protection violation" on every ELF binary.

Plan9 convention (matching original 6l case 5 and 8l): off=HEADR=0xC00, vaddr=INITTEXT=0x200C00, filesz=textsize, align=INITRND=0x200000.

The alignment constraint is satisfied: 0xC00 ≡ 0x200C00 (mod 0x200000).

https://claude.ai/code/session_01WGAwvvTwDg2yknFkmZ3qzs

…ITTEXT)

Linux-style text PT_LOAD (off=0, vaddr=INITTEXT-HEADR=0x200000) causes Plan9's
kernel ELF loader to map the binary incorrectly, producing "general protection
violation" on every ELF binary.

Plan9 convention (matching original 6l case 5 and 8l): off=HEADR=0xC00,
vaddr=INITTEXT=0x200C00, filesz=textsize, align=INITRND=0x200000.

The alignment constraint is satisfied: 0xC00 ≡ 0x200C00 (mod 0x200000).

https://claude.ai/code/session_01WGAwvvTwDg2yknFkmZ3qzs
@staalmannen staalmannen merged commit b18434e into main May 22, 2026
1 check passed

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request modifies the ELF program header generation in 6l to align with Plan 9 conventions by setting the file offset to the header size and the virtual address to INITTEXT. A review comment points out that the new alignment logic is fragile because it assumes the header size matches the sub-page offset of INITTEXT; it suggests adding a check to ensure the ELF alignment requirement is maintained to prevent the generation of malformed binaries if the header size changes.

Comment thread sys/src/cmd/6l/asm.c
Comment on lines +112 to +117
ph->off = textfoff; /* HEADR=0xC00; Plan9 convention: off=HEADR not 0 */
ph->vaddr = INITTEXT; /* 0x200C00; alignment ok: 0xC00 ≡ 0x200C00 (mod 0x200000) */
ph->paddr = INITTEXT;
ph->filesz = textsize;
ph->memsz = textsize;
ph->align = INITRND; /* 0x200000 */

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The alignment constraint vaddr % align == off % align now depends on textfoff matching the sub-page offset of INITTEXT (e.g., 0xC00). While this works for the current fixed header size, it is fragile. If textfoff changes (e.g., due to additional program headers), the ELF alignment requirement will be violated for the 2MB INITRND. Consider adding a check to ensure textfoff % INITRND == INITTEXT % INITRND to prevent generating malformed binaries if the header size ever deviates.

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