Fix stdarg on x86-64

this partly reverts 1803762e3 to fix stdarg on x86-64 again.  I've tried
to retain the apple specific changes from that commit.
Also include stdarg.h first in tcc.h, maybe that helps as well.
This commit is contained in:
Michael Matz 2020-04-15 04:13:05 +02:00
parent 1803762e3f
commit 8c6143d86f
5 changed files with 45 additions and 32 deletions

View File

@ -4,7 +4,24 @@
#ifdef __x86_64__
#ifndef _WIN64
#ifndef __APPLE__
//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 __va_list_struct va_list[1];
#else
/* This is sometimes a void* on TCC, which makes it unlikely to
work with va_copy, but until we have something better ... */
typedef __builtin_va_list va_list;
#endif
void __va_start(va_list ap, void *fp);
void *__va_arg(va_list ap, int arg_type, int size, int align);

View File

@ -1,9 +1,9 @@
/* ------------------------------------------------------------- */
/* function to get a stack backtrace on demand with a message */
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
int (*__rt_error)(void*, void*, const char *, va_list);

View File

@ -2,16 +2,14 @@
#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 */
#define __VA_GEN_REG 0
#define __VA_FLOAT_REG 1
#define __VA_STACK 2
enum __va_arg_type {
__va_gen_reg, __va_float_reg, __va_stack
};
/* GCC compatible definition of va_list. */
typedef struct {
@ -24,45 +22,43 @@ typedef struct {
char *reg_save_area;
} __va_list_struct;
void __va_start(va_list ap, void *fp)
void __va_start(__va_list_struct *ap, void *fp)
{
__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;
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 ap,
void *__va_arg(__va_list_struct *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 h_appen */
default: /* should never happen */
abort();
return 0;
}

View File

@ -928,15 +928,15 @@ LIBTCCAPI TCCState *tcc_new(void)
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");
/* 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 *");
#endif /* ndef TCC_TARGET_MACHO */
return s;
}

2
tcc.h
View File

@ -24,9 +24,9 @@
#define _GNU_SOURCE
#include "config.h"
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <math.h>