Index: buildsys.mk.in ================================================================== --- buildsys.mk.in +++ buildsys.mk.in @@ -128,11 +128,11 @@ all: ${MAKE} -s pre-all ${MAKE} -s subdirs ${MAKE} -s depend - ${MAKE} -s ${STATIC_LIB} ${STATIC_LIB_NOINST} ${STATIC_PIC_LIB} ${STATIC_PIC_LIB_NOINST} ${SHARED_LIB} ${SHARED_LIB_NOINST} ${FRAMEWORK} ${FRAMEWORK_NOINST} ${AMIGA_LIB} ${AMIGA_LIB_NOINST} ${PLUGIN} ${PLUGIN_NOINST} ${PROG} ${PROG_NOINST} ${JARFILE} locales + ${MAKE} -s ${STATIC_LIB} ${STATIC_LIB_NOINST} ${STATIC_PIC_LIB} ${STATIC_PIC_LIB_NOINST} ${STATIC_AMIGA_LIB} ${STATIC_AMIGA_LIB_NOINST} ${SHARED_LIB} ${SHARED_LIB_NOINST} ${FRAMEWORK} ${FRAMEWORK_NOINST} ${AMIGA_LIB} ${AMIGA_LIB_NOINST} ${PLUGIN} ${PLUGIN_NOINST} ${PROG} ${PROG_NOINST} ${JARFILE} locales ${MAKE} -s subdirs-after ${MAKE} -s post-all pre-all post-all: @@ -323,10 +323,48 @@ for i in $$ars; do \ dir=".$$(echo $$i | sed 's/\//_/g').objs"; \ rm -fr $$dir; \ done; \ fi + +${STATIC_AMIGA_LIB} ${STATIC_AMIGA_LIB_NOINST}: ${EXT_DEPS} ${AMIGA_LIB_OBJS} ${AMIGA_LIB_OBJS_EXTRA} + ${LINK_STATUS} + rm -f $@ + out="$@"; \ + objs=""; \ + ars=""; \ + for i in ${AMIGA_LIB_OBJS} ${AMIGA_LIB_OBJS_EXTRA}; do \ + case $$i in \ + *.a) \ + ars="$$ars $$i" \ + ;; \ + *.o) \ + objs="$$objs $$i" \ + ;; \ + esac \ + done; \ + for i in $$ars; do \ + dir=".$$(echo $$i | sed 's/\//_/g').objs"; \ + rm -fr $$dir; \ + mkdir -p $$dir; \ + cd $$dir; \ + ${AR} x ../$$i; \ + for j in *.o; do \ + objs="$$objs $$dir/$$j"; \ + done; \ + cd ..; \ + done; \ + if ${AR} cr $@ $$objs && ${RANLIB} $@; then \ + ${LINK_OK}; \ + else \ + rm -f $@; false; \ + ${LINK_FAILED}; \ + fi; \ + for i in $$ars; do \ + dir=".$$(echo $$i | sed 's/\//_/g').objs"; \ + rm -fr $$dir; \ + done locales: ${MO_FILES} .c.o: ${COMPILE_STATUS} @@ -646,11 +684,11 @@ else \ ${INSTALL_FAILED}; \ fi \ done - for i in "" ${STATIC_LIB} ${STATIC_PIC_LIB}; do \ + for i in "" ${STATIC_LIB} ${STATIC_PIC_LIB} ${STATIC_AMIGA_LIB}; do \ test x"$$i" = x"" && continue; \ ${INSTALL_STATUS}; \ if ${MKDIR_P} ${DESTDIR}${libdir} && ${INSTALL} -m 644 $$i ${DESTDIR}${libdir}/$$i; then \ ${INSTALL_OK}; \ else \ @@ -752,11 +790,11 @@ fi \ done rmdir ${DESTDIR}${prefix}/Library/Frameworks >/dev/null 2>&1 || true rmdir ${DESTDIR}${prefix}/Library >/dev/null 2>&1 || true - for i in "" ${STATIC_LIB} ${STATIC_PIC_LIB}; do \ + for i in "" ${STATIC_LIB} ${STATIC_PIC_LIB} ${STATIC_AMIGA_LIB}; do \ test x"$$i" = x"" && continue; \ if test -f ${DESTDIR}${libdir}/$$i; then \ if rm -f ${DESTDIR}${libdir}/$$i; then \ ${DELETE_OK}; \ else \ @@ -845,11 +883,11 @@ ${DIR_LEAVE}; \ done : >.deps - for i in "" ${DEPS} ${OBJS} ${OBJS_EXTRA} ${LIB_OBJS} ${LIB_OBJS_EXTRA} ${AMIGA_LIB_OBJS} ${AMIGA_LIB_OBJS_EXTRA} ${PLUGIN_OBJS} ${PROG} ${PROG_NOINST} ${SHARED_LIB} ${SHARED_LIB_NOINST} ${AMIGA_LIB} ${AMIGA_LIB_NOINST} ${STATIC_LIB} ${STATIC_LIB_NOINST} ${STATIC_PIC_LIB} ${STATIC_PIC_LIB_NOINST} ${FRAMEWORK} ${PLUGIN} ${PLUGIN_NOINST} ${CLEAN_LIB} ${MO_FILES} ${CLEAN}; do \ + for i in "" ${DEPS} ${OBJS} ${OBJS_EXTRA} ${LIB_OBJS} ${LIB_OBJS_EXTRA} ${AMIGA_LIB_OBJS} ${AMIGA_LIB_OBJS_EXTRA} ${PLUGIN_OBJS} ${PROG} ${PROG_NOINST} ${SHARED_LIB} ${SHARED_LIB_NOINST} ${AMIGA_LIB} ${AMIGA_LIB_NOINST} ${STATIC_LIB} ${STATIC_LIB_NOINST} ${STATIC_PIC_LIB} ${STATIC_PIC_LIB_NOINST} ${STATIC_AMIGA_LIB} ${STATIC_AMIGA_LIB_NOINST} ${FRAMEWORK} ${PLUGIN} ${PLUGIN_NOINST} ${CLEAN_LIB} ${MO_FILES} ${CLEAN}; do \ test x"$$i" = x"" && continue; \ if test -f $$i -o -d $$i; then \ if rm -fr $$i; then \ ${DELETE_OK}; \ else \ Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -53,10 +53,11 @@ enable_files="yes" # Required for reading ENV: enable_shared="no" supports_amiga_lib="yes" AS_IF([test x"$enable_amiga_lib" != x"no"], [ + AC_SUBST(OBJFW_AMIGA_LIB, objfw68k.library) AC_SUBST(OBJFWRT_AMIGA_LIB, objfwrt68k.library) AC_SUBST(SFDC_TARGET, m68k-amigaos) AC_SUBST(SFD_FILE, amigaos3.sfd) AC_SUBST(SFDC_INLINE_H, inline.h) dnl For 68000, GCC emits calls to helper functions that @@ -88,10 +89,11 @@ enable_files="yes" # Required for reading ENV: supports_amiga_lib="yes" check_pedantic="no" # Breaks generated inlines AS_IF([test x"$enable_amiga_lib" != x"no"], [ + AC_SUBST(OBJFW_AMIGA_LIB, objfw.library) AC_SUBST(OBJFWRT_AMIGA_LIB, objfwrt.library) AC_SUBST(CVINCLUDE_INLINE_H, inline.h) t="-mresident32 -ffreestanding -noixemul" AC_SUBST(AMIGA_LIB_CFLAGS, $t) t="-mresident32 -nostartfiles -nodefaultlibs" @@ -398,10 +400,25 @@ AC_SUBST(INVOCATION_A, "invocation.a") AC_SUBST(INVOCATION_INVOCATION_A, "invocation/invocation.a") AC_SUBST(LOOKUP_ASM_A, "lookup-asm.a") AC_SUBST(LOOKUP_ASM_LOOKUP_ASM_A, "lookup-asm/lookup-asm.a") ]) + +AS_IF([test x"$enable_amiga_lib" != x"no"], [ + AC_SUBST(EXCEPTIONS_AMIGALIB_A, "exceptions.amigalib.a") + AC_SUBST(EXCEPTIONS_EXCEPTIONS_AMIGALIB_A, + "exceptions/exceptions.amigalib.a") + AC_SUBST(FORWARDING_AMIGALIB_A, "forwarding.amigalib.a") + AC_SUBST(FORWARDING_FORWARDING_AMIGALIB_A, + "forwarding/forwarding.amigalib.a") + AC_SUBST(INVOCATION_AMIGALIB_A, "invocation.amigalib.a") + AC_SUBST(INVOCATION_INVOCATION_AMIGALIB_A, + "invocation/invocation.amigalib.a") + AC_SUBST(LOOKUP_ASM_AMIGALIB_A, "lookup-asm.amigalib.a") + AC_SUBST(LOOKUP_ASM_LOOKUP_ASM_AMIGALIB_A, + "lookup-asm/lookup-asm.amigalib.a") +]) AC_DEFINE_UNQUOTED(PLUGIN_SUFFIX, "$PLUGIN_SUFFIX", [Suffix for plugins]) AS_IF([test x"$enable_files" != x"no" -a x"$PLUGIN_SUFFIX" != x""], [ AC_SUBST(USE_SRCS_PLUGINS, '${SRCS_PLUGINS}') AC_SUBST(TESTPLUGIN, "plugin") @@ -415,10 +432,14 @@ TESTPLUGIN_LIBS="-L../../src -L../../src/runtime" TESTPLUGIN_LIBS="$TESTPLUGIN_LIBS -lobjfw \${RUNTIME_LIBS}" ]) AC_SUBST(TESTPLUGIN_LIBS) ]) + +AS_IF([test x"$enable_amiga_lib" != x"no"], [ + AC_SUBST(LINKLIB, linklib) +]) AC_MSG_CHECKING(whether we need -D_GNU_SOURCE) AC_EGREP_CPP(egrep_cpp_yes, [ #include @@ -531,11 +552,10 @@ AC_SUBST(RUNTIME_FRAMEWORK_LIBS, "-framework ObjFWRT") ]) AS_IF([test x"$enable_amiga_lib" != x"no"], [ AC_SUBST(RUNTIME_LIBS, "-lobjfwrt.library") - AC_SUBST(LINKLIB, linklib) tmp="../src/runtime/linklib/libobjfwrt.library.a" AC_SUBST(LIBOBJFWRT_DEP, "$tmp") AC_SUBST(LIBOBJFWRT_DEP_LVL2, "../$tmp") ], [ AC_SUBST(RUNTIME_LIBS, "-lobjfwrt") @@ -887,10 +907,15 @@ AC_SUBST(ENCODINGS_ENCODINGS_LIB_A, "encodings/encodings.lib.a") ]) AS_IF([test x"$enable_static" = x"yes" -o x"$enable_shared" = x"no"], [ AC_SUBST(ENCODINGS_A, "encodings.a") AC_SUBST(ENCODINGS_ENCODINGS_A, "encodings/encodings.a") + ]) + AS_IF([test x"$enable_amiga_lib" != x"no"], [ + AC_SUBST(ENCODINGS_AMIGALIB_A, "encodings.amigalib.a") + AC_SUBST(ENCODINGS_ENCODINGS_AMIGALIB_A, + "encodings/encodings.amigalib.a") ]) ]) AC_CHECK_FUNC(arc4random, [ AC_DEFINE(OF_HAVE_ARC4RANDOM, 1, [Whether we have arc4random()]) Index: extra.mk.in ================================================================== --- extra.mk.in +++ extra.mk.in @@ -1,8 +1,9 @@ OBJFW_SHARED_LIB = @OBJFW_SHARED_LIB@ OBJFW_STATIC_LIB = @OBJFW_STATIC_LIB@ OBJFW_FRAMEWORK = @OBJFW_FRAMEWORK@ +OBJFW_AMIGA_LIB = @OBJFW_AMIGA_LIB@ # When changing: Be sure to also change these in the Xcode project! OBJFW_LIB_MAJOR = 9 OBJFW_LIB_MINOR = 1 OBJFW_LIB_MAJOR_MINOR = ${OBJFW_LIB_MAJOR}.${OBJFW_LIB_MINOR} @@ -22,37 +23,47 @@ BIN_PREFIX = @BIN_PREFIX@ BRIDGE = @BRIDGE@ CVINCLUDE_INLINE_H = @CVINCLUDE_INLINE_H@ ENCODINGS = @ENCODINGS@ ENCODINGS_A = @ENCODINGS_A@ +ENCODINGS_AMIGALIB_A = @ENCODINGS_AMIGALIB_A@ ENCODINGS_ENCODINGS_A = @ENCODINGS_ENCODINGS_A@ +ENCODINGS_ENCODINGS_AMIGALIB_A = @ENCODINGS_ENCODINGS_AMIGALIB_A@ ENCODINGS_ENCODINGS_LIB_A = @ENCODINGS_ENCODINGS_LIB_A@ ENCODINGS_LIB_A = @ENCODINGS_LIB_A@ ENCODINGS_SRCS = @ENCODINGS_SRCS@ EXCEPTIONS_A = @EXCEPTIONS_A@ +EXCEPTIONS_AMIGALIB_A = @EXCEPTIONS_AMIGALIB_A@ EXCEPTIONS_EXCEPTIONS_A = @EXCEPTIONS_EXCEPTIONS_A@ +EXCEPTIONS_EXCEPTIONS_AMIGALIB_A = @EXCEPTIONS_EXCEPTIONS_AMIGALIB_A@ EXCEPTIONS_EXCEPTIONS_LIB_A = @EXCEPTIONS_EXCEPTIONS_LIB_A@ EXCEPTIONS_LIB_A = @EXCEPTIONS_LIB_A@ FISH_COMPLETIONS = @FISH_COMPLETIONS@ FORWARDING_A = @FORWARDING_A@ +FORWARDING_AMIGALIB_A = @FORWARDING_AMIGALIB_A@ FORWARDING_FORWARDING_A = @FORWARDING_FORWARDING_A@ +FORWARDING_FORWARDING_AMIGALIB_A = @FORWARDING_FORWARDING_AMIGALIB_A@ FORWARDING_FORWARDING_LIB_A = @FORWARDING_FORWARDING_LIB_A@ FORWARDING_LIB_A = @FORWARDING_LIB_A@ INSTANCE_M = @INSTANCE_M@ INVOCATION_A = @INVOCATION_A@ +INVOCATION_AMIGALIB_A = @INVOCATION_AMIGALIB_A@ INVOCATION_INVOCATION_A = @INVOCATION_INVOCATION_A@ +INVOCATION_INVOCATION_AMIGALIB_A = @INVOCATION_INVOCATION_AMIGALIB_A@ INVOCATION_INVOCATION_LIB_A = @INVOCATION_INVOCATION_LIB_A@ INVOCATION_LIB_A = @INVOCATION_LIB_A@ LIBBASES_M = @LIBBASES_M@ LIBOBJFWRT_DEP = @LIBOBJFWRT_DEP@ LIBOBJFWRT_DEP_LVL2 = @LIBOBJFWRT_DEP_LVL2@ LIBOBJFW_DEP = @LIBOBJFW_DEP@ LIBOBJFW_DEP_LVL2 = @LIBOBJFW_DEP_LVL2@ LINKLIB = @LINKLIB@ LOOKUP_ASM_A = @LOOKUP_ASM_A@ +LOOKUP_ASM_AMIGALIB_A = @LOOKUP_ASM_AMIGALIB_A@ LOOKUP_ASM_LIB_A = @LOOKUP_ASM_LIB_A@ LOOKUP_ASM_LOOKUP_ASM_A = @LOOKUP_ASM_LOOKUP_ASM_A@ +LOOKUP_ASM_LOOKUP_ASM_AMIGALIB_A = @LOOKUP_ASM_LOOKUP_ASM_AMIGALIB_A@ LOOKUP_ASM_LOOKUP_ASM_LIB_A = @LOOKUP_ASM_LOOKUP_ASM_LIB_A@ MAP_LDFLAGS = @MAP_LDFLAGS@ OFARC = @OFARC@ OFDNS = @OFDNS@ OFHASH = @OFHASH@ Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -5,10 +5,11 @@ DISTCLEAN = objfw-defs.h SHARED_LIB = ${OBJFW_SHARED_LIB} STATIC_LIB = ${OBJFW_STATIC_LIB} FRAMEWORK = ${OBJFW_FRAMEWORK} +AMIGA_LIB = ${OBJFW_AMIGA_LIB} LIB_MAJOR = ${OBJFW_LIB_MAJOR} LIB_MINOR = ${OBJFW_LIB_MINOR} SRCS = OFASN1BitString.m \ OFASN1Boolean.m \ @@ -236,15 +237,27 @@ LIB_OBJS_EXTRA = ${RUNTIME_RUNTIME_LIB_A} \ ${EXCEPTIONS_EXCEPTIONS_LIB_A} \ ${ENCODINGS_ENCODINGS_LIB_A} \ ${FORWARDING_FORWARDING_LIB_A} \ ${INVOCATION_INVOCATION_LIB_A} +AMIGA_LIB_OBJS_START = amiga-library.amigalib.o +AMIGA_LIB_OBJS_EXTRA = amiga-glue.amigalib.o \ + ${EXCEPTIONS_EXCEPTIONS_AMIGALIB_A} \ + ${ENCODINGS_ENCODINGS_AMIGALIB_A} \ + ${FORWARDING_FORWARDING_AMIGALIB_A} \ + ${INVOCATION_INVOCATION_AMIGALIB_A} \ + amiga-end.amigalib.o include ../buildsys.mk -CPPFLAGS += -I. -I.. -Iexceptions -Iruntime +CPPFLAGS += -I. -I.. -Iexceptions -Iruntime \ + -DOBJFW_AMIGA_LIB=\"${OBJFW_AMIGA_LIB}\" \ + -DOBJFW_LIB_MAJOR=${OBJFW_LIB_MAJOR} \ + -DOBJFW_LIB_MINOR=${OBJFW_LIB_MINOR} +AMIGA_LIB_CFLAGS += -DOF_COMPILING_AMIGA_LIBRARY LD = ${OBJC} FRAMEWORK_LIBS := -Fruntime \ ${RUNTIME_FRAMEWORK_LIBS} \ ${REEXPORT_RUNTIME_FRAMEWORK} \ ${LIBS} -LIBS := -Lruntime ${RUNTIME_LIBS} ${REEXPORT_RUNTIME} ${LIBS} +LIBS := -Lruntime -Lruntime/linklib ${RUNTIME_LIBS} ${REEXPORT_RUNTIME} ${LIBS} +AMIGA_LIB_LIBS = ${LIBS} ADDED src/amiga-end.m Index: src/amiga-end.m ================================================================== --- src/amiga-end.m +++ src/amiga-end.m @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018, 2019, 2020 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "platform.h" + +#ifdef OF_MORPHOS +__asm__ ( + ".section .ctors, \"aw\", @progbits\n" + " .long 0\n" +); +#else +__asm__ ( + "" +); +#endif ADDED src/amiga-glue.m Index: src/amiga-glue.m ================================================================== --- src/amiga-glue.m +++ src/amiga-glue.m @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018, 2019, 2020 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "amiga-library.h" +#import "platform.h" + +#ifdef OF_AMIGAOS_M68K +# define PPC_PARAMS(...) (void) +# define M68K_ARG OF_M68K_ARG +#else +# define PPC_PARAMS(...) (__VA_ARGS__) +# define M68K_ARG(...) +#endif + +#ifdef OF_MORPHOS +/* All __saveds functions in this file need to use the SysV ABI */ +__asm__ ( + ".section .text\n" + ".align 2\n" + "__restore_r13:\n" + " lwz %r13, 44(%r12)\n" + " blr\n" +); +#endif + +bool __saveds +glue_of_init PPC_PARAMS(unsigned int version, struct of_libc *libc, + FILE *stderr_) +{ + M68K_ARG(unsigned int, version, d0) + M68K_ARG(struct objc_libc *, libc, a0) + M68K_ARG(FILE *, stderr_, a1) + + return of_init(version, libc, stderr_); +} ADDED src/amiga-library.h Index: src/amiga-library.h ================================================================== --- src/amiga-library.h +++ src/amiga-library.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018, 2019, 2020 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "macros.h" + +#if defined(OF_COMPILING_AMIGA_LIBRARY) || defined(OF_COMPILING_AMIGA_LINKLIB) +struct of_libc { + void *_Nullable (*_Nonnull malloc)(size_t); + void *_Nullable (*_Nonnull calloc)(size_t, size_t); + void *_Nullable (*_Nonnull realloc)(void *_Nullable, size_t); + void (*_Nonnull free)(void *_Nullable); + int (*_Nonnull vfprintf)(FILE *_Nonnull restrict, + const char *restrict _Nonnull, va_list); + int (*_Nonnull vsnprintf)(const char *_Nonnull restrict, size_t, + const char *_Nonnull restrict, va_list); + void (*_Nonnull exit)(int); + void (*_Nonnull abort)(void); + char *_Nullable (*_Nonnull setlocale)(int, const char *_Nullable); + void (*_Nonnull __register_frame_info)(const void *_Nonnull, + void *_Nonnull); + void *_Nullable (*_Nonnull __deregister_frame_info)( + const void *_Nonnull); +}; +#endif ADDED src/amiga-library.m Index: src/amiga-library.m ================================================================== --- src/amiga-library.m +++ src/amiga-library.m @@ -0,0 +1,482 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018, 2019, 2020 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#include +#include +#include +#include + +#import "amiga-library.h" +#import "macros.h" + +#define CONCAT_VERSION2(major, minor) #major "." #minor +#define CONCAT_VERSION(major, minor) CONCAT_VERSION2(major, minor) +#define VERSION_STRING CONCAT_VERSION(OBJFW_LIB_MAJOR, OBJFW_LIB_MINOR) + +#if defined(OF_AMIGAOS_M68K) +# define DATA_OFFSET 0x7FFE +#elif defined(OF_MORPHOS) +# define DATA_OFFSET 0x8000 +#endif + +#ifdef OF_AMIGAOS_M68K +# define OF_M68K_REG(reg) __asm__(#reg) +#else +# define OF_M68K_REG(reg) +#endif + +/* This always needs to be the first thing in the file. */ +int +_start() +{ + return -1; +} + +struct ObjFWBase { + struct Library library; + void *segList; + struct ObjFWBase *parent; + char *dataSeg; + bool initialized; +}; + +#ifdef OF_AMIGAOS_M68K +extern uintptr_t __CTOR_LIST__[]; +extern const void *_EH_FRAME_BEGINS__; +extern void *_EH_FRAME_OBJECTS__; +#endif + +extern bool glue_of_init(void); + +#ifdef OF_MORPHOS +const ULONG __abox__ = 1; +#endif +struct ExecBase *SysBase; +struct of_libc libc; +FILE *stderr; + +#if defined(OF_AMIGAOS_M68K) +__asm__ ( + ".text\n" + ".globl ___restore_a4\n" + ".align 1\n" + "___restore_a4:\n" + " movea.l 42(a6), a4\n" + " rts" +); +#elif defined(OF_MORPHOS) +/* All __saveds functions in this file need to use the M68K ABI */ +__asm__ ( + ".section .text\n" + ".align 2\n" + "__restore_r13:\n" + " lwz %r13, 56(%r2)\n" + " lwz %r13, 44(%r13)\n" + " blr\n" +); +#endif + +static OF_INLINE char * +getDataSeg(void) +{ + char *dataSeg; + +#if defined(OF_AMIGAOS_M68K) + __asm__ ( + "move.l #___a4_init, %0" + : "=r"(dataSeg) + ); +#elif defined(OF_MORPHOS) + __asm__ ( + "lis %0, __r13_init@ha\n\t" + "la %0, __r13_init@l(%0)" + : "=r"(dataSeg) + ); +#endif + + return dataSeg; +} + +static OF_INLINE size_t +getDataSize(void) +{ + size_t dataSize; + +#if defined(OF_AMIGAOS_M68K) + __asm__ ( + "move.l #___data_size, %0\n\t" + "add.l #___bss_size, %0" + : "=r"(dataSize) + ); +#elif defined(OF_MORPHOS) + __asm__ ( + "lis %0, __sdata_size@ha\n\t" + "la %0, __sdata_size@l(%0)\n\t" + "lis %%r9, __sbss_size@ha\n\t" + "la %%r9, __sbss_size@l(%%r9)\n\t" + "add %0, %0, %%r9" + : "=r"(dataSize) + :: "r9" + ); +#endif + + return dataSize; +} + +static OF_INLINE size_t * +getDataDataRelocs(void) +{ + size_t *dataDataRelocs; + +#if defined(OF_AMIGAOS_M68K) + __asm__ ( + "move.l #___datadata_relocs, %0" + : "=r"(dataDataRelocs) + ); +#elif defined(OF_MORPHOS) + __asm__ ( + "lis %0, __datadata_relocs@ha\n\t" + "la %0, __datadata_relocs@l(%0)\n\t" + : "=r"(dataDataRelocs) + ); +#endif + + return dataDataRelocs; +} + +static struct Library * +lib_init(struct ObjFWBase *base OF_M68K_REG(d0), void *segList OF_M68K_REG(a0), + struct ExecBase *sysBase OF_M68K_REG(a6)) +{ +#if defined(OF_AMIGAOS_M68K) + __asm__ __volatile__ ( + "move.l a6, _SysBase" + :: "a"(sysBase) + ); +#elif defined(OF_MORPHOS) + __asm__ __volatile__ ( + "lis %%r9, SysBase@ha\n\t" + "stw %0, SysBase@l(%%r9)" + :: "r"(sysBase) : "r9" + ); +#endif + + base->segList = segList; + base->parent = NULL; + base->dataSeg = getDataSeg(); + + return &base->library; +} + +struct Library *__saveds +lib_open(void) +{ + OF_M68K_ARG(struct ObjFWBase *, base, a6) + + struct ObjFWBase *child; + size_t dataSize, *dataDataRelocs; + ptrdiff_t displacement; + + if (base->parent != NULL) + return NULL; + + base->library.lib_OpenCnt++; + base->library.lib_Flags &= ~LIBF_DELEXP; + + /* + * We cannot use malloc here, as that depends on the libc passed from + * the application. + */ + if ((child = AllocMem(base->library.lib_NegSize + + base->library.lib_PosSize, MEMF_ANY)) == NULL) { + base->library.lib_OpenCnt--; + return NULL; + } + + memcpy(child, (char *)base - base->library.lib_NegSize, + base->library.lib_NegSize + base->library.lib_PosSize); + + child = (struct ObjFWBase *)((char *)child + base->library.lib_NegSize); + child->library.lib_OpenCnt = 1; + child->parent = base; + + dataSize = getDataSize(); + + if ((child->dataSeg = AllocMem(dataSize, MEMF_ANY)) == NULL) { + FreeMem((char *)child - child->library.lib_NegSize, + child->library.lib_NegSize + child->library.lib_PosSize); + base->library.lib_OpenCnt--; + return NULL; + } + + memcpy(child->dataSeg, base->dataSeg - DATA_OFFSET, dataSize); + + dataDataRelocs = getDataDataRelocs(); + displacement = child->dataSeg - (base->dataSeg - DATA_OFFSET); + + for (size_t i = 1; i <= dataDataRelocs[0]; i++) + *(long *)(child->dataSeg + dataDataRelocs[i]) += displacement; + + child->dataSeg += DATA_OFFSET; + + return &child->library; +} + +static void * +expunge(struct ObjFWBase *base) +{ + void *segList; + + if (base->parent != NULL) { + base->parent->library.lib_Flags |= LIBF_DELEXP; + return 0; + } + + if (base->library.lib_OpenCnt > 0) { + base->library.lib_Flags |= LIBF_DELEXP; + return 0; + } + + segList = base->segList; + + Remove(&base->library.lib_Node); + FreeMem((char *)base - base->library.lib_NegSize, + base->library.lib_NegSize + base->library.lib_PosSize); + + return segList; +} + +static void *__saveds +lib_expunge(void) +{ + OF_M68K_ARG(struct ObjFWBase *, base, a6) + + return expunge(base); +} + +static void *__saveds +lib_close(void) +{ + OF_M68K_ARG(struct ObjFWBase *, base, a6) + + if (base->parent != NULL) { + struct ObjFWBase *parent = base->parent; + + FreeMem(base->dataSeg - DATA_OFFSET, getDataSize()); + FreeMem((char *)base - base->library.lib_NegSize, + base->library.lib_NegSize + base->library.lib_PosSize); + + base = parent; + } + + if (--base->library.lib_OpenCnt == 0 && + (base->library.lib_Flags & LIBF_DELEXP)) + return expunge(base); + + return NULL; +} + +static void * +lib_null(void) +{ + return NULL; +} + +bool +of_init(unsigned int version, struct of_libc *libc_, FILE *stderr_) +{ +#ifdef OF_AMIGAOS_M68K + OF_M68K_ARG(struct ObjFWBase *, base, a6) +#else + register struct ObjFWBase *r12 __asm__("r12"); + struct ObjFWBase *base = r12; +#endif + uintptr_t *iter, *iter0; + + if (version > 1) + return false; + + if (base->initialized) + return true; + + memcpy(&libc, libc_, sizeof(libc)); + stderr = stderr_; + +#ifdef OF_AMIGAOS_M68K + if ((size_t)_EH_FRAME_BEGINS__ != (size_t)_EH_FRAME_OBJECTS__) + return false; + + for (size_t i = 1; i <= (size_t)_EH_FRAME_BEGINS__; i++) + libc.__register_frame_info((&_EH_FRAME_BEGINS__)[i], + (&_EH_FRAME_OBJECTS__)[i]); + + iter0 = &__CTOR_LIST__[1]; +#elif defined(OF_MORPHOS) + __asm__ ( + "lis %0, ctors+4@ha\n\t" + "la %0, ctors+4@l(%0)\n\t" + : "=r"(iter0) + ); +#endif + + for (iter = iter0; *iter != 0; iter++); + + while (iter > iter0) { + void (*ctor)(void) = (void (*)(void))*--iter; + ctor(); + } + + base->initialized = true; + + return true; +} + +void * +malloc(size_t size) +{ + return libc.malloc(size); +} + +void * +calloc(size_t count, size_t size) +{ + return libc.calloc(count, size); +} + +void * +realloc(void *ptr, size_t size) +{ + return libc.realloc(ptr, size); +} + +void +free(void *ptr) +{ + libc.free(ptr); +} + +int +fprintf(FILE *restrict stream, const char *restrict fmt, ...) +{ + int ret; + va_list args; + + va_start(args, fmt); + ret = libc.vfprintf(stream, fmt, args); + va_end(args); + + return ret; +} + +int +vsnprintf(char *restrict str, size_t size, const char *restrict fmt, + va_list args) +{ + return libc.vsnprintf(str, size, fmt, args); +} + +void +exit(int status) +{ + libc.exit(status); + + OF_UNREACHABLE +} + +void +abort(void) +{ + libc.abort(); + + OF_UNREACHABLE +} + +char * +setlocale(int category, const char *locale) +{ + return libc.setlocale(category, locale); +} + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" +static CONST_APTR functionTable[] = { +#ifdef OF_MORPHOS + (CONST_APTR)FUNCARRAY_BEGIN, + (CONST_APTR)FUNCARRAY_32BIT_NATIVE, +#endif + (CONST_APTR)lib_open, + (CONST_APTR)lib_close, + (CONST_APTR)lib_expunge, + (CONST_APTR)lib_null, +#ifdef OF_MORPHOS + (CONST_APTR)-1, + (CONST_APTR)FUNCARRAY_32BIT_SYSTEMV, +#endif + (CONST_APTR)glue_of_init, +#ifdef OF_MORPHOS + (CONST_APTR)FUNCARRAY_END +#endif +}; +#pragma GCC diagnostic pop + +static struct { + ULONG dataSize; + CONST_APTR *functionTable; + ULONG *dataTable; + struct Library *(*initFunc)( + struct ObjFWBase *base OF_M68K_REG(d0), + void *segList OF_M68K_REG(a0), + struct ExecBase *execBase OF_M68K_REG(a6)); +} init_table = { + sizeof(struct ObjFWBase), + functionTable, + NULL, + lib_init +}; + +struct Resident resident = { + .rt_MatchWord = RTC_MATCHWORD, + .rt_MatchTag = &resident, + .rt_EndSkip = &resident + 1, + .rt_Flags = RTF_AUTOINIT +#ifdef OF_MORPHOS + | RTF_PPC | RTF_EXTENDED +#endif + , + .rt_Version = OBJFW_LIB_MAJOR, + .rt_Type = NT_LIBRARY, + .rt_Pri = 0, + .rt_Name = (char *)OBJFW_AMIGA_LIB, + .rt_IdString = (char *)"ObjFW " VERSION_STRING + " \xA9 2008-2020 Jonathan Schleifer", + .rt_Init = &init_table, +#ifdef OF_MORPHOS + .rt_Revision = OBJFW_LIB_MINOR, + .rt_Tags = NULL, +#endif +}; + +#ifdef OF_MORPHOS +__asm__ ( + ".section .ctors, \"aw\", @progbits\n" + "ctors:\n" + " .long -1\n" + ".section .text" +); +#endif Index: src/encodings/Makefile ================================================================== --- src/encodings/Makefile +++ src/encodings/Makefile @@ -1,10 +1,11 @@ include ../../extra.mk STATIC_PIC_LIB_NOINST = ${ENCODINGS_LIB_A} STATIC_LIB_NOINST = ${ENCODINGS_A} +STATIC_AMIGA_LIB_NOINST = ${ENCODINGS_AMIGALIB_A} SRCS = ${ENCODINGS_SRCS} include ../../buildsys.mk CPPFLAGS += -I. -I.. -I../.. -I../runtime -I../exceptions Index: src/exceptions/Makefile ================================================================== --- src/exceptions/Makefile +++ src/exceptions/Makefile @@ -1,9 +1,10 @@ include ../../extra.mk STATIC_PIC_LIB_NOINST = ${EXCEPTIONS_LIB_A} STATIC_LIB_NOINST = ${EXCEPTIONS_A} +STATIC_AMIGA_LIB_NOINST = ${EXCEPTIONS_AMIGALIB_A} SRCS = OFAllocFailedException.m \ OFChangeCurrentDirectoryPathFailedException.m \ OFChecksumMismatchException.m \ OFCopyItemFailedException.m \ Index: src/forwarding/Makefile ================================================================== --- src/forwarding/Makefile +++ src/forwarding/Makefile @@ -1,10 +1,11 @@ include ../../extra.mk STATIC_PIC_LIB_NOINST = ${FORWARDING_LIB_A} STATIC_LIB_NOINST = ${FORWARDING_A} +STATIC_AMIGA_LIB_NOINST = ${FORWARDING_AMIGALIB_A} SRCS = forwarding.S include ../../buildsys.mk ASFLAGS += -I../.. -I.. Index: src/invocation/Makefile ================================================================== --- src/invocation/Makefile +++ src/invocation/Makefile @@ -1,12 +1,13 @@ include ../../extra.mk STATIC_PIC_LIB_NOINST = ${INVOCATION_LIB_A} STATIC_LIB_NOINST = ${INVOCATION_A} +STATIC_AMIGA_LIB_NOINST = ${INVOCATION_AMIGALIB_A} SRCS = call.S \ invoke.m include ../../buildsys.mk ASFLAGS += -I../.. -I.. OBJCFLAGS += -I../.. -I.. -I../exceptions -I../runtime Index: src/macros.h ================================================================== --- src/macros.h +++ src/macros.h @@ -306,10 +306,23 @@ #if __has_attribute(__swift_name__) # define OF_SWIFT_NAME(name) __attribute__((__swift_name__(name))) #else # define OF_SWIFT_NAME(name) #endif + +#ifdef OF_COMPILING_AMIGA_LIBRARY +# if defined(__MORPHOS__) +# include +# define OF_M68K_ARG(type, name, reg) type name = (type)REG_##reg; +# else +# define OF_M68K_ARG(type, name, reg) \ + register type reg_##name __asm__(#reg); \ + type name = reg_##name; +# endif +# undef stderr +extern FILE *stderr; +#endif #ifdef __GNUC__ # ifdef OF_X86_64 # define OF_X86_64_ASM # endif Index: src/runtime/Makefile ================================================================== --- src/runtime/Makefile +++ src/runtime/Makefile @@ -39,12 +39,12 @@ includesubdir = ObjFWRT OBJS_EXTRA = ${LOOKUP_ASM_LOOKUP_ASM_A} LIB_OBJS_EXTRA = ${LOOKUP_ASM_LOOKUP_ASM_LIB_A} AMIGA_LIB_OBJS_START = amiga-library.amigalib.o -AMIGA_LIB_OBJS_EXTRA = amiga-glue.amigalib.o \ - ${LOOKUP_ASM_LOOKUP_ASM_A} \ +AMIGA_LIB_OBJS_EXTRA = amiga-glue.amigalib.o \ + ${LOOKUP_ASM_LOOKUP_ASM_AMIGALIB_A} \ amiga-end.amigalib.o include ../../buildsys.mk ${OBJFWRT_AMIGA_LIB}: inline.h Index: src/runtime/amiga-library.m ================================================================== --- src/runtime/amiga-library.m +++ src/runtime/amiga-library.m @@ -151,12 +151,11 @@ #ifdef OF_MORPHOS const ULONG __abox__ = 1; #endif struct ExecBase *SysBase; struct objc_libc libc; -FILE *stdout; -FILE *stderr; +FILE *stdout, *stderr; #if defined(OF_AMIGAOS_M68K) __asm__ ( ".text\n" ".globl ___restore_a4\n" @@ -405,10 +404,13 @@ #endif uintptr_t *iter, *iter0; if (version > 1) return false; + + if (base->initialized) + return true; memcpy(&libc, libc_, sizeof(libc)); stdout = stdout_; stderr = stderr_; @@ -707,11 +709,11 @@ .rt_Version = OBJFWRT_LIB_MAJOR, .rt_Type = NT_LIBRARY, .rt_Pri = 0, .rt_Name = (char *)OBJFWRT_AMIGA_LIB, .rt_IdString = (char *)"ObjFWRT " VERSION_STRING - " \xA9 2008-2019 Jonathan Schleifer", + " \xA9 2008-2020 Jonathan Schleifer", .rt_Init = &init_table, #ifdef OF_MORPHOS .rt_Revision = OBJFWRT_LIB_MINOR, .rt_Tags = NULL, #endif Index: src/runtime/lookup-asm/Makefile ================================================================== --- src/runtime/lookup-asm/Makefile +++ src/runtime/lookup-asm/Makefile @@ -1,10 +1,11 @@ include ../../../extra.mk STATIC_PIC_LIB_NOINST = ${LOOKUP_ASM_LIB_A} STATIC_LIB_NOINST = ${LOOKUP_ASM_A} +STATIC_AMIGA_LIB_NOINST = ${LOOKUP_ASM_AMIGALIB_A} SRCS = lookup-asm.S include ../../../buildsys.mk ASFLAGS += -I../../.. -I../.. Index: src/runtime/private.h ================================================================== --- src/runtime/private.h +++ src/runtime/private.h @@ -223,11 +223,12 @@ struct objc_libc { void *_Nullable (*_Nonnull malloc)(size_t); void *_Nullable (*_Nonnull calloc)(size_t, size_t); void *_Nullable (*_Nonnull realloc)(void *_Nullable, size_t); void (*_Nonnull free)(void *_Nullable); - int (*_Nonnull vfprintf)(FILE *_Nonnull, const char *_Nonnull, va_list); + int (*_Nonnull vfprintf)(FILE *_Nonnull restrict, + const char *_Nonnull restrict, va_list); int (*_Nonnull fflush)(FILE *_Nonnull); void (*_Nonnull abort)(void); # ifdef HAVE_SJLJ_EXCEPTIONS int (*_Nonnull _Unwind_SjLj_RaiseException)(void *_Nonnull); # else @@ -248,11 +249,12 @@ # else void (*_Nonnull _Unwind_Resume)(void *_Nonnull); # endif void (*_Nonnull __register_frame_info)(const void *_Nonnull, void *_Nonnull); - void *(*_Nonnull __deregister_frame_info)(const void *_Nonnull); + void *_Nullable (*_Nonnull __deregister_frame_info)( + const void *_Nonnull); }; #endif #ifdef OBJC_COMPILING_AMIGA_LIBRARY # if defined(__MORPHOS__)