From d2768384bf660fafb13d74402b8be7c97035fb26 Mon Sep 17 00:00:00 2001 From: yellows8 Date: Fri, 8 Sep 2017 19:20:25 -0400 Subject: [PATCH] Added buildscripts/ and README. --- README.md | 5 + buildscripts/base_rules | 111 ++++++++++++++++++++ buildscripts/base_tools | 33 ++++++ buildscripts/lib/switch.ld | 185 +++++++++++++++++++++++++++++++++ buildscripts/lib/switch.specs | 8 ++ buildscripts/lib/switch_crt0.s | 60 +++++++++++ buildscripts/switch_rules | 15 +++ 7 files changed, 417 insertions(+) create mode 100644 README.md create mode 100644 buildscripts/base_rules create mode 100644 buildscripts/base_tools create mode 100644 buildscripts/lib/switch.ld create mode 100644 buildscripts/lib/switch.specs create mode 100644 buildscripts/lib/switch_crt0.s create mode 100644 buildscripts/switch_rules diff --git a/README.md b/README.md new file mode 100644 index 00000000..a1616b63 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +Nintendo Switch AArch64-only userland library. Based on libctru. + +The \*rules files under buildscripts/ should be copied to "devkitA64/", while the content of buildscripts/lib/ should be copied to "devkitA64/aarch64-none-elf/lib/". +* Build switch_crt0 with: aarch64-none-elf-gcc -march=armv8-a -x assembler-with-cpp -c switch_crt0.s -o switch_crt0.o + diff --git a/buildscripts/base_rules b/buildscripts/base_rules new file mode 100644 index 00000000..45e4b8a9 --- /dev/null +++ b/buildscripts/base_rules @@ -0,0 +1,111 @@ +include $(DEVKITA64)/base_tools + +#--------------------------------------------------------------------------------- +# add portlibs path +#--------------------------------------------------------------------------------- +export PORTLIBS_PATH := $(DEVKITPRO)/portlibs + + +#--------------------------------------------------------------------------------- +%.a: +#--------------------------------------------------------------------------------- + @echo $(notdir $@) + @rm -f $@ + $(AR) -rc $@ $^ + +#--------------------------------------------------------------------------------- +%.arm.o: %.arm.cpp + @echo $(notdir $<) + $(CXX) -MMD -MP -MF $(DEPSDIR)/$*.arm.d $(CXXFLAGS) -marm -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.arm.o: %.arm.c + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.arm.d $(CFLAGS) -marm -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.arm.o: %.arm.m + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.arm.d $(OBJCFLAGS) -marm -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.thumb.o: %.thumb.cpp + @echo $(notdir $<) + $(CXX) -MMD -MP -MF $(DEPSDIR)/$*.thumb.d $(CXXFLAGS) -mthumb -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.thumb.o: %.thumb.c + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.thumb.d $(CFLAGS) -mthumb -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.thumb.o: %.thumb.m + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.thumb.d $(OBJCFLAGS) -mthumb -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.iwram.o: %.iwram.cpp + @echo $(notdir $<) + $(CXX) -MMD -MP -MF $(DEPSDIR)/$*.iwram.d $(CXXFLAGS) -marm -mlong-calls -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.iwram.o: %.iwram.c + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.iwram.d $(CFLAGS) -marm -mlong-calls -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.iwram.o: %.iwram.m + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.iwram.d $(OBJCFLAGS) -marm -mlong-calls -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.itcm.o: %.itcm.cpp + @echo $(notdir $<) + $(CXX) -MMD -MP -MF $(DEPSDIR)/$*.itcm.d $(CXXFLAGS) -marm -mlong-calls -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.itcm.o: %.itcm.c + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.itcm.d $(CFLAGS) -marm -mlong-calls -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.itcm.o: %.itcm.m + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.itcm.d $(OBJCFLAGS) -marm -mlong-calls -c $< -o $@ $(ERROR_FILTER) + + +#--------------------------------------------------------------------------------- +%.o: %.cpp + @echo $(notdir $<) + $(CXX) -MMD -MP -MF $(DEPSDIR)/$*.d $(CXXFLAGS) -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.o: %.c + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(CFLAGS) -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.o: %.m + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(OBJCFLAGS) -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.o: %.s + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.d -x assembler-with-cpp $(ASFLAGS) -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.o: %.S + @echo $(notdir $<) + $(CC) -MMD -MP -MF $(DEPSDIR)/$*.d -x assembler-with-cpp $(ASFLAGS) -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +# canned command sequence for binary data +#--------------------------------------------------------------------------------- +define bin2o + bin2s $< | $(AS) -o $(@) + echo "extern const u8" `(echo $( `(echo $(> `(echo $(> `(echo $(&1 | sed -e 's/\(.[a-zA-Z]\+\):\([0-9]\+\):/\1(\2):/g' +endif diff --git a/buildscripts/lib/switch.ld b/buildscripts/lib/switch.ld new file mode 100644 index 00000000..6d025135 --- /dev/null +++ b/buildscripts/lib/switch.ld @@ -0,0 +1,185 @@ +OUTPUT_ARCH(aarch64) +ENTRY(_start) + +PHDRS +{ + code PT_LOAD FLAGS(5) /* Read | Execute */; + rodata PT_LOAD FLAGS(4) /* Read */; + data PT_LOAD FLAGS(6) /* Read | Write */; +} + +SECTIONS +{ + /* =========== CODE section =========== */ + + PROVIDE(__start__ = 0x0); + . = __start__; + + .text ALIGN(0x1000) : + { + /* .init */ + KEEP( *(.crt0) ) + KEEP( *(.init) ) + . = ALIGN(4); + + /* .text */ + *(.text) + *(.text.*) + *(.glue_7) + *(.glue_7t) + *(.stub) + *(.gnu.warning) + *(.gnu.linkonce.t*) + . = ALIGN(4); + + /* .fini */ + KEEP( *(.fini) ) + . = ALIGN(4); + } : code + + /* =========== RODATA section =========== */ + + .rodata ALIGN(0x1000) : + { + *(.rodata) + *(.roda) + *(.rodata.*) + *all.rodata*(*) + *(.gnu.linkonce.r*) + SORT(CONSTRUCTORS) + . = ALIGN(4); + } : rodata + + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } : rodata + __exidx_start = .; + ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } : rodata + __exidx_end = .; + + /* =========== DATA section =========== */ + + .data ALIGN(0x1000) : + { + *(.data) + *(.data.*) + *(.gnu.linkonce.d*) + CONSTRUCTORS + . = ALIGN(4); + } : data + + .tdata ALIGN(4) : + { + __tdata_lma = .; + *(.tdata) + *(.tdata.*) + *(.gnu.linkonce.td.*) + . = ALIGN(4); + __tdata_lma_end = .; + } : data + + .tbss ALIGN(4) : + { + *(.tbss) + *(.tbss.*) + *(.gnu.linkonce.tb.*) + *(.tcommon) + . = ALIGN(4); + } : data + + .preinit_array ALIGN(4) : + { + PROVIDE (__preinit_array_start = .); + KEEP (*(.preinit_array)) + PROVIDE (__preinit_array_end = .); + } : data + + .init_array ALIGN(4) : + { + PROVIDE (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array)) + PROVIDE (__init_array_end = .); + } : data + + .fini_array ALIGN(4) : + { + PROVIDE (__fini_array_start = .); + KEEP (*(.fini_array)) + KEEP (*(SORT(.fini_array.*))) + PROVIDE (__fini_array_end = .); + } : data + + .ctors ALIGN(4) : + { + KEEP (*crtbegin.o(.ctors)) /* MUST be first -- GCC requires it */ + KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors)) + KEEP (*(SORT(.ctors.*))) + KEEP (*(.ctors)) + } : data + + .dtors ALIGN(4) : + { + KEEP (*crtbegin.o(.dtors)) + KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors)) + KEEP (*(SORT(.dtors.*))) + KEEP (*(.dtors)) + } : data + + __bss_start__ = .; + .bss ALIGN(4) : + { + *(.dynbss) + *(.bss) + *(.bss.*) + *(.gnu.linkonce.b*) + *(COMMON) + . = ALIGN(4); + + /* Reserve space for the TLS segment of the main thread */ + __tls_start = .; + . += + SIZEOF(.tdata) + SIZEOF(.tbss); + __tls_end = .; + } : data + __bss_end__ = .; + + __end__ = ABSOLUTE(.) ; + + /* ================== + ==== Metadata ==== + ================== */ + + /* Discard sections that difficult post-processing */ + /DISCARD/ : { *(.group .comment .note) } + + /* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + + /* DWARF debug sections. + Symbols in the DWARF debugging sections are relative to the beginning + of the section so we begin them at 0. */ + + /* DWARF 1 */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } + + /* GNU DWARF 1 extensions */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } + + /* DWARF 1.1 and DWARF 2 */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } + + /* DWARF 2 */ + .debug_info 0 : { *(.debug_info) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } +} diff --git a/buildscripts/lib/switch.specs b/buildscripts/lib/switch.specs new file mode 100644 index 00000000..a78feafc --- /dev/null +++ b/buildscripts/lib/switch.specs @@ -0,0 +1,8 @@ +%rename link old_link + +*link: +%(old_link) -T switch.ld%s -d --emit-relocs --export-dynamic --gc-sections + +*startfile: +switch_crt0%O%s crti%O%s crtbegin%O%s + diff --git a/buildscripts/lib/switch_crt0.s b/buildscripts/lib/switch_crt0.s new file mode 100644 index 00000000..c408ed51 --- /dev/null +++ b/buildscripts/lib/switch_crt0.s @@ -0,0 +1,60 @@ + .section ".crt0","ax" + .global _start, __service_ptr, __apt_appid, __heap_size, __linear_heap_size, __system_arglist, __system_runflags +#--------------------------------------------------------------------------------- +_start: +#--------------------------------------------------------------------------------- + bl startup +startup: + mov x4, x30 + sub x4, x4, #4 //x4 = _start addr + + // Clear the BSS section + ldr x0, =__bss_start__ + ldr x1, =__bss_end__ + sub x1, x1, x0 + add x0, x0, x4 + bl ClearMem + + // System initialization + //mov x0, x4 + //bl initSystem + + // Set up argc/argv arguments for main() + /*ldr x0, =__system_argc + ldr x1, =__system_argv + ldr x0, [x0] + ldr x1, [x1]*/ + mov x0, #0 + mov x1, #0 + + // Jump to user code + ldr x3, =main + add x3, x3, x4 + //ldr x30, =__ctru_exit + //br x3 + blr x3 + + _sysexit: + svc 0x7 + b . + +#--------------------------------------------------------------------------------- +# Clear memory to 0x00 if length != 0 +# x0 = Start Address +# x1 = Length +#--------------------------------------------------------------------------------- +ClearMem: +#--------------------------------------------------------------------------------- + mov x2, #3 // Round down to nearest word boundary + add x1, x1, x2 // Shouldn't be needed + bics x1, x1, x2 // Clear 2 LSB (and set Z) + beq ClearMem_end // Quit if copy size is 0 + + mov x2, #0 +ClrLoop: + str x2, [x0], #4 + subs x1, x1, #4 + bne ClrLoop + +ClearMem_end: + ret diff --git a/buildscripts/switch_rules b/buildscripts/switch_rules new file mode 100644 index 00000000..89c0a045 --- /dev/null +++ b/buildscripts/switch_rules @@ -0,0 +1,15 @@ +ifeq ($(strip $(DEVKITPRO)),) +$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=devkitPro) +endif + +include $(DEVKITA64)/base_rules + +PORTLIBS := $(PORTLIBS_PATH)/armv8-a $(PORTLIBS_PATH)/switch + +LIBNX ?= $(DEVKITPRO)/libnx + +#--------------------------------------------------------------------------------- +%.elf: + @echo linking $(notdir $@) + @$(LD) $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) -o $@ + @$(NM) -CSn $@ > $(notdir $*.lst)