From 9d2068c6309dc50dfdbbc30a5d6757683d3f884c Mon Sep 17 00:00:00 2001 From: grischka Date: Sun, 3 Mar 2024 20:40:53 +0100 Subject: tccrun: add option CONFIG_RUNMEM_RO=2 /* 0 = .text rwx other rw (memory: min 2 pages) */ /* 1 = .text rx other rw (memory: min 3 pages) */ /* 2 = .text rx .rdata ro .data/.bss rw (memory: min 4 pages) */ tcc -vv -run ... shows some info. Also when compiled with -DMEM_DEBUG: tcc -bench -run ... shows some memory usage --- Makefile | 2 +- libtcc.c | 5 +++-- tcc.h | 5 ++--- tccpe.c | 2 +- tccpp.c | 6 +++--- tccrun.c | 19 ++++++++++--------- tests/Makefile | 2 +- tests/tcctest.c | 2 ++ tests/tests2/Makefile | 8 ++++---- 9 files changed, 27 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 26764f5..2b247ea 100644 --- a/Makefile +++ b/Makefile @@ -491,7 +491,7 @@ test-install: tccdefs_.h @$(MAKE) -C tests TESTINSTALL=yes #_all clean: - @rm -f tcc$(EXESUF) tcc_c$(EXESUF) tcc_p$(EXESUF) *-tcc$(EXESUF) + @rm -f tcc *-tcc tcc_p tcc_c @rm -f tags ETAGS *.o *.a *.so* *.out *.log lib*.def *.exe *.dll @rm -f a.out *.dylib *_.h *.pod *.tcov @$(MAKE) -s -C lib $@ diff --git a/libtcc.c b/libtcc.c index 6d720e7..f8e0e33 100644 --- a/libtcc.c +++ b/libtcc.c @@ -2203,9 +2203,10 @@ PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time) #ifdef TCC_IS_NATIVE if (s1->run_size) { Section *s = s1->symtab; - int ms = s->data_offset + s->link->data_offset + s->hash->data_offset; + unsigned ms = s->data_offset + s->link->data_offset + s->hash->data_offset; + unsigned rs = s1->run_size; fprintf(stderr, ": %d to run, %d symbols, %d other,", - s1->run_size, ms, mem_cur_size - s1->run_size - ms); + rs, ms, mem_cur_size - rs - ms); } #endif fprintf(stderr, " %d max (bytes)\n", mem_max_size); diff --git a/tcc.h b/tcc.h index 77200e9..547ca9a 100644 --- a/tcc.h +++ b/tcc.h @@ -135,7 +135,7 @@ extern long double strtold (const char *__nptr, char **__endptr); /* include file debug */ /* #define INC_DEBUG */ /* memory leak debug (only for single threaded usage) */ -/* #define MEM_DEBUG */ +/* #define MEM_DEBUG 1,2,3 */ /* assembler debug */ /* #define ASM_DEBUG */ @@ -987,8 +987,7 @@ struct TCCState { #ifdef TCC_IS_NATIVE const char *run_main; /* entry for tcc_run() */ - void *run_mem; /* runtime_memory */ - void *run_ptr; /* ptr to runtime_memory (aligned) */ + void *run_ptr; /* runtime_memory */ unsigned run_size; /* size of runtime_memory */ #ifdef _WIN64 void *run_function_table; /* unwind data */ diff --git a/tccpe.c b/tccpe.c index 26fb229..020f359 100644 --- a/tccpe.c +++ b/tccpe.c @@ -732,7 +732,7 @@ static int pe_write(struct pe_info *pe) memcpy(psh->Name, sh_name, umin(strlen(sh_name), sizeof psh->Name)); if (si->cls == sec_debug) - need_strtab += pe_put_long_secname(psh->Name, sh_name); + need_strtab += pe_put_long_secname((char*)psh->Name, sh_name); psh->Characteristics = si->pe_flags; psh->VirtualAddress = addr; diff --git a/tccpp.c b/tccpp.c index 5a09a27..1c6b5c7 100644 --- a/tccpp.c +++ b/tccpp.c @@ -127,7 +127,7 @@ ST_FUNC void expect(const char *msg) #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size) #define TAL_DEBUG_PARAMS #else -#define TAL_DEBUG 1 +#define TAL_DEBUG MEM_DEBUG //#define TAL_INFO 1 /* collect and dump allocators stats */ #define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__) #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__) @@ -189,7 +189,7 @@ tail_call: al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed, (al->peak_p - al->buffer) * 100.0 / al->size); #endif -#ifdef TAL_DEBUG +#if TAL_DEBUG && TAL_DEBUG != 3 /* do not check TAL leaks with -DMEM_DEBUG=3 */ if (al->nb_allocs > 0) { uint8_t *p; fprintf(stderr, "TAL_DEBUG: memory leak %d chunk(s) (limit= %d)\n", @@ -203,7 +203,7 @@ tail_call: } p += header->size + sizeof(tal_header_t); } -#if MEM_DEBUG-0 == 2 +#if TAL_DEBUG == 2 exit(2); #endif } diff --git a/tccrun.c b/tccrun.c index 95d667c..56ddcc2 100644 --- a/tccrun.c +++ b/tccrun.c @@ -128,9 +128,9 @@ static int rt_mem(TCCState *s1, int size) return tcc_error_noabort("tccrun: could not map memory"); ptr_diff = (char*)prw - (char*)ptr; /* = size; */ //printf("map %p %p %p\n", ptr, prw, (void*)ptr_diff); + size *= 2; #else - s1->run_mem = tcc_malloc(size + PAGESIZE); /* one extra page to align malloc memory */ - ptr = (void*)PAGEALIGN(s1->run_mem); + ptr = tcc_malloc(size += PAGESIZE); /* one extra page to align malloc memory */ #endif s1->run_ptr = ptr; s1->run_size = size; @@ -188,14 +188,14 @@ ST_FUNC void tcc_run_free(TCCState *s1) st_unlink(s1); size = s1->run_size; #ifdef HAVE_SELINUX - munmap(ptr, size * 2); + munmap(ptr, size); #else /* unprotect memory to make it usable for malloc again */ - protect_pages(ptr, size, 2 /*rw*/); + protect_pages((void*)PAGEALIGN(ptr), size - PAGESIZE, 2 /*rw*/); # ifdef _WIN64 win64_del_function_table(s1->run_function_table); # endif - tcc_free(s1->run_mem); + tcc_free(ptr); #endif } @@ -286,8 +286,9 @@ static void cleanup_sections(TCCState *s1) } /* ------------------------------------------------------------- */ -/* 0 = .text rwx other rw */ -/* 1 = .text rx .rdata r .data/.bss rw */ +/* 0 = .text rwx other rw (memory >= 2 pages a 4096 bytes) */ +/* 1 = .text rx other rw (memory >= 3 pages) */ +/* 2 = .text rx .rdata ro .data/.bss rw (memory >= 4 pages) */ /* Some targets implement secutiry options that do not allow write in executable code. These targets need CONFIG_RUNMEM_RO=1. @@ -370,7 +371,7 @@ redo: align = 64; #endif /* start new page for different permissions */ - if (CONFIG_RUNMEM_RO || k == 0) + if (k <= CONFIG_RUNMEM_RO) align = PAGESIZE; } s->sh_addralign = align; @@ -387,7 +388,7 @@ redo: continue; #endif f = k; - if (CONFIG_RUNMEM_RO == 0) { + if (f >= CONFIG_RUNMEM_RO) { if (f != 0) continue; f = 3; /* change only SHF_EXECINSTR to rwx */ diff --git a/tests/Makefile b/tests/Makefile index c89e298..26924e5 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -342,7 +342,7 @@ cache: tcc_g clean: rm -f *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc *.gcc rm -f *-cc *-gcc *-tcc *.exe hello libtcc_test vla_test tcctest[1234] - rm -f asm-c-connect$(EXESUF) asm-c-connect-sep$(EXESUF) + rm -f asm-c-connect asm-c-connect-sep rm -f ex? tcc_g weaktest.*.txt *.def *.pdb *.obj libtcc_test_mt @$(MAKE) -C tests2 $@ @$(MAKE) -C pp $@ diff --git a/tests/tcctest.c b/tests/tcctest.c index eb47a0e..d46ec7a 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -3760,12 +3760,14 @@ void asm_dot_test(void) void asm_pcrel_test(void) { +#if defined(__i386__) unsigned o1, o2; /* subtract text-section label from forward or other-section label */ asm("1: mov $2f-1b,%%eax; mov %%eax,%0" : "=m"(o1)); /* verify ... */ asm("2: mov $2b,%%eax; sub $1b,%%eax; mov %%eax,%0" : "=m"(o2)); printf("%s : %x\n", __FUNCTION__, o1 - o2); /* should be zero */ +#endif } void asm_test(void) diff --git a/tests/tests2/Makefile b/tests/tests2/Makefile index 8ce149b..f485b25 100644 --- a/tests/tests2/Makefile +++ b/tests/tests2/Makefile @@ -55,10 +55,6 @@ endif # Some tests might need arguments ARGS = -ifneq (-$(CONFIG_WIN32)-,-yes-) -22_floating_point.test: FLAGS += -lm -24_math_library.test: FLAGS += -lm -endif 31_args.test : ARGS = arg1 arg2 arg3 arg4 arg5 46_grep.test : ARGS = '[^* ]*[:a:d: ]+\:\*-/: $$' $(SRC)/46_grep.c @@ -69,6 +65,10 @@ NORUN = # Some tests might need different flags FLAGS = 76_dollars_in_identifiers.test : FLAGS += -fdollars-in-identifiers +ifneq (-$(CONFIG_WIN32)-,-yes-) +22_floating_point.test: FLAGS += -lm +24_math_library.test: FLAGS += -lm +endif # These tests run several snippets from the same file one by one 60_errors_and_warnings.test : FLAGS += -dt -- cgit v1.2.3