mirror of
https://github.com/frida/tinycc
synced 2025-01-23 19:52:05 +03:00
Use _WIN32 for a windows hosted tcc and define it for the PE target.
This commit is contained in:
parent
b0d40c12da
commit
34140dd627
@ -1,9 +1,9 @@
|
||||
/* ---------------------------------------------- */
|
||||
/* alloca86b.S */
|
||||
|
||||
.globl __bound___alloca
|
||||
.globl __bound__alloca
|
||||
|
||||
__bound___alloca:
|
||||
__bound__alloca:
|
||||
pop %edx
|
||||
pop %eax
|
||||
mov %eax, %ecx
|
||||
|
100
tcc.c
100
tcc.c
@ -38,11 +38,11 @@
|
||||
#include <fcntl.h>
|
||||
#include <setjmp.h>
|
||||
#include <time.h>
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32
|
||||
#include <sys/timeb.h>
|
||||
// #include <windows.h>
|
||||
#endif
|
||||
#ifndef WIN32
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#include <sys/ucontext.h>
|
||||
#include <sys/mman.h>
|
||||
@ -86,12 +86,12 @@
|
||||
#define TCC_TARGET_I386
|
||||
#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)
|
||||
#define CONFIG_TCC_BCHECK /* enable bound checking code */
|
||||
#endif
|
||||
|
||||
#if defined(WIN32) && !defined(TCC_TARGET_PE)
|
||||
#if defined(_WIN32) && !defined(TCC_TARGET_PE)
|
||||
#define CONFIG_TCC_STATIC
|
||||
#endif
|
||||
|
||||
@ -722,7 +722,7 @@ static const char tcc_keywords[] =
|
||||
|
||||
#define TOK_UIDENT TOK_DEFINE
|
||||
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32
|
||||
int __stdcall GetModuleFileNameA(void *, char *, int);
|
||||
void *__stdcall GetProcAddress(void *, const char *);
|
||||
void *__stdcall GetModuleHandleA(const char *);
|
||||
@ -967,7 +967,7 @@ void *resolve_sym(TCCState *s1, const char *symbol, int type)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#elif !defined(WIN32)
|
||||
#elif !defined(_WIN32)
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
@ -1035,7 +1035,7 @@ static int strstart(const char *str, const char *val, const char **ptr)
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32
|
||||
char *normalize_slashes(char *path)
|
||||
{
|
||||
char *p;
|
||||
@ -1053,14 +1053,28 @@ char *w32_tcc_lib_path(void)
|
||||
GetModuleFileNameA(NULL, path, sizeof path);
|
||||
p = tcc_basename(normalize_slashes(strlwr(path)));
|
||||
if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
|
||||
p -= 5;
|
||||
p -= 5;
|
||||
else if (p > path)
|
||||
p--;
|
||||
p--;
|
||||
*p = 0;
|
||||
return strdup(path);
|
||||
}
|
||||
#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 */
|
||||
#ifdef MEM_DEBUG
|
||||
int mem_cur_size;
|
||||
@ -1887,7 +1901,9 @@ BufferedFile *tcc_open(TCCState *s1, const char *filename)
|
||||
bf->buf_end = bf->buffer;
|
||||
bf->buffer[0] = CH_EOB; /* put eob symbol */
|
||||
pstrcpy(bf->filename, sizeof(bf->filename), filename);
|
||||
#ifdef _WIN32
|
||||
normalize_slashes(bf->filename);
|
||||
#endif
|
||||
bf->line_num = 1;
|
||||
bf->ifndef_macro = 0;
|
||||
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
|
||||
@ -3566,16 +3582,16 @@ static inline void next_nomacro1(void)
|
||||
{
|
||||
TCCState *s1 = tcc_state;
|
||||
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;
|
||||
goto keep_tok_flags;
|
||||
goto keep_tok_flags;
|
||||
} else if (s1->include_stack_ptr == s1->include_stack ||
|
||||
!(parse_flags & PARSE_FLAG_PREPROCESS)) {
|
||||
/* no include left : end of file. */
|
||||
tok = TOK_EOF;
|
||||
} else {
|
||||
tok_flags &= ~TOK_FLAG_EOF;
|
||||
tok_flags &= ~TOK_FLAG_EOF;
|
||||
/* pop include file */
|
||||
|
||||
/* test if previous '#endif' was after a #ifdef at
|
||||
@ -3603,13 +3619,13 @@ static inline void next_nomacro1(void)
|
||||
break;
|
||||
|
||||
case '\n':
|
||||
file->line_num++;
|
||||
tok_flags |= TOK_FLAG_BOL;
|
||||
p++;
|
||||
file->line_num++;
|
||||
tok_flags |= TOK_FLAG_BOL;
|
||||
p++;
|
||||
if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
|
||||
goto redo_no_start;
|
||||
tok = TOK_LINEFEED;
|
||||
goto keep_tok_flags;
|
||||
goto keep_tok_flags;
|
||||
|
||||
case '#':
|
||||
/* XXX: simplify */
|
||||
@ -4168,8 +4184,8 @@ static int macro_subst_tok(TokenString *tok_str,
|
||||
parlevel++;
|
||||
else if (tok == ')')
|
||||
parlevel--;
|
||||
if (tok != TOK_LINEFEED)
|
||||
tok_str_add2(&str, tok, &tokc);
|
||||
if (tok != TOK_LINEFEED)
|
||||
tok_str_add2(&str, tok, &tokc);
|
||||
next_nomacro();
|
||||
}
|
||||
tok_str_add(&str, 0);
|
||||
@ -9388,6 +9404,9 @@ static int tcc_compile(TCCState *s1)
|
||||
ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
|
||||
text_section->sh_num, NULL);
|
||||
getcwd(buf, sizeof(buf));
|
||||
#ifdef _WIN32
|
||||
normalize_slashes(buf);
|
||||
#endif
|
||||
pstrcat(buf, sizeof(buf), "/");
|
||||
put_stabs_r(buf, N_SO, 0, 0,
|
||||
text_section->data_offset, text_section, section_sym);
|
||||
@ -9489,9 +9508,9 @@ static int tcc_preprocess(TCCState *s1)
|
||||
break;
|
||||
} else if (tok == TOK_LINEFEED) {
|
||||
last_is_space = 1;
|
||||
} else {
|
||||
if (!last_is_space)
|
||||
fputc(' ', s1->outfile);
|
||||
} else {
|
||||
if (!last_is_space)
|
||||
fputc(' ', s1->outfile);
|
||||
last_is_space = 0;
|
||||
}
|
||||
fputs(get_tok_str(tok, &tokc), s1->outfile);
|
||||
@ -9724,7 +9743,7 @@ static void rt_printline(unsigned long wanted_pc)
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
#if !defined(WIN32) && !defined(CONFIG_TCCBOOT)
|
||||
#if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
|
||||
|
||||
#ifdef __i386__
|
||||
|
||||
@ -9888,24 +9907,8 @@ int tcc_relocate(TCCState *s1)
|
||||
for(i = 1; i < s1->nb_sections; i++) {
|
||||
s = s1->sections[i];
|
||||
if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
|
||||
(SHF_ALLOC | SHF_EXECINSTR)) {
|
||||
#ifdef WIN32
|
||||
{
|
||||
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
|
||||
}
|
||||
(SHF_ALLOC | SHF_EXECINSTR))
|
||||
set_pages_executable(s->data, s->data_offset);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -9921,7 +9924,7 @@ int tcc_run(TCCState *s1, int argc, char **argv)
|
||||
prog_main = tcc_get_symbol_err(s1, "main");
|
||||
|
||||
if (do_debug) {
|
||||
#if defined(WIN32) || defined(CONFIG_TCCBOOT)
|
||||
#if defined(_WIN32) || defined(CONFIG_TCCBOOT)
|
||||
error("debug mode currently not available for Windows");
|
||||
#else
|
||||
struct sigaction sigact;
|
||||
@ -10022,6 +10025,7 @@ TCCState *tcc_new(void)
|
||||
tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
|
||||
#ifdef TCC_TARGET_PE
|
||||
tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
|
||||
tcc_define_symbol(s, "_WIN32", NULL);
|
||||
#else
|
||||
tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
|
||||
#endif
|
||||
@ -10461,7 +10465,7 @@ static char *tcc_basename(const char *name)
|
||||
char *p = strchr(name, 0);
|
||||
while (p > name
|
||||
&& p[-1] != '/'
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32
|
||||
&& p[-1] != '\\'
|
||||
#endif
|
||||
)
|
||||
@ -10473,7 +10477,7 @@ static char *tcc_basename(const char *name)
|
||||
|
||||
static int64_t getclock_us(void)
|
||||
{
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32
|
||||
struct _timeb tb;
|
||||
_ftime(&tb);
|
||||
return (tb.time * 1000LL + tb.millitm) * 1000LL;
|
||||
@ -10849,7 +10853,7 @@ int main(int argc, char **argv)
|
||||
char objfilename[1024];
|
||||
int64_t start_time = 0;
|
||||
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32
|
||||
tcc_lib_path = w32_tcc_lib_path();
|
||||
#endif
|
||||
|
||||
@ -10898,7 +10902,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
} else if (output_type != TCC_OUTPUT_MEMORY) {
|
||||
if (!outfile) {
|
||||
/* compute default outfile name */
|
||||
/* compute default outfile name */
|
||||
pstrcpy(objfilename, sizeof(objfilename) - 1,
|
||||
/* strip path */
|
||||
tcc_basename(files[0]));
|
||||
@ -10907,10 +10911,10 @@ int main(int argc, char **argv)
|
||||
#else
|
||||
if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
|
||||
char *ext = strrchr(objfilename, '.');
|
||||
if (!ext)
|
||||
if (!ext)
|
||||
goto default_outfile;
|
||||
/* add .o extension */
|
||||
strcpy(ext + 1, "o");
|
||||
strcpy(ext + 1, "o");
|
||||
} else {
|
||||
default_outfile:
|
||||
pstrcpy(objfilename, sizeof(objfilename), "a.out");
|
||||
|
Loading…
Reference in New Issue
Block a user