]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - opcodes/disassemble.c
c8fda59b6e1f5a1e2c64c428a868cb660185442e
[thirdparty/binutils-gdb.git] / opcodes / disassemble.c
1 /* Select disassembly routine for specified architecture.
2 Copyright (C) 1994-2018 Free Software Foundation, Inc.
3
4 This file is part of the GNU opcodes library.
5
6 This library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "disassemble.h"
23 #include "safe-ctype.h"
24 #include <assert.h>
25
26 #ifdef ARCH_all
27 #define ARCH_aarch64
28 #define ARCH_alpha
29 #define ARCH_arc
30 #define ARCH_arm
31 #define ARCH_avr
32 #define ARCH_bfin
33 #define ARCH_cr16
34 #define ARCH_cris
35 #define ARCH_crx
36 #define ARCH_d10v
37 #define ARCH_d30v
38 #define ARCH_dlx
39 #define ARCH_epiphany
40 #define ARCH_fr30
41 #define ARCH_frv
42 #define ARCH_ft32
43 #define ARCH_h8300
44 #define ARCH_hppa
45 #define ARCH_i386
46 #define ARCH_ia64
47 #define ARCH_ip2k
48 #define ARCH_iq2000
49 #define ARCH_lm32
50 #define ARCH_m32c
51 #define ARCH_m32r
52 #define ARCH_m68hc11
53 #define ARCH_m68hc12
54 #define ARCH_m68k
55 #define ARCH_mcore
56 #define ARCH_mep
57 #define ARCH_metag
58 #define ARCH_microblaze
59 #define ARCH_mips
60 #define ARCH_mmix
61 #define ARCH_mn10200
62 #define ARCH_mn10300
63 #define ARCH_moxie
64 #define ARCH_mt
65 #define ARCH_msp430
66 #define ARCH_nds32
67 #define ARCH_nfp
68 #define ARCH_nios2
69 #define ARCH_ns32k
70 #define ARCH_or1k
71 #define ARCH_pdp11
72 #define ARCH_pj
73 #define ARCH_powerpc
74 #define ARCH_pru
75 #define ARCH_riscv
76 #define ARCH_rs6000
77 #define ARCH_rl78
78 #define ARCH_rx
79 #define ARCH_s390
80 #define ARCH_score
81 #define ARCH_sh
82 #define ARCH_sparc
83 #define ARCH_spu
84 #define ARCH_tic30
85 #define ARCH_tic4x
86 #define ARCH_tic54x
87 #define ARCH_tic6x
88 #define ARCH_tic80
89 #define ARCH_tilegx
90 #define ARCH_tilepro
91 #define ARCH_v850
92 #define ARCH_vax
93 #define ARCH_visium
94 #define ARCH_wasm32
95 #define ARCH_xstormy16
96 #define ARCH_xc16x
97 #define ARCH_xgate
98 #define ARCH_xtensa
99 #define ARCH_z80
100 #define ARCH_z8k
101 #endif
102
103 #ifdef ARCH_m32c
104 #include "m32c-desc.h"
105 #endif
106
107 disassembler_ftype
108 disassembler (enum bfd_architecture a,
109 bfd_boolean big ATTRIBUTE_UNUSED,
110 unsigned long mach ATTRIBUTE_UNUSED,
111 bfd *abfd ATTRIBUTE_UNUSED)
112 {
113 disassembler_ftype disassemble;
114
115 switch (a)
116 {
117 /* If you add a case to this table, also add it to the
118 ARCH_all definition right above this function. */
119 #ifdef ARCH_aarch64
120 case bfd_arch_aarch64:
121 disassemble = print_insn_aarch64;
122 break;
123 #endif
124 #ifdef ARCH_alpha
125 case bfd_arch_alpha:
126 disassemble = print_insn_alpha;
127 break;
128 #endif
129 #ifdef ARCH_arc
130 case bfd_arch_arc:
131 disassemble = arc_get_disassembler (abfd);
132 break;
133 #endif
134 #ifdef ARCH_arm
135 case bfd_arch_arm:
136 if (big)
137 disassemble = print_insn_big_arm;
138 else
139 disassemble = print_insn_little_arm;
140 break;
141 #endif
142 #ifdef ARCH_avr
143 case bfd_arch_avr:
144 disassemble = print_insn_avr;
145 break;
146 #endif
147 #ifdef ARCH_bfin
148 case bfd_arch_bfin:
149 disassemble = print_insn_bfin;
150 break;
151 #endif
152 #ifdef ARCH_cr16
153 case bfd_arch_cr16:
154 disassemble = print_insn_cr16;
155 break;
156 #endif
157 #ifdef ARCH_cris
158 case bfd_arch_cris:
159 disassemble = cris_get_disassembler (abfd);
160 break;
161 #endif
162 #ifdef ARCH_crx
163 case bfd_arch_crx:
164 disassemble = print_insn_crx;
165 break;
166 #endif
167 #ifdef ARCH_d10v
168 case bfd_arch_d10v:
169 disassemble = print_insn_d10v;
170 break;
171 #endif
172 #ifdef ARCH_d30v
173 case bfd_arch_d30v:
174 disassemble = print_insn_d30v;
175 break;
176 #endif
177 #ifdef ARCH_dlx
178 case bfd_arch_dlx:
179 /* As far as I know we only handle big-endian DLX objects. */
180 disassemble = print_insn_dlx;
181 break;
182 #endif
183 #ifdef ARCH_h8300
184 case bfd_arch_h8300:
185 if (mach == bfd_mach_h8300h || mach == bfd_mach_h8300hn)
186 disassemble = print_insn_h8300h;
187 else if (mach == bfd_mach_h8300s
188 || mach == bfd_mach_h8300sn
189 || mach == bfd_mach_h8300sx
190 || mach == bfd_mach_h8300sxn)
191 disassemble = print_insn_h8300s;
192 else
193 disassemble = print_insn_h8300;
194 break;
195 #endif
196 #ifdef ARCH_hppa
197 case bfd_arch_hppa:
198 disassemble = print_insn_hppa;
199 break;
200 #endif
201 #ifdef ARCH_i386
202 case bfd_arch_i386:
203 case bfd_arch_iamcu:
204 case bfd_arch_l1om:
205 case bfd_arch_k1om:
206 disassemble = print_insn_i386;
207 break;
208 #endif
209 #ifdef ARCH_ia64
210 case bfd_arch_ia64:
211 disassemble = print_insn_ia64;
212 break;
213 #endif
214 #ifdef ARCH_ip2k
215 case bfd_arch_ip2k:
216 disassemble = print_insn_ip2k;
217 break;
218 #endif
219 #ifdef ARCH_epiphany
220 case bfd_arch_epiphany:
221 disassemble = print_insn_epiphany;
222 break;
223 #endif
224 #ifdef ARCH_fr30
225 case bfd_arch_fr30:
226 disassemble = print_insn_fr30;
227 break;
228 #endif
229 #ifdef ARCH_lm32
230 case bfd_arch_lm32:
231 disassemble = print_insn_lm32;
232 break;
233 #endif
234 #ifdef ARCH_m32r
235 case bfd_arch_m32r:
236 disassemble = print_insn_m32r;
237 break;
238 #endif
239 #if defined(ARCH_m68hc11) || defined(ARCH_m68hc12) \
240 || defined(ARCH_9s12x) || defined(ARCH_m9s12xg)
241 case bfd_arch_m68hc11:
242 disassemble = print_insn_m68hc11;
243 break;
244 case bfd_arch_m68hc12:
245 disassemble = print_insn_m68hc12;
246 break;
247 case bfd_arch_m9s12x:
248 disassemble = print_insn_m9s12x;
249 break;
250 case bfd_arch_m9s12xg:
251 disassemble = print_insn_m9s12xg;
252 break;
253 #endif
254 #if defined(ARCH_s12z)
255 case bfd_arch_s12z:
256 disassemble = print_insn_s12z;
257 break;
258 #endif
259 #ifdef ARCH_m68k
260 case bfd_arch_m68k:
261 disassemble = print_insn_m68k;
262 break;
263 #endif
264 #ifdef ARCH_mt
265 case bfd_arch_mt:
266 disassemble = print_insn_mt;
267 break;
268 #endif
269 #ifdef ARCH_microblaze
270 case bfd_arch_microblaze:
271 disassemble = print_insn_microblaze;
272 break;
273 #endif
274 #ifdef ARCH_msp430
275 case bfd_arch_msp430:
276 disassemble = print_insn_msp430;
277 break;
278 #endif
279 #ifdef ARCH_nds32
280 case bfd_arch_nds32:
281 disassemble = print_insn_nds32;
282 break;
283 #endif
284 #ifdef ARCH_nfp
285 case bfd_arch_nfp:
286 disassemble = print_insn_nfp;
287 break;
288 #endif
289 #ifdef ARCH_ns32k
290 case bfd_arch_ns32k:
291 disassemble = print_insn_ns32k;
292 break;
293 #endif
294 #ifdef ARCH_mcore
295 case bfd_arch_mcore:
296 disassemble = print_insn_mcore;
297 break;
298 #endif
299 #ifdef ARCH_mep
300 case bfd_arch_mep:
301 disassemble = print_insn_mep;
302 break;
303 #endif
304 #ifdef ARCH_metag
305 case bfd_arch_metag:
306 disassemble = print_insn_metag;
307 break;
308 #endif
309 #ifdef ARCH_mips
310 case bfd_arch_mips:
311 if (big)
312 disassemble = print_insn_big_mips;
313 else
314 disassemble = print_insn_little_mips;
315 break;
316 #endif
317 #ifdef ARCH_mmix
318 case bfd_arch_mmix:
319 disassemble = print_insn_mmix;
320 break;
321 #endif
322 #ifdef ARCH_mn10200
323 case bfd_arch_mn10200:
324 disassemble = print_insn_mn10200;
325 break;
326 #endif
327 #ifdef ARCH_mn10300
328 case bfd_arch_mn10300:
329 disassemble = print_insn_mn10300;
330 break;
331 #endif
332 #ifdef ARCH_nios2
333 case bfd_arch_nios2:
334 if (big)
335 disassemble = print_insn_big_nios2;
336 else
337 disassemble = print_insn_little_nios2;
338 break;
339 #endif
340 #ifdef ARCH_or1k
341 case bfd_arch_or1k:
342 disassemble = print_insn_or1k;
343 break;
344 #endif
345 #ifdef ARCH_pdp11
346 case bfd_arch_pdp11:
347 disassemble = print_insn_pdp11;
348 break;
349 #endif
350 #ifdef ARCH_pj
351 case bfd_arch_pj:
352 disassemble = print_insn_pj;
353 break;
354 #endif
355 #ifdef ARCH_powerpc
356 case bfd_arch_powerpc:
357 #endif
358 #ifdef ARCH_rs6000
359 case bfd_arch_rs6000:
360 #endif
361 #if defined ARCH_powerpc || defined ARCH_rs6000
362 if (big)
363 disassemble = print_insn_big_powerpc;
364 else
365 disassemble = print_insn_little_powerpc;
366 break;
367 #endif
368 #ifdef ARCH_pru
369 case bfd_arch_pru:
370 disassemble = print_insn_pru;
371 break;
372 #endif
373 #ifdef ARCH_riscv
374 case bfd_arch_riscv:
375 disassemble = print_insn_riscv;
376 break;
377 #endif
378 #ifdef ARCH_rl78
379 case bfd_arch_rl78:
380 disassemble = rl78_get_disassembler (abfd);
381 break;
382 #endif
383 #ifdef ARCH_rx
384 case bfd_arch_rx:
385 disassemble = print_insn_rx;
386 break;
387 #endif
388 #ifdef ARCH_s390
389 case bfd_arch_s390:
390 disassemble = print_insn_s390;
391 break;
392 #endif
393 #ifdef ARCH_score
394 case bfd_arch_score:
395 if (big)
396 disassemble = print_insn_big_score;
397 else
398 disassemble = print_insn_little_score;
399 break;
400 #endif
401 #ifdef ARCH_sh
402 case bfd_arch_sh:
403 disassemble = print_insn_sh;
404 break;
405 #endif
406 #ifdef ARCH_sparc
407 case bfd_arch_sparc:
408 disassemble = print_insn_sparc;
409 break;
410 #endif
411 #ifdef ARCH_spu
412 case bfd_arch_spu:
413 disassemble = print_insn_spu;
414 break;
415 #endif
416 #ifdef ARCH_tic30
417 case bfd_arch_tic30:
418 disassemble = print_insn_tic30;
419 break;
420 #endif
421 #ifdef ARCH_tic4x
422 case bfd_arch_tic4x:
423 disassemble = print_insn_tic4x;
424 break;
425 #endif
426 #ifdef ARCH_tic54x
427 case bfd_arch_tic54x:
428 disassemble = print_insn_tic54x;
429 break;
430 #endif
431 #ifdef ARCH_tic6x
432 case bfd_arch_tic6x:
433 disassemble = print_insn_tic6x;
434 break;
435 #endif
436 #ifdef ARCH_tic80
437 case bfd_arch_tic80:
438 disassemble = print_insn_tic80;
439 break;
440 #endif
441 #ifdef ARCH_ft32
442 case bfd_arch_ft32:
443 disassemble = print_insn_ft32;
444 break;
445 #endif
446 #ifdef ARCH_v850
447 case bfd_arch_v850:
448 case bfd_arch_v850_rh850:
449 disassemble = print_insn_v850;
450 break;
451 #endif
452 #ifdef ARCH_wasm32
453 case bfd_arch_wasm32:
454 disassemble = print_insn_wasm32;
455 break;
456 #endif
457 #ifdef ARCH_xgate
458 case bfd_arch_xgate:
459 disassemble = print_insn_xgate;
460 break;
461 #endif
462 #ifdef ARCH_xstormy16
463 case bfd_arch_xstormy16:
464 disassemble = print_insn_xstormy16;
465 break;
466 #endif
467 #ifdef ARCH_xc16x
468 case bfd_arch_xc16x:
469 disassemble = print_insn_xc16x;
470 break;
471 #endif
472 #ifdef ARCH_xtensa
473 case bfd_arch_xtensa:
474 disassemble = print_insn_xtensa;
475 break;
476 #endif
477 #ifdef ARCH_z80
478 case bfd_arch_z80:
479 disassemble = print_insn_z80;
480 break;
481 #endif
482 #ifdef ARCH_z8k
483 case bfd_arch_z8k:
484 if (mach == bfd_mach_z8001)
485 disassemble = print_insn_z8001;
486 else
487 disassemble = print_insn_z8002;
488 break;
489 #endif
490 #ifdef ARCH_vax
491 case bfd_arch_vax:
492 disassemble = print_insn_vax;
493 break;
494 #endif
495 #ifdef ARCH_visium
496 case bfd_arch_visium:
497 disassemble = print_insn_visium;
498 break;
499 #endif
500 #ifdef ARCH_frv
501 case bfd_arch_frv:
502 disassemble = print_insn_frv;
503 break;
504 #endif
505 #ifdef ARCH_moxie
506 case bfd_arch_moxie:
507 disassemble = print_insn_moxie;
508 break;
509 #endif
510 #ifdef ARCH_iq2000
511 case bfd_arch_iq2000:
512 disassemble = print_insn_iq2000;
513 break;
514 #endif
515 #ifdef ARCH_m32c
516 case bfd_arch_m32c:
517 disassemble = print_insn_m32c;
518 break;
519 #endif
520 #ifdef ARCH_tilegx
521 case bfd_arch_tilegx:
522 disassemble = print_insn_tilegx;
523 break;
524 #endif
525 #ifdef ARCH_tilepro
526 case bfd_arch_tilepro:
527 disassemble = print_insn_tilepro;
528 break;
529 #endif
530 default:
531 return 0;
532 }
533 return disassemble;
534 }
535
536 void
537 disassembler_usage (FILE *stream ATTRIBUTE_UNUSED)
538 {
539 #ifdef ARCH_aarch64
540 print_aarch64_disassembler_options (stream);
541 #endif
542 #ifdef ARCH_arc
543 print_arc_disassembler_options (stream);
544 #endif
545 #ifdef ARCH_arm
546 print_arm_disassembler_options (stream);
547 #endif
548 #ifdef ARCH_mips
549 print_mips_disassembler_options (stream);
550 #endif
551 #ifdef ARCH_nfp
552 print_nfp_disassembler_options (stream);
553 #endif
554 #ifdef ARCH_powerpc
555 print_ppc_disassembler_options (stream);
556 #endif
557 #ifdef ARCH_riscv
558 print_riscv_disassembler_options (stream);
559 #endif
560 #ifdef ARCH_i386
561 print_i386_disassembler_options (stream);
562 #endif
563 #ifdef ARCH_s390
564 print_s390_disassembler_options (stream);
565 #endif
566 #ifdef ARCH_wasm32
567 print_wasm32_disassembler_options (stream);
568 #endif
569
570 return;
571 }
572
573 void
574 disassemble_init_for_target (struct disassemble_info * info)
575 {
576 if (info == NULL)
577 return;
578
579 switch (info->arch)
580 {
581 #ifdef ARCH_aarch64
582 case bfd_arch_aarch64:
583 info->symbol_is_valid = aarch64_symbol_is_valid;
584 info->disassembler_needs_relocs = TRUE;
585 break;
586 #endif
587 #ifdef ARCH_arm
588 case bfd_arch_arm:
589 info->symbol_is_valid = arm_symbol_is_valid;
590 info->disassembler_needs_relocs = TRUE;
591 break;
592 #endif
593 #ifdef ARCH_ia64
594 case bfd_arch_ia64:
595 info->skip_zeroes = 16;
596 break;
597 #endif
598 #ifdef ARCH_tic4x
599 case bfd_arch_tic4x:
600 info->skip_zeroes = 32;
601 break;
602 #endif
603 #ifdef ARCH_mep
604 case bfd_arch_mep:
605 info->skip_zeroes = 256;
606 info->skip_zeroes_at_end = 0;
607 break;
608 #endif
609 #ifdef ARCH_metag
610 case bfd_arch_metag:
611 info->disassembler_needs_relocs = TRUE;
612 break;
613 #endif
614 #ifdef ARCH_m32c
615 case bfd_arch_m32c:
616 /* This processor in fact is little endian. The value set here
617 reflects the way opcodes are written in the cgen description. */
618 info->endian = BFD_ENDIAN_BIG;
619 if (! info->insn_sets)
620 {
621 info->insn_sets = cgen_bitset_create (ISA_MAX);
622 if (info->mach == bfd_mach_m16c)
623 cgen_bitset_set (info->insn_sets, ISA_M16C);
624 else
625 cgen_bitset_set (info->insn_sets, ISA_M32C);
626 }
627 break;
628 #endif
629 #ifdef ARCH_pru
630 case bfd_arch_pru:
631 info->disassembler_needs_relocs = TRUE;
632 break;
633 #endif
634 #ifdef ARCH_powerpc
635 case bfd_arch_powerpc:
636 #endif
637 #ifdef ARCH_rs6000
638 case bfd_arch_rs6000:
639 #endif
640 #if defined (ARCH_powerpc) || defined (ARCH_rs6000)
641 disassemble_init_powerpc (info);
642 break;
643 #endif
644 #ifdef ARCH_wasm32
645 case bfd_arch_wasm32:
646 disassemble_init_wasm32 (info);
647 break;
648 #endif
649 #ifdef ARCH_s390
650 case bfd_arch_s390:
651 disassemble_init_s390 (info);
652 break;
653 #endif
654 default:
655 break;
656 }
657 }
658
659 /* Remove whitespace and consecutive commas from OPTIONS. */
660
661 char *
662 remove_whitespace_and_extra_commas (char *options)
663 {
664 char *str;
665 size_t i, len;
666
667 if (options == NULL)
668 return NULL;
669
670 /* Strip off all trailing whitespace and commas. */
671 for (len = strlen (options); len > 0; len--)
672 {
673 if (!ISSPACE (options[len - 1]) && options[len - 1] != ',')
674 break;
675 options[len - 1] = '\0';
676 }
677
678 /* Convert all remaining whitespace to commas. */
679 for (i = 0; options[i] != '\0'; i++)
680 if (ISSPACE (options[i]))
681 options[i] = ',';
682
683 /* Remove consecutive commas. */
684 for (str = options; *str != '\0'; str++)
685 if (*str == ',' && (*(str + 1) == ',' || str == options))
686 {
687 char *next = str + 1;
688 while (*next == ',')
689 next++;
690 len = strlen (next);
691 if (str != options)
692 str++;
693 memmove (str, next, len);
694 next[len - (size_t)(next - str)] = '\0';
695 }
696 return (strlen (options) != 0) ? options : NULL;
697 }
698
699 /* Like STRCMP, but treat ',' the same as '\0' so that we match
700 strings like "foobar" against "foobar,xxyyzz,...". */
701
702 int
703 disassembler_options_cmp (const char *s1, const char *s2)
704 {
705 unsigned char c1, c2;
706
707 do
708 {
709 c1 = (unsigned char) *s1++;
710 if (c1 == ',')
711 c1 = '\0';
712 c2 = (unsigned char) *s2++;
713 if (c2 == ',')
714 c2 = '\0';
715 if (c1 == '\0')
716 return c1 - c2;
717 }
718 while (c1 == c2);
719
720 return c1 - c2;
721 }