diff --git a/Makefile b/Makefile index 5d83efc..e4fc907 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ EXPORT_UNPREFIXED := yes FREESTANDING := no ifeq ($(FREESTANDING),yes) - CFLAGS += -DFREESTANDING -isystem arch/${ARCH}/freestanding + CFLAGS += -DFREESTANDING EXPORT_UNPREFIXED = no endif @@ -31,7 +31,7 @@ endif LIBUCONTEXT_C_SRC = $(wildcard arch/${ARCH}/*.c) LIBUCONTEXT_S_SRC = $(wildcard arch/${ARCH}/*.S) -LIBUCONTEXT_VERSION := 0.13.1 +LIBUCONTEXT_VERSION := $(shell head VERSION) LIBUCONTEXT_OBJ = ${LIBUCONTEXT_C_SRC:.c=.o} ${LIBUCONTEXT_S_SRC:.S=.o} LIBUCONTEXT_SOVERSION = 0 LIBUCONTEXT_NAME = libucontext.so @@ -105,7 +105,7 @@ docs: ${MANPAGES} $(CC) -std=gnu99 -D_BSD_SOURCE -fPIC -DPIC ${CFLAGS} ${CPPFLAGS} -c -o $@ $< .S.o: - $(CC) -fPIC -DPIC -DLIBUCONTEXT_ASSEMBLY ${CFLAGS} ${CPPFLAGS} -c -o $@ $< + $(CC) -fPIC -DPIC ${CFLAGS} ${CPPFLAGS} -c -o $@ $< ${LIBUCONTEXT_NAME}_clean: rm -f ${LIBUCONTEXT_NAME} @@ -199,13 +199,13 @@ examples/cooperative_threading: examples/cooperative_threading.c ${LIBUCONTEXT_N ifeq ($(FREESTANDING),no) -include/libucontext/bits.h: arch/common/bits.h - cp arch/common/bits.h $@ +include/libucontext/bits.h: arch/common/include/libucontext/bits.h + cp $< $@ else -include/libucontext/bits.h: arch/${ARCH}/freestanding/bits.h - cp arch/${ARCH}/freestanding/bits.h $@ +include/libucontext/bits.h: arch/${ARCH}/include/libucontext/bits.h + cp $< $@ endif diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..ed0d9e9 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.13.1 \ No newline at end of file diff --git a/arch/aarch64/defs.h b/arch/aarch64/defs.h index 469e69a..ee9fc7f 100644 --- a/arch/aarch64/defs.h +++ b/arch/aarch64/defs.h @@ -19,7 +19,7 @@ #endif #define FETCH_LINKPTR(dest) \ - asm("mov x0, %0" : "=rm" ((dest))) + asm("mov %0, x19" : "=r" ((dest))) #include "common-defs.h" diff --git a/arch/aarch64/getcontext.S b/arch/aarch64/getcontext.S index 98b659e..1a4911b 100644 --- a/arch/aarch64/getcontext.S +++ b/arch/aarch64/getcontext.S @@ -14,7 +14,11 @@ ALIAS(getcontext, libucontext_getcontext) -FUNC(libucontext_getcontext) + .global PROC_NAME(libucontext_getcontext); + .align 2; + TYPE(libucontext_getcontext) + ENT(libucontext_getcontext) +PROC_NAME(libucontext_getcontext): str xzr, [x0, #REG_OFFSET(0)] /* save GPRs */ diff --git a/arch/aarch64/freestanding/bits.h b/arch/aarch64/include/libucontext/bits.h similarity index 58% rename from arch/aarch64/freestanding/bits.h rename to arch/aarch64/include/libucontext/bits.h index 3d6e8d7..8eb04f5 100644 --- a/arch/aarch64/freestanding/bits.h +++ b/arch/aarch64/include/libucontext/bits.h @@ -1,20 +1,20 @@ #ifndef LIBUCONTEXT_BITS_H #define LIBUCONTEXT_BITS_H -typedef unsigned long libucontext_greg_t; -typedef unsigned long libucontext_gregset_t[34]; +typedef unsigned long long int libucontext_greg_t; +typedef unsigned long int libucontext_sigset_t; typedef struct { __uint128_t vregs[32]; - unsigned int fpsr; - unsigned int fpcr; + libucontext_greg_t fpsr; + libucontext_greg_t fpcr; } libucontext_fpregset_t; -typedef struct sigcontext { - unsigned long fault_address; - unsigned long regs[31]; - unsigned long sp, pc, pstate; - long double __reserved[256]; +typedef struct { + libucontext_greg_t fault_address; + libucontext_greg_t regs[31]; + libucontext_greg_t sp, pc, pstate; + double __reserved[256]; } libucontext_mcontext_t; typedef struct { @@ -27,6 +27,7 @@ typedef struct libucontext_ucontext { unsigned long uc_flags; struct libucontext_ucontext *uc_link; libucontext_stack_t uc_stack; + libucontext_sigset_t uc_sigmask; unsigned char __pad[128]; libucontext_mcontext_t uc_mcontext; } libucontext_ucontext_t; diff --git a/arch/aarch64/makecontext.c b/arch/aarch64/makecontext.c index fb291c5..de666ee 100644 --- a/arch/aarch64/makecontext.c +++ b/arch/aarch64/makecontext.c @@ -10,29 +10,38 @@ * from the use of this software. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include #include #include #include "defs.h" +#include extern void libucontext_trampoline(void); +_Static_assert(offsetof(libucontext_ucontext_t, uc_mcontext.regs[0]) == R0_OFFSET, "R0_OFFSET is invalid"); +_Static_assert(offsetof(libucontext_ucontext_t, uc_mcontext.sp) == SP_OFFSET, "SP_OFFSET is invalid"); +_Static_assert(offsetof(libucontext_ucontext_t, uc_mcontext.pc) == PC_OFFSET, "PC_OFFSET is invalid"); +_Static_assert(offsetof(libucontext_ucontext_t, uc_mcontext.pstate) == PSTATE_OFFSET, "PSTATE_OFFSET is invalid"); void libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(void), int argc, ...) { - unsigned long *sp; - unsigned long *regp; + // specs require only 'int' arguments so stack arguments are int type + // while register argument take the full width of libucontext_greg_t + int *sp; + libucontext_greg_t *regp; va_list va; int i; - sp = (unsigned long *) ((uintptr_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); + sp = (int *) ((uintptr_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); sp -= argc < 8 ? 0 : argc - 8; - sp = (unsigned long *) (((uintptr_t) sp & -16L)); + sp = (int *) (((uintptr_t) sp & -16L)); ucp->uc_mcontext.sp = (uintptr_t) sp; ucp->uc_mcontext.pc = (uintptr_t) func; @@ -44,13 +53,14 @@ libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(void), int arg regp = &(ucp->uc_mcontext.regs[0]); for (i = 0; (i < argc && i < 8); i++) - *regp++ = va_arg (va, unsigned long); + *regp++ = va_arg (va, int); for (; i < argc; i++) - *sp++ = va_arg (va, unsigned long); + *sp++ = va_arg (va, int); va_end(va); } - +#ifdef EXPORT_UNPREFIXED extern __typeof(libucontext_makecontext) makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +#endif diff --git a/arch/aarch64/setcontext.S b/arch/aarch64/setcontext.S index d7c8d8c..70b9573 100644 --- a/arch/aarch64/setcontext.S +++ b/arch/aarch64/setcontext.S @@ -14,7 +14,11 @@ ALIAS(setcontext, libucontext_setcontext) -FUNC(libucontext_setcontext) + .global PROC_NAME(libucontext_setcontext); + .align 2; + TYPE(libucontext_setcontext) + ENT(libucontext_setcontext) +PROC_NAME(libucontext_setcontext): /* restore GPRs */ ldp x18, x19, [x0, #REG_OFFSET(18)] ldp x20, x21, [x0, #REG_OFFSET(20)] diff --git a/arch/aarch64/swapcontext.S b/arch/aarch64/swapcontext.S index 17a0b99..2af525e 100644 --- a/arch/aarch64/swapcontext.S +++ b/arch/aarch64/swapcontext.S @@ -14,7 +14,11 @@ ALIAS(swapcontext, libucontext_swapcontext) -FUNC(libucontext_swapcontext) + .global PROC_NAME(libucontext_swapcontext); + .align 2; + TYPE(libucontext_swapcontext) + ENT(libucontext_swapcontext) +PROC_NAME(libucontext_swapcontext): str xzr, [x0, #REG_OFFSET(0)] /* save GPRs */ @@ -50,7 +54,7 @@ FUNC(libucontext_swapcontext) /* move x1 to x0 and call setcontext */ mov x0, x1 - bl libucontext_setcontext + bl PROC_NAME(libucontext_setcontext) /* hmm, we came back here try to return */ mov x30, x28 diff --git a/arch/aarch64/trampoline.c b/arch/aarch64/trampoline.c index 83eb453..699a050 100644 --- a/arch/aarch64/trampoline.c +++ b/arch/aarch64/trampoline.c @@ -1,2 +1,3 @@ #include "defs.h" +#include #include "common-trampoline.c" diff --git a/arch/arm/defs.h b/arch/arm/defs.h index 0834d9e..3b6b9ba 100644 --- a/arch/arm/defs.h +++ b/arch/arm/defs.h @@ -6,7 +6,7 @@ #define TYPE(__proc) #define FETCH_LINKPTR(dest) \ - asm("movs r0, %0" : "=rm" ((dest))) + asm("movs %0, r4" : "=r" ((dest))) #include "common-defs.h" diff --git a/arch/arm/freestanding/bits.h b/arch/arm/include/libucontext/bits.h similarity index 100% rename from arch/arm/freestanding/bits.h rename to arch/arm/include/libucontext/bits.h diff --git a/arch/arm/makecontext.c b/arch/arm/makecontext.c index 9d1ba8d..fd46df1 100644 --- a/arch/arm/makecontext.c +++ b/arch/arm/makecontext.c @@ -10,7 +10,9 @@ * from the use of this software. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include @@ -19,6 +21,7 @@ #include "defs.h" +#include extern void libucontext_trampoline(void); @@ -56,5 +59,6 @@ libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(void), int arg va_end(va); } - +#ifdef EXPORT_UNPREFIXED extern __typeof(libucontext_makecontext) makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +#endif diff --git a/arch/arm/trampoline.c b/arch/arm/trampoline.c index 83eb453..699a050 100644 --- a/arch/arm/trampoline.c +++ b/arch/arm/trampoline.c @@ -1,2 +1,3 @@ #include "defs.h" +#include #include "common-trampoline.c" diff --git a/arch/common/common-defs.h b/arch/common/common-defs.h index 8a5f12d..8ba97bf 100644 --- a/arch/common/common-defs.h +++ b/arch/common/common-defs.h @@ -18,15 +18,27 @@ #endif #ifndef TYPE -# define TYPE(__proc) .type __proc, @function; +# ifdef __clang__ +# define TYPE(__proc) // .type not supported +# else +# define TYPE(__proc) .type __proc, @function; +#endif +#endif + +#ifndef PROC_NAME +# ifdef __MACH__ +# define PROC_NAME(__proc) _ ## __proc +# else +# define PROC_NAME(__proc) __proc +# endif #endif #define FUNC(__proc) \ - .global __proc; \ + .global PROC_NAME(__proc); \ .align 2; \ TYPE(__proc) \ ENT(__proc) \ -__proc: \ +PROC_NAME(__proc): \ SETUP_FRAME(__proc) #ifdef __clang__ #define END(__proc) @@ -46,10 +58,4 @@ __proc: \ #define REG_OFFSET(__reg) (MCONTEXT_GREGS + ((__reg) * REG_SZ)) -#ifndef LIBUCONTEXT_ASSEMBLY - -#include - -#endif - #endif diff --git a/arch/common/common-trampoline.c b/arch/common/common-trampoline.c index f182f99..dc5f2d3 100644 --- a/arch/common/common-trampoline.c +++ b/arch/common/common-trampoline.c @@ -19,6 +19,7 @@ libucontext_trampoline(void) { libucontext_ucontext_t *uc_link; + // FIXME: there's no guarantee that input is not clobbered! FETCH_LINKPTR(uc_link); if (uc_link == NULL) diff --git a/arch/common/bits.h b/arch/common/include/libucontext/bits.h similarity index 100% rename from arch/common/bits.h rename to arch/common/include/libucontext/bits.h diff --git a/arch/m68k/defs.h b/arch/m68k/defs.h index 6a00651..fc19252 100644 --- a/arch/m68k/defs.h +++ b/arch/m68k/defs.h @@ -27,7 +27,7 @@ #define PC_OFFSET REG_OFFSET(REG_PC) #define FETCH_LINKPTR(dest) \ - asm("mov.l (%%sp, %%d7.l * 4), %0" : "=rm" ((dest))) + asm("mov.l (%%sp, %%d7.l * 4), %0" :: "r" ((dest))) #include "common-defs.h" diff --git a/arch/m68k/freestanding/bits.h b/arch/m68k/include/libucontext/bits.h similarity index 100% rename from arch/m68k/freestanding/bits.h rename to arch/m68k/include/libucontext/bits.h diff --git a/arch/m68k/makecontext.c b/arch/m68k/makecontext.c index 2cbc0f7..fb380f1 100644 --- a/arch/m68k/makecontext.c +++ b/arch/m68k/makecontext.c @@ -10,13 +10,16 @@ * from the use of this software. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include #include #include #include "defs.h" +#include extern void libucontext_trampoline(void); @@ -55,5 +58,6 @@ libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(void), int arg *sp++ = (libucontext_greg_t) ucp->uc_link; } - +#ifdef EXPORT_UNPREFIXED extern __typeof(libucontext_makecontext) makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +#endif diff --git a/arch/m68k/trampoline.c b/arch/m68k/trampoline.c index 83eb453..699a050 100644 --- a/arch/m68k/trampoline.c +++ b/arch/m68k/trampoline.c @@ -1,2 +1,3 @@ #include "defs.h" +#include #include "common-trampoline.c" diff --git a/arch/mips/freestanding/bits.h b/arch/mips/include/libucontext/bits.h similarity index 100% rename from arch/mips/freestanding/bits.h rename to arch/mips/include/libucontext/bits.h diff --git a/arch/mips64/freestanding/bits.h b/arch/mips64/include/libucontext/bits.h similarity index 100% rename from arch/mips64/freestanding/bits.h rename to arch/mips64/include/libucontext/bits.h diff --git a/arch/ppc/makecontext.c b/arch/ppc/makecontext.c index cc2a882..f8ed384 100644 --- a/arch/ppc/makecontext.c +++ b/arch/ppc/makecontext.c @@ -11,12 +11,15 @@ * from the use of this software. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include "defs.h" +#include extern void libucontext_trampoline(void); @@ -55,5 +58,6 @@ libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(), int argc, . va_end(va); } - +#ifdef EXPORT_UNPREFIXED extern __typeof(libucontext_makecontext) makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +#endif diff --git a/arch/ppc64/makecontext.c b/arch/ppc64/makecontext.c index 9a9d8b2..e42d66a 100644 --- a/arch/ppc64/makecontext.c +++ b/arch/ppc64/makecontext.c @@ -11,12 +11,15 @@ * from the use of this software. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include "defs.h" +#include extern void libucontext_trampoline(void); @@ -56,5 +59,6 @@ libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(), int argc, . va_end(va); } - +#ifdef EXPORT_UNPREFIXED extern __typeof(libucontext_makecontext) makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +#endif diff --git a/arch/riscv64/defs.h b/arch/riscv64/defs.h index 191e8c2..d9b7647 100644 --- a/arch/riscv64/defs.h +++ b/arch/riscv64/defs.h @@ -48,7 +48,7 @@ #define PC_OFFSET REG_OFFSET(REG_PC) #define FETCH_LINKPTR(dest) \ - asm("mv %0, s1" : "=rm" ((dest))) + asm("mv %0, s1" : "=r" ((dest))) #include "common-defs.h" diff --git a/arch/riscv64/freestanding/bits.h b/arch/riscv64/include/libucontext/bits.h similarity index 100% rename from arch/riscv64/freestanding/bits.h rename to arch/riscv64/include/libucontext/bits.h diff --git a/arch/riscv64/makecontext.c b/arch/riscv64/makecontext.c index d8609f8..aee671c 100644 --- a/arch/riscv64/makecontext.c +++ b/arch/riscv64/makecontext.c @@ -10,13 +10,16 @@ * from the use of this software. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include #include #include #include "defs.h" +#include extern void libucontext_trampoline(void); @@ -56,5 +59,6 @@ libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(void), int arg va_end(va); } - +#ifdef EXPORT_UNPREFIXED extern __typeof(libucontext_makecontext) makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +#endif diff --git a/arch/riscv64/trampoline.c b/arch/riscv64/trampoline.c index 83eb453..699a050 100644 --- a/arch/riscv64/trampoline.c +++ b/arch/riscv64/trampoline.c @@ -1,2 +1,3 @@ #include "defs.h" +#include #include "common-trampoline.c" diff --git a/arch/s390x/freestanding/bits.h b/arch/s390x/include/libucontext/bits.h similarity index 100% rename from arch/s390x/freestanding/bits.h rename to arch/s390x/include/libucontext/bits.h diff --git a/arch/s390x/makecontext.c b/arch/s390x/makecontext.c index e74c6f3..f970da4 100644 --- a/arch/s390x/makecontext.c +++ b/arch/s390x/makecontext.c @@ -10,7 +10,9 @@ * from the use of this software. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include @@ -18,6 +20,7 @@ #include "defs.h" +#include extern void libucontext_trampoline(void); @@ -62,5 +65,6 @@ libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(void), int arg ucp->uc_mcontext.gregs[15] = (uintptr_t) sp; } - +#ifdef EXPORT_UNPREFIXED extern __typeof(libucontext_makecontext) makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +#endif diff --git a/arch/sh/defs.h b/arch/sh/defs.h index dd13d47..7cddaef 100644 --- a/arch/sh/defs.h +++ b/arch/sh/defs.h @@ -13,7 +13,7 @@ #define REG_MACL (21) #define FETCH_LINKPTR(dest) \ - asm("mov r8, %0" : "=rm" (dest)); + asm("mov r8, %0" : "=r" (dest)); #include "common-defs.h" diff --git a/arch/sh/freestanding/bits.h b/arch/sh/include/libucontext/bits.h similarity index 100% rename from arch/sh/freestanding/bits.h rename to arch/sh/include/libucontext/bits.h diff --git a/arch/sh/makecontext.c b/arch/sh/makecontext.c index ac0c2b7..9bc4782 100644 --- a/arch/sh/makecontext.c +++ b/arch/sh/makecontext.c @@ -10,13 +10,16 @@ * from the use of this software. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include #include #include #include "defs.h" +#include extern void libucontext_trampoline(void); @@ -53,5 +56,6 @@ libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(void), int arg va_end(va); } - +#ifdef EXPORT_UNPREFIXED extern __typeof(libucontext_makecontext) makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +#endif diff --git a/arch/sh/trampoline.c b/arch/sh/trampoline.c index 83eb453..699a050 100644 --- a/arch/sh/trampoline.c +++ b/arch/sh/trampoline.c @@ -1,2 +1,3 @@ #include "defs.h" +#include #include "common-trampoline.c" diff --git a/arch/x86/defs.h b/arch/x86/defs.h index 2b0c68d..0bf73ba 100644 --- a/arch/x86/defs.h +++ b/arch/x86/defs.h @@ -60,7 +60,7 @@ #define MCONTEXT_GREGS (20) #define FETCH_LINKPTR(dest) \ - asm("movl (%%esp, %%ebx, 4), %0" : "=rm" ((dest))); + asm("movl (%%esp, %%ebx, 4), %0" : "=r" ((dest))); #include "common-defs.h" diff --git a/arch/x86/freestanding/bits.h b/arch/x86/include/libucontext/bits.h similarity index 100% rename from arch/x86/freestanding/bits.h rename to arch/x86/include/libucontext/bits.h diff --git a/arch/x86/makecontext.c b/arch/x86/makecontext.c index fc292f9..8d7fbf5 100644 --- a/arch/x86/makecontext.c +++ b/arch/x86/makecontext.c @@ -11,12 +11,15 @@ * from the use of this software. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include #include #include "defs.h" +#include extern void libucontext_trampoline(void); @@ -53,5 +56,6 @@ libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(void), int arg *argp++ = (uintptr_t) ucp->uc_link; } - +#ifdef EXPORT_UNPREFIXED extern __typeof(libucontext_makecontext) makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +#endif diff --git a/arch/x86/trampoline.c b/arch/x86/trampoline.c index 83eb453..699a050 100644 --- a/arch/x86/trampoline.c +++ b/arch/x86/trampoline.c @@ -1,2 +1,3 @@ #include "defs.h" +#include #include "common-trampoline.c" diff --git a/arch/x86_64/defs.h b/arch/x86_64/defs.h index 4a53f96..c5fd126 100644 --- a/arch/x86_64/defs.h +++ b/arch/x86_64/defs.h @@ -32,7 +32,7 @@ #define REG_SZ (8) #define FETCH_LINKPTR(dest) \ - asm("movq (%%rbx), %0" : "=rm" ((dest))); + asm("movq (%%rbx), %0" : "=r" ((dest))); #include "common-defs.h" diff --git a/arch/x86_64/freestanding/bits.h b/arch/x86_64/include/libucontext/bits.h similarity index 100% rename from arch/x86_64/freestanding/bits.h rename to arch/x86_64/include/libucontext/bits.h diff --git a/arch/x86_64/makecontext.c b/arch/x86_64/makecontext.c index 0550ff6..66b809e 100644 --- a/arch/x86_64/makecontext.c +++ b/arch/x86_64/makecontext.c @@ -10,13 +10,16 @@ * from the use of this software. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include #include #include "defs.h" +#include extern void libucontext_trampoline(void); @@ -73,5 +76,6 @@ libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(void), int arg va_end(va); } - +#ifdef EXPORT_UNPREFIXED extern __typeof(libucontext_makecontext) makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +#endif diff --git a/arch/x86_64/trampoline.c b/arch/x86_64/trampoline.c index 83eb453..699a050 100644 --- a/arch/x86_64/trampoline.c +++ b/arch/x86_64/trampoline.c @@ -1,2 +1,3 @@ #include "defs.h" +#include #include "common-trampoline.c" diff --git a/include/libucontext/libucontext.h b/include/libucontext/libucontext.h index 8207126..037da0e 100644 --- a/include/libucontext/libucontext.h +++ b/include/libucontext/libucontext.h @@ -1,6 +1,7 @@ #ifndef LIBUCONTEXT_LIBUCONTEXT_H #define LIBUCONTEXT_LIBUCONTEXT_H +#include #include int libucontext_getcontext(libucontext_ucontext_t *); diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..e72dfa3 --- /dev/null +++ b/meson.build @@ -0,0 +1,175 @@ +project( + 'libucontext', + 'c', + meson_version : '>=0.55.0', + version : run_command('head', files('VERSION')).stdout() +) + +cpu = host_machine.cpu_family() +if cpu == 'sh4' + cpu = 'sh' +endif + +project_description = 'Portable implementation of ucontext' + +project_headers = [ + 'include/libucontext/libucontext.h' +] + +project_source_files = [ + 'arch' / cpu / 'getcontext.S', + 'arch' / cpu / 'setcontext.S', + 'arch' / cpu / 'swapcontext.S', +] +if cpu in ['mips', 'mips64'] + project_source_files += [ + 'arch' / cpu / 'makecontext.S' + ] +else + project_source_files += [ + 'arch' / cpu / 'makecontext.c' + ] +endif +if cpu in ['ppc', 'ppc64'] + project_source_files += [ + 'arch' / cpu / 'retfromsyscall.c' + ] +endif +if cpu not in ['mips', 'mips64', 'ppc', 'ppc64', 's390x'] + project_source_files += [ + 'arch' / cpu / 'trampoline.c' + ] +endif + +project_includes = [ + 'include', + 'arch/common' +] + +build_args = [ + '-std=gnu99', + '-D_BSD_SOURCE', + '-fPIC', + '-DPIC' +] + + +# =================================================================== + +# ====== +# Options +# ====== + +freestanding = get_option('freestanding') +export_unprefixed = get_option('export_unprefixed') +build_posix = true + +if freestanding + build_args += '-DFREESTANDING' + build_posix = false + export_unprefixed = false + project_headers += ['arch' / cpu / 'include/libucontext/bits.h'] + project_includes += ['arch' / cpu / 'include'] +else + project_headers += ['arch/common/include/libucontext/bits.h'] + project_includes += ['arch/common/include'] +endif + +if export_unprefixed + build_args += '-DEXPORT_UNPREFIXED' +endif + +# ====== +# Target +# ====== + +headers = include_directories(project_includes) +is_subproject = meson.is_subproject() +# Build only static library if subproject + +libucontext_target = both_libraries( + 'ucontext', + project_source_files, + version: meson.project_version(), + install : not is_subproject, + c_args : build_args, + include_directories : headers, +) +if is_subproject + libucontext_target = libucontext_target.get_static_lib() +endif +libucontext_dep = declare_dependency( + include_directories: headers, + link_with : libucontext_target +) + +if build_posix + libucontext_posix_target = both_libraries( + 'ucontext_posix', + project_source_files + ['libucontext_posix.c'], + version: meson.project_version(), + install : not is_subproject, + c_args : build_args, + include_directories : headers, + ) + if is_subproject + libucontext_posix_target = libucontext_posix_target.get_static_lib() + endif + libucontext_posix_dep = declare_dependency( + include_directories: headers, + link_with : libucontext_posix_target + ) +endif + +# ======= +# Project +# ======= + +if not is_subproject + # Make this library usable from the system's + # package manager. + install_headers(project_headers, subdir : meson.project_name()) + + pkg_mod = import('pkgconfig') + pkg_mod.generate( + name : meson.project_name(), + filebase : meson.project_name(), + description : project_description, + subdirs : meson.project_name(), + libraries : libucontext_target, + ) +endif + +# ==== +# Docs +# ==== + +# TODO: meson.build for docs +if not is_subproject + #subdir('docs') +endif + +# ========== +# Unit Tests +# ========== + +if not is_subproject + test('test_libucontext', + executable( + 'test_libucontext', + files('test_libucontext.c'), + dependencies : libucontext_dep, + install : false + ) + ) + if build_posix + test('test_libucontext_posix', + executable( + 'test_libucontext_posix', + files('test_libucontext_posix.c'), + dependencies : libucontext_posix_dep, + install : false + ) + ) + endif +endif diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 0000000..d4201d1 --- /dev/null +++ b/meson_options.txt @@ -0,0 +1,4 @@ +option('freestanding', type : 'boolean', value : false, + description: 'Do not use system headers') +option('export_unprefixed', type : 'boolean', value : true, + description: 'Export POSIX 2004 ucontext names as alises') \ No newline at end of file