Claude/upgrade ape c library mm z gd#145
Conversation
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
There was a problem hiding this comment.
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.
| ph->filesz = textfoff + textsize; /* header area + text */ | ||
| ph->memsz = textfoff + textsize; |
There was a problem hiding this comment.
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;
No description provided.