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