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