Use _WIN32 for a windows hosted tcc and define it for the PE target.

This commit is contained in:
grischka 2007-12-13 19:07:19 +00:00
parent b0d40c12da
commit 34140dd627
4 changed files with 1500 additions and 1496 deletions

View File

@ -1,9 +1,9 @@
/* ---------------------------------------------- */ /* ---------------------------------------------- */
/* alloca86b.S */ /* alloca86b.S */
.globl __bound___alloca .globl __bound__alloca
__bound___alloca: __bound__alloca:
pop %edx pop %edx
pop %eax pop %eax
mov %eax, %ecx mov %eax, %ecx

2
elf.h
View File

@ -21,7 +21,7 @@
#ifndef _ELF_H #ifndef _ELF_H
#define _ELF_H 1 #define _ELF_H 1
#ifndef WIN32 #ifndef _WIN32
#include <inttypes.h> #include <inttypes.h>
#else #else
#ifndef __int8_t_defined #ifndef __int8_t_defined

64
tcc.c
View File

@ -38,11 +38,11 @@
#include <fcntl.h> #include <fcntl.h>
#include <setjmp.h> #include <setjmp.h>
#include <time.h> #include <time.h>
#ifdef WIN32 #ifdef _WIN32
#include <sys/timeb.h> #include <sys/timeb.h>
// #include <windows.h> // #include <windows.h>
#endif #endif
#ifndef WIN32 #ifndef _WIN32
#include <sys/time.h> #include <sys/time.h>
#include <sys/ucontext.h> #include <sys/ucontext.h>
#include <sys/mman.h> #include <sys/mman.h>
@ -86,12 +86,12 @@
#define TCC_TARGET_I386 #define TCC_TARGET_I386
#endif #endif
#if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \ #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
!defined(TCC_TARGET_C67) !defined(TCC_TARGET_C67)
#define CONFIG_TCC_BCHECK /* enable bound checking code */ #define CONFIG_TCC_BCHECK /* enable bound checking code */
#endif #endif
#if defined(WIN32) && !defined(TCC_TARGET_PE) #if defined(_WIN32) && !defined(TCC_TARGET_PE)
#define CONFIG_TCC_STATIC #define CONFIG_TCC_STATIC
#endif #endif
@ -722,7 +722,7 @@ static const char tcc_keywords[] =
#define TOK_UIDENT TOK_DEFINE #define TOK_UIDENT TOK_DEFINE
#ifdef WIN32 #ifdef _WIN32
int __stdcall GetModuleFileNameA(void *, char *, int); int __stdcall GetModuleFileNameA(void *, char *, int);
void *__stdcall GetProcAddress(void *, const char *); void *__stdcall GetProcAddress(void *, const char *);
void *__stdcall GetModuleHandleA(const char *); void *__stdcall GetModuleHandleA(const char *);
@ -967,7 +967,7 @@ void *resolve_sym(TCCState *s1, const char *symbol, int type)
return NULL; return NULL;
} }
#elif !defined(WIN32) #elif !defined(_WIN32)
#include <dlfcn.h> #include <dlfcn.h>
@ -1035,7 +1035,7 @@ static int strstart(const char *str, const char *val, const char **ptr)
return 1; return 1;
} }
#ifdef WIN32 #ifdef _WIN32
char *normalize_slashes(char *path) char *normalize_slashes(char *path)
{ {
char *p; char *p;
@ -1061,6 +1061,20 @@ char *w32_tcc_lib_path(void)
} }
#endif #endif
void set_pages_executable(void *ptr, unsigned long length)
{
#ifdef _WIN32
unsigned long old_protect;
VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
#else
unsigned long start, end;
start = (unsigned long)ptr & ~(PAGESIZE - 1);
end = (unsigned long)ptr + length;
end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
#endif
}
/* memory management */ /* memory management */
#ifdef MEM_DEBUG #ifdef MEM_DEBUG
int mem_cur_size; int mem_cur_size;
@ -1887,7 +1901,9 @@ BufferedFile *tcc_open(TCCState *s1, const char *filename)
bf->buf_end = bf->buffer; bf->buf_end = bf->buffer;
bf->buffer[0] = CH_EOB; /* put eob symbol */ bf->buffer[0] = CH_EOB; /* put eob symbol */
pstrcpy(bf->filename, sizeof(bf->filename), filename); pstrcpy(bf->filename, sizeof(bf->filename), filename);
#ifdef _WIN32
normalize_slashes(bf->filename); normalize_slashes(bf->filename);
#endif
bf->line_num = 1; bf->line_num = 1;
bf->ifndef_macro = 0; bf->ifndef_macro = 0;
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr; bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
@ -9388,6 +9404,9 @@ static int tcc_compile(TCCState *s1)
ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0, ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
text_section->sh_num, NULL); text_section->sh_num, NULL);
getcwd(buf, sizeof(buf)); getcwd(buf, sizeof(buf));
#ifdef _WIN32
normalize_slashes(buf);
#endif
pstrcat(buf, sizeof(buf), "/"); pstrcat(buf, sizeof(buf), "/");
put_stabs_r(buf, N_SO, 0, 0, put_stabs_r(buf, N_SO, 0, 0,
text_section->data_offset, text_section, section_sym); text_section->data_offset, text_section, section_sym);
@ -9724,7 +9743,7 @@ static void rt_printline(unsigned long wanted_pc)
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
#if !defined(WIN32) && !defined(CONFIG_TCCBOOT) #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
#ifdef __i386__ #ifdef __i386__
@ -9888,24 +9907,8 @@ int tcc_relocate(TCCState *s1)
for(i = 1; i < s1->nb_sections; i++) { for(i = 1; i < s1->nb_sections; i++) {
s = s1->sections[i]; s = s1->sections[i];
if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) == if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
(SHF_ALLOC | SHF_EXECINSTR)) { (SHF_ALLOC | SHF_EXECINSTR))
#ifdef WIN32 set_pages_executable(s->data, s->data_offset);
{
unsigned long old_protect;
VirtualProtect(s->data, s->data_offset,
PAGE_EXECUTE_READWRITE, &old_protect);
}
#else
{
unsigned long start, end;
start = (unsigned long)(s->data) & ~(PAGESIZE - 1);
end = (unsigned long)(s->data + s->data_offset);
end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
mprotect((void *)start, end - start,
PROT_READ | PROT_WRITE | PROT_EXEC);
}
#endif
}
} }
return 0; return 0;
} }
@ -9921,7 +9924,7 @@ int tcc_run(TCCState *s1, int argc, char **argv)
prog_main = tcc_get_symbol_err(s1, "main"); prog_main = tcc_get_symbol_err(s1, "main");
if (do_debug) { if (do_debug) {
#if defined(WIN32) || defined(CONFIG_TCCBOOT) #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
error("debug mode currently not available for Windows"); error("debug mode currently not available for Windows");
#else #else
struct sigaction sigact; struct sigaction sigact;
@ -10022,6 +10025,7 @@ TCCState *tcc_new(void)
tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int"); tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
#ifdef TCC_TARGET_PE #ifdef TCC_TARGET_PE
tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short"); tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
tcc_define_symbol(s, "_WIN32", NULL);
#else #else
tcc_define_symbol(s, "__WCHAR_TYPE__", "int"); tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
#endif #endif
@ -10461,7 +10465,7 @@ static char *tcc_basename(const char *name)
char *p = strchr(name, 0); char *p = strchr(name, 0);
while (p > name while (p > name
&& p[-1] != '/' && p[-1] != '/'
#ifdef WIN32 #ifdef _WIN32
&& p[-1] != '\\' && p[-1] != '\\'
#endif #endif
) )
@ -10473,7 +10477,7 @@ static char *tcc_basename(const char *name)
static int64_t getclock_us(void) static int64_t getclock_us(void)
{ {
#ifdef WIN32 #ifdef _WIN32
struct _timeb tb; struct _timeb tb;
_ftime(&tb); _ftime(&tb);
return (tb.time * 1000LL + tb.millitm) * 1000LL; return (tb.time * 1000LL + tb.millitm) * 1000LL;
@ -10849,7 +10853,7 @@ int main(int argc, char **argv)
char objfilename[1024]; char objfilename[1024];
int64_t start_time = 0; int64_t start_time = 0;
#ifdef WIN32 #ifdef _WIN32
tcc_lib_path = w32_tcc_lib_path(); tcc_lib_path = w32_tcc_lib_path();
#endif #endif

View File

@ -397,7 +397,7 @@ ST int pe_find_import(TCCState * s1, const char *symbol, char *ret)
return sym_index; return sym_index;
} }
#ifdef WIN32 #ifdef _WIN32
ST void **pe_imp; ST void **pe_imp;
ST int nb_pe_imp; ST int nb_pe_imp;