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