x86: permit parenthesized expressions again as addressing scale factor
[binutils-gdb.git] / gas / config / tc-riscv.c
1 /* tc-riscv.c -- RISC-V assembler
2 Copyright (C) 2011-2021 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 GAS.
8
9 GAS 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, or (at your option)
12 any later version.
13
14 GAS 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 "as.h"
24 #include "config.h"
25 #include "subsegs.h"
26 #include "safe-ctype.h"
27
28 #include "itbl-ops.h"
29 #include "dwarf2dbg.h"
30 #include "dw2gencfi.h"
31
32 #include "bfd/cpu-riscv.h"
33 #include "bfd/elfxx-riscv.h"
34 #include "elf/riscv.h"
35 #include "opcode/riscv.h"
36
37 #include <stdint.h>
38
39 /* Information about an instruction, including its format, operands
40 and fixups. */
41 struct riscv_cl_insn
42 {
43 /* The opcode's entry in riscv_opcodes. */
44 const struct riscv_opcode *insn_mo;
45
46 /* The encoded instruction bits. */
47 insn_t insn_opcode;
48
49 /* The frag that contains the instruction. */
50 struct frag *frag;
51
52 /* The offset into FRAG of the first instruction byte. */
53 long where;
54
55 /* The relocs associated with the instruction, if any. */
56 fixS *fixp;
57 };
58
59 /* All RISC-V CSR belong to one of these classes. */
60 enum riscv_csr_class
61 {
62 CSR_CLASS_NONE,
63
64 CSR_CLASS_I,
65 CSR_CLASS_I_32, /* rv32 only */
66 CSR_CLASS_F, /* f-ext only */
67 CSR_CLASS_DEBUG /* debug CSR */
68 };
69
70 /* This structure holds all restricted conditions for a CSR. */
71 struct riscv_csr_extra
72 {
73 /* Class to which this CSR belongs. Used to decide whether or
74 not this CSR is legal in the current -march context. */
75 enum riscv_csr_class csr_class;
76
77 /* CSR may have differnet numbers in the previous priv spec. */
78 unsigned address;
79
80 /* Record the CSR is defined/valid in which versions. */
81 enum riscv_spec_class define_version;
82
83 /* Record the CSR is aborted/invalid from which versions. If it isn't
84 aborted in the current version, then it should be CSR_CLASS_VDRAFT. */
85 enum riscv_spec_class abort_version;
86
87 /* The CSR may have more than one setting. */
88 struct riscv_csr_extra *next;
89 };
90
91 /* All standard/Z* extensions defined in all supported ISA spec. */
92 struct riscv_ext_version
93 {
94 const char *name;
95 enum riscv_spec_class isa_spec_class;
96 int major_version;
97 int minor_version;
98 };
99
100 static const struct riscv_ext_version ext_version_table[] =
101 {
102 {"e", ISA_SPEC_CLASS_20191213, 1, 9},
103 {"e", ISA_SPEC_CLASS_20190608, 1, 9},
104 {"e", ISA_SPEC_CLASS_2P2, 1, 9},
105
106 {"i", ISA_SPEC_CLASS_20191213, 2, 1},
107 {"i", ISA_SPEC_CLASS_20190608, 2, 1},
108 {"i", ISA_SPEC_CLASS_2P2, 2, 0},
109
110 {"m", ISA_SPEC_CLASS_20191213, 2, 0},
111 {"m", ISA_SPEC_CLASS_20190608, 2, 0},
112 {"m", ISA_SPEC_CLASS_2P2, 2, 0},
113
114 {"a", ISA_SPEC_CLASS_20191213, 2, 1},
115 {"a", ISA_SPEC_CLASS_20190608, 2, 0},
116 {"a", ISA_SPEC_CLASS_2P2, 2, 0},
117
118 {"f", ISA_SPEC_CLASS_20191213, 2, 2},
119 {"f", ISA_SPEC_CLASS_20190608, 2, 2},
120 {"f", ISA_SPEC_CLASS_2P2, 2, 0},
121
122 {"d", ISA_SPEC_CLASS_20191213, 2, 2},
123 {"d", ISA_SPEC_CLASS_20190608, 2, 2},
124 {"d", ISA_SPEC_CLASS_2P2, 2, 0},
125
126 {"q", ISA_SPEC_CLASS_20191213, 2, 2},
127 {"q", ISA_SPEC_CLASS_20190608, 2, 2},
128 {"q", ISA_SPEC_CLASS_2P2, 2, 0},
129
130 {"c", ISA_SPEC_CLASS_20191213, 2, 0},
131 {"c", ISA_SPEC_CLASS_20190608, 2, 0},
132 {"c", ISA_SPEC_CLASS_2P2, 2, 0},
133
134 {"zicsr", ISA_SPEC_CLASS_20191213, 2, 0},
135 {"zicsr", ISA_SPEC_CLASS_20190608, 2, 0},
136
137 {"zifencei", ISA_SPEC_CLASS_20191213, 2, 0},
138 {"zifencei", ISA_SPEC_CLASS_20190608, 2, 0},
139
140 {"zihintpause", ISA_SPEC_CLASS_DRAFT, 1, 0},
141
142 {"zbb", ISA_SPEC_CLASS_DRAFT, 0, 93},
143 {"zba", ISA_SPEC_CLASS_DRAFT, 0, 93},
144 {"zbc", ISA_SPEC_CLASS_DRAFT, 0, 93},
145
146 /* Terminate the list. */
147 {NULL, 0, 0, 0}
148 };
149
150 #ifndef DEFAULT_ARCH
151 #define DEFAULT_ARCH "riscv64"
152 #endif
153
154 #ifndef DEFAULT_RISCV_ATTR
155 #define DEFAULT_RISCV_ATTR 0
156 #endif
157
158 /* Let riscv_after_parse_args set the default value according to xlen. */
159 #ifndef DEFAULT_RISCV_ARCH_WITH_EXT
160 #define DEFAULT_RISCV_ARCH_WITH_EXT NULL
161 #endif
162
163 /* Need to sync the version with RISC-V compiler. */
164 #ifndef DEFAULT_RISCV_ISA_SPEC
165 #define DEFAULT_RISCV_ISA_SPEC "2.2"
166 #endif
167
168 #ifndef DEFAULT_RISCV_PRIV_SPEC
169 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
170 #endif
171
172 static const char default_arch[] = DEFAULT_ARCH;
173 static const char *default_arch_with_ext = DEFAULT_RISCV_ARCH_WITH_EXT;
174 static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_NONE;
175 static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE;
176
177 static unsigned xlen = 0; /* The width of an x-register. */
178 static unsigned abi_xlen = 0; /* The width of a pointer in the ABI. */
179 static bool rve_abi = false;
180 enum float_abi
181 {
182 FLOAT_ABI_DEFAULT = -1,
183 FLOAT_ABI_SOFT,
184 FLOAT_ABI_SINGLE,
185 FLOAT_ABI_DOUBLE,
186 FLOAT_ABI_QUAD
187 };
188 static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
189
190 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
191 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
192
193 static unsigned elf_flags = 0;
194
195 /* Set the default_isa_spec. Return 0 if the spec isn't supported.
196 Otherwise, return 1. */
197
198 static int
199 riscv_set_default_isa_spec (const char *s)
200 {
201 enum riscv_spec_class class = ISA_SPEC_CLASS_NONE;
202 RISCV_GET_ISA_SPEC_CLASS (s, class);
203 if (class == ISA_SPEC_CLASS_NONE)
204 {
205 as_bad ("unknown default ISA spec `%s' set by "
206 "-misa-spec or --with-isa-spec", s);
207 return 0;
208 }
209 else
210 default_isa_spec = class;
211 return 1;
212 }
213
214 /* Set the default_priv_spec. Find the privileged elf attributes when
215 the input string is NULL. Return 0 if the spec isn't supported.
216 Otherwise, return 1. */
217
218 static int
219 riscv_set_default_priv_spec (const char *s)
220 {
221 enum riscv_spec_class class = PRIV_SPEC_CLASS_NONE;
222 unsigned major, minor, revision;
223 obj_attribute *attr;
224
225 RISCV_GET_PRIV_SPEC_CLASS (s, class);
226 if (class != PRIV_SPEC_CLASS_NONE)
227 {
228 default_priv_spec = class;
229 return 1;
230 }
231
232 if (s != NULL)
233 {
234 as_bad (_("unknown default privileged spec `%s' set by "
235 "-mpriv-spec or --with-priv-spec"), s);
236 return 0;
237 }
238
239 /* Set the default_priv_spec by the privileged elf attributes. */
240 attr = elf_known_obj_attributes_proc (stdoutput);
241 major = (unsigned) attr[Tag_RISCV_priv_spec].i;
242 minor = (unsigned) attr[Tag_RISCV_priv_spec_minor].i;
243 revision = (unsigned) attr[Tag_RISCV_priv_spec_revision].i;
244 /* Version 0.0.0 is the default value and meningless. */
245 if (major == 0 && minor == 0 && revision == 0)
246 return 1;
247
248 riscv_get_priv_spec_class_from_numbers (major, minor, revision, &class);
249 if (class != PRIV_SPEC_CLASS_NONE)
250 {
251 default_priv_spec = class;
252 return 1;
253 }
254
255 /* Still can not find the privileged spec class. */
256 as_bad (_("unknown default privileged spec `%d.%d.%d' set by "
257 "privileged elf attributes"), major, minor, revision);
258 return 0;
259 }
260
261 /* This is the set of options which the .option pseudo-op may modify. */
262 struct riscv_set_options
263 {
264 int pic; /* Generate position-independent code. */
265 int rvc; /* Generate RVC code. */
266 int rve; /* Generate RVE code. */
267 int relax; /* Emit relocs the linker is allowed to relax. */
268 int arch_attr; /* Emit architecture and privileged elf attributes. */
269 int csr_check; /* Enable the CSR checking. */
270 };
271
272 static struct riscv_set_options riscv_opts =
273 {
274 0, /* pic */
275 0, /* rvc */
276 0, /* rve */
277 1, /* relax */
278 DEFAULT_RISCV_ATTR, /* arch_attr */
279 0, /* csr_check */
280 };
281
282 static void
283 riscv_set_rvc (bool rvc_value)
284 {
285 if (rvc_value)
286 elf_flags |= EF_RISCV_RVC;
287
288 riscv_opts.rvc = rvc_value;
289 }
290
291 static void
292 riscv_set_rve (bool rve_value)
293 {
294 riscv_opts.rve = rve_value;
295 }
296
297 static riscv_subset_list_t riscv_subsets;
298
299 static bool
300 riscv_subset_supports (const char *feature)
301 {
302 struct riscv_subset_t *subset;
303
304 if (riscv_opts.rvc && (strcasecmp (feature, "c") == 0))
305 return true;
306
307 return riscv_lookup_subset (&riscv_subsets, feature, &subset);
308 }
309
310 static bool
311 riscv_multi_subset_supports (enum riscv_insn_class insn_class)
312 {
313 switch (insn_class)
314 {
315 case INSN_CLASS_I: return riscv_subset_supports ("i");
316 case INSN_CLASS_C: return riscv_subset_supports ("c");
317 case INSN_CLASS_A: return riscv_subset_supports ("a");
318 case INSN_CLASS_M: return riscv_subset_supports ("m");
319 case INSN_CLASS_F: return riscv_subset_supports ("f");
320 case INSN_CLASS_D: return riscv_subset_supports ("d");
321 case INSN_CLASS_Q: return riscv_subset_supports ("q");
322
323 case INSN_CLASS_F_AND_C:
324 return (riscv_subset_supports ("f")
325 && riscv_subset_supports ("c"));
326 case INSN_CLASS_D_AND_C:
327 return (riscv_subset_supports ("d")
328 && riscv_subset_supports ("c"));
329
330 case INSN_CLASS_ZICSR:
331 return riscv_subset_supports ("zicsr");
332 case INSN_CLASS_ZIFENCEI:
333 return riscv_subset_supports ("zifencei");
334 case INSN_CLASS_ZIHINTPAUSE:
335 return riscv_subset_supports ("zihintpause");
336
337 case INSN_CLASS_ZBB:
338 return riscv_subset_supports ("zbb");
339
340 case INSN_CLASS_ZBA:
341 return riscv_subset_supports ("zba");
342
343 case INSN_CLASS_ZBC:
344 return riscv_subset_supports ("zbc");
345
346 default:
347 as_fatal ("internal: unreachable");
348 return false;
349 }
350 }
351
352 /* Handle of the extension with version hash table. */
353 static htab_t ext_version_hash = NULL;
354
355 static htab_t
356 init_ext_version_hash (void)
357 {
358 const struct riscv_ext_version *table = ext_version_table;
359 htab_t hash = str_htab_create ();
360 int i = 0;
361
362 while (table[i].name)
363 {
364 const char *name = table[i].name;
365 if (str_hash_insert (hash, name, &table[i], 0) != NULL)
366 as_fatal (_("internal: duplicate %s"), name);
367
368 i++;
369 while (table[i].name
370 && strcmp (table[i].name, name) == 0)
371 i++;
372 }
373
374 return hash;
375 }
376
377 static void
378 riscv_get_default_ext_version (const char *name,
379 int *major_version,
380 int *minor_version)
381 {
382 struct riscv_ext_version *ext;
383
384 if (name == NULL || default_isa_spec == ISA_SPEC_CLASS_NONE)
385 return;
386
387 ext = (struct riscv_ext_version *) str_hash_find (ext_version_hash, name);
388 while (ext
389 && ext->name
390 && strcmp (ext->name, name) == 0)
391 {
392 if (ext->isa_spec_class == ISA_SPEC_CLASS_DRAFT
393 || ext->isa_spec_class == default_isa_spec)
394 {
395 *major_version = ext->major_version;
396 *minor_version = ext->minor_version;
397 return;
398 }
399 ext++;
400 }
401 }
402
403 /* Set which ISA and extensions are available. */
404
405 static void
406 riscv_set_arch (const char *s)
407 {
408 riscv_parse_subset_t rps;
409 rps.subset_list = &riscv_subsets;
410 rps.error_handler = as_bad;
411 rps.xlen = &xlen;
412 rps.get_default_version = riscv_get_default_ext_version;
413 rps.check_unknown_prefixed_ext = true;
414
415 if (s == NULL)
416 return;
417
418 riscv_release_subset_list (&riscv_subsets);
419 riscv_parse_subset (&rps, s);
420
421 /* To support .option rvc and rve. */
422 riscv_set_rvc (false);
423 if (riscv_subset_supports ("c"))
424 riscv_set_rvc (true);
425 riscv_set_rve (false);
426 if (riscv_subset_supports ("e"))
427 riscv_set_rve (true);
428 }
429
430 /* Indicate -mabi option is explictly set. */
431 static bool explicit_mabi = false;
432
433 static void
434 riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi, bool rve)
435 {
436 abi_xlen = new_xlen;
437 float_abi = new_float_abi;
438 rve_abi = rve;
439 }
440
441 /* If the -mabi option isn't set, then set the abi according to the
442 ISA string. Otherwise, check if there is any conflict. */
443
444 static void
445 riscv_set_abi_by_arch (void)
446 {
447 if (!explicit_mabi)
448 {
449 if (riscv_subset_supports ("q"))
450 riscv_set_abi (xlen, FLOAT_ABI_QUAD, false);
451 else if (riscv_subset_supports ("d"))
452 riscv_set_abi (xlen, FLOAT_ABI_DOUBLE, false);
453 else if (riscv_subset_supports ("e"))
454 riscv_set_abi (xlen, FLOAT_ABI_SOFT, true);
455 else
456 riscv_set_abi (xlen, FLOAT_ABI_SOFT, false);
457 }
458 else
459 {
460 gas_assert (abi_xlen != 0 && xlen != 0 && float_abi != FLOAT_ABI_DEFAULT);
461 if (abi_xlen > xlen)
462 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen);
463 else if (abi_xlen < xlen)
464 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen);
465
466 if (riscv_subset_supports ("e") && !rve_abi)
467 as_bad ("only the ilp32e ABI is supported for e extension");
468
469 if (float_abi == FLOAT_ABI_SINGLE
470 && !riscv_subset_supports ("f"))
471 as_bad ("ilp32f/lp64f ABI can't be used when f extension "
472 "isn't supported");
473 else if (float_abi == FLOAT_ABI_DOUBLE
474 && !riscv_subset_supports ("d"))
475 as_bad ("ilp32d/lp64d ABI can't be used when d extension "
476 "isn't supported");
477 else if (float_abi == FLOAT_ABI_QUAD
478 && !riscv_subset_supports ("q"))
479 as_bad ("ilp32q/lp64q ABI can't be used when q extension "
480 "isn't supported");
481 }
482
483 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
484 elf_flags &= ~EF_RISCV_FLOAT_ABI;
485 elf_flags |= float_abi << 1;
486
487 if (rve_abi)
488 elf_flags |= EF_RISCV_RVE;
489 }
490
491 /* Handle of the OPCODE hash table. */
492 static htab_t op_hash = NULL;
493
494 /* Handle of the type of .insn hash table. */
495 static htab_t insn_type_hash = NULL;
496
497 /* This array holds the chars that always start a comment. If the
498 pre-processor is disabled, these aren't very useful. */
499 const char comment_chars[] = "#";
500
501 /* This array holds the chars that only start a comment at the beginning of
502 a line. If the line seems to have the form '# 123 filename'
503 .line and .file directives will appear in the pre-processed output
504
505 Note that input_file.c hand checks for '#' at the beginning of the
506 first line of the input file. This is because the compiler outputs
507 #NO_APP at the beginning of its output.
508
509 Also note that C style comments are always supported. */
510 const char line_comment_chars[] = "#";
511
512 /* This array holds machine specific line separator characters. */
513 const char line_separator_chars[] = ";";
514
515 /* Chars that can be used to separate mant from exp in floating point nums. */
516 const char EXP_CHARS[] = "eE";
517
518 /* Chars that mean this number is a floating point constant.
519 As in 0f12.456 or 0d1.2345e12. */
520 const char FLT_CHARS[] = "rRsSfFdDxXpP";
521
522 /* Indicate we are already assemble any instructions or not. */
523 static bool start_assemble = false;
524
525 /* Indicate ELF attributes are explicitly set. */
526 static bool explicit_attr = false;
527
528 /* Indicate CSR or priv instructions are explicitly used. */
529 static bool explicit_priv_attr = false;
530
531 /* Macros for encoding relaxation state for RVC branches and far jumps. */
532 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
533 ((relax_substateT) \
534 (0xc0000000 \
535 | ((uncond) ? 1 : 0) \
536 | ((rvc) ? 2 : 0) \
537 | ((length) << 2)))
538 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
539 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
540 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
541 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
542
543 /* Is the given value a sign-extended 32-bit value? */
544 #define IS_SEXT_32BIT_NUM(x) \
545 (((x) &~ (offsetT) 0x7fffffff) == 0 \
546 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
547
548 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
549 #define IS_ZEXT_32BIT_NUM(x) \
550 (((x) &~ (offsetT) 0xffffffff) == 0 \
551 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
552
553 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
554 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
555 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
556 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
557
558 /* Determine if an instruction matches an opcode. */
559 #define OPCODE_MATCHES(OPCODE, OP) \
560 (((OPCODE) & MASK_##OP) == MATCH_##OP)
561
562 static char *expr_end;
563
564 /* The default target format to use. */
565
566 const char *
567 riscv_target_format (void)
568 {
569 if (target_big_endian)
570 return xlen == 64 ? "elf64-bigriscv" : "elf32-bigriscv";
571 else
572 return xlen == 64 ? "elf64-littleriscv" : "elf32-littleriscv";
573 }
574
575 /* Return the length of instruction INSN. */
576
577 static inline unsigned int
578 insn_length (const struct riscv_cl_insn *insn)
579 {
580 return riscv_insn_length (insn->insn_opcode);
581 }
582
583 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
584
585 static void
586 create_insn (struct riscv_cl_insn *insn, const struct riscv_opcode *mo)
587 {
588 insn->insn_mo = mo;
589 insn->insn_opcode = mo->match;
590 insn->frag = NULL;
591 insn->where = 0;
592 insn->fixp = NULL;
593 }
594
595 /* Install INSN at the location specified by its "frag" and "where" fields. */
596
597 static void
598 install_insn (const struct riscv_cl_insn *insn)
599 {
600 char *f = insn->frag->fr_literal + insn->where;
601 number_to_chars_littleendian (f, insn->insn_opcode, insn_length (insn));
602 }
603
604 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
605 and install the opcode in the new location. */
606
607 static void
608 move_insn (struct riscv_cl_insn *insn, fragS *frag, long where)
609 {
610 insn->frag = frag;
611 insn->where = where;
612 if (insn->fixp != NULL)
613 {
614 insn->fixp->fx_frag = frag;
615 insn->fixp->fx_where = where;
616 }
617 install_insn (insn);
618 }
619
620 /* Add INSN to the end of the output. */
621
622 static void
623 add_fixed_insn (struct riscv_cl_insn *insn)
624 {
625 char *f = frag_more (insn_length (insn));
626 move_insn (insn, frag_now, f - frag_now->fr_literal);
627 }
628
629 static void
630 add_relaxed_insn (struct riscv_cl_insn *insn, int max_chars, int var,
631 relax_substateT subtype, symbolS *symbol, offsetT offset)
632 {
633 frag_grow (max_chars);
634 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
635 frag_var (rs_machine_dependent, max_chars, var,
636 subtype, symbol, offset, NULL);
637 }
638
639 /* Compute the length of a branch sequence, and adjust the stored length
640 accordingly. If FRAGP is NULL, the worst-case length is returned. */
641
642 static unsigned
643 relaxed_branch_length (fragS *fragp, asection *sec, int update)
644 {
645 int jump, rvc, length = 8;
646
647 if (!fragp)
648 return length;
649
650 jump = RELAX_BRANCH_UNCOND (fragp->fr_subtype);
651 rvc = RELAX_BRANCH_RVC (fragp->fr_subtype);
652 length = RELAX_BRANCH_LENGTH (fragp->fr_subtype);
653
654 /* Assume jumps are in range; the linker will catch any that aren't. */
655 length = jump ? 4 : 8;
656
657 if (fragp->fr_symbol != NULL
658 && S_IS_DEFINED (fragp->fr_symbol)
659 && !S_IS_WEAK (fragp->fr_symbol)
660 && sec == S_GET_SEGMENT (fragp->fr_symbol))
661 {
662 offsetT val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
663 bfd_vma rvc_range = jump ? RVC_JUMP_REACH : RVC_BRANCH_REACH;
664 val -= fragp->fr_address + fragp->fr_fix;
665
666 if (rvc && (bfd_vma)(val + rvc_range/2) < rvc_range)
667 length = 2;
668 else if ((bfd_vma)(val + RISCV_BRANCH_REACH/2) < RISCV_BRANCH_REACH)
669 length = 4;
670 else if (!jump && rvc)
671 length = 6;
672 }
673
674 if (update)
675 fragp->fr_subtype = RELAX_BRANCH_ENCODE (jump, rvc, length);
676
677 return length;
678 }
679
680 /* Information about an opcode name, mnemonics and its value. */
681 struct opcode_name_t
682 {
683 const char *name;
684 unsigned int val;
685 };
686
687 /* List for all supported opcode name. */
688 static const struct opcode_name_t opcode_name_list[] =
689 {
690 {"C0", 0x0},
691 {"C1", 0x1},
692 {"C2", 0x2},
693
694 {"LOAD", 0x03},
695 {"LOAD_FP", 0x07},
696 {"CUSTOM_0", 0x0b},
697 {"MISC_MEM", 0x0f},
698 {"OP_IMM", 0x13},
699 {"AUIPC", 0x17},
700 {"OP_IMM_32", 0x1b},
701 /* 48b 0x1f. */
702
703 {"STORE", 0x23},
704 {"STORE_FP", 0x27},
705 {"CUSTOM_1", 0x2b},
706 {"AMO", 0x2f},
707 {"OP", 0x33},
708 {"LUI", 0x37},
709 {"OP_32", 0x3b},
710 /* 64b 0x3f. */
711
712 {"MADD", 0x43},
713 {"MSUB", 0x47},
714 {"NMADD", 0x4f},
715 {"NMSUB", 0x4b},
716 {"OP_FP", 0x53},
717 /*reserved 0x57. */
718 {"CUSTOM_2", 0x5b},
719 /* 48b 0x5f. */
720
721 {"BRANCH", 0x63},
722 {"JALR", 0x67},
723 /*reserved 0x5b. */
724 {"JAL", 0x6f},
725 {"SYSTEM", 0x73},
726 /*reserved 0x77. */
727 {"CUSTOM_3", 0x7b},
728 /* >80b 0x7f. */
729
730 {NULL, 0}
731 };
732
733 /* Hash table for lookup opcode name. */
734 static htab_t opcode_names_hash = NULL;
735
736 /* Initialization for hash table of opcode name. */
737
738 static void
739 init_opcode_names_hash (void)
740 {
741 const struct opcode_name_t *opcode;
742
743 for (opcode = &opcode_name_list[0]; opcode->name != NULL; ++opcode)
744 if (str_hash_insert (opcode_names_hash, opcode->name, opcode, 0) != NULL)
745 as_fatal (_("internal: duplicate %s"), opcode->name);
746 }
747
748 /* Find `s` is a valid opcode name or not, return the opcode name info
749 if found. */
750
751 static const struct opcode_name_t *
752 opcode_name_lookup (char **s)
753 {
754 char *e;
755 char save_c;
756 struct opcode_name_t *o;
757
758 /* Find end of name. */
759 e = *s;
760 if (is_name_beginner (*e))
761 ++e;
762 while (is_part_of_name (*e))
763 ++e;
764
765 /* Terminate name. */
766 save_c = *e;
767 *e = '\0';
768
769 o = (struct opcode_name_t *) str_hash_find (opcode_names_hash, *s);
770
771 /* Advance to next token if one was recognized. */
772 if (o)
773 *s = e;
774
775 *e = save_c;
776 expr_end = e;
777
778 return o;
779 }
780
781 enum reg_class
782 {
783 RCLASS_GPR,
784 RCLASS_FPR,
785 RCLASS_MAX,
786
787 RCLASS_CSR
788 };
789
790 static htab_t reg_names_hash = NULL;
791 static htab_t csr_extra_hash = NULL;
792
793 #define ENCODE_REG_HASH(cls, n) \
794 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
795 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
796 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
797
798 static void
799 hash_reg_name (enum reg_class class, const char *name, unsigned n)
800 {
801 void *hash = ENCODE_REG_HASH (class, n);
802 if (str_hash_insert (reg_names_hash, name, hash, 0) != NULL)
803 as_fatal (_("internal: duplicate %s"), name);
804 }
805
806 static void
807 hash_reg_names (enum reg_class class, const char * const names[], unsigned n)
808 {
809 unsigned i;
810
811 for (i = 0; i < n; i++)
812 hash_reg_name (class, names[i], i);
813 }
814
815 /* Init hash table csr_extra_hash to handle CSR. */
816
817 static void
818 riscv_init_csr_hash (const char *name,
819 unsigned address,
820 enum riscv_csr_class class,
821 enum riscv_spec_class define_version,
822 enum riscv_spec_class abort_version)
823 {
824 struct riscv_csr_extra *entry, *pre_entry;
825 bool need_enrty = true;
826
827 pre_entry = NULL;
828 entry = (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, name);
829 while (need_enrty && entry != NULL)
830 {
831 if (entry->csr_class == class
832 && entry->address == address
833 && entry->define_version == define_version
834 && entry->abort_version == abort_version)
835 need_enrty = false;
836 pre_entry = entry;
837 entry = entry->next;
838 }
839
840 /* Duplicate CSR. */
841 if (!need_enrty)
842 return;
843
844 entry = XNEW (struct riscv_csr_extra);
845 entry->csr_class = class;
846 entry->address = address;
847 entry->define_version = define_version;
848 entry->abort_version = abort_version;
849 entry->next = NULL;
850
851 if (pre_entry == NULL)
852 str_hash_insert (csr_extra_hash, name, entry, 0);
853 else
854 pre_entry->next = entry;
855 }
856
857 /* Return the CSR address after checking the ISA dependency and
858 the privileged spec version.
859
860 There are one warning and two errors for CSR,
861
862 Invalid CSR: the CSR was defined, but isn't allowed for the current ISA
863 or the privileged spec, report warning only if -mcsr-check is set.
864 Unknown CSR: the CSR has never been defined, report error.
865 Improper CSR: the CSR number over the range (> 0xfff), report error. */
866
867 static unsigned int
868 riscv_csr_address (const char *csr_name,
869 struct riscv_csr_extra *entry)
870 {
871 struct riscv_csr_extra *saved_entry = entry;
872 enum riscv_csr_class csr_class = entry->csr_class;
873 bool need_check_version = true;
874 bool result = true;
875
876 switch (csr_class)
877 {
878 case CSR_CLASS_I:
879 result = riscv_subset_supports ("i");
880 break;
881 case CSR_CLASS_I_32:
882 result = (xlen == 32 && riscv_subset_supports ("i"));
883 break;
884 case CSR_CLASS_F:
885 result = riscv_subset_supports ("f");
886 need_check_version = false;
887 break;
888 case CSR_CLASS_DEBUG:
889 need_check_version = false;
890 break;
891 default:
892 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class);
893 }
894
895 if (riscv_opts.csr_check && !result)
896 as_warn (_("invalid CSR `%s' for the current ISA"), csr_name);
897
898 while (entry != NULL)
899 {
900 if (!need_check_version
901 || (default_priv_spec >= entry->define_version
902 && default_priv_spec < entry->abort_version))
903 {
904 /* Find the CSR according to the specific version. */
905 return entry->address;
906 }
907 entry = entry->next;
908 }
909
910 /* Can not find the CSR address from the chosen privileged version,
911 so use the newly defined value. */
912 if (riscv_opts.csr_check)
913 {
914 const char *priv_name = NULL;
915 RISCV_GET_PRIV_SPEC_NAME (priv_name, default_priv_spec);
916 if (priv_name != NULL)
917 as_warn (_("invalid CSR `%s' for the privileged spec `%s'"),
918 csr_name, priv_name);
919 }
920
921 return saved_entry->address;
922 }
923
924 /* Return -1 if the CSR has never been defined. Otherwise, return
925 the address. */
926
927 static unsigned int
928 reg_csr_lookup_internal (const char *s)
929 {
930 struct riscv_csr_extra *r =
931 (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, s);
932
933 if (r == NULL)
934 return -1U;
935
936 return riscv_csr_address (s, r);
937 }
938
939 static unsigned int
940 reg_lookup_internal (const char *s, enum reg_class class)
941 {
942 void *r;
943
944 if (class == RCLASS_CSR)
945 return reg_csr_lookup_internal (s);
946
947 r = str_hash_find (reg_names_hash, s);
948 if (r == NULL || DECODE_REG_CLASS (r) != class)
949 return -1;
950
951 if (riscv_opts.rve && class == RCLASS_GPR && DECODE_REG_NUM (r) > 15)
952 return -1;
953
954 return DECODE_REG_NUM (r);
955 }
956
957 static bool
958 reg_lookup (char **s, enum reg_class class, unsigned int *regnop)
959 {
960 char *e;
961 char save_c;
962 int reg = -1;
963
964 /* Find end of name. */
965 e = *s;
966 if (is_name_beginner (*e))
967 ++e;
968 while (is_part_of_name (*e))
969 ++e;
970
971 /* Terminate name. */
972 save_c = *e;
973 *e = '\0';
974
975 /* Look for the register. Advance to next token if one was recognized. */
976 if ((reg = reg_lookup_internal (*s, class)) >= 0)
977 *s = e;
978
979 *e = save_c;
980 if (regnop)
981 *regnop = reg;
982 return reg >= 0;
983 }
984
985 static bool
986 arg_lookup (char **s, const char *const *array, size_t size, unsigned *regnop)
987 {
988 const char *p = strchr (*s, ',');
989 size_t i, len = p ? (size_t)(p - *s) : strlen (*s);
990
991 if (len == 0)
992 return false;
993
994 for (i = 0; i < size; i++)
995 if (array[i] != NULL && strncmp (array[i], *s, len) == 0)
996 {
997 *regnop = i;
998 *s += len;
999 return true;
1000 }
1001
1002 return false;
1003 }
1004
1005 /* For consistency checking, verify that all bits are specified either
1006 by the match/mask part of the instruction definition, or by the
1007 operand list. The `length` could be 0, 4 or 8, 0 for auto detection. */
1008
1009 static bool
1010 validate_riscv_insn (const struct riscv_opcode *opc, int length)
1011 {
1012 const char *p = opc->args;
1013 char c;
1014 insn_t used_bits = opc->mask;
1015 int insn_width;
1016 insn_t required_bits;
1017
1018 if (length == 0)
1019 insn_width = 8 * riscv_insn_length (opc->match);
1020 else
1021 insn_width = 8 * length;
1022
1023 required_bits = ~0ULL >> (64 - insn_width);
1024
1025 if ((used_bits & opc->match) != (opc->match & required_bits))
1026 {
1027 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
1028 opc->name, opc->args);
1029 return false;
1030 }
1031
1032 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
1033 while (*p)
1034 switch (c = *p++)
1035 {
1036 case 'C': /* RVC */
1037 switch (c = *p++)
1038 {
1039 case 'U': break; /* CRS1, constrained to equal RD. */
1040 case 'c': break; /* CRS1, constrained to equal sp. */
1041 case 'T': /* CRS2, floating point. */
1042 case 'V': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
1043 case 'S': /* CRS1S, floating point. */
1044 case 's': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
1045 case 'w': break; /* CRS1S, constrained to equal RD. */
1046 case 'D': /* CRS2S, floating point. */
1047 case 't': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
1048 case 'x': break; /* CRS2S, constrained to equal RD. */
1049 case 'z': break; /* CRS2S, constrained to be x0. */
1050 case '>': /* CITYPE immediate, compressed shift. */
1051 case 'u': /* CITYPE immediate, compressed lui. */
1052 case 'v': /* CITYPE immediate, li to compressed lui. */
1053 case 'o': /* CITYPE immediate, allow zero. */
1054 case 'j': used_bits |= ENCODE_CITYPE_IMM (-1U); break;
1055 case 'L': used_bits |= ENCODE_CITYPE_ADDI16SP_IMM (-1U); break;
1056 case 'm': used_bits |= ENCODE_CITYPE_LWSP_IMM (-1U); break;
1057 case 'n': used_bits |= ENCODE_CITYPE_LDSP_IMM (-1U); break;
1058 case '6': used_bits |= ENCODE_CSSTYPE_IMM (-1U); break;
1059 case 'M': used_bits |= ENCODE_CSSTYPE_SWSP_IMM (-1U); break;
1060 case 'N': used_bits |= ENCODE_CSSTYPE_SDSP_IMM (-1U); break;
1061 case '8': used_bits |= ENCODE_CIWTYPE_IMM (-1U); break;
1062 case 'K': used_bits |= ENCODE_CIWTYPE_ADDI4SPN_IMM (-1U); break;
1063 /* CLTYPE and CSTYPE have the same immediate encoding. */
1064 case '5': used_bits |= ENCODE_CLTYPE_IMM (-1U); break;
1065 case 'k': used_bits |= ENCODE_CLTYPE_LW_IMM (-1U); break;
1066 case 'l': used_bits |= ENCODE_CLTYPE_LD_IMM (-1U); break;
1067 case 'p': used_bits |= ENCODE_CBTYPE_IMM (-1U); break;
1068 case 'a': used_bits |= ENCODE_CJTYPE_IMM (-1U); break;
1069 case 'F': /* Compressed funct for .insn directive. */
1070 switch (c = *p++)
1071 {
1072 case '6': USE_BITS (OP_MASK_CFUNCT6, OP_SH_CFUNCT6); break;
1073 case '4': USE_BITS (OP_MASK_CFUNCT4, OP_SH_CFUNCT4); break;
1074 case '3': USE_BITS (OP_MASK_CFUNCT3, OP_SH_CFUNCT3); break;
1075 case '2': USE_BITS (OP_MASK_CFUNCT2, OP_SH_CFUNCT2); break;
1076 default:
1077 as_bad (_("internal: bad RISC-V opcode "
1078 "(unknown operand type `CF%c'): %s %s"),
1079 c, opc->name, opc->args);
1080 return false;
1081 }
1082 break;
1083 default:
1084 as_bad (_("internal: bad RISC-V opcode "
1085 "(unknown operand type `C%c'): %s %s"),
1086 c, opc->name, opc->args);
1087 return false;
1088 }
1089 break;
1090 case ',': break;
1091 case '(': break;
1092 case ')': break;
1093 case '<': USE_BITS (OP_MASK_SHAMTW, OP_SH_SHAMTW); break;
1094 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
1095 case 'A': break; /* Macro operand, must be symbol. */
1096 case 'B': break; /* Macro operand, must be symbol or constant. */
1097 case 'I': break; /* Macro operand, must be constant. */
1098 case 'D': /* RD, floating point. */
1099 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
1100 case 'Z': /* RS1, CSR number. */
1101 case 'S': /* RS1, floating point. */
1102 case 's': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1103 case 'U': /* RS1 and RS2 are the same, floating point. */
1104 USE_BITS (OP_MASK_RS1, OP_SH_RS1);
1105 /* Fall through. */
1106 case 'T': /* RS2, floating point. */
1107 case 't': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
1108 case 'R': /* RS3, floating point. */
1109 case 'r': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break;
1110 case 'm': USE_BITS (OP_MASK_RM, OP_SH_RM); break;
1111 case 'E': USE_BITS (OP_MASK_CSR, OP_SH_CSR); break;
1112 case 'P': USE_BITS (OP_MASK_PRED, OP_SH_PRED); break;
1113 case 'Q': USE_BITS (OP_MASK_SUCC, OP_SH_SUCC); break;
1114 case 'o': /* ITYPE immediate, load displacement. */
1115 case 'j': used_bits |= ENCODE_ITYPE_IMM (-1U); break;
1116 case 'a': used_bits |= ENCODE_JTYPE_IMM (-1U); break;
1117 case 'p': used_bits |= ENCODE_BTYPE_IMM (-1U); break;
1118 case 'q': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1119 case 'u': used_bits |= ENCODE_UTYPE_IMM (-1U); break;
1120 case 'z': break; /* Zero immediate. */
1121 case '[': break; /* Unused operand. */
1122 case ']': break; /* Unused operand. */
1123 case '0': break; /* AMO displacement, must to zero. */
1124 case '1': break; /* Relaxation operand. */
1125 case 'F': /* Funct for .insn directive. */
1126 switch (c = *p++)
1127 {
1128 case '7': USE_BITS (OP_MASK_FUNCT7, OP_SH_FUNCT7); break;
1129 case '3': USE_BITS (OP_MASK_FUNCT3, OP_SH_FUNCT3); break;
1130 case '2': USE_BITS (OP_MASK_FUNCT2, OP_SH_FUNCT2); break;
1131 default:
1132 as_bad (_("internal: bad RISC-V opcode "
1133 "(unknown operand type `F%c'): %s %s"),
1134 c, opc->name, opc->args);
1135 return false;
1136 }
1137 break;
1138 case 'O': /* Opcode for .insn directive. */
1139 switch (c = *p++)
1140 {
1141 case '4': USE_BITS (OP_MASK_OP, OP_SH_OP); break;
1142 case '2': USE_BITS (OP_MASK_OP2, OP_SH_OP2); break;
1143 default:
1144 as_bad (_("internal: bad RISC-V opcode "
1145 "(unknown operand type `F%c'): %s %s"),
1146 c, opc->name, opc->args);
1147 return false;
1148 }
1149 break;
1150 default:
1151 as_bad (_("internal: bad RISC-V opcode "
1152 "(unknown operand type `%c'): %s %s"),
1153 c, opc->name, opc->args);
1154 return false;
1155 }
1156 #undef USE_BITS
1157 if (used_bits != required_bits)
1158 {
1159 as_bad (_("internal: bad RISC-V opcode "
1160 "(bits 0x%lx undefined): %s %s"),
1161 ~(unsigned long)(used_bits & required_bits),
1162 opc->name, opc->args);
1163 return false;
1164 }
1165 return true;
1166 }
1167
1168 struct percent_op_match
1169 {
1170 const char *str;
1171 bfd_reloc_code_real_type reloc;
1172 };
1173
1174 /* Common hash table initialization function for instruction and .insn
1175 directive. */
1176
1177 static htab_t
1178 init_opcode_hash (const struct riscv_opcode *opcodes,
1179 bool insn_directive_p)
1180 {
1181 int i = 0;
1182 int length;
1183 htab_t hash = str_htab_create ();
1184 while (opcodes[i].name)
1185 {
1186 const char *name = opcodes[i].name;
1187 if (str_hash_insert (hash, name, &opcodes[i], 0) != NULL)
1188 as_fatal (_("internal: duplicate %s"), name);
1189
1190 do
1191 {
1192 if (opcodes[i].pinfo != INSN_MACRO)
1193 {
1194 if (insn_directive_p)
1195 length = ((name[0] == 'c') ? 2 : 4);
1196 else
1197 length = 0; /* Let assembler determine the length. */
1198 if (!validate_riscv_insn (&opcodes[i], length))
1199 as_fatal (_("internal: broken assembler. "
1200 "No assembly attempted"));
1201 }
1202 else
1203 gas_assert (!insn_directive_p);
1204 ++i;
1205 }
1206 while (opcodes[i].name && !strcmp (opcodes[i].name, name));
1207 }
1208
1209 return hash;
1210 }
1211
1212 /* This function is called once, at assembler startup time. It should set up
1213 all the tables, etc. that the MD part of the assembler will need. */
1214
1215 void
1216 md_begin (void)
1217 {
1218 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
1219
1220 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
1221 as_warn (_("could not set architecture and machine"));
1222
1223 op_hash = init_opcode_hash (riscv_opcodes, false);
1224 insn_type_hash = init_opcode_hash (riscv_insn_types, true);
1225
1226 reg_names_hash = str_htab_create ();
1227 hash_reg_names (RCLASS_GPR, riscv_gpr_names_numeric, NGPR);
1228 hash_reg_names (RCLASS_GPR, riscv_gpr_names_abi, NGPR);
1229 hash_reg_names (RCLASS_FPR, riscv_fpr_names_numeric, NFPR);
1230 hash_reg_names (RCLASS_FPR, riscv_fpr_names_abi, NFPR);
1231 /* Add "fp" as an alias for "s0". */
1232 hash_reg_name (RCLASS_GPR, "fp", 8);
1233
1234 /* Create and insert CSR hash tables. */
1235 csr_extra_hash = str_htab_create ();
1236 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
1237 riscv_init_csr_hash (#name, num, class, define_version, abort_version);
1238 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
1239 DECLARE_CSR(name, num, class, define_version, abort_version);
1240 #include "opcode/riscv-opc.h"
1241 #undef DECLARE_CSR
1242
1243 opcode_names_hash = str_htab_create ();
1244 init_opcode_names_hash ();
1245
1246 /* Set the default alignment for the text section. */
1247 record_alignment (text_section, riscv_opts.rvc ? 1 : 2);
1248 }
1249
1250 static insn_t
1251 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type, bfd_vma value)
1252 {
1253 switch (reloc_type)
1254 {
1255 case BFD_RELOC_32:
1256 return value;
1257
1258 case BFD_RELOC_RISCV_HI20:
1259 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1260
1261 case BFD_RELOC_RISCV_LO12_S:
1262 return ENCODE_STYPE_IMM (value);
1263
1264 case BFD_RELOC_RISCV_LO12_I:
1265 return ENCODE_ITYPE_IMM (value);
1266
1267 default:
1268 abort ();
1269 }
1270 }
1271
1272 /* Output an instruction. IP is the instruction information.
1273 ADDRESS_EXPR is an operand of the instruction to be used with
1274 RELOC_TYPE. */
1275
1276 static void
1277 append_insn (struct riscv_cl_insn *ip, expressionS *address_expr,
1278 bfd_reloc_code_real_type reloc_type)
1279 {
1280 dwarf2_emit_insn (0);
1281
1282 if (reloc_type != BFD_RELOC_UNUSED)
1283 {
1284 reloc_howto_type *howto;
1285
1286 gas_assert (address_expr);
1287 if (reloc_type == BFD_RELOC_12_PCREL
1288 || reloc_type == BFD_RELOC_RISCV_JMP)
1289 {
1290 int j = reloc_type == BFD_RELOC_RISCV_JMP;
1291 int best_case = riscv_insn_length (ip->insn_opcode);
1292 unsigned worst_case = relaxed_branch_length (NULL, NULL, 0);
1293
1294 if (now_seg == absolute_section)
1295 {
1296 as_bad (_("relaxable branches not supported in absolute section"));
1297 return;
1298 }
1299
1300 add_relaxed_insn (ip, worst_case, best_case,
1301 RELAX_BRANCH_ENCODE (j, best_case == 2, worst_case),
1302 address_expr->X_add_symbol,
1303 address_expr->X_add_number);
1304 return;
1305 }
1306 else
1307 {
1308 howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1309 if (howto == NULL)
1310 as_bad (_("internal: usupported RISC-V relocation number %d"),
1311 reloc_type);
1312
1313 ip->fixp = fix_new_exp (ip->frag, ip->where,
1314 bfd_get_reloc_size (howto),
1315 address_expr, false, reloc_type);
1316
1317 ip->fixp->fx_tcbit = riscv_opts.relax;
1318 }
1319 }
1320
1321 add_fixed_insn (ip);
1322 install_insn (ip);
1323
1324 /* We need to start a new frag after any instruction that can be
1325 optimized away or compressed by the linker during relaxation, to prevent
1326 the assembler from computing static offsets across such an instruction.
1327 This is necessary to get correct EH info. */
1328 if (reloc_type == BFD_RELOC_RISCV_HI20
1329 || reloc_type == BFD_RELOC_RISCV_PCREL_HI20
1330 || reloc_type == BFD_RELOC_RISCV_TPREL_HI20
1331 || reloc_type == BFD_RELOC_RISCV_TPREL_ADD)
1332 {
1333 frag_wane (frag_now);
1334 frag_new (0);
1335 }
1336 }
1337
1338 /* Build an instruction created by a macro expansion. This is passed
1339 a pointer to the count of instructions created so far, an expression,
1340 the name of the instruction to build, an operand format string, and
1341 corresponding arguments. */
1342
1343 static void
1344 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
1345 {
1346 const struct riscv_opcode *mo;
1347 struct riscv_cl_insn insn;
1348 bfd_reloc_code_real_type r;
1349 va_list args;
1350
1351 va_start (args, fmt);
1352
1353 r = BFD_RELOC_UNUSED;
1354 mo = (struct riscv_opcode *) str_hash_find (op_hash, name);
1355 gas_assert (mo);
1356
1357 /* Find a non-RVC variant of the instruction. append_insn will compress
1358 it if possible. */
1359 while (riscv_insn_length (mo->match) < 4)
1360 mo++;
1361 gas_assert (strcmp (name, mo->name) == 0);
1362
1363 create_insn (&insn, mo);
1364 for (;;)
1365 {
1366 switch (*fmt++)
1367 {
1368 case 'd':
1369 INSERT_OPERAND (RD, insn, va_arg (args, int));
1370 continue;
1371
1372 case 's':
1373 INSERT_OPERAND (RS1, insn, va_arg (args, int));
1374 continue;
1375
1376 case 't':
1377 INSERT_OPERAND (RS2, insn, va_arg (args, int));
1378 continue;
1379
1380 case 'j':
1381 case 'u':
1382 case 'q':
1383 gas_assert (ep != NULL);
1384 r = va_arg (args, int);
1385 continue;
1386
1387 case '\0':
1388 break;
1389 case ',':
1390 continue;
1391 default:
1392 as_fatal (_("internal: invalid macro"));
1393 }
1394 break;
1395 }
1396 va_end (args);
1397 gas_assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
1398
1399 append_insn (&insn, ep, r);
1400 }
1401
1402 /* Build an instruction created by a macro expansion. Like md_assemble but
1403 accept a printf-style format string and arguments. */
1404
1405 static void
1406 md_assemblef (const char *format, ...)
1407 {
1408 char *buf = NULL;
1409 va_list ap;
1410 int r;
1411
1412 va_start (ap, format);
1413
1414 r = vasprintf (&buf, format, ap);
1415
1416 if (r < 0)
1417 as_fatal (_("internal: vasprintf failed"));
1418
1419 md_assemble (buf);
1420 free(buf);
1421
1422 va_end (ap);
1423 }
1424
1425 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
1426 unset. */
1427
1428 static void
1429 normalize_constant_expr (expressionS *ex)
1430 {
1431 if (xlen > 32)
1432 return;
1433 if ((ex->X_op == O_constant || ex->X_op == O_symbol)
1434 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
1435 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
1436 - 0x80000000);
1437 }
1438
1439 /* Fail if an expression EX is not a constant. IP is the instruction using EX.
1440 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
1441
1442 static void
1443 check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex,
1444 bool maybe_csr)
1445 {
1446 if (ex->X_op == O_big)
1447 as_bad (_("unsupported large constant"));
1448 else if (maybe_csr && ex->X_op == O_symbol)
1449 as_bad (_("unknown CSR `%s'"),
1450 S_GET_NAME (ex->X_add_symbol));
1451 else if (ex->X_op != O_constant)
1452 as_bad (_("instruction %s requires absolute expression"),
1453 ip->insn_mo->name);
1454 normalize_constant_expr (ex);
1455 }
1456
1457 static symbolS *
1458 make_internal_label (void)
1459 {
1460 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg, frag_now,
1461 frag_now_fix ());
1462 }
1463
1464 /* Load an entry from the GOT. */
1465
1466 static void
1467 pcrel_access (int destreg, int tempreg, expressionS *ep,
1468 const char *lo_insn, const char *lo_pattern,
1469 bfd_reloc_code_real_type hi_reloc,
1470 bfd_reloc_code_real_type lo_reloc)
1471 {
1472 expressionS ep2;
1473 ep2.X_op = O_symbol;
1474 ep2.X_add_symbol = make_internal_label ();
1475 ep2.X_add_number = 0;
1476
1477 macro_build (ep, "auipc", "d,u", tempreg, hi_reloc);
1478 macro_build (&ep2, lo_insn, lo_pattern, destreg, tempreg, lo_reloc);
1479 }
1480
1481 static void
1482 pcrel_load (int destreg, int tempreg, expressionS *ep, const char *lo_insn,
1483 bfd_reloc_code_real_type hi_reloc,
1484 bfd_reloc_code_real_type lo_reloc)
1485 {
1486 pcrel_access (destreg, tempreg, ep, lo_insn, "d,s,j", hi_reloc, lo_reloc);
1487 }
1488
1489 static void
1490 pcrel_store (int srcreg, int tempreg, expressionS *ep, const char *lo_insn,
1491 bfd_reloc_code_real_type hi_reloc,
1492 bfd_reloc_code_real_type lo_reloc)
1493 {
1494 pcrel_access (srcreg, tempreg, ep, lo_insn, "t,s,q", hi_reloc, lo_reloc);
1495 }
1496
1497 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
1498
1499 static void
1500 riscv_call (int destreg, int tempreg, expressionS *ep,
1501 bfd_reloc_code_real_type reloc)
1502 {
1503 /* Ensure the jalr is emitted to the same frag as the auipc. */
1504 frag_grow (8);
1505 macro_build (ep, "auipc", "d,u", tempreg, reloc);
1506 macro_build (NULL, "jalr", "d,s", destreg, tempreg);
1507 /* See comment at end of append_insn. */
1508 frag_wane (frag_now);
1509 frag_new (0);
1510 }
1511
1512 /* Load an integer constant into a register. */
1513
1514 static void
1515 load_const (int reg, expressionS *ep)
1516 {
1517 int shift = RISCV_IMM_BITS;
1518 bfd_vma upper_imm, sign = (bfd_vma) 1 << (RISCV_IMM_BITS - 1);
1519 expressionS upper = *ep, lower = *ep;
1520 lower.X_add_number = ((ep->X_add_number & (sign + sign - 1)) ^ sign) - sign;
1521 upper.X_add_number -= lower.X_add_number;
1522
1523 if (ep->X_op != O_constant)
1524 {
1525 as_bad (_("unsupported large constant"));
1526 return;
1527 }
1528
1529 if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number))
1530 {
1531 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
1532 while (((upper.X_add_number >> shift) & 1) == 0)
1533 shift++;
1534
1535 upper.X_add_number = (int64_t) upper.X_add_number >> shift;
1536 load_const (reg, &upper);
1537
1538 md_assemblef ("slli x%d, x%d, 0x%x", reg, reg, shift);
1539 if (lower.X_add_number != 0)
1540 md_assemblef ("addi x%d, x%d, %" BFD_VMA_FMT "d", reg, reg,
1541 lower.X_add_number);
1542 }
1543 else
1544 {
1545 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
1546 int hi_reg = 0;
1547
1548 if (upper.X_add_number != 0)
1549 {
1550 /* Discard low part and zero-extend upper immediate. */
1551 upper_imm = ((uint32_t)upper.X_add_number >> shift);
1552
1553 md_assemblef ("lui x%d, 0x%" BFD_VMA_FMT "x", reg, upper_imm);
1554 hi_reg = reg;
1555 }
1556
1557 if (lower.X_add_number != 0 || hi_reg == 0)
1558 md_assemblef ("%s x%d, x%d, %" BFD_VMA_FMT "d", ADD32_INSN, reg, hi_reg,
1559 lower.X_add_number);
1560 }
1561 }
1562
1563 /* Zero extend and sign extend byte/half-word/word. */
1564
1565 static void
1566 riscv_ext (int destreg, int srcreg, unsigned shift, bool sign)
1567 {
1568 if (sign)
1569 {
1570 md_assemblef ("slli x%d, x%d, 0x%x", destreg, srcreg, shift);
1571 md_assemblef ("srai x%d, x%d, 0x%x", destreg, destreg, shift);
1572 }
1573 else
1574 {
1575 md_assemblef ("slli x%d, x%d, 0x%x", destreg, srcreg, shift);
1576 md_assemblef ("srli x%d, x%d, 0x%x", destreg, destreg, shift);
1577 }
1578 }
1579
1580 /* Expand RISC-V assembly macros into one or more instructions. */
1581
1582 static void
1583 macro (struct riscv_cl_insn *ip, expressionS *imm_expr,
1584 bfd_reloc_code_real_type *imm_reloc)
1585 {
1586 int rd = (ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD;
1587 int rs1 = (ip->insn_opcode >> OP_SH_RS1) & OP_MASK_RS1;
1588 int rs2 = (ip->insn_opcode >> OP_SH_RS2) & OP_MASK_RS2;
1589 int mask = ip->insn_mo->mask;
1590
1591 switch (mask)
1592 {
1593 case M_LI:
1594 load_const (rd, imm_expr);
1595 break;
1596
1597 case M_LA:
1598 case M_LLA:
1599 /* Load the address of a symbol into a register. */
1600 if (!IS_SEXT_32BIT_NUM (imm_expr->X_add_number))
1601 as_bad (_("offset too large"));
1602
1603 if (imm_expr->X_op == O_constant)
1604 load_const (rd, imm_expr);
1605 else if (riscv_opts.pic && mask == M_LA) /* Global PIC symbol. */
1606 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
1607 BFD_RELOC_RISCV_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1608 else /* Local PIC symbol, or any non-PIC symbol. */
1609 pcrel_load (rd, rd, imm_expr, "addi",
1610 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1611 break;
1612
1613 case M_LA_TLS_GD:
1614 pcrel_load (rd, rd, imm_expr, "addi",
1615 BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1616 break;
1617
1618 case M_LA_TLS_IE:
1619 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
1620 BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1621 break;
1622
1623 case M_LB:
1624 pcrel_load (rd, rd, imm_expr, "lb",
1625 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1626 break;
1627
1628 case M_LBU:
1629 pcrel_load (rd, rd, imm_expr, "lbu",
1630 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1631 break;
1632
1633 case M_LH:
1634 pcrel_load (rd, rd, imm_expr, "lh",
1635 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1636 break;
1637
1638 case M_LHU:
1639 pcrel_load (rd, rd, imm_expr, "lhu",
1640 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1641 break;
1642
1643 case M_LW:
1644 pcrel_load (rd, rd, imm_expr, "lw",
1645 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1646 break;
1647
1648 case M_LWU:
1649 pcrel_load (rd, rd, imm_expr, "lwu",
1650 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1651 break;
1652
1653 case M_LD:
1654 pcrel_load (rd, rd, imm_expr, "ld",
1655 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1656 break;
1657
1658 case M_FLW:
1659 pcrel_load (rd, rs1, imm_expr, "flw",
1660 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1661 break;
1662
1663 case M_FLD:
1664 pcrel_load (rd, rs1, imm_expr, "fld",
1665 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1666 break;
1667
1668 case M_SB:
1669 pcrel_store (rs2, rs1, imm_expr, "sb",
1670 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1671 break;
1672
1673 case M_SH:
1674 pcrel_store (rs2, rs1, imm_expr, "sh",
1675 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1676 break;
1677
1678 case M_SW:
1679 pcrel_store (rs2, rs1, imm_expr, "sw",
1680 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1681 break;
1682
1683 case M_SD:
1684 pcrel_store (rs2, rs1, imm_expr, "sd",
1685 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1686 break;
1687
1688 case M_FSW:
1689 pcrel_store (rs2, rs1, imm_expr, "fsw",
1690 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1691 break;
1692
1693 case M_FSD:
1694 pcrel_store (rs2, rs1, imm_expr, "fsd",
1695 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1696 break;
1697
1698 case M_CALL:
1699 riscv_call (rd, rs1, imm_expr, *imm_reloc);
1700 break;
1701
1702 case M_ZEXTH:
1703 riscv_ext (rd, rs1, xlen - 16, false);
1704 break;
1705
1706 case M_ZEXTW:
1707 riscv_ext (rd, rs1, xlen - 32, false);
1708 break;
1709
1710 case M_SEXTB:
1711 riscv_ext (rd, rs1, xlen - 8, true);
1712 break;
1713
1714 case M_SEXTH:
1715 riscv_ext (rd, rs1, xlen - 16, true);
1716 break;
1717
1718 default:
1719 as_bad (_("internal: macro %s not implemented"), ip->insn_mo->name);
1720 break;
1721 }
1722 }
1723
1724 static const struct percent_op_match percent_op_utype[] =
1725 {
1726 {"%tprel_hi", BFD_RELOC_RISCV_TPREL_HI20},
1727 {"%pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20},
1728 {"%got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20},
1729 {"%tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20},
1730 {"%tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20},
1731 {"%hi", BFD_RELOC_RISCV_HI20},
1732 {0, 0}
1733 };
1734
1735 static const struct percent_op_match percent_op_itype[] =
1736 {
1737 {"%lo", BFD_RELOC_RISCV_LO12_I},
1738 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I},
1739 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I},
1740 {0, 0}
1741 };
1742
1743 static const struct percent_op_match percent_op_stype[] =
1744 {
1745 {"%lo", BFD_RELOC_RISCV_LO12_S},
1746 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S},
1747 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S},
1748 {0, 0}
1749 };
1750
1751 static const struct percent_op_match percent_op_rtype[] =
1752 {
1753 {"%tprel_add", BFD_RELOC_RISCV_TPREL_ADD},
1754 {0, 0}
1755 };
1756
1757 static const struct percent_op_match percent_op_null[] =
1758 {
1759 {0, 0}
1760 };
1761
1762 /* Return true if *STR points to a relocation operator. When returning true,
1763 move *STR over the operator and store its relocation code in *RELOC.
1764 Leave both *STR and *RELOC alone when returning false. */
1765
1766 static bool
1767 parse_relocation (char **str, bfd_reloc_code_real_type *reloc,
1768 const struct percent_op_match *percent_op)
1769 {
1770 for ( ; percent_op->str; percent_op++)
1771 if (strncasecmp (*str, percent_op->str, strlen (percent_op->str)) == 0)
1772 {
1773 int len = strlen (percent_op->str);
1774
1775 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
1776 continue;
1777
1778 *str += strlen (percent_op->str);
1779 *reloc = percent_op->reloc;
1780
1781 /* Check whether the output BFD supports this relocation.
1782 If not, issue an error and fall back on something safe. */
1783 if (*reloc != BFD_RELOC_UNUSED
1784 && !bfd_reloc_type_lookup (stdoutput, *reloc))
1785 {
1786 as_bad ("internal: relocation %s isn't supported by the "
1787 "current ABI", percent_op->str);
1788 *reloc = BFD_RELOC_UNUSED;
1789 }
1790 return true;
1791 }
1792 return false;
1793 }
1794
1795 static void
1796 my_getExpression (expressionS *ep, char *str)
1797 {
1798 char *save_in;
1799
1800 save_in = input_line_pointer;
1801 input_line_pointer = str;
1802 expression (ep);
1803 expr_end = input_line_pointer;
1804 input_line_pointer = save_in;
1805 }
1806
1807 /* Parse string STR as a 16-bit relocatable operand. Store the
1808 expression in *EP and the relocation, if any, in RELOC.
1809 Return the number of relocation operators used (0 or 1).
1810
1811 On exit, EXPR_END points to the first character after the expression. */
1812
1813 static size_t
1814 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
1815 char *str, const struct percent_op_match *percent_op)
1816 {
1817 size_t reloc_index;
1818 unsigned crux_depth, str_depth, regno;
1819 char *crux;
1820
1821 /* First, check for integer registers. No callers can accept a reg, but
1822 we need to avoid accidentally creating a useless undefined symbol below,
1823 if this is an instruction pattern that can't match. A glibc build fails
1824 if this is removed. */
1825 if (reg_lookup (&str, RCLASS_GPR, &regno))
1826 {
1827 ep->X_op = O_register;
1828 ep->X_add_number = regno;
1829 expr_end = str;
1830 return 0;
1831 }
1832
1833 /* Search for the start of the main expression.
1834
1835 End the loop with CRUX pointing to the start of the main expression and
1836 with CRUX_DEPTH containing the number of open brackets at that point. */
1837 reloc_index = -1;
1838 str_depth = 0;
1839 do
1840 {
1841 reloc_index++;
1842 crux = str;
1843 crux_depth = str_depth;
1844
1845 /* Skip over whitespace and brackets, keeping count of the number
1846 of brackets. */
1847 while (*str == ' ' || *str == '\t' || *str == '(')
1848 if (*str++ == '(')
1849 str_depth++;
1850 }
1851 while (*str == '%'
1852 && reloc_index < 1
1853 && parse_relocation (&str, reloc, percent_op));
1854
1855 my_getExpression (ep, crux);
1856 str = expr_end;
1857
1858 /* Match every open bracket. */
1859 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
1860 if (*str++ == ')')
1861 crux_depth--;
1862
1863 if (crux_depth > 0)
1864 as_bad ("unclosed '('");
1865
1866 expr_end = str;
1867
1868 return reloc_index;
1869 }
1870
1871 /* Parse opcode name, could be an mnemonics or number. */
1872
1873 static size_t
1874 my_getOpcodeExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
1875 char *str, const struct percent_op_match *percent_op)
1876 {
1877 const struct opcode_name_t *o = opcode_name_lookup (&str);
1878
1879 if (o != NULL)
1880 {
1881 ep->X_op = O_constant;
1882 ep->X_add_number = o->val;
1883 return 0;
1884 }
1885
1886 return my_getSmallExpression (ep, reloc, str, percent_op);
1887 }
1888
1889 /* Detect and handle implicitly zero load-store offsets. For example,
1890 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return TRUE iff such
1891 an implicit offset was detected. */
1892
1893 static bool
1894 riscv_handle_implicit_zero_offset (expressionS *ep, const char *s)
1895 {
1896 /* Check whether there is only a single bracketed expression left.
1897 If so, it must be the base register and the constant must be zero. */
1898 if (*s == '(' && strchr (s + 1, '(') == 0)
1899 {
1900 ep->X_op = O_constant;
1901 ep->X_add_number = 0;
1902 return true;
1903 }
1904
1905 return false;
1906 }
1907
1908 /* All RISC-V CSR instructions belong to one of these classes. */
1909 enum csr_insn_type
1910 {
1911 INSN_NOT_CSR,
1912 INSN_CSRRW,
1913 INSN_CSRRS,
1914 INSN_CSRRC
1915 };
1916
1917 /* Return which CSR instruction is checking. */
1918
1919 static enum csr_insn_type
1920 riscv_csr_insn_type (insn_t insn)
1921 {
1922 if (((insn ^ MATCH_CSRRW) & MASK_CSRRW) == 0
1923 || ((insn ^ MATCH_CSRRWI) & MASK_CSRRWI) == 0)
1924 return INSN_CSRRW;
1925 else if (((insn ^ MATCH_CSRRS) & MASK_CSRRS) == 0
1926 || ((insn ^ MATCH_CSRRSI) & MASK_CSRRSI) == 0)
1927 return INSN_CSRRS;
1928 else if (((insn ^ MATCH_CSRRC) & MASK_CSRRC) == 0
1929 || ((insn ^ MATCH_CSRRCI) & MASK_CSRRCI) == 0)
1930 return INSN_CSRRC;
1931 else
1932 return INSN_NOT_CSR;
1933 }
1934
1935 /* CSRRW and CSRRWI always write CSR. CSRRS, CSRRC, CSRRSI and CSRRCI write
1936 CSR when RS1 isn't zero. The CSR is read only if the [11:10] bits of
1937 CSR address is 0x3. */
1938
1939 static bool
1940 riscv_csr_read_only_check (insn_t insn)
1941 {
1942 int csr = (insn & (OP_MASK_CSR << OP_SH_CSR)) >> OP_SH_CSR;
1943 int rs1 = (insn & (OP_MASK_RS1 << OP_SH_RS1)) >> OP_SH_RS1;
1944 int readonly = (((csr & (0x3 << 10)) >> 10) == 0x3);
1945 enum csr_insn_type csr_insn = riscv_csr_insn_type (insn);
1946
1947 if (readonly
1948 && (((csr_insn == INSN_CSRRS
1949 || csr_insn == INSN_CSRRC)
1950 && rs1 != 0)
1951 || csr_insn == INSN_CSRRW))
1952 return false;
1953
1954 return true;
1955 }
1956
1957 /* Return True if it is a privileged instruction. Otherwise, return FALSE.
1958
1959 uret is actually a N-ext instruction. So it is better to regard it as
1960 an user instruction rather than the priv instruction.
1961
1962 hret is used to return from traps in H-mode. H-mode is removed since
1963 the v1.10 priv spec, but probably be added in the new hypervisor spec.
1964 Therefore, hret should be controlled by the hypervisor spec rather than
1965 priv spec in the future.
1966
1967 dret is defined in the debug spec, so it should be checked in the future,
1968 too. */
1969
1970 static bool
1971 riscv_is_priv_insn (insn_t insn)
1972 {
1973 return (((insn ^ MATCH_SRET) & MASK_SRET) == 0
1974 || ((insn ^ MATCH_MRET) & MASK_MRET) == 0
1975 || ((insn ^ MATCH_SFENCE_VMA) & MASK_SFENCE_VMA) == 0
1976 || ((insn ^ MATCH_WFI) & MASK_WFI) == 0
1977 /* The sfence.vm is dropped in the v1.10 priv specs, but we still need to
1978 check it here to keep the compatible. */
1979 || ((insn ^ MATCH_SFENCE_VM) & MASK_SFENCE_VM) == 0);
1980 }
1981
1982 /* This routine assembles an instruction into its binary format. As a
1983 side effect, it sets the global variable imm_reloc to the type of
1984 relocation to do if one of the operands is an address expression. */
1985
1986 static const char *
1987 riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
1988 bfd_reloc_code_real_type *imm_reloc, htab_t hash)
1989 {
1990 char *s;
1991 const char *args;
1992 char c = 0;
1993 struct riscv_opcode *insn;
1994 char *argsStart;
1995 unsigned int regno;
1996 char save_c = 0;
1997 int argnum;
1998 const struct percent_op_match *p;
1999 const char *error = "unrecognized opcode";
2000 /* Indicate we are assembling instruction with CSR. */
2001 bool insn_with_csr = false;
2002
2003 /* Parse the name of the instruction. Terminate the string if whitespace
2004 is found so that str_hash_find only sees the name part of the string. */
2005 for (s = str; *s != '\0'; ++s)
2006 if (ISSPACE (*s))
2007 {
2008 save_c = *s;
2009 *s++ = '\0';
2010 break;
2011 }
2012
2013 insn = (struct riscv_opcode *) str_hash_find (hash, str);
2014
2015 argsStart = s;
2016 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
2017 {
2018 if ((insn->xlen_requirement != 0) && (xlen != insn->xlen_requirement))
2019 continue;
2020
2021 if (!riscv_multi_subset_supports (insn->insn_class))
2022 continue;
2023
2024 create_insn (ip, insn);
2025 argnum = 1;
2026
2027 imm_expr->X_op = O_absent;
2028 *imm_reloc = BFD_RELOC_UNUSED;
2029 p = percent_op_itype;
2030
2031 for (args = insn->args;; ++args)
2032 {
2033 s += strspn (s, " \t");
2034 switch (*args)
2035 {
2036 case '\0': /* End of args. */
2037 if (insn->pinfo != INSN_MACRO)
2038 {
2039 if (!insn->match_func (insn, ip->insn_opcode))
2040 break;
2041
2042 /* For .insn, insn->match and insn->mask are 0. */
2043 if (riscv_insn_length ((insn->match == 0 && insn->mask == 0)
2044 ? ip->insn_opcode
2045 : insn->match) == 2
2046 && !riscv_opts.rvc)
2047 break;
2048
2049 if (riscv_is_priv_insn (ip->insn_opcode))
2050 explicit_priv_attr = true;
2051
2052 /* Check if we write a read-only CSR by the CSR
2053 instruction. */
2054 if (insn_with_csr
2055 && riscv_opts.csr_check
2056 && !riscv_csr_read_only_check (ip->insn_opcode))
2057 {
2058 /* Restore the character in advance, since we want to
2059 report the detailed warning message here. */
2060 if (save_c)
2061 *(argsStart - 1) = save_c;
2062 as_warn (_("read-only CSR is written `%s'"), str);
2063 insn_with_csr = false;
2064 }
2065 }
2066 if (*s != '\0')
2067 break;
2068 /* Successful assembly. */
2069 error = NULL;
2070 insn_with_csr = false;
2071 goto out;
2072
2073 case 'C': /* RVC */
2074 switch (*++args)
2075 {
2076 case 's': /* RS1 x8-x15. */
2077 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2078 || !(regno >= 8 && regno <= 15))
2079 break;
2080 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2081 continue;
2082 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2083 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2084 || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno)
2085 break;
2086 continue;
2087 case 't': /* RS2 x8-x15. */
2088 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2089 || !(regno >= 8 && regno <= 15))
2090 break;
2091 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2092 continue;
2093 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2094 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2095 || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno)
2096 break;
2097 continue;
2098 case 'U': /* RS1, constrained to equal RD. */
2099 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2100 || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno)
2101 break;
2102 continue;
2103 case 'V': /* RS2 */
2104 if (!reg_lookup (&s, RCLASS_GPR, &regno))
2105 break;
2106 INSERT_OPERAND (CRS2, *ip, regno);
2107 continue;
2108 case 'c': /* RS1, constrained to equal sp. */
2109 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2110 || regno != X_SP)
2111 break;
2112 continue;
2113 case 'z': /* RS2, constrained to equal x0. */
2114 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2115 || regno != 0)
2116 break;
2117 continue;
2118 case '>': /* Shift amount, 0 - (XLEN-1). */
2119 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2120 || imm_expr->X_op != O_constant
2121 || (unsigned long) imm_expr->X_add_number >= xlen)
2122 break;
2123 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2124 rvc_imm_done:
2125 s = expr_end;
2126 imm_expr->X_op = O_absent;
2127 continue;
2128 case '5':
2129 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2130 || imm_expr->X_op != O_constant
2131 || imm_expr->X_add_number < 0
2132 || imm_expr->X_add_number >= 32
2133 || !VALID_CLTYPE_IMM ((valueT) imm_expr->X_add_number))
2134 break;
2135 ip->insn_opcode |= ENCODE_CLTYPE_IMM (imm_expr->X_add_number);
2136 goto rvc_imm_done;
2137 case '6':
2138 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2139 || imm_expr->X_op != O_constant
2140 || imm_expr->X_add_number < 0
2141 || imm_expr->X_add_number >= 64
2142 || !VALID_CSSTYPE_IMM ((valueT) imm_expr->X_add_number))
2143 break;
2144 ip->insn_opcode |= ENCODE_CSSTYPE_IMM (imm_expr->X_add_number);
2145 goto rvc_imm_done;
2146 case '8':
2147 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2148 || imm_expr->X_op != O_constant
2149 || imm_expr->X_add_number < 0
2150 || imm_expr->X_add_number >= 256
2151 || !VALID_CIWTYPE_IMM ((valueT) imm_expr->X_add_number))
2152 break;
2153 ip->insn_opcode |= ENCODE_CIWTYPE_IMM (imm_expr->X_add_number);
2154 goto rvc_imm_done;
2155 case 'j':
2156 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2157 || imm_expr->X_op != O_constant
2158 || imm_expr->X_add_number == 0
2159 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
2160 break;
2161 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2162 goto rvc_imm_done;
2163 case 'k':
2164 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2165 continue;
2166 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2167 || imm_expr->X_op != O_constant
2168 || !VALID_CLTYPE_LW_IMM ((valueT) imm_expr->X_add_number))
2169 break;
2170 ip->insn_opcode |= ENCODE_CLTYPE_LW_IMM (imm_expr->X_add_number);
2171 goto rvc_imm_done;
2172 case 'l':
2173 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2174 continue;
2175 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2176 || imm_expr->X_op != O_constant
2177 || !VALID_CLTYPE_LD_IMM ((valueT) imm_expr->X_add_number))
2178 break;
2179 ip->insn_opcode |= ENCODE_CLTYPE_LD_IMM (imm_expr->X_add_number);
2180 goto rvc_imm_done;
2181 case 'm':
2182 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2183 continue;
2184 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2185 || imm_expr->X_op != O_constant
2186 || !VALID_CITYPE_LWSP_IMM ((valueT) imm_expr->X_add_number))
2187 break;
2188 ip->insn_opcode |=
2189 ENCODE_CITYPE_LWSP_IMM (imm_expr->X_add_number);
2190 goto rvc_imm_done;
2191 case 'n':
2192 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2193 continue;
2194 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2195 || imm_expr->X_op != O_constant
2196 || !VALID_CITYPE_LDSP_IMM ((valueT) imm_expr->X_add_number))
2197 break;
2198 ip->insn_opcode |=
2199 ENCODE_CITYPE_LDSP_IMM (imm_expr->X_add_number);
2200 goto rvc_imm_done;
2201 case 'o':
2202 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2203 || imm_expr->X_op != O_constant
2204 /* C.addiw, c.li, and c.andi allow zero immediate.
2205 C.addi allows zero immediate as hint. Otherwise this
2206 is same as 'j'. */
2207 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
2208 break;
2209 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2210 goto rvc_imm_done;
2211 case 'K':
2212 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2213 || imm_expr->X_op != O_constant
2214 || imm_expr->X_add_number == 0
2215 || !VALID_CIWTYPE_ADDI4SPN_IMM ((valueT) imm_expr->X_add_number))
2216 break;
2217 ip->insn_opcode |=
2218 ENCODE_CIWTYPE_ADDI4SPN_IMM (imm_expr->X_add_number);
2219 goto rvc_imm_done;
2220 case 'L':
2221 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2222 || imm_expr->X_op != O_constant
2223 || !VALID_CITYPE_ADDI16SP_IMM ((valueT) imm_expr->X_add_number))
2224 break;
2225 ip->insn_opcode |=
2226 ENCODE_CITYPE_ADDI16SP_IMM (imm_expr->X_add_number);
2227 goto rvc_imm_done;
2228 case 'M':
2229 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2230 continue;
2231 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2232 || imm_expr->X_op != O_constant
2233 || !VALID_CSSTYPE_SWSP_IMM ((valueT) imm_expr->X_add_number))
2234 break;
2235 ip->insn_opcode |=
2236 ENCODE_CSSTYPE_SWSP_IMM (imm_expr->X_add_number);
2237 goto rvc_imm_done;
2238 case 'N':
2239 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2240 continue;
2241 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2242 || imm_expr->X_op != O_constant
2243 || !VALID_CSSTYPE_SDSP_IMM ((valueT) imm_expr->X_add_number))
2244 break;
2245 ip->insn_opcode |=
2246 ENCODE_CSSTYPE_SDSP_IMM (imm_expr->X_add_number);
2247 goto rvc_imm_done;
2248 case 'u':
2249 p = percent_op_utype;
2250 if (my_getSmallExpression (imm_expr, imm_reloc, s, p))
2251 break;
2252 rvc_lui:
2253 if (imm_expr->X_op != O_constant
2254 || imm_expr->X_add_number <= 0
2255 || imm_expr->X_add_number >= RISCV_BIGIMM_REACH
2256 || (imm_expr->X_add_number >= RISCV_RVC_IMM_REACH / 2
2257 && (imm_expr->X_add_number <
2258 RISCV_BIGIMM_REACH - RISCV_RVC_IMM_REACH / 2)))
2259 break;
2260 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2261 goto rvc_imm_done;
2262 case 'v':
2263 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2264 || (imm_expr->X_add_number & (RISCV_IMM_REACH - 1))
2265 || ((int32_t)imm_expr->X_add_number
2266 != imm_expr->X_add_number))
2267 break;
2268 imm_expr->X_add_number =
2269 ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS;
2270 goto rvc_lui;
2271 case 'p':
2272 goto branch;
2273 case 'a':
2274 goto jump;
2275 case 'S': /* Floating-point RS1 x8-x15. */
2276 if (!reg_lookup (&s, RCLASS_FPR, &regno)
2277 || !(regno >= 8 && regno <= 15))
2278 break;
2279 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2280 continue;
2281 case 'D': /* Floating-point RS2 x8-x15. */
2282 if (!reg_lookup (&s, RCLASS_FPR, &regno)
2283 || !(regno >= 8 && regno <= 15))
2284 break;
2285 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2286 continue;
2287 case 'T': /* Floating-point RS2. */
2288 if (!reg_lookup (&s, RCLASS_FPR, &regno))
2289 break;
2290 INSERT_OPERAND (CRS2, *ip, regno);
2291 continue;
2292 case 'F':
2293 switch (*++args)
2294 {
2295 case '6':
2296 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2297 || imm_expr->X_op != O_constant
2298 || imm_expr->X_add_number < 0
2299 || imm_expr->X_add_number >= 64)
2300 {
2301 as_bad (_("bad value for compressed funct6 "
2302 "field, value must be 0...64"));
2303 break;
2304 }
2305 INSERT_OPERAND (CFUNCT6, *ip, imm_expr->X_add_number);
2306 imm_expr->X_op = O_absent;
2307 s = expr_end;
2308 continue;
2309
2310 case '4':
2311 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2312 || imm_expr->X_op != O_constant
2313 || imm_expr->X_add_number < 0
2314 || imm_expr->X_add_number >= 16)
2315 {
2316 as_bad (_("bad value for compressed funct4 "
2317 "field, value must be 0...15"));
2318 break;
2319 }
2320 INSERT_OPERAND (CFUNCT4, *ip, imm_expr->X_add_number);
2321 imm_expr->X_op = O_absent;
2322 s = expr_end;
2323 continue;
2324
2325 case '3':
2326 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2327 || imm_expr->X_op != O_constant
2328 || imm_expr->X_add_number < 0
2329 || imm_expr->X_add_number >= 8)
2330 {
2331 as_bad (_("bad value for compressed funct3 "
2332 "field, value must be 0...7"));
2333 break;
2334 }
2335 INSERT_OPERAND (CFUNCT3, *ip, imm_expr->X_add_number);
2336 imm_expr->X_op = O_absent;
2337 s = expr_end;
2338 continue;
2339
2340 case '2':
2341 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2342 || imm_expr->X_op != O_constant
2343 || imm_expr->X_add_number < 0
2344 || imm_expr->X_add_number >= 4)
2345 {
2346 as_bad (_("bad value for compressed funct2 "
2347 "field, value must be 0...3"));
2348 break;
2349 }
2350 INSERT_OPERAND (CFUNCT2, *ip, imm_expr->X_add_number);
2351 imm_expr->X_op = O_absent;
2352 s = expr_end;
2353 continue;
2354
2355 default:
2356 as_bad (_("internal: unknown compressed funct "
2357 "field specifier `CF%c'"), *args);
2358 }
2359 break;
2360
2361 default:
2362 as_bad (_("internal: unknown compressed field "
2363 "specifier `C%c'"), *args);
2364 }
2365 break;
2366
2367 case ',':
2368 ++argnum;
2369 if (*s++ == *args)
2370 continue;
2371 s--;
2372 break;
2373
2374 case '(':
2375 case ')':
2376 case '[':
2377 case ']':
2378 if (*s++ == *args)
2379 continue;
2380 break;
2381
2382 case '<': /* Shift amount, 0 - 31. */
2383 my_getExpression (imm_expr, s);
2384 check_absolute_expr (ip, imm_expr, false);
2385 if ((unsigned long) imm_expr->X_add_number > 31)
2386 as_bad (_("improper shift amount (%lu)"),
2387 (unsigned long) imm_expr->X_add_number);
2388 INSERT_OPERAND (SHAMTW, *ip, imm_expr->X_add_number);
2389 imm_expr->X_op = O_absent;
2390 s = expr_end;
2391 continue;
2392
2393 case '>': /* Shift amount, 0 - (XLEN-1). */
2394 my_getExpression (imm_expr, s);
2395 check_absolute_expr (ip, imm_expr, false);
2396 if ((unsigned long) imm_expr->X_add_number >= xlen)
2397 as_bad (_("improper shift amount (%lu)"),
2398 (unsigned long) imm_expr->X_add_number);
2399 INSERT_OPERAND (SHAMT, *ip, imm_expr->X_add_number);
2400 imm_expr->X_op = O_absent;
2401 s = expr_end;
2402 continue;
2403
2404 case 'Z': /* CSRRxI immediate. */
2405 my_getExpression (imm_expr, s);
2406 check_absolute_expr (ip, imm_expr, false);
2407 if ((unsigned long) imm_expr->X_add_number > 31)
2408 as_bad (_("improper CSRxI immediate (%lu)"),
2409 (unsigned long) imm_expr->X_add_number);
2410 INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number);
2411 imm_expr->X_op = O_absent;
2412 s = expr_end;
2413 continue;
2414
2415 case 'E': /* Control register. */
2416 insn_with_csr = true;
2417 explicit_priv_attr = true;
2418 if (reg_lookup (&s, RCLASS_CSR, &regno))
2419 INSERT_OPERAND (CSR, *ip, regno);
2420 else
2421 {
2422 my_getExpression (imm_expr, s);
2423 check_absolute_expr (ip, imm_expr, true);
2424 if ((unsigned long) imm_expr->X_add_number > 0xfff)
2425 as_bad (_("improper CSR address (%lu)"),
2426 (unsigned long) imm_expr->X_add_number);
2427 INSERT_OPERAND (CSR, *ip, imm_expr->X_add_number);
2428 imm_expr->X_op = O_absent;
2429 s = expr_end;
2430 }
2431 continue;
2432
2433 case 'm': /* Rounding mode. */
2434 if (arg_lookup (&s, riscv_rm, ARRAY_SIZE (riscv_rm), &regno))
2435 {
2436 INSERT_OPERAND (RM, *ip, regno);
2437 continue;
2438 }
2439 break;
2440
2441 case 'P':
2442 case 'Q': /* Fence predecessor/successor. */
2443 if (arg_lookup (&s, riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ),
2444 &regno))
2445 {
2446 if (*args == 'P')
2447 INSERT_OPERAND (PRED, *ip, regno);
2448 else
2449 INSERT_OPERAND (SUCC, *ip, regno);
2450 continue;
2451 }
2452 break;
2453
2454 case 'd': /* Destination register. */
2455 case 's': /* Source register. */
2456 case 't': /* Target register. */
2457 case 'r': /* RS3 */
2458 if (reg_lookup (&s, RCLASS_GPR, &regno))
2459 {
2460 c = *args;
2461 if (*s == ' ')
2462 ++s;
2463
2464 /* Now that we have assembled one operand, we use the args
2465 string to figure out where it goes in the instruction. */
2466 switch (c)
2467 {
2468 case 's':
2469 INSERT_OPERAND (RS1, *ip, regno);
2470 break;
2471 case 'd':
2472 INSERT_OPERAND (RD, *ip, regno);
2473 break;
2474 case 't':
2475 INSERT_OPERAND (RS2, *ip, regno);
2476 break;
2477 case 'r':
2478 INSERT_OPERAND (RS3, *ip, regno);
2479 break;
2480 }
2481 continue;
2482 }
2483 break;
2484
2485 case 'D': /* Floating point RD. */
2486 case 'S': /* Floating point RS1. */
2487 case 'T': /* Floating point RS2. */
2488 case 'U': /* Floating point RS1 and RS2. */
2489 case 'R': /* Floating point RS3. */
2490 if (reg_lookup (&s, RCLASS_FPR, &regno))
2491 {
2492 c = *args;
2493 if (*s == ' ')
2494 ++s;
2495 switch (c)
2496 {
2497 case 'D':
2498 INSERT_OPERAND (RD, *ip, regno);
2499 break;
2500 case 'S':
2501 INSERT_OPERAND (RS1, *ip, regno);
2502 break;
2503 case 'U':
2504 INSERT_OPERAND (RS1, *ip, regno);
2505 /* Fall through. */
2506 case 'T':
2507 INSERT_OPERAND (RS2, *ip, regno);
2508 break;
2509 case 'R':
2510 INSERT_OPERAND (RS3, *ip, regno);
2511 break;
2512 }
2513 continue;
2514 }
2515 break;
2516
2517 case 'I':
2518 my_getExpression (imm_expr, s);
2519 if (imm_expr->X_op != O_big
2520 && imm_expr->X_op != O_constant)
2521 break;
2522 normalize_constant_expr (imm_expr);
2523 s = expr_end;
2524 continue;
2525
2526 case 'A':
2527 my_getExpression (imm_expr, s);
2528 normalize_constant_expr (imm_expr);
2529 /* The 'A' format specifier must be a symbol. */
2530 if (imm_expr->X_op != O_symbol)
2531 break;
2532 *imm_reloc = BFD_RELOC_32;
2533 s = expr_end;
2534 continue;
2535
2536 case 'B':
2537 my_getExpression (imm_expr, s);
2538 normalize_constant_expr (imm_expr);
2539 /* The 'B' format specifier must be a symbol or a constant. */
2540 if (imm_expr->X_op != O_symbol && imm_expr->X_op != O_constant)
2541 break;
2542 if (imm_expr->X_op == O_symbol)
2543 *imm_reloc = BFD_RELOC_32;
2544 s = expr_end;
2545 continue;
2546
2547 case 'j': /* Sign-extended immediate. */
2548 p = percent_op_itype;
2549 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
2550 goto alu_op;
2551 case 'q': /* Store displacement. */
2552 p = percent_op_stype;
2553 *imm_reloc = BFD_RELOC_RISCV_LO12_S;
2554 goto load_store;
2555 case 'o': /* Load displacement. */
2556 p = percent_op_itype;
2557 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
2558 goto load_store;
2559 case '1':
2560 /* This is used for TLS, where the fourth operand is
2561 %tprel_add, to get a relocation applied to an add
2562 instruction, for relaxation to use. */
2563 p = percent_op_rtype;
2564 goto alu_op;
2565 case '0': /* AMO displacement, which must be zero. */
2566 p = percent_op_null;
2567 load_store:
2568 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2569 continue;
2570 alu_op:
2571 /* If this value won't fit into a 16 bit offset, then go
2572 find a macro that will generate the 32 bit offset
2573 code pattern. */
2574 if (!my_getSmallExpression (imm_expr, imm_reloc, s, p))
2575 {
2576 normalize_constant_expr (imm_expr);
2577 if (imm_expr->X_op != O_constant
2578 || (*args == '0' && imm_expr->X_add_number != 0)
2579 || (*args == '1')
2580 || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2
2581 || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2)
2582 break;
2583 }
2584 s = expr_end;
2585 continue;
2586
2587 case 'p': /* PC-relative offset. */
2588 branch:
2589 *imm_reloc = BFD_RELOC_12_PCREL;
2590 my_getExpression (imm_expr, s);
2591 s = expr_end;
2592 continue;
2593
2594 case 'u': /* Upper 20 bits. */
2595 p = percent_op_utype;
2596 if (!my_getSmallExpression (imm_expr, imm_reloc, s, p))
2597 {
2598 if (imm_expr->X_op != O_constant)
2599 break;
2600
2601 if (imm_expr->X_add_number < 0
2602 || imm_expr->X_add_number >= (signed)RISCV_BIGIMM_REACH)
2603 as_bad (_("lui expression not in range 0..1048575"));
2604
2605 *imm_reloc = BFD_RELOC_RISCV_HI20;
2606 imm_expr->X_add_number <<= RISCV_IMM_BITS;
2607 }
2608 s = expr_end;
2609 continue;
2610
2611 case 'a': /* 20-bit PC-relative offset. */
2612 jump:
2613 my_getExpression (imm_expr, s);
2614 s = expr_end;
2615 *imm_reloc = BFD_RELOC_RISCV_JMP;
2616 continue;
2617
2618 case 'c':
2619 my_getExpression (imm_expr, s);
2620 s = expr_end;
2621 if (strcmp (s, "@plt") == 0)
2622 {
2623 *imm_reloc = BFD_RELOC_RISCV_CALL_PLT;
2624 s += 4;
2625 }
2626 else
2627 *imm_reloc = BFD_RELOC_RISCV_CALL;
2628 continue;
2629
2630 case 'O':
2631 switch (*++args)
2632 {
2633 case '4':
2634 if (my_getOpcodeExpression (imm_expr, imm_reloc, s, p)
2635 || imm_expr->X_op != O_constant
2636 || imm_expr->X_add_number < 0
2637 || imm_expr->X_add_number >= 128
2638 || (imm_expr->X_add_number & 0x3) != 3)
2639 {
2640 as_bad (_("bad value for opcode field, "
2641 "value must be 0...127 and "
2642 "lower 2 bits must be 0x3"));
2643 break;
2644 }
2645 INSERT_OPERAND (OP, *ip, imm_expr->X_add_number);
2646 imm_expr->X_op = O_absent;
2647 s = expr_end;
2648 continue;
2649
2650 case '2':
2651 if (my_getOpcodeExpression (imm_expr, imm_reloc, s, p)
2652 || imm_expr->X_op != O_constant
2653 || imm_expr->X_add_number < 0
2654 || imm_expr->X_add_number >= 3)
2655 {
2656 as_bad (_("bad value for opcode field, "
2657 "value must be 0...2"));
2658 break;
2659 }
2660 INSERT_OPERAND (OP2, *ip, imm_expr->X_add_number);
2661 imm_expr->X_op = O_absent;
2662 s = expr_end;
2663 continue;
2664
2665 default:
2666 as_bad (_("internal: unknown opcode field "
2667 "specifier `O%c'"), *args);
2668 }
2669 break;
2670
2671 case 'F':
2672 switch (*++args)
2673 {
2674 case '7':
2675 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2676 || imm_expr->X_op != O_constant
2677 || imm_expr->X_add_number < 0
2678 || imm_expr->X_add_number >= 128)
2679 {
2680 as_bad (_("bad value for funct7 field, "
2681 "value must be 0...127"));
2682 break;
2683 }
2684 INSERT_OPERAND (FUNCT7, *ip, imm_expr->X_add_number);
2685 imm_expr->X_op = O_absent;
2686 s = expr_end;
2687 continue;
2688
2689 case '3':
2690 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2691 || imm_expr->X_op != O_constant
2692 || imm_expr->X_add_number < 0
2693 || imm_expr->X_add_number >= 8)
2694 {
2695 as_bad (_("bad value for funct3 field, "
2696 "value must be 0...7"));
2697 break;
2698 }
2699 INSERT_OPERAND (FUNCT3, *ip, imm_expr->X_add_number);
2700 imm_expr->X_op = O_absent;
2701 s = expr_end;
2702 continue;
2703
2704 case '2':
2705 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2706 || imm_expr->X_op != O_constant
2707 || imm_expr->X_add_number < 0
2708 || imm_expr->X_add_number >= 4)
2709 {
2710 as_bad (_("bad value for funct2 field, "
2711 "value must be 0...3"));
2712 break;
2713 }
2714 INSERT_OPERAND (FUNCT2, *ip, imm_expr->X_add_number);
2715 imm_expr->X_op = O_absent;
2716 s = expr_end;
2717 continue;
2718
2719 default:
2720 as_bad (_("internal: unknown funct field "
2721 "specifier `F%c'\n"), *args);
2722 }
2723 break;
2724
2725 case 'z':
2726 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2727 || imm_expr->X_op != O_constant
2728 || imm_expr->X_add_number != 0)
2729 break;
2730 s = expr_end;
2731 imm_expr->X_op = O_absent;
2732 continue;
2733
2734 default:
2735 as_fatal (_("internal: unknown argument type `%c'"), *args);
2736 }
2737 break;
2738 }
2739 s = argsStart;
2740 error = _("illegal operands");
2741 insn_with_csr = false;
2742 }
2743
2744 out:
2745 /* Restore the character we might have clobbered above. */
2746 if (save_c)
2747 *(argsStart - 1) = save_c;
2748
2749 return error;
2750 }
2751
2752 void
2753 md_assemble (char *str)
2754 {
2755 struct riscv_cl_insn insn;
2756 expressionS imm_expr;
2757 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
2758
2759 /* The architecture and privileged elf attributes should be set
2760 before assembling. */
2761 if (!start_assemble)
2762 {
2763 start_assemble = true;
2764
2765 riscv_set_abi_by_arch ();
2766 if (!riscv_set_default_priv_spec (NULL))
2767 return;
2768 }
2769
2770 const char *error = riscv_ip (str, &insn, &imm_expr, &imm_reloc, op_hash);
2771
2772 if (error)
2773 {
2774 as_bad ("%s `%s'", error, str);
2775 return;
2776 }
2777
2778 if (insn.insn_mo->pinfo == INSN_MACRO)
2779 macro (&insn, &imm_expr, &imm_reloc);
2780 else
2781 append_insn (&insn, &imm_expr, imm_reloc);
2782 }
2783
2784 const char *
2785 md_atof (int type, char *litP, int *sizeP)
2786 {
2787 return ieee_md_atof (type, litP, sizeP, TARGET_BYTES_BIG_ENDIAN);
2788 }
2789
2790 void
2791 md_number_to_chars (char *buf, valueT val, int n)
2792 {
2793 if (target_big_endian)
2794 number_to_chars_bigendian (buf, val, n);
2795 else
2796 number_to_chars_littleendian (buf, val, n);
2797 }
2798
2799 const char *md_shortopts = "O::g::G:";
2800
2801 enum options
2802 {
2803 OPTION_MARCH = OPTION_MD_BASE,
2804 OPTION_PIC,
2805 OPTION_NO_PIC,
2806 OPTION_MABI,
2807 OPTION_RELAX,
2808 OPTION_NO_RELAX,
2809 OPTION_ARCH_ATTR,
2810 OPTION_NO_ARCH_ATTR,
2811 OPTION_CSR_CHECK,
2812 OPTION_NO_CSR_CHECK,
2813 OPTION_MISA_SPEC,
2814 OPTION_MPRIV_SPEC,
2815 OPTION_BIG_ENDIAN,
2816 OPTION_LITTLE_ENDIAN,
2817 OPTION_END_OF_ENUM
2818 };
2819
2820 struct option md_longopts[] =
2821 {
2822 {"march", required_argument, NULL, OPTION_MARCH},
2823 {"fPIC", no_argument, NULL, OPTION_PIC},
2824 {"fpic", no_argument, NULL, OPTION_PIC},
2825 {"fno-pic", no_argument, NULL, OPTION_NO_PIC},
2826 {"mabi", required_argument, NULL, OPTION_MABI},
2827 {"mrelax", no_argument, NULL, OPTION_RELAX},
2828 {"mno-relax", no_argument, NULL, OPTION_NO_RELAX},
2829 {"march-attr", no_argument, NULL, OPTION_ARCH_ATTR},
2830 {"mno-arch-attr", no_argument, NULL, OPTION_NO_ARCH_ATTR},
2831 {"mcsr-check", no_argument, NULL, OPTION_CSR_CHECK},
2832 {"mno-csr-check", no_argument, NULL, OPTION_NO_CSR_CHECK},
2833 {"misa-spec", required_argument, NULL, OPTION_MISA_SPEC},
2834 {"mpriv-spec", required_argument, NULL, OPTION_MPRIV_SPEC},
2835 {"mbig-endian", no_argument, NULL, OPTION_BIG_ENDIAN},
2836 {"mlittle-endian", no_argument, NULL, OPTION_LITTLE_ENDIAN},
2837
2838 {NULL, no_argument, NULL, 0}
2839 };
2840 size_t md_longopts_size = sizeof (md_longopts);
2841
2842 int
2843 md_parse_option (int c, const char *arg)
2844 {
2845 switch (c)
2846 {
2847 case OPTION_MARCH:
2848 default_arch_with_ext = arg;
2849 break;
2850
2851 case OPTION_NO_PIC:
2852 riscv_opts.pic = false;
2853 break;
2854
2855 case OPTION_PIC:
2856 riscv_opts.pic = true;
2857 break;
2858
2859 case OPTION_MABI:
2860 if (strcmp (arg, "ilp32") == 0)
2861 riscv_set_abi (32, FLOAT_ABI_SOFT, false);
2862 else if (strcmp (arg, "ilp32e") == 0)
2863 riscv_set_abi (32, FLOAT_ABI_SOFT, true);
2864 else if (strcmp (arg, "ilp32f") == 0)
2865 riscv_set_abi (32, FLOAT_ABI_SINGLE, false);
2866 else if (strcmp (arg, "ilp32d") == 0)
2867 riscv_set_abi (32, FLOAT_ABI_DOUBLE, false);
2868 else if (strcmp (arg, "ilp32q") == 0)
2869 riscv_set_abi (32, FLOAT_ABI_QUAD, false);
2870 else if (strcmp (arg, "lp64") == 0)
2871 riscv_set_abi (64, FLOAT_ABI_SOFT, false);
2872 else if (strcmp (arg, "lp64f") == 0)
2873 riscv_set_abi (64, FLOAT_ABI_SINGLE, false);
2874 else if (strcmp (arg, "lp64d") == 0)
2875 riscv_set_abi (64, FLOAT_ABI_DOUBLE, false);
2876 else if (strcmp (arg, "lp64q") == 0)
2877 riscv_set_abi (64, FLOAT_ABI_QUAD, false);
2878 else
2879 return 0;
2880 explicit_mabi = true;
2881 break;
2882
2883 case OPTION_RELAX:
2884 riscv_opts.relax = true;
2885 break;
2886
2887 case OPTION_NO_RELAX:
2888 riscv_opts.relax = false;
2889 break;
2890
2891 case OPTION_ARCH_ATTR:
2892 riscv_opts.arch_attr = true;
2893 break;
2894
2895 case OPTION_NO_ARCH_ATTR:
2896 riscv_opts.arch_attr = false;
2897 break;
2898
2899 case OPTION_CSR_CHECK:
2900 riscv_opts.csr_check = true;
2901 break;
2902
2903 case OPTION_NO_CSR_CHECK:
2904 riscv_opts.csr_check = false;
2905 break;
2906
2907 case OPTION_MISA_SPEC:
2908 return riscv_set_default_isa_spec (arg);
2909
2910 case OPTION_MPRIV_SPEC:
2911 return riscv_set_default_priv_spec (arg);
2912
2913 case OPTION_BIG_ENDIAN:
2914 target_big_endian = 1;
2915 break;
2916
2917 case OPTION_LITTLE_ENDIAN:
2918 target_big_endian = 0;
2919 break;
2920
2921 default:
2922 return 0;
2923 }
2924
2925 return 1;
2926 }
2927
2928 void
2929 riscv_after_parse_args (void)
2930 {
2931 /* The --with-arch is optional for now, so we still need to set the xlen
2932 according to the default_arch, which is set by the --target. */
2933 if (xlen == 0)
2934 {
2935 if (strcmp (default_arch, "riscv32") == 0)
2936 xlen = 32;
2937 else if (strcmp (default_arch, "riscv64") == 0)
2938 xlen = 64;
2939 else
2940 as_bad ("unknown default architecture `%s'", default_arch);
2941 }
2942 if (default_arch_with_ext == NULL)
2943 default_arch_with_ext = xlen == 64 ? "rv64g" : "rv32g";
2944
2945 /* Initialize the hash table for extensions with default version. */
2946 ext_version_hash = init_ext_version_hash ();
2947
2948 /* Set default specs. */
2949 if (default_isa_spec == ISA_SPEC_CLASS_NONE)
2950 riscv_set_default_isa_spec (DEFAULT_RISCV_ISA_SPEC);
2951 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
2952 riscv_set_default_priv_spec (DEFAULT_RISCV_PRIV_SPEC);
2953
2954 riscv_set_arch (default_arch_with_ext);
2955
2956 /* If the CIE to be produced has not been overridden on the command line,
2957 then produce version 3 by default. This allows us to use the full
2958 range of registers in a .cfi_return_column directive. */
2959 if (flag_dwarf_cie_version == -1)
2960 flag_dwarf_cie_version = 3;
2961 }
2962
2963 long
2964 md_pcrel_from (fixS *fixP)
2965 {
2966 return fixP->fx_where + fixP->fx_frag->fr_address;
2967 }
2968
2969 /* Apply a fixup to the object file. */
2970
2971 void
2972 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
2973 {
2974 unsigned int subtype;
2975 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
2976 bool relaxable = false;
2977 offsetT loc;
2978 segT sub_segment;
2979
2980 /* Remember value for tc_gen_reloc. */
2981 fixP->fx_addnumber = *valP;
2982
2983 switch (fixP->fx_r_type)
2984 {
2985 case BFD_RELOC_RISCV_HI20:
2986 case BFD_RELOC_RISCV_LO12_I:
2987 case BFD_RELOC_RISCV_LO12_S:
2988 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
2989 | bfd_getl32 (buf), buf);
2990 if (fixP->fx_addsy == NULL)
2991 fixP->fx_done = true;
2992 relaxable = true;
2993 break;
2994
2995 case BFD_RELOC_RISCV_GOT_HI20:
2996 case BFD_RELOC_RISCV_ADD8:
2997 case BFD_RELOC_RISCV_ADD16:
2998 case BFD_RELOC_RISCV_ADD32:
2999 case BFD_RELOC_RISCV_ADD64:
3000 case BFD_RELOC_RISCV_SUB6:
3001 case BFD_RELOC_RISCV_SUB8:
3002 case BFD_RELOC_RISCV_SUB16:
3003 case BFD_RELOC_RISCV_SUB32:
3004 case BFD_RELOC_RISCV_SUB64:
3005 case BFD_RELOC_RISCV_RELAX:
3006 break;
3007
3008 case BFD_RELOC_RISCV_TPREL_HI20:
3009 case BFD_RELOC_RISCV_TPREL_LO12_I:
3010 case BFD_RELOC_RISCV_TPREL_LO12_S:
3011 case BFD_RELOC_RISCV_TPREL_ADD:
3012 relaxable = true;
3013 /* Fall through. */
3014
3015 case BFD_RELOC_RISCV_TLS_GOT_HI20:
3016 case BFD_RELOC_RISCV_TLS_GD_HI20:
3017 case BFD_RELOC_RISCV_TLS_DTPREL32:
3018 case BFD_RELOC_RISCV_TLS_DTPREL64:
3019 if (fixP->fx_addsy != NULL)
3020 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3021 else
3022 as_bad_where (fixP->fx_file, fixP->fx_line,
3023 _("TLS relocation against a constant"));
3024 break;
3025
3026 case BFD_RELOC_32:
3027 /* Use pc-relative relocation for FDE initial location.
3028 The symbol address in .eh_frame may be adjusted in
3029 _bfd_elf_discard_section_eh_frame, and the content of
3030 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
3031 Therefore, we cannot insert a relocation whose addend symbol is
3032 in .eh_frame. Othrewise, the value may be adjusted twice. */
3033 if (fixP->fx_addsy && fixP->fx_subsy
3034 && (sub_segment = S_GET_SEGMENT (fixP->fx_subsy))
3035 && strcmp (sub_segment->name, ".eh_frame") == 0
3036 && S_GET_VALUE (fixP->fx_subsy)
3037 == fixP->fx_frag->fr_address + fixP->fx_where)
3038 {
3039 fixP->fx_r_type = BFD_RELOC_RISCV_32_PCREL;
3040 fixP->fx_subsy = NULL;
3041 break;
3042 }
3043 /* Fall through. */
3044 case BFD_RELOC_64:
3045 case BFD_RELOC_16:
3046 case BFD_RELOC_8:
3047 case BFD_RELOC_RISCV_CFA:
3048 if (fixP->fx_addsy && fixP->fx_subsy)
3049 {
3050 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
3051 fixP->fx_next->fx_addsy = fixP->fx_subsy;
3052 fixP->fx_next->fx_subsy = NULL;
3053 fixP->fx_next->fx_offset = 0;
3054 fixP->fx_subsy = NULL;
3055
3056 switch (fixP->fx_r_type)
3057 {
3058 case BFD_RELOC_64:
3059 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
3060 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
3061 break;
3062
3063 case BFD_RELOC_32:
3064 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
3065 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
3066 break;
3067
3068 case BFD_RELOC_16:
3069 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
3070 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
3071 break;
3072
3073 case BFD_RELOC_8:
3074 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
3075 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
3076 break;
3077
3078 case BFD_RELOC_RISCV_CFA:
3079 /* Load the byte to get the subtype. */
3080 subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]);
3081 loc = fixP->fx_frag->fr_fix - (subtype & 7);
3082 switch (subtype)
3083 {
3084 case DW_CFA_advance_loc1:
3085 fixP->fx_where = loc + 1;
3086 fixP->fx_next->fx_where = loc + 1;
3087 fixP->fx_r_type = BFD_RELOC_RISCV_SET8;
3088 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
3089 break;
3090
3091 case DW_CFA_advance_loc2:
3092 fixP->fx_size = 2;
3093 fixP->fx_next->fx_size = 2;
3094 fixP->fx_where = loc + 1;
3095 fixP->fx_next->fx_where = loc + 1;
3096 fixP->fx_r_type = BFD_RELOC_RISCV_SET16;
3097 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
3098 break;
3099
3100 case DW_CFA_advance_loc4:
3101 fixP->fx_size = 4;
3102 fixP->fx_next->fx_size = 4;
3103 fixP->fx_where = loc;
3104 fixP->fx_next->fx_where = loc;
3105 fixP->fx_r_type = BFD_RELOC_RISCV_SET32;
3106 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
3107 break;
3108
3109 default:
3110 if (subtype < 0x80 && (subtype & 0x40))
3111 {
3112 /* DW_CFA_advance_loc */
3113 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
3114 fixP->fx_next->fx_frag = fixP->fx_frag;
3115 fixP->fx_r_type = BFD_RELOC_RISCV_SET6;
3116 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6;
3117 }
3118 else
3119 as_fatal (_("internal: bad CFA value #%d"), subtype);
3120 break;
3121 }
3122 break;
3123
3124 default:
3125 /* This case is unreachable. */
3126 abort ();
3127 }
3128 }
3129 /* Fall through. */
3130
3131 case BFD_RELOC_RVA:
3132 /* If we are deleting this reloc entry, we must fill in the
3133 value now. This can happen if we have a .word which is not
3134 resolved when it appears but is later defined. */
3135 if (fixP->fx_addsy == NULL)
3136 {
3137 gas_assert (fixP->fx_size <= sizeof (valueT));
3138 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
3139 fixP->fx_done = 1;
3140 }
3141 break;
3142
3143 case BFD_RELOC_RISCV_JMP:
3144 if (fixP->fx_addsy)
3145 {
3146 /* Fill in a tentative value to improve objdump readability. */
3147 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
3148 bfd_vma delta = target - md_pcrel_from (fixP);
3149 bfd_putl32 (bfd_getl32 (buf) | ENCODE_JTYPE_IMM (delta), buf);
3150 }
3151 break;
3152
3153 case BFD_RELOC_12_PCREL:
3154 if (fixP->fx_addsy)
3155 {
3156 /* Fill in a tentative value to improve objdump readability. */
3157 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
3158 bfd_vma delta = target - md_pcrel_from (fixP);
3159 bfd_putl32 (bfd_getl32 (buf) | ENCODE_BTYPE_IMM (delta), buf);
3160 }
3161 break;
3162
3163 case BFD_RELOC_RISCV_RVC_BRANCH:
3164 if (fixP->fx_addsy)
3165 {
3166 /* Fill in a tentative value to improve objdump readability. */
3167 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
3168 bfd_vma delta = target - md_pcrel_from (fixP);
3169 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CBTYPE_IMM (delta), buf);
3170 }
3171 break;
3172
3173 case BFD_RELOC_RISCV_RVC_JUMP:
3174 if (fixP->fx_addsy)
3175 {
3176 /* Fill in a tentative value to improve objdump readability. */
3177 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
3178 bfd_vma delta = target - md_pcrel_from (fixP);
3179 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CJTYPE_IMM (delta), buf);
3180 }
3181 break;
3182
3183 case BFD_RELOC_RISCV_CALL:
3184 case BFD_RELOC_RISCV_CALL_PLT:
3185 relaxable = true;
3186 break;
3187
3188 case BFD_RELOC_RISCV_PCREL_HI20:
3189 case BFD_RELOC_RISCV_PCREL_LO12_S:
3190 case BFD_RELOC_RISCV_PCREL_LO12_I:
3191 relaxable = riscv_opts.relax;
3192 break;
3193
3194 case BFD_RELOC_RISCV_ALIGN:
3195 break;
3196
3197 default:
3198 /* We ignore generic BFD relocations we don't know about. */
3199 if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL)
3200 as_fatal (_("internal: bad relocation #%d"), fixP->fx_r_type);
3201 }
3202
3203 if (fixP->fx_subsy != NULL)
3204 as_bad_where (fixP->fx_file, fixP->fx_line,
3205 _("unsupported symbol subtraction"));
3206
3207 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
3208 if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL)
3209 {
3210 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
3211 fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL;
3212 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX;
3213 fixP->fx_next->fx_size = 0;
3214 }
3215 }
3216
3217 /* Because the value of .cfi_remember_state may changed after relaxation,
3218 we insert a fix to relocate it again in link-time. */
3219
3220 void
3221 riscv_pre_output_hook (void)
3222 {
3223 const frchainS *frch;
3224 segT s;
3225
3226 /* Save the current segment info. */
3227 segT seg = now_seg;
3228 subsegT subseg = now_subseg;
3229
3230 for (s = stdoutput->sections; s; s = s->next)
3231 for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next)
3232 {
3233 fragS *frag;
3234
3235 for (frag = frch->frch_root; frag; frag = frag->fr_next)
3236 {
3237 if (frag->fr_type == rs_cfa)
3238 {
3239 expressionS exp;
3240 expressionS *symval;
3241
3242 symval = symbol_get_value_expression (frag->fr_symbol);
3243 exp.X_op = O_subtract;
3244 exp.X_add_symbol = symval->X_add_symbol;
3245 exp.X_add_number = 0;
3246 exp.X_op_symbol = symval->X_op_symbol;
3247
3248 /* We must set the segment before creating a frag after all
3249 frag chains have been chained together. */
3250 subseg_set (s, frch->frch_subseg);
3251
3252 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
3253 BFD_RELOC_RISCV_CFA);
3254 }
3255 }
3256 }
3257
3258 /* Restore the original segment info. */
3259 subseg_set (seg, subseg);
3260 }
3261
3262 /* This structure is used to hold a stack of .option values. */
3263 struct riscv_option_stack
3264 {
3265 struct riscv_option_stack *next;
3266 struct riscv_set_options options;
3267 };
3268
3269 static struct riscv_option_stack *riscv_opts_stack;
3270
3271 /* Handle the .option pseudo-op. */
3272
3273 static void
3274 s_riscv_option (int x ATTRIBUTE_UNUSED)
3275 {
3276 char *name = input_line_pointer, ch;
3277
3278 while (!is_end_of_line[(unsigned char) *input_line_pointer])
3279 ++input_line_pointer;
3280 ch = *input_line_pointer;
3281 *input_line_pointer = '\0';
3282
3283 if (strcmp (name, "rvc") == 0)
3284 riscv_set_rvc (true);
3285 else if (strcmp (name, "norvc") == 0)
3286 riscv_set_rvc (false);
3287 else if (strcmp (name, "pic") == 0)
3288 riscv_opts.pic = true;
3289 else if (strcmp (name, "nopic") == 0)
3290 riscv_opts.pic = false;
3291 else if (strcmp (name, "relax") == 0)
3292 riscv_opts.relax = true;
3293 else if (strcmp (name, "norelax") == 0)
3294 riscv_opts.relax = false;
3295 else if (strcmp (name, "csr-check") == 0)
3296 riscv_opts.csr_check = true;
3297 else if (strcmp (name, "no-csr-check") == 0)
3298 riscv_opts.csr_check = false;
3299 else if (strcmp (name, "push") == 0)
3300 {
3301 struct riscv_option_stack *s;
3302
3303 s = (struct riscv_option_stack *) xmalloc (sizeof *s);
3304 s->next = riscv_opts_stack;
3305 s->options = riscv_opts;
3306 riscv_opts_stack = s;
3307 }
3308 else if (strcmp (name, "pop") == 0)
3309 {
3310 struct riscv_option_stack *s;
3311
3312 s = riscv_opts_stack;
3313 if (s == NULL)
3314 as_bad (_(".option pop with no .option push"));
3315 else
3316 {
3317 riscv_opts = s->options;
3318 riscv_opts_stack = s->next;
3319 free (s);
3320 }
3321 }
3322 else
3323 {
3324 as_warn (_("unrecognized .option directive: %s\n"), name);
3325 }
3326 *input_line_pointer = ch;
3327 demand_empty_rest_of_line ();
3328 }
3329
3330 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
3331 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
3332 use in DWARF debug information. */
3333
3334 static void
3335 s_dtprel (int bytes)
3336 {
3337 expressionS ex;
3338 char *p;
3339
3340 expression (&ex);
3341
3342 if (ex.X_op != O_symbol)
3343 {
3344 as_bad (_("unsupported use of %s"), (bytes == 8
3345 ? ".dtpreldword"
3346 : ".dtprelword"));
3347 ignore_rest_of_line ();
3348 }
3349
3350 p = frag_more (bytes);
3351 md_number_to_chars (p, 0, bytes);
3352 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, false,
3353 (bytes == 8
3354 ? BFD_RELOC_RISCV_TLS_DTPREL64
3355 : BFD_RELOC_RISCV_TLS_DTPREL32));
3356
3357 demand_empty_rest_of_line ();
3358 }
3359
3360 /* Handle the .bss pseudo-op. */
3361
3362 static void
3363 s_bss (int ignore ATTRIBUTE_UNUSED)
3364 {
3365 subseg_set (bss_section, 0);
3366 demand_empty_rest_of_line ();
3367 }
3368
3369 static void
3370 riscv_make_nops (char *buf, bfd_vma bytes)
3371 {
3372 bfd_vma i = 0;
3373
3374 /* RISC-V instructions cannot begin or end on odd addresses, so this case
3375 means we are not within a valid instruction sequence. It is thus safe
3376 to use a zero byte, even though that is not a valid instruction. */
3377 if (bytes % 2 == 1)
3378 buf[i++] = 0;
3379
3380 /* Use at most one 2-byte NOP. */
3381 if ((bytes - i) % 4 == 2)
3382 {
3383 number_to_chars_littleendian (buf + i, RVC_NOP, 2);
3384 i += 2;
3385 }
3386
3387 /* Fill the remainder with 4-byte NOPs. */
3388 for ( ; i < bytes; i += 4)
3389 number_to_chars_littleendian (buf + i, RISCV_NOP, 4);
3390 }
3391
3392 /* Called from md_do_align. Used to create an alignment frag in a
3393 code section by emitting a worst-case NOP sequence that the linker
3394 will later relax to the correct number of NOPs. We can't compute
3395 the correct alignment now because of other linker relaxations. */
3396
3397 bool
3398 riscv_frag_align_code (int n)
3399 {
3400 bfd_vma bytes = (bfd_vma) 1 << n;
3401 bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
3402 bfd_vma worst_case_bytes = bytes - insn_alignment;
3403 char *nops;
3404 expressionS ex;
3405
3406 /* If we are moving to a smaller alignment than the instruction size, then no
3407 alignment is required. */
3408 if (bytes <= insn_alignment)
3409 return true;
3410
3411 /* When not relaxing, riscv_handle_align handles code alignment. */
3412 if (!riscv_opts.relax)
3413 return false;
3414
3415 nops = frag_more (worst_case_bytes);
3416
3417 ex.X_op = O_constant;
3418 ex.X_add_number = worst_case_bytes;
3419
3420 riscv_make_nops (nops, worst_case_bytes);
3421
3422 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
3423 &ex, false, BFD_RELOC_RISCV_ALIGN);
3424
3425 return true;
3426 }
3427
3428 /* Implement HANDLE_ALIGN. */
3429
3430 void
3431 riscv_handle_align (fragS *fragP)
3432 {
3433 switch (fragP->fr_type)
3434 {
3435 case rs_align_code:
3436 /* When relaxing, riscv_frag_align_code handles code alignment. */
3437 if (!riscv_opts.relax)
3438 {
3439 bfd_signed_vma bytes = (fragP->fr_next->fr_address
3440 - fragP->fr_address - fragP->fr_fix);
3441 /* We have 4 byte uncompressed nops. */
3442 bfd_signed_vma size = 4;
3443 bfd_signed_vma excess = bytes % size;
3444 char *p = fragP->fr_literal + fragP->fr_fix;
3445
3446 if (bytes <= 0)
3447 break;
3448
3449 /* Insert zeros or compressed nops to get 4 byte alignment. */
3450 if (excess)
3451 {
3452 riscv_make_nops (p, excess);
3453 fragP->fr_fix += excess;
3454 p += excess;
3455 }
3456
3457 /* Insert variable number of 4 byte uncompressed nops. */
3458 riscv_make_nops (p, size);
3459 fragP->fr_var = size;
3460 }
3461 break;
3462
3463 default:
3464 break;
3465 }
3466 }
3467
3468 int
3469 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
3470 {
3471 return (fragp->fr_var = relaxed_branch_length (fragp, segtype, false));
3472 }
3473
3474 /* Translate internal representation of relocation info to BFD target
3475 format. */
3476
3477 arelent *
3478 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
3479 {
3480 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
3481
3482 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3483 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3484 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
3485 reloc->addend = fixp->fx_addnumber;
3486
3487 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
3488 if (reloc->howto == NULL)
3489 {
3490 if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8)
3491 && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL)
3492 {
3493 /* We don't have R_RISCV_8/16, but for this special case,
3494 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
3495 return reloc;
3496 }
3497
3498 as_bad_where (fixp->fx_file, fixp->fx_line,
3499 _("cannot represent %s relocation in object file"),
3500 bfd_get_reloc_code_name (fixp->fx_r_type));
3501 return NULL;
3502 }
3503
3504 return reloc;
3505 }
3506
3507 int
3508 riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
3509 {
3510 if (RELAX_BRANCH_P (fragp->fr_subtype))
3511 {
3512 offsetT old_var = fragp->fr_var;
3513 fragp->fr_var = relaxed_branch_length (fragp, sec, true);
3514 return fragp->fr_var - old_var;
3515 }
3516
3517 return 0;
3518 }
3519
3520 /* Expand far branches to multi-instruction sequences. */
3521
3522 static void
3523 md_convert_frag_branch (fragS *fragp)
3524 {
3525 bfd_byte *buf;
3526 expressionS exp;
3527 fixS *fixp;
3528 insn_t insn;
3529 int rs1, reloc;
3530
3531 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
3532
3533 exp.X_op = O_symbol;
3534 exp.X_add_symbol = fragp->fr_symbol;
3535 exp.X_add_number = fragp->fr_offset;
3536
3537 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
3538
3539 if (RELAX_BRANCH_RVC (fragp->fr_subtype))
3540 {
3541 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
3542 {
3543 case 8:
3544 case 4:
3545 /* Expand the RVC branch into a RISC-V one. */
3546 insn = bfd_getl16 (buf);
3547 rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S);
3548 if ((insn & MASK_C_J) == MATCH_C_J)
3549 insn = MATCH_JAL;
3550 else if ((insn & MASK_C_JAL) == MATCH_C_JAL)
3551 insn = MATCH_JAL | (X_RA << OP_SH_RD);
3552 else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ)
3553 insn = MATCH_BEQ | (rs1 << OP_SH_RS1);
3554 else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ)
3555 insn = MATCH_BNE | (rs1 << OP_SH_RS1);
3556 else
3557 abort ();
3558 bfd_putl32 (insn, buf);
3559 break;
3560
3561 case 6:
3562 /* Invert the branch condition. Branch over the jump. */
3563 insn = bfd_getl16 (buf);
3564 insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ;
3565 insn |= ENCODE_CBTYPE_IMM (6);
3566 bfd_putl16 (insn, buf);
3567 buf += 2;
3568 goto jump;
3569
3570 case 2:
3571 /* Just keep the RVC branch. */
3572 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
3573 ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH;
3574 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3575 2, &exp, false, reloc);
3576 buf += 2;
3577 goto done;
3578
3579 default:
3580 abort ();
3581 }
3582 }
3583
3584 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
3585 {
3586 case 8:
3587 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
3588
3589 /* Invert the branch condition. Branch over the jump. */
3590 insn = bfd_getl32 (buf);
3591 insn ^= MATCH_BEQ ^ MATCH_BNE;
3592 insn |= ENCODE_BTYPE_IMM (8);
3593 bfd_putl32 (insn, buf);
3594 buf += 4;
3595
3596 jump:
3597 /* Jump to the target. */
3598 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3599 4, &exp, false, BFD_RELOC_RISCV_JMP);
3600 bfd_putl32 (MATCH_JAL, buf);
3601 buf += 4;
3602 break;
3603
3604 case 4:
3605 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
3606 ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL;
3607 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3608 4, &exp, false, reloc);
3609 buf += 4;
3610 break;
3611
3612 default:
3613 abort ();
3614 }
3615
3616 done:
3617 fixp->fx_file = fragp->fr_file;
3618 fixp->fx_line = fragp->fr_line;
3619
3620 gas_assert (buf == (bfd_byte *)fragp->fr_literal
3621 + fragp->fr_fix + fragp->fr_var);
3622
3623 fragp->fr_fix += fragp->fr_var;
3624 }
3625
3626 /* Relax a machine dependent frag. This returns the amount by which
3627 the current size of the frag should change. */
3628
3629 void
3630 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
3631 fragS *fragp)
3632 {
3633 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
3634 md_convert_frag_branch (fragp);
3635 }
3636
3637 void
3638 md_show_usage (FILE *stream)
3639 {
3640 fprintf (stream, _("\
3641 RISC-V options:\n\
3642 -fpic generate position-independent code\n\
3643 -fno-pic don't generate position-independent code (default)\n\
3644 -march=ISA set the RISC-V architecture\n\
3645 -misa-spec=ISAspec set the RISC-V ISA spec (2.2, 20190608, 20191213)\n\
3646 -mpriv-spec=PRIVspec set the RISC-V privilege spec (1.9, 1.9.1, 1.10, 1.11)\n\
3647 -mabi=ABI set the RISC-V ABI\n\
3648 -mrelax enable relax (default)\n\
3649 -mno-relax disable relax\n\
3650 -march-attr generate RISC-V arch attribute\n\
3651 -mno-arch-attr don't generate RISC-V arch attribute\n\
3652 "));
3653 }
3654
3655 /* Standard calling conventions leave the CFA at SP on entry. */
3656
3657 void
3658 riscv_cfi_frame_initial_instructions (void)
3659 {
3660 cfi_add_CFA_def_cfa_register (X_SP);
3661 }
3662
3663 int
3664 tc_riscv_regname_to_dw2regnum (char *regname)
3665 {
3666 int reg;
3667
3668 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
3669 return reg;
3670
3671 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
3672 return reg + 32;
3673
3674 /* CSRs are numbered 4096 -> 8191. */
3675 if ((reg = reg_lookup_internal (regname, RCLASS_CSR)) >= 0)
3676 return reg + 4096;
3677
3678 as_bad (_("unknown register `%s'"), regname);
3679 return -1;
3680 }
3681
3682 void
3683 riscv_elf_final_processing (void)
3684 {
3685 riscv_set_abi_by_arch ();
3686 elf_elfheader (stdoutput)->e_flags |= elf_flags;
3687 }
3688
3689 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
3690 since these directives break relaxation when used with symbol deltas. */
3691
3692 static void
3693 s_riscv_leb128 (int sign)
3694 {
3695 expressionS exp;
3696 char *save_in = input_line_pointer;
3697
3698 expression (&exp);
3699 if (exp.X_op != O_constant)
3700 as_bad (_("non-constant .%cleb128 is not supported"), sign ? 's' : 'u');
3701 demand_empty_rest_of_line ();
3702
3703 input_line_pointer = save_in;
3704 return s_leb128 (sign);
3705 }
3706
3707 /* Parse the .insn directive. */
3708
3709 static void
3710 s_riscv_insn (int x ATTRIBUTE_UNUSED)
3711 {
3712 char *str = input_line_pointer;
3713 struct riscv_cl_insn insn;
3714 expressionS imm_expr;
3715 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
3716 char save_c;
3717
3718 while (!is_end_of_line[(unsigned char) *input_line_pointer])
3719 ++input_line_pointer;
3720
3721 save_c = *input_line_pointer;
3722 *input_line_pointer = '\0';
3723
3724 const char *error = riscv_ip (str, &insn, &imm_expr,
3725 &imm_reloc, insn_type_hash);
3726
3727 if (error)
3728 {
3729 as_bad ("%s `%s'", error, str);
3730 }
3731 else
3732 {
3733 gas_assert (insn.insn_mo->pinfo != INSN_MACRO);
3734 append_insn (&insn, &imm_expr, imm_reloc);
3735 }
3736
3737 *input_line_pointer = save_c;
3738 demand_empty_rest_of_line ();
3739 }
3740
3741 /* Update architecture and privileged elf attributes. If we don't set
3742 them, then try to output the default ones. */
3743
3744 static void
3745 riscv_write_out_attrs (void)
3746 {
3747 const char *arch_str, *priv_str, *p;
3748 /* versions[0]: major version.
3749 versions[1]: minor version.
3750 versions[2]: revision version. */
3751 unsigned versions[3] = {0}, number = 0;
3752 unsigned int i;
3753
3754 /* Re-write architecture elf attribute. */
3755 arch_str = riscv_arch_str (xlen, &riscv_subsets);
3756 bfd_elf_add_proc_attr_string (stdoutput, Tag_RISCV_arch, arch_str);
3757 xfree ((void *) arch_str);
3758
3759 /* For the file without any instruction, we don't set the default_priv_spec
3760 according to the privileged elf attributes since the md_assemble isn't
3761 called. */
3762 if (!start_assemble
3763 && !riscv_set_default_priv_spec (NULL))
3764 return;
3765
3766 /* If we already have set privileged elf attributes, then no need to do
3767 anything. Otherwise, don't generate or update them when no CSR and
3768 privileged instructions are used. */
3769 if (!explicit_priv_attr)
3770 return;
3771
3772 RISCV_GET_PRIV_SPEC_NAME (priv_str, default_priv_spec);
3773 p = priv_str;
3774 for (i = 0; *p; ++p)
3775 {
3776 if (*p == '.' && i < 3)
3777 {
3778 versions[i++] = number;
3779 number = 0;
3780 }
3781 else if (ISDIGIT (*p))
3782 number = (number * 10) + (*p - '0');
3783 else
3784 {
3785 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str);
3786 return;
3787 }
3788 }
3789 versions[i] = number;
3790
3791 /* Re-write privileged elf attributes. */
3792 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec, versions[0]);
3793 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_minor, versions[1]);
3794 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_revision, versions[2]);
3795 }
3796
3797 /* Add the default contents for the .riscv.attributes section. */
3798
3799 static void
3800 riscv_set_public_attributes (void)
3801 {
3802 if (riscv_opts.arch_attr || explicit_attr)
3803 riscv_write_out_attrs ();
3804 }
3805
3806 /* Called after all assembly has been done. */
3807
3808 void
3809 riscv_md_end (void)
3810 {
3811 riscv_set_public_attributes ();
3812 }
3813
3814 /* Given a symbolic attribute NAME, return the proper integer value.
3815 Returns -1 if the attribute is not known. */
3816
3817 int
3818 riscv_convert_symbolic_attribute (const char *name)
3819 {
3820 static const struct
3821 {
3822 const char *name;
3823 const int tag;
3824 }
3825 attribute_table[] =
3826 {
3827 /* When you modify this table you should
3828 also modify the list in doc/c-riscv.texi. */
3829 #define T(tag) {#tag, Tag_RISCV_##tag}, {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
3830 T(arch),
3831 T(priv_spec),
3832 T(priv_spec_minor),
3833 T(priv_spec_revision),
3834 T(unaligned_access),
3835 T(stack_align),
3836 #undef T
3837 };
3838
3839 if (name == NULL)
3840 return -1;
3841
3842 unsigned int i;
3843 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
3844 if (strcmp (name, attribute_table[i].name) == 0)
3845 return attribute_table[i].tag;
3846
3847 return -1;
3848 }
3849
3850 /* Parse a .attribute directive. */
3851
3852 static void
3853 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED)
3854 {
3855 int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
3856 unsigned old_xlen;
3857 obj_attribute *attr;
3858
3859 explicit_attr = true;
3860 switch (tag)
3861 {
3862 case Tag_RISCV_arch:
3863 old_xlen = xlen;
3864 attr = elf_known_obj_attributes_proc (stdoutput);
3865 if (!start_assemble)
3866 riscv_set_arch (attr[Tag_RISCV_arch].s);
3867 else
3868 as_fatal (_("architecture elf attributes must set before "
3869 "any instructions"));
3870
3871 if (old_xlen != xlen)
3872 {
3873 /* We must re-init bfd again if xlen is changed. */
3874 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
3875 bfd_find_target (riscv_target_format (), stdoutput);
3876
3877 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
3878 as_warn (_("could not set architecture and machine"));
3879 }
3880 break;
3881
3882 case Tag_RISCV_priv_spec:
3883 case Tag_RISCV_priv_spec_minor:
3884 case Tag_RISCV_priv_spec_revision:
3885 if (start_assemble)
3886 as_fatal (_("privileged elf attributes must set before "
3887 "any instructions"));
3888 break;
3889
3890 default:
3891 break;
3892 }
3893 }
3894
3895 /* RISC-V pseudo-ops table. */
3896 static const pseudo_typeS riscv_pseudo_table[] =
3897 {
3898 {"option", s_riscv_option, 0},
3899 {"half", cons, 2},
3900 {"word", cons, 4},
3901 {"dword", cons, 8},
3902 {"dtprelword", s_dtprel, 4},
3903 {"dtpreldword", s_dtprel, 8},
3904 {"bss", s_bss, 0},
3905 {"uleb128", s_riscv_leb128, 0},
3906 {"sleb128", s_riscv_leb128, 1},
3907 {"insn", s_riscv_insn, 0},
3908 {"attribute", s_riscv_attribute, 0},
3909
3910 { NULL, NULL, 0 },
3911 };
3912
3913 void
3914 riscv_pop_insert (void)
3915 {
3916 extern void pop_insert (const pseudo_typeS *);
3917
3918 pop_insert (riscv_pseudo_table);
3919 }