Skip to content

6l: use ELF32 (ELFCLASS32) format for Plan9 ELF output#147

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

6l: use ELF32 (ELFCLASS32) format for Plan9 ELF output#147
staalmannen merged 1 commit into
mainfrom
claude/upgrade-ape-c-library-mmZGd

Conversation

@staalmannen

Copy link
Copy Markdown
Owner

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

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
@staalmannen staalmannen merged commit 248d2ca into main May 22, 2026
1 check failed

@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 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.

Comment thread sys/src/cmd/ld/elf.c
Comment on lines +72 to +73
/* elfforce32: switch to ELF32 format after elfinit().
* Plan9's kernel exec only accepts ELF32, even on amd64. */

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 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.
 */

Comment thread sys/src/cmd/ld/elf.c
Comment on lines +77 to +82
elf64 = 0;
hdr.phoff = ELF32HDRSIZE;
hdr.shoff = ELF32HDRSIZE;
hdr.ehsize = ELF32HDRSIZE;
hdr.phentsize = ELF32PHDRSIZE;
hdr.shentsize = ELF32SHDRSIZE;

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 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;

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