Home | History | Annotate | Download | only in libdw
      1 /* Get attributes of the DIE.
      2    Copyright (C) 2004 Red Hat, Inc.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2004.
      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 "libdwP.h"
     20 
     21 
     22 ptrdiff_t
     23 dwarf_getattrs (Dwarf_Die *die, int (*callback) (Dwarf_Attribute *, void *),
     24 		void *arg, ptrdiff_t offset)
     25 {
     26   if (die == NULL)
     27     return -1l;
     28 
     29   unsigned char *die_addr = die->addr;
     30 
     31   /* Get the abbreviation code.  */
     32   unsigned int u128;
     33   get_uleb128 (u128, die_addr);
     34 
     35   if (die->abbrev == NULL)
     36     /* Find the abbreviation.  */
     37     die->abbrev = __libdw_findabbrev (die->cu, u128);
     38 
     39   if (die->abbrev == (Dwarf_Abbrev *) -1l)
     40     {
     41       __libdw_seterrno (DWARF_E_INVALID_DWARF);
     42       return -1l;
     43     }
     44 
     45   /* This is where the attributes start.  */
     46   unsigned char *attrp = die->abbrev->attrp + offset;
     47 
     48   /* Go over the list of attributes.  */
     49   Dwarf *dbg = die->cu->dbg;
     50   while (1)
     51     {
     52       /* Are we still in bounds?  */
     53       if (unlikely (attrp
     54 		    >= ((unsigned char *) dbg->sectiondata[IDX_debug_abbrev]->d_buf
     55 			+ dbg->sectiondata[IDX_debug_abbrev]->d_size)))
     56 	{
     57 	  __libdw_seterrno (DWARF_E_INVALID_DWARF);
     58 	  return -1;
     59 	}
     60 
     61       /* Get attribute name and form.  */
     62       Dwarf_Attribute attr;
     63       // XXX Fix bound checks
     64       get_uleb128 (attr.code, attrp);
     65       get_uleb128 (attr.form, attrp);
     66 
     67       /* We can stop if we found the attribute with value zero.  */
     68       if (attr.code == 0 && attr.form == 0)
     69         return 0;
     70 
     71       /* Fill in the rest.  */
     72       attr.valp = die_addr;
     73       attr.cu = die->cu;
     74 
     75       /* Now call the callback function.  */
     76       if (callback (&attr, arg) != DWARF_CB_OK)
     77 	return attrp - die->abbrev->attrp;
     78 
     79       /* Skip over the rest of this attribute (if there is any).  */
     80       if (attr.form != 0)
     81 	{
     82 	  size_t len = __libdw_form_val_len (dbg, die->cu, attr.form,
     83 					     die_addr);
     84 
     85 	  if (unlikely (len == (size_t) -1l))
     86 	    /* Something wrong with the file.  */
     87 	    return -1l;
     88 
     89 	  // XXX We need better boundary checks.
     90 	  die_addr += len;
     91 	}
     92     }
     93   /* NOTREACHED */
     94 }
     95