From 6ed6a36a51065060bd5e9bb516b85ff796e05f30 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Thu, 28 Feb 2013 16:40:18 +0100 Subject: [PATCH] Flush caches before -running program On some architectures, ARM for instance, the data and instruction caches are not coherent with each other. This is a problem for the -run feature since instructions are written in memory, and are thus written in the data cache first and then later flushed to the main memory. If the instructions are executed before they are pushed out of the cache, then the processor will fetch the old content from the memory and not the newly generated code. The solution is to flush from the data cache all the data in the memory region containing the instructions and to invalidate the same region in the instruction cache. --- lib/libtcc1.c | 11 +++++++++++ tccrun.c | 1 + 2 files changed, 12 insertions(+) diff --git a/lib/libtcc1.c b/lib/libtcc1.c index dacee283..bfe683b0 100644 --- a/lib/libtcc1.c +++ b/lib/libtcc1.c @@ -689,3 +689,14 @@ void __va_end(struct __va_list_struct *ap) } #endif /* __x86_64__ */ + +/* Flushing for tccrun */ +#if defined(__x86_64__) || defined(__i386__) + +void __clear_cache(char *beginning, char *end) +{ +} + +#else +#warning __clear_cache not defined for this architecture, avoid using tcc -run +#endif diff --git a/tccrun.c b/tccrun.c index 50178a81..174b6c21 100644 --- a/tccrun.c +++ b/tccrun.c @@ -225,6 +225,7 @@ static void set_pages_executable(void *ptr, unsigned long length) end = (addr_t)ptr + length; end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1); mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC); + __clear_cache(ptr, prog_main + length); #endif } -- 2.11.4.GIT