tcc_enter/exit_state(): do not use!

tcc_enter/exit_state() are meant exclusively to protect
the tcc_compile() and its sub-functions in tccpp.c,
tccgen.c, tccasm.c and xxx-gen.c.

Other files that are part of libtcc simply must not use global
variables.

- riscv64/last_hi: move to TCCState
  from 72250bece2

- tccrun.c: Using a fixed address would not work anyway
  ("tcc -run tcc.c -run ..." for example)
  from baacb0f52a

- tests/Makefile: support for a platform doesn't make sense if
  it doesn't pass our basic tests.
  from 591feda103
Also:
- tccgen: cleanup "duplicate member" (only 2 passes,
  avoids additional TokenSym field)
  from 170be79a42
This commit is contained in:
grischka 2020-12-07 19:02:42 +01:00
parent a06fef3b11
commit 8ff705554d
6 changed files with 36 additions and 63 deletions

View file

@ -155,11 +155,6 @@ ST_FUNC void relocate_plt(TCCState *s1)
} }
} }
struct pcrel_hi {
addr_t addr, val;
};
static struct pcrel_hi last_hi;
void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
addr_t addr, addr_t val) addr_t addr, addr_t val)
{ {

9
tcc.h
View file

@ -45,9 +45,6 @@
/* XXX: need to define this to use them in non ISOC99 context */ /* XXX: need to define this to use them in non ISOC99 context */
extern float strtof (const char *__nptr, char **__endptr); extern float strtof (const char *__nptr, char **__endptr);
extern long double strtold (const char *__nptr, char **__endptr); extern long double strtold (const char *__nptr, char **__endptr);
# if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
extern char **environ;
# endif
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
@ -436,7 +433,6 @@ typedef struct TokenSym {
struct Sym *sym_struct; /* direct pointer to structure */ struct Sym *sym_struct; /* direct pointer to structure */
struct Sym *sym_identifier; /* direct pointer to identifier */ struct Sym *sym_identifier; /* direct pointer to identifier */
int tok; /* token number */ int tok; /* token number */
int cnt;
int len; int len;
char str[1]; char str[1];
} TokenSym; } TokenSym;
@ -886,6 +882,11 @@ struct TCCState {
ElfW_Rel *qrel; ElfW_Rel *qrel;
# define qrel s1->qrel # define qrel s1->qrel
#ifdef TCC_TARGET_RISCV64
struct pcrel_hi { addr_t addr, val; } last_hi;
# define last_hi s1->last_hi
#endif
#ifdef TCC_TARGET_PE #ifdef TCC_TARGET_PE
/* PE info */ /* PE info */
int pe_subsystem; int pe_subsystem;

View file

@ -2137,9 +2137,6 @@ static int final_sections_reloc(TCCState *s1)
if (s1->nb_errors != 0) if (s1->nb_errors != 0)
return -1; return -1;
/* Some targets use static data between relocations (riscv64) */
tcc_enter_state (s1);
/* relocate sections */ /* relocate sections */
/* XXX: ignore sections with allocated relocations ? */ /* XXX: ignore sections with allocated relocations ? */
for(i = 1; i < s1->nb_sections; i++) { for(i = 1; i < s1->nb_sections; i++) {
@ -2148,8 +2145,6 @@ static int final_sections_reloc(TCCState *s1)
relocate_section(s1, s); relocate_section(s1, s);
} }
tcc_exit_state();
/* relocate relocation entries if the relocation tables are /* relocate relocation entries if the relocation tables are
allocated in the executable */ allocated in the executable */
for(i = 1; i < s1->nb_sections; i++) { for(i = 1; i < s1->nb_sections; i++) {

View file

@ -4321,38 +4321,19 @@ static Sym * find_field (CType *type, int v, int *cumofs)
return s; return s;
} }
/* static void check_fields (CType *type, int check)
* c = 0: reset symbol counter
* c = 1: increment symbol counter
* c = 2: check symbol counter
*/
static void check_fields (CType *type, int c)
{ {
Sym *s = type->ref; Sym *s = type->ref;
int v;
while ((s = s->next) != NULL) { while ((s = s->next) != NULL) {
if ((s->v & SYM_FIELD) && int v = s->v & ~SYM_FIELD;
(s->type.t & VT_BTYPE) == VT_STRUCT && if (v < SYM_FIRST_ANOM) {
(s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) { TokenSym *ts = table_ident[v - TOK_IDENT];
check_fields (&s->type, c); if (check && (ts->tok & SYM_FIELD))
} tcc_error("duplicate member '%s'", get_tok_str(v, NULL));
v = s->v & ~SYM_FIELD; ts->tok ^= SYM_FIELD;
if (v < tok_ident) } else if ((s->type.t & VT_BTYPE) == VT_STRUCT)
switch (c) { check_fields (&s->type, check);
case 0:
table_ident[v - TOK_IDENT]->cnt = 0;
break;
case 1:
table_ident[v - TOK_IDENT]->cnt++;
break;
case 2:
if (table_ident[v - TOK_IDENT]->cnt != 1)
tcc_error("duplicate member '%s'",
get_tok_str(v, NULL));
break;
}
} }
} }
@ -4809,9 +4790,8 @@ do_decl:
if (ad.cleanup_func) { if (ad.cleanup_func) {
tcc_warning("attribute '__cleanup__' ignored on type"); tcc_warning("attribute '__cleanup__' ignored on type");
} }
check_fields(type, 0);
check_fields(type, 1); check_fields(type, 1);
check_fields(type, 2); check_fields(type, 0);
struct_layout(type, &ad); struct_layout(type, &ad);
} }
} }

View file

@ -63,6 +63,10 @@ static void *win64_add_function_table(TCCState *s1);
static void win64_del_function_table(void *); static void win64_del_function_table(void *);
#endif #endif
#ifndef PAGESIZE
# define PAGESIZE 4096
#endif
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
/* Do all relocations (needed before using tcc_get_symbol()) /* Do all relocations (needed before using tcc_get_symbol())
Returns -1 on error. */ Returns -1 on error. */
@ -87,19 +91,18 @@ LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
int fd = mkstemp(tmpfname); int fd = mkstemp(tmpfname);
unlink(tmpfname); unlink(tmpfname);
ftruncate(fd, size); ftruncate(fd, size);
#ifdef __OpenBSD__ #ifdef __OpenBSD__
{ /* OpenBSD uses random 64 addresses that are far apart. This does not {
work for the x86_64 target. int offs;
This might be a problem in the future for other targets. */ size = (size + (PAGESIZE-1)) & ~(PAGESIZE-1);
static char *prw = (char *) 0x100000000; offs = (size + (0x100000-1)) & ~(0x100000-1);
char *pex = prw + 0x40000000; prx = NULL;
ptr = mmap(NULL, size + offs, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
ptr = mmap (prw, size, PROT_READ|PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0); if (ptr != MAP_FAILED) {
prx = mmap (pex, size, PROT_READ|PROT_EXEC, MAP_SHARED | MAP_FIXED, fd, 0); /* mmap RX memory at a fixed distance */
tcc_enter_state (s1); munmap((char*)ptr + size, offs);
prw += 0x100000000; prx = mmap((char*)ptr + offs, size, PROT_READ|PROT_EXEC, MAP_SHARED|MAP_FIXED, fd, 0);
tcc_exit_state(); }
} }
#else #else
ptr = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); ptr = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
@ -111,6 +114,7 @@ LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, prx); dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, prx);
ptr_diff = (char*)prx - (char*)ptr; ptr_diff = (char*)prx - (char*)ptr;
close(fd); close(fd);
//printf("map %p %p %p\n", ptr, prx, (void*)ptr_diff);
} }
#else #else
ptr = tcc_malloc(size); ptr = tcc_malloc(size);
@ -155,8 +159,12 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
#ifdef CONFIG_TCC_BACKTRACE #ifdef CONFIG_TCC_BACKTRACE
rt_context *rc = &g_rtctxt; rt_context *rc = &g_rtctxt;
#endif #endif
#if defined(__APPLE__) || defined(__FreeBSD__) #if defined(__APPLE__) || defined(__FreeBSD__)
char **envp = NULL; char **envp = NULL;
#elif defined(__OpenBSD__)
extern char **environ;
char **envp = environ;
#else #else
char **envp = environ; char **envp = environ;
#endif #endif
@ -333,9 +341,6 @@ static void set_pages_executable(TCCState *s1, void *ptr, unsigned long length)
void __clear_cache(void *beginning, void *end); void __clear_cache(void *beginning, void *end);
# ifndef HAVE_SELINUX # ifndef HAVE_SELINUX
addr_t start, end; addr_t start, end;
# ifndef PAGESIZE
# define PAGESIZE 4096
# endif
start = (addr_t)ptr & ~(PAGESIZE - 1); start = (addr_t)ptr & ~(PAGESIZE - 1);
end = (addr_t)ptr + length; end = (addr_t)ptr + length;
end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1); end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);

View file

@ -57,9 +57,6 @@ endif
ifeq (,$(filter i386 x86_64,$(ARCH))) ifeq (,$(filter i386 x86_64,$(ARCH)))
TESTS := $(filter-out dlltest asm-c-connect-test,$(TESTS)) TESTS := $(filter-out dlltest asm-c-connect-test,$(TESTS))
endif endif
ifeq ($(TARGETOS),OpenBSD)
TESTS := $(filter-out libtest_mt test3 dlltest asm-c-connect-test pp-dir btest test1b,$(TESTS))
endif
ifeq ($(OS),Windows_NT) # for libtcc_test to find libtcc.dll ifeq ($(OS),Windows_NT) # for libtcc_test to find libtcc.dll
PATH := $(CURDIR)/$(TOP)$(if $(findstring ;,$(PATH)),;,:)$(PATH) PATH := $(CURDIR)/$(TOP)$(if $(findstring ;,$(PATH)),;,:)$(PATH)