1 /*
2  *  COFF file handling for TCC
3  *
4  *  Copyright (c) 2003, 2004 TK
5  *  Copyright (c) 2004 Fabrice Bellard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
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
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #include "coff.h"
22 
23 #define MAXNSCNS 255		/* MAXIMUM NUMBER OF SECTIONS         */
24 #define MAX_STR_TABLE 1000000
25 AOUTHDR o_filehdr;		/* OPTIONAL (A.OUT) FILE HEADER       */
26 
27 SCNHDR section_header[MAXNSCNS];
28 
29 #define MAX_FUNCS 1000
30 #define MAX_FUNC_NAME_LENGTH 128
31 
32 int nFuncs;
33 char Func[MAX_FUNCS][MAX_FUNC_NAME_LENGTH];
34 char AssociatedFile[MAX_FUNCS][MAX_FUNC_NAME_LENGTH];
35 int LineNoFilePtr[MAX_FUNCS];
36 int EndAddress[MAX_FUNCS];
37 int LastLineNo[MAX_FUNCS];
38 int FuncEntries[MAX_FUNCS];
39 
40 BOOL OutputTheSection(Section * sect);
41 short int GetCoffFlags(const char *s);
42 void SortSymbolTable(void);
43 Section *FindSection(TCCState * s1, const char *sname);
44 
45 int C67_main_entry_point;
46 
47 int FindCoffSymbolIndex(const char *func_name);
48 int nb_syms;
49 
50 typedef struct {
51     long tag;
52     long size;
53     long fileptr;
54     long nextsym;
55     short int dummy;
56 } AUXFUNC;
57 
58 typedef struct {
59     long regmask;
60     unsigned short lineno;
61     unsigned short nentries;
62     int localframe;
63     int nextentry;
64     short int dummy;
65 } AUXBF;
66 
67 typedef struct {
68     long dummy;
69     unsigned short lineno;
70     unsigned short dummy1;
71     int dummy2;
72     int dummy3;
73     unsigned short dummy4;
74 } AUXEF;
75 
tcc_output_coff(TCCState * s1,FILE * f)76 int tcc_output_coff(TCCState *s1, FILE *f)
77 {
78     Section *tcc_sect;
79     SCNHDR *coff_sec;
80     int file_pointer;
81     char *Coff_str_table, *pCoff_str_table;
82     int CoffTextSectionNo, coff_nb_syms;
83     FILHDR file_hdr;		/* FILE HEADER STRUCTURE              */
84     Section *stext, *sdata, *sbss;
85     int i, NSectionsToOutput = 0;
86 
87     stext = FindSection(s1, ".text");
88     sdata = FindSection(s1, ".data");
89     sbss = FindSection(s1, ".bss");
90 
91     nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
92     coff_nb_syms = FindCoffSymbolIndex("XXXXXXXXXX1");
93 
94     file_hdr.f_magic = COFF_C67_MAGIC;	/* magic number */
95     file_hdr.f_timdat = 0;	/* time & date stamp */
96     file_hdr.f_opthdr = sizeof(AOUTHDR);	/* sizeof(optional hdr) */
97     file_hdr.f_flags = 0x1143;	/* flags (copied from what code composer does) */
98     file_hdr.f_TargetID = 0x99;	/* for C6x = 0x0099 */
99 
100     o_filehdr.magic = 0x0108;	/* see magic.h                          */
101     o_filehdr.vstamp = 0x0190;	/* version stamp                        */
102     o_filehdr.tsize = stext->data_offset;	/* text size in bytes, padded to FW bdry */
103     o_filehdr.dsize = sdata->data_offset;	/* initialized data "  "                */
104     o_filehdr.bsize = sbss->data_offset;	/* uninitialized data "   "             */
105     o_filehdr.entrypt = C67_main_entry_point;	/* entry pt.                          */
106     o_filehdr.text_start = stext->sh_addr;	/* base of text used for this file      */
107     o_filehdr.data_start = sdata->sh_addr;	/* base of data used for this file      */
108 
109 
110     // create all the section headers
111 
112     file_pointer = FILHSZ + sizeof(AOUTHDR);
113 
114     CoffTextSectionNo = -1;
115 
116     for (i = 1; i < s1->nb_sections; i++) {
117 	coff_sec = &section_header[i];
118 	tcc_sect = s1->sections[i];
119 
120 	if (OutputTheSection(tcc_sect)) {
121 	    NSectionsToOutput++;
122 
123 	    if (CoffTextSectionNo == -1 && tcc_sect == stext)
124 		CoffTextSectionNo = NSectionsToOutput;	// rem which coff sect number the .text sect is
125 
126 	    strcpy(coff_sec->s_name, tcc_sect->name);	/* section name */
127 
128 	    coff_sec->s_paddr = tcc_sect->sh_addr;	/* physical address */
129 	    coff_sec->s_vaddr = tcc_sect->sh_addr;	/* virtual address */
130 	    coff_sec->s_size = tcc_sect->data_offset;	/* section size */
131 	    coff_sec->s_scnptr = 0;	/* file ptr to raw data for section */
132 	    coff_sec->s_relptr = 0;	/* file ptr to relocation */
133 	    coff_sec->s_lnnoptr = 0;	/* file ptr to line numbers */
134 	    coff_sec->s_nreloc = 0;	/* number of relocation entries */
135 	    coff_sec->s_flags = GetCoffFlags(coff_sec->s_name);	/* flags */
136 	    coff_sec->s_reserved = 0;	/* reserved byte */
137 	    coff_sec->s_page = 0;	/* memory page id */
138 
139 	    file_pointer += sizeof(SCNHDR);
140 	}
141     }
142 
143     file_hdr.f_nscns = NSectionsToOutput;	/* number of sections */
144 
145     // now loop through and determine file pointer locations
146     // for the raw data
147 
148 
149     for (i = 1; i < s1->nb_sections; i++) {
150 	coff_sec = &section_header[i];
151 	tcc_sect = s1->sections[i];
152 
153 	if (OutputTheSection(tcc_sect)) {
154 	    // put raw data
155 	    coff_sec->s_scnptr = file_pointer;	/* file ptr to raw data for section */
156 	    file_pointer += coff_sec->s_size;
157 	}
158     }
159 
160     // now loop through and determine file pointer locations
161     // for the relocation data
162 
163     for (i = 1; i < s1->nb_sections; i++) {
164 	coff_sec = &section_header[i];
165 	tcc_sect = s1->sections[i];
166 
167 	if (OutputTheSection(tcc_sect)) {
168 	    // put relocations data
169 	    if (coff_sec->s_nreloc > 0) {
170 		coff_sec->s_relptr = file_pointer;	/* file ptr to relocation */
171 		file_pointer += coff_sec->s_nreloc * sizeof(struct reloc);
172 	    }
173 	}
174     }
175 
176     // now loop through and determine file pointer locations
177     // for the line number data
178 
179     for (i = 1; i < s1->nb_sections; i++) {
180 	coff_sec = &section_header[i];
181 	tcc_sect = s1->sections[i];
182 
183 	coff_sec->s_nlnno = 0;
184 	coff_sec->s_lnnoptr = 0;
185 
186 	if (do_debug && tcc_sect == stext) {
187 	    // count how many line nos data
188 
189 	    // also find association between source file name and function
190 	    // so we can sort the symbol table
191 
192 
193 	    Stab_Sym *sym, *sym_end;
194 	    char func_name[MAX_FUNC_NAME_LENGTH],
195 		last_func_name[MAX_FUNC_NAME_LENGTH];
196 	    unsigned long func_addr, last_pc, pc;
197 	    const char *incl_files[INCLUDE_STACK_SIZE];
198 	    int incl_index, len, last_line_num;
199 	    const char *str, *p;
200 
201 	    coff_sec->s_lnnoptr = file_pointer;	/* file ptr to linno */
202 
203 
204 	    func_name[0] = '\0';
205 	    func_addr = 0;
206 	    incl_index = 0;
207 	    last_func_name[0] = '\0';
208 	    last_pc = 0xffffffff;
209 	    last_line_num = 1;
210 	    sym = (Stab_Sym *) stab_section->data + 1;
211 	    sym_end =
212 		(Stab_Sym *) (stab_section->data +
213 			      stab_section->data_offset);
214 
215 	    nFuncs = 0;
216 	    while (sym < sym_end) {
217 		switch (sym->n_type) {
218 		    /* function start or end */
219 		case N_FUN:
220 		    if (sym->n_strx == 0) {
221 			// end of function
222 
223 			coff_sec->s_nlnno++;
224 			file_pointer += LINESZ;
225 
226 			pc = sym->n_value + func_addr;
227 			func_name[0] = '\0';
228 			func_addr = 0;
229 			EndAddress[nFuncs] = pc;
230 			FuncEntries[nFuncs] =
231 			    (file_pointer -
232 			     LineNoFilePtr[nFuncs]) / LINESZ - 1;
233 			LastLineNo[nFuncs++] = last_line_num + 1;
234 		    } else {
235 			// beginning of function
236 
237 			LineNoFilePtr[nFuncs] = file_pointer;
238 			coff_sec->s_nlnno++;
239 			file_pointer += LINESZ;
240 
241 			str =
242 			    (const char *) stabstr_section->data +
243 			    sym->n_strx;
244 
245 			p = strchr(str, ':');
246 			if (!p) {
247 			    pstrcpy(func_name, sizeof(func_name), str);
248 			    pstrcpy(Func[nFuncs], sizeof(func_name), str);
249 			} else {
250 			    len = p - str;
251 			    if (len > sizeof(func_name) - 1)
252 				len = sizeof(func_name) - 1;
253 			    memcpy(func_name, str, len);
254 			    memcpy(Func[nFuncs], str, len);
255 			    func_name[len] = '\0';
256 			}
257 
258 			// save the file that it came in so we can sort later
259 			pstrcpy(AssociatedFile[nFuncs], sizeof(func_name),
260 				incl_files[incl_index - 1]);
261 
262 			func_addr = sym->n_value;
263 		    }
264 		    break;
265 
266 		    /* line number info */
267 		case N_SLINE:
268 		    pc = sym->n_value + func_addr;
269 
270 		    last_pc = pc;
271 		    last_line_num = sym->n_desc;
272 
273 		    /* XXX: slow! */
274 		    strcpy(last_func_name, func_name);
275 
276 		    coff_sec->s_nlnno++;
277 		    file_pointer += LINESZ;
278 		    break;
279 		    /* include files */
280 		case N_BINCL:
281 		    str =
282 			(const char *) stabstr_section->data + sym->n_strx;
283 		  add_incl:
284 		    if (incl_index < INCLUDE_STACK_SIZE) {
285 			incl_files[incl_index++] = str;
286 		    }
287 		    break;
288 		case N_EINCL:
289 		    if (incl_index > 1)
290 			incl_index--;
291 		    break;
292 		case N_SO:
293 		    if (sym->n_strx == 0) {
294 			incl_index = 0;	/* end of translation unit */
295 		    } else {
296 			str =
297 			    (const char *) stabstr_section->data +
298 			    sym->n_strx;
299 			/* do not add path */
300 			len = strlen(str);
301 			if (len > 0 && str[len - 1] != '/')
302 			    goto add_incl;
303 		    }
304 		    break;
305 		}
306 		sym++;
307 	    }
308 	}
309 
310     }
311 
312     file_hdr.f_symptr = file_pointer;	/* file pointer to symtab */
313 
314     if (do_debug)
315 	file_hdr.f_nsyms = coff_nb_syms;	/* number of symtab entries */
316     else
317 	file_hdr.f_nsyms = 0;
318 
319     file_pointer += file_hdr.f_nsyms * SYMNMLEN;
320 
321     // OK now we are all set to write the file
322 
323 
324     fwrite(&file_hdr, FILHSZ, 1, f);
325     fwrite(&o_filehdr, sizeof(o_filehdr), 1, f);
326 
327     // write section headers
328     for (i = 1; i < s1->nb_sections; i++) {
329 	coff_sec = &section_header[i];
330 	tcc_sect = s1->sections[i];
331 
332 	if (OutputTheSection(tcc_sect)) {
333 	    fwrite(coff_sec, sizeof(SCNHDR), 1, f);
334 	}
335     }
336 
337     // write raw data
338     for (i = 1; i < s1->nb_sections; i++) {
339 	coff_sec = &section_header[i];
340 	tcc_sect = s1->sections[i];
341 
342 	if (OutputTheSection(tcc_sect)) {
343 	    fwrite(tcc_sect->data, tcc_sect->data_offset, 1, f);
344 	}
345     }
346 
347     // write relocation data
348     for (i = 1; i < s1->nb_sections; i++) {
349 	coff_sec = &section_header[i];
350 	tcc_sect = s1->sections[i];
351 
352 	if (OutputTheSection(tcc_sect)) {
353 	    // put relocations data
354 	    if (coff_sec->s_nreloc > 0) {
355 		fwrite(tcc_sect->reloc,
356 		       coff_sec->s_nreloc * sizeof(struct reloc), 1, f);
357 	    }
358 	}
359     }
360 
361 
362     // group the symbols in order of filename, func1, func2, etc
363     // finally global symbols
364 
365     if (do_debug)
366 	SortSymbolTable();
367 
368     // write line no data
369 
370     for (i = 1; i < s1->nb_sections; i++) {
371 	coff_sec = &section_header[i];
372 	tcc_sect = s1->sections[i];
373 
374 	if (do_debug && tcc_sect == stext) {
375 	    // count how many line nos data
376 
377 
378 	    Stab_Sym *sym, *sym_end;
379 	    char func_name[128], last_func_name[128];
380 	    unsigned long func_addr, last_pc, pc;
381 	    const char *incl_files[INCLUDE_STACK_SIZE];
382 	    int incl_index, len, last_line_num;
383 	    const char *str, *p;
384 
385 	    LINENO CoffLineNo;
386 
387 	    func_name[0] = '\0';
388 	    func_addr = 0;
389 	    incl_index = 0;
390 	    last_func_name[0] = '\0';
391 	    last_pc = 0;
392 	    last_line_num = 1;
393 	    sym = (Stab_Sym *) stab_section->data + 1;
394 	    sym_end =
395 		(Stab_Sym *) (stab_section->data +
396 			      stab_section->data_offset);
397 
398 	    while (sym < sym_end) {
399 		switch (sym->n_type) {
400 		    /* function start or end */
401 		case N_FUN:
402 		    if (sym->n_strx == 0) {
403 			// end of function
404 
405 			CoffLineNo.l_addr.l_paddr = last_pc;
406 			CoffLineNo.l_lnno = last_line_num + 1;
407 			fwrite(&CoffLineNo, 6, 1, f);
408 
409 			pc = sym->n_value + func_addr;
410 			func_name[0] = '\0';
411 			func_addr = 0;
412 		    } else {
413 			// beginning of function
414 
415 			str =
416 			    (const char *) stabstr_section->data +
417 			    sym->n_strx;
418 
419 
420 			p = strchr(str, ':');
421 			if (!p) {
422 			    pstrcpy(func_name, sizeof(func_name), str);
423 			} else {
424 			    len = p - str;
425 			    if (len > sizeof(func_name) - 1)
426 				len = sizeof(func_name) - 1;
427 			    memcpy(func_name, str, len);
428 			    func_name[len] = '\0';
429 			}
430 			func_addr = sym->n_value;
431 			last_pc = func_addr;
432 			last_line_num = -1;
433 
434 			// output a function begin
435 
436 			CoffLineNo.l_addr.l_symndx =
437 			    FindCoffSymbolIndex(func_name);
438 			CoffLineNo.l_lnno = 0;
439 
440 			fwrite(&CoffLineNo, 6, 1, f);
441 		    }
442 		    break;
443 
444 		    /* line number info */
445 		case N_SLINE:
446 		    pc = sym->n_value + func_addr;
447 
448 
449 		    /* XXX: slow! */
450 		    strcpy(last_func_name, func_name);
451 
452 		    // output a line reference
453 
454 		    CoffLineNo.l_addr.l_paddr = last_pc;
455 
456 		    if (last_line_num == -1) {
457 			CoffLineNo.l_lnno = sym->n_desc;
458 		    } else {
459 			CoffLineNo.l_lnno = last_line_num + 1;
460 		    }
461 
462 		    fwrite(&CoffLineNo, 6, 1, f);
463 
464 		    last_pc = pc;
465 		    last_line_num = sym->n_desc;
466 
467 		    break;
468 
469 		    /* include files */
470 		case N_BINCL:
471 		    str =
472 			(const char *) stabstr_section->data + sym->n_strx;
473 		  add_incl2:
474 		    if (incl_index < INCLUDE_STACK_SIZE) {
475 			incl_files[incl_index++] = str;
476 		    }
477 		    break;
478 		case N_EINCL:
479 		    if (incl_index > 1)
480 			incl_index--;
481 		    break;
482 		case N_SO:
483 		    if (sym->n_strx == 0) {
484 			incl_index = 0;	/* end of translation unit */
485 		    } else {
486 			str =
487 			    (const char *) stabstr_section->data +
488 			    sym->n_strx;
489 			/* do not add path */
490 			len = strlen(str);
491 			if (len > 0 && str[len - 1] != '/')
492 			    goto add_incl2;
493 		    }
494 		    break;
495 		}
496 		sym++;
497 	    }
498 	}
499     }
500 
501     // write symbol table
502     if (do_debug) {
503 	int k;
504 	struct syment csym;
505 	AUXFUNC auxfunc;
506 	AUXBF auxbf;
507 	AUXEF auxef;
508 	int i;
509 	Elf32_Sym *p;
510 	const char *name;
511 	int nstr;
512 	int n = 0;
513 
514 	Coff_str_table = (char *) tcc_malloc(MAX_STR_TABLE);
515 	pCoff_str_table = Coff_str_table;
516 	nstr = 0;
517 
518 	p = (Elf32_Sym *) symtab_section->data;
519 
520 
521 	for (i = 0; i < nb_syms; i++) {
522 
523 	    name = symtab_section->link->data + p->st_name;
524 
525 	    for (k = 0; k < 8; k++)
526 		csym._n._n_name[k] = 0;
527 
528 	    if (strlen(name) <= 8) {
529 		strcpy(csym._n._n_name, name);
530 	    } else {
531 		if (pCoff_str_table - Coff_str_table + strlen(name) >
532 		    MAX_STR_TABLE - 1)
533 		    error("String table too large");
534 
535 		csym._n._n_n._n_zeroes = 0;
536 		csym._n._n_n._n_offset =
537 		    pCoff_str_table - Coff_str_table + 4;
538 
539 		strcpy(pCoff_str_table, name);
540 		pCoff_str_table += strlen(name) + 1;	// skip over null
541 		nstr++;
542 	    }
543 
544 	    if (p->st_info == 4) {
545 		// put a filename symbol
546 		csym.n_value = 33;	// ?????
547 		csym.n_scnum = N_DEBUG;
548 		csym.n_type = 0;
549 		csym.n_sclass = C_FILE;
550 		csym.n_numaux = 0;
551 		fwrite(&csym, 18, 1, f);
552 		n++;
553 
554 	    } else if (p->st_info == 0x12) {
555 		// find the function data
556 
557 		for (k = 0; k < nFuncs; k++) {
558 		    if (strcmp(name, Func[k]) == 0)
559 			break;
560 		}
561 
562 		if (k >= nFuncs) {
563 		    char s[256];
564 
565 		    sprintf(s, "debug info can't find function: %s", name);
566 
567 		    error(s);
568 		}
569 		// put a Function Name
570 
571 		csym.n_value = p->st_value;	// physical address
572 		csym.n_scnum = CoffTextSectionNo;
573 		csym.n_type = MKTYPE(T_INT, DT_FCN, 0, 0, 0, 0, 0);
574 		csym.n_sclass = C_EXT;
575 		csym.n_numaux = 1;
576 		fwrite(&csym, 18, 1, f);
577 
578 		// now put aux info
579 
580 		auxfunc.tag = 0;
581 		auxfunc.size = EndAddress[k] - p->st_value;
582 		auxfunc.fileptr = LineNoFilePtr[k];
583 		auxfunc.nextsym = n + 6;	// tktk
584 		auxfunc.dummy = 0;
585 		fwrite(&auxfunc, 18, 1, f);
586 
587 		// put a .bf
588 
589 		strcpy(csym._n._n_name, ".bf");
590 		csym.n_value = p->st_value;	// physical address
591 		csym.n_scnum = CoffTextSectionNo;
592 		csym.n_type = 0;
593 		csym.n_sclass = C_FCN;
594 		csym.n_numaux = 1;
595 		fwrite(&csym, 18, 1, f);
596 
597 		// now put aux info
598 
599 		auxbf.regmask = 0;
600 		auxbf.lineno = 0;
601 		auxbf.nentries = FuncEntries[k];
602 		auxbf.localframe = 0;
603 		auxbf.nextentry = n + 6;
604 		auxbf.dummy = 0;
605 		fwrite(&auxbf, 18, 1, f);
606 
607 		// put a .ef
608 
609 		strcpy(csym._n._n_name, ".ef");
610 		csym.n_value = EndAddress[k];	// physical address
611 		csym.n_scnum = CoffTextSectionNo;
612 		csym.n_type = 0;
613 		csym.n_sclass = C_FCN;
614 		csym.n_numaux = 1;
615 		fwrite(&csym, 18, 1, f);
616 
617 		// now put aux info
618 
619 		auxef.dummy = 0;
620 		auxef.lineno = LastLineNo[k];
621 		auxef.dummy1 = 0;
622 		auxef.dummy2 = 0;
623 		auxef.dummy3 = 0;
624 		auxef.dummy4 = 0;
625 		fwrite(&auxef, 18, 1, f);
626 
627 		n += 6;
628 
629 	    } else {
630 		// try an put some type info
631 
632 		if ((p->st_other & VT_BTYPE) == VT_DOUBLE) {
633 		    csym.n_type = T_DOUBLE;	// int
634 		    csym.n_sclass = C_EXT;
635 		} else if ((p->st_other & VT_BTYPE) == VT_FLOAT) {
636 		    csym.n_type = T_FLOAT;
637 		    csym.n_sclass = C_EXT;
638 		} else if ((p->st_other & VT_BTYPE) == VT_INT) {
639 		    csym.n_type = T_INT;	// int
640 		    csym.n_sclass = C_EXT;
641 		} else if ((p->st_other & VT_BTYPE) == VT_SHORT) {
642 		    csym.n_type = T_SHORT;
643 		    csym.n_sclass = C_EXT;
644 		} else if ((p->st_other & VT_BTYPE) == VT_BYTE) {
645 		    csym.n_type = T_CHAR;
646 		    csym.n_sclass = C_EXT;
647 		} else {
648 		    csym.n_type = T_INT;	// just mark as a label
649 		    csym.n_sclass = C_LABEL;
650 		}
651 
652 
653 		csym.n_value = p->st_value;
654 		csym.n_scnum = 2;
655 		csym.n_numaux = 1;
656 		fwrite(&csym, 18, 1, f);
657 
658 		auxfunc.tag = 0;
659 		auxfunc.size = 0x20;
660 		auxfunc.fileptr = 0;
661 		auxfunc.nextsym = 0;
662 		auxfunc.dummy = 0;
663 		fwrite(&auxfunc, 18, 1, f);
664 		n++;
665 		n++;
666 
667 	    }
668 
669 	    p++;
670 	}
671     }
672 
673     if (do_debug) {
674 	// write string table
675 
676 	// first write the size
677 	i = pCoff_str_table - Coff_str_table;
678 	fwrite(&i, 4, 1, f);
679 
680 	// then write the strings
681 	fwrite(Coff_str_table, i, 1, f);
682 
683 	tcc_free(Coff_str_table);
684     }
685 
686     return 0;
687 }
688 
689 
690 
691 // group the symbols in order of filename, func1, func2, etc
692 // finally global symbols
693 
SortSymbolTable(void)694 void SortSymbolTable(void)
695 {
696     int i, j, k, n = 0;
697     Elf32_Sym *p, *p2, *NewTable;
698     char *name, *name2;
699 
700     NewTable = (Elf32_Sym *) tcc_malloc(nb_syms * sizeof(Elf32_Sym));
701 
702     p = (Elf32_Sym *) symtab_section->data;
703 
704 
705     // find a file symbol, copy it over
706     // then scan the whole symbol list and copy any function
707     // symbols that match the file association
708 
709     for (i = 0; i < nb_syms; i++) {
710 	if (p->st_info == 4) {
711 	    name = (char *) symtab_section->link->data + p->st_name;
712 
713 	    // this is a file symbol, copy it over
714 
715 	    NewTable[n++] = *p;
716 
717 	    p2 = (Elf32_Sym *) symtab_section->data;
718 
719 	    for (j = 0; j < nb_syms; j++) {
720 		if (p2->st_info == 0x12) {
721 		    // this is a func symbol
722 
723 		    name2 =
724 			(char *) symtab_section->link->data + p2->st_name;
725 
726 		    // find the function data index
727 
728 		    for (k = 0; k < nFuncs; k++) {
729 			if (strcmp(name2, Func[k]) == 0)
730 			    break;
731 		    }
732 
733 		    if (k >= nFuncs) {
734 			char s[256];
735 
736 			sprintf(s,
737 				"debug (sort) info can't find function: %s",
738 				name2);
739 
740 			error(s);
741 		    }
742 
743 		    if (strcmp(AssociatedFile[k], name) == 0) {
744 			// yes they match copy it over
745 
746 			NewTable[n++] = *p2;
747 		    }
748 		}
749 		p2++;
750 	    }
751 	}
752 	p++;
753     }
754 
755     // now all the filename and func symbols should have been copied over
756     // copy all the rest over (all except file and funcs)
757 
758     p = (Elf32_Sym *) symtab_section->data;
759     for (i = 0; i < nb_syms; i++) {
760 	if (p->st_info != 4 && p->st_info != 0x12) {
761 	    NewTable[n++] = *p;
762 	}
763 	p++;
764     }
765 
766     if (n != nb_syms)
767 	error("Internal Compiler error, debug info");
768 
769     // copy it all back
770 
771     p = (Elf32_Sym *) symtab_section->data;
772     for (i = 0; i < nb_syms; i++) {
773 	*p++ = NewTable[i];
774     }
775 
776     tcc_free(NewTable);
777 }
778 
779 
FindCoffSymbolIndex(const char * func_name)780 int FindCoffSymbolIndex(const char *func_name)
781 {
782     int i, n = 0;
783     Elf32_Sym *p;
784     char *name;
785 
786     p = (Elf32_Sym *) symtab_section->data;
787 
788     for (i = 0; i < nb_syms; i++) {
789 
790 	name = (char *) symtab_section->link->data + p->st_name;
791 
792 	if (p->st_info == 4) {
793 	    // put a filename symbol
794 	    n++;
795 	} else if (p->st_info == 0x12) {
796 
797 	    if (strcmp(func_name, name) == 0)
798 		return n;
799 
800 	    n += 6;
801 
802 	    // put a Function Name
803 
804 	    // now put aux info
805 
806 	    // put a .bf
807 
808 	    // now put aux info
809 
810 	    // put a .ef
811 
812 	    // now put aux info
813 
814 	} else {
815 	    n += 2;
816 	}
817 
818 	p++;
819     }
820 
821     return n;			// total number of symbols
822 }
823 
OutputTheSection(Section * sect)824 BOOL OutputTheSection(Section * sect)
825 {
826     const char *s = sect->name;
827 
828     if (!strcmp(s, ".text"))
829 	return true;
830     else if (!strcmp(s, ".data"))
831 	return true;
832     else
833 	return 0;
834 }
835 
GetCoffFlags(const char * s)836 short int GetCoffFlags(const char *s)
837 {
838     if (!strcmp(s, ".text"))
839 	return STYP_TEXT | STYP_DATA | STYP_ALIGN | 0x400;
840     else if (!strcmp(s, ".data"))
841 	return STYP_DATA;
842     else if (!strcmp(s, ".bss"))
843 	return STYP_BSS;
844     else if (!strcmp(s, ".stack"))
845 	return STYP_BSS | STYP_ALIGN | 0x200;
846     else if (!strcmp(s, ".cinit"))
847 	return STYP_COPY | STYP_DATA | STYP_ALIGN | 0x200;
848     else
849 	return 0;
850 }
851 
FindSection(TCCState * s1,const char * sname)852 Section *FindSection(TCCState * s1, const char *sname)
853 {
854     Section *s;
855     int i;
856 
857     for (i = 1; i < s1->nb_sections; i++) {
858 	s = s1->sections[i];
859 
860 	if (!strcmp(sname, s->name))
861 	    return s;
862     }
863 
864     error("could not find section %s", sname);
865     return 0;
866 }
867 
tcc_load_coff(TCCState * s1,int fd)868 int tcc_load_coff(TCCState * s1, int fd)
869 {
870 // tktk TokenSym *ts;
871 
872     FILE *f;
873     unsigned int str_size;
874     char *Coff_str_table, *name;
875     int i, k;
876     struct syment csym;
877     char name2[9];
878     FILHDR file_hdr;		/* FILE HEADER STRUCTURE              */
879 
880     f = fdopen(fd, "rb");
881     if (!f) {
882 	error("Unable to open .out file for input");
883     }
884 
885     if (fread(&file_hdr, FILHSZ, 1, f) != 1)
886 	error("error reading .out file for input");
887 
888     if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
889 	error("error reading .out file for input");
890 
891     // first read the string table
892 
893     if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
894 	error("error reading .out file for input");
895 
896     if (fread(&str_size, sizeof(int), 1, f) != 1)
897 	error("error reading .out file for input");
898 
899 
900     Coff_str_table = (char *) tcc_malloc(str_size);
901 
902     if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
903 	error("error reading .out file for input");
904 
905     // read/process all the symbols
906 
907     // seek back to symbols
908 
909     if (fseek(f, file_hdr.f_symptr, SEEK_SET))
910 	error("error reading .out file for input");
911 
912     for (i = 0; i < file_hdr.f_nsyms; i++) {
913 	if (fread(&csym, SYMESZ, 1, f) != 1)
914 	    error("error reading .out file for input");
915 
916 	if (csym._n._n_n._n_zeroes == 0) {
917 	    name = Coff_str_table + csym._n._n_n._n_offset - 4;
918 	} else {
919 	    name = csym._n._n_name;
920 
921 	    if (name[7] != 0) {
922 		for (k = 0; k < 8; k++)
923 		    name2[k] = name[k];
924 
925 		name2[8] = 0;
926 
927 		name = name2;
928 	    }
929 	}
930 //              if (strcmp("_DAC_Buffer",name)==0)  // tktk
931 //                      name[0]=0;
932 
933 	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
934 	    (csym.n_type == 0x18 && csym.n_sclass == 0x2) ||	// pointer to structure
935 	    (csym.n_type == 0x7 && csym.n_sclass == 0x2) ||	// doubles
936 	    (csym.n_type == 0x6 && csym.n_sclass == 0x2))	// floats
937 	{
938 	    // strip off any leading underscore (except for other main routine)
939 
940 	    if (name[0] == '_' && strcmp(name, "_main") != 0)
941 		name++;
942 
943 	    tcc_add_symbol(s1, name, csym.n_value);
944 	}
945 	// skip any aux records
946 
947 	if (csym.n_numaux == 1) {
948 	    if (fread(&csym, SYMESZ, 1, f) != 1)
949 		error("error reading .out file for input");
950 	    i++;
951 	}
952     }
953 
954     return 0;
955 }
956