update
[tinycc.git] / tcccoff.c
blobecc91edad7a3283eb71b50bb719b1ee470589e2c
1 /*
2 * COFF file handling for TCC
3 *
4 * Copyright (c) 2003, 2004 TK
5 * Copyright (c) 2004 Fabrice Bellard
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.
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.
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
21 #include "coff.h"
23 #define MAXNSCNS 255 /* MAXIMUM NUMBER OF SECTIONS */
24 #define MAX_STR_TABLE 1000000
25 AOUTHDR o_filehdr; /* OPTIONAL (A.OUT) FILE HEADER */
27 SCNHDR section_header[MAXNSCNS];
29 #define MAX_FUNCS 1000
30 #define MAX_FUNC_NAME_LENGTH 128
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];
40 BOOL OutputTheSection(Section * sect);
41 short int GetCoffFlags(const char *s);
42 void SortSymbolTable(void);
43 Section *FindSection(TCCState * s1, const char *sname);
45 int C67_main_entry_point;
47 int FindCoffSymbolIndex(const char *func_name);
48 int nb_syms;
50 typedef struct {
51 long tag;
52 long size;
53 long fileptr;
54 long nextsym;
55 short int dummy;
56 } AUXFUNC;
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;
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;
76 int tcc_output_coff(TCCState *s1, FILE *f)
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;
86 stext = FindSection(s1, ".text");
87 sdata = FindSection(s1, ".data");
88 sbss = FindSection(s1, ".bss");
90 nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
91 coff_nb_syms = FindCoffSymbolIndex("XXXXXXXXXX1");
93 file_hdr.f_magic = COFF_C67_MAGIC; /* magic number */
94 file_hdr.f_timdat = 0; /* time & date stamp */
95 file_hdr.f_opthdr = sizeof(AOUTHDR); /* sizeof(optional hdr) */
96 file_hdr.f_flags = 0x1143; /* flags (copied from what code composer does) */
97 file_hdr.f_TargetID = 0x99; /* for C6x = 0x0099 */
99 o_filehdr.magic = 0x0108; /* see magic.h */
100 o_filehdr.vstamp = 0x0190; /* version stamp */
101 o_filehdr.tsize = stext->data_offset; /* text size in bytes, padded to FW bdry */
102 o_filehdr.dsize = sdata->data_offset; /* initialized data " " */
103 o_filehdr.bsize = sbss->data_offset; /* uninitialized data " " */
104 o_filehdr.entrypt = C67_main_entry_point; /* entry pt. */
105 o_filehdr.text_start = stext->sh_addr; /* base of text used for this file */
106 o_filehdr.data_start = sdata->sh_addr; /* base of data used for this file */
109 // create all the section headers
111 int i, NSectionsToOutput = 0;
113 file_pointer = FILHSZ + sizeof(AOUTHDR);
115 CoffTextSectionNo = -1;
117 for (i = 1; i < s1->nb_sections; i++) {
118 coff_sec = &section_header[i];
119 tcc_sect = s1->sections[i];
121 if (OutputTheSection(tcc_sect)) {
122 NSectionsToOutput++;
124 if (CoffTextSectionNo == -1 && tcc_sect == stext)
125 CoffTextSectionNo = NSectionsToOutput; // rem which coff sect number the .text sect is
127 strcpy(coff_sec->s_name, tcc_sect->name); /* section name */
129 coff_sec->s_paddr = tcc_sect->sh_addr; /* physical address */
130 coff_sec->s_vaddr = tcc_sect->sh_addr; /* virtual address */
131 coff_sec->s_size = tcc_sect->data_offset; /* section size */
132 coff_sec->s_scnptr = 0; /* file ptr to raw data for section */
133 coff_sec->s_relptr = 0; /* file ptr to relocation */
134 coff_sec->s_lnnoptr = 0; /* file ptr to line numbers */
135 coff_sec->s_nreloc = 0; /* number of relocation entries */
136 coff_sec->s_flags = GetCoffFlags(coff_sec->s_name); /* flags */
137 coff_sec->s_reserved = 0; /* reserved byte */
138 coff_sec->s_page = 0; /* memory page id */
140 file_pointer += sizeof(SCNHDR);
144 file_hdr.f_nscns = NSectionsToOutput; /* number of sections */
146 // now loop through and determine file pointer locations
147 // for the raw data
150 for (i = 1; i < s1->nb_sections; i++) {
151 coff_sec = &section_header[i];
152 tcc_sect = s1->sections[i];
154 if (OutputTheSection(tcc_sect)) {
155 // put raw data
156 coff_sec->s_scnptr = file_pointer; /* file ptr to raw data for section */
157 file_pointer += coff_sec->s_size;
161 // now loop through and determine file pointer locations
162 // for the relocation data
164 for (i = 1; i < s1->nb_sections; i++) {
165 coff_sec = &section_header[i];
166 tcc_sect = s1->sections[i];
168 if (OutputTheSection(tcc_sect)) {
169 // put relocations data
170 if (coff_sec->s_nreloc > 0) {
171 coff_sec->s_relptr = file_pointer; /* file ptr to relocation */
172 file_pointer += coff_sec->s_nreloc * sizeof(struct reloc);
177 // now loop through and determine file pointer locations
178 // for the line number data
180 for (i = 1; i < s1->nb_sections; i++) {
181 coff_sec = &section_header[i];
182 tcc_sect = s1->sections[i];
184 coff_sec->s_nlnno = 0;
185 coff_sec->s_lnnoptr = 0;
187 if (do_debug && tcc_sect == stext) {
188 // count how many line nos data
190 // also find association between source file name and function
191 // so we can sort the symbol table
194 Stab_Sym *sym, *sym_end;
195 char func_name[MAX_FUNC_NAME_LENGTH],
196 last_func_name[MAX_FUNC_NAME_LENGTH];
197 unsigned long func_addr, last_pc, pc;
198 const char *incl_files[INCLUDE_STACK_SIZE];
199 int incl_index, len, last_line_num;
200 const char *str, *p;
202 coff_sec->s_lnnoptr = file_pointer; /* file ptr to linno */
205 func_name[0] = '\0';
206 func_addr = 0;
207 incl_index = 0;
208 last_func_name[0] = '\0';
209 last_pc = 0xffffffff;
210 last_line_num = 1;
211 sym = (Stab_Sym *) stab_section->data + 1;
212 sym_end =
213 (Stab_Sym *) (stab_section->data +
214 stab_section->data_offset);
216 nFuncs = 0;
217 while (sym < sym_end) {
218 switch (sym->n_type) {
219 /* function start or end */
220 case N_FUN:
221 if (sym->n_strx == 0) {
222 // end of function
224 coff_sec->s_nlnno++;
225 file_pointer += LINESZ;
227 pc = sym->n_value + func_addr;
228 func_name[0] = '\0';
229 func_addr = 0;
230 EndAddress[nFuncs] = pc;
231 FuncEntries[nFuncs] =
232 (file_pointer -
233 LineNoFilePtr[nFuncs]) / LINESZ - 1;
234 LastLineNo[nFuncs++] = last_line_num + 1;
235 } else {
236 // beginning of function
238 LineNoFilePtr[nFuncs] = file_pointer;
239 coff_sec->s_nlnno++;
240 file_pointer += LINESZ;
242 str =
243 (const char *) stabstr_section->data +
244 sym->n_strx;
246 p = strchr(str, ':');
247 if (!p) {
248 pstrcpy(func_name, sizeof(func_name), str);
249 pstrcpy(Func[nFuncs], sizeof(func_name), str);
250 } else {
251 len = p - str;
252 if (len > sizeof(func_name) - 1)
253 len = sizeof(func_name) - 1;
254 memcpy(func_name, str, len);
255 memcpy(Func[nFuncs], str, len);
256 func_name[len] = '\0';
259 // save the file that it came in so we can sort later
260 pstrcpy(AssociatedFile[nFuncs], sizeof(func_name),
261 incl_files[incl_index - 1]);
263 func_addr = sym->n_value;
265 break;
267 /* line number info */
268 case N_SLINE:
269 pc = sym->n_value + func_addr;
271 last_pc = pc;
272 last_line_num = sym->n_desc;
274 /* XXX: slow! */
275 strcpy(last_func_name, func_name);
277 coff_sec->s_nlnno++;
278 file_pointer += LINESZ;
279 break;
280 /* include files */
281 case N_BINCL:
282 str =
283 (const char *) stabstr_section->data + sym->n_strx;
284 add_incl:
285 if (incl_index < INCLUDE_STACK_SIZE) {
286 incl_files[incl_index++] = str;
288 break;
289 case N_EINCL:
290 if (incl_index > 1)
291 incl_index--;
292 break;
293 case N_SO:
294 if (sym->n_strx == 0) {
295 incl_index = 0; /* end of translation unit */
296 } else {
297 str =
298 (const char *) stabstr_section->data +
299 sym->n_strx;
300 /* do not add path */
301 len = strlen(str);
302 if (len > 0 && str[len - 1] != '/')
303 goto add_incl;
305 break;
307 sym++;
313 file_hdr.f_symptr = file_pointer; /* file pointer to symtab */
315 if (do_debug)
316 file_hdr.f_nsyms = coff_nb_syms; /* number of symtab entries */
317 else
318 file_hdr.f_nsyms = 0;
320 file_pointer += file_hdr.f_nsyms * SYMNMLEN;
322 // OK now we are all set to write the file
325 fwrite(&file_hdr, FILHSZ, 1, f);
326 fwrite(&o_filehdr, sizeof(o_filehdr), 1, f);
328 // write section headers
329 for (i = 1; i < s1->nb_sections; i++) {
330 coff_sec = &section_header[i];
331 tcc_sect = s1->sections[i];
333 if (OutputTheSection(tcc_sect)) {
334 fwrite(coff_sec, sizeof(SCNHDR), 1, f);
338 // write raw data
339 for (i = 1; i < s1->nb_sections; i++) {
340 coff_sec = &section_header[i];
341 tcc_sect = s1->sections[i];
343 if (OutputTheSection(tcc_sect)) {
344 fwrite(tcc_sect->data, tcc_sect->data_offset, 1, f);
348 // write relocation data
349 for (i = 1; i < s1->nb_sections; i++) {
350 coff_sec = &section_header[i];
351 tcc_sect = s1->sections[i];
353 if (OutputTheSection(tcc_sect)) {
354 // put relocations data
355 if (coff_sec->s_nreloc > 0) {
356 fwrite(tcc_sect->reloc,
357 coff_sec->s_nreloc * sizeof(struct reloc), 1, f);
363 // group the symbols in order of filename, func1, func2, etc
364 // finally global symbols
366 if (do_debug)
367 SortSymbolTable();
369 // write line no data
371 for (i = 1; i < s1->nb_sections; i++) {
372 coff_sec = &section_header[i];
373 tcc_sect = s1->sections[i];
375 if (do_debug && tcc_sect == stext) {
376 // count how many line nos data
379 Stab_Sym *sym, *sym_end;
380 char func_name[128], last_func_name[128];
381 unsigned long func_addr, last_pc, pc;
382 const char *incl_files[INCLUDE_STACK_SIZE];
383 int incl_index, len, last_line_num;
384 const char *str, *p;
386 LINENO CoffLineNo;
388 func_name[0] = '\0';
389 func_addr = 0;
390 incl_index = 0;
391 last_func_name[0] = '\0';
392 last_pc = 0;
393 last_line_num = 1;
394 sym = (Stab_Sym *) stab_section->data + 1;
395 sym_end =
396 (Stab_Sym *) (stab_section->data +
397 stab_section->data_offset);
399 while (sym < sym_end) {
400 switch (sym->n_type) {
401 /* function start or end */
402 case N_FUN:
403 if (sym->n_strx == 0) {
404 // end of function
406 CoffLineNo.l_addr.l_paddr = last_pc;
407 CoffLineNo.l_lnno = last_line_num + 1;
408 fwrite(&CoffLineNo, 6, 1, f);
410 pc = sym->n_value + func_addr;
411 func_name[0] = '\0';
412 func_addr = 0;
413 } else {
414 // beginning of function
416 str =
417 (const char *) stabstr_section->data +
418 sym->n_strx;
421 p = strchr(str, ':');
422 if (!p) {
423 pstrcpy(func_name, sizeof(func_name), str);
424 } else {
425 len = p - str;
426 if (len > sizeof(func_name) - 1)
427 len = sizeof(func_name) - 1;
428 memcpy(func_name, str, len);
429 func_name[len] = '\0';
431 func_addr = sym->n_value;
432 last_pc = func_addr;
433 last_line_num = -1;
435 // output a function begin
437 CoffLineNo.l_addr.l_symndx =
438 FindCoffSymbolIndex(func_name);
439 CoffLineNo.l_lnno = 0;
441 fwrite(&CoffLineNo, 6, 1, f);
443 break;
445 /* line number info */
446 case N_SLINE:
447 pc = sym->n_value + func_addr;
450 /* XXX: slow! */
451 strcpy(last_func_name, func_name);
453 // output a line reference
455 CoffLineNo.l_addr.l_paddr = last_pc;
457 if (last_line_num == -1) {
458 CoffLineNo.l_lnno = sym->n_desc;
459 } else {
460 CoffLineNo.l_lnno = last_line_num + 1;
463 fwrite(&CoffLineNo, 6, 1, f);
465 last_pc = pc;
466 last_line_num = sym->n_desc;
468 break;
470 /* include files */
471 case N_BINCL:
472 str =
473 (const char *) stabstr_section->data + sym->n_strx;
474 add_incl2:
475 if (incl_index < INCLUDE_STACK_SIZE) {
476 incl_files[incl_index++] = str;
478 break;
479 case N_EINCL:
480 if (incl_index > 1)
481 incl_index--;
482 break;
483 case N_SO:
484 if (sym->n_strx == 0) {
485 incl_index = 0; /* end of translation unit */
486 } else {
487 str =
488 (const char *) stabstr_section->data +
489 sym->n_strx;
490 /* do not add path */
491 len = strlen(str);
492 if (len > 0 && str[len - 1] != '/')
493 goto add_incl2;
495 break;
497 sym++;
502 // write symbol table
503 if (do_debug) {
504 int k;
505 struct syment csym;
506 AUXFUNC auxfunc;
507 AUXBF auxbf;
508 AUXEF auxef;
509 int i;
510 Elf32_Sym *p;
511 char *name;
512 char _name[MAX_FUNCS];
513 int nstr;
514 int n = 0;
516 Coff_str_table = (char *) tcc_malloc(MAX_STR_TABLE);
517 pCoff_str_table = Coff_str_table;
518 nstr = 0;
520 p = (Elf32_Sym *) symtab_section->data;
523 for (i = 0; i < nb_syms; i++) {
525 /* don't add underscores for Code Composer Version 2.xx */
527 #define ADD_UNDERSCORE 0
529 name = (char *) symtab_section->link->data + p->st_name;
531 #if ADD_UNDERSCORE
532 _name[0] = '_';
533 strcpy(_name + 1, name);
534 #else
535 strcpy(_name, name);
536 #endif
538 for (k = 0; k < 8; k++)
539 csym._n._n_name[k] = 0;
541 if (strlen(_name) <= 8) {
542 strcpy(csym._n._n_name, _name);
543 } else {
544 if (pCoff_str_table - Coff_str_table + strlen(_name) >
545 MAX_STR_TABLE - 1)
546 error("String table too large");
548 csym._n._n_n._n_zeroes = 0;
549 csym._n._n_n._n_offset =
550 pCoff_str_table - Coff_str_table + 4;
552 strcpy(pCoff_str_table, _name);
553 pCoff_str_table += strlen(_name) + 1; // skip over null
554 nstr++;
557 if (p->st_info == 4) {
558 // put a filename symbol
559 csym.n_value = 33; // ?????
560 csym.n_scnum = N_DEBUG;
561 csym.n_type = 0;
562 csym.n_sclass = C_FILE;
563 csym.n_numaux = 0;
564 fwrite(&csym, 18, 1, f);
565 n++;
567 } else if (p->st_info == 0x12) {
568 // find the function data
570 for (k = 0; k < nFuncs; k++) {
571 if (strcmp(name, Func[k]) == 0)
572 break;
575 if (k >= nFuncs) {
576 char s[256];
578 sprintf(s, "debug info can't find function: %s", name);
580 error(s);
582 // put a Function Name
584 csym.n_value = p->st_value; // physical address
585 csym.n_scnum = CoffTextSectionNo;
586 csym.n_type = MKTYPE(T_INT, DT_FCN, 0, 0, 0, 0, 0);
587 csym.n_sclass = C_EXT;
588 csym.n_numaux = 1;
589 fwrite(&csym, 18, 1, f);
591 // now put aux info
593 auxfunc.tag = 0;
594 auxfunc.size = EndAddress[k] - p->st_value;
595 auxfunc.fileptr = LineNoFilePtr[k];
596 auxfunc.nextsym = n + 6; // tktk
597 auxfunc.dummy = 0;
598 fwrite(&auxfunc, 18, 1, f);
600 // put a .bf
602 strcpy(csym._n._n_name, ".bf");
603 csym.n_value = p->st_value; // physical address
604 csym.n_scnum = CoffTextSectionNo;
605 csym.n_type = 0;
606 csym.n_sclass = C_FCN;
607 csym.n_numaux = 1;
608 fwrite(&csym, 18, 1, f);
610 // now put aux info
612 auxbf.regmask = 0;
613 auxbf.lineno = 0;
614 auxbf.nentries = FuncEntries[k];
615 auxbf.localframe = 0;
616 auxbf.nextentry = n + 6;
617 auxbf.dummy = 0;
618 fwrite(&auxbf, 18, 1, f);
620 // put a .ef
622 strcpy(csym._n._n_name, ".ef");
623 csym.n_value = EndAddress[k]; // physical address
624 csym.n_scnum = CoffTextSectionNo;
625 csym.n_type = 0;
626 csym.n_sclass = C_FCN;
627 csym.n_numaux = 1;
628 fwrite(&csym, 18, 1, f);
630 // now put aux info
632 auxef.dummy = 0;
633 auxef.lineno = LastLineNo[k];
634 auxef.dummy1 = 0;
635 auxef.dummy2 = 0;
636 auxef.dummy3 = 0;
637 auxef.dummy4 = 0;
638 fwrite(&auxef, 18, 1, f);
640 n += 6;
642 } else {
643 // try an put some type info
645 if ((p->st_other & VT_BTYPE) == VT_DOUBLE) {
646 csym.n_type = T_DOUBLE; // int
647 csym.n_sclass = C_EXT;
648 } else if ((p->st_other & VT_BTYPE) == VT_FLOAT) {
649 csym.n_type = T_FLOAT;
650 csym.n_sclass = C_EXT;
651 } else if ((p->st_other & VT_BTYPE) == VT_INT) {
652 csym.n_type = T_INT; // int
653 csym.n_sclass = C_EXT;
654 } else if ((p->st_other & VT_BTYPE) == VT_SHORT) {
655 csym.n_type = T_SHORT;
656 csym.n_sclass = C_EXT;
657 } else if ((p->st_other & VT_BTYPE) == VT_BYTE) {
658 csym.n_type = T_CHAR;
659 csym.n_sclass = C_EXT;
660 } else {
661 csym.n_type = T_INT; // just mark as a label
662 csym.n_sclass = C_LABEL;
666 csym.n_value = p->st_value;
667 csym.n_scnum = 2;
668 csym.n_numaux = 1;
669 fwrite(&csym, 18, 1, f);
671 auxfunc.tag = 0;
672 auxfunc.size = 0x20;
673 auxfunc.fileptr = 0;
674 auxfunc.nextsym = 0;
675 auxfunc.dummy = 0;
676 fwrite(&auxfunc, 18, 1, f);
677 n++;
678 n++;
682 p++;
686 if (do_debug) {
687 // write string table
689 // first write the size
690 i = pCoff_str_table - Coff_str_table;
691 fwrite(&i, 4, 1, f);
693 // then write the strings
694 fwrite(Coff_str_table, i, 1, f);
696 tcc_free(Coff_str_table);
699 return 0;
704 // group the symbols in order of filename, func1, func2, etc
705 // finally global symbols
707 void SortSymbolTable(void)
709 int i, j, k, n = 0;
710 Elf32_Sym *p, *p2, *NewTable;
711 char *name, *name2;
713 NewTable = (Elf32_Sym *) tcc_malloc(nb_syms * sizeof(Elf32_Sym));
715 p = (Elf32_Sym *) symtab_section->data;
718 // find a file symbol, copy it over
719 // then scan the whole symbol list and copy any function
720 // symbols that match the file association
722 for (i = 0; i < nb_syms; i++) {
723 if (p->st_info == 4) {
724 name = (char *) symtab_section->link->data + p->st_name;
726 // this is a file symbol, copy it over
728 NewTable[n++] = *p;
730 p2 = (Elf32_Sym *) symtab_section->data;
732 for (j = 0; j < nb_syms; j++) {
733 if (p2->st_info == 0x12) {
734 // this is a func symbol
736 name2 =
737 (char *) symtab_section->link->data + p2->st_name;
739 // find the function data index
741 for (k = 0; k < nFuncs; k++) {
742 if (strcmp(name2, Func[k]) == 0)
743 break;
746 if (k >= nFuncs) {
747 char s[256];
749 sprintf(s,
750 "debug (sort) info can't find function: %s",
751 name2);
753 error(s);
756 if (strcmp(AssociatedFile[k], name) == 0) {
757 // yes they match copy it over
759 NewTable[n++] = *p2;
762 p2++;
765 p++;
768 // now all the filename and func symbols should have been copied over
769 // copy all the rest over (all except file and funcs)
771 p = (Elf32_Sym *) symtab_section->data;
772 for (i = 0; i < nb_syms; i++) {
773 if (p->st_info != 4 && p->st_info != 0x12) {
774 NewTable[n++] = *p;
776 p++;
779 if (n != nb_syms)
780 error("Internal Compiler error, debug info");
782 // copy it all back
784 p = (Elf32_Sym *) symtab_section->data;
785 for (i = 0; i < nb_syms; i++) {
786 *p++ = NewTable[i];
789 tcc_free(NewTable);
793 int FindCoffSymbolIndex(const char *func_name)
795 int i, n = 0;
796 Elf32_Sym *p;
797 char *name;
799 p = (Elf32_Sym *) symtab_section->data;
801 for (i = 0; i < nb_syms; i++) {
803 name = (char *) symtab_section->link->data + p->st_name;
805 if (p->st_info == 4) {
806 // put a filename symbol
807 n++;
808 } else if (p->st_info == 0x12) {
810 if (strcmp(func_name, name) == 0)
811 return n;
813 n += 6;
815 // put a Function Name
817 // now put aux info
819 // put a .bf
821 // now put aux info
823 // put a .ef
825 // now put aux info
827 } else {
828 n += 2;
831 p++;
834 return n; // total number of symbols
837 BOOL OutputTheSection(Section * sect)
839 const char *s = sect->name;
841 if (!strcmp(s, ".text"))
842 return true;
843 else if (!strcmp(s, ".data"))
844 return true;
845 else
846 return 0;
849 short int GetCoffFlags(const char *s)
851 if (!strcmp(s, ".text"))
852 return STYP_TEXT | STYP_DATA | STYP_ALIGN | 0x400;
853 else if (!strcmp(s, ".data"))
854 return STYP_DATA;
855 else if (!strcmp(s, ".bss"))
856 return STYP_BSS;
857 else if (!strcmp(s, ".stack"))
858 return STYP_BSS | STYP_ALIGN | 0x200;
859 else if (!strcmp(s, ".cinit"))
860 return STYP_COPY | STYP_DATA | STYP_ALIGN | 0x200;
861 else
862 return 0;
865 Section *FindSection(TCCState * s1, const char *sname)
867 Section *s;
868 int i;
870 for (i = 1; i < s1->nb_sections; i++) {
871 s = s1->sections[i];
873 if (!strcmp(sname, s->name))
874 return s;
877 error("could not find section %s", sname);
878 return 0;
881 int tcc_load_coff(TCCState * s1, int fd)
883 // tktk TokenSym *ts;
885 FILE *f;
886 unsigned int str_size;
887 char *Coff_str_table, *name;
888 int i, k;
889 struct syment csym;
890 char name2[9];
891 FILHDR file_hdr; /* FILE HEADER STRUCTURE */
893 f = fdopen(fd, "rb");
894 if (!f) {
895 error("Unable to open .out file for input");
898 if (fread(&file_hdr, FILHSZ, 1, f) != 1)
899 error("error reading .out file for input");
901 if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
902 error("error reading .out file for input");
904 // first read the string table
906 if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
907 error("error reading .out file for input");
909 if (fread(&str_size, sizeof(int), 1, f) != 1)
910 error("error reading .out file for input");
913 Coff_str_table = (char *) tcc_malloc(str_size);
915 if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
916 error("error reading .out file for input");
918 // read/process all the symbols
920 // seek back to symbols
922 if (fseek(f, file_hdr.f_symptr, SEEK_SET))
923 error("error reading .out file for input");
925 for (i = 0; i < file_hdr.f_nsyms; i++) {
926 if (fread(&csym, SYMESZ, 1, f) != 1)
927 error("error reading .out file for input");
929 if (csym._n._n_n._n_zeroes == 0) {
930 name = Coff_str_table + csym._n._n_n._n_offset - 4;
931 } else {
932 name = csym._n._n_name;
934 if (name[7] != 0) {
935 for (k = 0; k < 8; k++)
936 name2[k] = name[k];
938 name2[8] = 0;
940 name = name2;
943 // if (strcmp("_DAC_Buffer",name)==0) // tktk
944 // name[0]=0;
946 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
947 (csym.n_type == 0x18 && csym.n_sclass == 0x2) || // pointer to structure
948 (csym.n_type == 0x7 && csym.n_sclass == 0x2) || // doubles
949 (csym.n_type == 0x6 && csym.n_sclass == 0x2)) // floats
951 // strip off any leading underscore (except for other main routine)
953 if (name[0] == '_' && strcmp(name, "_main") != 0)
954 name++;
956 tcc_add_symbol(s1, name, csym.n_value);
958 // skip any aux records
960 if (csym.n_numaux == 1) {
961 if (fread(&csym, SYMESZ, 1, f) != 1)
962 error("error reading .out file for input");
963 i++;
967 return 0;