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