📄 buildsym.c
字号:
/* Support routines for building symbol tables in GDB's internal format. Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.This file is part of GDB.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* This module provides subroutines used for creating and adding to the symbol table. These routines are called from various symbol- file-reading routines. Routines to support specific debugging information formats (stabs, DWARF, etc) belong somewhere else. */#include "defs.h"#include "bfd.h"#include "obstack.h"#include "symtab.h"#include "symfile.h" /* Needed for "struct complaint" */#include "objfiles.h"#include <string.h>/* Ask buildsym.h to define the vars it normally declares `extern'. */#define EXTERN /**/#include "buildsym.h" /* Our own declarations */#undef EXTERNstatic intcompare_line_numbers PARAMS ((const void *, const void *));static struct blockvector *make_blockvector PARAMS ((struct objfile *));/* Initial sizes of data structures. These are realloc'd larger if needed, and realloc'd down to the size actually used, when completed. */#define INITIAL_CONTEXT_STACK_SIZE 10#define INITIAL_LINE_VECTOR_LENGTH 1000/* Complaints about the symbols we have encountered. */struct complaint innerblock_complaint = {"inner block not inside outer block in %s", 0, 0};struct complaint innerblock_anon_complaint = {"inner block not inside outer block", 0, 0};struct complaint blockvector_complaint = {"block at 0x%x out of order", 0, 0};/* maintain the lists of symbols and blocks *//* Add a symbol to one of the lists of symbols. */voidadd_symbol_to_list (symbol, listhead) struct symbol *symbol; struct pending **listhead;{ register struct pending *link; /* We keep PENDINGSIZE symbols in each link of the list. If we don't have a link with room in it, add a new link. */ if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE) { if (free_pendings) { link = free_pendings; free_pendings = link->next; } else { link = (struct pending *) xmalloc (sizeof (struct pending)); } link->next = *listhead; *listhead = link; link->nsyms = 0; } (*listhead)->symbol[(*listhead)->nsyms++] = symbol;}/* Find a symbol on a pending list. */struct symbol *find_symbol_in_list (list, name, length) struct pending *list; char *name; int length;{ int j; char *pp; while (list != NULL) { for (j = list->nsyms; --j >= 0; ) { pp = SYMBOL_NAME (list->symbol[j]); if (*pp == *name && strncmp (pp, name, length) == 0 && pp[length] == '\0') { return (list->symbol[j]); } } list = list->next; } return (NULL);}/* At end of reading syms, or in case of quit, really free as many `struct pending's as we can easily find. *//* ARGSUSED */voidreally_free_pendings (foo) int foo;{ struct pending *next, *next1;#if 0 struct pending_block *bnext, *bnext1;#endif for (next = free_pendings; next; next = next1) { next1 = next->next; free ((PTR)next); } free_pendings = NULL;#if 0 /* Now we make the links in the symbol_obstack, so don't free them. */ for (bnext = pending_blocks; bnext; bnext = bnext1) { bnext1 = bnext->next; free ((PTR)bnext); }#endif pending_blocks = NULL; for (next = file_symbols; next != NULL; next = next1) { next1 = next->next; free ((PTR)next); } file_symbols = NULL; for (next = global_symbols; next != NULL; next = next1) { next1 = next->next; free ((PTR)next); } global_symbols = NULL;}/* Take one of the lists of symbols and make a block from it. Keep the order the symbols have in the list (reversed from the input file). Put the block on the list of pending blocks. */voidfinish_block (symbol, listhead, old_blocks, start, end, objfile) struct symbol *symbol; struct pending **listhead; struct pending_block *old_blocks; CORE_ADDR start, end; struct objfile *objfile;{ register struct pending *next, *next1; register struct block *block; register struct pending_block *pblock; struct pending_block *opblock; register int i; register int j; /* Count the length of the list of symbols. */ for (next = *listhead, i = 0; next; i += next->nsyms, next = next->next) { /*EMPTY*/; } block = (struct block *) obstack_alloc (&objfile -> symbol_obstack, (sizeof (struct block) + ((i - 1) * sizeof (struct symbol *)))); /* Copy the symbols into the block. */ BLOCK_NSYMS (block) = i; for (next = *listhead; next; next = next->next) { for (j = next->nsyms - 1; j >= 0; j--) { BLOCK_SYM (block, --i) = next->symbol[j]; } } BLOCK_START (block) = start; BLOCK_END (block) = end; /* Superblock filled in when containing block is made */ BLOCK_SUPERBLOCK (block) = NULL; BLOCK_GCC_COMPILED (block) = processing_gcc_compilation; /* Put the block in as the value of the symbol that names it. */ if (symbol) { SYMBOL_BLOCK_VALUE (symbol) = block; BLOCK_FUNCTION (block) = symbol; } else { BLOCK_FUNCTION (block) = NULL; } /* Now "free" the links of the list, and empty the list. */ for (next = *listhead; next; next = next1) { next1 = next->next; next->next = free_pendings; free_pendings = next; } *listhead = NULL; /* Install this block as the superblock of all blocks made since the start of this scope that don't have superblocks yet. */ opblock = NULL; for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next) { if (BLOCK_SUPERBLOCK (pblock->block) == NULL) {#if 1 /* Check to be sure the blocks are nested as we receive them. If the compiler/assembler/linker work, this just burns a small amount of time. */ if (BLOCK_START (pblock->block) < BLOCK_START (block) || BLOCK_END (pblock->block) > BLOCK_END (block)) { if (symbol) { complain (&innerblock_complaint, SYMBOL_NAME (symbol)); } else { complain (&innerblock_anon_complaint, 0); } BLOCK_START (pblock->block) = BLOCK_START (block); BLOCK_END (pblock->block) = BLOCK_END (block); }#endif BLOCK_SUPERBLOCK (pblock->block) = block; } opblock = pblock; } /* Record this block on the list of all blocks in the file. Put it after opblock, or at the beginning if opblock is 0. This puts the block in the list after all its subblocks. */ /* Allocate in the symbol_obstack to save time. It wastes a little space. */ pblock = (struct pending_block *) obstack_alloc (&objfile -> symbol_obstack, sizeof (struct pending_block)); pblock->block = block; if (opblock) { pblock->next = opblock->next; opblock->next = pblock; } else { pblock->next = pending_blocks; pending_blocks = pblock; }}static struct blockvector *make_blockvector (objfile) struct objfile *objfile;{ register struct pending_block *next; register struct blockvector *blockvector; register int i; /* Count the length of the list of blocks. */ for (next = pending_blocks, i = 0; next; next = next->next, i++) {;} blockvector = (struct blockvector *) obstack_alloc (&objfile -> symbol_obstack, (sizeof (struct blockvector) + (i - 1) * sizeof (struct block *))); /* Copy the blocks into the blockvector. This is done in reverse order, which happens to put the blocks into the proper order (ascending starting address). finish_block has hair to insert each block into the list after its subblocks in order to make sure this is true. */ BLOCKVECTOR_NBLOCKS (blockvector) = i; for (next = pending_blocks; next; next = next->next) { BLOCKVECTOR_BLOCK (blockvector, --i) = next->block; }#if 0 /* Now we make the links in the obstack, so don't free them. */ /* Now free the links of the list, and empty the list. */ for (next = pending_blocks; next; next = next1) { next1 = next->next; free (next); }#endif pending_blocks = NULL;#if 1 /* FIXME, shut this off after a while to speed up symbol reading. */ /* Some compilers output blocks in the wrong order, but we depend on their being in the right order so we can binary search. Check the order and moan about it. FIXME. */ if (BLOCKVECTOR_NBLOCKS (blockvector) > 1) { for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++) { if (BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i-1)) > BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i))) { complain (&blockvector_complaint, (char *) BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i))); } } }#endif return (blockvector);}/* Start recording information about source code that came from an included (or otherwise merged-in) source file with a different name. */voidstart_subfile (name, dirname) char *name; char *dirname;{ register struct subfile *subfile; /* See if this subfile is already known as a subfile of the current main source file. */ for (subfile = subfiles; subfile; subfile = subfile->next) { if (!strcmp (subfile->name, name)) { current_subfile = subfile; return; } } /* This subfile is not known. Add an entry for it. Make an entry for this subfile in the list of all subfiles of the current main source file. */ subfile = (struct subfile *) xmalloc (sizeof (struct subfile)); subfile->next = subfiles; subfiles = subfile; current_subfile = subfile; /* Save its name and compilation directory name */ subfile->name = strdup (name); subfile->dirname = (dirname == NULL) ? NULL : strdup (dirname); /* Initialize line-number recording for this subfile. */ subfile->line_vector = NULL;}/* For stabs readers, the first N_SO symbol is assumed to be the source file name, and the subfile struct is initialized using that assumption. If another N_SO symbol is later seen, immediately following the first one, then the first one is assumed to be the directory name and the second one is really the source file name. So we have to patch up the subfile struct by moving the old name value to dirname and remembering the new name. Some sanity checking is performed to ensure that the state of the subfile struct is reasonable and that the old name we are assuming to be a directory name actually is (by checking for a trailing '/'). */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -