gnu-v3-abi.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. /* Abstraction of GNU v3 abi.
  2. Contributed by Jim Blandy <jimb@redhat.com>
  3. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "value.h"
  17. #include "cp-abi.h"
  18. #include "cp-support.h"
  19. #include "demangle.h"
  20. #include "dwarf2.h"
  21. #include "objfiles.h"
  22. #include "valprint.h"
  23. #include "c-lang.h"
  24. #include "typeprint.h"
  25. #include <algorithm>
  26. #include "cli/cli-style.h"
  27. #include "dwarf2/loc.h"
  28. #include "inferior.h"
  29. static struct cp_abi_ops gnu_v3_abi_ops;
  30. /* A gdbarch key for std::type_info, in the event that it can't be
  31. found in the debug info. */
  32. static struct gdbarch_data *std_type_info_gdbarch_data;
  33. static int
  34. gnuv3_is_vtable_name (const char *name)
  35. {
  36. return startswith (name, "_ZTV");
  37. }
  38. static int
  39. gnuv3_is_operator_name (const char *name)
  40. {
  41. return startswith (name, CP_OPERATOR_STR);
  42. }
  43. /* To help us find the components of a vtable, we build ourselves a
  44. GDB type object representing the vtable structure. Following the
  45. V3 ABI, it goes something like this:
  46. struct gdb_gnu_v3_abi_vtable {
  47. / * An array of virtual call and virtual base offsets. The real
  48. length of this array depends on the class hierarchy; we use
  49. negative subscripts to access the elements. Yucky, but
  50. better than the alternatives. * /
  51. ptrdiff_t vcall_and_vbase_offsets[0];
  52. / * The offset from a virtual pointer referring to this table
  53. to the top of the complete object. * /
  54. ptrdiff_t offset_to_top;
  55. / * The type_info pointer for this class. This is really a
  56. std::type_info *, but GDB doesn't really look at the
  57. type_info object itself, so we don't bother to get the type
  58. exactly right. * /
  59. void *type_info;
  60. / * Virtual table pointers in objects point here. * /
  61. / * Virtual function pointers. Like the vcall/vbase array, the
  62. real length of this table depends on the class hierarchy. * /
  63. void (*virtual_functions[0]) ();
  64. };
  65. The catch, of course, is that the exact layout of this table
  66. depends on the ABI --- word size, endianness, alignment, etc. So
  67. the GDB type object is actually a per-architecture kind of thing.
  68. vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
  69. which refers to the struct type * for this structure, laid out
  70. appropriately for the architecture. */
  71. static struct gdbarch_data *vtable_type_gdbarch_data;
  72. /* Human-readable names for the numbers of the fields above. */
  73. enum {
  74. vtable_field_vcall_and_vbase_offsets,
  75. vtable_field_offset_to_top,
  76. vtable_field_type_info,
  77. vtable_field_virtual_functions
  78. };
  79. /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
  80. described above, laid out appropriately for ARCH.
  81. We use this function as the gdbarch per-architecture data
  82. initialization function. */
  83. static void *
  84. build_gdb_vtable_type (struct gdbarch *arch)
  85. {
  86. struct type *t;
  87. struct field *field_list, *field;
  88. int offset;
  89. struct type *void_ptr_type
  90. = builtin_type (arch)->builtin_data_ptr;
  91. struct type *ptr_to_void_fn_type
  92. = builtin_type (arch)->builtin_func_ptr;
  93. /* ARCH can't give us the true ptrdiff_t type, so we guess. */
  94. struct type *ptrdiff_type
  95. = arch_integer_type (arch, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
  96. /* We assume no padding is necessary, since GDB doesn't know
  97. anything about alignment at the moment. If this assumption bites
  98. us, we should add a gdbarch method which, given a type, returns
  99. the alignment that type requires, and then use that here. */
  100. /* Build the field list. */
  101. field_list = XCNEWVEC (struct field, 4);
  102. field = &field_list[0];
  103. offset = 0;
  104. /* ptrdiff_t vcall_and_vbase_offsets[0]; */
  105. field->set_name ("vcall_and_vbase_offsets");
  106. field->set_type (lookup_array_range_type (ptrdiff_type, 0, -1));
  107. field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
  108. offset += TYPE_LENGTH (field->type ());
  109. field++;
  110. /* ptrdiff_t offset_to_top; */
  111. field->set_name ("offset_to_top");
  112. field->set_type (ptrdiff_type);
  113. field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
  114. offset += TYPE_LENGTH (field->type ());
  115. field++;
  116. /* void *type_info; */
  117. field->set_name ("type_info");
  118. field->set_type (void_ptr_type);
  119. field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
  120. offset += TYPE_LENGTH (field->type ());
  121. field++;
  122. /* void (*virtual_functions[0]) (); */
  123. field->set_name ("virtual_functions");
  124. field->set_type (lookup_array_range_type (ptr_to_void_fn_type, 0, -1));
  125. field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
  126. offset += TYPE_LENGTH (field->type ());
  127. field++;
  128. /* We assumed in the allocation above that there were four fields. */
  129. gdb_assert (field == (field_list + 4));
  130. t = arch_type (arch, TYPE_CODE_STRUCT, offset * TARGET_CHAR_BIT, NULL);
  131. t->set_num_fields (field - field_list);
  132. t->set_fields (field_list);
  133. t->set_name ("gdb_gnu_v3_abi_vtable");
  134. INIT_CPLUS_SPECIFIC (t);
  135. return make_type_with_address_space (t, TYPE_INSTANCE_FLAG_CODE_SPACE);
  136. }
  137. /* Return the ptrdiff_t type used in the vtable type. */
  138. static struct type *
  139. vtable_ptrdiff_type (struct gdbarch *gdbarch)
  140. {
  141. struct type *vtable_type
  142. = (struct type *) gdbarch_data (gdbarch, vtable_type_gdbarch_data);
  143. /* The "offset_to_top" field has the appropriate (ptrdiff_t) type. */
  144. return vtable_type->field (vtable_field_offset_to_top).type ();
  145. }
  146. /* Return the offset from the start of the imaginary `struct
  147. gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
  148. (i.e., where objects' virtual table pointers point). */
  149. static int
  150. vtable_address_point_offset (struct gdbarch *gdbarch)
  151. {
  152. struct type *vtable_type
  153. = (struct type *) gdbarch_data (gdbarch, vtable_type_gdbarch_data);
  154. return (vtable_type->field (vtable_field_virtual_functions).loc_bitpos ()
  155. / TARGET_CHAR_BIT);
  156. }
  157. /* Determine whether structure TYPE is a dynamic class. Cache the
  158. result. */
  159. static int
  160. gnuv3_dynamic_class (struct type *type)
  161. {
  162. int fieldnum, fieldelem;
  163. type = check_typedef (type);
  164. gdb_assert (type->code () == TYPE_CODE_STRUCT
  165. || type->code () == TYPE_CODE_UNION);
  166. if (type->code () == TYPE_CODE_UNION)
  167. return 0;
  168. if (TYPE_CPLUS_DYNAMIC (type))
  169. return TYPE_CPLUS_DYNAMIC (type) == 1;
  170. ALLOCATE_CPLUS_STRUCT_TYPE (type);
  171. for (fieldnum = 0; fieldnum < TYPE_N_BASECLASSES (type); fieldnum++)
  172. if (BASETYPE_VIA_VIRTUAL (type, fieldnum)
  173. || gnuv3_dynamic_class (type->field (fieldnum).type ()))
  174. {
  175. TYPE_CPLUS_DYNAMIC (type) = 1;
  176. return 1;
  177. }
  178. for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
  179. for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
  180. fieldelem++)
  181. {
  182. struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
  183. if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
  184. {
  185. TYPE_CPLUS_DYNAMIC (type) = 1;
  186. return 1;
  187. }
  188. }
  189. TYPE_CPLUS_DYNAMIC (type) = -1;
  190. return 0;
  191. }
  192. /* Find the vtable for a value of CONTAINER_TYPE located at
  193. CONTAINER_ADDR. Return a value of the correct vtable type for this
  194. architecture, or NULL if CONTAINER does not have a vtable. */
  195. static struct value *
  196. gnuv3_get_vtable (struct gdbarch *gdbarch,
  197. struct type *container_type, CORE_ADDR container_addr)
  198. {
  199. struct type *vtable_type
  200. = (struct type *) gdbarch_data (gdbarch, vtable_type_gdbarch_data);
  201. struct type *vtable_pointer_type;
  202. struct value *vtable_pointer;
  203. CORE_ADDR vtable_address;
  204. container_type = check_typedef (container_type);
  205. gdb_assert (container_type->code () == TYPE_CODE_STRUCT);
  206. /* If this type does not have a virtual table, don't read the first
  207. field. */
  208. if (!gnuv3_dynamic_class (container_type))
  209. return NULL;
  210. /* We do not consult the debug information to find the virtual table.
  211. The ABI specifies that it is always at offset zero in any class,
  212. and debug information may not represent it.
  213. We avoid using value_contents on principle, because the object might
  214. be large. */
  215. /* Find the type "pointer to virtual table". */
  216. vtable_pointer_type = lookup_pointer_type (vtable_type);
  217. /* Load it from the start of the class. */
  218. vtable_pointer = value_at (vtable_pointer_type, container_addr);
  219. vtable_address = value_as_address (vtable_pointer);
  220. /* Correct it to point at the start of the virtual table, rather
  221. than the address point. */
  222. return value_at_lazy (vtable_type,
  223. vtable_address
  224. - vtable_address_point_offset (gdbarch));
  225. }
  226. static struct type *
  227. gnuv3_rtti_type (struct value *value,
  228. int *full_p, LONGEST *top_p, int *using_enc_p)
  229. {
  230. struct gdbarch *gdbarch;
  231. struct type *values_type = check_typedef (value_type (value));
  232. struct value *vtable;
  233. struct minimal_symbol *vtable_symbol;
  234. const char *vtable_symbol_name;
  235. const char *class_name;
  236. struct type *run_time_type;
  237. LONGEST offset_to_top;
  238. const char *atsign;
  239. /* We only have RTTI for dynamic class objects. */
  240. if (values_type->code () != TYPE_CODE_STRUCT
  241. || !gnuv3_dynamic_class (values_type))
  242. return NULL;
  243. /* Determine architecture. */
  244. gdbarch = values_type->arch ();
  245. if (using_enc_p)
  246. *using_enc_p = 0;
  247. vtable = gnuv3_get_vtable (gdbarch, values_type,
  248. value_as_address (value_addr (value)));
  249. if (vtable == NULL)
  250. return NULL;
  251. /* Find the linker symbol for this vtable. */
  252. vtable_symbol
  253. = lookup_minimal_symbol_by_pc (value_address (vtable)
  254. + value_embedded_offset (vtable)).minsym;
  255. if (! vtable_symbol)
  256. return NULL;
  257. /* The symbol's demangled name should be something like "vtable for
  258. CLASS", where CLASS is the name of the run-time type of VALUE.
  259. If we didn't like this approach, we could instead look in the
  260. type_info object itself to get the class name. But this way
  261. should work just as well, and doesn't read target memory. */
  262. vtable_symbol_name = vtable_symbol->demangled_name ();
  263. if (vtable_symbol_name == NULL
  264. || !startswith (vtable_symbol_name, "vtable for "))
  265. {
  266. warning (_("can't find linker symbol for virtual table for `%s' value"),
  267. TYPE_SAFE_NAME (values_type));
  268. if (vtable_symbol_name)
  269. warning (_(" found `%s' instead"), vtable_symbol_name);
  270. return NULL;
  271. }
  272. class_name = vtable_symbol_name + 11;
  273. /* Strip off @plt and version suffixes. */
  274. atsign = strchr (class_name, '@');
  275. if (atsign != NULL)
  276. {
  277. char *copy;
  278. copy = (char *) alloca (atsign - class_name + 1);
  279. memcpy (copy, class_name, atsign - class_name);
  280. copy[atsign - class_name] = '\0';
  281. class_name = copy;
  282. }
  283. /* Try to look up the class name as a type name. */
  284. /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
  285. run_time_type = cp_lookup_rtti_type (class_name, NULL);
  286. if (run_time_type == NULL)
  287. return NULL;
  288. /* Get the offset from VALUE to the top of the complete object.
  289. NOTE: this is the reverse of the meaning of *TOP_P. */
  290. offset_to_top
  291. = value_as_long (value_field (vtable, vtable_field_offset_to_top));
  292. if (full_p)
  293. *full_p = (- offset_to_top == value_embedded_offset (value)
  294. && (TYPE_LENGTH (value_enclosing_type (value))
  295. >= TYPE_LENGTH (run_time_type)));
  296. if (top_p)
  297. *top_p = - offset_to_top;
  298. return run_time_type;
  299. }
  300. /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
  301. function, of type FNTYPE. */
  302. static struct value *
  303. gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
  304. struct type *fntype, int vtable_index)
  305. {
  306. struct value *vtable, *vfn;
  307. /* Every class with virtual functions must have a vtable. */
  308. vtable = gnuv3_get_vtable (gdbarch, value_type (container),
  309. value_as_address (value_addr (container)));
  310. gdb_assert (vtable != NULL);
  311. /* Fetch the appropriate function pointer from the vtable. */
  312. vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
  313. vtable_index);
  314. /* If this architecture uses function descriptors directly in the vtable,
  315. then the address of the vtable entry is actually a "function pointer"
  316. (i.e. points to the descriptor). We don't need to scale the index
  317. by the size of a function descriptor; GCC does that before outputting
  318. debug information. */
  319. if (gdbarch_vtable_function_descriptors (gdbarch))
  320. vfn = value_addr (vfn);
  321. /* Cast the function pointer to the appropriate type. */
  322. vfn = value_cast (lookup_pointer_type (fntype), vfn);
  323. return vfn;
  324. }
  325. /* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
  326. for a description of the arguments. */
  327. static struct value *
  328. gnuv3_virtual_fn_field (struct value **value_p,
  329. struct fn_field *f, int j,
  330. struct type *vfn_base, int offset)
  331. {
  332. struct type *values_type = check_typedef (value_type (*value_p));
  333. struct gdbarch *gdbarch;
  334. /* Some simple sanity checks. */
  335. if (values_type->code () != TYPE_CODE_STRUCT)
  336. error (_("Only classes can have virtual functions."));
  337. /* Determine architecture. */
  338. gdbarch = values_type->arch ();
  339. /* Cast our value to the base class which defines this virtual
  340. function. This takes care of any necessary `this'
  341. adjustments. */
  342. if (vfn_base != values_type)
  343. *value_p = value_cast (vfn_base, *value_p);
  344. return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
  345. TYPE_FN_FIELD_VOFFSET (f, j));
  346. }
  347. /* Compute the offset of the baseclass which is
  348. the INDEXth baseclass of class TYPE,
  349. for value at VALADDR (in host) at ADDRESS (in target).
  350. The result is the offset of the baseclass value relative
  351. to (the address of)(ARG) + OFFSET.
  352. -1 is returned on error. */
  353. static int
  354. gnuv3_baseclass_offset (struct type *type, int index,
  355. const bfd_byte *valaddr, LONGEST embedded_offset,
  356. CORE_ADDR address, const struct value *val)
  357. {
  358. struct gdbarch *gdbarch;
  359. struct type *ptr_type;
  360. struct value *vtable;
  361. struct value *vbase_array;
  362. long int cur_base_offset, base_offset;
  363. /* Determine architecture. */
  364. gdbarch = type->arch ();
  365. ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
  366. /* If it isn't a virtual base, this is easy. The offset is in the
  367. type definition. */
  368. if (!BASETYPE_VIA_VIRTUAL (type, index))
  369. return TYPE_BASECLASS_BITPOS (type, index) / 8;
  370. /* If we have a DWARF expression for the offset, evaluate it. */
  371. if (type->field (index).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
  372. {
  373. struct dwarf2_property_baton baton;
  374. baton.property_type
  375. = lookup_pointer_type (type->field (index).type ());
  376. baton.locexpr = *type->field (index).loc_dwarf_block ();
  377. struct dynamic_prop prop;
  378. prop.set_locexpr (&baton);
  379. struct property_addr_info addr_stack;
  380. addr_stack.type = type;
  381. /* Note that we don't set "valaddr" here. Doing so causes
  382. regressions. FIXME. */
  383. addr_stack.addr = address + embedded_offset;
  384. addr_stack.next = nullptr;
  385. CORE_ADDR result;
  386. if (dwarf2_evaluate_property (&prop, nullptr, &addr_stack, &result,
  387. {addr_stack.addr}))
  388. return (int) (result - addr_stack.addr);
  389. }
  390. /* To access a virtual base, we need to use the vbase offset stored in
  391. our vtable. Recent GCC versions provide this information. If it isn't
  392. available, we could get what we needed from RTTI, or from drawing the
  393. complete inheritance graph based on the debug info. Neither is
  394. worthwhile. */
  395. cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
  396. if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
  397. error (_("Expected a negative vbase offset (old compiler?)"));
  398. cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
  399. if ((- cur_base_offset) % TYPE_LENGTH (ptr_type) != 0)
  400. error (_("Misaligned vbase offset."));
  401. cur_base_offset = cur_base_offset / ((int) TYPE_LENGTH (ptr_type));
  402. vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
  403. gdb_assert (vtable != NULL);
  404. vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
  405. base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
  406. return base_offset;
  407. }
  408. /* Locate a virtual method in DOMAIN or its non-virtual base classes
  409. which has virtual table index VOFFSET. The method has an associated
  410. "this" adjustment of ADJUSTMENT bytes. */
  411. static const char *
  412. gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
  413. LONGEST adjustment)
  414. {
  415. int i;
  416. /* Search this class first. */
  417. if (adjustment == 0)
  418. {
  419. int len;
  420. len = TYPE_NFN_FIELDS (domain);
  421. for (i = 0; i < len; i++)
  422. {
  423. int len2, j;
  424. struct fn_field *f;
  425. f = TYPE_FN_FIELDLIST1 (domain, i);
  426. len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
  427. check_stub_method_group (domain, i);
  428. for (j = 0; j < len2; j++)
  429. if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
  430. return TYPE_FN_FIELD_PHYSNAME (f, j);
  431. }
  432. }
  433. /* Next search non-virtual bases. If it's in a virtual base,
  434. we're out of luck. */
  435. for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
  436. {
  437. int pos;
  438. struct type *basetype;
  439. if (BASETYPE_VIA_VIRTUAL (domain, i))
  440. continue;
  441. pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
  442. basetype = domain->field (i).type ();
  443. /* Recurse with a modified adjustment. We don't need to adjust
  444. voffset. */
  445. if (adjustment >= pos && adjustment < pos + TYPE_LENGTH (basetype))
  446. return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
  447. }
  448. return NULL;
  449. }
  450. /* Decode GNU v3 method pointer. */
  451. static int
  452. gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
  453. const gdb_byte *contents,
  454. CORE_ADDR *value_p,
  455. LONGEST *adjustment_p)
  456. {
  457. struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
  458. struct type *offset_type = vtable_ptrdiff_type (gdbarch);
  459. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  460. CORE_ADDR ptr_value;
  461. LONGEST voffset, adjustment;
  462. int vbit;
  463. /* Extract the pointer to member. The first element is either a pointer
  464. or a vtable offset. For pointers, we need to use extract_typed_address
  465. to allow the back-end to convert the pointer to a GDB address -- but
  466. vtable offsets we must handle as integers. At this point, we do not
  467. yet know which case we have, so we extract the value under both
  468. interpretations and choose the right one later on. */
  469. ptr_value = extract_typed_address (contents, funcptr_type);
  470. voffset = extract_signed_integer (contents,
  471. TYPE_LENGTH (funcptr_type), byte_order);
  472. contents += TYPE_LENGTH (funcptr_type);
  473. adjustment = extract_signed_integer (contents,
  474. TYPE_LENGTH (offset_type), byte_order);
  475. if (!gdbarch_vbit_in_delta (gdbarch))
  476. {
  477. vbit = voffset & 1;
  478. voffset = voffset ^ vbit;
  479. }
  480. else
  481. {
  482. vbit = adjustment & 1;
  483. adjustment = adjustment >> 1;
  484. }
  485. *value_p = vbit? voffset : ptr_value;
  486. *adjustment_p = adjustment;
  487. return vbit;
  488. }
  489. /* GNU v3 implementation of cplus_print_method_ptr. */
  490. static void
  491. gnuv3_print_method_ptr (const gdb_byte *contents,
  492. struct type *type,
  493. struct ui_file *stream)
  494. {
  495. struct type *self_type = TYPE_SELF_TYPE (type);
  496. struct gdbarch *gdbarch = self_type->arch ();
  497. CORE_ADDR ptr_value;
  498. LONGEST adjustment;
  499. int vbit;
  500. /* Extract the pointer to member. */
  501. vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
  502. /* Check for NULL. */
  503. if (ptr_value == 0 && vbit == 0)
  504. {
  505. gdb_printf (stream, "NULL");
  506. return;
  507. }
  508. /* Search for a virtual method. */
  509. if (vbit)
  510. {
  511. CORE_ADDR voffset;
  512. const char *physname;
  513. /* It's a virtual table offset, maybe in this class. Search
  514. for a field with the correct vtable offset. First convert it
  515. to an index, as used in TYPE_FN_FIELD_VOFFSET. */
  516. voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
  517. physname = gnuv3_find_method_in (self_type, voffset, adjustment);
  518. /* If we found a method, print that. We don't bother to disambiguate
  519. possible paths to the method based on the adjustment. */
  520. if (physname)
  521. {
  522. gdb::unique_xmalloc_ptr<char> demangled_name
  523. = gdb_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
  524. gdb_printf (stream, "&virtual ");
  525. if (demangled_name == NULL)
  526. gdb_puts (physname, stream);
  527. else
  528. gdb_puts (demangled_name.get (), stream);
  529. return;
  530. }
  531. }
  532. else if (ptr_value != 0)
  533. {
  534. /* Found a non-virtual function: print out the type. */
  535. gdb_puts ("(", stream);
  536. c_print_type (type, "", stream, -1, 0, &type_print_raw_options);
  537. gdb_puts (") ", stream);
  538. }
  539. /* We didn't find it; print the raw data. */
  540. if (vbit)
  541. {
  542. gdb_printf (stream, "&virtual table offset ");
  543. print_longest (stream, 'd', 1, ptr_value);
  544. }
  545. else
  546. {
  547. struct value_print_options opts;
  548. get_user_print_options (&opts);
  549. print_address_demangle (&opts, gdbarch, ptr_value, stream, demangle);
  550. }
  551. if (adjustment)
  552. {
  553. gdb_printf (stream, ", this adjustment ");
  554. print_longest (stream, 'd', 1, adjustment);
  555. }
  556. }
  557. /* GNU v3 implementation of cplus_method_ptr_size. */
  558. static int
  559. gnuv3_method_ptr_size (struct type *type)
  560. {
  561. return 2 * TYPE_LENGTH (builtin_type (type->arch ())->builtin_data_ptr);
  562. }
  563. /* GNU v3 implementation of cplus_make_method_ptr. */
  564. static void
  565. gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
  566. CORE_ADDR value, int is_virtual)
  567. {
  568. struct gdbarch *gdbarch = type->arch ();
  569. int size = TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
  570. enum bfd_endian byte_order = type_byte_order (type);
  571. /* FIXME drow/2006-12-24: The adjustment of "this" is currently
  572. always zero, since the method pointer is of the correct type.
  573. But if the method pointer came from a base class, this is
  574. incorrect - it should be the offset to the base. The best
  575. fix might be to create the pointer to member pointing at the
  576. base class and cast it to the derived class, but that requires
  577. support for adjusting pointers to members when casting them -
  578. not currently supported by GDB. */
  579. if (!gdbarch_vbit_in_delta (gdbarch))
  580. {
  581. store_unsigned_integer (contents, size, byte_order, value | is_virtual);
  582. store_unsigned_integer (contents + size, size, byte_order, 0);
  583. }
  584. else
  585. {
  586. store_unsigned_integer (contents, size, byte_order, value);
  587. store_unsigned_integer (contents + size, size, byte_order, is_virtual);
  588. }
  589. }
  590. /* GNU v3 implementation of cplus_method_ptr_to_value. */
  591. static struct value *
  592. gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
  593. {
  594. struct gdbarch *gdbarch;
  595. const gdb_byte *contents = value_contents (method_ptr).data ();
  596. CORE_ADDR ptr_value;
  597. struct type *self_type, *final_type, *method_type;
  598. LONGEST adjustment;
  599. int vbit;
  600. self_type = TYPE_SELF_TYPE (check_typedef (value_type (method_ptr)));
  601. final_type = lookup_pointer_type (self_type);
  602. method_type = TYPE_TARGET_TYPE (check_typedef (value_type (method_ptr)));
  603. /* Extract the pointer to member. */
  604. gdbarch = self_type->arch ();
  605. vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
  606. /* First convert THIS to match the containing type of the pointer to
  607. member. This cast may adjust the value of THIS. */
  608. *this_p = value_cast (final_type, *this_p);
  609. /* Then apply whatever adjustment is necessary. This creates a somewhat
  610. strange pointer: it claims to have type FINAL_TYPE, but in fact it
  611. might not be a valid FINAL_TYPE. For instance, it might be a
  612. base class of FINAL_TYPE. And if it's not the primary base class,
  613. then printing it out as a FINAL_TYPE object would produce some pretty
  614. garbage.
  615. But we don't really know the type of the first argument in
  616. METHOD_TYPE either, which is why this happens. We can't
  617. dereference this later as a FINAL_TYPE, but once we arrive in the
  618. called method we'll have debugging information for the type of
  619. "this" - and that'll match the value we produce here.
  620. You can provoke this case by casting a Base::* to a Derived::*, for
  621. instance. */
  622. *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
  623. *this_p = value_ptradd (*this_p, adjustment);
  624. *this_p = value_cast (final_type, *this_p);
  625. if (vbit)
  626. {
  627. LONGEST voffset;
  628. voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
  629. return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
  630. method_type, voffset);
  631. }
  632. else
  633. return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
  634. }
  635. /* Objects of this type are stored in a hash table and a vector when
  636. printing the vtables for a class. */
  637. struct value_and_voffset
  638. {
  639. /* The value representing the object. */
  640. struct value *value;
  641. /* The maximum vtable offset we've found for any object at this
  642. offset in the outermost object. */
  643. int max_voffset;
  644. };
  645. /* Hash function for value_and_voffset. */
  646. static hashval_t
  647. hash_value_and_voffset (const void *p)
  648. {
  649. const struct value_and_voffset *o = (const struct value_and_voffset *) p;
  650. return value_address (o->value) + value_embedded_offset (o->value);
  651. }
  652. /* Equality function for value_and_voffset. */
  653. static int
  654. eq_value_and_voffset (const void *a, const void *b)
  655. {
  656. const struct value_and_voffset *ova = (const struct value_and_voffset *) a;
  657. const struct value_and_voffset *ovb = (const struct value_and_voffset *) b;
  658. return (value_address (ova->value) + value_embedded_offset (ova->value)
  659. == value_address (ovb->value) + value_embedded_offset (ovb->value));
  660. }
  661. /* Comparison function for value_and_voffset. */
  662. static bool
  663. compare_value_and_voffset (const struct value_and_voffset *va,
  664. const struct value_and_voffset *vb)
  665. {
  666. CORE_ADDR addra = (value_address (va->value)
  667. + value_embedded_offset (va->value));
  668. CORE_ADDR addrb = (value_address (vb->value)
  669. + value_embedded_offset (vb->value));
  670. return addra < addrb;
  671. }
  672. /* A helper function used when printing vtables. This determines the
  673. key (most derived) sub-object at each address and also computes the
  674. maximum vtable offset seen for the corresponding vtable. Updates
  675. OFFSET_HASH and OFFSET_VEC with a new value_and_voffset object, if
  676. needed. VALUE is the object to examine. */
  677. static void
  678. compute_vtable_size (htab_t offset_hash,
  679. std::vector<value_and_voffset *> *offset_vec,
  680. struct value *value)
  681. {
  682. int i;
  683. struct type *type = check_typedef (value_type (value));
  684. void **slot;
  685. struct value_and_voffset search_vo, *current_vo;
  686. gdb_assert (type->code () == TYPE_CODE_STRUCT);
  687. /* If the object is not dynamic, then we are done; as it cannot have
  688. dynamic base types either. */
  689. if (!gnuv3_dynamic_class (type))
  690. return;
  691. /* Update the hash and the vec, if needed. */
  692. search_vo.value = value;
  693. slot = htab_find_slot (offset_hash, &search_vo, INSERT);
  694. if (*slot)
  695. current_vo = (struct value_and_voffset *) *slot;
  696. else
  697. {
  698. current_vo = XNEW (struct value_and_voffset);
  699. current_vo->value = value;
  700. current_vo->max_voffset = -1;
  701. *slot = current_vo;
  702. offset_vec->push_back (current_vo);
  703. }
  704. /* Update the value_and_voffset object with the highest vtable
  705. offset from this class. */
  706. for (i = 0; i < TYPE_NFN_FIELDS (type); ++i)
  707. {
  708. int j;
  709. struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, i);
  710. for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (type, i); ++j)
  711. {
  712. if (TYPE_FN_FIELD_VIRTUAL_P (fn, j))
  713. {
  714. int voffset = TYPE_FN_FIELD_VOFFSET (fn, j);
  715. if (voffset > current_vo->max_voffset)
  716. current_vo->max_voffset = voffset;
  717. }
  718. }
  719. }
  720. /* Recurse into base classes. */
  721. for (i = 0; i < TYPE_N_BASECLASSES (type); ++i)
  722. compute_vtable_size (offset_hash, offset_vec, value_field (value, i));
  723. }
  724. /* Helper for gnuv3_print_vtable that prints a single vtable. */
  725. static void
  726. print_one_vtable (struct gdbarch *gdbarch, struct value *value,
  727. int max_voffset,
  728. struct value_print_options *opts)
  729. {
  730. int i;
  731. struct type *type = check_typedef (value_type (value));
  732. struct value *vtable;
  733. CORE_ADDR vt_addr;
  734. vtable = gnuv3_get_vtable (gdbarch, type,
  735. value_address (value)
  736. + value_embedded_offset (value));
  737. vt_addr = value_address (value_field (vtable,
  738. vtable_field_virtual_functions));
  739. gdb_printf (_("vtable for '%s' @ %s (subobject @ %s):\n"),
  740. TYPE_SAFE_NAME (type),
  741. paddress (gdbarch, vt_addr),
  742. paddress (gdbarch, (value_address (value)
  743. + value_embedded_offset (value))));
  744. for (i = 0; i <= max_voffset; ++i)
  745. {
  746. /* Initialize it just to avoid a GCC false warning. */
  747. CORE_ADDR addr = 0;
  748. int got_error = 0;
  749. struct value *vfn;
  750. gdb_printf ("[%d]: ", i);
  751. vfn = value_subscript (value_field (vtable,
  752. vtable_field_virtual_functions),
  753. i);
  754. if (gdbarch_vtable_function_descriptors (gdbarch))
  755. vfn = value_addr (vfn);
  756. try
  757. {
  758. addr = value_as_address (vfn);
  759. }
  760. catch (const gdb_exception_error &ex)
  761. {
  762. fprintf_styled (gdb_stdout, metadata_style.style (),
  763. _("<error: %s>"), ex.what ());
  764. got_error = 1;
  765. }
  766. if (!got_error)
  767. print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
  768. gdb_printf ("\n");
  769. }
  770. }
  771. /* Implementation of the print_vtable method. */
  772. static void
  773. gnuv3_print_vtable (struct value *value)
  774. {
  775. struct gdbarch *gdbarch;
  776. struct type *type;
  777. struct value *vtable;
  778. struct value_print_options opts;
  779. int count;
  780. value = coerce_ref (value);
  781. type = check_typedef (value_type (value));
  782. if (type->code () == TYPE_CODE_PTR)
  783. {
  784. value = value_ind (value);
  785. type = check_typedef (value_type (value));
  786. }
  787. get_user_print_options (&opts);
  788. /* Respect 'set print object'. */
  789. if (opts.objectprint)
  790. {
  791. value = value_full_object (value, NULL, 0, 0, 0);
  792. type = check_typedef (value_type (value));
  793. }
  794. gdbarch = type->arch ();
  795. vtable = NULL;
  796. if (type->code () == TYPE_CODE_STRUCT)
  797. vtable = gnuv3_get_vtable (gdbarch, type,
  798. value_as_address (value_addr (value)));
  799. if (!vtable)
  800. {
  801. gdb_printf (_("This object does not have a virtual function table\n"));
  802. return;
  803. }
  804. htab_up offset_hash (htab_create_alloc (1, hash_value_and_voffset,
  805. eq_value_and_voffset,
  806. xfree, xcalloc, xfree));
  807. std::vector<value_and_voffset *> result_vec;
  808. compute_vtable_size (offset_hash.get (), &result_vec, value);
  809. std::sort (result_vec.begin (), result_vec.end (),
  810. compare_value_and_voffset);
  811. count = 0;
  812. for (value_and_voffset *iter : result_vec)
  813. {
  814. if (iter->max_voffset >= 0)
  815. {
  816. if (count > 0)
  817. gdb_printf ("\n");
  818. print_one_vtable (gdbarch, iter->value, iter->max_voffset, &opts);
  819. ++count;
  820. }
  821. }
  822. }
  823. /* Return a GDB type representing `struct std::type_info', laid out
  824. appropriately for ARCH.
  825. We use this function as the gdbarch per-architecture data
  826. initialization function. */
  827. static void *
  828. build_std_type_info_type (struct gdbarch *arch)
  829. {
  830. struct type *t;
  831. struct field *field_list, *field;
  832. int offset;
  833. struct type *void_ptr_type
  834. = builtin_type (arch)->builtin_data_ptr;
  835. struct type *char_type
  836. = builtin_type (arch)->builtin_char;
  837. struct type *char_ptr_type
  838. = make_pointer_type (make_cv_type (1, 0, char_type, NULL), NULL);
  839. field_list = XCNEWVEC (struct field, 2);
  840. field = &field_list[0];
  841. offset = 0;
  842. /* The vtable. */
  843. field->set_name ("_vptr.type_info");
  844. field->set_type (void_ptr_type);
  845. field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
  846. offset += TYPE_LENGTH (field->type ());
  847. field++;
  848. /* The name. */
  849. field->set_name ("__name");
  850. field->set_type (char_ptr_type);
  851. field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
  852. offset += TYPE_LENGTH (field->type ());
  853. field++;
  854. gdb_assert (field == (field_list + 2));
  855. t = arch_type (arch, TYPE_CODE_STRUCT, offset * TARGET_CHAR_BIT, NULL);
  856. t->set_num_fields (field - field_list);
  857. t->set_fields (field_list);
  858. t->set_name ("gdb_gnu_v3_type_info");
  859. INIT_CPLUS_SPECIFIC (t);
  860. return t;
  861. }
  862. /* Implement the 'get_typeid_type' method. */
  863. static struct type *
  864. gnuv3_get_typeid_type (struct gdbarch *gdbarch)
  865. {
  866. struct symbol *typeinfo;
  867. struct type *typeinfo_type;
  868. typeinfo = lookup_symbol ("std::type_info", NULL, STRUCT_DOMAIN,
  869. NULL).symbol;
  870. if (typeinfo == NULL)
  871. typeinfo_type
  872. = (struct type *) gdbarch_data (gdbarch, std_type_info_gdbarch_data);
  873. else
  874. typeinfo_type = typeinfo->type ();
  875. return typeinfo_type;
  876. }
  877. /* Implement the 'get_typeid' method. */
  878. static struct value *
  879. gnuv3_get_typeid (struct value *value)
  880. {
  881. struct type *typeinfo_type;
  882. struct type *type;
  883. struct gdbarch *gdbarch;
  884. struct value *result;
  885. std::string type_name;
  886. gdb::unique_xmalloc_ptr<char> canonical;
  887. /* We have to handle values a bit trickily here, to allow this code
  888. to work properly with non_lvalue values that are really just
  889. disguised types. */
  890. if (value_lval_const (value) == lval_memory)
  891. value = coerce_ref (value);
  892. type = check_typedef (value_type (value));
  893. /* In the non_lvalue case, a reference might have slipped through
  894. here. */
  895. if (type->code () == TYPE_CODE_REF)
  896. type = check_typedef (TYPE_TARGET_TYPE (type));
  897. /* Ignore top-level cv-qualifiers. */
  898. type = make_cv_type (0, 0, type, NULL);
  899. gdbarch = type->arch ();
  900. type_name = type_to_string (type);
  901. if (type_name.empty ())
  902. error (_("cannot find typeinfo for unnamed type"));
  903. /* We need to canonicalize the type name here, because we do lookups
  904. using the demangled name, and so we must match the format it
  905. uses. E.g., GDB tends to use "const char *" as a type name, but
  906. the demangler uses "char const *". */
  907. canonical = cp_canonicalize_string (type_name.c_str ());
  908. const char *name = (canonical == nullptr
  909. ? type_name.c_str ()
  910. : canonical.get ());
  911. typeinfo_type = gnuv3_get_typeid_type (gdbarch);
  912. /* We check for lval_memory because in the "typeid (type-id)" case,
  913. the type is passed via a not_lval value object. */
  914. if (type->code () == TYPE_CODE_STRUCT
  915. && value_lval_const (value) == lval_memory
  916. && gnuv3_dynamic_class (type))
  917. {
  918. struct value *vtable, *typeinfo_value;
  919. CORE_ADDR address = value_address (value) + value_embedded_offset (value);
  920. vtable = gnuv3_get_vtable (gdbarch, type, address);
  921. if (vtable == NULL)
  922. error (_("cannot find typeinfo for object of type '%s'"),
  923. name);
  924. typeinfo_value = value_field (vtable, vtable_field_type_info);
  925. result = value_ind (value_cast (make_pointer_type (typeinfo_type, NULL),
  926. typeinfo_value));
  927. }
  928. else
  929. {
  930. std::string sym_name = std::string ("typeinfo for ") + name;
  931. bound_minimal_symbol minsym
  932. = lookup_minimal_symbol (sym_name.c_str (), NULL, NULL);
  933. if (minsym.minsym == NULL)
  934. error (_("could not find typeinfo symbol for '%s'"), name);
  935. result = value_at_lazy (typeinfo_type, BMSYMBOL_VALUE_ADDRESS (minsym));
  936. }
  937. return result;
  938. }
  939. /* Implement the 'get_typename_from_type_info' method. */
  940. static std::string
  941. gnuv3_get_typename_from_type_info (struct value *type_info_ptr)
  942. {
  943. struct gdbarch *gdbarch = value_type (type_info_ptr)->arch ();
  944. struct bound_minimal_symbol typeinfo_sym;
  945. CORE_ADDR addr;
  946. const char *symname;
  947. const char *class_name;
  948. const char *atsign;
  949. addr = value_as_address (type_info_ptr);
  950. typeinfo_sym = lookup_minimal_symbol_by_pc (addr);
  951. if (typeinfo_sym.minsym == NULL)
  952. error (_("could not find minimal symbol for typeinfo address %s"),
  953. paddress (gdbarch, addr));
  954. #define TYPEINFO_PREFIX "typeinfo for "
  955. #define TYPEINFO_PREFIX_LEN (sizeof (TYPEINFO_PREFIX) - 1)
  956. symname = typeinfo_sym.minsym->demangled_name ();
  957. if (symname == NULL || strncmp (symname, TYPEINFO_PREFIX,
  958. TYPEINFO_PREFIX_LEN))
  959. error (_("typeinfo symbol '%s' has unexpected name"),
  960. typeinfo_sym.minsym->linkage_name ());
  961. class_name = symname + TYPEINFO_PREFIX_LEN;
  962. /* Strip off @plt and version suffixes. */
  963. atsign = strchr (class_name, '@');
  964. if (atsign != NULL)
  965. return std::string (class_name, atsign - class_name);
  966. return class_name;
  967. }
  968. /* Implement the 'get_type_from_type_info' method. */
  969. static struct type *
  970. gnuv3_get_type_from_type_info (struct value *type_info_ptr)
  971. {
  972. /* We have to parse the type name, since in general there is not a
  973. symbol for a type. This is somewhat bogus since there may be a
  974. mis-parse. Another approach might be to re-use the demangler's
  975. internal form to reconstruct the type somehow. */
  976. std::string type_name = gnuv3_get_typename_from_type_info (type_info_ptr);
  977. expression_up expr (parse_expression (type_name.c_str ()));
  978. struct value *type_val = evaluate_type (expr.get ());
  979. return value_type (type_val);
  980. }
  981. /* Determine if we are currently in a C++ thunk. If so, get the address
  982. of the routine we are thunking to and continue to there instead. */
  983. static CORE_ADDR
  984. gnuv3_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
  985. {
  986. CORE_ADDR real_stop_pc, method_stop_pc, func_addr;
  987. struct gdbarch *gdbarch = get_frame_arch (frame);
  988. struct bound_minimal_symbol thunk_sym, fn_sym;
  989. struct obj_section *section;
  990. const char *thunk_name, *fn_name;
  991. real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
  992. if (real_stop_pc == 0)
  993. real_stop_pc = stop_pc;
  994. /* Find the linker symbol for this potential thunk. */
  995. thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
  996. section = find_pc_section (real_stop_pc);
  997. if (thunk_sym.minsym == NULL || section == NULL)
  998. return 0;
  999. /* The symbol's demangled name should be something like "virtual
  1000. thunk to FUNCTION", where FUNCTION is the name of the function
  1001. being thunked to. */
  1002. thunk_name = thunk_sym.minsym->demangled_name ();
  1003. if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
  1004. return 0;
  1005. fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
  1006. fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile);
  1007. if (fn_sym.minsym == NULL)
  1008. return 0;
  1009. method_stop_pc = BMSYMBOL_VALUE_ADDRESS (fn_sym);
  1010. /* Some targets have minimal symbols pointing to function descriptors
  1011. (powerpc 64 for example). Make sure to retrieve the address
  1012. of the real function from the function descriptor before passing on
  1013. the address to other layers of GDB. */
  1014. func_addr = gdbarch_convert_from_func_ptr_addr
  1015. (gdbarch, method_stop_pc, current_inferior ()->top_target ());
  1016. if (func_addr != 0)
  1017. method_stop_pc = func_addr;
  1018. real_stop_pc = gdbarch_skip_trampoline_code
  1019. (gdbarch, frame, method_stop_pc);
  1020. if (real_stop_pc == 0)
  1021. real_stop_pc = method_stop_pc;
  1022. return real_stop_pc;
  1023. }
  1024. /* A member function is in one these states. */
  1025. enum definition_style
  1026. {
  1027. DOES_NOT_EXIST_IN_SOURCE,
  1028. DEFAULTED_INSIDE,
  1029. DEFAULTED_OUTSIDE,
  1030. DELETED,
  1031. EXPLICIT,
  1032. };
  1033. /* Return how the given field is defined. */
  1034. static definition_style
  1035. get_def_style (struct fn_field *fn, int fieldelem)
  1036. {
  1037. if (TYPE_FN_FIELD_DELETED (fn, fieldelem))
  1038. return DELETED;
  1039. if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
  1040. return DOES_NOT_EXIST_IN_SOURCE;
  1041. switch (TYPE_FN_FIELD_DEFAULTED (fn, fieldelem))
  1042. {
  1043. case DW_DEFAULTED_no:
  1044. return EXPLICIT;
  1045. case DW_DEFAULTED_in_class:
  1046. return DEFAULTED_INSIDE;
  1047. case DW_DEFAULTED_out_of_class:
  1048. return DEFAULTED_OUTSIDE;
  1049. default:
  1050. break;
  1051. }
  1052. return EXPLICIT;
  1053. }
  1054. /* Helper functions to determine whether the given definition style
  1055. denotes that the definition is user-provided or implicit.
  1056. Being defaulted outside the class decl counts as an explicit
  1057. user-definition, while being defaulted inside is implicit. */
  1058. static bool
  1059. is_user_provided_def (definition_style def)
  1060. {
  1061. return def == EXPLICIT || def == DEFAULTED_OUTSIDE;
  1062. }
  1063. static bool
  1064. is_implicit_def (definition_style def)
  1065. {
  1066. return def == DOES_NOT_EXIST_IN_SOURCE || def == DEFAULTED_INSIDE;
  1067. }
  1068. /* Helper function to decide if METHOD_TYPE is a copy/move
  1069. constructor type for CLASS_TYPE. EXPECTED is the expected
  1070. type code for the "right-hand-side" argument.
  1071. This function is supposed to be used by the IS_COPY_CONSTRUCTOR_TYPE
  1072. and IS_MOVE_CONSTRUCTOR_TYPE functions below. Normally, you should
  1073. not need to call this directly. */
  1074. static bool
  1075. is_copy_or_move_constructor_type (struct type *class_type,
  1076. struct type *method_type,
  1077. type_code expected)
  1078. {
  1079. /* The method should take at least two arguments... */
  1080. if (method_type->num_fields () < 2)
  1081. return false;
  1082. /* ...and the second argument should be the same as the class
  1083. type, with the expected type code... */
  1084. struct type *arg_type = method_type->field (1).type ();
  1085. if (arg_type->code () != expected)
  1086. return false;
  1087. struct type *target = check_typedef (TYPE_TARGET_TYPE (arg_type));
  1088. if (!(class_types_same_p (target, class_type)))
  1089. return false;
  1090. /* ...and if any of the remaining arguments don't have a default value
  1091. then this is not a copy or move constructor, but just a
  1092. constructor. */
  1093. for (int i = 2; i < method_type->num_fields (); i++)
  1094. {
  1095. arg_type = method_type->field (i).type ();
  1096. /* FIXME aktemur/2019-10-31: As of this date, neither
  1097. clang++-7.0.0 nor g++-8.2.0 produce a DW_AT_default_value
  1098. attribute. GDB is also not set to read this attribute, yet.
  1099. Hence, we immediately return false if there are more than
  1100. 2 parameters.
  1101. GCC bug link:
  1102. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42959
  1103. */
  1104. return false;
  1105. }
  1106. return true;
  1107. }
  1108. /* Return true if METHOD_TYPE is a copy ctor type for CLASS_TYPE. */
  1109. static bool
  1110. is_copy_constructor_type (struct type *class_type,
  1111. struct type *method_type)
  1112. {
  1113. return is_copy_or_move_constructor_type (class_type, method_type,
  1114. TYPE_CODE_REF);
  1115. }
  1116. /* Return true if METHOD_TYPE is a move ctor type for CLASS_TYPE. */
  1117. static bool
  1118. is_move_constructor_type (struct type *class_type,
  1119. struct type *method_type)
  1120. {
  1121. return is_copy_or_move_constructor_type (class_type, method_type,
  1122. TYPE_CODE_RVALUE_REF);
  1123. }
  1124. /* Return pass-by-reference information for the given TYPE.
  1125. The rule in the v3 ABI document comes from section 3.1.1. If the
  1126. type has a non-trivial copy constructor or destructor, then the
  1127. caller must make a copy (by calling the copy constructor if there
  1128. is one or perform the copy itself otherwise), pass the address of
  1129. the copy, and then destroy the temporary (if necessary).
  1130. For return values with non-trivial copy/move constructors or
  1131. destructors, space will be allocated in the caller, and a pointer
  1132. will be passed as the first argument (preceding "this").
  1133. We don't have a bulletproof mechanism for determining whether a
  1134. constructor or destructor is trivial. For GCC and DWARF5 debug
  1135. information, we can check the calling_convention attribute,
  1136. the 'artificial' flag, the 'defaulted' attribute, and the
  1137. 'deleted' attribute. */
  1138. static struct language_pass_by_ref_info
  1139. gnuv3_pass_by_reference (struct type *type)
  1140. {
  1141. int fieldnum, fieldelem;
  1142. type = check_typedef (type);
  1143. /* Start with the default values. */
  1144. struct language_pass_by_ref_info info;
  1145. bool has_cc_attr = false;
  1146. bool is_pass_by_value = false;
  1147. bool is_dynamic = false;
  1148. definition_style cctor_def = DOES_NOT_EXIST_IN_SOURCE;
  1149. definition_style dtor_def = DOES_NOT_EXIST_IN_SOURCE;
  1150. definition_style mctor_def = DOES_NOT_EXIST_IN_SOURCE;
  1151. /* We're only interested in things that can have methods. */
  1152. if (type->code () != TYPE_CODE_STRUCT
  1153. && type->code () != TYPE_CODE_UNION)
  1154. return info;
  1155. /* The compiler may have emitted the calling convention attribute.
  1156. Note: GCC does not produce this attribute as of version 9.2.1.
  1157. Bug link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92418 */
  1158. if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_value)
  1159. {
  1160. has_cc_attr = true;
  1161. is_pass_by_value = true;
  1162. /* Do not return immediately. We have to find out if this type
  1163. is copy_constructible and destructible. */
  1164. }
  1165. if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_reference)
  1166. {
  1167. has_cc_attr = true;
  1168. is_pass_by_value = false;
  1169. }
  1170. /* A dynamic class has a non-trivial copy constructor.
  1171. See c++98 section 12.8 Copying class objects [class.copy]. */
  1172. if (gnuv3_dynamic_class (type))
  1173. is_dynamic = true;
  1174. for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
  1175. for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
  1176. fieldelem++)
  1177. {
  1178. struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
  1179. const char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
  1180. struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
  1181. if (name[0] == '~')
  1182. {
  1183. /* We've found a destructor.
  1184. There should be at most one dtor definition. */
  1185. gdb_assert (dtor_def == DOES_NOT_EXIST_IN_SOURCE);
  1186. dtor_def = get_def_style (fn, fieldelem);
  1187. }
  1188. else if (is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))
  1189. || TYPE_FN_FIELD_CONSTRUCTOR (fn, fieldelem))
  1190. {
  1191. /* FIXME drow/2007-09-23: We could do this using the name of
  1192. the method and the name of the class instead of dealing
  1193. with the mangled name. We don't have a convenient function
  1194. to strip off both leading scope qualifiers and trailing
  1195. template arguments yet. */
  1196. if (is_copy_constructor_type (type, fieldtype))
  1197. {
  1198. /* There may be more than one cctors. E.g.: one that
  1199. take a const parameter and another that takes a
  1200. non-const parameter. Such as:
  1201. class K {
  1202. K (const K &k)...
  1203. K (K &k)...
  1204. };
  1205. It is sufficient for the type to be non-trivial
  1206. even only one of the cctors is explicit.
  1207. Therefore, update the cctor_def value in the
  1208. implicit -> explicit direction, not backwards. */
  1209. if (is_implicit_def (cctor_def))
  1210. cctor_def = get_def_style (fn, fieldelem);
  1211. }
  1212. else if (is_move_constructor_type (type, fieldtype))
  1213. {
  1214. /* Again, there may be multiple move ctors. Update the
  1215. mctor_def value if we found an explicit def and the
  1216. existing one is not explicit. Otherwise retain the
  1217. existing value. */
  1218. if (is_implicit_def (mctor_def))
  1219. mctor_def = get_def_style (fn, fieldelem);
  1220. }
  1221. }
  1222. }
  1223. bool cctor_implicitly_deleted
  1224. = (mctor_def != DOES_NOT_EXIST_IN_SOURCE
  1225. && cctor_def == DOES_NOT_EXIST_IN_SOURCE);
  1226. bool cctor_explicitly_deleted = (cctor_def == DELETED);
  1227. if (cctor_implicitly_deleted || cctor_explicitly_deleted)
  1228. info.copy_constructible = false;
  1229. if (dtor_def == DELETED)
  1230. info.destructible = false;
  1231. info.trivially_destructible = is_implicit_def (dtor_def);
  1232. info.trivially_copy_constructible
  1233. = (is_implicit_def (cctor_def)
  1234. && !is_dynamic);
  1235. info.trivially_copyable
  1236. = (info.trivially_copy_constructible
  1237. && info.trivially_destructible
  1238. && !is_user_provided_def (mctor_def));
  1239. /* Even if all the constructors and destructors were artificial, one
  1240. of them may have invoked a non-artificial constructor or
  1241. destructor in a base class. If any base class needs to be passed
  1242. by reference, so does this class. Similarly for members, which
  1243. are constructed whenever this class is. We do not need to worry
  1244. about recursive loops here, since we are only looking at members
  1245. of complete class type. Also ignore any static members. */
  1246. for (fieldnum = 0; fieldnum < type->num_fields (); fieldnum++)
  1247. if (!field_is_static (&type->field (fieldnum)))
  1248. {
  1249. struct type *field_type = type->field (fieldnum).type ();
  1250. /* For arrays, make the decision based on the element type. */
  1251. if (field_type->code () == TYPE_CODE_ARRAY)
  1252. field_type = check_typedef (TYPE_TARGET_TYPE (field_type));
  1253. struct language_pass_by_ref_info field_info
  1254. = gnuv3_pass_by_reference (field_type);
  1255. if (!field_info.copy_constructible)
  1256. info.copy_constructible = false;
  1257. if (!field_info.destructible)
  1258. info.destructible = false;
  1259. if (!field_info.trivially_copyable)
  1260. info.trivially_copyable = false;
  1261. if (!field_info.trivially_copy_constructible)
  1262. info.trivially_copy_constructible = false;
  1263. if (!field_info.trivially_destructible)
  1264. info.trivially_destructible = false;
  1265. }
  1266. /* Consistency check. */
  1267. if (has_cc_attr && info.trivially_copyable != is_pass_by_value)
  1268. {
  1269. /* DWARF CC attribute is not the same as the inferred value;
  1270. use the DWARF attribute. */
  1271. info.trivially_copyable = is_pass_by_value;
  1272. }
  1273. return info;
  1274. }
  1275. static void
  1276. init_gnuv3_ops (void)
  1277. {
  1278. vtable_type_gdbarch_data
  1279. = gdbarch_data_register_post_init (build_gdb_vtable_type);
  1280. std_type_info_gdbarch_data
  1281. = gdbarch_data_register_post_init (build_std_type_info_type);
  1282. gnu_v3_abi_ops.shortname = "gnu-v3";
  1283. gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
  1284. gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
  1285. gnu_v3_abi_ops.is_destructor_name =
  1286. (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
  1287. gnu_v3_abi_ops.is_constructor_name =
  1288. (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
  1289. gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
  1290. gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
  1291. gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
  1292. gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
  1293. gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
  1294. gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
  1295. gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
  1296. gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
  1297. gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
  1298. gnu_v3_abi_ops.print_vtable = gnuv3_print_vtable;
  1299. gnu_v3_abi_ops.get_typeid = gnuv3_get_typeid;
  1300. gnu_v3_abi_ops.get_typeid_type = gnuv3_get_typeid_type;
  1301. gnu_v3_abi_ops.get_type_from_type_info = gnuv3_get_type_from_type_info;
  1302. gnu_v3_abi_ops.get_typename_from_type_info
  1303. = gnuv3_get_typename_from_type_info;
  1304. gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
  1305. gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
  1306. }
  1307. void _initialize_gnu_v3_abi ();
  1308. void
  1309. _initialize_gnu_v3_abi ()
  1310. {
  1311. init_gnuv3_ops ();
  1312. register_cp_abi (&gnu_v3_abi_ops);
  1313. set_cp_abi_as_auto_default (gnu_v3_abi_ops.shortname);
  1314. }