cpu-riscv.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* BFD backend for RISC-V
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. Contributed by Andrew Waterman (andrew@sifive.com).
  4. Based on MIPS target.
  5. This file is part of BFD, the Binary File Descriptor library.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING3. If not,
  16. see <http://www.gnu.org/licenses/>. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "cpu-riscv.h"
  21. static const bfd_arch_info_type *
  22. riscv_compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b)
  23. {
  24. if (a->arch != b->arch)
  25. return NULL;
  26. /* Machine compatibility is checked in
  27. _bfd_riscv_elf_merge_private_bfd_data. */
  28. return a;
  29. }
  30. /* Return TRUE if STRING matches the architecture described by INFO. */
  31. static bool
  32. riscv_scan (const struct bfd_arch_info *info, const char *string)
  33. {
  34. if (bfd_default_scan (info, string))
  35. return true;
  36. /* The incoming STRING might take the form of riscv:rvXXzzz, where XX is
  37. 32 or 64, and zzz are one or more extension characters. As we
  38. currently only have 3 architectures defined, 'riscv', 'riscv:rv32',
  39. and 'riscv:rv64', we would like to ignore the zzz for the purpose of
  40. matching here.
  41. However, we don't want the default 'riscv' to match over a more
  42. specific 'riscv:rv32' or 'riscv:rv64', so in the case of the default
  43. architecture (with the shorter 'riscv' name) we don't allow any
  44. special matching, but for the 'riscv:rvXX' cases, we allow a match
  45. with any additional trailing characters being ignored. */
  46. if (!info->the_default
  47. && strncasecmp (string, info->printable_name,
  48. strlen (info->printable_name)) == 0)
  49. return true;
  50. return false;
  51. }
  52. #define N(BITS, NUMBER, PRINT, DEFAULT, NEXT) \
  53. { \
  54. BITS, /* Bits in a word. */ \
  55. BITS, /* Bits in an address. */ \
  56. 8, /* Bits in a byte. */ \
  57. bfd_arch_riscv, \
  58. NUMBER, \
  59. "riscv", \
  60. PRINT, \
  61. 3, \
  62. DEFAULT, \
  63. riscv_compatible, \
  64. riscv_scan, \
  65. bfd_arch_default_fill, \
  66. NEXT, \
  67. 0 /* Maximum offset of a reloc from the start of an insn. */\
  68. }
  69. /* This enum must be kept in the same order as arch_info_struct. */
  70. enum
  71. {
  72. I_riscv64,
  73. I_riscv32
  74. };
  75. #define NN(index) (&arch_info_struct[(index) + 1])
  76. /* This array must be kept in the same order as the anonymous enum above,
  77. and each entry except the last should end with NN (my enum value). */
  78. static const bfd_arch_info_type arch_info_struct[] =
  79. {
  80. N (64, bfd_mach_riscv64, "riscv:rv64", false, NN (I_riscv64)),
  81. N (32, bfd_mach_riscv32, "riscv:rv32", false, NULL)
  82. };
  83. /* The default architecture is riscv:rv64. */
  84. const bfd_arch_info_type bfd_riscv_arch =
  85. N (64, 0, "riscv", true, &arch_info_struct[0]);
  86. /* List for all supported ISA spec versions. */
  87. const struct riscv_spec riscv_isa_specs[] =
  88. {
  89. {"2.2", ISA_SPEC_CLASS_2P2},
  90. {"20190608", ISA_SPEC_CLASS_20190608},
  91. {"20191213", ISA_SPEC_CLASS_20191213},
  92. };
  93. /* List for all supported privileged spec versions. */
  94. const struct riscv_spec riscv_priv_specs[] =
  95. {
  96. {"1.9.1", PRIV_SPEC_CLASS_1P9P1},
  97. {"1.10", PRIV_SPEC_CLASS_1P10},
  98. {"1.11", PRIV_SPEC_CLASS_1P11},
  99. {"1.12", PRIV_SPEC_CLASS_1P12},
  100. };
  101. /* Get the corresponding CSR version class by giving privilege
  102. version numbers. It is usually used to convert the priv
  103. attribute numbers into the corresponding class. */
  104. void
  105. riscv_get_priv_spec_class_from_numbers (unsigned int major,
  106. unsigned int minor,
  107. unsigned int revision,
  108. enum riscv_spec_class *class)
  109. {
  110. enum riscv_spec_class class_t = *class;
  111. char buf[36];
  112. if (revision != 0)
  113. snprintf (buf, sizeof (buf), "%u.%u.%u", major, minor, revision);
  114. else
  115. snprintf (buf, sizeof (buf), "%u.%u", major, minor);
  116. RISCV_GET_PRIV_SPEC_CLASS (buf, class_t);
  117. *class = class_t;
  118. }
  119. /* Define mapping symbols for riscv. */
  120. bool
  121. riscv_elf_is_mapping_symbols (const char *name)
  122. {
  123. return (!strncmp (name, "$d", 2)
  124. || !strncmp (name, "$x", 2));
  125. }