Remove globals from tcc code ##cleanup

This commit is contained in:
pancake 2022-01-25 00:11:08 +01:00 committed by pancake
parent 785e500a7e
commit 51032c662d
11 changed files with 1822 additions and 2377 deletions

View File

@ -1371,7 +1371,6 @@ static int cmd_type(void *data, const char *input) {
if (out) {
// remove previous types and save new edited types
sdb_reset (TDB);
r_parse_c_reset (core->parser);
r_anal_save_parsed_type (core->anal, out);
free (out);
}
@ -1687,7 +1686,6 @@ static int cmd_type(void *data, const char *input) {
r_core_cmd_help (core, help_msg_t_minus);
} else if (input[1] == '*') {
sdb_reset (TDB);
r_parse_c_reset (core->parser);
} else {
const char *name = r_str_trim_head_ro (input + 1);
if (*name) {

View File

@ -22,23 +22,14 @@
/********************************************************/
/* global variables */
ST_DATA RPVector *tcc_typedefs;
/* use GNU C extensions */
ST_DATA int gnu_ext = 1;
/* use TinyCC extensions */
ST_DATA int tcc_ext = 1;
/* XXX: get rid of this ASAP */
ST_DATA struct TCCState *tcc_state;
ST_DATA R_TH_LOCAL RPVector *tcc_typedefs;
/********************************************************/
#ifdef __WINDOWS__
// GCC appears to use '/' for relative paths and '\\' for absolute paths on Windows
static char *normalize_slashes(char *path)
{
static char *normalize_slashes(char *path) {
char *p;
if (path[1] == ':') {
for (p = path + 2; *p; p++) {
@ -58,8 +49,7 @@ static char *normalize_slashes(char *path)
#endif
/* strcat and truncate. */
PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
{
PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s) {
int len = strlen (buf);
if (len < buf_size) {
r_str_ncpy (buf + len, s, buf_size - len);
@ -67,19 +57,18 @@ PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
return buf;
}
PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num)
{
PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num) {
memcpy (out, in, num);
out[num] = '\0';
return out;
}
/* extract the basename of a file */
PUB_FUNC char *tcc_basename(const char *name)
{
PUB_FUNC char *tcc_basename(const char *name) {
char *p = strchr (name, 0);
while (p && p > name && !IS_DIRSEP (p[-1]))
while (p && p > name && !IS_DIRSEP (p[-1])) {
--p;
}
return p;
}
@ -87,33 +76,13 @@ PUB_FUNC char *tcc_basename(const char *name)
*
* (if no extension, return pointer to end-of-string)
*/
PUB_FUNC char *tcc_fileextension(const char *name)
{
PUB_FUNC char *tcc_fileextension(const char *name) {
char *b = tcc_basename (name);
char *e = strrchr (b, '.');
return e? e: strchr (b, 0);
}
/********************************************************/
/* memory management */
PUB_FUNC void *tcc_mallocz(unsigned long size)
{
void *ptr;
ptr = malloc (size);
memset (ptr, 0, size);
return ptr;
}
PUB_FUNC void tcc_memstats(void)
{
#ifdef MEM_DEBUG
printf ("memory: %d byte(s), max = %d byte(s)\n", mem_cur_size, mem_max_size);
#endif
}
/********************************************************/
/* dynarrays */
@ -150,15 +119,14 @@ ST_FUNC void dynarray_reset(void *pp, int *n)
*(void **) pp = NULL;
}
static void tcc_split_path(TCCState *s, void ***p_ary, int *p_nb_ary, const char *in)
{
static void tcc_split_path(TCCState *s, void ***p_ary, int *p_nb_ary, const char *in) {
const char *p;
do {
int c;
CString str;
cstr_new (&str);
for (p = in; c = *p, c != '\0' && c != PATHSEP; p++) {
for (p = in; c = *p, c != '\0' && c != *R_SYS_ENVSEP; p++) {
if (c == '{' && p[1] && p[2] == '}') {
c = p[1], p += 2;
if (c == 'B') {
@ -176,29 +144,26 @@ static void tcc_split_path(TCCState *s, void ***p_ary, int *p_nb_ary, const char
/********************************************************/
static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
{
static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap) {
int len;
len = strlen (buf);
vsnprintf (buf + len, buf_size - len, fmt, ap);
}
PUB_FUNC void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
{
PUB_FUNC void strcat_printf(char *buf, int buf_size, const char *fmt, ...) {
va_list ap;
va_start (ap, fmt);
strcat_vprintf (buf, buf_size, fmt, ap);
va_end (ap);
}
static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
{
static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap) {
char buf[2048];
BufferedFile **pf, *f;
buf[0] = '\0';
/* use upper file if inline ":asm:" or token ":paste:" */
for (f = file; f && f->filename[0] == ':'; f = f->prev) {
for (f = s1->file; f && f->filename[0] == ':'; f = f->prev) {
;
}
if (f) {
@ -224,8 +189,8 @@ static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
strcat_vprintf (buf, sizeof(buf), fmt, ap);
if (!s1->error_func) {
/* default case: stderr */
fprintf (stderr, "%s\n", buf);
/* default case */
eprintf ("%s\n", buf);
} else {
s1->error_func (s1->error_opaque, buf);
}
@ -242,9 +207,7 @@ LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque,
}
/* error without aborting current compilation */
PUB_FUNC void tcc_error(const char *fmt, ...)
{
TCCState *s1 = tcc_state;
PUB_FUNC void tcc_error(TCCState *s1, const char *fmt, ...) {
va_list ap;
va_start (ap, fmt);
@ -252,9 +215,7 @@ PUB_FUNC void tcc_error(const char *fmt, ...)
va_end (ap);
}
PUB_FUNC void tcc_warning(const char *fmt, ...)
{
TCCState *s1 = tcc_state;
PUB_FUNC void tcc_warning(TCCState *s1, const char *fmt, ...) {
va_list ap;
if (s1->warn_none) {
@ -269,12 +230,15 @@ PUB_FUNC void tcc_warning(const char *fmt, ...)
/********************************************************/
/* I/O layer */
ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
{
BufferedFile *bf;
ST_FUNC bool tcc_open_bf(TCCState *s1, const char *filename, int initlen) {
int buflen = initlen? initlen: IO_BUF_SIZE;
bf = malloc (sizeof(BufferedFile) + buflen);
BufferedFile *bf = malloc (sizeof (BufferedFile) + buflen);
if (!bf) {
// err
eprintf ("Error\n");
return false;
}
bf->buf_ptr = bf->buffer;
bf->buf_end = bf->buffer + initlen;
bf->buf_end[0] = CH_EOB;/* put eob symbol */
@ -286,23 +250,22 @@ ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
bf->ifndef_macro = 0;
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
bf->fd = -1;
bf->prev = file;
file = bf;
bf->prev = s1->file;
s1->file = bf;
return true;
}
ST_FUNC void tcc_close(void)
{
BufferedFile *bf = file;
ST_FUNC void tcc_close(TCCState *s1) {
BufferedFile *bf = s1->file;
if (bf->fd > 0) {
close (bf->fd);
total_lines += bf->line_num;
s1->total_lines += bf->line_num;
}
file = bf->prev;
s1->file = bf->prev;
free (bf);
}
ST_FUNC int tcc_open(TCCState *s1, const char *filename)
{
ST_FUNC int tcc_open(TCCState *s1, const char *filename) {
int fd;
if (!strcmp (filename, "-")) {
fd = 0, filename = "stdin";
@ -318,18 +281,17 @@ ST_FUNC int tcc_open(TCCState *s1, const char *filename)
}
tcc_open_bf (s1, filename, 0);
file->fd = fd;
s1->file->fd = fd;
return fd;
}
/* compile the C file opened in 'file'. Return non zero if errors. */
static int tcc_compile(TCCState *s1)
{
static int tcc_compile(TCCState *s1) {
Sym *define_start;
preprocess_init (s1);
funcname = "";
s1->funcname = "";
/* define some often used types */
int8_type.t = VT_INT8;
@ -338,16 +300,16 @@ static int tcc_compile(TCCState *s1)
int64_type.t = VT_INT64;
char_pointer_type.t = VT_INT8;
mk_pointer (&char_pointer_type);
mk_pointer (s1, &char_pointer_type);
if (tcc_state->bits != 64) {
if (s1->bits != 64) {
size_type.t = VT_INT32;
} else {
size_type.t = VT_INT64;
}
func_old_type.t = VT_FUNC;
func_old_type.ref = sym_push (SYM_FIELD, &int32_type, FUNC_CDECL, FUNC_OLD);
func_old_type.ref = sym_push (s1, SYM_FIELD, &int32_type, FUNC_CDECL, FUNC_OLD);
// FIXME: Should depend on the target options too
#ifdef TCC_TARGET_ARM
@ -368,120 +330,110 @@ static int tcc_compile(TCCState *s1)
}
#endif
define_start = define_stack;
nocode_wanted = 1;
define_start = s1->define_stack;
s1->nocode_wanted = 1;
#ifndef __wasi__
if (setjmp (s1->error_jmp_buf) == 0) {
s1->nb_errors = 0;
s1->error_set_jmp_enabled = true;
ch = file->buf_ptr[0];
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
s1->ch = s1->file->buf_ptr[0];
s1->tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
s1->parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
// parse_flags = PARSE_FLAG_TOK_NUM;
// pvtop = vtop;
next ();
decl0 (VT_CONST, 0);
if (tok != TOK_EOF) {
expect ("declaration");
next (s1);
decl0 (s1, VT_CONST, 0);
if (s1->tok != TOK_EOF) {
expect (s1, "declaration");
}
#if 0
if (pvtop != vtop) {
fprintf (stderr, "internal compiler error:"
eprintf ("internal compiler error:"
" vstack leak? (%d)", vtop - pvtop);
}
#endif
}
#endif
s1->error_set_jmp_enabled = false;
/* reset define stack, but leave -Dsymbols (may be incorrect if
they are undefined) */
free_defines (define_start);
free_defines (s1, define_start);
sym_pop (&global_stack, NULL);
sym_pop (&local_stack, NULL);
sym_pop (s1, &s1->global_stack, NULL);
sym_pop (s1, &s1->local_stack, NULL);
return s1->nb_errors != 0? -1: 0;
}
LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
{
LIBTCCAPI int tcc_compile_string(TCCState *s1, const char *str) {
int len = strlen (str);
tcc_open_bf (s, "<string>", len);
memcpy (file->buffer, str, len);
int ret = tcc_compile (s);
tcc_close ();
if (!tcc_open_bf (s1, "<string>", len)) {
return false;
}
memcpy (s1->file->buffer, str, len);
int ret = tcc_compile (s1);
tcc_close (s1);
return ret;
}
/* define a preprocessor symbol. A value can also be provided with the '=' operator */
LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
{
int len1, len2;
/* default value */
if (!value) {
value = "1";
}
len1 = strlen (sym);
len2 = strlen (value);
int len1 = strlen (sym);
int len2 = strlen (value);
/* init file structure */
tcc_open_bf (s1, "<define>", len1 + len2 + 1);
memcpy (file->buffer, sym, len1);
file->buffer[len1] = ' ';
memcpy (file->buffer + len1 + 1, value, len2);
memcpy (s1->file->buffer, sym, len1);
s1->file->buffer[len1] = ' ';
memcpy (s1->file->buffer + len1 + 1, value, len2);
/* parse with define parser */
ch = file->buf_ptr[0];
next_nomacro ();
parse_define ();
s1->ch = s1->file->buf_ptr[0];
next_nomacro (s1);
parse_define (s1);
tcc_close ();
tcc_close (s1);
}
/* undefine a preprocessor symbol */
LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
{
TokenSym *ts;
Sym *s;
ts = tok_alloc (sym, strlen (sym));
s = define_find (ts->tok);
LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym) {
TokenSym *ts = tok_alloc (s1, sym, strlen (sym));
Sym *s = define_find (s1, ts->tok);
/* undefine symbol by putting an invalid name */
if (s) {
define_undef (s);
define_undef (s1, s);
}
}
/* cleanup all static data used during compilation */
static void tcc_cleanup(void)
{
static void tcc_cleanup(TCCState *s1) {
int i, n;
if (!tcc_state) {
return;
}
tcc_state = NULL;
/* free -D defines */
free_defines (NULL);
free_defines (s1, NULL);
/* free tokens */
n = tok_ident - TOK_IDENT;
n = s1->tok_ident - TOK_IDENT;
for (i = 0; i < n; i++) {
free (table_ident[i]);
free (s1->table_ident[i]);
}
free (table_ident);
R_FREE (s1->table_ident);
/* free sym_pools */
dynarray_reset (&sym_pools, &nb_sym_pools);
dynarray_reset (&s1->sym_pools, &s1->nb_sym_pools);
/* string buffer */
cstr_free (&tokcstr);
cstr_free (&s1->tokcstr);
/* reset symbol stack */
sym_free_first = NULL;
s1->sym_free_first = NULL;
/* cleanup from error/setjmp */
macro_ptr = NULL;
s1->macro_ptr = NULL;
}
static void tcc_init_defines(TCCState *s) {
@ -492,10 +444,10 @@ static void tcc_init_defines(TCCState *s) {
int a, b, c;
/* we add dummy defines for some special macros to speed up tests
and to have working defined() */
define_push (TOK___LINE__, MACRO_OBJ, NULL, NULL);
define_push (TOK___FILE__, MACRO_OBJ, NULL, NULL);
define_push (TOK___DATE__, MACRO_OBJ, NULL, NULL);
define_push (TOK___TIME__, MACRO_OBJ, NULL, NULL);
define_push (s, TOK___LINE__, MACRO_OBJ, NULL, NULL);
define_push (s, TOK___FILE__, MACRO_OBJ, NULL, NULL);
define_push (s, TOK___DATE__, MACRO_OBJ, NULL, NULL);
define_push (s, TOK___TIME__, MACRO_OBJ, NULL, NULL);
/* define __TINYC__ 92X */
sscanf (TCC_VERSION, "%d.%d.%d", &a, &b, &c);
@ -606,31 +558,28 @@ static void tcc_init_defines(TCCState *s) {
#endif
}
LIBTCCAPI TCCState *tcc_new(const char *arch, int bits, const char *os)
{
TCCState *s;
LIBTCCAPI TCCState *tcc_new(const char *arch, int bits, const char *os) {
if (!arch || !os) {
return NULL;
}
tcc_cleanup ();
s = tcc_mallocz (sizeof (TCCState));
// tcc_cleanup (NULL); // wtf no globals anymore
TCCState *s = calloc (sizeof (TCCState), 1);
if (!s) {
return NULL;
}
tcc_state = s;
s->arch = strdup (arch);
s->bits = bits;
s->os = strdup (os);
s->anon_sym = SYM_FIRST_ANOM;
s->output_type = TCC_OUTPUT_MEMORY;
preprocess_new ();
preprocess_new (s);
s->include_stack_ptr = s->include_stack;
return s;
}
LIBTCCAPI void tcc_delete(TCCState *s1)
{
tcc_cleanup ();
LIBTCCAPI void tcc_delete(TCCState *s1) {
tcc_cleanup (s1);
/* free include paths */
dynarray_reset (&s1->cached_includes, &s1->nb_cached_includes);
@ -671,7 +620,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
int ret = tcc_open (s1, filename);
if (ret < 0) {
if (flags & AFF_PRINT_ERROR) {
fprintf (stderr, "file '%s' not found\n", filename);
eprintf ("file '%s' not found\n", filename);
}
return ret;
}
@ -690,26 +639,25 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
goto the_end;
}
if (ret < 0) {
tcc_error ("unrecognized file type");
tcc_error (s1, "unrecognized file type");
}
the_end:
tcc_close ();
tcc_close (s1);
return ret;
}
LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename, const char *directory)
{
LIBTCCAPI int tcc_add_file(TCCState *s1, const char *filename, const char *directory) {
if (directory) {
free (dir_name);
dir_name = strdup (directory);
free (s1->dir_name);
s1->dir_name = strdup (directory);
}
int flags = AFF_PRINT_ERROR;
if (s->output_type == TCC_OUTPUT_PREPROCESS) {
if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
flags |= AFF_PREPROCESS;
}
return tcc_add_file_internal (s, filename, flags);
return tcc_add_file_internal (s1, filename, flags);
}
#define WD_ALL 0x0001/* warning is activated when using -Wall */

View File

@ -1,234 +0,0 @@
/* Table of DBX symbol codes for the GNU system.
Copyright (C) 1988, 1997 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* This contains contribution from Cygnus Support. */
/* Global variable. Only the name is significant.
To find the address, look in the corresponding external symbol. */
__define_stab (N_GSYM, 0x20, "GSYM")
/* Function name for BSD Fortran. Only the name is significant.
To find the address, look in the corresponding external symbol. */
__define_stab (N_FNAME, 0x22, "FNAME")
/* Function name or text-segment variable for C. Value is its address.
Desc is supposedly starting line number, but GCC doesn't set it
and DBX seems not to miss it. */
__define_stab (N_FUN, 0x24, "FUN")
/* Data-segment variable with internal linkage. Value is its address.
"Static Sym". */
__define_stab (N_STSYM, 0x26, "STSYM")
/* BSS-segment variable with internal linkage. Value is its address. */
__define_stab (N_LCSYM, 0x28, "LCSYM")
/* Name of main routine. Only the name is significant.
This is not used in C. */
__define_stab (N_MAIN, 0x2a, "MAIN")
/* Global symbol in Pascal.
Supposedly the value is its line number; I'm skeptical. */
__define_stab (N_PC, 0x30, "PC")
/* Number of symbols: 0, files,,funcs,lines according to Ultrix V4.0. */
__define_stab (N_NSYMS, 0x32, "NSYMS")
/* "No DST map for sym: name, ,0,type,ignored" according to Ultrix V4.0. */
__define_stab (N_NOMAP, 0x34, "NOMAP")
/* New stab from Solaris. I don't know what it means, but it
don't seem to contain useful information. */
__define_stab (N_OBJ, 0x38, "OBJ")
/* New stab from Solaris. I don't know what it means, but it
don't seem to contain useful information. Possibly related to the
optimization flags used in this module. */
__define_stab (N_OPT, 0x3c, "OPT")
/* Register variable. Value is number of register. */
__define_stab (N_RSYM, 0x40, "RSYM")
/* Modula-2 compilation unit. Can someone say what info it contains? */
__define_stab (N_M2C, 0x42, "M2C")
/* Line number in text segment. Desc is the line number;
value is corresponding address. */
__define_stab (N_SLINE, 0x44, "SLINE")
/* Similar, for data segment. */
__define_stab (N_DSLINE, 0x46, "DSLINE")
/* Similar, for bss segment. */
__define_stab (N_BSLINE, 0x48, "BSLINE")
/* Sun's source-code browser stabs. ?? Don't know what the fields are.
Supposedly the field is "path to associated .cb file". THIS VALUE
OVERLAPS WITH N_BSLINE! */
__define_stab (N_BROWS, 0x48, "BROWS")
/* GNU Modula-2 definition module dependency. Value is the modification time
of the definition file. Other is non-zero if it is imported with the
GNU M2 keyword %INITIALIZE. Perhaps N_M2C can be used if there
are enough empty fields? */
__define_stab(N_DEFD, 0x4a, "DEFD")
/* THE FOLLOWING TWO STAB VALUES CONFLICT. Happily, one is for Modula-2
and one is for C++. Still,... */
/* GNU C++ exception variable. Name is variable name. */
__define_stab (N_EHDECL, 0x50, "EHDECL")
/* Modula2 info "for imc": name,,0,0,0 according to Ultrix V4.0. */
__define_stab (N_MOD2, 0x50, "MOD2")
/* GNU C++ `catch' clause. Value is its address. Desc is nonzero if
this entry is immediately followed by a CAUGHT stab saying what exception
was caught. Multiple CAUGHT stabs means that multiple exceptions
can be caught here. If Desc is 0, it means all exceptions are caught
here. */
__define_stab (N_CATCH, 0x54, "CATCH")
/* Structure or union element. Value is offset in the structure. */
__define_stab (N_SSYM, 0x60, "SSYM")
/* Name of main source file.
Value is starting text address of the compilation. */
__define_stab (N_SO, 0x64, "SO")
/* Automatic variable in the stack. Value is offset from frame pointer.
Also used for type descriptions. */
__define_stab (N_LSYM, 0x80, "LSYM")
/* Beginning of an include file. Only Sun uses this.
In an object file, only the name is significant.
The Sun linker puts data into some of the other fields. */
__define_stab (N_BINCL, 0x82, "BINCL")
/* Name of sub-source file (#include file).
Value is starting text address of the compilation. */
__define_stab (N_SOL, 0x84, "SOL")
/* Parameter variable. Value is offset from argument pointer.
(On most machines the argument pointer is the same as the frame pointer. */
__define_stab (N_PSYM, 0xa0, "PSYM")
/* End of an include file. No name.
This and N_BINCL act as brackets around the file's output.
In an object file, there is no significant data in this entry.
The Sun linker puts data into some of the fields. */
__define_stab (N_EINCL, 0xa2, "EINCL")
/* Alternate entry point. Value is its address. */
__define_stab (N_ENTRY, 0xa4, "ENTRY")
/* Beginning of lexical block.
The desc is the nesting level in lexical blocks.
The value is the address of the start of the text for the block.
The variables declared inside the block *precede* the N_LBRAC symbol. */
__define_stab (N_LBRAC, 0xc0, "LBRAC")
/* Place holder for deleted include file. Replaces a N_BINCL and everything
up to the corresponding N_EINCL. The Sun linker generates these when
it finds multiple identical copies of the symbols from an include file.
This appears only in output from the Sun linker. */
__define_stab (N_EXCL, 0xc2, "EXCL")
/* Modula-2 scope information. Can someone say what info it contains? */
__define_stab (N_SCOPE, 0xc4, "SCOPE")
/* End of a lexical block. Desc matches the N_LBRAC's desc.
The value is the address of the end of the text for the block. */
__define_stab (N_RBRAC, 0xe0, "RBRAC")
/* Begin named common block. Only the name is significant. */
__define_stab (N_BCOMM, 0xe2, "BCOMM")
/* End named common block. Only the name is significant
(and it should match the N_BCOMM). */
__define_stab (N_ECOMM, 0xe4, "ECOMM")
/* End common (local name): value is address.
I'm not sure how this is used. */
__define_stab (N_ECOML, 0xe8, "ECOML")
/* These STAB's are used on Gould systems for Non-Base register symbols
or something like that. FIXME. I have assigned the values at random
since I don't have a Gould here. Fixups from Gould folk welcome... */
__define_stab (N_NBTEXT, 0xF0, "NBTEXT")
__define_stab (N_NBDATA, 0xF2, "NBDATA")
__define_stab (N_NBBSS, 0xF4, "NBBSS")
__define_stab (N_NBSTS, 0xF6, "NBSTS")
__define_stab (N_NBLCS, 0xF8, "NBLCS")
/* Second symbol entry containing a length-value for the preceding entry.
The value is the length. */
__define_stab (N_LENG, 0xfe, "LENG")
/* The above information, in matrix format.
STAB MATRIX
_________________________________________________
| 00 - 1F are not dbx stab symbols |
| In most cases, the low bit is the EXTernal bit|
| 00 UNDEF | 02 ABS | 04 TEXT | 06 DATA |
| 01 |EXT | 03 |EXT | 05 |EXT | 07 |EXT |
| 08 BSS | 0A INDR | 0C FN_SEQ | 0E |
| 09 |EXT | 0B | 0D | 0F |
| 10 | 12 COMM | 14 SETA | 16 SETT |
| 11 | 13 | 15 | 17 |
| 18 SETD | 1A SETB | 1C SETV | 1E WARNING|
| 19 | 1B | 1D | 1F FN |
|_______________________________________________|
| Debug entries with bit 01 set are unused. |
| 20 GSYM | 22 FNAME | 24 FUN | 26 STSYM |
| 28 LCSYM | 2A MAIN | 2C | 2E |
| 30 PC | 32 NSYMS | 34 NOMAP | 36 |
| 38 OBJ | 3A | 3C OPT | 3E |
| 40 RSYM | 42 M2C | 44 SLINE | 46 DSLINE |
| 48 BSLINE*| 4A DEFD | 4C | 4E |
| 50 EHDECL*| 52 | 54 CATCH | 56 |
| 58 | 5A | 5C | 5E |
| 60 SSYM | 62 | 64 SO | 66 |
| 68 | 6A | 6C | 6E |
| 70 | 72 | 74 | 76 |
| 78 | 7A | 7C | 7E |
| 80 LSYM | 82 BINCL | 84 SOL | 86 |
| 88 | 8A | 8C | 8E |
| 90 | 92 | 94 | 96 |
| 98 | 9A | 9C | 9E |
| A0 PSYM | A2 EINCL | A4 ENTRY | A6 |
| A8 | AA | AC | AE |
| B0 | B2 | B4 | B6 |
| B8 | BA | BC | BE |
| C0 LBRAC | C2 EXCL | C4 SCOPE | C6 |
| C8 | CA | CC | CE |
| D0 | D2 | D4 | D6 |
| D8 | DA | DC | DE |
| E0 RBRAC | E2 BCOMM | E4 ECOMM | E6 |
| E8 ECOML | EA | EC | EE |
| F0 | F2 | F4 | F6 |
| F8 | FA | FC | FE LENG |
+-----------------------------------------------+
* 50 EHDECL is also MOD2.
* 48 BSLINE is also BROWS.
*/

View File

@ -1,17 +0,0 @@
#ifndef __GNU_STAB__
/* Indicate the GNU stab.h is in use. */
#define __GNU_STAB__
#define __define_stab(NAME, CODE, STRING) NAME=CODE,
enum __stab_debug_code
{
#include "stab.def"
LAST_UNUSED_STAB_CODE
};
#undef __define_stab
#endif /* __GNU_STAB_ */

View File

@ -79,7 +79,7 @@ typedef UINT_PTR uintptr_t;
# define O_BINARY 0
#endif
#include "stab.h"
// #include "stab.h"
#include "libtcc.h"
#ifndef __WINDOWS__
@ -101,7 +101,6 @@ typedef long long int int64_t;
#define MAX_ALIGN 8
#define PTR_SIZE 4
static int decl0(int l, int is_for_loop_init);
#if !defined (__HAIKU__)
typedef uint64_t addr_t;
@ -453,8 +452,68 @@ struct TCCState {
/* used by main and tcc_parse_args only */
char *deps_outfile; /* option -MF */
// === previously globals ====
bool const_wanted; /* true if constant wanted */
bool global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
size_t arraysize;
/* loc : local variable index
ind : output code index
rsym: return symbol
anon_sym: anonymous symbol index
*/
int anon_sym; // = SYM_FIRST_ANOM;
int loc;
int ch, tok;
CValue tokc;
const int *macro_ptr;
CString tokcstr; /* current parsed string, if any */
/* display benchmark infos */
int total_lines;
int total_bytes;
int tok_ident;
TokenSym **table_ident;
//
const char *global_type;
const char *global_symname;
Sym *global_stack;
Sym *local_stack;
Sym *define_stack;
bool nocode_wanted;
// XXX?
BufferedFile *file;
char *funcname;
int tok_flags;
int parse_flags;
void **sym_pools;
int nb_sym_pools;
Sym *sym_free_first;
char *dir_name;
#define SYM_POOL_NB (8192 / sizeof(Sym))
CType char_pointer_type, func_old_type;
CType int8_type, int16_type, int32_type, int64_type, size_type;
SValue vstack[1+/*to make bcheck happy*/ VSTACK_SIZE];
// #define vstack (s1->__vstack + 1)
SValue *vtop;
// more
int *macro_ptr_allocated;
const int *unget_saved_macro_ptr;
int unget_saved_buffer[TOK_MAX_SIZE + 1];
bool unget_buffer_enabled;
TokenSym *hash_ident[TOK_HASH_SIZE];
char token_buf[STRING_MAX_SIZE + 1];
/* true if isid(c) || isnum(c) || isdot(c) */
unsigned char isidnum_table[256 - CH_EOF];
};
static const bool gnu_ext = true; // move into tcc_state
static const bool tcc_ext = true;
/* The current value can be: */
#define VT_VALMASK 0x003f /* mask for value location, register or: */
#define VT_CONST 0x0030 /* constant in vc (must be first non register value) */
@ -707,45 +766,30 @@ enum tcc_token {
#define IS_DIRSEP(c) (c == '/')
#define IS_ABSPATH(p) IS_DIRSEP(p[0])
#define PATHCMP strcmp
/* 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);
#endif
#ifdef TCC_TARGET_PE
#define PATHSEP ';'
#else
#define PATHSEP ':'
#endif
/* space exlcuding newline */
static inline int is_space(int ch)
{
static inline int is_space(int ch) {
return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
}
static inline int isid(int c)
{
static inline int isid(int c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}
static inline int isnum(int c)
{
static inline int isnum(int c) {
return c >= '0' && c <= '9';
}
static inline int isdot(int c)
{
static inline int isdot(int c) {
return c == '.';
}
static inline int isoct(int c)
{
static inline int isoct(int c) {
return c >= '0' && c <= '7';
}
static inline int toup(int c)
{
static inline int toup(int c) {
return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
}
@ -766,23 +810,10 @@ extern char **tcc_cb_ptr;
/* ------------ libtcc.c ------------ */
/* use GNU C extensions */
ST_DATA int gnu_ext;
/* use Tiny C extensions */
ST_DATA int tcc_ext;
/* XXX: get rid of this ASAP */
ST_DATA struct TCCState *tcc_state;
static inline int tcc_nerr(void) {
return tcc_state->nb_errors;
static inline int tcc_nerr(TCCState *s1) {
return s1->nb_errors;
}
#ifdef MEM_DEBUG
ST_DATA int mem_cur_size;
ST_DATA int mem_max_size;
#endif
#define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
#define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
#define AFF_PREPROCESS 0x0004 /* preprocess file */
@ -792,15 +823,9 @@ PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s);
PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num);
PUB_FUNC char *tcc_basename(const char *name);
PUB_FUNC char *tcc_fileextension (const char *name);
PUB_FUNC void tcc_free(void *ptr);
PUB_FUNC void *tcc_malloc(unsigned long size);
PUB_FUNC void *tcc_mallocz(unsigned long size);
PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size);
PUB_FUNC char *tcc_strdup(const char *str);
PUB_FUNC void tcc_memstats(void);
PUB_FUNC void tcc_error_noabort(const char *fmt, ...);
PUB_FUNC void tcc_error(const char *fmt, ...);
PUB_FUNC void tcc_warning(const char *fmt, ...);
PUB_FUNC void tcc_error(TCCState *s1, const char *fmt, ...);
PUB_FUNC void tcc_warning(TCCState *s1, const char *fmt, ...);
PUB_FUNC void strcat_printf(char *buf, int buf_size, const char *fmt, ...);
/* other utilities */
@ -813,34 +838,21 @@ ST_FUNC void cstr_new(CString *cstr);
ST_FUNC void cstr_free(CString *cstr);
ST_FUNC void cstr_reset(CString *cstr);
ST_INLN void sym_free(Sym *sym);
ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long long c);
ST_FUNC Sym *sym_find2(Sym *s, int v);
ST_FUNC Sym *sym_push(int v, CType *type, int r, long long c);
ST_FUNC void sym_pop(Sym **ptop, Sym *b);
ST_INLN Sym *struct_find(int v);
ST_INLN Sym *sym_find(int v);
ST_FUNC Sym *global_identifier_push(int v, int t, long long c);
ST_INLN void sym_free(TCCState *s1, Sym *sym);
ST_FUNC Sym *sym_push2(TCCState *s1, Sym **ps, int v, int t, long long c);
ST_FUNC Sym *sym_push(TCCState *s1, int v, CType *type, int r, long long c);
ST_FUNC void sym_pop(TCCState *s1, Sym **ptop, Sym *b);
ST_INLN Sym *sym_find(TCCState *s1, int v);
ST_FUNC Sym *global_identifier_push(TCCState *s1, int v, int t, long long c);
ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
ST_FUNC bool tcc_open_bf(TCCState *s1, const char *filename, int initlen);
ST_FUNC int tcc_open(TCCState *s1, const char *filename);
ST_FUNC void tcc_close(void);
ST_FUNC void tcc_close(TCCState *s1);
ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv);
/* ------------ tccpp.c ------------ */
ST_DATA int ch, tok;
ST_DATA CValue tokc;
ST_DATA const int *macro_ptr;
ST_DATA CString tokcstr; /* current parsed string, if any */
/* display benchmark infos */
ST_DATA int total_lines;
ST_DATA int total_bytes;
ST_DATA int tok_ident;
ST_DATA TokenSym **table_ident;
#define TOK_FLAG_BOL 0x0001 /* beginning of line before */
#define TOK_FLAG_BOF 0x0002 /* beginning of file before */
#define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
@ -854,27 +866,27 @@ ST_DATA TokenSym **table_ident;
#define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
#define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
ST_FUNC TokenSym *tok_alloc(const char *str, int len);
ST_FUNC char *get_tok_str(int v, CValue *cv);
ST_FUNC void save_parse_state(ParseState *s);
ST_FUNC TokenSym *tok_alloc(TCCState *s1, const char *str, int len);
ST_FUNC char *get_tok_str(TCCState *s1, int v, CValue *cv);
ST_FUNC void save_parse_state(TCCState *s1, ParseState *s);
ST_INLN void tok_str_new(TokenString *s);
ST_FUNC void tok_str_free(int *str);
ST_FUNC void tok_str_add(TokenString *s, int t);
ST_FUNC void tok_str_add_tok(TokenString *s);
ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg);
ST_FUNC void define_undef(Sym *s);
ST_INLN Sym *define_find(int v);
ST_FUNC void free_defines(Sym *b);
ST_FUNC void parse_define(void);
ST_FUNC void preprocess(int is_bof);
ST_FUNC void next_nomacro(void);
ST_FUNC void next(void);
ST_INLN void unget_tok(int last_tok);
ST_FUNC void tok_str_add(TCCState *s1, TokenString *s, int t);
ST_FUNC void tok_str_add_tok(TCCState *s1, TokenString *s);
ST_INLN void define_push(TCCState *s1, int v, int macro_type, int *str, Sym *first_arg);
ST_FUNC void define_undef(TCCState *s1, Sym *s);
ST_INLN Sym *define_find(TCCState *s1, int v);
ST_FUNC void free_defines(TCCState *s1, Sym *b);
// ST_FUNC void parse_define(void);
ST_FUNC void preprocess(TCCState *s1, bool is_bof);
ST_FUNC void next_nomacro(TCCState *s1);
ST_INLN void unget_tok(TCCState *s1, int last_tok);
ST_FUNC void preprocess_init(TCCState *s1);
ST_FUNC void preprocess_new(void);
ST_FUNC void preprocess_new(TCCState *s1);
ST_FUNC int tcc_preprocess(TCCState *s1);
ST_FUNC void skip(int c);
ST_FUNC void expect(const char *msg);
ST_FUNC void next(TCCState *s1);
ST_FUNC void skip(TCCState *s1, int c);
ST_FUNC void expect(TCCState *s1, const char *msg);
/* ------------ tccgen.c ------------ */
@ -887,31 +899,17 @@ ST_FUNC void expect(const char *msg);
#define REG_LRET 2
#define REG_FRET 3
#define SYM_POOL_NB (8192 / sizeof(Sym))
ST_DATA CType char_pointer_type, func_old_type;
ST_DATA CType int8_type, int16_type, int32_type, int64_type, size_type;
ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop;
#define vstack (__vstack + 1)
ST_INLN bool is_structured(CType *t);
ST_INLN bool is_struct(CType *t);
ST_INLN bool is_union(CType *t);
ST_INLN bool is_float(int t);
ST_INLN bool not_structured(CType *t);
#ifdef TCC_TARGET_ARM
ST_FUNC int get_reg_ex(int rc, int rc2);
ST_FUNC void lexpand_nr(void);
#endif
ST_FUNC int type_size(CType *type, int *a);
ST_FUNC void mk_pointer(CType *type);
ST_FUNC int type_size(TCCState *s1, CType *type, int *a);
ST_FUNC void mk_pointer(TCCState *s1, CType *type);
ST_FUNC int lvalue_type(int t);
ST_FUNC void indir(void);
ST_FUNC void unary(void);
ST_FUNC void expr_prod(void);
ST_FUNC void expr_sum(void);
ST_FUNC void gexpr(void);
ST_FUNC long long expr_const(void);
ST_FUNC void indir(TCCState *s1);
ST_FUNC long long expr_const(TCCState *s1);
/********************************************************/
#undef ST_DATA
@ -927,22 +925,4 @@ PUB_FUNC void tcc_typedef_alias_fields(const char *alias);
extern void (*tcc_cb)(const char *, char **);
ST_DATA bool nocode_wanted;
// XXX?
static BufferedFile *file;
static char *funcname;
ST_DATA Sym *define_stack;
ST_DATA int tok_flags;
ST_DATA int parse_flags;
ST_DATA Sym *global_stack;
ST_DATA Sym *local_stack;
ST_DATA Sym *define_stack;
ST_DATA void **sym_pools;
ST_DATA int nb_sym_pools;
ST_DATA Sym *sym_free_first;
ST_DATA char *dir_name;
#endif /* _TCC_H */

File diff suppressed because it is too large Load Diff

View File

@ -1,78 +0,0 @@
/* Simple libc header for TCC
*
* Add any function you want from the libc there. This file is here
* only for your convenience so that you do not need to put the whole
* glibc include files on your floppy disk
*/
#ifndef _TCCLIB_H
#define _TCCLIB_H
#include <stddef.h>
#include <stdarg.h>
/* stdlib.h */
void *calloc(size_t nmemb, size_t size);
void *malloc(size_t size);
void free(void *ptr);
void *realloc(void *ptr, size_t size);
int atoi(const char *nptr);
long int strtol(const char *nptr, char **endptr, int base);
unsigned long int strtoul(const char *nptr, char **endptr, int base);
void exit(int);
/* stdio.h */
typedef struct __FILE FILE;
#define EOF (-1)
extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;
FILE *fopen(const char *path, const char *mode);
FILE *fdopen(int fildes, const char *mode);
FILE *freopen(const char *path, const char *mode, FILE *stream);
int fclose(FILE *stream);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream);
int fgetc(FILE *stream);
char *fgets(char *s, int size, FILE *stream);
int getc(FILE *stream);
int getchar(void);
char *gets(char *s);
int ungetc(int c, FILE *stream);
int fflush(FILE *stream);
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
int asprintf(char **strp, const char *format, ...);
int dprintf(int fd, const char *format, ...);
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
int vasprintf(char **strp, const char *format, va_list ap);
int vdprintf(int fd, const char *format, va_list ap);
void perror(const char *s);
/* string.h */
char *strcat(char *dest, const char *src);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
char *strcpy(char *dest, const char *src);
void *memcpy(void *dest, const void *src, size_t n);
void *memmove(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);
char *strdup(const char *s);
/* dlfcn.h */
#define RTLD_LAZY 0x001
#define RTLD_NOW 0x002
#define RTLD_GLOBAL 0x100
void *dlopen(const char *filename, int flag);
const char *dlerror(void);
void *dlsym(void *handle, char *symbol);
int dlclose(void *handle);
#endif /* _TCCLIB_H */

File diff suppressed because it is too large Load Diff

View File

@ -147,7 +147,7 @@ DEF(TOK_pack, "pack")
/* already defined for assembler */
DEF(TOK_ASM_push, "push")
DEF(TOK_ASM_pop, "pop")
#if 0
/* builtin functions or variables */
DEF(TOK___aeabi_memcpy, "__aeabi_memcpy")
DEF(TOK___aeabi_memcpy4, "__aeabi_memcpy4")
@ -227,3 +227,4 @@ DEF(TOK_memmove, "memmove")
DEF(TOK_strlen, "strlen")
DEF(TOK_strcpy, "strcpy")
DEF(TOK_alloca, "alloca")
#endif

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2013-2021 - pancake */
/* radare - LGPL - Copyright 2013-2022 - pancake */
#include "r_util.h"
#include "r_types.h"
@ -9,7 +9,8 @@
#include "c/tccpp.c"
#include "c/libtcc.c"
extern int tcc_sym_push(char *typename, int typesize, int meta);
static R_TH_LOCAL TCCState *s1 = NULL;
extern int tcc_sym_push(TCCState *s1, char *typename, int typesize, int meta);
/* parse C code and return it in key-value form */
@ -36,6 +37,7 @@ static bool __typeLoad(void *p, const char *k, const char *v) {
}
int btype = 0;
RAnal *anal = (RAnal*)p;
// TCCState *s1 = NULL; // XXX THIS WILL MAKE IT CRASH
//r_cons_printf ("tk %s=%s\n", k, v);
// TODO: Add unions support
if (!strncmp (v, "struct", 6) && strncmp (k, "struct.", 7)) {
@ -69,14 +71,14 @@ static bool __typeLoad(void *p, const char *k, const char *v) {
// TODO: Go recurse here
query = r_strf ("struct.%s.%s.meta", subtype, subname);
btype = sdb_num_get (anal->sdb_types, query, 0);
tcc_sym_push (subtype, 0, btype);
tcc_sym_push (s1, subtype, 0, btype);
}
free (subtype);
ptr = next;
} while (next);
free (members);
}
tcc_sym_push ((char *)typename, typesize, btype);
tcc_sym_push (s1, (char *)typename, typesize, btype);
}
return true;
}
@ -104,6 +106,7 @@ R_API char *r_parse_c_file(RAnal *anal, const char *path, const char *dir, char
if (!T) {
return NULL;
}
s1 = T; // XXX delete global
tcc_set_callback (T, &__appendString, &str);
tcc_set_error_func (T, (void *)error_msg, __errorFunc);
sdb_foreach (anal->sdb_types, __typeLoad, anal); // why is this needed??
@ -133,6 +136,7 @@ R_API char *r_parse_c_string(RAnal *anal, const char *code, char **error_msg) {
if (!T) {
return NULL;
}
s1 = T; // XXX delete global
tcc_set_callback (T, &__appendString, &str);
tcc_set_error_func (T, (void *)error_msg, __errorFunc);
sdb_foreach (anal->sdb_types, __typeLoad, NULL);
@ -142,8 +146,3 @@ R_API char *r_parse_c_string(RAnal *anal, const char *code, char **error_msg) {
tcc_delete (T);
return str;
}
// XXX do not use globals
R_API void r_parse_c_reset(RParse *p) {
anon_sym = SYM_FIRST_ANOM;
}

View File

@ -1,4 +1,4 @@
/* radare2 - LGPL - Copyright 2009-2021 - nibble, pancake, maijin */
/* radare2 - LGPL - Copyright 2009-2022 - nibble, pancake, maijin */
#include <stdio.h>
#include <r_types.h>
@ -40,10 +40,7 @@ R_API void r_parse_free(RParse *p) {
}
R_API bool r_parse_add(RParse *p, RParsePlugin *foo) {
bool itsFine = true;
if (foo->init) {
itsFine = foo->init (p, p->user);
}
bool itsFine = foo->init? foo->init (p, p->user): true;
if (itsFine) {
r_list_append (p->parsers, foo);
}
@ -60,9 +57,10 @@ static char *predotname(const char *name) {
}
R_API bool r_parse_use(RParse *p, const char *name) {
r_return_val_if_fail (p && name, false);
RListIter *iter;
RParsePlugin *h;
r_return_val_if_fail (p && name, false);
r_list_foreach (p->parsers, iter, h) {
if (!strcmp (h->name, name)) {
p->cur = h;