1 /* stab-ecoff.c
2 *
3 * $Id$
4 *
5 * Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
6 * Copyright 2002, 2003 Sam Hocevar <sam@hocevar.net>, Paris
7 *
8 * This software was derived from Elk 1.2, which was Copyright 1987, 1988,
9 * 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
10 * by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
11 * between TELES and Nixdorf Microprocessor Engineering, Berlin).
12 *
13 * Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
14 * owners or individual owners of copyright in this software, grant to any
15 * person or company a worldwide, royalty free, license to
16 *
17 * i) copy this software,
18 * ii) prepare derivative works based on this software,
19 * iii) distribute copies of this software or derivative works,
20 * iv) perform this software, or
21 * v) display this software,
22 *
23 * provided that this notice is not removed and that neither Oliver Laumann
24 * nor Teles nor Nixdorf are deemed to have made any representations as to
25 * the suitability of this software for any purpose nor are held responsible
26 * for any defects of this software.
27 *
28 * THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
29 */
30
31 /* On the SGI, <a.out.h> includes a file that defines a variable named
32 * auxtemp. This causes the linker to complain about this variable
33 * being multiply defined, because <a.out.h> was already included by
34 * load.vanilla.c.
35 */
36 #define _auxtemp Auxtemp
37
38 #include AOUT_H
39
40 extern void Free_Symbols (SYMTAB *);
41
Snarf_Symbols(FILE * fp)42 SYMTAB *Snarf_Symbols (FILE *fp) {
43 long fdi; /* a counter for the file desc table */
44 FDR *file_desc; /* pointer to the filedesc table */
45 struct filehdr file_hdr; /* pointer to the file header */
46 char *strbase;
47 HDRR sym_hdr; /* pointer to symbolic header */
48 long symi; /* a counter for the local symbol table */
49 SYMR *symbol; /* pointer to symbol table */
50
51 SYMTAB *tab;
52 char *p;
53 SYM *sp, **nextp;
54
55 Alloca_Begin;
56
57 /* Read file header and symbolic header
58 */
59 (void)rewind (fp);
60 if (fread ((char *)&file_hdr, sizeof (file_hdr), 1, fp) == 0) {
61 fclose (fp);
62 Primitive_Error ("cannot read a.out file header");
63 }
64 (void)fseek (fp, file_hdr.f_symptr, SEEK_SET);
65 if (fread ((char *)&sym_hdr, sizeof (sym_hdr), 1, fp) == 0) {
66 fclose (fp);
67 Primitive_Error ("cannot read a.out symbolic header");
68 }
69
70 tab = (SYMTAB *)Safe_Malloc (sizeof (SYMTAB));
71 tab->first = 0;
72 tab->strings = 0;
73 nextp = &tab->first;
74
75 /* Read symbol table
76 */
77 Alloca (symbol, SYMR*, sym_hdr.isymMax * sizeof (SYMR));
78 (void)fseek (fp, sym_hdr.cbSymOffset, SEEK_SET);
79 if (fread ((char *)symbol, sizeof (SYMR), sym_hdr.isymMax, fp) == 0) {
80 symerr:
81 fclose (fp);
82 Free_Symbols (tab);
83 Primitive_Error ("cannot read symbol/string/fd table");
84 }
85
86 /* Read string table
87 */
88 tab->strings = Safe_Malloc (sym_hdr.issMax);
89 (void)fseek (fp, sym_hdr.cbSsOffset, SEEK_SET);
90 if (fread (tab->strings, sym_hdr.issMax, 1, fp) == 0)
91 goto symerr;
92
93 /* Read file descriptor table
94 */
95 Alloca (file_desc, FDR*, sym_hdr.ifdMax * sizeof (FDR));
96 (void)fseek (fp, sym_hdr.cbFdOffset, SEEK_SET);
97 if (fread ((char *)file_desc, sizeof (FDR), sym_hdr.ifdMax, fp) == 0)
98 goto symerr;
99
100 /* For each file in the file descriptor table do:
101 */
102 for (fdi = 0; fdi < sym_hdr.ifdMax; fdi++) {
103 strbase = tab->strings + file_desc[fdi].issBase;
104 for (symi = file_desc[fdi].isymBase;
105 symi < file_desc[fdi].csym + file_desc[fdi].isymBase;
106 symi++) {
107 if (symbol[symi].st == stProc && symbol[symi].sc == scText) {
108 p = symbol[symi].iss + strbase;
109
110 /* Allocate and initialize node in the symbol table list;
111 * link node into list
112 */
113 sp = (SYM *)Safe_Malloc (sizeof (SYM));
114 sp->name = Safe_Malloc (strlen (p) + 1);
115 strcpy (sp->name, p);
116 sp->value = symbol[symi].value;
117 *nextp = sp;
118 nextp = &sp->next;
119 *nextp = 0;
120 }
121 }
122 }
123 Alloca_End;
124 return tab;
125 }
126
Open_File_And_Snarf_Symbols(char * name)127 SYMTAB *Open_File_And_Snarf_Symbols (char *name) {
128 FILE *fp;
129 SYMTAB *tab;
130
131 if ((fp = fopen (name, "r")) == NULL)
132 Primitive_Error ("can't open a.out file");
133 tab = Snarf_Symbols (fp);
134 (void)fclose (fp);
135 return tab;
136 }
137