Use _WIN32 for a windows hosted tcc and define it for the PE target.
This commit is contained in:
parent
b0d40c12da
commit
34140dd627
4 changed files with 1500 additions and 1496 deletions
|
@ -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
|
||||||
|
|
100
tcc.c
100
tcc.c
|
@ -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;
|
||||||
|
@ -1053,14 +1053,28 @@ char *w32_tcc_lib_path(void)
|
||||||
GetModuleFileNameA(NULL, path, sizeof path);
|
GetModuleFileNameA(NULL, path, sizeof path);
|
||||||
p = tcc_basename(normalize_slashes(strlwr(path)));
|
p = tcc_basename(normalize_slashes(strlwr(path)));
|
||||||
if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
|
if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
|
||||||
p -= 5;
|
p -= 5;
|
||||||
else if (p > path)
|
else if (p > path)
|
||||||
p--;
|
p--;
|
||||||
*p = 0;
|
*p = 0;
|
||||||
return strdup(path);
|
return strdup(path);
|
||||||
}
|
}
|
||||||
#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;
|
||||||
|
@ -3566,16 +3582,16 @@ static inline void next_nomacro1(void)
|
||||||
{
|
{
|
||||||
TCCState *s1 = tcc_state;
|
TCCState *s1 = tcc_state;
|
||||||
if ((parse_flags & PARSE_FLAG_LINEFEED)
|
if ((parse_flags & PARSE_FLAG_LINEFEED)
|
||||||
&& !(tok_flags & TOK_FLAG_EOF)) {
|
&& !(tok_flags & TOK_FLAG_EOF)) {
|
||||||
tok_flags |= TOK_FLAG_EOF;
|
tok_flags |= TOK_FLAG_EOF;
|
||||||
tok = TOK_LINEFEED;
|
tok = TOK_LINEFEED;
|
||||||
goto keep_tok_flags;
|
goto keep_tok_flags;
|
||||||
} else if (s1->include_stack_ptr == s1->include_stack ||
|
} else if (s1->include_stack_ptr == s1->include_stack ||
|
||||||
!(parse_flags & PARSE_FLAG_PREPROCESS)) {
|
!(parse_flags & PARSE_FLAG_PREPROCESS)) {
|
||||||
/* no include left : end of file. */
|
/* no include left : end of file. */
|
||||||
tok = TOK_EOF;
|
tok = TOK_EOF;
|
||||||
} else {
|
} else {
|
||||||
tok_flags &= ~TOK_FLAG_EOF;
|
tok_flags &= ~TOK_FLAG_EOF;
|
||||||
/* pop include file */
|
/* pop include file */
|
||||||
|
|
||||||
/* test if previous '#endif' was after a #ifdef at
|
/* test if previous '#endif' was after a #ifdef at
|
||||||
|
@ -3603,13 +3619,13 @@ static inline void next_nomacro1(void)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '\n':
|
case '\n':
|
||||||
file->line_num++;
|
file->line_num++;
|
||||||
tok_flags |= TOK_FLAG_BOL;
|
tok_flags |= TOK_FLAG_BOL;
|
||||||
p++;
|
p++;
|
||||||
if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
|
if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
|
||||||
goto redo_no_start;
|
goto redo_no_start;
|
||||||
tok = TOK_LINEFEED;
|
tok = TOK_LINEFEED;
|
||||||
goto keep_tok_flags;
|
goto keep_tok_flags;
|
||||||
|
|
||||||
case '#':
|
case '#':
|
||||||
/* XXX: simplify */
|
/* XXX: simplify */
|
||||||
|
@ -4168,8 +4184,8 @@ static int macro_subst_tok(TokenString *tok_str,
|
||||||
parlevel++;
|
parlevel++;
|
||||||
else if (tok == ')')
|
else if (tok == ')')
|
||||||
parlevel--;
|
parlevel--;
|
||||||
if (tok != TOK_LINEFEED)
|
if (tok != TOK_LINEFEED)
|
||||||
tok_str_add2(&str, tok, &tokc);
|
tok_str_add2(&str, tok, &tokc);
|
||||||
next_nomacro();
|
next_nomacro();
|
||||||
}
|
}
|
||||||
tok_str_add(&str, 0);
|
tok_str_add(&str, 0);
|
||||||
|
@ -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);
|
||||||
|
@ -9489,9 +9508,9 @@ static int tcc_preprocess(TCCState *s1)
|
||||||
break;
|
break;
|
||||||
} else if (tok == TOK_LINEFEED) {
|
} else if (tok == TOK_LINEFEED) {
|
||||||
last_is_space = 1;
|
last_is_space = 1;
|
||||||
} else {
|
} else {
|
||||||
if (!last_is_space)
|
if (!last_is_space)
|
||||||
fputc(' ', s1->outfile);
|
fputc(' ', s1->outfile);
|
||||||
last_is_space = 0;
|
last_is_space = 0;
|
||||||
}
|
}
|
||||||
fputs(get_tok_str(tok, &tokc), s1->outfile);
|
fputs(get_tok_str(tok, &tokc), s1->outfile);
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -10898,7 +10902,7 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
} else if (output_type != TCC_OUTPUT_MEMORY) {
|
} else if (output_type != TCC_OUTPUT_MEMORY) {
|
||||||
if (!outfile) {
|
if (!outfile) {
|
||||||
/* compute default outfile name */
|
/* compute default outfile name */
|
||||||
pstrcpy(objfilename, sizeof(objfilename) - 1,
|
pstrcpy(objfilename, sizeof(objfilename) - 1,
|
||||||
/* strip path */
|
/* strip path */
|
||||||
tcc_basename(files[0]));
|
tcc_basename(files[0]));
|
||||||
|
@ -10907,10 +10911,10 @@ int main(int argc, char **argv)
|
||||||
#else
|
#else
|
||||||
if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
|
if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
|
||||||
char *ext = strrchr(objfilename, '.');
|
char *ext = strrchr(objfilename, '.');
|
||||||
if (!ext)
|
if (!ext)
|
||||||
goto default_outfile;
|
goto default_outfile;
|
||||||
/* add .o extension */
|
/* add .o extension */
|
||||||
strcpy(ext + 1, "o");
|
strcpy(ext + 1, "o");
|
||||||
} else {
|
} else {
|
||||||
default_outfile:
|
default_outfile:
|
||||||
pstrcpy(objfilename, sizeof(objfilename), "a.out");
|
pstrcpy(objfilename, sizeof(objfilename), "a.out");
|
||||||
|
|
Loading…
Reference in a new issue