Fix 'invalid relocation entry' problem on ubuntu - from Bernhard Fischer
[tinycc/daniel.git] / tcccoff.c
blobde7d87c9dfe740304f4e8821d6435e8829e49054
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 stext = FindSection(s1, ".text");
88 sdata = FindSection(s1, ".data");
89 sbss = FindSection(s1, ".bss");
91 nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
92 coff_nb_syms = FindCoffSymbolIndex("XXXXXXXXXX1");
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 */
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 */
110 // create all the section headers
112 file_pointer = FILHSZ + sizeof(AOUTHDR);
114 CoffTextSectionNo = -1;
116 for (i = 1; i < s1->nb_sections; i++) {
117 coff_sec = &section_header[i];
118 tcc_sect = s1->sections[i];
120 if (OutputTheSection(tcc_sect)) {
121 NSectionsToOutput++;
123 if (CoffTextSectionNo == -1 && tcc_sect == stext)
124 CoffTextSectionNo = NSectionsToOutput; // rem which coff sect number the .text sect is
126 strcpy(coff_sec->s_name, tcc_sect->name); /* section name */
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 */
139 file_pointer += sizeof(SCNHDR);
143 file_hdr.f_nscns = NSectionsToOutput; /* number of sections */
145 // now loop through and determine file pointer locations
146 // for the raw data
149 for (i = 1; i < s1->nb_sections; i++) {
150 coff_sec = &section_header[i];
151 tcc_sect = s1->sections[i];
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;
160 // now loop through and determine file pointer locations
161 // for the relocation data
163 for (i = 1; i < s1->nb_sections; i++) {
164 coff_sec = &section_header[i];
165 tcc_sect = s1->sections[i];
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);
176 // now loop through and determine file pointer locations
177 // for the line number data
179 for (i = 1; i < s1->nb_sections; i++) {
180 coff_sec = &section_header[i];
181 tcc_sect = s1->sections[i];
183 coff_sec->s_nlnno = 0;
184 coff_sec->s_lnnoptr = 0;
186 if (do_debug && tcc_sect == stext) {
187 // count how many line nos data
189 // also find association between source file name and function
190 // so we can sort the symbol table
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;
201 coff_sec->s_lnnoptr = file_pointer; /* file ptr to linno */
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);
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
223 coff_sec->s_nlnno++;
224 file_pointer += LINESZ;
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
237 LineNoFilePtr[nFuncs] = file_pointer;
238 coff_sec->s_nlnno++;
239 file_pointer += LINESZ;
241 str =
242 (const char *) stabstr_section->data +
243 sym->n_strx;
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';
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]);
262 func_addr = sym->n_value;
264 break;
266 /* line number info */
267 case N_SLINE:
268 pc = sym->n_value + func_addr;
270 last_pc = pc;
271 last_line_num = sym->n_desc;
273 /* XXX: slow! */
274 strcpy(last_func_name, func_name);
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;
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;
304 break;
306 sym++;
312 file_hdr.f_symptr = file_pointer; /* file pointer to symtab */
314 if (do_debug)
315 file_hdr.f_nsyms = coff_nb_syms; /* number of symtab entries */
316 else
317 file_hdr.f_nsyms = 0;
319 file_pointer += file_hdr.f_nsyms * SYMNMLEN;
321 // OK now we are all set to write the file
324 fwrite(&file_hdr, FILHSZ, 1, f);
325 fwrite(&o_filehdr, sizeof(o_filehdr), 1, f);
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];
332 if (OutputTheSection(tcc_sect)) {
333 fwrite(coff_sec, sizeof(SCNHDR), 1, f);
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];
342 if (OutputTheSection(tcc_sect)) {
343 fwrite(tcc_sect->data, tcc_sect->data_offset, 1, f);
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];
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);
362 // group the symbols in order of filename, func1, func2, etc
363 // finally global symbols
365 if (do_debug)
366 SortSymbolTable();
368 // write line no data
370 for (i = 1; i < s1->nb_sections; i++) {
371 coff_sec = &section_header[i];
372 tcc_sect = s1->sections[i];
374 if (do_debug && tcc_sect == stext) {
375 // count how many line nos data
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;
385 LINENO CoffLineNo;
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);
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
405 CoffLineNo.l_addr.l_paddr = last_pc;
406 CoffLineNo.l_lnno = last_line_num + 1;
407 fwrite(&CoffLineNo, 6, 1, f);
409 pc = sym->n_value + func_addr;
410 func_name[0] = '\0';
411 func_addr = 0;
412 } else {
413 // beginning of function
415 str =
416 (const char *) stabstr_section->data +
417 sym->n_strx;
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';
430 func_addr = sym->n_value;
431 last_pc = func_addr;
432 last_line_num = -1;
434 // output a function begin
436 CoffLineNo.l_addr.l_symndx =
437 FindCoffSymbolIndex(func_name);
438 CoffLineNo.l_lnno = 0;
440 fwrite(&CoffLineNo, 6, 1, f);
442 break;
444 /* line number info */
445 case N_SLINE:
446 pc = sym->n_value + func_addr;
449 /* XXX: slow! */
450 strcpy(last_func_name, func_name);
452 // output a line reference
454 CoffLineNo.l_addr.l_paddr = last_pc;
456 if (last_line_num == -1) {
457 CoffLineNo.l_lnno = sym->n_desc;
458 } else {
459 CoffLineNo.l_lnno = last_line_num + 1;
462 fwrite(&CoffLineNo, 6, 1, f);
464 last_pc = pc;
465 last_line_num = sym->n_desc;
467 break;
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;
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;
494 break;
496 sym++;
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;
514 Coff_str_table = (char *) tcc_malloc(MAX_STR_TABLE);
515 pCoff_str_table = Coff_str_table;
516 nstr = 0;
518 p = (Elf32_Sym *) symtab_section->data;
521 for (i = 0; i < nb_syms; i++) {
523 name = symtab_section->link->data + p->st_name;
525 for (k = 0; k < 8; k++)
526 csym._n._n_name[k] = 0;
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");
535 csym._n._n_n._n_zeroes = 0;
536 csym._n._n_n._n_offset =
537 pCoff_str_table - Coff_str_table + 4;
539 strcpy(pCoff_str_table, name);
540 pCoff_str_table += strlen(name) + 1; // skip over null
541 nstr++;
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++;
554 } else if (p->st_info == 0x12) {
555 // find the function data
557 for (k = 0; k < nFuncs; k++) {
558 if (strcmp(name, Func[k]) == 0)
559 break;
562 if (k >= nFuncs) {
563 char s[256];
565 sprintf(s, "debug info can't find function: %s", name);
567 error(s);
569 // put a Function Name
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);
578 // now put aux info
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);
587 // put a .bf
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);
597 // now put aux info
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);
607 // put a .ef
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);
617 // now put aux info
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);
627 n += 6;
629 } else {
630 // try an put some type info
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;
653 csym.n_value = p->st_value;
654 csym.n_scnum = 2;
655 csym.n_numaux = 1;
656 fwrite(&csym, 18, 1, f);
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++;
669 p++;
673 if (do_debug) {
674 // write string table
676 // first write the size
677 i = pCoff_str_table - Coff_str_table;
678 fwrite(&i, 4, 1, f);
680 // then write the strings
681 fwrite(Coff_str_table, i, 1, f);
683 tcc_free(Coff_str_table);
686 return 0;
691 // group the symbols in order of filename, func1, func2, etc
692 // finally global symbols
694 void SortSymbolTable(void)
696 int i, j, k, n = 0;
697 Elf32_Sym *p, *p2, *NewTable;
698 char *name, *name2;
700 NewTable = (Elf32_Sym *) tcc_malloc(nb_syms * sizeof(Elf32_Sym));
702 p = (Elf32_Sym *) symtab_section->data;
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
709 for (i = 0; i < nb_syms; i++) {
710 if (p->st_info == 4) {
711 name = (char *) symtab_section->link->data + p->st_name;
713 // this is a file symbol, copy it over
715 NewTable[n++] = *p;
717 p2 = (Elf32_Sym *) symtab_section->data;
719 for (j = 0; j < nb_syms; j++) {
720 if (p2->st_info == 0x12) {
721 // this is a func symbol
723 name2 =
724 (char *) symtab_section->link->data + p2->st_name;
726 // find the function data index
728 for (k = 0; k < nFuncs; k++) {
729 if (strcmp(name2, Func[k]) == 0)
730 break;
733 if (k >= nFuncs) {
734 char s[256];
736 sprintf(s,
737 "debug (sort) info can't find function: %s",
738 name2);
740 error(s);
743 if (strcmp(AssociatedFile[k], name) == 0) {
744 // yes they match copy it over
746 NewTable[n++] = *p2;
749 p2++;
752 p++;
755 // now all the filename and func symbols should have been copied over
756 // copy all the rest over (all except file and funcs)
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;
763 p++;
766 if (n != nb_syms)
767 error("Internal Compiler error, debug info");
769 // copy it all back
771 p = (Elf32_Sym *) symtab_section->data;
772 for (i = 0; i < nb_syms; i++) {
773 *p++ = NewTable[i];
776 tcc_free(NewTable);
780 int FindCoffSymbolIndex(const char *func_name)
782 int i, n = 0;
783 Elf32_Sym *p;
784 char *name;
786 p = (Elf32_Sym *) symtab_section->data;
788 for (i = 0; i < nb_syms; i++) {
790 name = (char *) symtab_section->link->data + p->st_name;
792 if (p->st_info == 4) {
793 // put a filename symbol
794 n++;
795 } else if (p->st_info == 0x12) {
797 if (strcmp(func_name, name) == 0)
798 return n;
800 n += 6;
802 // put a Function Name
804 // now put aux info
806 // put a .bf
808 // now put aux info
810 // put a .ef
812 // now put aux info
814 } else {
815 n += 2;
818 p++;
821 return n; // total number of symbols
824 BOOL OutputTheSection(Section * sect)
826 const char *s = sect->name;
828 if (!strcmp(s, ".text"))
829 return true;
830 else if (!strcmp(s, ".data"))
831 return true;
832 else
833 return 0;
836 short int GetCoffFlags(const char *s)
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;
852 Section *FindSection(TCCState * s1, const char *sname)
854 Section *s;
855 int i;
857 for (i = 1; i < s1->nb_sections; i++) {
858 s = s1->sections[i];
860 if (!strcmp(sname, s->name))
861 return s;
864 error("could not find section %s", sname);
865 return 0;
868 int tcc_load_coff(TCCState * s1, int fd)
870 // tktk TokenSym *ts;
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 */
880 f = fdopen(fd, "rb");
881 if (!f) {
882 error("Unable to open .out file for input");
885 if (fread(&file_hdr, FILHSZ, 1, f) != 1)
886 error("error reading .out file for input");
888 if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
889 error("error reading .out file for input");
891 // first read the string table
893 if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
894 error("error reading .out file for input");
896 if (fread(&str_size, sizeof(int), 1, f) != 1)
897 error("error reading .out file for input");
900 Coff_str_table = (char *) tcc_malloc(str_size);
902 if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
903 error("error reading .out file for input");
905 // read/process all the symbols
907 // seek back to symbols
909 if (fseek(f, file_hdr.f_symptr, SEEK_SET))
910 error("error reading .out file for input");
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");
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;
921 if (name[7] != 0) {
922 for (k = 0; k < 8; k++)
923 name2[k] = name[k];
925 name2[8] = 0;
927 name = name2;
930 // if (strcmp("_DAC_Buffer",name)==0) // tktk
931 // name[0]=0;
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
938 // strip off any leading underscore (except for other main routine)
940 if (name[0] == '_' && strcmp(name, "_main") != 0)
941 name++;
943 tcc_add_symbol(s1, name, csym.n_value);
945 // skip any aux records
947 if (csym.n_numaux == 1) {
948 if (fread(&csym, SYMESZ, 1, f) != 1)
949 error("error reading .out file for input");
950 i++;
954 return 0;