Debian Codegraph @

src/fcml_intel_rend.c

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2020 Slawomir Wojtasiak
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /* There is a bit of code duplication between GAS and Intel instruction renderers.
21 * It evaluated up to this point due to historical reasons and it will be probably fixed in
22 * the near future by providing one generic customizable rendering template as a base for
23 * instruction renderers.
24 */
25
26 #include "fcml_intel_rend.h"
27
28 #include <stdio.h>
29
30 #include <fcml_types.h>
31 #include <fcml_renderer.h>
32 #include <fcml_common.h>
33 #include <fcml_dialect.h>
34 #include <fcml_disassembler.h>
35
36 #include "fcml_common_dialect.h"
37 #include "fcml_mnemonic_parser.h"
38 #include "fcml_ceh.h"
39 #include "fcml_dialect_int.h"
40 #include "fcml_env_int.h"
41 #include "fcml_rend_utils.h"
42 #include "fcml_stream.h"
43 #include "fcml_utils.h"
44
45 #define FCML_REND_LOCAL_BUFFER_SIZE 512
46
47 static fcml_string integer_formats[6][4] = {
48 /* Signed integer values.*/
49 {
50 FCML_PRI_INT8_DEC, FCML_PRI_INT16_DEC, FCML_PRI_INT32_DEC,
51 FCML_PRI_INT64_DEC
52 },
53 /* Unsigned integer values.*/
54 {
55 FCML_PRI_UINT8_DEC, FCML_PRI_UINT16_DEC, FCML_PRI_UINT32_DEC,
56 FCML_PRI_UINT64_DEC
57 },
58 /* Signed hex values.*/
59 {
60 FCML_PRI_INT8_HEX FCML_TEXT("h"),
61 FCML_PRI_INT16_HEX FCML_TEXT("h"),
62 FCML_PRI_INT32_HEX FCML_TEXT("h"),
63 FCML_PRI_INT64_HEX FCML_TEXT("h")
64 },
65 /* Unsigned hex values.*/
66 {
67 FCML_PRI_INT8_HEX FCML_TEXT("h"),
68 FCML_PRI_INT16_HEX FCML_TEXT("h"),
69 FCML_PRI_INT32_HEX FCML_TEXT("h"),
70 FCML_PRI_INT64_HEX FCML_TEXT("h")
71 },
72 /* Signed hex values without leading zeros. */
73 {
74 FCML_PRI_INT8_HEX_NO_ZEROS FCML_TEXT("h"),
75 FCML_PRI_INT16_HEX_NO_ZEROS FCML_TEXT("h"),
76 FCML_PRI_INT32_HEX_NO_ZEROS FCML_TEXT("h"),
77 FCML_PRI_INT64_HEX_NO_ZEROS FCML_TEXT("h")
78 },
79 /* Unsigned hex values without leading zeros. */
80 {
81 FCML_PRI_INT8_HEX_NO_ZEROS FCML_TEXT("h"),
82 FCML_PRI_INT16_HEX_NO_ZEROS FCML_TEXT("h"),
83 FCML_PRI_INT32_HEX_NO_ZEROS FCML_TEXT("h"),
84 FCML_PRI_INT64_HEX_NO_ZEROS FCML_TEXT("h")
85 }
86 };
87
88 static fcml_ceh_error operand_renderer_imm(
89 const fcml_st_render_config *config,
90 fcml_st_memory_stream *output_stream,
91 const fcml_st_operand *operand) {
92 const fcml_st_integer *immediate = &(operand->immediate);
93 return fcml_fn_rend_utils_format_append_integer(
94 integer_formats, output_stream,
95 immediate, config->render_flags & FCML_REND_FLAG_HEX_IMM,
96 config->render_flags & FCML_REND_FLAG_REMOVE_LEADING_ZEROS);
97 }
98
99 static fcml_ceh_error operand_renderer_reg(
100 const fcml_st_dialect_context_int *dialect_context,
101 fcml_st_memory_stream *output_stream,
102 const fcml_st_disassembler_result *result,
103 const fcml_st_operand *operand) {
104 fcml_fn_rend_utils_format_append_reg(dialect_context, output_stream,
105 &(operand->reg),
106 result->instruction_details.prefixes_details.is_rex);
107 return FCML_CEH_GEC_NO_ERROR;
108 }
109
110 static fcml_ceh_error rend_size_operator(fcml_usize size_operator,
111 fcml_string buffer, fcml_usize buffer_len,
112 fcml_bool is_media_instruction) {
113
114 fcml_string size_operator_printable = NULL;
115
116 switch (size_operator) {
117 case 0:
118 break;
119 case 8:
120 size_operator_printable = FCML_TEXT("byte ptr ");
121 break;
122 case 16:
123 size_operator_printable = FCML_TEXT("word ptr ");
124 break;
125 case 32:
126 size_operator_printable = FCML_TEXT("dword ptr ");
127 break;
128 case 48:
129 size_operator_printable = FCML_TEXT("fword ptr ");
130 break;
131 case 64:
132 size_operator_printable = is_media_instruction ?
133 FCML_TEXT("mmword ptr ") : FCML_TEXT("qword ptr ");
134 break;
135 case 80:
136 size_operator_printable = FCML_TEXT("tbyte ptr ");
137 break;
138 case 128:
139 size_operator_printable = is_media_instruction ?
140 FCML_TEXT("xmmword ptr ") : FCML_TEXT("oword ptr ");
141 break;
142 case 256:
143 size_operator_printable = is_media_instruction ?
144 FCML_TEXT("ymmword ptr ") : FCML_TEXT("qqword ");
145 break;
146 case 512:
147 size_operator_printable = FCML_TEXT("zmmword ptr ");
148 break;
149 default:
150 fcml_fn_env_str_snprintf(buffer, buffer_len, FCML_TEXT("%dbyte ptr "),
151 size_operator / 8);
152 break;
153 }
154
155 if (size_operator_printable) {
156 fcml_fn_env_str_strncpy(buffer, size_operator_printable, buffer_len);
157 }
158
159 return FCML_CEH_GEC_NO_ERROR;
160 }
161
162 static fcml_ceh_error operand_renderer_address(
163 const fcml_st_render_config *config,
164 const fcml_st_dialect_context_int *dialect_context,
165 fcml_st_memory_stream *output_stream,
166 const fcml_st_disassembler_result *result,
167 const fcml_st_operand *operand) {
168
169 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
170 const fcml_st_address *address = &(operand->address);
171 const fcml_st_prefixes_details *prefixes =
172 &(result->instruction_details.prefixes_details);
173 fcml_hints hints = operand->hints;
174 fcml_uint32_t render_flags = config->render_flags;
175
176 if (hints & FCML_OP_HINT_DISPLACEMENT_RELATIVE_ADDRESS) {
177
178 fcml_st_integer integer;
179 fcml_ceh_error error = fcml_fn_utils_offset_to_integer(
180 &(address->offset), &integer);
181 if (error) {
182 return error;
183 }
184
185 error = fcml_fn_rend_utils_format_append_integer(
186 integer_formats, output_stream,
187 &integer, FCML_TRUE,
188 config->render_flags & FCML_REND_FLAG_REMOVE_LEADING_ZEROS);
189
190 return error;
191 }
192
193 if (address->size_operator > 0) {
194 fcml_char buffer[32] = { 0 };
195 rend_size_operator(address->size_operator, buffer, sizeof(buffer),
196 operand->hints & FCML_OP_HINT_MULTIMEDIA_INSTRUCTION);
197 fcml_fn_rend_utils_format_append_str(output_stream, buffer);
198 }
199
200 if (!address->segment_selector.is_default_reg
201 || (render_flags & FCML_REND_FLAG_RENDER_DEFAULT_SEG)) {
202 fcml_fn_rend_utils_format_append_reg(dialect_context, output_stream,
203 &(address->segment_selector.segment_selector),
204 prefixes->is_rex);
205 fcml_fn_rend_utils_format_append_str(output_stream, FCML_TEXT(":"));
206 }
207
208 fcml_fn_rend_utils_format_append_str(output_stream, FCML_TEXT("["));
209
210 const fcml_st_effective_address *effective_address =
211 &(address->effective_address);
212
213 fcml_bool first = FCML_TRUE;
214
215 /* Adds SIB hints to all instructions where SIB presents.*/
216 if ((render_flags & FCML_REND_FLAG_RENDER_SIB_HINT)
217 && (result->instruction_details.modrm_details.sib.is_not_null)) {
218 fcml_fn_rend_utils_format_append_str(output_stream, FCML_TEXT("sib "));
219 }
220
221 if ((render_flags & FCML_REND_FLAG_RENDER_ABS_HINT)
222 && (operand->hints & FCML_OP_HINT_ABSOLUTE_ADDRESSING)) {
223 fcml_fn_rend_utils_format_append_str(output_stream, FCML_TEXT("abs "));
224 }
225
226 if ((render_flags & FCML_REND_FLAG_RENDER_REL_HINT)
227 && (operand->hints & FCML_OP_HINT_RELATIVE_ADDRESSING)) {
228 fcml_fn_rend_utils_format_append_str(output_stream, FCML_TEXT("rel "));
229 }
230
231 if (address->address_form == FCML_AF_COMBINED) {
232
233 /* Append base register.*/
234 if (!is_reg_undef(&(effective_address->base))) {
235 fcml_fn_rend_utils_format_append_reg(dialect_context, output_stream,
236 &(effective_address->base), prefixes->is_rex);
237 first = FCML_FALSE;
238 }
239
240 /* Append index register.*/
241 if (!is_reg_undef(&(effective_address->index))) {
242 fcml_fn_rend_utils_format_append_str_if(output_stream,
243 FCML_TEXT("+"), !first);
244 fcml_fn_rend_utils_format_append_reg(dialect_context, output_stream,
245 &(effective_address->index), prefixes->is_rex);
246 first = FCML_FALSE;
247 }
248
249 /* Append scale.*/
250 if (effective_address->scale_factor > 0) {
251
252 fcml_fn_rend_utils_format_append_str_if(output_stream,
253 FCML_TEXT("*"), !first);
254 first = FCML_FALSE;
255
256 fcml_st_integer scale_value = { 0 };
257 scale_value.is_signed = FCML_FALSE;
258 scale_value.size = 8;
259 scale_value.int8 = (fcml_int8_t) effective_address->scale_factor;
260
261 fcml_fn_rend_utils_format_append_integer(
262 integer_formats, output_stream,
263 &scale_value, FCML_FALSE, FCML_FALSE);
264 }
265
266 /* Displacement.*/
267 if (effective_address->displacement.size > 0) {
268
269 if ((render_flags & FCML_REND_FLAG_HEX_DISPLACEMENT)
270 || !fcml_fn_utils_is_disp_neg(
271 &(effective_address->displacement))) {
272 fcml_fn_rend_utils_format_append_str_if(output_stream,
273 FCML_TEXT("+"), !first);
274 first = FCML_FALSE;
275 }
276
277 error = fcml_fn_rend_utils_format_append_integer(
278 integer_formats, output_stream,
279 &(effective_address->displacement),
280 config->render_flags & FCML_REND_FLAG_HEX_DISPLACEMENT,
281 config->render_flags & FCML_REND_FLAG_REMOVE_LEADING_ZEROS);
282 }
283
284 } else if (address->address_form == FCML_AF_OFFSET) {
285
286 /* Offset.*/
287
288 fcml_st_integer integer;
289 fcml_ceh_error error = fcml_fn_utils_offset_to_integer(
290 &(address->offset), &integer);
291 if (error) {
292 return error;
293 }
294
295 error = fcml_fn_rend_utils_format_append_integer(
296 integer_formats, output_stream,
297 &integer, FCML_TRUE,
298 config->render_flags & FCML_REND_FLAG_REMOVE_LEADING_ZEROS);
299 } else {
300
301 /* Unknown address form. */
302
303 error = FCML_CEH_GEC_INVALID_INPUT;
304 }
305
306 fcml_fn_rend_utils_format_append_str(output_stream, FCML_TEXT("]"));
307
308 return error;
309 }
310
311 static fcml_ceh_error operand_renderer_far_pointer(
312 const fcml_st_render_config *config,
313 fcml_st_memory_stream *output_stream,
314 const fcml_st_operand *operand) {
315
316 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
317
318 fcml_st_integer integer = { 0 };
319 integer.is_signed = FCML_TRUE;
320 integer.size = 16;
321 integer.int16 = operand->far_pointer.segment;
322
323 fcml_fn_rend_utils_format_append_integer(
324 integer_formats, output_stream, &integer,
325 FCML_TRUE,
326 config->render_flags & FCML_REND_FLAG_REMOVE_LEADING_ZEROS);
327
328 fcml_fn_rend_utils_format_append_str(output_stream, FCML_TEXT(":"));
329
330 integer.size = operand->far_pointer.offset_size;
331 switch (integer.size) {
332 case FCML_DS_16:
333 integer.int16 = operand->far_pointer.offset16;
334 break;
335 case FCML_DS_32:
336 integer.int32 = operand->far_pointer.offset32;
337 break;
338 }
339
340 fcml_fn_rend_utils_format_append_integer(
341 integer_formats, output_stream, &integer,
342 FCML_TRUE,
343 config->render_flags & FCML_REND_FLAG_REMOVE_LEADING_ZEROS);
344
345 return error;
346 }
347
348 static fcml_ceh_error print_operand(const fcml_st_render_config *config,
349 const fcml_st_dialect_context_int *dialect_context,
350 fcml_st_memory_stream *output_stream,
351 const fcml_st_disassembler_result *result, fcml_int operand_index) {
352
353 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
354
355 const fcml_st_operand *operand =
356 &(result->instruction.operands[operand_index]);
357 switch (operand->type) {
358 case FCML_OT_IMMEDIATE:
359 error = operand_renderer_imm(config, output_stream, operand);
360 break;
361 case FCML_OT_FAR_POINTER:
362 error = operand_renderer_far_pointer(config, output_stream, operand);
363 break;
364 case FCML_OT_ADDRESS:
365 error = operand_renderer_address(config,
366 dialect_context, output_stream, result, operand);
367 break;
368 case FCML_OT_REGISTER:
369 error = operand_renderer_reg(dialect_context, output_stream, result,
370 operand);
371 break;
372 case FCML_OT_VIRTUAL:
373 break;
374 case FCML_OT_NONE:
375 break;
376 default:
377 error = FCML_CEH_GEC_INVALID_INPUT;
378 }
379
380 /* Print operand attributes just after the operand. */
381 if (!error) {
382 fcml_fn_rend_utils_format_append_operand_decorators(dialect_context,
383 output_stream, &(operand->decorators));
384 }
385
386 return error;
387 }
388
389 static fcml_string resolve_mnemonic(const fcml_st_disassembler_result *result) {
390
391 const fcml_st_instruction *instruction = &(result->instruction);
392
393 if (instruction->mnemonic) {
394 // If there is mnemonic already available use it instead on resolving it again.
395 return instruction->mnemonic;
396 } else {
397 return NULL;
398 }
399 }
400
401 fcml_ceh_error fcml_fn_rend_render_instruction_intel(
402 const fcml_st_dialect *dialect_context,
403 const fcml_st_render_config *config,
404 fcml_st_memory_stream *output_stream,
405 const fcml_st_disassembler_result *result) {
406
407 fcml_st_dialect_context_int *dialect_context_int =
408 (fcml_st_dialect_context_int*) dialect_context;
409 fcml_char local_buffer[FCML_REND_LOCAL_BUFFER_SIZE] = { 0 };
410 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
411 fcml_uint32_t render_flags = config->render_flags;
412
413 /* Local stream. */
414 fcml_st_memory_stream local_stream;
415 local_stream.base_address = local_buffer;
416 local_stream.size = sizeof(local_buffer);
417 local_stream.offset = 0;
418
419 /* Instruction code. */
420 if (render_flags & FCML_REND_FLAG_RENDER_CODE) {
421 fcml_fn_rend_utils_format_append_code(config, output_stream,
422 result->instruction_details.instruction_code,
423 result->instruction_details.instruction_size);
424 }
425
426 fcml_int len = 0;
427
428 /* Instruction prefixes like LOCK. */
429 len += fcml_ifn_rend_utils_print_prefixes(output_stream,
430 &(result->instruction_details.prefixes_details), render_flags);
431
432 /* Mnemonic.*/
433 len += fcml_fn_rend_utils_format_append_str(output_stream,
434 resolve_mnemonic(result));
435
436 /* Short form, so operands should be ignored. */
437 if (result->instruction_details.is_shortcut) {
438 return error;
439 }
440
441 /* Add hints.*/
442 if (result->instruction.hints & FCML_HINT_FAR_POINTER) {
443 len += fcml_fn_rend_utils_format_append_str(output_stream,
444 FCML_TEXT(" far"));
445 }
446 if ((render_flags & FCML_REND_FLAG_RENDER_INDIRECT_HINT)
447 && (result->instruction.hints & FCML_HINT_INDIRECT_POINTER)) {
448 len += fcml_fn_rend_utils_format_append_str(output_stream,
449 FCML_TEXT(" indirect"));
450 }
451
452 /* Add all operands.*/
453 fcml_bool first_render = FCML_TRUE;
454 fcml_int i;
455 for (i = 0; i < FCML_OPERANDS_COUNT; i++) {
456 if (result->instruction.operands[i].type != FCML_OT_NONE) {
457 error = print_operand(config,
458 dialect_context_int, &local_stream, result, i);
459 if (!error) {
460 first_render = fcml_fn_rend_utils_render_operand_separator(
461 config, output_stream, len, first_render);
462 fcml_fn_rend_utils_format_append_stream(output_stream,
463 &local_stream);
464 fcml_fn_stream_clean(&local_stream);
465 } else {
466 break;
467 }
468 } else {
469 break;
470 }
471 }
472
473 fcml_fn_rend_utils_format_finish_str(output_stream);
474
475 return error;
476 }