]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - opcodes/arm-dis.c
19990502 sourceware import
[thirdparty/binutils-gdb.git] / opcodes / arm-dis.c
CommitLineData
252b5132
RH
1/* Instruction printing code for the ARM
2 Copyright (C) 1994, 95, 96, 97, 1998 Free Software Foundation, Inc.
3 Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
4 Modification by James G. Smith (jsmith@cygnus.co.uk)
5
6This file is part of libopcodes.
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2 of the License, or (at your option)
11any later version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22#include "dis-asm.h"
23#define DEFINE_TABLE
24#include "arm-opc.h"
25#include "coff/internal.h"
26#include "libcoff.h"
27#include "opintl.h"
28
29/* FIXME: This shouldn't be done here */
30#include "elf-bfd.h"
31#include "elf/internal.h"
32#include "elf/arm.h"
33
34static char *arm_conditional[] =
35{"eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
36 "hi", "ls", "ge", "lt", "gt", "le", "", "nv"};
37
38static char *arm_regnames[] =
39{"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
40 "r8", "r9", "sl", "fp", "ip", "sp", "lr", "pc"};
41
42static char *arm_fp_const[] =
43{"0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0"};
44
45static char *arm_shift[] =
46{"lsl", "lsr", "asr", "ror"};
47
48static int print_insn_arm PARAMS ((bfd_vma, struct disassemble_info *,
49 long));
50
51static void
52arm_decode_shift (given, func, stream)
53 long given;
54 fprintf_ftype func;
55 void *stream;
56{
57 func (stream, "%s", arm_regnames[given & 0xf]);
58 if ((given & 0xff0) != 0)
59 {
60 if ((given & 0x10) == 0)
61 {
62 int amount = (given & 0xf80) >> 7;
63 int shift = (given & 0x60) >> 5;
64 if (amount == 0)
65 {
66 if (shift == 3)
67 {
68 func (stream, ", rrx");
69 return;
70 }
71 amount = 32;
72 }
73 func (stream, ", %s #%d", arm_shift[shift], amount);
74 }
75 else
76 func (stream, ", %s %s", arm_shift[(given & 0x60) >> 5],
77 arm_regnames[(given & 0xf00) >> 8]);
78 }
79}
80
81/* Print one instruction from PC on INFO->STREAM.
82 Return the size of the instruction (always 4 on ARM). */
83
84static int
85print_insn_arm (pc, info, given)
86 bfd_vma pc;
87 struct disassemble_info *info;
88 long given;
89{
90 struct arm_opcode * insn;
91 void * stream = info->stream;
92 fprintf_ftype func = info->fprintf_func;
93
94 for (insn = arm_opcodes; insn->assembler; insn++)
95 {
96 if ((given & insn->mask) == insn->value)
97 {
98 char * c;
99
100 for (c = insn->assembler; *c; c++)
101 {
102 if (*c == '%')
103 {
104 switch (*++c)
105 {
106 case '%':
107 func (stream, "%%");
108 break;
109
110 case 'a':
111 if (((given & 0x000f0000) == 0x000f0000)
112 && ((given & 0x02000000) == 0))
113 {
114 int offset = given & 0xfff;
115
116 func (stream, "[pc");
117
118 if (given & 0x01000000)
119 {
120 if ((given & 0x00800000) == 0)
121 offset = - offset;
122
123 /* pre-indexed */
124 func (stream, ", #%x]", offset);
125
126 offset += pc + 8;
127
128 /* Cope with the possibility of write-back being used.
129 Probably a very dangerous thing for the programmer
130 to do, but who are we to argue ? */
131 if (given & 0x00200000)
132 func (stream, "!");
133 }
134 else
135 {
136 /* post indexed */
137 func (stream, "], #%x", offset);
138
139 offset = pc + 8; /* ie ignore the offset */
140 }
141
142 func (stream, "\t; ");
143 info->print_address_func (offset, info);
144 }
145 else
146 {
147 func (stream, "[%s",
148 arm_regnames[(given >> 16) & 0xf]);
149 if ((given & 0x01000000) != 0)
150 {
151 if ((given & 0x02000000) == 0)
152 {
153 int offset = given & 0xfff;
154 if (offset)
155 func (stream, ", %s#%d",
156 (((given & 0x00800000) == 0)
157 ? "-" : ""), offset);
158 }
159 else
160 {
161 func (stream, ", %s",
162 (((given & 0x00800000) == 0)
163 ? "-" : ""));
164 arm_decode_shift (given, func, stream);
165 }
166
167 func (stream, "]%s",
168 ((given & 0x00200000) != 0) ? "!" : "");
169 }
170 else
171 {
172 if ((given & 0x02000000) == 0)
173 {
174 int offset = given & 0xfff;
175 if (offset)
176 func (stream, "], %s#%d",
177 (((given & 0x00800000) == 0)
178 ? "-" : ""), offset);
179 else
180 func (stream, "]");
181 }
182 else
183 {
184 func (stream, "], %s",
185 (((given & 0x00800000) == 0)
186 ? "-" : ""));
187 arm_decode_shift (given, func, stream);
188 }
189 }
190 }
191 break;
192
193 case 's':
194 if ((given & 0x004f0000) == 0x004f0000)
195 {
196 /* PC relative with immediate offset */
197 int offset = ((given & 0xf00) >> 4) | (given & 0xf);
198 if ((given & 0x00800000) == 0)
199 offset = -offset;
200 (*info->print_address_func)
201 (offset + pc + 8, info);
202 }
203 else
204 {
205 func (stream, "[%s",
206 arm_regnames[(given >> 16) & 0xf]);
207 if ((given & 0x01000000) != 0)
208 {
209 /* pre-indexed */
210 if ((given & 0x00400000) == 0x00400000)
211 {
212 /* immediate */
213 int offset = ((given & 0xf00) >> 4) | (given & 0xf);
214 if (offset)
215 func (stream, ", %s#%d",
216 (((given & 0x00800000) == 0)
217 ? "-" : ""), offset);
218 }
219 else
220 {
221 /* register */
222 func (stream, ", %s%s",
223 (((given & 0x00800000) == 0)
224 ? "-" : ""),
225 arm_regnames[given & 0xf]);
226 }
227
228 func (stream, "]%s",
229 ((given & 0x00200000) != 0) ? "!" : "");
230 }
231 else
232 {
233 /* post-indexed */
234 if ((given & 0x00400000) == 0x00400000)
235 {
236 /* immediate */
237 int offset = ((given & 0xf00) >> 4) | (given & 0xf);
238 if (offset)
239 func (stream, "], %s#%d",
240 (((given & 0x00800000) == 0)
241 ? "-" : ""), offset);
242 else
243 func (stream, "]");
244 }
245 else
246 {
247 /* register */
248 func (stream, "], %s%s",
249 (((given & 0x00800000) == 0)
250 ? "-" : ""),
251 arm_regnames[given & 0xf]);
252 }
253 }
254 }
255 break;
256
257 case 'b':
258 (*info->print_address_func)
259 (BDISP (given) * 4 + pc + 8, info);
260 break;
261
262 case 'c':
263 func (stream, "%s",
264 arm_conditional [(given >> 28) & 0xf]);
265 break;
266
267 case 'm':
268 {
269 int started = 0;
270 int reg;
271
272 func (stream, "{");
273 for (reg = 0; reg < 16; reg++)
274 if ((given & (1 << reg)) != 0)
275 {
276 if (started)
277 func (stream, ", ");
278 started = 1;
279 func (stream, "%s", arm_regnames[reg]);
280 }
281 func (stream, "}");
282 }
283 break;
284
285 case 'o':
286 if ((given & 0x02000000) != 0)
287 {
288 int rotate = (given & 0xf00) >> 7;
289 int immed = (given & 0xff);
290 func (stream, "#%d",
291 ((immed << (32 - rotate))
292 | (immed >> rotate)) & 0xffffffff);
293 }
294 else
295 arm_decode_shift (given, func, stream);
296 break;
297
298 case 'p':
299 if ((given & 0x0000f000) == 0x0000f000)
300 func (stream, "p");
301 break;
302
303 case 't':
304 if ((given & 0x01200000) == 0x00200000)
305 func (stream, "t");
306 break;
307
308 case 'h':
309 if ((given & 0x00000020) == 0x00000020)
310 func (stream, "h");
311 else
312 func (stream, "b");
313 break;
314
315 case 'A':
316 func (stream, "[%s", arm_regnames [(given >> 16) & 0xf]);
317 if ((given & 0x01000000) != 0)
318 {
319 int offset = given & 0xff;
320 if (offset)
321 func (stream, ", %s#%d]%s",
322 ((given & 0x00800000) == 0 ? "-" : ""),
323 offset * 4,
324 ((given & 0x00200000) != 0 ? "!" : ""));
325 else
326 func (stream, "]");
327 }
328 else
329 {
330 int offset = given & 0xff;
331 if (offset)
332 func (stream, "], %s#%d",
333 ((given & 0x00800000) == 0 ? "-" : ""),
334 offset * 4);
335 else
336 func (stream, "]");
337 }
338 break;
339
340 case 'C':
341 switch (given & 0x00090000)
342 {
343 default:
344 func (stream, "_???");
345 break;
346 case 0x90000:
347 func (stream, "_all");
348 break;
349 case 0x10000:
350 func (stream, "_ctl");
351 break;
352 case 0x80000:
353 func (stream, "_flg");
354 break;
355 }
356 break;
357
358 case 'F':
359 switch (given & 0x00408000)
360 {
361 case 0:
362 func (stream, "4");
363 break;
364 case 0x8000:
365 func (stream, "1");
366 break;
367 case 0x00400000:
368 func (stream, "2");
369 break;
370 default:
371 func (stream, "3");
372 }
373 break;
374
375 case 'P':
376 switch (given & 0x00080080)
377 {
378 case 0:
379 func (stream, "s");
380 break;
381 case 0x80:
382 func (stream, "d");
383 break;
384 case 0x00080000:
385 func (stream, "e");
386 break;
387 default:
388 func (stream, _("<illegal precision>"));
389 break;
390 }
391 break;
392 case 'Q':
393 switch (given & 0x00408000)
394 {
395 case 0:
396 func (stream, "s");
397 break;
398 case 0x8000:
399 func (stream, "d");
400 break;
401 case 0x00400000:
402 func (stream, "e");
403 break;
404 default:
405 func (stream, "p");
406 break;
407 }
408 break;
409 case 'R':
410 switch (given & 0x60)
411 {
412 case 0:
413 break;
414 case 0x20:
415 func (stream, "p");
416 break;
417 case 0x40:
418 func (stream, "m");
419 break;
420 default:
421 func (stream, "z");
422 break;
423 }
424 break;
425
426 case '0': case '1': case '2': case '3': case '4':
427 case '5': case '6': case '7': case '8': case '9':
428 {
429 int bitstart = *c++ - '0';
430 int bitend = 0;
431 while (*c >= '0' && *c <= '9')
432 bitstart = (bitstart * 10) + *c++ - '0';
433
434 switch (*c)
435 {
436 case '-':
437 c++;
438 while (*c >= '0' && *c <= '9')
439 bitend = (bitend * 10) + *c++ - '0';
440 if (!bitend)
441 abort ();
442 switch (*c)
443 {
444 case 'r':
445 {
446 long reg;
447 reg = given >> bitstart;
448 reg &= (2 << (bitend - bitstart)) - 1;
449 func (stream, "%s", arm_regnames[reg]);
450 }
451 break;
452 case 'd':
453 {
454 long reg;
455 reg = given >> bitstart;
456 reg &= (2 << (bitend - bitstart)) - 1;
457 func (stream, "%d", reg);
458 }
459 break;
460 case 'x':
461 {
462 long reg;
463 reg = given >> bitstart;
464 reg &= (2 << (bitend - bitstart)) - 1;
465 func (stream, "0x%08x", reg);
466 }
467 break;
468 case 'f':
469 {
470 long reg;
471 reg = given >> bitstart;
472 reg &= (2 << (bitend - bitstart)) - 1;
473 if (reg > 7)
474 func (stream, "#%s",
475 arm_fp_const[reg & 7]);
476 else
477 func (stream, "f%d", reg);
478 }
479 break;
480 default:
481 abort ();
482 }
483 break;
484 case '`':
485 c++;
486 if ((given & (1 << bitstart)) == 0)
487 func (stream, "%c", *c);
488 break;
489 case '\'':
490 c++;
491 if ((given & (1 << bitstart)) != 0)
492 func (stream, "%c", *c);
493 break;
494 case '?':
495 ++c;
496 if ((given & (1 << bitstart)) != 0)
497 func (stream, "%c", *c++);
498 else
499 func (stream, "%c", *++c);
500 break;
501 default:
502 abort ();
503 }
504 break;
505
506 default:
507 abort ();
508 }
509 }
510 }
511 else
512 func (stream, "%c", *c);
513 }
514 return 4;
515 }
516 }
517 abort ();
518}
519
520/* Print one instruction from PC on INFO->STREAM.
521 Return the size of the instruction. */
522
523static int
524print_insn_thumb (pc, info, given)
525 bfd_vma pc;
526 struct disassemble_info *info;
527 long given;
528{
529 struct thumb_opcode *insn;
530 void *stream = info->stream;
531 fprintf_ftype func = info->fprintf_func;
532
533 for (insn = thumb_opcodes; insn->assembler; insn++)
534 {
535 if ((given & insn->mask) == insn->value)
536 {
537 char *c = insn->assembler;
538
539 /* Special processing for Thumb 2 instruction BL sequence: */
540 if (!*c) /* check for empty (not NULL) assembler string */
541 {
542 info->bytes_per_chunk = 4;
543 info->bytes_per_line = 4;
544
545 func (stream, "%04x\tbl\t", given & 0xffff);
546 (*info->print_address_func)
547 (BDISP23 (given) * 2 + pc + 4, info);
548 return 4;
549 }
550 else
551 {
552 info->bytes_per_chunk = 2;
553 info->bytes_per_line = 4;
554
555 given &= 0xffff;
556 func (stream, "%04x\t", given);
557 for (; *c; c++)
558 {
559 if (*c == '%')
560 {
561 int domaskpc = 0;
562 int domasklr = 0;
563 switch (*++c)
564 {
565 case '%':
566 func (stream, "%%");
567 break;
568
569 case 'S':
570 {
571 long reg;
572 reg = (given >> 3) & 0x7;
573 if (given & (1 << 6))
574 reg += 8;
575 func (stream, "%s", arm_regnames[reg]);
576 }
577 break;
578
579 case 'D':
580 {
581 long reg;
582 reg = given & 0x7;
583 if (given & (1 << 7))
584 reg += 8;
585 func (stream, "%s", arm_regnames[reg]);
586 }
587 break;
588
589 case 'T':
590 func (stream, "%s",
591 arm_conditional [(given >> 8) & 0xf]);
592 break;
593
594 case 'N':
595 if (given & (1 << 8))
596 domasklr = 1;
597 /* fall through */
598 case 'O':
599 if (*c == 'O' && (given & (1 << 8)))
600 domaskpc = 1;
601 /* fall through */
602 case 'M':
603 {
604 int started = 0;
605 int reg;
606 func (stream, "{");
607 /* It would be nice if we could spot
608 ranges, and generate the rS-rE format: */
609 for (reg = 0; (reg < 8); reg++)
610 if ((given & (1 << reg)) != 0)
611 {
612 if (started)
613 func (stream, ", ");
614 started = 1;
615 func (stream, "%s", arm_regnames[reg]);
616 }
617
618 if (domasklr)
619 {
620 if (started)
621 func (stream, ", ");
622 started = 1;
623 func (stream, "lr");
624 }
625
626 if (domaskpc)
627 {
628 if (started)
629 func (stream, ", ");
630 func (stream, "pc");
631 }
632
633 func (stream, "}");
634 }
635 break;
636
637
638 case '0': case '1': case '2': case '3': case '4':
639 case '5': case '6': case '7': case '8': case '9':
640 {
641 int bitstart = *c++ - '0';
642 int bitend = 0;
643 while (*c >= '0' && *c <= '9')
644 bitstart = (bitstart * 10) + *c++ - '0';
645
646 switch (*c)
647 {
648 case '-':
649 {
650 long reg;
651 c++;
652 while (*c >= '0' && *c <= '9')
653 bitend = (bitend * 10) + *c++ - '0';
654 if (!bitend)
655 abort ();
656 reg = given >> bitstart;
657 reg &= (2 << (bitend - bitstart)) - 1;
658 switch (*c)
659 {
660 case 'r':
661 func (stream, "%s", arm_regnames[reg]);
662 break;
663
664 case 'd':
665 func (stream, "%d", reg);
666 break;
667
668 case 'H':
669 func (stream, "%d", reg << 1);
670 break;
671
672 case 'W':
673 func (stream, "%d", reg << 2);
674 break;
675
676 case 'a':
677 /* PC-relative address -- the bottom two
678 bits of the address are dropped before
679 the calculation. */
680 info->print_address_func
681 (((pc + 4) & ~3) + (reg << 2), info);
682 break;
683
684 case 'x':
685 func (stream, "0x%04x", reg);
686 break;
687
688 case 'I':
689 reg = ((reg ^ (1 << bitend)) - (1 << bitend));
690 func (stream, "%d", reg);
691 break;
692
693 case 'B':
694 reg = ((reg ^ (1 << bitend)) - (1 << bitend));
695 (*info->print_address_func)
696 (reg * 2 + pc + 4, info);
697 break;
698
699 default:
700 abort();
701 }
702 }
703 break;
704
705 case '\'':
706 c++;
707 if ((given & (1 << bitstart)) != 0)
708 func (stream, "%c", *c);
709 break;
710
711 case '?':
712 ++c;
713 if ((given & (1 << bitstart)) != 0)
714 func (stream, "%c", *c++);
715 else
716 func (stream, "%c", *++c);
717 break;
718
719 default:
720 abort();
721 }
722 }
723 break;
724
725 default:
726 abort ();
727 }
728 }
729 else
730 func (stream, "%c", *c);
731 }
732 }
733 return 2;
734 }
735 }
736
737 /* no match */
738 abort ();
739}
740
741/* NOTE: There are no checks in these routines that the relevant number of data bytes exist */
742
743int
744print_insn_big_arm (pc, info)
745 bfd_vma pc;
746 struct disassemble_info *info;
747{
748 unsigned char b[4];
749 long given;
750 int status;
751 coff_symbol_type *cs;
752 elf_symbol_type *es;
753 int is_thumb;
754
755 is_thumb = false;
756 if (info->symbols != NULL)
757 {
758 if (bfd_asymbol_flavour (*info->symbols) == bfd_target_coff_flavour)
759 {
760 cs = coffsymbol (*info->symbols);
761 is_thumb = (cs->native->u.syment.n_sclass == C_THUMBEXT
762 || cs->native->u.syment.n_sclass == C_THUMBSTAT
763 || cs->native->u.syment.n_sclass == C_THUMBLABEL
764 || cs->native->u.syment.n_sclass == C_THUMBEXTFUNC
765 || cs->native->u.syment.n_sclass == C_THUMBSTATFUNC);
766
767 }
768 else if (bfd_asymbol_flavour (*info->symbols) == bfd_target_elf_flavour)
769 {
770 es = *(elf_symbol_type **)(info->symbols);
771 is_thumb = ELF_ST_TYPE (es->internal_elf_sym.st_info) ==
772 STT_ARM_TFUNC;
773 }
774 }
775
776 info->bytes_per_chunk = 4;
777 info->display_endian = BFD_ENDIAN_BIG;
778
779 /* Always fetch word aligned values. */
780
781 status = (*info->read_memory_func) (pc & ~ 0x3, (bfd_byte *) &b[0], 4, info);
782 if (status != 0)
783 {
784 (*info->memory_error_func) (status, pc, info);
785 return -1;
786 }
787
788 if (is_thumb)
789 {
790 if (pc & 0x2)
791 {
792 given = (b[2] << 8) | b[3];
793
794 status = info->read_memory_func ((pc + 4) & ~ 0x3, (bfd_byte *) b, 4, info);
795 if (status != 0)
796 {
797 info->memory_error_func (status, pc + 4, info);
798 return -1;
799 }
800
801 given |= (b[0] << 24) | (b[1] << 16);
802 }
803 else
804 {
805 given = (b[0] << 8) | b[1] | (b[2] << 24) | (b[3] << 16);
806 }
807 }
808 else
809 {
810 given = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
811 }
812
813 if (is_thumb)
814 {
815 status = print_insn_thumb (pc, info, given);
816 }
817 else
818 {
819 status = print_insn_arm (pc, info, given);
820 }
821
822 return status;
823}
824
825int
826print_insn_little_arm (pc, info)
827 bfd_vma pc;
828 struct disassemble_info * info;
829{
830 unsigned char b[4];
831 long given;
832 int status;
833 coff_symbol_type *cs;
834 elf_symbol_type *es;
835 int is_thumb;
836
837 is_thumb = false;
838 if (info->symbols != NULL)
839 {
840 if (bfd_asymbol_flavour (*info->symbols) == bfd_target_coff_flavour)
841 {
842 cs = coffsymbol (*info->symbols);
843 is_thumb = (cs->native->u.syment.n_sclass == C_THUMBEXT
844 || cs->native->u.syment.n_sclass == C_THUMBSTAT
845 || cs->native->u.syment.n_sclass == C_THUMBLABEL
846 || cs->native->u.syment.n_sclass == C_THUMBEXTFUNC
847 || cs->native->u.syment.n_sclass == C_THUMBSTATFUNC);
848
849 }
850 else if (bfd_asymbol_flavour (*info->symbols) == bfd_target_elf_flavour)
851 {
852 es = *(elf_symbol_type **)(info->symbols);
853 is_thumb = ELF_ST_TYPE (es->internal_elf_sym.st_info) ==
854 STT_ARM_TFUNC;
855 }
856 }
857
858 info->bytes_per_chunk = 4;
859 info->display_endian = BFD_ENDIAN_LITTLE;
860
861 status = (*info->read_memory_func) (pc, (bfd_byte *) &b[0], 4, info);
862 if (status != 0 && is_thumb)
863 {
864 info->bytes_per_chunk = 2;
865
866 status = info->read_memory_func (pc, (bfd_byte *) b, 2, info);
867 b[3] = b[2] = 0;
868 }
869 if (status != 0)
870 {
871 (*info->memory_error_func) (status, pc, info);
872 return -1;
873 }
874
875 given = (b[0]) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
876
877 if (is_thumb)
878 {
879 status = print_insn_thumb (pc, info, given);
880 }
881 else
882 {
883 status = print_insn_arm (pc, info, given);
884 }
885
886 return status;
887}