6l: use ELF32 (ELFCLASS32) format for Plan9 ELF output#147
Conversation
Plan9's kernel exec only accepts ELF32 format, even on amd64. The original 6l -H5 case always wrote ELFCLASS32 (cput(1) in case 5). Our new asmb_elf() was generating ELFCLASS64 which Plan9's kernel rejects with "exec header invalid". Fix: add elfforce32() to elf.c, call it in asmb_elf() after elfinit(). This switches elf64=0 and resets all header/phdr/shdr size fields to ELF32 values. All Plan9 userspace addresses fit comfortably in 32 bits (INITTEXT=0x200C00). The EM_X86_64 machine type in an ELF32 header (ELFCLASS32 + machine=62) is the exact format the original 6l -H5 used and what Plan9/9front expects. https://claude.ai/code/session_01WGAwvvTwDg2yknFkmZ3qzs
There was a problem hiding this comment.
Code Review
This pull request modifies the ELF assembly process to force ELF32 format for Plan 9 compatibility, as the kernel exec requires it even on amd64. It introduces an elfforce32 function to reset header sizes and offsets. Feedback suggests improving comment style consistency for the new function and ensuring the global header's class identifier is updated within elfforce32 to maintain internal consistency.
| /* elfforce32: switch to ELF32 format after elfinit(). | ||
| * Plan9's kernel exec only accepts ELF32, even on amd64. */ |
There was a problem hiding this comment.
The comment style for elfforce32 is inconsistent with the standard Plan 9 multi-line comment style used elsewhere in the project. It is recommended to use the standard format for better maintainability and consistency.
/*
* elfforce32: switch to ELF32 format after elfinit().
* Plan9's kernel exec only accepts ELF32, even on amd64.
*/| elf64 = 0; | ||
| hdr.phoff = ELF32HDRSIZE; | ||
| hdr.shoff = ELF32HDRSIZE; | ||
| hdr.ehsize = ELF32HDRSIZE; | ||
| hdr.phentsize = ELF32PHDRSIZE; | ||
| hdr.shentsize = ELF32SHDRSIZE; |
There was a problem hiding this comment.
The elfforce32 function should also update hdr.ident[EI_CLASS] to ELFCLASS32 to ensure the global hdr structure remains internally consistent after the format switch. While asmbelf currently overwrites this field, keeping elfforce32 self-contained prevents potential issues if hdr is used elsewhere before being fully re-initialized.
elf64 = 0;
hdr.ident[EI_CLASS] = ELFCLASS32;
hdr.phoff = ELF32HDRSIZE;
hdr.shoff = ELF32HDRSIZE;
hdr.ehsize = ELF32HDRSIZE;
hdr.phentsize = ELF32PHDRSIZE;
hdr.shentsize = ELF32SHDRSIZE;
Plan9's kernel exec only accepts ELF32 format, even on amd64. The original 6l -H5 case always wrote ELFCLASS32 (cput(1) in case 5). Our new asmb_elf() was generating ELFCLASS64 which Plan9's kernel rejects with "exec header invalid".
Fix: add elfforce32() to elf.c, call it in asmb_elf() after elfinit(). This switches elf64=0 and resets all header/phdr/shdr size fields to ELF32 values. All Plan9 userspace addresses fit comfortably in 32 bits (INITTEXT=0x200C00).
The EM_X86_64 machine type in an ELF32 header (ELFCLASS32 + machine=62) is the exact format the original 6l -H5 used and what Plan9/9front expects.
https://claude.ai/code/session_01WGAwvvTwDg2yknFkmZ3qzs