Unified · Split
| @@ -29,7 +29,6 @@ · 28 unmodified lines | |||
| 29 | #include <fcntl.h> | 29 | #include <fcntl.h> |
| 30 | #ifndef WIN32 | 30 | #ifndef WIN32 |
| 31 | #include <sys/ucontext.h> | 31 | #include <sys/ucontext.h> |
| 32 | #include <sys/mman.h> | ||
| 33 | #endif | 32 | #endif |
| 34 | #include "elf.h" | 33 | #include "elf.h" |
| 35 | #include "stab.h" | 34 | #include "stab.h" |
| @@ -43,6 +42,8 @@ · 7 unmodified lines | |||
| 43 | /* preprocessor debug */ | 42 | /* preprocessor debug */ |
| 44 | //#define PP_DEBUG | 43 | //#define PP_DEBUG |
| 45 | 44 | ||
| 45 | //#define MEM_DEBUG | ||
| 46 | |||
| 46 | /* target selection */ | 47 | /* target selection */ |
| 47 | //#define TCC_TARGET_I386 /* i386 code generator */ | 48 | //#define TCC_TARGET_I386 /* i386 code generator */ |
| 48 | //#define TCC_TARGET_IL /* .NET CLI generator */ | 49 | //#define TCC_TARGET_IL /* .NET CLI generator */ |
| @@ -64,10 +65,6 @@ · 15 unmodified lines | |||
| 64 | executables or dlls */ | 65 | executables or dlls */ |
| 65 | #define CONFIG_TCC_CRT_PREFIX "/usr/lib" | 66 | #define CONFIG_TCC_CRT_PREFIX "/usr/lib" |
| 66 | 67 | ||
| 67 | /* amount of virtual memory associated to a section (currently, we do | ||
| 68 | not realloc them) */ | ||
| 69 | #define SECTION_VSIZE (1024 * 1024) | ||
| 70 | |||
| 71 | #define INCLUDE_STACK_SIZE 32 | 68 | #define INCLUDE_STACK_SIZE 32 |
| 72 | #define IFDEF_STACK_SIZE 64 | 69 | #define IFDEF_STACK_SIZE 64 |
| 73 | #define VSTACK_SIZE 64 | 70 | #define VSTACK_SIZE 64 |
| @@ -85,6 +82,13 @@ typedef struct TokenSym { · 11 unmodified lines | |||
| 85 | char str[1]; | 82 | char str[1]; |
| 86 | } TokenSym; | 83 | } TokenSym; |
| 87 | 84 | ||
| 85 | typedef struct CString { | ||
| 86 | int size; /* size in bytes */ | ||
| 87 | void *data; /* either 'char *' or 'int *' */ | ||
| 88 | int size_allocated; | ||
| 89 | void *data_allocated; /* if non NULL, data has been malloced */ | ||
| 90 | } CString; | ||
| 91 | |||
| 88 | /* constant value */ | 92 | /* constant value */ |
| 89 | typedef union CValue { | 93 | typedef union CValue { |
| 90 | long double ld; | 94 | long double ld; |
| @@ -95,7 +99,7 @@ typedef union CValue { · 4 unmodified lines | |||
| 95 | unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */ | 99 | unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */ |
| 96 | long long ll; | 100 | long long ll; |
| 97 | unsigned long long ull; | 101 | unsigned long long ull; |
| 98 | struct TokenSym *ts; | 102 | struct CString *cstr; |
| 99 | struct Sym *sym; | 103 | struct Sym *sym; |
| 100 | void *ptr; | 104 | void *ptr; |
| 101 | int tab[1]; | 105 | int tab[1]; |
| @@ -135,6 +139,7 @@ typedef struct SymStack { · 33 unmodified lines | |||
| 135 | typedef struct Section { | 139 | typedef struct Section { |
| 136 | unsigned long data_offset; /* current data offset */ | 140 | unsigned long data_offset; /* current data offset */ |
| 137 | unsigned char *data; /* section data */ | 141 | unsigned char *data; /* section data */ |
| 142 | unsigned long data_allocated; /* used for realloc() handling */ | ||
| 138 | int sh_name; /* elf section name (only used during output) */ | 143 | int sh_name; /* elf section name (only used during output) */ |
| 139 | int sh_num; /* elf section number */ | 144 | int sh_num; /* elf section number */ |
| 140 | int sh_type; /* elf section type */ | 145 | int sh_type; /* elf section type */ |
| @@ -223,6 +228,7 @@ typedef struct TokenString { · 82 unmodified lines | |||
| 223 | struct BufferedFile *file; | 228 | struct BufferedFile *file; |
| 224 | int ch, ch1, tok, tok1; | 229 | int ch, ch1, tok, tok1; |
| 225 | CValue tokc, tok1c; | 230 | CValue tokc, tok1c; |
| 231 | CString tokcstr; /* current parsed string, if any */ | ||
| 226 | int return_linefeed; /* if true, line feed is returned as a token */ | 232 | int return_linefeed; /* if true, line feed is returned as a token */ |
| 227 | 233 | ||
| 228 | /* sections */ | 234 | /* sections */ |
| @@ -284,6 +290,8 @@ BufferedFile *include_stack[INCLUDE_STACK_SIZE], **include_stack_ptr; · 55 unmodified lines | |||
| 284 | int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; | 290 | int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; |
| 285 | char **include_paths; | 291 | char **include_paths; |
| 286 | int nb_include_paths; | 292 | int nb_include_paths; |
| 293 | char **sysinclude_paths; | ||
| 294 | int nb_sysinclude_paths; | ||
| 287 | int char_pointer_type; | 295 | int char_pointer_type; |
| 288 | int func_old_type; | 296 | int func_old_type; |
| 289 | 297 | ||
| @@ -427,6 +435,9 @@ struct TCCState { · 137 unmodified lines | |||
| 427 | #define TOK_A_SHL 0x81 | 435 | #define TOK_A_SHL 0x81 |
| 428 | #define TOK_A_SAR 0x82 | 436 | #define TOK_A_SAR 0x82 |
| 429 | 437 | ||
| 438 | /* WARNING: the content of this string encodes token numbers */ | ||
| 439 | static char tok_two_chars[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266"; | ||
| 440 | |||
| 430 | #define TOK_EOF (-1) /* end of file */ | 441 | #define TOK_EOF (-1) /* end of file */ |
| 431 | #define TOK_LINEFEED 10 /* line feed */ | 442 | #define TOK_LINEFEED 10 /* line feed */ |
| 432 | 443 | ||
| @@ -578,6 +589,7 @@ Sym *sym_push(int v, int t, int r, int c); · 145 unmodified lines | |||
| 578 | int type_size(int t, int *a); | 589 | int type_size(int t, int *a); |
| 579 | int pointed_type(int t); | 590 | int pointed_type(int t); |
| 580 | int pointed_size(int t); | 591 | int pointed_size(int t); |
| 592 | static int lvalue_type(int t); | ||
| 581 | int is_compatible_types(int t1, int t2); | 593 | int is_compatible_types(int t1, int t2); |
| 582 | int parse_btype(int *type_ptr, AttributeDef *ad); | 594 | int parse_btype(int *type_ptr, AttributeDef *ad); |
| 583 | int type_decl(AttributeDef *ad, int *v, int t, int td); | 595 | int type_decl(AttributeDef *ad, int *v, int t, int td); |
| @@ -594,6 +606,7 @@ static Sym *get_sym_ref(int t, Section *sec, · 10 unmodified lines | |||
| 594 | unsigned long offset, unsigned long size); | 606 | unsigned long offset, unsigned long size); |
| 595 | 607 | ||
| 596 | /* section generation */ | 608 | /* section generation */ |
| 609 | static void section_realloc(Section *sec, unsigned long new_size); | ||
| 597 | static void *section_ptr(Section *sec, unsigned long size); | 610 | static void *section_ptr(Section *sec, unsigned long size); |
| 598 | static void *section_ptr_add(Section *sec, unsigned long size); | 611 | static void *section_ptr_add(Section *sec, unsigned long size); |
| 599 | static void put_extern_sym(Sym *sym, Section *section, | 612 | static void put_extern_sym(Sym *sym, Section *section, |
| @@ -603,6 +616,8 @@ static int put_elf_str(Section *s, const char *sym); · 3 unmodified lines | |||
| 603 | static int put_elf_sym(Section *s, | 616 | static int put_elf_sym(Section *s, |
| 604 | unsigned long value, unsigned long size, | 617 | unsigned long value, unsigned long size, |
| 605 | int info, int other, int shndx, const char *name); | 618 | int info, int other, int shndx, const char *name); |
| 619 | static int add_elf_sym(Section *s, unsigned long value, unsigned long size, | ||
| 620 | int info, int sh_num, const char *name); | ||
| 606 | static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, | 621 | static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, |
| 607 | int type, int symbol); | 622 | int type, int symbol); |
| 608 | static void put_stabs(const char *str, int type, int other, int desc, | 623 | static void put_stabs(const char *str, int type, int other, int desc, |
| @@ -720,6 +735,70 @@ static char *pstrcat(char *buf, int buf_size, const char *s) · 111 unmodified lines | |||
| 720 | return buf; | 735 | return buf; |
| 721 | } | 736 | } |
| 722 | 737 | ||
| 738 | /* memory management */ | ||
| 739 | #ifdef MEM_DEBUG | ||
| 740 | int mem_cur_size; | ||
| 741 | int mem_max_size; | ||
| 742 | #endif | ||
| 743 | |||
| 744 | static inline void tcc_free(void *ptr) | ||
| 745 | { | ||
| 746 | #ifdef MEM_DEBUG | ||
| 747 | mem_cur_size -= malloc_usable_size(ptr); | ||
| 748 | #endif | ||
| 749 | free(ptr); | ||
| 750 | } | ||
| 751 | |||
| 752 | static void *tcc_malloc(unsigned long size) | ||
| 753 | { | ||
| 754 | void *ptr; | ||
| 755 | ptr = malloc(size); | ||
| 756 | if (!ptr) | ||
| 757 | error("memory full"); | ||
| 758 | #ifdef MEM_DEBUG | ||
| 759 | mem_cur_size += malloc_usable_size(ptr); | ||
| 760 | if (mem_cur_size > mem_max_size) | ||
| 761 | mem_max_size = mem_cur_size; | ||
| 762 | #endif | ||
| 763 | return ptr; | ||
| 764 | } | ||
| 765 | |||
| 766 | static void *tcc_mallocz(unsigned long size) | ||
| 767 | { | ||
| 768 | void *ptr; | ||
| 769 | ptr = tcc_malloc(size); | ||
| 770 | memset(ptr, 0, size); | ||
| 771 | return ptr; | ||
| 772 | } | ||
| 773 | |||
| 774 | static inline void *tcc_realloc(void *ptr, unsigned long size) | ||
| 775 | { | ||
| 776 | void *ptr1; | ||
| 777 | #ifdef MEM_DEBUG | ||
| 778 | mem_cur_size -= malloc_usable_size(ptr); | ||
| 779 | #endif | ||
| 780 | ptr1 = realloc(ptr, size); | ||
| 781 | #ifdef MEM_DEBUG | ||
| 782 | /* NOTE: count not correct if alloc error, but not critical */ | ||
| 783 | mem_cur_size += malloc_usable_size(ptr1); | ||
| 784 | if (mem_cur_size > mem_max_size) | ||
| 785 | mem_max_size = mem_cur_size; | ||
| 786 | #endif | ||
| 787 | return ptr1; | ||
| 788 | } | ||
| 789 | |||
| 790 | static char *tcc_strdup(const char *str) | ||
| 791 | { | ||
| 792 | char *ptr; | ||
| 793 | ptr = tcc_malloc(strlen(str) + 1); | ||
| 794 | strcpy(ptr, str); | ||
| 795 | return ptr; | ||
| 796 | } | ||
| 797 | |||
| 798 | #define free(p) use_tcc_free(p) | ||
| 799 | #define malloc(s) use_tcc_malloc(s) | ||
| 800 | #define realloc(p, s) use_tcc_realloc(p, s) | ||
| 801 | |||
| 723 | static void dynarray_add(void ***ptab, int *nb_ptr, void *data) | 802 | static void dynarray_add(void ***ptab, int *nb_ptr, void *data) |
| 724 | { | 803 | { |
| 725 | int nb, nb_alloc; | 804 | int nb, nb_alloc; |
| @@ -733,7 +812,7 @@ static void dynarray_add(void ***ptab, int *nb_ptr, void *data) · 7 unmodified lines | |||
| 733 | nb_alloc = 1; | 812 | nb_alloc = 1; |
| 734 | else | 813 | else |
| 735 | nb_alloc = nb * 2; | 814 | nb_alloc = nb * 2; |
| 736 | pp = realloc(pp, nb_alloc * sizeof(void *)); | 815 | pp = tcc_realloc(pp, nb_alloc * sizeof(void *)); |
| 737 | if (!pp) | 816 | if (!pp) |
| 738 | error("memory full"); | 817 | error("memory full"); |
| 739 | *ptab = pp; | 818 | *ptab = pp; |
| @@ -745,12 +824,8 @@ static void dynarray_add(void ***ptab, int *nb_ptr, void *data) · 5 unmodified lines | |||
| 745 | Section *new_section(const char *name, int sh_type, int sh_flags) | 824 | Section *new_section(const char *name, int sh_type, int sh_flags) |
| 746 | { | 825 | { |
| 747 | Section *sec; | 826 | Section *sec; |
| 748 | void *data; | ||
| 749 | 827 | ||
| 750 | sec = malloc(sizeof(Section)); | 828 | sec = tcc_mallocz(sizeof(Section)); |
| 751 | if (!sec) | ||
| 752 | error("memory full"); | ||
| 753 | memset(sec, 0, sizeof(Section)); | ||
| 754 | pstrcpy(sec->name, sizeof(sec->name), name); | 829 | pstrcpy(sec->name, sizeof(sec->name), name); |
| 755 | sec->sh_type = sh_type; | 830 | sec->sh_type = sh_type; |
| 756 | sec->sh_flags = sh_flags; | 831 | sec->sh_flags = sh_flags; |
| @@ -769,23 +844,8 @@ Section *new_section(const char *name, int sh_type, int sh_flags) · 12 unmodified lines | |||
| 769 | sec->sh_addralign = 32; /* default conservative alignment */ | 844 | sec->sh_addralign = 32; /* default conservative alignment */ |
| 770 | break; | 845 | break; |
| 771 | } | 846 | } |
| 772 | #ifdef WIN32 | ||
| 773 | /* XXX: currently, a single malloc */ | ||
| 774 | data = malloc(SECTION_VSIZE); | ||
| 775 | if (data == NULL) | ||
| 776 | error("could not alloc section '%s'", name); | ||
| 777 | #else | ||
| 778 | data = mmap(NULL, SECTION_VSIZE, | ||
| 779 | PROT_EXEC | PROT_READ | PROT_WRITE, | ||
| 780 | MAP_PRIVATE | MAP_ANONYMOUS, | ||
| 781 | -1, 0); | ||
| 782 | if (data == (void *)(-1)) | ||
| 783 | error("could not mmap section '%s'", name); | ||
| 784 | #endif | ||
| 785 | sec->data = data; | ||
| 786 | sec->data_offset = 0; | ||
| 787 | 847 | ||
| 788 | /* only add section if not private */ | 848 | /* only add section if not private */ |
| 789 | if (!(sh_flags & SHF_PRIVATE)) { | 849 | if (!(sh_flags & SHF_PRIVATE)) { |
| 790 | sec->sh_num = nb_sections; | 850 | sec->sh_num = nb_sections; |
| 791 | dynarray_add((void ***)§ions, &nb_sections, sec); | 851 | dynarray_add((void ***)§ions, &nb_sections, sec); |
| @@ -793,18 +853,47 @@ Section *new_section(const char *name, int sh_type, int sh_flags) · 1 unmodified line | |||
| 793 | return sec; | 853 | return sec; |
| 794 | } | 854 | } |
| 795 | 855 | ||
| 796 | /* reserve at least 'size' bytes in section 'sec' from sec->data_offset */ | 856 | /* realloc section and set its content to zero */ |
| 797 | static void *section_ptr(Section *sec, unsigned long size) | 857 | static void section_realloc(Section *sec, unsigned long new_size) |
| 798 | { | 858 | { |
| 799 | return sec->data + sec->data_offset; | 859 | unsigned long size; |
| 860 | unsigned char *data; | ||
| 861 | |||
| 862 | size = sec->data_allocated; | ||
| 863 | if (size == 0) | ||
| 864 | size = 1; | ||
| 865 | while (size < new_size) | ||
| 866 | size = size * 2; | ||
| 867 | data = tcc_realloc(sec->data, size); | ||
| 868 | if (!data) | ||
| 869 | error("memory full"); | ||
| 870 | memset(data + sec->data_allocated, 0, size - sec->data_allocated); | ||
| 871 | sec->data = data; | ||
| 872 | sec->data_allocated = size; | ||
| 873 | } | ||
| 874 | |||
| 875 | /* reserve at least 'size' bytes in section 'sec' from | ||
| 876 | sec->data_offset. Optimized for speed */ | ||
| 877 | static inline void *section_ptr(Section *sec, unsigned long size) | ||
| 878 | { | ||
| 879 | unsigned long offset, offset1; | ||
| 880 | offset = sec->data_offset; | ||
| 881 | offset1 = offset + size; | ||
| 882 | if (offset1 > sec->data_allocated) | ||
| 883 | section_realloc(sec, offset1); | ||
| 884 | return sec->data + offset; | ||
| 800 | } | 885 | } |
| 801 | 886 | ||
| 802 | static void *section_ptr_add(Section *sec, unsigned long size) | 887 | static void *section_ptr_add(Section *sec, unsigned long size) |
| 803 | { | 888 | { |
| 804 | void *ptr; | 889 | unsigned long offset, offset1; |
| 805 | ptr = sec->data + sec->data_offset; | 890 | |
| 806 | sec->data_offset += size; | 891 | offset = sec->data_offset; |
| 807 | return ptr; | 892 | offset1 = offset + size; |
| 893 | if (offset1 > sec->data_allocated) | ||
| 894 | section_realloc(sec, offset1); | ||
| 895 | sec->data_offset = offset1; | ||
| 896 | return sec->data + offset; | ||
| 808 | } | 897 | } |
| 809 | 898 | ||
| 810 | /* return a reference to a section, and create it if it does not | 899 | /* return a reference to a section, and create it if it does not |
| @@ -822,6 +911,63 @@ Section *find_section(const char *name) · 11 unmodified lines | |||
| 822 | return new_section(name, SHT_PROGBITS, SHF_ALLOC); | 911 | return new_section(name, SHT_PROGBITS, SHF_ALLOC); |
| 823 | } | 912 | } |
| 824 | 913 | ||
| 914 | /* update sym->c so that it points to an external symbol in section | ||
| 915 | 'section' with value 'value' */ | ||
| 916 | static void put_extern_sym(Sym *sym, Section *section, | ||
| 917 | unsigned long value, unsigned long size) | ||
| 918 | { | ||
| 919 | int sym_type, sym_bind, sh_num, info; | ||
| 920 | Elf32_Sym *esym; | ||
| 921 | const char *name; | ||
| 922 | char buf[32]; | ||
| 923 | |||
| 924 | if (section) | ||
| 925 | sh_num = section->sh_num; | ||
| 926 | else | ||
| 927 | sh_num = SHN_UNDEF; | ||
| 928 | if (!sym->c) { | ||
| 929 | if ((sym->t & VT_BTYPE) == VT_FUNC) | ||
| 930 | sym_type = STT_FUNC; | ||
| 931 | else | ||
| 932 | sym_type = STT_OBJECT; | ||
| 933 | if (sym->t & VT_STATIC) | ||
| 934 | sym_bind = STB_LOCAL; | ||
| 935 | else | ||
| 936 | sym_bind = STB_GLOBAL; | ||
| 937 | |||
| 938 | name = get_tok_str(sym->v, NULL); | ||
| 939 | #ifdef CONFIG_TCC_BCHECK | ||
| 940 | if (do_bounds_check) { | ||
| 941 | /* if bound checking is activated, we change some function | ||
| 942 | names by adding the "__bound" prefix */ | ||
| 943 | switch(sym->v) { | ||
| 944 | case TOK_malloc: | ||
| 945 | case TOK_free: | ||
| 946 | case TOK_realloc: | ||
| 947 | case TOK_memalign: | ||
| 948 | case TOK_calloc: | ||
| 949 | case TOK_memcpy: | ||
| 950 | case TOK_memmove: | ||
| 951 | case TOK_memset: | ||
| 952 | case TOK_strlen: | ||
| 953 | case TOK_strcpy: | ||
| 954 | strcpy(buf, "__bound_"); | ||
| 955 | strcat(buf, name); | ||
| 956 | name = buf; | ||
| 957 | break; | ||
| 958 | } | ||
| 959 | } | ||
| 960 | #endif | ||
| 961 | info = ELF32_ST_INFO(sym_bind, sym_type); | ||
| 962 | sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name); | ||
| 963 | } else { | ||
| 964 | esym = &((Elf32_Sym *)symtab_section->data)[sym->c]; | ||
| 965 | esym->st_value = value; | ||
| 966 | esym->st_size = size; | ||
| 967 | esym->st_shndx = sh_num; | ||
| 968 | } | ||
| 969 | } | ||
| 970 | |||
| 825 | /* add a new relocation entry to symbol 'sym' in section 's' */ | 971 | /* add a new relocation entry to symbol 'sym' in section 's' */ |
| 826 | static void greloc(Section *s, Sym *sym, unsigned long offset, int type) | 972 | static void greloc(Section *s, Sym *sym, unsigned long offset, int type) |
| 827 | { | 973 | { |
| @@ -937,15 +1083,13 @@ TokenSym *tok_alloc(const char *str, int len) · 109 unmodified lines | |||
| 937 | /* expand token table if needed */ | 1083 | /* expand token table if needed */ |
| 938 | i = tok_ident - TOK_IDENT; | 1084 | i = tok_ident - TOK_IDENT; |
| 939 | if ((i % TOK_ALLOC_INCR) == 0) { | 1085 | if ((i % TOK_ALLOC_INCR) == 0) { |
| 940 | ptable = realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *)); | 1086 | ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *)); |
| 941 | if (!ptable) | 1087 | if (!ptable) |
| 942 | error("memory full"); | 1088 | error("memory full"); |
| 943 | table_ident = ptable; | 1089 | table_ident = ptable; |
| 944 | } | 1090 | } |
| 945 | 1091 | ||
| 946 | ts = malloc(sizeof(TokenSym) + len); | 1092 | ts = tcc_malloc(sizeof(TokenSym) + len); |
| 947 | if (!ts) | ||
| 948 | error("memory full"); | ||
| 949 | table_ident[i] = ts; | 1093 | table_ident[i] = ts; |
| 950 | ts->tok = tok_ident++; | 1094 | ts->tok = tok_ident++; |
| 951 | ts->len = len; | 1095 | ts->len = len; |
| @@ -955,80 +1099,186 @@ TokenSym *tok_alloc(const char *str, int len) · 3 unmodified lines | |||
| 955 | return ts; | 1099 | return ts; |
| 956 | } | 1100 | } |
| 957 | 1101 | ||
| 958 | void add_char(char **pp, int c) | 1102 | /* CString handling */ |
| 1103 | |||
| 1104 | static void cstr_realloc(CString *cstr, int new_size) | ||
| 1105 | { | ||
| 1106 | int size; | ||
| 1107 | void *data; | ||
| 1108 | |||
| 1109 | size = cstr->size_allocated; | ||
| 1110 | if (size == 0) | ||
| 1111 | size = 8; /* no need to allocate a too small first string */ | ||
| 1112 | while (size < new_size) | ||
| 1113 | size = size * 2; | ||
| 1114 | data = tcc_realloc(cstr->data_allocated, size); | ||
| 1115 | if (!data) | ||
| 1116 | error("memory full"); | ||
| 1117 | cstr->data_allocated = data; | ||
| 1118 | cstr->size_allocated = size; | ||
| 1119 | cstr->data = data; | ||
| 1120 | } | ||
| 1121 | |||
| 1122 | /* add a byte */ | ||
| 1123 | static void cstr_ccat(CString *cstr, int ch) | ||
| 1124 | { | ||
| 1125 | int size; | ||
| 1126 | size = cstr->size + 1; | ||
| 1127 | if (size > cstr->size_allocated) | ||
| 1128 | cstr_realloc(cstr, size); | ||
| 1129 | ((unsigned char *)cstr->data)[size - 1] = ch; | ||
| 1130 | cstr->size = size; | ||
| 1131 | } | ||
| 1132 | |||
| 1133 | static void cstr_cat(CString *cstr, const char *str) | ||
| 1134 | { | ||
| 1135 | int c; | ||
| 1136 | for(;;) { | ||
| 1137 | c = *str; | ||
| 1138 | if (c == '\0') | ||
| 1139 | break; | ||
| 1140 | cstr_ccat(cstr, c); | ||
| 1141 | str++; | ||
| 1142 | } | ||
| 1143 | } | ||
| 1144 | |||
| 1145 | /* add a wide char */ | ||
| 1146 | static void cstr_wccat(CString *cstr, int ch) | ||
| 1147 | { | ||
| 1148 | int size; | ||
| 1149 | size = cstr->size + sizeof(int); | ||
| 1150 | if (size > cstr->size_allocated) | ||
| 1151 | cstr_realloc(cstr, size); | ||
| 1152 | *(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch; | ||
| 1153 | cstr->size = size; | ||
| 1154 | } | ||
| 1155 | |||
| 1156 | static void cstr_new(CString *cstr) | ||
| 1157 | { | ||
| 1158 | memset(cstr, 0, sizeof(CString)); | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | /* free string and reset it to NULL */ | ||
| 1162 | static void cstr_free(CString *cstr) | ||
| 1163 | { | ||
| 1164 | tcc_free(cstr->data_allocated); | ||
| 1165 | cstr_new(cstr); | ||
| 1166 | } | ||
| 1167 | |||
| 1168 | #define cstr_reset(cstr) cstr_free(cstr) | ||
| 1169 | |||
| 1170 | /* XXX: unicode ? */ | ||
| 1171 | static void add_char(CString *cstr, int c) | ||
| 959 | { | 1172 | { |
| 960 | char *p; | ||
| 961 | p = *pp; | ||
| 962 | if (c == '\'' || c == '\"' || c == '\\') { | 1173 | if (c == '\'' || c == '\"' || c == '\\') { |
| 963 | /* XXX: could be more precise if char or string */ | 1174 | /* XXX: could be more precise if char or string */ |
| 964 | *p++ = '\\'; | 1175 | cstr_ccat(cstr, '\\'); |
| 965 | } | 1176 | } |
| 966 | if (c >= 32 && c <= 126) { | 1177 | if (c >= 32 && c <= 126) { |
| 967 | *p++ = c; | 1178 | cstr_ccat(cstr, c); |
| 968 | } else { | 1179 | } else { |
| 969 | *p++ = '\\'; | 1180 | cstr_ccat(cstr, '\\'); |
| 970 | if (c == '\n') { | 1181 | if (c == '\n') { |
| 971 | *p++ = 'n'; | 1182 | cstr_ccat(cstr, 'n'); |
| 972 | } else { | 1183 | } else { |
| 973 | *p++ = '0' + ((c >> 6) & 7); | 1184 | cstr_ccat(cstr, '0' + ((c >> 6) & 7)); |
| 974 | *p++ = '0' + ((c >> 3) & 7); | 1185 | cstr_ccat(cstr, '0' + ((c >> 3) & 7)); |
| 975 | *p++ = '0' + (c & 7); | 1186 | cstr_ccat(cstr, '0' + (c & 7)); |
| 976 | } | 1187 | } |
| 977 | } | 1188 | } |
| 978 | *pp = p; | ||
| 979 | } | 1189 | } |
| 980 | 1190 | ||
| 981 | /* XXX: buffer overflow */ | 1191 | /* XXX: buffer overflow */ |
| 1192 | /* XXX: float tokens */ | ||
| 982 | char *get_tok_str(int v, CValue *cv) | 1193 | char *get_tok_str(int v, CValue *cv) |
| 983 | { | 1194 | { |
| 984 | static char buf[STRING_MAX_SIZE + 1]; | 1195 | static char buf[STRING_MAX_SIZE + 1]; |
| 985 | TokenSym *ts; | 1196 | static CString cstr_buf; |
| 1197 | CString *cstr; | ||
| 1198 | unsigned char *q; | ||
| 986 | char *p; | 1199 | char *p; |
| 987 | int i; | 1200 | int i, len; |
| 988 | 1201 | ||
| 989 | if (v == TOK_CINT || v == TOK_CUINT) { | 1202 | /* NOTE: to go faster, we give a fixed buffer for small strings */ |
| 990 | sprintf(buf, "%u", cv->ui); | 1203 | cstr_reset(&cstr_buf); |
| 991 | return buf; | 1204 | cstr_buf.data = buf; |
| 992 | } else if (v == TOK_CCHAR || v == TOK_LCHAR) { | 1205 | cstr_buf.size_allocated = sizeof(buf); |
| 993 | p = buf; | 1206 | p = buf; |
| 994 | *p++ = '\''; | 1207 | |
| 995 | add_char(&p, cv->i); | 1208 | switch(v) { |
| 996 | *p++ = '\''; | 1209 | case TOK_CINT: |
| 997 | *p = '\0'; | 1210 | case TOK_CUINT: |
| 998 | return buf; | 1211 | /* XXX: not exact */ |
| 999 | } else if (v == TOK_STR || v == TOK_LSTR) { | 1212 | sprintf(p, "%u", cv->ui); |
| 1000 | ts = cv->ts; | 1213 | break; |
| 1001 | p = buf; | 1214 | case TOK_CCHAR: |
| 1002 | *p++ = '\"'; | 1215 | case TOK_LCHAR: |
| 1003 | for(i=0;i<ts->len;i++) | 1216 | cstr_ccat(&cstr_buf, '\''); |
| 1004 | add_char(&p, ts->str[i]); | 1217 | add_char(&cstr_buf, cv->i); |
| 1005 | *p++ = '\"'; | 1218 | cstr_ccat(&cstr_buf, '\''); |
| 1006 | *p = '\0'; | 1219 | cstr_ccat(&cstr_buf, '\0'); |
| 1007 | return buf; | 1220 | break; |
| 1008 | } else if (v < TOK_IDENT) { | 1221 | case TOK_STR: |
| 1009 | p = buf; | 1222 | case TOK_LSTR: |
| 1010 | *p++ = v; | 1223 | cstr = cv->cstr; |
| 1011 | *p = '\0'; | 1224 | cstr_ccat(&cstr_buf, '\"'); |
| 1012 | return buf; | 1225 | if (v == TOK_STR) { |
| 1013 | } else if (v < tok_ident) { | 1226 | len = cstr->size - 1; |
| 1014 | return table_ident[v - TOK_IDENT]->str; | 1227 | for(i=0;i<len;i++) |
| 1015 | } else if (v >= SYM_FIRST_ANOM) { | 1228 | add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]); |
| 1016 | /* special name for anonymous symbol */ | 1229 | } else { |
| 1017 | sprintf(buf, "L.%u", v - SYM_FIRST_ANOM); | 1230 | len = (cstr->size / sizeof(int)) - 1; |
| 1018 | return buf; | 1231 | for(i=0;i<len;i++) |
| 1019 | } else { | 1232 | add_char(&cstr_buf, ((int *)cstr->data)[i]); |
| 1020 | /* should never happen */ | 1233 | } |
| 1021 | return NULL; | 1234 | cstr_ccat(&cstr_buf, '\"'); |
| 1235 | cstr_ccat(&cstr_buf, '\0'); | ||
| 1236 | break; | ||
| 1237 | case TOK_LT: | ||
| 1238 | v = '<'; | ||
| 1239 | goto addv; | ||
| 1240 | case TOK_GT: | ||
| 1241 | v = '>'; | ||
| 1242 | goto addv; | ||
| 1243 | case TOK_A_SHL: | ||
| 1244 | return strcpy(p, "<<="); | ||
| 1245 | case TOK_A_SAR: | ||
| 1246 | return strcpy(p, ">>="); | ||
| 1247 | default: | ||
| 1248 | if (v < TOK_IDENT) { | ||
| 1249 | /* search in two bytes table */ | ||
| 1250 | q = tok_two_chars; | ||
| 1251 | while (*q) { | ||
| 1252 | if (q[2] == v) { | ||
| 1253 | *p++ = q[0]; | ||
| 1254 | *p++ = q[1]; | ||
| 1255 | *p = '\0'; | ||
| 1256 | return buf; | ||
| 1257 | } | ||
| 1258 | q += 3; | ||
| 1259 | } | ||
| 1260 | addv: | ||
| 1261 | *p++ = v; | ||
| 1262 | *p = '\0'; | ||
| 1263 | } else if (v < tok_ident) { | ||
| 1264 | return table_ident[v - TOK_IDENT]->str; | ||
| 1265 | } else if (v >= SYM_FIRST_ANOM) { | ||
| 1266 | /* special name for anonymous symbol */ | ||
| 1267 | sprintf(p, "L.%u", v - SYM_FIRST_ANOM); | ||
| 1268 | } else { | ||
| 1269 | /* should never happen */ | ||
| 1270 | return NULL; | ||
| 1271 | } | ||
| 1272 | break; | ||
| 1022 | } | 1273 | } |
| 1274 | return cstr_buf.data; | ||
| 1023 | } | 1275 | } |
| 1024 | 1276 | ||
| 1025 | /* push, without hashing */ | 1277 | /* push, without hashing */ |
| 1026 | Sym *sym_push2(Sym **ps, int v, int t, int c) | 1278 | Sym *sym_push2(Sym **ps, int v, int t, int c) |
| 1027 | { | 1279 | { |
| 1028 | Sym *s; | 1280 | Sym *s; |
| 1029 | s = malloc(sizeof(Sym)); | 1281 | s = tcc_malloc(sizeof(Sym)); |
| 1030 | if (!s) | ||
| 1031 | error("memory full"); | ||
| 1032 | s->v = v; | 1282 | s->v = v; |
| 1033 | s->t = t; | 1283 | s->t = t; |
| 1034 | s->c = c; | 1284 | s->c = c; |
| @@ -1115,7 +1365,7 @@ void sym_pop(SymStack *st, Sym *b) · 80 unmodified lines | |||
| 1115 | used for #undef symbols) */ | 1365 | used for #undef symbols) */ |
| 1116 | if (s->v) | 1366 | if (s->v) |
| 1117 | st->hash[HASH_SYM(s->v)] = s->hash_next; | 1367 | st->hash[HASH_SYM(s->v)] = s->hash_next; |
| 1118 | free(s); | 1368 | tcc_free(s); |
| 1119 | s = ss; | 1369 | s = ss; |
| 1120 | } | 1370 | } |
| 1121 | st->top = b; | 1371 | st->top = b; |
| @@ -1146,7 +1396,7 @@ BufferedFile *tcc_open(const char *filename) · 24 unmodified lines | |||
| 1146 | fd = open(filename, O_RDONLY); | 1396 | fd = open(filename, O_RDONLY); |
| 1147 | if (fd < 0) | 1397 | if (fd < 0) |
| 1148 | return NULL; | 1398 | return NULL; |
| 1149 | bf = malloc(sizeof(BufferedFile)); | 1399 | bf = tcc_malloc(sizeof(BufferedFile)); |
| 1150 | if (!bf) { | 1400 | if (!bf) { |
| 1151 | close(fd); | 1401 | close(fd); |
| 1152 | return NULL; | 1402 | return NULL; |
| @@ -1165,7 +1415,7 @@ void tcc_close(BufferedFile *bf) · 12 unmodified lines | |||
| 1165 | { | 1415 | { |
| 1166 | total_lines += bf->line_num; | 1416 | total_lines += bf->line_num; |
| 1167 | close(bf->fd); | 1417 | close(bf->fd); |
| 1168 | free(bf); | 1418 | tcc_free(bf); |
| 1169 | } | 1419 | } |
| 1170 | 1420 | ||
| 1171 | /* read one char. MUST call tcc_fillbuf if CH_EOB is read */ | 1421 | /* read one char. MUST call tcc_fillbuf if CH_EOB is read */ |
| @@ -1371,6 +1621,29 @@ static inline void tok_str_new(TokenString *s) · 199 unmodified lines | |||
| 1371 | s->last_line_num = -1; | 1621 | s->last_line_num = -1; |
| 1372 | } | 1622 | } |
| 1373 | 1623 | ||
| 1624 | static void tok_str_free(int *str) | ||
| 1625 | { | ||
| 1626 | const int *p; | ||
| 1627 | CString *cstr; | ||
| 1628 | int t; | ||
| 1629 | |||
| 1630 | p = str; | ||
| 1631 | for(;;) { | ||
| 1632 | t = *p++; | ||
| 1633 | if (t == 0) | ||
| 1634 | break; | ||
| 1635 | if (t == TOK_STR || t == TOK_LSTR) { | ||
| 1636 | /* XXX: use a macro to be portable on 64 bit ? */ | ||
| 1637 | cstr = (CString *)(*p++); | ||
| 1638 | cstr_free(cstr); | ||
| 1639 | tcc_free(cstr); | ||
| 1640 | } else { | ||
| 1641 | p += tok_ext_size(t); | ||
| 1642 | } | ||
| 1643 | } | ||
| 1644 | tcc_free(str); | ||
| 1645 | } | ||
| 1646 | |||
| 1374 | static void tok_str_add(TokenString *s, int t) | 1647 | static void tok_str_add(TokenString *s, int t) |
| 1375 | { | 1648 | { |
| 1376 | int len, *str; | 1649 | int len, *str; |
| @@ -1378,7 +1651,7 @@ static void tok_str_add(TokenString *s, int t) · 1 unmodified line | |||
| 1378 | len = s->len; | 1651 | len = s->len; |
| 1379 | str = s->str; | 1652 | str = s->str; |
| 1380 | if ((len & 63) == 0) { | 1653 | if ((len & 63) == 0) { |
| 1381 | str = realloc(str, (len + 64) * sizeof(int)); | 1654 | str = tcc_realloc(str, (len + 64) * sizeof(int)); |
| 1382 | if (!str) | 1655 | if (!str) |
| 1383 | return; | 1656 | return; |
| 1384 | s->str = str; | 1657 | s->str = str; |
| @@ -1389,11 +1662,28 @@ static void tok_str_add(TokenString *s, int t) · 4 unmodified lines | |||
| 1389 | 1662 | ||
| 1390 | static void tok_str_add2(TokenString *s, int t, CValue *cv) | 1663 | static void tok_str_add2(TokenString *s, int t, CValue *cv) |
| 1391 | { | 1664 | { |
| 1392 | int n, i; | 1665 | int n, i, size; |
| 1666 | CString *cstr, *cstr1; | ||
| 1667 | CValue cv1; | ||
| 1668 | |||
| 1393 | tok_str_add(s, t); | 1669 | tok_str_add(s, t); |
| 1394 | n = tok_ext_size(t); | 1670 | if (t == TOK_STR || t == TOK_LSTR) { |
| 1395 | for(i=0;i<n;i++) | 1671 | /* special case: need to duplicate string */ |
| 1396 | tok_str_add(s, cv->tab[i]); | 1672 | cstr1 = cv->cstr; |
| 1673 | cstr = tcc_malloc(sizeof(CString)); | ||
| 1674 | size = cstr1->size; | ||
| 1675 | cstr->size = size; | ||
| 1676 | cstr->size_allocated = size; | ||
| 1677 | cstr->data_allocated = tcc_malloc(size); | ||
| 1678 | cstr->data = cstr->data_allocated; | ||
| 1679 | memcpy(cstr->data_allocated, cstr1->data_allocated, size); | ||
| 1680 | cv1.cstr = cstr; | ||
| 1681 | tok_str_add(s, cv1.tab[0]); | ||
| 1682 | } else { | ||
| 1683 | n = tok_ext_size(t); | ||
| 1684 | for(i=0;i<n;i++) | ||
| 1685 | tok_str_add(s, cv->tab[i]); | ||
| 1686 | } | ||
| 1397 | } | 1687 | } |
| 1398 | 1688 | ||
| 1399 | /* add the current parse token in token string 's' */ | 1689 | /* add the current parse token in token string 's' */ |
| @@ -1457,7 +1747,7 @@ int expr_preprocess(void) · 57 unmodified lines | |||
| 1457 | next(); | 1747 | next(); |
| 1458 | c = expr_const(); | 1748 | c = expr_const(); |
| 1459 | macro_ptr = NULL; | 1749 | macro_ptr = NULL; |
| 1460 | free(str.str); | 1750 | tok_str_free(str.str); |
| 1461 | return c != 0; | 1751 | return c != 0; |
| 1462 | } | 1752 | } |
| 1463 | 1753 | ||
| @@ -1534,7 +1824,7 @@ void parse_define(void) · 70 unmodified lines | |||
| 1534 | 1824 | ||
| 1535 | void preprocess(void) | 1825 | void preprocess(void) |
| 1536 | { | 1826 | { |
| 1537 | int size, i, c; | 1827 | int size, i, c, n; |
| 1538 | char buf[1024], *q, *p; | 1828 | char buf[1024], *q, *p; |
| 1539 | char buf1[1024]; | 1829 | char buf1[1024]; |
| 1540 | BufferedFile *f; | 1830 | BufferedFile *f; |
| @@ -1563,23 +1853,46 @@ void preprocess(void) · 22 unmodified lines | |||
| 1563 | read_name: | 1853 | read_name: |
| 1564 | minp(); | 1854 | minp(); |
| 1565 | q = buf; | 1855 | q = buf; |
| 1566 | while (ch != c && ch != '\n' && ch != -1) { | 1856 | while (ch != c && ch != '\n' && ch != CH_EOF) { |
| 1567 | if ((q - buf) < sizeof(buf) - 1) | 1857 | if ((q - buf) < sizeof(buf) - 1) |
| 1568 | *q++ = ch; | 1858 | *q++ = ch; |
| 1569 | minp(); | 1859 | minp(); |
| 1570 | } | 1860 | } |
| 1571 | *q = '\0'; | 1861 | *q = '\0'; |
| 1862 | /* eat all spaces and comments after include */ | ||
| 1863 | /* XXX: slightly incorrect */ | ||
| 1864 | while (ch1 != '\n' && ch1 != CH_EOF) | ||
| 1865 | inp(); | ||
| 1572 | } else { | 1866 | } else { |
| 1867 | /* computed #include : either we have only strings or | ||
| 1868 | we have anything enclosed in '<>' */ | ||
| 1573 | next(); | 1869 | next(); |
| 1574 | if (tok != TOK_STR) | 1870 | buf[0] = '\0'; |
| 1575 | error("#include syntax error"); | 1871 | if (tok == TOK_STR) { |
| 1576 | pstrcpy(buf, sizeof(buf), get_tok_str(tok, &tokc)); | 1872 | while (tok != TOK_LINEFEED) { |
| 1577 | c = '\"'; | 1873 | if (tok != TOK_STR) { |
| 1874 | include_syntax: | ||
| 1875 | error("'#include' expects \"FILENAME\" or <FILENAME>"); | ||
| 1876 | } | ||
| 1877 | pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data); | ||
| 1878 | next(); | ||
| 1879 | } | ||
| 1880 | c = '\"'; | ||
| 1881 | } else { | ||
| 1882 | int len; | ||
| 1883 | while (tok != TOK_LINEFEED) { | ||
| 1884 | pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc)); | ||
| 1885 | next(); | ||
| 1886 | } | ||
| 1887 | len = strlen(buf); | ||
| 1888 | /* check syntax and remove '<>' */ | ||
| 1889 | if (len < 2 || buf[0] != '<' || buf[len - 1] != '>') | ||
| 1890 | goto include_syntax; | ||
| 1891 | memmove(buf, buf + 1, len - 2); | ||
| 1892 | buf[len - 2] = '\0'; | ||
| 1893 | c = '>'; | ||
| 1894 | } | ||
| 1578 | } | 1895 | } |
| 1579 | /* eat all spaces and comments after include */ | ||
| 1580 | /* XXX: slightly incorrect */ | ||
| 1581 | while (ch1 != '\n' && ch1 != -1) | ||
| 1582 | inp(); | ||
| 1583 | 1896 | ||
| 1584 | if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE) | 1897 | if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE) |
| 1585 | error("memory full"); | 1898 | error("memory full"); |
| @@ -1598,11 +1911,17 @@ void preprocess(void) · 12 unmodified lines | |||
| 1598 | if (f) | 1911 | if (f) |
| 1599 | goto found; | 1912 | goto found; |
| 1600 | } | 1913 | } |
| 1601 | /* now search in standard include path */ | 1914 | /* now search in all the include paths */ |
| 1602 | for(i=nb_include_paths - 1;i>=0;i--) { | 1915 | n = nb_include_paths + nb_sysinclude_paths; |
| 1603 | strcpy(buf1, include_paths[i]); | 1916 | for(i = 0; i < n; i++) { |
| 1604 | strcat(buf1, "/"); | 1917 | const char *path; |
| 1605 | strcat(buf1, buf); | 1918 | if (i < nb_include_paths) |
| 1919 | path = include_paths[i]; | ||
| 1920 | else | ||
| 1921 | path = sysinclude_paths[i - nb_include_paths]; | ||
| 1922 | pstrcpy(buf1, sizeof(buf1), path); | ||
| 1923 | pstrcat(buf1, sizeof(buf1), "/"); | ||
| 1924 | pstrcat(buf1, sizeof(buf1), buf); | ||
| 1606 | f = tcc_open(buf1); | 1925 | f = tcc_open(buf1); |
| 1607 | if (f) | 1926 | if (f) |
| 1608 | goto found; | 1927 | goto found; |
| @@ -1665,18 +1984,20 @@ void preprocess(void) · 56 unmodified lines | |||
| 1665 | error("#endif without matching #if"); | 1984 | error("#endif without matching #if"); |
| 1666 | ifdef_stack_ptr--; | 1985 | ifdef_stack_ptr--; |
| 1667 | } else if (tok == TOK_LINE) { | 1986 | } else if (tok == TOK_LINE) { |
| 1987 | int line_num; | ||
| 1668 | next(); | 1988 | next(); |
| 1669 | if (tok != TOK_CINT) | 1989 | if (tok != TOK_CINT) |
| 1670 | error("#line"); | 1990 | error("#line"); |
| 1671 | file->line_num = tokc.i; | 1991 | line_num = tokc.i; |
| 1672 | skip_spaces(); | 1992 | next(); |
| 1673 | if (ch != '\n') { | 1993 | if (tok != TOK_LINEFEED) { |
| 1674 | next(); | ||
| 1675 | if (tok != TOK_STR) | 1994 | if (tok != TOK_STR) |
| 1676 | error("#line"); | 1995 | error("#line"); |
| 1677 | pstrcpy(file->filename, sizeof(file->filename), | 1996 | pstrcpy(file->filename, sizeof(file->filename), |
| 1678 | get_tok_str(tok, &tokc)); | 1997 | (char *)tokc.cstr->data); |
| 1679 | } | 1998 | } |
| 1999 | /* NOTE: we do it there to avoid problems with linefeed */ | ||
| 2000 | file->line_num = line_num; | ||
| 1680 | } else if (tok == TOK_ERROR) { | 2001 | } else if (tok == TOK_ERROR) { |
| 1681 | error("#error"); | 2002 | error("#error"); |
| 1682 | } | 2003 | } |
| @@ -2126,20 +2447,24 @@ void next_nomacro1(void) · 443 unmodified lines | |||
| 2126 | tok = TOK_STR; | 2447 | tok = TOK_STR; |
| 2127 | str_const: | 2448 | str_const: |
| 2128 | minp(); | 2449 | minp(); |
| 2129 | q = token_buf; | 2450 | cstr_reset(&tokcstr); |
| 2130 | while (ch != '\"') { | 2451 | while (ch != '\"') { |
| 2131 | b = getq(); | 2452 | b = getq(); |
| 2132 | if (ch == -1) | 2453 | if (ch == CH_EOF) |
| 2133 | error("unterminated string"); | 2454 | error("unterminated string"); |
| 2134 | if (q >= token_buf + STRING_MAX_SIZE) | 2455 | if (tok == TOK_STR) |
| 2135 | error("string too long"); | 2456 | cstr_ccat(&tokcstr, b); |
| 2136 | *q++ = b; | 2457 | else |
| 2458 | cstr_wccat(&tokcstr, b); | ||
| 2137 | } | 2459 | } |
| 2138 | *q = '\0'; | 2460 | if (tok == TOK_STR) |
| 2139 | tokc.ts = tok_alloc(token_buf, q - token_buf); | 2461 | cstr_ccat(&tokcstr, '\0'); |
| 2462 | else | ||
| 2463 | cstr_wccat(&tokcstr, '\0'); | ||
| 2464 | tokc.cstr = &tokcstr; | ||
| 2140 | minp(); | 2465 | minp(); |
| 2141 | } else { | 2466 | } else { |
| 2142 | q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266"; | 2467 | q = tok_two_chars; |
| 2143 | /* two chars */ | 2468 | /* two chars */ |
| 2144 | tok = ch; | 2469 | tok = ch; |
| 2145 | cinp(); | 2470 | cinp(); |
| @@ -2194,10 +2519,10 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 48 unmodified lines | |||
| 2194 | { | 2519 | { |
| 2195 | int *st, last_tok, t, notfirst; | 2520 | int *st, last_tok, t, notfirst; |
| 2196 | Sym *s; | 2521 | Sym *s; |
| 2197 | TokenSym *ts; | ||
| 2198 | CValue cval; | 2522 | CValue cval; |
| 2199 | TokenString str; | 2523 | TokenString str; |
| 2200 | 2524 | CString cstr; | |
| 2525 | |||
| 2201 | tok_str_new(&str); | 2526 | tok_str_new(&str); |
| 2202 | last_tok = 0; | 2527 | last_tok = 0; |
| 2203 | while(1) { | 2528 | while(1) { |
| @@ -2211,23 +2536,24 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 7 unmodified lines | |||
| 2211 | break; | 2536 | break; |
| 2212 | s = sym_find2(args, t); | 2537 | s = sym_find2(args, t); |
| 2213 | if (s) { | 2538 | if (s) { |
| 2214 | token_buf[0] = '\0'; | 2539 | cstr_new(&cstr); |
| 2215 | st = (int *)s->c; | 2540 | st = (int *)s->c; |
| 2216 | notfirst = 0; | 2541 | notfirst = 0; |
| 2217 | while (*st) { | 2542 | while (*st) { |
| 2218 | if (notfirst) | 2543 | if (notfirst) |
| 2219 | pstrcat(token_buf, sizeof(token_buf), " "); | 2544 | cstr_ccat(&cstr, ' '); |
| 2220 | t = tok_get(&st, &cval); | 2545 | t = tok_get(&st, &cval); |
| 2221 | pstrcat(token_buf, sizeof(token_buf), get_tok_str(t, &cval)); | 2546 | cstr_cat(&cstr, get_tok_str(t, &cval)); |
| 2222 | notfirst = 1; | 2547 | notfirst = 1; |
| 2223 | } | 2548 | } |
| 2549 | cstr_ccat(&cstr, '\0'); | ||
| 2224 | #ifdef PP_DEBUG | 2550 | #ifdef PP_DEBUG |
| 2225 | printf("stringize: %s\n", token_buf); | 2551 | printf("stringize: %s\n", (char *)cstr.data); |
| 2226 | #endif | 2552 | #endif |
| 2227 | /* add string */ | 2553 | /* add string */ |
| 2228 | ts = tok_alloc(token_buf, 0); | 2554 | cval.cstr = &cstr; |
| 2229 | cval.ts = ts; | ||
| 2230 | tok_str_add2(&str, TOK_STR, &cval); | 2555 | tok_str_add2(&str, TOK_STR, &cval); |
| 2556 | cstr_free(&cstr); | ||
| 2231 | } else { | 2557 | } else { |
| 2232 | tok_str_add2(&str, t, &cval); | 2558 | tok_str_add2(&str, t, &cval); |
| 2233 | } | 2559 | } |
| @@ -2244,12 +2570,17 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 10 unmodified lines | |||
| 2244 | problems */ | 2570 | problems */ |
| 2245 | if (gnu_ext && s->t && *st == 0 && | 2571 | if (gnu_ext && s->t && *st == 0 && |
| 2246 | last_tok == TOK_TWOSHARPS && | 2572 | last_tok == TOK_TWOSHARPS && |
| 2247 | str.len >= 2&& str.str[str.len - 2] == ',') { | 2573 | str.len >= 2 && str.str[str.len - 2] == ',') { |
| 2248 | /* suppress ',' '##' */ | 2574 | /* suppress ',' '##' */ |
| 2249 | str.len -= 2; | 2575 | str.len -= 2; |
| 2250 | } else { | 2576 | } else { |
| 2251 | while (*st) | 2577 | int t1; |
| 2252 | tok_str_add(&str, *st++); | 2578 | for(;;) { |
| 2579 | t1 = tok_get(&st, &cval); | ||
| 2580 | if (!t1) | ||
| 2581 | break; | ||
| 2582 | tok_str_add2(&str, t1, &cval); | ||
| 2583 | } | ||
| 2253 | } | 2584 | } |
| 2254 | } else { | 2585 | } else { |
| 2255 | macro_subst(&str, nested_list, st); | 2586 | macro_subst(&str, nested_list, st); |
| @@ -2321,9 +2652,11 @@ void macro_subst(TokenString *tok_str, · 65 unmodified lines | |||
| 2321 | Sym *s, *args, *sa, *sa1; | 2652 | Sym *s, *args, *sa, *sa1; |
| 2322 | int parlevel, *mstr, t, *saved_macro_ptr; | 2653 | int parlevel, *mstr, t, *saved_macro_ptr; |
| 2323 | int mstr_allocated, *macro_str1; | 2654 | int mstr_allocated, *macro_str1; |
| 2655 | CString cstr; | ||
| 2324 | CValue cval; | 2656 | CValue cval; |
| 2325 | TokenString str; | 2657 | TokenString str; |
| 2326 | 2658 | char *cstrval; | |
| 2659 | |||
| 2327 | saved_macro_ptr = macro_ptr; | 2660 | saved_macro_ptr = macro_ptr; |
| 2328 | macro_ptr = macro_str; | 2661 | macro_ptr = macro_str; |
| 2329 | macro_str1 = NULL; | 2662 | macro_str1 = NULL; |
| @@ -2342,14 +2675,21 @@ void macro_subst(TokenString *tok_str, · 12 unmodified lines | |||
| 2342 | cval.i = file->line_num; | 2675 | cval.i = file->line_num; |
| 2343 | tok_str_add2(tok_str, TOK_CINT, &cval); | 2676 | tok_str_add2(tok_str, TOK_CINT, &cval); |
| 2344 | } else if (tok == TOK___FILE__) { | 2677 | } else if (tok == TOK___FILE__) { |
| 2345 | cval.ts = tok_alloc(file->filename, 0); | 2678 | cstrval = file->filename; |
| 2679 | goto add_cstr; | ||
| 2346 | tok_str_add2(tok_str, TOK_STR, &cval); | 2680 | tok_str_add2(tok_str, TOK_STR, &cval); |
| 2347 | } else if (tok == TOK___DATE__) { | 2681 | } else if (tok == TOK___DATE__) { |
| 2348 | cval.ts = tok_alloc("Jan 1 1970", 0); | 2682 | cstrval = "Jan 1 1970"; |
| 2349 | tok_str_add2(tok_str, TOK_STR, &cval); | 2683 | goto add_cstr; |
| 2350 | } else if (tok == TOK___TIME__) { | 2684 | } else if (tok == TOK___TIME__) { |
| 2351 | cval.ts = tok_alloc("00:00:00", 0); | 2685 | cstrval = "00:00:00"; |
| 2686 | add_cstr: | ||
| 2687 | cstr_new(&cstr); | ||
| 2688 | cstr_cat(&cstr, cstrval); | ||
| 2689 | cstr_ccat(&cstr, '\0'); | ||
| 2690 | cval.cstr = &cstr; | ||
| 2352 | tok_str_add2(tok_str, TOK_STR, &cval); | 2691 | tok_str_add2(tok_str, TOK_STR, &cval); |
| 2692 | cstr_free(&cstr); | ||
| 2353 | } else if ((s = sym_find1(&define_stack, tok)) != NULL) { | 2693 | } else if ((s = sym_find1(&define_stack, tok)) != NULL) { |
| 2354 | /* if symbol is a macro, prepare substitution */ | 2694 | /* if symbol is a macro, prepare substitution */ |
| 2355 | /* if nested substitution, do nothing */ | 2695 | /* if nested substitution, do nothing */ |
| @@ -2423,8 +2763,8 @@ void macro_subst(TokenString *tok_str, · 67 unmodified lines | |||
| 2423 | sa = args; | 2763 | sa = args; |
| 2424 | while (sa) { | 2764 | while (sa) { |
| 2425 | sa1 = sa->prev; | 2765 | sa1 = sa->prev; |
| 2426 | free((int *)sa->c); | 2766 | tok_str_free((int *)sa->c); |
| 2427 | free(sa); | 2767 | tcc_free(sa); |
| 2428 | sa = sa1; | 2768 | sa = sa1; |
| 2429 | } | 2769 | } |
| 2430 | mstr_allocated = 1; | 2770 | mstr_allocated = 1; |
| @@ -2434,9 +2774,9 @@ void macro_subst(TokenString *tok_str, · 3 unmodified lines | |||
| 2434 | /* pop nested defined symbol */ | 2774 | /* pop nested defined symbol */ |
| 2435 | sa1 = *nested_list; | 2775 | sa1 = *nested_list; |
| 2436 | *nested_list = sa1->prev; | 2776 | *nested_list = sa1->prev; |
| 2437 | free(sa1); | 2777 | tcc_free(sa1); |
| 2438 | if (mstr_allocated) | 2778 | if (mstr_allocated) |
| 2439 | free(mstr); | 2779 | tok_str_free(mstr); |
| 2440 | } else { | 2780 | } else { |
| 2441 | no_subst: | 2781 | no_subst: |
| 2442 | /* no need to add if reading input stream */ | 2782 | /* no need to add if reading input stream */ |
| @@ -2450,7 +2790,7 @@ void macro_subst(TokenString *tok_str, · 7 unmodified lines | |||
| 2450 | } | 2790 | } |
| 2451 | macro_ptr = saved_macro_ptr; | 2791 | macro_ptr = saved_macro_ptr; |
| 2452 | if (macro_str1) | 2792 | if (macro_str1) |
| 2453 | free(macro_str1); | 2793 | tok_str_free(macro_str1); |
| 2454 | } | 2794 | } |
| 2455 | 2795 | ||
| 2456 | /* return next token with macro substitution */ | 2796 | /* return next token with macro substitution */ |
| @@ -2485,7 +2825,7 @@ void next(void) · 28 unmodified lines | |||
| 2485 | next_nomacro(); | 2825 | next_nomacro(); |
| 2486 | if (tok == 0) { | 2826 | if (tok == 0) { |
| 2487 | /* end of macro string: free it */ | 2827 | /* end of macro string: free it */ |
| 2488 | free(macro_ptr_allocated); | 2828 | tok_str_free(macro_ptr_allocated); |
| 2489 | macro_ptr = NULL; | 2829 | macro_ptr = NULL; |
| 2490 | goto redo; | 2830 | goto redo; |
| 2491 | } | 2831 | } |
| @@ -2594,14 +2934,13 @@ void vdup(void) · 102 unmodified lines | |||
| 2594 | /* save r to the memory stack, and mark it as being free */ | 2934 | /* save r to the memory stack, and mark it as being free */ |
| 2595 | void save_reg(int r) | 2935 | void save_reg(int r) |
| 2596 | { | 2936 | { |
| 2597 | int l, i, saved, t, size, align; | 2937 | int l, saved, t, size, align; |
| 2598 | SValue *p, sv; | 2938 | SValue *p, sv; |
| 2599 | 2939 | ||
| 2600 | /* modify all stack values */ | 2940 | /* modify all stack values */ |
| 2601 | saved = 0; | 2941 | saved = 0; |
| 2602 | l = 0; | 2942 | l = 0; |
| 2603 | for(p=vstack;p<=vtop;p++) { | 2943 | for(p=vstack;p<=vtop;p++) { |
| 2604 | i = p->r & VT_VALMASK; | ||
| 2605 | if ((p->r & VT_VALMASK) == r || | 2944 | if ((p->r & VT_VALMASK) == r || |
| 2606 | (p->r2 & VT_VALMASK) == r) { | 2945 | (p->r2 & VT_VALMASK) == r) { |
| 2607 | /* must save value on stack if not already done */ | 2946 | /* must save value on stack if not already done */ |
| @@ -2632,11 +2971,14 @@ void save_reg(int r) · 24 unmodified lines | |||
| 2632 | saved = 1; | 2971 | saved = 1; |
| 2633 | } | 2972 | } |
| 2634 | /* mark that stack entry as being saved on the stack */ | 2973 | /* mark that stack entry as being saved on the stack */ |
| 2635 | if (p->r & VT_LVAL) | 2974 | if (p->r & VT_LVAL) { |
| 2636 | t = VT_LLOCAL; | 2975 | /* also suppress the bounded flag because the |
| 2637 | else | 2976 | relocation address of the function was stored in |
| 2638 | t = VT_LOCAL; | 2977 | p->c.ul */ |
| 2639 | p->r = VT_LVAL | t; | 2978 | p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL; |
| 2979 | } else { | ||
| 2980 | p->r = lvalue_type(p->t) | VT_LOCAL; | ||
| 2981 | } | ||
| 2640 | p->r2 = VT_CONST; | 2982 | p->r2 = VT_CONST; |
| 2641 | p->c.ul = l; | 2983 | p->c.ul = l; |
| 2642 | } | 2984 | } |
| @@ -2710,14 +3052,14 @@ void gaddrof(void) · 67 unmodified lines | |||
| 2710 | vtop->r &= ~VT_LVAL; | 3052 | vtop->r &= ~VT_LVAL; |
| 2711 | /* tricky: if saved lvalue, then we can go back to lvalue */ | 3053 | /* tricky: if saved lvalue, then we can go back to lvalue */ |
| 2712 | if ((vtop->r & VT_VALMASK) == VT_LLOCAL) | 3054 | if ((vtop->r & VT_VALMASK) == VT_LLOCAL) |
| 2713 | vtop->r = (vtop->r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL; | 3055 | vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL; |
| 2714 | } | 3056 | } |
| 2715 | 3057 | ||
| 2716 | #ifdef CONFIG_TCC_BCHECK | 3058 | #ifdef CONFIG_TCC_BCHECK |
| 2717 | /* generate lvalue bound code */ | 3059 | /* generate lvalue bound code */ |
| 2718 | void gbound(void) | 3060 | void gbound(void) |
| 2719 | { | 3061 | { |
| 2720 | int lval_type; | 3062 | int lval_type, t1; |
| 2721 | 3063 | ||
| 2722 | vtop->r &= ~VT_MUSTBOUND; | 3064 | vtop->r &= ~VT_MUSTBOUND; |
| 2723 | /* if lvalue, then use checking code before dereferencing */ | 3065 | /* if lvalue, then use checking code before dereferencing */ |
| @@ -2725,10 +3067,14 @@ void gbound(void) · 1 unmodified line | |||
| 2725 | /* if not VT_BOUNDED value, then make one */ | 3067 | /* if not VT_BOUNDED value, then make one */ |
| 2726 | if (!(vtop->r & VT_BOUNDED)) { | 3068 | if (!(vtop->r & VT_BOUNDED)) { |
| 2727 | lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL); | 3069 | lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL); |
| 3070 | /* must save type because we must set it to int to get pointer */ | ||
| 3071 | t1 = vtop->t; | ||
| 3072 | vtop->t = VT_INT; | ||
| 2728 | gaddrof(); | 3073 | gaddrof(); |
| 2729 | vpushi(0); | 3074 | vpushi(0); |
| 2730 | gen_bounded_ptr_add(); | 3075 | gen_bounded_ptr_add(); |
| 2731 | vtop->r |= lval_type; | 3076 | vtop->r |= lval_type; |
| 3077 | vtop->t = t1; | ||
| 2732 | } | 3078 | } |
| 2733 | /* then check for dereferencing */ | 3079 | /* then check for dereferencing */ |
| 2734 | gen_bounded_ptr_deref(); | 3080 | gen_bounded_ptr_deref(); |
| @@ -4127,7 +4473,7 @@ void parse_attribute(AttributeDef *ad) · 1392 unmodified lines | |||
| 4127 | skip('('); | 4473 | skip('('); |
| 4128 | if (tok != TOK_STR) | 4474 | if (tok != TOK_STR) |
| 4129 | expect("section name"); | 4475 | expect("section name"); |
| 4130 | ad->section = find_section(tokc.ts->str); | 4476 | ad->section = find_section((char *)tokc.cstr->data); |
| 4131 | next(); | 4477 | next(); |
| 4132 | skip(')'); | 4478 | skip(')'); |
| 4133 | break; | 4479 | break; |
| @@ -4717,6 +5063,8 @@ void unary(void) · 583 unmodified lines | |||
| 4717 | fc = data_offset; | 5063 | fc = data_offset; |
| 4718 | /* we must declare it as an array first to use initializer parser */ | 5064 | /* we must declare it as an array first to use initializer parser */ |
| 4719 | t = VT_ARRAY | mk_pointer(t); | 5065 | t = VT_ARRAY | mk_pointer(t); |
| 5066 | /* XXX: fix it */ | ||
| 5067 | section_ptr(data_section, 1024); | ||
| 4720 | decl_initializer(t, data_section, data_offset, 1, 0); | 5068 | decl_initializer(t, data_section, data_offset, 1, 0); |
| 4721 | size = type_size(t, &align); | 5069 | size = type_size(t, &align); |
| 4722 | data_offset += size; | 5070 | data_offset += size; |
| @@ -4943,8 +5291,8 @@ void unary(void) · 220 unmodified lines | |||
| 4943 | expect("',' or ')'"); | 5291 | expect("',' or ')'"); |
| 4944 | gfunc_param_typed(&gf, s, args->next); | 5292 | gfunc_param_typed(&gf, s, args->next); |
| 4945 | s1 = args->prev; | 5293 | s1 = args->prev; |
| 4946 | free((int *)args->c); | 5294 | tok_str_free((int *)args->c); |
| 4947 | free(args); | 5295 | tcc_free(args); |
| 4948 | args = s1; | 5296 | args = s1; |
| 4949 | } | 5297 | } |
| 4950 | restore_parse_state(&saved_parse_state); | 5298 | restore_parse_state(&saved_parse_state); |
| @@ -5622,7 +5970,6 @@ void decl_initializer(int t, Section *sec, unsigned long c, int first, int size_ · 671 unmodified lines | |||
| 5622 | int index, array_length, n, no_oblock, nb, parlevel, i; | 5970 | int index, array_length, n, no_oblock, nb, parlevel, i; |
| 5623 | int t1, size1, align1, expr_type; | 5971 | int t1, size1, align1, expr_type; |
| 5624 | Sym *s, *f; | 5972 | Sym *s, *f; |
| 5625 | TokenSym *ts; | ||
| 5626 | 5973 | ||
| 5627 | if (t & VT_ARRAY) { | 5974 | if (t & VT_ARRAY) { |
| 5628 | s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT)); | 5975 | s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT)); |
| @@ -5644,19 +5991,30 @@ void decl_initializer(int t, Section *sec, unsigned long c, int first, int size_ · 15 unmodified lines | |||
| 5644 | (t1 & VT_BTYPE) == VT_INT) || | 5991 | (t1 & VT_BTYPE) == VT_INT) || |
| 5645 | (tok == TOK_STR && | 5992 | (tok == TOK_STR && |
| 5646 | (t1 & VT_BTYPE) == VT_BYTE)) { | 5993 | (t1 & VT_BTYPE) == VT_BYTE)) { |
| 5647 | /* XXX: move multiple string parsing in parser ? */ | ||
| 5648 | while (tok == TOK_STR || tok == TOK_LSTR) { | 5994 | while (tok == TOK_STR || tok == TOK_LSTR) { |
| 5649 | ts = tokc.ts; | 5995 | int cstr_len, ch; |
| 5996 | CString *cstr; | ||
| 5997 | |||
| 5998 | cstr = tokc.cstr; | ||
| 5650 | /* compute maximum number of chars wanted */ | 5999 | /* compute maximum number of chars wanted */ |
| 5651 | nb = ts->len; | 6000 | if (tok == TOK_STR) |
| 6001 | cstr_len = cstr->size; | ||
| 6002 | else | ||
| 6003 | cstr_len = cstr->size / sizeof(int); | ||
| 6004 | cstr_len--; | ||
| 6005 | nb = cstr_len; | ||
| 5652 | if (n >= 0 && nb > (n - array_length)) | 6006 | if (n >= 0 && nb > (n - array_length)) |
| 5653 | nb = n - array_length; | 6007 | nb = n - array_length; |
| 5654 | if (!size_only) { | 6008 | if (!size_only) { |
| 5655 | if (ts->len > nb) | 6009 | if (cstr_len > nb) |
| 5656 | warning("initializer-string for array is too long"); | 6010 | warning("initializer-string for array is too long"); |
| 5657 | for(i=0;i<nb;i++) { | 6011 | for(i=0;i<nb;i++) { |
| 5658 | init_putv(t1, sec, c + (array_length + i) * size1, | 6012 | if (tok == TOK_STR) |
| 5659 | ts->str[i], EXPR_VAL); | 6013 | ch = ((unsigned char *)cstr->data)[i]; |
| 6014 | else | ||
| 6015 | ch = ((int *)cstr->data)[i]; | ||
| 6016 | init_putv(t1, sec, c + (array_length + i) * size1, | ||
| 6017 | ch, EXPR_VAL); | ||
| 5660 | } | 6018 | } |
| 5661 | } | 6019 | } |
| 5662 | array_length += nb; | 6020 | array_length += nb; |
| @@ -5866,6 +6224,10 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 203 unmodified lines | |||
| 5866 | if (do_bounds_check) | 6224 | if (do_bounds_check) |
| 5867 | data_offset++; | 6225 | data_offset++; |
| 5868 | sec->data_offset = data_offset; | 6226 | sec->data_offset = data_offset; |
| 6227 | /* allocate section space to put the data */ | ||
| 6228 | if (sec->sh_type != SHT_NOBITS && | ||
| 6229 | data_offset > sec->data_allocated) | ||
| 6230 | section_realloc(sec, data_offset); | ||
| 5869 | } | 6231 | } |
| 5870 | if (!sec) { | 6232 | if (!sec) { |
| 5871 | if (v) { | 6233 | if (v) { |
| @@ -5920,7 +6282,7 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 48 unmodified lines | |||
| 5920 | decl_initializer(t, sec, addr, 1, 0); | 6282 | decl_initializer(t, sec, addr, 1, 0); |
| 5921 | /* restore parse state if needed */ | 6283 | /* restore parse state if needed */ |
| 5922 | if (init_str.str) { | 6284 | if (init_str.str) { |
| 5923 | free(init_str.str); | 6285 | tok_str_free(init_str.str); |
| 5924 | restore_parse_state(&saved_parse_state); | 6286 | restore_parse_state(&saved_parse_state); |
| 5925 | } | 6287 | } |
| 5926 | } | 6288 | } |
| @@ -6127,7 +6489,7 @@ void decl(int l) · 200 unmodified lines | |||
| 6127 | ind = 0; /* for safety */ | 6489 | ind = 0; /* for safety */ |
| 6128 | 6490 | ||
| 6129 | #ifdef CONFIG_REG_VARS | 6491 | #ifdef CONFIG_REG_VARS |
| 6130 | free(func_str.str); | 6492 | tok_str_free(func_str.str); |
| 6131 | restore_parse_state(&saved_parse_state); | 6493 | restore_parse_state(&saved_parse_state); |
| 6132 | #endif | 6494 | #endif |
| 6133 | break; | 6495 | break; |
| @@ -6180,6 +6542,7 @@ static int tcc_compile(TCCState *s) · 46 unmodified lines | |||
| 6180 | include_stack_ptr = include_stack; | 6542 | include_stack_ptr = include_stack; |
| 6181 | ifdef_stack_ptr = ifdef_stack; | 6543 | ifdef_stack_ptr = ifdef_stack; |
| 6182 | 6544 | ||
| 6545 | /* XXX: not ANSI compliant: bound checking says error */ | ||
| 6183 | vtop = vstack - 1; | 6546 | vtop = vstack - 1; |
| 6184 | anon_sym = SYM_FIRST_ANOM; | 6547 | anon_sym = SYM_FIRST_ANOM; |
| 6185 | 6548 | ||
| @@ -6293,1905 +6656,23 @@ void tcc_undefine_symbol(TCCState *s1, const char *sym) · 107 unmodified lines | |||
| 6293 | sym_undef(&define_stack, s); | 6656 | sym_undef(&define_stack, s); |
| 6294 | } | 6657 | } |
| 6295 | 6658 | ||
| 6296 | static int put_elf_str(Section *s, const char *sym) | 6659 | #include "tccelf.c" |
| 6297 | { | ||
| 6298 | int offset, len; | ||
| 6299 | char *ptr; | ||
| 6300 | |||
| 6301 | len = strlen(sym) + 1; | ||
| 6302 | offset = s->data_offset; | ||
| 6303 | ptr = section_ptr_add(s, len); | ||
| 6304 | memcpy(ptr, sym, len); | ||
| 6305 | return offset; | ||
| 6306 | } | ||
| 6307 | |||
| 6308 | /* elf symbol hashing function */ | ||
| 6309 | static unsigned long elf_hash(const unsigned char *name) | ||
| 6310 | { | ||
| 6311 | unsigned long h = 0, g; | ||
| 6312 | |||
| 6313 | while (*name) { | ||
| 6314 | h = (h << 4) + *name++; | ||
| 6315 | g = h & 0xf0000000; | ||
| 6316 | if (g) | ||
| 6317 | h ^= g >> 24; | ||
| 6318 | h &= ~g; | ||
| 6319 | } | ||
| 6320 | return h; | ||
| 6321 | } | ||
| 6322 | |||
| 6323 | /* rebuild hash table of section s */ | ||
| 6324 | /* NOTE: we do factorize the hash table code to go faster */ | ||
| 6325 | static void rebuild_hash(Section *s, unsigned int nb_buckets) | ||
| 6326 | { | ||
| 6327 | Elf32_Sym *sym; | ||
| 6328 | int *ptr, *hash, nb_syms, sym_index, h; | ||
| 6329 | char *strtab; | ||
| 6330 | |||
| 6331 | strtab = s->link->data; | ||
| 6332 | nb_syms = s->data_offset / sizeof(Elf32_Sym); | ||
| 6333 | |||
| 6334 | s->hash->data_offset = 0; | ||
| 6335 | ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int)); | ||
| 6336 | ptr[0] = nb_buckets; | ||
| 6337 | ptr[1] = nb_syms; | ||
| 6338 | ptr += 2; | ||
| 6339 | hash = ptr; | ||
| 6340 | memset(hash, 0, (nb_buckets + 1) * sizeof(int)); | ||
| 6341 | ptr += nb_buckets + 1; | ||
| 6342 | |||
| 6343 | sym = (Elf32_Sym *)s->data + 1; | ||
| 6344 | for(sym_index = 1; sym_index < nb_syms; sym_index++) { | ||
| 6345 | if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) { | ||
| 6346 | h = elf_hash(strtab + sym->st_name) % nb_buckets; | ||
| 6347 | *ptr = hash[h]; | ||
| 6348 | hash[h] = sym_index; | ||
| 6349 | } else { | ||
| 6350 | *ptr = 0; | ||
| 6351 | } | ||
| 6352 | ptr++; | ||
| 6353 | sym++; | ||
| 6354 | } | ||
| 6355 | } | ||
| 6356 | |||
| 6357 | /* return the symbol number */ | ||
| 6358 | static int put_elf_sym(Section *s, | ||
| 6359 | unsigned long value, unsigned long size, | ||
| 6360 | int info, int other, int shndx, const char *name) | ||
| 6361 | { | ||
| 6362 | int name_offset, sym_index; | ||
| 6363 | int nbuckets, h; | ||
| 6364 | Elf32_Sym *sym; | ||
| 6365 | Section *hs; | ||
| 6366 | |||
| 6367 | sym = section_ptr_add(s, sizeof(Elf32_Sym)); | ||
| 6368 | if (name) | ||
| 6369 | name_offset = put_elf_str(s->link, name); | ||
| 6370 | else | ||
| 6371 | name_offset = 0; | ||
| 6372 | /* XXX: endianness */ | ||
| 6373 | sym->st_name = name_offset; | ||
| 6374 | sym->st_value = value; | ||
| 6375 | sym->st_size = size; | ||
| 6376 | sym->st_info = info; | ||
| 6377 | sym->st_other = other; | ||
| 6378 | sym->st_shndx = shndx; | ||
| 6379 | sym_index = sym - (Elf32_Sym *)s->data; | ||
| 6380 | hs = s->hash; | ||
| 6381 | if (hs) { | ||
| 6382 | int *ptr, *base; | ||
| 6383 | ptr = section_ptr_add(hs, sizeof(int)); | ||
| 6384 | base = (int *)hs->data; | ||
| 6385 | /* only add global or weak symbols */ | ||
| 6386 | if (ELF32_ST_BIND(info) != STB_LOCAL) { | ||
| 6387 | /* add another hashing entry */ | ||
| 6388 | nbuckets = base[0]; | ||
| 6389 | h = elf_hash(name) % nbuckets; | ||
| 6390 | *ptr = base[2 + h]; | ||
| 6391 | base[2 + h] = sym_index; | ||
| 6392 | base[1]++; | ||
| 6393 | /* we resize the hash table */ | ||
| 6394 | hs->nb_hashed_syms++; | ||
| 6395 | if (hs->nb_hashed_syms > 2 * nbuckets) { | ||
| 6396 | rebuild_hash(s, 2 * nbuckets); | ||
| 6397 | } | ||
| 6398 | } else { | ||
| 6399 | *ptr = 0; | ||
| 6400 | base[1]++; | ||
| 6401 | } | ||
| 6402 | } | ||
| 6403 | return sym_index; | ||
| 6404 | } | ||
| 6405 | |||
| 6406 | /* find global ELF symbol 'name' and return its index. Return 0 if not | ||
| 6407 | found. */ | ||
| 6408 | static int find_elf_sym(Section *s, const char *name) | ||
| 6409 | { | ||
| 6410 | Elf32_Sym *sym; | ||
| 6411 | Section *hs; | ||
| 6412 | int nbuckets, sym_index, h; | ||
| 6413 | const char *name1; | ||
| 6414 | |||
| 6415 | hs = s->hash; | ||
| 6416 | if (!hs) | ||
| 6417 | return 0; | ||
| 6418 | nbuckets = ((int *)hs->data)[0]; | ||
| 6419 | h = elf_hash(name) % nbuckets; | ||
| 6420 | sym_index = ((int *)hs->data)[2 + h]; | ||
| 6421 | while (sym_index != 0) { | ||
| 6422 | sym = &((Elf32_Sym *)s->data)[sym_index]; | ||
| 6423 | name1 = s->link->data + sym->st_name; | ||
| 6424 | if (!strcmp(name, name1)) | ||
| 6425 | return sym_index; | ||
| 6426 | sym_index = ((int *)hs->data)[2 + nbuckets + sym_index]; | ||
| 6427 | } | ||
| 6428 | return 0; | ||
| 6429 | } | ||
| 6430 | 6660 | ||
| 6431 | /* return elf symbol value or error */ | 6661 | /* print the position in the source file of PC value 'pc' by reading |
| 6432 | static unsigned long get_elf_sym_val(const char *name) | 6662 | the stabs debug information */ |
| 6663 | static void rt_printline(unsigned long wanted_pc) | ||
| 6433 | { | 6664 | { |
| 6434 | int sym_index; | 6665 | Stab_Sym *sym, *sym_end; |
| 6435 | Elf32_Sym *sym; | 6666 | char func_name[128], last_func_name[128]; |
| 6436 | 6667 | unsigned long func_addr, last_pc, pc; | |
| 6437 | sym_index = find_elf_sym(symtab_section, name); | 6668 | const char *incl_files[INCLUDE_STACK_SIZE]; |
| 6438 | if (!sym_index) | 6669 | int incl_index, len, last_line_num, i; |
| 6439 | error("%s not defined", name); | 6670 | const char *str, *p; |
| 6440 | sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; | ||
| 6441 | return sym->st_value; | ||
| 6442 | } | ||
| 6443 | |||
| 6444 | /* add an elf symbol : check if it is already defined and patch | ||
| 6445 | it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */ | ||
| 6446 | static int add_elf_sym(Section *s, unsigned long value, unsigned long size, | ||
| 6447 | int info, int sh_num, const char *name) | ||
| 6448 | { | ||
| 6449 | Elf32_Sym *esym; | ||
| 6450 | int sym_bind, sym_index, sym_type, esym_bind; | ||
| 6451 | |||
| 6452 | sym_bind = ELF32_ST_BIND(info); | ||
| 6453 | sym_type = ELF32_ST_TYPE(info); | ||
| 6454 | |||
| 6455 | if (sym_bind != STB_LOCAL) { | ||
| 6456 | /* we search global or weak symbols */ | ||
| 6457 | sym_index = find_elf_sym(s, name); | ||
| 6458 | if (!sym_index) | ||
| 6459 | goto do_def; | ||
| 6460 | esym = &((Elf32_Sym *)s->data)[sym_index]; | ||
| 6461 | if (esym->st_shndx != SHN_UNDEF) { | ||
| 6462 | esym_bind = ELF32_ST_BIND(esym->st_info); | ||
| 6463 | if (sh_num == SHN_UNDEF) { | ||
| 6464 | /* ignore adding of undefined symbol if the | ||
| 6465 | corresponding symbol is already defined */ | ||
| 6466 | } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) { | ||
| 6467 | /* global overrides weak, so patch */ | ||
| 6468 | goto do_patch; | ||
| 6469 | } else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) { | ||
| 6470 | /* weak is ignored if already global */ | ||
| 6471 | } else { | ||
| 6472 | #if 0 | ||
| 6473 | printf("new_bind=%d new_shndx=%d last_bind=%d old_shndx=%d\n", | ||
| 6474 | sym_bind, sh_num, esym_bind, esym->st_shndx); | ||
| 6475 | #endif | ||
| 6476 | /* NOTE: we accept that two DLL define the same symbol */ | ||
| 6477 | if (s != dynsymtab_section) | ||
| 6478 | error("'%s' defined twice", name); | ||
| 6479 | } | ||
| 6480 | } else { | ||
| 6481 | do_patch: | ||
| 6482 | esym->st_info = ELF32_ST_INFO(sym_bind, sym_type); | ||
| 6483 | esym->st_shndx = sh_num; | ||
| 6484 | esym->st_value = value; | ||
| 6485 | esym->st_size = size; | ||
| 6486 | } | ||
| 6487 | } else { | ||
| 6488 | do_def: | ||
| 6489 | sym_index = put_elf_sym(s, value, size, | ||
| 6490 | ELF32_ST_INFO(sym_bind, sym_type), 0, | ||
| 6491 | sh_num, name); | ||
| 6492 | } | ||
| 6493 | return sym_index; | ||
| 6494 | } | ||
| 6495 | |||
| 6496 | /* update sym->c so that it points to an external symbol in section | ||
| 6497 | 'section' with value 'value' */ | ||
| 6498 | static void put_extern_sym(Sym *sym, Section *section, | ||
| 6499 | unsigned long value, unsigned long size) | ||
| 6500 | { | ||
| 6501 | int sym_type, sym_bind, sh_num, info; | ||
| 6502 | Elf32_Sym *esym; | ||
| 6503 | const char *name; | ||
| 6504 | char buf[32]; | ||
| 6505 | |||
| 6506 | if (section) | ||
| 6507 | sh_num = section->sh_num; | ||
| 6508 | else | ||
| 6509 | sh_num = SHN_UNDEF; | ||
| 6510 | if (!sym->c) { | ||
| 6511 | if ((sym->t & VT_BTYPE) == VT_FUNC) | ||
| 6512 | sym_type = STT_FUNC; | ||
| 6513 | else | ||
| 6514 | sym_type = STT_OBJECT; | ||
| 6515 | if (sym->t & VT_STATIC) | ||
| 6516 | sym_bind = STB_LOCAL; | ||
| 6517 | else | ||
| 6518 | sym_bind = STB_GLOBAL; | ||
| 6519 | |||
| 6520 | name = get_tok_str(sym->v, NULL); | ||
| 6521 | #ifdef CONFIG_TCC_BCHECK | ||
| 6522 | if (do_bounds_check) { | ||
| 6523 | /* if bound checking is activated, we change some function | ||
| 6524 | names by adding the "__bound" prefix */ | ||
| 6525 | switch(sym->v) { | ||
| 6526 | case TOK_malloc: | ||
| 6527 | case TOK_free: | ||
| 6528 | case TOK_realloc: | ||
| 6529 | case TOK_memalign: | ||
| 6530 | case TOK_calloc: | ||
| 6531 | case TOK_memcpy: | ||
| 6532 | case TOK_memmove: | ||
| 6533 | case TOK_memset: | ||
| 6534 | case TOK_strlen: | ||
| 6535 | case TOK_strcpy: | ||
| 6536 | strcpy(buf, "__bound_"); | ||
| 6537 | strcat(buf, name); | ||
| 6538 | name = buf; | ||
| 6539 | break; | ||
| 6540 | } | ||
| 6541 | } | ||
| 6542 | #endif | ||
| 6543 | info = ELF32_ST_INFO(sym_bind, sym_type); | ||
| 6544 | sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name); | ||
| 6545 | } else { | ||
| 6546 | esym = &((Elf32_Sym *)symtab_section->data)[sym->c]; | ||
| 6547 | esym->st_value = value; | ||
| 6548 | esym->st_size = size; | ||
| 6549 | esym->st_shndx = sh_num; | ||
| 6550 | } | ||
| 6551 | } | ||
| 6552 | |||
| 6553 | /* put relocation */ | ||
| 6554 | static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, | ||
| 6555 | int type, int symbol) | ||
| 6556 | { | ||
| 6557 | char buf[256]; | ||
| 6558 | Section *sr; | ||
| 6559 | Elf32_Rel *rel; | ||
| 6560 | |||
| 6561 | sr = s->reloc; | ||
| 6562 | if (!sr) { | ||
| 6563 | /* if no relocation section, create it */ | ||
| 6564 | snprintf(buf, sizeof(buf), ".rel%s", s->name); | ||
| 6565 | /* if the symtab is allocated, then we consider the relocation | ||
| 6566 | are also */ | ||
| 6567 | sr = new_section(buf, SHT_REL, symtab->sh_flags); | ||
| 6568 | sr->sh_entsize = sizeof(Elf32_Rel); | ||
| 6569 | sr->link = symtab; | ||
| 6570 | sr->sh_info = s->sh_num; | ||
| 6571 | s->reloc = sr; | ||
| 6572 | } | ||
| 6573 | rel = section_ptr_add(sr, sizeof(Elf32_Rel)); | ||
| 6574 | rel->r_offset = offset; | ||
| 6575 | rel->r_info = ELF32_R_INFO(symbol, type); | ||
| 6576 | } | ||
| 6577 | |||
| 6578 | /* put stab debug information */ | ||
| 6579 | |||
| 6580 | typedef struct { | ||
| 6581 | unsigned long n_strx; /* index into string table of name */ | ||
| 6582 | unsigned char n_type; /* type of symbol */ | ||
| 6583 | unsigned char n_other; /* misc info (usually empty) */ | ||
| 6584 | unsigned short n_desc; /* description field */ | ||
| 6585 | unsigned long n_value; /* value of symbol */ | ||
| 6586 | } Stab_Sym; | ||
| 6587 | |||
| 6588 | static void put_stabs(const char *str, int type, int other, int desc, | ||
| 6589 | unsigned long value) | ||
| 6590 | { | ||
| 6591 | Stab_Sym *sym; | ||
| 6592 | |||
| 6593 | sym = section_ptr_add(stab_section, sizeof(Stab_Sym)); | ||
| 6594 | if (str) { | ||
| 6595 | sym->n_strx = put_elf_str(stabstr_section, str); | ||
| 6596 | } else { | ||
| 6597 | sym->n_strx = 0; | ||
| 6598 | } | ||
| 6599 | sym->n_type = type; | ||
| 6600 | sym->n_other = other; | ||
| 6601 | sym->n_desc = desc; | ||
| 6602 | sym->n_value = value; | ||
| 6603 | } | ||
| 6604 | |||
| 6605 | static void put_stabs_r(const char *str, int type, int other, int desc, | ||
| 6606 | unsigned long value, Section *sec, int sym_index) | ||
| 6607 | { | ||
| 6608 | put_stabs(str, type, other, desc, value); | ||
| 6609 | put_elf_reloc(symtab_section, stab_section, | ||
| 6610 | stab_section->data_offset - sizeof(unsigned long), | ||
| 6611 | R_DATA_32, sym_index); | ||
| 6612 | } | ||
| 6613 | |||
| 6614 | static void put_stabn(int type, int other, int desc, int value) | ||
| 6615 | { | ||
| 6616 | put_stabs(NULL, type, other, desc, value); | ||
| 6617 | } | ||
| 6618 | |||
| 6619 | static void put_stabd(int type, int other, int desc) | ||
| 6620 | { | ||
| 6621 | put_stabs(NULL, type, other, desc, 0); | ||
| 6622 | } | ||
| 6623 | |||
| 6624 | /* In an ELF file symbol table, the local symbols must appear below | ||
| 6625 | the global and weak ones. Since TCC cannot sort it while generating | ||
| 6626 | the code, we must do it after. All the relocation tables are also | ||
| 6627 | modified to take into account the symbol table sorting */ | ||
| 6628 | static void sort_syms(Section *s) | ||
| 6629 | { | ||
| 6630 | int *old_to_new_syms; | ||
| 6631 | Elf32_Sym *new_syms; | ||
| 6632 | int nb_syms, i; | ||
| 6633 | Elf32_Sym *p, *q; | ||
| 6634 | Elf32_Rel *rel, *rel_end; | ||
| 6635 | Section *sr; | ||
| 6636 | int type, sym_index; | ||
| 6637 | |||
| 6638 | nb_syms = s->data_offset / sizeof(Elf32_Sym); | ||
| 6639 | new_syms = malloc(nb_syms * sizeof(Elf32_Sym)); | ||
| 6640 | if (!new_syms) | ||
| 6641 | error("memory full"); | ||
| 6642 | old_to_new_syms = malloc(nb_syms * sizeof(int)); | ||
| 6643 | if (!old_to_new_syms) | ||
| 6644 | error("memory full"); | ||
| 6645 | /* first pass for local symbols */ | ||
| 6646 | p = (Elf32_Sym *)s->data; | ||
| 6647 | q = new_syms; | ||
| 6648 | for(i = 0; i < nb_syms; i++) { | ||
| 6649 | if (ELF32_ST_BIND(p->st_info) == STB_LOCAL) { | ||
| 6650 | old_to_new_syms[i] = q - new_syms; | ||
| 6651 | *q++ = *p; | ||
| 6652 | } | ||
| 6653 | p++; | ||
| 6654 | } | ||
| 6655 | /* save the number of local symbols in section header */ | ||
| 6656 | s->sh_info = q - new_syms; | ||
| 6657 | |||
| 6658 | /* then second pass for non local symbols */ | ||
| 6659 | p = (Elf32_Sym *)s->data; | ||
| 6660 | for(i = 0; i < nb_syms; i++) { | ||
| 6661 | if (ELF32_ST_BIND(p->st_info) != STB_LOCAL) { | ||
| 6662 | old_to_new_syms[i] = q - new_syms; | ||
| 6663 | *q++ = *p; | ||
| 6664 | } | ||
| 6665 | p++; | ||
| 6666 | } | ||
| 6667 | |||
| 6668 | /* we copy the new symbols to the old */ | ||
| 6669 | memcpy(s->data, new_syms, nb_syms * sizeof(Elf32_Sym)); | ||
| 6670 | free(new_syms); | ||
| 6671 | |||
| 6672 | /* now we modify all the relocations */ | ||
| 6673 | for(i = 1; i < nb_sections; i++) { | ||
| 6674 | sr = sections[i]; | ||
| 6675 | if (sr->sh_type == SHT_REL && sr->link == s) { | ||
| 6676 | rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); | ||
| 6677 | for(rel = (Elf32_Rel *)sr->data; | ||
| 6678 | rel < rel_end; | ||
| 6679 | rel++) { | ||
| 6680 | sym_index = ELF32_R_SYM(rel->r_info); | ||
| 6681 | type = ELF32_R_TYPE(rel->r_info); | ||
| 6682 | sym_index = old_to_new_syms[sym_index]; | ||
| 6683 | rel->r_info = ELF32_R_INFO(sym_index, type); | ||
| 6684 | } | ||
| 6685 | } | ||
| 6686 | } | ||
| 6687 | |||
| 6688 | free(old_to_new_syms); | ||
| 6689 | } | ||
| 6690 | |||
| 6691 | /* relocate common symbols in the .bss section */ | ||
| 6692 | static void relocate_common_syms(void) | ||
| 6693 | { | ||
| 6694 | Elf32_Sym *sym, *sym_end; | ||
| 6695 | unsigned long offset, align; | ||
| 6696 | |||
| 6697 | sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset); | ||
| 6698 | for(sym = (Elf32_Sym *)symtab_section->data + 1; | ||
| 6699 | sym < sym_end; | ||
| 6700 | sym++) { | ||
| 6701 | if (sym->st_shndx == SHN_COMMON) { | ||
| 6702 | /* align symbol */ | ||
| 6703 | align = sym->st_value; | ||
| 6704 | offset = bss_section->data_offset; | ||
| 6705 | offset = (offset + align - 1) & -align; | ||
| 6706 | sym->st_value = offset; | ||
| 6707 | sym->st_shndx = bss_section->sh_num; | ||
| 6708 | offset += sym->st_size; | ||
| 6709 | bss_section->data_offset = offset; | ||
| 6710 | } | ||
| 6711 | } | ||
| 6712 | } | ||
| 6713 | |||
| 6714 | static void *resolve_sym(const char *sym) | ||
| 6715 | { | ||
| 6716 | return dlsym(NULL, sym); | ||
| 6717 | } | ||
| 6718 | |||
| 6719 | /* relocate symbol table, resolve undefined symbols if do_resolve is | ||
| 6720 | true and output error if undefined symbol. */ | ||
| 6721 | static void relocate_syms(int do_resolve) | ||
| 6722 | { | ||
| 6723 | Elf32_Sym *sym, *esym, *sym_end; | ||
| 6724 | int sym_bind, sh_num, sym_index; | ||
| 6725 | const char *name; | ||
| 6726 | unsigned long addr; | ||
| 6727 | |||
| 6728 | sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset); | ||
| 6729 | for(sym = (Elf32_Sym *)symtab_section->data + 1; | ||
| 6730 | sym < sym_end; | ||
| 6731 | sym++) { | ||
| 6732 | sh_num = sym->st_shndx; | ||
| 6733 | if (sh_num == SHN_UNDEF) { | ||
| 6734 | name = strtab_section->data + sym->st_name; | ||
| 6735 | if (do_resolve) { | ||
| 6736 | name = symtab_section->link->data + sym->st_name; | ||
| 6737 | addr = (unsigned long)resolve_sym(name); | ||
| 6738 | if (addr) { | ||
| 6739 | sym->st_value = addr; | ||
| 6740 | goto found; | ||
| 6741 | } | ||
| 6742 | } else if (dynsym) { | ||
| 6743 | /* if dynamic symbol exist, then use it */ | ||
| 6744 | sym_index = find_elf_sym(dynsym, name); | ||
| 6745 | if (sym_index) { | ||
| 6746 | esym = &((Elf32_Sym *)dynsym->data)[sym_index]; | ||
| 6747 | sym->st_value = esym->st_value; | ||
| 6748 | goto found; | ||
| 6749 | } | ||
| 6750 | } | ||
| 6751 | /* XXX: _fp_hw seems to be part of the ABI, so we ignore | ||
| 6752 | it */ | ||
| 6753 | if (!strcmp(name, "_fp_hw")) | ||
| 6754 | goto found; | ||
| 6755 | /* only weak symbols are accepted to be undefined. Their | ||
| 6756 | value is zero */ | ||
| 6757 | sym_bind = ELF32_ST_BIND(sym->st_info); | ||
| 6758 | if (sym_bind == STB_WEAK) { | ||
| 6759 | sym->st_value = 0; | ||
| 6760 | } else { | ||
| 6761 | error("undefined symbol '%s'", name); | ||
| 6762 | } | ||
| 6763 | } else if (sh_num < SHN_LORESERVE) { | ||
| 6764 | /* add section base */ | ||
| 6765 | sym->st_value += sections[sym->st_shndx]->sh_addr; | ||
| 6766 | } | ||
| 6767 | found: ; | ||
| 6768 | } | ||
| 6769 | } | ||
| 6770 | |||
| 6771 | /* relocate a given section (CPU dependant) */ | ||
| 6772 | static void relocate_section(TCCState *s1, Section *s) | ||
| 6773 | { | ||
| 6774 | Section *sr; | ||
| 6775 | Elf32_Rel *rel, *rel_end, *qrel; | ||
| 6776 | Elf32_Sym *sym; | ||
| 6777 | int type, sym_index, esym_index; | ||
| 6778 | unsigned char *ptr; | ||
| 6779 | unsigned long val, addr; | ||
| 6780 | |||
| 6781 | sr = s->reloc; | ||
| 6782 | rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); | ||
| 6783 | qrel = (Elf32_Rel *)sr->data; | ||
| 6784 | for(rel = qrel; | ||
| 6785 | rel < rel_end; | ||
| 6786 | rel++) { | ||
| 6787 | ptr = s->data + rel->r_offset; | ||
| 6788 | |||
| 6789 | sym_index = ELF32_R_SYM(rel->r_info); | ||
| 6790 | sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; | ||
| 6791 | val = sym->st_value; | ||
| 6792 | type = ELF32_R_TYPE(rel->r_info); | ||
| 6793 | addr = s->sh_addr + rel->r_offset; | ||
| 6794 | |||
| 6795 | /* CPU specific */ | ||
| 6796 | switch(type) { | ||
| 6797 | case R_386_32: | ||
| 6798 | if (s1->output_type == TCC_OUTPUT_DLL) { | ||
| 6799 | esym_index = symtab_to_dynsym[sym_index]; | ||
| 6800 | qrel->r_offset = rel->r_offset; | ||
| 6801 | if (esym_index) { | ||
| 6802 | qrel->r_info = ELF32_R_INFO(esym_index, R_386_32); | ||
| 6803 | qrel++; | ||
| 6804 | break; | ||
| 6805 | } else { | ||
| 6806 | qrel->r_info = ELF32_R_INFO(0, R_386_RELATIVE); | ||
| 6807 | qrel++; | ||
| 6808 | } | ||
| 6809 | } | ||
| 6810 | *(int *)ptr += val; | ||
| 6811 | break; | ||
| 6812 | case R_386_PC32: | ||
| 6813 | if (s1->output_type == TCC_OUTPUT_DLL) { | ||
| 6814 | /* DLL relocation */ | ||
| 6815 | esym_index = symtab_to_dynsym[sym_index]; | ||
| 6816 | if (esym_index) { | ||
| 6817 | qrel->r_offset = rel->r_offset; | ||
| 6818 | qrel->r_info = ELF32_R_INFO(esym_index, R_386_PC32); | ||
| 6819 | qrel++; | ||
| 6820 | break; | ||
| 6821 | } | ||
| 6822 | } | ||
| 6823 | *(int *)ptr += val - addr; | ||
| 6824 | break; | ||
| 6825 | case R_386_PLT32: | ||
| 6826 | *(int *)ptr += val - addr; | ||
| 6827 | break; | ||
| 6828 | case R_386_GLOB_DAT: | ||
| 6829 | case R_386_JMP_SLOT: | ||
| 6830 | *(int *)ptr = val; | ||
| 6831 | break; | ||
| 6832 | case R_386_GOTPC: | ||
| 6833 | *(int *)ptr += got->sh_addr - addr; | ||
| 6834 | break; | ||
| 6835 | case R_386_GOTOFF: | ||
| 6836 | *(int *)ptr += val - got->sh_addr; | ||
| 6837 | break; | ||
| 6838 | case R_386_GOT32: | ||
| 6839 | /* we load the got offset */ | ||
| 6840 | *(int *)ptr += got_offsets[sym_index]; | ||
| 6841 | break; | ||
| 6842 | } | ||
| 6843 | } | ||
| 6844 | /* if the relocation is allocated, we change its symbol table */ | ||
| 6845 | if (sr->sh_flags & SHF_ALLOC) | ||
| 6846 | sr->link = dynsym; | ||
| 6847 | } | ||
| 6848 | |||
| 6849 | /* relocate relocation table in 'sr' */ | ||
| 6850 | static void relocate_rel(Section *sr) | ||
| 6851 | { | ||
| 6852 | Section *s; | ||
| 6853 | Elf32_Rel *rel, *rel_end; | ||
| 6854 | |||
| 6855 | s = sections[sr->sh_info]; | ||
| 6856 | rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); | ||
| 6857 | for(rel = (Elf32_Rel *)sr->data; | ||
| 6858 | rel < rel_end; | ||
| 6859 | rel++) { | ||
| 6860 | rel->r_offset += s->sh_addr; | ||
| 6861 | } | ||
| 6862 | } | ||
| 6863 | |||
| 6864 | /* count the number of dynamic relocations so that we can reserve | ||
| 6865 | their space */ | ||
| 6866 | static int prepare_dynamic_rel(Section *sr) | ||
| 6867 | { | ||
| 6868 | Elf32_Rel *rel, *rel_end; | ||
| 6869 | int sym_index, esym_index, type, count; | ||
| 6870 | |||
| 6871 | count = 0; | ||
| 6872 | rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); | ||
| 6873 | for(rel = (Elf32_Rel *)sr->data; rel < rel_end; rel++) { | ||
| 6874 | sym_index = ELF32_R_SYM(rel->r_info); | ||
| 6875 | type = ELF32_R_TYPE(rel->r_info); | ||
| 6876 | switch(type) { | ||
| 6877 | case R_386_32: | ||
| 6878 | count++; | ||
| 6879 | break; | ||
| 6880 | case R_386_PC32: | ||
| 6881 | esym_index = symtab_to_dynsym[sym_index]; | ||
| 6882 | if (esym_index) | ||
| 6883 | count++; | ||
| 6884 | break; | ||
| 6885 | default: | ||
| 6886 | break; | ||
| 6887 | } | ||
| 6888 | } | ||
| 6889 | if (count) { | ||
| 6890 | /* allocate the section */ | ||
| 6891 | sr->sh_flags |= SHF_ALLOC; | ||
| 6892 | sr->sh_size = count * sizeof(Elf32_Rel); | ||
| 6893 | } | ||
| 6894 | return count; | ||
| 6895 | } | ||
| 6896 | |||
| 6897 | static void put_got_offset(int index, unsigned long val) | ||
| 6898 | { | ||
| 6899 | int n; | ||
| 6900 | unsigned long *tab; | ||
| 6901 | |||
| 6902 | if (index >= nb_got_offsets) { | ||
| 6903 | /* find immediately bigger power of 2 and reallocate array */ | ||
| 6904 | n = 1; | ||
| 6905 | while (index >= n) | ||
| 6906 | n *= 2; | ||
| 6907 | tab = realloc(got_offsets, n * sizeof(unsigned long)); | ||
| 6908 | if (!tab) | ||
| 6909 | error("memory full"); | ||
| 6910 | got_offsets = tab; | ||
| 6911 | memset(got_offsets + nb_got_offsets, 0, | ||
| 6912 | (n - nb_got_offsets) * sizeof(unsigned long)); | ||
| 6913 | nb_got_offsets = n; | ||
| 6914 | } | ||
| 6915 | got_offsets[index] = val; | ||
| 6916 | } | ||
| 6917 | |||
| 6918 | /* XXX: suppress that */ | ||
| 6919 | static void put32(unsigned char *p, unsigned int val) | ||
| 6920 | { | ||
| 6921 | p[0] = val; | ||
| 6922 | p[1] = val >> 8; | ||
| 6923 | p[2] = val >> 16; | ||
| 6924 | p[3] = val >> 24; | ||
| 6925 | } | ||
| 6926 | |||
| 6927 | static void build_got(void) | ||
| 6928 | { | ||
| 6929 | unsigned char *ptr; | ||
| 6930 | |||
| 6931 | /* if no got, then create it */ | ||
| 6932 | got = new_section(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); | ||
| 6933 | got->sh_entsize = 4; | ||
| 6934 | add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT), | ||
| 6935 | got->sh_num, "_GLOBAL_OFFSET_TABLE_"); | ||
| 6936 | ptr = section_ptr_add(got, 3 * sizeof(int)); | ||
| 6937 | /* keep space for _DYNAMIC pointer, if present */ | ||
| 6938 | put32(ptr, 0); | ||
| 6939 | /* two dummy got entries */ | ||
| 6940 | put32(ptr + 4, 0); | ||
| 6941 | put32(ptr + 8, 0); | ||
| 6942 | } | ||
| 6943 | |||
| 6944 | /* put a got entry corresponding to a symbol in symtab_section. 'size' | ||
| 6945 | and 'info' can be modifed if more precise info comes from the DLL */ | ||
| 6946 | static void put_got_entry(int reloc_type, unsigned long size, int info, | ||
| 6947 | int sym_index) | ||
| 6948 | { | ||
| 6949 | int index; | ||
| 6950 | const char *name; | ||
| 6951 | Elf32_Sym *sym; | ||
| 6952 | unsigned long offset; | ||
| 6953 | int *ptr; | ||
| 6954 | |||
| 6955 | if (!got) | ||
| 6956 | build_got(); | ||
| 6957 | |||
| 6958 | /* if a got entry already exists for that symbol, no need to add one */ | ||
| 6959 | if (sym_index < nb_got_offsets && | ||
| 6960 | got_offsets[sym_index] != 0) | ||
| 6961 | return; | ||
| 6962 | |||
| 6963 | put_got_offset(sym_index, got->data_offset); | ||
| 6964 | |||
| 6965 | if (dynsym) { | ||
| 6966 | sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; | ||
| 6967 | name = symtab_section->link->data + sym->st_name; | ||
| 6968 | offset = sym->st_value; | ||
| 6969 | /* NOTE: we put temporarily the got offset */ | ||
| 6970 | if (reloc_type == R_386_JMP_SLOT) { | ||
| 6971 | nb_plt_entries++; | ||
| 6972 | offset = got->data_offset; | ||
| 6973 | } | ||
| 6974 | index = put_elf_sym(dynsym, offset, | ||
| 6975 | size, info, 0, sym->st_shndx, name); | ||
| 6976 | /* put a got entry */ | ||
| 6977 | put_elf_reloc(dynsym, got, | ||
| 6978 | got->data_offset, | ||
| 6979 | reloc_type, index); | ||
| 6980 | } | ||
| 6981 | ptr = section_ptr_add(got, sizeof(int)); | ||
| 6982 | *ptr = 0; | ||
| 6983 | } | ||
| 6984 | |||
| 6985 | /* build GOT and PLT entries */ | ||
| 6986 | static void build_got_entries(void) | ||
| 6987 | { | ||
| 6988 | Section *s, *symtab; | ||
| 6989 | Elf32_Rel *rel, *rel_end; | ||
| 6990 | Elf32_Sym *sym; | ||
| 6991 | int i, type, reloc_type, sym_index; | ||
| 6992 | |||
| 6993 | for(i = 1; i < nb_sections; i++) { | ||
| 6994 | s = sections[i]; | ||
| 6995 | if (s->sh_type != SHT_REL) | ||
| 6996 | continue; | ||
| 6997 | /* no need to handle got relocations */ | ||
| 6998 | if (s->link != symtab_section) | ||
| 6999 | continue; | ||
| 7000 | symtab = s->link; | ||
| 7001 | rel_end = (Elf32_Rel *)(s->data + s->data_offset); | ||
| 7002 | for(rel = (Elf32_Rel *)s->data; | ||
| 7003 | rel < rel_end; | ||
| 7004 | rel++) { | ||
| 7005 | type = ELF32_R_TYPE(rel->r_info); | ||
| 7006 | switch(type) { | ||
| 7007 | case R_386_GOT32: | ||
| 7008 | case R_386_GOTOFF: | ||
| 7009 | case R_386_GOTPC: | ||
| 7010 | case R_386_PLT32: | ||
| 7011 | if (!got) | ||
| 7012 | build_got(); | ||
| 7013 | if (type == R_386_GOT32 || type == R_386_PLT32) { | ||
| 7014 | sym_index = ELF32_R_SYM(rel->r_info); | ||
| 7015 | sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; | ||
| 7016 | /* look at the symbol got offset. If none, then add one */ | ||
| 7017 | if (type == R_386_GOT32) | ||
| 7018 | reloc_type = R_386_GLOB_DAT; | ||
| 7019 | else | ||
| 7020 | reloc_type = R_386_JMP_SLOT; | ||
| 7021 | put_got_entry(reloc_type, sym->st_size, sym->st_info, | ||
| 7022 | sym_index); | ||
| 7023 | } | ||
| 7024 | break; | ||
| 7025 | default: | ||
| 7026 | break; | ||
| 7027 | } | ||
| 7028 | } | ||
| 7029 | } | ||
| 7030 | } | ||
| 7031 | |||
| 7032 | static Section *new_symtab(const char *symtab_name, int sh_type, int sh_flags, | ||
| 7033 | const char *strtab_name, | ||
| 7034 | const char *hash_name, int hash_sh_flags) | ||
| 7035 | { | ||
| 7036 | Section *symtab, *strtab, *hash; | ||
| 7037 | int *ptr, nb_buckets; | ||
| 7038 | |||
| 7039 | symtab = new_section(symtab_name, sh_type, sh_flags); | ||
| 7040 | symtab->sh_entsize = sizeof(Elf32_Sym); | ||
| 7041 | strtab = new_section(strtab_name, SHT_STRTAB, sh_flags); | ||
| 7042 | put_elf_str(strtab, ""); | ||
| 7043 | symtab->link = strtab; | ||
| 7044 | put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL); | ||
| 7045 | |||
| 7046 | nb_buckets = 1; | ||
| 7047 | |||
| 7048 | hash = new_section(hash_name, SHT_HASH, hash_sh_flags); | ||
| 7049 | hash->sh_entsize = sizeof(int); | ||
| 7050 | symtab->hash = hash; | ||
| 7051 | hash->link = symtab; | ||
| 7052 | |||
| 7053 | ptr = section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int)); | ||
| 7054 | ptr[0] = nb_buckets; | ||
| 7055 | ptr[1] = 1; | ||
| 7056 | memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int)); | ||
| 7057 | return symtab; | ||
| 7058 | } | ||
| 7059 | |||
| 7060 | /* put dynamic tag */ | ||
| 7061 | static void put_dt(Section *dynamic, int dt, unsigned long val) | ||
| 7062 | { | ||
| 7063 | Elf32_Dyn *dyn; | ||
| 7064 | dyn = section_ptr_add(dynamic, sizeof(Elf32_Dyn)); | ||
| 7065 | dyn->d_tag = dt; | ||
| 7066 | dyn->d_un.d_val = val; | ||
| 7067 | } | ||
| 7068 | |||
| 7069 | /* add tcc runtime libraries */ | ||
| 7070 | static void tcc_add_runtime(TCCState *s1) | ||
| 7071 | { | ||
| 7072 | char buf[1024]; | ||
| 7073 | unsigned long *ptr; | ||
| 7074 | int i; | ||
| 7075 | Section *s; | ||
| 7076 | |||
| 7077 | snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "libtcc1.o"); | ||
| 7078 | tcc_add_file(s1, buf); | ||
| 7079 | #ifdef CONFIG_TCC_BCHECK | ||
| 7080 | if (do_bounds_check) { | ||
| 7081 | /* XXX: add an object file to do that */ | ||
| 7082 | ptr = section_ptr_add(bounds_section, sizeof(unsigned long)); | ||
| 7083 | *ptr = 0; | ||
| 7084 | add_elf_sym(symtab_section, 0, 0, | ||
| 7085 | ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), | ||
| 7086 | bounds_section->sh_num, "__bounds_start"); | ||
| 7087 | add_elf_sym(symtab_section, (long)&rt_error, 0, | ||
| 7088 | ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), | ||
| 7089 | SHN_ABS, "rt_error"); | ||
| 7090 | /* add bound check code */ | ||
| 7091 | snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "bcheck.o"); | ||
| 7092 | tcc_add_file(s1, buf); | ||
| 7093 | } | ||
| 7094 | #endif | ||
| 7095 | /* add libc if not memory output */ | ||
| 7096 | if (s1->output_type != TCC_OUTPUT_MEMORY) { | ||
| 7097 | tcc_add_library(s1, "c"); | ||
| 7098 | tcc_add_file(s1, CONFIG_TCC_CRT_PREFIX "/crtn.o"); | ||
| 7099 | } | ||
| 7100 | /* add various standard linker symbols */ | ||
| 7101 | add_elf_sym(symtab_section, | ||
| 7102 | text_section->data_offset, 0, | ||
| 7103 | ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), | ||
| 7104 | text_section->sh_num, "_etext"); | ||
| 7105 | add_elf_sym(symtab_section, | ||
| 7106 | data_section->data_offset, 0, | ||
| 7107 | ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), | ||
| 7108 | data_section->sh_num, "_edata"); | ||
| 7109 | add_elf_sym(symtab_section, | ||
| 7110 | bss_section->data_offset, 0, | ||
| 7111 | ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), | ||
| 7112 | bss_section->sh_num, "_end"); | ||
| 7113 | /* add start and stop symbols for sections whose name can be | ||
| 7114 | expressed in C */ | ||
| 7115 | for(i = 1; i < nb_sections; i++) { | ||
| 7116 | s = sections[i]; | ||
| 7117 | if (s->sh_type == SHT_PROGBITS && | ||
| 7118 | (s->sh_flags & SHF_ALLOC)) { | ||
| 7119 | const char *p; | ||
| 7120 | int ch; | ||
| 7121 | |||
| 7122 | /* check if section name can be expressed in C */ | ||
| 7123 | p = s->name; | ||
| 7124 | for(;;) { | ||
| 7125 | ch = *p; | ||
| 7126 | if (!ch) | ||
| 7127 | break; | ||
| 7128 | if (!isid(ch) && !isnum(ch)) | ||
| 7129 | goto next_sec; | ||
| 7130 | p++; | ||
| 7131 | } | ||
| 7132 | snprintf(buf, sizeof(buf), "__start_%s", s->name); | ||
| 7133 | add_elf_sym(symtab_section, | ||
| 7134 | 0, 0, | ||
| 7135 | ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), | ||
| 7136 | s->sh_num, buf); | ||
| 7137 | snprintf(buf, sizeof(buf), "__stop_%s", s->name); | ||
| 7138 | add_elf_sym(symtab_section, | ||
| 7139 | s->data_offset, 0, | ||
| 7140 | ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), | ||
| 7141 | s->sh_num, buf); | ||
| 7142 | } | ||
| 7143 | next_sec: ; | ||
| 7144 | } | ||
| 7145 | } | ||
| 7146 | |||
| 7147 | /* add dynamic sections so that the executable is dynamically linked */ | ||
| 7148 | static char elf_interp[] = "/lib/ld-linux.so.2"; | ||
| 7149 | |||
| 7150 | #define ELF_START_ADDR 0x08048000 | ||
| 7151 | #define ELF_PAGE_SIZE 0x1000 | ||
| 7152 | |||
| 7153 | /* output an ELF file */ | ||
| 7154 | /* XXX: handle realloc'ed sections (instead of mmaping them) */ | ||
| 7155 | /* XXX: suppress unneeded sections */ | ||
| 7156 | int tcc_output_file(TCCState *s1, const char *filename) | ||
| 7157 | { | ||
| 7158 | Elf32_Ehdr ehdr; | ||
| 7159 | FILE *f; | ||
| 7160 | int fd, mode; | ||
| 7161 | int *section_order; | ||
| 7162 | int shnum, i, phnum, file_offset, offset, size, j, tmp, sh_order_index, k; | ||
| 7163 | unsigned long addr; | ||
| 7164 | Section *strsec, *s; | ||
| 7165 | Elf32_Shdr shdr, *sh; | ||
| 7166 | Elf32_Phdr *phdr, *ph; | ||
| 7167 | Section *interp, *plt, *dynamic, *dynstr; | ||
| 7168 | unsigned long saved_dynamic_data_offset; | ||
| 7169 | Elf32_Sym *sym; | ||
| 7170 | int type, file_type; | ||
| 7171 | unsigned long rel_addr, rel_size; | ||
| 7172 | |||
| 7173 | file_type = s1->output_type; | ||
| 7174 | |||
| 7175 | if (file_type != TCC_OUTPUT_OBJ) | ||
| 7176 | tcc_add_runtime(s1); | ||
| 7177 | |||
| 7178 | interp = NULL; | ||
| 7179 | dynamic = NULL; | ||
| 7180 | dynsym = NULL; | ||
| 7181 | got = NULL; | ||
| 7182 | nb_plt_entries = 0; | ||
| 7183 | plt = NULL; /* avoid warning */ | ||
| 7184 | dynstr = NULL; /* avoid warning */ | ||
| 7185 | saved_dynamic_data_offset = 0; /* avoid warning */ | ||
| 7186 | |||
| 7187 | if (file_type != TCC_OUTPUT_OBJ) { | ||
| 7188 | |||
| 7189 | relocate_common_syms(); | ||
| 7190 | |||
| 7191 | if (!static_link) { | ||
| 7192 | const char *name; | ||
| 7193 | int sym_index, index; | ||
| 7194 | Elf32_Sym *esym, *sym_end; | ||
| 7195 | |||
| 7196 | if (file_type == TCC_OUTPUT_EXE) { | ||
| 7197 | char *ptr; | ||
| 7198 | /* add interpreter section only if executable */ | ||
| 7199 | interp = new_section(".interp", SHT_PROGBITS, SHF_ALLOC); | ||
| 7200 | interp->sh_addralign = 1; | ||
| 7201 | ptr = section_ptr_add(interp, sizeof(elf_interp)); | ||
| 7202 | strcpy(ptr, elf_interp); | ||
| 7203 | } | ||
| 7204 | |||
| 7205 | /* add dynamic symbol table */ | ||
| 7206 | dynsym = new_symtab(".dynsym", SHT_DYNSYM, SHF_ALLOC, | ||
| 7207 | ".dynstr", | ||
| 7208 | ".hash", SHF_ALLOC); | ||
| 7209 | dynstr = dynsym->link; | ||
| 7210 | |||
| 7211 | /* add dynamic section */ | ||
| 7212 | dynamic = new_section(".dynamic", SHT_DYNAMIC, | ||
| 7213 | SHF_ALLOC | SHF_WRITE); | ||
| 7214 | dynamic->link = dynstr; | ||
| 7215 | dynamic->sh_entsize = sizeof(Elf32_Dyn); | ||
| 7216 | |||
| 7217 | /* add PLT */ | ||
| 7218 | plt = new_section(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR); | ||
| 7219 | plt->sh_entsize = 4; | ||
| 7220 | |||
| 7221 | build_got(); | ||
| 7222 | |||
| 7223 | /* scan for undefined symbols and see if they are in the | ||
| 7224 | dynamic symbols. If a symbol STT_FUNC is found, then we | ||
| 7225 | add it in the PLT. If a symbol STT_OBJECT is found, we | ||
| 7226 | add it in the .bss section with a suitable relocation */ | ||
| 7227 | sym_end = (Elf32_Sym *)(symtab_section->data + | ||
| 7228 | symtab_section->data_offset); | ||
| 7229 | if (file_type == TCC_OUTPUT_EXE) { | ||
| 7230 | for(sym = (Elf32_Sym *)symtab_section->data + 1; | ||
| 7231 | sym < sym_end; | ||
| 7232 | sym++) { | ||
| 7233 | if (sym->st_shndx == SHN_UNDEF) { | ||
| 7234 | name = symtab_section->link->data + sym->st_name; | ||
| 7235 | sym_index = find_elf_sym(dynsymtab_section, name); | ||
| 7236 | if (sym_index) { | ||
| 7237 | esym = &((Elf32_Sym *)dynsymtab_section->data)[sym_index]; | ||
| 7238 | type = ELF32_ST_TYPE(esym->st_info); | ||
| 7239 | if (type == STT_FUNC) { | ||
| 7240 | put_got_entry(R_386_JMP_SLOT, esym->st_size, | ||
| 7241 | esym->st_info, | ||
| 7242 | sym - (Elf32_Sym *)symtab_section->data); | ||
| 7243 | } else if (type == STT_OBJECT) { | ||
| 7244 | unsigned long offset; | ||
| 7245 | offset = bss_section->data_offset; | ||
| 7246 | /* XXX: which alignment ? */ | ||
| 7247 | offset = (offset + 8 - 1) & -8; | ||
| 7248 | index = put_elf_sym(dynsym, offset, esym->st_size, | ||
| 7249 | esym->st_info, 0, | ||
| 7250 | bss_section->sh_num, name); | ||
| 7251 | put_elf_reloc(dynsym, bss_section, | ||
| 7252 | offset, R_386_COPY, index); | ||
| 7253 | offset += esym->st_size; | ||
| 7254 | bss_section->data_offset = offset; | ||
| 7255 | } | ||
| 7256 | } else { | ||
| 7257 | /* STB_WEAK undefined symbols are accepted */ | ||
| 7258 | /* XXX: _fp_hw seems to be part of the ABI, so we ignore | ||
| 7259 | it */ | ||
| 7260 | if (ELF32_ST_BIND(sym->st_info) == STB_WEAK || | ||
| 7261 | !strcmp(name, "_fp_hw")) { | ||
| 7262 | } else { | ||
| 7263 | error("undefined symbol '%s'", name); | ||
| 7264 | } | ||
| 7265 | } | ||
| 7266 | } | ||
| 7267 | } | ||
| 7268 | |||
| 7269 | /* now look at unresolved dynamic symbols and export | ||
| 7270 | corresponding symbol */ | ||
| 7271 | sym_end = (Elf32_Sym *)(dynsymtab_section->data + | ||
| 7272 | dynsymtab_section->data_offset); | ||
| 7273 | for(esym = (Elf32_Sym *)dynsymtab_section->data + 1; | ||
| 7274 | esym < sym_end; | ||
| 7275 | esym++) { | ||
| 7276 | if (esym->st_shndx == SHN_UNDEF) { | ||
| 7277 | name = dynsymtab_section->link->data + esym->st_name; | ||
| 7278 | sym_index = find_elf_sym(symtab_section, name); | ||
| 7279 | if (sym_index) { | ||
| 7280 | sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; | ||
| 7281 | put_elf_sym(dynsym, sym->st_value, sym->st_size, | ||
| 7282 | sym->st_info, 0, | ||
| 7283 | sym->st_shndx, name); | ||
| 7284 | } else { | ||
| 7285 | if (ELF32_ST_BIND(esym->st_info) == STB_WEAK) { | ||
| 7286 | /* weak symbols can stay undefined */ | ||
| 7287 | } else { | ||
| 7288 | warning("undefined dynamic symbol '%s'", name); | ||
| 7289 | } | ||
| 7290 | } | ||
| 7291 | } | ||
| 7292 | } | ||
| 7293 | } else { | ||
| 7294 | int nb_syms; | ||
| 7295 | /* shared library case : we simply export all the global symbols */ | ||
| 7296 | nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym); | ||
| 7297 | symtab_to_dynsym = malloc(sizeof(int) * nb_syms); | ||
| 7298 | memset(symtab_to_dynsym, 0, sizeof(int) * nb_syms); | ||
| 7299 | for(sym = (Elf32_Sym *)symtab_section->data + 1; | ||
| 7300 | sym < sym_end; | ||
| 7301 | sym++) { | ||
| 7302 | if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) { | ||
| 7303 | name = symtab_section->link->data + sym->st_name; | ||
| 7304 | index = put_elf_sym(dynsym, sym->st_value, sym->st_size, | ||
| 7305 | sym->st_info, 0, | ||
| 7306 | sym->st_shndx, name); | ||
| 7307 | symtab_to_dynsym[sym - (Elf32_Sym *)symtab_section->data] = | ||
| 7308 | index; | ||
| 7309 | } | ||
| 7310 | } | ||
| 7311 | } | ||
| 7312 | |||
| 7313 | build_got_entries(); | ||
| 7314 | |||
| 7315 | /* update PLT/GOT sizes so that we can allocate their space */ | ||
| 7316 | plt->data_offset += 16 * (nb_plt_entries + 1); | ||
| 7317 | |||
| 7318 | /* add a list of needed dlls */ | ||
| 7319 | for(i = 0; i < nb_loaded_dlls; i++) { | ||
| 7320 | DLLReference *dllref = loaded_dlls[i]; | ||
| 7321 | if (dllref->level == 0) | ||
| 7322 | put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name)); | ||
| 7323 | } | ||
| 7324 | /* XXX: currently, since we do not handle PIC code, we | ||
| 7325 | must relocate the readonly segments */ | ||
| 7326 | if (file_type == TCC_OUTPUT_DLL) | ||
| 7327 | put_dt(dynamic, DT_TEXTREL, 0); | ||
| 7328 | |||
| 7329 | /* add necessary space for other entries */ | ||
| 7330 | saved_dynamic_data_offset = dynamic->data_offset; | ||
| 7331 | dynamic->data_offset += 8 * 9; | ||
| 7332 | } else { | ||
| 7333 | /* still need to build got entries in case of static link */ | ||
| 7334 | build_got_entries(); | ||
| 7335 | } | ||
| 7336 | } | ||
| 7337 | |||
| 7338 | memset(&ehdr, 0, sizeof(ehdr)); | ||
| 7339 | |||
| 7340 | /* we add a section for symbols */ | ||
| 7341 | strsec = new_section(".shstrtab", SHT_STRTAB, 0); | ||
| 7342 | put_elf_str(strsec, ""); | ||
| 7343 | |||
| 7344 | /* compute number of sections */ | ||
| 7345 | shnum = nb_sections; | ||
| 7346 | |||
| 7347 | /* this array is used to reorder sections in the output file */ | ||
| 7348 | section_order = malloc(sizeof(int) * shnum); | ||
| 7349 | if (!section_order) | ||
| 7350 | error("memory full"); | ||
| 7351 | section_order[0] = 0; | ||
| 7352 | sh_order_index = 1; | ||
| 7353 | |||
| 7354 | /* compute number of program headers */ | ||
| 7355 | switch(file_type) { | ||
| 7356 | default: | ||
| 7357 | case TCC_OUTPUT_OBJ: | ||
| 7358 | phnum = 0; | ||
| 7359 | break; | ||
| 7360 | case TCC_OUTPUT_EXE: | ||
| 7361 | if (!static_link) | ||
| 7362 | phnum = 4; | ||
| 7363 | else | ||
| 7364 | phnum = 2; | ||
| 7365 | break; | ||
| 7366 | case TCC_OUTPUT_DLL: | ||
| 7367 | phnum = 3; | ||
| 7368 | break; | ||
| 7369 | } | ||
| 7370 | |||
| 7371 | /* allocate strings for section names and decide if an unallocated | ||
| 7372 | section should be output */ | ||
| 7373 | /* NOTE: the strsec section comes last, so its size is also | ||
| 7374 | correct ! */ | ||
| 7375 | for(i = 1; i < nb_sections; i++) { | ||
| 7376 | s = sections[i]; | ||
| 7377 | s->sh_name = put_elf_str(strsec, s->name); | ||
| 7378 | /* when generating a DLL, we include relocations but we may | ||
| 7379 | patch them */ | ||
| 7380 | if (file_type == TCC_OUTPUT_DLL && | ||
| 7381 | s->sh_type == SHT_REL && | ||
| 7382 | !(s->sh_flags & SHF_ALLOC)) { | ||
| 7383 | prepare_dynamic_rel(s); | ||
| 7384 | } else if (do_debug || | ||
| 7385 | file_type == TCC_OUTPUT_OBJ || | ||
| 7386 | (s->sh_flags & SHF_ALLOC) || | ||
| 7387 | i == (nb_sections - 1)) { | ||
| 7388 | /* we output all sections if debug or object file */ | ||
| 7389 | s->sh_size = s->data_offset; | ||
| 7390 | } | ||
| 7391 | } | ||
| 7392 | |||
| 7393 | /* allocate program segment headers */ | ||
| 7394 | phdr = malloc(phnum * sizeof(Elf32_Phdr)); | ||
| 7395 | if (!phdr) | ||
| 7396 | error("memory full"); | ||
| 7397 | memset(phdr, 0, phnum * sizeof(Elf32_Phdr)); | ||
| 7398 | |||
| 7399 | file_offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr); | ||
| 7400 | if (phnum > 0) { | ||
| 7401 | /* compute section to program header mapping */ | ||
| 7402 | if (file_type == TCC_OUTPUT_DLL) | ||
| 7403 | addr = 0; | ||
| 7404 | else | ||
| 7405 | addr = ELF_START_ADDR; | ||
| 7406 | |||
| 7407 | /* dynamic relocation table information, for .dynamic section */ | ||
| 7408 | rel_size = 0; | ||
| 7409 | rel_addr = 0; | ||
| 7410 | |||
| 7411 | /* compute address after headers */ | ||
| 7412 | addr += (file_offset & (ELF_PAGE_SIZE - 1)); | ||
| 7413 | |||
| 7414 | /* leave one program header for the program interpreter */ | ||
| 7415 | ph = &phdr[0]; | ||
| 7416 | if (interp) | ||
| 7417 | ph++; | ||
| 7418 | |||
| 7419 | for(j = 0; j < 2; j++) { | ||
| 7420 | ph->p_type = PT_LOAD; | ||
| 7421 | if (j == 0) | ||
| 7422 | ph->p_flags = PF_R | PF_X; | ||
| 7423 | else | ||
| 7424 | ph->p_flags = PF_R | PF_W; | ||
| 7425 | ph->p_align = ELF_PAGE_SIZE; | ||
| 7426 | |||
| 7427 | /* we do the following ordering: interp, symbol tables, | ||
| 7428 | relocations, progbits, nobits */ | ||
| 7429 | /* XXX: do faster and simpler sorting */ | ||
| 7430 | for(k = 0; k < 5; k++) { | ||
| 7431 | for(i = 1; i < nb_sections; i++) { | ||
| 7432 | s = sections[i]; | ||
| 7433 | /* compute if section should be included */ | ||
| 7434 | if (j == 0) { | ||
| 7435 | if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != | ||
| 7436 | SHF_ALLOC) | ||
| 7437 | continue; | ||
| 7438 | } else { | ||
| 7439 | if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != | ||
| 7440 | (SHF_ALLOC | SHF_WRITE)) | ||
| 7441 | continue; | ||
| 7442 | } | ||
| 7443 | if (s == interp) { | ||
| 7444 | if (k != 0) | ||
| 7445 | continue; | ||
| 7446 | } else if (s->sh_type == SHT_DYNSYM || | ||
| 7447 | s->sh_type == SHT_STRTAB || | ||
| 7448 | s->sh_type == SHT_HASH) { | ||
| 7449 | if (k != 1) | ||
| 7450 | continue; | ||
| 7451 | } else if (s->sh_type == SHT_REL) { | ||
| 7452 | if (k != 2) | ||
| 7453 | continue; | ||
| 7454 | } else if (s->sh_type == SHT_NOBITS) { | ||
| 7455 | if (k != 4) | ||
| 7456 | continue; | ||
| 7457 | } else { | ||
| 7458 | if (k != 3) | ||
| 7459 | continue; | ||
| 7460 | } | ||
| 7461 | section_order[sh_order_index++] = i; | ||
| 7462 | |||
| 7463 | /* section matches: we align it and add its size */ | ||
| 7464 | tmp = file_offset; | ||
| 7465 | file_offset = (file_offset + s->sh_addralign - 1) & | ||
| 7466 | ~(s->sh_addralign - 1); | ||
| 7467 | s->sh_offset = file_offset; | ||
| 7468 | addr += file_offset - tmp; | ||
| 7469 | s->sh_addr = addr; | ||
| 7470 | |||
| 7471 | /* update program header infos */ | ||
| 7472 | if (ph->p_offset == 0) { | ||
| 7473 | ph->p_offset = file_offset; | ||
| 7474 | ph->p_vaddr = addr; | ||
| 7475 | ph->p_paddr = ph->p_vaddr; | ||
| 7476 | } | ||
| 7477 | /* update dynamic relocation infos */ | ||
| 7478 | if (s->sh_type == SHT_REL) { | ||
| 7479 | if (rel_size == 0) | ||
| 7480 | rel_addr = addr; | ||
| 7481 | rel_size += s->sh_size; | ||
| 7482 | } | ||
| 7483 | addr += s->sh_size; | ||
| 7484 | if (s->sh_type != SHT_NOBITS) | ||
| 7485 | file_offset += s->sh_size; | ||
| 7486 | } | ||
| 7487 | } | ||
| 7488 | ph->p_filesz = file_offset - ph->p_offset; | ||
| 7489 | ph->p_memsz = addr - ph->p_vaddr; | ||
| 7490 | ph++; | ||
| 7491 | } | ||
| 7492 | |||
| 7493 | /* if interpreter, then add corresponing program header */ | ||
| 7494 | if (interp) { | ||
| 7495 | ph = &phdr[0]; | ||
| 7496 | |||
| 7497 | ph->p_type = PT_INTERP; | ||
| 7498 | ph->p_offset = interp->sh_offset; | ||
| 7499 | ph->p_vaddr = interp->sh_addr; | ||
| 7500 | ph->p_paddr = ph->p_vaddr; | ||
| 7501 | ph->p_filesz = interp->sh_size; | ||
| 7502 | ph->p_memsz = interp->sh_size; | ||
| 7503 | ph->p_flags = PF_R; | ||
| 7504 | ph->p_align = interp->sh_addralign; | ||
| 7505 | } | ||
| 7506 | |||
| 7507 | /* if dynamic section, then add corresponing program header */ | ||
| 7508 | if (dynamic) { | ||
| 7509 | int plt_offset; | ||
| 7510 | unsigned char *p; | ||
| 7511 | Elf32_Sym *sym_end; | ||
| 7512 | |||
| 7513 | ph = &phdr[phnum - 1]; | ||
| 7514 | |||
| 7515 | ph->p_type = PT_DYNAMIC; | ||
| 7516 | ph->p_offset = dynamic->sh_offset; | ||
| 7517 | ph->p_vaddr = dynamic->sh_addr; | ||
| 7518 | ph->p_paddr = ph->p_vaddr; | ||
| 7519 | ph->p_filesz = dynamic->sh_size; | ||
| 7520 | ph->p_memsz = dynamic->sh_size; | ||
| 7521 | ph->p_flags = PF_R | PF_W; | ||
| 7522 | ph->p_align = dynamic->sh_addralign; | ||
| 7523 | |||
| 7524 | /* put GOT dynamic section address */ | ||
| 7525 | put32(got->data, dynamic->sh_addr); | ||
| 7526 | |||
| 7527 | /* compute the PLT */ | ||
| 7528 | plt->data_offset = 0; | ||
| 7529 | |||
| 7530 | /* first plt entry */ | ||
| 7531 | p = section_ptr_add(plt, 16); | ||
| 7532 | p[0] = 0xff; /* pushl got + 4 */ | ||
| 7533 | p[1] = 0x35; | ||
| 7534 | put32(p + 2, got->sh_addr + 4); | ||
| 7535 | p[6] = 0xff; /* jmp *(got + 8) */ | ||
| 7536 | p[7] = 0x25; | ||
| 7537 | put32(p + 8, got->sh_addr + 8); | ||
| 7538 | |||
| 7539 | /* relocation symbols in .dynsym and build PLT. */ | ||
| 7540 | plt_offset = 0; | ||
| 7541 | sym_end = (Elf32_Sym *)(dynsym->data + dynsym->data_offset); | ||
| 7542 | for(sym = (Elf32_Sym *)dynsym->data + 1; | ||
| 7543 | sym < sym_end; | ||
| 7544 | sym++) { | ||
| 7545 | type = ELF32_ST_TYPE(sym->st_info); | ||
| 7546 | if (sym->st_shndx == SHN_UNDEF) { | ||
| 7547 | if (type == STT_FUNC) { | ||
| 7548 | /* one more entry in PLT */ | ||
| 7549 | p = section_ptr_add(plt, 16); | ||
| 7550 | p[0] = 0xff; /* jmp *(got + x) */ | ||
| 7551 | p[1] = 0x25; | ||
| 7552 | put32(p + 2, got->sh_addr + sym->st_value); | ||
| 7553 | p[6] = 0x68; /* push $xxx */ | ||
| 7554 | put32(p + 7, plt_offset); | ||
| 7555 | p[11] = 0xe9; /* jmp plt_start */ | ||
| 7556 | put32(p + 12, -(plt->data_offset)); | ||
| 7557 | |||
| 7558 | /* patch symbol value to point to plt */ | ||
| 7559 | sym->st_value = plt->sh_addr + p - plt->data; | ||
| 7560 | |||
| 7561 | plt_offset += 8; | ||
| 7562 | } | ||
| 7563 | } else if (sym->st_shndx < SHN_LORESERVE) { | ||
| 7564 | /* do symbol relocation */ | ||
| 7565 | sym->st_value += sections[sym->st_shndx]->sh_addr; | ||
| 7566 | } | ||
| 7567 | } | ||
| 7568 | /* put dynamic section entries */ | ||
| 7569 | |||
| 7570 | dynamic->data_offset = saved_dynamic_data_offset; | ||
| 7571 | put_dt(dynamic, DT_HASH, dynsym->hash->sh_addr); | ||
| 7572 | put_dt(dynamic, DT_STRTAB, dynstr->sh_addr); | ||
| 7573 | put_dt(dynamic, DT_SYMTAB, dynsym->sh_addr); | ||
| 7574 | put_dt(dynamic, DT_STRSZ, dynstr->data_offset); | ||
| 7575 | put_dt(dynamic, DT_SYMENT, sizeof(Elf32_Sym)); | ||
| 7576 | put_dt(dynamic, DT_REL, rel_addr); | ||
| 7577 | put_dt(dynamic, DT_RELSZ, rel_size); | ||
| 7578 | put_dt(dynamic, DT_RELENT, sizeof(Elf32_Rel)); | ||
| 7579 | put_dt(dynamic, DT_NULL, 0); | ||
| 7580 | } | ||
| 7581 | |||
| 7582 | ehdr.e_phentsize = sizeof(Elf32_Phdr); | ||
| 7583 | ehdr.e_phnum = phnum; | ||
| 7584 | ehdr.e_phoff = sizeof(Elf32_Ehdr); | ||
| 7585 | } | ||
| 7586 | |||
| 7587 | /* all other sections come after */ | ||
| 7588 | for(i = 1; i < nb_sections; i++) { | ||
| 7589 | s = sections[i]; | ||
| 7590 | if (phnum > 0 && (s->sh_flags & SHF_ALLOC)) | ||
| 7591 | continue; | ||
| 7592 | section_order[sh_order_index++] = i; | ||
| 7593 | |||
| 7594 | file_offset = (file_offset + s->sh_addralign - 1) & | ||
| 7595 | ~(s->sh_addralign - 1); | ||
| 7596 | s->sh_offset = file_offset; | ||
| 7597 | if (s->sh_type != SHT_NOBITS) | ||
| 7598 | file_offset += s->sh_size; | ||
| 7599 | } | ||
| 7600 | |||
| 7601 | /* if building executable or DLL, then relocate each section | ||
| 7602 | except the GOT which is already relocated */ | ||
| 7603 | if (file_type != TCC_OUTPUT_OBJ) { | ||
| 7604 | relocate_syms(0); | ||
| 7605 | |||
| 7606 | /* relocate sections */ | ||
| 7607 | /* XXX: ignore sections with allocated relocations ? */ | ||
| 7608 | for(i = 1; i < nb_sections; i++) { | ||
| 7609 | s = sections[i]; | ||
| 7610 | if (s->reloc && s != got) | ||
| 7611 | relocate_section(s1, s); | ||
| 7612 | } | ||
| 7613 | |||
| 7614 | /* relocate relocation entries if the relocation tables are | ||
| 7615 | allocated in the executable */ | ||
| 7616 | for(i = 1; i < nb_sections; i++) { | ||
| 7617 | s = sections[i]; | ||
| 7618 | if ((s->sh_flags & SHF_ALLOC) && | ||
| 7619 | s->sh_type == SHT_REL) { | ||
| 7620 | relocate_rel(s); | ||
| 7621 | } | ||
| 7622 | } | ||
| 7623 | |||
| 7624 | /* get entry point address */ | ||
| 7625 | if (file_type == TCC_OUTPUT_EXE) | ||
| 7626 | ehdr.e_entry = get_elf_sym_val("_start"); | ||
| 7627 | else | ||
| 7628 | ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */ | ||
| 7629 | } | ||
| 7630 | |||
| 7631 | sort_syms(symtab_section); | ||
| 7632 | |||
| 7633 | /* align to 4 */ | ||
| 7634 | file_offset = (file_offset + 3) & -4; | ||
| 7635 | |||
| 7636 | /* fill header */ | ||
| 7637 | ehdr.e_ident[0] = ELFMAG0; | ||
| 7638 | ehdr.e_ident[1] = ELFMAG1; | ||
| 7639 | ehdr.e_ident[2] = ELFMAG2; | ||
| 7640 | ehdr.e_ident[3] = ELFMAG3; | ||
| 7641 | ehdr.e_ident[4] = ELFCLASS32; | ||
| 7642 | ehdr.e_ident[5] = ELFDATA2LSB; | ||
| 7643 | ehdr.e_ident[6] = EV_CURRENT; | ||
| 7644 | switch(file_type) { | ||
| 7645 | default: | ||
| 7646 | case TCC_OUTPUT_EXE: | ||
| 7647 | ehdr.e_type = ET_EXEC; | ||
| 7648 | break; | ||
| 7649 | case TCC_OUTPUT_DLL: | ||
| 7650 | ehdr.e_type = ET_DYN; | ||
| 7651 | break; | ||
| 7652 | case TCC_OUTPUT_OBJ: | ||
| 7653 | ehdr.e_type = ET_REL; | ||
| 7654 | break; | ||
| 7655 | } | ||
| 7656 | ehdr.e_machine = EM_386; | ||
| 7657 | ehdr.e_version = EV_CURRENT; | ||
| 7658 | ehdr.e_shoff = file_offset; | ||
| 7659 | ehdr.e_ehsize = sizeof(Elf32_Ehdr); | ||
| 7660 | ehdr.e_shentsize = sizeof(Elf32_Shdr); | ||
| 7661 | ehdr.e_shnum = shnum; | ||
| 7662 | ehdr.e_shstrndx = shnum - 1; | ||
| 7663 | |||
| 7664 | /* write elf file */ | ||
| 7665 | if (file_type == TCC_OUTPUT_OBJ) | ||
| 7666 | mode = 0666; | ||
| 7667 | else | ||
| 7668 | mode = 0777; | ||
| 7669 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode); | ||
| 7670 | if (fd < 0) | ||
| 7671 | error("could not write '%s'", filename); | ||
| 7672 | |||
| 7673 | f = fdopen(fd, "w"); | ||
| 7674 | fwrite(&ehdr, 1, sizeof(Elf32_Ehdr), f); | ||
| 7675 | fwrite(phdr, 1, phnum * sizeof(Elf32_Phdr), f); | ||
| 7676 | offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr); | ||
| 7677 | for(i=1;i<nb_sections;i++) { | ||
| 7678 | s = sections[section_order[i]]; | ||
| 7679 | if (s->sh_type != SHT_NOBITS) { | ||
| 7680 | while (offset < s->sh_offset) { | ||
| 7681 | fputc(0, f); | ||
| 7682 | offset++; | ||
| 7683 | } | ||
| 7684 | size = s->sh_size; | ||
| 7685 | fwrite(s->data, 1, size, f); | ||
| 7686 | offset += size; | ||
| 7687 | } | ||
| 7688 | } | ||
| 7689 | while (offset < ehdr.e_shoff) { | ||
| 7690 | fputc(0, f); | ||
| 7691 | offset++; | ||
| 7692 | } | ||
| 7693 | |||
| 7694 | /* output section headers */ | ||
| 7695 | for(i=0;i<nb_sections;i++) { | ||
| 7696 | sh = &shdr; | ||
| 7697 | memset(sh, 0, sizeof(Elf32_Shdr)); | ||
| 7698 | s = sections[i]; | ||
| 7699 | if (s) { | ||
| 7700 | sh->sh_name = s->sh_name; | ||
| 7701 | sh->sh_type = s->sh_type; | ||
| 7702 | sh->sh_flags = s->sh_flags; | ||
| 7703 | sh->sh_entsize = s->sh_entsize; | ||
| 7704 | sh->sh_info = s->sh_info; | ||
| 7705 | if (s->link) | ||
| 7706 | sh->sh_link = s->link->sh_num; | ||
| 7707 | sh->sh_addralign = s->sh_addralign; | ||
| 7708 | sh->sh_addr = s->sh_addr; | ||
| 7709 | sh->sh_offset = s->sh_offset; | ||
| 7710 | sh->sh_size = s->sh_size; | ||
| 7711 | } | ||
| 7712 | fwrite(sh, 1, sizeof(Elf32_Shdr), f); | ||
| 7713 | } | ||
| 7714 | fclose(f); | ||
| 7715 | |||
| 7716 | free(section_order); | ||
| 7717 | free(phdr); | ||
| 7718 | return 0; | ||
| 7719 | } | ||
| 7720 | |||
| 7721 | static void *load_data(int fd, unsigned long file_offset, unsigned long size) | ||
| 7722 | { | ||
| 7723 | void *data; | ||
| 7724 | |||
| 7725 | data = malloc(size); | ||
| 7726 | if (!data) | ||
| 7727 | error("memory full"); | ||
| 7728 | lseek(fd, file_offset, SEEK_SET); | ||
| 7729 | read(fd, data, size); | ||
| 7730 | return data; | ||
| 7731 | } | ||
| 7732 | |||
| 7733 | typedef struct SectionMergeInfo { | ||
| 7734 | Section *s; /* corresponding existing section */ | ||
| 7735 | unsigned long offset; /* offset of the new section in the existing section */ | ||
| 7736 | int new_section; /* true if section 's' was added */ | ||
| 7737 | } SectionMergeInfo; | ||
| 7738 | |||
| 7739 | /* load an object file and merge it with current files */ | ||
| 7740 | /* XXX: handle correctly stab (debug) info */ | ||
| 7741 | static int tcc_load_object_file(TCCState *s1, | ||
| 7742 | int fd, unsigned long file_offset) | ||
| 7743 | { | ||
| 7744 | Elf32_Ehdr ehdr; | ||
| 7745 | Elf32_Shdr *shdr, *sh; | ||
| 7746 | int size, i, j, offset, offseti, nb_syms, sym_index; | ||
| 7747 | unsigned char *strsec, *strtab; | ||
| 7748 | int *old_to_new_syms; | ||
| 7749 | char *sh_name, *name; | ||
| 7750 | SectionMergeInfo *sm_table, *sm; | ||
| 7751 | Elf32_Sym *sym, *symtab; | ||
| 7752 | Elf32_Rel *rel, *rel_end; | ||
| 7753 | Section *s; | ||
| 7754 | |||
| 7755 | if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) | ||
| 7756 | goto fail; | ||
| 7757 | if (ehdr.e_ident[0] != ELFMAG0 || | ||
| 7758 | ehdr.e_ident[1] != ELFMAG1 || | ||
| 7759 | ehdr.e_ident[2] != ELFMAG2 || | ||
| 7760 | ehdr.e_ident[3] != ELFMAG3) | ||
| 7761 | goto fail; | ||
| 7762 | /* test if object file */ | ||
| 7763 | if (ehdr.e_type != ET_REL) | ||
| 7764 | goto fail; | ||
| 7765 | /* test CPU specific stuff */ | ||
| 7766 | if (ehdr.e_ident[5] != ELFDATA2LSB || | ||
| 7767 | ehdr.e_machine != EM_386) { | ||
| 7768 | fail: | ||
| 7769 | error("invalid object file"); | ||
| 7770 | } | ||
| 7771 | /* read sections */ | ||
| 7772 | shdr = load_data(fd, file_offset + ehdr.e_shoff, | ||
| 7773 | sizeof(Elf32_Shdr) * ehdr.e_shnum); | ||
| 7774 | sm_table = malloc(sizeof(SectionMergeInfo) * ehdr.e_shnum); | ||
| 7775 | if (!sm_table) | ||
| 7776 | error("memory full"); | ||
| 7777 | memset(sm_table, 0, sizeof(SectionMergeInfo) * ehdr.e_shnum); | ||
| 7778 | |||
| 7779 | /* load section names */ | ||
| 7780 | sh = &shdr[ehdr.e_shstrndx]; | ||
| 7781 | strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size); | ||
| 7782 | |||
| 7783 | /* load symtab and strtab */ | ||
| 7784 | symtab = NULL; | ||
| 7785 | strtab = NULL; | ||
| 7786 | nb_syms = 0; | ||
| 7787 | for(i = 1; i < ehdr.e_shnum; i++) { | ||
| 7788 | sh = &shdr[i]; | ||
| 7789 | if (sh->sh_type == SHT_SYMTAB) { | ||
| 7790 | if (symtab) | ||
| 7791 | error("object must contain only one symtab"); | ||
| 7792 | nb_syms = sh->sh_size / sizeof(Elf32_Sym); | ||
| 7793 | symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size); | ||
| 7794 | sm_table[i].s = symtab_section; | ||
| 7795 | |||
| 7796 | /* now load strtab */ | ||
| 7797 | sh = &shdr[sh->sh_link]; | ||
| 7798 | strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size); | ||
| 7799 | } | ||
| 7800 | } | ||
| 7801 | |||
| 7802 | /* now examine each section and try to merge its content with the | ||
| 7803 | ones in memory */ | ||
| 7804 | for(i = 1; i < ehdr.e_shnum; i++) { | ||
| 7805 | /* no need to examine section name strtab */ | ||
| 7806 | if (i == ehdr.e_shstrndx) | ||
| 7807 | continue; | ||
| 7808 | sh = &shdr[i]; | ||
| 7809 | sh_name = strsec + sh->sh_name; | ||
| 7810 | /* ignore sections types we do not handle */ | ||
| 7811 | if (sh->sh_type != SHT_PROGBITS && | ||
| 7812 | sh->sh_type != SHT_REL && | ||
| 7813 | sh->sh_type != SHT_NOBITS) | ||
| 7814 | continue; | ||
| 7815 | if (sh->sh_addralign < 1) | ||
| 7816 | sh->sh_addralign = 1; | ||
| 7817 | /* find corresponding section, if any */ | ||
| 7818 | for(j = 1; j < nb_sections;j++) { | ||
| 7819 | s = sections[j]; | ||
| 7820 | if (!strcmp(s->name, sh_name)) | ||
| 7821 | goto found; | ||
| 7822 | } | ||
| 7823 | /* not found: create new section */ | ||
| 7824 | s = new_section(sh_name, sh->sh_type, sh->sh_flags); | ||
| 7825 | /* take as much info as possible from the section. sh_link and | ||
| 7826 | sh_info will be updated later */ | ||
| 7827 | s->sh_addralign = sh->sh_addralign; | ||
| 7828 | s->sh_entsize = sh->sh_entsize; | ||
| 7829 | sm_table[i].new_section = 1; | ||
| 7830 | found: | ||
| 7831 | if (sh->sh_type != s->sh_type) | ||
| 7832 | goto fail; | ||
| 7833 | |||
| 7834 | /* align start of section */ | ||
| 7835 | offset = s->data_offset; | ||
| 7836 | size = sh->sh_addralign - 1; | ||
| 7837 | offset = (offset + size) & ~size; | ||
| 7838 | if (sh->sh_addralign > s->sh_addralign) | ||
| 7839 | s->sh_addralign = sh->sh_addralign; | ||
| 7840 | s->data_offset = offset; | ||
| 7841 | sm_table[i].offset = offset; | ||
| 7842 | sm_table[i].s = s; | ||
| 7843 | /* concatenate sections */ | ||
| 7844 | size = sh->sh_size; | ||
| 7845 | if (sh->sh_type != SHT_NOBITS) { | ||
| 7846 | unsigned char *ptr; | ||
| 7847 | lseek(fd, file_offset + sh->sh_offset, SEEK_SET); | ||
| 7848 | ptr = section_ptr(s, size); | ||
| 7849 | read(fd, ptr, size); | ||
| 7850 | } | ||
| 7851 | s->data_offset += size; | ||
| 7852 | } | ||
| 7853 | |||
| 7854 | /* second short pass to update sh_link and sh_info fields of new | ||
| 7855 | sections */ | ||
| 7856 | sm = sm_table; | ||
| 7857 | for(i = 1; i < ehdr.e_shnum; i++) { | ||
| 7858 | s = sm_table[i].s; | ||
| 7859 | if (!s || !sm_table[i].new_section) | ||
| 7860 | continue; | ||
| 7861 | sh = &shdr[i]; | ||
| 7862 | if (sh->sh_link > 0) | ||
| 7863 | s->link = sm_table[sh->sh_link].s; | ||
| 7864 | if (sh->sh_type == SHT_REL) { | ||
| 7865 | s->sh_info = sm_table[sh->sh_info].s->sh_num; | ||
| 7866 | /* update backward link */ | ||
| 7867 | sections[s->sh_info]->reloc = s; | ||
| 7868 | } | ||
| 7869 | } | ||
| 7870 | |||
| 7871 | /* resolve symbols */ | ||
| 7872 | old_to_new_syms = malloc(nb_syms * sizeof(int)); | ||
| 7873 | if (!old_to_new_syms) | ||
| 7874 | error("memory full"); | ||
| 7875 | memset(old_to_new_syms, 0, nb_syms * sizeof(int)); | ||
| 7876 | sym = symtab + 1; | ||
| 7877 | for(i = 1; i < nb_syms; i++, sym++) { | ||
| 7878 | if (sym->st_shndx != SHN_UNDEF && | ||
| 7879 | sym->st_shndx < SHN_LORESERVE) { | ||
| 7880 | sm = &sm_table[sym->st_shndx]; | ||
| 7881 | /* if no corresponding section added, no need to add symbol */ | ||
| 7882 | if (!sm->s) | ||
| 7883 | continue; | ||
| 7884 | /* convert section number */ | ||
| 7885 | sym->st_shndx = sm->s->sh_num; | ||
| 7886 | /* offset value */ | ||
| 7887 | sym->st_value += sm->offset; | ||
| 7888 | } | ||
| 7889 | /* add symbol */ | ||
| 7890 | name = strtab + sym->st_name; | ||
| 7891 | sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size, | ||
| 7892 | sym->st_info, sym->st_shndx, name); | ||
| 7893 | old_to_new_syms[i] = sym_index; | ||
| 7894 | } | ||
| 7895 | |||
| 7896 | /* third pass to patch relocation entries */ | ||
| 7897 | for(i = 1; i < ehdr.e_shnum; i++) { | ||
| 7898 | s = sm_table[i].s; | ||
| 7899 | if (!s) | ||
| 7900 | continue; | ||
| 7901 | sh = &shdr[i]; | ||
| 7902 | offset = sm_table[i].offset; | ||
| 7903 | switch(s->sh_type) { | ||
| 7904 | case SHT_REL: | ||
| 7905 | /* take relocation offset information */ | ||
| 7906 | offseti = sm_table[sh->sh_info].offset; | ||
| 7907 | rel_end = (Elf32_Rel *)(s->data + s->data_offset); | ||
| 7908 | for(rel = (Elf32_Rel *)(s->data + offset); | ||
| 7909 | rel < rel_end; | ||
| 7910 | rel++) { | ||
| 7911 | int type; | ||
| 7912 | unsigned sym_index; | ||
| 7913 | /* convert symbol index */ | ||
| 7914 | type = ELF32_R_TYPE(rel->r_info); | ||
| 7915 | sym_index = ELF32_R_SYM(rel->r_info); | ||
| 7916 | /* NOTE: only one symtab assumed */ | ||
| 7917 | if (sym_index >= nb_syms) | ||
| 7918 | goto invalid_reloc; | ||
| 7919 | sym_index = old_to_new_syms[sym_index]; | ||
| 7920 | if (!sym_index) { | ||
| 7921 | invalid_reloc: | ||
| 7922 | error("Invalid relocation entry"); | ||
| 7923 | } | ||
| 7924 | rel->r_info = ELF32_R_INFO(sym_index, type); | ||
| 7925 | /* offset the relocation offset */ | ||
| 7926 | rel->r_offset += offseti; | ||
| 7927 | } | ||
| 7928 | break; | ||
| 7929 | default: | ||
| 7930 | break; | ||
| 7931 | } | ||
| 7932 | } | ||
| 7933 | free(symtab); | ||
| 7934 | free(strtab); | ||
| 7935 | free(old_to_new_syms); | ||
| 7936 | free(sm_table); | ||
| 7937 | free(shdr); | ||
| 7938 | return 0; | ||
| 7939 | } | ||
| 7940 | |||
| 7941 | #define ARMAG "!<arch>\012" /* For COFF and a.out archives */ | ||
| 7942 | |||
| 7943 | typedef struct ArchiveHeader { | ||
| 7944 | char ar_name[16]; /* name of this member */ | ||
| 7945 | char ar_date[12]; /* file mtime */ | ||
| 7946 | char ar_uid[6]; /* owner uid; printed as decimal */ | ||
| 7947 | char ar_gid[6]; /* owner gid; printed as decimal */ | ||
| 7948 | char ar_mode[8]; /* file mode, printed as octal */ | ||
| 7949 | char ar_size[10]; /* file size, printed as decimal */ | ||
| 7950 | char ar_fmag[2]; /* should contain ARFMAG */ | ||
| 7951 | } ArchiveHeader; | ||
| 7952 | |||
| 7953 | /* load a '.a' file */ | ||
| 7954 | static int tcc_load_archive(TCCState *s1, int fd) | ||
| 7955 | { | ||
| 7956 | ArchiveHeader hdr; | ||
| 7957 | char ar_size[11]; | ||
| 7958 | char ar_name[17]; | ||
| 7959 | char magic[8]; | ||
| 7960 | int size, len, i; | ||
| 7961 | unsigned long file_offset; | ||
| 7962 | |||
| 7963 | /* skip magic which was already checked */ | ||
| 7964 | read(fd, magic, sizeof(magic)); | ||
| 7965 | |||
| 7966 | for(;;) { | ||
| 7967 | len = read(fd, &hdr, sizeof(hdr)); | ||
| 7968 | if (len == 0) | ||
| 7969 | break; | ||
| 7970 | if (len != sizeof(hdr)) | ||
| 7971 | error("invalid archive"); | ||
| 7972 | memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size)); | ||
| 7973 | ar_size[sizeof(hdr.ar_size)] = '\0'; | ||
| 7974 | size = strtol(ar_size, NULL, 0); | ||
| 7975 | memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name)); | ||
| 7976 | for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) { | ||
| 7977 | if (ar_name[i] != ' ') | ||
| 7978 | break; | ||
| 7979 | } | ||
| 7980 | ar_name[i + 1] = '\0'; | ||
| 7981 | // printf("name='%s' size=%d %s\n", ar_name, size, ar_size); | ||
| 7982 | file_offset = lseek(fd, 0, SEEK_CUR); | ||
| 7983 | if (!strcmp(ar_name, "/") || | ||
| 7984 | !strcmp(ar_name, "//") || | ||
| 7985 | !strcmp(ar_name, "__.SYMDEF") || | ||
| 7986 | !strcmp(ar_name, "__.SYMDEF/") || | ||
| 7987 | !strcmp(ar_name, "ARFILENAMES/")) { | ||
| 7988 | /* skip symbol table or archive names */ | ||
| 7989 | } else { | ||
| 7990 | tcc_load_object_file(s1, fd, file_offset); | ||
| 7991 | } | ||
| 7992 | /* align to even */ | ||
| 7993 | size = (size + 1) & ~1; | ||
| 7994 | lseek(fd, file_offset + size, SEEK_SET); | ||
| 7995 | } | ||
| 7996 | return 0; | ||
| 7997 | } | ||
| 7998 | |||
| 7999 | /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL | ||
| 8000 | is referenced by the user (so it should be added as DT_NEEDED in | ||
| 8001 | the generated ELF file) */ | ||
| 8002 | static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) | ||
| 8003 | { | ||
| 8004 | Elf32_Ehdr ehdr; | ||
| 8005 | Elf32_Shdr *shdr, *sh, *sh1; | ||
| 8006 | int i, nb_syms, nb_dts, sym_bind; | ||
| 8007 | Elf32_Sym *sym, *dynsym; | ||
| 8008 | Elf32_Dyn *dt, *dynamic; | ||
| 8009 | unsigned char *dynstr; | ||
| 8010 | const char *name, *soname, *p; | ||
| 8011 | DLLReference *dllref; | ||
| 8012 | |||
| 8013 | read(fd, &ehdr, sizeof(ehdr)); | ||
| 8014 | |||
| 8015 | /* test CPU specific stuff */ | ||
| 8016 | if (ehdr.e_ident[5] != ELFDATA2LSB || | ||
| 8017 | ehdr.e_machine != EM_386) | ||
| 8018 | error("bad architecture"); | ||
| 8019 | |||
| 8020 | /* read sections */ | ||
| 8021 | shdr = load_data(fd, ehdr.e_shoff, sizeof(Elf32_Shdr) * ehdr.e_shnum); | ||
| 8022 | |||
| 8023 | /* load dynamic section and dynamic symbols */ | ||
| 8024 | nb_syms = 0; | ||
| 8025 | nb_dts = 0; | ||
| 8026 | dynamic = NULL; | ||
| 8027 | dynsym = NULL; /* avoid warning */ | ||
| 8028 | dynstr = NULL; /* avoid warning */ | ||
| 8029 | for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) { | ||
| 8030 | switch(sh->sh_type) { | ||
| 8031 | case SHT_DYNAMIC: | ||
| 8032 | nb_dts = sh->sh_size / sizeof(Elf32_Dyn); | ||
| 8033 | dynamic = load_data(fd, sh->sh_offset, sh->sh_size); | ||
| 8034 | break; | ||
| 8035 | case SHT_DYNSYM: | ||
| 8036 | nb_syms = sh->sh_size / sizeof(Elf32_Sym); | ||
| 8037 | dynsym = load_data(fd, sh->sh_offset, sh->sh_size); | ||
| 8038 | sh1 = &shdr[sh->sh_link]; | ||
| 8039 | dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size); | ||
| 8040 | break; | ||
| 8041 | default: | ||
| 8042 | break; | ||
| 8043 | } | ||
| 8044 | } | ||
| 8045 | |||
| 8046 | /* compute the real library name */ | ||
| 8047 | soname = filename; | ||
| 8048 | p = strrchr(soname, '/'); | ||
| 8049 | if (p) | ||
| 8050 | soname = p + 1; | ||
| 8051 | |||
| 8052 | for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) { | ||
| 8053 | if (dt->d_tag == DT_SONAME) { | ||
| 8054 | soname = dynstr + dt->d_un.d_val; | ||
| 8055 | } | ||
| 8056 | } | ||
| 8057 | |||
| 8058 | /* if the dll is already loaded, do not load it */ | ||
| 8059 | for(i = 0; i < nb_loaded_dlls; i++) { | ||
| 8060 | dllref = loaded_dlls[i]; | ||
| 8061 | if (!strcmp(soname, dllref->name)) { | ||
| 8062 | /* but update level if needed */ | ||
| 8063 | if (level < dllref->level) | ||
| 8064 | dllref->level = level; | ||
| 8065 | goto the_end; | ||
| 8066 | } | ||
| 8067 | } | ||
| 8068 | |||
| 8069 | // printf("loading dll '%s'\n", soname); | ||
| 8070 | |||
| 8071 | /* add the dll and its level */ | ||
| 8072 | dllref = malloc(sizeof(DLLReference) + strlen(soname)); | ||
| 8073 | dllref->level = level; | ||
| 8074 | strcpy(dllref->name, soname); | ||
| 8075 | dynarray_add((void ***)&loaded_dlls, &nb_loaded_dlls, dllref); | ||
| 8076 | |||
| 8077 | /* add dynamic symbols in dynsym_section */ | ||
| 8078 | for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) { | ||
| 8079 | sym_bind = ELF32_ST_BIND(sym->st_info); | ||
| 8080 | if (sym_bind == STB_LOCAL) | ||
| 8081 | continue; | ||
| 8082 | name = dynstr + sym->st_name; | ||
| 8083 | add_elf_sym(dynsymtab_section, sym->st_value, sym->st_size, | ||
| 8084 | sym->st_info, sym->st_shndx, name); | ||
| 8085 | } | ||
| 8086 | |||
| 8087 | /* load all referenced DLLs */ | ||
| 8088 | for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) { | ||
| 8089 | switch(dt->d_tag) { | ||
| 8090 | case DT_NEEDED: | ||
| 8091 | name = dynstr + dt->d_un.d_val; | ||
| 8092 | for(i = 0; i < nb_loaded_dlls; i++) { | ||
| 8093 | dllref = loaded_dlls[i]; | ||
| 8094 | if (!strcmp(name, dllref->name)) | ||
| 8095 | goto already_loaded; | ||
| 8096 | } | ||
| 8097 | if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) | ||
| 8098 | error("referenced dll '%s' not found", name); | ||
| 8099 | already_loaded: | ||
| 8100 | break; | ||
| 8101 | } | ||
| 8102 | } | ||
| 8103 | the_end: | ||
| 8104 | free(shdr); | ||
| 8105 | return 0; | ||
| 8106 | } | ||
| 8107 | |||
| 8108 | /* return -2 if error and CH_EOF if eof */ | ||
| 8109 | static void ld_skipspaces(void) | ||
| 8110 | { | ||
| 8111 | while (ch == ' ' || ch == '\t' || ch == '\n') | ||
| 8112 | cinp(); | ||
| 8113 | } | ||
| 8114 | |||
| 8115 | static int ld_get_cmd(char *cmd, int cmd_size) | ||
| 8116 | { | ||
| 8117 | char *q; | ||
| 8118 | |||
| 8119 | ld_skipspaces(); | ||
| 8120 | if (ch == CH_EOF) | ||
| 8121 | return -1; | ||
| 8122 | q = cmd; | ||
| 8123 | for(;;) { | ||
| 8124 | if (!((ch >= 'a' && ch <= 'z') || | ||
| 8125 | (ch >= 'A' && ch <= 'Z') || | ||
| 8126 | (ch >= '0' && ch <= '9') || | ||
| 8127 | strchr("/.-_+=$:\\,~?*", ch))) | ||
| 8128 | break; | ||
| 8129 | if ((q - cmd) >= (cmd_size - 1)) | ||
| 8130 | return -2; | ||
| 8131 | *q++ = ch; | ||
| 8132 | cinp(); | ||
| 8133 | } | ||
| 8134 | *q = '\0'; | ||
| 8135 | return 0; | ||
| 8136 | } | ||
| 8137 | |||
| 8138 | /* interpret a subset of GNU ldscripts to handle the dummy libc.so | ||
| 8139 | files */ | ||
| 8140 | static int tcc_load_ldscript(TCCState *s1) | ||
| 8141 | { | ||
| 8142 | char cmd[64]; | ||
| 8143 | char filename[1024]; | ||
| 8144 | int ret; | ||
| 8145 | |||
| 8146 | inp(); | ||
| 8147 | cinp(); | ||
| 8148 | for(;;) { | ||
| 8149 | ret = ld_get_cmd(cmd, sizeof(cmd)); | ||
| 8150 | if (ret == CH_EOF) | ||
| 8151 | return 0; | ||
| 8152 | else if (ret < 0) | ||
| 8153 | return -1; | ||
| 8154 | // printf("cmd='%s'\n", cmd); | ||
| 8155 | if (!strcmp(cmd, "INPUT") || | ||
| 8156 | !strcmp(cmd, "GROUP")) { | ||
| 8157 | ld_skipspaces(); | ||
| 8158 | if (ch != '(') | ||
| 8159 | expect("("); | ||
| 8160 | cinp(); | ||
| 8161 | for(;;) { | ||
| 8162 | ld_get_cmd(filename, sizeof(filename)); | ||
| 8163 | tcc_add_file(s1, filename); | ||
| 8164 | ld_skipspaces(); | ||
| 8165 | if (ch == ',') { | ||
| 8166 | cinp(); | ||
| 8167 | } else if (ch == ')') { | ||
| 8168 | cinp(); | ||
| 8169 | break; | ||
| 8170 | } else if (ch == CH_EOF) { | ||
| 8171 | error("unexpected end of file"); | ||
| 8172 | } | ||
| 8173 | } | ||
| 8174 | } else { | ||
| 8175 | return -1; | ||
| 8176 | } | ||
| 8177 | } | ||
| 8178 | return 0; | ||
| 8179 | } | ||
| 8180 | |||
| 8181 | /* print the position in the source file of PC value 'pc' by reading | ||
| 8182 | the stabs debug information */ | ||
| 8183 | static void rt_printline(unsigned long wanted_pc) | ||
| 8184 | { | ||
| 8185 | Stab_Sym *sym, *sym_end; | ||
| 8186 | char func_name[128]; | ||
| 8187 | unsigned long func_addr, last_pc, pc; | ||
| 8188 | const char *incl_files[INCLUDE_STACK_SIZE]; | ||
| 8189 | int incl_index, len, last_line_num, i; | ||
| 8190 | const char *str, *p; | ||
| 8191 | 6671 | ||
| 8192 | func_name[0] = '\0'; | 6672 | func_name[0] = '\0'; |
| 8193 | func_addr = 0; | 6673 | func_addr = 0; |
| 8194 | incl_index = 0; | 6674 | incl_index = 0; |
| 6675 | last_func_name[0] = '\0'; | ||
| 8195 | last_pc = 0xffffffff; | 6676 | last_pc = 0xffffffff; |
| 8196 | last_line_num = 1; | 6677 | last_line_num = 1; |
| 8197 | sym = (Stab_Sym *)stab_section->data + 1; | 6678 | sym = (Stab_Sym *)stab_section->data + 1; |
| @@ -8225,6 +6706,8 @@ static void rt_printline(unsigned long wanted_pc) · 27 unmodified lines | |||
| 8225 | goto found; | 6706 | goto found; |
| 8226 | last_pc = pc; | 6707 | last_pc = pc; |
| 8227 | last_line_num = sym->n_desc; | 6708 | last_line_num = sym->n_desc; |
| 6709 | /* XXX: slow! */ | ||
| 6710 | strcpy(last_func_name, func_name); | ||
| 8228 | break; | 6711 | break; |
| 8229 | /* include files */ | 6712 | /* include files */ |
| 8230 | case N_BINCL: | 6713 | case N_BINCL: |
| @@ -8263,8 +6746,8 @@ static void rt_printline(unsigned long wanted_pc) · 32 unmodified lines | |||
| 8263 | fprintf(stderr, "%s:%d: ", | 6746 | fprintf(stderr, "%s:%d: ", |
| 8264 | incl_files[incl_index - 1], last_line_num); | 6747 | incl_files[incl_index - 1], last_line_num); |
| 8265 | } | 6748 | } |
| 8266 | if (func_name[0] != '\0') { | 6749 | if (last_func_name[0] != '\0') { |
| 8267 | fprintf(stderr, "in function '%s()': ", func_name); | 6750 | fprintf(stderr, "in function '%s()': ", last_func_name); |
| 8268 | } | 6751 | } |
| 8269 | } | 6752 | } |
| 8270 | 6753 | ||
| @@ -8329,18 +6812,25 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 58 unmodified lines | |||
| 8329 | { | 6812 | { |
| 8330 | Section *s; | 6813 | Section *s; |
| 8331 | int (*prog_main)(int, char **); | 6814 | int (*prog_main)(int, char **); |
| 8332 | void (*bound_init)(void); | ||
| 8333 | int i; | 6815 | int i; |
| 8334 | 6816 | ||
| 8335 | tcc_add_runtime(s1); | 6817 | tcc_add_runtime(s1); |
| 8336 | 6818 | ||
| 8337 | relocate_common_syms(); | 6819 | relocate_common_syms(); |
| 8338 | 6820 | ||
| 8339 | /* compute relocation address : section are relocated in place */ | 6821 | /* compute relocation address : section are relocated in place. We |
| 6822 | also alloc the bss space */ | ||
| 8340 | for(i = 1; i < nb_sections; i++) { | 6823 | for(i = 1; i < nb_sections; i++) { |
| 8341 | s = sections[i]; | 6824 | s = sections[i]; |
| 8342 | if (s->sh_flags & SHF_ALLOC) | 6825 | if (s->sh_flags & SHF_ALLOC) { |
| 8343 | s->sh_addr = (unsigned long)s->data; | 6826 | void *data; |
| 6827 | if (s->sh_type == SHT_NOBITS) { | ||
| 6828 | data = tcc_mallocz(s->data_offset); | ||
| 6829 | } else { | ||
| 6830 | data = s->data; | ||
| 6831 | } | ||
| 6832 | s->sh_addr = (unsigned long)data; | ||
| 6833 | } | ||
| 8344 | } | 6834 | } |
| 8345 | 6835 | ||
| 8346 | relocate_syms(1); | 6836 | relocate_syms(1); |
| @@ -8374,6 +6864,13 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 27 unmodified lines | |||
| 8374 | 6864 | ||
| 8375 | #ifdef CONFIG_TCC_BCHECK | 6865 | #ifdef CONFIG_TCC_BCHECK |
| 8376 | if (do_bounds_check) { | 6866 | if (do_bounds_check) { |
| 6867 | void (*bound_init)(void); | ||
| 6868 | void **bound_error_func; | ||
| 6869 | |||
| 6870 | /* set error function */ | ||
| 6871 | bound_error_func = (void **)get_elf_sym_val("__bound_error_func"); | ||
| 6872 | *bound_error_func = rt_error; | ||
| 6873 | |||
| 8377 | /* XXX: use .init section so that it also work in binary ? */ | 6874 | /* XXX: use .init section so that it also work in binary ? */ |
| 8378 | bound_init = (void *)get_elf_sym_val("__bound_init"); | 6875 | bound_init = (void *)get_elf_sym_val("__bound_init"); |
| 8379 | bound_init(); | 6876 | bound_init(); |
| @@ -8387,15 +6884,15 @@ TCCState *tcc_new(void) · 7 unmodified lines | |||
| 8387 | char *p, *r; | 6884 | char *p, *r; |
| 8388 | TCCState *s; | 6885 | TCCState *s; |
| 8389 | 6886 | ||
| 8390 | s = malloc(sizeof(TCCState)); | 6887 | s = tcc_malloc(sizeof(TCCState)); |
| 8391 | if (!s) | 6888 | if (!s) |
| 8392 | return NULL; | 6889 | return NULL; |
| 8393 | s->output_type = TCC_OUTPUT_MEMORY; | 6890 | s->output_type = TCC_OUTPUT_MEMORY; |
| 8394 | 6891 | ||
| 8395 | /* default include paths */ | 6892 | /* default include paths */ |
| 8396 | tcc_add_include_path(s, "/usr/include"); | 6893 | tcc_add_sysinclude_path(s, "/usr/local/include"); |
| 8397 | tcc_add_include_path(s, CONFIG_TCC_PREFIX "/lib/tcc/include"); | 6894 | tcc_add_sysinclude_path(s, "/usr/include"); |
| 8398 | tcc_add_include_path(s, "/usr/local/include"); | 6895 | tcc_add_sysinclude_path(s, CONFIG_TCC_PREFIX "/lib/tcc/include"); |
| 8399 | 6896 | ||
| 8400 | /* add all tokens */ | 6897 | /* add all tokens */ |
| 8401 | tok_ident = TOK_IDENT; | 6898 | tok_ident = TOK_IDENT; |
| @@ -8443,20 +6940,27 @@ TCCState *tcc_new(void) · 41 unmodified lines | |||
| 8443 | 6940 | ||
| 8444 | void tcc_delete(TCCState *s) | 6941 | void tcc_delete(TCCState *s) |
| 8445 | { | 6942 | { |
| 8446 | free(s); | 6943 | tcc_free(s); |
| 8447 | } | 6944 | } |
| 8448 | 6945 | ||
| 8449 | int tcc_add_include_path(TCCState *s, const char *pathname) | 6946 | int tcc_add_include_path(TCCState *s, const char *pathname) |
| 8450 | { | 6947 | { |
| 8451 | char *pathname1; | 6948 | char *pathname1; |
| 8452 | 6949 | ||
| 8453 | pathname1 = strdup(pathname); | 6950 | pathname1 = tcc_strdup(pathname); |
| 8454 | if (!pathname1) | ||
| 8455 | return -1; | ||
| 8456 | dynarray_add((void ***)&include_paths, &nb_include_paths, pathname1); | 6951 | dynarray_add((void ***)&include_paths, &nb_include_paths, pathname1); |
| 8457 | return 0; | 6952 | return 0; |
| 8458 | } | 6953 | } |
| 8459 | 6954 | ||
| 6955 | int tcc_add_sysinclude_path(TCCState *s, const char *pathname) | ||
| 6956 | { | ||
| 6957 | char *pathname1; | ||
| 6958 | |||
| 6959 | pathname1 = tcc_strdup(pathname); | ||
| 6960 | dynarray_add((void ***)&sysinclude_paths, &nb_sysinclude_paths, pathname1); | ||
| 6961 | return 0; | ||
| 6962 | } | ||
| 6963 | |||
| 8460 | static int tcc_add_file_internal(TCCState *s, const char *filename, int flags) | 6964 | static int tcc_add_file_internal(TCCState *s, const char *filename, int flags) |
| 8461 | { | 6965 | { |
| 8462 | const char *ext; | 6966 | const char *ext; |
| @@ -8526,9 +7030,7 @@ int tcc_add_library_path(TCCState *s, const char *pathname) · 63 unmodified lines | |||
| 8526 | { | 7030 | { |
| 8527 | char *pathname1; | 7031 | char *pathname1; |
| 8528 | 7032 | ||
| 8529 | pathname1 = strdup(pathname); | 7033 | pathname1 = tcc_strdup(pathname); |
| 8530 | if (!pathname1) | ||
| 8531 | return -1; | ||
| 8532 | dynarray_add((void ***)&library_paths, &nb_library_paths, pathname1); | 7034 | dynarray_add((void ***)&library_paths, &nb_library_paths, pathname1); |
| 8533 | return 0; | 7035 | return 0; |
| 8534 | } | 7036 | } |
| @@ -8636,7 +7138,7 @@ int tcc_set_output_type(TCCState *s, int output_type) · 101 unmodified lines | |||
| 8636 | 7138 | ||
| 8637 | void help(void) | 7139 | void help(void) |
| 8638 | { | 7140 | { |
| 8639 | printf("tcc version 0.9.9 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" | 7141 | printf("tcc version 0.9.10pre1 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" |
| 8640 | "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n" | 7142 | "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n" |
| 8641 | " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n" | 7143 | " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n" |
| 8642 | " [--] infile1 [infile2... --] [infile_args...]\n" | 7144 | " [--] infile1 [infile2... --] [infile_args...]\n" |
| @@ -8659,7 +7161,7 @@ void help(void) · 16 unmodified lines | |||
| 8659 | #endif | 7161 | #endif |
| 8660 | "Linker options:\n" | 7162 | "Linker options:\n" |
| 8661 | " -Ldir add library path 'dir'\n" | 7163 | " -Ldir add library path 'dir'\n" |
| 8662 | " -llib link with dynamic library 'lib'\n" | 7164 | " -llib link with dynamic or static library 'lib'\n" |
| 8663 | " -shared generate a shared library\n" | 7165 | " -shared generate a shared library\n" |
| 8664 | " -static static linking\n" | 7166 | " -static static linking\n" |
| 8665 | ); | 7167 | ); |
| @@ -8750,12 +7252,6 @@ int main(int argc, char **argv) · 84 unmodified lines | |||
| 8750 | if (outfile && output_type == TCC_OUTPUT_MEMORY) | 7252 | if (outfile && output_type == TCC_OUTPUT_MEMORY) |
| 8751 | output_type = TCC_OUTPUT_EXE; | 7253 | output_type = TCC_OUTPUT_EXE; |
| 8752 | 7254 | ||
| 8753 | /* warning if not supported features */ | ||
| 8754 | #ifdef CONFIG_TCC_BCHECK | ||
| 8755 | if (do_bounds_check && output_type != TCC_OUTPUT_MEMORY) | ||
| 8756 | warning("bounds checking is currently only supported for in-memory execution"); | ||
| 8757 | #endif | ||
| 8758 | |||
| 8759 | tcc_set_output_type(s, output_type); | 7255 | tcc_set_output_type(s, output_type); |
| 8760 | 7256 | ||
| 8761 | tcc_add_file(s, argv[optind]); | 7257 | tcc_add_file(s, argv[optind]); |
| @@ -8781,6 +7277,9 @@ int main(int argc, char **argv) · 19 unmodified lines | |||
| 8781 | if (do_bench) { | 7277 | if (do_bench) { |
| 8782 | printf("total: %d idents, %d lines, %d bytes\n", | 7278 | printf("total: %d idents, %d lines, %d bytes\n", |
| 8783 | tok_ident - TOK_IDENT, total_lines, total_bytes); | 7279 | tok_ident - TOK_IDENT, total_lines, total_bytes); |
| 7280 | #ifdef MEM_DEBUG | ||
| 7281 | printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size); | ||
| 7282 | #endif | ||
| 8784 | } | 7283 | } |
| 8785 | 7284 | ||
| 8786 | if (s->output_type != TCC_OUTPUT_MEMORY) { | 7285 | if (s->output_type != TCC_OUTPUT_MEMORY) { |