]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-bpf.c
7e1bd5d40b8d3a4a8ec3ed78d8b2da22a24cf2fd
[thirdparty/binutils-gdb.git] / gas / config / tc-bpf.c
1 /* tc-bpf.c -- Assembler for the Linux eBPF.
2 Copyright (C) 2019-2023 Free Software Foundation, Inc.
3 Contributed by Oracle, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 #include "as.h"
23 #include "subsegs.h"
24 #include "symcat.h"
25 #include "opcode/bpf.h"
26 #include "elf/common.h"
27 #include "elf/bpf.h"
28 #include "dwarf2dbg.h"
29 #include "libiberty.h"
30 #include <ctype.h>
31
32 /* Data structure representing a parsed BPF instruction. */
33
34 struct bpf_insn
35 {
36 enum bpf_insn_id id;
37 int size; /* Instruction size in bytes. */
38 bpf_insn_word opcode;
39 uint8_t dst;
40 uint8_t src;
41 expressionS offset16;
42 expressionS imm32;
43 expressionS imm64;
44 expressionS disp16;
45 expressionS disp32;
46
47 unsigned int has_dst : 1;
48 unsigned int has_src : 1;
49 unsigned int has_offset16 : 1;
50 unsigned int has_disp16 : 1;
51 unsigned int has_disp32 : 1;
52 unsigned int has_imm32 : 1;
53 unsigned int has_imm64 : 1;
54
55 unsigned int is_relaxable : 1;
56 expressionS *relaxed_exp;
57 };
58
59 const char comment_chars[] = ";#";
60 const char line_comment_chars[] = "#";
61 const char line_separator_chars[] = "`";
62 const char EXP_CHARS[] = "eE";
63 const char FLT_CHARS[] = "fFdD";
64
65 /* Like s_lcomm_internal in gas/read.c but the alignment string
66 is allowed to be optional. */
67
68 static symbolS *
69 pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
70 {
71 addressT align = 0;
72
73 SKIP_WHITESPACE ();
74
75 if (needs_align
76 && *input_line_pointer == ',')
77 {
78 align = parse_align (needs_align - 1);
79
80 if (align == (addressT) -1)
81 return NULL;
82 }
83 else
84 {
85 if (size >= 8)
86 align = 3;
87 else if (size >= 4)
88 align = 2;
89 else if (size >= 2)
90 align = 1;
91 else
92 align = 0;
93 }
94
95 bss_alloc (symbolP, size, align);
96 return symbolP;
97 }
98
99 static void
100 pe_lcomm (int needs_align)
101 {
102 s_comm_internal (needs_align * 2, pe_lcomm_internal);
103 }
104
105 /* The target specific pseudo-ops which we support. */
106 const pseudo_typeS md_pseudo_table[] =
107 {
108 { "half", cons, 2 },
109 { "word", cons, 4 },
110 { "dword", cons, 8 },
111 { "lcomm", pe_lcomm, 1 },
112 { NULL, NULL, 0 }
113 };
114
115 \f
116
117 /* Command-line options processing. */
118
119 enum options
120 {
121 OPTION_LITTLE_ENDIAN = OPTION_MD_BASE,
122 OPTION_BIG_ENDIAN,
123 OPTION_XBPF,
124 OPTION_DIALECT,
125 OPTION_ISA_SPEC,
126 OPTION_NO_RELAX,
127 };
128
129 struct option md_longopts[] =
130 {
131 { "EL", no_argument, NULL, OPTION_LITTLE_ENDIAN },
132 { "EB", no_argument, NULL, OPTION_BIG_ENDIAN },
133 { "mxbpf", no_argument, NULL, OPTION_XBPF },
134 { "mdialect", required_argument, NULL, OPTION_DIALECT},
135 { "misa-spec", required_argument, NULL, OPTION_ISA_SPEC},
136 { "mno-relax", no_argument, NULL, OPTION_NO_RELAX},
137 { NULL, no_argument, NULL, 0 },
138 };
139
140 size_t md_longopts_size = sizeof (md_longopts);
141
142 const char * md_shortopts = "";
143
144 /* BPF supports little-endian and big-endian variants. The following
145 global records what endianness to use. It can be configured using
146 command-line options. It defaults to the host endianness
147 initialized in md_begin. */
148
149 static int set_target_endian = 0;
150 extern int target_big_endian;
151
152 /* Whether to relax branch instructions. Default is yes. Can be
153 changed using the -mno-relax command line option. */
154
155 static int do_relax = 1;
156
157 /* The ISA specification can be one of BPF_V1, BPF_V2, BPF_V3, BPF_V4
158 or BPF_XPBF. The ISA spec to use can be configured using
159 command-line options. It defaults to the latest BPF spec. */
160
161 static int isa_spec = BPF_V4;
162
163 /* The assembler supports two different dialects: "normal" syntax and
164 "pseudoc" syntax. The dialect to use can be configured using
165 command-line options. */
166
167 enum target_asm_dialect
168 {
169 DIALECT_NORMAL,
170 DIALECT_PSEUDOC
171 };
172
173 static int asm_dialect = DIALECT_NORMAL;
174
175 int
176 md_parse_option (int c, const char * arg)
177 {
178 switch (c)
179 {
180 case OPTION_BIG_ENDIAN:
181 set_target_endian = 1;
182 target_big_endian = 1;
183 break;
184 case OPTION_LITTLE_ENDIAN:
185 set_target_endian = 0;
186 target_big_endian = 0;
187 break;
188 case OPTION_DIALECT:
189 if (strcmp (arg, "normal") == 0)
190 asm_dialect = DIALECT_NORMAL;
191 else if (strcmp (arg, "pseudoc") == 0)
192 asm_dialect = DIALECT_PSEUDOC;
193 else
194 as_fatal (_("-mdialect=%s is not valid. Expected normal or pseudoc"),
195 arg);
196 break;
197 case OPTION_ISA_SPEC:
198 if (strcmp (arg, "v1") == 0)
199 isa_spec = BPF_V1;
200 else if (strcmp (arg, "v2") == 0)
201 isa_spec = BPF_V2;
202 else if (strcmp (arg, "v3") == 0)
203 isa_spec = BPF_V3;
204 else if (strcmp (arg, "v4") == 0)
205 isa_spec = BPF_V4;
206 else if (strcmp (arg, "xbpf") == 0)
207 isa_spec = BPF_XBPF;
208 else
209 as_fatal (_("-misa-spec=%s is not valid. Expected v1, v2, v3, v4 o xbpf"),
210 arg);
211 break;
212 case OPTION_XBPF:
213 /* This is an alias for -misa-spec=xbpf. */
214 isa_spec = BPF_XBPF;
215 break;
216 case OPTION_NO_RELAX:
217 do_relax = 0;
218 break;
219 default:
220 return 0;
221 }
222
223 return 1;
224 }
225
226 void
227 md_show_usage (FILE * stream)
228 {
229 fprintf (stream, _("\nBPF options:\n"));
230 fprintf (stream, _("\
231 BPF options:\n\
232 -EL generate code for a little endian machine\n\
233 -EB generate code for a big endian machine\n\
234 -mdialect=DIALECT set the assembly dialect (normal, pseudoc)\n\
235 -misa-spec set the BPF ISA spec (v1, v2, v3, v4, xbpf)\n\
236 -mxbpf alias for -misa-spec=xbpf\n"));
237 }
238
239 \f
240 /* This function is called once, at assembler startup time. This
241 should set up all the tables, etc that the MD part of the assembler
242 needs. */
243
244 void
245 md_begin (void)
246 {
247 /* If not specified in the command line, use the host
248 endianness. */
249 if (!set_target_endian)
250 {
251 #ifdef WORDS_BIGENDIAN
252 target_big_endian = 1;
253 #else
254 target_big_endian = 0;
255 #endif
256 }
257
258 /* Ensure that lines can begin with '*' in BPF store pseudoc instruction. */
259 lex_type['*'] |= LEX_BEGIN_NAME;
260
261 /* Set the machine type. */
262 bfd_default_set_arch_mach (stdoutput, bfd_arch_bpf, bfd_mach_bpf);
263 }
264
265 /* Round up a section size to the appropriate boundary. */
266
267 valueT
268 md_section_align (segT segment, valueT size)
269 {
270 int align = bfd_section_alignment (segment);
271
272 return ((size + (1 << align) - 1) & -(1 << align));
273 }
274
275 /* Return non-zero if the indicated VALUE has overflowed the maximum
276 range expressible by an signed number with the indicated number of
277 BITS. */
278
279 static bool
280 signed_overflow (offsetT value, unsigned bits)
281 {
282 offsetT lim;
283 if (bits >= sizeof (offsetT) * 8)
284 return false;
285 lim = (offsetT) 1 << (bits - 1);
286 return (value < -lim || value >= lim);
287 }
288
289 \f
290 /* Functions concerning relocs. */
291
292 /* The location from which a PC relative jump should be calculated,
293 given a PC relative reloc. */
294
295 long
296 md_pcrel_from_section (fixS *fixP, segT sec)
297 {
298 if (fixP->fx_addsy != (symbolS *) NULL
299 && (! S_IS_DEFINED (fixP->fx_addsy)
300 || (S_GET_SEGMENT (fixP->fx_addsy) != sec)
301 || S_IS_EXTERNAL (fixP->fx_addsy)
302 || S_IS_WEAK (fixP->fx_addsy)))
303 {
304 /* The symbol is undefined (or is defined but not in this section).
305 Let the linker figure it out. */
306 return 0;
307 }
308
309 return fixP->fx_where + fixP->fx_frag->fr_address;
310 }
311
312 /* Write a value out to the object file, using the appropriate endianness. */
313
314 void
315 md_number_to_chars (char * buf, valueT val, int n)
316 {
317 if (target_big_endian)
318 number_to_chars_bigendian (buf, val, n);
319 else
320 number_to_chars_littleendian (buf, val, n);
321 }
322
323 arelent *
324 tc_gen_reloc (asection *sec ATTRIBUTE_UNUSED, fixS *fixP)
325 {
326 bfd_reloc_code_real_type r_type = fixP->fx_r_type;
327 arelent *reloc;
328
329 reloc = XNEW (arelent);
330
331 if (fixP->fx_pcrel)
332 {
333 r_type = (r_type == BFD_RELOC_8 ? BFD_RELOC_8_PCREL
334 : r_type == BFD_RELOC_16 ? BFD_RELOC_16_PCREL
335 : r_type == BFD_RELOC_24 ? BFD_RELOC_24_PCREL
336 : r_type == BFD_RELOC_32 ? BFD_RELOC_32_PCREL
337 : r_type == BFD_RELOC_64 ? BFD_RELOC_64_PCREL
338 : r_type);
339 }
340
341 reloc->howto = bfd_reloc_type_lookup (stdoutput, r_type);
342
343 if (reloc->howto == (reloc_howto_type *) NULL)
344 {
345 as_bad_where (fixP->fx_file, fixP->fx_line,
346 _("relocation is not supported"));
347 return NULL;
348 }
349
350 //XXX gas_assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
351
352 reloc->sym_ptr_ptr = XNEW (asymbol *);
353 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
354
355 /* Use fx_offset for these cases. */
356 if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
357 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
358 reloc->addend = fixP->fx_offset;
359 else
360 reloc->addend = fixP->fx_addnumber;
361
362 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
363 return reloc;
364 }
365
366 \f
367 /* Relaxations supported by this assembler. */
368
369 #define RELAX_BRANCH_ENCODE(uncond, constant, length) \
370 ((relax_substateT) \
371 (0xc0000000 \
372 | ((uncond) ? 1 : 0) \
373 | ((constant) ? 2 : 0) \
374 | ((length) << 2)))
375
376 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
377 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xff)
378 #define RELAX_BRANCH_CONST(i) (((i) & 2) != 0)
379 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
380
381
382 /* Compute the length of a branch seuqence, and adjust the stored
383 length accordingly. If FRAG is NULL, the worst-case length is
384 returned. */
385
386 static unsigned
387 relaxed_branch_length (fragS *fragp, asection *sec, int update)
388 {
389 int length, uncond;
390
391 if (!fragp)
392 return 8 * 3;
393
394 uncond = RELAX_BRANCH_UNCOND (fragp->fr_subtype);
395 length = RELAX_BRANCH_LENGTH (fragp->fr_subtype);
396
397 if (uncond)
398 /* Length is the same for both JA and JAL. */
399 length = 8;
400 else
401 {
402 if (RELAX_BRANCH_CONST (fragp->fr_subtype))
403 {
404 int64_t val = fragp->fr_offset;
405
406 if (val < -32768 || val > 32767)
407 length = 8 * 3;
408 else
409 length = 8;
410 }
411 else if (fragp->fr_symbol != NULL
412 && S_IS_DEFINED (fragp->fr_symbol)
413 && !S_IS_WEAK (fragp->fr_symbol)
414 && sec == S_GET_SEGMENT (fragp->fr_symbol))
415 {
416 offsetT val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
417
418 /* Convert to 64-bit words, minus one. */
419 val = (val - 8) / 8;
420
421 /* See if it fits in the signed 16-bits field. */
422 if (val < -32768 || val > 32767)
423 length = 8 * 3;
424 else
425 length = 8;
426 }
427 else
428 /* Use short version, and let the linker relax instead, if
429 appropriate and if supported. */
430 length = 8;
431 }
432
433 if (update)
434 fragp->fr_subtype = RELAX_BRANCH_ENCODE (uncond,
435 RELAX_BRANCH_CONST (fragp->fr_subtype),
436 length);
437
438 return length;
439 }
440
441 /* Estimate the size of a variant frag before relaxing. */
442
443 int
444 md_estimate_size_before_relax (fragS *fragp, asection *sec)
445 {
446 return (fragp->fr_var = relaxed_branch_length (fragp, sec, true));
447 }
448
449 /* Read a BPF instruction word from BUF. */
450
451 static uint64_t
452 read_insn_word (bfd_byte *buf)
453 {
454 return bfd_getb64 (buf);
455 }
456
457 /* Write the given signed 16-bit value in the given BUFFER using the
458 target endianness. */
459
460 static void
461 encode_int16 (int16_t value, char *buffer)
462 {
463 uint16_t val = value;
464
465 if (target_big_endian)
466 {
467 buffer[0] = (val >> 8) & 0xff;
468 buffer[1] = val & 0xff;
469 }
470 else
471 {
472 buffer[1] = (val >> 8) & 0xff;
473 buffer[0] = val & 0xff;
474 }
475 }
476
477 /* Write the given signed 32-bit value in the given BUFFER using the
478 target endianness. */
479
480 static void
481 encode_int32 (int32_t value, char *buffer)
482 {
483 uint32_t val = value;
484
485 if (target_big_endian)
486 {
487 buffer[0] = (val >> 24) & 0xff;
488 buffer[1] = (val >> 16) & 0xff;
489 buffer[2] = (val >> 8) & 0xff;
490 buffer[3] = val & 0xff;
491 }
492 else
493 {
494 buffer[3] = (val >> 24) & 0xff;
495 buffer[2] = (val >> 16) & 0xff;
496 buffer[1] = (val >> 8) & 0xff;
497 buffer[0] = value & 0xff;
498 }
499 }
500
501 /* Write a BPF instruction to BUF. */
502
503 static void
504 write_insn_bytes (bfd_byte *buf, char *bytes)
505 {
506 int i;
507
508 for (i = 0; i < 8; ++i)
509 md_number_to_chars ((char *) buf + i, (valueT) bytes[i], 1);
510 }
511
512 /* *FRAGP has been relaxed to its final size, and now needs to have
513 the bytes inside it modified to conform to the new size.
514
515 Called after relaxation is finished.
516 fragP->fr_type == rs_machine_dependent.
517 fragP->fr_subtype is the subtype of what the address relaxed to. */
518
519 void
520 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
521 segT sec ATTRIBUTE_UNUSED,
522 fragS *fragp ATTRIBUTE_UNUSED)
523 {
524 bfd_byte *buf = (bfd_byte *) fragp->fr_literal + fragp->fr_fix;
525 expressionS exp;
526 fixS *fixp;
527 bpf_insn_word word;
528 int disp_is_known = 0;
529 int64_t disp_to_target = 0;
530
531 uint64_t code;
532
533 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
534
535 /* Expression to be used in any resulting relocation in the relaxed
536 instructions. */
537 exp.X_op = O_symbol;
538 exp.X_add_symbol = fragp->fr_symbol;
539 exp.X_add_number = fragp->fr_offset;
540
541 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
542
543 /* Read an instruction word from the instruction to be relaxed, and
544 get the code. */
545 word = read_insn_word (buf);
546 code = (word >> 60) & 0xf;
547
548 /* Determine whether the 16-bit displacement to the target is known
549 at this point. */
550 if (RELAX_BRANCH_CONST (fragp->fr_subtype))
551 {
552 disp_to_target = fragp->fr_offset;
553 disp_is_known = 1;
554 }
555 else if (fragp->fr_symbol != NULL
556 && S_IS_DEFINED (fragp->fr_symbol)
557 && !S_IS_WEAK (fragp->fr_symbol)
558 && sec == S_GET_SEGMENT (fragp->fr_symbol))
559 {
560 offsetT val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
561 /* Convert to 64-bit blocks minus one. */
562 disp_to_target = (val - 8) / 8;
563 disp_is_known = 1;
564 }
565
566 /* The displacement should fit in a signed 32-bit number. */
567 if (disp_is_known && signed_overflow (disp_to_target, 32))
568 as_bad_where (fragp->fr_file, fragp->fr_line,
569 _("signed instruction operand out of range, shall fit in 32 bits"));
570
571 /* Now relax particular jump instructions. */
572 if (code == BPF_CODE_JA)
573 {
574 /* Unconditional jump.
575 JA d16 -> JAL d32 */
576
577 gas_assert (RELAX_BRANCH_UNCOND (fragp->fr_subtype));
578
579 if (disp_is_known)
580 {
581 if (disp_to_target >= -32768 && disp_to_target <= 32767)
582 {
583 /* 16-bit disp is known and in range. Install a fixup
584 for the disp16 if the branch value is not constant.
585 This will be resolved by the assembler and units
586 converted. */
587
588 if (!RELAX_BRANCH_CONST (fragp->fr_subtype))
589 {
590 /* Install fixup for the JA. */
591 reloc_howto_type *reloc_howto
592 = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_BPF_DISP16);
593 if (!reloc_howto)
594 abort();
595
596 fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
597 bfd_get_reloc_size (reloc_howto),
598 &exp,
599 reloc_howto->pc_relative,
600 BFD_RELOC_BPF_DISP16);
601 fixp->fx_file = fragp->fr_file;
602 fixp->fx_line = fragp->fr_line;
603 }
604 }
605 else
606 {
607 /* 16-bit disp is known and not in range. Turn the JA
608 into a JAL with a 32-bit displacement. */
609 char bytes[8];
610
611 bytes[0] = ((BPF_CLASS_JMP32|BPF_CODE_JA|BPF_SRC_K) >> 56) & 0xff;
612 bytes[1] = (word >> 48) & 0xff;
613 bytes[2] = 0; /* disp16 high */
614 bytes[3] = 0; /* disp16 lo */
615 encode_int32 ((int32_t) disp_to_target, bytes + 4);
616
617 write_insn_bytes (buf, bytes);
618 }
619 }
620 else
621 {
622 /* The displacement to the target is not known. Do not
623 relax. The linker will maybe do it if it chooses to. */
624
625 reloc_howto_type *reloc_howto = NULL;
626
627 gas_assert (!RELAX_BRANCH_CONST (fragp->fr_subtype));
628
629 /* Install fixup for the JA. */
630 reloc_howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_BPF_DISP16);
631 if (!reloc_howto)
632 abort ();
633
634 fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
635 bfd_get_reloc_size (reloc_howto),
636 &exp,
637 reloc_howto->pc_relative,
638 BFD_RELOC_BPF_DISP16);
639 fixp->fx_file = fragp->fr_file;
640 fixp->fx_line = fragp->fr_line;
641 }
642
643 buf += 8;
644 }
645 else
646 {
647 /* Conditional jump.
648 JXX d16 -> JXX +1; JA +1; JAL d32 */
649
650 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
651
652 if (disp_is_known)
653 {
654 if (disp_to_target >= -32768 && disp_to_target <= 32767)
655 {
656 /* 16-bit disp is known and in range. Install a fixup
657 for the disp16 if the branch value is not constant.
658 This will be resolved by the assembler and units
659 converted. */
660
661 if (!RELAX_BRANCH_CONST (fragp->fr_subtype))
662 {
663 /* Install fixup for the branch. */
664 reloc_howto_type *reloc_howto
665 = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_BPF_DISP16);
666 if (!reloc_howto)
667 abort();
668
669 fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
670 bfd_get_reloc_size (reloc_howto),
671 &exp,
672 reloc_howto->pc_relative,
673 BFD_RELOC_BPF_DISP16);
674 fixp->fx_file = fragp->fr_file;
675 fixp->fx_line = fragp->fr_line;
676 }
677
678 buf += 8;
679 }
680 else
681 {
682 /* 16-bit disp is known and not in range. Turn the JXX
683 into a sequence JXX +1; JA +1; JAL d32. */
684
685 char bytes[8];
686
687 /* First, set the 16-bit offset in the current
688 instruction to 1. */
689
690 if (target_big_endian)
691 bfd_putb16 (1, buf + 2);
692 else
693 bfd_putl16 (1, buf + 2);
694 buf += 8;
695
696 /* Then, write the JA + 1 */
697
698 bytes[0] = 0x05; /* JA */
699 bytes[1] = 0x0;
700 encode_int16 (1, bytes + 2);
701 bytes[4] = 0x0;
702 bytes[5] = 0x0;
703 bytes[6] = 0x0;
704 bytes[7] = 0x0;
705 write_insn_bytes (buf, bytes);
706 buf += 8;
707
708 /* Finally, write the JAL to the target. */
709
710 bytes[0] = ((BPF_CLASS_JMP32|BPF_CODE_JA|BPF_SRC_K) >> 56) & 0xff;
711 bytes[1] = 0;
712 bytes[2] = 0;
713 bytes[3] = 0;
714 encode_int32 ((int32_t) disp_to_target, bytes + 4);
715 write_insn_bytes (buf, bytes);
716 buf += 8;
717 }
718 }
719 else
720 {
721 /* The displacement to the target is not known. Do not
722 relax. The linker will maybe do it if it chooses to. */
723
724 reloc_howto_type *reloc_howto = NULL;
725
726 gas_assert (!RELAX_BRANCH_CONST (fragp->fr_subtype));
727
728 /* Install fixup for the conditional jump. */
729 reloc_howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_BPF_DISP16);
730 if (!reloc_howto)
731 abort ();
732
733 fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
734 bfd_get_reloc_size (reloc_howto),
735 &exp,
736 reloc_howto->pc_relative,
737 BFD_RELOC_BPF_DISP16);
738 fixp->fx_file = fragp->fr_file;
739 fixp->fx_line = fragp->fr_line;
740 buf += 8;
741 }
742 }
743
744 gas_assert (buf == (bfd_byte *)fragp->fr_literal
745 + fragp->fr_fix + fragp->fr_var);
746
747 fragp->fr_fix += fragp->fr_var;
748 }
749
750 \f
751 /* Apply a fixS (fixup of an instruction or data that we didn't have
752 enough info to complete immediately) to the data in a frag. */
753
754 void
755 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
756 {
757 char *where = fixP->fx_frag->fr_literal + fixP->fx_where;
758
759 switch (fixP->fx_r_type)
760 {
761 case BFD_RELOC_BPF_DISP16:
762 /* Convert from bytes to number of 64-bit words to the target,
763 minus one. */
764 *valP = (((long) (*valP)) - 8) / 8;
765 break;
766 case BFD_RELOC_BPF_DISPCALL32:
767 case BFD_RELOC_BPF_DISP32:
768 /* Convert from bytes to number of 64-bit words to the target,
769 minus one. */
770 *valP = (((long) (*valP)) - 8) / 8;
771
772 if (fixP->fx_r_type == BFD_RELOC_BPF_DISPCALL32)
773 {
774 /* eBPF supports two kind of CALL instructions: the so
775 called pseudo calls ("bpf to bpf") and external calls
776 ("bpf to kernel").
777
778 Both kind of calls use the same instruction (CALL).
779 However, external calls are constructed by passing a
780 constant argument to the instruction, whereas pseudo
781 calls result from expressions involving symbols. In
782 practice, instructions requiring a fixup are interpreted
783 as pseudo-calls. If we are executing this code, this is
784 a pseudo call.
785
786 The kernel expects for pseudo-calls to be annotated by
787 having BPF_PSEUDO_CALL in the SRC field of the
788 instruction. But beware the infamous nibble-swapping of
789 eBPF and take endianness into account here.
790
791 Note that the CALL instruction has only one operand, so
792 this code is executed only once per instruction. */
793 md_number_to_chars (where + 1, target_big_endian ? 0x01 : 0x10, 1);
794 }
795 break;
796 case BFD_RELOC_16_PCREL:
797 /* Convert from bytes to number of 64-bit words to the target,
798 minus one. */
799 *valP = (((long) (*valP)) - 8) / 8;
800 break;
801 default:
802 break;
803 }
804
805 if (fixP->fx_addsy == (symbolS *) NULL)
806 fixP->fx_done = 1;
807
808 if (fixP->fx_done)
809 {
810 /* We're finished with this fixup. Install it because
811 bfd_install_relocation won't be called to do it. */
812 switch (fixP->fx_r_type)
813 {
814 case BFD_RELOC_8:
815 md_number_to_chars (where, *valP, 1);
816 break;
817 case BFD_RELOC_16:
818 md_number_to_chars (where, *valP, 2);
819 break;
820 case BFD_RELOC_32:
821 md_number_to_chars (where, *valP, 4);
822 break;
823 case BFD_RELOC_64:
824 md_number_to_chars (where, *valP, 8);
825 break;
826 case BFD_RELOC_BPF_DISP16:
827 md_number_to_chars (where + 2, (uint16_t) *valP, 2);
828 break;
829 case BFD_RELOC_BPF_DISP32:
830 case BFD_RELOC_BPF_DISPCALL32:
831 md_number_to_chars (where + 4, (uint32_t) *valP, 4);
832 break;
833 case BFD_RELOC_16_PCREL:
834 md_number_to_chars (where + 2, (uint32_t) *valP, 2);
835 break;
836 default:
837 as_bad_where (fixP->fx_file, fixP->fx_line,
838 _("internal error: can't install fix for reloc type %d (`%s')"),
839 fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
840 break;
841 }
842 }
843
844 /* Tuck `value' away for use by tc_gen_reloc.
845 See the comment describing fx_addnumber in write.h.
846 This field is misnamed (or misused :-). */
847 fixP->fx_addnumber = *valP;
848 }
849
850 \f
851 /* Instruction writing routines. */
852
853 /* Encode a BPF instruction in the given buffer BYTES. Non-constant
854 immediates are encoded as zeroes. */
855
856 static void
857 encode_insn (struct bpf_insn *insn, char *bytes, int relaxed)
858 {
859 uint8_t src, dst;
860
861 /* Zero all the bytes. */
862 memset (bytes, 0, 16);
863
864 /* First encode the opcodes. Note that we have to handle the
865 endianness groups of the BPF instructions: 8 | 4 | 4 | 16 |
866 32. */
867 if (target_big_endian)
868 {
869 /* code */
870 bytes[0] = (insn->opcode >> 56) & 0xff;
871 /* regs */
872 bytes[1] = (insn->opcode >> 48) & 0xff;
873 /* offset16 */
874 bytes[2] = (insn->opcode >> 40) & 0xff;
875 bytes[3] = (insn->opcode >> 32) & 0xff;
876 /* imm32 */
877 bytes[4] = (insn->opcode >> 24) & 0xff;
878 bytes[5] = (insn->opcode >> 16) & 0xff;
879 bytes[6] = (insn->opcode >> 8) & 0xff;
880 bytes[7] = insn->opcode & 0xff;
881 }
882 else
883 {
884 /* code */
885 bytes[0] = (insn->opcode >> 56) & 0xff;
886 /* regs */
887 bytes[1] = (((((insn->opcode >> 48) & 0xff) & 0xf) << 4)
888 | (((insn->opcode >> 48) & 0xff) & 0xf));
889 /* offset16 */
890 bytes[3] = (insn->opcode >> 40) & 0xff;
891 bytes[2] = (insn->opcode >> 32) & 0xff;
892 /* imm32 */
893 bytes[7] = (insn->opcode >> 24) & 0xff;
894 bytes[6] = (insn->opcode >> 16) & 0xff;
895 bytes[5] = (insn->opcode >> 8) & 0xff;
896 bytes[4] = insn->opcode & 0xff;
897 }
898
899 /* Now the registers. */
900 src = insn->has_src ? insn->src : 0;
901 dst = insn->has_dst ? insn->dst : 0;
902
903 if (target_big_endian)
904 bytes[1] = ((dst & 0xf) << 4) | (src & 0xf);
905 else
906 bytes[1] = ((src & 0xf) << 4) | (dst & 0xf);
907
908 /* Now the immediates that are known to be constant. */
909
910 if (insn->has_imm32 && insn->imm32.X_op == O_constant)
911 {
912 int64_t imm = insn->imm32.X_add_number;
913
914 if (signed_overflow (imm, 32))
915 as_bad (_("signed immediate out of range, shall fit in 32 bits"));
916 else
917 encode_int32 (insn->imm32.X_add_number, bytes + 4);
918 }
919
920 if (insn->has_disp32 && insn->disp32.X_op == O_constant)
921 {
922 int64_t disp = insn->disp32.X_add_number;
923
924 if (signed_overflow (disp, 32))
925 as_bad (_("signed pc-relative offset out of range, shall fit in 32 bits"));
926 else
927 encode_int32 (insn->disp32.X_add_number, bytes + 4);
928 }
929
930 if (insn->has_offset16 && insn->offset16.X_op == O_constant)
931 {
932 int64_t offset = insn->offset16.X_add_number;
933
934 if (signed_overflow (offset, 16))
935 as_bad (_("signed pc-relative offset out of range, shall fit in 16 bits"));
936 else
937 encode_int16 (insn->offset16.X_add_number, bytes + 2);
938 }
939
940 if (insn->has_disp16 && insn->disp16.X_op == O_constant)
941 {
942 int64_t disp = insn->disp16.X_add_number;
943
944 if (!relaxed && signed_overflow (disp, 16))
945 as_bad (_("signed pc-relative offset out of range, shall fit in 16 bits"));
946 else
947 encode_int16 (insn->disp16.X_add_number, bytes + 2);
948 }
949
950 if (insn->has_imm64 && insn->imm64.X_op == O_constant)
951 {
952 uint64_t imm64 = insn->imm64.X_add_number;
953
954 if (target_big_endian)
955 {
956 bytes[12] = (imm64 >> 56) & 0xff;
957 bytes[13] = (imm64 >> 48) & 0xff;
958 bytes[14] = (imm64 >> 40) & 0xff;
959 bytes[15] = (imm64 >> 32) & 0xff;
960 bytes[4] = (imm64 >> 24) & 0xff;
961 bytes[5] = (imm64 >> 16) & 0xff;
962 bytes[6] = (imm64 >> 8) & 0xff;
963 bytes[7] = imm64 & 0xff;
964 }
965 else
966 {
967 bytes[15] = (imm64 >> 56) & 0xff;
968 bytes[14] = (imm64 >> 48) & 0xff;
969 bytes[13] = (imm64 >> 40) & 0xff;
970 bytes[12] = (imm64 >> 32) & 0xff;
971 bytes[7] = (imm64 >> 24) & 0xff;
972 bytes[6] = (imm64 >> 16) & 0xff;
973 bytes[5] = (imm64 >> 8) & 0xff;
974 bytes[4] = imm64 & 0xff;
975 }
976 }
977 }
978
979 /* Install the fixups in INSN in their proper location in the
980 specified FRAG at the location pointed by WHERE. */
981
982 static void
983 install_insn_fixups (struct bpf_insn *insn, fragS *frag, long where)
984 {
985 if (insn->has_imm64)
986 {
987 switch (insn->imm64.X_op)
988 {
989 case O_symbol:
990 case O_subtract:
991 case O_add:
992 {
993 reloc_howto_type *reloc_howto;
994 int size;
995
996 reloc_howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_BPF_64);
997 if (!reloc_howto)
998 abort ();
999
1000 size = bfd_get_reloc_size (reloc_howto);
1001
1002 fix_new_exp (frag, where,
1003 size, &insn->imm64, reloc_howto->pc_relative,
1004 BFD_RELOC_BPF_64);
1005 break;
1006 }
1007 case O_constant:
1008 /* Already handled in encode_insn. */
1009 break;
1010 default:
1011 abort ();
1012 }
1013 }
1014
1015 if (insn->has_imm32)
1016 {
1017 switch (insn->imm32.X_op)
1018 {
1019 case O_symbol:
1020 case O_subtract:
1021 case O_add:
1022 case O_uminus:
1023 {
1024 reloc_howto_type *reloc_howto;
1025 int size;
1026
1027 reloc_howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
1028 if (!reloc_howto)
1029 abort ();
1030
1031 size = bfd_get_reloc_size (reloc_howto);
1032
1033 fix_new_exp (frag, where + 4,
1034 size, &insn->imm32, reloc_howto->pc_relative,
1035 BFD_RELOC_32);
1036 break;
1037 }
1038 case O_constant:
1039 /* Already handled in encode_insn. */
1040 break;
1041 default:
1042 abort ();
1043 }
1044 }
1045
1046 if (insn->has_disp32)
1047 {
1048 switch (insn->disp32.X_op)
1049 {
1050 case O_symbol:
1051 case O_subtract:
1052 case O_add:
1053 {
1054 reloc_howto_type *reloc_howto;
1055 int size;
1056 unsigned int bfd_reloc
1057 = (insn->id == BPF_INSN_CALL
1058 ? BFD_RELOC_BPF_DISPCALL32
1059 : BFD_RELOC_BPF_DISP32);
1060
1061 reloc_howto = bfd_reloc_type_lookup (stdoutput, bfd_reloc);
1062 if (!reloc_howto)
1063 abort ();
1064
1065 size = bfd_get_reloc_size (reloc_howto);
1066
1067 fix_new_exp (frag, where,
1068 size, &insn->disp32, reloc_howto->pc_relative,
1069 bfd_reloc);
1070 break;
1071 }
1072 case O_constant:
1073 /* Already handled in encode_insn. */
1074 break;
1075 default:
1076 abort ();
1077 }
1078 }
1079
1080 if (insn->has_offset16)
1081 {
1082 switch (insn->offset16.X_op)
1083 {
1084 case O_symbol:
1085 case O_subtract:
1086 case O_add:
1087 {
1088 reloc_howto_type *reloc_howto;
1089 int size;
1090
1091 /* XXX we really need a new pc-rel offset in bytes
1092 relocation for this. */
1093 reloc_howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_BPF_DISP16);
1094 if (!reloc_howto)
1095 abort ();
1096
1097 size = bfd_get_reloc_size (reloc_howto);
1098
1099 fix_new_exp (frag, where,
1100 size, &insn->offset16, reloc_howto->pc_relative,
1101 BFD_RELOC_BPF_DISP16);
1102 break;
1103 }
1104 case O_constant:
1105 /* Already handled in encode_insn. */
1106 break;
1107 default:
1108 abort ();
1109 }
1110 }
1111
1112 if (insn->has_disp16)
1113 {
1114 switch (insn->disp16.X_op)
1115 {
1116 case O_symbol:
1117 case O_subtract:
1118 case O_add:
1119 {
1120 reloc_howto_type *reloc_howto;
1121 int size;
1122
1123 reloc_howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_BPF_DISP16);
1124 if (!reloc_howto)
1125 abort ();
1126
1127 size = bfd_get_reloc_size (reloc_howto);
1128
1129 fix_new_exp (frag, where,
1130 size, &insn->disp16, reloc_howto->pc_relative,
1131 BFD_RELOC_BPF_DISP16);
1132 break;
1133 }
1134 case O_constant:
1135 /* Already handled in encode_insn. */
1136 break;
1137 default:
1138 abort ();
1139 }
1140 }
1141
1142 }
1143
1144 /* Add a new insn to the list of instructions. */
1145
1146 static void
1147 add_fixed_insn (struct bpf_insn *insn)
1148 {
1149 char *this_frag = frag_more (insn->size);
1150 char bytes[16];
1151 int i;
1152
1153 /* First encode the known parts of the instruction, including
1154 opcodes and constant immediates, and write them to the frag. */
1155 encode_insn (insn, bytes, 0 /* relax */);
1156 for (i = 0; i < insn->size; ++i)
1157 md_number_to_chars (this_frag + i, (valueT) bytes[i], 1);
1158
1159 /* Now install the instruction fixups. */
1160 install_insn_fixups (insn, frag_now,
1161 this_frag - frag_now->fr_literal);
1162 }
1163
1164 /* Add a new relaxable to the list of instructions. */
1165
1166 static void
1167 add_relaxed_insn (struct bpf_insn *insn, expressionS *exp)
1168 {
1169 char bytes[16];
1170 int i;
1171 char *this_frag;
1172 unsigned worst_case = relaxed_branch_length (NULL, NULL, 0);
1173 unsigned best_case = insn->size;
1174
1175 /* We only support relaxing branches, for the moment. */
1176 relax_substateT subtype
1177 = RELAX_BRANCH_ENCODE (insn->id == BPF_INSN_JAR,
1178 exp->X_op == O_constant,
1179 worst_case);
1180
1181 frag_grow (worst_case);
1182 this_frag = frag_more (0);
1183
1184 /* First encode the known parts of the instruction, including
1185 opcodes and constant immediates, and write them to the frag. */
1186 encode_insn (insn, bytes, 1 /* relax */);
1187 for (i = 0; i < insn->size; ++i)
1188 md_number_to_chars (this_frag + i, (valueT) bytes[i], 1);
1189
1190 /* Note that instruction fixups will be applied once the frag is
1191 relaxed, in md_convert_frag. */
1192 frag_var (rs_machine_dependent,
1193 worst_case, best_case,
1194 subtype, exp->X_add_symbol, exp->X_add_number /* offset */,
1195 NULL);
1196 }
1197
1198 \f
1199 /* Parse an operand expression. Returns the first character that is
1200 not part of the expression, or NULL in case of parse error.
1201
1202 See md_operand below to see how exp_parse_failed is used. */
1203
1204 static int exp_parse_failed = 0;
1205
1206 static char *
1207 parse_expression (char *s, expressionS *exp)
1208 {
1209 char *saved_input_line_pointer = input_line_pointer;
1210 char *saved_s = s;
1211
1212 exp_parse_failed = 0;
1213 input_line_pointer = s;
1214 expression (exp);
1215 s = input_line_pointer;
1216 input_line_pointer = saved_input_line_pointer;
1217
1218 switch (exp->X_op == O_absent || exp_parse_failed)
1219 return NULL;
1220
1221 /* The expression parser may consume trailing whitespaces. We have
1222 to undo that since the instruction templates may be expecting
1223 these whitespaces. */
1224 {
1225 char *p;
1226 for (p = s - 1; p >= saved_s && *p == ' '; --p)
1227 --s;
1228 }
1229
1230 return s;
1231 }
1232
1233 /* Parse a BPF register name and return the corresponding register
1234 number. Return NULL in case of parse error, or a pointer to the
1235 first character in S that is not part of the register name. */
1236
1237 static char *
1238 parse_bpf_register (char *s, char rw, uint8_t *regno)
1239 {
1240 if (asm_dialect == DIALECT_NORMAL)
1241 {
1242 rw = 'r';
1243 if (*s != '%')
1244 return NULL;
1245 s += 1;
1246
1247 if (*s == 'f' && *(s + 1) == 'p')
1248 {
1249 *regno = 10;
1250 s += 2;
1251 return s;
1252 }
1253 }
1254
1255 if (*s != rw)
1256 return NULL;
1257 s += 1;
1258
1259 if (*s == '1')
1260 {
1261 if (*(s + 1) == '0')
1262 {
1263 *regno = 10;
1264 s += 2;
1265 }
1266 else
1267 {
1268 *regno = 1;
1269 s += 1;
1270 }
1271 }
1272 else if (*s >= '0' && *s <= '9')
1273 {
1274 *regno = *s - '0';
1275 s += 1;
1276 }
1277
1278 return s;
1279 }
1280
1281 /* Collect a parse error message. */
1282
1283 static int partial_match_length = 0;
1284 static char *errmsg = NULL;
1285
1286 static void
1287 parse_error (int length, const char *fmt, ...)
1288 {
1289 if (length > partial_match_length)
1290 {
1291 va_list args;
1292
1293 free (errmsg);
1294 va_start (args, fmt);
1295 errmsg = xvasprintf (fmt, args);
1296 va_end (args);
1297 partial_match_length = length;
1298 }
1299 }
1300
1301 /* Assemble a machine instruction in STR and emit the frags/bytes it
1302 assembles to. */
1303
1304 void
1305 md_assemble (char *str ATTRIBUTE_UNUSED)
1306 {
1307 /* There are two different syntaxes that can be used to write BPF
1308 instructions. One is very conventional and like any other
1309 assembly language where each instruction is conformed by an
1310 instruction mnemonic followed by its operands. This is what we
1311 call the "normal" syntax. The other syntax tries to look like C
1312 statements. We have to support both syntaxes in this assembler.
1313
1314 One of the many nuisances introduced by this eccentricity is that
1315 in the pseudo-c syntax it is not possible to hash the opcodes
1316 table by instruction mnemonic, because there is none. So we have
1317 no other choice than to try to parse all instruction opcodes
1318 until one matches. This is slow.
1319
1320 Another problem is that emitting detailed diagnostics becomes
1321 tricky, since the lack of mnemonic means it is not clear what
1322 instruction was intended by the user, and we cannot emit
1323 diagnostics for every attempted template. So if an instruction
1324 is not parsed, we report the diagnostic corresponding to the
1325 partially parsed instruction that was matched further. */
1326
1327 unsigned int idx = 0;
1328 struct bpf_insn insn;
1329 const struct bpf_opcode *opcode;
1330
1331 /* Initialize the global diagnostic variables. See the parse_error
1332 function above. */
1333 partial_match_length = 0;
1334 errmsg = NULL;
1335
1336 #define PARSE_ERROR(...) parse_error (s - str, __VA_ARGS__)
1337
1338 while ((opcode = bpf_get_opcode (idx++)) != NULL)
1339 {
1340 const char *p;
1341 char *s;
1342 const char *template
1343 = (asm_dialect == DIALECT_PSEUDOC ? opcode->pseudoc : opcode->normal);
1344
1345 /* Do not try to match opcodes with a higher version than the
1346 selected ISA spec. */
1347 if (opcode->version > isa_spec)
1348 continue;
1349
1350 memset (&insn, 0, sizeof (struct bpf_insn));
1351 insn.size = 8;
1352 for (s = str, p = template; *p != '\0';)
1353 {
1354 if (*p == ' ')
1355 {
1356 /* Expect zero or more spaces. */
1357 while (*s != '\0' && (*s == ' ' || *s == '\t'))
1358 s += 1;
1359 p += 1;
1360 }
1361 else if (*p == '%')
1362 {
1363 if (*(p + 1) == '%')
1364 {
1365 if (*s != '%')
1366 {
1367 PARSE_ERROR ("expected '%%'");
1368 break;
1369 }
1370 p += 2;
1371 s += 1;
1372 }
1373 else if (*(p + 1) == 'w')
1374 {
1375 /* Expect zero or more spaces. */
1376 while (*s != '\0' && (*s == ' ' || *s == '\t'))
1377 s += 1;
1378 p += 2;
1379 }
1380 else if (*(p + 1) == 'W')
1381 {
1382 /* Expect one or more spaces. */
1383 if (*s != ' ' && *s != '\t')
1384 {
1385 PARSE_ERROR ("expected white space, got '%s'",
1386 s);
1387 break;
1388 }
1389 while (*s != '\0' && (*s == ' ' || *s == '\t'))
1390 s += 1;
1391 p += 2;
1392 }
1393 else if (strncmp (p, "%dr", 3) == 0)
1394 {
1395 uint8_t regno;
1396 char *news = parse_bpf_register (s, 'r', &regno);
1397
1398 if (news == NULL || (insn.has_dst && regno != insn.dst))
1399 {
1400 if (news != NULL)
1401 PARSE_ERROR ("expected register r%d, got r%d",
1402 insn.dst, regno);
1403 else
1404 PARSE_ERROR ("expected register name, got '%s'", s);
1405 break;
1406 }
1407 s = news;
1408 insn.dst = regno;
1409 insn.has_dst = 1;
1410 p += 3;
1411 }
1412 else if (strncmp (p, "%sr", 3) == 0)
1413 {
1414 uint8_t regno;
1415 char *news = parse_bpf_register (s, 'r', &regno);
1416
1417 if (news == NULL || (insn.has_src && regno != insn.src))
1418 {
1419 if (news != NULL)
1420 PARSE_ERROR ("expected register r%d, got r%d",
1421 insn.dst, regno);
1422 else
1423 PARSE_ERROR ("expected register name, got '%s'", s);
1424 break;
1425 }
1426 s = news;
1427 insn.src = regno;
1428 insn.has_src = 1;
1429 p += 3;
1430 }
1431 else if (strncmp (p, "%dw", 3) == 0)
1432 {
1433 uint8_t regno;
1434 char *news = parse_bpf_register (s, 'w', &regno);
1435
1436 if (news == NULL || (insn.has_dst && regno != insn.dst))
1437 {
1438 if (news != NULL)
1439 PARSE_ERROR ("expected register r%d, got r%d",
1440 insn.dst, regno);
1441 else
1442 PARSE_ERROR ("expected register name, got '%s'", s);
1443 break;
1444 }
1445 s = news;
1446 insn.dst = regno;
1447 insn.has_dst = 1;
1448 p += 3;
1449 }
1450 else if (strncmp (p, "%sw", 3) == 0)
1451 {
1452 uint8_t regno;
1453 char *news = parse_bpf_register (s, 'w', &regno);
1454
1455 if (news == NULL || (insn.has_src && regno != insn.src))
1456 {
1457 if (news != NULL)
1458 PARSE_ERROR ("expected register r%d, got r%d",
1459 insn.dst, regno);
1460 else
1461 PARSE_ERROR ("expected register name, got '%s'", s);
1462 break;
1463 }
1464 s = news;
1465 insn.src = regno;
1466 insn.has_src = 1;
1467 p += 3;
1468 }
1469 else if (strncmp (p, "%i32", 4) == 0
1470 || strncmp (p, "%I32", 4) == 0)
1471 {
1472 if (p[1] == 'I')
1473 {
1474 while (*s == ' ' || *s == '\t')
1475 s += 1;
1476 if (*s != '+' && *s != '-')
1477 {
1478 PARSE_ERROR ("expected `+' or `-', got `%c'", *s);
1479 break;
1480 }
1481 }
1482
1483 s = parse_expression (s, &insn.imm32);
1484 if (s == NULL)
1485 {
1486 PARSE_ERROR ("expected signed 32-bit immediate");
1487 break;
1488 }
1489 insn.has_imm32 = 1;
1490 p += 4;
1491 }
1492 else if (strncmp (p, "%o16", 4) == 0)
1493 {
1494 while (*s == ' ' || *s == '\t')
1495 s += 1;
1496 if (*s != '+' && *s != '-')
1497 {
1498 PARSE_ERROR ("expected `+' or `-', got `%c'", *s);
1499 break;
1500 }
1501
1502 s = parse_expression (s, &insn.offset16);
1503 if (s == NULL)
1504 {
1505 PARSE_ERROR ("expected signed 16-bit offset");
1506 break;
1507 }
1508 insn.has_offset16 = 1;
1509 p += 4;
1510 }
1511 else if (strncmp (p, "%d16", 4) == 0)
1512 {
1513 s = parse_expression (s, &insn.disp16);
1514 if (s == NULL)
1515 {
1516 PARSE_ERROR ("expected signed 16-bit displacement");
1517 break;
1518 }
1519 insn.has_disp16 = 1;
1520 insn.is_relaxable = 1;
1521 p += 4;
1522 }
1523 else if (strncmp (p, "%d32", 4) == 0)
1524 {
1525 s = parse_expression (s, &insn.disp32);
1526 if (s == NULL)
1527 {
1528 PARSE_ERROR ("expected signed 32-bit displacement");
1529 break;
1530 }
1531 insn.has_disp32 = 1;
1532 p += 4;
1533 }
1534 else if (strncmp (p, "%i64", 4) == 0)
1535 {
1536 s = parse_expression (s, &insn.imm64);
1537 if (s == NULL)
1538 {
1539 PARSE_ERROR ("expected signed 64-bit immediate");
1540 break;
1541 }
1542 insn.has_imm64 = 1;
1543 insn.size = 16;
1544 p += 4;
1545 }
1546 else
1547 as_fatal (_("invalid %%-tag in BPF opcode '%s'\n"), template);
1548 }
1549 else
1550 {
1551 /* Match a literal character. */
1552 if (*s != *p)
1553 {
1554 if (*s == '\0')
1555 PARSE_ERROR ("expected '%c'", *p);
1556 else if (*s == '%')
1557 {
1558 /* This is to workaround a bug in as_bad. */
1559 char tmp[3];
1560
1561 tmp[0] = '%';
1562 tmp[1] = '%';
1563 tmp[2] = '\0';
1564
1565 PARSE_ERROR ("expected '%c', got '%s'", *p, tmp);
1566 }
1567 else
1568 PARSE_ERROR ("expected '%c', got '%c'", *p, *s);
1569 break;
1570 }
1571 p += 1;
1572 s += 1;
1573 }
1574 }
1575
1576 if (*p == '\0')
1577 {
1578 /* Allow white spaces at the end of the line. */
1579 while (*s != '\0' && (*s == ' ' || *s == '\t'))
1580 s += 1;
1581 if (*s == '\0')
1582 /* We parsed an instruction successfully. */
1583 break;
1584 PARSE_ERROR ("extra junk at end of line");
1585 }
1586 }
1587
1588 if (opcode == NULL)
1589 {
1590 as_bad (_("unrecognized instruction `%s'"), str);
1591 if (errmsg != NULL)
1592 {
1593 as_bad ("%s", errmsg);
1594 free (errmsg);
1595 }
1596
1597 return;
1598 }
1599 insn.id = opcode->id;
1600 insn.opcode = opcode->opcode;
1601
1602 #undef PARSE_ERROR
1603
1604 /* Generate the frags and fixups for the parsed instruction. */
1605 if (do_relax && isa_spec >= BPF_V4 && insn.is_relaxable)
1606 {
1607 expressionS *relaxable_exp = NULL;
1608
1609 if (insn.has_disp16)
1610 relaxable_exp = &insn.disp16;
1611 else
1612 abort ();
1613
1614 add_relaxed_insn (&insn, relaxable_exp);
1615 }
1616 else
1617 add_fixed_insn (&insn);
1618
1619 /* Emit DWARF2 debugging information. */
1620 dwarf2_emit_insn (insn.size);
1621 }
1622
1623 /* Parse an operand that is machine-specific. */
1624
1625 void
1626 md_operand (expressionS *expressionP)
1627 {
1628 /* If this hook is invoked it means GAS failed to parse a generic
1629 expression. We should inhibit the as_bad in expr.c, so we can fail
1630 while parsing instruction alternatives. To do that, we change the
1631 expression to not have an O_absent. But then we also need to set
1632 exp_parse_failed to parse_expression above does the right thing. */
1633 ++input_line_pointer;
1634 expressionP->X_op = O_constant;
1635 expressionP->X_add_number = 0;
1636 exp_parse_failed = 1;
1637 }
1638
1639 symbolS *
1640 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1641 {
1642 return NULL;
1643 }
1644
1645 \f
1646 /* Turn a string in input_line_pointer into a floating point constant
1647 of type TYPE, and store the appropriate bytes in *LITP. The number
1648 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1649 returned, or NULL on OK. */
1650
1651 const char *
1652 md_atof (int type, char *litP, int *sizeP)
1653 {
1654 return ieee_md_atof (type, litP, sizeP, false);
1655 }
1656
1657 \f
1658 /* Determine whether the equal sign in the given string corresponds to
1659 a BPF instruction, i.e. when it is not to be considered a symbol
1660 assignment. */
1661
1662 bool
1663 bpf_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char *str ATTRIBUTE_UNUSED)
1664 {
1665 uint8_t regno;
1666
1667 /* Only pseudo-c instructions can have equal signs, and of these,
1668 all that could be confused with a symbol assignment all start
1669 with a register name. */
1670 if (asm_dialect == DIALECT_PSEUDOC)
1671 {
1672 char *w = parse_bpf_register (str, 'w', &regno);
1673 char *r = parse_bpf_register (str, 'r', &regno);
1674
1675 if ((w != NULL && *w == '\0')
1676 || (r != NULL && *r == '\0'))
1677 return 1;
1678 }
1679
1680 return 0;
1681 }