Get aarch64 hello world working

$ m=aarch64-tiny
    $ make -j8 m=$m o/$m/tool/hello/hello.com o/third_party/qemu/qemu-aarch64
    $ o/third_party/qemu/qemu-aarch64 o/$m/tool/hello/hello.com
    hello world
    $ ls -hal o/$m/tool/hello/hello.com
    -rwxr-xr-x 1 jart jart 4.0K May  9 05:04 o/aarch64-tiny/tool/hello/hello.com
This commit is contained in:
Justine Tunney 2023-05-09 02:35:05 -07:00
parent e5e3cdf447
commit ae0ee59614
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
174 changed files with 1454 additions and 851 deletions

View file

@ -42,6 +42,7 @@
#include "libc/nexgen32e/uart.internal.h"
#include "libc/nt/pedef.internal.h"
#include "libc/runtime/pc.internal.h"
#include "ape/ape.internal.h"
#include "libc/sysv/consts/prot.h"
#define USE_SYMBOL_HACK 1
@ -587,7 +588,7 @@ ape_disk:
the bourne executable & linkable format */
#if SupportsWindows() || SupportsMetal() || SupportsXnu()
#ifdef APE_IS_SHELL_SCRIPT
apesh: .ascii "\n@\n#'\"\n" // sixth edition shebang
.ascii "m=\"$(uname -m)\"\n"
@ -698,6 +699,8 @@ apesh: .ascii "\n@\n#'\"\n" // sixth edition shebang
.ascii "PHDRS='' <<'@'\n"
.endobj apesh
#if !IsTiny()
// elf program headers get inserted here
// because they need to be in the first 4096 bytes
.section .emush,"a",@progbits
@ -786,6 +789,8 @@ emush: .ascii "\n@\n#'\"\n"
.ascii "exit 1\n"
.endobj emush
#endif /* !IsTiny() */
#ifdef APE_LOADER
.section .ape.loader,"a",@progbits
.balign 64
@ -798,7 +803,7 @@ ape_loader_end:
.endobj ape_loader_end,globl
.previous
#endif /* APE_LOADER */
#endif /* SupportsWindows() || SupportsMetal() || SupportsXnu() */
#endif /* APE_IS_SHELL_SCRIPT */
#if SupportsSystemv() || SupportsMetal()
.section .elf.phdrs,"a",@progbits
@ -1024,6 +1029,7 @@ ape_macho:
.previous /* .macho */
#endif /* SupportsXnu() */
#if SupportsWindows() || SupportsMetal()
/*
@ -1100,8 +1106,6 @@ PETEXT = 0b01100000000000000000000001100000
PEDATA = 0b11000000000000000000000011000000
PEIMPS = 0b11000000000000000000000001000000
#if SupportsWindows() || SupportsMetal()
.section .pe.header,"a",@progbits
.balign __SIZEOF_POINTER__
ape_pe: .ascin "PE",4
@ -1190,6 +1194,8 @@ ape_pe: .ascin "PE",4
#endif /* SupportsWindows() || SupportsMetal() */
#if SupportsWindows()
.section .idata.ro.idt.1,"a",@progbits
.type ape_idata_idtend,@object
.type ape_idata_idt,@object
@ -1205,6 +1211,24 @@ ape_idata_idt:
ape_idata_idtend:
.previous
.section .piro.data.sort.iat.1,"aw",@progbits
.type ape_idata_iatend,@object
.type ape_idata_iat,@object
.globl ape_idata_iat,ape_idata_iatend
.hidden ape_idata_iat,ape_idata_iatend
ape_idata_iat:
.previous/*
...
decentralized content
...
*/.section .piro.data.sort.iat.3,"aw",@progbits
ape_idata_iatend:
.previous
#endif /* SupportsWindows() */
#if SupportsMetal()
.section .piro.data.sort.metal_gdt,"aw",@progbits
.balign 8
_gdt:
@ -1241,21 +1265,6 @@ _gdt_end:
.endobj _gdt,global,hidden
.previous
.section .piro.data.sort.iat.1,"aw",@progbits
.type ape_idata_iatend,@object
.type ape_idata_iat,@object
.globl ape_idata_iat,ape_idata_iatend
.hidden ape_idata_iat,ape_idata_iatend
ape_idata_iat:
.previous/*
...
decentralized content
...
*/.section .piro.data.sort.iat.3,"aw",@progbits
ape_idata_iatend:
.previous
#if SupportsMetal()
/*
αcτµαlly pδrταblε εxεcµταblε § early-stage read-only data
@ -1882,7 +1891,6 @@ ape_pad_text:
.type ape_pad_privileged,@object
.hidden ape_pad_privileged
ape_pad_privileged:
.balign 4096
.previous
.section .ape.pad.rodata,"a",@progbits
@ -1897,11 +1905,13 @@ ape_pad_rodata:
ape_pad_data:
.previous
#if SupportsWindows()
.section .idata.ro,"a",@progbits
.type ape_idata_ro,@object
.hidden ape_idata_ro
ape_idata_ro:
.previous
#endif /* SupportsWindows() */
.section .dataprologue,"aw",@progbits
.type __data_start,@object
@ -1931,6 +1941,8 @@ __bss_start:
__bss_end:
.previous
#ifdef APE_IS_SHELL_SCRIPT
.section .blink,"a",@progbits
.globl blink_aarch64_size
blink_aarch64:
@ -1947,5 +1959,7 @@ blink_darwin_arm64:
blink_darwin_arm64_size = . - blink_darwin_arm64
.previous
#endif /* APE_IS_SHELL_SCRIPT */
.end


9
ape/ape.internal.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef COSMOPOLITAN_APE_APE_INTERNAL_H_
#define COSMOPOLITAN_APE_APE_INTERNAL_H_
#include "libc/dce.h"
#if SupportsWindows() || SupportsMetal() || SupportsXnu()
#define APE_IS_SHELL_SCRIPT
#endif
#endif /* COSMOPOLITAN_APE_APE_INTERNAL_H_ */

View file

@ -181,8 +181,15 @@
#include "libc/elf/pf2prot.internal.h"
#include "libc/nt/pedef.internal.h"
#include "libc/thread/tls.h"
#include "ape/ape.internal.h"
#include "libc/zip.h"
#ifdef __x86__
#define CODE_GRANULE 1
#else
#define CODE_GRANULE 4
#endif
ENTRY(_start)
PHDRS {
@ -250,10 +257,11 @@ SECTIONS {
KEEP(*(SORT_BY_NAME(.sort.text.real.*)))
/* Code we want earlier in the binary w/o modifications */
KEEP(*(.ape.loader))
. = ALIGN(CODE_GRANULE);
HIDDEN(_ereal = .);
/*END: realmode addressability guarantee */
/*BEGIN: morphable code */
. += 1;
. += CODE_GRANULE;
/* Normal Code */
*(.start)
@ -291,13 +299,12 @@ SECTIONS {
/* Privileged code invulnerable to magic */
KEEP(*(.ape.pad.privileged));
. += . > 0 ? 1 : 0;
. = ALIGN(__privileged_end > __privileged_start ? PAGESIZE : 1);
/*END: morphable code */
HIDDEN(__privileged_start = .);
. += . > 0 ? 1 : 0;
*(.privileged)
HIDDEN(__privileged_end = .);
. += . > 0 ? 1 : 0;
. += . > 0 ? CODE_GRANULE : 0;
/*BEGIN: Read Only Data */
@ -383,7 +390,7 @@ SECTIONS {
KEEP(*(.dataprologue))
*(.data .data.*)
KEEP(*(SORT_BY_NAME(.sort.data.*)))
. += . > 0 ? 1 : 0;
. += . > 0 ? CODE_GRANULE : 0;
KEEP(*(.gotprologue))
*(.got)
@ -420,7 +427,7 @@ SECTIONS {
*(.piro.bss)
KEEP(*(SORT_BY_NAME(.piro.bss.sort.*)))
HIDDEN(__piro_end = .);
. += . > 0 ? 1 : 0;
. += . > 0 ? CODE_GRANULE : 0;
/*END: Post-Initialization Read-Only */
/* Statically Allocated Empty Space */
@ -567,6 +574,8 @@ SHSTUB2(ape_loader_dd_count,
? ROUNDUP(ape_loader_end - ape_loader, PAGESIZE) / 64
: 0);
#if defined(APE_IS_SHELL_SCRIPT) && !IsTiny()
HIDDEN(blink_aarch64_b0 = RVA(blink_aarch64) % 10 + 48);
HIDDEN(blink_aarch64_b1 = RVA(blink_aarch64) < 10 ? 32 : RVA(blink_aarch64) / 10 % 10 + 48);
HIDDEN(blink_aarch64_b2 = RVA(blink_aarch64) < 100 ? 32 : RVA(blink_aarch64) / 100 % 10 + 48);
@ -611,6 +620,8 @@ HIDDEN(blink_darwin_arm64_size_b7 = blink_darwin_arm64_size < 10000000 ? 32 : bl
HIDDEN(blink_darwin_arm64_size_b8 = blink_darwin_arm64_size < 100000000 ? 32 : blink_darwin_arm64_size / 100000000 % 10 + 48);
HIDDEN(blink_darwin_arm64_size_b9 = blink_darwin_arm64_size < 1000000000 ? 32 : blink_darwin_arm64_size / 1000000000 % 10 + 48);
#endif /* APE_IS_SHELL_SCRIPT */
#if SupportsMetal()
HIDDEN(v_ape_realsectors =
MIN(0x70000 - IMAGE_BASE_REAL, ROUNDUP(RVA(_ezip), 512)) / 512);

View file

@ -22,7 +22,14 @@ APE_INCS = $(filter %.inc,$(APE_FILES))
APE = o/$(MODE)/ape/ape.o \
o/$(MODE)/ape/ape.lds
ifeq ($(MODE), aarch64)
APELINK = \
$(COMPILE) \
-ALINK.ape \
$(LINK) \
$(LINKARGS) \
$(OUTPUT_OPTION)
ifeq ($(ARCH), aarch64)
APE_SRCS = ape/ape.S
APE_OBJS = o/$(MODE)/ape/ape.o
@ -42,13 +49,6 @@ APE_COPY_SELF = \
o/$(MODE)/ape/ape.lds \
o/$(MODE)/ape/ape-copy-self.o
APELINK = \
$(COMPILE) \
-ALINK.ape \
$(LINK) \
$(LINKARGS) \
$(OUTPUT_OPTION)
APE_LOADER_FLAGS = \
-DNDEBUG \
-iquote. \
@ -75,6 +75,7 @@ o/$(MODE)/ape/public/ape.lds: OVERRIDE_CPPFLAGS += -UCOSMO
o/$(MODE)/ape/public/ape.lds: \
ape/public/ape.lds \
ape/ape.lds \
ape/ape.internal.h \
ape/macros.internal.h \
ape/relocations.h \
libc/intrin/bits.h \
@ -98,6 +99,7 @@ o/$(MODE)/ape/ape-no-modify-self.o: \
ape/macros.internal.h \
ape/notice.inc \
ape/relocations.h \
ape/ape.internal.h \
libc/dce.h \
libc/elf/def.h \
libc/intrin/asancodes.h \
@ -127,6 +129,7 @@ o/$(MODE)/ape/ape-copy-self.o: \
ape/macros.internal.h \
ape/notice.inc \
ape/relocations.h \
ape/ape.internal.h \
libc/dce.h \
libc/elf/def.h \
libc/intrin/asancodes.h \
@ -202,6 +205,7 @@ o/$(MODE)/ape/ape.lds: \
ape/ape.lds \
ape/macros.internal.h \
ape/relocations.h \
ape/ape.internal.h \
libc/intrin/bits.h \
libc/thread/tls.h \
libc/calls/struct/timespec.h \