summaryrefslogtreecommitdiff
path: root/libdwarf/dwarf_siblingof.c
blob: 04a3ab6d43ffc2f4a7493409b7b976a48a9de927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* Return descriptor for sibling of die.
   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
   Written by Ulrich Drepper <drepper@redhat.com>, 2000.

   This program is Open Source software; you can redistribute it and/or
   modify it under the terms of the Open Software License version 1.0 as
   published by the Open Source Initiative.

   You should have received a copy of the Open Software License along
   with this program; if not, you may obtain a copy of the Open Software
   License version 1.0 from http://www.opensource.org/licenses/osl.php or
   by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
   3001 King Ranch Road, Ukiah, CA 95482.   */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <dwarf.h>
#include <stdlib.h>

#include <libdwarfP.h>


int
dwarf_siblingof (dbg, die, return_sub, error)
     Dwarf_Debug dbg;
     Dwarf_Die die;
     Dwarf_Die *return_sub;
     Dwarf_Error *error;
{
  Dwarf_Small *die_addr;
  Dwarf_CU_Info cu;
  Dwarf_Unsigned u128;
  Dwarf_Die new_die;

  if (dbg == NULL)
    return DW_DLV_ERROR;

  if (die == NULL)
    {
      Dwarf_Unsigned die_offset;

      /* We are supposed to return the DW_TAG_compile_unit die for the
	 current compile unit.  For this to succeed the user must have
	 looked for the compile unit before.  */
      if (dbg->cu_list_current == NULL)
	{
	  __libdwarf_error (dbg, error, DW_E_NO_CU);
	  return DW_DLV_ERROR;
	}

      cu = dbg->cu_list_current;

      die_offset = (cu->offset + 2 * cu->offset_size - 4 + sizeof (Dwarf_Half)
		    + cu->offset_size + 1);

      /* Check whether this is withing the debug section.  */
      if (die_offset >= dbg->sections[IDX_debug_info].size)
	return DW_DLV_NO_ENTRY;

      /* Compute the pointer.  */
      die_addr = ((Dwarf_Small *) dbg->sections[IDX_debug_info].addr
		  + die_offset);
    }
  else
    {
      unsigned int level = 0;

      /* We start from the given die.  */
      cu = die->cu;

      /* Address of the given die.  */
      die_addr = die->addr;

      /* Search for the beginning of the next die on this level.  We
         must not return the dies for children of the given die.  */
      do
	{
	  Dwarf_Abbrev abbrev;
	  Dwarf_Small *attrp;
	  Dwarf_Word attr_name;
	  Dwarf_Word attr_form;

	  /* Get abbrev code.  */
	  get_uleb128 (u128, die_addr);
	  /*  And get the abbreviation itself.  */
	  abbrev = __libdwarf_get_abbrev (dbg, cu, u128, error);
	  if (abbrev == NULL)
	    return DW_DLV_ERROR;

	  /* This is where the attributes start.  */
	  attrp = abbrev->attrp;

	  /* Does this abbreviation have children?  */
	  if (abbrev->has_children)
	    ++level;

	  while (1)
	    {
	      /* Are we still in bounds?  */
	      if (attrp >= ((Dwarf_Small *)dbg->sections[IDX_debug_abbrev].addr
			    + dbg->sections[IDX_debug_abbrev].size))
		{
		  __libdwarf_error (dbg, error, DW_E_INVALID_DWARF);
		  return DW_DLV_ERROR;
		}

	      /* Get attribute name and form.

		 XXX We don't check whether this reads beyond the end of
		 the section.  */
	      get_uleb128 (attr_name, attrp);
	      get_uleb128 (attr_form, attrp);

	      /* We can stop if we found the attribute with value zero.  */
	      if (attr_name == 0 && attr_form == 0)
		break;

	      /* See whether this is an sibling attribute which would help
		 us to skip ahead.  */
	      if (attr_name == DW_AT_sibling)
		{
		  /* Cool.  We just have to decode the parameter and we know
		     the offset.  */
		  Dwarf_Unsigned offset;

		  switch (attr_form)
		    {
		    case DW_FORM_ref1:
		      offset = *die_addr;
		      break;

		    case DW_FORM_ref2:
		      offset = read_2ubyte_unaligned (dbg, die_addr);
		      break;

		    case DW_FORM_ref4:
		      offset = read_4ubyte_unaligned (dbg, die_addr);
		      break;

		    case DW_FORM_ref8:
		      offset = read_8ubyte_unaligned (dbg, die_addr);
		      break;

		    case DW_FORM_ref_udata:
		      get_uleb128 (offset, die_addr);
		      break;

		    default:
		      /* The above are all legal forms.  Everything else is
			 an error.  */
		      __libdwarf_error (dbg, error, DW_E_INVALID_DWARF);
		      return DW_DLV_ERROR;
		    }

		  /* Compute the new address.  Some sanity check first,
		     though.  */
		  if (unlikely (offset > 2 * cu->offset_size - 4 + cu->length))
		    {
		      __libdwarf_error (dbg, error, DW_E_INVALID_DWARF);
		      return DW_DLV_ERROR;
		    }

		  die_addr =
		    ((Dwarf_Small *) dbg->sections[IDX_debug_info].addr
		     + cu->offset + offset);

		  /* Even if the abbreviation has children we have stepped
		     over them now.  */
		  if (abbrev->has_children)
		    --level;
		  break;
		}

	      /* Skip over the rest of this attribute (if there is any).  */
	      if (attr_form != 0)
		{
		  size_t len;

		  if (unlikely (__libdwarf_form_val_len (dbg, cu, attr_form,
							 die_addr, &len, error)
				!= DW_DLV_OK))
		    return DW_DLV_ERROR;

		  die_addr += len;
		}
	    }

	  /* Check that we are not yet at the end.  */
	  if (*die_addr == 0)
	    {
	      if (level == 0)
		return DW_DLV_NO_ENTRY;

	      do
		++die_addr;
	      while (--level > 0 && *die_addr == 0);
	    }
	}
      while (level > 0);
    }

  /* Are we at the end.  */
  if (die != NULL
      && die_addr >= ((Dwarf_Small *) dbg->sections[IDX_debug_info].addr
		      + cu->offset + cu->length + 2 * cu->offset_size - 4))
    return DW_DLV_NO_ENTRY;

  /* See whether there is another sibling available or whether this is
     the end.  */
  if (*die_addr == 0)
    return DW_DLV_NO_ENTRY;

  /* There is more data.  Create the data structure.  */
  new_die = (Dwarf_Die) malloc (sizeof (struct Dwarf_Die_s));
  if (new_die == NULL)
    {
      __libdwarf_error (dbg, error, DW_E_NOMEM);
      return DW_DLV_ERROR;
    }

#ifdef DWARF_DEBUG
  new_die->memtag = DW_DLA_DIE;
#endif

  /* Remember the address.  */
  new_die->addr = die_addr;

  /* And the compile unit.  */
  new_die->cu = cu;

  /* 7.5.2  Debugging Information Entry

     Each debugging information entry begins with an unsigned LEB128
     number containing the abbreviation code for the entry.  */
  get_uleb128 (u128, die_addr);

  /* Find the abbreviation.  */
  new_die->abbrev = __libdwarf_get_abbrev (dbg, cu, u128, error);
  if (new_die->abbrev == NULL)
    {
      free (new_die);
      return DW_DLV_ERROR;
    }

  /* If we are looking for the first entry this must be a compile unit.  */
  if (die == NULL && unlikely (new_die->abbrev->tag != DW_TAG_compile_unit))
    {
      free (new_die);
      __libdwarf_error (dbg, error, DW_E_1ST_NO_CU);
      return DW_DLV_ERROR;
    }

  *return_sub = new_die;
  return DW_DLV_OK;
}