Make tcclib1.a compile on macOS again

When compiling on macOS (at least in version 10.12) the TCC compiler failed
to compile libtcc1.a. Three problems were solved:
 - The predefined macro "__APPLE__" is now available, as it is tested in the
   libc darwin header files
 - the libtcc1 Makefile defined _ANSI_SOURCE, although it used signals
 - stdargs.h defined va_list differently from the darwin libc.
   If the darwin standard library was included BEFORE stdargs this caused
   problems.
 - the darwin libc generated a warning if GCC < 4 was used
 - additional defines are predefined now to make darwin libc headers compile.
This commit is contained in:
Robert Hoelzl 2020-04-12 00:39:47 +02:00
parent 38ab621b55
commit 1803762e3f
5 changed files with 41 additions and 45 deletions

View File

@ -4,22 +4,10 @@
#ifdef __x86_64__
#ifndef _WIN64
//This should be in sync with the declaration on our lib/libtcc1.c
/* GCC compatible definition of va_list. */
typedef struct {
unsigned int gp_offset;
unsigned int fp_offset;
union {
unsigned int overflow_offset;
char *overflow_arg_area;
};
char *reg_save_area;
} __va_list_struct;
typedef __builtin_va_list va_list;
typedef __va_list_struct va_list[1];
void __va_start(__va_list_struct *ap, void *fp);
void *__va_arg(__va_list_struct *ap, int arg_type, int size, int align);
void __va_start(va_list ap, void *fp);
void *__va_arg(va_list ap, int arg_type, int size, int align);
#define va_start(ap, last) __va_start(ap, __builtin_frame_address(0))
#define va_arg(ap, type) \
@ -27,9 +15,6 @@ void *__va_arg(__va_list_struct *ap, int arg_type, int size, int align);
#define va_copy(dest, src) (*(dest) = *(src))
#define va_end(ap) ((void)0)
/* avoid conflicting definition for va_list on Macs. */
#define _VA_LIST_T
#else /* _WIN64 */
typedef char *va_list;
#define va_start(ap,last) __builtin_va_start(ap,last)

View File

@ -36,10 +36,6 @@ else
DSO_O = dsohandle.o
endif
ifdef CONFIG_OSX
XFLAGS += -D_ANSI_SOURCE
endif
I386_O = libtcc1.o alloca86.o alloca86-bt.o $(BT_O)
X86_64_O = libtcc1.o alloca86_64.o alloca86_64-bt.o $(BT_O)
ARM_O = libtcc1.o armeabi.o alloca-arm.o armflush.o $(BT_O)

View File

@ -3,6 +3,7 @@
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
int (*__rt_error)(void*, void*, const char *, va_list);

View File

@ -2,14 +2,16 @@
#if defined __x86_64__
#include "stdarg.h"
/* Avoid include files, they may not be available when cross compiling */
extern void *memset(void *s, int c, __SIZE_TYPE__ n);
extern void abort(void);
/* This should be in sync with our include/stdarg.h */
enum __va_arg_type {
__va_gen_reg, __va_float_reg, __va_stack
};
#define __VA_GEN_REG 0
#define __VA_FLOAT_REG 1
#define __VA_STACK 2
/* GCC compatible definition of va_list. */
typedef struct {
@ -22,43 +24,45 @@ typedef struct {
char *reg_save_area;
} __va_list_struct;
void __va_start(__va_list_struct *ap, void *fp)
void __va_start(va_list ap, void *fp)
{
memset(ap, 0, sizeof(__va_list_struct));
*ap = *(__va_list_struct *)((char *)fp - 16);
ap->overflow_arg_area = (char *)fp + ap->overflow_offset;
ap->reg_save_area = (char *)fp - 176 - 16;
__va_list_struct * _ap = (__va_list_struct*) ap;
memset(_ap, 0, sizeof(__va_list_struct));
*_ap = *(__va_list_struct *)((char *)fp - 16);
_ap->overflow_arg_area = (char *)fp + _ap->overflow_offset;
_ap->reg_save_area = (char *)fp - 176 - 16;
}
void *__va_arg(__va_list_struct *ap,
enum __va_arg_type arg_type,
void *__va_arg(va_list ap,
int arg_type,
int size, int align)
{
__va_list_struct * _ap = (__va_list_struct*) ap;
size = (size + 7) & ~7;
align = (align + 7) & ~7;
switch (arg_type) {
case __va_gen_reg:
if (ap->gp_offset + size <= 48) {
ap->gp_offset += size;
return ap->reg_save_area + ap->gp_offset - size;
case __VA_GEN_REG:
if (_ap->gp_offset + size <= 48) {
_ap->gp_offset += size;
return _ap->reg_save_area + _ap->gp_offset - size;
}
goto use_overflow_area;
case __va_float_reg:
if (ap->fp_offset < 128 + 48) {
ap->fp_offset += 16;
return ap->reg_save_area + ap->fp_offset - 16;
case __VA_FLOAT_REG:
if (_ap->fp_offset < 128 + 48) {
_ap->fp_offset += 16;
return _ap->reg_save_area + _ap->fp_offset - 16;
}
size = 8;
goto use_overflow_area;
case __va_stack:
case __VA_STACK:
use_overflow_area:
ap->overflow_arg_area += size;
ap->overflow_arg_area = (char*)((long long)(ap->overflow_arg_area + align - 1) & -align);
return ap->overflow_arg_area - size;
_ap->overflow_arg_area += size;
_ap->overflow_arg_area = (char*)((long long)(_ap->overflow_arg_area + align - 1) & -align);
return _ap->overflow_arg_area - size;
default: /* should never happen */
default: /* should never h_appen */
abort();
return 0;
}

View File

@ -927,6 +927,16 @@ LIBTCCAPI TCCState *tcc_new(void)
/* Some GCC builtins that are simple to express as macros. */
tcc_define_symbol(s, "__builtin_extract_return_addr(x)", "x");
#endif /* ndef TCC_TARGET_PE */
#ifdef TCC_TARGET_MACHO
/* emulate APPLE-GCC to make libc's headerfiles compile: */
tcc_define_symbol(s, "__APPLE__", "1");
tcc_define_symbol(s, "__GNUC__", "4"); /* darwin emits warning on GCC<4 */
/* avoids usage of GCC/clang specific builtins in libc-headerfiles: */
tcc_define_symbol(s, "__FINITE_MATH_ONLY__", "1");
tcc_define_symbol(s, "_FORTIFY_SOURCE", "0");
#endif /* ndef TCC_TARGET_MACHO */
tcc_define_symbol(s, "__builtin_va_list", "void *");
return s;
}