HPCToolkit
x86-unwind-support.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
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 //************************* System Include Files ****************************
48 
49 #include <stdlib.h>
50 #include <stdio.h>
51 
52 #include <assert.h>
53 #include <ucontext.h>
54 
55 //*************************** User Include Files ****************************
56 
57 #include "x86-decoder.h"
58 #include "unwind.h"
59 
60 //*************************** Forward Declarations **************************
61 
62 
63 //***************************************************************************
64 // interface functions
65 //***************************************************************************
66 
67 static void*
68 actual_get_branch_target(void *ins, xed_decoded_inst_t *xptr,
69  xed_operand_values_t *vals)
70 {
71  int offset = xed_operand_values_get_branch_displacement_int32(vals);
72 
73  void *end_of_call_inst = ins + xed_decoded_inst_get_length(xptr);
74  void *target = end_of_call_inst + offset;
75  return target;
76 }
77 
78 
79 void *
80 x86_get_branch_target(void *ins, xed_decoded_inst_t *xptr)
81 {
82  const xed_inst_t *xi = xed_decoded_inst_inst(xptr);
83  const xed_operand_t *op0 = xed_inst_operand(xi, 0);
84  xed_operand_enum_t op0_name = xed_operand_name(op0);
85  xed_operand_type_enum_t op0_type = xed_operand_type(op0);
86 
87  if (op0_name == XED_OPERAND_RELBR &&
88  op0_type == XED_OPERAND_TYPE_IMM_CONST) {
89  xed_operand_values_t *vals = xed_decoded_inst_operands(xptr);
90 
91  if (xed_operand_values_has_branch_displacement(vals)) {
92  void *vaddr = actual_get_branch_target(ins, xptr, vals);
93  return vaddr;
94  }
95  }
96  return NULL;
97 }
98 
static void * actual_get_branch_target(void *ins, xed_decoded_inst_t *xptr, xed_operand_values_t *vals)
void * x86_get_branch_target(void *ins, xed_decoded_inst_t *xptr)
#define NULL
Definition: ElfHelper.cpp:85