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