Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support routines for building symbol tables in GDB's internal format. |
3666a048 | 2 | Copyright (C) 1986-2021 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 | 18 | |
c906108c | 19 | #include "defs.h" |
d55e5aa6 | 20 | #include "buildsym-legacy.h" |
4de283e4 | 21 | #include "bfd.h" |
d55e5aa6 | 22 | #include "gdb_obstack.h" |
4de283e4 TT |
23 | #include "symtab.h" |
24 | #include "symfile.h" | |
25 | #include "objfiles.h" | |
d55e5aa6 | 26 | #include "gdbtypes.h" |
4de283e4 TT |
27 | #include "complaints.h" |
28 | #include "expression.h" /* For "enum exp_opcode" used by... */ | |
29 | #include "filenames.h" /* For DOSish file names. */ | |
d55e5aa6 | 30 | #include "macrotab.h" |
4de283e4 TT |
31 | #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */ |
32 | #include "block.h" | |
33 | #include "cp-support.h" | |
34 | #include "dictionary.h" | |
35 | #include "addrmap.h" | |
36 | #include <algorithm> | |
9219021c | 37 | |
0a0edcd5 | 38 | /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat |
c906108c SS |
39 | questionable--see comment where we call them). */ |
40 | ||
41 | #include "stabsread.h" | |
42 | ||
93eed41f TT |
43 | /* List of blocks already made (lexical contexts already closed). |
44 | This is used at the end to make the blockvector. */ | |
45 | ||
46 | struct pending_block | |
47 | { | |
48 | struct pending_block *next; | |
49 | struct block *block; | |
50 | }; | |
51 | ||
c906108c SS |
52 | /* Initial sizes of data structures. These are realloc'd larger if |
53 | needed, and realloc'd down to the size actually used, when | |
54 | completed. */ | |
55 | ||
c906108c SS |
56 | #define INITIAL_LINE_VECTOR_LENGTH 1000 |
57 | \f | |
58 | ||
ab209f6f TT |
59 | buildsym_compunit::buildsym_compunit (struct objfile *objfile_, |
60 | const char *name, | |
61 | const char *comp_dir_, | |
62 | enum language language_, | |
63 | CORE_ADDR last_addr) | |
cbb09508 | 64 | : m_objfile (objfile_), |
ab209f6f | 65 | m_last_source_file (name == nullptr ? nullptr : xstrdup (name)), |
cbb09508 KS |
66 | m_comp_dir (comp_dir_ == nullptr ? nullptr : xstrdup (comp_dir_)), |
67 | m_language (language_), | |
ab209f6f TT |
68 | m_last_source_start_addr (last_addr) |
69 | { | |
70 | /* Allocate the compunit symtab now. The caller needs it to allocate | |
71 | non-primary symtabs. It is also needed by get_macro_table. */ | |
cbb09508 | 72 | m_compunit_symtab = allocate_compunit_symtab (m_objfile, name); |
ab209f6f TT |
73 | |
74 | /* Build the subfile for NAME (the main source file) so that we can record | |
75 | a pointer to it for later. | |
76 | IMPORTANT: Do not allocate a struct symtab for NAME here. | |
77 | It can happen that the debug info provides a different path to NAME than | |
78 | DIRNAME,NAME. We cope with this in watch_main_source_file_lossage but | |
79 | that only works if the main_subfile doesn't have a symtab yet. */ | |
80 | start_subfile (name); | |
81 | /* Save this so that we don't have to go looking for it at the end | |
82 | of the subfiles list. */ | |
cbb09508 | 83 | m_main_subfile = m_current_subfile; |
ab209f6f TT |
84 | } |
85 | ||
86 | buildsym_compunit::~buildsym_compunit () | |
87 | { | |
88 | struct subfile *subfile, *nextsub; | |
89 | ||
90 | if (m_pending_macros != nullptr) | |
91 | free_macro_table (m_pending_macros); | |
92 | ||
cbb09508 | 93 | for (subfile = m_subfiles; |
ab209f6f TT |
94 | subfile != NULL; |
95 | subfile = nextsub) | |
96 | { | |
97 | nextsub = subfile->next; | |
98 | xfree (subfile->name); | |
99 | xfree (subfile->line_vector); | |
100 | xfree (subfile); | |
101 | } | |
102 | ||
103 | struct pending *next, *next1; | |
104 | ||
105 | for (next = m_file_symbols; next != NULL; next = next1) | |
106 | { | |
107 | next1 = next->next; | |
108 | xfree ((void *) next); | |
109 | } | |
110 | ||
111 | for (next = m_global_symbols; next != NULL; next = next1) | |
112 | { | |
113 | next1 = next->next; | |
114 | xfree ((void *) next); | |
115 | } | |
116 | } | |
117 | ||
118 | struct macro_table * | |
119 | buildsym_compunit::get_macro_table () | |
120 | { | |
121 | if (m_pending_macros == nullptr) | |
cbb09508 | 122 | m_pending_macros = new_macro_table (&m_objfile->per_bfd->storage_obstack, |
be1e3d3e | 123 | &m_objfile->per_bfd->string_cache, |
cbb09508 | 124 | m_compunit_symtab); |
ab209f6f TT |
125 | return m_pending_macros; |
126 | } | |
127 | ||
4a64f543 | 128 | /* Maintain the lists of symbols and blocks. */ |
c906108c | 129 | |
93bf33fd | 130 | /* Add a symbol to one of the lists of symbols. */ |
c906108c SS |
131 | |
132 | void | |
133 | add_symbol_to_list (struct symbol *symbol, struct pending **listhead) | |
134 | { | |
52f0bd74 | 135 | struct pending *link; |
c906108c SS |
136 | |
137 | /* If this is an alias for another symbol, don't add it. */ | |
4d4eaa30 | 138 | if (symbol->linkage_name () && symbol->linkage_name ()[0] == '#') |
c906108c SS |
139 | return; |
140 | ||
4a64f543 | 141 | /* We keep PENDINGSIZE symbols in each link of the list. If we |
c906108c SS |
142 | don't have a link with room in it, add a new link. */ |
143 | if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE) | |
144 | { | |
1d376700 | 145 | link = XNEW (struct pending); |
c906108c SS |
146 | link->next = *listhead; |
147 | *listhead = link; | |
148 | link->nsyms = 0; | |
149 | } | |
150 | ||
151 | (*listhead)->symbol[(*listhead)->nsyms++] = symbol; | |
152 | } | |
153 | ||
154 | /* Find a symbol named NAME on a LIST. NAME need not be | |
155 | '\0'-terminated; LENGTH is the length of the name. */ | |
156 | ||
157 | struct symbol * | |
158 | find_symbol_in_list (struct pending *list, char *name, int length) | |
159 | { | |
160 | int j; | |
0d5cff50 | 161 | const char *pp; |
c906108c SS |
162 | |
163 | while (list != NULL) | |
164 | { | |
165 | for (j = list->nsyms; --j >= 0;) | |
166 | { | |
987012b8 | 167 | pp = list->symbol[j]->linkage_name (); |
5aafa1cc PM |
168 | if (*pp == *name && strncmp (pp, name, length) == 0 |
169 | && pp[length] == '\0') | |
c906108c SS |
170 | { |
171 | return (list->symbol[j]); | |
172 | } | |
173 | } | |
174 | list = list->next; | |
175 | } | |
176 | return (NULL); | |
177 | } | |
178 | ||
6b213a47 TT |
179 | /* Record BLOCK on the list of all blocks in the file. Put it after |
180 | OPBLOCK, or at the beginning if opblock is NULL. This puts the | |
181 | block in the list after all its subblocks. */ | |
182 | ||
4a2125f5 TT |
183 | void |
184 | buildsym_compunit::record_pending_block (struct block *block, | |
185 | struct pending_block *opblock) | |
6b213a47 TT |
186 | { |
187 | struct pending_block *pblock; | |
188 | ||
4a2125f5 | 189 | pblock = XOBNEW (&m_pending_block_obstack, struct pending_block); |
6b213a47 TT |
190 | pblock->block = block; |
191 | if (opblock) | |
192 | { | |
193 | pblock->next = opblock->next; | |
194 | opblock->next = pblock; | |
195 | } | |
196 | else | |
197 | { | |
4a2125f5 TT |
198 | pblock->next = m_pending_blocks; |
199 | m_pending_blocks = pblock; | |
6b213a47 TT |
200 | } |
201 | } | |
202 | ||
c906108c SS |
203 | /* Take one of the lists of symbols and make a block from it. Keep |
204 | the order the symbols have in the list (reversed from the input | |
205 | file). Put the block on the list of pending blocks. */ | |
206 | ||
4a2125f5 TT |
207 | struct block * |
208 | buildsym_compunit::finish_block_internal | |
209 | (struct symbol *symbol, | |
210 | struct pending **listhead, | |
211 | struct pending_block *old_blocks, | |
212 | const struct dynamic_prop *static_link, | |
213 | CORE_ADDR start, CORE_ADDR end, | |
214 | int is_global, int expandable) | |
c906108c | 215 | { |
08feed99 | 216 | struct gdbarch *gdbarch = m_objfile->arch (); |
52f0bd74 AC |
217 | struct pending *next, *next1; |
218 | struct block *block; | |
219 | struct pending_block *pblock; | |
c906108c | 220 | struct pending_block *opblock; |
c906108c | 221 | |
84a146c9 | 222 | block = (is_global |
cbb09508 KS |
223 | ? allocate_global_block (&m_objfile->objfile_obstack) |
224 | : allocate_block (&m_objfile->objfile_obstack)); | |
c906108c | 225 | |
261397f8 DJ |
226 | if (symbol) |
227 | { | |
b026f593 KS |
228 | BLOCK_MULTIDICT (block) |
229 | = mdict_create_linear (&m_objfile->objfile_obstack, *listhead); | |
261397f8 DJ |
230 | } |
231 | else | |
c906108c | 232 | { |
6d30eef8 DE |
233 | if (expandable) |
234 | { | |
b026f593 KS |
235 | BLOCK_MULTIDICT (block) = mdict_create_hashed_expandable (m_language); |
236 | mdict_add_pending (BLOCK_MULTIDICT (block), *listhead); | |
6d30eef8 DE |
237 | } |
238 | else | |
239 | { | |
b026f593 KS |
240 | BLOCK_MULTIDICT (block) = |
241 | mdict_create_hashed (&m_objfile->objfile_obstack, *listhead); | |
6d30eef8 | 242 | } |
c906108c SS |
243 | } |
244 | ||
245 | BLOCK_START (block) = start; | |
246 | BLOCK_END (block) = end; | |
c906108c | 247 | |
c906108c SS |
248 | /* Put the block in as the value of the symbol that names it. */ |
249 | ||
250 | if (symbol) | |
251 | { | |
252 | struct type *ftype = SYMBOL_TYPE (symbol); | |
b026f593 | 253 | struct mdict_iterator miter; |
c906108c SS |
254 | SYMBOL_BLOCK_VALUE (symbol) = block; |
255 | BLOCK_FUNCTION (block) = symbol; | |
256 | ||
1f704f76 | 257 | if (ftype->num_fields () <= 0) |
c906108c SS |
258 | { |
259 | /* No parameter type information is recorded with the | |
260 | function's type. Set that from the type of the | |
4a64f543 | 261 | parameter symbols. */ |
c906108c SS |
262 | int nparams = 0, iparams; |
263 | struct symbol *sym; | |
8157b174 TT |
264 | |
265 | /* Here we want to directly access the dictionary, because | |
266 | we haven't fully initialized the block yet. */ | |
b026f593 | 267 | ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym) |
c906108c | 268 | { |
2a2d4dc3 AS |
269 | if (SYMBOL_IS_ARGUMENT (sym)) |
270 | nparams++; | |
c906108c SS |
271 | } |
272 | if (nparams > 0) | |
273 | { | |
5e33d5f4 | 274 | ftype->set_num_fields (nparams); |
3cabb6b0 SM |
275 | ftype->set_fields |
276 | ((struct field *) | |
277 | TYPE_ALLOC (ftype, nparams * sizeof (struct field))); | |
c906108c | 278 | |
de4f826b | 279 | iparams = 0; |
8157b174 TT |
280 | /* Here we want to directly access the dictionary, because |
281 | we haven't fully initialized the block yet. */ | |
b026f593 | 282 | ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym) |
c906108c | 283 | { |
de4f826b DC |
284 | if (iparams == nparams) |
285 | break; | |
286 | ||
2a2d4dc3 | 287 | if (SYMBOL_IS_ARGUMENT (sym)) |
c906108c | 288 | { |
5d14b6e5 | 289 | ftype->field (iparams).set_type (SYMBOL_TYPE (sym)); |
8176bb6d | 290 | TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0; |
c906108c | 291 | iparams++; |
c906108c SS |
292 | } |
293 | } | |
294 | } | |
295 | } | |
296 | } | |
297 | else | |
298 | { | |
299 | BLOCK_FUNCTION (block) = NULL; | |
300 | } | |
301 | ||
63e43d3a | 302 | if (static_link != NULL) |
cbb09508 | 303 | objfile_register_static_link (m_objfile, block, static_link); |
63e43d3a | 304 | |
1d376700 | 305 | /* Now free the links of the list, and empty the list. */ |
c906108c SS |
306 | |
307 | for (next = *listhead; next; next = next1) | |
308 | { | |
309 | next1 = next->next; | |
1d376700 | 310 | xfree (next); |
c906108c SS |
311 | } |
312 | *listhead = NULL; | |
313 | ||
c906108c | 314 | /* Check to be sure that the blocks have an end address that is |
4a64f543 | 315 | greater than starting address. */ |
c906108c SS |
316 | |
317 | if (BLOCK_END (block) < BLOCK_START (block)) | |
318 | { | |
319 | if (symbol) | |
320 | { | |
b98664d3 | 321 | complaint (_("block end address less than block " |
3e43a32a | 322 | "start address in %s (patched it)"), |
987012b8 | 323 | symbol->print_name ()); |
c906108c SS |
324 | } |
325 | else | |
326 | { | |
b98664d3 | 327 | complaint (_("block end address %s less than block " |
3e43a32a | 328 | "start address %s (patched it)"), |
5af949e3 UW |
329 | paddress (gdbarch, BLOCK_END (block)), |
330 | paddress (gdbarch, BLOCK_START (block))); | |
c906108c | 331 | } |
4a64f543 | 332 | /* Better than nothing. */ |
c906108c SS |
333 | BLOCK_END (block) = BLOCK_START (block); |
334 | } | |
c906108c SS |
335 | |
336 | /* Install this block as the superblock of all blocks made since the | |
337 | start of this scope that don't have superblocks yet. */ | |
338 | ||
339 | opblock = NULL; | |
4a2125f5 | 340 | for (pblock = m_pending_blocks; |
c0219d42 MS |
341 | pblock && pblock != old_blocks; |
342 | pblock = pblock->next) | |
c906108c SS |
343 | { |
344 | if (BLOCK_SUPERBLOCK (pblock->block) == NULL) | |
345 | { | |
c906108c | 346 | /* Check to be sure the blocks are nested as we receive |
4a64f543 | 347 | them. If the compiler/assembler/linker work, this just |
14711c82 DJ |
348 | burns a small amount of time. |
349 | ||
350 | Skip blocks which correspond to a function; they're not | |
351 | physically nested inside this other blocks, only | |
352 | lexically nested. */ | |
353 | if (BLOCK_FUNCTION (pblock->block) == NULL | |
354 | && (BLOCK_START (pblock->block) < BLOCK_START (block) | |
355 | || BLOCK_END (pblock->block) > BLOCK_END (block))) | |
c906108c SS |
356 | { |
357 | if (symbol) | |
358 | { | |
b98664d3 | 359 | complaint (_("inner block not inside outer block in %s"), |
987012b8 | 360 | symbol->print_name ()); |
c906108c SS |
361 | } |
362 | else | |
363 | { | |
b98664d3 | 364 | complaint (_("inner block (%s-%s) not " |
3e43a32a | 365 | "inside outer block (%s-%s)"), |
5af949e3 UW |
366 | paddress (gdbarch, BLOCK_START (pblock->block)), |
367 | paddress (gdbarch, BLOCK_END (pblock->block)), | |
368 | paddress (gdbarch, BLOCK_START (block)), | |
369 | paddress (gdbarch, BLOCK_END (block))); | |
c906108c SS |
370 | } |
371 | if (BLOCK_START (pblock->block) < BLOCK_START (block)) | |
372 | BLOCK_START (pblock->block) = BLOCK_START (block); | |
373 | if (BLOCK_END (pblock->block) > BLOCK_END (block)) | |
374 | BLOCK_END (pblock->block) = BLOCK_END (block); | |
375 | } | |
c906108c SS |
376 | BLOCK_SUPERBLOCK (pblock->block) = block; |
377 | } | |
378 | opblock = pblock; | |
379 | } | |
380 | ||
22cee43f PMR |
381 | block_set_using (block, |
382 | (is_global | |
4a2125f5 TT |
383 | ? m_global_using_directives |
384 | : m_local_using_directives), | |
cbb09508 | 385 | &m_objfile->objfile_obstack); |
22cee43f | 386 | if (is_global) |
4a2125f5 | 387 | m_global_using_directives = NULL; |
22cee43f | 388 | else |
4a2125f5 | 389 | m_local_using_directives = NULL; |
27aa8d6a | 390 | |
6b213a47 | 391 | record_pending_block (block, opblock); |
801e3a5b JB |
392 | |
393 | return block; | |
c906108c SS |
394 | } |
395 | ||
84a146c9 | 396 | struct block * |
4a2125f5 TT |
397 | buildsym_compunit::finish_block (struct symbol *symbol, |
398 | struct pending_block *old_blocks, | |
399 | const struct dynamic_prop *static_link, | |
400 | CORE_ADDR start, CORE_ADDR end) | |
84a146c9 | 401 | { |
4a2125f5 TT |
402 | return finish_block_internal (symbol, &m_local_symbols, |
403 | old_blocks, static_link, start, end, 0, 0); | |
84a146c9 | 404 | } |
de4f826b | 405 | |
801e3a5b JB |
406 | /* Record that the range of addresses from START to END_INCLUSIVE |
407 | (inclusive, like it says) belongs to BLOCK. BLOCK's start and end | |
408 | addresses must be set already. You must apply this function to all | |
409 | BLOCK's children before applying it to BLOCK. | |
410 | ||
411 | If a call to this function complicates the picture beyond that | |
412 | already provided by BLOCK_START and BLOCK_END, then we create an | |
413 | address map for the block. */ | |
414 | void | |
4a2125f5 TT |
415 | buildsym_compunit::record_block_range (struct block *block, |
416 | CORE_ADDR start, | |
417 | CORE_ADDR end_inclusive) | |
801e3a5b JB |
418 | { |
419 | /* If this is any different from the range recorded in the block's | |
420 | own BLOCK_START and BLOCK_END, then note that the address map has | |
421 | become interesting. Note that even if this block doesn't have | |
422 | any "interesting" ranges, some later block might, so we still | |
423 | need to record this block in the addrmap. */ | |
424 | if (start != BLOCK_START (block) | |
425 | || end_inclusive + 1 != BLOCK_END (block)) | |
4a2125f5 | 426 | m_pending_addrmap_interesting = true; |
801e3a5b | 427 | |
4a2125f5 TT |
428 | if (m_pending_addrmap == nullptr) |
429 | m_pending_addrmap = addrmap_create_mutable (&m_pending_addrmap_obstack); | |
801e3a5b | 430 | |
4a2125f5 | 431 | addrmap_set_empty (m_pending_addrmap, start, end_inclusive, block); |
801e3a5b JB |
432 | } |
433 | ||
4a2125f5 TT |
434 | struct blockvector * |
435 | buildsym_compunit::make_blockvector () | |
c906108c | 436 | { |
52f0bd74 AC |
437 | struct pending_block *next; |
438 | struct blockvector *blockvector; | |
439 | int i; | |
c906108c SS |
440 | |
441 | /* Count the length of the list of blocks. */ | |
442 | ||
4a2125f5 | 443 | for (next = m_pending_blocks, i = 0; next; next = next->next, i++) |
5ac04550 | 444 | { |
c906108c SS |
445 | } |
446 | ||
447 | blockvector = (struct blockvector *) | |
cbb09508 | 448 | obstack_alloc (&m_objfile->objfile_obstack, |
c906108c SS |
449 | (sizeof (struct blockvector) |
450 | + (i - 1) * sizeof (struct block *))); | |
451 | ||
4a64f543 | 452 | /* Copy the blocks into the blockvector. This is done in reverse |
c906108c | 453 | order, which happens to put the blocks into the proper order |
4a64f543 | 454 | (ascending starting address). finish_block has hair to insert |
c906108c SS |
455 | each block into the list after its subblocks in order to make |
456 | sure this is true. */ | |
457 | ||
458 | BLOCKVECTOR_NBLOCKS (blockvector) = i; | |
4a2125f5 | 459 | for (next = m_pending_blocks; next; next = next->next) |
c906108c SS |
460 | { |
461 | BLOCKVECTOR_BLOCK (blockvector, --i) = next->block; | |
462 | } | |
463 | ||
4a2125f5 | 464 | free_pending_blocks (); |
c906108c | 465 | |
801e3a5b JB |
466 | /* If we needed an address map for this symtab, record it in the |
467 | blockvector. */ | |
4a2125f5 | 468 | if (m_pending_addrmap != nullptr && m_pending_addrmap_interesting) |
801e3a5b | 469 | BLOCKVECTOR_MAP (blockvector) |
cbb09508 | 470 | = addrmap_create_fixed (m_pending_addrmap, &m_objfile->objfile_obstack); |
801e3a5b JB |
471 | else |
472 | BLOCKVECTOR_MAP (blockvector) = 0; | |
4aad0dfc | 473 | |
c906108c | 474 | /* Some compilers output blocks in the wrong order, but we depend on |
4a64f543 | 475 | their being in the right order so we can binary search. Check the |
4aad0dfc DE |
476 | order and moan about it. |
477 | Note: Remember that the first two blocks are the global and static | |
478 | blocks. We could special case that fact and begin checking at block 2. | |
479 | To avoid making that assumption we do not. */ | |
c906108c SS |
480 | if (BLOCKVECTOR_NBLOCKS (blockvector) > 1) |
481 | { | |
482 | for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++) | |
483 | { | |
484 | if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1)) | |
485 | > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i))) | |
486 | { | |
59527da0 JB |
487 | CORE_ADDR start |
488 | = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)); | |
c906108c | 489 | |
b98664d3 | 490 | complaint (_("block at %s out of order"), |
bb599908 | 491 | hex_string ((LONGEST) start)); |
c906108c SS |
492 | } |
493 | } | |
494 | } | |
c906108c SS |
495 | |
496 | return (blockvector); | |
497 | } | |
498 | \f | |
499 | /* Start recording information about source code that came from an | |
500 | included (or otherwise merged-in) source file with a different | |
4d663531 | 501 | name. NAME is the name of the file (cannot be NULL). */ |
c906108c SS |
502 | |
503 | void | |
4a2125f5 | 504 | buildsym_compunit::start_subfile (const char *name) |
c906108c | 505 | { |
43f3e411 | 506 | const char *subfile_dirname; |
52f0bd74 | 507 | struct subfile *subfile; |
c906108c | 508 | |
cbb09508 | 509 | subfile_dirname = m_comp_dir.get (); |
c906108c | 510 | |
43f3e411 DE |
511 | /* See if this subfile is already registered. */ |
512 | ||
cbb09508 | 513 | for (subfile = m_subfiles; subfile; subfile = subfile->next) |
c906108c | 514 | { |
84ba0adf DJ |
515 | char *subfile_name; |
516 | ||
517 | /* If NAME is an absolute path, and this subfile is not, then | |
518 | attempt to create an absolute path to compare. */ | |
519 | if (IS_ABSOLUTE_PATH (name) | |
520 | && !IS_ABSOLUTE_PATH (subfile->name) | |
43f3e411 DE |
521 | && subfile_dirname != NULL) |
522 | subfile_name = concat (subfile_dirname, SLASH_STRING, | |
6eb7ee03 | 523 | subfile->name, (char *) NULL); |
84ba0adf DJ |
524 | else |
525 | subfile_name = subfile->name; | |
526 | ||
527 | if (FILENAME_CMP (subfile_name, name) == 0) | |
c906108c | 528 | { |
4a2125f5 | 529 | m_current_subfile = subfile; |
84ba0adf DJ |
530 | if (subfile_name != subfile->name) |
531 | xfree (subfile_name); | |
c906108c SS |
532 | return; |
533 | } | |
84ba0adf DJ |
534 | if (subfile_name != subfile->name) |
535 | xfree (subfile_name); | |
c906108c SS |
536 | } |
537 | ||
43f3e411 | 538 | /* This subfile is not known. Add an entry for it. */ |
c906108c | 539 | |
8d749320 | 540 | subfile = XNEW (struct subfile); |
43f3e411 | 541 | memset (subfile, 0, sizeof (struct subfile)); |
4a2125f5 | 542 | subfile->buildsym_compunit = this; |
43f3e411 | 543 | |
cbb09508 KS |
544 | subfile->next = m_subfiles; |
545 | m_subfiles = subfile; | |
43f3e411 | 546 | |
4a2125f5 | 547 | m_current_subfile = subfile; |
c906108c | 548 | |
b74db436 | 549 | subfile->name = xstrdup (name); |
c906108c SS |
550 | |
551 | /* Initialize line-number recording for this subfile. */ | |
552 | subfile->line_vector = NULL; | |
553 | ||
554 | /* Default the source language to whatever can be deduced from the | |
555 | filename. If nothing can be deduced (such as for a C/C++ include | |
556 | file with a ".h" extension), then inherit whatever language the | |
557 | previous subfile had. This kludgery is necessary because there | |
558 | is no standard way in some object formats to record the source | |
559 | language. Also, when symtabs are allocated we try to deduce a | |
560 | language then as well, but it is too late for us to use that | |
561 | information while reading symbols, since symtabs aren't allocated | |
562 | until after all the symbols have been processed for a given | |
4a64f543 | 563 | source file. */ |
c906108c SS |
564 | |
565 | subfile->language = deduce_language_from_filename (subfile->name); | |
5aafa1cc PM |
566 | if (subfile->language == language_unknown |
567 | && subfile->next != NULL) | |
c906108c SS |
568 | { |
569 | subfile->language = subfile->next->language; | |
570 | } | |
571 | ||
25caa7a8 | 572 | /* If the filename of this subfile ends in .C, then change the |
c906108c | 573 | language of any pending subfiles from C to C++. We also accept |
25caa7a8 | 574 | any other C++ suffixes accepted by deduce_language_from_filename. */ |
c906108c SS |
575 | /* Likewise for f2c. */ |
576 | ||
577 | if (subfile->name) | |
578 | { | |
579 | struct subfile *s; | |
580 | enum language sublang = deduce_language_from_filename (subfile->name); | |
581 | ||
582 | if (sublang == language_cplus || sublang == language_fortran) | |
cbb09508 | 583 | for (s = m_subfiles; s != NULL; s = s->next) |
c906108c SS |
584 | if (s->language == language_c) |
585 | s->language = sublang; | |
586 | } | |
587 | ||
588 | /* And patch up this file if necessary. */ | |
589 | if (subfile->language == language_c | |
590 | && subfile->next != NULL | |
591 | && (subfile->next->language == language_cplus | |
592 | || subfile->next->language == language_fortran)) | |
593 | { | |
594 | subfile->language = subfile->next->language; | |
595 | } | |
596 | } | |
597 | ||
598 | /* For stabs readers, the first N_SO symbol is assumed to be the | |
599 | source file name, and the subfile struct is initialized using that | |
600 | assumption. If another N_SO symbol is later seen, immediately | |
601 | following the first one, then the first one is assumed to be the | |
602 | directory name and the second one is really the source file name. | |
603 | ||
604 | So we have to patch up the subfile struct by moving the old name | |
605 | value to dirname and remembering the new name. Some sanity | |
606 | checking is performed to ensure that the state of the subfile | |
607 | struct is reasonable and that the old name we are assuming to be a | |
4a64f543 | 608 | directory name actually is (by checking for a trailing '/'). */ |
c906108c SS |
609 | |
610 | void | |
4a2125f5 TT |
611 | buildsym_compunit::patch_subfile_names (struct subfile *subfile, |
612 | const char *name) | |
c906108c | 613 | { |
43f3e411 | 614 | if (subfile != NULL |
cbb09508 | 615 | && m_comp_dir == NULL |
43f3e411 | 616 | && subfile->name != NULL |
0ba1096a | 617 | && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1])) |
c906108c | 618 | { |
cbb09508 | 619 | m_comp_dir.reset (subfile->name); |
1b36a34b | 620 | subfile->name = xstrdup (name); |
46212e0b | 621 | set_last_source_file (name); |
c906108c SS |
622 | |
623 | /* Default the source language to whatever can be deduced from | |
dda83cd7 SM |
624 | the filename. If nothing can be deduced (such as for a C/C++ |
625 | include file with a ".h" extension), then inherit whatever | |
626 | language the previous subfile had. This kludgery is | |
627 | necessary because there is no standard way in some object | |
628 | formats to record the source language. Also, when symtabs | |
629 | are allocated we try to deduce a language then as well, but | |
630 | it is too late for us to use that information while reading | |
631 | symbols, since symtabs aren't allocated until after all the | |
632 | symbols have been processed for a given 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 | } | |
642 | \f | |
643 | /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for | |
644 | switching source files (different subfiles, as we call them) within | |
645 | one object file, but using a stack rather than in an arbitrary | |
646 | order. */ | |
647 | ||
648 | void | |
4a2125f5 | 649 | buildsym_compunit::push_subfile () |
c906108c | 650 | { |
4a2125f5 TT |
651 | gdb_assert (m_current_subfile != NULL); |
652 | gdb_assert (m_current_subfile->name != NULL); | |
653 | m_subfile_stack.push_back (m_current_subfile->name); | |
c906108c SS |
654 | } |
655 | ||
8419ee53 | 656 | const char * |
4a2125f5 | 657 | buildsym_compunit::pop_subfile () |
c906108c | 658 | { |
4a2125f5 TT |
659 | gdb_assert (!m_subfile_stack.empty ()); |
660 | const char *name = m_subfile_stack.back (); | |
661 | m_subfile_stack.pop_back (); | |
8419ee53 | 662 | return name; |
c906108c SS |
663 | } |
664 | \f | |
665 | /* Add a linetable entry for line number LINE and address PC to the | |
666 | line vector for SUBFILE. */ | |
667 | ||
668 | void | |
4a2125f5 | 669 | buildsym_compunit::record_line (struct subfile *subfile, int line, |
8c95582d | 670 | CORE_ADDR pc, bool is_stmt) |
c906108c SS |
671 | { |
672 | struct linetable_entry *e; | |
c906108c | 673 | |
c906108c SS |
674 | /* Make sure line vector exists and is big enough. */ |
675 | if (!subfile->line_vector) | |
676 | { | |
677 | subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH; | |
678 | subfile->line_vector = (struct linetable *) | |
679 | xmalloc (sizeof (struct linetable) | |
c5aa993b | 680 | + subfile->line_vector_length * sizeof (struct linetable_entry)); |
c906108c | 681 | subfile->line_vector->nitems = 0; |
4a2125f5 | 682 | m_have_line_numbers = true; |
c906108c SS |
683 | } |
684 | ||
bbe3dc41 | 685 | if (subfile->line_vector->nitems >= subfile->line_vector_length) |
c906108c SS |
686 | { |
687 | subfile->line_vector_length *= 2; | |
688 | subfile->line_vector = (struct linetable *) | |
689 | xrealloc ((char *) subfile->line_vector, | |
690 | (sizeof (struct linetable) | |
691 | + (subfile->line_vector_length | |
692 | * sizeof (struct linetable_entry)))); | |
693 | } | |
694 | ||
a25198bb BE |
695 | /* Normally, we treat lines as unsorted. But the end of sequence |
696 | marker is special. We sort line markers at the same PC by line | |
697 | number, so end of sequence markers (which have line == 0) appear | |
698 | first. This is right if the marker ends the previous function, | |
699 | and there is no padding before the next function. But it is | |
700 | wrong if the previous line was empty and we are now marking a | |
701 | switch to a different subfile. We must leave the end of sequence | |
702 | marker at the end of this group of lines, not sort the empty line | |
703 | to after the marker. The easiest way to accomplish this is to | |
704 | delete any empty lines from our table, if they are followed by | |
705 | end of sequence markers. All we lose is the ability to set | |
706 | breakpoints at some lines which contain no instructions | |
707 | anyway. */ | |
708 | if (line == 0) | |
607ae575 | 709 | { |
e4ad960a | 710 | struct linetable_entry *last = nullptr; |
a25198bb | 711 | while (subfile->line_vector->nitems > 0) |
607ae575 | 712 | { |
e4ad960a TV |
713 | last = subfile->line_vector->item + subfile->line_vector->nitems - 1; |
714 | if (last->pc != pc) | |
64dc2d4b | 715 | break; |
a25198bb | 716 | subfile->line_vector->nitems--; |
607ae575 | 717 | } |
e4ad960a TV |
718 | |
719 | /* Ignore an end-of-sequence marker marking an empty sequence. */ | |
720 | if (last == nullptr || last->line == 0) | |
721 | return; | |
607ae575 DJ |
722 | } |
723 | ||
c906108c SS |
724 | e = subfile->line_vector->item + subfile->line_vector->nitems++; |
725 | e->line = line; | |
8c95582d | 726 | e->is_stmt = is_stmt ? 1 : 0; |
607ae575 | 727 | e->pc = pc; |
c906108c SS |
728 | } |
729 | ||
c906108c | 730 | \f |
4a64f543 MS |
731 | /* Subroutine of end_symtab to simplify it. Look for a subfile that |
732 | matches the main source file's basename. If there is only one, and | |
733 | if the main source file doesn't have any symbol or line number | |
734 | information, then copy this file's symtab and line_vector to the | |
735 | main source file's subfile and discard the other subfile. This can | |
736 | happen because of a compiler bug or from the user playing games | |
737 | with #line or from things like a distributed build system that | |
43f3e411 DE |
738 | manipulates the debug info. This can also happen from an innocent |
739 | symlink in the paths, we don't canonicalize paths here. */ | |
4584e32e | 740 | |
4a2125f5 TT |
741 | void |
742 | buildsym_compunit::watch_main_source_file_lossage () | |
4584e32e | 743 | { |
43f3e411 | 744 | struct subfile *mainsub, *subfile; |
4584e32e | 745 | |
43f3e411 | 746 | /* Get the main source file. */ |
cbb09508 | 747 | mainsub = m_main_subfile; |
43f3e411 | 748 | |
4a64f543 | 749 | /* If the main source file doesn't have any line number or symbol |
7bab9b58 | 750 | info, look for an alias in another subfile. */ |
4584e32e | 751 | |
43f3e411 DE |
752 | if (mainsub->line_vector == NULL |
753 | && mainsub->symtab == NULL) | |
4584e32e | 754 | { |
43f3e411 | 755 | const char *mainbase = lbasename (mainsub->name); |
4584e32e DE |
756 | int nr_matches = 0; |
757 | struct subfile *prevsub; | |
758 | struct subfile *mainsub_alias = NULL; | |
759 | struct subfile *prev_mainsub_alias = NULL; | |
760 | ||
761 | prevsub = NULL; | |
cbb09508 | 762 | for (subfile = m_subfiles; |
43f3e411 | 763 | subfile != NULL; |
4584e32e DE |
764 | subfile = subfile->next) |
765 | { | |
43f3e411 DE |
766 | if (subfile == mainsub) |
767 | continue; | |
0ba1096a | 768 | if (filename_cmp (lbasename (subfile->name), mainbase) == 0) |
4584e32e DE |
769 | { |
770 | ++nr_matches; | |
771 | mainsub_alias = subfile; | |
772 | prev_mainsub_alias = prevsub; | |
773 | } | |
774 | prevsub = subfile; | |
775 | } | |
776 | ||
777 | if (nr_matches == 1) | |
778 | { | |
43f3e411 | 779 | gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub); |
4584e32e DE |
780 | |
781 | /* Found a match for the main source file. | |
782 | Copy its line_vector and symtab to the main subfile | |
783 | and then discard it. */ | |
784 | ||
43f3e411 DE |
785 | mainsub->line_vector = mainsub_alias->line_vector; |
786 | mainsub->line_vector_length = mainsub_alias->line_vector_length; | |
787 | mainsub->symtab = mainsub_alias->symtab; | |
4584e32e DE |
788 | |
789 | if (prev_mainsub_alias == NULL) | |
cbb09508 | 790 | m_subfiles = mainsub_alias->next; |
4584e32e DE |
791 | else |
792 | prev_mainsub_alias->next = mainsub_alias->next; | |
98387a29 | 793 | xfree (mainsub_alias->name); |
4584e32e DE |
794 | xfree (mainsub_alias); |
795 | } | |
796 | } | |
797 | } | |
798 | ||
4359dff1 JK |
799 | /* Implementation of the first part of end_symtab. It allows modifying |
800 | STATIC_BLOCK before it gets finalized by end_symtab_from_static_block. | |
801 | If the returned value is NULL there is no blockvector created for | |
802 | this symtab (you still must call end_symtab_from_static_block). | |
c906108c | 803 | |
4359dff1 JK |
804 | END_ADDR is the same as for end_symtab: the address of the end of the |
805 | file's text. | |
c906108c | 806 | |
4359dff1 | 807 | If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made |
36586728 TT |
808 | expandable. |
809 | ||
810 | If REQUIRED is non-zero, then a symtab is created even if it does | |
811 | not contain any symbols. */ | |
6d30eef8 | 812 | |
4359dff1 | 813 | struct block * |
4a2125f5 TT |
814 | buildsym_compunit::end_symtab_get_static_block (CORE_ADDR end_addr, |
815 | int expandable, int required) | |
c906108c | 816 | { |
c906108c SS |
817 | /* Finish the lexical context of the last function in the file; pop |
818 | the context stack. */ | |
819 | ||
4a2125f5 | 820 | if (!m_context_stack.empty ()) |
c906108c | 821 | { |
a60f3166 | 822 | struct context_stack cstk = pop_context (); |
4359dff1 | 823 | |
c906108c | 824 | /* Make a block for the local symbols within. */ |
c233e9c6 | 825 | finish_block (cstk.name, cstk.old_blocks, NULL, |
a60f3166 | 826 | cstk.start_addr, end_addr); |
c906108c | 827 | |
4a2125f5 | 828 | if (!m_context_stack.empty ()) |
c906108c SS |
829 | { |
830 | /* This is said to happen with SCO. The old coffread.c | |
831 | code simply emptied the context stack, so we do the | |
832 | same. FIXME: Find out why it is happening. This is not | |
833 | believed to happen in most cases (even for coffread.c); | |
834 | it used to be an abort(). */ | |
b98664d3 | 835 | complaint (_("Context stack not empty in end_symtab")); |
4a2125f5 | 836 | m_context_stack.clear (); |
c906108c SS |
837 | } |
838 | } | |
839 | ||
840 | /* Reordered executables may have out of order pending blocks; if | |
841 | OBJF_REORDERED is true, then sort the pending blocks. */ | |
6d30eef8 | 842 | |
cbb09508 | 843 | if ((m_objfile->flags & OBJF_REORDERED) && m_pending_blocks) |
c906108c | 844 | { |
07e7f39f | 845 | struct pending_block *pb; |
c906108c | 846 | |
b05628f0 | 847 | std::vector<block *> barray; |
c906108c | 848 | |
4a2125f5 | 849 | for (pb = m_pending_blocks; pb != NULL; pb = pb->next) |
b05628f0 | 850 | barray.push_back (pb->block); |
07e7f39f | 851 | |
5033013f UW |
852 | /* Sort blocks by start address in descending order. Blocks with the |
853 | same start address must remain in the original order to preserve | |
854 | inline function caller/callee relationships. */ | |
855 | std::stable_sort (barray.begin (), barray.end (), | |
856 | [] (const block *a, const block *b) | |
857 | { | |
858 | return BLOCK_START (a) > BLOCK_START (b); | |
859 | }); | |
07e7f39f | 860 | |
b05628f0 | 861 | int i = 0; |
4a2125f5 | 862 | for (pb = m_pending_blocks; pb != NULL; pb = pb->next) |
b05628f0 | 863 | pb->block = barray[i++]; |
c906108c SS |
864 | } |
865 | ||
866 | /* Cleanup any undefined types that have been left hanging around | |
867 | (this needs to be done before the finish_blocks so that | |
868 | file_symbols is still good). | |
c5aa993b | 869 | |
0a0edcd5 | 870 | Both cleanup_undefined_stabs_types and finish_global_stabs are stabs |
c906108c SS |
871 | specific, but harmless for other symbol readers, since on gdb |
872 | startup or when finished reading stabs, the state is set so these | |
873 | are no-ops. FIXME: Is this handled right in case of QUIT? Can | |
874 | we make this cleaner? */ | |
875 | ||
cbb09508 KS |
876 | cleanup_undefined_stabs_types (m_objfile); |
877 | finish_global_stabs (m_objfile); | |
c906108c | 878 | |
36586728 | 879 | if (!required |
4a2125f5 TT |
880 | && m_pending_blocks == NULL |
881 | && m_file_symbols == NULL | |
882 | && m_global_symbols == NULL | |
883 | && !m_have_line_numbers | |
884 | && m_pending_macros == NULL | |
885 | && m_global_using_directives == NULL) | |
c906108c | 886 | { |
4359dff1 JK |
887 | /* Ignore symtabs that have no functions with real debugging info. */ |
888 | return NULL; | |
889 | } | |
890 | else | |
891 | { | |
892 | /* Define the STATIC_BLOCK. */ | |
e148f09d | 893 | return finish_block_internal (NULL, get_file_symbols (), NULL, NULL, |
4a2125f5 | 894 | m_last_source_start_addr, |
2c99ee5c | 895 | end_addr, 0, expandable); |
4359dff1 JK |
896 | } |
897 | } | |
898 | ||
7bab9b58 DE |
899 | /* Subroutine of end_symtab_from_static_block to simplify it. |
900 | Handle the "have blockvector" case. | |
901 | See end_symtab_from_static_block for a description of the arguments. */ | |
902 | ||
4a2125f5 TT |
903 | struct compunit_symtab * |
904 | buildsym_compunit::end_symtab_with_blockvector (struct block *static_block, | |
905 | int section, int expandable) | |
4359dff1 | 906 | { |
cbb09508 | 907 | struct compunit_symtab *cu = m_compunit_symtab; |
4359dff1 JK |
908 | struct blockvector *blockvector; |
909 | struct subfile *subfile; | |
7bab9b58 | 910 | CORE_ADDR end_addr; |
4359dff1 | 911 | |
7bab9b58 | 912 | gdb_assert (static_block != NULL); |
cbb09508 | 913 | gdb_assert (m_subfiles != NULL); |
7bab9b58 DE |
914 | |
915 | end_addr = BLOCK_END (static_block); | |
916 | ||
917 | /* Create the GLOBAL_BLOCK and build the blockvector. */ | |
e148f09d | 918 | finish_block_internal (NULL, get_global_symbols (), NULL, NULL, |
4a2125f5 | 919 | m_last_source_start_addr, end_addr, |
7bab9b58 | 920 | 1, expandable); |
43f3e411 | 921 | blockvector = make_blockvector (); |
c906108c | 922 | |
f56ce883 DE |
923 | /* Read the line table if it has to be read separately. |
924 | This is only used by xcoffread.c. */ | |
cbb09508 KS |
925 | if (m_objfile->sf->sym_read_linetable != NULL) |
926 | m_objfile->sf->sym_read_linetable (m_objfile); | |
c906108c | 927 | |
4584e32e DE |
928 | /* Handle the case where the debug info specifies a different path |
929 | for the main source file. It can cause us to lose track of its | |
930 | line number information. */ | |
931 | watch_main_source_file_lossage (); | |
932 | ||
43f3e411 DE |
933 | /* Now create the symtab objects proper, if not already done, |
934 | one for each subfile. */ | |
c906108c | 935 | |
cbb09508 | 936 | for (subfile = m_subfiles; |
43f3e411 DE |
937 | subfile != NULL; |
938 | subfile = subfile->next) | |
c906108c SS |
939 | { |
940 | int linetablesize = 0; | |
c906108c | 941 | |
7bab9b58 | 942 | if (subfile->line_vector) |
c906108c | 943 | { |
7bab9b58 DE |
944 | linetablesize = sizeof (struct linetable) + |
945 | subfile->line_vector->nitems * sizeof (struct linetable_entry); | |
946 | ||
3d92a3e3 AB |
947 | const auto lte_is_less_than |
948 | = [] (const linetable_entry &ln1, | |
949 | const linetable_entry &ln2) -> bool | |
950 | { | |
d8cc8af6 TV |
951 | if (ln1.pc == ln2.pc |
952 | && ((ln1.line == 0) != (ln2.line == 0))) | |
953 | return ln1.line == 0; | |
954 | ||
3d92a3e3 AB |
955 | return (ln1.pc < ln2.pc); |
956 | }; | |
957 | ||
958 | /* Like the pending blocks, the line table may be scrambled in | |
959 | reordered executables. Sort it if OBJF_REORDERED is true. It | |
960 | is important to preserve the order of lines at the same | |
961 | address, as this maintains the inline function caller/callee | |
962 | relationships, this is why std::stable_sort is used. */ | |
cbb09508 | 963 | if (m_objfile->flags & OBJF_REORDERED) |
3d92a3e3 AB |
964 | std::stable_sort (subfile->line_vector->item, |
965 | subfile->line_vector->item | |
966 | + subfile->line_vector->nitems, | |
967 | lte_is_less_than); | |
7bab9b58 | 968 | } |
9182c5bc | 969 | |
7bab9b58 DE |
970 | /* Allocate a symbol table if necessary. */ |
971 | if (subfile->symtab == NULL) | |
43f3e411 | 972 | subfile->symtab = allocate_symtab (cu, subfile->name); |
5accd1a0 | 973 | struct symtab *symtab = subfile->symtab; |
9182c5bc | 974 | |
7bab9b58 | 975 | /* Fill in its components. */ |
43f3e411 | 976 | |
7bab9b58 DE |
977 | if (subfile->line_vector) |
978 | { | |
979 | /* Reallocate the line table on the symbol obstack. */ | |
8435453b | 980 | SYMTAB_LINETABLE (symtab) = (struct linetable *) |
cbb09508 | 981 | obstack_alloc (&m_objfile->objfile_obstack, linetablesize); |
8435453b DE |
982 | memcpy (SYMTAB_LINETABLE (symtab), subfile->line_vector, |
983 | linetablesize); | |
c906108c | 984 | } |
24be086d | 985 | else |
c906108c | 986 | { |
8435453b | 987 | SYMTAB_LINETABLE (symtab) = NULL; |
c906108c | 988 | } |
c906108c | 989 | |
7bab9b58 DE |
990 | /* Use whatever language we have been using for this |
991 | subfile, not the one that was deduced in allocate_symtab | |
992 | from the filename. We already did our own deducing when | |
993 | we created the subfile, and we may have altered our | |
994 | opinion of what language it is from things we found in | |
995 | the symbols. */ | |
996 | symtab->language = subfile->language; | |
43f3e411 | 997 | } |
c906108c | 998 | |
43f3e411 DE |
999 | /* Make sure the symtab of main_subfile is the first in its list. */ |
1000 | { | |
1001 | struct symtab *main_symtab, *prev_symtab; | |
1002 | ||
cbb09508 | 1003 | main_symtab = m_main_subfile->symtab; |
43f3e411 | 1004 | prev_symtab = NULL; |
5accd1a0 | 1005 | for (symtab *symtab : compunit_filetabs (cu)) |
43f3e411 DE |
1006 | { |
1007 | if (symtab == main_symtab) | |
1008 | { | |
1009 | if (prev_symtab != NULL) | |
1010 | { | |
1011 | prev_symtab->next = main_symtab->next; | |
1012 | main_symtab->next = COMPUNIT_FILETABS (cu); | |
1013 | COMPUNIT_FILETABS (cu) = main_symtab; | |
1014 | } | |
1015 | break; | |
1016 | } | |
1017 | prev_symtab = symtab; | |
1018 | } | |
1019 | gdb_assert (main_symtab == COMPUNIT_FILETABS (cu)); | |
1020 | } | |
84a146c9 | 1021 | |
0ab9ce85 | 1022 | /* Fill out the compunit symtab. */ |
84a146c9 | 1023 | |
cbb09508 | 1024 | if (m_comp_dir != NULL) |
43f3e411 DE |
1025 | { |
1026 | /* Reallocate the dirname on the symbol obstack. */ | |
cbb09508 | 1027 | const char *comp_dir = m_comp_dir.get (); |
021887d8 TT |
1028 | COMPUNIT_DIRNAME (cu) = obstack_strdup (&m_objfile->objfile_obstack, |
1029 | comp_dir); | |
c906108c SS |
1030 | } |
1031 | ||
43f3e411 | 1032 | /* Save the debug format string (if any) in the symtab. */ |
cbb09508 | 1033 | COMPUNIT_DEBUGFORMAT (cu) = m_debugformat; |
43f3e411 DE |
1034 | |
1035 | /* Similarly for the producer. */ | |
cbb09508 | 1036 | COMPUNIT_PRODUCER (cu) = m_producer; |
43f3e411 DE |
1037 | |
1038 | COMPUNIT_BLOCKVECTOR (cu) = blockvector; | |
7bab9b58 | 1039 | { |
43f3e411 | 1040 | struct block *b = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK); |
cb1df416 | 1041 | |
43f3e411 | 1042 | set_block_compunit_symtab (b, cu); |
7bab9b58 | 1043 | } |
cb1df416 | 1044 | |
43f3e411 DE |
1045 | COMPUNIT_BLOCK_LINE_SECTION (cu) = section; |
1046 | ||
4a2125f5 | 1047 | COMPUNIT_MACRO_TABLE (cu) = release_macros (); |
43f3e411 | 1048 | |
7bab9b58 DE |
1049 | /* Default any symbols without a specified symtab to the primary symtab. */ |
1050 | { | |
1051 | int block_i; | |
1052 | ||
43f3e411 | 1053 | /* The main source file's symtab. */ |
5accd1a0 | 1054 | struct symtab *symtab = COMPUNIT_FILETABS (cu); |
43f3e411 | 1055 | |
7bab9b58 DE |
1056 | for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++) |
1057 | { | |
1058 | struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i); | |
1059 | struct symbol *sym; | |
b026f593 | 1060 | struct mdict_iterator miter; |
7bab9b58 DE |
1061 | |
1062 | /* Inlined functions may have symbols not in the global or | |
1063 | static symbol lists. */ | |
1064 | if (BLOCK_FUNCTION (block) != NULL) | |
08be3fe3 DE |
1065 | if (symbol_symtab (BLOCK_FUNCTION (block)) == NULL) |
1066 | symbol_set_symtab (BLOCK_FUNCTION (block), symtab); | |
7bab9b58 DE |
1067 | |
1068 | /* Note that we only want to fix up symbols from the local | |
1069 | blocks, not blocks coming from included symtabs. That is why | |
1070 | we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */ | |
b026f593 | 1071 | ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym) |
08be3fe3 DE |
1072 | if (symbol_symtab (sym) == NULL) |
1073 | symbol_set_symtab (sym, symtab); | |
7bab9b58 DE |
1074 | } |
1075 | } | |
edb3359d | 1076 | |
43f3e411 | 1077 | add_compunit_symtab_to_objfile (cu); |
43f3e411 DE |
1078 | |
1079 | return cu; | |
7bab9b58 DE |
1080 | } |
1081 | ||
1082 | /* Implementation of the second part of end_symtab. Pass STATIC_BLOCK | |
1083 | as value returned by end_symtab_get_static_block. | |
1084 | ||
1085 | SECTION is the same as for end_symtab: the section number | |
1086 | (in objfile->section_offsets) of the blockvector and linetable. | |
1087 | ||
1088 | If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made | |
1089 | expandable. */ | |
1090 | ||
43f3e411 | 1091 | struct compunit_symtab * |
4a2125f5 TT |
1092 | buildsym_compunit::end_symtab_from_static_block (struct block *static_block, |
1093 | int section, int expandable) | |
7bab9b58 | 1094 | { |
43f3e411 | 1095 | struct compunit_symtab *cu; |
7bab9b58 DE |
1096 | |
1097 | if (static_block == NULL) | |
1098 | { | |
0ab9ce85 DE |
1099 | /* Handle the "no blockvector" case. |
1100 | When this happens there is nothing to record, so there's nothing | |
1101 | to do: memory will be freed up later. | |
1102 | ||
1103 | Note: We won't be adding a compunit to the objfile's list of | |
1104 | compunits, so there's nothing to unchain. However, since each symtab | |
1105 | is added to the objfile's obstack we can't free that space. | |
1106 | We could do better, but this is believed to be a sufficiently rare | |
1107 | event. */ | |
43f3e411 | 1108 | cu = NULL; |
7bab9b58 DE |
1109 | } |
1110 | else | |
43f3e411 | 1111 | cu = end_symtab_with_blockvector (static_block, section, expandable); |
cb1df416 | 1112 | |
43f3e411 | 1113 | return cu; |
6d30eef8 DE |
1114 | } |
1115 | ||
4359dff1 JK |
1116 | /* Finish the symbol definitions for one main source file, close off |
1117 | all the lexical contexts for that file (creating struct block's for | |
1118 | them), then make the struct symtab for that file and put it in the | |
1119 | list of all such. | |
1120 | ||
1121 | END_ADDR is the address of the end of the file's text. SECTION is | |
1122 | the section number (in objfile->section_offsets) of the blockvector | |
1123 | and linetable. | |
1124 | ||
1125 | Note that it is possible for end_symtab() to return NULL. In | |
1126 | particular, for the DWARF case at least, it will return NULL when | |
1127 | it finds a compilation unit that has exactly one DIE, a | |
1128 | TAG_compile_unit DIE. This can happen when we link in an object | |
1129 | file that was compiled from an empty source file. Returning NULL | |
1130 | is probably not the correct thing to do, because then gdb will | |
1131 | never know about this empty file (FIXME). | |
1132 | ||
1133 | If you need to modify STATIC_BLOCK before it is finalized you should | |
1134 | call end_symtab_get_static_block and end_symtab_from_static_block | |
1135 | yourself. */ | |
6d30eef8 | 1136 | |
43f3e411 | 1137 | struct compunit_symtab * |
4a2125f5 | 1138 | buildsym_compunit::end_symtab (CORE_ADDR end_addr, int section) |
6d30eef8 | 1139 | { |
4359dff1 JK |
1140 | struct block *static_block; |
1141 | ||
4d663531 DE |
1142 | static_block = end_symtab_get_static_block (end_addr, 0, 0); |
1143 | return end_symtab_from_static_block (static_block, section, 0); | |
6d30eef8 DE |
1144 | } |
1145 | ||
4359dff1 | 1146 | /* Same as end_symtab except create a symtab that can be later added to. */ |
6d30eef8 | 1147 | |
43f3e411 | 1148 | struct compunit_symtab * |
4a2125f5 | 1149 | buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr, int section) |
6d30eef8 | 1150 | { |
4359dff1 JK |
1151 | struct block *static_block; |
1152 | ||
4d663531 DE |
1153 | static_block = end_symtab_get_static_block (end_addr, 1, 0); |
1154 | return end_symtab_from_static_block (static_block, section, 1); | |
6d30eef8 DE |
1155 | } |
1156 | ||
1157 | /* Subroutine of augment_type_symtab to simplify it. | |
43f3e411 DE |
1158 | Attach the main source file's symtab to all symbols in PENDING_LIST that |
1159 | don't have one. */ | |
6d30eef8 DE |
1160 | |
1161 | static void | |
43f3e411 DE |
1162 | set_missing_symtab (struct pending *pending_list, |
1163 | struct compunit_symtab *cu) | |
6d30eef8 DE |
1164 | { |
1165 | struct pending *pending; | |
1166 | int i; | |
1167 | ||
1168 | for (pending = pending_list; pending != NULL; pending = pending->next) | |
801e3a5b | 1169 | { |
6d30eef8 DE |
1170 | for (i = 0; i < pending->nsyms; ++i) |
1171 | { | |
08be3fe3 DE |
1172 | if (symbol_symtab (pending->symbol[i]) == NULL) |
1173 | symbol_set_symtab (pending->symbol[i], COMPUNIT_FILETABS (cu)); | |
6d30eef8 | 1174 | } |
801e3a5b | 1175 | } |
6d30eef8 | 1176 | } |
c906108c | 1177 | |
6d30eef8 DE |
1178 | /* Same as end_symtab, but for the case where we're adding more symbols |
1179 | to an existing symtab that is known to contain only type information. | |
1180 | This is the case for DWARF4 Type Units. */ | |
1181 | ||
1182 | void | |
4a2125f5 | 1183 | buildsym_compunit::augment_type_symtab () |
6d30eef8 | 1184 | { |
cbb09508 | 1185 | struct compunit_symtab *cust = m_compunit_symtab; |
43f3e411 | 1186 | const struct blockvector *blockvector = COMPUNIT_BLOCKVECTOR (cust); |
6d30eef8 | 1187 | |
4a2125f5 | 1188 | if (!m_context_stack.empty ()) |
a60f3166 | 1189 | complaint (_("Context stack not empty in augment_type_symtab")); |
4a2125f5 | 1190 | if (m_pending_blocks != NULL) |
b98664d3 | 1191 | complaint (_("Blocks in a type symtab")); |
4a2125f5 | 1192 | if (m_pending_macros != NULL) |
b98664d3 | 1193 | complaint (_("Macro in a type symtab")); |
4a2125f5 | 1194 | if (m_have_line_numbers) |
b98664d3 | 1195 | complaint (_("Line numbers recorded in a type symtab")); |
6d30eef8 | 1196 | |
4a2125f5 | 1197 | if (m_file_symbols != NULL) |
6d30eef8 DE |
1198 | { |
1199 | struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK); | |
1200 | ||
1201 | /* First mark any symbols without a specified symtab as belonging | |
1202 | to the primary symtab. */ | |
4a2125f5 | 1203 | set_missing_symtab (m_file_symbols, cust); |
6d30eef8 | 1204 | |
b026f593 | 1205 | mdict_add_pending (BLOCK_MULTIDICT (block), m_file_symbols); |
6d30eef8 DE |
1206 | } |
1207 | ||
4a2125f5 | 1208 | if (m_global_symbols != NULL) |
6d30eef8 DE |
1209 | { |
1210 | struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK); | |
1211 | ||
1212 | /* First mark any symbols without a specified symtab as belonging | |
1213 | to the primary symtab. */ | |
4a2125f5 | 1214 | set_missing_symtab (m_global_symbols, cust); |
6d30eef8 | 1215 | |
b026f593 | 1216 | mdict_add_pending (BLOCK_MULTIDICT (block), |
4a2125f5 | 1217 | m_global_symbols); |
6d30eef8 | 1218 | } |
c906108c SS |
1219 | } |
1220 | ||
1221 | /* Push a context block. Args are an identifying nesting level | |
1222 | (checkable when you pop it), and the starting PC address of this | |
1223 | context. */ | |
1224 | ||
1225 | struct context_stack * | |
4a2125f5 | 1226 | buildsym_compunit::push_context (int desc, CORE_ADDR valu) |
c906108c | 1227 | { |
4a2125f5 TT |
1228 | m_context_stack.emplace_back (); |
1229 | struct context_stack *newobj = &m_context_stack.back (); | |
c906108c | 1230 | |
fe978cb0 | 1231 | newobj->depth = desc; |
4a2125f5 TT |
1232 | newobj->locals = m_local_symbols; |
1233 | newobj->old_blocks = m_pending_blocks; | |
fe978cb0 | 1234 | newobj->start_addr = valu; |
4a2125f5 | 1235 | newobj->local_using_directives = m_local_using_directives; |
fe978cb0 | 1236 | newobj->name = NULL; |
c906108c | 1237 | |
4a2125f5 TT |
1238 | m_local_symbols = NULL; |
1239 | m_local_using_directives = NULL; | |
c906108c | 1240 | |
fe978cb0 | 1241 | return newobj; |
c906108c | 1242 | } |
0c5e171a | 1243 | |
a672ef13 | 1244 | /* Pop a context block. Returns the address of the context block just |
4a64f543 | 1245 | popped. */ |
a672ef13 | 1246 | |
a60f3166 | 1247 | struct context_stack |
4a2125f5 | 1248 | buildsym_compunit::pop_context () |
0c5e171a | 1249 | { |
4a2125f5 TT |
1250 | gdb_assert (!m_context_stack.empty ()); |
1251 | struct context_stack result = m_context_stack.back (); | |
1252 | m_context_stack.pop_back (); | |
a60f3166 | 1253 | return result; |
0c5e171a | 1254 | } |