1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Design and support routines derived from dbxread.c, and UMAX COFF
3 specific routines written 9/1/87 by David D. Johnson, Brown University.
4 Revised 11/27/87 ddj@cs.brown.edu
5 Copyright (C) 1987, 1988 Free Software Foundation, Inc.
7 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
8 WARRANTY. No author or distributor accepts responsibility to anyone
9 for the consequences of using it or for whether it serves any
10 particular purpose or works at all, unless he says so in writing.
11 Refer to the GDB General Public License for full details.
13 Everyone is granted permission to copy, modify and redistribute GDB,
14 but only under the conditions described in the GDB General Public
15 License. A copy of this license is supposed to have been given to you
16 along with GDB so you can know your rights and responsibilities. It
17 should be in a file named COPYING. Among other things, the copyright
18 notice and this notice must be preserved on all copies.
20 In other words, go ahead and share GDB, but don't try to stop
21 anyone else from sharing it farther. Help stamp out software hoarding!
27 #include "initialize.h"
33 #include <sys/param.h>
36 static void add_symbol_to_list ();
37 static void read_coff_symtab ();
38 static void patch_opaque_types ();
39 static struct type *decode_function_type ();
40 static struct type *decode_type ();
41 static struct type *decode_base_type ();
42 static struct type *read_enum_type ();
43 static struct type *read_struct_type ();
44 static void finish_block ();
45 static struct blockvector *make_blockvector ();
46 static struct symbol *process_coff_symbol ();
47 static int init_stringtab ();
48 static void free_stringtab ();
49 static char *getfilename ();
50 static char *getsymname ();
51 static int init_lineno ();
52 static void enter_linenos ();
54 extern void free_all_symtabs ();
58 /* Name of source file whose symbol data we are now processing.
59 This comes from a symbol named ".file". */
61 static char *last_source_file;
63 /* Core address of start and end of text of current source file.
64 This comes from a ".text" symbol where x_nlinno > 0. */
66 static CORE_ADDR cur_src_start_addr;
67 static CORE_ADDR cur_src_end_addr;
69 /* End of the text segment of the executable file,
70 as found in the symbol _etext. */
72 static CORE_ADDR end_of_text_addr;
74 /* The addresses of the symbol table stream and number of symbols
75 of the object file we are reading (as copied into core). */
77 static FILE *nlist_stream_global;
78 static int nlist_nsyms_global;
80 /* The file and text section headers of the symbol file */
82 static FILHDR file_hdr;
83 static SCNHDR text_hdr;
85 /* The index in the symbol table of the last coff symbol that was processed. */
89 /* Vector of types defined so far, indexed by their coff symnum. */
91 static struct typevector *type_vector;
93 /* Number of elements allocated for type_vector currently. */
95 static int type_vector_length;
97 /* Vector of line number information. */
99 static struct linetable *line_vector;
101 /* Index of next entry to go in line_vector_index. */
103 static int line_vector_index;
105 /* Last line number recorded in the line vector. */
107 static int prev_line_number;
109 /* Number of elements allocated for line_vector currently. */
111 static int line_vector_length;
113 /* Chain of typedefs of pointers to empty struct/union types.
114 They are chained thru the SYMBOL_VALUE. */
117 static struct symbol *opaque_type_chain[HASHSIZE];
119 /* Record the symbols defined for each context in a list.
120 We don't create a struct block for the context until we
121 know how long to make it. */
125 struct pending *next;
126 struct symbol *symbol;
129 /* Here are the three lists that symbols are put on. */
131 struct pending *file_symbols; /* static at top level, and types */
133 struct pending *global_symbols; /* global functions and variables */
135 struct pending *local_symbols; /* everything local to lexical context */
137 /* List of unclosed lexical contexts
138 (that will become blocks, eventually). */
142 struct context_stack *next;
143 struct pending *locals;
144 struct pending_block *old_blocks;
146 CORE_ADDR start_addr;
150 struct context_stack *context_stack;
152 /* Nonzero if within a function (so symbols should be local,
153 if nothing says specifically). */
157 /* List of blocks already made (lexical contexts already closed).
158 This is used at the end to make the blockvector. */
162 struct pending_block *next;
166 struct pending_block *pending_blocks;
168 extern CORE_ADDR first_object_file_end; /* From blockframe.c */
170 /* File name symbols were loaded from. */
172 static char *symfile;
174 /* Look up a coff type-number index. Return the address of the slot
175 where the type for that index is stored.
176 The type-number is in INDEX.
178 This can be used for finding the type associated with that index
179 or for associating a new type with the index. */
181 static struct type **
182 coff_lookup_type (index)
185 if (index >= type_vector_length)
187 type_vector_length *= 2;
188 type_vector = (struct typevector *)
189 xrealloc (type_vector, sizeof (struct typevector)
190 + type_vector_length * sizeof (struct type *));
191 bzero (&type_vector->type[type_vector_length / 2],
192 type_vector_length * sizeof (struct type *) / 2);
194 return &type_vector->type[index];
197 /* Make sure there is a type allocated for type number index
198 and return the type object.
199 This can create an empty (zeroed) type object. */
202 coff_alloc_type (index)
205 register struct type **type_addr = coff_lookup_type (index);
206 register struct type *type = *type_addr;
208 /* If we are referring to a type not known at all yet,
209 allocate an empty type for it.
210 We will fill it in later if we find out how. */
213 type = (struct type *) obstack_alloc (symbol_obstack,
214 sizeof (struct type));
215 bzero (type, sizeof (struct type));
221 /* maintain the lists of symbols and blocks */
223 /* Add a symbol to one of the lists of symbols. */
225 add_symbol_to_list (symbol, listhead)
226 struct symbol *symbol;
227 struct pending **listhead;
229 register struct pending *link
230 = (struct pending *) xmalloc (sizeof (struct pending));
232 link->next = *listhead;
233 link->symbol = symbol;
237 /* Take one of the lists of symbols and make a block from it.
238 Put the block on the list of pending blocks. */
241 finish_block (symbol, listhead, old_blocks, start, end)
242 struct symbol *symbol;
243 struct pending **listhead;
244 struct pending_block *old_blocks;
245 CORE_ADDR start, end;
247 register struct pending *next, *next1;
248 register struct block *block;
249 register struct pending_block *pblock;
250 struct pending_block *opblock;
253 /* Count the length of the list of symbols. */
255 for (next = *listhead, i = 0; next; next = next->next, i++);
257 block = (struct block *)
258 obstack_alloc (symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
260 /* Copy the symbols into the block. */
262 BLOCK_NSYMS (block) = i;
263 for (next = *listhead; next; next = next->next)
264 BLOCK_SYM (block, --i) = next->symbol;
266 BLOCK_START (block) = start;
267 BLOCK_END (block) = end;
268 BLOCK_SUPERBLOCK (block) = 0; /* Filled in when containing block is made */
270 /* Put the block in as the value of the symbol that names it. */
274 SYMBOL_BLOCK_VALUE (symbol) = block;
275 BLOCK_FUNCTION (block) = symbol;
278 BLOCK_FUNCTION (block) = 0;
280 /* Now free the links of the list, and empty the list. */
282 for (next = *listhead; next; next = next1)
289 /* Install this block as the superblock
290 of all blocks made since the start of this scope
291 that don't have superblocks yet. */
294 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
296 if (BLOCK_SUPERBLOCK (pblock->block) == 0)
297 BLOCK_SUPERBLOCK (pblock->block) = block;
301 /* Record this block on the list of all blocks in the file.
302 Put it after opblock, or at the beginning if opblock is 0.
303 This puts the block in the list after all its subblocks. */
305 pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
306 pblock->block = block;
309 pblock->next = opblock->next;
310 opblock->next = pblock;
314 pblock->next = pending_blocks;
315 pending_blocks = pblock;
319 static struct blockvector *
322 register struct pending_block *next, *next1;
323 register struct blockvector *blockvector;
326 /* Count the length of the list of blocks. */
328 for (next = pending_blocks, i = 0; next; next = next->next, i++);
330 blockvector = (struct blockvector *)
331 obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
333 /* Copy the blocks into the blockvector.
334 This is done in reverse order, which happens to put
335 the blocks into the proper order (ascending starting address).
336 finish_block has hair to insert each block into the list
337 after its subblocks in order to make sure this is true. */
339 BLOCKVECTOR_NBLOCKS (blockvector) = i;
340 for (next = pending_blocks; next; next = next->next)
341 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
343 /* Now free the links of the list, and empty the list. */
345 for (next = pending_blocks; next; next = next1)
355 /* Manage the vector of line numbers. */
358 record_line (line, pc)
362 /* Make sure line vector is big enough. */
364 if (line_vector_index + 2 >= line_vector_length)
366 line_vector_length *= 2;
367 line_vector = (struct linetable *)
368 xrealloc (line_vector, sizeof (struct linetable)
369 + line_vector_length * sizeof (int));
372 /* If this line is not continguous with previous one recorded,
373 all lines between subsequent line and current one are same pc.
374 Add one item to line vector, and if more than one line skipped,
375 record a line-number entry for it. */
376 if (prev_line_number > 0 && line != prev_line_number + 1)
377 line_vector->item[line_vector_index++] = pc;
378 if (prev_line_number < 0 || line > prev_line_number + 2)
379 line_vector->item[line_vector_index++] = - line;
380 prev_line_number = line;
382 /* Record the core address of the line. */
383 line_vector->item[line_vector_index++] = pc;
386 /* Start a new symtab for a new source file.
387 This is called when a COFF ".file" symbol is seen;
388 it indicates the start of data for one original source file. */
397 last_source_file = 0;
399 /* Initialize the source file information for this file. */
401 line_vector_index = 0;
402 line_vector_length = 1000;
403 prev_line_number = -2; /* Force first line number to be explicit */
404 line_vector = (struct linetable *)
405 xmalloc (sizeof (struct linetable) + line_vector_length * sizeof (int));
408 /* Save the vital information for use when closing off the current file.
409 NAME is the file name the symbols came from, START_ADDR is the first
410 text address for the file, and SIZE is the number of bytes of text. */
413 complete_symtab (name, start_addr, size)
415 CORE_ADDR start_addr;
418 last_source_file = savestring (name, strlen (name));
419 cur_src_start_addr = start_addr;
420 cur_src_end_addr = start_addr + size;
423 /* Finish the symbol definitions for one main source file,
424 close off all the lexical contexts for that file
425 (creating struct block's for them), then make the