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