Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions sys/src/cmd/6l/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ asmbelf(long textfoff, vlong datafoff, vlong datva)
eh->ident[EI_MAG1] = ELFMAG1;
eh->ident[EI_MAG2] = ELFMAG2;
eh->ident[EI_MAG3] = ELFMAG3;
eh->ident[EI_CLASS] = ELFCLASS64;
eh->ident[EI_CLASS] = ELFCLASS32; /* Plan9 kernel exec requires ELF32 */
eh->ident[EI_DATA] = ELFDATA2LSB;
eh->ident[EI_VERSION] = EV_CURRENT;
eh->type = ET_EXEC;
Expand All @@ -207,7 +207,11 @@ asmbelf(long textfoff, vlong datafoff, vlong datva)
}

/*
* asmb_elf: assemble ELF64 output (called by asmb when HEADTYPE==5).
* asmb_elf: assemble ELF32/AMD64 output (called by asmb when HEADTYPE==5).
*
* Plan9's kernel exec only accepts ELF32 format (ELFCLASS32), even on amd64.
* This matches the original 6l -H5 behaviour (cput(1) = ELFCLASS32 in case 5).
* All Plan9 userspace addresses fit in 32 bits, so ELF32 is sufficient.
*/
static void
asmb_elf(void)
Expand All @@ -218,8 +222,9 @@ asmb_elf(void)
uchar *op1;
vlong datva, datafoff, pad;

/* Initialize ELF state */
/* Initialize ELF state, then force ELF32 for Plan9 compatibility */
elfinit();
elfforce32();

/* NULL section header must be at index 0 — before any other shdr */
newElfShdr(0);
Expand Down
13 changes: 13 additions & 0 deletions sys/src/cmd/ld/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ elfinit(void)
}
}

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

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

void
elfforce32(void)
{
elf64 = 0;
hdr.phoff = ELF32HDRSIZE;
hdr.shoff = ELF32HDRSIZE;
hdr.ehsize = ELF32HDRSIZE;
hdr.phentsize = ELF32PHDRSIZE;
hdr.shentsize = ELF32SHDRSIZE;
Comment on lines +77 to +82

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;

}

ElfEhdr*
getElfEhdr(void)
{
Expand Down
1 change: 1 addition & 0 deletions sys/src/cmd/ld/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ struct Elf_Note

/* Declarations */
void elfinit(void);
void elfforce32(void);
ElfEhdr* getElfEhdr(void);
ElfShdr* newElfShdr(vlong);
ElfPhdr* newElfPhdr(void);
Expand Down
Loading