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