diff --git a/include/stddef.h b/include/stddef.h index 3d328daa..6a56f6e0 100644 --- a/include/stddef.h +++ b/include/stddef.h @@ -19,9 +19,7 @@ typedef union { long long __ll; long double __ld; } max_align_t; #undef offsetof #define offsetof(type, field) __builtin_offsetof(type, field) -#if defined __i386__ || defined __x86_64__ void *alloca(size_t size); -#endif #endif diff --git a/include/tccdefs.h b/include/tccdefs.h index 5c6535a0..d7596ac6 100644 --- a/include/tccdefs.h +++ b/include/tccdefs.h @@ -347,12 +347,8 @@ __MAYBE_REDIR(void*, calloc, (__SIZE_TYPE__, __SIZE_TYPE__)) __MAYBE_REDIR(void*, memalign, (__SIZE_TYPE__, __SIZE_TYPE__)) __MAYBE_REDIR(void, free, (void*)) -#if defined __i386__ || defined __x86_64__ __BOTH(void*, alloca, (__SIZE_TYPE__)) void *alloca(__SIZE_TYPE__); -#else - __BUILTIN(void*, alloca, (__SIZE_TYPE__)) -#endif __BUILTIN(void, abort, (void)) __BOUND(void, longjmp, ()) #if !defined _WIN32 diff --git a/lib/Makefile b/lib/Makefile index 4d02c337..942add92 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -35,12 +35,12 @@ endif XFLAGS += -I$(TOP) -I386_O = libtcc1.o alloca-bt.o $(COMMON_O) -X86_64_O = libtcc1.o alloca-bt.o $(COMMON_O) +I386_O = libtcc1.o $(COMMON_O) +X86_64_O = libtcc1.o $(COMMON_O) ARM_O = libtcc1.o armeabi.o armflush.o $(COMMON_O) ARM64_O = lib-arm64.o $(COMMON_O) RISCV64_O = lib-arm64.o $(COMMON_O) -COMMON_O = stdatomic.o atomic.o builtin.o alloca.o +COMMON_O = stdatomic.o atomic.o builtin.o alloca.o alloca-bt.o WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o LIN_O = dsohandle.o OSX_O = diff --git a/lib/alloca-bt.S b/lib/alloca-bt.S index 30c36e62..9092fdb8 100644 --- a/lib/alloca-bt.S +++ b/lib/alloca-bt.S @@ -92,5 +92,109 @@ p3: ret #endif +/* ---------------------------------------------- */ +#elif defined __arm__ + +.globl _(__bound_alloca) +_(__bound_alloca): + mov r1, r0 + add r0, r0, #1 + rsb sp, r0, sp + bic sp, sp, #7 + mov r0, sp + push { lr } + bl _(__bound_new_region) + pop { lr } + mov r0, sp + mov pc, lr + +/* ---------------------------------------------- */ +#elif defined __aarch64__ || defined __arm64__ + +.globl _(__bound_alloca) +_(__bound_alloca): +#ifdef __TINYC__ + .int 0xaa0003e1 + .int 0x91004000 + .int 0x927cec00 +#ifdef _WIN32 + .int 0xb4000160 + .int 0xd2820002 + .int 0xeb02001f + .int 0x540000c3 + .int 0xcb2263e3 + .int 0xf940007f + .int 0xcb2263ff + .int 0xcb020000 + .int 0x17fffffa + .int 0xb4000040 +#endif + .int 0xcb2063ff + .int 0x910003e0 + .int 0xa9bf7bfd + .reloc ., R_AARCH64_CALL26, _(__bound_new_region) + .int 0x94000000 + .int 0xa8c17bfd + .int 0x910003e0 + .int 0xd65f03c0 +#else + mov x1, x0 + add x0, x0, #16 // Round up to 16-byte boundary + and x0, x0, #-16 // Ensure 16-byte alignment +#ifdef _WIN32 + cbz x0, p100 // If size is 0, skip to return + // Windows requires page-wise allocation with stack probing + mov x2, #4096 // Page size = 4096 bytes + +p101: + cmp x0, x2 // Compare remaining size with page size + b.lo p102 // If less than page, jump to remainder + + // Probe first, then allocate + sub x3, sp, x2 // Calculate guard page address (sp - 4096) + ldr xzr, [x3] // Touch guard page FIRST + sub sp, sp, x2 // THEN allocate the page + + sub x0, x0, x2 // Decrement remaining size + b p101 // Continue loop + +p102: + // Allocate remaining bytes (less than one page) + cbz x0, p100 // If no remaining bytes, skip + sub sp, sp, x0 // Allocate remaining space +#else + // Non-Windows: simple one-time allocation + sub sp, sp, x0 // Allocate space on stack +#endif + +p100: + mov x0, sp // Return allocated address + stp x29, x30, [sp, #-16]! + bl _(__bound_new_region) + ldp x29, x30, [sp], #16 + mov x0, sp // Return allocated address + ret // Return to caller +#endif + +/* ---------------------------------------------- */ +#elif defined __riscv + +.globl _(__bound_alloca) +_(__bound_alloca): + mv a1, a0 + sub sp, sp, a0 + addi sp, sp, -16 + andi sp, sp, -16 + add a0, sp, zero + addi sp,sp,-16 + sd s0,0(sp) + sd ra,8(sp) + jal _(__bound_new_region) + ld s0,0(sp) + ld ra,8(sp) + addi sp,sp,16 + add a0, sp, zero + ret + /* ---------------------------------------------- */ #endif diff --git a/lib/alloca.S b/lib/alloca.S index f37be59d..e42abc1c 100644 --- a/lib/alloca.S +++ b/lib/alloca.S @@ -70,25 +70,18 @@ p3: /* ---------------------------------------------- */ #elif defined __arm__ - .text - .align 2 - .global alloca - .type alloca, %function -alloca: +.globl _(alloca) +_(alloca): rsb sp, r0, sp bic sp, sp, #7 mov r0, sp mov pc, lr - .size alloca, .-alloca /* ---------------------------------------------- */ #elif defined __aarch64__ || defined __arm64__ - .text - .align 2 - .global alloca - .type alloca, %function -alloca: +.globl _(alloca) +_(alloca): #ifdef __TINYC__ .int 0x91003c00 .int 0x927cec00 @@ -102,16 +95,16 @@ alloca: .int 0xcb2163ff .int 0xcb010000 .int 0x17fffffa -#endif .int 0xb4000040 +#endif .int 0xcb2063ff .int 0x910003e0 .int 0xd65f03c0 #else add x0, x0, #15 // Round up to 16-byte boundary and x0, x0, #-16 // Ensure 16-byte alignment - cbz x0, p100 // If size is 0, skip to return #ifdef _WIN32 + cbz x0, p100 // If size is 0, skip to return // Windows requires page-wise allocation with stack probing mov x1, #4096 // Page size = 4096 bytes @@ -140,20 +133,16 @@ p100: mov x0, sp // Return allocated address ret // Return to caller #endif - .size alloca, .-alloca + /* ---------------------------------------------- */ #elif defined __riscv - .text - .align 2 - .global alloca - .type alloca, %function -alloca: - sub sp, sp, a0 - addi sp, sp, -15 - andi sp, sp, -16 - add a0, sp, zero - ret - .size alloca, .-alloca -/* ---------------------------------------------- */ +.globl _(alloca) +_(alloca): + sub sp, sp, a0 + addi sp, sp, -15 + andi sp, sp, -16 + add a0, sp, zero + ret + #endif diff --git a/libtcc.c b/libtcc.c index 8809e617..01c4c7fd 100644 --- a/libtcc.c +++ b/libtcc.c @@ -40,7 +40,7 @@ #elif defined(TCC_TARGET_ARM64) #include "arm64-gen.c" #include "arm64-link.c" -#include "arm-asm.c" +#include "arm64-asm.c" #elif defined(TCC_TARGET_C67) #include "c67-gen.c" #include "c67-link.c" diff --git a/tcc.h b/tcc.h index 310409df..058507fe 100644 --- a/tcc.h +++ b/tcc.h @@ -385,7 +385,7 @@ extern long double strtold (const char *__nptr, char **__endptr); #elif defined TCC_TARGET_ARM64 # include "arm64-gen.c" # include "arm64-link.c" -# include "arm-asm.c" +# include "arm64-asm.c" #elif defined TCC_TARGET_C67 # define TCC_TARGET_COFF # include "coff.h" diff --git a/tccasm.c b/tccasm.c index b6fc34e5..1c0e1b86 100644 --- a/tccasm.c +++ b/tccasm.c @@ -984,6 +984,23 @@ static void asm_parse_directive(TCCState *s1, int global) skip('@'); next(); break; + case TOK_ASMDIR_reloc: + { + ExprValue e; + + next(); + asm_expr(s1, &e); + skip(','); +#ifdef __aarch64__ + if (strcmp(get_tok_str(tok, NULL), "R_AARCH64_CALL26")) +#endif + tcc_error("unimp: reloc '%s' unknown", get_tok_str(tok, NULL)); + next(); + skip(','); + greloca(cur_text_section, get_asm_sym(tok, NULL), e.v, R_AARCH64_CALL26, 0); + next(); + } + break; default: tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL)); break; diff --git a/tccgen.c b/tccgen.c index f8e81b81..0caeb151 100644 --- a/tccgen.c +++ b/tccgen.c @@ -1701,10 +1701,8 @@ ST_FUNC void gbound_args(int nb_args) gfunc_call(1); func_bound_add_epilog = 1; } -#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 if (v == TOK_alloca) func_bound_add_epilog = 1; -#endif #if TARGETOS_NetBSD if (v == TOK_longjmp) /* undo rename to __longjmp14 */ sv->sym->asm_label = TOK___bound_longjmp; diff --git a/tcctok.h b/tcctok.h index 49dce0b2..4a16a22f 100644 --- a/tcctok.h +++ b/tcctok.h @@ -301,9 +301,7 @@ DEF(TOK___fixxfdi, "__fixxfdi") #endif -#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 DEF(TOK_alloca, "alloca") -#endif #if defined TCC_TARGET_PE DEF(TOK___chkstk, "__chkstk") @@ -415,6 +413,7 @@ DEF_ASMDIR(long) DEF_ASMDIR(int) DEF_ASMDIR(symver) + DEF_ASMDIR(reloc) DEF_ASMDIR(section) /* must be last directive */ #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 diff --git a/tests/boundtest.c b/tests/boundtest.c index 8f150bbe..c92b93c2 100644 --- a/tests/boundtest.c +++ b/tests/boundtest.c @@ -169,14 +169,6 @@ int test13(void) return strlen(tab); } -#if defined __i386__ || defined __x86_64__ -#define allocf(x) -#else -#undef alloca -#define alloca(x) malloc(x) -#define allocf(x) free(x) -#endif - int test14(void) { char *p = alloca(TAB_SIZE); @@ -184,7 +176,6 @@ int test14(void) memset(p, 'a', TAB_SIZE); p[TAB_SIZE-1] = 0; ret = strlen(p); - allocf(p); return ret; } @@ -196,7 +187,6 @@ int test15(void) memset(p, 'a', TAB_SIZE); p[TAB_SIZE-1] = 0; ret = strlen(p); - allocf(p); return ret; } @@ -211,8 +201,6 @@ int test16() /* Test alloca embedded in a larger expression */ printf("alloca : %s : %s\n", p, strcpy(q=alloca(strlen(demo)+1),demo) ); - allocf(p); - allocf(q); return 0; } @@ -228,8 +216,6 @@ int test17() /* Test alloca embedded in a larger expression */ printf("alloca : %s : %s\n", p, strcpy(q=alloca(strlen(demo)),demo) ); - allocf(p); - allocf(q); return 0; } diff --git a/tests/tcctest.c b/tests/tcctest.c index 6a16e945..3ddefaca 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -2941,14 +2941,12 @@ void old_style_function_test(void) void alloca_test() { -#if defined __i386__ || defined __x86_64__ || defined __arm__ char *p = alloca(16); strcpy(p,"123456789012345"); printf("alloca: p is %s\n", p); char *demo = "This is only a test.\n"; /* Test alloca embedded in a larger expression */ printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) ); -#endif } void *bounds_checking_is_enabled() @@ -4208,7 +4206,6 @@ double get100 () { return 100.0; } void callsave_test(void) { -#if defined __i386__ || defined __x86_64__ || defined __arm__ int i, s; double *d; double t; s = sizeof (double); printf ("callsavetest: %d\n", s); @@ -4221,7 +4218,6 @@ void callsave_test(void) generates a segfault. */ i = d[0] > get100 (); printf ("%d\n", i); -#endif }