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