From fb3a10620e8b790a6e166a3542c7b4891e4d05b8 Mon Sep 17 00:00:00 2001 From: andrew_programmer Date: Fri, 24 Aug 2007 19:33:11 +0000 Subject: [PATCH] Update Kolibri version of TinyC.Some bugfixes. git-svn-id: svn://kolibrios.org@609 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/develop/metcc/trunk/source/Makefile | 4 + .../develop/metcc/trunk/source/console.asm | 85 ++++ programs/develop/metcc/trunk/source/start.asm | 136 ++++++ programs/develop/metcc/trunk/source/tcc.c | 202 +++++--- programs/develop/metcc/trunk/source/tccelf.c | 4 +- programs/develop/metcc/trunk/source/tccmeos.c | 458 +++++++++--------- programs/develop/metcc/trunk/source/tccpe.c | 4 +- programs/develop/metcc/trunk/source/tcctest.c | 14 +- 8 files changed, 590 insertions(+), 317 deletions(-) create mode 100644 programs/develop/metcc/trunk/source/Makefile create mode 100644 programs/develop/metcc/trunk/source/console.asm create mode 100644 programs/develop/metcc/trunk/source/start.asm diff --git a/programs/develop/metcc/trunk/source/Makefile b/programs/develop/metcc/trunk/source/Makefile new file mode 100644 index 0000000000..63d75e633d --- /dev/null +++ b/programs/develop/metcc/trunk/source/Makefile @@ -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 diff --git a/programs/develop/metcc/trunk/source/console.asm b/programs/develop/metcc/trunk/source/console.asm new file mode 100644 index 0000000000..83c464abbf --- /dev/null +++ b/programs/develop/metcc/trunk/source/console.asm @@ -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 diff --git a/programs/develop/metcc/trunk/source/start.asm b/programs/develop/metcc/trunk/source/start.asm new file mode 100644 index 0000000000..2f81ac6571 --- /dev/null +++ b/programs/develop/metcc/trunk/source/start.asm @@ -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 \ No newline at end of file diff --git a/programs/develop/metcc/trunk/source/tcc.c b/programs/develop/metcc/trunk/source/tcc.c index 11cdda58a1..b37bf29693 100644 --- a/programs/develop/metcc/trunk/source/tcc.c +++ b/programs/develop/metcc/trunk/source/tcc.c @@ -31,20 +31,21 @@ #include #include #include -#include +//#include #include #include -#include +//#include #include #include #include #ifdef WIN32 #include #endif -#ifndef WIN32 +//#ifndef WIN32 + #include -#include -#endif +//#include +//#endif #endif /* !CONFIG_TCCBOOT */ @@ -73,6 +74,8 @@ //#define TCC_TARGET_I386 /* i386 code generator */ //#define TCC_TARGET_ARM /* ARMv4 code generator */ //#define TCC_TARGET_C67 /* TMS320C67xx code generator */ +//---------------------------------------------------------------- +#define TCC_TARGET_MEOS /* default target is I386 */ #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \ @@ -718,7 +721,7 @@ int __stdcall FreeConsole(void); #define strtoll (long long)strtol #endif #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) -/* currently incorrect */ + long double strtold(const char *nptr, char **endptr) { return (long double)strtod(nptr, endptr); @@ -727,12 +730,20 @@ float strtof(const char *nptr, char **endptr) { return (float)strtod(nptr, endptr); } + #else + /* 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); +//extern long long strtoll(const char *__nptr, char **__endptr, int __base) #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 *pstrcat(char *buf, int buf_size, const char *s); static const char *tcc_basename(const char *name); @@ -924,7 +935,7 @@ typedef struct TCCSyms { static TCCSyms tcc_syms[] = { #if !defined(CONFIG_TCCBOOT) TCCSYM(printf) - TCCSYM(fprintf) + TCCSYM(printf) TCCSYM(fopen) TCCSYM(fclose) #endif @@ -944,14 +955,15 @@ void *resolve_sym(TCCState *s1, const char *symbol, int type) } #elif !defined(WIN32) - -#include +//------------------------------------------------------------------------- +//#include void *resolve_sym(TCCState *s1, const char *sym, int type) { - return dlsym(RTLD_DEFAULT, sym); + return(0); + //return dlsym(RTLD_DEFAULT, sym); } - +//------------------------------------------------------------------------- #endif /********************************************************/ @@ -1373,7 +1385,7 @@ void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap) if (!s1->error_func) { /* default case: stderr */ - fprintf(stderr, "%s\n", buf); + printf("%s\n", buf); } else { 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 */ -static inline void inp(void) +static inline void input(void) { ch = *(++(file->buf_ptr)); /* end of buffer/file handling */ @@ -1899,16 +1911,16 @@ static inline void inp(void) static void handle_stray(void) { while (ch == '\\') { - inp(); + input(); if (ch == '\n') { file->line_num++; - inp(); + input(); } else if (ch == '\r') { - inp(); + input(); if (ch != '\n') goto fail; file->line_num++; - inp(); + input(); } else { fail: error("stray '\\' in program"); @@ -1967,7 +1979,7 @@ static int handle_stray1(uint8_t *p) strings or comments */ static void minp(void) { - inp(); + input(); if (ch == '\\') handle_stray(); } @@ -2839,7 +2851,7 @@ static void preprocess(int is_bof) /* eat all spaces and comments after include */ /* XXX: slightly incorrect */ while (ch1 != '\n' && ch1 != CH_EOF) - inp(); + input(); #endif } else { /* computed #include : either we have only strings or @@ -3363,7 +3375,7 @@ void parse_number(const char *p) } *q = '\0'; t = toup(ch); - errno = 0; + //errno = 0; if (t == 'F') { ch = *p++; tok = TOK_CFLOAT; @@ -5907,17 +5919,21 @@ static int type_size(CType *type, int *a) { Sym *s; int bt; + int size; bt = type->t & VT_BTYPE; if (bt == VT_STRUCT) { /* struct/union */ s = type->ref; *a = s->r; + return s->c; } else if (bt == VT_PTR) { + if (type->t & VT_ARRAY) { 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 { *a = PTR_SIZE; return PTR_SIZE; @@ -6581,9 +6597,13 @@ static void struct_decl(CType *type, int u) bit field */ if (lbit_pos == 0) { if (a == TOK_STRUCT) { - c = (c + align - 1) & -align; + //17.09.2007 + //c = (c + align - 1) & -align; offset = c; - c += size; + //c += size; + if (size<=4) {c=c+size;} + else + {c=c+align;} } else { offset = 0; if (size > c) @@ -9332,10 +9352,10 @@ static void asm_global_instr(void) #if defined(TCC_TARGET_PE) || defined(TCC_TARGET_MEOS) #include "tccpe.c" -#endif - -#ifdef TCC_TARGET_MEOS -#include "tccmeos.c" +#endif + +#ifdef TCC_TARGET_MEOS +#include "tccmeos.c" #endif /* 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; const char *str, *p; - fprintf(stderr, "0x%08lx:", wanted_pc); + printf("0x%08lx:", wanted_pc); func_name[0] = '\0'; func_addr = 0; @@ -9444,20 +9464,20 @@ static void rt_printline(unsigned long wanted_pc) } } /* did not find any info: */ - fprintf(stderr, " ???\n"); + printf(" ???\n"); return; found: if (last_func_name[0] != '\0') { - fprintf(stderr, " %s()", last_func_name); + printf(" %s()", last_func_name); } if (incl_index > 0) { - fprintf(stderr, " (%s:%d", + printf(" (%s:%d", incl_files[incl_index - 1], last_line_num); for(i = incl_index - 2; i >= 0; i--) - fprintf(stderr, ", included from %s", incl_files[i]); - fprintf(stderr, ")"); + printf(", included from %s", incl_files[i]); + printf(")"); } - fprintf(stderr, "\n"); + printf("\n"); } #if !defined(WIN32) && !defined(CONFIG_TCCBOOT) @@ -9471,6 +9491,7 @@ static void rt_printline(unsigned long wanted_pc) #endif /* return the PC at frame level 'level'. Return non zero if not found */ +/* static int rt_get_caller_pc(unsigned long *paddr, 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]; #endif for(i=1;i= 0xc0000000) return -1; fp = ((unsigned long *)fp)[0]; @@ -9504,18 +9525,21 @@ static int rt_get_caller_pc(unsigned long *paddr, return 0; } } +*/ #else #warning add arch specific rt_get_caller_pc() - +/* static int rt_get_caller_pc(unsigned long *paddr, ucontext_t *uc, int level) { return -1; } +*/ #endif /* emit a run time error at position 'pc' */ +/* void rt_error(ucontext_t *uc, const char *fmt, ...) { va_list ap; @@ -9523,23 +9547,25 @@ void rt_error(ucontext_t *uc, const char *fmt, ...) int i; va_start(ap, fmt); - fprintf(stderr, "Runtime error: "); - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); + printf("Runtime error: "); + //vfprintf(stderr, fmt, ap); + printf("\n"); for(i=0;ioutput_type != TCC_OUTPUT_OBJ) - tcc_add_file(s,".\\start.o"); +#endif +#if defined(TCC_TARGET_MEOS) + if (s->output_type != TCC_OUTPUT_OBJ) + tcc_add_file(s,".//start.o"); #endif return 0; } @@ -10229,9 +10262,6 @@ void help(void) " -r relocatable output\n" "Debugger options:\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" ); } @@ -10351,20 +10381,30 @@ static const char *outfile; int parse_args(TCCState *s, int argc, char **argv) { int optind; + int i; const TCCOption *popt; const char *optarg, *p1, *r1; char *r; + /* + printf("\n%d\n",argc); + for(i=0;i= argc) { if (nb_files == 0 && !print_search_dirs) - goto show_help; + goto show_help; else break; } r = argv[optind++]; if (r[0] != '-') { + /* add a new file */ dynarray_add((void ***)&files, &nb_files, r); if (!multiple_files) { @@ -10378,7 +10418,7 @@ int parse_args(TCCState *s, int argc, char **argv) for(;;) { p1 = popt->name; if (p1 == NULL) - error("invalid option -- '%s'", r); + printf("\n invalid option -- '%s'", r); r1 = r + 1; for(;;) { if (*p1 == '\0') @@ -10396,7 +10436,7 @@ int parse_args(TCCState *s, int argc, char **argv) optarg = r1; } else { if (optind >= argc) - error("argument to '%s' is missing", r); + printf("\n argument to '%s' is missing", r); optarg = argv[optind++]; } } else { @@ -10412,7 +10452,7 @@ int parse_args(TCCState *s, int argc, char **argv) exit(1); case TCC_OPTION_I: if (tcc_add_include_path(s, optarg) < 0) - error("too many include paths"); + printf("\n too many include paths"); break; case TCC_OPTION_D: { @@ -10531,10 +10571,10 @@ int parse_args(TCCState *s, int argc, char **argv) } else #endif { - error("target %s not found", p); + printf("\n target %s not found", p); } } else { - error("unsupported linker option '%s'", optarg); + printf("\n unsupported linker option '%s'", optarg); } } break; @@ -10542,6 +10582,7 @@ int parse_args(TCCState *s, int argc, char **argv) if (s->warn_unsupported) { unsupported_option: warning("unsupported option '%s'", r); + printf("\n unsupported option '%s'", r); } break; } @@ -10550,14 +10591,16 @@ int parse_args(TCCState *s, int argc, char **argv) return optind; } -int main(int argc, char **argv) +int app_main(int argc, char **argv) { int i; TCCState *s; int nb_objfiles, ret, optind; char objfilename[1024]; int64_t start_time = 0; + int bug; + printf("\nTinyC compiler started.\n "); #ifdef WIN32 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */ @@ -10586,8 +10629,11 @@ int main(int argc, char **argv) nb_libraries = 0; reloc_output = 0; print_search_dirs = 0; - - optind = parse_args(s, argc - 1, argv + 1) + 1; + printf("TinyC initializated.\n"); + 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) { /* enough for Linux kernel */ @@ -10602,13 +10648,14 @@ int main(int argc, char **argv) if (outfile && output_type == TCC_OUTPUT_MEMORY) 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) { /* accepts only a single input file */ if (nb_objfiles != 1) - error("cannot specify multiple files with -c"); + printf("\n cannot specify multiple files with -c"); if (nb_libraries != 0) - error("cannot specify libraries with -c"); + printf("\n cannot specify libraries with -c"); } if (output_type != TCC_OUTPUT_MEMORY) { @@ -10679,14 +10726,14 @@ int main(int argc, char **argv) #ifdef TCC_TARGET_PE if (s->output_type != TCC_OUTPUT_OBJ) { ret = tcc_output_pe(s, outfile); - } else -#else -#ifdef TCC_TARGET_MEOS + } else +#else +#ifdef TCC_TARGET_MEOS if (s->output_type != TCC_OUTPUT_OBJ) { ret = tcc_output_me(s, outfile); - } else + } else +#endif #endif -#endif { 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); } #endif + printf("\n TinyC finished work\n"); return ret; } diff --git a/programs/develop/metcc/trunk/source/tccelf.c b/programs/develop/metcc/trunk/source/tccelf.c index 0e521854a1..78919ca182 100644 --- a/programs/develop/metcc/trunk/source/tccelf.c +++ b/programs/develop/metcc/trunk/source/tccelf.c @@ -2224,7 +2224,7 @@ static int ld_next(TCCState *s1, char *name, int name_size) case '\v': case '\r': case '\n': - inp(); + input(); goto redo; case '/': minp(); @@ -2266,7 +2266,7 @@ static int ld_next(TCCState *s1, char *name, int name_size) break; default: c = ch; - inp(); + input(); break; } #if 0 diff --git a/programs/develop/metcc/trunk/source/tccmeos.c b/programs/develop/metcc/trunk/source/tccmeos.c index aa38582159..c1295b8e40 100644 --- a/programs/develop/metcc/trunk/source/tccmeos.c +++ b/programs/develop/metcc/trunk/source/tccmeos.c @@ -16,235 +16,233 @@ * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -typedef struct { - char magic[8]; - int version; - int entry_point; - int image_size; - int memory_size; - int stack; - int params; - int argv; -} IMAGE_MEOS_FILE_HEADER,*PIMAGE_MEOS_FILE_HEADER; -typedef struct _meos_section_info{ - int sh_addr; - void* data; - int data_size; - int sec_num; - struct _meos_section_info* next; -} meos_section_info; -typedef struct { - TCCState* s1; - IMAGE_MEOS_FILE_HEADER header; - meos_section_info* code_sections; - meos_section_info* data_sections; - meos_section_info* bss_sections; -} me_info; - -meos_section_info* findsection(me_info* me,int num) -{ - meos_section_info* si; - for(si=me->code_sections;si;si=si->next) - { - if (si->sec_num==num) - return si; - } - for (si=me->data_sections;si;si=si->next) - { - if (si->sec_num==num) - return si; - } - for (si=me->bss_sections;si;si=si->next) - { - if (si->sec_num==num) - return si; - } - return (meos_section_info*)0; -} - -void build_reloc(me_info* me) -{ - int flag; - Elf32_Rel *rel, *rel_, *rel_end; - Section *sr; - meos_section_info* s; - meos_section_info* ss; - s=me->code_sections; - rel=0; - rel_end=0; - flag=0; - for(;;) - { - sr=me->s1->sections[s->sec_num]->reloc; - if (sr) - { + */ + +typedef struct { + char magic[8]; + int version; + int entry_point; + int image_size; + int memory_size; + int stack; + int params; + int argv; +} IMAGE_MEOS_FILE_HEADER,*PIMAGE_MEOS_FILE_HEADER; +typedef struct _meos_section_info{ + int sh_addr; + void* data; + int data_size; + int sec_num; + struct _meos_section_info* next; +} meos_section_info; +typedef struct { + TCCState* s1; + IMAGE_MEOS_FILE_HEADER header; + meos_section_info* code_sections; + meos_section_info* data_sections; + meos_section_info* bss_sections; +} me_info; + +meos_section_info* findsection(me_info* me,int num) +{ + meos_section_info* si; + for(si=me->code_sections;si;si=si->next) + { + if (si->sec_num==num) + return si; + } + for (si=me->data_sections;si;si=si->next) + { + if (si->sec_num==num) + return si; + } + for (si=me->bss_sections;si;si=si->next) + { + if (si->sec_num==num) + return si; + } + return (meos_section_info*)0; +} + +void build_reloc(me_info* me) +{ + int flag; + Elf32_Rel *rel, *rel_, *rel_end; + Section *sr; + meos_section_info* s; + meos_section_info* ss; + s=me->code_sections; + rel=0; + rel_end=0; + flag=0; + for(;;) + { + sr=me->s1->sections[s->sec_num]->reloc; + if (sr) + { rel = (Elf32_Rel *) sr->data; - rel_end = (Elf32_Rel *) (sr->data + sr->data_offset); - } - rel_=rel; - while (rel_r_info); - rel_=rel+1; + rel_end = (Elf32_Rel *) (sr->data + sr->data_offset); + } + rel_=rel; + while (rel_r_info); + rel_=rel+1; if (type != R_386_PC32 && type != R_386_32) - continue; - int sym = ELF32_R_SYM(rel->r_info); - if (sym>symtab_section->data_offset/sizeof(Elf32_Sym)) - continue; - Elf32_Sym* esym = ((Elf32_Sym *)symtab_section->data)+sym; - int sect=esym->st_shndx; - ss=findsection(me,sect); - if (ss==0) - ss=me->bss_sections; - if (rel->r_offset>s->data_size) - continue; - if (type==R_386_PC32) - *(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) - *(int*)(rel->r_offset+s->data)+=ss->sh_addr+esym->st_value; - } - rel=rel_; - s=s->next; - if (s==0) - { - if (flag) break; - s=me->data_sections; - if (s==0) break; - flag=1; - continue; - } - } -} - -void assign_addresses(me_info* me) -{ - int i; - meos_section_info* si; - for (i=1;is1->nb_sections;i++) - { - Section* s=me->s1->sections[i]; - if (strcmp(".text",s->name)==0) - { - si=tcc_malloc(sizeof(meos_section_info)); - si->data=s->data; - si->data_size=s->data_offset; - si->next=me->code_sections; - si->sec_num=i; - me->code_sections=si; - continue; - } - if (strcmp(".data",s->name)==0) - { - si=tcc_malloc(sizeof(meos_section_info)); - si->data=s->data; - si->data_size=s->data_offset; - si->next=me->data_sections; - si->sec_num=i; - me->data_sections=si; - continue; - } - if (strcmp(".bss",s->name)==0) - { - si=tcc_malloc(sizeof(meos_section_info)); - si->data_size=s->data_offset; - si->next=me->bss_sections; - si->sec_num=i; - me->bss_sections=si; - continue; - } - } - int addr; - addr=sizeof(IMAGE_MEOS_FILE_HEADER); - for (si=me->code_sections;si;si=si->next) - { - si->sh_addr=addr; - addr+=si->data_size; - } - for (si=me->data_sections;si;si=si->next) - { - si->sh_addr=addr; - addr+=si->data_size; - } - me->header.image_size=addr; - for (si=me->bss_sections;si;si=si->next) - { - si->sh_addr=addr; - addr+=si->data_size; - } - addr+=4096; - addr=(addr+4)&(~3); - me->header.stack=addr; - me->header.memory_size=addr; - build_reloc(me); -} -int tcc_find_symbol_me(me_info* me, const char *sym_name) -{ - int i; - int symtab; - int strtab; - symtab=0; - strtab=0; - for (i=1;is1->nb_sections;i++) - { - Section* s; - s=me->s1->sections[i]; - if (strcmp(s->name,".symtab")==0) - { - symtab=i; - } - if (strcmp(s->name,".strtab")==0) - { - strtab=i; - } - } - if (symtab==0 || strtab==0) - return 0; - Elf32_Sym* s,*se; - char* name; - s=(Elf32_Sym*)me->s1->sections[symtab]->data; - se=(Elf32_Sym*)(((void*)s)+me->s1->sections[symtab]->data_offset); - name=(char*)me->s1->sections[strtab]->data; - while (sst_name,sym_name)==0) - { - return s->st_value+findsection(me,s->st_shndx)->sh_addr; - } - s++; - } - return 0; -} -const char* me_magic="MENUET01"; -int tcc_output_me(TCCState* s1,const char *filename) -{ - me_info me; - int i; - FILE* f; - //printf("%d\n",s1->nb_sections); - memset(&me,0,sizeof(me)); - me.s1=s1; - relocate_common_syms(); - assign_addresses(&me); - me.header.version=1; - me.header.entry_point=tcc_find_symbol_me(&me,"start"); - 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++) - me.header.magic[i]=me_magic[i]; - /*me.header.magic[0]='M';me.header.magic[1]='E'; - me.header.magic[2]='N';me.header.magic[3]='U'; - me.header.magic[4]='E';me.header.magic[5]='T'; - me.header.magic[6]='0';me.header.magic[7]='1';*/ - fwrite(&me.header,1,sizeof(IMAGE_MEOS_FILE_HEADER),f); - meos_section_info* si; - for(si=me.code_sections;si;si=si->next) - fwrite(si->data,1,si->data_size,f); - for (si=me.data_sections;si;si=si->next) - fwrite(si->data,1,si->data_size,f); - fclose(f); - return 0; -} + continue; + int sym = ELF32_R_SYM(rel->r_info); + if (sym>symtab_section->data_offset/sizeof(Elf32_Sym)) + continue; + Elf32_Sym* esym = ((Elf32_Sym *)symtab_section->data)+sym; + int sect=esym->st_shndx; + ss=findsection(me,sect); + if (ss==0) continue; + if (rel->r_offset>s->data_size) + continue; + if (type==R_386_PC32) + *(int*)(rel->r_offset+s->data)+=ss->sh_addr+esym->st_value-rel->r_offset-s->sh_addr; + else if (type==R_386_32) + *(int*)(rel->r_offset+s->data)+=ss->sh_addr+esym->st_value; + } + rel=rel_; + s=s->next; + if (s==0) + { + if (flag) break; + s=me->data_sections; + if (s==0) break; + flag=1; + continue; + } + } +} + +void assign_addresses(me_info* me) +{ + int i; + meos_section_info* si; + for (i=1;is1->nb_sections;i++) + { + Section* s=me->s1->sections[i]; + if (strcmp(".text",s->name)==0) + { + si=tcc_malloc(sizeof(meos_section_info)); + si->data=s->data; + si->data_size=s->data_offset; + si->next=me->code_sections; + si->sec_num=i; + me->code_sections=si; + continue; + } + if (strcmp(".data",s->name)==0) + { + si=tcc_malloc(sizeof(meos_section_info)); + si->data=s->data; + si->data_size=s->data_offset; + si->next=me->data_sections; + si->sec_num=i; + me->data_sections=si; + continue; + } + if (strcmp(".bss",s->name)==0) + { + si=tcc_malloc(sizeof(meos_section_info)); + si->data_size=s->data_offset; + si->next=me->bss_sections; + si->sec_num=i; + me->bss_sections=si; + continue; + } + } + int addr; + addr=sizeof(IMAGE_MEOS_FILE_HEADER); + for (si=me->code_sections;si;si=si->next) + { + si->sh_addr=addr; + addr+=si->data_size; + } + for (si=me->data_sections;si;si=si->next) + { + si->sh_addr=addr; + addr+=si->data_size; + } + me->header.image_size=addr; + for (si=me->bss_sections;si;si=si->next) + { + si->sh_addr=addr; + addr+=si->data_size; + } + addr+=4096; + addr=(addr+4)&(~3); + me->header.stack=addr; + me->header.memory_size=addr; + build_reloc(me); +} +int tcc_find_symbol_me(me_info* me, const char *sym_name) +{ + int i; + int symtab; + int strtab; + symtab=0; + strtab=0; + for (i=1;is1->nb_sections;i++) + { + Section* s; + s=me->s1->sections[i]; + if (strcmp(s->name,".symtab")==0) + { + symtab=i; + } + if (strcmp(s->name,".strtab")==0) + { + strtab=i; + } + } + if (symtab==0 || strtab==0) + return 0; + Elf32_Sym* s,*se; + char* name; + s=(Elf32_Sym*)me->s1->sections[symtab]->data; + se=(Elf32_Sym*)(((void*)s)+me->s1->sections[symtab]->data_offset); + name=(char*)me->s1->sections[strtab]->data; + while (sst_name,sym_name)==0) + { + return s->st_value+findsection(me,s->st_shndx)->sh_addr; + } + s++; + } + return 0; +} +const char* me_magic="MENUET01"; +int tcc_output_me(TCCState* s1,const char *filename) +{ + me_info me; + int i; + FILE* f; + //printf("%d\n",s1->nb_sections); + memset(&me,0,sizeof(me)); + me.s1=s1; + relocate_common_syms(); + assign_addresses(&me); + me.header.entry_point=tcc_find_symbol_me(&me,"start"); + 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++) + me.header.magic[i]=me_magic[i]; + /*me.header.magic[0]='M';me.header.magic[1]='E'; + me.header.magic[2]='N';me.header.magic[3]='U'; + me.header.magic[4]='E';me.header.magic[5]='T'; + me.header.magic[6]='0';me.header.magic[7]='1';*/ + fwrite(&me.header,1,sizeof(IMAGE_MEOS_FILE_HEADER),f); + meos_section_info* si; + for(si=me.code_sections;si;si=si->next) + fwrite(si->data,1,si->data_size,f); + for (si=me.data_sections;si;si=si->next) + fwrite(si->data,1,si->data_size,f); + fclose(f); + return 0; +} diff --git a/programs/develop/metcc/trunk/source/tccpe.c b/programs/develop/metcc/trunk/source/tccpe.c index 5911296c3a..26fd0c15d9 100644 --- a/programs/develop/metcc/trunk/source/tccpe.c +++ b/programs/develop/metcc/trunk/source/tccpe.c @@ -1083,7 +1083,7 @@ int pe_load_def_file(TCCState * s1, FILE * fp) int f = 0, sym_index; char *p, line[120], dllname[40]; while (fgets(line, sizeof line, fp)) { - p = strchr(line, 0); + //p = strchr(line, 0); while (p > line && p[-1] <= ' ') --p; *p = 0; @@ -1136,7 +1136,7 @@ void pe_guess_outfile(char *objfilename, int output_type) { char *ext = strrchr(objfilename, '.'); if (NULL == ext) - ext = strchr(objfilename, 0); + //ext = strchr(objfilename, 0); if (output_type == TCC_OUTPUT_DLL) strcpy(ext, ".dll"); else diff --git a/programs/develop/metcc/trunk/source/tcctest.c b/programs/develop/metcc/trunk/source/tcctest.c index 70f2d9b6a5..790250eea2 100644 --- a/programs/develop/metcc/trunk/source/tcctest.c +++ b/programs/develop/metcc/trunk/source/tcctest.c @@ -138,7 +138,7 @@ int qq(int x) void macro_test(void) { - printf("macro:\n"); + printf("macro:\n"); pf("N=%d\n", N); printf("aaa=%d\n", AAA); @@ -1676,14 +1676,15 @@ void whitespace_test(void) { char *str; - #if 1 + +#if 1 pri\ -ntf("whitspace:\n"); +ntf("whitspace:\n"); #endif pf("N=%d\n", 2); #ifdef CORRECT_CR_HANDLING - pri\ + pri\ ntf("aaa=%d\n", 3); #endif @@ -1695,11 +1696,12 @@ ntf("min=%d\n", 4); printf("len1=%d\n", strlen(" ")); #ifdef CORRECT_CR_HANDLING - str = " + str = " "; printf("len1=%d str[0]=%d\n", strlen(str), str[0]); #endif - printf("len1=%d\n", strlen(" a + printf("len1=%d\n", strlen(" +a ")); #endif /* ACCEPT_CR_IN_STRINGS */ }