Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support routines for building symbol tables in GDB's internal format. |
197e01b6 | 2 | Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, |
9b254dd1 | 3 | 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008 |
25caa7a8 | 4 | Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 11 | (at your option) any later version. |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b | 18 | You should have received a copy of the GNU General Public License |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
20 | |
21 | /* This module provides subroutines used for creating and adding to | |
22 | the symbol table. These routines are called from various symbol- | |
23 | file-reading routines. | |
24 | ||
25 | Routines to support specific debugging information formats (stabs, | |
26 | DWARF, etc) belong somewhere else. */ | |
27 | ||
28 | #include "defs.h" | |
29 | #include "bfd.h" | |
04ea0df1 | 30 | #include "gdb_obstack.h" |
c906108c | 31 | #include "symtab.h" |
72367fb4 | 32 | #include "symfile.h" |
c906108c SS |
33 | #include "objfiles.h" |
34 | #include "gdbtypes.h" | |
0c5e171a | 35 | #include "gdb_assert.h" |
c906108c SS |
36 | #include "complaints.h" |
37 | #include "gdb_string.h" | |
91b9ff21 | 38 | #include "expression.h" /* For "enum exp_opcode" used by... */ |
357e46e7 | 39 | #include "bcache.h" |
d5166ae1 | 40 | #include "filenames.h" /* For DOSish file names */ |
99d9066e | 41 | #include "macrotab.h" |
261397f8 | 42 | #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */ |
fe898f56 | 43 | #include "block.h" |
9219021c | 44 | #include "cp-support.h" |
de4f826b | 45 | #include "dictionary.h" |
801e3a5b | 46 | #include "addrmap.h" |
9219021c | 47 | |
c906108c | 48 | /* Ask buildsym.h to define the vars it normally declares `extern'. */ |
c5aa993b JM |
49 | #define EXTERN |
50 | /**/ | |
c906108c SS |
51 | #include "buildsym.h" /* Our own declarations */ |
52 | #undef EXTERN | |
53 | ||
54 | /* For cleanup_undefined_types and finish_global_stabs (somewhat | |
55 | questionable--see comment where we call them). */ | |
56 | ||
57 | #include "stabsread.h" | |
58 | ||
94d09e04 DE |
59 | /* List of subfiles. */ |
60 | ||
61 | static struct subfile *subfiles; | |
62 | ||
c906108c SS |
63 | /* List of free `struct pending' structures for reuse. */ |
64 | ||
65 | static struct pending *free_pendings; | |
66 | ||
67 | /* Non-zero if symtab has line number info. This prevents an | |
68 | otherwise empty symtab from being tossed. */ | |
69 | ||
70 | static int have_line_numbers; | |
801e3a5b JB |
71 | |
72 | /* The mutable address map for the compilation unit whose symbols | |
73 | we're currently reading. The symtabs' shared blockvector will | |
74 | point to a fixed copy of this. */ | |
75 | static struct addrmap *pending_addrmap; | |
76 | ||
77 | /* The obstack on which we allocate pending_addrmap. | |
78 | If pending_addrmap is NULL, this is uninitialized; otherwise, it is | |
79 | initialized (and holds pending_addrmap). */ | |
80 | static struct obstack pending_addrmap_obstack; | |
81 | ||
82 | /* Non-zero if we recorded any ranges in the addrmap that are | |
83 | different from those in the blockvector already. We set this to | |
84 | zero when we start processing a symfile, and if it's still zero at | |
85 | the end, then we just toss the addrmap. */ | |
86 | static int pending_addrmap_interesting; | |
87 | ||
c906108c SS |
88 | \f |
89 | static int compare_line_numbers (const void *ln1p, const void *ln2p); | |
90 | \f | |
91 | ||
92 | /* Initial sizes of data structures. These are realloc'd larger if | |
93 | needed, and realloc'd down to the size actually used, when | |
94 | completed. */ | |
95 | ||
96 | #define INITIAL_CONTEXT_STACK_SIZE 10 | |
97 | #define INITIAL_LINE_VECTOR_LENGTH 1000 | |
98 | \f | |
99 | ||
c906108c SS |
100 | /* maintain the lists of symbols and blocks */ |
101 | ||
59527da0 JB |
102 | /* Add a pending list to free_pendings. */ |
103 | void | |
104 | add_free_pendings (struct pending *list) | |
105 | { | |
52f0bd74 | 106 | struct pending *link = list; |
59527da0 JB |
107 | |
108 | if (list) | |
109 | { | |
110 | while (link->next) link = link->next; | |
111 | link->next = free_pendings; | |
112 | free_pendings = list; | |
113 | } | |
114 | } | |
115 | ||
9219021c DC |
116 | /* Add a symbol to one of the lists of symbols. While we're at it, if |
117 | we're in the C++ case and don't have full namespace debugging info, | |
118 | check to see if it references an anonymous namespace; if so, add an | |
119 | appropriate using directive. */ | |
c906108c SS |
120 | |
121 | void | |
122 | add_symbol_to_list (struct symbol *symbol, struct pending **listhead) | |
123 | { | |
52f0bd74 | 124 | struct pending *link; |
c906108c SS |
125 | |
126 | /* If this is an alias for another symbol, don't add it. */ | |
127 | if (symbol->ginfo.name && symbol->ginfo.name[0] == '#') | |
128 | return; | |
129 | ||
130 | /* We keep PENDINGSIZE symbols in each link of the list. If we | |
131 | don't have a link with room in it, add a new link. */ | |
132 | if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE) | |
133 | { | |
134 | if (free_pendings) | |
135 | { | |
136 | link = free_pendings; | |
137 | free_pendings = link->next; | |
138 | } | |
139 | else | |
140 | { | |
141 | link = (struct pending *) xmalloc (sizeof (struct pending)); | |
142 | } | |
143 | ||
144 | link->next = *listhead; | |
145 | *listhead = link; | |
146 | link->nsyms = 0; | |
147 | } | |
148 | ||
149 | (*listhead)->symbol[(*listhead)->nsyms++] = symbol; | |
9219021c DC |
150 | |
151 | /* Check to see if we might need to look for a mention of anonymous | |
152 | namespaces. */ | |
153 | ||
154 | if (SYMBOL_LANGUAGE (symbol) == language_cplus) | |
155 | cp_scan_for_anonymous_namespaces (symbol); | |
c906108c SS |
156 | } |
157 | ||
158 | /* Find a symbol named NAME on a LIST. NAME need not be | |
159 | '\0'-terminated; LENGTH is the length of the name. */ | |
160 | ||
161 | struct symbol * | |
162 | find_symbol_in_list (struct pending *list, char *name, int length) | |
163 | { | |
164 | int j; | |
165 | char *pp; | |
166 | ||
167 | while (list != NULL) | |
168 | { | |
169 | for (j = list->nsyms; --j >= 0;) | |
170 | { | |
22abf04a | 171 | pp = DEPRECATED_SYMBOL_NAME (list->symbol[j]); |
c906108c SS |
172 | if (*pp == *name && strncmp (pp, name, length) == 0 && |
173 | pp[length] == '\0') | |
174 | { | |
175 | return (list->symbol[j]); | |
176 | } | |
177 | } | |
178 | list = list->next; | |
179 | } | |
180 | return (NULL); | |
181 | } | |
182 | ||
183 | /* At end of reading syms, or in case of quit, really free as many | |
184 | `struct pending's as we can easily find. */ | |
185 | ||
c906108c | 186 | void |
bde58177 | 187 | really_free_pendings (void *dummy) |
c906108c SS |
188 | { |
189 | struct pending *next, *next1; | |
190 | ||
191 | for (next = free_pendings; next; next = next1) | |
192 | { | |
193 | next1 = next->next; | |
b8c9b27d | 194 | xfree ((void *) next); |
c906108c SS |
195 | } |
196 | free_pendings = NULL; | |
197 | ||
198 | free_pending_blocks (); | |
199 | ||
200 | for (next = file_symbols; next != NULL; next = next1) | |
201 | { | |
202 | next1 = next->next; | |
b8c9b27d | 203 | xfree ((void *) next); |
c906108c SS |
204 | } |
205 | file_symbols = NULL; | |
206 | ||
207 | for (next = global_symbols; next != NULL; next = next1) | |
208 | { | |
209 | next1 = next->next; | |
b8c9b27d | 210 | xfree ((void *) next); |
c906108c SS |
211 | } |
212 | global_symbols = NULL; | |
99d9066e JB |
213 | |
214 | if (pending_macros) | |
215 | free_macro_table (pending_macros); | |
801e3a5b JB |
216 | |
217 | if (pending_addrmap) | |
218 | { | |
219 | obstack_free (&pending_addrmap_obstack, NULL); | |
220 | pending_addrmap = NULL; | |
221 | } | |
c906108c SS |
222 | } |
223 | ||
224 | /* This function is called to discard any pending blocks. */ | |
225 | ||
226 | void | |
227 | free_pending_blocks (void) | |
228 | { | |
89ba75b1 JB |
229 | /* The links are made in the objfile_obstack, so we only need to |
230 | reset PENDING_BLOCKS. */ | |
c906108c SS |
231 | pending_blocks = NULL; |
232 | } | |
233 | ||
234 | /* Take one of the lists of symbols and make a block from it. Keep | |
235 | the order the symbols have in the list (reversed from the input | |
236 | file). Put the block on the list of pending blocks. */ | |
237 | ||
801e3a5b | 238 | struct block * |
c906108c SS |
239 | finish_block (struct symbol *symbol, struct pending **listhead, |
240 | struct pending_block *old_blocks, | |
241 | CORE_ADDR start, CORE_ADDR end, | |
242 | struct objfile *objfile) | |
243 | { | |
52f0bd74 AC |
244 | struct pending *next, *next1; |
245 | struct block *block; | |
246 | struct pending_block *pblock; | |
c906108c | 247 | struct pending_block *opblock; |
c906108c | 248 | |
4a146b47 | 249 | block = allocate_block (&objfile->objfile_obstack); |
c906108c | 250 | |
261397f8 DJ |
251 | if (symbol) |
252 | { | |
4a146b47 | 253 | BLOCK_DICT (block) = dict_create_linear (&objfile->objfile_obstack, |
de4f826b | 254 | *listhead); |
261397f8 DJ |
255 | } |
256 | else | |
c906108c | 257 | { |
4a146b47 | 258 | BLOCK_DICT (block) = dict_create_hashed (&objfile->objfile_obstack, |
de4f826b | 259 | *listhead); |
c906108c SS |
260 | } |
261 | ||
262 | BLOCK_START (block) = start; | |
263 | BLOCK_END (block) = end; | |
264 | /* Superblock filled in when containing block is made */ | |
265 | BLOCK_SUPERBLOCK (block) = NULL; | |
9219021c | 266 | BLOCK_NAMESPACE (block) = NULL; |
c906108c | 267 | |
c906108c SS |
268 | /* Put the block in as the value of the symbol that names it. */ |
269 | ||
270 | if (symbol) | |
271 | { | |
272 | struct type *ftype = SYMBOL_TYPE (symbol); | |
de4f826b | 273 | struct dict_iterator iter; |
c906108c SS |
274 | SYMBOL_BLOCK_VALUE (symbol) = block; |
275 | BLOCK_FUNCTION (block) = symbol; | |
276 | ||
277 | if (TYPE_NFIELDS (ftype) <= 0) | |
278 | { | |
279 | /* No parameter type information is recorded with the | |
280 | function's type. Set that from the type of the | |
281 | parameter symbols. */ | |
282 | int nparams = 0, iparams; | |
283 | struct symbol *sym; | |
de4f826b | 284 | ALL_BLOCK_SYMBOLS (block, iter, sym) |
c906108c | 285 | { |
c906108c SS |
286 | switch (SYMBOL_CLASS (sym)) |
287 | { | |
288 | case LOC_ARG: | |
289 | case LOC_REF_ARG: | |
290 | case LOC_REGPARM: | |
291 | case LOC_REGPARM_ADDR: | |
4c2df51b | 292 | case LOC_COMPUTED_ARG: |
c906108c SS |
293 | nparams++; |
294 | break; | |
295 | case LOC_UNDEF: | |
296 | case LOC_CONST: | |
297 | case LOC_STATIC: | |
c906108c SS |
298 | case LOC_REGISTER: |
299 | case LOC_LOCAL: | |
300 | case LOC_TYPEDEF: | |
301 | case LOC_LABEL: | |
302 | case LOC_BLOCK: | |
303 | case LOC_CONST_BYTES: | |
c906108c SS |
304 | case LOC_UNRESOLVED: |
305 | case LOC_OPTIMIZED_OUT: | |
4c2df51b | 306 | case LOC_COMPUTED: |
c906108c SS |
307 | default: |
308 | break; | |
309 | } | |
310 | } | |
311 | if (nparams > 0) | |
312 | { | |
313 | TYPE_NFIELDS (ftype) = nparams; | |
314 | TYPE_FIELDS (ftype) = (struct field *) | |
315 | TYPE_ALLOC (ftype, nparams * sizeof (struct field)); | |
316 | ||
de4f826b DC |
317 | iparams = 0; |
318 | ALL_BLOCK_SYMBOLS (block, iter, sym) | |
c906108c | 319 | { |
de4f826b DC |
320 | if (iparams == nparams) |
321 | break; | |
322 | ||
c906108c SS |
323 | switch (SYMBOL_CLASS (sym)) |
324 | { | |
325 | case LOC_ARG: | |
326 | case LOC_REF_ARG: | |
327 | case LOC_REGPARM: | |
328 | case LOC_REGPARM_ADDR: | |
4c2df51b | 329 | case LOC_COMPUTED_ARG: |
c906108c | 330 | TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym); |
8176bb6d | 331 | TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0; |
c906108c SS |
332 | iparams++; |
333 | break; | |
334 | case LOC_UNDEF: | |
335 | case LOC_CONST: | |
336 | case LOC_STATIC: | |
c906108c SS |
337 | case LOC_REGISTER: |
338 | case LOC_LOCAL: | |
339 | case LOC_TYPEDEF: | |
340 | case LOC_LABEL: | |
341 | case LOC_BLOCK: | |
342 | case LOC_CONST_BYTES: | |
c906108c SS |
343 | case LOC_UNRESOLVED: |
344 | case LOC_OPTIMIZED_OUT: | |
4c2df51b | 345 | case LOC_COMPUTED: |
c906108c SS |
346 | default: |
347 | break; | |
348 | } | |
349 | } | |
350 | } | |
351 | } | |
9219021c DC |
352 | |
353 | /* If we're in the C++ case, set the block's scope. */ | |
354 | if (SYMBOL_LANGUAGE (symbol) == language_cplus) | |
355 | { | |
4a146b47 | 356 | cp_set_block_scope (symbol, block, &objfile->objfile_obstack); |
9219021c | 357 | } |
c906108c SS |
358 | } |
359 | else | |
360 | { | |
361 | BLOCK_FUNCTION (block) = NULL; | |
362 | } | |
363 | ||
364 | /* Now "free" the links of the list, and empty the list. */ | |
365 | ||
366 | for (next = *listhead; next; next = next1) | |
367 | { | |
368 | next1 = next->next; | |
369 | next->next = free_pendings; | |
370 | free_pendings = next; | |
371 | } | |
372 | *listhead = NULL; | |
373 | ||
c906108c SS |
374 | /* Check to be sure that the blocks have an end address that is |
375 | greater than starting address */ | |
376 | ||
377 | if (BLOCK_END (block) < BLOCK_START (block)) | |
378 | { | |
379 | if (symbol) | |
380 | { | |
23136709 | 381 | complaint (&symfile_complaints, |
3d263c1d | 382 | _("block end address less than block start address in %s (patched it)"), |
de5ad195 | 383 | SYMBOL_PRINT_NAME (symbol)); |
c906108c SS |
384 | } |
385 | else | |
386 | { | |
23136709 | 387 | complaint (&symfile_complaints, |
3d263c1d | 388 | _("block end address 0x%s less than block start address 0x%s (patched it)"), |
23136709 | 389 | paddr_nz (BLOCK_END (block)), paddr_nz (BLOCK_START (block))); |
c906108c SS |
390 | } |
391 | /* Better than nothing */ | |
392 | BLOCK_END (block) = BLOCK_START (block); | |
393 | } | |
c906108c SS |
394 | |
395 | /* Install this block as the superblock of all blocks made since the | |
396 | start of this scope that don't have superblocks yet. */ | |
397 | ||
398 | opblock = NULL; | |
c0219d42 MS |
399 | for (pblock = pending_blocks; |
400 | pblock && pblock != old_blocks; | |
401 | pblock = pblock->next) | |
c906108c SS |
402 | { |
403 | if (BLOCK_SUPERBLOCK (pblock->block) == NULL) | |
404 | { | |
c906108c SS |
405 | /* Check to be sure the blocks are nested as we receive |
406 | them. If the compiler/assembler/linker work, this just | |
14711c82 DJ |
407 | burns a small amount of time. |
408 | ||
409 | Skip blocks which correspond to a function; they're not | |
410 | physically nested inside this other blocks, only | |
411 | lexically nested. */ | |
412 | if (BLOCK_FUNCTION (pblock->block) == NULL | |
413 | && (BLOCK_START (pblock->block) < BLOCK_START (block) | |
414 | || BLOCK_END (pblock->block) > BLOCK_END (block))) | |
c906108c SS |
415 | { |
416 | if (symbol) | |
417 | { | |
23136709 | 418 | complaint (&symfile_complaints, |
3d263c1d | 419 | _("inner block not inside outer block in %s"), |
de5ad195 | 420 | SYMBOL_PRINT_NAME (symbol)); |
c906108c SS |
421 | } |
422 | else | |
423 | { | |
23136709 | 424 | complaint (&symfile_complaints, |
3d263c1d | 425 | _("inner block (0x%s-0x%s) not inside outer block (0x%s-0x%s)"), |
23136709 KB |
426 | paddr_nz (BLOCK_START (pblock->block)), |
427 | paddr_nz (BLOCK_END (pblock->block)), | |
428 | paddr_nz (BLOCK_START (block)), | |
429 | paddr_nz (BLOCK_END (block))); | |
c906108c SS |
430 | } |
431 | if (BLOCK_START (pblock->block) < BLOCK_START (block)) | |
432 | BLOCK_START (pblock->block) = BLOCK_START (block); | |
433 | if (BLOCK_END (pblock->block) > BLOCK_END (block)) | |
434 | BLOCK_END (pblock->block) = BLOCK_END (block); | |
435 | } | |
c906108c SS |
436 | BLOCK_SUPERBLOCK (pblock->block) = block; |
437 | } | |
438 | opblock = pblock; | |
439 | } | |
440 | ||
441 | record_pending_block (objfile, block, opblock); | |
801e3a5b JB |
442 | |
443 | return block; | |
c906108c SS |
444 | } |
445 | ||
de4f826b | 446 | |
c906108c SS |
447 | /* Record BLOCK on the list of all blocks in the file. Put it after |
448 | OPBLOCK, or at the beginning if opblock is NULL. This puts the | |
449 | block in the list after all its subblocks. | |
450 | ||
4a146b47 | 451 | Allocate the pending block struct in the objfile_obstack to save |
c906108c SS |
452 | time. This wastes a little space. FIXME: Is it worth it? */ |
453 | ||
454 | void | |
455 | record_pending_block (struct objfile *objfile, struct block *block, | |
456 | struct pending_block *opblock) | |
457 | { | |
52f0bd74 | 458 | struct pending_block *pblock; |
c906108c SS |
459 | |
460 | pblock = (struct pending_block *) | |
4a146b47 | 461 | obstack_alloc (&objfile->objfile_obstack, sizeof (struct pending_block)); |
c906108c SS |
462 | pblock->block = block; |
463 | if (opblock) | |
464 | { | |
465 | pblock->next = opblock->next; | |
466 | opblock->next = pblock; | |
467 | } | |
468 | else | |
469 | { | |
470 | pblock->next = pending_blocks; | |
471 | pending_blocks = pblock; | |
472 | } | |
473 | } | |
474 | ||
801e3a5b JB |
475 | |
476 | /* Record that the range of addresses from START to END_INCLUSIVE | |
477 | (inclusive, like it says) belongs to BLOCK. BLOCK's start and end | |
478 | addresses must be set already. You must apply this function to all | |
479 | BLOCK's children before applying it to BLOCK. | |
480 | ||
481 | If a call to this function complicates the picture beyond that | |
482 | already provided by BLOCK_START and BLOCK_END, then we create an | |
483 | address map for the block. */ | |
484 | void | |
485 | record_block_range (struct block *block, | |
486 | CORE_ADDR start, CORE_ADDR end_inclusive) | |
487 | { | |
488 | /* If this is any different from the range recorded in the block's | |
489 | own BLOCK_START and BLOCK_END, then note that the address map has | |
490 | become interesting. Note that even if this block doesn't have | |
491 | any "interesting" ranges, some later block might, so we still | |
492 | need to record this block in the addrmap. */ | |
493 | if (start != BLOCK_START (block) | |
494 | || end_inclusive + 1 != BLOCK_END (block)) | |
495 | pending_addrmap_interesting = 1; | |
496 | ||
497 | if (! pending_addrmap) | |
498 | { | |
499 | obstack_init (&pending_addrmap_obstack); | |
500 | pending_addrmap = addrmap_create_mutable (&pending_addrmap_obstack); | |
501 | } | |
502 | ||
503 | addrmap_set_empty (pending_addrmap, start, end_inclusive, block); | |
504 | } | |
505 | ||
506 | ||
822e978b | 507 | static struct blockvector * |
c906108c SS |
508 | make_blockvector (struct objfile *objfile) |
509 | { | |
52f0bd74 AC |
510 | struct pending_block *next; |
511 | struct blockvector *blockvector; | |
512 | int i; | |
c906108c SS |
513 | |
514 | /* Count the length of the list of blocks. */ | |
515 | ||
516 | for (next = pending_blocks, i = 0; next; next = next->next, i++) | |
517 | {; | |
518 | } | |
519 | ||
520 | blockvector = (struct blockvector *) | |
4a146b47 | 521 | obstack_alloc (&objfile->objfile_obstack, |
c906108c SS |
522 | (sizeof (struct blockvector) |
523 | + (i - 1) * sizeof (struct block *))); | |
524 | ||
525 | /* Copy the blocks into the blockvector. This is done in reverse | |
526 | order, which happens to put the blocks into the proper order | |
527 | (ascending starting address). finish_block has hair to insert | |
528 | each block into the list after its subblocks in order to make | |
529 | sure this is true. */ | |
530 | ||
531 | BLOCKVECTOR_NBLOCKS (blockvector) = i; | |
532 | for (next = pending_blocks; next; next = next->next) | |
533 | { | |
534 | BLOCKVECTOR_BLOCK (blockvector, --i) = next->block; | |
535 | } | |
536 | ||
89ba75b1 | 537 | free_pending_blocks (); |
c906108c | 538 | |
801e3a5b JB |
539 | /* If we needed an address map for this symtab, record it in the |
540 | blockvector. */ | |
541 | if (pending_addrmap && pending_addrmap_interesting) | |
542 | BLOCKVECTOR_MAP (blockvector) | |
543 | = addrmap_create_fixed (pending_addrmap, &objfile->objfile_obstack); | |
544 | else | |
545 | BLOCKVECTOR_MAP (blockvector) = 0; | |
546 | ||
c906108c SS |
547 | /* Some compilers output blocks in the wrong order, but we depend on |
548 | their being in the right order so we can binary search. Check the | |
a239dc23 | 549 | order and moan about it. */ |
c906108c SS |
550 | if (BLOCKVECTOR_NBLOCKS (blockvector) > 1) |
551 | { | |
552 | for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++) | |
553 | { | |
554 | if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1)) | |
555 | > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i))) | |
556 | { | |
59527da0 JB |
557 | CORE_ADDR start |
558 | = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)); | |
c906108c | 559 | |
3d263c1d | 560 | complaint (&symfile_complaints, _("block at %s out of order"), |
bb599908 | 561 | hex_string ((LONGEST) start)); |
c906108c SS |
562 | } |
563 | } | |
564 | } | |
c906108c SS |
565 | |
566 | return (blockvector); | |
567 | } | |
568 | \f | |
569 | /* Start recording information about source code that came from an | |
570 | included (or otherwise merged-in) source file with a different | |
571 | name. NAME is the name of the file (cannot be NULL), DIRNAME is | |
0b0287a1 | 572 | the directory in which the file was compiled (or NULL if not known). */ |
c906108c SS |
573 | |
574 | void | |
575 | start_subfile (char *name, char *dirname) | |
576 | { | |
52f0bd74 | 577 | struct subfile *subfile; |
c906108c SS |
578 | |
579 | /* See if this subfile is already known as a subfile of the current | |
580 | main source file. */ | |
581 | ||
582 | for (subfile = subfiles; subfile; subfile = subfile->next) | |
583 | { | |
84ba0adf DJ |
584 | char *subfile_name; |
585 | ||
586 | /* If NAME is an absolute path, and this subfile is not, then | |
587 | attempt to create an absolute path to compare. */ | |
588 | if (IS_ABSOLUTE_PATH (name) | |
589 | && !IS_ABSOLUTE_PATH (subfile->name) | |
590 | && subfile->dirname != NULL) | |
591 | subfile_name = concat (subfile->dirname, SLASH_STRING, | |
592 | subfile->name, NULL); | |
593 | else | |
594 | subfile_name = subfile->name; | |
595 | ||
596 | if (FILENAME_CMP (subfile_name, name) == 0) | |
c906108c SS |
597 | { |
598 | current_subfile = subfile; | |
84ba0adf DJ |
599 | if (subfile_name != subfile->name) |
600 | xfree (subfile_name); | |
c906108c SS |
601 | return; |
602 | } | |
84ba0adf DJ |
603 | if (subfile_name != subfile->name) |
604 | xfree (subfile_name); | |
c906108c SS |
605 | } |
606 | ||
607 | /* This subfile is not known. Add an entry for it. Make an entry | |
608 | for this subfile in the list of all subfiles of the current main | |
609 | source file. */ | |
610 | ||
611 | subfile = (struct subfile *) xmalloc (sizeof (struct subfile)); | |
59527da0 | 612 | memset ((char *) subfile, 0, sizeof (struct subfile)); |
c906108c SS |
613 | subfile->next = subfiles; |
614 | subfiles = subfile; | |
615 | current_subfile = subfile; | |
616 | ||
617 | /* Save its name and compilation directory name */ | |
618 | subfile->name = (name == NULL) ? NULL : savestring (name, strlen (name)); | |
619 | subfile->dirname = | |
620 | (dirname == NULL) ? NULL : savestring (dirname, strlen (dirname)); | |
621 | ||
622 | /* Initialize line-number recording for this subfile. */ | |
623 | subfile->line_vector = NULL; | |
624 | ||
625 | /* Default the source language to whatever can be deduced from the | |
626 | filename. If nothing can be deduced (such as for a C/C++ include | |
627 | file with a ".h" extension), then inherit whatever language the | |
628 | previous subfile had. This kludgery is necessary because there | |
629 | is no standard way in some object formats to record the source | |
630 | language. Also, when symtabs are allocated we try to deduce a | |
631 | language then as well, but it is too late for us to use that | |
632 | information while reading symbols, since symtabs aren't allocated | |
633 | until after all the symbols have been processed for a given | |
634 | source file. */ | |
635 | ||
636 | subfile->language = deduce_language_from_filename (subfile->name); | |
637 | if (subfile->language == language_unknown && | |
638 | subfile->next != NULL) | |
639 | { | |
640 | subfile->language = subfile->next->language; | |
641 | } | |
642 | ||
643 | /* Initialize the debug format string to NULL. We may supply it | |
644 | later via a call to record_debugformat. */ | |
645 | subfile->debugformat = NULL; | |
646 | ||
303b6f5d DJ |
647 | /* Similarly for the producer. */ |
648 | subfile->producer = NULL; | |
649 | ||
25caa7a8 | 650 | /* If the filename of this subfile ends in .C, then change the |
c906108c | 651 | language of any pending subfiles from C to C++. We also accept |
25caa7a8 | 652 | any other C++ suffixes accepted by deduce_language_from_filename. */ |
c906108c SS |
653 | /* Likewise for f2c. */ |
654 | ||
655 | if (subfile->name) | |
656 | { | |
657 | struct subfile *s; | |
658 | enum language sublang = deduce_language_from_filename (subfile->name); | |
659 | ||
660 | if (sublang == language_cplus || sublang == language_fortran) | |
661 | for (s = subfiles; s != NULL; s = s->next) | |
662 | if (s->language == language_c) | |
663 | s->language = sublang; | |
664 | } | |
665 | ||
666 | /* And patch up this file if necessary. */ | |
667 | if (subfile->language == language_c | |
668 | && subfile->next != NULL | |
669 | && (subfile->next->language == language_cplus | |
670 | || subfile->next->language == language_fortran)) | |
671 | { | |
672 | subfile->language = subfile->next->language; | |
673 | } | |
674 | } | |
675 | ||
676 | /* For stabs readers, the first N_SO symbol is assumed to be the | |
677 | source file name, and the subfile struct is initialized using that | |
678 | assumption. If another N_SO symbol is later seen, immediately | |
679 | following the first one, then the first one is assumed to be the | |
680 | directory name and the second one is really the source file name. | |
681 | ||
682 | So we have to patch up the subfile struct by moving the old name | |
683 | value to dirname and remembering the new name. Some sanity | |
684 | checking is performed to ensure that the state of the subfile | |
685 | struct is reasonable and that the old name we are assuming to be a | |
686 | directory name actually is (by checking for a trailing '/'). */ | |
687 | ||
688 | void | |
689 | patch_subfile_names (struct subfile *subfile, char *name) | |
690 | { | |
691 | if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL | |
692 | && subfile->name[strlen (subfile->name) - 1] == '/') | |
693 | { | |
694 | subfile->dirname = subfile->name; | |
695 | subfile->name = savestring (name, strlen (name)); | |
696 | last_source_file = name; | |
697 | ||
698 | /* Default the source language to whatever can be deduced from | |
699 | the filename. If nothing can be deduced (such as for a C/C++ | |
700 | include file with a ".h" extension), then inherit whatever | |
701 | language the previous subfile had. This kludgery is | |
702 | necessary because there is no standard way in some object | |
703 | formats to record the source language. Also, when symtabs | |
704 | are allocated we try to deduce a language then as well, but | |
705 | it is too late for us to use that information while reading | |
706 | symbols, since symtabs aren't allocated until after all the | |
707 | symbols have been processed for a given source file. */ | |
708 | ||
709 | subfile->language = deduce_language_from_filename (subfile->name); | |
710 | if (subfile->language == language_unknown && | |
711 | subfile->next != NULL) | |
712 | { | |
713 | subfile->language = subfile->next->language; | |
714 | } | |
715 | } | |
716 | } | |
717 | \f | |
718 | /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for | |
719 | switching source files (different subfiles, as we call them) within | |
720 | one object file, but using a stack rather than in an arbitrary | |
721 | order. */ | |
722 | ||
723 | void | |
724 | push_subfile (void) | |
725 | { | |
52f0bd74 | 726 | struct subfile_stack *tem |
c906108c SS |
727 | = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack)); |
728 | ||
729 | tem->next = subfile_stack; | |
730 | subfile_stack = tem; | |
731 | if (current_subfile == NULL || current_subfile->name == NULL) | |
732 | { | |
3d263c1d | 733 | internal_error (__FILE__, __LINE__, _("failed internal consistency check")); |
c906108c SS |
734 | } |
735 | tem->name = current_subfile->name; | |
736 | } | |
737 | ||
738 | char * | |
739 | pop_subfile (void) | |
740 | { | |
52f0bd74 AC |
741 | char *name; |
742 | struct subfile_stack *link = subfile_stack; | |
c906108c SS |
743 | |
744 | if (link == NULL) | |
745 | { | |
3d263c1d | 746 | internal_error (__FILE__, __LINE__, _("failed internal consistency check")); |
c906108c SS |
747 | } |
748 | name = link->name; | |
749 | subfile_stack = link->next; | |
b8c9b27d | 750 | xfree ((void *) link); |
c906108c SS |
751 | return (name); |
752 | } | |
753 | \f | |
754 | /* Add a linetable entry for line number LINE and address PC to the | |
755 | line vector for SUBFILE. */ | |
756 | ||
757 | void | |
aa1ee363 | 758 | record_line (struct subfile *subfile, int line, CORE_ADDR pc) |
c906108c SS |
759 | { |
760 | struct linetable_entry *e; | |
761 | /* Ignore the dummy line number in libg.o */ | |
762 | ||
763 | if (line == 0xffff) | |
764 | { | |
765 | return; | |
766 | } | |
767 | ||
768 | /* Make sure line vector exists and is big enough. */ | |
769 | if (!subfile->line_vector) | |
770 | { | |
771 | subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH; | |
772 | subfile->line_vector = (struct linetable *) | |
773 | xmalloc (sizeof (struct linetable) | |
c5aa993b | 774 | + subfile->line_vector_length * sizeof (struct linetable_entry)); |
c906108c SS |
775 | subfile->line_vector->nitems = 0; |
776 | have_line_numbers = 1; | |
777 | } | |
778 | ||
779 | if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length) | |
780 | { | |
781 | subfile->line_vector_length *= 2; | |
782 | subfile->line_vector = (struct linetable *) | |
783 | xrealloc ((char *) subfile->line_vector, | |
784 | (sizeof (struct linetable) | |
785 | + (subfile->line_vector_length | |
786 | * sizeof (struct linetable_entry)))); | |
787 | } | |
788 | ||
607ae575 DJ |
789 | pc = gdbarch_addr_bits_remove (current_gdbarch, pc); |
790 | ||
791 | /* Normally, we treat lines as unsorted. But the end of sequence | |
792 | marker is special. We sort line markers at the same PC by line | |
793 | number, so end of sequence markers (which have line == 0) appear | |
794 | first. This is right if the marker ends the previous function, | |
795 | and there is no padding before the next function. But it is | |
796 | wrong if the previous line was empty and we are now marking a | |
797 | switch to a different subfile. We must leave the end of sequence | |
798 | marker at the end of this group of lines, not sort the empty line | |
799 | to after the marker. The easiest way to accomplish this is to | |
800 | delete any empty lines from our table, if they are followed by | |
801 | end of sequence markers. All we lose is the ability to set | |
802 | breakpoints at some lines which contain no instructions | |
803 | anyway. */ | |
804 | if (line == 0 && subfile->line_vector->nitems > 0) | |
805 | { | |
806 | e = subfile->line_vector->item + subfile->line_vector->nitems - 1; | |
807 | while (subfile->line_vector->nitems > 0 && e->pc == pc) | |
808 | { | |
809 | e--; | |
810 | subfile->line_vector->nitems--; | |
811 | } | |
812 | } | |
813 | ||
c906108c SS |
814 | e = subfile->line_vector->item + subfile->line_vector->nitems++; |
815 | e->line = line; | |
607ae575 | 816 | e->pc = pc; |
c906108c SS |
817 | } |
818 | ||
819 | /* Needed in order to sort line tables from IBM xcoff files. Sigh! */ | |
820 | ||
821 | static int | |
822 | compare_line_numbers (const void *ln1p, const void *ln2p) | |
823 | { | |
824 | struct linetable_entry *ln1 = (struct linetable_entry *) ln1p; | |
825 | struct linetable_entry *ln2 = (struct linetable_entry *) ln2p; | |
826 | ||
827 | /* Note: this code does not assume that CORE_ADDRs can fit in ints. | |
828 | Please keep it that way. */ | |
829 | if (ln1->pc < ln2->pc) | |
830 | return -1; | |
831 | ||
832 | if (ln1->pc > ln2->pc) | |
833 | return 1; | |
834 | ||
835 | /* If pc equal, sort by line. I'm not sure whether this is optimum | |
836 | behavior (see comment at struct linetable in symtab.h). */ | |
837 | return ln1->line - ln2->line; | |
838 | } | |
839 | \f | |
840 | /* Start a new symtab for a new source file. Called, for example, | |
841 | when a stabs symbol of type N_SO is seen, or when a DWARF | |
842 | TAG_compile_unit DIE is seen. It indicates the start of data for | |
0b0287a1 DE |
843 | one original source file. |
844 | ||
845 | NAME is the name of the file (cannot be NULL). DIRNAME is the directory in | |
846 | which the file was compiled (or NULL if not known). START_ADDR is the | |
847 | lowest address of objects in the file (or 0 if not known). */ | |
c906108c SS |
848 | |
849 | void | |
850 | start_symtab (char *name, char *dirname, CORE_ADDR start_addr) | |
851 | { | |
c906108c SS |
852 | last_source_file = name; |
853 | last_source_start_addr = start_addr; | |
854 | file_symbols = NULL; | |
855 | global_symbols = NULL; | |
856 | within_function = 0; | |
857 | have_line_numbers = 0; | |
858 | ||
859 | /* Context stack is initially empty. Allocate first one with room | |
860 | for 10 levels; reuse it forever afterward. */ | |
861 | if (context_stack == NULL) | |
862 | { | |
863 | context_stack_size = INITIAL_CONTEXT_STACK_SIZE; | |
864 | context_stack = (struct context_stack *) | |
865 | xmalloc (context_stack_size * sizeof (struct context_stack)); | |
866 | } | |
867 | context_stack_depth = 0; | |
868 | ||
801e3a5b JB |
869 | /* We shouldn't have any address map at this point. */ |
870 | gdb_assert (! pending_addrmap); | |
871 | ||
9219021c DC |
872 | /* Set up support for C++ namespace support, in case we need it. */ |
873 | ||
874 | cp_initialize_namespace (); | |
875 | ||
c906108c SS |
876 | /* Initialize the list of sub source files with one entry for this |
877 | file (the top-level source file). */ | |
878 | ||
879 | subfiles = NULL; | |
880 | current_subfile = NULL; | |
881 | start_subfile (name, dirname); | |
882 | } | |
883 | ||
4584e32e DE |
884 | /* Subroutine of end_symtab to simplify it. |
885 | Look for a subfile that matches the main source file's basename. | |
886 | If there is only one, and if the main source file doesn't have any | |
887 | symbol or line number information, then copy this file's symtab and | |
888 | line_vector to the main source file's subfile and discard the other subfile. | |
889 | This can happen because of a compiler bug or from the user playing games | |
890 | with #line or from things like a distributed build system that manipulates | |
891 | the debug info. */ | |
892 | ||
893 | static void | |
894 | watch_main_source_file_lossage (void) | |
895 | { | |
896 | struct subfile *mainsub, *subfile; | |
897 | ||
898 | /* Find the main source file. | |
899 | This loop could be eliminated if start_symtab saved it for us. */ | |
900 | mainsub = NULL; | |
901 | for (subfile = subfiles; subfile; subfile = subfile->next) | |
902 | { | |
903 | /* The main subfile is guaranteed to be the last one. */ | |
904 | if (subfile->next == NULL) | |
905 | mainsub = subfile; | |
906 | } | |
907 | ||
908 | /* If the main source file doesn't have any line number or symbol info, | |
909 | look for an alias in another subfile. | |
910 | We have to watch for mainsub == NULL here. It's a quirk of end_symtab, | |
911 | it can return NULL so there may not be a main subfile. */ | |
912 | ||
913 | if (mainsub | |
914 | && mainsub->line_vector == NULL | |
915 | && mainsub->symtab == NULL) | |
916 | { | |
917 | const char *mainbase = lbasename (mainsub->name); | |
918 | int nr_matches = 0; | |
919 | struct subfile *prevsub; | |
920 | struct subfile *mainsub_alias = NULL; | |
921 | struct subfile *prev_mainsub_alias = NULL; | |
922 | ||
923 | prevsub = NULL; | |
924 | for (subfile = subfiles; | |
925 | /* Stop before we get to the last one. */ | |
926 | subfile->next; | |
927 | subfile = subfile->next) | |
928 | { | |
929 | if (strcmp (lbasename (subfile->name), mainbase) == 0) | |
930 | { | |
931 | ++nr_matches; | |
932 | mainsub_alias = subfile; | |
933 | prev_mainsub_alias = prevsub; | |
934 | } | |
935 | prevsub = subfile; | |
936 | } | |
937 | ||
938 | if (nr_matches == 1) | |
939 | { | |
940 | gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub); | |
941 | ||
942 | /* Found a match for the main source file. | |
943 | Copy its line_vector and symtab to the main subfile | |
944 | and then discard it. */ | |
945 | ||
946 | mainsub->line_vector = mainsub_alias->line_vector; | |
947 | mainsub->line_vector_length = mainsub_alias->line_vector_length; | |
948 | mainsub->symtab = mainsub_alias->symtab; | |
949 | ||
950 | if (prev_mainsub_alias == NULL) | |
951 | subfiles = mainsub_alias->next; | |
952 | else | |
953 | prev_mainsub_alias->next = mainsub_alias->next; | |
954 | xfree (mainsub_alias); | |
955 | } | |
956 | } | |
957 | } | |
958 | ||
c906108c SS |
959 | /* Finish the symbol definitions for one main source file, close off |
960 | all the lexical contexts for that file (creating struct block's for | |
961 | them), then make the struct symtab for that file and put it in the | |
962 | list of all such. | |
963 | ||
964 | END_ADDR is the address of the end of the file's text. SECTION is | |
965 | the section number (in objfile->section_offsets) of the blockvector | |
966 | and linetable. | |
967 | ||
968 | Note that it is possible for end_symtab() to return NULL. In | |
969 | particular, for the DWARF case at least, it will return NULL when | |
970 | it finds a compilation unit that has exactly one DIE, a | |
971 | TAG_compile_unit DIE. This can happen when we link in an object | |
972 | file that was compiled from an empty source file. Returning NULL | |
973 | is probably not the correct thing to do, because then gdb will | |
974 | never know about this empty file (FIXME). */ | |
975 | ||
976 | struct symtab * | |
977 | end_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section) | |
978 | { | |
52f0bd74 AC |
979 | struct symtab *symtab = NULL; |
980 | struct blockvector *blockvector; | |
981 | struct subfile *subfile; | |
982 | struct context_stack *cstk; | |
c906108c SS |
983 | struct subfile *nextsub; |
984 | ||
985 | /* Finish the lexical context of the last function in the file; pop | |
986 | the context stack. */ | |
987 | ||
988 | if (context_stack_depth > 0) | |
989 | { | |
990 | cstk = pop_context (); | |
991 | /* Make a block for the local symbols within. */ | |
992 | finish_block (cstk->name, &local_symbols, cstk->old_blocks, | |
993 | cstk->start_addr, end_addr, objfile); | |
994 | ||
995 | if (context_stack_depth > 0) | |
996 | { | |
997 | /* This is said to happen with SCO. The old coffread.c | |
998 | code simply emptied the context stack, so we do the | |
999 | same. FIXME: Find out why it is happening. This is not | |
1000 | believed to happen in most cases (even for coffread.c); | |
1001 | it used to be an abort(). */ | |
23136709 | 1002 | complaint (&symfile_complaints, |
3d263c1d | 1003 | _("Context stack not empty in end_symtab")); |
c906108c SS |
1004 | context_stack_depth = 0; |
1005 | } | |
1006 | } | |
1007 | ||
1008 | /* Reordered executables may have out of order pending blocks; if | |
1009 | OBJF_REORDERED is true, then sort the pending blocks. */ | |
1010 | if ((objfile->flags & OBJF_REORDERED) && pending_blocks) | |
1011 | { | |
1012 | /* FIXME! Remove this horrid bubble sort and use merge sort!!! */ | |
1013 | int swapped; | |
1014 | do | |
1015 | { | |
1016 | struct pending_block *pb, *pbnext; | |
1017 | ||
1018 | pb = pending_blocks; | |
1019 | pbnext = pb->next; | |
1020 | swapped = 0; | |
1021 | ||
1022 | while (pbnext) | |
1023 | { | |
1024 | /* swap blocks if unordered! */ | |
1025 | ||
1026 | if (BLOCK_START (pb->block) < BLOCK_START (pbnext->block)) | |
1027 | { | |
1028 | struct block *tmp = pb->block; | |
1029 | pb->block = pbnext->block; | |
1030 | pbnext->block = tmp; | |
1031 | swapped = 1; | |
1032 | } | |
1033 | pb = pbnext; | |
1034 | pbnext = pbnext->next; | |
1035 | } | |
1036 | } | |
1037 | while (swapped); | |
1038 | } | |
1039 | ||
1040 | /* Cleanup any undefined types that have been left hanging around | |
1041 | (this needs to be done before the finish_blocks so that | |
1042 | file_symbols is still good). | |
c5aa993b | 1043 | |
c906108c SS |
1044 | Both cleanup_undefined_types and finish_global_stabs are stabs |
1045 | specific, but harmless for other symbol readers, since on gdb | |
1046 | startup or when finished reading stabs, the state is set so these | |
1047 | are no-ops. FIXME: Is this handled right in case of QUIT? Can | |
1048 | we make this cleaner? */ | |
1049 | ||
1050 | cleanup_undefined_types (); | |
1051 | finish_global_stabs (objfile); | |
1052 | ||
1053 | if (pending_blocks == NULL | |
1054 | && file_symbols == NULL | |
1055 | && global_symbols == NULL | |
99d9066e JB |
1056 | && have_line_numbers == 0 |
1057 | && pending_macros == NULL) | |
c906108c SS |
1058 | { |
1059 | /* Ignore symtabs that have no functions with real debugging | |
1060 | info. */ | |
1061 | blockvector = NULL; | |
1062 | } | |
1063 | else | |
1064 | { | |
1065 | /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the | |
1066 | blockvector. */ | |
1067 | finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr, | |
1068 | objfile); | |
1069 | finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr, | |
1070 | objfile); | |
1071 | blockvector = make_blockvector (objfile); | |
9219021c | 1072 | cp_finalize_namespace (BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK), |
4a146b47 | 1073 | &objfile->objfile_obstack); |
c906108c SS |
1074 | } |
1075 | ||
c295b2e5 JB |
1076 | /* Read the line table if it has to be read separately. */ |
1077 | if (objfile->sf->sym_read_linetable != NULL) | |
1078 | objfile->sf->sym_read_linetable (); | |
c906108c | 1079 | |
4584e32e DE |
1080 | /* Handle the case where the debug info specifies a different path |
1081 | for the main source file. It can cause us to lose track of its | |
1082 | line number information. */ | |
1083 | watch_main_source_file_lossage (); | |
1084 | ||
c906108c SS |
1085 | /* Now create the symtab objects proper, one for each subfile. */ |
1086 | /* (The main file is the last one on the chain.) */ | |
1087 | ||
1088 | for (subfile = subfiles; subfile; subfile = nextsub) | |
1089 | { | |
1090 | int linetablesize = 0; | |
1091 | symtab = NULL; | |
1092 | ||
1093 | /* If we have blocks of symbols, make a symtab. Otherwise, just | |
1094 | ignore this file and any line number info in it. */ | |
1095 | if (blockvector) | |
1096 | { | |
1097 | if (subfile->line_vector) | |
1098 | { | |
1099 | linetablesize = sizeof (struct linetable) + | |
1100 | subfile->line_vector->nitems * sizeof (struct linetable_entry); | |
c906108c SS |
1101 | |
1102 | /* Like the pending blocks, the line table may be | |
1103 | scrambled in reordered executables. Sort it if | |
1104 | OBJF_REORDERED is true. */ | |
1105 | if (objfile->flags & OBJF_REORDERED) | |
1106 | qsort (subfile->line_vector->item, | |
1107 | subfile->line_vector->nitems, | |
c5aa993b | 1108 | sizeof (struct linetable_entry), compare_line_numbers); |
c906108c SS |
1109 | } |
1110 | ||
1111 | /* Now, allocate a symbol table. */ | |
cb1df416 DJ |
1112 | if (subfile->symtab == NULL) |
1113 | symtab = allocate_symtab (subfile->name, objfile); | |
1114 | else | |
1115 | symtab = subfile->symtab; | |
c906108c SS |
1116 | |
1117 | /* Fill in its components. */ | |
1118 | symtab->blockvector = blockvector; | |
99d9066e | 1119 | symtab->macro_table = pending_macros; |
c906108c SS |
1120 | if (subfile->line_vector) |
1121 | { | |
1122 | /* Reallocate the line table on the symbol obstack */ | |
1123 | symtab->linetable = (struct linetable *) | |
4a146b47 | 1124 | obstack_alloc (&objfile->objfile_obstack, linetablesize); |
c906108c SS |
1125 | memcpy (symtab->linetable, subfile->line_vector, linetablesize); |
1126 | } | |
1127 | else | |
1128 | { | |
1129 | symtab->linetable = NULL; | |
1130 | } | |
1131 | symtab->block_line_section = section; | |
1132 | if (subfile->dirname) | |
1133 | { | |
1134 | /* Reallocate the dirname on the symbol obstack */ | |
1135 | symtab->dirname = (char *) | |
4a146b47 | 1136 | obstack_alloc (&objfile->objfile_obstack, |
c906108c SS |
1137 | strlen (subfile->dirname) + 1); |
1138 | strcpy (symtab->dirname, subfile->dirname); | |
1139 | } | |
1140 | else | |
1141 | { | |
1142 | symtab->dirname = NULL; | |
1143 | } | |
1144 | symtab->free_code = free_linetable; | |
de4f826b | 1145 | symtab->free_func = NULL; |
c906108c SS |
1146 | |
1147 | /* Use whatever language we have been using for this | |
1148 | subfile, not the one that was deduced in allocate_symtab | |
1149 | from the filename. We already did our own deducing when | |
1150 | we created the subfile, and we may have altered our | |
1151 | opinion of what language it is from things we found in | |
1152 | the symbols. */ | |
1153 | symtab->language = subfile->language; | |
1154 | ||
1155 | /* Save the debug format string (if any) in the symtab */ | |
1156 | if (subfile->debugformat != NULL) | |
1157 | { | |
1158 | symtab->debugformat = obsavestring (subfile->debugformat, | |
c5aa993b | 1159 | strlen (subfile->debugformat), |
4a146b47 | 1160 | &objfile->objfile_obstack); |
c906108c SS |
1161 | } |
1162 | ||
303b6f5d DJ |
1163 | /* Similarly for the producer. */ |
1164 | if (subfile->producer != NULL) | |
1165 | symtab->producer = obsavestring (subfile->producer, | |
1166 | strlen (subfile->producer), | |
1167 | &objfile->objfile_obstack); | |
1168 | ||
c906108c SS |
1169 | /* All symtabs for the main file and the subfiles share a |
1170 | blockvector, so we need to clear primary for everything | |
1171 | but the main file. */ | |
1172 | ||
1173 | symtab->primary = 0; | |
1174 | } | |
1175 | if (subfile->name != NULL) | |
1176 | { | |
b8c9b27d | 1177 | xfree ((void *) subfile->name); |
c906108c SS |
1178 | } |
1179 | if (subfile->dirname != NULL) | |
1180 | { | |
b8c9b27d | 1181 | xfree ((void *) subfile->dirname); |
c906108c SS |
1182 | } |
1183 | if (subfile->line_vector != NULL) | |
1184 | { | |
b8c9b27d | 1185 | xfree ((void *) subfile->line_vector); |
c906108c SS |
1186 | } |
1187 | if (subfile->debugformat != NULL) | |
1188 | { | |
b8c9b27d | 1189 | xfree ((void *) subfile->debugformat); |
c906108c | 1190 | } |
303b6f5d DJ |
1191 | if (subfile->producer != NULL) |
1192 | xfree (subfile->producer); | |
c906108c SS |
1193 | |
1194 | nextsub = subfile->next; | |
b8c9b27d | 1195 | xfree ((void *) subfile); |
c906108c SS |
1196 | } |
1197 | ||
1198 | /* Set this for the main source file. */ | |
1199 | if (symtab) | |
1200 | { | |
1201 | symtab->primary = 1; | |
1202 | } | |
1203 | ||
cb1df416 DJ |
1204 | /* Default any symbols without a specified symtab to the primary |
1205 | symtab. */ | |
1206 | if (blockvector) | |
1207 | { | |
1208 | int block_i; | |
1209 | ||
1210 | for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++) | |
1211 | { | |
1212 | struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i); | |
1213 | struct symbol *sym; | |
1214 | struct dict_iterator iter; | |
1215 | ||
1216 | for (sym = dict_iterator_first (BLOCK_DICT (block), &iter); | |
1217 | sym != NULL; | |
1218 | sym = dict_iterator_next (&iter)) | |
1219 | if (SYMBOL_SYMTAB (sym) == NULL) | |
1220 | SYMBOL_SYMTAB (sym) = symtab; | |
1221 | } | |
1222 | } | |
1223 | ||
c906108c SS |
1224 | last_source_file = NULL; |
1225 | current_subfile = NULL; | |
99d9066e | 1226 | pending_macros = NULL; |
801e3a5b JB |
1227 | if (pending_addrmap) |
1228 | { | |
1229 | obstack_free (&pending_addrmap_obstack, NULL); | |
1230 | pending_addrmap = NULL; | |
1231 | } | |
c906108c SS |
1232 | |
1233 | return symtab; | |
1234 | } | |
1235 | ||
1236 | /* Push a context block. Args are an identifying nesting level | |
1237 | (checkable when you pop it), and the starting PC address of this | |
1238 | context. */ | |
1239 | ||
1240 | struct context_stack * | |
1241 | push_context (int desc, CORE_ADDR valu) | |
1242 | { | |
52f0bd74 | 1243 | struct context_stack *new; |
c906108c SS |
1244 | |
1245 | if (context_stack_depth == context_stack_size) | |
1246 | { | |
1247 | context_stack_size *= 2; | |
1248 | context_stack = (struct context_stack *) | |
1249 | xrealloc ((char *) context_stack, | |
c5aa993b | 1250 | (context_stack_size * sizeof (struct context_stack))); |
c906108c SS |
1251 | } |
1252 | ||
1253 | new = &context_stack[context_stack_depth++]; | |
1254 | new->depth = desc; | |
1255 | new->locals = local_symbols; | |
1256 | new->params = param_symbols; | |
1257 | new->old_blocks = pending_blocks; | |
1258 | new->start_addr = valu; | |
1259 | new->name = NULL; | |
1260 | ||
1261 | local_symbols = NULL; | |
1262 | param_symbols = NULL; | |
1263 | ||
1264 | return new; | |
1265 | } | |
0c5e171a | 1266 | |
a672ef13 KD |
1267 | /* Pop a context block. Returns the address of the context block just |
1268 | popped. */ | |
1269 | ||
0c5e171a KD |
1270 | struct context_stack * |
1271 | pop_context (void) | |
1272 | { | |
1273 | gdb_assert (context_stack_depth > 0); | |
1274 | return (&context_stack[--context_stack_depth]); | |
1275 | } | |
1276 | ||
c906108c | 1277 | \f |
357e46e7 | 1278 | |
c906108c SS |
1279 | /* Compute a small integer hash code for the given name. */ |
1280 | ||
1281 | int | |
1282 | hashname (char *name) | |
1283 | { | |
357e46e7 | 1284 | return (hash(name,strlen(name)) % HASHSIZE); |
c906108c SS |
1285 | } |
1286 | \f | |
1287 | ||
1288 | void | |
1289 | record_debugformat (char *format) | |
1290 | { | |
1291 | current_subfile->debugformat = savestring (format, strlen (format)); | |
1292 | } | |
1293 | ||
303b6f5d DJ |
1294 | void |
1295 | record_producer (const char *producer) | |
1296 | { | |
05279ca0 JB |
1297 | /* The producer is not always provided in the debugging info. |
1298 | Do nothing if PRODUCER is NULL. */ | |
1299 | if (producer == NULL) | |
1300 | return; | |
1301 | ||
303b6f5d DJ |
1302 | current_subfile->producer = savestring (producer, strlen (producer)); |
1303 | } | |
1304 | ||
c906108c SS |
1305 | /* Merge the first symbol list SRCLIST into the second symbol list |
1306 | TARGETLIST by repeated calls to add_symbol_to_list(). This | |
1307 | procedure "frees" each link of SRCLIST by adding it to the | |
1308 | free_pendings list. Caller must set SRCLIST to a null list after | |
1309 | calling this function. | |
1310 | ||
1311 | Void return. */ | |
1312 | ||
1313 | void | |
1314 | merge_symbol_lists (struct pending **srclist, struct pending **targetlist) | |
1315 | { | |
52f0bd74 | 1316 | int i; |
c906108c SS |
1317 | |
1318 | if (!srclist || !*srclist) | |
1319 | return; | |
1320 | ||
1321 | /* Merge in elements from current link. */ | |
1322 | for (i = 0; i < (*srclist)->nsyms; i++) | |
1323 | add_symbol_to_list ((*srclist)->symbol[i], targetlist); | |
1324 | ||
1325 | /* Recurse on next. */ | |
1326 | merge_symbol_lists (&(*srclist)->next, targetlist); | |
1327 | ||
1328 | /* "Free" the current link. */ | |
1329 | (*srclist)->next = free_pendings; | |
1330 | free_pendings = (*srclist); | |
1331 | } | |
1332 | \f | |
1333 | /* Initialize anything that needs initializing when starting to read a | |
1334 | fresh piece of a symbol file, e.g. reading in the stuff | |
1335 | corresponding to a psymtab. */ | |
1336 | ||
1337 | void | |
fba45db2 | 1338 | buildsym_init (void) |
c906108c SS |
1339 | { |
1340 | free_pendings = NULL; | |
1341 | file_symbols = NULL; | |
1342 | global_symbols = NULL; | |
1343 | pending_blocks = NULL; | |
99d9066e | 1344 | pending_macros = NULL; |
801e3a5b JB |
1345 | |
1346 | /* We shouldn't have any address map at this point. */ | |
1347 | gdb_assert (! pending_addrmap); | |
1348 | pending_addrmap_interesting = 0; | |
c906108c SS |
1349 | } |
1350 | ||
1351 | /* Initialize anything that needs initializing when a completely new | |
1352 | symbol file is specified (not just adding some symbols from another | |
1353 | file, e.g. a shared library). */ | |
1354 | ||
1355 | void | |
fba45db2 | 1356 | buildsym_new_init (void) |
c906108c SS |
1357 | { |
1358 | buildsym_init (); | |
1359 | } |