gdb-2.8
[platform/upstream/binutils.git] / gdb / coffread.c
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.
6
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.
12
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.
19
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!
22 */
23 \f
24 #include "defs.h"
25 #include "param.h"
26 #ifdef COFF_FORMAT
27 #include "initialize.h"
28 #include "symtab.h"
29
30 #include <a.out.h>
31 #include <stdio.h>
32 #include <obstack.h>
33 #include <sys/param.h>
34 #include <sys/file.h>
35
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 ();
53
54 extern void free_all_symtabs ();
55
56 START_FILE
57
58 /* Name of source file whose symbol data we are now processing.
59    This comes from a symbol named ".file".  */
60
61 static char *last_source_file;
62
63 /* Core address of start and end of text of current source file.
64    This comes from a ".text" symbol where x_nlinno > 0.  */
65
66 static CORE_ADDR cur_src_start_addr;
67 static CORE_ADDR cur_src_end_addr;
68
69 /* End of the text segment of the executable file,
70    as found in the symbol _etext.  */
71
72 static CORE_ADDR end_of_text_addr;
73
74 /* The addresses of the symbol table stream and number of symbols
75    of the object file we are reading (as copied into core).  */
76
77 static FILE *nlist_stream_global;
78 static int nlist_nsyms_global;
79
80 /* The file and text section headers of the symbol file */
81
82 static FILHDR file_hdr;
83 static SCNHDR text_hdr;
84
85 /* The index in the symbol table of the last coff symbol that was processed.  */
86
87 static int symnum;
88
89 /* Vector of types defined so far, indexed by their coff symnum.  */
90
91 static struct typevector *type_vector;
92
93 /* Number of elements allocated for type_vector currently.  */
94
95 static int type_vector_length;
96
97 /* Vector of line number information.  */
98
99 static struct linetable *line_vector;
100
101 /* Index of next entry to go in line_vector_index.  */
102
103 static int line_vector_index;
104
105 /* Last line number recorded in the line vector.  */
106
107 static int prev_line_number;
108
109 /* Number of elements allocated for line_vector currently.  */
110
111 static int line_vector_length;
112
113 /* Chain of typedefs of pointers to empty struct/union types.
114    They are chained thru the SYMBOL_VALUE.  */
115
116 #define HASHSIZE 127
117 static struct symbol *opaque_type_chain[HASHSIZE];
118
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.  */
122
123 struct pending
124 {
125   struct pending *next;
126   struct symbol *symbol;
127 };
128
129 /* Here are the three lists that symbols are put on.  */
130
131 struct pending *file_symbols;   /* static at top level, and types */
132
133 struct pending *global_symbols; /* global functions and variables */
134
135 struct pending *local_symbols;  /* everything local to lexical context */
136
137 /* List of unclosed lexical contexts
138    (that will become blocks, eventually).  */
139
140 struct context_stack
141 {
142   struct context_stack *next;
143   struct pending *locals;
144   struct pending_block *old_blocks;
145   struct symbol *name;
146   CORE_ADDR start_addr;
147   int depth;
148 };
149
150 struct context_stack *context_stack;
151
152 /* Nonzero if within a function (so symbols should be local,
153    if nothing says specifically).  */
154
155 int within_function;
156
157 /* List of blocks already made (lexical contexts already closed).
158    This is used at the end to make the blockvector.  */
159
160 struct pending_block
161 {
162   struct pending_block *next;
163   struct block *block;
164 };
165
166 struct pending_block *pending_blocks;
167
168 extern CORE_ADDR first_object_file_end; /* From blockframe.c */
169
170 /* File name symbols were loaded from.  */
171
172 static char *symfile;
173 \f
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. 
177
178    This can be used for finding the type associated with that index
179    or for associating a new type with the index.  */
180
181 static struct type **
182 coff_lookup_type (index)
183      register int index;
184 {
185   if (index >= type_vector_length)
186     {
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);
193     }
194   return &type_vector->type[index];
195 }
196
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.  */
200
201 static struct type *
202 coff_alloc_type (index)
203      int index;
204 {
205   register struct type **type_addr = coff_lookup_type (index);
206   register struct type *type = *type_addr;
207
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.  */
211   if (type == 0)
212     {
213       type = (struct type *) obstack_alloc (symbol_obstack,
214                                             sizeof (struct type));
215       bzero (type, sizeof (struct type));
216       *type_addr = type;
217     }
218   return type;
219 }
220 \f
221 /* maintain the lists of symbols and blocks */
222
223 /* Add a symbol to one of the lists of symbols.  */
224 static void
225 add_symbol_to_list (symbol, listhead)
226      struct symbol *symbol;
227      struct pending **listhead;
228 {
229   register struct pending *link
230     = (struct pending *) xmalloc (sizeof (struct pending));
231
232   link->next = *listhead;
233   link->symbol = symbol;
234   *listhead = link;
235 }
236
237 /* Take one of the lists of symbols and make a block from it.
238    Put the block on the list of pending blocks.  */
239
240 static void
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;
246 {
247   register struct pending *next, *next1;
248   register struct block *block;
249   register struct pending_block *pblock;
250   struct pending_block *opblock;
251   register int i;
252
253   /* Count the length of the list of symbols.  */
254
255   for (next = *listhead, i = 0; next; next = next->next, i++);
256
257   block = (struct block *)
258             obstack_alloc (symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
259
260   /* Copy the symbols into the block.  */
261
262   BLOCK_NSYMS (block) = i;
263   for (next = *listhead; next; next = next->next)
264     BLOCK_SYM (block, --i) = next->symbol;
265
266   BLOCK_START (block) = start;
267   BLOCK_END (block) = end;
268   BLOCK_SUPERBLOCK (block) = 0; /* Filled in when containing block is made */
269
270   /* Put the block in as the value of the symbol that names it.  */
271
272   if (symbol)
273     {
274       SYMBOL_BLOCK_VALUE (symbol) = block;
275       BLOCK_FUNCTION (block) = symbol;
276     }
277   else
278     BLOCK_FUNCTION (block) = 0;
279
280   /* Now free the links of the list, and empty the list.  */
281
282   for (next = *listhead; next; next = next1)
283     {
284       next1 = next->next;
285       free (next);
286     }
287   *listhead = 0;
288
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.  */
292
293   opblock = 0;
294   for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
295     {
296       if (BLOCK_SUPERBLOCK (pblock->block) == 0)
297         BLOCK_SUPERBLOCK (pblock->block) = block;
298       opblock = pblock;
299     }
300
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.  */
304
305   pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
306   pblock->block = block;
307   if (opblock)
308     {
309       pblock->next = opblock->next;
310       opblock->next = pblock;
311     }
312   else
313     {
314       pblock->next = pending_blocks;
315       pending_blocks = pblock;
316     }
317 }
318
319 static struct blockvector *
320 make_blockvector ()
321 {
322   register struct pending_block *next, *next1;
323   register struct blockvector *blockvector;
324   register int i;
325
326   /* Count the length of the list of blocks.  */
327
328   for (next = pending_blocks, i = 0; next; next = next->next, i++);
329
330   blockvector = (struct blockvector *)
331                   obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
332
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.  */
338
339   BLOCKVECTOR_NBLOCKS (blockvector) = i;
340   for (next = pending_blocks; next; next = next->next)
341     BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
342
343   /* Now free the links of the list, and empty the list.  */
344
345   for (next = pending_blocks; next; next = next1)
346     {
347       next1 = next->next;
348       free (next);
349     }
350   pending_blocks = 0;
351
352   return blockvector;
353 }
354
355 /* Manage the vector of line numbers.  */
356
357 static
358 record_line (line, pc)
359      int line;
360      CORE_ADDR pc;
361 {
362   /* Make sure line vector is big enough.  */
363
364   if (line_vector_index + 2 >= line_vector_length)
365     {
366       line_vector_length *= 2;
367       line_vector = (struct linetable *)
368         xrealloc (line_vector, sizeof (struct linetable)
369                                 + line_vector_length * sizeof (int));
370     }
371
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;
381
382   /* Record the core address of the line.  */
383   line_vector->item[line_vector_index++] = pc;
384 }
385 \f
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.  */
389
390 static void
391 start_symtab ()
392 {
393   file_symbols = 0;
394   global_symbols = 0;
395   context_stack = 0;
396   within_function = 0;
397   last_source_file = 0;
398
399   /* Initialize the source file information for this file.  */
400
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));
406 }
407
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.  */
411
412 static void
413 complete_symtab (name, start_addr, size)
414     char *name;
415     CORE_ADDR start_addr;
416     unsigned int size;
417 {
418   last_source_file = savestring (name, strlen (name));
419   cur_src_start_addr = start_addr;
420   cur_src_end_addr = start_addr + size;
421 }
422
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
426    struct sym