dwarf2.c

来自「基于4个mips核的noc设计」· C语言 代码 · 共 1,670 行 · 第 1/3 页

C
1,670
字号
/* DWARF 2 support.   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001   Free Software Foundation, Inc.   Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions   (gavin@cygnus.com).   From the dwarf2read.c header:   Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,   Inc.  with support from Florida State University (under contract   with the Ada Joint Program Office), and Silicon Graphics, Inc.   Initial contribution by Brent Benson, Harris Computer Systems, Inc.,   based on Fred Fish's (Cygnus Support) implementation of DWARF 1   support in dwarfread.cThis file is part of BFD.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 (atyour option) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */#include "bfd.h"#include "sysdep.h"#include "libiberty.h"#include "libbfd.h"#include "elf-bfd.h"#include "elf/dwarf2.h"/* The data in the .debug_line statement prologue looks like this.  */struct line_head{  unsigned int total_length;  unsigned short version;  unsigned int prologue_length;  unsigned char minimum_instruction_length;  unsigned char default_is_stmt;  int line_base;  unsigned char line_range;  unsigned char opcode_base;  unsigned char *standard_opcode_lengths;};/* Attributes have a name and a value.  */struct attribute{  enum dwarf_attribute name;  enum dwarf_form form;  union  {    char *str;    struct dwarf_block *blk;    unsigned int unsnd;    int snd;    bfd_vma addr;  }  u;};/* Get at parts of an attribute structure.  */#define DW_STRING(attr)    ((attr)->u.str)#define DW_UNSND(attr)     ((attr)->u.unsnd)#define DW_BLOCK(attr)     ((attr)->u.blk)#define DW_SND(attr)       ((attr)->u.snd)#define DW_ADDR(attr)	   ((attr)->u.addr)/* Blocks are a bunch of untyped bytes.  */struct dwarf_block{  unsigned int size;  char *data;};struct dwarf2_debug{  /* A list of all previously read comp_units.  */  struct comp_unit* all_comp_units;  /* The next unread compilation unit within the .debug_info section.     Zero indicates that the .debug_info section has not been loaded     into a buffer yet.  */  char* info_ptr;  /* Pointer to the end of the .debug_info section memory buffer.  */  char* info_ptr_end;  /* Pointer to the .debug_abbrev section loaded into memory.  */  char* dwarf_abbrev_buffer;  /* Length of the loaded .debug_abbrev section.  */  unsigned long dwarf_abbrev_size;  /* Buffer for decode_line_info.  */  char *dwarf_line_buffer;  /* Length of the loaded .debug_line section.  */  unsigned long dwarf_line_size;};struct arange{  struct arange *next;  bfd_vma low;  bfd_vma high;};/* A minimal decoding of DWARF2 compilation units.  We only decode   what's needed to get to the line number information.  */struct comp_unit{  /* Chain the previously read compilation units.  */  struct comp_unit* next_unit;  /* Keep the bdf convenient (for memory allocation).  */  bfd* abfd;  /* The lowest and higest addresses contained in this compilation     unit as specified in the compilation unit header.  */  struct arange arange;  /* The DW_AT_name attribute (for error messages).  */  char* name;  /* The abbrev hash table.  */  struct abbrev_info** abbrevs;  /* Note that an error was found by comp_unit_find_nearest_line.  */  int error;  /* The DW_AT_comp_dir attribute.  */  char* comp_dir;  /* True if there is a line number table associated with this comp. unit.  */  int stmtlist;  /* The offset into .debug_line of the line number table.  */  unsigned long line_offset;  /* Pointer to the first child die for the comp unit.  */  char *first_child_die_ptr;  /* The end of the comp unit.  */  char *end_ptr;  /* The decoded line number, NULL if not yet decoded.  */  struct line_info_table* line_table;  /* A list of the functions found in this comp. unit.  */  struct funcinfo* function_table;  /* Address size for this unit - from unit header.  */  unsigned char addr_size;};/* VERBATIM   The following function up to the END VERBATIM mark are   copied directly from dwarf2read.c.  *//* Read dwarf information from a buffer.  */static unsigned intread_1_byte (abfd, buf)     bfd *abfd ATTRIBUTE_UNUSED;     char *buf;{  return bfd_get_8 (abfd, (bfd_byte *) buf);}static intread_1_signed_byte (abfd, buf)     bfd *abfd ATTRIBUTE_UNUSED;     char *buf;{  return bfd_get_signed_8 (abfd, (bfd_byte *) buf);}static unsigned intread_2_bytes (abfd, buf)     bfd *abfd;     char *buf;{  return bfd_get_16 (abfd, (bfd_byte *) buf);}#if 0  /* This is not used.  */static intread_2_signed_bytes (abfd, buf)     bfd *abfd;     char *buf;{  return bfd_get_signed_16 (abfd, (bfd_byte *) buf);}#endifstatic unsigned intread_4_bytes (abfd, buf)     bfd *abfd;     char *buf;{  return bfd_get_32 (abfd, (bfd_byte *) buf);}#if 0  /* This is not used.  */static intread_4_signed_bytes (abfd, buf)     bfd *abfd;     char *buf;{  return bfd_get_signed_32 (abfd, (bfd_byte *) buf);}#endifstatic unsigned intread_8_bytes (abfd, buf)     bfd *abfd;     char *buf;{  return bfd_get_64 (abfd, (bfd_byte *) buf);}static char *read_n_bytes (abfd, buf, size)     bfd *abfd ATTRIBUTE_UNUSED;     char *buf;     unsigned int size ATTRIBUTE_UNUSED;{  /* If the size of a host char is 8 bits, we can return a pointer     to the buffer, otherwise we have to copy the data to a buffer     allocated on the temporary obstack.  */  return buf;}static char *read_string (abfd, buf, bytes_read_ptr)     bfd *abfd ATTRIBUTE_UNUSED;     char *buf;     unsigned int *bytes_read_ptr;{  /* If the size of a host char is 8 bits, we can return a pointer     to the string, otherwise we have to copy the string to a buffer     allocated on the temporary obstack.  */  if (*buf == '\0')    {      *bytes_read_ptr = 1;      return NULL;    }  *bytes_read_ptr = strlen (buf) + 1;  return buf;}static unsigned intread_unsigned_leb128 (abfd, buf, bytes_read_ptr)     bfd *abfd ATTRIBUTE_UNUSED;     char *buf;     unsigned int *bytes_read_ptr;{  unsigned int  result;  unsigned int  num_read;  int           shift;  unsigned char byte;  result   = 0;  shift    = 0;  num_read = 0;  do    {      byte = bfd_get_8 (abfd, (bfd_byte *) buf);      buf ++;      num_read ++;      result |= ((byte & 0x7f) << shift);      shift += 7;    }  while (byte & 0x80);  * bytes_read_ptr = num_read;  return result;}static intread_signed_leb128 (abfd, buf, bytes_read_ptr)     bfd *abfd ATTRIBUTE_UNUSED;     char *buf;     unsigned int * bytes_read_ptr;{  int           result;  int           shift;  int           num_read;  unsigned char byte;  result = 0;  shift = 0;  num_read = 0;  do    {      byte = bfd_get_8 (abfd, (bfd_byte *) buf);      buf ++;      num_read ++;      result |= ((byte & 0x7f) << shift);      shift += 7;    }  while (byte & 0x80);  if ((shift < 32) && (byte & 0x40))    result |= -(1 << shift);  * bytes_read_ptr = num_read;  return result;}/* END VERBATIM */static bfd_vmaread_address (unit, buf)     struct comp_unit* unit;     char *buf;{  switch (unit->addr_size)    {    case 8:      return bfd_get_64 (unit->abfd, (bfd_byte *) buf);    case 4:      return bfd_get_32 (unit->abfd, (bfd_byte *) buf);    case 2:      return bfd_get_16 (unit->abfd, (bfd_byte *) buf);    default:      abort ();    }}/* This data structure holds the information of an abbrev.  */struct abbrev_info{  unsigned int number;		/* Number identifying abbrev.  */  enum dwarf_tag tag;		/* DWARF tag.  */  int has_children;		/* Boolean.  */  unsigned int num_attrs;	/* Number of attributes.  */  struct attr_abbrev *attrs;	/* An array of attribute descriptions.  */  struct abbrev_info *next;	/* Next in chain.  */};struct attr_abbrev{  enum dwarf_attribute name;  enum dwarf_form form;};#ifndef ABBREV_HASH_SIZE#define ABBREV_HASH_SIZE 121#endif#ifndef ATTR_ALLOC_CHUNK#define ATTR_ALLOC_CHUNK 4#endif/* Lookup an abbrev_info structure in the abbrev hash table.  */static struct abbrev_info *lookup_abbrev (number,abbrevs)     unsigned int number;     struct abbrev_info **abbrevs;{  unsigned int hash_number;  struct abbrev_info *abbrev;  hash_number = number % ABBREV_HASH_SIZE;  abbrev = abbrevs[hash_number];  while (abbrev)    {      if (abbrev->number == number)	return abbrev;      else	abbrev = abbrev->next;    }  return NULL;}/* In DWARF version 2, the description of the debugging information is   stored in a separate .debug_abbrev section.  Before we read any   dies from a section we read in all abbreviations and install them   in a hash table.  */static struct abbrev_info**read_abbrevs (abfd, offset, stash)     bfd * abfd;     unsigned int offset;     struct dwarf2_debug *stash;{  struct abbrev_info **abbrevs;  char *abbrev_ptr;  struct abbrev_info *cur_abbrev;  unsigned int abbrev_number, bytes_read, abbrev_name;  unsigned int abbrev_form, hash_number;  if (! stash->dwarf_abbrev_buffer)    {      asection *msec;      msec = bfd_get_section_by_name (abfd, ".debug_abbrev");      if (! msec)	{	  (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));	  bfd_set_error (bfd_error_bad_value);	  return 0;	}      stash->dwarf_abbrev_size = msec->_raw_size;      stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, stash->dwarf_abbrev_size);      if (! stash->dwarf_abbrev_buffer)	  return 0;      if (! bfd_get_section_contents (abfd, msec,				      stash->dwarf_abbrev_buffer, 0,				      stash->dwarf_abbrev_size))	return 0;    }  if (offset >= stash->dwarf_abbrev_size)    {      (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%u) greater than or equal to abbrev size (%u)."),			     offset, stash->dwarf_abbrev_size );      bfd_set_error (bfd_error_bad_value);      return 0;    }  abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE);  abbrev_ptr = stash->dwarf_abbrev_buffer + offset;  abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);  abbrev_ptr += bytes_read;  /* Loop until we reach an abbrev number of 0.  */  while (abbrev_number)    {      cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info));      /* Read in abbrev header.  */      cur_abbrev->number = abbrev_number;      cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);      abbrev_ptr += bytes_read;      cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);      abbrev_ptr += 1;      /* Now read in declarations.  */      abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);      abbrev_ptr += bytes_read;      abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);      abbrev_ptr += bytes_read;      while (abbrev_name)	{	  if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)	    {	      cur_abbrev->attrs = (struct attr_abbrev *)		bfd_realloc (cur_abbrev->attrs,			     (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)			     * sizeof (struct attr_abbrev));	      if (! cur_abbrev->attrs)		return 0;	    }	  cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;	  cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;	  abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);	  abbrev_ptr += bytes_read;	  abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);	  abbrev_ptr += bytes_read;	}      hash_number = abbrev_number % ABBREV_HASH_SIZE;      cur_abbrev->next = abbrevs[hash_number];      abbrevs[hash_number] = cur_abbrev;      /* Get next abbreviation.         Under Irix6 the abbreviations for a compilation unit are not	 always properly terminated with an abbrev number of 0.	 Exit loop if we encounter an abbreviation which we have	 already read (which means we are about to read the abbreviations	 for the next compile unit) or if the end of the abbreviation	 table is reached.  */      if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)	    >= stash->dwarf_abbrev_size)	break;      abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);      abbrev_ptr += bytes_read;      if (lookup_abbrev (abbrev_number,abbrevs) != NULL)	break;    }  return abbrevs;}/* Read an attribute described by an abbreviated attribute.  */static char *read_attribute (attr, abbrev, unit, info_ptr)     struct attribute   *attr;     struct attr_abbrev *abbrev;     struct comp_unit   *unit;     char               *info_ptr;{  bfd *abfd = unit->abfd;  unsigned int bytes_read;  struct dwarf_block *blk;  attr->name = abbrev->name;  attr->form = abbrev->form;  switch (abbrev->form)    {    case DW_FORM_addr:    case DW_FORM_ref_addr:      DW_ADDR (attr) = read_address (unit, info_ptr);      info_ptr += unit->addr_size;      break;    case DW_FORM_block2:      blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));      blk->size = read_2_bytes (abfd, info_ptr);      info_ptr += 2;      blk->data = read_n_bytes (abfd, info_ptr, blk->size);      info_ptr += blk->size;      DW_BLOCK (attr) = blk;      break;    case DW_FORM_block4:      blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));      blk->size = read_4_bytes (abfd, info_ptr);      info_ptr += 4;      blk->data = read_n_bytes (abfd, info_ptr, blk->size);      info_ptr += blk->size;      DW_BLOCK (attr) = blk;      break;    case DW_FORM_data2:      DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);      info_ptr += 2;      break;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?