| 1 | /* Abstraction of GNU v3 abi. |
| 2 | Contributed by Jim Blandy <jimb@redhat.com> |
| 3 | |
| 4 | Copyright (C) 2001, 2002, 2003, 2005, 2006 |
| 5 | Free Software Foundation, Inc. |
| 6 | |
| 7 | This file is part of GDB. |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or |
| 10 | modify it under the terms of the GNU General Public License as |
| 11 | published by the Free Software Foundation; either version 2 of the |
| 12 | License, or (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
| 21 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 22 | Boston, MA 02110-1301, USA. */ |
| 23 | |
| 24 | #include "defs.h" |
| 25 | #include "value.h" |
| 26 | #include "cp-abi.h" |
| 27 | #include "cp-support.h" |
| 28 | #include "demangle.h" |
| 29 | #include "valprint.h" |
| 30 | |
| 31 | #include "gdb_assert.h" |
| 32 | #include "gdb_string.h" |
| 33 | |
| 34 | static struct cp_abi_ops gnu_v3_abi_ops; |
| 35 | |
| 36 | static int |
| 37 | gnuv3_is_vtable_name (const char *name) |
| 38 | { |
| 39 | return strncmp (name, "_ZTV", 4) == 0; |
| 40 | } |
| 41 | |
| 42 | static int |
| 43 | gnuv3_is_operator_name (const char *name) |
| 44 | { |
| 45 | return strncmp (name, "operator", 8) == 0; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /* To help us find the components of a vtable, we build ourselves a |
| 50 | GDB type object representing the vtable structure. Following the |
| 51 | V3 ABI, it goes something like this: |
| 52 | |
| 53 | struct gdb_gnu_v3_abi_vtable { |
| 54 | |
| 55 | / * An array of virtual call and virtual base offsets. The real |
| 56 | length of this array depends on the class hierarchy; we use |
| 57 | negative subscripts to access the elements. Yucky, but |
| 58 | better than the alternatives. * / |
| 59 | ptrdiff_t vcall_and_vbase_offsets[0]; |
| 60 | |
| 61 | / * The offset from a virtual pointer referring to this table |
| 62 | to the top of the complete object. * / |
| 63 | ptrdiff_t offset_to_top; |
| 64 | |
| 65 | / * The type_info pointer for this class. This is really a |
| 66 | std::type_info *, but GDB doesn't really look at the |
| 67 | type_info object itself, so we don't bother to get the type |
| 68 | exactly right. * / |
| 69 | void *type_info; |
| 70 | |
| 71 | / * Virtual table pointers in objects point here. * / |
| 72 | |
| 73 | / * Virtual function pointers. Like the vcall/vbase array, the |
| 74 | real length of this table depends on the class hierarchy. * / |
| 75 | void (*virtual_functions[0]) (); |
| 76 | |
| 77 | }; |
| 78 | |
| 79 | The catch, of course, is that the exact layout of this table |
| 80 | depends on the ABI --- word size, endianness, alignment, etc. So |
| 81 | the GDB type object is actually a per-architecture kind of thing. |
| 82 | |
| 83 | vtable_type_gdbarch_data is a gdbarch per-architecture data pointer |
| 84 | which refers to the struct type * for this structure, laid out |
| 85 | appropriately for the architecture. */ |
| 86 | static struct gdbarch_data *vtable_type_gdbarch_data; |
| 87 | |
| 88 | |
| 89 | /* Human-readable names for the numbers of the fields above. */ |
| 90 | enum { |
| 91 | vtable_field_vcall_and_vbase_offsets, |
| 92 | vtable_field_offset_to_top, |
| 93 | vtable_field_type_info, |
| 94 | vtable_field_virtual_functions |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable', |
| 99 | described above, laid out appropriately for ARCH. |
| 100 | |
| 101 | We use this function as the gdbarch per-architecture data |
| 102 | initialization function. We assume that the gdbarch framework |
| 103 | calls the per-architecture data initialization functions after it |
| 104 | sets current_gdbarch to the new architecture. */ |
| 105 | static void * |
| 106 | build_gdb_vtable_type (struct gdbarch *arch) |
| 107 | { |
| 108 | struct type *t; |
| 109 | struct field *field_list, *field; |
| 110 | int offset; |
| 111 | |
| 112 | struct type *void_ptr_type |
| 113 | = lookup_pointer_type (builtin_type_void); |
| 114 | struct type *ptr_to_void_fn_type |
| 115 | = lookup_pointer_type (lookup_function_type (builtin_type_void)); |
| 116 | |
| 117 | /* ARCH can't give us the true ptrdiff_t type, so we guess. */ |
| 118 | struct type *ptrdiff_type |
| 119 | = init_type (TYPE_CODE_INT, TARGET_PTR_BIT / TARGET_CHAR_BIT, 0, |
| 120 | "ptrdiff_t", 0); |
| 121 | |
| 122 | /* We assume no padding is necessary, since GDB doesn't know |
| 123 | anything about alignment at the moment. If this assumption bites |
| 124 | us, we should add a gdbarch method which, given a type, returns |
| 125 | the alignment that type requires, and then use that here. */ |
| 126 | |
| 127 | /* Build the field list. */ |
| 128 | field_list = xmalloc (sizeof (struct field [4])); |
| 129 | memset (field_list, 0, sizeof (struct field [4])); |
| 130 | field = &field_list[0]; |
| 131 | offset = 0; |
| 132 | |
| 133 | /* ptrdiff_t vcall_and_vbase_offsets[0]; */ |
| 134 | FIELD_NAME (*field) = "vcall_and_vbase_offsets"; |
| 135 | FIELD_TYPE (*field) |
| 136 | = create_array_type (0, ptrdiff_type, |
| 137 | create_range_type (0, builtin_type_int, 0, -1)); |
| 138 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; |
| 139 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); |
| 140 | field++; |
| 141 | |
| 142 | /* ptrdiff_t offset_to_top; */ |
| 143 | FIELD_NAME (*field) = "offset_to_top"; |
| 144 | FIELD_TYPE (*field) = ptrdiff_type; |
| 145 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; |
| 146 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); |
| 147 | field++; |
| 148 | |
| 149 | /* void *type_info; */ |
| 150 | FIELD_NAME (*field) = "type_info"; |
| 151 | FIELD_TYPE (*field) = void_ptr_type; |
| 152 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; |
| 153 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); |
| 154 | field++; |
| 155 | |
| 156 | /* void (*virtual_functions[0]) (); */ |
| 157 | FIELD_NAME (*field) = "virtual_functions"; |
| 158 | FIELD_TYPE (*field) |
| 159 | = create_array_type (0, ptr_to_void_fn_type, |
| 160 | create_range_type (0, builtin_type_int, 0, -1)); |
| 161 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; |
| 162 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); |
| 163 | field++; |
| 164 | |
| 165 | /* We assumed in the allocation above that there were four fields. */ |
| 166 | gdb_assert (field == (field_list + 4)); |
| 167 | |
| 168 | t = init_type (TYPE_CODE_STRUCT, offset, 0, 0, 0); |
| 169 | TYPE_NFIELDS (t) = field - field_list; |
| 170 | TYPE_FIELDS (t) = field_list; |
| 171 | TYPE_TAG_NAME (t) = "gdb_gnu_v3_abi_vtable"; |
| 172 | |
| 173 | return t; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /* Return the offset from the start of the imaginary `struct |
| 178 | gdb_gnu_v3_abi_vtable' object to the vtable's "address point" |
| 179 | (i.e., where objects' virtual table pointers point). */ |
| 180 | static int |
| 181 | vtable_address_point_offset (void) |
| 182 | { |
| 183 | struct type *vtable_type = gdbarch_data (current_gdbarch, |
| 184 | vtable_type_gdbarch_data); |
| 185 | |
| 186 | return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions) |
| 187 | / TARGET_CHAR_BIT); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | static struct type * |
| 192 | gnuv3_rtti_type (struct value *value, |
| 193 | int *full_p, int *top_p, int *using_enc_p) |
| 194 | { |
| 195 | struct type *vtable_type = gdbarch_data (current_gdbarch, |
| 196 | vtable_type_gdbarch_data); |
| 197 | struct type *values_type = check_typedef (value_type (value)); |
| 198 | CORE_ADDR vtable_address; |
| 199 | struct value *vtable; |
| 200 | struct minimal_symbol *vtable_symbol; |
| 201 | const char *vtable_symbol_name; |
| 202 | const char *class_name; |
| 203 | struct type *run_time_type; |
| 204 | struct type *base_type; |
| 205 | LONGEST offset_to_top; |
| 206 | |
| 207 | /* We only have RTTI for class objects. */ |
| 208 | if (TYPE_CODE (values_type) != TYPE_CODE_CLASS) |
| 209 | return NULL; |
| 210 | |
| 211 | /* If we can't find the virtual table pointer for values_type, we |
| 212 | can't find the RTTI. */ |
| 213 | fill_in_vptr_fieldno (values_type); |
| 214 | if (TYPE_VPTR_FIELDNO (values_type) == -1) |
| 215 | return NULL; |
| 216 | |
| 217 | if (using_enc_p) |
| 218 | *using_enc_p = 0; |
| 219 | |
| 220 | /* Fetch VALUE's virtual table pointer, and tweak it to point at |
| 221 | an instance of our imaginary gdb_gnu_v3_abi_vtable structure. */ |
| 222 | base_type = check_typedef (TYPE_VPTR_BASETYPE (values_type)); |
| 223 | if (values_type != base_type) |
| 224 | { |
| 225 | value = value_cast (base_type, value); |
| 226 | if (using_enc_p) |
| 227 | *using_enc_p = 1; |
| 228 | } |
| 229 | vtable_address |
| 230 | = value_as_address (value_field (value, TYPE_VPTR_FIELDNO (values_type))); |
| 231 | vtable = value_at_lazy (vtable_type, |
| 232 | vtable_address - vtable_address_point_offset ()); |
| 233 | |
| 234 | /* Find the linker symbol for this vtable. */ |
| 235 | vtable_symbol |
| 236 | = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtable) |
| 237 | + value_offset (vtable) |
| 238 | + value_embedded_offset (vtable)); |
| 239 | if (! vtable_symbol) |
| 240 | return NULL; |
| 241 | |
| 242 | /* The symbol's demangled name should be something like "vtable for |
| 243 | CLASS", where CLASS is the name of the run-time type of VALUE. |
| 244 | If we didn't like this approach, we could instead look in the |
| 245 | type_info object itself to get the class name. But this way |
| 246 | should work just as well, and doesn't read target memory. */ |
| 247 | vtable_symbol_name = SYMBOL_DEMANGLED_NAME (vtable_symbol); |
| 248 | if (vtable_symbol_name == NULL |
| 249 | || strncmp (vtable_symbol_name, "vtable for ", 11)) |
| 250 | { |
| 251 | warning (_("can't find linker symbol for virtual table for `%s' value"), |
| 252 | TYPE_NAME (values_type)); |
| 253 | if (vtable_symbol_name) |
| 254 | warning (_(" found `%s' instead"), vtable_symbol_name); |
| 255 | return NULL; |
| 256 | } |
| 257 | class_name = vtable_symbol_name + 11; |
| 258 | |
| 259 | /* Try to look up the class name as a type name. */ |
| 260 | /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */ |
| 261 | run_time_type = cp_lookup_rtti_type (class_name, NULL); |
| 262 | if (run_time_type == NULL) |
| 263 | return NULL; |
| 264 | |
| 265 | /* Get the offset from VALUE to the top of the complete object. |
| 266 | NOTE: this is the reverse of the meaning of *TOP_P. */ |
| 267 | offset_to_top |
| 268 | = value_as_long (value_field (vtable, vtable_field_offset_to_top)); |
| 269 | |
| 270 | if (full_p) |
| 271 | *full_p = (- offset_to_top == value_embedded_offset (value) |
| 272 | && (TYPE_LENGTH (value_enclosing_type (value)) |
| 273 | >= TYPE_LENGTH (run_time_type))); |
| 274 | if (top_p) |
| 275 | *top_p = - offset_to_top; |
| 276 | |
| 277 | return run_time_type; |
| 278 | } |
| 279 | |
| 280 | /* Find the vtable for CONTAINER and return a value of the correct |
| 281 | vtable type for this architecture. */ |
| 282 | |
| 283 | static struct value * |
| 284 | gnuv3_get_vtable (struct value *container) |
| 285 | { |
| 286 | struct type *vtable_type = gdbarch_data (current_gdbarch, |
| 287 | vtable_type_gdbarch_data); |
| 288 | struct type *vtable_pointer_type; |
| 289 | struct value *vtable_pointer; |
| 290 | CORE_ADDR vtable_pointer_address, vtable_address; |
| 291 | |
| 292 | /* We do not consult the debug information to find the virtual table. |
| 293 | The ABI specifies that it is always at offset zero in any class, |
| 294 | and debug information may not represent it. We won't issue an |
| 295 | error if there's a class with virtual functions but no virtual table |
| 296 | pointer, but something's already gone seriously wrong if that |
| 297 | happens. |
| 298 | |
| 299 | We avoid using value_contents on principle, because the object might |
| 300 | be large. */ |
| 301 | |
| 302 | /* Find the type "pointer to virtual table". */ |
| 303 | vtable_pointer_type = lookup_pointer_type (vtable_type); |
| 304 | |
| 305 | /* Load it from the start of the class. */ |
| 306 | vtable_pointer_address = value_as_address (value_addr (container)); |
| 307 | vtable_pointer = value_at (vtable_pointer_type, vtable_pointer_address); |
| 308 | vtable_address = value_as_address (vtable_pointer); |
| 309 | |
| 310 | /* Correct it to point at the start of the virtual table, rather |
| 311 | than the address point. */ |
| 312 | return value_at_lazy (vtable_type, |
| 313 | vtable_address - vtable_address_point_offset ()); |
| 314 | } |
| 315 | |
| 316 | /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual |
| 317 | function, of type FNTYPE. */ |
| 318 | |
| 319 | static struct value * |
| 320 | gnuv3_get_virtual_fn (struct value *container, struct type *fntype, |
| 321 | int vtable_index) |
| 322 | { |
| 323 | struct value *vtable = gnuv3_get_vtable (container); |
| 324 | struct value *vfn; |
| 325 | |
| 326 | /* Fetch the appropriate function pointer from the vtable. */ |
| 327 | vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions), |
| 328 | value_from_longest (builtin_type_int, vtable_index)); |
| 329 | |
| 330 | /* If this architecture uses function descriptors directly in the vtable, |
| 331 | then the address of the vtable entry is actually a "function pointer" |
| 332 | (i.e. points to the descriptor). We don't need to scale the index |
| 333 | by the size of a function descriptor; GCC does that before outputing |
| 334 | debug information. */ |
| 335 | if (gdbarch_vtable_function_descriptors (current_gdbarch)) |
| 336 | vfn = value_addr (vfn); |
| 337 | |
| 338 | /* Cast the function pointer to the appropriate type. */ |
| 339 | vfn = value_cast (lookup_pointer_type (fntype), vfn); |
| 340 | |
| 341 | return vfn; |
| 342 | } |
| 343 | |
| 344 | /* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h |
| 345 | for a description of the arguments. */ |
| 346 | |
| 347 | static struct value * |
| 348 | gnuv3_virtual_fn_field (struct value **value_p, |
| 349 | struct fn_field *f, int j, |
| 350 | struct type *vfn_base, int offset) |
| 351 | { |
| 352 | struct type *values_type = check_typedef (value_type (*value_p)); |
| 353 | |
| 354 | /* Some simple sanity checks. */ |
| 355 | if (TYPE_CODE (values_type) != TYPE_CODE_CLASS) |
| 356 | error (_("Only classes can have virtual functions.")); |
| 357 | |
| 358 | /* Cast our value to the base class which defines this virtual |
| 359 | function. This takes care of any necessary `this' |
| 360 | adjustments. */ |
| 361 | if (vfn_base != values_type) |
| 362 | *value_p = value_cast (vfn_base, *value_p); |
| 363 | |
| 364 | return gnuv3_get_virtual_fn (*value_p, TYPE_FN_FIELD_TYPE (f, j), |
| 365 | TYPE_FN_FIELD_VOFFSET (f, j)); |
| 366 | } |
| 367 | |
| 368 | /* Compute the offset of the baseclass which is |
| 369 | the INDEXth baseclass of class TYPE, |
| 370 | for value at VALADDR (in host) at ADDRESS (in target). |
| 371 | The result is the offset of the baseclass value relative |
| 372 | to (the address of)(ARG) + OFFSET. |
| 373 | |
| 374 | -1 is returned on error. */ |
| 375 | static int |
| 376 | gnuv3_baseclass_offset (struct type *type, int index, const bfd_byte *valaddr, |
| 377 | CORE_ADDR address) |
| 378 | { |
| 379 | struct type *vtable_type = gdbarch_data (current_gdbarch, |
| 380 | vtable_type_gdbarch_data); |
| 381 | struct value *vtable; |
| 382 | struct type *vbasetype; |
| 383 | struct value *offset_val, *vbase_array; |
| 384 | CORE_ADDR vtable_address; |
| 385 | long int cur_base_offset, base_offset; |
| 386 | |
| 387 | /* If it isn't a virtual base, this is easy. The offset is in the |
| 388 | type definition. */ |
| 389 | if (!BASETYPE_VIA_VIRTUAL (type, index)) |
| 390 | return TYPE_BASECLASS_BITPOS (type, index) / 8; |
| 391 | |
| 392 | /* To access a virtual base, we need to use the vbase offset stored in |
| 393 | our vtable. Recent GCC versions provide this information. If it isn't |
| 394 | available, we could get what we needed from RTTI, or from drawing the |
| 395 | complete inheritance graph based on the debug info. Neither is |
| 396 | worthwhile. */ |
| 397 | cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8; |
| 398 | if (cur_base_offset >= - vtable_address_point_offset ()) |
| 399 | error (_("Expected a negative vbase offset (old compiler?)")); |
| 400 | |
| 401 | cur_base_offset = cur_base_offset + vtable_address_point_offset (); |
| 402 | if ((- cur_base_offset) % TYPE_LENGTH (builtin_type_void_data_ptr) != 0) |
| 403 | error (_("Misaligned vbase offset.")); |
| 404 | cur_base_offset = cur_base_offset |
| 405 | / ((int) TYPE_LENGTH (builtin_type_void_data_ptr)); |
| 406 | |
| 407 | /* We're now looking for the cur_base_offset'th entry (negative index) |
| 408 | in the vcall_and_vbase_offsets array. We used to cast the object to |
| 409 | its TYPE_VPTR_BASETYPE, and reference the vtable as TYPE_VPTR_FIELDNO; |
| 410 | however, that cast can not be done without calling baseclass_offset again |
| 411 | if the TYPE_VPTR_BASETYPE is a virtual base class, as described in the |
| 412 | v3 C++ ABI Section 2.4.I.2.b. Fortunately the ABI guarantees that the |
| 413 | vtable pointer will be located at the beginning of the object, so we can |
| 414 | bypass the casting. Verify that the TYPE_VPTR_FIELDNO is in fact at the |
| 415 | start of whichever baseclass it resides in, as a sanity measure - iff |
| 416 | we have debugging information for that baseclass. */ |
| 417 | |
| 418 | vbasetype = TYPE_VPTR_BASETYPE (type); |
| 419 | if (TYPE_VPTR_FIELDNO (vbasetype) < 0) |
| 420 | fill_in_vptr_fieldno (vbasetype); |
| 421 | |
| 422 | if (TYPE_VPTR_FIELDNO (vbasetype) >= 0 |
| 423 | && TYPE_FIELD_BITPOS (vbasetype, TYPE_VPTR_FIELDNO (vbasetype)) != 0) |
| 424 | error (_("Illegal vptr offset in class %s"), |
| 425 | TYPE_NAME (vbasetype) ? TYPE_NAME (vbasetype) : "<unknown>"); |
| 426 | |
| 427 | vtable_address = value_as_address (value_at_lazy (builtin_type_void_data_ptr, |
| 428 | address)); |
| 429 | vtable = value_at_lazy (vtable_type, |
| 430 | vtable_address - vtable_address_point_offset ()); |
| 431 | offset_val = value_from_longest(builtin_type_int, cur_base_offset); |
| 432 | vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets); |
| 433 | base_offset = value_as_long (value_subscript (vbase_array, offset_val)); |
| 434 | return base_offset; |
| 435 | } |
| 436 | |
| 437 | /* Locate a virtual method in DOMAIN or its non-virtual base classes |
| 438 | which has virtual table index VOFFSET. The method has an associated |
| 439 | "this" adjustment of ADJUSTMENT bytes. */ |
| 440 | |
| 441 | const char * |
| 442 | gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset, |
| 443 | LONGEST adjustment) |
| 444 | { |
| 445 | int i; |
| 446 | const char *physname; |
| 447 | |
| 448 | /* Search this class first. */ |
| 449 | physname = NULL; |
| 450 | if (adjustment == 0) |
| 451 | { |
| 452 | int len; |
| 453 | |
| 454 | len = TYPE_NFN_FIELDS (domain); |
| 455 | for (i = 0; i < len; i++) |
| 456 | { |
| 457 | int len2, j; |
| 458 | struct fn_field *f; |
| 459 | |
| 460 | f = TYPE_FN_FIELDLIST1 (domain, i); |
| 461 | len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i); |
| 462 | |
| 463 | check_stub_method_group (domain, i); |
| 464 | for (j = 0; j < len2; j++) |
| 465 | if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset) |
| 466 | return TYPE_FN_FIELD_PHYSNAME (f, j); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /* Next search non-virtual bases. If it's in a virtual base, |
| 471 | we're out of luck. */ |
| 472 | for (i = 0; i < TYPE_N_BASECLASSES (domain); i++) |
| 473 | { |
| 474 | int pos; |
| 475 | struct type *basetype; |
| 476 | |
| 477 | if (BASETYPE_VIA_VIRTUAL (domain, i)) |
| 478 | continue; |
| 479 | |
| 480 | pos = TYPE_BASECLASS_BITPOS (domain, i) / 8; |
| 481 | basetype = TYPE_FIELD_TYPE (domain, i); |
| 482 | /* Recurse with a modified adjustment. We don't need to adjust |
| 483 | voffset. */ |
| 484 | if (adjustment >= pos && adjustment < pos + TYPE_LENGTH (basetype)) |
| 485 | return gnuv3_find_method_in (basetype, voffset, adjustment - pos); |
| 486 | } |
| 487 | |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | /* GNU v3 implementation of cplus_print_method_ptr. */ |
| 492 | |
| 493 | static void |
| 494 | gnuv3_print_method_ptr (const gdb_byte *contents, |
| 495 | struct type *type, |
| 496 | struct ui_file *stream) |
| 497 | { |
| 498 | CORE_ADDR ptr_value; |
| 499 | LONGEST adjustment; |
| 500 | struct type *domain; |
| 501 | int vbit; |
| 502 | |
| 503 | domain = TYPE_DOMAIN_TYPE (type); |
| 504 | |
| 505 | /* Extract the pointer to member. */ |
| 506 | ptr_value = extract_typed_address (contents, builtin_type_void_func_ptr); |
| 507 | contents += TYPE_LENGTH (builtin_type_void_func_ptr); |
| 508 | adjustment = extract_signed_integer (contents, |
| 509 | TYPE_LENGTH (builtin_type_long)); |
| 510 | |
| 511 | if (!gdbarch_vbit_in_delta (current_gdbarch)) |
| 512 | { |
| 513 | vbit = ptr_value & 1; |
| 514 | ptr_value = ptr_value ^ vbit; |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | vbit = adjustment & 1; |
| 519 | adjustment = adjustment >> 1; |
| 520 | } |
| 521 | |
| 522 | /* Check for NULL. */ |
| 523 | if (ptr_value == 0 && vbit == 0) |
| 524 | { |
| 525 | fprintf_filtered (stream, "NULL"); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | /* Search for a virtual method. */ |
| 530 | if (vbit) |
| 531 | { |
| 532 | CORE_ADDR voffset; |
| 533 | const char *physname; |
| 534 | |
| 535 | /* It's a virtual table offset, maybe in this class. Search |
| 536 | for a field with the correct vtable offset. First convert it |
| 537 | to an index, as used in TYPE_FN_FIELD_VOFFSET. */ |
| 538 | voffset = ptr_value / TYPE_LENGTH (builtin_type_long); |
| 539 | |
| 540 | physname = gnuv3_find_method_in (domain, voffset, adjustment); |
| 541 | |
| 542 | /* If we found a method, print that. We don't bother to disambiguate |
| 543 | possible paths to the method based on the adjustment. */ |
| 544 | if (physname) |
| 545 | { |
| 546 | char *demangled_name = cplus_demangle (physname, |
| 547 | DMGL_ANSI | DMGL_PARAMS); |
| 548 | if (demangled_name != NULL) |
| 549 | { |
| 550 | fprintf_filtered (stream, "&virtual "); |
| 551 | fputs_filtered (demangled_name, stream); |
| 552 | xfree (demangled_name); |
| 553 | return; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | /* We didn't find it; print the raw data. */ |
| 559 | if (vbit) |
| 560 | { |
| 561 | fprintf_filtered (stream, "&virtual table offset "); |
| 562 | print_longest (stream, 'd', 1, ptr_value); |
| 563 | } |
| 564 | else |
| 565 | print_address_demangle (ptr_value, stream, demangle); |
| 566 | |
| 567 | if (adjustment) |
| 568 | { |
| 569 | fprintf_filtered (stream, ", this adjustment "); |
| 570 | print_longest (stream, 'd', 1, adjustment); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /* GNU v3 implementation of cplus_method_ptr_size. */ |
| 575 | |
| 576 | static int |
| 577 | gnuv3_method_ptr_size (void) |
| 578 | { |
| 579 | return 2 * TYPE_LENGTH (builtin_type_void_data_ptr); |
| 580 | } |
| 581 | |
| 582 | /* GNU v3 implementation of cplus_make_method_ptr. */ |
| 583 | |
| 584 | static void |
| 585 | gnuv3_make_method_ptr (gdb_byte *contents, CORE_ADDR value, int is_virtual) |
| 586 | { |
| 587 | int size = TYPE_LENGTH (builtin_type_void_data_ptr); |
| 588 | |
| 589 | /* FIXME drow/2006-12-24: The adjustment of "this" is currently |
| 590 | always zero, since the method pointer is of the correct type. |
| 591 | But if the method pointer came from a base class, this is |
| 592 | incorrect - it should be the offset to the base. The best |
| 593 | fix might be to create the pointer to member pointing at the |
| 594 | base class and cast it to the derived class, but that requires |
| 595 | support for adjusting pointers to members when casting them - |
| 596 | not currently supported by GDB. */ |
| 597 | |
| 598 | if (!gdbarch_vbit_in_delta (current_gdbarch)) |
| 599 | { |
| 600 | store_unsigned_integer (contents, size, value | is_virtual); |
| 601 | store_unsigned_integer (contents + size, size, 0); |
| 602 | } |
| 603 | else |
| 604 | { |
| 605 | store_unsigned_integer (contents, size, value); |
| 606 | store_unsigned_integer (contents + size, size, is_virtual); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | /* GNU v3 implementation of cplus_method_ptr_to_value. */ |
| 611 | |
| 612 | static struct value * |
| 613 | gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr) |
| 614 | { |
| 615 | const gdb_byte *contents = value_contents (method_ptr); |
| 616 | CORE_ADDR ptr_value; |
| 617 | struct type *final_type, *method_type; |
| 618 | LONGEST adjustment; |
| 619 | struct value *adjval; |
| 620 | int vbit; |
| 621 | |
| 622 | final_type = TYPE_DOMAIN_TYPE (check_typedef (value_type (method_ptr))); |
| 623 | final_type = lookup_pointer_type (final_type); |
| 624 | |
| 625 | method_type = TYPE_TARGET_TYPE (check_typedef (value_type (method_ptr))); |
| 626 | |
| 627 | ptr_value = extract_typed_address (contents, builtin_type_void_func_ptr); |
| 628 | contents += TYPE_LENGTH (builtin_type_void_func_ptr); |
| 629 | adjustment = extract_signed_integer (contents, |
| 630 | TYPE_LENGTH (builtin_type_long)); |
| 631 | |
| 632 | if (!gdbarch_vbit_in_delta (current_gdbarch)) |
| 633 | { |
| 634 | vbit = ptr_value & 1; |
| 635 | ptr_value = ptr_value ^ vbit; |
| 636 | } |
| 637 | else |
| 638 | { |
| 639 | vbit = adjustment & 1; |
| 640 | adjustment = adjustment >> 1; |
| 641 | } |
| 642 | |
| 643 | /* First convert THIS to match the containing type of the pointer to |
| 644 | member. This cast may adjust the value of THIS. */ |
| 645 | *this_p = value_cast (final_type, *this_p); |
| 646 | |
| 647 | /* Then apply whatever adjustment is necessary. This creates a somewhat |
| 648 | strange pointer: it claims to have type FINAL_TYPE, but in fact it |
| 649 | might not be a valid FINAL_TYPE. For instance, it might be a |
| 650 | base class of FINAL_TYPE. And if it's not the primary base class, |
| 651 | then printing it out as a FINAL_TYPE object would produce some pretty |
| 652 | garbage. |
| 653 | |
| 654 | But we don't really know the type of the first argument in |
| 655 | METHOD_TYPE either, which is why this happens. We can't |
| 656 | dereference this later as a FINAL_TYPE, but once we arrive in the |
| 657 | called method we'll have debugging information for the type of |
| 658 | "this" - and that'll match the value we produce here. |
| 659 | |
| 660 | You can provoke this case by casting a Base::* to a Derived::*, for |
| 661 | instance. */ |
| 662 | *this_p = value_cast (builtin_type_void_data_ptr, *this_p); |
| 663 | adjval = value_from_longest (builtin_type_long, adjustment); |
| 664 | *this_p = value_add (*this_p, adjval); |
| 665 | *this_p = value_cast (final_type, *this_p); |
| 666 | |
| 667 | if (vbit) |
| 668 | { |
| 669 | LONGEST voffset = ptr_value / TYPE_LENGTH (builtin_type_long); |
| 670 | return gnuv3_get_virtual_fn (value_ind (*this_p), method_type, voffset); |
| 671 | } |
| 672 | else |
| 673 | return value_from_pointer (lookup_pointer_type (method_type), ptr_value); |
| 674 | } |
| 675 | |
| 676 | static void |
| 677 | init_gnuv3_ops (void) |
| 678 | { |
| 679 | vtable_type_gdbarch_data = gdbarch_data_register_post_init (build_gdb_vtable_type); |
| 680 | |
| 681 | gnu_v3_abi_ops.shortname = "gnu-v3"; |
| 682 | gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI"; |
| 683 | gnu_v3_abi_ops.doc = "G++ Version 3 ABI"; |
| 684 | gnu_v3_abi_ops.is_destructor_name = |
| 685 | (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor; |
| 686 | gnu_v3_abi_ops.is_constructor_name = |
| 687 | (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor; |
| 688 | gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name; |
| 689 | gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name; |
| 690 | gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type; |
| 691 | gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field; |
| 692 | gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset; |
| 693 | gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr; |
| 694 | gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size; |
| 695 | gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr; |
| 696 | gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value; |
| 697 | } |
| 698 | |
| 699 | extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */ |
| 700 | |
| 701 | void |
| 702 | _initialize_gnu_v3_abi (void) |
| 703 | { |
| 704 | init_gnuv3_ops (); |
| 705 | |
| 706 | register_cp_abi (&gnu_v3_abi_ops); |
| 707 | } |