riscv: asm: Add branch to label
[tinycc.git] / tcccoff.c
blob56064cdd6dcdff411ef26b6a73fa23d83c237294
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 /* XXX: this file uses tcc_error() to the effect of exit(1) */
25 #undef _tcc_error
27 #define MAXNSCNS 255 /* MAXIMUM NUMBER OF SECTIONS */
28 #define MAX_STR_TABLE 1000000
29 AOUTHDR o_filehdr; /* OPTIONAL (A.OUT) FILE HEADER */
31 SCNHDR section_header[MAXNSCNS];
33 #define MAX_FUNCS 1000
34 #define MAX_FUNC_NAME_LENGTH 128
36 int nFuncs;
37 char Func[MAX_FUNCS][MAX_FUNC_NAME_LENGTH];
38 char AssociatedFile[MAX_FUNCS][MAX_FUNC_NAME_LENGTH];
39 int LineNoFilePtr[MAX_FUNCS];
40 int EndAddress[MAX_FUNCS];
41 int LastLineNo[MAX_FUNCS];
42 int FuncEntries[MAX_FUNCS];
44 int OutputTheSection(Section * sect);
45 short int GetCoffFlags(const char *s);
46 void SortSymbolTable(TCCState *s1);
47 Section *FindSection(TCCState * s1, const char *sname);
49 int C67_main_entry_point;
51 int FindCoffSymbolIndex(TCCState * s1, const char *func_name);
52 int nb_syms;
54 typedef struct {
55 long tag;
56 long size;
57 long fileptr;
58 long nextsym;
59 short int dummy;
60 } AUXFUNC;
62 typedef struct {
63 long regmask;
64 unsigned short lineno;
65 unsigned short nentries;
66 int localframe;
67 int nextentry;
68 short int dummy;
69 } AUXBF;
71 typedef struct {
72 long dummy;
73 unsigned short lineno;
74 unsigned short dummy1;
75 int dummy2;
76 int dummy3;
77 unsigned short dummy4;
78 } AUXEF;
80 ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
82 Section *tcc_sect;
83 SCNHDR *coff_sec;
84 int file_pointer;
85 char *Coff_str_table, *pCoff_str_table;
86 int CoffTextSectionNo, coff_nb_syms;
87 FILHDR file_hdr; /* FILE HEADER STRUCTURE */
88 Section *stext, *sdata, *sbss;
89 int i, NSectionsToOutput = 0;
91 Coff_str_table = pCoff_str_table = NULL;
93 stext = FindSection(s1, ".text");
94 sdata = FindSection(s1, ".data");
95 sbss = FindSection(s1, ".bss");
97 nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
98 coff_nb_syms = FindCoffSymbolIndex(s1, "XXXXXXXXXX1");
100 file_hdr.f_magic = COFF_C67_MAGIC; /* magic number */
101 file_hdr.f_timdat = 0; /* time & date stamp */
102 file_hdr.f_opthdr = sizeof(AOUTHDR); /* sizeof(optional hdr) */
103 file_hdr.f_flags = 0x1143; /* flags (copied from what code composer does) */
104 file_hdr.f_TargetID = 0x99; /* for C6x = 0x0099 */
106 o_filehdr.magic = 0x0108; /* see magic.h */
107 o_filehdr.vstamp = 0x0190; /* version stamp */
108 o_filehdr.tsize = stext->data_offset; /* text size in bytes, padded to FW bdry */
109 o_filehdr.dsize = sdata->data_offset; /* initialized data " " */
110 o_filehdr.bsize = sbss->data_offset; /* uninitialized data " " */
111 o_filehdr.entrypt = C67_main_entry_point; /* entry pt. */
112 o_filehdr.text_start = stext->sh_addr; /* base of text used for this file */
113 o_filehdr.data_start = sdata->sh_addr; /* base of data used for this file */
116 // create all the section headers
118 file_pointer = FILHSZ + sizeof(AOUTHDR);
120 CoffTextSectionNo = -1;
122 for (i = 1; i < s1->nb_sections; i++) {
123 coff_sec = &section_header[i];
124 tcc_sect = s1->sections[i];
126 if (OutputTheSection(tcc_sect)) {
127 NSectionsToOutput++;
129 if (CoffTextSectionNo == -1 && tcc_sect == stext)
130 CoffTextSectionNo = NSectionsToOutput; // rem which coff sect number the .text sect is
132 strcpy(coff_sec->s_name, tcc_sect->name); /* section name */
134 coff_sec->s_paddr = tcc_sect->sh_addr; /* physical address */
135 coff_sec->s_vaddr = tcc_sect->sh_addr; /* virtual address */
136 coff_sec->s_size = tcc_sect->data_offset; /* section size */
137 coff_sec->s_scnptr = 0; /* file ptr to raw data for section */
138 coff_sec->s_relptr = 0; /* file ptr to relocation */
139 coff_sec->s_lnnoptr = 0; /* file ptr to line numbers */
140 coff_sec->s_nreloc = 0; /* number of relocation entries */
141 coff_sec->s_flags = GetCoffFlags(coff_sec->s_name); /* flags */
142 coff_sec->s_reserved = 0; /* reserved byte */
143 coff_sec->s_page = 0; /* memory page id */
145 file_pointer += sizeof(SCNHDR);
149 file_hdr.f_nscns = NSectionsToOutput; /* number of sections */
151 // now loop through and determine file pointer locations
152 // for the raw data
155 for (i = 1; i < s1->nb_sections; i++) {
156 coff_sec = &section_header[i];
157 tcc_sect = s1->sections[i];
159 if (OutputTheSection(tcc_sect)) {
160 // put raw data
161 coff_sec->s_scnptr = file_pointer; /* file ptr to raw data for section */
162 file_pointer += coff_sec->s_size;
166 // now loop through and determine file pointer locations
167 // for the relocation data
169 for (i = 1; i < s1->nb_sections; i++) {
170 coff_sec = &section_header[i];
171 tcc_sect = s1->sections[i];
173 if (OutputTheSection(tcc_sect)) {
174 // put relocations data
175 if (coff_sec->s_nreloc > 0) {
176 coff_sec->s_relptr = file_pointer; /* file ptr to relocation */
177 file_pointer += coff_sec->s_nreloc * sizeof(struct reloc);
182 // now loop through and determine file pointer locations
183 // for the line number data
185 for (i = 1; i < s1->nb_sections; i++) {
186 coff_sec = &section_header[i];
187 tcc_sect = s1->sections[i];
189 coff_sec->s_nlnno = 0;
190 coff_sec->s_lnnoptr = 0;
192 if (s1->do_debug && tcc_sect == stext) {
193 // count how many line nos data
195 // also find association between source file name and function
196 // so we can sort the symbol table
199 Stab_Sym *sym, *sym_end;
200 char func_name[MAX_FUNC_NAME_LENGTH],
201 last_func_name[MAX_FUNC_NAME_LENGTH];
202 unsigned long func_addr, last_pc, pc;
203 const char *incl_files[INCLUDE_STACK_SIZE];
204 int incl_index, len, last_line_num;
205 const char *str, *p;
207 coff_sec->s_lnnoptr = file_pointer; /* file ptr to linno */
210 func_name[0] = '\0';
211 func_addr = 0;
212 incl_index = 0;
213 last_func_name[0] = '\0';
214 last_pc = 0xffffffff;
215 last_line_num = 1;
216 sym = (Stab_Sym *) stab_section->data + 1;
217 sym_end =
218 (Stab_Sym *) (stab_section->data +
219 stab_section->data_offset);
221 nFuncs = 0;
222 while (sym < sym_end) {
223 switch (sym->n_type) {
224 /* function start or end */
225 case N_FUN:
226 if (sym->n_strx == 0) {
227 // end of function
229 coff_sec->s_nlnno++;
230 file_pointer += LINESZ;
232 pc = sym->n_value + func_addr;
233 func_name[0] = '\0';
234 func_addr = 0;
235 EndAddress[nFuncs] = pc;
236 FuncEntries[nFuncs] =
237 (file_pointer -
238 LineNoFilePtr[nFuncs]) / LINESZ - 1;
239 LastLineNo[nFuncs++] = last_line_num + 1;
240 } else {
241 // beginning of function
243 LineNoFilePtr[nFuncs] = file_pointer;
244 coff_sec->s_nlnno++;
245 file_pointer += LINESZ;
247 str =
248 (const char *) stabstr_section->data +
249 sym->n_strx;
251 p = strchr(str, ':');
252 if (!p) {
253 pstrcpy(func_name, sizeof(func_name), str);
254 pstrcpy(Func[nFuncs], sizeof(func_name), str);
255 } else {
256 len = p - str;
257 if (len > sizeof(func_name) - 1)
258 len = sizeof(func_name) - 1;
259 memcpy(func_name, str, len);
260 memcpy(Func[nFuncs], str, len);
261 func_name[len] = '\0';
264 // save the file that it came in so we can sort later
265 pstrcpy(AssociatedFile[nFuncs], sizeof(func_name),
266 incl_files[incl_index - 1]);
268 func_addr = sym->n_value;
270 break;
272 /* line number info */
273 case N_SLINE:
274 pc = sym->n_value + func_addr;
276 last_pc = pc;
277 last_line_num = sym->n_desc;
279 /* XXX: slow! */
280 strcpy(last_func_name, func_name);
282 coff_sec->s_nlnno++;
283 file_pointer += LINESZ;
284 break;
285 /* include files */
286 case N_BINCL:
287 str =
288 (const char *) stabstr_section->data + sym->n_strx;
289 add_incl:
290 if (incl_index < INCLUDE_STACK_SIZE) {
291 incl_files[incl_index++] = str;
293 break;
294 case N_EINCL:
295 if (incl_index > 1)
296 incl_index--;
297 break;
298 case N_SO:
299 if (sym->n_strx == 0) {
300 incl_index = 0; /* end of translation unit */
301 } else {
302 str =
303 (const char *) stabstr_section->data +
304 sym->n_strx;
305 /* do not add path */
306 len = strlen(str);
307 if (len > 0 && str[len - 1] != '/')
308 goto add_incl;
310 break;
312 sym++;
318 file_hdr.f_symptr = file_pointer; /* file pointer to symtab */
320 if (s1->do_debug)
321 file_hdr.f_nsyms = coff_nb_syms; /* number of symtab entries */
322 else
323 file_hdr.f_nsyms = 0;
325 file_pointer += file_hdr.f_nsyms * SYMNMLEN;
327 // OK now we are all set to write the file
330 fwrite(&file_hdr, FILHSZ, 1, f);
331 fwrite(&o_filehdr, sizeof(o_filehdr), 1, f);
333 // write section headers
334 for (i = 1; i < s1->nb_sections; i++) {
335 coff_sec = &section_header[i];
336 tcc_sect = s1->sections[i];
338 if (OutputTheSection(tcc_sect)) {
339 fwrite(coff_sec, sizeof(SCNHDR), 1, f);
343 // write raw data
344 for (i = 1; i < s1->nb_sections; i++) {
345 coff_sec = &section_header[i];
346 tcc_sect = s1->sections[i];
348 if (OutputTheSection(tcc_sect)) {
349 fwrite(tcc_sect->data, tcc_sect->data_offset, 1, f);
353 // write relocation data
354 for (i = 1; i < s1->nb_sections; i++) {
355 coff_sec = &section_header[i];
356 tcc_sect = s1->sections[i];
358 if (OutputTheSection(tcc_sect)) {
359 // put relocations data
360 if (coff_sec->s_nreloc > 0) {
361 fwrite(tcc_sect->reloc,
362 coff_sec->s_nreloc * sizeof(struct reloc), 1, f);
368 // group the symbols in order of filename, func1, func2, etc
369 // finally global symbols
371 if (s1->do_debug)
372 SortSymbolTable(s1);
374 // write line no data
376 for (i = 1; i < s1->nb_sections; i++) {
377 coff_sec = &section_header[i];
378 tcc_sect = s1->sections[i];
380 if (s1->do_debug && tcc_sect == stext) {
381 // count how many line nos data
384 Stab_Sym *sym, *sym_end;
385 char func_name[128], last_func_name[128];
386 unsigned long func_addr, last_pc, pc;
387 const char *incl_files[INCLUDE_STACK_SIZE];
388 int incl_index, len, last_line_num;
389 const char *str, *p;
391 LINENO CoffLineNo;
393 func_name[0] = '\0';
394 func_addr = 0;
395 incl_index = 0;
396 last_func_name[0] = '\0';
397 last_pc = 0;
398 last_line_num = 1;
399 sym = (Stab_Sym *) stab_section->data + 1;
400 sym_end =
401 (Stab_Sym *) (stab_section->data +
402 stab_section->data_offset);
404 while (sym < sym_end) {
405 switch (sym->n_type) {
406 /* function start or end */
407 case N_FUN:
408 if (sym->n_strx == 0) {
409 // end of function
411 CoffLineNo.l_addr.l_paddr = last_pc;
412 CoffLineNo.l_lnno = last_line_num + 1;
413 fwrite(&CoffLineNo, 6, 1, f);
415 pc = sym->n_value + func_addr;
416 func_name[0] = '\0';
417 func_addr = 0;
418 } else {
419 // beginning of function
421 str =
422 (const char *) stabstr_section->data +
423 sym->n_strx;
426 p = strchr(str, ':');
427 if (!p) {
428 pstrcpy(func_name, sizeof(func_name), str);
429 } else {
430 len = p - str;
431 if (len > sizeof(func_name) - 1)
432 len = sizeof(func_name) - 1;
433 memcpy(func_name, str, len);
434 func_name[len] = '\0';
436 func_addr = sym->n_value;
437 last_pc = func_addr;
438 last_line_num = -1;
440 // output a function begin
442 CoffLineNo.l_addr.l_symndx =
443 FindCoffSymbolIndex(s1, func_name);
444 CoffLineNo.l_lnno = 0;
446 fwrite(&CoffLineNo, 6, 1, f);
448 break;
450 /* line number info */
451 case N_SLINE:
452 pc = sym->n_value + func_addr;
455 /* XXX: slow! */
456 strcpy(last_func_name, func_name);
458 // output a line reference
460 CoffLineNo.l_addr.l_paddr = last_pc;
462 if (last_line_num == -1) {
463 CoffLineNo.l_lnno = sym->n_desc;
464 } else {
465 CoffLineNo.l_lnno = last_line_num + 1;
468 fwrite(&CoffLineNo, 6, 1, f);
470 last_pc = pc;
471 last_line_num = sym->n_desc;
473 break;
475 /* include files */
476 case N_BINCL:
477 str =
478 (const char *) stabstr_section->data + sym->n_strx;
479 add_incl2:
480 if (incl_index < INCLUDE_STACK_SIZE) {
481 incl_files[incl_index++] = str;
483 break;
484 case N_EINCL:
485 if (incl_index > 1)
486 incl_index--;
487 break;
488 case N_SO:
489 if (sym->n_strx == 0) {
490 incl_index = 0; /* end of translation unit */
491 } else {
492 str =
493 (const char *) stabstr_section->data +
494 sym->n_strx;
495 /* do not add path */
496 len = strlen(str);
497 if (len > 0 && str[len - 1] != '/')
498 goto add_incl2;
500 break;
502 sym++;
507 // write symbol table
508 if (s1->do_debug) {
509 int k;
510 struct syment csym;
511 AUXFUNC auxfunc;
512 AUXBF auxbf;
513 AUXEF auxef;
514 int i;
515 Elf32_Sym *p;
516 const char *name;
517 int nstr;
518 int n = 0;
520 Coff_str_table = (char *) tcc_malloc(MAX_STR_TABLE);
521 pCoff_str_table = Coff_str_table;
522 nstr = 0;
524 p = (Elf32_Sym *) symtab_section->data;
527 for (i = 0; i < nb_syms; i++) {
529 name = symtab_section->link->data + p->st_name;
531 for (k = 0; k < 8; k++)
532 csym._n._n_name[k] = 0;
534 if (strlen(name) <= 8) {
535 strcpy(csym._n._n_name, name);
536 } else {
537 if (pCoff_str_table - Coff_str_table + strlen(name) >
538 MAX_STR_TABLE - 1)
539 tcc_error("String table too large");
541 csym._n._n_n._n_zeroes = 0;
542 csym._n._n_n._n_offset =
543 pCoff_str_table - Coff_str_table + 4;
545 strcpy(pCoff_str_table, name);
546 pCoff_str_table += strlen(name) + 1; // skip over null
547 nstr++;
550 if (p->st_info == 4) {
551 // put a filename symbol
552 csym.n_value = 33; // ?????
553 csym.n_scnum = N_DEBUG;
554 csym.n_type = 0;
555 csym.n_sclass = C_FILE;
556 csym.n_numaux = 0;
557 fwrite(&csym, 18, 1, f);
558 n++;
560 } else if (p->st_info == 0x12) {
561 // find the function data
563 for (k = 0; k < nFuncs; k++) {
564 if (strcmp(name, Func[k]) == 0)
565 break;
568 if (k >= nFuncs) {
569 tcc_error("debug info can't find function: %s", name);
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(TCCState *s1)
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 tcc_error("debug (sort) info can't find function: %s", name2);
739 if (strcmp(AssociatedFile[k], name) == 0) {
740 // yes they match copy it over
742 NewTable[n++] = *p2;
745 p2++;
748 p++;
751 // now all the filename and func symbols should have been copied over
752 // copy all the rest over (all except file and funcs)
754 p = (Elf32_Sym *) symtab_section->data;
755 for (i = 0; i < nb_syms; i++) {
756 if (p->st_info != 4 && p->st_info != 0x12) {
757 NewTable[n++] = *p;
759 p++;
762 if (n != nb_syms)
763 tcc_error("Internal Compiler error, debug info");
765 // copy it all back
767 p = (Elf32_Sym *) symtab_section->data;
768 for (i = 0; i < nb_syms; i++) {
769 *p++ = NewTable[i];
772 tcc_free(NewTable);
776 int FindCoffSymbolIndex(TCCState *s1, const char *func_name)
778 int i, n = 0;
779 Elf32_Sym *p;
780 char *name;
782 p = (Elf32_Sym *) symtab_section->data;
784 for (i = 0; i < nb_syms; i++) {
786 name = (char *) symtab_section->link->data + p->st_name;
788 if (p->st_info == 4) {
789 // put a filename symbol
790 n++;
791 } else if (p->st_info == 0x12) {
793 if (strcmp(func_name, name) == 0)
794 return n;
796 n += 6;
798 // put a Function Name
800 // now put aux info
802 // put a .bf
804 // now put aux info
806 // put a .ef
808 // now put aux info
810 } else {
811 n += 2;
814 p++;
817 return n; // total number of symbols
820 int OutputTheSection(Section * sect)
822 const char *s = sect->name;
824 if (!strcmp(s, ".text"))
825 return 1;
826 else if (!strcmp(s, ".data"))
827 return 1;
828 else
829 return 0;
832 short int GetCoffFlags(const char *s)
834 if (!strcmp(s, ".text"))
835 return STYP_TEXT | STYP_DATA | STYP_ALIGN | 0x400;
836 else if (!strcmp(s, ".data"))
837 return STYP_DATA;
838 else if (!strcmp(s, ".bss"))
839 return STYP_BSS;
840 else if (!strcmp(s, ".stack"))
841 return STYP_BSS | STYP_ALIGN | 0x200;
842 else if (!strcmp(s, ".cinit"))
843 return STYP_COPY | STYP_DATA | STYP_ALIGN | 0x200;
844 else
845 return 0;
848 Section *FindSection(TCCState * s1, const char *sname)
850 Section *s;
851 int i;
853 for (i = 1; i < s1->nb_sections; i++) {
854 s = s1->sections[i];
856 if (!strcmp(sname, s->name))
857 return s;
860 tcc_error("could not find section %s", sname);
861 return 0;
864 ST_FUNC int tcc_load_coff(TCCState * s1, int fd)
866 // tktk TokenSym *ts;
868 FILE *f;
869 unsigned int str_size;
870 char *Coff_str_table, *name;
871 int i, k;
872 struct syment csym;
873 char name2[9];
874 FILHDR file_hdr; /* FILE HEADER STRUCTURE */
876 f = fdopen(fd, "rb");
877 if (!f) {
878 tcc_error("Unable to open .out file for input");
881 if (fread(&file_hdr, FILHSZ, 1, f) != 1)
882 tcc_error("error reading .out file for input");
884 if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
885 tcc_error("error reading .out file for input");
887 // first read the string table
889 if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
890 tcc_error("error reading .out file for input");
892 if (fread(&str_size, sizeof(int), 1, f) != 1)
893 tcc_error("error reading .out file for input");
896 Coff_str_table = (char *) tcc_malloc(str_size);
898 if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
899 tcc_error("error reading .out file for input");
901 // read/process all the symbols
903 // seek back to symbols
905 if (fseek(f, file_hdr.f_symptr, SEEK_SET))
906 tcc_error("error reading .out file for input");
908 for (i = 0; i < file_hdr.f_nsyms; i++) {
909 if (fread(&csym, SYMESZ, 1, f) != 1)
910 tcc_error("error reading .out file for input");
912 if (csym._n._n_n._n_zeroes == 0) {
913 name = Coff_str_table + csym._n._n_n._n_offset - 4;
914 } else {
915 name = csym._n._n_name;
917 if (name[7] != 0) {
918 for (k = 0; k < 8; k++)
919 name2[k] = name[k];
921 name2[8] = 0;
923 name = name2;
926 // if (strcmp("_DAC_Buffer",name)==0) // tktk
927 // name[0]=0;
929 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
930 (csym.n_type == 0x18 && csym.n_sclass == 0x2) || // pointer to structure
931 (csym.n_type == 0x7 && csym.n_sclass == 0x2) || // doubles
932 (csym.n_type == 0x6 && csym.n_sclass == 0x2)) // floats
934 // strip off any leading underscore (except for other main routine)
936 if (name[0] == '_' && strcmp(name, "_main") != 0)
937 name++;
939 tcc_add_symbol(s1, name, (void*)(uintptr_t)csym.n_value);
941 // skip any aux records
943 if (csym.n_numaux == 1) {
944 if (fread(&csym, SYMESZ, 1, f) != 1)
945 tcc_error("error reading .out file for input");
946 i++;
950 return 0;