22067ab29bec74c1e1f5660370163be9a739273a
[deliverable/binutils-gdb.git] / bfd / cpu-riscv.c
1 /* BFD backend for RISC-V
2 Copyright (C) 2011-2020 Free Software Foundation, Inc.
3
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on MIPS target.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not,
21 see <http://www.gnu.org/licenses/>. */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26
27 /* This routine is provided two arch_infos and returns an arch_info
28 that is compatible with both, or NULL if none exists. */
29
30 static const bfd_arch_info_type *
31 riscv_compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b)
32 {
33 if (a->arch != b->arch)
34 return NULL;
35
36 /* Machine compatibility is checked in
37 _bfd_riscv_elf_merge_private_bfd_data. */
38
39 return a;
40 }
41
42 /* Return TRUE if STRING matches the architecture described by INFO. */
43
44 static bfd_boolean
45 riscv_scan (const struct bfd_arch_info *info, const char *string)
46 {
47 if (bfd_default_scan (info, string))
48 return TRUE;
49
50 /* The incoming STRING might take the form of riscv:rvXXzzz, where XX is
51 32 or 64, and zzz are one or more extension characters. As we
52 currently only have 3 architectures defined, 'riscv', 'riscv:rv32',
53 and 'riscv:rv64', we would like to ignore the zzz for the purpose of
54 matching here.
55
56 However, we don't want the default 'riscv' to match over a more
57 specific 'riscv:rv32' or 'riscv:rv64', so in the case of the default
58 architecture (with the shorter 'riscv' name) we don't allow any
59 special matching, but for the 'riscv:rvXX' cases, we allow a match
60 with any additional trailing characters being ignored. */
61 if (!info->the_default
62 && strncasecmp (string, info->printable_name,
63 strlen (info->printable_name)) == 0)
64 return TRUE;
65
66 return FALSE;
67 }
68
69 #define N(BITS, NUMBER, PRINT, DEFAULT, NEXT) \
70 { \
71 BITS, /* Bits in a word. */ \
72 BITS, /* Bits in an address. */ \
73 8, /* Bits in a byte. */ \
74 bfd_arch_riscv, \
75 NUMBER, \
76 "riscv", \
77 PRINT, \
78 3, \
79 DEFAULT, \
80 riscv_compatible, \
81 riscv_scan, \
82 bfd_arch_default_fill, \
83 NEXT, \
84 0 /* Maximum offset of a reloc from the start of an insn. */\
85 }
86
87 /* This enum must be kept in the same order as arch_info_struct. */
88 enum
89 {
90 I_riscv64,
91 I_riscv32
92 };
93
94 #define NN(index) (&arch_info_struct[(index) + 1])
95
96 /* This array must be kept in the same order as the anonymous enum above,
97 and each entry except the last should end with NN (my enum value). */
98 static const bfd_arch_info_type arch_info_struct[] =
99 {
100 N (64, bfd_mach_riscv64, "riscv:rv64", FALSE, NN (I_riscv64)),
101 N (32, bfd_mach_riscv32, "riscv:rv32", FALSE, NULL)
102 };
103
104 /* The default architecture is riscv:rv64. */
105
106 const bfd_arch_info_type bfd_riscv_arch =
107 N (64, 0, "riscv", TRUE, &arch_info_struct[0]);
This page took 0.030247 seconds and 3 git commands to generate.