1 /* Helper functions for form handling.
2 Copyright (C) 2003-2009, 2014 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2003.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
42 __libdw_form_val_compute_len (struct Dwarf_CU *cu, unsigned int form,
43 const unsigned char *valp)
45 const unsigned char *startp = valp;
46 const unsigned char *endp = cu->endp;
50 /* NB: This doesn't cover constant form lengths, which are
51 already handled by the inlined __libdw_form_val_len. */
55 result = cu->address_size;
58 case DW_FORM_ref_addr:
59 result = cu->version == 2 ? cu->address_size : cu->offset_size;
63 case DW_FORM_sec_offset:
64 case DW_FORM_GNU_ref_alt:
65 case DW_FORM_GNU_strp_alt:
66 result = cu->offset_size;
70 if (unlikely ((size_t) (endp - startp) < 1))
76 if (unlikely ((size_t) (endp - startp) < 2))
78 result = read_2ubyte_unaligned (cu->dbg, valp) + 2;
82 if (unlikely ((size_t) (endp - startp) < 4))
84 result = read_4ubyte_unaligned (cu->dbg, valp) + 4;
89 get_uleb128 (u128, valp, endp);
90 result = u128 + (valp - startp);
95 const unsigned char *endstrp = memchr (valp, '\0',
96 (size_t) (endp - startp));
97 if (unlikely (endstrp == NULL))
99 result = (size_t) (endstrp - startp) + 1;
105 case DW_FORM_ref_udata:
106 get_uleb128 (u128, valp, endp);
107 result = valp - startp;
110 case DW_FORM_indirect:
111 get_uleb128 (u128, valp, endp);
112 // XXX Is this really correct?
113 result = __libdw_form_val_len (cu, u128, valp);
114 if (result != (size_t) -1)
115 result += valp - startp;
124 if (unlikely (result > (size_t) (endp - startp)))
127 __libdw_seterrno (DWARF_E_INVALID_DWARF);
128 result = (size_t) -1;