Unified · Split
| @@ -520,7 +520,8 @@ void expr_eq(void); · 519 unmodified lines | ||
| 520 | 520 | void gexpr(void); |
| 521 | 521 | void decl(int l); |
| 522 | 522 | void decl_initializer(int t, int r, int c, int first, int size_only); |
| 523 | −int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init); | |
| 523 | +int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, | |
| 524 | + int v, int scope); | |
| 524 | 525 | int gv(int rc); |
| 525 | 526 | void gv2(int rc1, int rc2); |
| 526 | 527 | void move_reg(int r, int s); |
| @@ -4670,7 +4671,7 @@ void unary(void) · 4143 unmodified lines | ||
| 4670 | 4671 | if (!(ft & VT_ARRAY)) |
| 4671 | 4672 | r |= lvalue_type(ft); |
| 4672 | 4673 | memset(&ad, 0, sizeof(AttributeDef)); |
| 4673 | − fc = decl_initializer_alloc(ft, &ad, r, 1); | |
| 4674 | + fc = decl_initializer_alloc(ft, &ad, r, 1, 0, 0); | |
| 4674 | 4675 | vset(ft, r, fc); |
| 4675 | 4676 | } else { |
| 4676 | 4677 | unary(); |
| @@ -5674,8 +5675,11 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 997 unmodified lines | ||
| 5674 | 5675 | |
| 5675 | 5676 | /* parse an initializer for type 't' if 'has_init' is true, and |
| 5676 | 5677 | allocate space in local or global data space ('r' is either |
| 5677 | − VT_LOCAL or VT_CONST). The allocated address in returned */ | |
| 5678 | −int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) | |
| 5678 | + VT_LOCAL or VT_CONST). The allocated address in returned. If 'v' is | |
| 5679 | + non zero, then an associated variable 'v' of scope 'scope' is | |
| 5680 | + declared before initializers are parsed. */ | |
| 5681 | +int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, | |
| 5682 | + int v, int scope) | |
| 5679 | 5683 | { |
| 5680 | 5684 | int size, align, addr, data_offset; |
| 5681 | 5685 | int level; |
| @@ -5785,6 +5789,24 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 103 unmodified lines | ||
| 5785 | 5789 | } |
| 5786 | 5790 | sec->data_ptr = (unsigned char *)data_offset; |
| 5787 | 5791 | } |
| 5792 | + if (v) { | |
| 5793 | + Sym *sym; | |
| 5794 | + if (scope == VT_CONST) { | |
| 5795 | + /* global scope: see if already defined */ | |
| 5796 | + sym = sym_find(v); | |
| 5797 | + if (!sym) | |
| 5798 | + goto do_def; | |
| 5799 | + if (!is_compatible_types(sym->t, t)) | |
| 5800 | + error("incompatible types for redefinition of '%s'", | |
| 5801 | + get_tok_str(v, NULL)); | |
| 5802 | + if (!(sym->r & VT_FORWARD)) | |
| 5803 | + error("redefinition of '%s'", get_tok_str(v, NULL)); | |
| 5804 | + greloc_patch(sym, addr); | |
| 5805 | + } else { | |
| 5806 | + do_def: | |
| 5807 | + sym_push(v, t, r, addr); | |
| 5808 | + } | |
| 5809 | + } | |
| 5788 | 5810 | if (has_init) { |
| 5789 | 5811 | decl_initializer(t, r, addr, 1, 0); |
| 5790 | 5812 | /* restore parse state if needed */ |
| @@ -5798,16 +5820,8 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 7 unmodified lines | ||
| 5798 | 5820 | |
| 5799 | 5821 | void put_func_debug(int t) |
| 5800 | 5822 | { |
| 5801 | − int bind; | |
| 5802 | 5823 | char buf[512]; |
| 5803 | 5824 | |
| 5804 | − if (t & VT_STATIC) | |
| 5805 | − bind = STB_LOCAL; | |
| 5806 | − else | |
| 5807 | − bind = STB_GLOBAL; | |
| 5808 | − put_elf_sym(symtab_section, ind, 0, | |
| 5809 | − ELF32_ST_INFO(bind, STT_FUNC), 0, | |
| 5810 | − cur_text_section->sh_num, funcname); | |
| 5811 | 5825 | /* stabs info */ |
| 5812 | 5826 | /* XXX: we put here a dummy type */ |
| 5813 | 5827 | snprintf(buf, sizeof(buf), "%s:%c1", |
| @@ -5880,7 +5894,7 @@ void analyse_function(void) · 66 unmodified lines | ||
| 5880 | 5894 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ |
| 5881 | 5895 | void decl(int l) |
| 5882 | 5896 | { |
| 5883 | − int t, b, v, addr, has_init, r; | |
| 5897 | + int t, b, v, has_init, r, bind; | |
| 5884 | 5898 | Sym *sym; |
| 5885 | 5899 | AttributeDef ad; |
| 5886 | 5900 | |
| @@ -5972,6 +5986,15 @@ void decl(int l) · 85 unmodified lines | ||
| 5972 | 5986 | } |
| 5973 | 5987 | sym->r = VT_CONST; |
| 5974 | 5988 | funcname = get_tok_str(v, NULL); |
| 5989 | + /* add symbol info */ | |
| 5990 | + if (t & VT_STATIC) | |
| 5991 | + bind = STB_LOCAL; | |
| 5992 | + else | |
| 5993 | + bind = STB_GLOBAL; | |
| 5994 | + put_elf_sym(symtab_section, ind, 0, | |
| 5995 | + ELF32_ST_INFO(bind, STT_FUNC), 0, | |
| 5996 | + cur_text_section->sh_num, funcname); | |
| 5997 | + | |
| 5975 | 5998 | /* put debug symbol */ |
| 5976 | 5999 | if (do_debug) |
| 5977 | 6000 | put_func_debug(t); |
| @@ -6027,23 +6050,8 @@ void decl(int l) · 49 unmodified lines | ||
| 6027 | 6050 | has_init = (tok == '='); |
| 6028 | 6051 | if (has_init) |
| 6029 | 6052 | next(); |
| 6030 | − addr = decl_initializer_alloc(t, &ad, r, | |
| 6031 | − has_init); | |
| 6032 | − if (l == VT_CONST) { | |
| 6033 | − /* global scope: see if already defined */ | |
| 6034 | − sym = sym_find(v); | |
| 6035 | − if (!sym) | |
| 6036 | − goto do_def; | |
| 6037 | − if (!is_compatible_types(sym->t, t)) | |
| 6038 | − error("incompatible types for redefinition of '%s'", | |
| 6039 | − get_tok_str(v, NULL)); | |
| 6040 | − if (!(sym->r & VT_FORWARD)) | |
| 6041 | − error("redefinition of '%s'", get_tok_str(v, NULL)); | |
| 6042 | − greloc_patch(sym, addr); | |
| 6043 | − } else { | |
| 6044 | − do_def: | |
| 6045 | − sym_push(v, t, r, addr); | |
| 6046 | − } | |
| 6053 | + decl_initializer_alloc(t, &ad, r, | |
| 6054 | + has_init, v, l); | |
| 6047 | 6055 | } |
| 6048 | 6056 | } |
| 6049 | 6057 | if (tok != ',') { |
| @@ -6310,8 +6318,8 @@ static int put_elf_str(Section *s, const char *sym) · 260 unmodified lines | ||
| 6310 | 6318 | } |
| 6311 | 6319 | |
| 6312 | 6320 | static void put_elf_sym(Section *s, |
| 6313 | − unsigned long value, unsigned long size, | |
| 6314 | − int info, int other, int shndx, const char *name) | |
| 6321 | + unsigned long value, unsigned long size, | |
| 6322 | + int info, int other, int shndx, const char *name) | |
| 6315 | 6323 | { |
| 6316 | 6324 | int name_offset; |
| 6317 | 6325 | Elf32_Sym *sym; |
| @@ -6393,7 +6401,8 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 75 unmodified lines | ||
| 6393 | 6401 | phnum = 0; |
| 6394 | 6402 | for(sec = first_section; sec != NULL; sec = sec->next) { |
| 6395 | 6403 | shnum++; |
| 6396 | − if (sec->sh_flags & SHF_ALLOC) | |
| 6404 | + if ((sec->sh_flags & SHF_ALLOC) && | |
| 6405 | + file_type != TCC_FILE_OBJ) | |
| 6397 | 6406 | phnum++; |
| 6398 | 6407 | } |
| 6399 | 6408 | /* allocate section headers */ |
| @@ -6401,6 +6410,7 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 1 unmodified line | ||
| 6401 | 6410 | if (!shdr) |
| 6402 | 6411 | error("memory full"); |
| 6403 | 6412 | memset(shdr, 0, shnum * sizeof(Elf32_Shdr)); |
| 6413 | + | |
| 6404 | 6414 | /* allocate program segment headers */ |
| 6405 | 6415 | phdr = malloc(phnum * sizeof(Elf32_Phdr)); |
| 6406 | 6416 | if (!phdr) |
| @@ -6434,24 +6444,26 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 27 unmodified lines | ||
| 6434 | 6444 | file_offset += sh->sh_size; |
| 6435 | 6445 | } |
| 6436 | 6446 | /* build program headers (simplistic - not fully correct) */ |
| 6437 | − j = 0; | |
| 6438 | − for(i=1;i<shnum;i++) { | |
| 6439 | − sh = &shdr[i]; | |
| 6440 | − if (sh->sh_type == SHT_PROGBITS && | |
| 6441 | − (sh->sh_flags & SHF_ALLOC) != 0) { | |
| 6442 | − ph = &phdr[j++]; | |
| 6443 | − ph->p_type = PT_LOAD; | |
| 6444 | − ph->p_offset = sh->sh_offset; | |
| 6445 | − ph->p_vaddr = sh->sh_addr; | |
| 6446 | − ph->p_paddr = ph->p_vaddr; | |
| 6447 | − ph->p_filesz = sh->sh_size; | |
| 6448 | − ph->p_memsz = sh->sh_size; | |
| 6449 | − ph->p_flags = PF_R; | |
| 6450 | − if (sh->sh_flags & SHF_WRITE) | |
| 6451 | − ph->p_flags |= PF_W; | |
| 6452 | − if (sh->sh_flags & SHF_EXECINSTR) | |
| 6453 | − ph->p_flags |= PF_X; | |
| 6454 | − ph->p_align = sh->sh_addralign; | |
| 6447 | + if (phnum > 0) { | |
| 6448 | + j = 0; | |
| 6449 | + for(i=1;i<shnum;i++) { | |
| 6450 | + sh = &shdr[i]; | |
| 6451 | + if (sh->sh_type == SHT_PROGBITS && | |
| 6452 | + (sh->sh_flags & SHF_ALLOC) != 0) { | |
| 6453 | + ph = &phdr[j++]; | |
| 6454 | + ph->p_type = PT_LOAD; | |
| 6455 | + ph->p_offset = sh->sh_offset; | |
| 6456 | + ph->p_vaddr = sh->sh_addr; | |
| 6457 | + ph->p_paddr = ph->p_vaddr; | |
| 6458 | + ph->p_filesz = sh->sh_size; | |
| 6459 | + ph->p_memsz = sh->sh_size; | |
| 6460 | + ph->p_flags = PF_R; | |
| 6461 | + if (sh->sh_flags & SHF_WRITE) | |
| 6462 | + ph->p_flags |= PF_W; | |
| 6463 | + if (sh->sh_flags & SHF_EXECINSTR) | |
| 6464 | + ph->p_flags |= PF_X; | |
| 6465 | + ph->p_align = sh->sh_addralign; | |
| 6466 | + } | |
| 6455 | 6467 | } |
| 6456 | 6468 | } |
| 6457 | 6469 | |
| @@ -6466,7 +6478,18 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 8 unmodified lines | ||
| 6466 | 6478 | ehdr.e_ident[4] = ELFCLASS32; |
| 6467 | 6479 | ehdr.e_ident[5] = ELFDATA2LSB; |
| 6468 | 6480 | ehdr.e_ident[6] = EV_CURRENT; |
| 6469 | − ehdr.e_type = ET_EXEC; | |
| 6481 | + switch(file_type) { | |
| 6482 | + default: | |
| 6483 | + case TCC_FILE_EXE: | |
| 6484 | + ehdr.e_type = ET_EXEC; | |
| 6485 | + break; | |
| 6486 | + case TCC_FILE_DLL: | |
| 6487 | + ehdr.e_type = ET_DYN; | |
| 6488 | + break; | |
| 6489 | + case TCC_FILE_OBJ: | |
| 6490 | + ehdr.e_type = ET_REL; | |
| 6491 | + break; | |
| 6492 | + } | |
| 6470 | 6493 | ehdr.e_machine = EM_386; |
| 6471 | 6494 | ehdr.e_version = EV_CURRENT; |
| 6472 | 6495 | ehdr.e_entry = 0; /* XXX: patch it */ |
| @@ -6502,6 +6525,9 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 29 unmodified lines | ||
| 6502 | 6525 | } |
| 6503 | 6526 | fwrite(shdr, 1, shnum * sizeof(Elf32_Shdr), f); |
| 6504 | 6527 | fclose(f); |
| 6528 | + | |
| 6529 | + free(shdr); | |
| 6530 | + free(phdr); | |
| 6505 | 6531 | return 0; |
| 6506 | 6532 | } |
| 6507 | 6533 | |
| @@ -6736,7 +6762,15 @@ TCCState *tcc_new(void) · 228 unmodified lines | ||
| 6736 | 6762 | data_section = new_section(".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); |
| 6737 | 6763 | /* XXX: should change type to SHT_NOBITS */ |
| 6738 | 6764 | bss_section = new_section(".bss", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); |
| 6739 | − | |
| 6765 | + | |
| 6766 | + /* symbols are always generated for linking stage */ | |
| 6767 | + symtab_section = new_section(".symtab", SHT_SYMTAB, 0); | |
| 6768 | + symtab_section->sh_entsize = sizeof(Elf32_Sym); | |
| 6769 | + strtab_section = new_section(".strtab", SHT_STRTAB, 0); | |
| 6770 | + put_elf_str(strtab_section, ""); | |
| 6771 | + symtab_section->link = strtab_section; | |
| 6772 | + put_elf_sym(symtab_section, 0, 0, 0, 0, 0, NULL); | |
| 6773 | + | |
| 6740 | 6774 | return s; |
| 6741 | 6775 | } |
| 6742 | 6776 | |
| @@ -6764,7 +6798,7 @@ void help(void) · 21 unmodified lines | ||
| 6764 | 6798 | { |
| 6765 | 6799 | printf("tcc version 0.9.8 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" |
| 6766 | 6800 | "usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]\n" |
| 6767 | − " [-i infile] infile [infile_args...]\n" | |
| 6801 | + " [-i infile] [-bench] infile [infile_args...]\n" | |
| 6768 | 6802 | "\n" |
| 6769 | 6803 | "-Idir : add include path 'dir'\n" |
| 6770 | 6804 | "-Dsym[=val] : define 'sym' with value 'val'\n" |
| @@ -6775,16 +6809,18 @@ void help(void) · 4 unmodified lines | ||
| 6775 | 6809 | "-b : compile with built-in memory and bounds checker (implies -g)\n" |
| 6776 | 6810 | #endif |
| 6777 | 6811 | "-i infile : compile infile\n" |
| 6812 | + "-bench : output compilation statistics\n" | |
| 6778 | 6813 | ); |
| 6779 | 6814 | } |
| 6780 | 6815 | |
| 6781 | 6816 | int main(int argc, char **argv) |
| 6782 | 6817 | { |
| 6783 | 6818 | char *r, *outfile; |
| 6784 | − int optind; | |
| 6819 | + int optind, file_type; | |
| 6785 | 6820 | TCCState *s; |
| 6786 | 6821 | |
| 6787 | 6822 | s = tcc_new(); |
| 6823 | + file_type = TCC_FILE_EXE; | |
| 6788 | 6824 | |
| 6789 | 6825 | optind = 1; |
| 6790 | 6826 | outfile = NULL; |
| @@ -6852,17 +6888,15 @@ int main(int argc, char **argv) · 61 unmodified lines | ||
| 6852 | 6888 | stab_section->link = stabstr_section; |
| 6853 | 6889 | /* put first entry */ |
| 6854 | 6890 | put_stabs("", 0, 0, 0, 0); |
| 6855 | − | |
| 6856 | − /* elf symbols */ | |
| 6857 | − symtab_section = new_section(".symtab", SHT_SYMTAB, 0); | |
| 6858 | − symtab_section->sh_entsize = sizeof(Elf32_Sym); | |
| 6859 | − strtab_section = new_section(".strtab", SHT_STRTAB, 0); | |
| 6860 | − put_elf_str(strtab_section, ""); | |
| 6861 | − symtab_section->link = strtab_section; | |
| 6862 | − put_elf_sym(symtab_section, 0, 0, 0, 0, 0, NULL); | |
| 6863 | 6891 | } |
| 6892 | + } else | |
| 6893 | + /* the following options are only for testing, so not | |
| 6894 | + documented */ | |
| 6895 | + if (r[1] == 'c') { | |
| 6896 | + file_type = TCC_FILE_OBJ; | |
| 6897 | + } else if (!strcmp(r + 1, "shared")) { | |
| 6898 | + file_type = TCC_FILE_DLL; | |
| 6864 | 6899 | } else if (r[1] == 'o') { |
| 6865 | − /* currently, only for testing, so not documented */ | |
| 6866 | 6900 | if (optind >= argc) |
| 6867 | 6901 | goto show_help; |
| 6868 | 6902 | outfile = argv[optind++]; |
| @@ -6879,7 +6913,7 @@ int main(int argc, char **argv) · 10 unmodified lines | ||
| 6879 | 6913 | } |
| 6880 | 6914 | |
| 6881 | 6915 | if (outfile) { |
| 6882 | − tcc_output_file(s, outfile, TCC_FILE_EXE); | |
| 6916 | + tcc_output_file(s, outfile, file_type); | |
| 6883 | 6917 | return 0; |
| 6884 | 6918 | } else { |
| 6885 | 6919 | return tcc_run(s, argc - optind, argv + optind); |