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,
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 */
extern float strtof (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
#ifdef _WIN32
@ -436,7 +433,6 @@ typedef struct TokenSym {
struct Sym *sym_struct; /* direct pointer to structure */
struct Sym *sym_identifier; /* direct pointer to identifier */
int tok; /* token number */
int cnt;
int len;
char str[1];
} TokenSym;
@ -886,6 +882,11 @@ struct TCCState {
ElfW_Rel *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
/* PE info */
int pe_subsystem;

View File

@ -2137,9 +2137,6 @@ static int final_sections_reloc(TCCState *s1)
if (s1->nb_errors != 0)
return -1;
/* Some targets use static data between relocations (riscv64) */
tcc_enter_state (s1);
/* relocate sections */
/* XXX: ignore sections with allocated relocations ? */
for(i = 1; i < s1->nb_sections; i++) {
@ -2148,8 +2145,6 @@ static int final_sections_reloc(TCCState *s1)
relocate_section(s1, s);
}
tcc_exit_state();
/* relocate relocation entries if the relocation tables are
allocated in the executable */
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;
}
/*
* c = 0: reset symbol counter
* c = 1: increment symbol counter
* c = 2: check symbol counter
*/
static void check_fields (CType *type, int c)
static void check_fields (CType *type, int check)
{
Sym *s = type->ref;
int v;
while ((s = s->next) != NULL) {
if ((s->v & SYM_FIELD) &&
(s->type.t & VT_BTYPE) == VT_STRUCT &&
(s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) {
check_fields (&s->type, c);
}
v = s->v & ~SYM_FIELD;
if (v < tok_ident)
switch (c) {
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;
}
int v = s->v & ~SYM_FIELD;
if (v < SYM_FIRST_ANOM) {
TokenSym *ts = table_ident[v - TOK_IDENT];
if (check && (ts->tok & SYM_FIELD))
tcc_error("duplicate member '%s'", get_tok_str(v, NULL));
ts->tok ^= SYM_FIELD;
} else if ((s->type.t & VT_BTYPE) == VT_STRUCT)
check_fields (&s->type, check);
}
}
@ -4809,9 +4790,8 @@ do_decl:
if (ad.cleanup_func) {
tcc_warning("attribute '__cleanup__' ignored on type");
}
check_fields(type, 0);
check_fields(type, 1);
check_fields(type, 2);
check_fields(type, 0);
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 *);
#endif
#ifndef PAGESIZE
# define PAGESIZE 4096
#endif
/* ------------------------------------------------------------- */
/* Do all relocations (needed before using tcc_get_symbol())
Returns -1 on error. */
@ -87,19 +91,18 @@ LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
int fd = mkstemp(tmpfname);
unlink(tmpfname);
ftruncate(fd, size);
#ifdef __OpenBSD__
{ /* OpenBSD uses random 64 addresses that are far apart. This does not
work for the x86_64 target.
This might be a problem in the future for other targets. */
static char *prw = (char *) 0x100000000;
char *pex = prw + 0x40000000;
ptr = mmap (prw, size, PROT_READ|PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0);
prx = mmap (pex, size, PROT_READ|PROT_EXEC, MAP_SHARED | MAP_FIXED, fd, 0);
tcc_enter_state (s1);
prw += 0x100000000;
tcc_exit_state();
{
int offs;
size = (size + (PAGESIZE-1)) & ~(PAGESIZE-1);
offs = (size + (0x100000-1)) & ~(0x100000-1);
prx = NULL;
ptr = mmap(NULL, size + offs, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr != MAP_FAILED) {
/* mmap RX memory at a fixed distance */
munmap((char*)ptr + size, offs);
prx = mmap((char*)ptr + offs, size, PROT_READ|PROT_EXEC, MAP_SHARED|MAP_FIXED, fd, 0);
}
}
#else
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);
ptr_diff = (char*)prx - (char*)ptr;
close(fd);
//printf("map %p %p %p\n", ptr, prx, (void*)ptr_diff);
}
#else
ptr = tcc_malloc(size);
@ -155,8 +159,12 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
#ifdef CONFIG_TCC_BACKTRACE
rt_context *rc = &g_rtctxt;
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
char **envp = NULL;
#elif defined(__OpenBSD__)
extern char **environ;
char **envp = environ;
#else
char **envp = environ;
#endif
@ -333,9 +341,6 @@ static void set_pages_executable(TCCState *s1, void *ptr, unsigned long length)
void __clear_cache(void *beginning, void *end);
# ifndef HAVE_SELINUX
addr_t start, end;
# ifndef PAGESIZE
# define PAGESIZE 4096
# endif
start = (addr_t)ptr & ~(PAGESIZE - 1);
end = (addr_t)ptr + length;
end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);

View File

@ -57,9 +57,6 @@ endif
ifeq (,$(filter i386 x86_64,$(ARCH)))
TESTS := $(filter-out dlltest asm-c-connect-test,$(TESTS))
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
PATH := $(CURDIR)/$(TOP)$(if $(findstring ;,$(PATH)),;,:)$(PATH)