mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-02 07:50:31 +00:00
Add APE support for 32-bit programs
This is an experimental proof of concept demo: make -j8 o//examples/i386.i386.com o//examples/i386.i386.com --32 echo $? # <-- prints main32's argc parameter Please note there's no libc support at the moment. This change is just for fun and shouldn't be interpreted as intent to support. Having this project on the whole support i386 would be tough because the calling conventions are so different. There really isn't much in the way of low hanging fruit opportunities to do something like NOP out REX prefixes and call it a day. It'd likely need disjoint builds or possibly better yet really cleverly crafted code generation in //third_party/chibicc! Another challenge is is that Linux changed its SYSCALL ordinals when it migrated from i386 to x86_64. Linux used to use the same magic numbers as everyone else for functions like exit/write/read/etc. BSDs on the other hand didn't pointlessly renumber things. So we'd need to find a way to define the ordinals in libc/sysv/syscalls.sh twice for Linux. The same goes for other ISAs too. Especially MIPS. For the Linux Kernel alone, syscall magic numbers and data structure layouts are totally different, and that's likely the case for other ISAs on other operating systems too. Probably because ISA code historically got contributed to open source by the companies that made the chips. Ulrich Drepper wrote an amusing essay on the subject some years back.
This commit is contained in:
parent
e345b42d78
commit
face6b61d5
11 changed files with 190 additions and 2 deletions
32
ape/ape.S
32
ape/ape.S
|
@ -487,6 +487,36 @@ apesh: .ascii "'\n#'\"\n" # sixth edition shebang
|
|||
.ascii "el"
|
||||
#endif
|
||||
.ascii "if exec 7<> \"$o\"; then\n"
|
||||
|
||||
// Writes 32-bit ELF header if --32 is the first argument
|
||||
// This should be changed to check $(uname -m) on ENOEXEC
|
||||
.ascii "if [ x\"$1\" = x--32 ]; then\n"
|
||||
.ascii "printf '"
|
||||
.ascii "\\177ELF" # 0x0: ⌂ELF
|
||||
.ascii "\\1" # 4: legacy mode
|
||||
.ascii "\\1" # 5: little endian
|
||||
.ascii "\\1" # 6: elf v1.o
|
||||
.ascii "\\011" # 7: FreeBSD
|
||||
.ascii "\\0" # 8: os/abi ver.
|
||||
.ascii "\\0\\0\\0" # 9: padding 3/7
|
||||
.ascii "\\0\\0\\0\\0" # padding 4/7
|
||||
.ascii "\\2\\0" # 10: εxεcµταblε
|
||||
.ascii "\\3\\0" # 12: i386
|
||||
.ascii "\\1\\0\\0\\0" # 14: elf v1.o
|
||||
.shstub ape_elf32_entry,4 # 18: e_entry
|
||||
.shstub ape_elf32_phoff,4 # 1c: e_phoff
|
||||
.shstub ape_elf32_shoff,4 # 20: e_shoff
|
||||
.ascii "\\0\\0\\0\\0" # 24: e_flags
|
||||
.ascii "\\100\\0" # 28: e_ehsize
|
||||
.ascii "\\040\\0" # 2a: e_phentsize
|
||||
.shstub ape_elf32_phnum,2 # 2c: e_phnum
|
||||
.ascii "\\0\\0" # 2e: e_shentsize
|
||||
.shstub ape_elf32_shnum,2 # 30: e_shnum
|
||||
.shstub ape_elf32_shstrndx,2 # 32: e_shstrndx
|
||||
.ascii "' >&7\n"
|
||||
.ascii "else\n"
|
||||
|
||||
// Standard 64-bit ELF Header
|
||||
.ascii "printf '"
|
||||
.ascii "\\177ELF" # 0x0: ⌂ELF
|
||||
.ascii "\\2" # 4: long mode
|
||||
|
@ -510,6 +540,8 @@ apesh: .ascii "'\n#'\"\n" # sixth edition shebang
|
|||
.shstub ape_elf_shnum,2 # 3c: e_shnum
|
||||
.shstub ape_elf_shstrndx,2 # 3e: e_shstrndx
|
||||
.ascii "' >&7\n"
|
||||
.ascii "fi\n"
|
||||
|
||||
.ascii "exec 7<&-\n"
|
||||
.ascii "fi\n"
|
||||
.ascii "exec \"$0\" \"$@\"\n" # etxtbsy tail recursion
|
||||
|
|
12
ape/ape.lds
12
ape/ape.lds
|
@ -207,6 +207,9 @@ SECTIONS {
|
|||
HIDDEN(ape_phdrs = .);
|
||||
KEEP(*(.elf.phdrs))
|
||||
HIDDEN(ape_phdrs_end = .);
|
||||
HIDDEN(ape_phdrs32 = .);
|
||||
KEEP(*(.elf.phdrs32))
|
||||
HIDDEN(ape_phdrs32_end = .);
|
||||
|
||||
/* OpenBSD */
|
||||
. = ALIGN(__SIZEOF_POINTER__);
|
||||
|
@ -246,7 +249,7 @@ SECTIONS {
|
|||
/*END: realmode addressability guarantee */
|
||||
|
||||
/* Normal Code */
|
||||
*(.start)
|
||||
KEEP(*(.start))
|
||||
KEEP(*(.initprologue))
|
||||
KEEP(*(SORT_BY_NAME(.init.*)))
|
||||
KEEP(*(.init))
|
||||
|
@ -445,6 +448,13 @@ PFSTUB4(ape_elf_phnum, (ape_phdrs_end - ape_phdrs) / 56);
|
|||
PFSTUB4(ape_elf_shnum, 0);
|
||||
PFSTUB4(ape_elf_shstrndx, 0);
|
||||
|
||||
PFSTUB4(ape_elf32_entry, _start32);
|
||||
PFSTUB4(ape_elf32_phoff, RVA(ape_phdrs32));
|
||||
PFSTUB4(ape_elf32_shoff, 0);
|
||||
PFSTUB4(ape_elf32_phnum, (ape_phdrs32_end - ape_phdrs32) / 32);
|
||||
PFSTUB4(ape_elf32_shnum, 0);
|
||||
PFSTUB4(ape_elf32_shstrndx, 0);
|
||||
|
||||
HIDDEN(__privileged_addr = ROUNDDOWN(__privileged_start, PAGESIZE));
|
||||
HIDDEN(__privileged_size = (ROUNDUP(__privileged_end, PAGESIZE) -
|
||||
ROUNDDOWN(__privileged_start, PAGESIZE)));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue