1 /*BEGIN_LEGAL
2 
3 Copyright (c) 2018 Intel Corporation
4 
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8 
9       http://www.apache.org/licenses/LICENSE-2.0
10 
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16 
17 END_LEGAL */
18 
19 // 2014-11-20: This file has code duplication with other parts of the XED
20 // code base. It is intended to be the bare minimum required to allow
21 // clients to use the ILD standalone (libxed-ild) library. Ultimately, I
22 // should remove the code duplication by splitting a few more of the files.
23 
24 #include "xed-types.h"
25 #include "xed-util.h"
26 #include "xed-portability.h"
27 #include "xed-portability-private.h"
28 #include "xed-common-defs.h"
29 #include "xed-common-hdrs.h"
30 #include "xed-state.h"
31 #include "xed-decoded-inst.h"
32 #include "xed-operand-accessors.h"
33 
34 #include <stdio.h>  //fprint, stderr
35 #include <string.h> //memset
36 
37 #if defined(__linux__) && defined(__STDC_HOSTED__) && __STDC_HOSTED__ == 0
38    extern void abort (void)  __attribute__ ((__noreturn__));
39 #else
40 #  include <stdlib.h>
41 #endif
42 
43 int xed_verbose=2;
44 #if defined(XED_MESSAGES)
45 FILE* xed_log_file;
46 #endif
47 
xed_derror(const char * s)48 void xed_derror(const char* s) {
49     XED2DIE((xed_log_file,"%s\n", s));
50     (void)s; //pacify compiler when msgs are disabled
51 }
52 
53 static xed_user_abort_function_t xed_user_abort_function = 0;
54 
55 
56 static void* xed_user_abort_other = 0;
57 
xed_register_abort_function(xed_user_abort_function_t fn,void * other)58 void xed_register_abort_function(xed_user_abort_function_t fn,
59                                  void* other) {
60     xed_user_abort_function = fn;
61     xed_user_abort_other = other;
62 }
63 
xed_internal_assert(const char * msg,const char * file,int line)64 void xed_internal_assert( const char* msg, const char* file, int line) {
65     if (xed_user_abort_function) {
66       (*xed_user_abort_function)(msg, file, line, xed_user_abort_other);
67     }
68     else {
69       fprintf(stderr,"ASSERTION FAILURE %s at %s:%d\n", msg, file, line);
70     }
71     abort();
72 }
73 
74 
75 
76 
xed_operand_values_set_mode(xed_operand_values_t * p,const xed_state_t * dstate)77 void xed_operand_values_set_mode(xed_operand_values_t* p,
78                                  const xed_state_t* dstate)  {
79 
80     /* set MODE, SMODE and REALMODE */
81     xed3_operand_set_realmode(p,0);
82     switch(xed_state_get_machine_mode(dstate))
83     {
84       case XED_MACHINE_MODE_LONG_64:
85         xed3_operand_set_mode(p,2);
86         xed3_operand_set_smode(p,2);
87         return;
88 
89       case XED_MACHINE_MODE_LEGACY_32:
90       case XED_MACHINE_MODE_LONG_COMPAT_32:
91         xed3_operand_set_mode(p,1);
92         break;
93 
94       case XED_MACHINE_MODE_REAL_16:
95         xed3_operand_set_realmode(p,1);
96         xed3_operand_set_mode(p,0);
97         break;
98 
99       case XED_MACHINE_MODE_LEGACY_16:
100       case XED_MACHINE_MODE_LONG_COMPAT_16:
101         xed3_operand_set_mode(p,0);
102         break;
103       default:
104         xed_derror("Bad machine mode in xed_operand_values_set_mode() call");
105     }
106 
107     // 64b mode returns above. this is for 16/32b modes only
108     switch(xed_state_get_stack_address_width(dstate))    {
109       case XED_ADDRESS_WIDTH_16b:
110         xed3_operand_set_smode(p,0);
111         break;
112       case XED_ADDRESS_WIDTH_32b:
113         xed3_operand_set_smode(p,1);
114         break;
115       default:
116         break;
117     }
118 }
119 
120 XED_DLL_EXPORT void
xed_decoded_inst_zero_set_mode(xed_decoded_inst_t * p,const xed_state_t * dstate)121 xed_decoded_inst_zero_set_mode(xed_decoded_inst_t* p,
122                                const xed_state_t* dstate)
123 {
124     memset(p, 0, sizeof(xed_decoded_inst_t));
125     xed_operand_values_set_mode(p,dstate);
126 }
127 
xed_set_verbosity(int v)128 void xed_set_verbosity(int v) {
129     xed_verbose = v;
130 }
131 
xed_set_log_file(void * o)132 void  xed_set_log_file(void* o) {
133 #if defined(XED_MESSAGES)
134     xed_log_file = (FILE*)o;
135 #else
136     (void)o;
137 #endif
138 
139 }
140 
141