Make sect_offset and cu_offset strong typedefs instead of structs
A while ago, back when GDB was a C program, the sect_offset and cu_offset types were made structs in order to prevent incorrect mixing of those offsets. Now that we require C++11, we can make them integers again, while keeping the safety, by exploiting "enum class". We can add a bit more safety, even, by defining operators that the types _should_ support, helping making the suspicious uses stand out more. Getting at the underlying type is done with the new to_underlying function added by the previous patch, which also helps better spot where do we need to step out of the safety net. Mostly, that's around parsing the DWARF, and when we print the offset for complaint/debug purposes. But there are other occasional uses. Since we have to define the sect_offset/cu_offset types in a header anyway, I went ahead and generalized/library-fied the idea of "offset" types, making it trivial to add more such types if we find a use. See common/offset-type.h and the DEFINE_OFFSET_TYPE macro. I needed a couple generaly-useful preprocessor bits (e.g., yet another CONCAT implementation), so I started a new common/preprocessor.h file. I included units tests covering the "offset" types API. These are mostly compile-time tests, using SFINAE to check that expressions that shouldn't compile (e.g., comparing unrelated offset types) really are invalid and would fail to compile. This same idea appeared in my pending enum-flags revamp from a few months ago (though this version is a bit further modernized compared to what I had posted), and I plan on reusing the "check valid expression" bits added here in that series, so I went ahead and defined the CHECK_VALID_EXPR macro in its own header -- common/valid-expr.h. I think that's nicer regardless. I was borderline between calling the new types "offset" types, or "index" types, BTW. I stuck with "offset" simply because that's what we're already calling them, mostly. gdb/ChangeLog: 2017-04-04 Pedro Alves <palves@redhat.com> * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add unittests/offset-type-selftests.c. (SUBDIR_UNITTESTS_OBS): Add offset-type-selftests.o. * common/offset-type.h: New file. * common/preprocessor.h: New file. * common/traits.h: New file. * common/valid-expr.h: New file. * dwarf2expr.c: Include "common/underlying.h". Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2expr.h: Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2loc.c: Include "common/underlying.h". Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2read.c: Adjust to use sect_offset and cu_offset strong typedefs throughout. * gdbtypes.h: Include "common/offset-type.h". (cu_offset): Now an offset type (strong typedef) instead of a struct. (sect_offset): Likewise. (union call_site_parameter_u): Rename "param_offset" field to "param_cu_off". * unittests/offset-type-selftests.c: New file.
This commit is contained in:
parent
ecfb656c37
commit
9c54172556
@ -1,3 +1,28 @@
|
||||
2017-04-04 Pedro Alves <palves@redhat.com>
|
||||
|
||||
* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
|
||||
unittests/offset-type-selftests.c.
|
||||
(SUBDIR_UNITTESTS_OBS): Add offset-type-selftests.o.
|
||||
* common/offset-type.h: New file.
|
||||
* common/preprocessor.h: New file.
|
||||
* common/traits.h: New file.
|
||||
* common/valid-expr.h: New file.
|
||||
* dwarf2expr.c: Include "common/underlying.h". Adjust to use
|
||||
sect_offset and cu_offset strong typedefs throughout.
|
||||
* dwarf2expr.h: Adjust to use sect_offset and cu_offset strong
|
||||
typedefs throughout.
|
||||
* dwarf2loc.c: Include "common/underlying.h". Adjust to use
|
||||
sect_offset and cu_offset strong typedefs throughout.
|
||||
* dwarf2read.c: Adjust to use sect_offset and cu_offset strong
|
||||
typedefs throughout.
|
||||
* gdbtypes.h: Include "common/offset-type.h".
|
||||
(cu_offset): Now an offset type (strong typedef) instead of a
|
||||
struct.
|
||||
(sect_offset): Likewise.
|
||||
(union call_site_parameter_u): Rename "param_offset" field to
|
||||
"param_cu_off".
|
||||
* unittests/offset-type-selftests.c: New file.
|
||||
|
||||
2017-04-04 Pedro Alves <palves@redhat.com>
|
||||
|
||||
* common/underlying.h: New file.
|
||||
|
@ -524,10 +524,12 @@ SUBDIR_PYTHON_LDFLAGS =
|
||||
SUBDIR_PYTHON_CFLAGS =
|
||||
|
||||
SUBDIR_UNITTESTS_SRCS = \
|
||||
unittests/function-view-selftests.c
|
||||
unittests/function-view-selftests.c \
|
||||
unittests/offset-type-selftests.c
|
||||
|
||||
SUBDIR_UNITTESTS_OBS = \
|
||||
function-view-selftests.o
|
||||
function-view-selftests.o \
|
||||
offset-type-selftests.o
|
||||
|
||||
# Opcodes currently live in one of two places. Either they are in the
|
||||
# opcode library, typically ../opcodes, or they are in a header file
|
||||
|
149
gdb/common/offset-type.h
Normal file
149
gdb/common/offset-type.h
Normal file
@ -0,0 +1,149 @@
|
||||
/* Offset types for GDB.
|
||||
|
||||
Copyright (C) 2017 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GDB.
|
||||
|
||||
This program 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; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/* Define an "offset" type. Offset types are distinct integer types
|
||||
that are used to represent an offset into anything that is
|
||||
addressable. For example, an offset into a DWARF debug section.
|
||||
The idea is catch mixing unrelated offset types at compile time, in
|
||||
code that needs to manipulate multiple different kinds of offsets
|
||||
that are easily confused. They're safer to use than native
|
||||
integers, because they have no implicit conversion to anything.
|
||||
And also, since they're implemented as "enum class" strong
|
||||
typedefs, they're still integers ABI-wise, making them a bit more
|
||||
efficient than wrapper structs on some ABIs.
|
||||
|
||||
Some properties of offset types, loosely modeled on pointers:
|
||||
|
||||
- You can compare offsets of the same type for equality and order.
|
||||
You can't compare an offset with an unrelated type.
|
||||
|
||||
- You can add/substract an integer to/from an offset, which gives
|
||||
you back a shifted offset.
|
||||
|
||||
- You can subtract two offsets of the same type, which gives you
|
||||
back the delta as an integer (of the enum class's underlying
|
||||
type), not as an offset type.
|
||||
|
||||
- You can't add two offsets of the same type, as that would not
|
||||
make sense.
|
||||
|
||||
However, unlike pointers, you can't deference offset types. */
|
||||
|
||||
#ifndef COMMON_OFFSET_TYPE_H
|
||||
#define COMMON_OFFSET_TYPE_H
|
||||
|
||||
/* Declare TYPE as being an offset type. This declares the type and
|
||||
enables the operators defined below. */
|
||||
#define DEFINE_OFFSET_TYPE(TYPE, UNDERLYING) \
|
||||
enum class TYPE : UNDERLYING {}; \
|
||||
void is_offset_type (TYPE)
|
||||
|
||||
/* The macro macro is all you need to know use offset types. The rest
|
||||
below is all implementation detail. */
|
||||
|
||||
/* For each enum class type that you want to support relational
|
||||
operators, declare an "is_offset_type" overload that has exactly
|
||||
one parameter, of type that enum class. E.g.,:
|
||||
|
||||
void is_offset_type (sect_offset);
|
||||
|
||||
The function does not need to be defined, only declared.
|
||||
DEFINE_OFFSET_TYPE declares this.
|
||||
|
||||
A function declaration is preferred over a traits type, because the
|
||||
former allows calling the DEFINE_OFFSET_TYPE macro inside a
|
||||
namespace to define the corresponding offset type in that
|
||||
namespace. The compiler finds the corresponding is_offset_type
|
||||
function via ADL.
|
||||
*/
|
||||
|
||||
#define DEFINE_OFFSET_REL_OP(OP) \
|
||||
template<typename E, \
|
||||
typename = decltype (is_offset_type (std::declval<E> ()))> \
|
||||
constexpr bool \
|
||||
operator OP (E lhs, E rhs) \
|
||||
{ \
|
||||
using underlying = typename std::underlying_type<E>::type; \
|
||||
return (static_cast<underlying> (lhs) \
|
||||
OP static_cast<underlying> (lhs)); \
|
||||
}
|
||||
|
||||
DEFINE_OFFSET_REL_OP(>)
|
||||
DEFINE_OFFSET_REL_OP(>=)
|
||||
DEFINE_OFFSET_REL_OP(<)
|
||||
DEFINE_OFFSET_REL_OP(<=)
|
||||
|
||||
/* Adding or subtracting an integer to an offset type shifts the
|
||||
offset. This is like "PTR = PTR + INT" and "PTR += INT". */
|
||||
|
||||
#define DEFINE_OFFSET_ARITHM_OP(OP) \
|
||||
template<typename E, \
|
||||
typename = decltype (is_offset_type (std::declval<E> ()))> \
|
||||
constexpr E \
|
||||
operator OP (E lhs, typename std::underlying_type<E>::type rhs) \
|
||||
{ \
|
||||
using underlying = typename std::underlying_type<E>::type; \
|
||||
return (E) (static_cast<underlying> (lhs) OP rhs); \
|
||||
} \
|
||||
\
|
||||
template<typename E, \
|
||||
typename = decltype (is_offset_type (std::declval<E> ()))> \
|
||||
constexpr E \
|
||||
operator OP (typename std::underlying_type<E>::type lhs, E rhs) \
|
||||
{ \
|
||||
using underlying = typename std::underlying_type<E>::type; \
|
||||
return (E) (lhs OP static_cast<underlying> (rhs)); \
|
||||
} \
|
||||
\
|
||||
template<typename E, \
|
||||
typename = decltype (is_offset_type (std::declval<E> ()))> \
|
||||
E & \
|
||||
operator OP ## = (E &lhs, typename std::underlying_type<E>::type rhs) \
|
||||
{ \
|
||||
using underlying = typename std::underlying_type<E>::type; \
|
||||
lhs = (E) (static_cast<underlying> (lhs) OP rhs); \
|
||||
return lhs; \
|
||||
}
|
||||
|
||||
DEFINE_OFFSET_ARITHM_OP(+)
|
||||
DEFINE_OFFSET_ARITHM_OP(-)
|
||||
|
||||
/* Adding two offset types doesn't make sense, just like "PTR + PTR"
|
||||
doesn't make sense. This is defined as a deleted function so that
|
||||
a compile error easily brings you to this comment. */
|
||||
|
||||
template<typename E,
|
||||
typename = decltype (is_offset_type (std::declval<E> ()))>
|
||||
constexpr typename std::underlying_type<E>::type
|
||||
operator+ (E lhs, E rhs) = delete;
|
||||
|
||||
/* Subtracting two offset types, however, gives you back the
|
||||
difference between the offsets, as an underlying type. Similar to
|
||||
how "PTR2 - PTR1" returns a ptrdiff_t. */
|
||||
|
||||
template<typename E,
|
||||
typename = decltype (is_offset_type (std::declval<E> ()))>
|
||||
constexpr typename std::underlying_type<E>::type
|
||||
operator- (E lhs, E rhs)
|
||||
{
|
||||
using underlying = typename std::underlying_type<E>::type;
|
||||
return static_cast<underlying> (lhs) - static_cast<underlying> (rhs);
|
||||
}
|
||||
|
||||
#endif /* COMMON_OFFSET_TYPE_H */
|
31
gdb/common/preprocessor.h
Normal file
31
gdb/common/preprocessor.h
Normal file
@ -0,0 +1,31 @@
|
||||
/* Copyright (C) 2017 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GDB.
|
||||
|
||||
This program 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; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
#ifndef COMMON_PREPROC_H
|
||||
#define COMMON_PREPROC_H
|
||||
|
||||
/* Generally useful preprocessor bits. */
|
||||
|
||||
/* Concatenate two tokens. */
|
||||
#define CONCAT_1(a, b) a ## b
|
||||
#define CONCAT(a, b) CONCAT_1 (a, b)
|
||||
|
||||
/* Escape parens out. Useful if you need to pass an argument that
|
||||
includes commas to another macro. */
|
||||
#define ESC(...) __VA_ARGS__
|
||||
|
||||
#endif /* COMMON_PREPROC */
|
34
gdb/common/traits.h
Normal file
34
gdb/common/traits.h
Normal file
@ -0,0 +1,34 @@
|
||||
/* Copyright (C) 2017 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GDB.
|
||||
|
||||
This program 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; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
#ifndef COMMON_TRAITS_H
|
||||
#define COMMON_TRAITS_H
|
||||
|
||||
namespace gdb {
|
||||
|
||||
/* Pre C++14-safe (CWG 1558) version of C++17's std::void_t. See
|
||||
<http://en.cppreference.com/w/cpp/types/void_t>. */
|
||||
|
||||
template<typename... Ts>
|
||||
struct make_void { typedef void type; };
|
||||
|
||||
template<typename... Ts>
|
||||
using void_t = typename make_void<Ts...>::type;
|
||||
|
||||
}
|
||||
|
||||
#endif /* COMMON_TRAITS_H */
|
108
gdb/common/valid-expr.h
Normal file
108
gdb/common/valid-expr.h
Normal file
@ -0,0 +1,108 @@
|
||||
/* Compile-time valid expression checker for GDB, the GNU debugger.
|
||||
|
||||
Copyright (C) 2017 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GDB.
|
||||
|
||||
This program 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; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/* Helper macros used to build compile-time unit tests that make sure
|
||||
that invalid expressions that should not compile would not compile,
|
||||
and that expressions that should compile do compile, and have the
|
||||
right type. This is mainly used to verify that some utility's API
|
||||
is really as safe as intended. */
|
||||
|
||||
#ifndef COMMON_VALID_EXPR_H
|
||||
#define COMMON_VALID_EXPR_H
|
||||
|
||||
#include "common/preprocessor.h"
|
||||
#include "common/traits.h"
|
||||
|
||||
/* Macro that uses SFINAE magic to detect whether the EXPR expression
|
||||
is either valid or ill-formed, at compile time, without actually
|
||||
producing compile-time errors. I.e., check that bad uses of the
|
||||
types (e.g., involving mismatching types) would be caught at
|
||||
compile time. If the expression is valid, also check whether the
|
||||
expression has the right type.
|
||||
|
||||
EXPR must be defined in terms of some of the template parameters,
|
||||
so that template substitution failure discards the overload instead
|
||||
of causing a real compile error. TYPES is thus the list of types
|
||||
involved in the expression, and TYPENAMES is the same list, but
|
||||
with each element prefixed by "typename". These are passed as
|
||||
template parameter types to the templates within the macro.
|
||||
|
||||
VALID is a boolean that indicates whether the expression is
|
||||
supposed to be valid or invalid.
|
||||
|
||||
EXPR_TYPE is the expected type of EXPR. Only meaningful iff VALID
|
||||
is true. If VALID is false, then you must pass "void" as expected
|
||||
type.
|
||||
|
||||
Each invocation of the macro is wrapped in its own namespace to
|
||||
avoid ODR violations. The generated namespace only includes the
|
||||
line number, so client code should wrap sets of calls in a
|
||||
test-specific namespace too, to fully guarantee uniqueness between
|
||||
the multiple clients in the codebase. */
|
||||
#define CHECK_VALID_EXPR_INT(TYPENAMES, TYPES, VALID, EXPR_TYPE, EXPR) \
|
||||
namespace CONCAT (check_valid_expr, __LINE__) { \
|
||||
\
|
||||
template<typename, typename, typename = void> \
|
||||
struct is_valid_expression \
|
||||
: std::false_type {}; \
|
||||
\
|
||||
template <TYPENAMES> \
|
||||
struct is_valid_expression<TYPES, gdb::void_t<decltype (EXPR)>> \
|
||||
: std::true_type {}; \
|
||||
\
|
||||
static_assert (is_valid_expression<TYPES>::value == VALID, \
|
||||
""); \
|
||||
\
|
||||
template<TYPENAMES, typename = void> \
|
||||
struct is_same_type \
|
||||
: std::is_same<EXPR_TYPE, void> {}; \
|
||||
\
|
||||
template <TYPENAMES> \
|
||||
struct is_same_type<TYPES, gdb::void_t<decltype (EXPR)>> \
|
||||
: std::is_same<EXPR_TYPE, decltype (EXPR)> {}; \
|
||||
\
|
||||
static_assert (is_same_type<TYPES>::value, ""); \
|
||||
} /* namespace */
|
||||
|
||||
/* A few convenience macros that support expressions involving a
|
||||
varying numbers of types. If you need more types, feel free to add
|
||||
another variant. */
|
||||
|
||||
#define CHECK_VALID_EXPR_1(T1, VALID, EXPR_TYPE, EXPR) \
|
||||
CHECK_VALID_EXPR_INT (ESC (typename T1), \
|
||||
ESC (T1), \
|
||||
VALID, EXPR_TYPE, EXPR)
|
||||
|
||||
#define CHECK_VALID_EXPR_2(T1, T2, VALID, EXPR_TYPE, EXPR) \
|
||||
CHECK_VALID_EXPR_INT (ESC (typename T1, typename T2), \
|
||||
ESC (T1, T2), \
|
||||
VALID, EXPR_TYPE, EXPR)
|
||||
|
||||
#define CHECK_VALID_EXPR_3(T1, T2, T3, VALID, EXPR_TYPE, EXPR) \
|
||||
CHECK_VALID_EXPR_INT (ESC (typename T1, typename T2, typename T3), \
|
||||
ESC (T1, T2, T3), \
|
||||
VALID, EXPR_TYPE, EXPR)
|
||||
|
||||
#define CHECK_VALID_EXPR_4(T1, T2, T3, T4, VALID, EXPR_TYPE, EXPR) \
|
||||
CHECK_VALID_EXPR_INT (ESC (typename T1, typename T2, \
|
||||
typename T3, typename T4), \
|
||||
ESC (T1, T2, T3, T4), \
|
||||
VALID, EXPR_TYPE, EXPR)
|
||||
|
||||
#endif /* COMMON_VALID_EXPR_H */
|
@ -27,6 +27,7 @@
|
||||
#include "dwarf2.h"
|
||||
#include "dwarf2expr.h"
|
||||
#include "dwarf2loc.h"
|
||||
#include "common/underlying.h"
|
||||
|
||||
/* Cookie for gdbarch data. */
|
||||
|
||||
@ -317,7 +318,7 @@ dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset)
|
||||
}
|
||||
else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
|
||||
{
|
||||
p->v.ptr.die.sect_off = this->len;
|
||||
p->v.ptr.die_sect_off = (sect_offset) this->len;
|
||||
p->v.ptr.offset = value_as_long (fetch (0));
|
||||
}
|
||||
else if (p->location == DWARF_VALUE_REGISTER)
|
||||
@ -976,11 +977,9 @@ dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
|
||||
|
||||
if (op == DW_OP_deref_type || op == DW_OP_GNU_deref_type)
|
||||
{
|
||||
cu_offset type_die;
|
||||
|
||||
op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
|
||||
type_die.cu_off = uoffset;
|
||||
type = get_base_type (type_die, 0);
|
||||
cu_offset type_die_cu_off = (cu_offset) uoffset;
|
||||
type = get_base_type (type_die_cu_off, 0);
|
||||
}
|
||||
else
|
||||
type = address_type;
|
||||
@ -1283,21 +1282,19 @@ dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
|
||||
|
||||
case DW_OP_call2:
|
||||
{
|
||||
cu_offset offset;
|
||||
|
||||
offset.cu_off = extract_unsigned_integer (op_ptr, 2, byte_order);
|
||||
cu_offset cu_off
|
||||
= (cu_offset) extract_unsigned_integer (op_ptr, 2, byte_order);
|
||||
op_ptr += 2;
|
||||
this->dwarf_call (offset);
|
||||
this->dwarf_call (cu_off);
|
||||
}
|
||||
goto no_push;
|
||||
|
||||
case DW_OP_call4:
|
||||
{
|
||||
cu_offset offset;
|
||||
|
||||
offset.cu_off = extract_unsigned_integer (op_ptr, 4, byte_order);
|
||||
cu_offset cu_off
|
||||
= (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
|
||||
op_ptr += 4;
|
||||
this->dwarf_call (offset);
|
||||
this->dwarf_call (cu_off);
|
||||
}
|
||||
goto no_push;
|
||||
|
||||
@ -1344,8 +1341,8 @@ dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
|
||||
{
|
||||
union call_site_parameter_u kind_u;
|
||||
|
||||
kind_u.param_offset.cu_off = extract_unsigned_integer (op_ptr, 4,
|
||||
byte_order);
|
||||
kind_u.param_cu_off
|
||||
= (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
|
||||
op_ptr += 4;
|
||||
this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET,
|
||||
kind_u,
|
||||
@ -1356,18 +1353,18 @@ dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
|
||||
case DW_OP_const_type:
|
||||
case DW_OP_GNU_const_type:
|
||||
{
|
||||
cu_offset type_die;
|
||||
int n;
|
||||
const gdb_byte *data;
|
||||
struct type *type;
|
||||
|
||||
op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
|
||||
type_die.cu_off = uoffset;
|
||||
cu_offset type_die_cu_off = (cu_offset) uoffset;
|
||||
|
||||
n = *op_ptr++;
|
||||
data = op_ptr;
|
||||
op_ptr += n;
|
||||
|
||||
type = get_base_type (type_die, n);
|
||||
type = get_base_type (type_die_cu_off, n);
|
||||
result_val = value_from_contents (type, data);
|
||||
}
|
||||
break;
|
||||
@ -1375,14 +1372,13 @@ dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
|
||||
case DW_OP_regval_type:
|
||||
case DW_OP_GNU_regval_type:
|
||||
{
|
||||
cu_offset type_die;
|
||||
struct type *type;
|
||||
|
||||
op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
|
||||
op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
|
||||
type_die.cu_off = uoffset;
|
||||
cu_offset type_die_cu_off = (cu_offset) uoffset;
|
||||
|
||||
type = get_base_type (type_die, 0);
|
||||
type = get_base_type (type_die_cu_off, 0);
|
||||
result_val = this->get_reg_value (type, reg);
|
||||
}
|
||||
break;
|
||||
@ -1392,16 +1388,15 @@ dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
|
||||
case DW_OP_reinterpret:
|
||||
case DW_OP_GNU_reinterpret:
|
||||
{
|
||||
cu_offset type_die;
|
||||
struct type *type;
|
||||
|
||||
op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
|
||||
type_die.cu_off = uoffset;
|
||||
cu_offset type_die_cu_off = (cu_offset) uoffset;
|
||||
|
||||
if (type_die.cu_off == 0)
|
||||
if (to_underlying (type_die_cu_off) == 0)
|
||||
type = address_type;
|
||||
else
|
||||
type = get_base_type (type_die, 0);
|
||||
type = get_base_type (type_die_cu_off, 0);
|
||||
|
||||
result_val = fetch (0);
|
||||
pop ();
|
||||
|
@ -172,16 +172,16 @@ struct dwarf_expr_context
|
||||
virtual CORE_ADDR get_tls_address (CORE_ADDR offset) = 0;
|
||||
|
||||
/* Execute DW_AT_location expression for the DWARF expression
|
||||
subroutine in the DIE at DIE_OFFSET in the CU. Do not touch
|
||||
subroutine in the DIE at DIE_CU_OFF in the CU. Do not touch
|
||||
STACK while it being passed to and returned from the called DWARF
|
||||
subroutine. */
|
||||
virtual void dwarf_call (cu_offset die_offset) = 0;
|
||||
virtual void dwarf_call (cu_offset die_cu_off) = 0;
|
||||
|
||||
/* Return the base type given by the indicated DIE. This can throw
|
||||
an exception if the DIE is invalid or does not represent a base
|
||||
type. SIZE is non-zero if this function should verify that the
|
||||
resulting type has the correct size. */
|
||||
virtual struct type *get_base_type (cu_offset die, int size)
|
||||
/* Return the base type given by the indicated DIE at DIE_CU_OFF.
|
||||
This can throw an exception if the DIE is invalid or does not
|
||||
represent a base type. SIZE is non-zero if this function should
|
||||
verify that the resulting type has the correct size. */
|
||||
virtual struct type *get_base_type (cu_offset die_cu_off, int size)
|
||||
{
|
||||
/* Anything will do. */
|
||||
return builtin_type (this->gdbarch)->builtin_int;
|
||||
@ -249,7 +249,7 @@ struct dwarf_expr_piece
|
||||
struct
|
||||
{
|
||||
/* The referent DIE from DW_OP_implicit_pointer. */
|
||||
sect_offset die;
|
||||
sect_offset die_sect_off;
|
||||
/* The byte offset into the resulting data. */
|
||||
LONGEST offset;
|
||||
} ptr;
|
||||
|
@ -42,6 +42,7 @@
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <unordered_set>
|
||||
#include "common/underlying.h"
|
||||
|
||||
extern int dwarf_always_disassemble;
|
||||
|
||||
@ -1191,7 +1192,7 @@ call_site_parameter_matches (struct call_site_parameter *parameter,
|
||||
case CALL_SITE_PARAMETER_FB_OFFSET:
|
||||
return kind_u.fb_offset == parameter->u.fb_offset;
|
||||
case CALL_SITE_PARAMETER_PARAM_OFFSET:
|
||||
return kind_u.param_offset.cu_off == parameter->u.param_offset.cu_off;
|
||||
return kind_u.param_cu_off == parameter->u.param_cu_off;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -2255,7 +2256,8 @@ indirect_pieced_value (struct value *value)
|
||||
TYPE_LENGTH (type), byte_order);
|
||||
byte_offset += piece->v.ptr.offset;
|
||||
|
||||
return indirect_synthetic_pointer (piece->v.ptr.die, byte_offset, c->per_cu,
|
||||
return indirect_synthetic_pointer (piece->v.ptr.die_sect_off,
|
||||
byte_offset, c->per_cu,
|
||||
frame, type);
|
||||
}
|
||||
|
||||
@ -2280,7 +2282,7 @@ coerce_pieced_ref (const struct value *value)
|
||||
gdb_assert (closure != NULL);
|
||||
gdb_assert (closure->n_pieces == 1);
|
||||
|
||||
return indirect_synthetic_pointer (closure->pieces->v.ptr.die,
|
||||
return indirect_synthetic_pointer (closure->pieces->v.ptr.die_sect_off,
|
||||
closure->pieces->v.ptr.offset,
|
||||
closure->per_cu, frame, type);
|
||||
}
|
||||
@ -3642,12 +3644,11 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
|
||||
{
|
||||
struct dwarf2_locexpr_baton block;
|
||||
int size = (op == DW_OP_call2 ? 2 : 4);
|
||||
cu_offset offset;
|
||||
|
||||
uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
|
||||
op_ptr += size;
|
||||
|
||||
offset.cu_off = uoffset;
|
||||
cu_offset offset = (cu_offset) uoffset;
|
||||
block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
|
||||
get_ax_pc, expr);
|
||||
|
||||
@ -4192,15 +4193,15 @@ disassemble_dwarf_expression (struct ui_file *stream,
|
||||
case DW_OP_GNU_deref_type:
|
||||
{
|
||||
int addr_size = *data++;
|
||||
cu_offset offset;
|
||||
struct type *type;
|
||||
|
||||
data = safe_read_uleb128 (data, end, &ul);
|
||||
offset.cu_off = ul;
|
||||
cu_offset offset = (cu_offset) ul;
|
||||
type = dwarf2_get_die_type (offset, per_cu);
|
||||
fprintf_filtered (stream, "<");
|
||||
type_print (type, "", stream, -1);
|
||||
fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
|
||||
fprintf_filtered (stream, " [0x%s]> %d",
|
||||
phex_nz (to_underlying (offset), 0),
|
||||
addr_size);
|
||||
}
|
||||
break;
|
||||
@ -4208,15 +4209,15 @@ disassemble_dwarf_expression (struct ui_file *stream,
|
||||
case DW_OP_const_type:
|
||||
case DW_OP_GNU_const_type:
|
||||
{
|
||||
cu_offset type_die;
|
||||
struct type *type;
|
||||
|
||||
data = safe_read_uleb128 (data, end, &ul);
|
||||
type_die.cu_off = ul;
|
||||
cu_offset type_die = (cu_offset) ul;
|
||||
type = dwarf2_get_die_type (type_die, per_cu);
|
||||
fprintf_filtered (stream, "<");
|
||||
type_print (type, "", stream, -1);
|
||||
fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
|
||||
fprintf_filtered (stream, " [0x%s]>",
|
||||
phex_nz (to_underlying (type_die), 0));
|
||||
}
|
||||
break;
|
||||
|
||||
@ -4224,18 +4225,17 @@ disassemble_dwarf_expression (struct ui_file *stream,
|
||||
case DW_OP_GNU_regval_type:
|
||||
{
|
||||
uint64_t reg;
|
||||
cu_offset type_die;
|
||||
struct type *type;
|
||||
|
||||
data = safe_read_uleb128 (data, end, ®);
|
||||
data = safe_read_uleb128 (data, end, &ul);
|
||||
type_die.cu_off = ul;
|
||||
cu_offset type_die = (cu_offset) ul;
|
||||
|
||||
type = dwarf2_get_die_type (type_die, per_cu);
|
||||
fprintf_filtered (stream, "<");
|
||||
type_print (type, "", stream, -1);
|
||||
fprintf_filtered (stream, " [0x%s]> [$%s]",
|
||||
phex_nz (type_die.cu_off, 0),
|
||||
phex_nz (to_underlying (type_die), 0),
|
||||
locexpr_regname (arch, reg));
|
||||
}
|
||||
break;
|
||||
@ -4245,12 +4245,10 @@ disassemble_dwarf_expression (struct ui_file *stream,
|
||||
case DW_OP_reinterpret:
|
||||
case DW_OP_GNU_reinterpret:
|
||||
{
|
||||
cu_offset type_die;
|
||||
|
||||
data = safe_read_uleb128 (data, end, &ul);
|
||||
type_die.cu_off = ul;
|
||||
cu_offset type_die = (cu_offset) ul;
|
||||
|
||||
if (type_die.cu_off == 0)
|
||||
if (to_underlying (type_die) == 0)
|
||||
fprintf_filtered (stream, "<0>");
|
||||
else
|
||||
{
|
||||
@ -4259,7 +4257,8 @@ disassemble_dwarf_expression (struct ui_file *stream,
|
||||
type = dwarf2_get_die_type (type_die, per_cu);
|
||||
fprintf_filtered (stream, "<");
|
||||
type_print (type, "", stream, -1);
|
||||
fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
|
||||
fprintf_filtered (stream, " [0x%s]>",
|
||||
phex_nz (to_underlying (type_die), 0));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
641
gdb/dwarf2read.c
641
gdb/dwarf2read.c
File diff suppressed because it is too large
Load Diff
@ -45,6 +45,7 @@
|
||||
*/
|
||||
|
||||
#include "hashtab.h"
|
||||
#include "common/offset-type.h"
|
||||
|
||||
/* Forward declarations for prototypes. */
|
||||
struct field;
|
||||
@ -57,18 +58,11 @@ struct language_defn;
|
||||
|
||||
/* * Offset relative to the start of its containing CU (compilation
|
||||
unit). */
|
||||
typedef struct
|
||||
{
|
||||
unsigned int cu_off;
|
||||
} cu_offset;
|
||||
DEFINE_OFFSET_TYPE (cu_offset, unsigned int);
|
||||
|
||||
/* * Offset relative to the start of its .debug_info or .debug_types
|
||||
section. */
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned int sect_off;
|
||||
} sect_offset;
|
||||
DEFINE_OFFSET_TYPE (sect_offset, unsigned int);
|
||||
|
||||
/* Some macros for char-based bitfields. */
|
||||
|
||||
@ -1108,7 +1102,7 @@ union call_site_parameter_u
|
||||
DW_TAG_formal_parameter which is referenced by both
|
||||
caller and the callee. */
|
||||
|
||||
cu_offset param_offset;
|
||||
cu_offset param_cu_off;
|
||||
};
|
||||
|
||||
struct call_site_parameter
|
||||
|
178
gdb/unittests/offset-type-selftests.c
Normal file
178
gdb/unittests/offset-type-selftests.c
Normal file
@ -0,0 +1,178 @@
|
||||
/* Self tests for offset types for GDB, the GNU debugger.
|
||||
|
||||
Copyright (C) 2017 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GDB.
|
||||
|
||||
This program 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; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
#include "defs.h"
|
||||
#include "selftest.h"
|
||||
#include "common/offset-type.h"
|
||||
#include "common/underlying.h"
|
||||
#include "common/valid-expr.h"
|
||||
|
||||
namespace selftests {
|
||||
namespace offset_type {
|
||||
|
||||
DEFINE_OFFSET_TYPE (off_A, unsigned int);
|
||||
DEFINE_OFFSET_TYPE (off_B, unsigned int);
|
||||
|
||||
/* First, compile-time tests that:
|
||||
|
||||
- make sure that incorrect operations with mismatching types are
|
||||
caught at compile time.
|
||||
|
||||
- make sure that the same operations but involving the right types
|
||||
do compile and that they return the correct type.
|
||||
*/
|
||||
|
||||
#define CHECK_VALID(VALID, EXPR_TYPE, EXPR) \
|
||||
CHECK_VALID_EXPR_2 (off_A, off_B, VALID, EXPR_TYPE, EXPR)
|
||||
|
||||
off_A lval_a {};
|
||||
off_B lval_b {};
|
||||
|
||||
using undrl = std::underlying_type<off_A>::type;
|
||||
|
||||
/* Offset +/- underlying. */
|
||||
|
||||
CHECK_VALID (true, off_A, off_A {} + undrl {});
|
||||
CHECK_VALID (true, off_A, off_A {} - undrl {});
|
||||
CHECK_VALID (true, off_A, undrl {} + off_A {});
|
||||
CHECK_VALID (true, off_A, undrl {} - off_A {});
|
||||
|
||||
/* Add offset types. Both same and different. */
|
||||
|
||||
CHECK_VALID (false, void, off_A {} + off_A {});
|
||||
CHECK_VALID (false, void, off_A {} + off_B {});
|
||||
|
||||
/* Subtract offset types. Both same and different. */
|
||||
|
||||
CHECK_VALID (false, void, off_B {} - off_A {});
|
||||
CHECK_VALID (true, undrl, off_A {} - off_A {});
|
||||
|
||||
/* Add/assign offset types. Both same and different. */
|
||||
|
||||
CHECK_VALID (false, void, lval_a += off_A {});
|
||||
CHECK_VALID (false, void, lval_a += off_B {});
|
||||
CHECK_VALID (false, void, lval_a -= off_A {});
|
||||
CHECK_VALID (false, void, lval_a -= off_B {});
|
||||
|
||||
/* operator OP+= (offset, underlying), lvalue ref on the lhs. */
|
||||
|
||||
CHECK_VALID (true, off_A&, lval_a += undrl {});
|
||||
CHECK_VALID (true, off_A&, lval_a -= undrl {});
|
||||
|
||||
/* operator OP+= (offset, underlying), rvalue ref on the lhs. */
|
||||
|
||||
CHECK_VALID (false, void, off_A {} += undrl {});
|
||||
CHECK_VALID (false, void, off_A {} -= undrl {});
|
||||
|
||||
/* Rel ops, with same type. */
|
||||
|
||||
CHECK_VALID (true, bool, off_A {} < off_A {});
|
||||
CHECK_VALID (true, bool, off_A {} > off_A {});
|
||||
CHECK_VALID (true, bool, off_A {} <= off_A {});
|
||||
CHECK_VALID (true, bool, off_A {} >= off_A {});
|
||||
|
||||
/* Rel ops, with unrelated offset types. */
|
||||
|
||||
CHECK_VALID (false, void, off_A {} < off_B {});
|
||||
CHECK_VALID (false, void, off_A {} > off_B {});
|
||||
CHECK_VALID (false, void, off_A {} <= off_B {});
|
||||
CHECK_VALID (false, void, off_A {} >= off_B {});
|
||||
|
||||
/* Rel ops, with unrelated types. */
|
||||
|
||||
CHECK_VALID (false, void, off_A {} < undrl {});
|
||||
CHECK_VALID (false, void, off_A {} > undrl {});
|
||||
CHECK_VALID (false, void, off_A {} <= undrl {});
|
||||
CHECK_VALID (false, void, off_A {} >= undrl {});
|
||||
|
||||
static void
|
||||
run_tests ()
|
||||
{
|
||||
/* Test op+ and op-. */
|
||||
{
|
||||
constexpr off_A a {};
|
||||
static_assert (to_underlying (a) == 0, "");
|
||||
|
||||
{
|
||||
constexpr off_A res1 = a + 2;
|
||||
static_assert (to_underlying (res1) == 2, "");
|
||||
|
||||
constexpr off_A res2 = res1 - 1;
|
||||
static_assert (to_underlying (res2) == 1, "");
|
||||
}
|
||||
|
||||
{
|
||||
constexpr off_A res1 = 2 + a;
|
||||
static_assert (to_underlying (res1) == 2, "");
|
||||
|
||||
constexpr off_A res2 = 3 - res1;
|
||||
static_assert (to_underlying (res2) == 1, "");
|
||||
}
|
||||
}
|
||||
|
||||
/* Test op+= and op-=. */
|
||||
{
|
||||
off_A o {};
|
||||
|
||||
o += 10;
|
||||
SELF_CHECK (to_underlying (o) == 10);
|
||||
o -= 5;
|
||||
SELF_CHECK (to_underlying (o) == 5);
|
||||
}
|
||||
|
||||
/* Test op-. */
|
||||
{
|
||||
constexpr off_A o1 = (off_A) 10;
|
||||
constexpr off_A o2 = (off_A) 20;
|
||||
|
||||
constexpr unsigned int delta = o2 - o1;
|
||||
|
||||
static_assert (delta == 10, "");
|
||||
}
|
||||
|
||||
/* Test <, <=, >, >=. */
|
||||
{
|
||||
constexpr off_A o1 = (off_A) 10;
|
||||
constexpr off_A o2 = (off_A) 20;
|
||||
|
||||
static_assert (o1 < o2, "");
|
||||
static_assert (!(o2 < o1), "");
|
||||
|
||||
static_assert (o2 > o1, "");
|
||||
static_assert (!(o1 > o2), "");
|
||||
|
||||
static_assert (o1 <= o2, "");
|
||||
static_assert (!(o2 <= o1), "");
|
||||
|
||||
static_assert (o2 >= o1, "");
|
||||
static_assert (!(o1 >= o2), "");
|
||||
|
||||
static_assert (o1 <= o1, "");
|
||||
static_assert (o1 >= o1, "");
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace offset_type */
|
||||
} /* namespace selftests */
|
||||
|
||||
void
|
||||
_initialize_offset_type_selftests ()
|
||||
{
|
||||
register_self_test (selftests::offset_type::run_tests);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user