mirror of
https://github.com/mirror/tinycc.git
synced 2025-01-15 05:20:06 +08:00
Add support of x86-64.
Most change was done in #ifdef TCC_TARGET_X86_64. So, nothing should be broken by this change. Summary of current status of x86-64 support: - produces x86-64 object files and executables. - the x86-64 code generator is based on x86's. -- for long long integers, we use 64bit registers instead of tcc's generic implementation. -- for float or double, we use SSE. SSE registers are not utilized well (we only use xmm0 and xmm1). -- for long double, we use x87 FPU. - passes make test. - passes ./libtcc_test. - can compile tcc.c. The compiled tcc can compile tcc.c, too. (there should be some bugs since the binary size of tcc2 and tcc3 is differ where tcc tcc.c -o tcc2 and tcc2 tcc.c -o tcc3) - can compile links browser. It seems working. - not tested well. I tested this work only on my linux box with few programs. - calling convention of long-double-integer or struct is not exactly the same as GCC's x86-64 ABI. - implementation of tcc -run is naive (tcc -run tcctest.c works, but tcc -run tcc.c doesn't work). Relocating 64bit addresses seems to be not as simple as 32bit environments. - shared object support isn't unimplemented - no bounds checker support - some builtin functions such as __divdi3 aren't supported
This commit is contained in:
parent
fb0ac27691
commit
0a9873aa22
23
Makefile
23
Makefile
@ -9,20 +9,28 @@ LIBS=-lm
|
||||
ifndef CONFIG_NOLDL
|
||||
LIBS+=-ldl
|
||||
endif
|
||||
ifneq ($(ARCH),x86-64)
|
||||
BCHECK_O=bcheck.o
|
||||
endif
|
||||
endif
|
||||
CFLAGS_P=$(CFLAGS) -pg -static -DCONFIG_TCC_STATIC
|
||||
LIBS_P=
|
||||
|
||||
ifneq ($(GCC_MAJOR),2)
|
||||
CFLAGS+=-fno-strict-aliasing
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),i386)
|
||||
CFLAGS+=-mpreferred-stack-boundary=2
|
||||
ifeq ($(GCC_MAJOR),2)
|
||||
CFLAGS+=-m386 -malign-functions=0
|
||||
else
|
||||
CFLAGS+=-march=i386 -falign-functions=0 -fno-strict-aliasing
|
||||
CFLAGS+=-march=i386 -falign-functions=0
|
||||
ifneq ($(GCC_MAJOR),3)
|
||||
CFLAGS+=-Wno-pointer-sign -Wno-sign-compare
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
DISAS=objdump -d
|
||||
INSTALL=install
|
||||
@ -50,6 +58,9 @@ ifdef CONFIG_CROSS
|
||||
PROGS+=c67-tcc$(EXESUF) i386-win32-tcc$(EXESUF)
|
||||
endif
|
||||
endif
|
||||
ifeq ($(ARCH),x86-64)
|
||||
PROGS=tcc$(EXESUF)
|
||||
endif
|
||||
|
||||
ifdef CONFIG_USE_LIBGCC
|
||||
LIBTCC1=
|
||||
@ -163,6 +174,10 @@ ARMFLAGS += $(if $(shell grep -l "^Features.* \(vfp\|iwmmxt\) " /proc/cpuinfo),-
|
||||
tcc$(EXESUF): tcc.c arm-gen.c tccelf.c tccasm.c tcctok.h libtcc.h
|
||||
$(CC) $(CFLAGS) -DTCC_TARGET_ARM $(ARMFLAGS) -o $@ $< $(LIBS)
|
||||
endif
|
||||
ifeq ($(ARCH),x86-64)
|
||||
tcc$(EXESUF): tcc.c tccelf.c tccasm.c tcctok.h libtcc.h x86_64-gen.c
|
||||
$(CC) $(CFLAGS) -DTCC_TARGET_X86_64 -o $@ $< $(LIBS)
|
||||
endif
|
||||
endif
|
||||
|
||||
# Cross Tiny C Compilers
|
||||
@ -238,7 +253,9 @@ else
|
||||
ifndef CONFIG_USE_LIBGCC
|
||||
$(INSTALL) -m644 libtcc1.a "$(DESTDIR)$(tccdir)"
|
||||
endif
|
||||
ifneq ($(ARCH),x86-64)
|
||||
$(INSTALL) -m644 $(BCHECK_O) "$(DESTDIR)$(tccdir)"
|
||||
endif
|
||||
$(INSTALL) -m644 stdarg.h stddef.h stdbool.h float.h varargs.h \
|
||||
tcclib.h "$(DESTDIR)$(tccdir)/include"
|
||||
endif
|
||||
@ -271,9 +288,13 @@ libinstall: libtcc.a
|
||||
libtcc.o: tcc.c i386-gen.c Makefile
|
||||
ifdef CONFIG_WIN32
|
||||
$(CC) $(CFLAGS) -DTCC_TARGET_PE -DLIBTCC -c -o $@ $<
|
||||
else
|
||||
ifeq ($(ARCH),x86-64)
|
||||
$(CC) $(CFLAGS) -DTCC_TARGET_X86_64 -DLIBTCC -c -o $@ $<
|
||||
else
|
||||
$(CC) $(CFLAGS) -DLIBTCC -c -o $@ $<
|
||||
endif
|
||||
endif
|
||||
|
||||
libtcc.a: libtcc.o
|
||||
$(AR) rcs $@ $^
|
||||
|
6
configure
vendored
6
configure
vendored
@ -39,6 +39,9 @@ case "$cpu" in
|
||||
i386|i486|i586|i686|i86pc|BePC)
|
||||
cpu="x86"
|
||||
;;
|
||||
x86_64)
|
||||
cpu="x86-64"
|
||||
;;
|
||||
armv4l)
|
||||
cpu="armv4l"
|
||||
;;
|
||||
@ -313,6 +316,9 @@ echo "EXESUF=$EXESUF" >> config.mak
|
||||
if test "$cpu" = "x86" ; then
|
||||
echo "ARCH=i386" >> config.mak
|
||||
echo "#define HOST_I386 1" >> $TMPH
|
||||
elif test "$cpu" = "x86-64" ; then
|
||||
echo "ARCH=x86-64" >> config.mak
|
||||
echo "#define HOST_X86_64 1" >> $TMPH
|
||||
elif test "$cpu" = "armv4l" ; then
|
||||
echo "ARCH=arm" >> config.mak
|
||||
echo "#define HOST_ARM 1" >> $TMPH
|
||||
|
@ -106,6 +106,9 @@ union float_long {
|
||||
long l;
|
||||
};
|
||||
|
||||
/* XXX: we don't support several builtin supports for now */
|
||||
#ifndef __x86_64__
|
||||
|
||||
/* XXX: use gcc/tcc intrinsic ? */
|
||||
#if defined(__i386__)
|
||||
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
|
||||
@ -482,6 +485,8 @@ unsigned short __tcc_fpu_control = 0x137f;
|
||||
unsigned short __tcc_int_fpu_control = 0x137f | 0x0c00;
|
||||
#endif
|
||||
|
||||
#endif /* !__x86_64__ */
|
||||
|
||||
/* XXX: fix tcc's code generator to do this instead */
|
||||
float __floatundisf(unsigned long long a)
|
||||
{
|
||||
|
73
stdarg.h
73
stdarg.h
@ -1,6 +1,75 @@
|
||||
#ifndef _STDARG_H
|
||||
#define _STDARG_H
|
||||
|
||||
#ifdef __x86_64__
|
||||
|
||||
#ifdef __TINYC__
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/* GCC compatible definition of va_list. */
|
||||
struct __va_list_struct {
|
||||
unsigned int gp_offset;
|
||||
unsigned int fp_offset;
|
||||
union {
|
||||
unsigned int overflow_offset;
|
||||
char *overflow_arg_area;
|
||||
};
|
||||
char *reg_save_area;
|
||||
};
|
||||
|
||||
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); }
|
||||
|
||||
/* 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 = *(struct __va_list_struct*)( \
|
||||
(char*)__builtin_frame_address(0) - 16), \
|
||||
ap->overflow_arg_area = ((char *)__builtin_frame_address(0) + \
|
||||
ap->overflow_offset), \
|
||||
ap->reg_save_area = (char *)__builtin_frame_address(0) - 176 - 16 \
|
||||
)
|
||||
#define va_arg(ap, type) \
|
||||
(*(type*)(__builtin_types_compatible_p(type, long double) \
|
||||
? (ap->overflow_arg_area += 16, \
|
||||
ap->overflow_arg_area - 16) \
|
||||
: __builtin_types_compatible_p(type, double) \
|
||||
? (ap->fp_offset < 128 + 48 \
|
||||
? (ap->fp_offset += 16, \
|
||||
ap->reg_save_area + ap->fp_offset - 16) \
|
||||
: (ap->overflow_arg_area += 8, \
|
||||
ap->overflow_arg_area - 8)) \
|
||||
: (ap->gp_offset < 48 \
|
||||
? (ap->gp_offset += 8, \
|
||||
ap->reg_save_area + ap->gp_offset - 8) \
|
||||
: (ap->overflow_arg_area += 8, \
|
||||
ap->overflow_arg_area - 8)) \
|
||||
))
|
||||
#define va_copy(dest, src) \
|
||||
((dest) = (va_list)malloc(sizeof(struct __va_list_struct)), \
|
||||
*(dest) = *(src))
|
||||
#define va_end(ap) __va_list_free(ap)
|
||||
|
||||
#else
|
||||
|
||||
/* for GNU C */
|
||||
|
||||
typedef __builtin_va_list va_list;
|
||||
|
||||
#define va_start(ap, last) __builtin_va_start(ap, last)
|
||||
#define va_arg(ap, type) __builtin_va_arg(ap, type)
|
||||
#define va_copy(dest, src) __builtin_va_copy(dest, src)
|
||||
#define va_end(ap) __builtin_va_end(ap)
|
||||
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
typedef char *va_list;
|
||||
|
||||
/* only correct for i386 */
|
||||
@ -9,8 +78,10 @@ typedef char *va_list;
|
||||
#define va_copy(dest, src) (dest) = (src)
|
||||
#define va_end(ap)
|
||||
|
||||
#endif
|
||||
|
||||
/* fix a buggy dependency on GCC in libio.h */
|
||||
typedef va_list __gnuc_va_list;
|
||||
#define _VA_LIST_DEFINED
|
||||
|
||||
#endif
|
||||
#endif /* _STDARG_H */
|
||||
|
138
tcc.c
138
tcc.c
@ -79,15 +79,16 @@
|
||||
//#define TCC_TARGET_I386 /* i386 code generator */
|
||||
//#define TCC_TARGET_ARM /* ARMv4 code generator */
|
||||
//#define TCC_TARGET_C67 /* TMS320C67xx code generator */
|
||||
//#define TCC_TARGET_X86_64 /* x86-64 code generator */
|
||||
|
||||
/* default target is I386 */
|
||||
#if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
|
||||
!defined(TCC_TARGET_C67)
|
||||
!defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
|
||||
#define TCC_TARGET_I386
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
|
||||
!defined(TCC_TARGET_C67)
|
||||
!defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
|
||||
#define CONFIG_TCC_BCHECK /* enable bound checking code */
|
||||
#endif
|
||||
|
||||
@ -96,7 +97,8 @@
|
||||
#endif
|
||||
|
||||
/* define it to include assembler support */
|
||||
#if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
|
||||
#if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67) && \
|
||||
!defined(TCC_TARGET_X86_64)
|
||||
#define CONFIG_TCC_ASM
|
||||
#endif
|
||||
|
||||
@ -531,6 +533,12 @@ struct TCCState {
|
||||
|
||||
/* output file for preprocessing */
|
||||
FILE *outfile;
|
||||
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
/* buffer to store jump tables */
|
||||
char *jmp_table;
|
||||
int jmp_table_num;
|
||||
#endif
|
||||
};
|
||||
|
||||
/* The current value can be: */
|
||||
@ -938,6 +946,10 @@ static inline int is_float(int t)
|
||||
#include "c67-gen.c"
|
||||
#endif
|
||||
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
#include "x86_64-gen.c"
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_TCC_STATIC
|
||||
|
||||
#define RTLD_LAZY 0x001
|
||||
@ -4769,26 +4781,33 @@ void save_reg(int r)
|
||||
r = p->r & VT_VALMASK;
|
||||
/* store register in the stack */
|
||||
type = &p->type;
|
||||
#ifndef TCC_TARGET_X86_64
|
||||
if ((p->r & VT_LVAL) ||
|
||||
(!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
|
||||
type = &int_type;
|
||||
#else
|
||||
if (p->r & VT_LVAL)
|
||||
type = &char_pointer_type;
|
||||
#endif
|
||||
size = type_size(type, &align);
|
||||
loc = (loc - size) & -align;
|
||||
sv.type.t = type->t;
|
||||
sv.r = VT_LOCAL | VT_LVAL;
|
||||
sv.c.ul = loc;
|
||||
store(r, &sv);
|
||||
#ifdef TCC_TARGET_I386
|
||||
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
|
||||
/* x86 specific: need to pop fp register ST0 if saved */
|
||||
if (r == TREG_ST0) {
|
||||
o(0xd9dd); /* fstp %st(1) */
|
||||
}
|
||||
#endif
|
||||
#ifndef TCC_TARGET_X86_64
|
||||
/* special long long case */
|
||||
if ((type->t & VT_BTYPE) == VT_LLONG) {
|
||||
sv.c.ul += 4;
|
||||
store(p->r2, &sv);
|
||||
}
|
||||
#endif
|
||||
l = loc;
|
||||
saved = 1;
|
||||
}
|
||||
@ -4939,8 +4958,7 @@ void gbound(void)
|
||||
register value (such as structures). */
|
||||
int gv(int rc)
|
||||
{
|
||||
int r, r2, rc2, bit_pos, bit_size, size, align, i;
|
||||
unsigned long long ll;
|
||||
int r, rc2, bit_pos, bit_size, size, align, i;
|
||||
|
||||
/* NOTE: get_reg can modify vstack[] */
|
||||
if (vtop->type.t & VT_BITFIELD) {
|
||||
@ -5019,7 +5037,10 @@ int gv(int rc)
|
||||
((vtop->type.t & VT_BTYPE) == VT_LLONG &&
|
||||
!(reg_classes[vtop->r2] & rc2))) {
|
||||
r = get_reg(rc);
|
||||
#ifndef TCC_TARGET_X86_64
|
||||
if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
|
||||
int r2;
|
||||
unsigned long long ll;
|
||||
/* two register type load : expand to two words
|
||||
temporarily */
|
||||
if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
||||
@ -5059,7 +5080,9 @@ int gv(int rc)
|
||||
vpop();
|
||||
/* write second register */
|
||||
vtop->r2 = r2;
|
||||
} else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
|
||||
} else
|
||||
#endif
|
||||
if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
|
||||
int t1, t;
|
||||
/* lvalue of scalar type : need to use lvalue type
|
||||
because of possible cast */
|
||||
@ -5224,7 +5247,7 @@ void vpop(void)
|
||||
{
|
||||
int v;
|
||||
v = vtop->r & VT_VALMASK;
|
||||
#ifdef TCC_TARGET_I386
|
||||
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
|
||||
/* for x86, we need to pop the FP stack */
|
||||
if (v == TREG_ST0 && !nocode_wanted) {
|
||||
o(0xd9dd); /* fstp %st(1) */
|
||||
@ -5265,6 +5288,11 @@ void gv_dup(void)
|
||||
sv.type.t = VT_INT;
|
||||
if (is_float(t)) {
|
||||
rc = RC_FLOAT;
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
if ((t & VT_BTYPE) == VT_LDOUBLE) {
|
||||
rc = RC_ST0;
|
||||
}
|
||||
#endif
|
||||
sv.type.t = t;
|
||||
}
|
||||
r = gv(rc);
|
||||
@ -5278,6 +5306,7 @@ void gv_dup(void)
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef TCC_TARGET_X86_64
|
||||
/* generate CPU independent (unsigned) long long operations */
|
||||
void gen_opl(int op)
|
||||
{
|
||||
@ -5512,6 +5541,7 @@ void gen_opl(int op)
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* handle integer constant optimizations and various machine
|
||||
independent opt */
|
||||
@ -5790,7 +5820,11 @@ void gen_op(int op)
|
||||
if (op >= TOK_ULT && op <= TOK_LOR) {
|
||||
check_comparison_pointer_types(vtop - 1, vtop, op);
|
||||
/* pointers are handled are unsigned */
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
t = VT_LLONG | VT_UNSIGNED;
|
||||
#else
|
||||
t = VT_INT | VT_UNSIGNED;
|
||||
#endif
|
||||
goto std_op;
|
||||
}
|
||||
/* if both pointers, then it must be the '-' op */
|
||||
@ -5802,7 +5836,11 @@ void gen_op(int op)
|
||||
u = pointed_size(&vtop[-1].type);
|
||||
gen_opic(op);
|
||||
/* set to integer type */
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
vtop->type.t = VT_LLONG;
|
||||
#else
|
||||
vtop->type.t = VT_INT;
|
||||
#endif
|
||||
vpushi(u);
|
||||
gen_op(TOK_PDIV);
|
||||
} else {
|
||||
@ -5815,8 +5853,18 @@ void gen_op(int op)
|
||||
swap(&t1, &t2);
|
||||
}
|
||||
type1 = vtop[-1].type;
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
{
|
||||
CValue cval;
|
||||
CType ctype;
|
||||
ctype.t = VT_LLONG;
|
||||
cval.ull = pointed_size(&vtop[-1].type);
|
||||
vsetc(&ctype, VT_CONST, &cval);
|
||||
}
|
||||
#else
|
||||
/* XXX: cast to int ? (long long case) */
|
||||
vpushi(pointed_size(&vtop[-1].type));
|
||||
#endif
|
||||
gen_op('*');
|
||||
#ifdef CONFIG_TCC_BCHECK
|
||||
/* if evaluating constant expression, no code should be
|
||||
@ -6099,6 +6147,7 @@ static void gen_cast(CType *type)
|
||||
} else if ((dbt & VT_BTYPE) == VT_LLONG) {
|
||||
if ((sbt & VT_BTYPE) != VT_LLONG) {
|
||||
/* scalar to long long */
|
||||
#ifndef TCC_TARGET_X86_64
|
||||
/* machine independent conversion */
|
||||
gv(RC_INT);
|
||||
/* generate high word */
|
||||
@ -6113,6 +6162,14 @@ static void gen_cast(CType *type)
|
||||
/* patch second register */
|
||||
vtop[-1].r2 = vtop->r;
|
||||
vpop();
|
||||
#else
|
||||
int r = gv(RC_INT);
|
||||
if (sbt != (VT_INT | VT_UNSIGNED)) {
|
||||
/* x86_64 specific: movslq */
|
||||
o(0x6348);
|
||||
o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
} else if (dbt == VT_BOOL) {
|
||||
/* scalar to bool */
|
||||
@ -6571,20 +6628,31 @@ void vstore(void)
|
||||
#endif
|
||||
if (!nocode_wanted) {
|
||||
rc = RC_INT;
|
||||
if (is_float(ft))
|
||||
if (is_float(ft)) {
|
||||
rc = RC_FLOAT;
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
if ((ft & VT_BTYPE) == VT_LDOUBLE) {
|
||||
rc = RC_ST0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
r = gv(rc); /* generate value */
|
||||
/* if lvalue was saved on stack, must read it */
|
||||
if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
|
||||
SValue sv;
|
||||
t = get_reg(RC_INT);
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
sv.type.t = VT_PTR;
|
||||
#else
|
||||
sv.type.t = VT_INT;
|
||||
#endif
|
||||
sv.r = VT_LOCAL | VT_LVAL;
|
||||
sv.c.ul = vtop[-1].c.ul;
|
||||
load(t, &sv);
|
||||
vtop[-1].r = t | VT_LVAL;
|
||||
}
|
||||
store(r, vtop - 1);
|
||||
#ifndef TCC_TARGET_X86_64
|
||||
/* two word case handling : store second register at word + 4 */
|
||||
if ((ft & VT_BTYPE) == VT_LLONG) {
|
||||
vswap();
|
||||
@ -6598,6 +6666,7 @@ void vstore(void)
|
||||
/* XXX: it works because r2 is spilled last ! */
|
||||
store(vtop->r2, vtop - 1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
vswap();
|
||||
vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
|
||||
@ -7107,7 +7176,11 @@ the_end:
|
||||
|
||||
/* long is never used as type */
|
||||
if ((t & VT_BTYPE) == VT_LONG)
|
||||
#ifndef TCC_TARGET_X86_64
|
||||
t = (t & ~VT_BTYPE) | VT_INT;
|
||||
#else
|
||||
t = (t & ~VT_BTYPE) | VT_LLONG;
|
||||
#endif
|
||||
type->t = t;
|
||||
return type_found;
|
||||
}
|
||||
@ -8044,8 +8117,14 @@ static void expr_eq(void)
|
||||
if (vtop != vstack) {
|
||||
/* needed to avoid having different registers saved in
|
||||
each branch */
|
||||
if (is_float(vtop->type.t))
|
||||
if (is_float(vtop->type.t)) {
|
||||
rc = RC_FLOAT;
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
|
||||
rc = RC_ST0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else
|
||||
rc = RC_INT;
|
||||
gv(rc);
|
||||
@ -8115,6 +8194,11 @@ static void expr_eq(void)
|
||||
rc = RC_INT;
|
||||
if (is_float(type.t)) {
|
||||
rc = RC_FLOAT;
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
|
||||
rc = RC_ST0;
|
||||
}
|
||||
#endif
|
||||
} else if ((type.t & VT_BTYPE) == VT_LLONG) {
|
||||
/* for long longs, we use fixed registers to avoid having
|
||||
to handle a complicated move */
|
||||
@ -9982,6 +10066,30 @@ static int rt_get_caller_pc(unsigned long *paddr,
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#elif defined(__x86_64__)
|
||||
/* return the PC at frame level 'level'. Return non zero if not found */
|
||||
static int rt_get_caller_pc(unsigned long *paddr,
|
||||
ucontext_t *uc, int level)
|
||||
{
|
||||
unsigned long fp;
|
||||
int i;
|
||||
|
||||
if (level == 0) {
|
||||
/* XXX: only support linux */
|
||||
*paddr = uc->uc_mcontext.gregs[REG_RIP];
|
||||
return 0;
|
||||
} else {
|
||||
fp = uc->uc_mcontext.gregs[REG_RBP];
|
||||
for(i=1;i<level;i++) {
|
||||
/* XXX: check address validity with program info */
|
||||
if (fp <= 0x1000 || fp >= 0xc0000000)
|
||||
return -1;
|
||||
fp = ((unsigned long *)fp)[0];
|
||||
}
|
||||
*paddr = ((unsigned long *)fp)[1];
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
#warning add arch specific rt_get_caller_pc()
|
||||
@ -10235,6 +10343,9 @@ TCCState *tcc_new(void)
|
||||
#if defined(TCC_TARGET_I386)
|
||||
tcc_define_symbol(s, "__i386__", NULL);
|
||||
#endif
|
||||
#if defined(TCC_TARGET_X86_64)
|
||||
tcc_define_symbol(s, "__x86_64__", NULL);
|
||||
#endif
|
||||
#if defined(TCC_TARGET_ARM)
|
||||
tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
|
||||
tcc_define_symbol(s, "__arm_elf__", NULL);
|
||||
@ -10301,6 +10412,10 @@ TCCState *tcc_new(void)
|
||||
/* XXX: currently the PE linker is not ready to support that */
|
||||
s->leading_underscore = 1;
|
||||
#endif
|
||||
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
s->jmp_table = NULL;
|
||||
#endif
|
||||
return s;
|
||||
}
|
||||
|
||||
@ -10336,6 +10451,9 @@ void tcc_delete(TCCState *s1)
|
||||
dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
|
||||
dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
|
||||
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
tcc_free(s1->jmp_table);
|
||||
#endif
|
||||
tcc_free(s1);
|
||||
}
|
||||
|
||||
|
139
tccelf.c
139
tccelf.c
@ -282,6 +282,9 @@ static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
|
||||
rel = section_ptr_add(sr, sizeof(ElfW_Rel));
|
||||
rel->r_offset = offset;
|
||||
rel->r_info = ELFW(R_INFO)(symbol, type);
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
rel->r_addend = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* put stab debug information */
|
||||
@ -469,6 +472,33 @@ static void relocate_syms(TCCState *s1, int do_resolve)
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
#define JMP_TABLE_ENTRY_SIZE 14
|
||||
#define JMP_TABLE_ENTRY_MAX_NUM 4096
|
||||
static unsigned long add_jmp_table(TCCState *s1, unsigned long val)
|
||||
{
|
||||
char *p;
|
||||
if (!s1->jmp_table) {
|
||||
int size = JMP_TABLE_ENTRY_SIZE * JMP_TABLE_ENTRY_MAX_NUM;
|
||||
s1->jmp_table_num = 0;
|
||||
s1->jmp_table = (char *)tcc_malloc(size);
|
||||
set_pages_executable(s1->jmp_table, size);
|
||||
}
|
||||
if (s1->jmp_table_num == JMP_TABLE_ENTRY_MAX_NUM) {
|
||||
error("relocating >%d symbols are not supported",
|
||||
JMP_TABLE_ENTRY_MAX_NUM);
|
||||
}
|
||||
p = s1->jmp_table + s1->jmp_table_num * JMP_TABLE_ENTRY_SIZE;
|
||||
s1->jmp_table_num++;
|
||||
/* jmp *0x0(%rip) */
|
||||
p[0] = 0xff;
|
||||
p[1] = 0x25;
|
||||
*(int *)(p + 2) = 0;
|
||||
*(unsigned long *)(p + 6) = val;
|
||||
return (unsigned long)p;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* relocate a given section (CPU dependent) */
|
||||
static void relocate_section(TCCState *s1, Section *s)
|
||||
{
|
||||
@ -493,6 +523,10 @@ static void relocate_section(TCCState *s1, Section *s)
|
||||
sym_index = ELFW(R_SYM)(rel->r_info);
|
||||
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
|
||||
val = sym->st_value;
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
/* XXX: not tested */
|
||||
val += rel->r_addend;
|
||||
#endif
|
||||
type = ELFW(R_TYPE)(rel->r_info);
|
||||
addr = s->sh_addr + rel->r_offset;
|
||||
|
||||
@ -620,6 +654,62 @@ static void relocate_section(TCCState *s1, Section *s)
|
||||
fprintf(stderr,"FIXME: handle reloc type %x at %lx [%.8x] to %lx\n",
|
||||
type,addr,(unsigned int )ptr,val);
|
||||
break;
|
||||
#elif defined(TCC_TARGET_X86_64)
|
||||
case R_X86_64_64:
|
||||
*(long long *)ptr += val;
|
||||
break;
|
||||
case R_X86_64_32:
|
||||
case R_X86_64_32S:
|
||||
*(int *)ptr += val;
|
||||
break;
|
||||
case R_X86_64_PC32: {
|
||||
long diff = val - addr;
|
||||
if (diff < -2147483648 || diff > 2147483647) {
|
||||
/* XXX: naive support for over 32bit jump */
|
||||
if (s1->output_type == TCC_OUTPUT_MEMORY) {
|
||||
val = add_jmp_table(s1, val);
|
||||
diff = val - addr;
|
||||
}
|
||||
if (diff <= -2147483647 || diff > 2147483647) {
|
||||
#if 0
|
||||
/* output memory map to debug easily */
|
||||
FILE* fp;
|
||||
char buf[4096];
|
||||
int size;
|
||||
Dl_info di;
|
||||
printf("%ld - %ld = %ld\n", val, addr, diff);
|
||||
dladdr((void *)addr, &di);
|
||||
printf("addr = %lx = %lx+%lx(%s) ptr=%p\n",
|
||||
addr, s->sh_addr, rel->r_offset, di.dli_sname,
|
||||
ptr);
|
||||
fp = fopen("/proc/self/maps", "r");
|
||||
size = fread(buf, 1, 4095, fp);
|
||||
buf[size] = '\0';
|
||||
printf("%s", buf);
|
||||
#endif
|
||||
error("internal error: relocation failed");
|
||||
}
|
||||
}
|
||||
*(int *)ptr += val - addr;
|
||||
}
|
||||
break;
|
||||
case R_X86_64_PLT32:
|
||||
*(int *)ptr += val - addr;
|
||||
break;
|
||||
case R_X86_64_GLOB_DAT:
|
||||
case R_X86_64_JUMP_SLOT:
|
||||
*(int *)ptr = val;
|
||||
break;
|
||||
case R_X86_64_GOTPCREL:
|
||||
*(int *)ptr += s1->got->sh_addr - addr;
|
||||
break;
|
||||
case R_X86_64_GOTTPOFF:
|
||||
*(int *)ptr += val - s1->got->sh_addr;
|
||||
break;
|
||||
case R_X86_64_GOT32:
|
||||
/* we load the got offset */
|
||||
*(int *)ptr += s1->got_offsets[sym_index];
|
||||
break;
|
||||
#else
|
||||
#error unsupported processor
|
||||
#endif
|
||||
@ -708,7 +798,8 @@ static void put32(unsigned char *p, uint32_t val)
|
||||
p[3] = val >> 24;
|
||||
}
|
||||
|
||||
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_ARM)
|
||||
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_ARM) || \
|
||||
defined(TCC_TARGET_X86_64)
|
||||
static uint32_t get32(unsigned char *p)
|
||||
{
|
||||
return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
|
||||
@ -769,8 +860,14 @@ static void put_got_entry(TCCState *s1,
|
||||
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
|
||||
name = symtab_section->link->data + sym->st_name;
|
||||
offset = sym->st_value;
|
||||
#ifdef TCC_TARGET_I386
|
||||
if (reloc_type == R_386_JMP_SLOT) {
|
||||
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
|
||||
if (reloc_type ==
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
R_X86_64_JUMP_SLOT
|
||||
#else
|
||||
R_386_JMP_SLOT
|
||||
#endif
|
||||
) {
|
||||
Section *plt;
|
||||
uint8_t *p;
|
||||
int modrm;
|
||||
@ -934,6 +1031,25 @@ static void build_got_entries(TCCState *s1)
|
||||
sym_index);
|
||||
}
|
||||
break;
|
||||
#elif defined(TCC_TARGET_X86_64)
|
||||
case R_X86_64_GOT32:
|
||||
case R_X86_64_GOTTPOFF:
|
||||
case R_X86_64_GOTPCREL:
|
||||
case R_X86_64_PLT32:
|
||||
if (!s1->got)
|
||||
build_got(s1);
|
||||
if (type == R_X86_64_GOT32 || type == R_X86_64_PLT32) {
|
||||
sym_index = ELFW(R_SYM)(rel->r_info);
|
||||
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
|
||||
/* look at the symbol got offset. If none, then add one */
|
||||
if (type == R_X86_64_GOT32)
|
||||
reloc_type = R_X86_64_GLOB_DAT;
|
||||
else
|
||||
reloc_type = R_X86_64_JUMP_SLOT;
|
||||
put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
|
||||
sym_index);
|
||||
}
|
||||
break;
|
||||
#else
|
||||
#error unsupported CPU
|
||||
#endif
|
||||
@ -1130,6 +1246,8 @@ static char elf_interp[] = "/usr/libexec/ld-elf.so.1";
|
||||
#else
|
||||
#ifdef TCC_ARM_EABI
|
||||
static char elf_interp[] = "/lib/ld-linux.so.3";
|
||||
#elif defined(TCC_TARGET_X86_64)
|
||||
static char elf_interp[] = "/lib/ld-linux-x86-64.so.2";
|
||||
#else
|
||||
static char elf_interp[] = "/lib/ld-linux.so.2";
|
||||
#endif
|
||||
@ -1593,6 +1711,15 @@ int elf_output_file(TCCState *s1, const char *filename)
|
||||
put32(p + 2, get32(p + 2) + s1->got->sh_addr);
|
||||
p += 16;
|
||||
}
|
||||
#elif defined(TCC_TARGET_X86_64)
|
||||
int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
|
||||
put32(p + 2, get32(p + 2) + x);
|
||||
put32(p + 8, get32(p + 8) + x - 6);
|
||||
p += 16;
|
||||
while (p < p_end) {
|
||||
put32(p + 2, get32(p + 2) + x + s1->plt->data - p);
|
||||
p += 16;
|
||||
}
|
||||
#elif defined(TCC_TARGET_ARM)
|
||||
int x;
|
||||
x=s1->got->sh_addr - s1->plt->sh_addr - 12;
|
||||
@ -1632,9 +1759,15 @@ int elf_output_file(TCCState *s1, const char *filename)
|
||||
put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
|
||||
put_dt(dynamic, DT_STRSZ, dynstr->data_offset);
|
||||
put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
|
||||
#ifdef TCC_TARGET_X86_64
|
||||
put_dt(dynamic, DT_RELA, rel_addr);
|
||||
put_dt(dynamic, DT_RELASZ, rel_size);
|
||||
put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
|
||||
#else
|
||||
put_dt(dynamic, DT_REL, rel_addr);
|
||||
put_dt(dynamic, DT_RELSZ, rel_size);
|
||||
put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
|
||||
#endif
|
||||
if (do_debug)
|
||||
put_dt(dynamic, DT_DEBUG, 0);
|
||||
put_dt(dynamic, DT_NULL, 0);
|
||||
|
1355
x86_64-gen.c
Normal file
1355
x86_64-gen.c
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user