⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcccoff.c

📁 tiny c compiler
💻 C
📖 第 1 页 / 共 2 页
字号:
		    } else {			str =			    (const char *) stabstr_section->data +			    sym->n_strx;			/* do not add path */			len = strlen(str);			if (len > 0 && str[len - 1] != '/')			    goto add_incl2;		    }		    break;		}		sym++;	    }	}    }    // write symbol table    if (do_debug) {	int k;	struct syment csym;	AUXFUNC auxfunc;	AUXBF auxbf;	AUXEF auxef;	int i;	Elf32_Sym *p;	char *name;	char _name[MAX_FUNCS];	int nstr;	int n = 0;	Coff_str_table = (char *) tcc_malloc(MAX_STR_TABLE);	pCoff_str_table = Coff_str_table;	nstr = 0;	p = (Elf32_Sym *) symtab_section->data;	for (i = 0; i < nb_syms; i++) {	    /* don't add underscores for Code Composer Version 2.xx */#define ADD_UNDERSCORE 0	    name = (char *) symtab_section->link->data + p->st_name;#if ADD_UNDERSCORE	    _name[0] = '_';	    strcpy(_name + 1, name);#else	    strcpy(_name, name);#endif	    for (k = 0; k < 8; k++)		csym._n._n_name[k] = 0;	    if (strlen(_name) <= 8) {		strcpy(csym._n._n_name, _name);	    } else {		if (pCoff_str_table - Coff_str_table + strlen(_name) >		    MAX_STR_TABLE - 1)		    error("String table too large");		csym._n._n_n._n_zeroes = 0;		csym._n._n_n._n_offset =		    pCoff_str_table - Coff_str_table + 4;		strcpy(pCoff_str_table, _name);		pCoff_str_table += strlen(_name) + 1;	// skip over null		nstr++;	    }	    if (p->st_info == 4) {		// put a filename symbol		csym.n_value = 33;	// ?????		csym.n_scnum = N_DEBUG;		csym.n_type = 0;		csym.n_sclass = C_FILE;		csym.n_numaux = 0;		fwrite(&csym, 18, 1, f);		n++;	    } else if (p->st_info == 0x12) {		// find the function data		for (k = 0; k < nFuncs; k++) {		    if (strcmp(name, Func[k]) == 0)			break;		}		if (k >= nFuncs) {		    char s[256];		    sprintf(s, "debug info can't find function: %s", name);		    error(s);		}		// put a Function Name		csym.n_value = p->st_value;	// physical address		csym.n_scnum = CoffTextSectionNo;		csym.n_type = MKTYPE(T_INT, DT_FCN, 0, 0, 0, 0, 0);		csym.n_sclass = C_EXT;		csym.n_numaux = 1;		fwrite(&csym, 18, 1, f);		// now put aux info		auxfunc.tag = 0;		auxfunc.size = EndAddress[k] - p->st_value;		auxfunc.fileptr = LineNoFilePtr[k];		auxfunc.nextsym = n + 6;	// tktk		auxfunc.dummy = 0;		fwrite(&auxfunc, 18, 1, f);		// put a .bf		strcpy(csym._n._n_name, ".bf");		csym.n_value = p->st_value;	// physical address		csym.n_scnum = CoffTextSectionNo;		csym.n_type = 0;		csym.n_sclass = C_FCN;		csym.n_numaux = 1;		fwrite(&csym, 18, 1, f);		// now put aux info		auxbf.regmask = 0;		auxbf.lineno = 0;		auxbf.nentries = FuncEntries[k];		auxbf.localframe = 0;		auxbf.nextentry = n + 6;		auxbf.dummy = 0;		fwrite(&auxbf, 18, 1, f);		// put a .ef		strcpy(csym._n._n_name, ".ef");		csym.n_value = EndAddress[k];	// physical address  		csym.n_scnum = CoffTextSectionNo;		csym.n_type = 0;		csym.n_sclass = C_FCN;		csym.n_numaux = 1;		fwrite(&csym, 18, 1, f);		// now put aux info		auxef.dummy = 0;		auxef.lineno = LastLineNo[k];		auxef.dummy1 = 0;		auxef.dummy2 = 0;		auxef.dummy3 = 0;		auxef.dummy4 = 0;		fwrite(&auxef, 18, 1, f);		n += 6;	    } else {		// try an put some type info		if ((p->st_other & VT_BTYPE) == VT_DOUBLE) {		    csym.n_type = T_DOUBLE;	// int		    csym.n_sclass = C_EXT;		} else if ((p->st_other & VT_BTYPE) == VT_FLOAT) {		    csym.n_type = T_FLOAT;		    csym.n_sclass = C_EXT;		} else if ((p->st_other & VT_BTYPE) == VT_INT) {		    csym.n_type = T_INT;	// int		    csym.n_sclass = C_EXT;		} else if ((p->st_other & VT_BTYPE) == VT_SHORT) {		    csym.n_type = T_SHORT;		    csym.n_sclass = C_EXT;		} else if ((p->st_other & VT_BTYPE) == VT_BYTE) {		    csym.n_type = T_CHAR;		    csym.n_sclass = C_EXT;		} else {		    csym.n_type = T_INT;	// just mark as a label		    csym.n_sclass = C_LABEL;		}		csym.n_value = p->st_value;		csym.n_scnum = 2;		csym.n_numaux = 1;		fwrite(&csym, 18, 1, f);		auxfunc.tag = 0;		auxfunc.size = 0x20;		auxfunc.fileptr = 0;		auxfunc.nextsym = 0;		auxfunc.dummy = 0;		fwrite(&auxfunc, 18, 1, f);		n++;		n++;	    }	    p++;	}    }    if (do_debug) {	// write string table	// first write the size	i = pCoff_str_table - Coff_str_table;	fwrite(&i, 4, 1, f);	// then write the strings	fwrite(Coff_str_table, i, 1, f);	tcc_free(Coff_str_table);    }    return 0;}// group the symbols in order of filename, func1, func2, etc// finally global symbolsvoid SortSymbolTable(void){    int i, j, k, n = 0;    Elf32_Sym *p, *p2, *NewTable;    char *name, *name2;    NewTable = (Elf32_Sym *) tcc_malloc(nb_syms * sizeof(Elf32_Sym));    p = (Elf32_Sym *) symtab_section->data;    // find a file symbol, copy it over    // then scan the whole symbol list and copy any function    // symbols that match the file association    for (i = 0; i < nb_syms; i++) {	if (p->st_info == 4) {	    name = (char *) symtab_section->link->data + p->st_name;	    // this is a file symbol, copy it over	    NewTable[n++] = *p;	    p2 = (Elf32_Sym *) symtab_section->data;	    for (j = 0; j < nb_syms; j++) {		if (p2->st_info == 0x12) {		    // this is a func symbol		    name2 =			(char *) symtab_section->link->data + p2->st_name;		    // find the function data index		    for (k = 0; k < nFuncs; k++) {			if (strcmp(name2, Func[k]) == 0)			    break;		    }		    if (k >= nFuncs) {			char s[256];			sprintf(s,				"debug (sort) info can't find function: %s",				name2);			error(s);		    }		    if (strcmp(AssociatedFile[k], name) == 0) {			// yes they match copy it over			NewTable[n++] = *p2;		    }		}		p2++;	    }	}	p++;    }    // now all the filename and func symbols should have been copied over    // copy all the rest over (all except file and funcs)    p = (Elf32_Sym *) symtab_section->data;    for (i = 0; i < nb_syms; i++) {	if (p->st_info != 4 && p->st_info != 0x12) {	    NewTable[n++] = *p;	}	p++;    }    if (n != nb_syms)	error("Internal Compiler error, debug info");    // copy it all back    p = (Elf32_Sym *) symtab_section->data;    for (i = 0; i < nb_syms; i++) {	*p++ = NewTable[i];    }    tcc_free(NewTable);}int FindCoffSymbolIndex(const char *func_name){    int i, n = 0;    Elf32_Sym *p;    char *name;    p = (Elf32_Sym *) symtab_section->data;    for (i = 0; i < nb_syms; i++) {	name = (char *) symtab_section->link->data + p->st_name;	if (p->st_info == 4) {	    // put a filename symbol	    n++;	} else if (p->st_info == 0x12) {	    if (strcmp(func_name, name) == 0)		return n;	    n += 6;	    // put a Function Name	    // now put aux info	    // put a .bf	    // now put aux info	    // put a .ef	    // now put aux info	} else {	    n += 2;	}	p++;    }    return n;			// total number of symbols}BOOL OutputTheSection(Section * sect){    const char *s = sect->name;    if (!strcmp(s, ".text"))	return true;    else if (!strcmp(s, ".data"))	return true;    else	return 0;}short int GetCoffFlags(const char *s){    if (!strcmp(s, ".text"))	return STYP_TEXT | STYP_DATA | STYP_ALIGN | 0x400;    else if (!strcmp(s, ".data"))	return STYP_DATA;    else if (!strcmp(s, ".bss"))	return STYP_BSS;    else if (!strcmp(s, ".stack"))	return STYP_BSS | STYP_ALIGN | 0x200;    else if (!strcmp(s, ".cinit"))	return STYP_COPY | STYP_DATA | STYP_ALIGN | 0x200;    else	return 0;}Section *FindSection(TCCState * s1, const char *sname){    Section *s;    int i;    for (i = 1; i < s1->nb_sections; i++) {	s = s1->sections[i];	if (!strcmp(sname, s->name))	    return s;    }    error("could not find section %s", sname);    return 0;}int tcc_load_coff(TCCState * s1, int fd){// tktk TokenSym *ts;    FILE *f;    unsigned int str_size;    char *Coff_str_table, *name;    int i, k;    struct syment csym;    char name2[9];    FILHDR file_hdr;		/* FILE HEADER STRUCTURE              */    f = fdopen(fd, "rb");    if (!f) {	error("Unable to open .out file for input");    }    if (fread(&file_hdr, FILHSZ, 1, f) != 1)	error("error reading .out file for input");    if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)	error("error reading .out file for input");    // first read the string table    if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))	error("error reading .out file for input");    if (fread(&str_size, sizeof(int), 1, f) != 1)	error("error reading .out file for input");    Coff_str_table = (char *) tcc_malloc(str_size);    if (fread(Coff_str_table, str_size - 4, 1, f) != 1)	error("error reading .out file for input");    // read/process all the symbols    // seek back to symbols    if (fseek(f, file_hdr.f_symptr, SEEK_SET))	error("error reading .out file for input");    for (i = 0; i < file_hdr.f_nsyms; i++) {	if (fread(&csym, SYMESZ, 1, f) != 1)	    error("error reading .out file for input");	if (csym._n._n_n._n_zeroes == 0) {	    name = Coff_str_table + csym._n._n_n._n_offset - 4;	} else {	    name = csym._n._n_name;	    if (name[7] != 0) {		for (k = 0; k < 8; k++)		    name2[k] = name[k];		name2[8] = 0;		name = name2;	    }	}//              if (strcmp("_DAC_Buffer",name)==0)  // tktk//                      name[0]=0;	if (((csym.n_type & 0x30) == 0x20 && csym.n_sclass == 0x2) || ((csym.n_type & 0x30) == 0x30 && csym.n_sclass == 0x2) || (csym.n_type == 0x4 && csym.n_sclass == 0x2) || (csym.n_type == 0x8 && csym.n_sclass == 0x2) ||	// structures	    (csym.n_type == 0x18 && csym.n_sclass == 0x2) ||	// pointer to structure	    (csym.n_type == 0x7 && csym.n_sclass == 0x2) ||	// doubles	    (csym.n_type == 0x6 && csym.n_sclass == 0x2))	// floats	{	    // strip off any leading underscore (except for other main routine)	    if (name[0] == '_' && strcmp(name, "_main") != 0)		name++;	    tcc_add_symbol(s1, name, csym.n_value);	}	// skip any aux records	if (csym.n_numaux == 1) {	    if (fread(&csym, SYMESZ, 1, f) != 1)		error("error reading .out file for input");	    i++;	}    }    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -