summaryrefslogtreecommitdiff
path: root/libdw/libdw_form.c
blob: 400454401b66ce298b8bdab643b14798eb6f0a56 (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
/* Helper functions for form handling.
   Copyright (C) 2003-2009, 2014 Red Hat, Inc.
   This file is part of elfutils.
   Written by Ulrich Drepper <drepper@redhat.com>, 2003.

   This file is free software; you can redistribute it and/or modify
   it under the terms of either

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at
       your option) any later version

   or both in parallel, as here.

   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 copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.  */

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

#include <dwarf.h>
#include <string.h>

#include "libdwP.h"


size_t
internal_function
__libdw_form_val_compute_len (struct Dwarf_CU *cu, unsigned int form,
			      const unsigned char *valp)
{
  const unsigned char *startp = valp;
  const unsigned char *endp = cu->endp;
  Dwarf_Word u128;
  size_t result;

  /* NB: This doesn't cover constant form lengths, which are
     already handled by the inlined __libdw_form_val_len.  */
  switch (form)
    {
    case DW_FORM_addr:
      result = cu->address_size;
      break;

    case DW_FORM_ref_addr:
      result = cu->version == 2 ? cu->address_size : cu->offset_size;
      break;

    case DW_FORM_strp:
    case DW_FORM_strp_sup:
    case DW_FORM_line_strp:
    case DW_FORM_sec_offset:
    case DW_FORM_GNU_ref_alt:
    case DW_FORM_GNU_strp_alt:
      result = cu->offset_size;
      break;

    case DW_FORM_block1:
      if (unlikely ((size_t) (endp - startp) < 1))
	goto invalid;
      result = *valp + 1;
      break;

    case DW_FORM_block2:
      if (unlikely ((size_t) (endp - startp) < 2))
	goto invalid;
      result = read_2ubyte_unaligned (cu->dbg, valp) + 2;
      break;

    case DW_FORM_block4:
      if (unlikely ((size_t) (endp - startp) < 4))
	goto invalid;
      result = read_4ubyte_unaligned (cu->dbg, valp) + 4;
      break;

    case DW_FORM_block:
    case DW_FORM_exprloc:
      if (valp >= endp)
       goto invalid;
      get_uleb128 (u128, valp, endp);
      result = u128 + (valp - startp);
      break;

    case DW_FORM_string:
      {
	const unsigned char *endstrp = memchr (valp, '\0',
					       (size_t) (endp - startp));
	if (unlikely (endstrp == NULL))
	  goto invalid;
	result = (size_t) (endstrp - startp) + 1;
	break;
      }

    case DW_FORM_sdata:
    case DW_FORM_udata:
    case DW_FORM_ref_udata:
    case DW_FORM_addrx:
    case DW_FORM_loclistx:
    case DW_FORM_rnglistx:
    case DW_FORM_strx:
    case DW_FORM_GNU_addr_index:
    case DW_FORM_GNU_str_index:
      if (valp >= endp)
       goto invalid;
      get_uleb128 (u128, valp, endp);
      result = valp - startp;
      break;

    case DW_FORM_indirect:
      /* The amount of data to skip in the DIE is the size of the actual
	 FORM data (which is __libdw_form_val_len) plus the size of the
	 uleb128 encoding that FORM (which is valp - startp).  */
      if (valp >= endp)
	goto invalid;
      get_uleb128 (u128, valp, endp);
      if (*valp == DW_FORM_indirect || *valp == DW_FORM_implicit_const)
	return (size_t) -1;
      result = __libdw_form_val_len (cu, u128, valp);
      if (result != (size_t) -1)
	result += valp - startp;
      else
        return (size_t) -1;
      break;

    default:
      goto invalid;
    }

  if (unlikely (result > (size_t) (endp - startp)))
    {
    invalid:
      __libdw_seterrno (DWARF_E_INVALID_DWARF);
      result = (size_t) -1;
    }

  return result;
}