HPCToolkit
x86ISAXed.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 "x86ISAXed.hpp"
73 
75 
76 extern "C" {
77 #include <xed-interface.h>
78 }
79 
80 //*************************** Global Variables ***************************
81 static xed_state_t xed_machine_state =
82 #if defined (HOST_CPU_x86_64)
83  { XED_MACHINE_MODE_LONG_64,
84  XED_ADDRESS_WIDTH_64b };
85 #else
86  { XED_MACHINE_MODE_LONG_COMPAT_32,
87  XED_ADDRESS_WIDTH_32b };
88 #endif
89 
90 //*************************** cache decoder ***************************
91 static MachInsn *mi;
92 static xed_decoded_inst_t xedd;
93 
94 static xed_decoded_inst_t*
96 {
97  if (cmi == mi) {
98  return &xedd;
99  }
100  else {
101  mi = cmi;
102 
103  xed_decoded_inst_t *xptr = &xedd;
104 
105  xed_decoded_inst_zero_set_mode(xptr, &xed_machine_state);
106  xed_decoded_inst_zero_keep_mode(xptr);
107 
108  xed_error_enum_t xed_error = xed_decode(xptr, (uint8_t*) mi, 15);
109  if (xed_error == XED_ERROR_NONE) {
110  return xptr;
111  }
112  else {
113  return NULL;
114  }
115  }
116 }
117 
118 //*************************** x86ISAXed ***************************
119 
120 x86ISAXed::x86ISAXed(bool is_x86_64)
121 {
122  xed_tables_init();
123 }
124 
125 
127 {
128 }
129 
130 
134 {
135  xed_decoded_inst_t *xptr = getDecodeXED(mi);
136 
137  ISA::InsnDesc d;
138 
139  if (xptr == NULL) {
140  return d;
141  }
142 
143  int offset;
144  xed_operand_values_t *vals;
145  xed_iclass_enum_t xiclass = xed_decoded_inst_get_iclass(xptr);
146 
147  switch(xiclass) {
148  case XED_ICLASS_INVALID:
150  break;
151 
152  // unconditional jump
153  case XED_ICLASS_JMP: case XED_ICLASS_JMP_FAR:
154  vals = xed_decoded_inst_operands(xptr);
155  offset = xed_operand_values_get_branch_displacement_int32(vals);
156  if (offset != 0) {
158  } else {
160  }
161  break;
162 
163  // return
164  case XED_ICLASS_RET_FAR: case XED_ICLASS_RET_NEAR:
166  break;
167 
168  // conditional branch
169  case XED_ICLASS_JB: case XED_ICLASS_JBE:
170  case XED_ICLASS_JL: case XED_ICLASS_JLE:
171  case XED_ICLASS_JNB: case XED_ICLASS_JNBE:
172  case XED_ICLASS_JNL: case XED_ICLASS_JNLE:
173  case XED_ICLASS_JNO: case XED_ICLASS_JNP:
174  case XED_ICLASS_JNS: case XED_ICLASS_JNZ:
175  case XED_ICLASS_JO: case XED_ICLASS_JP:
176  case XED_ICLASS_JRCXZ: case XED_ICLASS_JS:
177  case XED_ICLASS_JZ:
178  vals = xed_decoded_inst_operands(xptr);
179  offset = xed_operand_values_get_branch_displacement_int32(vals);
180  if (offset != 0) {
182  } else {
183  d.set(ISA::InsnDesc::INT_BR_COND_IND); // arbitrarily choose int
184  }
185  break;
186 
187  case XED_ICLASS_CALL_FAR:
188  case XED_ICLASS_CALL_NEAR:
189  vals = xed_decoded_inst_operands(xptr);
190  offset = xed_operand_values_get_branch_displacement_int32(vals);
191  if (offset != 0) {
193  } else {
195  }
196  break;
197 
198  default:
200  break;
201  }
202  return d;
203 }
204 
205 
206 
207 ushort
209 {
210  xed_decoded_inst_t *xptr = getDecodeXED(mi);
211 
212  if (xptr == NULL) {
213  return 0;
214  }
215  return (ushort)xed_decoded_inst_get_length(xptr);
216 }
217 
218 
219 VMA
222 {
223  xed_decoded_inst_t *xptr = getDecodeXED(mi);
224 
225  if (xptr != NULL) {
226  xed_operand_values_t *vals = xed_decoded_inst_operands(xptr);
227  int offset = xed_operand_values_get_branch_displacement_int32(vals);
228 
229  int len = xed_decoded_inst_get_length(xptr);
230  char* insn_end = (char*)mi + len;
231  VMA absoluteTarget = (VMA)(insn_end + offset);
232 
233  if (absoluteTarget != 0) {
234  VMA relativeTarget = GNUvma2vma(absoluteTarget, mi, vma);
235  return relativeTarget;
236  }
237  }
238  return 0;
239 }
240 
241 
242 //****************************************************************************
243 
static xed_decoded_inst_t * getDecodeXED(MachInsn *cmi)
Definition: x86ISAXed.cpp:95
bfd_vma VMA
Definition: ISATypes.hpp:79
static xed_state_t xed_machine_state
Definition: x86ISAXed.cpp:81
virtual VMA getInsnTargetVMA(MachInsn *mi, VMA vma, ushort opIndex, ushort sz=0)
Definition: x86ISAXed.cpp:220
unsigned short int ushort
Definition: uint.h:120
void set(IType t)
Definition: ISA.hpp:348
virtual ~x86ISAXed()
Definition: x86ISAXed.cpp:126
static xed_decoded_inst_t xedd
Definition: x86ISAXed.cpp:92
static VMA GNUvma2vma(bfd_vma di_vma, MachInsn *GCC_ATTR_UNUSED insn_addr, VMA insn_vma)
Definition: IA64ISA.cpp:92
void MachInsn
Definition: ISATypes.hpp:87
#define NULL
Definition: ElfHelper.cpp:85
virtual InsnDesc getInsnDesc(MachInsn *mi, ushort opIndex, ushort sz=0)
Definition: x86ISAXed.cpp:132
x86ISAXed(bool is_x86_64=false)
Definition: x86ISAXed.cpp:120
#define GCC_ATTR_UNUSED
Definition: gcc-attr.h:80
static MachInsn * mi
Definition: x86ISAXed.cpp:91
virtual ushort getInsnSize(MachInsn *mi)
Definition: x86ISAXed.cpp:208