From 0ac29b53dc038408b08e0f8515ae6edabc145f76 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Thu, 20 Apr 2017 22:01:50 +0200 Subject: [PATCH] Add support of musl-libc The port is functional. Bound checking is not supported yet. --- Makefile | 1 + configure | 2 ++ include/stdarg.h | 4 ++++ lib/Makefile | 4 ++++ lib/bcheck.c | 2 +- libtcc.c | 4 ++++ tcc.h | 20 ++++++++++++++++---- tccelf.c | 2 +- 8 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 3be01b36..c74d833e 100644 --- a/Makefile +++ b/Makefile @@ -73,6 +73,7 @@ NATIVE_DEFINES_$(CONFIG_i386) += -DTCC_TARGET_I386 NATIVE_DEFINES_$(CONFIG_x86_64) += -DTCC_TARGET_X86_64 NATIVE_DEFINES_$(CONFIG_WIN32) += -DTCC_TARGET_PE NATIVE_DEFINES_$(CONFIG_uClibc) += -DTCC_UCLIBC +NATIVE_DEFINES_$(CONFIG_musl) += -DTCC_MUSL NATIVE_DEFINES_$(CONFIG_arm) += -DTCC_TARGET_ARM NATIVE_DEFINES_$(CONFIG_arm_eabihf) += -DTCC_ARM_EABI -DTCC_ARM_HARDFLOAT NATIVE_DEFINES_$(CONFIG_arm_eabi) += -DTCC_ARM_EABI diff --git a/configure b/configure index d1cb838b..4072082b 100755 --- a/configure +++ b/configure @@ -352,6 +352,8 @@ if test -z "$cross_prefix" ; then if test -f "/lib/ld-uClibc.so.0" ; then confvars="$confvars uClibc" + elif test -f "/lib/ld-musl-$cpu.so.1"; then + confvars="$confvars musl" fi fi else diff --git a/include/stdarg.h b/include/stdarg.h index 10ce733b..9adfc610 100644 --- a/include/stdarg.h +++ b/include/stdarg.h @@ -16,7 +16,11 @@ typedef struct { char *reg_save_area; } __va_list_struct; +/* Avoid conflicting definition for va_list on musl libc */ +#ifndef __DEFINED_va_list typedef __va_list_struct va_list[1]; +#define __DEFINED_va_list +#endif void __va_start(__va_list_struct *ap, void *fp); void *__va_arg(__va_list_struct *ap, int arg_type, int size, int align); diff --git a/lib/Makefile b/lib/Makefile index 3c5b988e..98617434 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -30,6 +30,10 @@ ifeq ($(TARGETOS),Darwin) BCHECK_O = endif +ifeq ($(CONFIG_musl),yes) + BCHECK_O = +endif + I386_O = libtcc1.o alloca86.o alloca86-bt.o $(BCHECK_O) X86_64_O = libtcc1.o alloca86_64.o alloca86_64-bt.o $(BCHECK_O) ARM_O = libtcc1.o armeabi.o alloca-arm.o diff --git a/lib/bcheck.c b/lib/bcheck.c index 9eb012f0..41c08aa2 100644 --- a/lib/bcheck.c +++ b/lib/bcheck.c @@ -48,7 +48,7 @@ #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \ || defined(__DragonFly__) || defined(__dietlibc__) \ || defined(__UCLIBC__) || defined(__OpenBSD__) || defined(__NetBSD__) \ - || defined(_WIN32) || defined(TCC_UCLIBC) + || defined(_WIN32) || defined(TCC_UCLIBC) || defined(TCC_MUSL) //#warning Bound checking does not support malloc (etc.) in this environment. #undef CONFIG_TCC_MALLOC_HOOKS #undef HAVE_MEMALIGN diff --git a/libtcc.c b/libtcc.c index 337a6f22..99bf7836 100644 --- a/libtcc.c +++ b/libtcc.c @@ -851,6 +851,10 @@ LIBTCCAPI TCCState *tcc_new(void) tcc_define_symbol(s, "__ILP32__", NULL); #endif +#if defined(TCC_MUSL) + tcc_define_symbol(s, "__builtin_va_list", "void *"); +#endif /* TCC_MUSL */ + #ifdef TCC_TARGET_PE tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short"); tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short"); diff --git a/tcc.h b/tcc.h index 30e8dec1..5b9d4a3c 100644 --- a/tcc.h +++ b/tcc.h @@ -136,7 +136,7 @@ #if !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \ !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_C67) && \ - !defined(CONFIG_USE_LIBGCC) + !defined(CONFIG_USE_LIBGCC) && !defined(TCC_MUSL) #define CONFIG_TCC_BCHECK /* enable bound checking code */ #endif @@ -239,11 +239,23 @@ # elif defined(TCC_UCLIBC) # define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0" /* is there a uClibc for x86_64 ? */ # elif defined TCC_TARGET_ARM64 -# define CONFIG_TCC_ELFINTERP "/lib/ld-linux-aarch64.so.1" +# if defined(TCC_MUSL) +# define CONFIG_TCC_ELFINTERP "/lib/ld-musl-aarch64.so.1" +# else +# define CONFIG_TCC_ELFINTERP "/lib/ld-linux-aarch64.so.1" +# endif # elif defined(TCC_TARGET_X86_64) -# define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2" +# if defined(TCC_MUSL) +# define CONFIG_TCC_ELFINTERP "/lib/ld-musl-x86_64.so.1" +# else +# define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2" +# endif # elif !defined(TCC_ARM_EABI) -# define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2" +# if defined(TCC_MUSL) +# define CONFIG_TCC_ELFINTERP "/lib/ld-musl-arm.so.1" +# else +# define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2" +# endif # endif #endif diff --git a/tccelf.c b/tccelf.c index 52085450..6a27495a 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1718,7 +1718,7 @@ static int final_sections_reloc(TCCState *s1) /* XXX: ignore sections with allocated relocations ? */ for(i = 1; i < s1->nb_sections; i++) { s = s1->sections[i]; -#ifdef TCC_TARGET_I386 +#if defined(TCC_TARGET_I386) || defined(TCC_MUSL) if (s->reloc && s != s1->got && (s->sh_flags & SHF_ALLOC)) //gr /* On X86 gdb 7.3 works in any case but gdb 6.6 will crash if SHF_ALLOC checking is removed */