Update Kolibri version of TinyC.Some bugfixes.

git-svn-id: svn://kolibrios.org@609 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
andrew_programmer 2007-08-24 19:33:11 +00:00
parent 47fe4cff9c
commit fb3a10620e
8 changed files with 590 additions and 317 deletions

View File

@ -0,0 +1,4 @@
OUTFILE = ktcc.kex
OBJS = tcc.o console.o
CGLAGS =-O2 -g -Wall -mpreferred-stack-boundary=2 -march=i386 -falign-functions=0 -fno-strict-aliasing
include $(MENUETDEV)/makefiles/Makefile_for_program

View File

@ -0,0 +1,85 @@
format ELF
section '.text' executable
public console_init
public console_printf
public console_exit
align 4
console_init:
pushad
mov eax,[console_init_status]
test eax,eax
jnz console_initializated
mov [console_init_status],1
mov eax,68
mov ebx,19
mov ecx,console_path
int 0x40
test eax,eax
jz console_not_loaded
mov ebx,[eax+4]
mov [con_start],ebx
mov ebx,[eax+4+16]
mov [con_init],ebx
mov ebx,[eax+4+32]
mov [con_printf],ebx
push 1
call [con_start]
push caption
push -1
push -1
push -1
push -1
call [con_init]
console_not_loaded:
console_initializated:
popad
ret
align 4
console_printf:
pop [return_addres]
call [con_printf]
;add esp,8
push [return_addres]
ret
align 4
console_exit:
push 0
call [con_exit]
ret
;-----------------------------
console_path db '/sys/dll/console.obj',0
caption db 'Console',0
align 4
con_start rd 1
con_init rd 1
con_printf rd 1
con_exit rd 1
console_init_status rd 1
return_addres rd 1

View File

@ -0,0 +1,136 @@
format ELF
section '.text' executable
public start
;extrn mf_init
extrn main
;include 'debug2.inc'
__DEBUG__=0
;start_:
virtual at 0
db 'MENUET01' ; 1. Magic number (8 bytes)
dd 0x01 ; 2. Version of executable file
dd start ; 3. Start address
dd 0x0 ; 4. Size of image
dd 0x100000 ; 5. Size of needed memory
dd 0x100000 ; 6. Pointer to stack
hparams dd 0x0 ; 7. Pointer to program arguments
hpath dd 0x0 ; 8. Pointer to program path
end virtual
start:
;DEBUGF 'Start programm\n'
;init heap of memory
mov eax,68
mov ebx,11
int 0x40
;DEBUGF ' path "%s"\n params "%s"\n', .path, .params
; check for overflow
mov al, [path+buf_len-1]
or al, [params+buf_len-1]
jnz .crash
; check if path written by OS
mov eax, [hparams]
test eax, eax
jz .without_path
mov eax, path
.without_path:
mov esi, eax
call push_param
; retrieving parameters
mov esi, params
xor edx, edx ; dl - èä¸ò ïàðàìåòð(1) èëè ðàçäåëèòåëè(0)
; dh - ñèìâîë ñ êîòîðîãî íà÷àëñÿ ïàðàìåòð (1 êàâû÷êè, 0 îñòàëüíîå)
mov ecx, 1 ; cl = 1
; ch = 0 ïðîñòî íîëü
.parse:
lodsb
test al, al
jz .run
test dl, dl
jnz .findendparam
;{åñëè áûë ðàçäåëèòåëü
cmp al, ' '
jz .parse ;çàãðóæåí ïðîáåë, ãðóçèì ñëåäóþùèé ñèìâîë
mov dl, cl ;íà÷èíàåòñÿ ïàðàìåòð
cmp al, '"'
jz @f ;çàãðóæåíû êàâû÷êè
mov dh, ch ;ïàðàìåòð áåç êàâû÷åê
dec esi
call push_param
inc esi
jmp .parse
@@:
mov dh, cl ;ïàðàìåòð â êàâû÷åêàõ
call push_param ;åñëè íå ïðîáåë çíà÷èò íà÷èíàåòñÿ êàêîé òî ïàðàìåòð
jmp .parse ;åñëè áûë ðàçäåëèòåëü}
.findendparam:
test dh, dh
jz @f ; áåç êàâû÷åê
cmp al, '"'
jz .clear
jmp .parse
@@:
cmp al, ' '
jnz .parse
.clear:
lea ebx, [esi - 1]
mov [ebx], ch
mov dl, ch
jmp .parse
.run:
;DEBUGF 'call main(%x, %x) with params:\n', [argc], argv
if __DEBUG__ = 1
mov ecx, [argc]
@@:
lea esi, [ecx * 4 + argv-4]
DEBUGF '0x%x) "%s"\n', cx, [esi]
loop @b
end if
push [argc]
push argv
call main
.exit:
;DEBUGF 'Exit from prog\n';
xor eax,eax
dec eax
int 0x40
dd -1
.crash:
;DEBUGF 'E:buffer overflowed\n'
jmp .exit
;============================
push_param:
;============================
;parameters
; esi - pointer
;description
; procedure increase argc
; and add pointer to array argv
; procedure changes ebx
mov ebx, [argc]
cmp ebx, max_parameters
jae .dont_add
mov [argv+4*ebx], esi
inc [argc]
.dont_add:
ret
;==============================
public params as '__argv'
public path as '__path'
section '.bss'
buf_len = 0x400
max_parameters=0x20
argc rd 1
argv rd max_parameters
path rb buf_len
params rb buf_len
;section '.data'
;include_debug_strings ; ALWAYS present in data section

View File

@ -31,20 +31,21 @@
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <errno.h> //#include <errno.h>
#include <math.h> #include <math.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h> //#include <signal.h>
#include <fcntl.h> #include <fcntl.h>
#include <setjmp.h> #include <setjmp.h>
#include <time.h> #include <time.h>
#ifdef WIN32 #ifdef WIN32
#include <sys/timeb.h> #include <sys/timeb.h>
#endif #endif
#ifndef WIN32 //#ifndef WIN32
#include <sys/time.h> #include <sys/time.h>
#include <sys/ucontext.h> //#include <sys/ucontext.h>
#endif //#endif
#endif /* !CONFIG_TCCBOOT */ #endif /* !CONFIG_TCCBOOT */
@ -73,6 +74,8 @@
//#define TCC_TARGET_I386 /* i386 code generator */ //#define TCC_TARGET_I386 /* i386 code generator */
//#define TCC_TARGET_ARM /* ARMv4 code generator */ //#define TCC_TARGET_ARM /* ARMv4 code generator */
//#define TCC_TARGET_C67 /* TMS320C67xx code generator */ //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
//----------------------------------------------------------------
#define TCC_TARGET_MEOS
/* default target is I386 */ /* default target is I386 */
#if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \ #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
@ -718,7 +721,7 @@ int __stdcall FreeConsole(void);
#define strtoll (long long)strtol #define strtoll (long long)strtol
#endif #endif
#elif defined(TCC_UCLIBC) || defined(__FreeBSD__) #elif defined(TCC_UCLIBC) || defined(__FreeBSD__)
/* currently incorrect */
long double strtold(const char *nptr, char **endptr) long double strtold(const char *nptr, char **endptr)
{ {
return (long double)strtod(nptr, endptr); return (long double)strtod(nptr, endptr);
@ -727,12 +730,20 @@ float strtof(const char *nptr, char **endptr)
{ {
return (float)strtod(nptr, endptr); return (float)strtod(nptr, endptr);
} }
#else #else
/* 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);
//extern long long strtoll(const char *__nptr, char **__endptr, int __base)
#endif #endif
#define strtold (long double)strtod
#define strtof (float)strtod
#define strtoll (long long)strtol
static char *pstrcpy(char *buf, int buf_size, const char *s); static char *pstrcpy(char *buf, int buf_size, const char *s);
static char *pstrcat(char *buf, int buf_size, const char *s); static char *pstrcat(char *buf, int buf_size, const char *s);
static const char *tcc_basename(const char *name); static const char *tcc_basename(const char *name);
@ -924,7 +935,7 @@ typedef struct TCCSyms {
static TCCSyms tcc_syms[] = { static TCCSyms tcc_syms[] = {
#if !defined(CONFIG_TCCBOOT) #if !defined(CONFIG_TCCBOOT)
TCCSYM(printf) TCCSYM(printf)
TCCSYM(fprintf) TCCSYM(printf)
TCCSYM(fopen) TCCSYM(fopen)
TCCSYM(fclose) TCCSYM(fclose)
#endif #endif
@ -944,14 +955,15 @@ void *resolve_sym(TCCState *s1, const char *symbol, int type)
} }
#elif !defined(WIN32) #elif !defined(WIN32)
//-------------------------------------------------------------------------
#include <dlfcn.h> //#include <dlfcn.h>
void *resolve_sym(TCCState *s1, const char *sym, int type) void *resolve_sym(TCCState *s1, const char *sym, int type)
{ {
return dlsym(RTLD_DEFAULT, sym); return(0);
//return dlsym(RTLD_DEFAULT, sym);
} }
//-------------------------------------------------------------------------
#endif #endif
/********************************************************/ /********************************************************/
@ -1373,7 +1385,7 @@ void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
if (!s1->error_func) { if (!s1->error_func) {
/* default case: stderr */ /* default case: stderr */
fprintf(stderr, "%s\n", buf); printf("%s\n", buf);
} else { } else {
s1->error_func(s1->error_opaque, buf); s1->error_func(s1->error_opaque, buf);
} }
@ -1887,7 +1899,7 @@ static int handle_eob(void)
} }
/* read next char from current input file and handle end of input buffer */ /* read next char from current input file and handle end of input buffer */
static inline void inp(void) static inline void input(void)
{ {
ch = *(++(file->buf_ptr)); ch = *(++(file->buf_ptr));
/* end of buffer/file handling */ /* end of buffer/file handling */
@ -1899,16 +1911,16 @@ static inline void inp(void)
static void handle_stray(void) static void handle_stray(void)
{ {
while (ch == '\\') { while (ch == '\\') {
inp(); input();
if (ch == '\n') { if (ch == '\n') {
file->line_num++; file->line_num++;
inp(); input();
} else if (ch == '\r') { } else if (ch == '\r') {
inp(); input();
if (ch != '\n') if (ch != '\n')
goto fail; goto fail;
file->line_num++; file->line_num++;
inp(); input();
} else { } else {
fail: fail:
error("stray '\\' in program"); error("stray '\\' in program");
@ -1967,7 +1979,7 @@ static int handle_stray1(uint8_t *p)
strings or comments */ strings or comments */
static void minp(void) static void minp(void)
{ {
inp(); input();
if (ch == '\\') if (ch == '\\')
handle_stray(); handle_stray();
} }
@ -2839,7 +2851,7 @@ static void preprocess(int is_bof)
/* eat all spaces and comments after include */ /* eat all spaces and comments after include */
/* XXX: slightly incorrect */ /* XXX: slightly incorrect */
while (ch1 != '\n' && ch1 != CH_EOF) while (ch1 != '\n' && ch1 != CH_EOF)
inp(); input();
#endif #endif
} else { } else {
/* computed #include : either we have only strings or /* computed #include : either we have only strings or
@ -3363,7 +3375,7 @@ void parse_number(const char *p)
} }
*q = '\0'; *q = '\0';
t = toup(ch); t = toup(ch);
errno = 0; //errno = 0;
if (t == 'F') { if (t == 'F') {
ch = *p++; ch = *p++;
tok = TOK_CFLOAT; tok = TOK_CFLOAT;
@ -5907,17 +5919,21 @@ static int type_size(CType *type, int *a)
{ {
Sym *s; Sym *s;
int bt; int bt;
int size;
bt = type->t & VT_BTYPE; bt = type->t & VT_BTYPE;
if (bt == VT_STRUCT) { if (bt == VT_STRUCT) {
/* struct/union */ /* struct/union */
s = type->ref; s = type->ref;
*a = s->r; *a = s->r;
return s->c; return s->c;
} else if (bt == VT_PTR) { } else if (bt == VT_PTR) {
if (type->t & VT_ARRAY) { if (type->t & VT_ARRAY) {
s = type->ref; s = type->ref;
return type_size(&s->type, a) * s->c; size=type_size(&s->type, a) * s->c;
return size;//type_size(&s->type, a) * s->c;
} else { } else {
*a = PTR_SIZE; *a = PTR_SIZE;
return PTR_SIZE; return PTR_SIZE;
@ -6581,9 +6597,13 @@ static void struct_decl(CType *type, int u)
bit field */ bit field */
if (lbit_pos == 0) { if (lbit_pos == 0) {
if (a == TOK_STRUCT) { if (a == TOK_STRUCT) {
c = (c + align - 1) & -align; //17.09.2007
//c = (c + align - 1) & -align;
offset = c; offset = c;
c += size; //c += size;
if (size<=4) {c=c+size;}
else
{c=c+align;}
} else { } else {
offset = 0; offset = 0;
if (size > c) if (size > c)
@ -9332,10 +9352,10 @@ static void asm_global_instr(void)
#if defined(TCC_TARGET_PE) || defined(TCC_TARGET_MEOS) #if defined(TCC_TARGET_PE) || defined(TCC_TARGET_MEOS)
#include "tccpe.c" #include "tccpe.c"
#endif #endif
#ifdef TCC_TARGET_MEOS #ifdef TCC_TARGET_MEOS
#include "tccmeos.c" #include "tccmeos.c"
#endif #endif
/* print the position in the source file of PC value 'pc' by reading /* print the position in the source file of PC value 'pc' by reading
@ -9349,7 +9369,7 @@ static void rt_printline(unsigned long wanted_pc)
int incl_index, len, last_line_num, i; int incl_index, len, last_line_num, i;
const char *str, *p; const char *str, *p;
fprintf(stderr, "0x%08lx:", wanted_pc); printf("0x%08lx:", wanted_pc);
func_name[0] = '\0'; func_name[0] = '\0';
func_addr = 0; func_addr = 0;
@ -9444,20 +9464,20 @@ static void rt_printline(unsigned long wanted_pc)
} }
} }
/* did not find any info: */ /* did not find any info: */
fprintf(stderr, " ???\n"); printf(" ???\n");
return; return;
found: found:
if (last_func_name[0] != '\0') { if (last_func_name[0] != '\0') {
fprintf(stderr, " %s()", last_func_name); printf(" %s()", last_func_name);
} }
if (incl_index > 0) { if (incl_index > 0) {
fprintf(stderr, " (%s:%d", printf(" (%s:%d",
incl_files[incl_index - 1], last_line_num); incl_files[incl_index - 1], last_line_num);
for(i = incl_index - 2; i >= 0; i--) for(i = incl_index - 2; i >= 0; i--)
fprintf(stderr, ", included from %s", incl_files[i]); printf(", included from %s", incl_files[i]);
fprintf(stderr, ")"); printf(")");
} }
fprintf(stderr, "\n"); printf("\n");
} }
#if !defined(WIN32) && !defined(CONFIG_TCCBOOT) #if !defined(WIN32) && !defined(CONFIG_TCCBOOT)
@ -9471,6 +9491,7 @@ static void rt_printline(unsigned long wanted_pc)
#endif #endif
/* return the PC at frame level 'level'. Return non zero if not found */ /* return the PC at frame level 'level'. Return non zero if not found */
/*
static int rt_get_caller_pc(unsigned long *paddr, static int rt_get_caller_pc(unsigned long *paddr,
ucontext_t *uc, int level) ucontext_t *uc, int level)
{ {
@ -9495,7 +9516,7 @@ static int rt_get_caller_pc(unsigned long *paddr,
fp = uc->uc_mcontext.gregs[REG_EBP]; fp = uc->uc_mcontext.gregs[REG_EBP];
#endif #endif
for(i=1;i<level;i++) { for(i=1;i<level;i++) {
/* XXX: check address validity with program info */ // XXX: check address validity with program info
if (fp <= 0x1000 || fp >= 0xc0000000) if (fp <= 0x1000 || fp >= 0xc0000000)
return -1; return -1;
fp = ((unsigned long *)fp)[0]; fp = ((unsigned long *)fp)[0];
@ -9504,18 +9525,21 @@ static int rt_get_caller_pc(unsigned long *paddr,
return 0; return 0;
} }
} }
*/
#else #else
#warning add arch specific rt_get_caller_pc() #warning add arch specific rt_get_caller_pc()
/*
static int rt_get_caller_pc(unsigned long *paddr, static int rt_get_caller_pc(unsigned long *paddr,
ucontext_t *uc, int level) ucontext_t *uc, int level)
{ {
return -1; return -1;
} }
*/
#endif #endif
/* emit a run time error at position 'pc' */ /* emit a run time error at position 'pc' */
/*
void rt_error(ucontext_t *uc, const char *fmt, ...) void rt_error(ucontext_t *uc, const char *fmt, ...)
{ {
va_list ap; va_list ap;
@ -9523,23 +9547,25 @@ void rt_error(ucontext_t *uc, const char *fmt, ...)
int i; int i;
va_start(ap, fmt); va_start(ap, fmt);
fprintf(stderr, "Runtime error: "); printf("Runtime error: ");
vfprintf(stderr, fmt, ap); //vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n"); printf("\n");
for(i=0;i<num_callers;i++) { for(i=0;i<num_callers;i++) {
if (rt_get_caller_pc(&pc, uc, i) < 0) if (rt_get_caller_pc(&pc, uc, i) < 0)
break; break;
if (i == 0) if (i == 0)
fprintf(stderr, "at "); printf("at ");
else else
fprintf(stderr, "by "); printf("by ");
rt_printline(pc); rt_printline(pc);
} }
exit(255); exit(255);
va_end(ap); va_end(ap);
} }
*/
/* signal handler for fatal errors */ /* signal handler for fatal errors */
/*
static void sig_error(int signum, siginfo_t *siginf, void *puc) static void sig_error(int signum, siginfo_t *siginf, void *puc)
{ {
ucontext_t *uc = puc; ucontext_t *uc = puc;
@ -9575,8 +9601,10 @@ static void sig_error(int signum, siginfo_t *siginf, void *puc)
} }
exit(255); exit(255);
} }
*/
#endif #endif
/* do all relocations (needed before using tcc_get_symbol()) */ /* do all relocations (needed before using tcc_get_symbol()) */
int tcc_relocate(TCCState *s1) int tcc_relocate(TCCState *s1)
{ {
@ -9636,17 +9664,17 @@ int tcc_run(TCCState *s1, int argc, char **argv)
#if defined(WIN32) || defined(CONFIG_TCCBOOT) #if defined(WIN32) || defined(CONFIG_TCCBOOT)
error("debug mode currently not available for Windows"); error("debug mode currently not available for Windows");
#else #else
struct sigaction sigact; //struct sigaction sigact;
/* install TCC signal handlers to print debug info on fatal /* install TCC signal handlers to print debug info on fatal
runtime errors */ runtime errors */
sigact.sa_flags = SA_SIGINFO | SA_RESETHAND; //sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
sigact.sa_sigaction = sig_error; //sigact.sa_sigaction = sig_error;
sigemptyset(&sigact.sa_mask); //sigemptyset(&sigact.sa_mask);
sigaction(SIGFPE, &sigact, NULL); //sigaction(SIGFPE, &sigact, NULL);
sigaction(SIGILL, &sigact, NULL); //sigaction(SIGILL, &sigact, NULL);
sigaction(SIGSEGV, &sigact, NULL); //sigaction(SIGSEGV, &sigact, NULL);
sigaction(SIGBUS, &sigact, NULL); //sigaction(SIGBUS, &sigact, NULL);
sigaction(SIGABRT, &sigact, NULL); //sigaction(SIGABRT, &sigact, NULL);
#endif #endif
} }
@ -9741,12 +9769,13 @@ TCCState *tcc_new(void)
snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path); snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
tcc_add_library_path(s, buf); tcc_add_library_path(s, buf);
} }
#else #else
#ifdef TCC_TARGET_MEOS #ifdef TCC_TARGET_MEOS
tcc_add_library_path(s, ".//lib");
#else #else
tcc_add_library_path(s, "/usr/local/lib"); tcc_add_library_path(s, "/usr/local/lib");
tcc_add_library_path(s, "/usr/lib"); tcc_add_library_path(s, "/usr/lib");
tcc_add_library_path(s, "/lib"); tcc_add_library_path(s, "/lib");
#endif #endif
#endif #endif
@ -10041,6 +10070,10 @@ int tcc_set_output_type(TCCState *s, int output_type)
tcc_add_sysinclude_path(s, "/usr/local/include"); tcc_add_sysinclude_path(s, "/usr/local/include");
tcc_add_sysinclude_path(s, "/usr/include"); tcc_add_sysinclude_path(s, "/usr/include");
#endif #endif
#if defined(TCC_TARGET_MEOS)
tcc_add_sysinclude_path(s, ".//include");
#endif
snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path); snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
tcc_add_sysinclude_path(s, buf); tcc_add_sysinclude_path(s, buf);
#ifdef TCC_TARGET_PE #ifdef TCC_TARGET_PE
@ -10086,10 +10119,10 @@ int tcc_set_output_type(TCCState *s, int output_type)
tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o"); tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o"); tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
} }
#endif #endif
#if defined(TCC_TARGET_MEOS) #if defined(TCC_TARGET_MEOS)
if (s->output_type != TCC_OUTPUT_OBJ) if (s->output_type != TCC_OUTPUT_OBJ)
tcc_add_file(s,".\\start.o"); tcc_add_file(s,".//start.o");
#endif #endif
return 0; return 0;
} }
@ -10229,9 +10262,6 @@ void help(void)
" -r relocatable output\n" " -r relocatable output\n"
"Debugger options:\n" "Debugger options:\n"
" -g generate runtime debug info\n" " -g generate runtime debug info\n"
#ifdef CONFIG_TCC_BCHECK
" -b compile with built-in memory and bounds checker (implies -g)\n"
#endif
" -bt N show N callers in stack traces\n" " -bt N show N callers in stack traces\n"
); );
} }
@ -10351,20 +10381,30 @@ static const char *outfile;
int parse_args(TCCState *s, int argc, char **argv) int parse_args(TCCState *s, int argc, char **argv)
{ {
int optind; int optind;
int i;
const TCCOption *popt; const TCCOption *popt;
const char *optarg, *p1, *r1; const char *optarg, *p1, *r1;
char *r; char *r;
/*
printf("\n%d\n",argc);
for(i=0;i<argc;i++)
{
printf("\n parameter %d = %s",i+1,argv[i]);
}
printf("\n");
*/
optind = 0; optind = 0;
while (1) { while (1) {
if (optind >= argc) { if (optind >= argc) {
if (nb_files == 0 && !print_search_dirs) if (nb_files == 0 && !print_search_dirs)
goto show_help; goto show_help;
else else
break; break;
} }
r = argv[optind++]; r = argv[optind++];
if (r[0] != '-') { if (r[0] != '-') {
/* add a new file */ /* add a new file */
dynarray_add((void ***)&files, &nb_files, r); dynarray_add((void ***)&files, &nb_files, r);
if (!multiple_files) { if (!multiple_files) {
@ -10378,7 +10418,7 @@ int parse_args(TCCState *s, int argc, char **argv)
for(;;) { for(;;) {
p1 = popt->name; p1 = popt->name;
if (p1 == NULL) if (p1 == NULL)
error("invalid option -- '%s'", r); printf("\n invalid option -- '%s'", r);
r1 = r + 1; r1 = r + 1;
for(;;) { for(;;) {
if (*p1 == '\0') if (*p1 == '\0')
@ -10396,7 +10436,7 @@ int parse_args(TCCState *s, int argc, char **argv)
optarg = r1; optarg = r1;
} else { } else {
if (optind >= argc) if (optind >= argc)
error("argument to '%s' is missing", r); printf("\n argument to '%s' is missing", r);
optarg = argv[optind++]; optarg = argv[optind++];
} }
} else { } else {
@ -10412,7 +10452,7 @@ int parse_args(TCCState *s, int argc, char **argv)
exit(1); exit(1);
case TCC_OPTION_I: case TCC_OPTION_I:
if (tcc_add_include_path(s, optarg) < 0) if (tcc_add_include_path(s, optarg) < 0)
error("too many include paths"); printf("\n too many include paths");
break; break;
case TCC_OPTION_D: case TCC_OPTION_D:
{ {
@ -10531,10 +10571,10 @@ int parse_args(TCCState *s, int argc, char **argv)
} else } else
#endif #endif
{ {
error("target %s not found", p); printf("\n target %s not found", p);
} }
} else { } else {
error("unsupported linker option '%s'", optarg); printf("\n unsupported linker option '%s'", optarg);
} }
} }
break; break;
@ -10542,6 +10582,7 @@ int parse_args(TCCState *s, int argc, char **argv)
if (s->warn_unsupported) { if (s->warn_unsupported) {
unsupported_option: unsupported_option:
warning("unsupported option '%s'", r); warning("unsupported option '%s'", r);
printf("\n unsupported option '%s'", r);
} }
break; break;
} }
@ -10550,14 +10591,16 @@ int parse_args(TCCState *s, int argc, char **argv)
return optind; return optind;
} }
int main(int argc, char **argv) int app_main(int argc, char **argv)
{ {
int i; int i;
TCCState *s; TCCState *s;
int nb_objfiles, ret, optind; int nb_objfiles, ret, optind;
char objfilename[1024]; char objfilename[1024];
int64_t start_time = 0; int64_t start_time = 0;
int bug;
printf("\nTinyC compiler started.\n ");
#ifdef WIN32 #ifdef WIN32
/* on win32, we suppose the lib and includes are at the location /* on win32, we suppose the lib and includes are at the location
of 'tcc.exe' */ of 'tcc.exe' */
@ -10586,8 +10629,11 @@ int main(int argc, char **argv)
nb_libraries = 0; nb_libraries = 0;
reloc_output = 0; reloc_output = 0;
print_search_dirs = 0; print_search_dirs = 0;
printf("TinyC initializated.\n");
optind = parse_args(s, argc - 1, argv + 1) + 1; bug=argc;
if (bug==0) {bug==1;}
optind = parse_args(s, bug - 1, argv + 1) + 1;
printf("\n Arguments parsed.\n");
if (print_search_dirs) { if (print_search_dirs) {
/* enough for Linux kernel */ /* enough for Linux kernel */
@ -10602,13 +10648,14 @@ int main(int argc, char **argv)
if (outfile && output_type == TCC_OUTPUT_MEMORY) if (outfile && output_type == TCC_OUTPUT_MEMORY)
output_type = TCC_OUTPUT_EXE; output_type = TCC_OUTPUT_EXE;
/* check -c consistency : only single file handled. XXX: checks file type */ /* check -c
consistency : only single file handled. XXX: checks file type */
if (output_type == TCC_OUTPUT_OBJ && !reloc_output) { if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
/* accepts only a single input file */ /* accepts only a single input file */
if (nb_objfiles != 1) if (nb_objfiles != 1)
error("cannot specify multiple files with -c"); printf("\n cannot specify multiple files with -c");
if (nb_libraries != 0) if (nb_libraries != 0)
error("cannot specify libraries with -c"); printf("\n cannot specify libraries with -c");
} }
if (output_type != TCC_OUTPUT_MEMORY) { if (output_type != TCC_OUTPUT_MEMORY) {
@ -10679,14 +10726,14 @@ int main(int argc, char **argv)
#ifdef TCC_TARGET_PE #ifdef TCC_TARGET_PE
if (s->output_type != TCC_OUTPUT_OBJ) { if (s->output_type != TCC_OUTPUT_OBJ) {
ret = tcc_output_pe(s, outfile); ret = tcc_output_pe(s, outfile);
} else } else
#else #else
#ifdef TCC_TARGET_MEOS #ifdef TCC_TARGET_MEOS
if (s->output_type != TCC_OUTPUT_OBJ) { if (s->output_type != TCC_OUTPUT_OBJ) {
ret = tcc_output_me(s, outfile); ret = tcc_output_me(s, outfile);
} else } else
#endif
#endif #endif
#endif
{ {
tcc_output_file(s, outfile); tcc_output_file(s, outfile);
@ -10702,6 +10749,7 @@ int main(int argc, char **argv)
printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size); printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
} }
#endif #endif
printf("\n TinyC finished work\n");
return ret; return ret;
} }

View File

@ -2224,7 +2224,7 @@ static int ld_next(TCCState *s1, char *name, int name_size)
case '\v': case '\v':
case '\r': case '\r':
case '\n': case '\n':
inp(); input();
goto redo; goto redo;
case '/': case '/':
minp(); minp();
@ -2266,7 +2266,7 @@ static int ld_next(TCCState *s1, char *name, int name_size)
break; break;
default: default:
c = ch; c = ch;
inp(); input();
break; break;
} }
#if 0 #if 0

View File

@ -16,235 +16,233 @@
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
typedef struct { typedef struct {
char magic[8]; char magic[8];
int version; int version;
int entry_point; int entry_point;
int image_size; int image_size;
int memory_size; int memory_size;
int stack; int stack;
int params; int params;
int argv; int argv;
} IMAGE_MEOS_FILE_HEADER,*PIMAGE_MEOS_FILE_HEADER; } IMAGE_MEOS_FILE_HEADER,*PIMAGE_MEOS_FILE_HEADER;
typedef struct _meos_section_info{ typedef struct _meos_section_info{
int sh_addr; int sh_addr;
void* data; void* data;
int data_size; int data_size;
int sec_num; int sec_num;
struct _meos_section_info* next; struct _meos_section_info* next;
} meos_section_info; } meos_section_info;
typedef struct { typedef struct {
TCCState* s1; TCCState* s1;
IMAGE_MEOS_FILE_HEADER header; IMAGE_MEOS_FILE_HEADER header;
meos_section_info* code_sections; meos_section_info* code_sections;
meos_section_info* data_sections; meos_section_info* data_sections;
meos_section_info* bss_sections; meos_section_info* bss_sections;
} me_info; } me_info;
meos_section_info* findsection(me_info* me,int num) meos_section_info* findsection(me_info* me,int num)
{ {
meos_section_info* si; meos_section_info* si;
for(si=me->code_sections;si;si=si->next) for(si=me->code_sections;si;si=si->next)
{ {
if (si->sec_num==num) if (si->sec_num==num)
return si; return si;
} }
for (si=me->data_sections;si;si=si->next) for (si=me->data_sections;si;si=si->next)
{ {
if (si->sec_num==num) if (si->sec_num==num)
return si; return si;
} }
for (si=me->bss_sections;si;si=si->next) for (si=me->bss_sections;si;si=si->next)
{ {
if (si->sec_num==num) if (si->sec_num==num)
return si; return si;
} }
return (meos_section_info*)0; return (meos_section_info*)0;
} }
void build_reloc(me_info* me) void build_reloc(me_info* me)
{ {
int flag; int flag;
Elf32_Rel *rel, *rel_, *rel_end; Elf32_Rel *rel, *rel_, *rel_end;
Section *sr; Section *sr;
meos_section_info* s; meos_section_info* s;
meos_section_info* ss; meos_section_info* ss;
s=me->code_sections; s=me->code_sections;
rel=0; rel=0;
rel_end=0; rel_end=0;
flag=0; flag=0;
for(;;) for(;;)
{ {
sr=me->s1->sections[s->sec_num]->reloc; sr=me->s1->sections[s->sec_num]->reloc;
if (sr) if (sr)
{ {
rel = (Elf32_Rel *) sr->data; rel = (Elf32_Rel *) sr->data;
rel_end = (Elf32_Rel *) (sr->data + sr->data_offset); rel_end = (Elf32_Rel *) (sr->data + sr->data_offset);
} }
rel_=rel; rel_=rel;
while (rel_<rel_end){ while (rel_<rel_end){
rel=rel_; rel=rel_;
int type = ELF32_R_TYPE(rel->r_info); int type = ELF32_R_TYPE(rel->r_info);
rel_=rel+1; rel_=rel+1;
if (type != R_386_PC32 && type != R_386_32) if (type != R_386_PC32 && type != R_386_32)
continue; continue;
int sym = ELF32_R_SYM(rel->r_info); int sym = ELF32_R_SYM(rel->r_info);
if (sym>symtab_section->data_offset/sizeof(Elf32_Sym)) if (sym>symtab_section->data_offset/sizeof(Elf32_Sym))
continue; continue;
Elf32_Sym* esym = ((Elf32_Sym *)symtab_section->data)+sym; Elf32_Sym* esym = ((Elf32_Sym *)symtab_section->data)+sym;
int sect=esym->st_shndx; int sect=esym->st_shndx;
ss=findsection(me,sect); ss=findsection(me,sect);
if (ss==0) if (ss==0) continue;
ss=me->bss_sections; if (rel->r_offset>s->data_size)
if (rel->r_offset>s->data_size) continue;
continue; if (type==R_386_PC32)
if (type==R_386_PC32) *(int*)(rel->r_offset+s->data)+=ss->sh_addr+esym->st_value-rel->r_offset-s->sh_addr;
*(int*)(rel->r_offset+s->data)=ss->sh_addr+esym->st_value-rel->r_offset-s->sh_addr-4; else if (type==R_386_32)
else if (type==R_386_32) *(int*)(rel->r_offset+s->data)+=ss->sh_addr+esym->st_value;
*(int*)(rel->r_offset+s->data)+=ss->sh_addr+esym->st_value; }
} rel=rel_;
rel=rel_; s=s->next;
s=s->next; if (s==0)
if (s==0) {
{ if (flag) break;
if (flag) break; s=me->data_sections;
s=me->data_sections; if (s==0) break;
if (s==0) break; flag=1;
flag=1; continue;
continue; }
} }
} }
}
void assign_addresses(me_info* me)
void assign_addresses(me_info* me) {
{ int i;
int i; meos_section_info* si;
meos_section_info* si; for (i=1;i<me->s1->nb_sections;i++)
for (i=1;i<me->s1->nb_sections;i++) {
{ Section* s=me->s1->sections[i];
Section* s=me->s1->sections[i]; if (strcmp(".text",s->name)==0)
if (strcmp(".text",s->name)==0) {
{ si=tcc_malloc(sizeof(meos_section_info));
si=tcc_malloc(sizeof(meos_section_info)); si->data=s->data;
si->data=s->data; si->data_size=s->data_offset;
si->data_size=s->data_offset; si->next=me->code_sections;
si->next=me->code_sections; si->sec_num=i;
si->sec_num=i; me->code_sections=si;
me->code_sections=si; continue;
continue; }
} if (strcmp(".data",s->name)==0)
if (strcmp(".data",s->name)==0) {
{ si=tcc_malloc(sizeof(meos_section_info));
si=tcc_malloc(sizeof(meos_section_info)); si->data=s->data;
si->data=s->data; si->data_size=s->data_offset;
si->data_size=s->data_offset; si->next=me->data_sections;
si->next=me->data_sections; si->sec_num=i;
si->sec_num=i; me->data_sections=si;
me->data_sections=si; continue;
continue; }
} if (strcmp(".bss",s->name)==0)
if (strcmp(".bss",s->name)==0) {
{ si=tcc_malloc(sizeof(meos_section_info));
si=tcc_malloc(sizeof(meos_section_info)); si->data_size=s->data_offset;
si->data_size=s->data_offset; si->next=me->bss_sections;
si->next=me->bss_sections; si->sec_num=i;
si->sec_num=i; me->bss_sections=si;
me->bss_sections=si; continue;
continue; }
} }
} int addr;
int addr; addr=sizeof(IMAGE_MEOS_FILE_HEADER);
addr=sizeof(IMAGE_MEOS_FILE_HEADER); for (si=me->code_sections;si;si=si->next)
for (si=me->code_sections;si;si=si->next) {
{ si->sh_addr=addr;
si->sh_addr=addr; addr+=si->data_size;
addr+=si->data_size; }
} for (si=me->data_sections;si;si=si->next)
for (si=me->data_sections;si;si=si->next) {
{ si->sh_addr=addr;
si->sh_addr=addr; addr+=si->data_size;
addr+=si->data_size; }
} me->header.image_size=addr;
me->header.image_size=addr; for (si=me->bss_sections;si;si=si->next)
for (si=me->bss_sections;si;si=si->next) {
{ si->sh_addr=addr;
si->sh_addr=addr; addr+=si->data_size;
addr+=si->data_size; }
} addr+=4096;
addr+=4096; addr=(addr+4)&(~3);
addr=(addr+4)&(~3); me->header.stack=addr;
me->header.stack=addr; me->header.memory_size=addr;
me->header.memory_size=addr; build_reloc(me);
build_reloc(me); }
} int tcc_find_symbol_me(me_info* me, const char *sym_name)
int tcc_find_symbol_me(me_info* me, const char *sym_name) {
{ int i;
int i; int symtab;
int symtab; int strtab;
int strtab; symtab=0;
symtab=0; strtab=0;
strtab=0; for (i=1;i<me->s1->nb_sections;i++)
for (i=1;i<me->s1->nb_sections;i++) {
{ Section* s;
Section* s; s=me->s1->sections[i];
s=me->s1->sections[i]; if (strcmp(s->name,".symtab")==0)
if (strcmp(s->name,".symtab")==0) {
{ symtab=i;
symtab=i; }
} if (strcmp(s->name,".strtab")==0)
if (strcmp(s->name,".strtab")==0) {
{ strtab=i;
strtab=i; }
} }
} if (symtab==0 || strtab==0)
if (symtab==0 || strtab==0) return 0;
return 0; Elf32_Sym* s,*se;
Elf32_Sym* s,*se; char* name;
char* name; s=(Elf32_Sym*)me->s1->sections[symtab]->data;
s=(Elf32_Sym*)me->s1->sections[symtab]->data; se=(Elf32_Sym*)(((void*)s)+me->s1->sections[symtab]->data_offset);
se=(Elf32_Sym*)(((void*)s)+me->s1->sections[symtab]->data_offset); name=(char*)me->s1->sections[strtab]->data;
name=(char*)me->s1->sections[strtab]->data; while (s<se)
while (s<se) {
{ if (strcmp(name+s->st_name,sym_name)==0)
if (strcmp(name+s->st_name,sym_name)==0) {
{ return s->st_value+findsection(me,s->st_shndx)->sh_addr;
return s->st_value+findsection(me,s->st_shndx)->sh_addr; }
} s++;
s++; }
} return 0;
return 0; }
} const char* me_magic="MENUET01";
const char* me_magic="MENUET01"; int tcc_output_me(TCCState* s1,const char *filename)
int tcc_output_me(TCCState* s1,const char *filename) {
{ me_info me;
me_info me; int i;
int i; FILE* f;
FILE* f; //printf("%d\n",s1->nb_sections);
//printf("%d\n",s1->nb_sections); memset(&me,0,sizeof(me));
memset(&me,0,sizeof(me)); me.s1=s1;
me.s1=s1; relocate_common_syms();
relocate_common_syms(); assign_addresses(&me);
assign_addresses(&me); me.header.entry_point=tcc_find_symbol_me(&me,"start");
me.header.version=1; me.header.params= tcc_find_symbol_me(&me,"__argv"); // <--
me.header.entry_point=tcc_find_symbol_me(&me,"start"); me.header.argv= tcc_find_symbol_me(&me,"__path"); // <--
me.header.params= tcc_find_symbol_me(&me,"__argv"); // <--
me.header.argv= tcc_find_symbol_me(&me,"__path"); // <-- f=fopen(filename,"wb");
for (i=0;i<8;i++)
f=fopen(filename,"wb"); me.header.magic[i]=me_magic[i];
for (i=0;i<8;i++) /*me.header.magic[0]='M';me.header.magic[1]='E';
me.header.magic[i]=me_magic[i]; me.header.magic[2]='N';me.header.magic[3]='U';
/*me.header.magic[0]='M';me.header.magic[1]='E'; me.header.magic[4]='E';me.header.magic[5]='T';
me.header.magic[2]='N';me.header.magic[3]='U'; me.header.magic[6]='0';me.header.magic[7]='1';*/
me.header.magic[4]='E';me.header.magic[5]='T'; fwrite(&me.header,1,sizeof(IMAGE_MEOS_FILE_HEADER),f);
me.header.magic[6]='0';me.header.magic[7]='1';*/ meos_section_info* si;
fwrite(&me.header,1,sizeof(IMAGE_MEOS_FILE_HEADER),f); for(si=me.code_sections;si;si=si->next)
meos_section_info* si; fwrite(si->data,1,si->data_size,f);
for(si=me.code_sections;si;si=si->next) for (si=me.data_sections;si;si=si->next)
fwrite(si->data,1,si->data_size,f); fwrite(si->data,1,si->data_size,f);
for (si=me.data_sections;si;si=si->next) fclose(f);
fwrite(si->data,1,si->data_size,f); return 0;
fclose(f); }
return 0;
}

View File

@ -1083,7 +1083,7 @@ int pe_load_def_file(TCCState * s1, FILE * fp)
int f = 0, sym_index; int f = 0, sym_index;
char *p, line[120], dllname[40]; char *p, line[120], dllname[40];
while (fgets(line, sizeof line, fp)) { while (fgets(line, sizeof line, fp)) {
p = strchr(line, 0); //p = strchr(line, 0);
while (p > line && p[-1] <= ' ') while (p > line && p[-1] <= ' ')
--p; --p;
*p = 0; *p = 0;
@ -1136,7 +1136,7 @@ void pe_guess_outfile(char *objfilename, int output_type)
{ {
char *ext = strrchr(objfilename, '.'); char *ext = strrchr(objfilename, '.');
if (NULL == ext) if (NULL == ext)
ext = strchr(objfilename, 0); //ext = strchr(objfilename, 0);
if (output_type == TCC_OUTPUT_DLL) if (output_type == TCC_OUTPUT_DLL)
strcpy(ext, ".dll"); strcpy(ext, ".dll");
else else

View File

@ -138,7 +138,7 @@ int qq(int x)
void macro_test(void) void macro_test(void)
{ {
printf("macro:\n"); printf("macro:\n");
pf("N=%d\n", N); pf("N=%d\n", N);
printf("aaa=%d\n", AAA); printf("aaa=%d\n", AAA);
@ -1676,14 +1676,15 @@ void whitespace_test(void)
{ {
char *str; char *str;
#if 1
#if 1
pri\ pri\
ntf("whitspace:\n"); ntf("whitspace:\n");
#endif #endif
pf("N=%d\n", 2); pf("N=%d\n", 2);
#ifdef CORRECT_CR_HANDLING #ifdef CORRECT_CR_HANDLING
pri\ pri\
ntf("aaa=%d\n", 3); ntf("aaa=%d\n", 3);
#endif #endif
@ -1695,11 +1696,12 @@ ntf("min=%d\n", 4);
printf("len1=%d\n", strlen(" printf("len1=%d\n", strlen("
")); "));
#ifdef CORRECT_CR_HANDLING #ifdef CORRECT_CR_HANDLING
str = " str = "
"; ";
printf("len1=%d str[0]=%d\n", strlen(str), str[0]); printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
#endif #endif
printf("len1=%d\n", strlen(" a printf("len1=%d\n", strlen("
a
")); "));
#endif /* ACCEPT_CR_IN_STRINGS */ #endif /* ACCEPT_CR_IN_STRINGS */
} }