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