1 /* Copyright (c) 2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include <elfutils/libdw.h>
13 #include "mc_object_info.h"
14 #include "mc_private.h"
16 static int mc_dwarf_push_value(mc_expression_state_t state, Dwarf_Off value)
18 if (state->stack_size >= MC_EXPRESSION_STACK_SIZE)
19 return MC_EXPRESSION_E_STACK_OVERFLOW;
21 state->stack[state->stack_size++] = value;
25 /** Convert a DWARF register into a libunwind register
27 * DWARF and libunwind does not use the same convention for numbering the
28 * registers on some architectures. The function makes the necessary
31 static int mc_dwarf_register_to_libunwind(int dwarf_register)
33 #if defined(UNW_TARGET_X86_64)
34 // It seems for this arch, DWARF and libunwind agree in the numbering:
35 return dwarf_register;
36 #elif defined(UNW_TARGET_X86)
37 // Could't find the authoritative source of information for this.
38 // This is inspired from http://source.winehq.org/source/dlls/dbghelp/cpu_i386.c#L517.
39 switch (dwarf_register) {
59 return UNW_X86_EFLAGS;
89 xbt_die("Bad/unknown register number.");
92 #error This architecture is not supported yet for DWARF expression evaluation.
96 int mc_dwarf_execute_expression(size_t n, const Dwarf_Op * ops,
97 mc_expression_state_t state)
99 for (int i = 0; i != n; ++i) {
101 const Dwarf_Op *op = ops + i;
102 uint8_t atom = op->atom;
141 mc_dwarf_register_to_libunwind(op->atom - DW_OP_breg0);
144 return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
145 unw_get_reg(state->cursor, register_id, &res);
146 error = mc_dwarf_push_value(state, res + op->number);
150 // Push the CFA (Canonical Frame Addresse):
151 case DW_OP_call_frame_cfa:
153 // UNW_X86_64_CFA does not return the CFA DWARF expects
154 // (it is a synonym for UNW_X86_64_RSP) so copy the cursor,
155 // unwind it once in order to find the parent SP:
158 return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
161 unw_cursor_t cursor = *(state->cursor);
165 unw_get_reg(&cursor, UNW_TDEP_SP, &res);
166 error = mc_dwarf_push_value(state, res);
174 if (!state->frame_base)
175 return MC_EXPRESSION_E_MISSING_FRAME_BASE;
176 uintptr_t fb = ((uintptr_t) state->frame_base) + op->number;
177 error = mc_dwarf_push_value(state, fb);
184 // Short constant literals:
185 // DW_OP_lit15 pushed the 15 on the stack.
218 error = mc_dwarf_push_value(state, atom - DW_OP_lit0);
221 // Address from the base address of this ELF object.
222 // Push the address on the stack (base_address + argument).
224 if (!state->object_info)
225 return MC_EXPRESSION_E_NO_BASE_ADDRESS;
226 if (state->stack_size == MC_EXPRESSION_STACK_SIZE)
227 return MC_EXPRESSION_E_STACK_OVERFLOW;
228 Dwarf_Off addr = (Dwarf_Off) (uintptr_t)
229 MC_object_base_address(state->object_info) + op->number;
230 error = mc_dwarf_push_value(state, addr);
233 // General constants:
234 // Push the constant argument on the stack.
245 if (state->stack_size == MC_EXPRESSION_STACK_SIZE)
246 return MC_EXPRESSION_E_STACK_OVERFLOW;
247 error = mc_dwarf_push_value(state, op->number);
250 // ***** Stack manipulation:
252 // Push another copy/duplicate the value at the top of the stack:
254 if (state->stack_size == 0)
255 return MC_EXPRESSION_E_STACK_UNDERFLOW;
257 error = mc_dwarf_push_value(state, state->stack[state->stack_size - 1]);
260 // Pop/drop the top of the stack:
262 if (state->stack_size == 0)
263 return MC_EXPRESSION_E_STACK_UNDERFLOW;
268 // Swap the two top-most value of the stack:
270 if (state->stack_size < 2)
271 return MC_EXPRESSION_E_STACK_UNDERFLOW;
273 uintptr_t temp = state->stack[state->stack_size - 2];
274 state->stack[state->stack_size - 2] =
275 state->stack[state->stack_size - 1];
276 state->stack[state->stack_size - 1] = temp;
280 // Duplicate the value under the top of the stack:
282 if (state->stack_size < 2)
283 return MC_EXPRESSION_E_STACK_UNDERFLOW;
284 error = mc_dwarf_push_value(state, state->stack[state->stack_size - 2]);
288 // Those usually take the top of the stack and the next value as argument
289 // and replace the top of the stack with the computed value
290 // (stack.top() += stack.before_top()).
293 if (state->stack_size < 2)
294 return MC_EXPRESSION_E_STACK_UNDERFLOW;
297 state->stack[state->stack_size - 2] +
298 state->stack[state->stack_size - 1];
299 state->stack[state->stack_size - 2] = result;
305 if (state->stack_size < 2)
306 return MC_EXPRESSION_E_STACK_UNDERFLOW;
309 state->stack[state->stack_size - 2] -
310 state->stack[state->stack_size - 1];
311 state->stack[state->stack_size - 2] = result;
316 case DW_OP_plus_uconst:
317 if (state->stack_size == 0)
318 return MC_EXPRESSION_E_STACK_UNDERFLOW;
319 state->stack[state->stack_size - 1] += op->number;
323 if (state->stack_size == 0)
324 return MC_EXPRESSION_E_STACK_UNDERFLOW;
325 state->stack[state->stack_size - 1] =
326 ~state->stack[state->stack_size - 1];
330 if (state->stack_size == 0)
331 return MC_EXPRESSION_E_STACK_UNDERFLOW;
333 intptr_t value = state->stack[state->stack_size - 1];
336 state->stack[state->stack_size - 1] = value;
341 if (state->stack_size < 2)
342 return MC_EXPRESSION_E_STACK_UNDERFLOW;
345 state->stack[state->stack_size - 2] -
346 state->stack[state->stack_size - 1];
347 state->stack[state->stack_size - 2] = result;
353 if (state->stack_size < 2)
354 return MC_EXPRESSION_E_STACK_UNDERFLOW;
357 state->stack[state->stack_size -
358 2] & state->stack[state->stack_size - 1];
359 state->stack[state->stack_size - 2] = result;
365 if (state->stack_size < 2)
366 return MC_EXPRESSION_E_STACK_UNDERFLOW;
369 state->stack[state->stack_size -
370 2] | state->stack[state->stack_size - 1];
371 state->stack[state->stack_size - 2] = result;
377 if (state->stack_size < 2)
378 return MC_EXPRESSION_E_STACK_UNDERFLOW;
381 state->stack[state->stack_size -
382 2] ^ state->stack[state->stack_size - 1];
383 state->stack[state->stack_size - 2] = result;
391 // ***** Deference (memory fetch)
393 case DW_OP_deref_size:
394 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
397 if (state->stack_size == 0)
398 return MC_EXPRESSION_E_STACK_UNDERFLOW;
401 uintptr_t address = (uintptr_t) state->stack[state->stack_size - 1];
402 if (!state->address_space)
403 xbt_die("Missing address space");
404 MC_address_space_read(
405 state->address_space, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
406 &state->stack[state->stack_size - 1], (const void*) address,
407 sizeof(uintptr_t), state->process_index);
413 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
424 /** \brief Resolve a location expression
425 * \deprecated Use mc_dwarf_resolve_expression
427 void mc_dwarf_resolve_location(mc_location_t location,
428 mc_expression_t expression,
429 mc_object_info_t object_info,
431 void *frame_pointer_address,
432 mc_address_space_t address_space, int process_index)
434 s_mc_expression_state_t state;
435 memset(&state, 0, sizeof(s_mc_expression_state_t));
436 state.frame_base = frame_pointer_address;
438 state.address_space = address_space;
439 state.object_info = object_info;
440 state.process_index = process_index;
442 if (expression->size >= 1
443 && expression->ops[0].atom >=DW_OP_reg0 && expression->ops[0].atom <= DW_OP_reg31) {
444 int dwarf_register = expression->ops[0].atom - DW_OP_reg0;
445 xbt_assert(c, "Missing frame context for register operation DW_OP_reg%i",
447 location->memory_location = NULL;
448 location->cursor = c;
449 location->register_id = mc_dwarf_register_to_libunwind(dwarf_register);
453 if (mc_dwarf_execute_expression(expression->size, expression->ops, &state))
454 xbt_die("Error evaluating DWARF expression");
455 if (state.stack_size == 0)
456 xbt_die("No value on the stack");
458 location->memory_location = (void*) state.stack[state.stack_size - 1];
459 location->cursor = NULL;
460 location->register_id = 0;
464 static mc_expression_t mc_find_expression(mc_location_list_t locations, unw_word_t ip) {
465 for (size_t i = 0; i != locations->size; ++i) {
466 mc_expression_t expression = locations->locations + i;
467 if ((expression->lowpc == NULL && expression->highpc == NULL)
468 || (ip && ip >= (unw_word_t) expression->lowpc
469 && ip < (unw_word_t) expression->highpc)) {
476 void mc_dwarf_resolve_locations(mc_location_t location,
477 mc_location_list_t locations,
478 mc_object_info_t object_info,
480 void *frame_pointer_address,
481 mc_address_space_t address_space, int process_index)
486 if (unw_get_reg(c, UNW_REG_IP, &ip))
487 xbt_die("Could not resolve IP");
490 mc_expression_t expression = mc_find_expression(locations, ip);
492 mc_dwarf_resolve_location(location,
493 expression, object_info, c,
494 frame_pointer_address, address_space, process_index);
496 xbt_die("Could not resolve location");
500 /** \brief Find the frame base of a given frame
505 void *mc_find_frame_base(dw_frame_t frame, mc_object_info_t object_info,
506 unw_cursor_t * unw_cursor)
508 s_mc_location_t location;
509 mc_dwarf_resolve_locations(&location,
510 &frame->frame_base, object_info,
511 unw_cursor, NULL, NULL, -1);
512 switch(mc_get_location_type(&location)) {
513 case MC_LOCATION_TYPE_ADDRESS:
514 return location.memory_location;
516 case MC_LOCATION_TYPE_REGISTER: {
517 // This is a special case.
518 // The register if not the location of the frame base
519 // (a frame base cannot be located in a register)
520 // Instead, DWARF defines this to mean that the register
521 // contains the address of the frame base.
523 unw_get_reg(location.cursor, location.register_id, &word);
528 xbt_die("Cannot handle non-address frame base");
529 return NULL; // Unreachable
533 void mc_dwarf_expression_clear(mc_expression_t expression)
535 free(expression->ops);
536 expression->ops = NULL;
537 expression->size = 0;
538 expression->lowpc = NULL;
539 expression->highpc = NULL;
542 void mc_dwarf_location_list_clear(mc_location_list_t list)
544 for (size_t i = 0; i != list->size; ++i) {
545 mc_dwarf_expression_clear(list->locations + i);
547 free(list->locations);
548 list->locations = NULL;
552 void mc_dwarf_expression_init(mc_expression_t expression, size_t len,
555 expression->lowpc = NULL;
556 expression->highpc = NULL;
557 expression->size = len;
558 expression->ops = xbt_malloc(len * sizeof(Dwarf_Op));
559 memcpy(expression->ops, ops, len * sizeof(Dwarf_Op));
562 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target,
563 size_t len, Dwarf_Op * ops)
566 target->locations = (mc_expression_t) xbt_malloc(sizeof(s_mc_expression_t));
567 mc_dwarf_expression_init(target->locations, len, ops);
570 void mc_dwarf_location_list_init(mc_location_list_t list, mc_object_info_t info,
571 Dwarf_Die * die, Dwarf_Attribute * attr)
573 if (list->locations) {
574 mc_dwarf_location_list_clear(list);
578 ptrdiff_t offset = 0;
579 Dwarf_Addr base, start, end;
585 offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
588 else if (offset == -1)
589 xbt_die("Error while loading location list");
594 (mc_expression_t) realloc(list->locations,
595 list->size * sizeof(s_mc_expression_t));
596 mc_expression_t expression = list->locations + i;
597 expression->ops = NULL;
598 mc_dwarf_expression_init(expression, len, ops);
602 flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
603 // If start == 0, this is not a location list:
604 expression->lowpc = start == 0 ? NULL : (char *) base + start;
605 expression->highpc = start == 0 ? NULL : (char *) base + end;