Home | History | Annotate | Download | only in libdw
      1 /* Get abbreviation at given offset.
      2    Copyright (C) 2003, 2004 Red Hat, Inc.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2003.
      4 
      5    This program is Open Source software; you can redistribute it and/or
      6    modify it under the terms of the Open Software License version 1.0 as
      7    published by the Open Source Initiative.
      8 
      9    You should have received a copy of the Open Software License along
     10    with this program; if not, you may obtain a copy of the Open Software
     11    License version 1.0 from http://www.opensource.org/licenses/osl.php or
     12    by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
     13    3001 King Ranch Road, Ukiah, CA 95482.   */
     14 
     15 #ifdef HAVE_CONFIG_H
     16 # include <config.h>
     17 #endif
     18 
     19 #include <assert.h>
     20 #include <dwarf.h>
     21 #include "libdwP.h"
     22 
     23 
     24 Dwarf_Abbrev *
     25 internal_function_def
     26 __libdw_getabbrev (dbg, cu, offset, lengthp, result)
     27      Dwarf *dbg;
     28      struct Dwarf_CU *cu;
     29      Dwarf_Off offset;
     30      size_t *lengthp;
     31      Dwarf_Abbrev *result;
     32 {
     33   /* Don't fail if there is not .debug_abbrev section.  */
     34   if (dbg->sectiondata[IDX_debug_abbrev] == NULL)
     35     return NULL;
     36 
     37   unsigned char *abbrevp
     38     = (unsigned char *) dbg->sectiondata[IDX_debug_abbrev]->d_buf + offset;
     39   if (*abbrevp == '\0')
     40     /* We are past the last entry.  */
     41     return NULL;
     42 
     43   /* 7.5.3 Abbreviations Tables
     44 
     45      [...] Each declaration begins with an unsigned LEB128 number
     46      representing the abbreviation code itself.  [...]  The
     47      abbreviation code is followed by another unsigned LEB128
     48      number that encodes the entry's tag.  [...]
     49 
     50      [...] Following the tag encoding is a 1-byte value that
     51      determines whether a debugging information entry using this
     52      abbreviation has child entries or not. [...]
     53 
     54      [...] Finally, the child encoding is followed by a series of
     55      attribute specifications. Each attribute specification
     56      consists of two parts. The first part is an unsigned LEB128
     57      number representing the attribute's name. The second part is
     58      an unsigned LEB128 number representing the attribute's form.  */
     59   unsigned char *start_abbrevp = abbrevp;
     60   unsigned int code;
     61   get_uleb128 (code, abbrevp);
     62 
     63   /* Check whether this code is already in the hash table.  */
     64   bool foundit = false;
     65   Dwarf_Abbrev *abb = NULL;
     66   if (cu == NULL
     67       || (abb = Dwarf_Abbrev_Hash_find (&cu->abbrev_hash, code, NULL)) == NULL)
     68     {
     69       if (result == NULL)
     70 	abb = libdw_typed_alloc (dbg, Dwarf_Abbrev);
     71       else
     72 	abb = result;
     73     }
     74   else
     75     {
     76       foundit = true;
     77 
     78       assert (abb->offset == offset);
     79 
     80       /* If the caller doesn't need the length we are done.  */
     81       if (lengthp == NULL)
     82 	goto out;
     83     }
     84 
     85   /* If there is already a value in the hash table we are going to
     86      overwrite its content.  This must not be a problem, since the
     87      content better be the same.  */
     88   abb->code = code;
     89   get_uleb128 (abb->tag, abbrevp);
     90   abb->has_children = *abbrevp++ == DW_CHILDREN_yes;
     91   abb->attrp = abbrevp;
     92   abb->offset = offset;
     93 
     94   /* Skip over all the attributes and count them while doing so.  */
     95   abb->attrcnt = 0;
     96   unsigned int attrname;
     97   unsigned int attrform;
     98   do
     99     {
    100       get_uleb128 (attrname, abbrevp);
    101       get_uleb128 (attrform, abbrevp);
    102     }
    103   while (attrname != 0 && attrform != 0 && ++abb->attrcnt);
    104 
    105   /* Return the length to the caller if she asked for it.  */
    106   if (lengthp != NULL)
    107     *lengthp = abbrevp - start_abbrevp;
    108 
    109   /* Add the entry to the hash table.  */
    110   if (cu != NULL && ! foundit)
    111     (void) Dwarf_Abbrev_Hash_insert (&cu->abbrev_hash, abb->code, abb);
    112 
    113  out:
    114   return abb;
    115 }
    116 
    117 
    118 Dwarf_Abbrev *
    119 dwarf_getabbrev (die, offset, lengthp)
    120      Dwarf_Die *die;
    121      Dwarf_Off offset;
    122      size_t *lengthp;
    123 {
    124   return __libdw_getabbrev (die->cu->dbg, die->cu,
    125 			    die->cu->orig_abbrev_offset + offset, lengthp,
    126 			    NULL);
    127 }
    128