Skip to content

Commit 248d2ca

Browse files
authored
Merge pull request #147 from staalmannen/claude/upgrade-ape-c-library-mmZGd
6l: use ELF32 (ELFCLASS32) format for Plan9 ELF output
2 parents b18434e + 0979a42 commit 248d2ca

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

sys/src/cmd/6l/asm.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ asmbelf(long textfoff, vlong datafoff, vlong datva)
190190
eh->ident[EI_MAG1] = ELFMAG1;
191191
eh->ident[EI_MAG2] = ELFMAG2;
192192
eh->ident[EI_MAG3] = ELFMAG3;
193-
eh->ident[EI_CLASS] = ELFCLASS64;
193+
eh->ident[EI_CLASS] = ELFCLASS32; /* Plan9 kernel exec requires ELF32 */
194194
eh->ident[EI_DATA] = ELFDATA2LSB;
195195
eh->ident[EI_VERSION] = EV_CURRENT;
196196
eh->type = ET_EXEC;
@@ -207,7 +207,11 @@ asmbelf(long textfoff, vlong datafoff, vlong datva)
207207
}
208208

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

221-
/* Initialize ELF state */
225+
/* Initialize ELF state, then force ELF32 for Plan9 compatibility */
222226
elfinit();
227+
elfforce32();
223228

224229
/* NULL section header must be at index 0 — before any other shdr */
225230
newElfShdr(0);

sys/src/cmd/ld/elf.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ elfinit(void)
6969
}
7070
}
7171

72+
/* elfforce32: switch to ELF32 format after elfinit().
73+
* Plan9's kernel exec only accepts ELF32, even on amd64. */
74+
void
75+
elfforce32(void)
76+
{
77+
elf64 = 0;
78+
hdr.phoff = ELF32HDRSIZE;
79+
hdr.shoff = ELF32HDRSIZE;
80+
hdr.ehsize = ELF32HDRSIZE;
81+
hdr.phentsize = ELF32PHDRSIZE;
82+
hdr.shentsize = ELF32SHDRSIZE;
83+
}
84+
7285
ElfEhdr*
7386
getElfEhdr(void)
7487
{

sys/src/cmd/ld/elf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ struct Elf_Note
209209

210210
/* Declarations */
211211
void elfinit(void);
212+
void elfforce32(void);
212213
ElfEhdr* getElfEhdr(void);
213214
ElfShdr* newElfShdr(vlong);
214215
ElfPhdr* newElfPhdr(void);

0 commit comments

Comments
 (0)