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