diff --git a/libtcc.c b/libtcc.c index 472f0d7..7224c52 100644 --- a/libtcc.c +++ b/libtcc.c @@ -502,6 +502,12 @@ PUB_FUNC void tcc_enter_state(TCCState *s1) tcc_state = s1; } +PUB_FUNC void tcc_exit_state(void) +{ + tcc_state = NULL; + POST_SEM(); +} + static void error1(int mode, const char *fmt, va_list ap) { char buf[2048]; @@ -513,11 +519,9 @@ static void error1(int mode, const char *fmt, va_list ap) /* can happen only if called from tcc_malloc(): 'out of memory' */ goto no_file; - if (!s1->error_set_jmp_enabled) { + if (s1 && !s1->error_set_jmp_enabled) /* tcc_state just was set by tcc_enter_state() */ - tcc_state = NULL; - POST_SEM(); - } + tcc_exit_state(); if (mode == ERROR_WARN) { if (s1->warn_none) @@ -684,8 +688,7 @@ static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd) Alternatively we could use thread local storage for those global variables, which may or may not have advantages */ - WAIT_SEM(); - tcc_state = s1; + tcc_enter_state(s1); if (setjmp(s1->error_jmp_buf) == 0) { int is_asm; @@ -722,8 +725,7 @@ static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd) preprocess_end(s1); tccelf_end_file(s1); - tcc_state = NULL; - POST_SEM(); + tcc_exit_state(); return s1->nb_errors != 0 ? -1 : 0; } diff --git a/tcc.h b/tcc.h index 12979a7..4bb9013 100644 --- a/tcc.h +++ b/tcc.h @@ -1544,6 +1544,12 @@ ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name); ST_FUNC int set_global_sym(TCCState *s1, const char *name, Section *sec, long offs); +/* Browse each elem of type in section starting at elem + using variable */ +#define for_each_elem(sec, startoff, elem, type) \ + for (elem = (type *) sec->data + startoff; \ + elem < (type *) (sec->data + sec->data_offset); elem++) + #ifndef TCC_TARGET_PE ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level); ST_FUNC int tcc_load_ldscript(TCCState *s1, int fd); @@ -1789,6 +1795,7 @@ ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename) #define total_bytes TCC_STATE_VAR(total_bytes) PUB_FUNC void tcc_enter_state(TCCState *s1); +PUB_FUNC void tcc_exit_state(void); /********************************************************/ #endif /* _TCC_H */ diff --git a/tccelf.c b/tccelf.c index b8dee7c..58f24a2 100644 --- a/tccelf.c +++ b/tccelf.c @@ -855,12 +855,6 @@ ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc) return &s1->sym_attrs[index]; } -/* Browse each elem of type in section starting at elem - using variable */ -#define for_each_elem(sec, startoff, elem, type) \ - for (elem = (type *) sec->data + startoff; \ - elem < (type *) (sec->data + sec->data_offset); elem++) - /* In an ELF file symbol table, the local symbols must appear below the global and weak ones. Since TCC cannot sort it while generating the code, we must do it after. All the relocation tables are also