Subversion Repositories Kolibri OS

Rev

Rev 647 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 647 Rev 6429
Line 16... Line 16...
16
 *
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
20
 */
-
 
21
 
21
#include "coff.h"
22
#include "tcc.h"
Line 22... Line 23...
22
 
23
 
23
#define MAXNSCNS 255		/* MAXIMUM NUMBER OF SECTIONS         */
24
#define MAXNSCNS 255		/* MAXIMUM NUMBER OF SECTIONS         */
24
#define MAX_STR_TABLE 1000000
25
#define MAX_STR_TABLE 1000000
Line 35... Line 36...
35
int LineNoFilePtr[MAX_FUNCS];
36
int LineNoFilePtr[MAX_FUNCS];
36
int EndAddress[MAX_FUNCS];
37
int EndAddress[MAX_FUNCS];
37
int LastLineNo[MAX_FUNCS];
38
int LastLineNo[MAX_FUNCS];
38
int FuncEntries[MAX_FUNCS];
39
int FuncEntries[MAX_FUNCS];
Line 39... Line 40...
39
 
40
 
40
BOOL OutputTheSection(Section * sect);
41
int OutputTheSection(Section * sect);
41
short int GetCoffFlags(const char *s);
42
short int GetCoffFlags(const char *s);
42
void SortSymbolTable(void);
43
void SortSymbolTable(void);
Line 43... Line 44...
43
Section *FindSection(TCCState * s1, const char *sname);
44
Section *FindSection(TCCState * s1, const char *sname);
Line 71... Line 72...
71
    int dummy2;
72
    int dummy2;
72
    int dummy3;
73
    int dummy3;
73
    unsigned short dummy4;
74
    unsigned short dummy4;
74
} AUXEF;
75
} AUXEF;
Line 75... Line 76...
75
 
76
 
76
int tcc_output_coff(TCCState *s1, FILE *f)
77
ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
77
{
78
{
78
    Section *tcc_sect;
79
    Section *tcc_sect;
79
    SCNHDR *coff_sec;
80
    SCNHDR *coff_sec;
80
    int file_pointer;
81
    int file_pointer;
81
    char *Coff_str_table, *pCoff_str_table;
82
    char *Coff_str_table, *pCoff_str_table;
82
    int CoffTextSectionNo, coff_nb_syms;
83
    int CoffTextSectionNo, coff_nb_syms;
83
    FILHDR file_hdr;		/* FILE HEADER STRUCTURE              */
84
    FILHDR file_hdr;		/* FILE HEADER STRUCTURE              */
84
    Section *stext, *sdata, *sbss;
85
    Section *stext, *sdata, *sbss;
Line -... Line 86...
-
 
86
    int i, NSectionsToOutput = 0;
-
 
87
 
85
    int i, NSectionsToOutput = 0;
88
    Coff_str_table = pCoff_str_table = NULL;
86
 
89
 
87
    stext = FindSection(s1, ".text");
90
    stext = FindSection(s1, ".text");
Line 88... Line 91...
88
    sdata = FindSection(s1, ".data");
91
    sdata = FindSection(s1, ".data");
Line 181... Line 184...
181
	tcc_sect = s1->sections[i];
184
	tcc_sect = s1->sections[i];
Line 182... Line 185...
182
 
185
 
183
	coff_sec->s_nlnno = 0;
186
	coff_sec->s_nlnno = 0;
Line 184... Line 187...
184
	coff_sec->s_lnnoptr = 0;
187
	coff_sec->s_lnnoptr = 0;
185
 
188
 
Line 186... Line 189...
186
	if (do_debug && tcc_sect == stext) {
189
	if (s1->do_debug && tcc_sect == stext) {
187
	    // count how many line nos data
190
	    // count how many line nos data
Line 309... Line 312...
309
 
312
 
Line 310... Line 313...
310
    }
313
    }
Line 311... Line 314...
311
 
314
 
312
    file_hdr.f_symptr = file_pointer;	/* file pointer to symtab */
315
    file_hdr.f_symptr = file_pointer;	/* file pointer to symtab */
313
 
316
 
314
    if (do_debug)
317
    if (s1->do_debug)
Line 315... Line 318...
315
	file_hdr.f_nsyms = coff_nb_syms;	/* number of symtab entries */
318
	file_hdr.f_nsyms = coff_nb_syms;	/* number of symtab entries */
Line 360... Line 363...
360
 
363
 
361
 
364
 
Line 362... Line 365...
362
    // group the symbols in order of filename, func1, func2, etc
365
    // group the symbols in order of filename, func1, func2, etc
363
    // finally global symbols
366
    // finally global symbols
Line 364... Line 367...
364
 
367
 
Line 365... Line 368...
365
    if (do_debug)
368
    if (s1->do_debug)
366
	SortSymbolTable();
369
	SortSymbolTable();
367
 
370
 
Line 368... Line 371...
368
    // write line no data
371
    // write line no data
369
 
372
 
Line 370... Line 373...
370
    for (i = 1; i < s1->nb_sections; i++) {
373
    for (i = 1; i < s1->nb_sections; i++) {
371
	coff_sec = §ion_header[i];
374
	coff_sec = §ion_header[i];
Line 497... Line 500...
497
	    }
500
	    }
498
	}
501
	}
499
    }
502
    }
Line 500... Line 503...
500
 
503
 
501
    // write symbol table
504
    // write symbol table
502
    if (do_debug) {
505
    if (s1->do_debug) {
503
	int k;
506
	int k;
504
	struct syment csym;
507
	struct syment csym;
505
	AUXFUNC auxfunc;
508
	AUXFUNC auxfunc;
506
	AUXBF auxbf;
509
	AUXBF auxbf;
Line 528... Line 531...
528
	    if (strlen(name) <= 8) {
531
	    if (strlen(name) <= 8) {
529
		strcpy(csym._n._n_name, name);
532
		strcpy(csym._n._n_name, name);
530
	    } else {
533
	    } else {
531
		if (pCoff_str_table - Coff_str_table + strlen(name) >
534
		if (pCoff_str_table - Coff_str_table + strlen(name) >
532
		    MAX_STR_TABLE - 1)
535
		    MAX_STR_TABLE - 1)
533
		    error("String table too large");
536
		    tcc_error("String table too large");
Line 534... Line 537...
534
 
537
 
535
		csym._n._n_n._n_zeroes = 0;
538
		csym._n._n_n._n_zeroes = 0;
536
		csym._n._n_n._n_offset =
539
		csym._n._n_n._n_offset =
Line 558... Line 561...
558
		    if (strcmp(name, Func[k]) == 0)
561
		    if (strcmp(name, Func[k]) == 0)
559
			break;
562
			break;
560
		}
563
		}
Line 561... Line 564...
561
 
564
 
562
		if (k >= nFuncs) {
-
 
563
		    char s[256];
-
 
564
 
565
		if (k >= nFuncs) {
565
		    sprintf(s, "debug info can't find function: %s", name);
-
 
566
 
-
 
567
		    error(s);
566
		    tcc_error("debug info can't find function: %s", name);
568
		}
567
		}
Line 569... Line 568...
569
		// put a Function Name
568
		// put a Function Name
570
 
569
 
Line 668... Line 667...
668
 
667
 
669
	    p++;
668
	    p++;
670
	}
669
	}
Line 671... Line 670...
671
    }
670
    }
672
 
671
 
Line 673... Line 672...
673
    if (do_debug) {
672
    if (s1->do_debug) {
674
	// write string table
673
	// write string table
675
 
674
 
Line 729... Line 728...
729
			if (strcmp(name2, Func[k]) == 0)
728
			if (strcmp(name2, Func[k]) == 0)
730
			    break;
729
			    break;
731
		    }
730
		    }
Line 732... Line 731...
732
 
731
 
733
		    if (k >= nFuncs) {
-
 
734
			char s[256];
-
 
735
 
-
 
736
			sprintf(s,
732
		    if (k >= nFuncs) {
737
				"debug (sort) info can't find function: %s",
-
 
738
				name2);
-
 
739
 
-
 
740
			error(s);
733
                        tcc_error("debug (sort) info can't find function: %s", name2);
Line 741... Line 734...
741
		    }
734
		    }
742
 
735
 
Line 762... Line 755...
762
	}
755
	}
763
	p++;
756
	p++;
764
    }
757
    }
Line 765... Line 758...
765
 
758
 
766
    if (n != nb_syms)
759
    if (n != nb_syms)
Line 767... Line 760...
767
	error("Internal Compiler error, debug info");
760
	tcc_error("Internal Compiler error, debug info");
Line 768... Line 761...
768
 
761
 
769
    // copy it all back
762
    // copy it all back
Line 819... Line 812...
819
    }
812
    }
Line 820... Line 813...
820
 
813
 
821
    return n;			// total number of symbols
814
    return n;			// total number of symbols
Line 822... Line 815...
822
}
815
}
823
 
816
 
824
BOOL OutputTheSection(Section * sect)
817
int OutputTheSection(Section * sect)
Line 825... Line 818...
825
{
818
{
826
    const char *s = sect->name;
819
    const char *s = sect->name;
827
 
820
 
828
    if (!strcmp(s, ".text"))
821
    if (!strcmp(s, ".text"))
829
	return true;
822
	return 1;
830
    else if (!strcmp(s, ".data"))
823
    else if (!strcmp(s, ".data"))
831
	return true;
824
	return 1;
Line 832... Line 825...
832
    else
825
    else
Line 859... Line 852...
859
 
852
 
860
	if (!strcmp(sname, s->name))
853
	if (!strcmp(sname, s->name))
861
	    return s;
854
	    return s;
Line 862... Line 855...
862
    }
855
    }
863
 
856
 
864
    error("could not find section %s", sname);
857
    tcc_error("could not find section %s", sname);
Line 865... Line 858...
865
    return 0;
858
    return 0;
866
}
859
}
867
 
860
 
Line 868... Line 861...
868
int tcc_load_coff(TCCState * s1, int fd)
861
ST_FUNC int tcc_load_coff(TCCState * s1, int fd)
869
{
862
{
Line 877... Line 870...
877
    char name2[9];
870
    char name2[9];
878
    FILHDR file_hdr;		/* FILE HEADER STRUCTURE              */
871
    FILHDR file_hdr;		/* FILE HEADER STRUCTURE              */
Line 879... Line 872...
879
 
872
 
880
    f = fdopen(fd, "rb");
873
    f = fdopen(fd, "rb");
881
    if (!f) {
874
    if (!f) {
882
	error("Unable to open .out file for input");
875
	tcc_error("Unable to open .out file for input");
Line 883... Line 876...
883
    }
876
    }
884
 
877
 
Line 885... Line 878...
885
    if (fread(&file_hdr, FILHSZ, 1, f) != 1)
878
    if (fread(&file_hdr, FILHSZ, 1, f) != 1)
886
	error("error reading .out file for input");
879
	tcc_error("error reading .out file for input");
Line 887... Line 880...
887
 
880
 
Line 888... Line 881...
888
    if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
881
    if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
889
	error("error reading .out file for input");
882
	tcc_error("error reading .out file for input");
Line 890... Line 883...
890
 
883
 
891
    // first read the string table
884
    // first read the string table
Line 892... Line 885...
892
 
885
 
Line 893... Line 886...
893
    if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
886
    if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
894
	error("error reading .out file for input");
887
	tcc_error("error reading .out file for input");
Line 895... Line 888...
895
 
888
 
Line 896... Line 889...
896
    if (fread(&str_size, sizeof(int), 1, f) != 1)
889
    if (fread(&str_size, sizeof(int), 1, f) != 1)
Line 897... Line 890...
897
	error("error reading .out file for input");
890
	tcc_error("error reading .out file for input");
898
 
891
 
Line 899... Line 892...
899
 
892
 
900
    Coff_str_table = (char *) tcc_malloc(str_size);
893
    Coff_str_table = (char *) tcc_malloc(str_size);
901
 
894
 
Line 902... Line 895...
902
    if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
895
    if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
903
	error("error reading .out file for input");
896
	tcc_error("error reading .out file for input");
904
 
897
 
905
    // read/process all the symbols
898
    // read/process all the symbols
Line 938... Line 931...
938
	    // strip off any leading underscore (except for other main routine)
931
	    // strip off any leading underscore (except for other main routine)
Line 939... Line 932...
939
 
932
 
940
	    if (name[0] == '_' && strcmp(name, "_main") != 0)
933
	    if (name[0] == '_' && strcmp(name, "_main") != 0)
Line 941... Line 934...
941
		name++;
934
		name++;
942
 
935
 
943
	    tcc_add_symbol(s1, name, csym.n_value);
936
	    tcc_add_symbol(s1, name, (void*)(uintptr_t)csym.n_value);
Line 944... Line 937...
944
	}
937
	}
945
	// skip any aux records
938
	// skip any aux records
946
 
939
 
947
	if (csym.n_numaux == 1) {
940
	if (csym.n_numaux == 1) {
948
	    if (fread(&csym, SYMESZ, 1, f) != 1)
941
	    if (fread(&csym, SYMESZ, 1, f) != 1)
949
		error("error reading .out file for input");
942
		tcc_error("error reading .out file for input");
Line 950... Line 943...
950
	    i++;
943
	    i++;