aboutsummaryrefslogtreecommitdiff
path: root/libdwarf/dwarf_get_fde_list_eh.c
blob: 5282a6256bef9c8dfa1ae72960e124c5a98c1e11 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* Get frame descriptions.  GCC version using .eh_frame.
   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
   This file is part of Red Hat elfutils.
   Written by Ulrich Drepper <drepper@redhat.com>, 2000.

   Red Hat elfutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by the
   Free Software Foundation; version 2 of the License.

   Red Hat elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with Red Hat elfutils; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.

   Red Hat elfutils is an included package of the Open Invention Network.
   An included package of the Open Invention Network is a package for which
   Open Invention Network licensees cross-license their patents.  No patent
   license is granted, either expressly or impliedly, by designation as an
   included package.  Should you wish to participate in the Open Invention
   Network licensing program, please visit www.openinventionnetwork.com
   <http://www.openinventionnetwork.com>.  */

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

#include <stdlib.h>
#include <string.h>

#include <libdwarfP.h>


struct cielist
{
  Dwarf_Cie cie;
  struct cielist *next;
};


struct fdelist
{
  Dwarf_Fde fde;
  Dwarf_Small *cie_id_ptr;
  struct fdelist *next;
};


int
dwarf_get_fde_list_eh (dbg, cie_data, cie_element_count, fde_data,
		       fde_element_count, error)
     Dwarf_Debug dbg;
     Dwarf_Cie **cie_data;
     Dwarf_Signed *cie_element_count;
     Dwarf_Fde **fde_data;
     Dwarf_Signed *fde_element_count;
     Dwarf_Error *error;
{
  Dwarf_Small *readp;
  Dwarf_Small *readendp;
  struct cielist *cielist = NULL;
  struct cielist *copy_cielist;
  unsigned int ncielist = 0;
  struct fdelist *fdelist = NULL;
  unsigned int nfdelist = 0;

  if (dbg->sections[IDX_eh_frame].addr == NULL)
    return DW_DLV_NO_ENTRY;

  readp = (Dwarf_Small *) dbg->sections[IDX_eh_frame].addr;
  readendp = readp + dbg->sections[IDX_eh_frame].size;

  while (readp < readendp)
    {
      /* Each CIE contains the following:

	 1. CIE_length (initial length)

	 A constant that gives the number of bytes of the CIE
	 structure, not including the length field, itself [...].

	 2. CIE_id

	 A constant that is used to distinguish CIEs from FDEs.

	 3. version (ubyte) [...]

	 4. augmentation (array of ubyte)

	 A null-terminated string that identifies the augmentation to
	 this CIE or to the FDEs that use it.

	 5. code_alignment_factor (unsigned LEB128)

	 A constant that is factored out of all advance location
	 instructions (see below).

	 6. data_alignment_factor (signed LEB128)

	 A constant that is factored out of all offset instructions
	 [...].

	 7. return_address_register (ubyte)

	 A constant that indicates which column in the rule table
	 represents the return address of the function.

	 8. initial_instructions (array of ubyte) [...] */
      Dwarf_Small *fde_cie_start;
      Dwarf_Small *readstartp;
      Dwarf_Small *cie_id_ptr;
      Dwarf_Unsigned length;
      unsigned int address_size;
      Dwarf_Unsigned start_offset;
      Dwarf_Unsigned cie_id;

      /* Remember where this entry started.  */
      fde_cie_start = readp;
      start_offset = (readp
		      - (Dwarf_Small *) dbg->sections[IDX_eh_frame].addr);

      length = read_4ubyte_unaligned (dbg, readp);
      address_size = 4;
      readp += 4;
      if (length == 0xffffffff)
	{
	  length = read_8ubyte_unaligned (dbg, readp);
	  readp += 8;
	  address_size = 8;
	}
      readstartp = readp;

      /* Requirement from the DWARF specification.  */
      if (unlikely (length % address_size != 0))
	{
	  /* XXX Free resources.  */
	  __libdwarf_error (dbg, error, DW_E_INVALID_DWARF);
	  return DW_DLV_ERROR;
	}

      /* No more entries.  */
      if (length == 0)
	break;

      cie_id_ptr = readp;
      if (address_size == 4)
	{
	  cie_id = read_4sbyte_unaligned (dbg, readp);
	  readp += 4;
	}
      else
	{
	  cie_id = read_8sbyte_unaligned (dbg, readp);
	  readp += 8;
	}

      /* Now we can distinguish between CIEs and FDEs.  gcc uses 0 to
	 signal the record is a CIE.  */
      if (cie_id == 0)
	{
	  char *augmentation;
	  Dwarf_Unsigned code_alignment_factor;
	  Dwarf_Signed data_alignment_factor;
	  Dwarf_Small *initial_instructions;
	  Dwarf_Small return_address_register;
	  struct cielist *new_cie;

	  if (unlikely (*readp++ != CIE_VERSION))
	    {
	      __libdwarf_error (dbg, error, DW_E_INVALID_DWARF);
	      return DW_DLV_ERROR;
	    }

	  augmentation = (char *) readp;
	  readp += strlen (augmentation) + 1;

	  if (strcmp (augmentation, "") == 0)
	    {
	      get_uleb128 (code_alignment_factor, readp);
	      get_sleb128 (data_alignment_factor, readp);
	      return_address_register = *readp++;
	      initial_instructions = readp;
	    }
	  else if (strcmp (augmentation, "eh") == 0)
	    {
	      /* GCC exception handling.  It has an extra field next
		 which is the address of a exception table.  We ignore
		 this value since it's only used at runtime by the
		 exception handling.  */
	      readp += address_size;

	      /* Now the standard fields.  */
	      get_uleb128 (code_alignment_factor, readp);
	      get_sleb128 (data_alignment_factor, readp);
	      return_address_register = *readp++;
	      initial_instructions = readp;
	    }
	  else
	    {
	      /* We don't know this augmentation.  Skip the rest.  The
		 specification says that nothing after the augmentation
		 string is usable.  */
	      code_alignment_factor = 0;
	      data_alignment_factor = 0;
	      return_address_register = 0;
	      initial_instructions = NULL;
	    }

	  /* Go to the next record.  */
	  readp = readstartp + length;

	  /* Create the new CIE record.  */
	  new_cie = (struct cielist *) alloca (sizeof (struct cielist));
	  new_cie->cie = (Dwarf_Cie) malloc (sizeof (struct Dwarf_Cie_s));
	  if (new_cie->cie == NULL)
	    {
	      __libdwarf_error (dbg, error, DW_E_NOMEM);
	      return DW_DLV_ERROR;
	    }

	  new_cie->cie->dbg = dbg;
	  new_cie->cie->length = length;
	  new_cie->cie->augmentation = augmentation;
	  new_cie->cie->code_alignment_factor = code_alignment_factor;
	  new_cie->cie->data_alignment_factor = data_alignment_factor;
	  new_cie->cie->return_address_register = return_address_register;
	  new_cie->cie->initial_instructions = initial_instructions;
	  new_cie->cie->initial_instructions_length =
	    readp - initial_instructions;

	  new_cie->cie->offset = start_offset;
	  new_cie->cie->index = ncielist;
	  new_cie->next = cielist;
	  cielist = new_cie;
	  ++ncielist;
	}
      else
	{
	  Dwarf_Addr initial_location;
	  Dwarf_Unsigned address_range;
	  Dwarf_Small *instructions;
	  struct fdelist *new_fde;
	  struct cielist *cie;

	  if (address_size == 4)
	    {
	      initial_location = read_4ubyte_unaligned (dbg, readp);
	      readp += 4;
	      address_range = read_4ubyte_unaligned (dbg, readp);
	      readp += 4;
	    }
	  else
	    {
	      initial_location = read_8ubyte_unaligned (dbg, readp);
	      readp += 8;
	      address_range = read_8ubyte_unaligned (dbg, readp);
	      readp += 8;
	    }

	  instructions = readp;

	  /* Go to the next record.  */
	  readp = readstartp + length;

	  /* Create the new FDE record.  */
	  new_fde = (struct fdelist *) alloca (sizeof (struct fdelist));
	  new_fde->fde = (Dwarf_Fde) malloc (sizeof (struct Dwarf_Fde_s));
	  if (new_fde->fde == NULL)
	    {
	      __libdwarf_error (dbg, error, DW_E_NOMEM);
	      return DW_DLV_ERROR;
	    }

	  new_fde->fde->initial_location = initial_location;
	  new_fde->fde->address_range = address_range;
	  new_fde->fde->instructions = instructions;
	  new_fde->fde->instructions_length = readp - instructions;
	  new_fde->fde->fde_bytes = fde_cie_start;
	  new_fde->fde->fde_byte_length = readstartp + length - fde_cie_start;
	  new_fde->fde->cie = NULL;
	  new_fde->cie_id_ptr = cie_id_ptr;

	  for (cie = cielist; cie != NULL; cie = cie->next)
	    /* This test takes the non-standard way of using the CIE ID
	       in the GNU .eh_frame sectio into account.  Instead of being
	       a direct offset in the section it is a offset from the
	       location of the FDE'S CIE ID value itself to the CIE entry.  */
	    if (cie->cie->offset
		== (size_t) (cie_id_ptr - cie_id
			     - (Dwarf_Small *) dbg->sections[IDX_eh_frame].addr))
	      {
		new_fde->fde->cie = cie->cie;
		break;
	      }

	  new_fde->fde->offset = cie_id;
	  new_fde->next = fdelist;
	  fdelist = new_fde;
	  ++nfdelist;
	}
    }

  /* There must always be at least one CIE.  */
  if (unlikely (ncielist == 0))
    {
      __libdwarf_error (dbg, error, DW_E_INVALID_DWARF);
      return DW_DLV_ERROR;
    }

  /* Create the lists.  */
  *cie_data = (Dwarf_Cie *) malloc (ncielist * sizeof (struct Dwarf_Cie_s));
  if (nfdelist > 0)
    *fde_data = (Dwarf_Fde *) malloc (nfdelist * sizeof (struct Dwarf_Fde_s));
  else
    *fde_data = NULL;
  if ((nfdelist > 0 && *fde_data == NULL) || *cie_data == NULL)
    {
      __libdwarf_error (dbg, error, DW_E_NOMEM);
      return DW_DLV_ERROR;
    }

  /* Remember the counts.  */
  dbg->fde_cnt = nfdelist;
  dbg->cie_cnt = ncielist;

  /* Add all the CIEs.  */
  copy_cielist = cielist;
  *cie_element_count = ncielist;
  while (ncielist-- > 0)
    {
      (*cie_data)[ncielist] = cielist->cie;
      cielist = cielist->next;
    }

  /* Add all the FDEs.  */
  *fde_element_count = nfdelist;
  while (nfdelist-- > 0)
    {
      (*fde_data)[nfdelist] = fdelist->fde;

      if (fdelist->fde->cie == NULL)
	{
	  /* We have not yet found the CIE.  Search now that we know
             about all of them.  */
	  cielist = copy_cielist;
	  do
	    {
	      if (cielist->cie->offset
		  == (size_t) (fdelist->cie_id_ptr - fdelist->fde->offset
			       - (Dwarf_Small *) dbg->sections[IDX_eh_frame].addr))
		{
		  fdelist->fde->cie = cielist->cie;
		  break;
		}
	      cielist = cielist->next;
	    }
	  while (cielist != NULL);

	  if (cielist == NULL)
	    {
	      /* There is no matching CIE.  This is bad.  */
	      /* XXX Free everything.  */
	      __libdwarf_error (dbg, error, DW_E_INVALID_DWARF);
	      return DW_DLV_ERROR;
	    }
	}

      fdelist = fdelist->next;
    }

  return DW_DLV_OK;
}