189ad24e7Sgus knight/*
289ad24e7Sgus knight *  COFF file handling for TCC
389ad24e7Sgus knight *
489ad24e7Sgus knight *  Copyright (c) 2003, 2004 TK
589ad24e7Sgus knight *  Copyright (c) 2004 Fabrice Bellard
689ad24e7Sgus knight *
789ad24e7Sgus knight * This library is free software; you can redistribute it and/or
889ad24e7Sgus knight * modify it under the terms of the GNU Lesser General Public
989ad24e7Sgus knight * License as published by the Free Software Foundation; either
1089ad24e7Sgus knight * version 2 of the License, or (at your option) any later version.
1189ad24e7Sgus knight *
1289ad24e7Sgus knight * This library is distributed in the hope that it will be useful,
1389ad24e7Sgus knight * but WITHOUT ANY WARRANTY; without even the implied warranty of
1489ad24e7Sgus knight * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1589ad24e7Sgus knight * Lesser General Public License for more details.
1689ad24e7Sgus knight *
1789ad24e7Sgus knight * You should have received a copy of the GNU Lesser General Public
1889ad24e7Sgus knight * License along with this library; if not, write to the Free Software
1989ad24e7Sgus knight * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2089ad24e7Sgus knight */
2189ad24e7Sgus knight
2289ad24e7Sgus knight#include "tcc.h"
2389ad24e7Sgus knight
2489ad24e7Sgus knight#define MAXNSCNS 255		/* MAXIMUM NUMBER OF SECTIONS         */
2589ad24e7Sgus knight#define MAX_STR_TABLE 1000000
2689ad24e7Sgus knightAOUTHDR o_filehdr;		/* OPTIONAL (A.OUT) FILE HEADER       */
2789ad24e7Sgus knight
2889ad24e7Sgus knightSCNHDR section_header[MAXNSCNS];
2989ad24e7Sgus knight
3089ad24e7Sgus knight#define MAX_FUNCS 1000
3189ad24e7Sgus knight#define MAX_FUNC_NAME_LENGTH 128
3289ad24e7Sgus knight
3389ad24e7Sgus knightint nFuncs;
3489ad24e7Sgus knightchar Func[MAX_FUNCS][MAX_FUNC_NAME_LENGTH];
3589ad24e7Sgus knightchar AssociatedFile[MAX_FUNCS][MAX_FUNC_NAME_LENGTH];
3689ad24e7Sgus knightint LineNoFilePtr[MAX_FUNCS];
3789ad24e7Sgus knightint EndAddress[MAX_FUNCS];
3889ad24e7Sgus knightint LastLineNo[MAX_FUNCS];
3989ad24e7Sgus knightint FuncEntries[MAX_FUNCS];
4089ad24e7Sgus knight
4189ad24e7Sgus knightint OutputTheSection(Section * sect);
4289ad24e7Sgus knightshort int GetCoffFlags(const char *s);
4372729d8eSgrischkavoid SortSymbolTable(TCCState *s1);
4489ad24e7Sgus knightSection *FindSection(TCCState * s1, const char *sname);
4589ad24e7Sgus knight
4689ad24e7Sgus knightint C67_main_entry_point;
4789ad24e7Sgus knight
4872729d8eSgrischkaint FindCoffSymbolIndex(TCCState * s1, const char *func_name);
4989ad24e7Sgus knightint nb_syms;
5089ad24e7Sgus knight
5189ad24e7Sgus knighttypedef struct {
5289ad24e7Sgus knight    long tag;
5389ad24e7Sgus knight    long size;
5489ad24e7Sgus knight    long fileptr;
5589ad24e7Sgus knight    long nextsym;
5689ad24e7Sgus knight    short int dummy;
5789ad24e7Sgus knight} AUXFUNC;
5889ad24e7Sgus knight
5989ad24e7Sgus knighttypedef struct {
6089ad24e7Sgus knight    long regmask;
6189ad24e7Sgus knight    unsigned short lineno;
6289ad24e7Sgus knight    unsigned short nentries;
6389ad24e7Sgus knight    int localframe;
6489ad24e7Sgus knight    int nextentry;
6589ad24e7Sgus knight    short int dummy;
6689ad24e7Sgus knight} AUXBF;
6789ad24e7Sgus knight
6889ad24e7Sgus knighttypedef struct {
6989ad24e7Sgus knight    long dummy;
7089ad24e7Sgus knight    unsigned short lineno;
7189ad24e7Sgus knight    unsigned short dummy1;
7289ad24e7Sgus knight    int dummy2;
7389ad24e7Sgus knight    int dummy3;
7489ad24e7Sgus knight    unsigned short dummy4;
7589ad24e7Sgus knight} AUXEF;
7689ad24e7Sgus knight
7789ad24e7Sgus knightST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
7889ad24e7Sgus knight{
7989ad24e7Sgus knight    Section *tcc_sect;
8089ad24e7Sgus knight    SCNHDR *coff_sec;
8189ad24e7Sgus knight    int file_pointer;
8289ad24e7Sgus knight    char *Coff_str_table, *pCoff_str_table;
8389ad24e7Sgus knight    int CoffTextSectionNo, coff_nb_syms;
8489ad24e7Sgus knight    FILHDR file_hdr;		/* FILE HEADER STRUCTURE              */
8589ad24e7Sgus knight    Section *stext, *sdata, *sbss;
8689ad24e7Sgus knight    int i, NSectionsToOutput = 0;
8789ad24e7Sgus knight
8889ad24e7Sgus knight    Coff_str_table = pCoff_str_table = NULL;
8989ad24e7Sgus knight
9089ad24e7Sgus knight    stext = FindSection(s1, ".text");
9189ad24e7Sgus knight    sdata = FindSection(s1, ".data");
9289ad24e7Sgus knight    sbss = FindSection(s1, ".bss");
9389ad24e7Sgus knight
9489ad24e7Sgus knight    nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
9572729d8eSgrischka    coff_nb_syms = FindCoffSymbolIndex(s1, "XXXXXXXXXX1");
9689ad24e7Sgus knight
9789ad24e7Sgus knight    file_hdr.f_magic = COFF_C67_MAGIC;	/* magic number */
9889ad24e7Sgus knight    file_hdr.f_timdat = 0;	/* time & date stamp */
9989ad24e7Sgus knight    file_hdr.f_opthdr = sizeof(AOUTHDR);	/* sizeof(optional hdr) */
10089ad24e7Sgus knight    file_hdr.f_flags = 0x1143;	/* flags (copied from what code composer does) */
10189ad24e7Sgus knight    file_hdr.f_TargetID = 0x99;	/* for C6x = 0x0099 */
10289ad24e7Sgus knight
10389ad24e7Sgus knight    o_filehdr.magic = 0x0108;	/* see magic.h                          */
10489ad24e7Sgus knight    o_filehdr.vstamp = 0x0190;	/* version stamp                        */
10589ad24e7Sgus knight    o_filehdr.tsize = stext->data_offset;	/* text size in bytes, padded to FW bdry */
10689ad24e7Sgus knight    o_filehdr.dsize = sdata->data_offset;	/* initialized data "  "                */
10789ad24e7Sgus knight    o_filehdr.bsize = sbss->data_offset;	/* uninitialized data "   "             */
10889ad24e7Sgus knight    o_filehdr.entrypt = C67_main_entry_point;	/* entry pt.                          */
10989ad24e7Sgus knight    o_filehdr.text_start = stext->sh_addr;	/* base of text used for this file      */
11089ad24e7Sgus knight    o_filehdr.data_start = sdata->sh_addr;	/* base of data used for this file      */
11189ad24e7Sgus knight
11289ad24e7Sgus knight
11389ad24e7Sgus knight    // create all the section headers
11489ad24e7Sgus knight
11589ad24e7Sgus knight    file_pointer = FILHSZ + sizeof(AOUTHDR);
11689ad24e7Sgus knight
11789ad24e7Sgus knight    CoffTextSectionNo = -1;
11889ad24e7Sgus knight
11989ad24e7Sgus knight    for (i = 1; i < s1->nb_sections; i++) {
12089ad24e7Sgus knight	coff_sec = &section_header[i];
12189ad24e7Sgus knight	tcc_sect = s1->sections[i];
12289ad24e7Sgus knight
12389ad24e7Sgus knight	if (OutputTheSection(tcc_sect)) {
12489ad24e7Sgus knight	    NSectionsToOutput++;
12589ad24e7Sgus knight
12689ad24e7Sgus knight	    if (CoffTextSectionNo == -1 && tcc_sect == stext)
12789ad24e7Sgus knight		CoffTextSectionNo = NSectionsToOutput;	// rem which coff sect number the .text sect is
12889ad24e7Sgus knight
12989ad24e7Sgus knight	    strcpy(coff_sec->s_name, tcc_sect->name);	/* section name */
13089ad24e7Sgus knight
13189ad24e7Sgus knight	    coff_sec->s_paddr = tcc_sect->sh_addr;	/* physical address */
13289ad24e7Sgus knight	    coff_sec->s_vaddr = tcc_sect->sh_addr;	/* virtual address */
13389ad24e7Sgus knight	    coff_sec->s_size = tcc_sect->data_offset;	/* section size */
13489ad24e7Sgus knight	    coff_sec->s_scnptr = 0;	/* file ptr to raw data for section */
13589ad24e7Sgus knight	    coff_sec->s_relptr = 0;	/* file ptr to relocation */
13689ad24e7Sgus knight	    coff_sec->s_lnnoptr = 0;	/* file ptr to line numbers */
13789ad24e7Sgus knight	    coff_sec->s_nreloc = 0;	/* number of relocation entries */
13889ad24e7Sgus knight	    coff_sec->s_flags = GetCoffFlags(coff_sec->s_name);	/* flags */
13989ad24e7Sgus knight	    coff_sec->s_reserved = 0;	/* reserved byte */
14089ad24e7Sgus knight	    coff_sec->s_page = 0;	/* memory page id */
14189ad24e7Sgus knight
14289ad24e7Sgus knight	    file_pointer += sizeof(SCNHDR);
14389ad24e7Sgus knight	}
14489ad24e7Sgus knight    }
14589ad24e7Sgus knight
14689ad24e7Sgus knight    file_hdr.f_nscns = NSectionsToOutput;	/* number of sections */
14789ad24e7Sgus knight
14889ad24e7Sgus knight    // now loop through and determine file pointer locations
14989ad24e7Sgus knight    // for the raw data
15089ad24e7Sgus knight
15189ad24e7Sgus knight
15289ad24e7Sgus knight    for (i = 1; i < s1->nb_sections; i++) {
15389ad24e7Sgus knight	coff_sec = &section_header[i];
15489ad24e7Sgus knight	tcc_sect = s1->sections[i];
15589ad24e7Sgus knight
15689ad24e7Sgus knight	if (OutputTheSection(tcc_sect)) {
15789ad24e7Sgus knight	    // put raw data
15889ad24e7Sgus knight	    coff_sec->s_scnptr = file_pointer;	/* file ptr to raw data for section */
15989ad24e7Sgus knight	    file_pointer += coff_sec->s_size;
16089ad24e7Sgus knight	}
16189ad24e7Sgus knight    }
16289ad24e7Sgus knight
16389ad24e7Sgus knight    // now loop through and determine file pointer locations
16489ad24e7Sgus knight    // for the relocation data
16589ad24e7Sgus knight
16689ad24e7Sgus knight    for (i = 1; i < s1->nb_sections; i++) {
16789ad24e7Sgus knight	coff_sec = &section_header[i];
16889ad24e7Sgus knight	tcc_sect = s1->sections[i];
16989ad24e7Sgus knight
17089ad24e7Sgus knight	if (OutputTheSection(tcc_sect)) {
17189ad24e7Sgus knight	    // put relocations data
17289ad24e7Sgus knight	    if (coff_sec->s_nreloc > 0) {
17389ad24e7Sgus knight		coff_sec->s_relptr = file_pointer;	/* file ptr to relocation */
17489ad24e7Sgus knight		file_pointer += coff_sec->s_nreloc * sizeof(struct reloc);
17589ad24e7Sgus knight	    }
17689ad24e7Sgus knight	}
17789ad24e7Sgus knight    }
17889ad24e7Sgus knight
17989ad24e7Sgus knight    // now loop through and determine file pointer locations
18089ad24e7Sgus knight    // for the line number data
18189ad24e7Sgus knight
18289ad24e7Sgus knight    for (i = 1; i < s1->nb_sections; i++) {
18389ad24e7Sgus knight	coff_sec = &section_header[i];
18489ad24e7Sgus knight	tcc_sect = s1->sections[i];
18589ad24e7Sgus knight
18689ad24e7Sgus knight	coff_sec->s_nlnno = 0;
18789ad24e7Sgus knight	coff_sec->s_lnnoptr = 0;
18889ad24e7Sgus knight
18989ad24e7Sgus knight	if (s1->do_debug && tcc_sect == stext) {
19089ad24e7Sgus knight	    // count how many line nos data
19189ad24e7Sgus knight
19289ad24e7Sgus knight	    // also find association between source file name and function
19389ad24e7Sgus knight	    // so we can sort the symbol table
19489ad24e7Sgus knight
19589ad24e7Sgus knight
19689ad24e7Sgus knight	    Stab_Sym *sym, *sym_end;
19789ad24e7Sgus knight	    char func_name[MAX_FUNC_NAME_LENGTH],
19889ad24e7Sgus knight		last_func_name[MAX_FUNC_NAME_LENGTH];
19989ad24e7Sgus knight	    unsigned long func_addr, last_pc, pc;
20089ad24e7Sgus knight	    const char *incl_files[INCLUDE_STACK_SIZE];
20189ad24e7Sgus knight	    int incl_index, len, last_line_num;
20289ad24e7Sgus knight	    const char *str, *p;
20389ad24e7Sgus knight
20489ad24e7Sgus knight	    coff_sec->s_lnnoptr = file_pointer;	/* file ptr to linno */
20589ad24e7Sgus knight
20689ad24e7Sgus knight
20789ad24e7Sgus knight	    func_name[0] = '\0';
20889ad24e7Sgus knight	    func_addr = 0;
20989ad24e7Sgus knight	    incl_index = 0;
21089ad24e7Sgus knight	    last_func_name[0] = '\0';
21189ad24e7Sgus knight	    last_pc = 0xffffffff;
21289ad24e7Sgus knight	    last_line_num = 1;
21389ad24e7Sgus knight	    sym = (Stab_Sym *) stab_section->data + 1;
21489ad24e7Sgus knight	    sym_end =
21589ad24e7Sgus knight		(Stab_Sym *) (stab_section->data +
21689ad24e7Sgus knight			      stab_section->data_offset);
21789ad24e7Sgus knight
21889ad24e7Sgus knight	    nFuncs = 0;
21989ad24e7Sgus knight	    while (sym < sym_end) {
22089ad24e7Sgus knight		switch (sym->n_type) {
22189ad24e7Sgus knight		    /* function start or end */
22289ad24e7Sgus knight		case N_FUN:
22389ad24e7Sgus knight		    if (sym->n_strx == 0) {
22489ad24e7Sgus knight			// end of function
22589ad24e7Sgus knight
22689ad24e7Sgus knight			coff_sec->s_nlnno++;
22789ad24e7Sgus knight			file_pointer += LINESZ;
22889ad24e7Sgus knight
22989ad24e7Sgus knight			pc = sym->n_value + func_addr;
23089ad24e7Sgus knight			func_name[0] = '\0';
23189ad24e7Sgus knight			func_addr = 0;
23289ad24e7Sgus knight			EndAddress[nFuncs] = pc;
23389ad24e7Sgus knight			FuncEntries[nFuncs] =
23489ad24e7Sgus knight			    (file_pointer -
23589ad24e7Sgus knight			     LineNoFilePtr[nFuncs]) / LINESZ - 1;
23689ad24e7Sgus knight			LastLineNo[nFuncs++] = last_line_num + 1;
23789ad24e7Sgus knight		    } else {
23889ad24e7Sgus knight			// beginning of function
23989ad24e7Sgus knight
24089ad24e7Sgus knight			LineNoFilePtr[nFuncs] = file_pointer;
24189ad24e7Sgus knight			coff_sec->s_nlnno++;
24289ad24e7Sgus knight			file_pointer += LINESZ;
24389ad24e7Sgus knight
24489ad24e7Sgus knight			str =
24589ad24e7Sgus knight			    (const char *) stabstr_section->data +
24689ad24e7Sgus knight			    sym->n_strx;
24789ad24e7Sgus knight
24889ad24e7Sgus knight			p = strchr(str, ':');
24989ad24e7Sgus knight			if (!p) {
25089ad24e7Sgus knight			    pstrcpy(func_name, sizeof(func_name), str);
25189ad24e7Sgus knight			    pstrcpy(Func[nFuncs], sizeof(func_name), str);
25289ad24e7Sgus knight			} else {
25389ad24e7Sgus knight			    len = p - str;
25489ad24e7Sgus knight			    if (len > sizeof(func_name) - 1)
25589ad24e7Sgus knight				len = sizeof(func_name) - 1;
25689ad24e7Sgus knight			    memcpy(func_name, str, len);
25789ad24e7Sgus knight			    memcpy(Func[nFuncs], str, len);
25889ad24e7Sgus knight			    func_name[len] = '\0';
25989ad24e7Sgus knight			}
26089ad24e7Sgus knight
26189ad24e7Sgus knight			// save the file that it came in so we can sort later
26289ad24e7Sgus knight			pstrcpy(AssociatedFile[nFuncs], sizeof(func_name),
26389ad24e7Sgus knight				incl_files[incl_index - 1]);
26489ad24e7Sgus knight
26589ad24e7Sgus knight			func_addr = sym->n_value;
26689ad24e7Sgus knight		    }
26789ad24e7Sgus knight		    break;
26889ad24e7Sgus knight
26989ad24e7Sgus knight		    /* line number info */
27089ad24e7Sgus knight		case N_SLINE:
27189ad24e7Sgus knight		    pc = sym->n_value + func_addr;
27289ad24e7Sgus knight
27389ad24e7Sgus knight		    last_pc = pc;
27489ad24e7Sgus knight		    last_line_num = sym->n_desc;
27589ad24e7Sgus knight
27689ad24e7Sgus knight		    /* XXX: slow! */
27789ad24e7Sgus knight		    strcpy(last_func_name, func_name);
27889ad24e7Sgus knight
27989ad24e7Sgus knight		    coff_sec->s_nlnno++;
28089ad24e7Sgus knight		    file_pointer += LINESZ;
28189ad24e7Sgus knight		    break;
28289ad24e7Sgus knight		    /* include files */
28389ad24e7Sgus knight		case N_BINCL:
28489ad24e7Sgus knight		    str =
28589ad24e7Sgus knight			(const char *) stabstr_section->data + sym->n_strx;
28689ad24e7Sgus knight		  add_incl:
28789ad24e7Sgus knight		    if (incl_index < INCLUDE_STACK_SIZE) {
28889ad24e7Sgus knight			incl_files[incl_index++] = str;
28989ad24e7Sgus knight		    }
29089ad24e7Sgus knight		    break;
29189ad24e7Sgus knight		case N_EINCL:
29289ad24e7Sgus knight		    if (incl_index > 1)
29389ad24e7Sgus knight			incl_index--;
29489ad24e7Sgus knight		    break;
29589ad24e7Sgus knight		case N_SO:
29689ad24e7Sgus knight		    if (sym->n_strx == 0) {
29789ad24e7Sgus knight			incl_index = 0;	/* end of translation unit */
29889ad24e7Sgus knight		    } else {
29989ad24e7Sgus knight			str =
30089ad24e7Sgus knight			    (const char *) stabstr_section->data +
30189ad24e7Sgus knight			    sym->n_strx;
30289ad24e7Sgus knight			/* do not add path */
30389ad24e7Sgus knight			len = strlen(str);
30489ad24e7Sgus knight			if (len > 0 && str[len - 1] != '/')
30589ad24e7Sgus knight			    goto add_incl;
30689ad24e7Sgus knight		    }
30789ad24e7Sgus knight		    break;
30889ad24e7Sgus knight		}
30989ad24e7Sgus knight		sym++;
31089ad24e7Sgus knight	    }
31189ad24e7Sgus knight	}
31289ad24e7Sgus knight
31389ad24e7Sgus knight    }
31489ad24e7Sgus knight
31589ad24e7Sgus knight    file_hdr.f_symptr = file_pointer;	/* file pointer to symtab */
31689ad24e7Sgus knight
31789ad24e7Sgus knight    if (s1->do_debug)
31889ad24e7Sgus knight	file_hdr.f_nsyms = coff_nb_syms;	/* number of symtab entries */
31989ad24e7Sgus knight    else
32089ad24e7Sgus knight	file_hdr.f_nsyms = 0;
32189ad24e7Sgus knight
32289ad24e7Sgus knight    file_pointer += file_hdr.f_nsyms * SYMNMLEN;
32389ad24e7Sgus knight
32489ad24e7Sgus knight    // OK now we are all set to write the file
32589ad24e7Sgus knight
32689ad24e7Sgus knight
32789ad24e7Sgus knight    fwrite(&file_hdr, FILHSZ, 1, f);
32889ad24e7Sgus knight    fwrite(&o_filehdr, sizeof(o_filehdr), 1, f);
32989ad24e7Sgus knight
33089ad24e7Sgus knight    // write section headers
33189ad24e7Sgus knight    for (i = 1; i < s1->nb_sections; i++) {
33289ad24e7Sgus knight	coff_sec = &section_header[i];
33389ad24e7Sgus knight	tcc_sect = s1->sections[i];
33489ad24e7Sgus knight
33589ad24e7Sgus knight	if (OutputTheSection(tcc_sect)) {
33689ad24e7Sgus knight	    fwrite(coff_sec, sizeof(SCNHDR), 1, f);
33789ad24e7Sgus knight	}
33889ad24e7Sgus knight    }
33989ad24e7Sgus knight
34089ad24e7Sgus knight    // write raw data
34189ad24e7Sgus knight    for (i = 1; i < s1->nb_sections; i++) {
34289ad24e7Sgus knight	coff_sec = &section_header[i];
34389ad24e7Sgus knight	tcc_sect = s1->sections[i];
34489ad24e7Sgus knight
34589ad24e7Sgus knight	if (OutputTheSection(tcc_sect)) {
34689ad24e7Sgus knight	    fwrite(tcc_sect->data, tcc_sect->data_offset, 1, f);
34789ad24e7Sgus knight	}
34889ad24e7Sgus knight    }
34989ad24e7Sgus knight
35089ad24e7Sgus knight    // write relocation data
35189ad24e7Sgus knight    for (i = 1; i < s1->nb_sections; i++) {
35289ad24e7Sgus knight	coff_sec = &section_header[i];
35389ad24e7Sgus knight	tcc_sect = s1->sections[i];
35489ad24e7Sgus knight
35589ad24e7Sgus knight	if (OutputTheSection(tcc_sect)) {
35689ad24e7Sgus knight	    // put relocations data
35789ad24e7Sgus knight	    if (coff_sec->s_nreloc > 0) {
35889ad24e7Sgus knight		fwrite(tcc_sect->reloc,
35989ad24e7Sgus knight		       coff_sec->s_nreloc * sizeof(struct reloc), 1, f);
36089ad24e7Sgus knight	    }
36189ad24e7Sgus knight	}
36289ad24e7Sgus knight    }
36389ad24e7Sgus knight
36489ad24e7Sgus knight
36589ad24e7Sgus knight    // group the symbols in order of filename, func1, func2, etc
36689ad24e7Sgus knight    // finally global symbols
36789ad24e7Sgus knight
36889ad24e7Sgus knight    if (s1->do_debug)
36972729d8eSgrischka	SortSymbolTable(s1);
37089ad24e7Sgus knight
37189ad24e7Sgus knight    // write line no data
37289ad24e7Sgus knight
37389ad24e7Sgus knight    for (i = 1; i < s1->nb_sections; i++) {
37489ad24e7Sgus knight	coff_sec = &section_header[i];
37589ad24e7Sgus knight	tcc_sect = s1->sections[i];
37689ad24e7Sgus knight
37789ad24e7Sgus knight	if (s1->do_debug && tcc_sect == stext) {
37889ad24e7Sgus knight	    // count how many line nos data
37989ad24e7Sgus knight
38089ad24e7Sgus knight
38189ad24e7Sgus knight	    Stab_Sym *sym, *sym_end;
38289ad24e7Sgus knight	    char func_name[128], last_func_name[128];
38389ad24e7Sgus knight	    unsigned long func_addr, last_pc, pc;
38489ad24e7Sgus knight	    const char *incl_files[INCLUDE_STACK_SIZE];
38589ad24e7Sgus knight	    int incl_index, len, last_line_num;
38689ad24e7Sgus knight	    const char *str, *p;
38789ad24e7Sgus knight
38889ad24e7Sgus knight	    LINENO CoffLineNo;
38989ad24e7Sgus knight
39089ad24e7Sgus knight	    func_name[0] = '\0';
39189ad24e7Sgus knight	    func_addr = 0;
39289ad24e7Sgus knight	    incl_index = 0;
39389ad24e7Sgus knight	    last_func_name[0] = '\0';
39489ad24e7Sgus knight	    last_pc = 0;
39589ad24e7Sgus knight	    last_line_num = 1;
39689ad24e7Sgus knight	    sym = (Stab_Sym *) stab_section->data + 1;
39789ad24e7Sgus knight	    sym_end =
39889ad24e7Sgus knight		(Stab_Sym *) (stab_section->data +
39989ad24e7Sgus knight			      stab_section->data_offset);
40089ad24e7Sgus knight
40189ad24e7Sgus knight	    while (sym < sym_end) {
40289ad24e7Sgus knight		switch (sym->n_type) {
40389ad24e7Sgus knight		    /* function start or end */
40489ad24e7Sgus knight		case N_FUN:
40589ad24e7Sgus knight		    if (sym->n_strx == 0) {
40689ad24e7Sgus knight			// end of function
40789ad24e7Sgus knight
40889ad24e7Sgus knight			CoffLineNo.l_addr.l_paddr = last_pc;
40989ad24e7Sgus knight			CoffLineNo.l_lnno = last_line_num + 1;
41089ad24e7Sgus knight			fwrite(&CoffLineNo, 6, 1, f);
41189ad24e7Sgus knight
41289ad24e7Sgus knight			pc = sym->n_value + func_addr;
41389ad24e7Sgus knight			func_name[0] = '\0';
41489ad24e7Sgus knight			func_addr = 0;
41589ad24e7Sgus knight		    } else {
41689ad24e7Sgus knight			// beginning of function
41789ad24e7Sgus knight
41889ad24e7Sgus knight			str =
41989ad24e7Sgus knight			    (const char *) stabstr_section->data +
42089ad24e7Sgus knight			    sym->n_strx;
42189ad24e7Sgus knight
42289ad24e7Sgus knight
42389ad24e7Sgus knight			p = strchr(str, ':');
42489ad24e7Sgus knight			if (!p) {
42589ad24e7Sgus knight			    pstrcpy(func_name, sizeof(func_name), str);
42689ad24e7Sgus knight			} else {
42789ad24e7Sgus knight			    len = p - str;
42889ad24e7Sgus knight			    if (len > sizeof(func_name) - 1)
42989ad24e7Sgus knight				len = sizeof(func_name) - 1;
43089ad24e7Sgus knight			    memcpy(func_name, str, len);
43189ad24e7Sgus knight			    func_name[len] = '\0';
43289ad24e7Sgus knight			}
43389ad24e7Sgus knight			func_addr = sym->n_value;
43489ad24e7Sgus knight			last_pc = func_addr;
43589ad24e7Sgus knight			last_line_num = -1;
43689ad24e7Sgus knight
43789ad24e7Sgus knight			// output a function begin
43889ad24e7Sgus knight
43989ad24e7Sgus knight			CoffLineNo.l_addr.l_symndx =
44072729d8eSgrischka			    FindCoffSymbolIndex(s1, func_name);
44189ad24e7Sgus knight			CoffLineNo.l_lnno = 0;
44289ad24e7Sgus knight
44389ad24e7Sgus knight			fwrite(&CoffLineNo, 6, 1, f);
44489ad24e7Sgus knight		    }
44589ad24e7Sgus knight		    break;
44689ad24e7Sgus knight
44789ad24e7Sgus knight		    /* line number info */
44889ad24e7Sgus knight		case N_SLINE:
44989ad24e7Sgus knight		    pc = sym->n_value + func_addr;
45089ad24e7Sgus knight
45189ad24e7Sgus knight
45289ad24e7Sgus knight		    /* XXX: slow! */
45389ad24e7Sgus knight		    strcpy(last_func_name, func_name);
45489ad24e7Sgus knight
45589ad24e7Sgus knight		    // output a line reference
45689ad24e7Sgus knight
45789ad24e7Sgus knight		    CoffLineNo.l_addr.l_paddr = last_pc;
45889ad24e7Sgus knight
45989ad24e7Sgus knight		    if (last_line_num == -1) {
46089ad24e7Sgus knight			CoffLineNo.l_lnno = sym->n_desc;
46189ad24e7Sgus knight		    } else {
46289ad24e7Sgus knight			CoffLineNo.l_lnno = last_line_num + 1;
46389ad24e7Sgus knight		    }
46489ad24e7Sgus knight
46589ad24e7Sgus knight		    fwrite(&CoffLineNo, 6, 1, f);
46689ad24e7Sgus knight
46789ad24e7Sgus knight		    last_pc = pc;
46889ad24e7Sgus knight		    last_line_num = sym->n_desc;
46989ad24e7Sgus knight
47089ad24e7Sgus knight		    break;
47189ad24e7Sgus knight
47289ad24e7Sgus knight		    /* include files */
47389ad24e7Sgus knight		case N_BINCL:
47489ad24e7Sgus knight		    str =
47589ad24e7Sgus knight			(const char *) stabstr_section->data + sym->n_strx;
47689ad24e7Sgus knight		  add_incl2:
47789ad24e7Sgus knight		    if (incl_index < INCLUDE_STACK_SIZE) {
47889ad24e7Sgus knight			incl_files[incl_index++] = str;
47989ad24e7Sgus knight		    }
48089ad24e7Sgus knight		    break;
48189ad24e7Sgus knight		case N_EINCL:
48289ad24e7Sgus knight		    if (incl_index > 1)
48389ad24e7Sgus knight			incl_index--;
48489ad24e7Sgus knight		    break;
48589ad24e7Sgus knight		case N_SO:
48689ad24e7Sgus knight		    if (sym->n_strx == 0) {
48789ad24e7Sgus knight			incl_index = 0;	/* end of translation unit */
48889ad24e7Sgus knight		    } else {
48989ad24e7Sgus knight			str =
49089ad24e7Sgus knight			    (const char *) stabstr_section->data +
49189ad24e7Sgus knight			    sym->n_strx;
49289ad24e7Sgus knight			/* do not add path */
49389ad24e7Sgus knight			len = strlen(str);
49489ad24e7Sgus knight			if (len > 0 && str[len - 1] != '/')
49589ad24e7Sgus knight			    goto add_incl2;
49689ad24e7Sgus knight		    }
49789ad24e7Sgus knight		    break;
49889ad24e7Sgus knight		}
49989ad24e7Sgus knight		sym++;
50089ad24e7Sgus knight	    }
50189ad24e7Sgus knight	}
50289ad24e7Sgus knight    }
50389ad24e7Sgus knight
50489ad24e7Sgus knight    // write symbol table
50589ad24e7Sgus knight    if (s1->do_debug) {
50689ad24e7Sgus knight	int k;
50789ad24e7Sgus knight	struct syment csym;
50889ad24e7Sgus knight	AUXFUNC auxfunc;
50989ad24e7Sgus knight	AUXBF auxbf;
51089ad24e7Sgus knight	AUXEF auxef;
51189ad24e7Sgus knight	int i;
51289ad24e7Sgus knight	Elf32_Sym *p;
51389ad24e7Sgus knight	const char *name;
51489ad24e7Sgus knight	int nstr;
51589ad24e7Sgus knight	int n = 0;
51689ad24e7Sgus knight
51789ad24e7Sgus knight	Coff_str_table = (char *) tcc_malloc(MAX_STR_TABLE);
51889ad24e7Sgus knight	pCoff_str_table = Coff_str_table;
51989ad24e7Sgus knight	nstr = 0;
52089ad24e7Sgus knight
52189ad24e7Sgus knight	p = (Elf32_Sym *) symtab_section->data;
52289ad24e7Sgus knight
52389ad24e7Sgus knight
52489ad24e7Sgus knight	for (i = 0; i < nb_syms; i++) {
52589ad24e7Sgus knight
52689ad24e7Sgus knight	    name = symtab_section->link->data + p->st_name;
52789ad24e7Sgus knight
52889ad24e7Sgus knight	    for (k = 0; k < 8; k++)
52989ad24e7Sgus knight		csym._n._n_name[k] = 0;
53089ad24e7Sgus knight
53189ad24e7Sgus knight	    if (strlen(name) <= 8) {
53289ad24e7Sgus knight		strcpy(csym._n._n_name, name);
53389ad24e7Sgus knight	    } else {
53489ad24e7Sgus knight		if (pCoff_str_table - Coff_str_table + strlen(name) >
53589ad24e7Sgus knight		    MAX_STR_TABLE - 1)
53689ad24e7Sgus knight		    tcc_error("String table too large");
53789ad24e7Sgus knight
53889ad24e7Sgus knight		csym._n._n_n._n_zeroes = 0;
53989ad24e7Sgus knight		csym._n._n_n._n_offset =
54089ad24e7Sgus knight		    pCoff_str_table - Coff_str_table + 4;
54189ad24e7Sgus knight
54289ad24e7Sgus knight		strcpy(pCoff_str_table, name);
54389ad24e7Sgus knight		pCoff_str_table += strlen(name) + 1;	// skip over null
54489ad24e7Sgus knight		nstr++;
54589ad24e7Sgus knight	    }
54689ad24e7Sgus knight
54789ad24e7Sgus knight	    if (p->st_info == 4) {
54889ad24e7Sgus knight		// put a filename symbol
54989ad24e7Sgus knight		csym.n_value = 33;	// ?????
55089ad24e7Sgus knight		csym.n_scnum = N_DEBUG;
55189ad24e7Sgus knight		csym.n_type = 0;
55289ad24e7Sgus knight		csym.n_sclass = C_FILE;
55389ad24e7Sgus knight		csym.n_numaux = 0;
55489ad24e7Sgus knight		fwrite(&csym, 18, 1, f);
55589ad24e7Sgus knight		n++;
55689ad24e7Sgus knight
55789ad24e7Sgus knight	    } else if (p->st_info == 0x12) {
55889ad24e7Sgus knight		// find the function data
55989ad24e7Sgus knight
56089ad24e7Sgus knight		for (k = 0; k < nFuncs; k++) {
56189ad24e7Sgus knight		    if (strcmp(name, Func[k]) == 0)
56289ad24e7Sgus knight			break;
56389ad24e7Sgus knight		}
56489ad24e7Sgus knight
56589ad24e7Sgus knight		if (k >= nFuncs) {
56689ad24e7Sgus knight		    tcc_error("debug info can't find function: %s", name);
56789ad24e7Sgus knight		}
56889ad24e7Sgus knight		// put a Function Name
56989ad24e7Sgus knight
57089ad24e7Sgus knight		csym.n_value = p->st_value;	// physical address
57189ad24e7Sgus knight		csym.n_scnum = CoffTextSectionNo;
57289ad24e7Sgus knight		csym.n_type = MKTYPE(T_INT, DT_FCN, 0, 0, 0, 0, 0);
57389ad24e7Sgus knight		csym.n_sclass = C_EXT;
57489ad24e7Sgus knight		csym.n_numaux = 1;
57589ad24e7Sgus knight		fwrite(&csym, 18, 1, f);
57689ad24e7Sgus knight
57789ad24e7Sgus knight		// now put aux info
57889ad24e7Sgus knight
57989ad24e7Sgus knight		auxfunc.tag = 0;
58089ad24e7Sgus knight		auxfunc.size = EndAddress[k] - p->st_value;
58189ad24e7Sgus knight		auxfunc.fileptr = LineNoFilePtr[k];
58289ad24e7Sgus knight		auxfunc.nextsym = n + 6;	// tktk
58389ad24e7Sgus knight		auxfunc.dummy = 0;
58489ad24e7Sgus knight		fwrite(&auxfunc, 18, 1, f);
58589ad24e7Sgus knight
58689ad24e7Sgus knight		// put a .bf
58789ad24e7Sgus knight
58889ad24e7Sgus knight		strcpy(csym._n._n_name, ".bf");
58989ad24e7Sgus knight		csym.n_value = p->st_value;	// physical address
59089ad24e7Sgus knight		csym.n_scnum = CoffTextSectionNo;
59189ad24e7Sgus knight		csym.n_type = 0;
59289ad24e7Sgus knight		csym.n_sclass = C_FCN;
59389ad24e7Sgus knight		csym.n_numaux = 1;
59489ad24e7Sgus knight		fwrite(&csym, 18, 1, f);
59589ad24e7Sgus knight
59689ad24e7Sgus knight		// now put aux info
59789ad24e7Sgus knight
59889ad24e7Sgus knight		auxbf.regmask = 0;
59989ad24e7Sgus knight		auxbf.lineno = 0;
60089ad24e7Sgus knight		auxbf.nentries = FuncEntries[k];
60189ad24e7Sgus knight		auxbf.localframe = 0;
60289ad24e7Sgus knight		auxbf.nextentry = n + 6;
60389ad24e7Sgus knight		auxbf.dummy = 0;
60489ad24e7Sgus knight		fwrite(&auxbf, 18, 1, f);
60589ad24e7Sgus knight
60689ad24e7Sgus knight		// put a .ef
60789ad24e7Sgus knight
60889ad24e7Sgus knight		strcpy(csym._n._n_name, ".ef");
60989ad24e7Sgus knight		csym.n_value = EndAddress[k];	// physical address
61089ad24e7Sgus knight		csym.n_scnum = CoffTextSectionNo;
61189ad24e7Sgus knight		csym.n_type = 0;
61289ad24e7Sgus knight		csym.n_sclass = C_FCN;
61389ad24e7Sgus knight		csym.n_numaux = 1;
61489ad24e7Sgus knight		fwrite(&csym, 18, 1, f);
61589ad24e7Sgus knight
61689ad24e7Sgus knight		// now put aux info
61789ad24e7Sgus knight
61889ad24e7Sgus knight		auxef.dummy = 0;
61989ad24e7Sgus knight		auxef.lineno = LastLineNo[k];
62089ad24e7Sgus knight		auxef.dummy1 = 0;
62189ad24e7Sgus knight		auxef.dummy2 = 0;
62289ad24e7Sgus knight		auxef.dummy3 = 0;
62389ad24e7Sgus knight		auxef.dummy4 = 0;
62489ad24e7Sgus knight		fwrite(&auxef, 18, 1, f);
62589ad24e7Sgus knight
62689ad24e7Sgus knight		n += 6;
62789ad24e7Sgus knight
62889ad24e7Sgus knight	    } else {
62989ad24e7Sgus knight		// try an put some type info
63089ad24e7Sgus knight
63189ad24e7Sgus knight		if ((p->st_other & VT_BTYPE) == VT_DOUBLE) {
63289ad24e7Sgus knight		    csym.n_type = T_DOUBLE;	// int
63389ad24e7Sgus knight		    csym.n_sclass = C_EXT;
63489ad24e7Sgus knight		} else if ((p->st_other & VT_BTYPE) == VT_FLOAT) {
63589ad24e7Sgus knight		    csym.n_type = T_FLOAT;
63689ad24e7Sgus knight		    csym.n_sclass = C_EXT;
63789ad24e7Sgus knight		} else if ((p->st_other & VT_BTYPE) == VT_INT) {
63889ad24e7Sgus knight		    csym.n_type = T_INT;	// int
63989ad24e7Sgus knight		    csym.n_sclass = C_EXT;
64089ad24e7Sgus knight		} else if ((p->st_other & VT_BTYPE) == VT_SHORT) {
64189ad24e7Sgus knight		    csym.n_type = T_SHORT;
64289ad24e7Sgus knight		    csym.n_sclass = C_EXT;
64389ad24e7Sgus knight		} else if ((p->st_other & VT_BTYPE) == VT_BYTE) {
64489ad24e7Sgus knight		    csym.n_type = T_CHAR;
64589ad24e7Sgus knight		    csym.n_sclass = C_EXT;
64689ad24e7Sgus knight		} else {
64789ad24e7Sgus knight		    csym.n_type = T_INT;	// just mark as a label
64889ad24e7Sgus knight		    csym.n_sclass = C_LABEL;
64989ad24e7Sgus knight		}
65089ad24e7Sgus knight
65189ad24e7Sgus knight
65289ad24e7Sgus knight		csym.n_value = p->st_value;
65389ad24e7Sgus knight		csym.n_scnum = 2;
65489ad24e7Sgus knight		csym.n_numaux = 1;
65589ad24e7Sgus knight		fwrite(&csym, 18, 1, f);
65689ad24e7Sgus knight
65789ad24e7Sgus knight		auxfunc.tag = 0;
65889ad24e7Sgus knight		auxfunc.size = 0x20;
65989ad24e7Sgus knight		auxfunc.fileptr = 0;
66089ad24e7Sgus knight		auxfunc.nextsym = 0;
66189ad24e7Sgus knight		auxfunc.dummy = 0;
66289ad24e7Sgus knight		fwrite(&auxfunc, 18, 1, f);
66389ad24e7Sgus knight		n++;
66489ad24e7Sgus knight		n++;
66589ad24e7Sgus knight
66689ad24e7Sgus knight	    }
66789ad24e7Sgus knight
66889ad24e7Sgus knight	    p++;
66989ad24e7Sgus knight	}
67089ad24e7Sgus knight    }
67189ad24e7Sgus knight
67289ad24e7Sgus knight    if (s1->do_debug) {
67389ad24e7Sgus knight	// write string table
67489ad24e7Sgus knight
67589ad24e7Sgus knight	// first write the size
67689ad24e7Sgus knight	i = pCoff_str_table - Coff_str_table;
67789ad24e7Sgus knight	fwrite(&i, 4, 1, f);
67889ad24e7Sgus knight
67989ad24e7Sgus knight	// then write the strings
68089ad24e7Sgus knight	fwrite(Coff_str_table, i, 1, f);
68189ad24e7Sgus knight
68289ad24e7Sgus knight	tcc_free(Coff_str_table);
68389ad24e7Sgus knight    }
68489ad24e7Sgus knight
68589ad24e7Sgus knight    return 0;
68689ad24e7Sgus knight}
68789ad24e7Sgus knight
68889ad24e7Sgus knight
68989ad24e7Sgus knight
69089ad24e7Sgus knight// group the symbols in order of filename, func1, func2, etc
69189ad24e7Sgus knight// finally global symbols
69289ad24e7Sgus knight
69372729d8eSgrischkavoid SortSymbolTable(TCCState *s1)
69489ad24e7Sgus knight{
69589ad24e7Sgus knight    int i, j, k, n = 0;
69689ad24e7Sgus knight    Elf32_Sym *p, *p2, *NewTable;
69789ad24e7Sgus knight    char *name, *name2;
69889ad24e7Sgus knight
69989ad24e7Sgus knight    NewTable = (Elf32_Sym *) tcc_malloc(nb_syms * sizeof(Elf32_Sym));
70089ad24e7Sgus knight
70189ad24e7Sgus knight    p = (Elf32_Sym *) symtab_section->data;
70289ad24e7Sgus knight
70389ad24e7Sgus knight
70489ad24e7Sgus knight    // find a file symbol, copy it over
70589ad24e7Sgus knight    // then scan the whole symbol list and copy any function
70689ad24e7Sgus knight    // symbols that match the file association
70789ad24e7Sgus knight
70889ad24e7Sgus knight    for (i = 0; i < nb_syms; i++) {
70989ad24e7Sgus knight	if (p->st_info == 4) {
71089ad24e7Sgus knight	    name = (char *) symtab_section->link->data + p->st_name;
71189ad24e7Sgus knight
71289ad24e7Sgus knight	    // this is a file symbol, copy it over
71389ad24e7Sgus knight
71489ad24e7Sgus knight	    NewTable[n++] = *p;
71589ad24e7Sgus knight
71689ad24e7Sgus knight	    p2 = (Elf32_Sym *) symtab_section->data;
71789ad24e7Sgus knight
71889ad24e7Sgus knight	    for (j = 0; j < nb_syms; j++) {
71989ad24e7Sgus knight		if (p2->st_info == 0x12) {
72089ad24e7Sgus knight		    // this is a func symbol
72189ad24e7Sgus knight
72289ad24e7Sgus knight		    name2 =
72389ad24e7Sgus knight			(char *) symtab_section->link->data + p2->st_name;
72489ad24e7Sgus knight
72589ad24e7Sgus knight		    // find the function data index
72689ad24e7Sgus knight
72789ad24e7Sgus knight		    for (k = 0; k < nFuncs; k++) {
72889ad24e7Sgus knight			if (strcmp(name2, Func[k]) == 0)
72989ad24e7Sgus knight			    break;
73089ad24e7Sgus knight		    }
73189ad24e7Sgus knight
73289ad24e7Sgus knight		    if (k >= nFuncs) {
73389ad24e7Sgus knight                        tcc_error("debug (sort) info can't find function: %s", name2);
73489ad24e7Sgus knight		    }
73589ad24e7Sgus knight
73689ad24e7Sgus knight		    if (strcmp(AssociatedFile[k], name) == 0) {
73789ad24e7Sgus knight			// yes they match copy it over
73889ad24e7Sgus knight
73989ad24e7Sgus knight			NewTable[n++] = *p2;
74089ad24e7Sgus knight		    }
74189ad24e7Sgus knight		}
74289ad24e7Sgus knight		p2++;
74389ad24e7Sgus knight	    }
74489ad24e7Sgus knight	}
74589ad24e7Sgus knight	p++;
74689ad24e7Sgus knight    }
74789ad24e7Sgus knight
74889ad24e7Sgus knight    // now all the filename and func symbols should have been copied over
74989ad24e7Sgus knight    // copy all the rest over (all except file and funcs)
75089ad24e7Sgus knight
75189ad24e7Sgus knight    p = (Elf32_Sym *) symtab_section->data;
75289ad24e7Sgus knight    for (i = 0; i < nb_syms; i++) {
75389ad24e7Sgus knight	if (p->st_info != 4 && p->st_info != 0x12) {
75489ad24e7Sgus knight	    NewTable[n++] = *p;
75589ad24e7Sgus knight	}
75689ad24e7Sgus knight	p++;
75789ad24e7Sgus knight    }
75889ad24e7Sgus knight
75989ad24e7Sgus knight    if (n != nb_syms)
76089ad24e7Sgus knight	tcc_error("Internal Compiler error, debug info");
76189ad24e7Sgus knight
76289ad24e7Sgus knight    // copy it all back
76389ad24e7Sgus knight
76489ad24e7Sgus knight    p = (Elf32_Sym *) symtab_section->data;
76589ad24e7Sgus knight    for (i = 0; i < nb_syms; i++) {
76689ad24e7Sgus knight	*p++ = NewTable[i];
76789ad24e7Sgus knight    }
76889ad24e7Sgus knight
76989ad24e7Sgus knight    tcc_free(NewTable);
77089ad24e7Sgus knight}
77189ad24e7Sgus knight
77289ad24e7Sgus knight
77372729d8eSgrischkaint FindCoffSymbolIndex(TCCState *s1, const char *func_name)
77489ad24e7Sgus knight{
77589ad24e7Sgus knight    int i, n = 0;
77689ad24e7Sgus knight    Elf32_Sym *p;
77789ad24e7Sgus knight    char *name;
77889ad24e7Sgus knight
77989ad24e7Sgus knight    p = (Elf32_Sym *) symtab_section->data;
78089ad24e7Sgus knight
78189ad24e7Sgus knight    for (i = 0; i < nb_syms; i++) {
78289ad24e7Sgus knight
78389ad24e7Sgus knight	name = (char *) symtab_section->link->data + p->st_name;
78489ad24e7Sgus knight
78589ad24e7Sgus knight	if (p->st_info == 4) {
78689ad24e7Sgus knight	    // put a filename symbol
78789ad24e7Sgus knight	    n++;
78889ad24e7Sgus knight	} else if (p->st_info == 0x12) {
78989ad24e7Sgus knight
79089ad24e7Sgus knight	    if (strcmp(func_name, name) == 0)
79189ad24e7Sgus knight		return n;
79289ad24e7Sgus knight
79389ad24e7Sgus knight	    n += 6;
79489ad24e7Sgus knight
79589ad24e7Sgus knight	    // put a Function Name
79689ad24e7Sgus knight
79789ad24e7Sgus knight	    // now put aux info
79889ad24e7Sgus knight
79989ad24e7Sgus knight	    // put a .bf
80089ad24e7Sgus knight
80189ad24e7Sgus knight	    // now put aux info
80289ad24e7Sgus knight
80389ad24e7Sgus knight	    // put a .ef
80489ad24e7Sgus knight
80589ad24e7Sgus knight	    // now put aux info
80689ad24e7Sgus knight
80789ad24e7Sgus knight	} else {
80889ad24e7Sgus knight	    n += 2;
80989ad24e7Sgus knight	}
81089ad24e7Sgus knight
81189ad24e7Sgus knight	p++;
81289ad24e7Sgus knight    }
81389ad24e7Sgus knight
81489ad24e7Sgus knight    return n;			// total number of symbols
81589ad24e7Sgus knight}
81689ad24e7Sgus knight
81789ad24e7Sgus knightint OutputTheSection(Section * sect)
81889ad24e7Sgus knight{
81989ad24e7Sgus knight    const char *s = sect->name;
82089ad24e7Sgus knight
82189ad24e7Sgus knight    if (!strcmp(s, ".text"))
82289ad24e7Sgus knight	return 1;
82389ad24e7Sgus knight    else if (!strcmp(s, ".data"))
82489ad24e7Sgus knight	return 1;
82589ad24e7Sgus knight    else
82689ad24e7Sgus knight	return 0;
82789ad24e7Sgus knight}
82889ad24e7Sgus knight
82989ad24e7Sgus knightshort int GetCoffFlags(const char *s)
83089ad24e7Sgus knight{
83189ad24e7Sgus knight    if (!strcmp(s, ".text"))
83289ad24e7Sgus knight	return STYP_TEXT | STYP_DATA | STYP_ALIGN | 0x400;
83389ad24e7Sgus knight    else if (!strcmp(s, ".data"))
83489ad24e7Sgus knight	return STYP_DATA;
83589ad24e7Sgus knight    else if (!strcmp(s, ".bss"))
83689ad24e7Sgus knight	return STYP_BSS;
83789ad24e7Sgus knight    else if (!strcmp(s, ".stack"))
83889ad24e7Sgus knight	return STYP_BSS | STYP_ALIGN | 0x200;
83989ad24e7Sgus knight    else if (!strcmp(s, ".cinit"))
84089ad24e7Sgus knight	return STYP_COPY | STYP_DATA | STYP_ALIGN | 0x200;
84189ad24e7Sgus knight    else
84289ad24e7Sgus knight	return 0;
84389ad24e7Sgus knight}
84489ad24e7Sgus knight
84589ad24e7Sgus knightSection *FindSection(TCCState * s1, const char *sname)
84689ad24e7Sgus knight{
84789ad24e7Sgus knight    Section *s;
84889ad24e7Sgus knight    int i;
84989ad24e7Sgus knight
85089ad24e7Sgus knight    for (i = 1; i < s1->nb_sections; i++) {
85189ad24e7Sgus knight	s = s1->sections[i];
85289ad24e7Sgus knight
85389ad24e7Sgus knight	if (!strcmp(sname, s->name))
85489ad24e7Sgus knight	    return s;
85589ad24e7Sgus knight    }
85689ad24e7Sgus knight
85789ad24e7Sgus knight    tcc_error("could not find section %s", sname);
85889ad24e7Sgus knight    return 0;
85989ad24e7Sgus knight}
86089ad24e7Sgus knight
86189ad24e7Sgus knightST_FUNC int tcc_load_coff(TCCState * s1, int fd)
86289ad24e7Sgus knight{
86389ad24e7Sgus knight// tktk TokenSym *ts;
86489ad24e7Sgus knight
86589ad24e7Sgus knight    FILE *f;
86689ad24e7Sgus knight    unsigned int str_size;
86789ad24e7Sgus knight    char *Coff_str_table, *name;
86889ad24e7Sgus knight    int i, k;
86989ad24e7Sgus knight    struct syment csym;
87089ad24e7Sgus knight    char name2[9];
87189ad24e7Sgus knight    FILHDR file_hdr;		/* FILE HEADER STRUCTURE              */
87289ad24e7Sgus knight
87389ad24e7Sgus knight    f = fdopen(fd, "rb");
87489ad24e7Sgus knight    if (!f) {
87589ad24e7Sgus knight	tcc_error("Unable to open .out file for input");
87689ad24e7Sgus knight    }
87789ad24e7Sgus knight
87889ad24e7Sgus knight    if (fread(&file_hdr, FILHSZ, 1, f) != 1)
87989ad24e7Sgus knight	tcc_error("error reading .out file for input");
88089ad24e7Sgus knight
88189ad24e7Sgus knight    if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
88289ad24e7Sgus knight	tcc_error("error reading .out file for input");
88389ad24e7Sgus knight
88489ad24e7Sgus knight    // first read the string table
88589ad24e7Sgus knight
88689ad24e7Sgus knight    if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
88789ad24e7Sgus knight	tcc_error("error reading .out file for input");
88889ad24e7Sgus knight
88989ad24e7Sgus knight    if (fread(&str_size, sizeof(int), 1, f) != 1)
89089ad24e7Sgus knight	tcc_error("error reading .out file for input");
89189ad24e7Sgus knight
89289ad24e7Sgus knight
89389ad24e7Sgus knight    Coff_str_table = (char *) tcc_malloc(str_size);
89489ad24e7Sgus knight
89589ad24e7Sgus knight    if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
89689ad24e7Sgus knight	tcc_error("error reading .out file for input");
89789ad24e7Sgus knight
89889ad24e7Sgus knight    // read/process all the symbols
89989ad24e7Sgus knight
90089ad24e7Sgus knight    // seek back to symbols
90189ad24e7Sgus knight
90289ad24e7Sgus knight    if (fseek(f, file_hdr.f_symptr, SEEK_SET))
90389ad24e7Sgus knight	tcc_error("error reading .out file for input");
90489ad24e7Sgus knight
90589ad24e7Sgus knight    for (i = 0; i < file_hdr.f_nsyms; i++) {
90689ad24e7Sgus knight	if (fread(&csym, SYMESZ, 1, f) != 1)
90789ad24e7Sgus knight	    tcc_error("error reading .out file for input");
90889ad24e7Sgus knight
90989ad24e7Sgus knight	if (csym._n._n_n._n_zeroes == 0) {
91089ad24e7Sgus knight	    name = Coff_str_table + csym._n._n_n._n_offset - 4;
91189ad24e7Sgus knight	} else {
91289ad24e7Sgus knight	    name = csym._n._n_name;
91389ad24e7Sgus knight
91489ad24e7Sgus knight	    if (name[7] != 0) {
91589ad24e7Sgus knight		for (k = 0; k < 8; k++)
91689ad24e7Sgus knight		    name2[k] = name[k];
91789ad24e7Sgus knight
91889ad24e7Sgus knight		name2[8] = 0;
91989ad24e7Sgus knight
92089ad24e7Sgus knight		name = name2;
92189ad24e7Sgus knight	    }
92289ad24e7Sgus knight	}
92389ad24e7Sgus knight//              if (strcmp("_DAC_Buffer",name)==0)  // tktk
92489ad24e7Sgus knight//                      name[0]=0;
92589ad24e7Sgus knight
92689ad24e7Sgus knight	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
92789ad24e7Sgus knight	    (csym.n_type == 0x18 && csym.n_sclass == 0x2) ||	// pointer to structure
92889ad24e7Sgus knight	    (csym.n_type == 0x7 && csym.n_sclass == 0x2) ||	// doubles
92989ad24e7Sgus knight	    (csym.n_type == 0x6 && csym.n_sclass == 0x2))	// floats
93089ad24e7Sgus knight	{
93189ad24e7Sgus knight	    // strip off any leading underscore (except for other main routine)
93289ad24e7Sgus knight
93389ad24e7Sgus knight	    if (name[0] == '_' && strcmp(name, "_main") != 0)
93489ad24e7Sgus knight		name++;
93589ad24e7Sgus knight
93689ad24e7Sgus knight	    tcc_add_symbol(s1, name, (void*)(uintptr_t)csym.n_value);
93789ad24e7Sgus knight	}
93889ad24e7Sgus knight	// skip any aux records
93989ad24e7Sgus knight
94089ad24e7Sgus knight	if (csym.n_numaux == 1) {
94189ad24e7Sgus knight	    if (fread(&csym, SYMESZ, 1, f) != 1)
94289ad24e7Sgus knight		tcc_error("error reading .out file for input");
94389ad24e7Sgus knight	    i++;
94489ad24e7Sgus knight	}
94589ad24e7Sgus knight    }
94689ad24e7Sgus knight
94789ad24e7Sgus knight    return 0;
94889ad24e7Sgus knight}
949