re-apply VLA by Thomas Preud'homme
[tinycc.git] / tcccoff.c
blob3ac4c65c69a90b54471be6d676ffe159066073de
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
22 #include "tcc.h"
24 #define MAXNSCNS 255 /* MAXIMUM NUMBER OF SECTIONS */
25 #define MAX_STR_TABLE 1000000
26 AOUTHDR o_filehdr; /* OPTIONAL (A.OUT) FILE HEADER */
28 SCNHDR section_header[MAXNSCNS];
30 #define MAX_FUNCS 1000
31 #define MAX_FUNC_NAME_LENGTH 128
33 int nFuncs;
34 char Func[MAX_FUNCS][MAX_FUNC_NAME_LENGTH];
35 char AssociatedFile[MAX_FUNCS][MAX_FUNC_NAME_LENGTH];
36 int LineNoFilePtr[MAX_FUNCS];
37 int EndAddress[MAX_FUNCS];
38 int LastLineNo[MAX_FUNCS];
39 int FuncEntries[MAX_FUNCS];
41 BOOL OutputTheSection(Section * sect);
42 short int GetCoffFlags(const char *s);
43 void SortSymbolTable(void);
44 Section *FindSection(TCCState * s1, const char *sname);
46 int C67_main_entry_point;
48 int FindCoffSymbolIndex(const char *func_name);
49 int nb_syms;
51 typedef struct {
52 long tag;
53 long size;
54 long fileptr;
55 long nextsym;
56 short int dummy;
57 } AUXFUNC;
59 typedef struct {
60 long regmask;
61 unsigned short lineno;
62 unsigned short nentries;
63 int localframe;
64 int nextentry;
65 short int dummy;
66 } AUXBF;
68 typedef struct {
69 long dummy;
70 unsigned short lineno;
71 unsigned short dummy1;
72 int dummy2;
73 int dummy3;
74 unsigned short dummy4;
75 } AUXEF;
77 ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
79 Section *tcc_sect;
80 SCNHDR *coff_sec;
81 int file_pointer;
82 char *Coff_str_table, *pCoff_str_table;
83 int CoffTextSectionNo, coff_nb_syms;
84 FILHDR file_hdr; /* FILE HEADER STRUCTURE */
85 Section *stext, *sdata, *sbss;
86 int i, NSectionsToOutput = 0;
88 Coff_str_table = pCoff_str_table = NULL;
90 stext = FindSection(s1, ".text");
91 sdata = FindSection(s1, ".data");
92 sbss = FindSection(s1, ".bss");
94 nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
95 coff_nb_syms = FindCoffSymbolIndex("XXXXXXXXXX1");
97 file_hdr.f_magic = COFF_C67_MAGIC; /* magic number */
98 file_hdr.f_timdat = 0; /* time & date stamp */
99 file_hdr.f_opthdr = sizeof(AOUTHDR); /* sizeof(optional hdr) */
100 file_hdr.f_flags = 0x1143; /* flags (copied from what code composer does) */
101 file_hdr.f_TargetID = 0x99; /* for C6x = 0x0099 */
103 o_filehdr.magic = 0x0108; /* see magic.h */
104 o_filehdr.vstamp = 0x0190; /* version stamp */
105 o_filehdr.tsize = stext->data_offset; /* text size in bytes, padded to FW bdry */
106 o_filehdr.dsize = sdata->data_offset; /* initialized data " " */
107 o_filehdr.bsize = sbss->data_offset; /* uninitialized data " " */
108 o_filehdr.entrypt = C67_main_entry_point; /* entry pt. */
109 o_filehdr.text_start = stext->sh_addr; /* base of text used for this file */
110 o_filehdr.data_start = sdata->sh_addr; /* base of data used for this file */
113 // create all the section headers
115 file_pointer = FILHSZ + sizeof(AOUTHDR);
117 CoffTextSectionNo = -1;
119 for (i = 1; i < s1->nb_sections; i++) {
120 coff_sec = &section_header[i];
121 tcc_sect = s1->sections[i];
123 if (OutputTheSection(tcc_sect)) {
124 NSectionsToOutput++;
126 if (CoffTextSectionNo == -1 && tcc_sect == stext)
127 CoffTextSectionNo = NSectionsToOutput; // rem which coff sect number the .text sect is
129 strcpy(coff_sec->s_name, tcc_sect->name); /* section name */
131 coff_sec->s_paddr = tcc_sect->sh_addr; /* physical address */
132 coff_sec->s_vaddr = tcc_sect->sh_addr; /* virtual address */
133 coff_sec->s_size = tcc_sect->data_offset; /* section size */
134 coff_sec->s_scnptr = 0; /* file ptr to raw data for section */
135 coff_sec->s_relptr = 0; /* file ptr to relocation */
136 coff_sec->s_lnnoptr = 0; /* file ptr to line numbers */
137 coff_sec->s_nreloc = 0; /* number of relocation entries */
138 coff_sec->s_flags = GetCoffFlags(coff_sec->s_name); /* flags */
139 coff_sec->s_reserved = 0; /* reserved byte */
140 coff_sec->s_page = 0; /* memory page id */
142 file_pointer += sizeof(SCNHDR);
146 file_hdr.f_nscns = NSectionsToOutput; /* number of sections */
148 // now loop through and determine file pointer locations
149 // for the raw data
152 for (i = 1; i < s1->nb_sections; i++) {
153 coff_sec = &section_header[i];
154 tcc_sect = s1->sections[i];
156 if (OutputTheSection(tcc_sect)) {
157 // put raw data
158 coff_sec->s_scnptr = file_pointer; /* file ptr to raw data for section */
159 file_pointer += coff_sec->s_size;
163 // now loop through and determine file pointer locations
164 // for the relocation data
166 for (i = 1; i < s1->nb_sections; i++) {
167 coff_sec = &section_header[i];
168 tcc_sect = s1->sections[i];
170 if (OutputTheSection(tcc_sect)) {
171 // put relocations data
172 if (coff_sec->s_nreloc > 0) {
173 coff_sec->s_relptr = file_pointer; /* file ptr to relocation */
174 file_pointer += coff_sec->s_nreloc * sizeof(struct reloc);
179 // now loop through and determine file pointer locations
180 // for the line number data
182 for (i = 1; i < s1->nb_sections; i++) {
183 coff_sec = &section_header[i];
184 tcc_sect = s1->sections[i];
186 coff_sec->s_nlnno = 0;
187 coff_sec->s_lnnoptr = 0;
189 if (s1->do_debug && tcc_sect == stext) {
190 // count how many line nos data
192 // also find association between source file name and function
193 // so we can sort the symbol table
196 Stab_Sym *sym, *sym_end;
197 char func_name[MAX_FUNC_NAME_LENGTH],
198 last_func_name[MAX_FUNC_NAME_LENGTH];
199 unsigned long func_addr, last_pc, pc;
200 const char *incl_files[INCLUDE_STACK_SIZE];
201 int incl_index, len, last_line_num;
202 const char *str, *p;
204 coff_sec->s_lnnoptr = file_pointer; /* file ptr to linno */
207 func_name[0] = '\0';
208 func_addr = 0;
209 incl_index = 0;
210 last_func_name[0] = '\0';
211 last_pc = 0xffffffff;
212 last_line_num = 1;
213 sym = (Stab_Sym *) stab_section->data + 1;
214 sym_end =
215 (Stab_Sym *) (stab_section->data +
216 stab_section->data_offset);
218 nFuncs = 0;
219 while (sym < sym_end) {
220 switch (sym->n_type) {
221 /* function start or end */
222 case N_FUN:
223 if (sym->n_strx == 0) {
224 // end of function
226 coff_sec->s_nlnno++;
227 file_pointer += LINESZ;
229 pc = sym->n_value + func_addr;
230 func_name[0] = '\0';
231 func_addr = 0;
232 EndAddress[nFuncs] = pc;
233 FuncEntries[nFuncs] =
234 (file_pointer -
235 LineNoFilePtr[nFuncs]) / LINESZ - 1;
236 LastLineNo[nFuncs++] = last_line_num + 1;
237 } else {
238 // beginning of function
240 LineNoFilePtr[nFuncs] = file_pointer;
241 coff_sec->s_nlnno++;
242 file_pointer += LINESZ;
244 str =
245 (const char *) stabstr_section->data +
246 sym->n_strx;
248 p = strchr(str, ':');
249 if (!p) {
250 pstrcpy(func_name, sizeof(func_name), str);
251 pstrcpy(Func[nFuncs], sizeof(func_name), str);
252 } else {
253 len = p - str;
254 if (len > sizeof(func_name) - 1)
255 len = sizeof(func_name) - 1;
256 memcpy(func_name, str, len);
257 memcpy(Func[nFuncs], str, len);
258 func_name[len] = '\0';
261 // save the file that it came in so we can sort later
262 pstrcpy(AssociatedFile[nFuncs], sizeof(func_name),
263 incl_files[incl_index - 1]);
265 func_addr = sym->n_value;
267 break;
269 /* line number info */
270 case N_SLINE:
271 pc = sym->n_value + func_addr;
273 last_pc = pc;
274 last_line_num = sym->n_desc;
276 /* XXX: slow! */
277 strcpy(last_func_name, func_name);
279 coff_sec->s_nlnno++;
280 file_pointer += LINESZ;
281 break;
282 /* include files */
283 case N_BINCL:
284 str =
285 (const char *) stabstr_section->data + sym->n_strx;
286 add_incl:
287 if (incl_index < INCLUDE_STACK_SIZE) {
288 incl_files[incl_index++] = str;
290 break;
291 case N_EINCL:
292 if (incl_index > 1)
293 incl_index--;
294 break;
295 case N_SO:
296 if (sym->n_strx == 0) {
297 incl_index = 0; /* end of translation unit */
298 } else {
299 str =
300 (const char *) stabstr_section->data +
301 sym->n_strx;
302 /* do not add path */
303 len = strlen(str);
304 if (len > 0 && str[len - 1] != '/')
305 goto add_incl;
307 break;
309 sym++;
315 file_hdr.f_symptr = file_pointer; /* file pointer to symtab */
317 if (s1->do_debug)
318 file_hdr.f_nsyms = coff_nb_syms; /* number of symtab entries */
319 else
320 file_hdr.f_nsyms = 0;
322 file_pointer += file_hdr.f_nsyms * SYMNMLEN;
324 // OK now we are all set to write the file
327 fwrite(&file_hdr, FILHSZ, 1, f);
328 fwrite(&o_filehdr, sizeof(o_filehdr), 1, f);
330 // write section headers
331 for (i = 1; i < s1->nb_sections; i++) {
332 coff_sec = &section_header[i];
333 tcc_sect = s1->sections[i];
335 if (OutputTheSection(tcc_sect)) {
336 fwrite(coff_sec, sizeof(SCNHDR), 1, f);
340 // write raw data
341 for (i = 1; i < s1->nb_sections; i++) {
342 coff_sec = &section_header[i];
343 tcc_sect = s1->sections[i];
345 if (OutputTheSection(tcc_sect)) {
346 fwrite(tcc_sect->data, tcc_sect->data_offset, 1, f);
350 // write relocation data
351 for (i = 1; i < s1->nb_sections; i++) {
352 coff_sec = &section_header[i];
353 tcc_sect = s1->sections[i];
355 if (OutputTheSection(tcc_sect)) {
356 // put relocations data
357 if (coff_sec->s_nreloc > 0) {
358 fwrite(tcc_sect->reloc,
359 coff_sec->s_nreloc * sizeof(struct reloc), 1, f);
365 // group the symbols in order of filename, func1, func2, etc
366 // finally global symbols
368 if (s1->do_debug)
369 SortSymbolTable();
371 // write line no data
373 for (i = 1; i < s1->nb_sections; i++) {
374 coff_sec = &section_header[i];
375 tcc_sect = s1->sections[i];
377 if (s1->do_debug && tcc_sect == stext) {
378 // count how many line nos data
381 Stab_Sym *sym, *sym_end;
382 char func_name[128], last_func_name[128];
383 unsigned long func_addr, last_pc, pc;
384 const char *incl_files[INCLUDE_STACK_SIZE];
385 int incl_index, len, last_line_num;
386 const char *str, *p;
388 LINENO CoffLineNo;
390 func_name[0] = '\0';
391 func_addr = 0;
392 incl_index = 0;
393 last_func_name[0] = '\0';
394 last_pc = 0;
395 last_line_num = 1;
396 sym = (Stab_Sym *) stab_section->data + 1;
397 sym_end =
398 (Stab_Sym *) (stab_section->data +
399 stab_section->data_offset);
401 while (sym < sym_end) {
402 switch (sym->n_type) {
403 /* function start or end */
404 case N_FUN:
405 if (sym->n_strx == 0) {
406 // end of function
408 CoffLineNo.l_addr.l_paddr = last_pc;
409 CoffLineNo.l_lnno = last_line_num + 1;
410 fwrite(&CoffLineNo, 6, 1, f);
412 pc = sym->n_value + func_addr;
413 func_name[0] = '\0';
414 func_addr = 0;
415 } else {
416 // beginning of function
418 str =
419 (const char *) stabstr_section->data +
420 sym->n_strx;
423 p = strchr(str, ':');
424 if (!p) {
425 pstrcpy(func_name, sizeof(func_name), str);
426 } else {
427 len = p - str;
428 if (len > sizeof(func_name) - 1)
429 len = sizeof(func_name) - 1;
430 memcpy(func_name, str, len);
431 func_name[len] = '\0';
433 func_addr = sym->n_value;
434 last_pc = func_addr;
435 last_line_num = -1;
437 // output a function begin
439 CoffLineNo.l_addr.l_symndx =
440 FindCoffSymbolIndex(func_name);
441 CoffLineNo.l_lnno = 0;
443 fwrite(&CoffLineNo, 6, 1, f);
445 break;
447 /* line number info */
448 case N_SLINE:
449 pc = sym->n_value + func_addr;
452 /* XXX: slow! */
453 strcpy(last_func_name, func_name);
455 // output a line reference
457 CoffLineNo.l_addr.l_paddr = last_pc;
459 if (last_line_num == -1) {
460 CoffLineNo.l_lnno = sym->n_desc;
461 } else {
462 CoffLineNo.l_lnno = last_line_num + 1;
465 fwrite(&CoffLineNo, 6, 1, f);
467 last_pc = pc;
468 last_line_num = sym->n_desc;
470 break;
472 /* include files */
473 case N_BINCL:
474 str =
475 (const char *) stabstr_section->data + sym->n_strx;
476 add_incl2:
477 if (incl_index < INCLUDE_STACK_SIZE) {
478 incl_files[incl_index++] = str;
480 break;
481 case N_EINCL:
482 if (incl_index > 1)
483 incl_index--;
484 break;
485 case N_SO:
486 if (sym->n_strx == 0) {
487 incl_index = 0; /* end of translation unit */
488 } else {
489 str =
490 (const char *) stabstr_section->data +
491 sym->n_strx;
492 /* do not add path */
493 len = strlen(str);
494 if (len > 0 && str[len - 1] != '/')
495 goto add_incl2;
497 break;
499 sym++;
504 // write symbol table
505 if (s1->do_debug) {
506 int k;
507 struct syment csym;
508 AUXFUNC auxfunc;
509 AUXBF auxbf;
510 AUXEF auxef;
511 int i;
512 Elf32_Sym *p;
513 const char *name;
514 int nstr;
515 int n = 0;
517 Coff_str_table = (char *) tcc_malloc(MAX_STR_TABLE);
518 pCoff_str_table = Coff_str_table;
519 nstr = 0;
521 p = (Elf32_Sym *) symtab_section->data;
524 for (i = 0; i < nb_syms; i++) {
526 name = symtab_section->link->data + p->st_name;
528 for (k = 0; k < 8; k++)
529 csym._n._n_name[k] = 0;
531 if (strlen(name) <= 8) {
532 strcpy(csym._n._n_name, name);
533 } else {
534 if (pCoff_str_table - Coff_str_table + strlen(name) >
535 MAX_STR_TABLE - 1)
536 error("String table too large");
538 csym._n._n_n._n_zeroes = 0;
539 csym._n._n_n._n_offset =
540 pCoff_str_table - Coff_str_table + 4;
542 strcpy(pCoff_str_table, name);
543 pCoff_str_table += strlen(name) + 1; // skip over null
544 nstr++;
547 if (p->st_info == 4) {
548 // put a filename symbol
549 csym.n_value = 33; // ?????
550 csym.n_scnum = N_DEBUG;
551 csym.n_type = 0;
552 csym.n_sclass = C_FILE;
553 csym.n_numaux = 0;
554 fwrite(&csym, 18, 1, f);
555 n++;
557 } else if (p->st_info == 0x12) {
558 // find the function data
560 for (k = 0; k < nFuncs; k++) {
561 if (strcmp(name, Func[k]) == 0)
562 break;
565 if (k >= nFuncs) {
566 char s[256];
568 sprintf(s, "debug info can't find function: %s", name);
570 error(s);
572 // put a Function Name
574 csym.n_value = p->st_value; // physical address
575 csym.n_scnum = CoffTextSectionNo;
576 csym.n_type = MKTYPE(T_INT, DT_FCN, 0, 0, 0, 0, 0);
577 csym.n_sclass = C_EXT;
578 csym.n_numaux = 1;
579 fwrite(&csym, 18, 1, f);
581 // now put aux info
583 auxfunc.tag = 0;
584 auxfunc.size = EndAddress[k] - p->st_value;
585 auxfunc.fileptr = LineNoFilePtr[k];
586 auxfunc.nextsym = n + 6; // tktk
587 auxfunc.dummy = 0;
588 fwrite(&auxfunc, 18, 1, f);
590 // put a .bf
592 strcpy(csym._n._n_name, ".bf");
593 csym.n_value = p->st_value; // physical address
594 csym.n_scnum = CoffTextSectionNo;
595 csym.n_type = 0;
596 csym.n_sclass = C_FCN;
597 csym.n_numaux = 1;
598 fwrite(&csym, 18, 1, f);
600 // now put aux info
602 auxbf.regmask = 0;
603 auxbf.lineno = 0;
604 auxbf.nentries = FuncEntries[k];
605 auxbf.localframe = 0;
606 auxbf.nextentry = n + 6;
607 auxbf.dummy = 0;
608 fwrite(&auxbf, 18, 1, f);
610 // put a .ef
612 strcpy(csym._n._n_name, ".ef");
613 csym.n_value = EndAddress[k]; // physical address
614 csym.n_scnum = CoffTextSectionNo;
615 csym.n_type = 0;
616 csym.n_sclass = C_FCN;
617 csym.n_numaux = 1;
618 fwrite(&csym, 18, 1, f);
620 // now put aux info
622 auxef.dummy = 0;
623 auxef.lineno = LastLineNo[k];
624 auxef.dummy1 = 0;
625 auxef.dummy2 = 0;
626 auxef.dummy3 = 0;
627 auxef.dummy4 = 0;
628 fwrite(&auxef, 18, 1, f);
630 n += 6;
632 } else {
633 // try an put some type info
635 if ((p->st_other & VT_BTYPE) == VT_DOUBLE) {
636 csym.n_type = T_DOUBLE; // int
637 csym.n_sclass = C_EXT;
638 } else if ((p->st_other & VT_BTYPE) == VT_FLOAT) {
639 csym.n_type = T_FLOAT;
640 csym.n_sclass = C_EXT;
641 } else if ((p->st_other & VT_BTYPE) == VT_INT) {
642 csym.n_type = T_INT; // int
643 csym.n_sclass = C_EXT;
644 } else if ((p->st_other & VT_BTYPE) == VT_SHORT) {
645 csym.n_type = T_SHORT;
646 csym.n_sclass = C_EXT;
647 } else if ((p->st_other & VT_BTYPE) == VT_BYTE) {
648 csym.n_type = T_CHAR;
649 csym.n_sclass = C_EXT;
650 } else {
651 csym.n_type = T_INT; // just mark as a label
652 csym.n_sclass = C_LABEL;
656 csym.n_value = p->st_value;
657 csym.n_scnum = 2;
658 csym.n_numaux = 1;
659 fwrite(&csym, 18, 1, f);
661 auxfunc.tag = 0;
662 auxfunc.size = 0x20;
663 auxfunc.fileptr = 0;
664 auxfunc.nextsym = 0;
665 auxfunc.dummy = 0;
666 fwrite(&auxfunc, 18, 1, f);
667 n++;
668 n++;
672 p++;
676 if (s1->do_debug) {
677 // write string table
679 // first write the size
680 i = pCoff_str_table - Coff_str_table;
681 fwrite(&i, 4, 1, f);
683 // then write the strings
684 fwrite(Coff_str_table, i, 1, f);
686 tcc_free(Coff_str_table);
689 return 0;
694 // group the symbols in order of filename, func1, func2, etc
695 // finally global symbols
697 void SortSymbolTable(void)
699 int i, j, k, n = 0;
700 Elf32_Sym *p, *p2, *NewTable;
701 char *name, *name2;
703 NewTable = (Elf32_Sym *) tcc_malloc(nb_syms * sizeof(Elf32_Sym));
705 p = (Elf32_Sym *) symtab_section->data;
708 // find a file symbol, copy it over
709 // then scan the whole symbol list and copy any function
710 // symbols that match the file association
712 for (i = 0; i < nb_syms; i++) {
713 if (p->st_info == 4) {
714 name = (char *) symtab_section->link->data + p->st_name;
716 // this is a file symbol, copy it over
718 NewTable[n++] = *p;
720 p2 = (Elf32_Sym *) symtab_section->data;
722 for (j = 0; j < nb_syms; j++) {
723 if (p2->st_info == 0x12) {
724 // this is a func symbol
726 name2 =
727 (char *) symtab_section->link->data + p2->st_name;
729 // find the function data index
731 for (k = 0; k < nFuncs; k++) {
732 if (strcmp(name2, Func[k]) == 0)
733 break;
736 if (k >= nFuncs) {
737 char s[256];
739 sprintf(s,
740 "debug (sort) info can't find function: %s",
741 name2);
743 error(s);
746 if (strcmp(AssociatedFile[k], name) == 0) {
747 // yes they match copy it over
749 NewTable[n++] = *p2;
752 p2++;
755 p++;
758 // now all the filename and func symbols should have been copied over
759 // copy all the rest over (all except file and funcs)
761 p = (Elf32_Sym *) symtab_section->data;
762 for (i = 0; i < nb_syms; i++) {
763 if (p->st_info != 4 && p->st_info != 0x12) {
764 NewTable[n++] = *p;
766 p++;
769 if (n != nb_syms)
770 error("Internal Compiler error, debug info");
772 // copy it all back
774 p = (Elf32_Sym *) symtab_section->data;
775 for (i = 0; i < nb_syms; i++) {
776 *p++ = NewTable[i];
779 tcc_free(NewTable);
783 int FindCoffSymbolIndex(const char *func_name)
785 int i, n = 0;
786 Elf32_Sym *p;
787 char *name;
789 p = (Elf32_Sym *) symtab_section->data;
791 for (i = 0; i < nb_syms; i++) {
793 name = (char *) symtab_section->link->data + p->st_name;
795 if (p->st_info == 4) {
796 // put a filename symbol
797 n++;
798 } else if (p->st_info == 0x12) {
800 if (strcmp(func_name, name) == 0)
801 return n;
803 n += 6;
805 // put a Function Name
807 // now put aux info
809 // put a .bf
811 // now put aux info
813 // put a .ef
815 // now put aux info
817 } else {
818 n += 2;
821 p++;
824 return n; // total number of symbols
827 BOOL OutputTheSection(Section * sect)
829 const char *s = sect->name;
831 if (!strcmp(s, ".text"))
832 return true;
833 else if (!strcmp(s, ".data"))
834 return true;
835 else
836 return 0;
839 short int GetCoffFlags(const char *s)
841 if (!strcmp(s, ".text"))
842 return STYP_TEXT | STYP_DATA | STYP_ALIGN | 0x400;
843 else if (!strcmp(s, ".data"))
844 return STYP_DATA;
845 else if (!strcmp(s, ".bss"))
846 return STYP_BSS;
847 else if (!strcmp(s, ".stack"))
848 return STYP_BSS | STYP_ALIGN | 0x200;
849 else if (!strcmp(s, ".cinit"))
850 return STYP_COPY | STYP_DATA | STYP_ALIGN | 0x200;
851 else
852 return 0;
855 Section *FindSection(TCCState * s1, const char *sname)
857 Section *s;
858 int i;
860 for (i = 1; i < s1->nb_sections; i++) {
861 s = s1->sections[i];
863 if (!strcmp(sname, s->name))
864 return s;
867 error("could not find section %s", sname);
868 return 0;
871 ST_FUNC int tcc_load_coff(TCCState * s1, int fd)
873 // tktk TokenSym *ts;
875 FILE *f;
876 unsigned int str_size;
877 char *Coff_str_table, *name;
878 int i, k;
879 struct syment csym;
880 char name2[9];
881 FILHDR file_hdr; /* FILE HEADER STRUCTURE */
883 f = fdopen(fd, "rb");
884 if (!f) {
885 error("Unable to open .out file for input");
888 if (fread(&file_hdr, FILHSZ, 1, f) != 1)
889 error("error reading .out file for input");
891 if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
892 error("error reading .out file for input");
894 // first read the string table
896 if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
897 error("error reading .out file for input");
899 if (fread(&str_size, sizeof(int), 1, f) != 1)
900 error("error reading .out file for input");
903 Coff_str_table = (char *) tcc_malloc(str_size);
905 if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
906 error("error reading .out file for input");
908 // read/process all the symbols
910 // seek back to symbols
912 if (fseek(f, file_hdr.f_symptr, SEEK_SET))
913 error("error reading .out file for input");
915 for (i = 0; i < file_hdr.f_nsyms; i++) {
916 if (fread(&csym, SYMESZ, 1, f) != 1)
917 error("error reading .out file for input");
919 if (csym._n._n_n._n_zeroes == 0) {
920 name = Coff_str_table + csym._n._n_n._n_offset - 4;
921 } else {
922 name = csym._n._n_name;
924 if (name[7] != 0) {
925 for (k = 0; k < 8; k++)
926 name2[k] = name[k];
928 name2[8] = 0;
930 name = name2;
933 // if (strcmp("_DAC_Buffer",name)==0) // tktk
934 // name[0]=0;
936 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
937 (csym.n_type == 0x18 && csym.n_sclass == 0x2) || // pointer to structure
938 (csym.n_type == 0x7 && csym.n_sclass == 0x2) || // doubles
939 (csym.n_type == 0x6 && csym.n_sclass == 0x2)) // floats
941 // strip off any leading underscore (except for other main routine)
943 if (name[0] == '_' && strcmp(name, "_main") != 0)
944 name++;
946 tcc_add_symbol(s1, name, (void*)(uplong)csym.n_value);
948 // skip any aux records
950 if (csym.n_numaux == 1) {
951 if (fread(&csym, SYMESZ, 1, f) != 1)
952 error("error reading .out file for input");
953 i++;
957 return 0;