]>
Commit | Line | Data |
---|---|---|
d07734e3 | 1 | /* Support routines for building symbol tables in GDB's internal format. |
93297ea0 JG |
2 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992 |
3 | Free Software Foundation, Inc. | |
c0302457 JG |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | /* This module provides subroutines used for creating and adding to | |
22 | the symbol table. These routines are called from various symbol- | |
d07734e3 | 23 | file-reading routines. |
c0302457 | 24 | |
d07734e3 FF |
25 | Routines to support specific debugging information formats (stabs, |
26 | DWARF, etc) belong somewhere else. */ | |
c0302457 JG |
27 | |
28 | #include "defs.h" | |
d07734e3 | 29 | #include "bfd.h" |
c0302457 JG |
30 | #include "obstack.h" |
31 | #include "symtab.h" | |
c0302457 | 32 | #include "symfile.h" /* Needed for "struct complaint" */ |
5e2e79f8 | 33 | #include "objfiles.h" |
51b80b00 | 34 | #include "complaints.h" |
c0302457 | 35 | #include <string.h> |
c0302457 JG |
36 | |
37 | /* Ask buildsym.h to define the vars it normally declares `extern'. */ | |
38 | #define EXTERN /**/ | |
39 | #include "buildsym.h" /* Our own declarations */ | |
40 | #undef EXTERN | |
41 | ||
100f92e2 JK |
42 | /* For cleanup_undefined_types and finish_global_stabs (somewhat |
43 | questionable--see comment where we call them). */ | |
44 | #include "stabsread.h" | |
45 | ||
1ab3bf1b JG |
46 | static int |
47 | compare_line_numbers PARAMS ((const void *, const void *)); | |
48 | ||
49 | static struct blockvector * | |
50 | make_blockvector PARAMS ((struct objfile *)); | |
51 | ||
1ab3bf1b | 52 | \f |
4137c5fc JG |
53 | /* Initial sizes of data structures. These are realloc'd larger if needed, |
54 | and realloc'd down to the size actually used, when completed. */ | |
55 | ||
56 | #define INITIAL_CONTEXT_STACK_SIZE 10 | |
4137c5fc | 57 | #define INITIAL_LINE_VECTOR_LENGTH 1000 |
d07734e3 | 58 | |
c0302457 JG |
59 | \f |
60 | /* Complaints about the symbols we have encountered. */ | |
61 | ||
62 | struct complaint innerblock_complaint = | |
63 | {"inner block not inside outer block in %s", 0, 0}; | |
64 | ||
76512886 JG |
65 | struct complaint innerblock_anon_complaint = |
66 | {"inner block not inside outer block", 0, 0}; | |
67 | ||
c0302457 | 68 | struct complaint blockvector_complaint = |
5573d7d4 | 69 | {"block at 0x%lx out of order", 0, 0}; |
c0302457 | 70 | |
c0302457 JG |
71 | \f |
72 | /* maintain the lists of symbols and blocks */ | |
73 | ||
74 | /* Add a symbol to one of the lists of symbols. */ | |
d07734e3 | 75 | |
c0302457 JG |
76 | void |
77 | add_symbol_to_list (symbol, listhead) | |
78 | struct symbol *symbol; | |
79 | struct pending **listhead; | |
80 | { | |
d07734e3 FF |
81 | register struct pending *link; |
82 | ||
c0302457 JG |
83 | /* We keep PENDINGSIZE symbols in each link of the list. |
84 | If we don't have a link with room in it, add a new link. */ | |
d07734e3 | 85 | if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE) |
c0302457 | 86 | { |
c0302457 JG |
87 | if (free_pendings) |
88 | { | |
89 | link = free_pendings; | |
90 | free_pendings = link->next; | |
91 | } | |
92 | else | |
d07734e3 FF |
93 | { |
94 | link = (struct pending *) xmalloc (sizeof (struct pending)); | |
95 | } | |
c0302457 JG |
96 | |
97 | link->next = *listhead; | |
98 | *listhead = link; | |
99 | link->nsyms = 0; | |
100 | } | |
101 | ||
102 | (*listhead)->symbol[(*listhead)->nsyms++] = symbol; | |
103 | } | |
104 | ||
c438b3af JK |
105 | /* Find a symbol named NAME on a LIST. NAME need not be '\0'-terminated; |
106 | LENGTH is the length of the name. */ | |
d07734e3 | 107 | |
a048c8f5 JG |
108 | struct symbol * |
109 | find_symbol_in_list (list, name, length) | |
110 | struct pending *list; | |
111 | char *name; | |
112 | int length; | |
113 | { | |
114 | int j; | |
d07734e3 | 115 | char *pp; |
a048c8f5 | 116 | |
d07734e3 FF |
117 | while (list != NULL) |
118 | { | |
119 | for (j = list->nsyms; --j >= 0; ) | |
120 | { | |
121 | pp = SYMBOL_NAME (list->symbol[j]); | |
122 | if (*pp == *name && strncmp (pp, name, length) == 0 && | |
123 | pp[length] == '\0') | |
124 | { | |
125 | return (list->symbol[j]); | |
126 | } | |
127 | } | |
128 | list = list->next; | |
a048c8f5 | 129 | } |
d07734e3 | 130 | return (NULL); |
a048c8f5 JG |
131 | } |
132 | ||
c0302457 | 133 | /* At end of reading syms, or in case of quit, |
d07734e3 | 134 | really free as many `struct pending's as we can easily find. */ |
c0302457 JG |
135 | |
136 | /* ARGSUSED */ | |
137 | void | |
138 | really_free_pendings (foo) | |
139 | int foo; | |
140 | { | |
141 | struct pending *next, *next1; | |
142 | #if 0 | |
143 | struct pending_block *bnext, *bnext1; | |
144 | #endif | |
145 | ||
146 | for (next = free_pendings; next; next = next1) | |
147 | { | |
148 | next1 = next->next; | |
84ffdec2 | 149 | free ((PTR)next); |
c0302457 | 150 | } |
d07734e3 | 151 | free_pendings = NULL; |
c0302457 JG |
152 | |
153 | #if 0 /* Now we make the links in the symbol_obstack, so don't free them. */ | |
154 | for (bnext = pending_blocks; bnext; bnext = bnext1) | |
155 | { | |
156 | bnext1 = bnext->next; | |
84ffdec2 | 157 | free ((PTR)bnext); |
c0302457 JG |
158 | } |
159 | #endif | |
d07734e3 | 160 | pending_blocks = NULL; |
c0302457 | 161 | |
d07734e3 | 162 | for (next = file_symbols; next != NULL; next = next1) |
c0302457 JG |
163 | { |
164 | next1 = next->next; | |
84ffdec2 | 165 | free ((PTR)next); |
c0302457 | 166 | } |
d07734e3 | 167 | file_symbols = NULL; |
c0302457 | 168 | |
d07734e3 | 169 | for (next = global_symbols; next != NULL; next = next1) |
c0302457 JG |
170 | { |
171 | next1 = next->next; | |
84ffdec2 | 172 | free ((PTR)next); |
c0302457 | 173 | } |
d07734e3 | 174 | global_symbols = NULL; |
c0302457 JG |
175 | } |
176 | ||
177 | /* Take one of the lists of symbols and make a block from it. | |
178 | Keep the order the symbols have in the list (reversed from the input file). | |
179 | Put the block on the list of pending blocks. */ | |
180 | ||
181 | void | |
1ab3bf1b | 182 | finish_block (symbol, listhead, old_blocks, start, end, objfile) |
c0302457 JG |
183 | struct symbol *symbol; |
184 | struct pending **listhead; | |
185 | struct pending_block *old_blocks; | |
186 | CORE_ADDR start, end; | |
1ab3bf1b | 187 | struct objfile *objfile; |
c0302457 JG |
188 | { |
189 | register struct pending *next, *next1; | |
190 | register struct block *block; | |
191 | register struct pending_block *pblock; | |
192 | struct pending_block *opblock; | |
193 | register int i; | |
d07734e3 | 194 | register int j; |
c0302457 JG |
195 | |
196 | /* Count the length of the list of symbols. */ | |
197 | ||
cd46ffad FF |
198 | for (next = *listhead, i = 0; |
199 | next; | |
200 | i += next->nsyms, next = next->next) | |
d07734e3 FF |
201 | { |
202 | /*EMPTY*/; | |
203 | } | |
c0302457 | 204 | |
1ab3bf1b | 205 | block = (struct block *) obstack_alloc (&objfile -> symbol_obstack, |
a048c8f5 | 206 | (sizeof (struct block) + ((i - 1) * sizeof (struct symbol *)))); |
c0302457 JG |
207 | |
208 | /* Copy the symbols into the block. */ | |
209 | ||
210 | BLOCK_NSYMS (block) = i; | |
211 | for (next = *listhead; next; next = next->next) | |
212 | { | |
c0302457 | 213 | for (j = next->nsyms - 1; j >= 0; j--) |
d07734e3 FF |
214 | { |
215 | BLOCK_SYM (block, --i) = next->symbol[j]; | |
216 | } | |
c0302457 JG |
217 | } |
218 | ||
219 | BLOCK_START (block) = start; | |
220 | BLOCK_END (block) = end; | |
d07734e3 FF |
221 | /* Superblock filled in when containing block is made */ |
222 | BLOCK_SUPERBLOCK (block) = NULL; | |
c0302457 JG |
223 | BLOCK_GCC_COMPILED (block) = processing_gcc_compilation; |
224 | ||
225 | /* Put the block in as the value of the symbol that names it. */ | |
226 | ||
227 | if (symbol) | |
228 | { | |
229 | SYMBOL_BLOCK_VALUE (symbol) = block; | |
230 | BLOCK_FUNCTION (block) = symbol; | |
231 | } | |
232 | else | |
d07734e3 FF |
233 | { |
234 | BLOCK_FUNCTION (block) = NULL; | |
235 | } | |
c0302457 JG |
236 | |
237 | /* Now "free" the links of the list, and empty the list. */ | |
238 | ||
239 | for (next = *listhead; next; next = next1) | |
240 | { | |
241 | next1 = next->next; | |
242 | next->next = free_pendings; | |
243 | free_pendings = next; | |
244 | } | |
d07734e3 | 245 | *listhead = NULL; |
c0302457 JG |
246 | |
247 | /* Install this block as the superblock | |
248 | of all blocks made since the start of this scope | |
249 | that don't have superblocks yet. */ | |
250 | ||
d07734e3 | 251 | opblock = NULL; |
c0302457 JG |
252 | for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next) |
253 | { | |
d07734e3 FF |
254 | if (BLOCK_SUPERBLOCK (pblock->block) == NULL) |
255 | { | |
c0302457 | 256 | #if 1 |
d07734e3 FF |
257 | /* Check to be sure the blocks are nested as we receive them. |
258 | If the compiler/assembler/linker work, this just burns a small | |
259 | amount of time. */ | |
260 | if (BLOCK_START (pblock->block) < BLOCK_START (block) || | |
261 | BLOCK_END (pblock->block) > BLOCK_END (block)) | |
262 | { | |
263 | if (symbol) | |
264 | { | |
2e4964ad FF |
265 | complain (&innerblock_complaint, |
266 | SYMBOL_SOURCE_NAME (symbol)); | |
d07734e3 FF |
267 | } |
268 | else | |
269 | { | |
51b80b00 | 270 | complain (&innerblock_anon_complaint); |
d07734e3 FF |
271 | } |
272 | BLOCK_START (pblock->block) = BLOCK_START (block); | |
273 | BLOCK_END (pblock->block) = BLOCK_END (block); | |
274 | } | |
c0302457 | 275 | #endif |
d07734e3 FF |
276 | BLOCK_SUPERBLOCK (pblock->block) = block; |
277 | } | |
c0302457 JG |
278 | opblock = pblock; |
279 | } | |
280 | ||
281 | /* Record this block on the list of all blocks in the file. | |
282 | Put it after opblock, or at the beginning if opblock is 0. | |
283 | This puts the block in the list after all its subblocks. */ | |
284 | ||
285 | /* Allocate in the symbol_obstack to save time. | |
286 | It wastes a little space. */ | |
287 | pblock = (struct pending_block *) | |
1ab3bf1b | 288 | obstack_alloc (&objfile -> symbol_obstack, |
c0302457 JG |
289 | sizeof (struct pending_block)); |
290 | pblock->block = block; | |
291 | if (opblock) | |
292 | { | |
293 | pblock->next = opblock->next; | |
294 | opblock->next = pblock; | |
295 | } | |
296 | else | |
297 | { | |
298 | pblock->next = pending_blocks; | |
299 | pending_blocks = pblock; | |
300 | } | |
301 | } | |
302 | ||
1ab3bf1b JG |
303 | static struct blockvector * |
304 | make_blockvector (objfile) | |
c438b3af | 305 | struct objfile *objfile; |
c0302457 JG |
306 | { |
307 | register struct pending_block *next; | |
308 | register struct blockvector *blockvector; | |
309 | register int i; | |
310 | ||
311 | /* Count the length of the list of blocks. */ | |
312 | ||
d07734e3 | 313 | for (next = pending_blocks, i = 0; next; next = next->next, i++) {;} |
c0302457 JG |
314 | |
315 | blockvector = (struct blockvector *) | |
1ab3bf1b | 316 | obstack_alloc (&objfile -> symbol_obstack, |
c0302457 JG |
317 | (sizeof (struct blockvector) |
318 | + (i - 1) * sizeof (struct block *))); | |
319 | ||
320 | /* Copy the blocks into the blockvector. | |
321 | This is done in reverse order, which happens to put | |
322 | the blocks into the proper order (ascending starting address). | |
323 | finish_block has hair to insert each block into the list | |
324 | after its subblocks in order to make sure this is true. */ | |
325 | ||
326 | BLOCKVECTOR_NBLOCKS (blockvector) = i; | |
d07734e3 FF |
327 | for (next = pending_blocks; next; next = next->next) |
328 | { | |
329 | BLOCKVECTOR_BLOCK (blockvector, --i) = next->block; | |
330 | } | |
c0302457 JG |
331 | |
332 | #if 0 /* Now we make the links in the obstack, so don't free them. */ | |
333 | /* Now free the links of the list, and empty the list. */ | |
334 | ||
335 | for (next = pending_blocks; next; next = next1) | |
336 | { | |
337 | next1 = next->next; | |
338 | free (next); | |
339 | } | |
340 | #endif | |
d07734e3 | 341 | pending_blocks = NULL; |
c0302457 JG |
342 | |
343 | #if 1 /* FIXME, shut this off after a while to speed up symbol reading. */ | |
344 | /* Some compilers output blocks in the wrong order, but we depend | |
345 | on their being in the right order so we can binary search. | |
346 | Check the order and moan about it. FIXME. */ | |
347 | if (BLOCKVECTOR_NBLOCKS (blockvector) > 1) | |
d07734e3 FF |
348 | { |
349 | for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++) | |
350 | { | |
351 | if (BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i-1)) | |
352 | > BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i))) | |
353 | { | |
833e0d94 JK |
354 | |
355 | /* FIXME-32x64: loses if CORE_ADDR doesn't fit in a | |
356 | long. Possible solutions include a version of | |
357 | complain which takes a callback, a | |
358 | sprintf_address_numeric to match | |
359 | print_address_numeric, or a way to set up a GDB_FILE | |
360 | * which causes sprintf rather than fprintf to be | |
361 | called. */ | |
362 | ||
d07734e3 | 363 | complain (&blockvector_complaint, |
5573d7d4 | 364 | (unsigned long) BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i))); |
d07734e3 FF |
365 | } |
366 | } | |
c0302457 JG |
367 | } |
368 | #endif | |
369 | ||
d07734e3 | 370 | return (blockvector); |
c0302457 | 371 | } |
d07734e3 | 372 | |
c0302457 | 373 | \f |
4137c5fc | 374 | /* Start recording information about source code that came from an included |
c438b3af JK |
375 | (or otherwise merged-in) source file with a different name. NAME is |
376 | the name of the file (cannot be NULL), DIRNAME is the directory in which | |
377 | it resides (or NULL if not known). */ | |
c0302457 JG |
378 | |
379 | void | |
4137c5fc JG |
380 | start_subfile (name, dirname) |
381 | char *name; | |
382 | char *dirname; | |
383 | { | |
384 | register struct subfile *subfile; | |
385 | ||
386 | /* See if this subfile is already known as a subfile of the | |
387 | current main source file. */ | |
388 | ||
389 | for (subfile = subfiles; subfile; subfile = subfile->next) | |
390 | { | |
2e4964ad | 391 | if (STREQ (subfile->name, name)) |
4137c5fc JG |
392 | { |
393 | current_subfile = subfile; | |
394 | return; | |
395 | } | |
396 | } | |
397 | ||
398 | /* This subfile is not known. Add an entry for it. | |
399 | Make an entry for this subfile in the list of all subfiles | |
400 | of the current main source file. */ | |
401 | ||
402 | subfile = (struct subfile *) xmalloc (sizeof (struct subfile)); | |
403 | subfile->next = subfiles; | |
404 | subfiles = subfile; | |
405 | current_subfile = subfile; | |
406 | ||
407 | /* Save its name and compilation directory name */ | |
7c622b41 | 408 | subfile->name = (name == NULL)? NULL : strdup (name); |
3416d90b | 409 | subfile->dirname = (dirname == NULL) ? NULL : strdup (dirname); |
4137c5fc JG |
410 | |
411 | /* Initialize line-number recording for this subfile. */ | |
d07734e3 | 412 | subfile->line_vector = NULL; |
2e4964ad FF |
413 | |
414 | /* Default the source language to whatever can be deduced from | |
415 | the filename. If nothing can be deduced (such as for a C/C++ | |
416 | include file with a ".h" extension), then inherit whatever | |
417 | language the previous subfile had. This kludgery is necessary | |
418 | because there is no standard way in some object formats to | |
419 | record the source language. Also, when symtabs are allocated | |
420 | we try to deduce a language then as well, but it is too late | |
421 | for us to use that information while reading symbols, since | |
422 | symtabs aren't allocated until after all the symbols have | |
423 | been processed for a given source file. */ | |
424 | ||
425 | subfile->language = deduce_language_from_filename (subfile->name); | |
426 | if (subfile->language == language_unknown && | |
427 | subfile->next != NULL) | |
428 | { | |
429 | subfile->language = subfile->next->language; | |
430 | } | |
56ad756a JK |
431 | |
432 | /* cfront output is a C program, so in most ways it looks like a C | |
433 | program. But to demangle we need to set the language to C++. We | |
434 | can distinguish cfront code by the fact that it has #line | |
435 | directives which specify a file name ending in .C. | |
436 | ||
437 | So if the filename of this subfile ends in .C, then change the language | |
8b05f64a JK |
438 | of any pending subfiles from C to C++. We also accept any other C++ |
439 | suffixes accepted by deduce_language_from_filename (in particular, | |
440 | some people use .cxx with cfront). */ | |
56ad756a JK |
441 | |
442 | if (subfile->name) | |
443 | { | |
56ad756a JK |
444 | struct subfile *s; |
445 | ||
8b05f64a | 446 | if (deduce_language_from_filename (subfile->name) == language_cplus) |
56ad756a JK |
447 | for (s = subfiles; s != NULL; s = s->next) |
448 | if (s->language == language_c) | |
449 | s->language = language_cplus; | |
450 | } | |
451 | ||
452 | /* And patch up this file if necessary. */ | |
453 | if (subfile->language == language_c | |
454 | && subfile->next != NULL | |
455 | && subfile->next->language == language_cplus) | |
456 | { | |
457 | subfile->language = language_cplus; | |
458 | } | |
4137c5fc | 459 | } |
d07734e3 | 460 | |
3416d90b FF |
461 | /* For stabs readers, the first N_SO symbol is assumed to be the source |
462 | file name, and the subfile struct is initialized using that assumption. | |
463 | If another N_SO symbol is later seen, immediately following the first | |
464 | one, then the first one is assumed to be the directory name and the | |
465 | second one is really the source file name. | |
466 | ||
467 | So we have to patch up the subfile struct by moving the old name value to | |
468 | dirname and remembering the new name. Some sanity checking is performed | |
469 | to ensure that the state of the subfile struct is reasonable and that the | |
470 | old name we are assuming to be a directory name actually is (by checking | |
471 | for a trailing '/'). */ | |
472 | ||
473 | void | |
474 | patch_subfile_names (subfile, name) | |
475 | struct subfile *subfile; | |
476 | char *name; | |
477 | { | |
478 | if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL | |
479 | && subfile->name[strlen(subfile->name)-1] == '/') | |
480 | { | |
481 | subfile->dirname = subfile->name; | |
482 | subfile->name = strdup (name); | |
2e4964ad FF |
483 | |
484 | /* Default the source language to whatever can be deduced from | |
485 | the filename. If nothing can be deduced (such as for a C/C++ | |
486 | include file with a ".h" extension), then inherit whatever | |
487 | language the previous subfile had. This kludgery is necessary | |
488 | because there is no standard way in some object formats to | |
489 | record the source language. Also, when symtabs are allocated | |
490 | we try to deduce a language then as well, but it is too late | |
491 | for us to use that information while reading symbols, since | |
492 | symtabs aren't allocated until after all the symbols have | |
493 | been processed for a given source file. */ | |
494 | ||
495 | subfile->language = deduce_language_from_filename (subfile->name); | |
496 | if (subfile->language == language_unknown && | |
497 | subfile->next != NULL) | |
498 | { | |
499 | subfile->language = subfile->next->language; | |
500 | } | |
3416d90b FF |
501 | } |
502 | } | |
503 | ||
4137c5fc | 504 | \f |
a048c8f5 JG |
505 | /* Handle the N_BINCL and N_EINCL symbol types |
506 | that act like N_SOL for switching source files | |
507 | (different subfiles, as we call them) within one object file, | |
508 | but using a stack rather than in an arbitrary order. */ | |
509 | ||
510 | void | |
511 | push_subfile () | |
512 | { | |
513 | register struct subfile_stack *tem | |
514 | = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack)); | |
515 | ||
516 | tem->next = subfile_stack; | |
517 | subfile_stack = tem; | |
d07734e3 FF |
518 | if (current_subfile == NULL || current_subfile->name == NULL) |
519 | { | |
520 | abort (); | |
521 | } | |
a048c8f5 | 522 | tem->name = current_subfile->name; |
a048c8f5 JG |
523 | } |
524 | ||
525 | char * | |
526 | pop_subfile () | |
527 | { | |
528 | register char *name; | |
529 | register struct subfile_stack *link = subfile_stack; | |
530 | ||
d07734e3 FF |
531 | if (link == NULL) |
532 | { | |
533 | abort (); | |
534 | } | |
a048c8f5 JG |
535 | name = link->name; |
536 | subfile_stack = link->next; | |
84ffdec2 | 537 | free ((PTR)link); |
d07734e3 | 538 | return (name); |
a048c8f5 | 539 | } |
d07734e3 | 540 | |
a048c8f5 | 541 | \f |
be7d4f3f JK |
542 | /* Add a linetable entry for line number LINE and address PC to the line |
543 | vector for SUBFILE. */ | |
4137c5fc JG |
544 | |
545 | void | |
546 | record_line (subfile, line, pc) | |
547 | register struct subfile *subfile; | |
c0302457 JG |
548 | int line; |
549 | CORE_ADDR pc; | |
550 | { | |
551 | struct linetable_entry *e; | |
552 | /* Ignore the dummy line number in libg.o */ | |
553 | ||
554 | if (line == 0xffff) | |
d07734e3 FF |
555 | { |
556 | return; | |
557 | } | |
c0302457 | 558 | |
4137c5fc | 559 | /* Make sure line vector exists and is big enough. */ |
d07734e3 FF |
560 | if (!subfile->line_vector) |
561 | { | |
562 | subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH; | |
563 | subfile->line_vector = (struct linetable *) | |
4137c5fc JG |
564 | xmalloc (sizeof (struct linetable) |
565 | + subfile->line_vector_length * sizeof (struct linetable_entry)); | |
d07734e3 FF |
566 | subfile->line_vector->nitems = 0; |
567 | } | |
c0302457 | 568 | |
4137c5fc | 569 | if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length) |
c0302457 | 570 | { |
4137c5fc JG |
571 | subfile->line_vector_length *= 2; |
572 | subfile->line_vector = (struct linetable *) | |
1ab3bf1b | 573 | xrealloc ((char *) subfile->line_vector, (sizeof (struct linetable) |
d07734e3 | 574 | + subfile->line_vector_length * sizeof (struct linetable_entry))); |
c0302457 JG |
575 | } |
576 | ||
4137c5fc | 577 | e = subfile->line_vector->item + subfile->line_vector->nitems++; |
c0302457 JG |
578 | e->line = line; e->pc = pc; |
579 | } | |
4137c5fc JG |
580 | |
581 | ||
582 | /* Needed in order to sort line tables from IBM xcoff files. Sigh! */ | |
583 | ||
1ab3bf1b JG |
584 | static int |
585 | compare_line_numbers (ln1p, ln2p) | |
586 | const PTR ln1p; | |
587 | const PTR ln2p; | |
4137c5fc | 588 | { |
c438b3af JK |
589 | struct linetable_entry *ln1 = (struct linetable_entry *) ln1p; |
590 | struct linetable_entry *ln2 = (struct linetable_entry *) ln2p; | |
591 | ||
592 | /* Note: this code does not assume that CORE_ADDRs can fit in ints. | |
593 | Please keep it that way. */ | |
594 | if (ln1->pc < ln2->pc) | |
595 | return -1; | |
596 | ||
597 | if (ln1->pc > ln2->pc) | |
598 | return 1; | |
599 | ||
600 | /* If pc equal, sort by line. I'm not sure whether this is optimum | |
601 | behavior (see comment at struct linetable in symtab.h). */ | |
602 | return ln1->line - ln2->line; | |
4137c5fc | 603 | } |
1ab3bf1b | 604 | |
c0302457 JG |
605 | \f |
606 | /* Start a new symtab for a new source file. | |
d07734e3 FF |
607 | Called, for example, when a stabs symbol of type N_SO is seen, or when |
608 | a DWARF TAG_compile_unit DIE is seen. | |
609 | It indicates the start of data for one original source file. */ | |
c0302457 JG |
610 | |
611 | void | |
612 | start_symtab (name, dirname, start_addr) | |
613 | char *name; | |
614 | char *dirname; | |
615 | CORE_ADDR start_addr; | |
616 | { | |
617 | ||
618 | last_source_file = name; | |
619 | last_source_start_addr = start_addr; | |
d07734e3 FF |
620 | file_symbols = NULL; |
621 | global_symbols = NULL; | |
c0302457 JG |
622 | within_function = 0; |
623 | ||
a048c8f5 JG |
624 | /* Context stack is initially empty. Allocate first one with room for |
625 | 10 levels; reuse it forever afterward. */ | |
d07734e3 FF |
626 | if (context_stack == NULL) |
627 | { | |
628 | context_stack_size = INITIAL_CONTEXT_STACK_SIZE; | |
629 | context_stack = (struct context_stack *) | |
630 | xmalloc (context_stack_size * sizeof (struct context_stack)); | |
631 | } | |
c0302457 JG |
632 | context_stack_depth = 0; |
633 | ||
c0302457 JG |
634 | /* Initialize the list of sub source files with one entry |
635 | for this file (the top-level source file). */ | |
636 | ||
d07734e3 FF |
637 | subfiles = NULL; |
638 | current_subfile = NULL; | |
c0302457 JG |
639 | start_subfile (name, dirname); |
640 | } | |
641 | ||
642 | /* Finish the symbol definitions for one main source file, | |
643 | close off all the lexical contexts for that file | |
644 | (creating struct block's for them), then make the struct symtab | |
645 | for that file and put it in the list of all such. | |
646 | ||
7b5d9650 | 647 | END_ADDR is the address of the end of the file's text. |
3c02636b JK |
648 | SECTION is the section number (in objfile->section_offsets) of |
649 | the blockvector and linetable. | |
7b5d9650 FF |
650 | |
651 | Note that it is possible for end_symtab() to return NULL. In particular, | |
652 | for the DWARF case at least, it will return NULL when it finds a | |
653 | compilation unit that has exactly one DIE, a TAG_compile_unit DIE. This | |
654 | can happen when we link in an object file that was compiled from an empty | |
655 | source file. Returning NULL is probably not the correct thing to do, | |
656 | because then gdb will never know about this empty file (FIXME). */ | |
c0302457 JG |
657 | |
658 | struct symtab * | |
3c02636b | 659 | end_symtab (end_addr, sort_pending, sort_linevec, objfile, section) |
c0302457 | 660 | CORE_ADDR end_addr; |
4137c5fc JG |
661 | int sort_pending; |
662 | int sort_linevec; | |
a048c8f5 | 663 | struct objfile *objfile; |
3c02636b | 664 | int section; |
c0302457 | 665 | { |
fee933f1 | 666 | register struct symtab *symtab = NULL; |
c0302457 JG |
667 | register struct blockvector *blockvector; |
668 | register struct subfile *subfile; | |
d07734e3 | 669 | register struct context_stack *cstk; |
c0302457 JG |
670 | struct subfile *nextsub; |
671 | ||
672 | /* Finish the lexical context of the last function in the file; | |
673 | pop the context stack. */ | |
674 | ||
675 | if (context_stack_depth > 0) | |
676 | { | |
c0302457 JG |
677 | context_stack_depth--; |
678 | cstk = &context_stack[context_stack_depth]; | |
679 | /* Make a block for the local symbols within. */ | |
680 | finish_block (cstk->name, &local_symbols, cstk->old_blocks, | |
1ab3bf1b | 681 | cstk->start_addr, end_addr, objfile); |
a048c8f5 | 682 | |
a048c8f5 | 683 | if (context_stack_depth > 0) |
d07734e3 | 684 | { |
f91b837d JK |
685 | /* This is said to happen with SCO. The old coffread.c code |
686 | simply emptied the context stack, so we do the same. FIXME: | |
687 | Find out why it is happening. This is not believed to happen | |
688 | in most cases (even for coffread.c); it used to be an abort(). */ | |
689 | static struct complaint msg = | |
690 | {"Context stack not empty in end_symtab", 0, 0}; | |
691 | complain (&msg); | |
692 | context_stack_depth = 0; | |
d07734e3 | 693 | } |
c0302457 JG |
694 | } |
695 | ||
9b280a7f | 696 | /* It is unfortunate that in xcoff, pending blocks might not be ordered |
4137c5fc JG |
697 | in this stage. Especially, blocks for static functions will show up at |
698 | the end. We need to sort them, so tools like `find_pc_function' and | |
699 | `find_pc_block' can work reliably. */ | |
d07734e3 FF |
700 | |
701 | if (sort_pending && pending_blocks) | |
702 | { | |
703 | /* FIXME! Remove this horrid bubble sort and use qsort!!! */ | |
704 | int swapped; | |
705 | do | |
706 | { | |
707 | struct pending_block *pb, *pbnext; | |
708 | ||
709 | pb = pending_blocks; | |
710 | pbnext = pb->next; | |
711 | swapped = 0; | |
712 | ||
713 | while (pbnext) | |
714 | { | |
715 | /* swap blocks if unordered! */ | |
716 | ||
717 | if (BLOCK_START(pb->block) < BLOCK_START(pbnext->block)) | |
718 | { | |
719 | struct block *tmp = pb->block; | |
720 | pb->block = pbnext->block; | |
721 | pbnext->block = tmp; | |
722 | swapped = 1; | |
723 | } | |
724 | pb = pbnext; | |
725 | pbnext = pbnext->next; | |
726 | } | |
727 | } while (swapped); | |
728 | } | |
4137c5fc | 729 | |
c0302457 JG |
730 | /* Cleanup any undefined types that have been left hanging around |
731 | (this needs to be done before the finish_blocks so that | |
d07734e3 | 732 | file_symbols is still good). |
c438b3af JK |
733 | |
734 | Both cleanup_undefined_types and finish_global_stabs are stabs | |
735 | specific, but harmless for other symbol readers, since on gdb | |
736 | startup or when finished reading stabs, the state is set so these | |
737 | are no-ops. FIXME: Is this handled right in case of QUIT? Can | |
738 | we make this cleaner? */ | |
739 | ||
c0302457 | 740 | cleanup_undefined_types (); |
d07734e3 | 741 | finish_global_stabs (objfile); |
c0302457 | 742 | |
d07734e3 FF |
743 | if (pending_blocks == NULL |
744 | && file_symbols == NULL | |
745 | && global_symbols == NULL) | |
746 | { | |
747 | /* Ignore symtabs that have no functions with real debugging info */ | |
748 | blockvector = NULL; | |
749 | } | |
750 | else | |
751 | { | |
752 | /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the blockvector. */ | |
753 | finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr, | |
754 | objfile); | |
755 | finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr, | |
756 | objfile); | |
757 | blockvector = make_blockvector (objfile); | |
758 | } | |
c0302457 | 759 | |
818de002 | 760 | #ifdef PROCESS_LINENUMBER_HOOK |
9b280a7f | 761 | PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */ |
818de002 PB |
762 | #endif |
763 | ||
c0302457 JG |
764 | /* Now create the symtab objects proper, one for each subfile. */ |
765 | /* (The main file is the last one on the chain.) */ | |
766 | ||
767 | for (subfile = subfiles; subfile; subfile = nextsub) | |
768 | { | |
fee933f1 | 769 | int linetablesize = 0; |
a048c8f5 JG |
770 | /* If we have blocks of symbols, make a symtab. |
771 | Otherwise, just ignore this file and any line number info in it. */ | |
d07734e3 FF |
772 | symtab = NULL; |
773 | if (blockvector) | |
774 | { | |
775 | if (subfile->line_vector) | |
776 | { | |
d07734e3 FF |
777 | linetablesize = sizeof (struct linetable) + |
778 | subfile->line_vector->nitems * sizeof (struct linetable_entry); | |
c438b3af JK |
779 | #if 0 |
780 | /* I think this is artifact from before it went on the obstack. | |
781 | I doubt we'll need the memory between now and when we | |
782 | free it later in this function. */ | |
783 | /* First, shrink the linetable to make more memory. */ | |
d07734e3 FF |
784 | subfile->line_vector = (struct linetable *) |
785 | xrealloc ((char *) subfile->line_vector, linetablesize); | |
c438b3af JK |
786 | #endif |
787 | /* If sort_linevec is false, we might want just check to make | |
788 | sure they are sorted and complain() if not, as a way of | |
789 | tracking down compilers/symbol readers which don't get | |
790 | them sorted right. */ | |
d07734e3 FF |
791 | |
792 | if (sort_linevec) | |
793 | qsort (subfile->line_vector->item, | |
794 | subfile->line_vector->nitems, | |
795 | sizeof (struct linetable_entry), compare_line_numbers); | |
796 | } | |
797 | ||
798 | /* Now, allocate a symbol table. */ | |
799 | symtab = allocate_symtab (subfile->name, objfile); | |
4137c5fc | 800 | |
d07734e3 FF |
801 | /* Fill in its components. */ |
802 | symtab->blockvector = blockvector; | |
803 | if (subfile->line_vector) | |
804 | { | |
805 | /* Reallocate the line table on the symbol obstack */ | |
806 | symtab->linetable = (struct linetable *) | |
807 | obstack_alloc (&objfile -> symbol_obstack, linetablesize); | |
808 | memcpy (symtab->linetable, subfile->line_vector, linetablesize); | |
809 | } | |
810 | else | |
811 | { | |
812 | symtab->linetable = NULL; | |
813 | } | |
3c02636b | 814 | symtab->block_line_section = section; |
8275e802 FF |
815 | if (subfile->dirname) |
816 | { | |
817 | /* Reallocate the dirname on the symbol obstack */ | |
818 | symtab->dirname = (char *) | |
819 | obstack_alloc (&objfile -> symbol_obstack, | |
820 | strlen (subfile -> dirname) + 1); | |
821 | strcpy (symtab->dirname, subfile->dirname); | |
822 | } | |
823 | else | |
824 | { | |
825 | symtab->dirname = NULL; | |
826 | } | |
d07734e3 FF |
827 | symtab->free_code = free_linetable; |
828 | symtab->free_ptr = NULL; | |
2b5a8d9c | 829 | |
2e4964ad FF |
830 | /* Use whatever language we have been using for this subfile, |
831 | not the one that was deduced in allocate_symtab from the | |
832 | filename. We already did our own deducing when we created | |
833 | the subfile, and we may have altered our opinion of what | |
834 | language it is from things we found in the symbols. */ | |
835 | symtab->language = subfile->language; | |
836 | ||
3c02636b JK |
837 | /* All symtabs for the main file and the subfiles share a |
838 | blockvector, so we need to clear primary for everything but | |
839 | the main file. */ | |
2b5a8d9c | 840 | |
3c02636b | 841 | symtab->primary = 0; |
d07734e3 | 842 | } |
3416d90b FF |
843 | if (subfile->name != NULL) |
844 | { | |
845 | free ((PTR) subfile->name); | |
846 | } | |
847 | if (subfile->dirname != NULL) | |
848 | { | |
849 | free ((PTR) subfile->dirname); | |
850 | } | |
851 | if (subfile->line_vector != NULL) | |
d07734e3 | 852 | { |
3416d90b | 853 | free ((PTR) subfile->line_vector); |
d07734e3 | 854 | } |
4137c5fc | 855 | |
c0302457 | 856 | nextsub = subfile->next; |
84ffdec2 | 857 | free ((PTR)subfile); |
c0302457 JG |
858 | } |
859 | ||
3c02636b | 860 | /* Set this for the main source file. */ |
1eeba686 | 861 | if (symtab) |
d07734e3 | 862 | { |
3c02636b | 863 | symtab->primary = 1; |
d07734e3 | 864 | } |
2b5a8d9c | 865 | |
d07734e3 FF |
866 | last_source_file = NULL; |
867 | current_subfile = NULL; | |
4137c5fc | 868 | |
d07734e3 | 869 | return (symtab); |
c0302457 | 870 | } |
a048c8f5 JG |
871 | |
872 | ||
873 | /* Push a context block. Args are an identifying nesting level (checkable | |
874 | when you pop it), and the starting PC address of this context. */ | |
875 | ||
876 | struct context_stack * | |
877 | push_context (desc, valu) | |
878 | int desc; | |
879 | CORE_ADDR valu; | |
880 | { | |
881 | register struct context_stack *new; | |
882 | ||
883 | if (context_stack_depth == context_stack_size) | |
884 | { | |
885 | context_stack_size *= 2; | |
886 | context_stack = (struct context_stack *) | |
1ab3bf1b JG |
887 | xrealloc ((char *) context_stack, |
888 | (context_stack_size * sizeof (struct context_stack))); | |
a048c8f5 JG |
889 | } |
890 | ||
891 | new = &context_stack[context_stack_depth++]; | |
892 | new->depth = desc; | |
893 | new->locals = local_symbols; | |
894 | new->old_blocks = pending_blocks; | |
895 | new->start_addr = valu; | |
d07734e3 | 896 | new->name = NULL; |
a048c8f5 | 897 | |
d07734e3 | 898 | local_symbols = NULL; |
a048c8f5 | 899 | |
d07734e3 | 900 | return (new); |
a048c8f5 | 901 | } |
d07734e3 | 902 | |
48f075eb SS |
903 | \f |
904 | /* Compute a small integer hash code for the given name. */ | |
905 | ||
906 | int | |
907 | hashname (name) | |
908 | char *name; | |
909 | { | |
910 | register char *p = name; | |
911 | register int total = p[0]; | |
912 | register int c; | |
913 | ||
914 | c = p[1]; | |
915 | total += c << 2; | |
916 | if (c) | |
917 | { | |
918 | c = p[2]; | |
919 | total += c << 4; | |
920 | if (c) | |
921 | { | |
922 | total += p[3] << 6; | |
923 | } | |
924 | } | |
925 | ||
926 | /* Ensure result is positive. */ | |
927 | if (total < 0) | |
928 | { | |
929 | total += (1000 << 6); | |
930 | } | |
931 | return (total % HASHSIZE); | |
932 | } | |
933 | ||
c0302457 JG |
934 | \f |
935 | /* Initialize anything that needs initializing when starting to read | |
936 | a fresh piece of a symbol file, e.g. reading in the stuff corresponding | |
937 | to a psymtab. */ | |
938 | ||
939 | void | |
940 | buildsym_init () | |
941 | { | |
d07734e3 FF |
942 | free_pendings = NULL; |
943 | file_symbols = NULL; | |
944 | global_symbols = NULL; | |
945 | pending_blocks = NULL; | |
c0302457 JG |
946 | } |
947 | ||
948 | /* Initialize anything that needs initializing when a completely new | |
949 | symbol file is specified (not just adding some symbols from another | |
950 | file, e.g. a shared library). */ | |
951 | ||
952 | void | |
953 | buildsym_new_init () | |
954 | { | |
c0302457 JG |
955 | buildsym_init (); |
956 | } | |
957 | ||
d07734e3 | 958 | /* Initializer for this module */ |
095db7ce | 959 | |
c0302457 JG |
960 | void |
961 | _initialize_buildsym () | |
962 | { | |
c0302457 | 963 | } |