use remote-utils facilities for baud_rate
[deliverable/binutils-gdb.git] / gdb / buildsym.c
CommitLineData
d07734e3 1/* Support routines for building symbol tables in GDB's internal format.
93297ea0
JG
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992
3 Free Software Foundation, Inc.
c0302457
JG
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21/* This module provides subroutines used for creating and adding to
22 the symbol table. These routines are called from various symbol-
d07734e3 23 file-reading routines.
c0302457 24
d07734e3
FF
25 Routines to support specific debugging information formats (stabs,
26 DWARF, etc) belong somewhere else. */
c0302457
JG
27
28#include "defs.h"
d07734e3 29#include "bfd.h"
c0302457
JG
30#include "obstack.h"
31#include "symtab.h"
c0302457 32#include "symfile.h" /* Needed for "struct complaint" */
5e2e79f8 33#include "objfiles.h"
51b80b00 34#include "complaints.h"
c0302457 35#include <string.h>
c0302457
JG
36
37/* Ask buildsym.h to define the vars it normally declares `extern'. */
38#define EXTERN /**/
39#include "buildsym.h" /* Our own declarations */
40#undef EXTERN
41
1ab3bf1b
JG
42static int
43compare_line_numbers PARAMS ((const void *, const void *));
44
45static struct blockvector *
46make_blockvector PARAMS ((struct objfile *));
47
1ab3bf1b 48\f
4137c5fc
JG
49/* Initial sizes of data structures. These are realloc'd larger if needed,
50 and realloc'd down to the size actually used, when completed. */
51
52#define INITIAL_CONTEXT_STACK_SIZE 10
4137c5fc 53#define INITIAL_LINE_VECTOR_LENGTH 1000
d07734e3 54
c0302457
JG
55\f
56/* Complaints about the symbols we have encountered. */
57
58struct complaint innerblock_complaint =
59 {"inner block not inside outer block in %s", 0, 0};
60
76512886
JG
61struct complaint innerblock_anon_complaint =
62 {"inner block not inside outer block", 0, 0};
63
c0302457 64struct complaint blockvector_complaint =
76512886 65 {"block at 0x%x out of order", 0, 0};
c0302457 66
c0302457
JG
67\f
68/* maintain the lists of symbols and blocks */
69
70/* Add a symbol to one of the lists of symbols. */
d07734e3 71
c0302457
JG
72void
73add_symbol_to_list (symbol, listhead)
74 struct symbol *symbol;
75 struct pending **listhead;
76{
d07734e3
FF
77 register struct pending *link;
78
c0302457
JG
79 /* We keep PENDINGSIZE symbols in each link of the list.
80 If we don't have a link with room in it, add a new link. */
d07734e3 81 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
c0302457 82 {
c0302457
JG
83 if (free_pendings)
84 {
85 link = free_pendings;
86 free_pendings = link->next;
87 }
88 else
d07734e3
FF
89 {
90 link = (struct pending *) xmalloc (sizeof (struct pending));
91 }
c0302457
JG
92
93 link->next = *listhead;
94 *listhead = link;
95 link->nsyms = 0;
96 }
97
98 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
99}
100
c438b3af
JK
101/* Find a symbol named NAME on a LIST. NAME need not be '\0'-terminated;
102 LENGTH is the length of the name. */
d07734e3 103
a048c8f5
JG
104struct symbol *
105find_symbol_in_list (list, name, length)
106 struct pending *list;
107 char *name;
108 int length;
109{
110 int j;
d07734e3 111 char *pp;
a048c8f5 112
d07734e3
FF
113 while (list != NULL)
114 {
115 for (j = list->nsyms; --j >= 0; )
116 {
117 pp = SYMBOL_NAME (list->symbol[j]);
118 if (*pp == *name && strncmp (pp, name, length) == 0 &&
119 pp[length] == '\0')
120 {
121 return (list->symbol[j]);
122 }
123 }
124 list = list->next;
a048c8f5 125 }
d07734e3 126 return (NULL);
a048c8f5
JG
127}
128
c0302457 129/* At end of reading syms, or in case of quit,
d07734e3 130 really free as many `struct pending's as we can easily find. */
c0302457
JG
131
132/* ARGSUSED */
133void
134really_free_pendings (foo)
135 int foo;
136{
137 struct pending *next, *next1;
138#if 0
139 struct pending_block *bnext, *bnext1;
140#endif
141
142 for (next = free_pendings; next; next = next1)
143 {
144 next1 = next->next;
84ffdec2 145 free ((PTR)next);
c0302457 146 }
d07734e3 147 free_pendings = NULL;
c0302457
JG
148
149#if 0 /* Now we make the links in the symbol_obstack, so don't free them. */
150 for (bnext = pending_blocks; bnext; bnext = bnext1)
151 {
152 bnext1 = bnext->next;
84ffdec2 153 free ((PTR)bnext);
c0302457
JG
154 }
155#endif
d07734e3 156 pending_blocks = NULL;
c0302457 157
d07734e3 158 for (next = file_symbols; next != NULL; next = next1)
c0302457
JG
159 {
160 next1 = next->next;
84ffdec2 161 free ((PTR)next);
c0302457 162 }
d07734e3 163 file_symbols = NULL;
c0302457 164
d07734e3 165 for (next = global_symbols; next != NULL; next = next1)
c0302457
JG
166 {
167 next1 = next->next;
84ffdec2 168 free ((PTR)next);
c0302457 169 }
d07734e3 170 global_symbols = NULL;
c0302457
JG
171}
172
173/* Take one of the lists of symbols and make a block from it.
174 Keep the order the symbols have in the list (reversed from the input file).
175 Put the block on the list of pending blocks. */
176
177void
1ab3bf1b 178finish_block (symbol, listhead, old_blocks, start, end, objfile)
c0302457
JG
179 struct symbol *symbol;
180 struct pending **listhead;
181 struct pending_block *old_blocks;
182 CORE_ADDR start, end;
1ab3bf1b 183 struct objfile *objfile;
c0302457
JG
184{
185 register struct pending *next, *next1;
186 register struct block *block;
187 register struct pending_block *pblock;
188 struct pending_block *opblock;
189 register int i;
d07734e3 190 register int j;
c0302457
JG
191
192 /* Count the length of the list of symbols. */
193
cd46ffad
FF
194 for (next = *listhead, i = 0;
195 next;
196 i += next->nsyms, next = next->next)
d07734e3
FF
197 {
198 /*EMPTY*/;
199 }
c0302457 200
1ab3bf1b 201 block = (struct block *) obstack_alloc (&objfile -> symbol_obstack,
a048c8f5 202 (sizeof (struct block) + ((i - 1) * sizeof (struct symbol *))));
c0302457
JG
203
204 /* Copy the symbols into the block. */
205
206 BLOCK_NSYMS (block) = i;
207 for (next = *listhead; next; next = next->next)
208 {
c0302457 209 for (j = next->nsyms - 1; j >= 0; j--)
d07734e3
FF
210 {
211 BLOCK_SYM (block, --i) = next->symbol[j];
212 }
c0302457
JG
213 }
214
215 BLOCK_START (block) = start;
216 BLOCK_END (block) = end;
d07734e3
FF
217 /* Superblock filled in when containing block is made */
218 BLOCK_SUPERBLOCK (block) = NULL;
c0302457
JG
219 BLOCK_GCC_COMPILED (block) = processing_gcc_compilation;
220
221 /* Put the block in as the value of the symbol that names it. */
222
223 if (symbol)
224 {
225 SYMBOL_BLOCK_VALUE (symbol) = block;
226 BLOCK_FUNCTION (block) = symbol;
227 }
228 else
d07734e3
FF
229 {
230 BLOCK_FUNCTION (block) = NULL;
231 }
c0302457
JG
232
233 /* Now "free" the links of the list, and empty the list. */
234
235 for (next = *listhead; next; next = next1)
236 {
237 next1 = next->next;
238 next->next = free_pendings;
239 free_pendings = next;
240 }
d07734e3 241 *listhead = NULL;
c0302457
JG
242
243 /* Install this block as the superblock
244 of all blocks made since the start of this scope
245 that don't have superblocks yet. */
246
d07734e3 247 opblock = NULL;
c0302457
JG
248 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
249 {
d07734e3
FF
250 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
251 {
c0302457 252#if 1
d07734e3
FF
253 /* Check to be sure the blocks are nested as we receive them.
254 If the compiler/assembler/linker work, this just burns a small
255 amount of time. */
256 if (BLOCK_START (pblock->block) < BLOCK_START (block) ||
257 BLOCK_END (pblock->block) > BLOCK_END (block))
258 {
259 if (symbol)
260 {
2e4964ad
FF
261 complain (&innerblock_complaint,
262 SYMBOL_SOURCE_NAME (symbol));
d07734e3
FF
263 }
264 else
265 {
51b80b00 266 complain (&innerblock_anon_complaint);
d07734e3
FF
267 }
268 BLOCK_START (pblock->block) = BLOCK_START (block);
269 BLOCK_END (pblock->block) = BLOCK_END (block);
270 }
c0302457 271#endif
d07734e3
FF
272 BLOCK_SUPERBLOCK (pblock->block) = block;
273 }
c0302457
JG
274 opblock = pblock;
275 }
276
277 /* Record this block on the list of all blocks in the file.
278 Put it after opblock, or at the beginning if opblock is 0.
279 This puts the block in the list after all its subblocks. */
280
281 /* Allocate in the symbol_obstack to save time.
282 It wastes a little space. */
283 pblock = (struct pending_block *)
1ab3bf1b 284 obstack_alloc (&objfile -> symbol_obstack,
c0302457
JG
285 sizeof (struct pending_block));
286 pblock->block = block;
287 if (opblock)
288 {
289 pblock->next = opblock->next;
290 opblock->next = pblock;
291 }
292 else
293 {
294 pblock->next = pending_blocks;
295 pending_blocks = pblock;
296 }
297}
298
1ab3bf1b
JG
299static struct blockvector *
300make_blockvector (objfile)
c438b3af 301 struct objfile *objfile;
c0302457
JG
302{
303 register struct pending_block *next;
304 register struct blockvector *blockvector;
305 register int i;
306
307 /* Count the length of the list of blocks. */
308
d07734e3 309 for (next = pending_blocks, i = 0; next; next = next->next, i++) {;}
c0302457
JG
310
311 blockvector = (struct blockvector *)
1ab3bf1b 312 obstack_alloc (&objfile -> symbol_obstack,
c0302457
JG
313 (sizeof (struct blockvector)
314 + (i - 1) * sizeof (struct block *)));
315
316 /* Copy the blocks into the blockvector.
317 This is done in reverse order, which happens to put
318 the blocks into the proper order (ascending starting address).
319 finish_block has hair to insert each block into the list
320 after its subblocks in order to make sure this is true. */
321
322 BLOCKVECTOR_NBLOCKS (blockvector) = i;
d07734e3
FF
323 for (next = pending_blocks; next; next = next->next)
324 {
325 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
326 }
c0302457
JG
327
328#if 0 /* Now we make the links in the obstack, so don't free them. */
329 /* Now free the links of the list, and empty the list. */
330
331 for (next = pending_blocks; next; next = next1)
332 {
333 next1 = next->next;
334 free (next);
335 }
336#endif
d07734e3 337 pending_blocks = NULL;
c0302457
JG
338
339#if 1 /* FIXME, shut this off after a while to speed up symbol reading. */
340 /* Some compilers output blocks in the wrong order, but we depend
341 on their being in the right order so we can binary search.
342 Check the order and moan about it. FIXME. */
343 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
d07734e3
FF
344 {
345 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
346 {
347 if (BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i-1))
348 > BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)))
349 {
350 complain (&blockvector_complaint,
51b80b00 351 BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)));
d07734e3
FF
352 }
353 }
c0302457
JG
354 }
355#endif
356
d07734e3 357 return (blockvector);
c0302457 358}
d07734e3 359
c0302457 360\f
4137c5fc 361/* Start recording information about source code that came from an included
c438b3af
JK
362 (or otherwise merged-in) source file with a different name. NAME is
363 the name of the file (cannot be NULL), DIRNAME is the directory in which
364 it resides (or NULL if not known). */
c0302457
JG
365
366void
4137c5fc
JG
367start_subfile (name, dirname)
368 char *name;
369 char *dirname;
370{
371 register struct subfile *subfile;
372
373 /* See if this subfile is already known as a subfile of the
374 current main source file. */
375
376 for (subfile = subfiles; subfile; subfile = subfile->next)
377 {
2e4964ad 378 if (STREQ (subfile->name, name))
4137c5fc
JG
379 {
380 current_subfile = subfile;
381 return;
382 }
383 }
384
385 /* This subfile is not known. Add an entry for it.
386 Make an entry for this subfile in the list of all subfiles
387 of the current main source file. */
388
389 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
390 subfile->next = subfiles;
391 subfiles = subfile;
392 current_subfile = subfile;
393
394 /* Save its name and compilation directory name */
7c622b41 395 subfile->name = (name == NULL)? NULL : strdup (name);
3416d90b 396 subfile->dirname = (dirname == NULL) ? NULL : strdup (dirname);
4137c5fc
JG
397
398 /* Initialize line-number recording for this subfile. */
d07734e3 399 subfile->line_vector = NULL;
2e4964ad
FF
400
401 /* Default the source language to whatever can be deduced from
402 the filename. If nothing can be deduced (such as for a C/C++
403 include file with a ".h" extension), then inherit whatever
404 language the previous subfile had. This kludgery is necessary
405 because there is no standard way in some object formats to
406 record the source language. Also, when symtabs are allocated
407 we try to deduce a language then as well, but it is too late
408 for us to use that information while reading symbols, since
409 symtabs aren't allocated until after all the symbols have
410 been processed for a given source file. */
411
412 subfile->language = deduce_language_from_filename (subfile->name);
413 if (subfile->language == language_unknown &&
414 subfile->next != NULL)
415 {
416 subfile->language = subfile->next->language;
417 }
56ad756a
JK
418
419 /* cfront output is a C program, so in most ways it looks like a C
420 program. But to demangle we need to set the language to C++. We
421 can distinguish cfront code by the fact that it has #line
422 directives which specify a file name ending in .C.
423
424 So if the filename of this subfile ends in .C, then change the language
425 of any pending subfiles from C to C++. .cc is also accepted, even
426 though I don't think cfront allows it. */
427
428 if (subfile->name)
429 {
430 char *p;
431 struct subfile *s;
432
433 p = strrchr (subfile->name, '.');
434 if (p != NULL
996ccb30
JK
435 && ((p[1] == 'C' && p[2] == '\0')
436 || (p[1] == 'c' && p[2] == 'c' && p[3] == '\0')))
56ad756a
JK
437 for (s = subfiles; s != NULL; s = s->next)
438 if (s->language == language_c)
439 s->language = language_cplus;
440 }
441
442 /* And patch up this file if necessary. */
443 if (subfile->language == language_c
444 && subfile->next != NULL
445 && subfile->next->language == language_cplus)
446 {
447 subfile->language = language_cplus;
448 }
4137c5fc 449}
d07734e3 450
3416d90b
FF
451/* For stabs readers, the first N_SO symbol is assumed to be the source
452 file name, and the subfile struct is initialized using that assumption.
453 If another N_SO symbol is later seen, immediately following the first
454 one, then the first one is assumed to be the directory name and the
455 second one is really the source file name.
456
457 So we have to patch up the subfile struct by moving the old name value to
458 dirname and remembering the new name. Some sanity checking is performed
459 to ensure that the state of the subfile struct is reasonable and that the
460 old name we are assuming to be a directory name actually is (by checking
461 for a trailing '/'). */
462
463void
464patch_subfile_names (subfile, name)
465 struct subfile *subfile;
466 char *name;
467{
468 if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
469 && subfile->name[strlen(subfile->name)-1] == '/')
470 {
471 subfile->dirname = subfile->name;
472 subfile->name = strdup (name);
2e4964ad
FF
473
474 /* Default the source language to whatever can be deduced from
475 the filename. If nothing can be deduced (such as for a C/C++
476 include file with a ".h" extension), then inherit whatever
477 language the previous subfile had. This kludgery is necessary
478 because there is no standard way in some object formats to
479 record the source language. Also, when symtabs are allocated
480 we try to deduce a language then as well, but it is too late
481 for us to use that information while reading symbols, since
482 symtabs aren't allocated until after all the symbols have
483 been processed for a given source file. */
484
485 subfile->language = deduce_language_from_filename (subfile->name);
486 if (subfile->language == language_unknown &&
487 subfile->next != NULL)
488 {
489 subfile->language = subfile->next->language;
490 }
3416d90b
FF
491 }
492}
493
4137c5fc 494\f
a048c8f5
JG
495/* Handle the N_BINCL and N_EINCL symbol types
496 that act like N_SOL for switching source files
497 (different subfiles, as we call them) within one object file,
498 but using a stack rather than in an arbitrary order. */
499
500void
501push_subfile ()
502{
503 register struct subfile_stack *tem
504 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
505
506 tem->next = subfile_stack;
507 subfile_stack = tem;
d07734e3
FF
508 if (current_subfile == NULL || current_subfile->name == NULL)
509 {
510 abort ();
511 }
a048c8f5 512 tem->name = current_subfile->name;
a048c8f5
JG
513}
514
515char *
516pop_subfile ()
517{
518 register char *name;
519 register struct subfile_stack *link = subfile_stack;
520
d07734e3
FF
521 if (link == NULL)
522 {
523 abort ();
524 }
a048c8f5
JG
525 name = link->name;
526 subfile_stack = link->next;
84ffdec2 527 free ((PTR)link);
d07734e3 528 return (name);
a048c8f5 529}
d07734e3 530
a048c8f5 531\f
be7d4f3f
JK
532/* Add a linetable entry for line number LINE and address PC to the line
533 vector for SUBFILE. */
4137c5fc
JG
534
535void
536record_line (subfile, line, pc)
537 register struct subfile *subfile;
c0302457
JG
538 int line;
539 CORE_ADDR pc;
540{
541 struct linetable_entry *e;
542 /* Ignore the dummy line number in libg.o */
543
544 if (line == 0xffff)
d07734e3
FF
545 {
546 return;
547 }
c0302457 548
4137c5fc 549 /* Make sure line vector exists and is big enough. */
d07734e3
FF
550 if (!subfile->line_vector)
551 {
552 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
553 subfile->line_vector = (struct linetable *)
4137c5fc
JG
554 xmalloc (sizeof (struct linetable)
555 + subfile->line_vector_length * sizeof (struct linetable_entry));
d07734e3
FF
556 subfile->line_vector->nitems = 0;
557 }
c0302457 558
4137c5fc 559 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
c0302457 560 {
4137c5fc
JG
561 subfile->line_vector_length *= 2;
562 subfile->line_vector = (struct linetable *)
1ab3bf1b 563 xrealloc ((char *) subfile->line_vector, (sizeof (struct linetable)
d07734e3 564 + subfile->line_vector_length * sizeof (struct linetable_entry)));
c0302457
JG
565 }
566
4137c5fc 567 e = subfile->line_vector->item + subfile->line_vector->nitems++;
c0302457
JG
568 e->line = line; e->pc = pc;
569}
4137c5fc
JG
570
571
572/* Needed in order to sort line tables from IBM xcoff files. Sigh! */
573
1ab3bf1b
JG
574static int
575compare_line_numbers (ln1p, ln2p)
576 const PTR ln1p;
577 const PTR ln2p;
4137c5fc 578{
c438b3af
JK
579 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
580 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
581
582 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
583 Please keep it that way. */
584 if (ln1->pc < ln2->pc)
585 return -1;
586
587 if (ln1->pc > ln2->pc)
588 return 1;
589
590 /* If pc equal, sort by line. I'm not sure whether this is optimum
591 behavior (see comment at struct linetable in symtab.h). */
592 return ln1->line - ln2->line;
4137c5fc 593}
1ab3bf1b 594
c0302457
JG
595\f
596/* Start a new symtab for a new source file.
d07734e3
FF
597 Called, for example, when a stabs symbol of type N_SO is seen, or when
598 a DWARF TAG_compile_unit DIE is seen.
599 It indicates the start of data for one original source file. */
c0302457
JG
600
601void
602start_symtab (name, dirname, start_addr)
603 char *name;
604 char *dirname;
605 CORE_ADDR start_addr;
606{
607
608 last_source_file = name;
609 last_source_start_addr = start_addr;
d07734e3
FF
610 file_symbols = NULL;
611 global_symbols = NULL;
c0302457
JG
612 within_function = 0;
613
a048c8f5
JG
614 /* Context stack is initially empty. Allocate first one with room for
615 10 levels; reuse it forever afterward. */
d07734e3
FF
616 if (context_stack == NULL)
617 {
618 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
619 context_stack = (struct context_stack *)
620 xmalloc (context_stack_size * sizeof (struct context_stack));
621 }
c0302457
JG
622 context_stack_depth = 0;
623
c0302457
JG
624 /* Initialize the list of sub source files with one entry
625 for this file (the top-level source file). */
626
d07734e3
FF
627 subfiles = NULL;
628 current_subfile = NULL;
c0302457
JG
629 start_subfile (name, dirname);
630}
631
632/* Finish the symbol definitions for one main source file,
633 close off all the lexical contexts for that file
634 (creating struct block's for them), then make the struct symtab
635 for that file and put it in the list of all such.
636
7b5d9650 637 END_ADDR is the address of the end of the file's text.
3c02636b
JK
638 SECTION is the section number (in objfile->section_offsets) of
639 the blockvector and linetable.
7b5d9650
FF
640
641 Note that it is possible for end_symtab() to return NULL. In particular,
642 for the DWARF case at least, it will return NULL when it finds a
643 compilation unit that has exactly one DIE, a TAG_compile_unit DIE. This
644 can happen when we link in an object file that was compiled from an empty
645 source file. Returning NULL is probably not the correct thing to do,
646 because then gdb will never know about this empty file (FIXME). */
c0302457
JG
647
648struct symtab *
3c02636b 649end_symtab (end_addr, sort_pending, sort_linevec, objfile, section)
c0302457 650 CORE_ADDR end_addr;
4137c5fc
JG
651 int sort_pending;
652 int sort_linevec;
a048c8f5 653 struct objfile *objfile;
3c02636b 654 int section;
c0302457 655{
fee933f1 656 register struct symtab *symtab = NULL;
c0302457
JG
657 register struct blockvector *blockvector;
658 register struct subfile *subfile;
d07734e3 659 register struct context_stack *cstk;
c0302457
JG
660 struct subfile *nextsub;
661
662 /* Finish the lexical context of the last function in the file;
663 pop the context stack. */
664
665 if (context_stack_depth > 0)
666 {
c0302457
JG
667 context_stack_depth--;
668 cstk = &context_stack[context_stack_depth];
669 /* Make a block for the local symbols within. */
670 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
1ab3bf1b 671 cstk->start_addr, end_addr, objfile);
a048c8f5 672
d07734e3
FF
673 /* Debug: if context stack still has something in it,
674 we are in trouble. */
a048c8f5 675 if (context_stack_depth > 0)
d07734e3
FF
676 {
677 abort ();
678 }
c0302457
JG
679 }
680
9b280a7f 681 /* It is unfortunate that in xcoff, pending blocks might not be ordered
4137c5fc
JG
682 in this stage. Especially, blocks for static functions will show up at
683 the end. We need to sort them, so tools like `find_pc_function' and
684 `find_pc_block' can work reliably. */
d07734e3
FF
685
686 if (sort_pending && pending_blocks)
687 {
688 /* FIXME! Remove this horrid bubble sort and use qsort!!! */
689 int swapped;
690 do
691 {
692 struct pending_block *pb, *pbnext;
693
694 pb = pending_blocks;
695 pbnext = pb->next;
696 swapped = 0;
697
698 while (pbnext)
699 {
700 /* swap blocks if unordered! */
701
702 if (BLOCK_START(pb->block) < BLOCK_START(pbnext->block))
703 {
704 struct block *tmp = pb->block;
705 pb->block = pbnext->block;
706 pbnext->block = tmp;
707 swapped = 1;
708 }
709 pb = pbnext;
710 pbnext = pbnext->next;
711 }
712 } while (swapped);
713 }
4137c5fc 714
c0302457
JG
715 /* Cleanup any undefined types that have been left hanging around
716 (this needs to be done before the finish_blocks so that
d07734e3 717 file_symbols is still good).
c438b3af
JK
718
719 Both cleanup_undefined_types and finish_global_stabs are stabs
720 specific, but harmless for other symbol readers, since on gdb
721 startup or when finished reading stabs, the state is set so these
722 are no-ops. FIXME: Is this handled right in case of QUIT? Can
723 we make this cleaner? */
724
c0302457 725 cleanup_undefined_types ();
d07734e3 726 finish_global_stabs (objfile);
c0302457 727
d07734e3
FF
728 if (pending_blocks == NULL
729 && file_symbols == NULL
730 && global_symbols == NULL)
731 {
732 /* Ignore symtabs that have no functions with real debugging info */
733 blockvector = NULL;
734 }
735 else
736 {
737 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the blockvector. */
738 finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr,
739 objfile);
740 finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr,
741 objfile);
742 blockvector = make_blockvector (objfile);
743 }
c0302457 744
818de002 745#ifdef PROCESS_LINENUMBER_HOOK
9b280a7f 746 PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */
818de002
PB
747#endif
748
c0302457
JG
749 /* Now create the symtab objects proper, one for each subfile. */
750 /* (The main file is the last one on the chain.) */
751
752 for (subfile = subfiles; subfile; subfile = nextsub)
753 {
fee933f1 754 int linetablesize = 0;
a048c8f5
JG
755 /* If we have blocks of symbols, make a symtab.
756 Otherwise, just ignore this file and any line number info in it. */
d07734e3
FF
757 symtab = NULL;
758 if (blockvector)
759 {
760 if (subfile->line_vector)
761 {
d07734e3
FF
762 linetablesize = sizeof (struct linetable) +
763 subfile->line_vector->nitems * sizeof (struct linetable_entry);
c438b3af
JK
764#if 0
765 /* I think this is artifact from before it went on the obstack.
766 I doubt we'll need the memory between now and when we
767 free it later in this function. */
768 /* First, shrink the linetable to make more memory. */
d07734e3
FF
769 subfile->line_vector = (struct linetable *)
770 xrealloc ((char *) subfile->line_vector, linetablesize);
c438b3af
JK
771#endif
772 /* If sort_linevec is false, we might want just check to make
773 sure they are sorted and complain() if not, as a way of
774 tracking down compilers/symbol readers which don't get
775 them sorted right. */
d07734e3
FF
776
777 if (sort_linevec)
778 qsort (subfile->line_vector->item,
779 subfile->line_vector->nitems,
780 sizeof (struct linetable_entry), compare_line_numbers);
781 }
782
783 /* Now, allocate a symbol table. */
784 symtab = allocate_symtab (subfile->name, objfile);
4137c5fc 785
d07734e3
FF
786 /* Fill in its components. */
787 symtab->blockvector = blockvector;
788 if (subfile->line_vector)
789 {
790 /* Reallocate the line table on the symbol obstack */
791 symtab->linetable = (struct linetable *)
792 obstack_alloc (&objfile -> symbol_obstack, linetablesize);
793 memcpy (symtab->linetable, subfile->line_vector, linetablesize);
794 }
795 else
796 {
797 symtab->linetable = NULL;
798 }
3c02636b 799 symtab->block_line_section = section;
8275e802
FF
800 if (subfile->dirname)
801 {
802 /* Reallocate the dirname on the symbol obstack */
803 symtab->dirname = (char *)
804 obstack_alloc (&objfile -> symbol_obstack,
805 strlen (subfile -> dirname) + 1);
806 strcpy (symtab->dirname, subfile->dirname);
807 }
808 else
809 {
810 symtab->dirname = NULL;
811 }
d07734e3
FF
812 symtab->free_code = free_linetable;
813 symtab->free_ptr = NULL;
2b5a8d9c 814
2e4964ad
FF
815 /* Use whatever language we have been using for this subfile,
816 not the one that was deduced in allocate_symtab from the
817 filename. We already did our own deducing when we created
818 the subfile, and we may have altered our opinion of what
819 language it is from things we found in the symbols. */
820 symtab->language = subfile->language;
821
3c02636b
JK
822 /* All symtabs for the main file and the subfiles share a
823 blockvector, so we need to clear primary for everything but
824 the main file. */
2b5a8d9c 825
3c02636b 826 symtab->primary = 0;
d07734e3 827 }
3416d90b
FF
828 if (subfile->name != NULL)
829 {
830 free ((PTR) subfile->name);
831 }
832 if (subfile->dirname != NULL)
833 {
834 free ((PTR) subfile->dirname);
835 }
836 if (subfile->line_vector != NULL)
d07734e3 837 {
3416d90b 838 free ((PTR) subfile->line_vector);
d07734e3 839 }
4137c5fc 840
c0302457 841 nextsub = subfile->next;
84ffdec2 842 free ((PTR)subfile);
c0302457
JG
843 }
844
3c02636b 845 /* Set this for the main source file. */
1eeba686 846 if (symtab)
d07734e3 847 {
3c02636b 848 symtab->primary = 1;
d07734e3 849 }
2b5a8d9c 850
d07734e3
FF
851 last_source_file = NULL;
852 current_subfile = NULL;
4137c5fc 853
d07734e3 854 return (symtab);
c0302457 855}
a048c8f5
JG
856
857
858/* Push a context block. Args are an identifying nesting level (checkable
859 when you pop it), and the starting PC address of this context. */
860
861struct context_stack *
862push_context (desc, valu)
863 int desc;
864 CORE_ADDR valu;
865{
866 register struct context_stack *new;
867
868 if (context_stack_depth == context_stack_size)
869 {
870 context_stack_size *= 2;
871 context_stack = (struct context_stack *)
1ab3bf1b
JG
872 xrealloc ((char *) context_stack,
873 (context_stack_size * sizeof (struct context_stack)));
a048c8f5
JG
874 }
875
876 new = &context_stack[context_stack_depth++];
877 new->depth = desc;
878 new->locals = local_symbols;
879 new->old_blocks = pending_blocks;
880 new->start_addr = valu;
d07734e3 881 new->name = NULL;
a048c8f5 882
d07734e3 883 local_symbols = NULL;
a048c8f5 884
d07734e3 885 return (new);
a048c8f5 886}
d07734e3 887
48f075eb
SS
888\f
889/* Compute a small integer hash code for the given name. */
890
891int
892hashname (name)
893 char *name;
894{
895 register char *p = name;
896 register int total = p[0];
897 register int c;
898
899 c = p[1];
900 total += c << 2;
901 if (c)
902 {
903 c = p[2];
904 total += c << 4;
905 if (c)
906 {
907 total += p[3] << 6;
908 }
909 }
910
911 /* Ensure result is positive. */
912 if (total < 0)
913 {
914 total += (1000 << 6);
915 }
916 return (total % HASHSIZE);
917}
918
c0302457
JG
919\f
920/* Initialize anything that needs initializing when starting to read
921 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
922 to a psymtab. */
923
924void
925buildsym_init ()
926{
d07734e3
FF
927 free_pendings = NULL;
928 file_symbols = NULL;
929 global_symbols = NULL;
930 pending_blocks = NULL;
c0302457
JG
931}
932
933/* Initialize anything that needs initializing when a completely new
934 symbol file is specified (not just adding some symbols from another
935 file, e.g. a shared library). */
936
937void
938buildsym_new_init ()
939{
c0302457
JG
940 buildsym_init ();
941}
942
d07734e3 943/* Initializer for this module */
095db7ce 944
c0302457
JG
945void
946_initialize_buildsym ()
947{
c0302457 948}
This page took 0.198871 seconds and 4 git commands to generate.