Skip to content

Claude/upgrade ape c library mm z gd#145

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

Claude/upgrade ape c library mm z gd#145
staalmannen merged 2 commits into
mainfrom
claude/upgrade-ape-c-library-mmZGd

Conversation

@staalmannen

Copy link
Copy Markdown
Owner

No description provided.

claude added 2 commits May 22, 2026 08:47
Plan9's kernel (and standard ELF loaders) require the first
PT_LOAD to cover the ELF header from file offset 0. Previously
the segment started at p_offset=HEADR (0xC00), leaving the ELF
header unmapped, causing the kernel to reject the binary.

New layout: p_offset=0, p_vaddr=0x200000, p_filesz=HEADR+textsize.
The ELF header/phdr area (0..HEADR-1) maps to 0x200000..0x200BFF
as read+exec padding; text code is at vaddr 0x200C00 as before.
Alignment: 0 ≡ 0x200000 (mod 0x1000) ✓

https://claude.ai/code/session_01WGAwvvTwDg2yknFkmZ3qzs
When DWARF is enabled, dwarfaddelfheaders() was called before the NULL
section header was created, giving DWARF sections indices 0-3 and the
NULL shdr a non-zero index. The ELF spec requires shdr[0] to be a null
entry. Move newElfShdr(0) to right after elfinit() so it is always the
first section header.

https://claude.ai/code/session_01WGAwvvTwDg2yknFkmZ3qzs
@staalmannen staalmannen merged commit 1d30151 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 generation logic in the 6l linker to include the ELF header within the first loadable segment by adjusting the program header offsets and addresses. Additionally, it moves the creation of the NULL section header to ensure it is correctly positioned at index 0. Review feedback highlighted a potential 32-bit integer overflow when calculating the combined size of the header and text; it is recommended to cast the operands to vlong to ensure 64-bit arithmetic is used.

Comment thread sys/src/cmd/6l/asm.c
Comment on lines +115 to +116
ph->filesz = textfoff + textsize; /* header area + text */
ph->memsz = textfoff + textsize;

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 calculation textfoff + textsize is performed using long arithmetic, which is 32-bit on Plan 9. If the combined size of the header and text exceeds 2GB, this will overflow before being assigned to the 64-bit vlong fields ph->filesz and ph->memsz. Casting textfoff to vlong ensures the addition is performed in 64-bit arithmetic and avoids potential issues with sign extension if the result becomes negative in 32-bit space.

	ph->filesz = (vlong)textfoff + textsize;      /* header area + text             */
	ph->memsz  = (vlong)textfoff + textsize;

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