HPCToolkit
x86ISABinutils.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 //************************* System Include Files ****************************
61 
62 #include <iostream>
63 using std::ostream;
64 
65 #include <cstdarg>
66 #include <cstring> // for 'memcpy'
67 
68 //*************************** User Include Files ****************************
69 
70 #include <include/gnu_dis-asm.h>
71 
72 #include "x86ISABinutils.hpp"
73 
75 
76 //*************************** macros ***************************
77 
78 
79 
80 //*************************** Forward Declarations ***************************
81 
82 
83 
84 //****************************************************************************
85 // x86ISA
86 //****************************************************************************
87 
89  : m_is_x86_64(is_x86_64), m_di(NULL), m_di_dis(NULL)
90 {
91  // See 'dis-asm.h'
92  m_di = new disassemble_info;
93  init_disassemble_info(m_di, stdout, GNUbu_fprintf_stub);
94  m_di->arch = bfd_arch_i386; // bfd_get_arch(abfd);
95  if (m_is_x86_64) { // bfd_get_mach(abfd); needed in print_insn()
96  m_di->mach = bfd_mach_x86_64;
97  }
98  else {
99  m_di->mach = bfd_mach_i386_i386;
100  }
101  m_di->endian = BFD_ENDIAN_LITTLE;
102  m_di->read_memory_func = GNUbu_read_memory; // vs. 'buffer_read_memory'
103  m_di->print_address_func = GNUbu_print_addr_stub; // vs. 'generic_print_addr'
104 
105  m_di_dis = new disassemble_info;
106  init_disassemble_info(m_di_dis, stdout, GNUbu_fprintf);
107  m_di_dis->application_data = (void*)&m_dis_data;
108  m_di_dis->arch = m_di->arch;
109  m_di_dis->mach = m_di->mach;
110  m_di_dis->endian = m_di->endian;
111  m_di_dis->read_memory_func = GNUbu_read_memory;
112  m_di_dis->print_address_func = GNU_print_addr;
113 }
114 
115 
117 {
118  delete m_di;
119  delete m_di_dis;
120 }
121 
122 
123 ushort
125 {
126  ushort size;
127  DecodingCache *cache;
128 
129  if ((cache = cacheLookup(mi)) == NULL) {
130  size = (ushort)print_insn_i386(PTR_TO_BFDVMA(mi), m_di);
131  cacheSet(mi, size);
132  }
133  else {
134  size = cache->insnSize;
135  }
136  return size;
137 }
138 
139 
143 {
144  ISA::InsnDesc d;
145 
146  if (cacheLookup(mi) == NULL) {
147  int size = print_insn_i386(PTR_TO_BFDVMA(mi), m_di);
148  cacheSet(mi, (ushort)size);
149  }
150 
151  switch(m_di->insn_type) {
152  case dis_noninsn:
154  break;
155  case dis_branch:
156  if (m_di->target != 0) {
158  }
159  else {
161  }
162  break;
163  case dis_condbranch:
164  if (m_di->target != 0) {
165  d.set(InsnDesc::INT_BR_COND_REL); // arbitrarily choose int
166  }
167  else {
168  d.set(InsnDesc::INT_BR_COND_IND); // arbitrarily choose int
169  }
170  break;
171  case dis_jsr:
172  if (m_di->target != 0) {
174  }
175  else {
177  }
178  break;
179  case dis_condjsr:
180  d.set(InsnDesc::OTHER);
181  break;
182 #if defined(HAVE_HPC_GNUBINUTILS)
183  case dis_return:
185  break;
186 #endif
187  case dis_dref:
188  case dis_dref2:
190  break;
191  default:
192  d.set(InsnDesc::OTHER);
193  break;
194  }
195  return d;
196 }
197 
198 
199 
200 
201 VMA
204 {
205  if (cacheLookup(mi) == NULL) {
206  int size = print_insn_i386(PTR_TO_BFDVMA(mi), m_di);
207  cacheSet(mi, (ushort)size);
208  }
209 
210  // The target field is only set on instructions with targets.
211  if (m_di->target != 0) {
212  return GNUvma2vma(m_di->target, mi, vma);
213  }
214  else {
215  return 0;
216  }
217 }
218 
219 
220 
221 void
223  ushort GCC_ATTR_UNUSED opIndex)
224 {
226  m_dis_data.insn_vma = vma;
227 
228  m_di_dis->stream = (void*)&os;
229  print_insn_i386(PTR_TO_BFDVMA(mi), m_di_dis);
230 }
231 
232 
233 //****************************************************************************
ushort insnSize
Definition: ISA.hpp:93
bfd_vma VMA
Definition: ISATypes.hpp:79
#define PTR_TO_BFDVMA(x)
Definition: ISATypes.hpp:94
int GNUbu_fprintf(void *stream, const char *format,...)
Definition: ISA.cpp:173
int GNUbu_fprintf_stub(void *GCC_ATTR_UNUSED stream, const char *GCC_ATTR_UNUSED format,...)
Definition: ISA.cpp:191
DecodingCache * cacheLookup(MachInsn *cmi)
Definition: ISA.hpp:489
virtual ~x86ISABinutils()
#define dis_return
Definition: gnu_dis-asm.h:79
virtual InsnDesc getInsnDesc(MachInsn *mi, ushort opIndex, ushort sz=0)
x86ISABinutils(bool is_x86_64=false)
virtual ushort getInsnSize(MachInsn *mi)
unsigned short int ushort
Definition: uint.h:120
virtual VMA getInsnTargetVMA(MachInsn *mi, VMA vma, ushort opIndex, ushort sz=0)
void set(IType t)
Definition: ISA.hpp:348
struct disassemble_info * m_di_dis
virtual void decode(std::ostream &os, MachInsn *mi, VMA vma, ushort opIndex)
int GNUbu_read_memory(bfd_vma vma, bfd_byte *myaddr, unsigned int len, struct disassemble_info *GCC_ATTR_UNUSED di)
Definition: ISA.cpp:206
void cacheSet(MachInsn *cmi, ushort size)
Definition: ISA.hpp:501
MachInsn * insn_addr
Definition: ISA.hpp:516
GNUbu_disdata m_dis_data
static VMA GNUvma2vma(bfd_vma di_vma, MachInsn *GCC_ATTR_UNUSED insn_addr, VMA insn_vma)
Definition: IA64ISA.cpp:92
void GNUbu_print_addr_stub(bfd_vma GCC_ATTR_UNUSED di_vma, struct disassemble_info *GCC_ATTR_UNUSED di)
Definition: ISA.cpp:199
VMA insn_vma
Definition: ISA.hpp:517
void MachInsn
Definition: ISATypes.hpp:87
#define NULL
Definition: ElfHelper.cpp:85
void GNU_print_addr(bfd_vma di_vma, struct disassemble_info *di)
Definition: x86ISA.cpp:82
struct disassemble_info * m_di
#define GCC_ATTR_UNUSED
Definition: gcc-attr.h:80
static MachInsn * mi
Definition: x86ISAXed.cpp:91