Fixed compilation error in i386-asm.c
[tinycc/kirr.git] / tcccoff.c
blob0dcbe50f0c3fa4936f348c09f09d4be4a3137783
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;
85 int i, NSectionsToOutput = 0;
87 Coff_str_table = pCoff_str_table = NULL;
89 stext = FindSection(s1, ".text");
90 sdata = FindSection(s1, ".data");
91 sbss = FindSection(s1, ".bss");
93 nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
94 coff_nb_syms = FindCoffSymbolIndex("XXXXXXXXXX1");
96 file_hdr.f_magic = COFF_C67_MAGIC; /* magic number */
97 file_hdr.f_timdat = 0; /* time & date stamp */
98 file_hdr.f_opthdr = sizeof(AOUTHDR); /* sizeof(optional hdr) */
99 file_hdr.f_flags = 0x1143; /* flags (copied from what code composer does) */
100 file_hdr.f_TargetID = 0x99; /* for C6x = 0x0099 */
102 o_filehdr.magic = 0x0108; /* see magic.h */
103 o_filehdr.vstamp = 0x0190; /* version stamp */
104 o_filehdr.tsize = stext->data_offset; /* text size in bytes, padded to FW bdry */
105 o_filehdr.dsize = sdata->data_offset; /* initialized data " " */
106 o_filehdr.bsize = sbss->data_offset; /* uninitialized data " " */
107 o_filehdr.entrypt = C67_main_entry_point; /* entry pt. */
108 o_filehdr.text_start = stext->sh_addr; /* base of text used for this file */
109 o_filehdr.data_start = sdata->sh_addr; /* base of data used for this file */
112 // create all the section headers
114 file_pointer = FILHSZ + sizeof(AOUTHDR);
116 CoffTextSectionNo = -1;
118 for (i = 1; i < s1->nb_sections; i++) {
119 coff_sec = &section_header[i];
120 tcc_sect = s1->sections[i];
122 if (OutputTheSection(tcc_sect)) {
123 NSectionsToOutput++;
125 if (CoffTextSectionNo == -1 && tcc_sect == stext)
126 CoffTextSectionNo = NSectionsToOutput; // rem which coff sect number the .text sect is
128 strcpy(coff_sec->s_name, tcc_sect->name); /* section name */
130 coff_sec->s_paddr = tcc_sect->sh_addr; /* physical address */
131 coff_sec->s_vaddr = tcc_sect->sh_addr; /* virtual address */
132 coff_sec->s_size = tcc_sect->data_offset; /* section size */
133 coff_sec->s_scnptr = 0; /* file ptr to raw data for section */
134 coff_sec->s_relptr = 0; /* file ptr to relocation */
135 coff_sec->s_lnnoptr = 0; /* file ptr to line numbers */
136 coff_sec->s_nreloc = 0; /* number of relocation entries */
137 coff_sec->s_flags = GetCoffFlags(coff_sec->s_name); /* flags */
138 coff_sec->s_reserved = 0; /* reserved byte */
139 coff_sec->s_page = 0; /* memory page id */
141 file_pointer += sizeof(SCNHDR);
145 file_hdr.f_nscns = NSectionsToOutput; /* number of sections */
147 // now loop through and determine file pointer locations
148 // for the raw data
151 for (i = 1; i < s1->nb_sections; i++) {
152 coff_sec = &section_header[i];
153 tcc_sect = s1->sections[i];
155 if (OutputTheSection(tcc_sect)) {
156 // put raw data
157 coff_sec->s_scnptr = file_pointer; /* file ptr to raw data for section */
158 file_pointer += coff_sec->s_size;
162 // now loop through and determine file pointer locations
163 // for the relocation data
165 for (i = 1; i < s1->nb_sections; i++) {
166 coff_sec = &section_header[i];
167 tcc_sect = s1->sections[i];
169 if (OutputTheSection(tcc_sect)) {
170 // put relocations data
171 if (coff_sec->s_nreloc > 0) {
172 coff_sec->s_relptr = file_pointer; /* file ptr to relocation */
173 file_pointer += coff_sec->s_nreloc * sizeof(struct reloc);
178 // now loop through and determine file pointer locations
179 // for the line number data
181 for (i = 1; i < s1->nb_sections; i++) {
182 coff_sec = &section_header[i];
183 tcc_sect = s1->sections[i];
185 coff_sec->s_nlnno = 0;
186 coff_sec->s_lnnoptr = 0;
188 if (s1->do_debug && tcc_sect == stext) {
189 // count how many line nos data
191 // also find association between source file name and function
192 // so we can sort the symbol table
195 Stab_Sym *sym, *sym_end;
196 char func_name[MAX_FUNC_NAME_LENGTH],
197 last_func_name[MAX_FUNC_NAME_LENGTH];
198 unsigned long func_addr, last_pc, pc;
199 const char *incl_files[INCLUDE_STACK_SIZE];
200 int incl_index, len, last_line_num;
201 const char *str, *p;
203 coff_sec->s_lnnoptr = file_pointer; /* file ptr to linno */
206 func_name[0] = '\0';
207 func_addr = 0;
208 incl_index = 0;
209 last_func_name[0] = '\0';
210 last_pc = 0xffffffff;
211 last_line_num = 1;
212 sym = (Stab_Sym *) stab_section->data + 1;
213 sym_end =
214 (Stab_Sym *) (stab_section->data +
215 stab_section->data_offset);
217 nFuncs = 0;
218 while (sym < sym_end) {
219 switch (sym->n_type) {
220 /* function start or end */
221 case N_FUN:
222 if (sym->n_strx == 0) {
223 // end of function
225 coff_sec->s_nlnno++;
226 file_pointer += LINESZ;
228 pc = sym->n_value + func_addr;
229 func_name[0] = '\0';
230 func_addr = 0;
231 EndAddress[nFuncs] = pc;
232 FuncEntries[nFuncs] =
233 (file_pointer -
234 LineNoFilePtr[nFuncs]) / LINESZ - 1;
235 LastLineNo[nFuncs++] = last_line_num + 1;
236 } else {
237 // beginning of function
239 LineNoFilePtr[nFuncs] = file_pointer;
240 coff_sec->s_nlnno++;
241 file_pointer += LINESZ;
243 str =
244 (const char *) stabstr_section->data +
245 sym->n_strx;
247 p = strchr(str, ':');
248 if (!p) {
249 pstrcpy(func_name, sizeof(func_name), str);
250 pstrcpy(Func[nFuncs], sizeof(func_name), str);
251 } else {
252 len = p - str;
253 if (len > sizeof(func_name) - 1)
254 len = sizeof(func_name) - 1;
255 memcpy(func_name, str, len);
256 memcpy(Func[nFuncs], str, len);
257 func_name[len] = '\0';
260 // save the file that it came in so we can sort later
261 pstrcpy(AssociatedFile[nFuncs], sizeof(func_name),
262 incl_files[incl_index - 1]);
264 func_addr = sym->n_value;
266 break;
268 /* line number info */
269 case N_SLINE:
270 pc = sym->n_value + func_addr;
272 last_pc = pc;
273 last_line_num = sym->n_desc;
275 /* XXX: slow! */
276 strcpy(last_func_name, func_name);
278 coff_sec->s_nlnno++;
279 file_pointer += LINESZ;
280 break;
281 /* include files */
282 case N_BINCL:
283 str =
284 (const char *) stabstr_section->data + sym->n_strx;
285 add_incl:
286 if (incl_index < INCLUDE_STACK_SIZE) {
287 incl_files[incl_index++] = str;
289 break;
290 case N_EINCL:
291 if (incl_index > 1)
292 incl_index--;
293 break;
294 case N_SO:
295 if (sym->n_strx == 0) {
296 incl_index = 0; /* end of translation unit */
297 } else {
298 str =
299 (const char *) stabstr_section->data +
300 sym->n_strx;
301 /* do not add path */
302 len = strlen(str);
303 if (len > 0 && str[len - 1] != '/')
304 goto add_incl;
306 break;
308 sym++;
314 file_hdr.f_symptr = file_pointer; /* file pointer to symtab */
316 if (s1->do_debug)
317 file_hdr.f_nsyms = coff_nb_syms; /* number of symtab entries */
318 else
319 file_hdr.f_nsyms = 0;
321 file_pointer += file_hdr.f_nsyms * SYMNMLEN;
323 // OK now we are all set to write the file
326 fwrite(&file_hdr, FILHSZ, 1, f);
327 fwrite(&o_filehdr, sizeof(o_filehdr), 1, f);
329 // write section headers
330 for (i = 1; i < s1->nb_sections; i++) {
331 coff_sec = &section_header[i];
332 tcc_sect = s1->sections[i];
334 if (OutputTheSection(tcc_sect)) {
335 fwrite(coff_sec, sizeof(SCNHDR), 1, f);
339 // write raw data
340 for (i = 1; i < s1->nb_sections; i++) {
341 coff_sec = &section_header[i];
342 tcc_sect = s1->sections[i];
344 if (OutputTheSection(tcc_sect)) {
345 fwrite(tcc_sect->data, tcc_sect->data_offset, 1, f);
349 // write relocation data
350 for (i = 1; i < s1->nb_sections; i++) {
351 coff_sec = &section_header[i];
352 tcc_sect = s1->sections[i];
354 if (OutputTheSection(tcc_sect)) {
355 // put relocations data
356 if (coff_sec->s_nreloc > 0) {
357 fwrite(tcc_sect->reloc,
358 coff_sec->s_nreloc * sizeof(struct reloc), 1, f);
364 // group the symbols in order of filename, func1, func2, etc
365 // finally global symbols
367 if (s1->do_debug)
368 SortSymbolTable();
370 // write line no data
372 for (i = 1; i < s1->nb_sections; i++) {
373 coff_sec = &section_header[i];
374 tcc_sect = s1->sections[i];
376 if (s1->do_debug && tcc_sect == stext) {
377 // count how many line nos data
380 Stab_Sym *sym, *sym_end;
381 char func_name[128], last_func_name[128];
382 unsigned long func_addr, last_pc, pc;
383 const char *incl_files[INCLUDE_STACK_SIZE];
384 int incl_index, len, last_line_num;
385 const char *str, *p;
387 LINENO CoffLineNo;
389 func_name[0] = '\0';
390 func_addr = 0;
391 incl_index = 0;
392 last_func_name[0] = '\0';
393 last_pc = 0;
394 last_line_num = 1;
395 sym = (Stab_Sym *) stab_section->data + 1;
396 sym_end =
397 (Stab_Sym *) (stab_section->data +
398 stab_section->data_offset);
400 while (sym < sym_end) {
401 switch (sym->n_type) {
402 /* function start or end */
403 case N_FUN:
404 if (sym->n_strx == 0) {
405 // end of function
407 CoffLineNo.l_addr.l_paddr = last_pc;
408 CoffLineNo.l_lnno = last_line_num + 1;
409 fwrite(&CoffLineNo, 6, 1, f);
411 pc = sym->n_value + func_addr;
412 func_name[0] = '\0';
413 func_addr = 0;
414 } else {
415 // beginning of function
417 str =
418 (const char *) stabstr_section->data +
419 sym->n_strx;
422 p = strchr(str, ':');
423 if (!p) {
424 pstrcpy(func_name, sizeof(func_name), str);
425 } else {
426 len = p - str;
427 if (len > sizeof(func_name) - 1)
428 len = sizeof(func_name) - 1;
429 memcpy(func_name, str, len);
430 func_name[len] = '\0';
432 func_addr = sym->n_value;
433 last_pc = func_addr;
434 last_line_num = -1;
436 // output a function begin
438 CoffLineNo.l_addr.l_symndx =
439 FindCoffSymbolIndex(func_name);
440 CoffLineNo.l_lnno = 0;
442 fwrite(&CoffLineNo, 6, 1, f);
444 break;
446 /* line number info */
447 case N_SLINE:
448 pc = sym->n_value + func_addr;
451 /* XXX: slow! */
452 strcpy(last_func_name, func_name);
454 // output a line reference
456 CoffLineNo.l_addr.l_paddr = last_pc;
458 if (last_line_num == -1) {
459 CoffLineNo.l_lnno = sym->n_desc;
460 } else {
461 CoffLineNo.l_lnno = last_line_num + 1;
464 fwrite(&CoffLineNo, 6, 1, f);
466 last_pc = pc;
467 last_line_num = sym->n_desc;
469 break;
471 /* include files */
472 case N_BINCL:
473 str =
474 (const char *) stabstr_section->data + sym->n_strx;
475 add_incl2:
476 if (incl_index < INCLUDE_STACK_SIZE) {
477 incl_files[incl_index++] = str;
479 break;
480 case N_EINCL:
481 if (incl_index > 1)
482 incl_index--;
483 break;
484 case N_SO:
485 if (sym->n_strx == 0) {
486 incl_index = 0; /* end of translation unit */
487 } else {
488 str =
489 (const char *) stabstr_section->data +
490 sym->n_strx;
491 /* do not add path */
492 len = strlen(str);
493 if (len > 0 && str[len - 1] != '/')
494 goto add_incl2;
496 break;
498 sym++;
503 // write symbol table
504 if (s1->do_debug) {
505 int k;
506 struct syment csym;
507 AUXFUNC auxfunc;
508 AUXBF auxbf;
509 AUXEF auxef;
510 int i;
511 Elf32_Sym *p;
512 const char *name;
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 name = symtab_section->link->data + p->st_name;
527 for (k = 0; k < 8; k++)
528 csym._n._n_name[k] = 0;
530 if (strlen(name) <= 8) {
531 strcpy(csym._n._n_name, name);
532 } else {
533 if (pCoff_str_table - Coff_str_table + strlen(name) >
534 MAX_STR_TABLE - 1)
535 error("String table too large");
537 csym._n._n_n._n_zeroes = 0;
538 csym._n._n_n._n_offset =
539 pCoff_str_table - Coff_str_table + 4;
541 strcpy(pCoff_str_table, name);
542 pCoff_str_table += strlen(name) + 1; // skip over null
543 nstr++;
546 if (p->st_info == 4) {
547 // put a filename symbol
548 csym.n_value = 33; // ?????
549 csym.n_scnum = N_DEBUG;
550 csym.n_type = 0;
551 csym.n_sclass = C_FILE;
552 csym.n_numaux = 0;
553 fwrite(&csym, 18, 1, f);
554 n++;
556 } else if (p->st_info == 0x12) {
557 // find the function data
559 for (k = 0; k < nFuncs; k++) {
560 if (strcmp(name, Func[k]) == 0)
561 break;
564 if (k >= nFuncs) {
565 char s[256];
567 sprintf(s, "debug info can't find function: %s", name);
569 error(s);
571 // put a Function Name
573 csym.n_value = p->st_value; // physical address
574 csym.n_scnum = CoffTextSectionNo;
575 csym.n_type = MKTYPE(T_INT, DT_FCN, 0, 0, 0, 0, 0);
576 csym.n_sclass = C_EXT;
577 csym.n_numaux = 1;
578 fwrite(&csym, 18, 1, f);
580 // now put aux info
582 auxfunc.tag = 0;
583 auxfunc.size = EndAddress[k] - p->st_value;
584 auxfunc.fileptr = LineNoFilePtr[k];
585 auxfunc.nextsym = n + 6; // tktk
586 auxfunc.dummy = 0;
587 fwrite(&auxfunc, 18, 1, f);
589 // put a .bf
591 strcpy(csym._n._n_name, ".bf");
592 csym.n_value = p->st_value; // physical address
593 csym.n_scnum = CoffTextSectionNo;
594 csym.n_type = 0;
595 csym.n_sclass = C_FCN;
596 csym.n_numaux = 1;
597 fwrite(&csym, 18, 1, f);
599 // now put aux info
601 auxbf.regmask = 0;
602 auxbf.lineno = 0;
603 auxbf.nentries = FuncEntries[k];
604 auxbf.localframe = 0;
605 auxbf.nextentry = n + 6;
606 auxbf.dummy = 0;
607 fwrite(&auxbf, 18, 1, f);
609 // put a .ef
611 strcpy(csym._n._n_name, ".ef");
612 csym.n_value = EndAddress[k]; // physical address
613 csym.n_scnum = CoffTextSectionNo;
614 csym.n_type = 0;
615 csym.n_sclass = C_FCN;
616 csym.n_numaux = 1;
617 fwrite(&csym, 18, 1, f);
619 // now put aux info
621 auxef.dummy = 0;
622 auxef.lineno = LastLineNo[k];
623 auxef.dummy1 = 0;
624 auxef.dummy2 = 0;
625 auxef.dummy3 = 0;
626 auxef.dummy4 = 0;
627 fwrite(&auxef, 18, 1, f);
629 n += 6;
631 } else {
632 // try an put some type info
634 if ((p->st_other & VT_BTYPE) == VT_DOUBLE) {
635 csym.n_type = T_DOUBLE; // int
636 csym.n_sclass = C_EXT;
637 } else if ((p->st_other & VT_BTYPE) == VT_FLOAT) {
638 csym.n_type = T_FLOAT;
639 csym.n_sclass = C_EXT;
640 } else if ((p->st_other & VT_BTYPE) == VT_INT) {
641 csym.n_type = T_INT; // int
642 csym.n_sclass = C_EXT;
643 } else if ((p->st_other & VT_BTYPE) == VT_SHORT) {
644 csym.n_type = T_SHORT;
645 csym.n_sclass = C_EXT;
646 } else if ((p->st_other & VT_BTYPE) == VT_BYTE) {
647 csym.n_type = T_CHAR;
648 csym.n_sclass = C_EXT;
649 } else {
650 csym.n_type = T_INT; // just mark as a label
651 csym.n_sclass = C_LABEL;
655 csym.n_value = p->st_value;
656 csym.n_scnum = 2;
657 csym.n_numaux = 1;
658 fwrite(&csym, 18, 1, f);
660 auxfunc.tag = 0;
661 auxfunc.size = 0x20;
662 auxfunc.fileptr = 0;
663 auxfunc.nextsym = 0;
664 auxfunc.dummy = 0;
665 fwrite(&auxfunc, 18, 1, f);
666 n++;
667 n++;
671 p++;
675 if (s1->do_debug) {
676 // write string table
678 // first write the size
679 i = pCoff_str_table - Coff_str_table;
680 fwrite(&i, 4, 1, f);
682 // then write the strings
683 fwrite(Coff_str_table, i, 1, f);
685 tcc_free(Coff_str_table);
688 return 0;
693 // group the symbols in order of filename, func1, func2, etc
694 // finally global symbols
696 void SortSymbolTable(void)
698 int i, j, k, n = 0;
699 Elf32_Sym *p, *p2, *NewTable;
700 char *name, *name2;
702 NewTable = (Elf32_Sym *) tcc_malloc(nb_syms * sizeof(Elf32_Sym));
704 p = (Elf32_Sym *) symtab_section->data;
707 // find a file symbol, copy it over
708 // then scan the whole symbol list and copy any function
709 // symbols that match the file association
711 for (i = 0; i < nb_syms; i++) {
712 if (p->st_info == 4) {
713 name = (char *) symtab_section->link->data + p->st_name;
715 // this is a file symbol, copy it over
717 NewTable[n++] = *p;
719 p2 = (Elf32_Sym *) symtab_section->data;
721 for (j = 0; j < nb_syms; j++) {
722 if (p2->st_info == 0x12) {
723 // this is a func symbol
725 name2 =
726 (char *) symtab_section->link->data + p2->st_name;
728 // find the function data index
730 for (k = 0; k < nFuncs; k++) {
731 if (strcmp(name2, Func[k]) == 0)
732 break;
735 if (k >= nFuncs) {
736 char s[256];
738 sprintf(s,
739 "debug (sort) info can't find function: %s",
740 name2);
742 error(s);
745 if (strcmp(AssociatedFile[k], name) == 0) {
746 // yes they match copy it over
748 NewTable[n++] = *p2;
751 p2++;
754 p++;
757 // now all the filename and func symbols should have been copied over
758 // copy all the rest over (all except file and funcs)
760 p = (Elf32_Sym *) symtab_section->data;
761 for (i = 0; i < nb_syms; i++) {
762 if (p->st_info != 4 && p->st_info != 0x12) {
763 NewTable[n++] = *p;
765 p++;
768 if (n != nb_syms)
769 error("Internal Compiler error, debug info");
771 // copy it all back
773 p = (Elf32_Sym *) symtab_section->data;
774 for (i = 0; i < nb_syms; i++) {
775 *p++ = NewTable[i];
778 tcc_free(NewTable);
782 int FindCoffSymbolIndex(const char *func_name)
784 int i, n = 0;
785 Elf32_Sym *p;
786 char *name;
788 p = (Elf32_Sym *) symtab_section->data;
790 for (i = 0; i < nb_syms; i++) {
792 name = (char *) symtab_section->link->data + p->st_name;
794 if (p->st_info == 4) {
795 // put a filename symbol
796 n++;
797 } else if (p->st_info == 0x12) {
799 if (strcmp(func_name, name) == 0)
800 return n;
802 n += 6;
804 // put a Function Name
806 // now put aux info
808 // put a .bf
810 // now put aux info
812 // put a .ef
814 // now put aux info
816 } else {
817 n += 2;
820 p++;
823 return n; // total number of symbols
826 BOOL OutputTheSection(Section * sect)
828 const char *s = sect->name;
830 if (!strcmp(s, ".text"))
831 return true;
832 else if (!strcmp(s, ".data"))
833 return true;
834 else
835 return 0;
838 short int GetCoffFlags(const char *s)
840 if (!strcmp(s, ".text"))
841 return STYP_TEXT | STYP_DATA | STYP_ALIGN | 0x400;
842 else if (!strcmp(s, ".data"))
843 return STYP_DATA;
844 else if (!strcmp(s, ".bss"))
845 return STYP_BSS;
846 else if (!strcmp(s, ".stack"))
847 return STYP_BSS | STYP_ALIGN | 0x200;
848 else if (!strcmp(s, ".cinit"))
849 return STYP_COPY | STYP_DATA | STYP_ALIGN | 0x200;
850 else
851 return 0;
854 Section *FindSection(TCCState * s1, const char *sname)
856 Section *s;
857 int i;
859 for (i = 1; i < s1->nb_sections; i++) {
860 s = s1->sections[i];
862 if (!strcmp(sname, s->name))
863 return s;
866 error("could not find section %s", sname);
867 return 0;
870 int tcc_load_coff(TCCState * s1, int fd)
872 // tktk TokenSym *ts;
874 FILE *f;
875 unsigned int str_size;
876 char *Coff_str_table, *name;
877 int i, k;
878 struct syment csym;
879 char name2[9];
880 FILHDR file_hdr; /* FILE HEADER STRUCTURE */
882 f = fdopen(fd, "rb");
883 if (!f) {
884 error("Unable to open .out file for input");
887 if (fread(&file_hdr, FILHSZ, 1, f) != 1)
888 error("error reading .out file for input");
890 if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
891 error("error reading .out file for input");
893 // first read the string table
895 if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
896 error("error reading .out file for input");
898 if (fread(&str_size, sizeof(int), 1, f) != 1)
899 error("error reading .out file for input");
902 Coff_str_table = (char *) tcc_malloc(str_size);
904 if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
905 error("error reading .out file for input");
907 // read/process all the symbols
909 // seek back to symbols
911 if (fseek(f, file_hdr.f_symptr, SEEK_SET))
912 error("error reading .out file for input");
914 for (i = 0; i < file_hdr.f_nsyms; i++) {
915 if (fread(&csym, SYMESZ, 1, f) != 1)
916 error("error reading .out file for input");
918 if (csym._n._n_n._n_zeroes == 0) {
919 name = Coff_str_table + csym._n._n_n._n_offset - 4;
920 } else {
921 name = csym._n._n_name;
923 if (name[7] != 0) {
924 for (k = 0; k < 8; k++)
925 name2[k] = name[k];
927 name2[8] = 0;
929 name = name2;
932 // if (strcmp("_DAC_Buffer",name)==0) // tktk
933 // name[0]=0;
935 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
936 (csym.n_type == 0x18 && csym.n_sclass == 0x2) || // pointer to structure
937 (csym.n_type == 0x7 && csym.n_sclass == 0x2) || // doubles
938 (csym.n_type == 0x6 && csym.n_sclass == 0x2)) // floats
940 // strip off any leading underscore (except for other main routine)
942 if (name[0] == '_' && strcmp(name, "_main") != 0)
943 name++;
945 tcc_add_symbol(s1, name, (void*)csym.n_value);
947 // skip any aux records
949 if (csym.n_numaux == 1) {
950 if (fread(&csym, SYMESZ, 1, f) != 1)
951 error("error reading .out file for input");
952 i++;
956 return 0;