From ebb874e2164308b1b9a53a2b1f6d1b9f65894059 Mon Sep 17 00:00:00 2001 From: Shinichiro Hamaji Date: Wed, 1 Apr 2009 03:45:18 +0900 Subject: [PATCH] Remove multiple definition error caused by combination of x86-64 and va_list. We need malloc and free to implement va_start and va_end. Since malloc and free may be replaced by #define, we add __builtin_malloc and __builtin_free. --- stdarg.h | 10 +++------- tcc.c | 18 ++++++++++++++++++ tcctok.h | 4 ++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/stdarg.h b/stdarg.h index 899358ce..c49e6dfd 100644 --- a/stdarg.h +++ b/stdarg.h @@ -20,14 +20,10 @@ struct __va_list_struct { typedef struct __va_list_struct *va_list; -/* avoid #define malloc tcc_malloc. - XXX: add __malloc or something into libtcc? */ -inline void *__va_list_malloc(size_t size) { return malloc(size); } -inline void __va_list_free(void *ptr) { free(ptr); } - +/* we use __builtin_(malloc|free) to avoid #define malloc tcc_malloc */ /* XXX: this lacks the support of aggregated types. */ #define va_start(ap, last) \ - (ap = (va_list)__va_list_malloc(sizeof(struct __va_list_struct)), \ + (ap = (va_list)__builtin_malloc(sizeof(struct __va_list_struct)), \ *ap = *(struct __va_list_struct*)( \ (char*)__builtin_frame_address(0) - 16), \ ap->overflow_arg_area = ((char *)__builtin_frame_address(0) + \ @@ -53,7 +49,7 @@ inline void __va_list_free(void *ptr) { free(ptr); } #define va_copy(dest, src) \ ((dest) = (va_list)malloc(sizeof(struct __va_list_struct)), \ *(dest) = *(src)) -#define va_end(ap) __va_list_free(ap) +#define va_end(ap) __builtin_free(ap) #else diff --git a/tcc.c b/tcc.c index ceca30f7..f707791c 100644 --- a/tcc.c +++ b/tcc.c @@ -7750,6 +7750,24 @@ static void unary(void) vset(&type, VT_LOCAL, 0); } break; +#ifdef TCC_TARGET_X86_64 + case TOK_builtin_malloc: + { + char *p = file->buf_ptr; + file->buf_ptr = "malloc"; + next_nomacro1(); + file->buf_ptr = p; + goto tok_identifier; + } + case TOK_builtin_free: + { + char *p = file->buf_ptr; + file->buf_ptr = "free"; + next_nomacro1(); + file->buf_ptr = p; + goto tok_identifier; + } +#endif case TOK_INC: case TOK_DEC: t = tok; diff --git a/tcctok.h b/tcctok.h index ef7bf7af..33bed29f 100644 --- a/tcctok.h +++ b/tcctok.h @@ -110,6 +110,10 @@ DEF(TOK_builtin_types_compatible_p, "__builtin_types_compatible_p") DEF(TOK_builtin_constant_p, "__builtin_constant_p") DEF(TOK_builtin_frame_address, "__builtin_frame_address") +#ifdef TCC_TARGET_X86_64 + DEF(TOK_builtin_malloc, "__builtin_malloc") + DEF(TOK_builtin_free, "__builtin_free") +#endif DEF(TOK_REGPARM1, "regparm") DEF(TOK_REGPARM2, "__regparm__")