arch-utils.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. /* Dynamic architecture support for GDB, the GNU debugger.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "gdbcmd.h"
  17. #include "inferior.h" /* enum CALL_DUMMY_LOCATION et al. */
  18. #include "infrun.h"
  19. #include "regcache.h"
  20. #include "sim-regno.h"
  21. #include "gdbcore.h"
  22. #include "osabi.h"
  23. #include "target-descriptions.h"
  24. #include "objfiles.h"
  25. #include "language.h"
  26. #include "symtab.h"
  27. #include "dummy-frame.h"
  28. #include "frame-unwind.h"
  29. #include "reggroups.h"
  30. #include "auxv.h"
  31. #include "observable.h"
  32. #include "gdbsupport/version.h"
  33. #include "floatformat.h"
  34. #include "dis-asm.h"
  35. bool
  36. default_displaced_step_hw_singlestep (struct gdbarch *gdbarch)
  37. {
  38. return !gdbarch_software_single_step_p (gdbarch);
  39. }
  40. CORE_ADDR
  41. displaced_step_at_entry_point (struct gdbarch *gdbarch)
  42. {
  43. CORE_ADDR addr;
  44. int bp_len;
  45. addr = entry_point_address ();
  46. /* Inferior calls also use the entry point as a breakpoint location.
  47. We don't want displaced stepping to interfere with those
  48. breakpoints, so leave space. */
  49. gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
  50. addr += bp_len * 2;
  51. return addr;
  52. }
  53. int
  54. legacy_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  55. {
  56. /* Only makes sense to supply raw registers. */
  57. gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
  58. /* NOTE: cagney/2002-05-13: The old code did it this way and it is
  59. suspected that some GDB/SIM combinations may rely on this
  60. behaviour. The default should be one2one_register_sim_regno
  61. (below). */
  62. if (gdbarch_register_name (gdbarch, regnum) != NULL
  63. && gdbarch_register_name (gdbarch, regnum)[0] != '\0')
  64. return regnum;
  65. else
  66. return LEGACY_SIM_REGNO_IGNORE;
  67. }
  68. /* See arch-utils.h */
  69. std::string
  70. default_memtag_to_string (struct gdbarch *gdbarch, struct value *tag)
  71. {
  72. error (_("This architecture has no method to convert a memory tag to"
  73. " a string."));
  74. }
  75. /* See arch-utils.h */
  76. bool
  77. default_tagged_address_p (struct gdbarch *gdbarch, struct value *address)
  78. {
  79. /* By default, assume the address is untagged. */
  80. return false;
  81. }
  82. /* See arch-utils.h */
  83. bool
  84. default_memtag_matches_p (struct gdbarch *gdbarch, struct value *address)
  85. {
  86. /* By default, assume the tags match. */
  87. return true;
  88. }
  89. /* See arch-utils.h */
  90. bool
  91. default_set_memtags (struct gdbarch *gdbarch, struct value *address,
  92. size_t length, const gdb::byte_vector &tags,
  93. memtag_type tag_type)
  94. {
  95. /* By default, return true (successful); */
  96. return true;
  97. }
  98. /* See arch-utils.h */
  99. struct value *
  100. default_get_memtag (struct gdbarch *gdbarch, struct value *address,
  101. memtag_type tag_type)
  102. {
  103. /* By default, return no tag. */
  104. return nullptr;
  105. }
  106. CORE_ADDR
  107. generic_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  108. {
  109. return 0;
  110. }
  111. CORE_ADDR
  112. generic_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
  113. {
  114. return 0;
  115. }
  116. int
  117. generic_in_solib_return_trampoline (struct gdbarch *gdbarch,
  118. CORE_ADDR pc, const char *name)
  119. {
  120. return 0;
  121. }
  122. int
  123. generic_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  124. {
  125. return 0;
  126. }
  127. int
  128. default_code_of_frame_writable (struct gdbarch *gdbarch,
  129. struct frame_info *frame)
  130. {
  131. return 1;
  132. }
  133. /* Helper functions for gdbarch_inner_than */
  134. int
  135. core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
  136. {
  137. return (lhs < rhs);
  138. }
  139. int
  140. core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
  141. {
  142. return (lhs > rhs);
  143. }
  144. /* Misc helper functions for targets. */
  145. CORE_ADDR
  146. core_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr)
  147. {
  148. return addr;
  149. }
  150. CORE_ADDR
  151. convert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
  152. struct target_ops *targ)
  153. {
  154. return addr;
  155. }
  156. int
  157. no_op_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  158. {
  159. return reg;
  160. }
  161. void
  162. default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
  163. {
  164. return;
  165. }
  166. /* See arch-utils.h. */
  167. void
  168. default_make_symbol_special (struct symbol *sym, struct objfile *objfile)
  169. {
  170. return;
  171. }
  172. /* See arch-utils.h. */
  173. CORE_ADDR
  174. default_adjust_dwarf2_addr (CORE_ADDR pc)
  175. {
  176. return pc;
  177. }
  178. /* See arch-utils.h. */
  179. CORE_ADDR
  180. default_adjust_dwarf2_line (CORE_ADDR addr, int rel)
  181. {
  182. return addr;
  183. }
  184. /* See arch-utils.h. */
  185. bool
  186. default_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
  187. struct dwarf2_frame_state *fs)
  188. {
  189. return false;
  190. }
  191. int
  192. cannot_register_not (struct gdbarch *gdbarch, int regnum)
  193. {
  194. return 0;
  195. }
  196. /* Legacy version of target_virtual_frame_pointer(). Assumes that
  197. there is an gdbarch_deprecated_fp_regnum and that it is the same,
  198. cooked or raw. */
  199. void
  200. legacy_virtual_frame_pointer (struct gdbarch *gdbarch,
  201. CORE_ADDR pc,
  202. int *frame_regnum,
  203. LONGEST *frame_offset)
  204. {
  205. /* FIXME: cagney/2002-09-13: This code is used when identifying the
  206. frame pointer of the current PC. It is assuming that a single
  207. register and an offset can determine this. I think it should
  208. instead generate a byte code expression as that would work better
  209. with things like Dwarf2's CFI. */
  210. if (gdbarch_deprecated_fp_regnum (gdbarch) >= 0
  211. && gdbarch_deprecated_fp_regnum (gdbarch)
  212. < gdbarch_num_regs (gdbarch))
  213. *frame_regnum = gdbarch_deprecated_fp_regnum (gdbarch);
  214. else if (gdbarch_sp_regnum (gdbarch) >= 0
  215. && gdbarch_sp_regnum (gdbarch)
  216. < gdbarch_num_regs (gdbarch))
  217. *frame_regnum = gdbarch_sp_regnum (gdbarch);
  218. else
  219. /* Should this be an internal error? I guess so, it is reflecting
  220. an architectural limitation in the current design. */
  221. internal_error (__FILE__, __LINE__,
  222. _("No virtual frame pointer available"));
  223. *frame_offset = 0;
  224. }
  225. /* Return a floating-point format for a floating-point variable of
  226. length LEN in bits. If non-NULL, NAME is the name of its type.
  227. If no suitable type is found, return NULL. */
  228. const struct floatformat **
  229. default_floatformat_for_type (struct gdbarch *gdbarch,
  230. const char *name, int len)
  231. {
  232. const struct floatformat **format = NULL;
  233. /* Check if this is a bfloat16 type. It has the same size as the
  234. IEEE half float type, so we use the base type name to tell them
  235. apart. */
  236. if (name != nullptr && strcmp (name, "__bf16") == 0
  237. && len == gdbarch_bfloat16_bit (gdbarch))
  238. format = gdbarch_bfloat16_format (gdbarch);
  239. else if (len == gdbarch_half_bit (gdbarch))
  240. format = gdbarch_half_format (gdbarch);
  241. else if (len == gdbarch_float_bit (gdbarch))
  242. format = gdbarch_float_format (gdbarch);
  243. else if (len == gdbarch_double_bit (gdbarch))
  244. format = gdbarch_double_format (gdbarch);
  245. else if (len == gdbarch_long_double_bit (gdbarch))
  246. format = gdbarch_long_double_format (gdbarch);
  247. /* On i386 the 'long double' type takes 96 bits,
  248. while the real number of used bits is only 80,
  249. both in processor and in memory.
  250. The code below accepts the real bit size. */
  251. else if (gdbarch_long_double_format (gdbarch) != NULL
  252. && len == gdbarch_long_double_format (gdbarch)[0]->totalsize)
  253. format = gdbarch_long_double_format (gdbarch);
  254. return format;
  255. }
  256. int
  257. generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
  258. struct type *type)
  259. {
  260. return 0;
  261. }
  262. int
  263. default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
  264. {
  265. return 0;
  266. }
  267. int
  268. generic_instruction_nullified (struct gdbarch *gdbarch,
  269. struct regcache *regcache)
  270. {
  271. return 0;
  272. }
  273. int
  274. default_remote_register_number (struct gdbarch *gdbarch,
  275. int regno)
  276. {
  277. return regno;
  278. }
  279. /* See arch-utils.h. */
  280. int
  281. default_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
  282. {
  283. return 0;
  284. }
  285. /* Functions to manipulate the endianness of the target. */
  286. static enum bfd_endian target_byte_order_user = BFD_ENDIAN_UNKNOWN;
  287. static const char endian_big[] = "big";
  288. static const char endian_little[] = "little";
  289. static const char endian_auto[] = "auto";
  290. static const char *const endian_enum[] =
  291. {
  292. endian_big,
  293. endian_little,
  294. endian_auto,
  295. NULL,
  296. };
  297. static const char *set_endian_string = endian_auto;
  298. enum bfd_endian
  299. selected_byte_order (void)
  300. {
  301. return target_byte_order_user;
  302. }
  303. /* Called by ``show endian''. */
  304. static void
  305. show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
  306. const char *value)
  307. {
  308. if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
  309. if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG)
  310. gdb_printf (file, _("The target endianness is set automatically "
  311. "(currently big endian).\n"));
  312. else
  313. gdb_printf (file, _("The target endianness is set automatically "
  314. "(currently little endian).\n"));
  315. else
  316. if (target_byte_order_user == BFD_ENDIAN_BIG)
  317. gdb_printf (file,
  318. _("The target is set to big endian.\n"));
  319. else
  320. gdb_printf (file,
  321. _("The target is set to little endian.\n"));
  322. }
  323. static void
  324. set_endian (const char *ignore_args, int from_tty, struct cmd_list_element *c)
  325. {
  326. struct gdbarch_info info;
  327. if (set_endian_string == endian_auto)
  328. {
  329. target_byte_order_user = BFD_ENDIAN_UNKNOWN;
  330. if (! gdbarch_update_p (info))
  331. internal_error (__FILE__, __LINE__,
  332. _("set_endian: architecture update failed"));
  333. }
  334. else if (set_endian_string == endian_little)
  335. {
  336. info.byte_order = BFD_ENDIAN_LITTLE;
  337. if (! gdbarch_update_p (info))
  338. gdb_printf (gdb_stderr,
  339. _("Little endian target not supported by GDB\n"));
  340. else
  341. target_byte_order_user = BFD_ENDIAN_LITTLE;
  342. }
  343. else if (set_endian_string == endian_big)
  344. {
  345. info.byte_order = BFD_ENDIAN_BIG;
  346. if (! gdbarch_update_p (info))
  347. gdb_printf (gdb_stderr,
  348. _("Big endian target not supported by GDB\n"));
  349. else
  350. target_byte_order_user = BFD_ENDIAN_BIG;
  351. }
  352. else
  353. internal_error (__FILE__, __LINE__,
  354. _("set_endian: bad value"));
  355. show_endian (gdb_stdout, from_tty, NULL, NULL);
  356. }
  357. /* Given SELECTED, a currently selected BFD architecture, and
  358. TARGET_DESC, the current target description, return what
  359. architecture to use.
  360. SELECTED may be NULL, in which case we return the architecture
  361. associated with TARGET_DESC. If SELECTED specifies a variant
  362. of the architecture associated with TARGET_DESC, return the
  363. more specific of the two.
  364. If SELECTED is a different architecture, but it is accepted as
  365. compatible by the target, we can use the target architecture.
  366. If SELECTED is obviously incompatible, warn the user. */
  367. static const struct bfd_arch_info *
  368. choose_architecture_for_target (const struct target_desc *target_desc,
  369. const struct bfd_arch_info *selected)
  370. {
  371. const struct bfd_arch_info *from_target = tdesc_architecture (target_desc);
  372. const struct bfd_arch_info *compat1, *compat2;
  373. if (selected == NULL)
  374. return from_target;
  375. if (from_target == NULL)
  376. return selected;
  377. /* struct bfd_arch_info objects are singletons: that is, there's
  378. supposed to be exactly one instance for a given machine. So you
  379. can tell whether two are equivalent by comparing pointers. */
  380. if (from_target == selected)
  381. return selected;
  382. /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
  383. incompatible. But if they are compatible, it returns the 'more
  384. featureful' of the two arches. That is, if A can run code
  385. written for B, but B can't run code written for A, then it'll
  386. return A.
  387. Some targets (e.g. MIPS as of 2006-12-04) don't fully
  388. implement this, instead always returning NULL or the first
  389. argument. We detect that case by checking both directions. */
  390. compat1 = selected->compatible (selected, from_target);
  391. compat2 = from_target->compatible (from_target, selected);
  392. if (compat1 == NULL && compat2 == NULL)
  393. {
  394. /* BFD considers the architectures incompatible. Check our
  395. target description whether it accepts SELECTED as compatible
  396. anyway. */
  397. if (tdesc_compatible_p (target_desc, selected))
  398. return from_target;
  399. warning (_("Selected architecture %s is not compatible "
  400. "with reported target architecture %s"),
  401. selected->printable_name, from_target->printable_name);
  402. return selected;
  403. }
  404. if (compat1 == NULL)
  405. return compat2;
  406. if (compat2 == NULL)
  407. return compat1;
  408. if (compat1 == compat2)
  409. return compat1;
  410. /* If the two didn't match, but one of them was a default
  411. architecture, assume the more specific one is correct. This
  412. handles the case where an executable or target description just
  413. says "mips", but the other knows which MIPS variant. */
  414. if (compat1->the_default)
  415. return compat2;
  416. if (compat2->the_default)
  417. return compat1;
  418. /* We have no idea which one is better. This is a bug, but not
  419. a critical problem; warn the user. */
  420. warning (_("Selected architecture %s is ambiguous with "
  421. "reported target architecture %s"),
  422. selected->printable_name, from_target->printable_name);
  423. return selected;
  424. }
  425. /* Functions to manipulate the architecture of the target. */
  426. enum set_arch { set_arch_auto, set_arch_manual };
  427. static const struct bfd_arch_info *target_architecture_user;
  428. static const char *set_architecture_string;
  429. const char *
  430. selected_architecture_name (void)
  431. {
  432. if (target_architecture_user == NULL)
  433. return NULL;
  434. else
  435. return set_architecture_string;
  436. }
  437. /* Called if the user enters ``show architecture'' without an
  438. argument. */
  439. static void
  440. show_architecture (struct ui_file *file, int from_tty,
  441. struct cmd_list_element *c, const char *value)
  442. {
  443. if (target_architecture_user == NULL)
  444. gdb_printf (file, _("The target architecture is set to "
  445. "\"auto\" (currently \"%s\").\n"),
  446. gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
  447. else
  448. gdb_printf (file, _("The target architecture is set to \"%s\".\n"),
  449. set_architecture_string);
  450. }
  451. /* Called if the user enters ``set architecture'' with or without an
  452. argument. */
  453. static void
  454. set_architecture (const char *ignore_args,
  455. int from_tty, struct cmd_list_element *c)
  456. {
  457. struct gdbarch_info info;
  458. if (strcmp (set_architecture_string, "auto") == 0)
  459. {
  460. target_architecture_user = NULL;
  461. if (!gdbarch_update_p (info))
  462. internal_error (__FILE__, __LINE__,
  463. _("could not select an architecture automatically"));
  464. }
  465. else
  466. {
  467. info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
  468. if (info.bfd_arch_info == NULL)
  469. internal_error (__FILE__, __LINE__,
  470. _("set_architecture: bfd_scan_arch failed"));
  471. if (gdbarch_update_p (info))
  472. target_architecture_user = info.bfd_arch_info;
  473. else
  474. gdb_printf (gdb_stderr,
  475. _("Architecture `%s' not recognized.\n"),
  476. set_architecture_string);
  477. }
  478. show_architecture (gdb_stdout, from_tty, NULL, NULL);
  479. }
  480. /* Try to select a global architecture that matches "info". Return
  481. non-zero if the attempt succeeds. */
  482. int
  483. gdbarch_update_p (struct gdbarch_info info)
  484. {
  485. struct gdbarch *new_gdbarch;
  486. /* Check for the current file. */
  487. if (info.abfd == NULL)
  488. info.abfd = current_program_space->exec_bfd ();
  489. if (info.abfd == NULL)
  490. info.abfd = core_bfd;
  491. /* Check for the current target description. */
  492. if (info.target_desc == NULL)
  493. info.target_desc = target_current_description ();
  494. new_gdbarch = gdbarch_find_by_info (info);
  495. /* If there no architecture by that name, reject the request. */
  496. if (new_gdbarch == NULL)
  497. {
  498. if (gdbarch_debug)
  499. gdb_printf (gdb_stdlog, "gdbarch_update_p: "
  500. "Architecture not found\n");
  501. return 0;
  502. }
  503. /* If it is the same old architecture, accept the request (but don't
  504. swap anything). */
  505. if (new_gdbarch == target_gdbarch ())
  506. {
  507. if (gdbarch_debug)
  508. gdb_printf (gdb_stdlog, "gdbarch_update_p: "
  509. "Architecture %s (%s) unchanged\n",
  510. host_address_to_string (new_gdbarch),
  511. gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
  512. return 1;
  513. }
  514. /* It's a new architecture, swap it in. */
  515. if (gdbarch_debug)
  516. gdb_printf (gdb_stdlog, "gdbarch_update_p: "
  517. "New architecture %s (%s) selected\n",
  518. host_address_to_string (new_gdbarch),
  519. gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
  520. set_target_gdbarch (new_gdbarch);
  521. return 1;
  522. }
  523. /* Return the architecture for ABFD. If no suitable architecture
  524. could be find, return NULL. */
  525. struct gdbarch *
  526. gdbarch_from_bfd (bfd *abfd)
  527. {
  528. struct gdbarch_info info;
  529. info.abfd = abfd;
  530. return gdbarch_find_by_info (info);
  531. }
  532. /* Set the dynamic target-system-dependent parameters (architecture,
  533. byte-order) using information found in the BFD */
  534. void
  535. set_gdbarch_from_file (bfd *abfd)
  536. {
  537. struct gdbarch_info info;
  538. struct gdbarch *gdbarch;
  539. info.abfd = abfd;
  540. info.target_desc = target_current_description ();
  541. gdbarch = gdbarch_find_by_info (info);
  542. if (gdbarch == NULL)
  543. error (_("Architecture of file not recognized."));
  544. set_target_gdbarch (gdbarch);
  545. }
  546. /* Initialize the current architecture. Update the ``set
  547. architecture'' command so that it specifies a list of valid
  548. architectures. */
  549. #ifdef DEFAULT_BFD_ARCH
  550. extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
  551. static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
  552. #else
  553. static const bfd_arch_info_type *default_bfd_arch;
  554. #endif
  555. #ifdef DEFAULT_BFD_VEC
  556. extern const bfd_target DEFAULT_BFD_VEC;
  557. static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
  558. #else
  559. static const bfd_target *default_bfd_vec;
  560. #endif
  561. static enum bfd_endian default_byte_order = BFD_ENDIAN_UNKNOWN;
  562. /* Printable names of architectures. Used as the enum list of the
  563. "set arch" command. */
  564. static std::vector<const char *> arches;
  565. void
  566. initialize_current_architecture (void)
  567. {
  568. arches = gdbarch_printable_names ();
  569. /* Find a default architecture. */
  570. if (default_bfd_arch == NULL)
  571. {
  572. /* Choose the architecture by taking the first one
  573. alphabetically. */
  574. const char *chosen = arches[0];
  575. for (const char *arch : arches)
  576. {
  577. if (strcmp (arch, chosen) < 0)
  578. chosen = arch;
  579. }
  580. if (chosen == NULL)
  581. internal_error (__FILE__, __LINE__,
  582. _("initialize_current_architecture: No arch"));
  583. default_bfd_arch = bfd_scan_arch (chosen);
  584. if (default_bfd_arch == NULL)
  585. internal_error (__FILE__, __LINE__,
  586. _("initialize_current_architecture: Arch not found"));
  587. }
  588. gdbarch_info info;
  589. info.bfd_arch_info = default_bfd_arch;
  590. /* Take several guesses at a byte order. */
  591. if (default_byte_order == BFD_ENDIAN_UNKNOWN
  592. && default_bfd_vec != NULL)
  593. {
  594. /* Extract BFD's default vector's byte order. */
  595. switch (default_bfd_vec->byteorder)
  596. {
  597. case BFD_ENDIAN_BIG:
  598. default_byte_order = BFD_ENDIAN_BIG;
  599. break;
  600. case BFD_ENDIAN_LITTLE:
  601. default_byte_order = BFD_ENDIAN_LITTLE;
  602. break;
  603. default:
  604. break;
  605. }
  606. }
  607. if (default_byte_order == BFD_ENDIAN_UNKNOWN)
  608. {
  609. /* look for ``*el-*'' in the target name. */
  610. const char *chp;
  611. chp = strchr (target_name, '-');
  612. if (chp != NULL
  613. && chp - 2 >= target_name
  614. && startswith (chp - 2, "el"))
  615. default_byte_order = BFD_ENDIAN_LITTLE;
  616. }
  617. if (default_byte_order == BFD_ENDIAN_UNKNOWN)
  618. {
  619. /* Wire it to big-endian!!! */
  620. default_byte_order = BFD_ENDIAN_BIG;
  621. }
  622. info.byte_order = default_byte_order;
  623. info.byte_order_for_code = info.byte_order;
  624. if (! gdbarch_update_p (info))
  625. internal_error (__FILE__, __LINE__,
  626. _("initialize_current_architecture: Selection of "
  627. "initial architecture failed"));
  628. /* Create the ``set architecture'' command appending ``auto'' to the
  629. list of architectures. */
  630. {
  631. /* Append ``auto''. */
  632. set_architecture_string = "auto";
  633. arches.push_back (set_architecture_string);
  634. arches.push_back (nullptr);
  635. set_show_commands architecture_cmds
  636. = add_setshow_enum_cmd ("architecture", class_support,
  637. arches.data (), &set_architecture_string,
  638. _("Set architecture of target."),
  639. _("Show architecture of target."), NULL,
  640. set_architecture, show_architecture,
  641. &setlist, &showlist);
  642. add_alias_cmd ("processor", architecture_cmds.set, class_support, 1,
  643. &setlist);
  644. }
  645. }
  646. /* Similar to init, but this time fill in the blanks. Information is
  647. obtained from the global "set ..." options and explicitly
  648. initialized INFO fields. */
  649. void
  650. gdbarch_info_fill (struct gdbarch_info *info)
  651. {
  652. /* "(gdb) set architecture ...". */
  653. if (info->bfd_arch_info == NULL
  654. && target_architecture_user)
  655. info->bfd_arch_info = target_architecture_user;
  656. /* From the file. */
  657. if (info->bfd_arch_info == NULL
  658. && info->abfd != NULL
  659. && bfd_get_arch (info->abfd) != bfd_arch_unknown
  660. && bfd_get_arch (info->abfd) != bfd_arch_obscure)
  661. info->bfd_arch_info = bfd_get_arch_info (info->abfd);
  662. /* From the target. */
  663. if (info->target_desc != NULL)
  664. info->bfd_arch_info = choose_architecture_for_target
  665. (info->target_desc, info->bfd_arch_info);
  666. /* From the default. */
  667. if (info->bfd_arch_info == NULL)
  668. info->bfd_arch_info = default_bfd_arch;
  669. /* "(gdb) set byte-order ...". */
  670. if (info->byte_order == BFD_ENDIAN_UNKNOWN
  671. && target_byte_order_user != BFD_ENDIAN_UNKNOWN)
  672. info->byte_order = target_byte_order_user;
  673. /* From the INFO struct. */
  674. if (info->byte_order == BFD_ENDIAN_UNKNOWN
  675. && info->abfd != NULL)
  676. info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG
  677. : bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE
  678. : BFD_ENDIAN_UNKNOWN);
  679. /* From the default. */
  680. if (info->byte_order == BFD_ENDIAN_UNKNOWN)
  681. info->byte_order = default_byte_order;
  682. info->byte_order_for_code = info->byte_order;
  683. /* Wire the default to the last selected byte order. */
  684. default_byte_order = info->byte_order;
  685. /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */
  686. /* From the manual override, or from file. */
  687. if (info->osabi == GDB_OSABI_UNKNOWN)
  688. info->osabi = gdbarch_lookup_osabi (info->abfd);
  689. /* From the target. */
  690. if (info->osabi == GDB_OSABI_UNKNOWN && info->target_desc != NULL)
  691. info->osabi = tdesc_osabi (info->target_desc);
  692. /* From the configured default. */
  693. #ifdef GDB_OSABI_DEFAULT
  694. if (info->osabi == GDB_OSABI_UNKNOWN)
  695. info->osabi = GDB_OSABI_DEFAULT;
  696. #endif
  697. /* If we still don't know which osabi to pick, pick none. */
  698. if (info->osabi == GDB_OSABI_UNKNOWN)
  699. info->osabi = GDB_OSABI_NONE;
  700. /* Must have at least filled in the architecture. */
  701. gdb_assert (info->bfd_arch_info != NULL);
  702. }
  703. /* Return "current" architecture. If the target is running, this is
  704. the architecture of the selected frame. Otherwise, the "current"
  705. architecture defaults to the target architecture.
  706. This function should normally be called solely by the command
  707. interpreter routines to determine the architecture to execute a
  708. command in. */
  709. struct gdbarch *
  710. get_current_arch (void)
  711. {
  712. if (has_stack_frames ())
  713. return get_frame_arch (get_selected_frame (NULL));
  714. else
  715. return target_gdbarch ();
  716. }
  717. int
  718. default_has_shared_address_space (struct gdbarch *gdbarch)
  719. {
  720. /* Simply say no. In most unix-like targets each inferior/process
  721. has its own address space. */
  722. return 0;
  723. }
  724. int
  725. default_fast_tracepoint_valid_at (struct gdbarch *gdbarch, CORE_ADDR addr,
  726. std::string *msg)
  727. {
  728. /* We don't know if maybe the target has some way to do fast
  729. tracepoints that doesn't need gdbarch, so always say yes. */
  730. if (msg)
  731. msg->clear ();
  732. return 1;
  733. }
  734. const gdb_byte *
  735. default_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  736. int *lenptr)
  737. {
  738. int kind = gdbarch_breakpoint_kind_from_pc (gdbarch, pcptr);
  739. return gdbarch_sw_breakpoint_from_kind (gdbarch, kind, lenptr);
  740. }
  741. int
  742. default_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
  743. struct regcache *regcache,
  744. CORE_ADDR *pcptr)
  745. {
  746. return gdbarch_breakpoint_kind_from_pc (gdbarch, pcptr);
  747. }
  748. void
  749. default_gen_return_address (struct gdbarch *gdbarch,
  750. struct agent_expr *ax, struct axs_value *value,
  751. CORE_ADDR scope)
  752. {
  753. error (_("This architecture has no method to collect a return address."));
  754. }
  755. int
  756. default_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
  757. struct type *type)
  758. {
  759. /* Usually, the return value's address is stored the in the "first hidden"
  760. parameter if the return value should be passed by reference, as
  761. specified in ABI. */
  762. return !(language_pass_by_reference (type).trivially_copyable);
  763. }
  764. int default_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
  765. {
  766. return 0;
  767. }
  768. int default_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
  769. {
  770. return 0;
  771. }
  772. int default_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
  773. {
  774. return 0;
  775. }
  776. /* See arch-utils.h. */
  777. bool
  778. default_program_breakpoint_here_p (struct gdbarch *gdbarch,
  779. CORE_ADDR address)
  780. {
  781. int len;
  782. const gdb_byte *bpoint = gdbarch_breakpoint_from_pc (gdbarch, &address, &len);
  783. /* Software breakpoints unsupported? */
  784. if (bpoint == nullptr)
  785. return false;
  786. gdb_byte *target_mem = (gdb_byte *) alloca (len);
  787. /* Enable the automatic memory restoration from breakpoints while
  788. we read the memory. Otherwise we may find temporary breakpoints, ones
  789. inserted by GDB, and flag them as permanent breakpoints. */
  790. scoped_restore restore_memory
  791. = make_scoped_restore_show_memory_breakpoints (0);
  792. if (target_read_memory (address, target_mem, len) == 0)
  793. {
  794. /* Check if this is a breakpoint instruction for this architecture,
  795. including ones used by GDB. */
  796. if (memcmp (target_mem, bpoint, len) == 0)
  797. return true;
  798. }
  799. return false;
  800. }
  801. void
  802. default_skip_permanent_breakpoint (struct regcache *regcache)
  803. {
  804. struct gdbarch *gdbarch = regcache->arch ();
  805. CORE_ADDR current_pc = regcache_read_pc (regcache);
  806. int bp_len;
  807. gdbarch_breakpoint_from_pc (gdbarch, &current_pc, &bp_len);
  808. current_pc += bp_len;
  809. regcache_write_pc (regcache, current_pc);
  810. }
  811. CORE_ADDR
  812. default_infcall_mmap (CORE_ADDR size, unsigned prot)
  813. {
  814. error (_("This target does not support inferior memory allocation by mmap."));
  815. }
  816. void
  817. default_infcall_munmap (CORE_ADDR addr, CORE_ADDR size)
  818. {
  819. /* Memory reserved by inferior mmap is kept leaked. */
  820. }
  821. /* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
  822. created in inferior memory by GDB (normally it is set by ld.so). */
  823. std::string
  824. default_gcc_target_options (struct gdbarch *gdbarch)
  825. {
  826. return string_printf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
  827. (gdbarch_ptr_bit (gdbarch) == 64
  828. ? " -mcmodel=large" : ""));
  829. }
  830. /* gdbarch gnu_triplet_regexp method. */
  831. const char *
  832. default_gnu_triplet_regexp (struct gdbarch *gdbarch)
  833. {
  834. return gdbarch_bfd_arch_info (gdbarch)->arch_name;
  835. }
  836. /* Default method for gdbarch_addressable_memory_unit_size. The default is
  837. based on the bits_per_byte defined in the bfd library for the current
  838. architecture, this is usually 8-bits, and so this function will usually
  839. return 1 indicating 1 byte is 1 octet. */
  840. int
  841. default_addressable_memory_unit_size (struct gdbarch *gdbarch)
  842. {
  843. return gdbarch_bfd_arch_info (gdbarch)->bits_per_byte / 8;
  844. }
  845. void
  846. default_guess_tracepoint_registers (struct gdbarch *gdbarch,
  847. struct regcache *regcache,
  848. CORE_ADDR addr)
  849. {
  850. int pc_regno = gdbarch_pc_regnum (gdbarch);
  851. gdb_byte *regs;
  852. /* This guessing code below only works if the PC register isn't
  853. a pseudo-register. The value of a pseudo-register isn't stored
  854. in the (non-readonly) regcache -- instead it's recomputed
  855. (probably from some other cached raw register) whenever the
  856. register is read. In this case, a custom method implementation
  857. should be used by the architecture. */
  858. if (pc_regno < 0 || pc_regno >= gdbarch_num_regs (gdbarch))
  859. return;
  860. regs = (gdb_byte *) alloca (register_size (gdbarch, pc_regno));
  861. store_unsigned_integer (regs, register_size (gdbarch, pc_regno),
  862. gdbarch_byte_order (gdbarch), addr);
  863. regcache->raw_supply (pc_regno, regs);
  864. }
  865. int
  866. default_print_insn (bfd_vma memaddr, disassemble_info *info)
  867. {
  868. disassembler_ftype disassemble_fn;
  869. disassemble_fn = disassembler (info->arch, info->endian == BFD_ENDIAN_BIG,
  870. info->mach, current_program_space->exec_bfd ());
  871. gdb_assert (disassemble_fn != NULL);
  872. return (*disassemble_fn) (memaddr, info);
  873. }
  874. /* See arch-utils.h. */
  875. CORE_ADDR
  876. gdbarch_skip_prologue_noexcept (gdbarch *gdbarch, CORE_ADDR pc) noexcept
  877. {
  878. CORE_ADDR new_pc = pc;
  879. try
  880. {
  881. new_pc = gdbarch_skip_prologue (gdbarch, pc);
  882. }
  883. catch (const gdb_exception &ex)
  884. {}
  885. return new_pc;
  886. }
  887. /* See arch-utils.h. */
  888. bool
  889. default_in_indirect_branch_thunk (gdbarch *gdbarch, CORE_ADDR pc)
  890. {
  891. return false;
  892. }
  893. /* See arch-utils.h. */
  894. ULONGEST
  895. default_type_align (struct gdbarch *gdbarch, struct type *type)
  896. {
  897. return 0;
  898. }
  899. /* See arch-utils.h. */
  900. std::string
  901. default_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
  902. {
  903. return "";
  904. }
  905. /* See arch-utils.h. */
  906. void
  907. default_read_core_file_mappings
  908. (struct gdbarch *gdbarch,
  909. struct bfd *cbfd,
  910. read_core_file_mappings_pre_loop_ftype pre_loop_cb,
  911. read_core_file_mappings_loop_ftype loop_cb)
  912. {
  913. }
  914. /* Static function declarations */
  915. static void alloc_gdbarch_data (struct gdbarch *);
  916. /* Non-zero if we want to trace architecture code. */
  917. #ifndef GDBARCH_DEBUG
  918. #define GDBARCH_DEBUG 0
  919. #endif
  920. unsigned int gdbarch_debug = GDBARCH_DEBUG;
  921. static void
  922. show_gdbarch_debug (struct ui_file *file, int from_tty,
  923. struct cmd_list_element *c, const char *value)
  924. {
  925. gdb_printf (file, _("Architecture debugging is %s.\n"), value);
  926. }
  927. static const char *
  928. pformat (const struct floatformat **format)
  929. {
  930. if (format == NULL)
  931. return "(null)";
  932. else
  933. /* Just print out one of them - this is only for diagnostics. */
  934. return format[0]->name;
  935. }
  936. static const char *
  937. pstring (const char *string)
  938. {
  939. if (string == NULL)
  940. return "(null)";
  941. return string;
  942. }
  943. static const char *
  944. pstring_ptr (char **string)
  945. {
  946. if (string == NULL || *string == NULL)
  947. return "(null)";
  948. return *string;
  949. }
  950. /* Helper function to print a list of strings, represented as "const
  951. char *const *". The list is printed comma-separated. */
  952. static const char *
  953. pstring_list (const char *const *list)
  954. {
  955. static char ret[100];
  956. const char *const *p;
  957. size_t offset = 0;
  958. if (list == NULL)
  959. return "(null)";
  960. ret[0] = '\0';
  961. for (p = list; *p != NULL && offset < sizeof (ret); ++p)
  962. {
  963. size_t s = xsnprintf (ret + offset, sizeof (ret) - offset, "%s, ", *p);
  964. offset += 2 + s;
  965. }
  966. if (offset > 0)
  967. {
  968. gdb_assert (offset - 2 < sizeof (ret));
  969. ret[offset - 2] = '\0';
  970. }
  971. return ret;
  972. }
  973. #include "gdbarch.c"
  974. obstack *gdbarch_obstack (gdbarch *arch)
  975. {
  976. return arch->obstack;
  977. }
  978. /* See gdbarch.h. */
  979. char *
  980. gdbarch_obstack_strdup (struct gdbarch *arch, const char *string)
  981. {
  982. return obstack_strdup (arch->obstack, string);
  983. }
  984. /* Free a gdbarch struct. This should never happen in normal
  985. operation --- once you've created a gdbarch, you keep it around.
  986. However, if an architecture's init function encounters an error
  987. building the structure, it may need to clean up a partially
  988. constructed gdbarch. */
  989. void
  990. gdbarch_free (struct gdbarch *arch)
  991. {
  992. struct obstack *obstack;
  993. gdb_assert (arch != NULL);
  994. gdb_assert (!arch->initialized_p);
  995. obstack = arch->obstack;
  996. obstack_free (obstack, 0); /* Includes the ARCH. */
  997. xfree (obstack);
  998. }
  999. struct gdbarch_tdep *
  1000. gdbarch_tdep (struct gdbarch *gdbarch)
  1001. {
  1002. if (gdbarch_debug >= 2)
  1003. gdb_printf (gdb_stdlog, "gdbarch_tdep called\n");
  1004. return gdbarch->tdep;
  1005. }
  1006. /* Keep a registry of per-architecture data-pointers required by GDB
  1007. modules. */
  1008. struct gdbarch_data
  1009. {
  1010. unsigned index;
  1011. int init_p;
  1012. gdbarch_data_pre_init_ftype *pre_init;
  1013. gdbarch_data_post_init_ftype *post_init;
  1014. };
  1015. struct gdbarch_data_registration
  1016. {
  1017. struct gdbarch_data *data;
  1018. struct gdbarch_data_registration *next;
  1019. };
  1020. struct gdbarch_data_registry
  1021. {
  1022. unsigned nr;
  1023. struct gdbarch_data_registration *registrations;
  1024. };
  1025. static struct gdbarch_data_registry gdbarch_data_registry =
  1026. {
  1027. 0, NULL,
  1028. };
  1029. static struct gdbarch_data *
  1030. gdbarch_data_register (gdbarch_data_pre_init_ftype *pre_init,
  1031. gdbarch_data_post_init_ftype *post_init)
  1032. {
  1033. struct gdbarch_data_registration **curr;
  1034. /* Append the new registration. */
  1035. for (curr = &gdbarch_data_registry.registrations;
  1036. (*curr) != NULL;
  1037. curr = &(*curr)->next);
  1038. (*curr) = XNEW (struct gdbarch_data_registration);
  1039. (*curr)->next = NULL;
  1040. (*curr)->data = XNEW (struct gdbarch_data);
  1041. (*curr)->data->index = gdbarch_data_registry.nr++;
  1042. (*curr)->data->pre_init = pre_init;
  1043. (*curr)->data->post_init = post_init;
  1044. (*curr)->data->init_p = 1;
  1045. return (*curr)->data;
  1046. }
  1047. struct gdbarch_data *
  1048. gdbarch_data_register_pre_init (gdbarch_data_pre_init_ftype *pre_init)
  1049. {
  1050. return gdbarch_data_register (pre_init, NULL);
  1051. }
  1052. struct gdbarch_data *
  1053. gdbarch_data_register_post_init (gdbarch_data_post_init_ftype *post_init)
  1054. {
  1055. return gdbarch_data_register (NULL, post_init);
  1056. }
  1057. /* Create/delete the gdbarch data vector. */
  1058. static void
  1059. alloc_gdbarch_data (struct gdbarch *gdbarch)
  1060. {
  1061. gdb_assert (gdbarch->data == NULL);
  1062. gdbarch->nr_data = gdbarch_data_registry.nr;
  1063. gdbarch->data = GDBARCH_OBSTACK_CALLOC (gdbarch, gdbarch->nr_data, void *);
  1064. }
  1065. /* Return the current value of the specified per-architecture
  1066. data-pointer. */
  1067. void *
  1068. gdbarch_data (struct gdbarch *gdbarch, struct gdbarch_data *data)
  1069. {
  1070. gdb_assert (data->index < gdbarch->nr_data);
  1071. if (gdbarch->data[data->index] == NULL)
  1072. {
  1073. /* The data-pointer isn't initialized, call init() to get a
  1074. value. */
  1075. if (data->pre_init != NULL)
  1076. /* Mid architecture creation: pass just the obstack, and not
  1077. the entire architecture, as that way it isn't possible for
  1078. pre-init code to refer to undefined architecture
  1079. fields. */
  1080. gdbarch->data[data->index] = data->pre_init (gdbarch->obstack);
  1081. else if (gdbarch->initialized_p
  1082. && data->post_init != NULL)
  1083. /* Post architecture creation: pass the entire architecture
  1084. (as all fields are valid), but be careful to also detect
  1085. recursive references. */
  1086. {
  1087. gdb_assert (data->init_p);
  1088. data->init_p = 0;
  1089. gdbarch->data[data->index] = data->post_init (gdbarch);
  1090. data->init_p = 1;
  1091. }
  1092. else
  1093. internal_error (__FILE__, __LINE__,
  1094. _("gdbarch post-init data field can only be used "
  1095. "after gdbarch is fully initialised"));
  1096. gdb_assert (gdbarch->data[data->index] != NULL);
  1097. }
  1098. return gdbarch->data[data->index];
  1099. }
  1100. /* Keep a registry of the architectures known by GDB. */
  1101. struct gdbarch_registration
  1102. {
  1103. enum bfd_architecture bfd_architecture;
  1104. gdbarch_init_ftype *init;
  1105. gdbarch_dump_tdep_ftype *dump_tdep;
  1106. struct gdbarch_list *arches;
  1107. struct gdbarch_registration *next;
  1108. };
  1109. static struct gdbarch_registration *gdbarch_registry = NULL;
  1110. std::vector<const char *>
  1111. gdbarch_printable_names ()
  1112. {
  1113. /* Accumulate a list of names based on the registed list of
  1114. architectures. */
  1115. std::vector<const char *> arches;
  1116. for (gdbarch_registration *rego = gdbarch_registry;
  1117. rego != nullptr;
  1118. rego = rego->next)
  1119. {
  1120. const struct bfd_arch_info *ap
  1121. = bfd_lookup_arch (rego->bfd_architecture, 0);
  1122. if (ap == nullptr)
  1123. internal_error (__FILE__, __LINE__,
  1124. _("gdbarch_architecture_names: multi-arch unknown"));
  1125. do
  1126. {
  1127. arches.push_back (ap->printable_name);
  1128. ap = ap->next;
  1129. }
  1130. while (ap != NULL);
  1131. }
  1132. return arches;
  1133. }
  1134. void
  1135. gdbarch_register (enum bfd_architecture bfd_architecture,
  1136. gdbarch_init_ftype *init,
  1137. gdbarch_dump_tdep_ftype *dump_tdep)
  1138. {
  1139. struct gdbarch_registration **curr;
  1140. const struct bfd_arch_info *bfd_arch_info;
  1141. /* Check that BFD recognizes this architecture */
  1142. bfd_arch_info = bfd_lookup_arch (bfd_architecture, 0);
  1143. if (bfd_arch_info == NULL)
  1144. {
  1145. internal_error (__FILE__, __LINE__,
  1146. _("gdbarch: Attempt to register "
  1147. "unknown architecture (%d)"),
  1148. bfd_architecture);
  1149. }
  1150. /* Check that we haven't seen this architecture before. */
  1151. for (curr = &gdbarch_registry;
  1152. (*curr) != NULL;
  1153. curr = &(*curr)->next)
  1154. {
  1155. if (bfd_architecture == (*curr)->bfd_architecture)
  1156. internal_error (__FILE__, __LINE__,
  1157. _("gdbarch: Duplicate registration "
  1158. "of architecture (%s)"),
  1159. bfd_arch_info->printable_name);
  1160. }
  1161. /* log it */
  1162. if (gdbarch_debug)
  1163. gdb_printf (gdb_stdlog, "register_gdbarch_init (%s, %s)\n",
  1164. bfd_arch_info->printable_name,
  1165. host_address_to_string (init));
  1166. /* Append it */
  1167. (*curr) = XNEW (struct gdbarch_registration);
  1168. (*curr)->bfd_architecture = bfd_architecture;
  1169. (*curr)->init = init;
  1170. (*curr)->dump_tdep = dump_tdep;
  1171. (*curr)->arches = NULL;
  1172. (*curr)->next = NULL;
  1173. }
  1174. void
  1175. register_gdbarch_init (enum bfd_architecture bfd_architecture,
  1176. gdbarch_init_ftype *init)
  1177. {
  1178. gdbarch_register (bfd_architecture, init, NULL);
  1179. }
  1180. /* Look for an architecture using gdbarch_info. */
  1181. struct gdbarch_list *
  1182. gdbarch_list_lookup_by_info (struct gdbarch_list *arches,
  1183. const struct gdbarch_info *info)
  1184. {
  1185. for (; arches != NULL; arches = arches->next)
  1186. {
  1187. if (info->bfd_arch_info != arches->gdbarch->bfd_arch_info)
  1188. continue;
  1189. if (info->byte_order != arches->gdbarch->byte_order)
  1190. continue;
  1191. if (info->osabi != arches->gdbarch->osabi)
  1192. continue;
  1193. if (info->target_desc != arches->gdbarch->target_desc)
  1194. continue;
  1195. return arches;
  1196. }
  1197. return NULL;
  1198. }
  1199. /* Find an architecture that matches the specified INFO. Create a new
  1200. architecture if needed. Return that new architecture. */
  1201. struct gdbarch *
  1202. gdbarch_find_by_info (struct gdbarch_info info)
  1203. {
  1204. struct gdbarch *new_gdbarch;
  1205. struct gdbarch_registration *rego;
  1206. /* Fill in missing parts of the INFO struct using a number of
  1207. sources: "set ..."; INFOabfd supplied; and the global
  1208. defaults. */
  1209. gdbarch_info_fill (&info);
  1210. /* Must have found some sort of architecture. */
  1211. gdb_assert (info.bfd_arch_info != NULL);
  1212. if (gdbarch_debug)
  1213. {
  1214. gdb_printf (gdb_stdlog,
  1215. "gdbarch_find_by_info: info.bfd_arch_info %s\n",
  1216. (info.bfd_arch_info != NULL
  1217. ? info.bfd_arch_info->printable_name
  1218. : "(null)"));
  1219. gdb_printf (gdb_stdlog,
  1220. "gdbarch_find_by_info: info.byte_order %d (%s)\n",
  1221. info.byte_order,
  1222. (info.byte_order == BFD_ENDIAN_BIG ? "big"
  1223. : info.byte_order == BFD_ENDIAN_LITTLE ? "little"
  1224. : "default"));
  1225. gdb_printf (gdb_stdlog,
  1226. "gdbarch_find_by_info: info.osabi %d (%s)\n",
  1227. info.osabi, gdbarch_osabi_name (info.osabi));
  1228. gdb_printf (gdb_stdlog,
  1229. "gdbarch_find_by_info: info.abfd %s\n",
  1230. host_address_to_string (info.abfd));
  1231. }
  1232. /* Find the tdep code that knows about this architecture. */
  1233. for (rego = gdbarch_registry;
  1234. rego != NULL;
  1235. rego = rego->next)
  1236. if (rego->bfd_architecture == info.bfd_arch_info->arch)
  1237. break;
  1238. if (rego == NULL)
  1239. {
  1240. if (gdbarch_debug)
  1241. gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
  1242. "No matching architecture\n");
  1243. return 0;
  1244. }
  1245. /* Ask the tdep code for an architecture that matches "info". */
  1246. new_gdbarch = rego->init (info, rego->arches);
  1247. /* Did the tdep code like it? No. Reject the change and revert to
  1248. the old architecture. */
  1249. if (new_gdbarch == NULL)
  1250. {
  1251. if (gdbarch_debug)
  1252. gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
  1253. "Target rejected architecture\n");
  1254. return NULL;
  1255. }
  1256. /* Is this a pre-existing architecture (as determined by already
  1257. being initialized)? Move it to the front of the architecture
  1258. list (keeping the list sorted Most Recently Used). */
  1259. if (new_gdbarch->initialized_p)
  1260. {
  1261. struct gdbarch_list **list;
  1262. struct gdbarch_list *self;
  1263. if (gdbarch_debug)
  1264. gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
  1265. "Previous architecture %s (%s) selected\n",
  1266. host_address_to_string (new_gdbarch),
  1267. new_gdbarch->bfd_arch_info->printable_name);
  1268. /* Find the existing arch in the list. */
  1269. for (list = &rego->arches;
  1270. (*list) != NULL && (*list)->gdbarch != new_gdbarch;
  1271. list = &(*list)->next);
  1272. /* It had better be in the list of architectures. */
  1273. gdb_assert ((*list) != NULL && (*list)->gdbarch == new_gdbarch);
  1274. /* Unlink SELF. */
  1275. self = (*list);
  1276. (*list) = self->next;
  1277. /* Insert SELF at the front. */
  1278. self->next = rego->arches;
  1279. rego->arches = self;
  1280. /* Return it. */
  1281. return new_gdbarch;
  1282. }
  1283. /* It's a new architecture. */
  1284. if (gdbarch_debug)
  1285. gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
  1286. "New architecture %s (%s) selected\n",
  1287. host_address_to_string (new_gdbarch),
  1288. new_gdbarch->bfd_arch_info->printable_name);
  1289. /* Insert the new architecture into the front of the architecture
  1290. list (keep the list sorted Most Recently Used). */
  1291. {
  1292. struct gdbarch_list *self = XNEW (struct gdbarch_list);
  1293. self->next = rego->arches;
  1294. self->gdbarch = new_gdbarch;
  1295. rego->arches = self;
  1296. }
  1297. /* Check that the newly installed architecture is valid. Plug in
  1298. any post init values. */
  1299. new_gdbarch->dump_tdep = rego->dump_tdep;
  1300. verify_gdbarch (new_gdbarch);
  1301. new_gdbarch->initialized_p = 1;
  1302. if (gdbarch_debug)
  1303. gdbarch_dump (new_gdbarch, gdb_stdlog);
  1304. return new_gdbarch;
  1305. }
  1306. /* Make the specified architecture current. */
  1307. void
  1308. set_target_gdbarch (struct gdbarch *new_gdbarch)
  1309. {
  1310. gdb_assert (new_gdbarch != NULL);
  1311. gdb_assert (new_gdbarch->initialized_p);
  1312. current_inferior ()->gdbarch = new_gdbarch;
  1313. gdb::observers::architecture_changed.notify (new_gdbarch);
  1314. registers_changed ();
  1315. }
  1316. /* Return the current inferior's arch. */
  1317. struct gdbarch *
  1318. target_gdbarch (void)
  1319. {
  1320. return current_inferior ()->gdbarch;
  1321. }
  1322. void _initialize_gdbarch_utils ();
  1323. void
  1324. _initialize_gdbarch_utils ()
  1325. {
  1326. add_setshow_enum_cmd ("endian", class_support,
  1327. endian_enum, &set_endian_string,
  1328. _("Set endianness of target."),
  1329. _("Show endianness of target."),
  1330. NULL, set_endian, show_endian,
  1331. &setlist, &showlist);
  1332. add_setshow_zuinteger_cmd ("arch", class_maintenance, &gdbarch_debug, _("\
  1333. Set architecture debugging."), _("\
  1334. Show architecture debugging."), _("\
  1335. When non-zero, architecture debugging is enabled."),
  1336. NULL,
  1337. show_gdbarch_debug,
  1338. &setdebuglist, &showdebuglist);
  1339. }