6 #include <elfutils/libdw.h>
8 #include "mc_private.h"
10 static int mc_dwarf_push_value(mc_expression_state_t state, Dwarf_Off value) {
11 if(state->stack_size>=MC_EXPRESSION_STACK_SIZE)
12 return MC_EXPRESSION_E_STACK_OVERFLOW;
14 state->stack[state->stack_size++] = value;
18 int mc_dwarf_execute_expression(
19 size_t n, const Dwarf_Op* ops, mc_expression_state_t state) {
20 for(int i=0; i!=n; ++i) {
22 const Dwarf_Op* op = ops + i;
23 uint8_t atom = op->atom;
29 case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
30 case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
31 case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
32 case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
33 case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
34 case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
35 case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
36 case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:{
37 int register_id = op->atom - DW_OP_breg0;
40 return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
41 unw_get_reg(state->cursor, register_id, &res);
42 error = mc_dwarf_push_value(state, res + op->number);
46 // Push the CFA (Canonical Frame Addresse):
47 case DW_OP_call_frame_cfa:
49 // UNW_X86_64_CFA does not return the CFA DWARF expects
50 // (it is a synonym for UNW_X86_64_RSP) so copy the cursor,
51 // unwind it once in order to find the parent SP:
54 return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
57 unw_cursor_t cursor = *(state->cursor);
61 unw_get_reg(&cursor, UNW_TDEP_SP, &res);
62 error = mc_dwarf_push_value(state, res);
70 if(!state->frame_base)
71 return MC_EXPRESSION_E_MISSING_FRAME_BASE;
72 error = mc_dwarf_push_value(state, ((uintptr_t)state->frame_base) + op->number);
79 case DW_OP_lit0: case DW_OP_lit1: case DW_OP_lit2: case DW_OP_lit3:
80 case DW_OP_lit4: case DW_OP_lit5: case DW_OP_lit6: case DW_OP_lit7:
81 case DW_OP_lit8: case DW_OP_lit9: case DW_OP_lit10: case DW_OP_lit11:
82 case DW_OP_lit12: case DW_OP_lit13: case DW_OP_lit14: case DW_OP_lit15:
83 case DW_OP_lit16: case DW_OP_lit17: case DW_OP_lit18: case DW_OP_lit19:
84 case DW_OP_lit20: case DW_OP_lit21: case DW_OP_lit22: case DW_OP_lit23:
85 case DW_OP_lit24: case DW_OP_lit25: case DW_OP_lit26: case DW_OP_lit27:
86 case DW_OP_lit28: case DW_OP_lit29: case DW_OP_lit30: case DW_OP_lit31:
87 error = mc_dwarf_push_value(state, atom - DW_OP_lit0);
91 if(!state->object_info)
92 return MC_EXPRESSION_E_NO_BASE_ADDRESS;
93 if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
94 return MC_EXPRESSION_E_STACK_OVERFLOW;
95 error = mc_dwarf_push_value(state,
96 (Dwarf_Off)(uintptr_t)MC_object_base_address(state->object_info) + op->number);
109 if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
110 return MC_EXPRESSION_E_STACK_OVERFLOW;
111 error = mc_dwarf_push_value(state, op->number);
114 // Stack manipulation:
116 // Push the value at the top of the stack:
118 if(state->stack_size==0)
119 return MC_EXPRESSION_E_STACK_UNDERFLOW;
121 error = mc_dwarf_push_value(state, state->stack[state->stack_size-1]);
125 if(state->stack_size==0)
126 return MC_EXPRESSION_E_STACK_UNDERFLOW;
132 if(state->stack_size<2)
133 return MC_EXPRESSION_E_STACK_UNDERFLOW;
135 uintptr_t temp = state->stack[state->stack_size-2];
136 state->stack[state->stack_size-2] = state->stack[state->stack_size-1];
137 state->stack[state->stack_size-1] = temp;
142 if(state->stack_size<2)
143 return MC_EXPRESSION_E_STACK_UNDERFLOW;
144 error = mc_dwarf_push_value(state, state->stack[state->stack_size-2]);
150 if(state->stack_size<2)
151 return MC_EXPRESSION_E_STACK_UNDERFLOW;
153 uintptr_t result = state->stack[state->stack_size-2] + state->stack[state->stack_size-1];
154 state->stack[state->stack_size-2] = result;
160 if(state->stack_size<2)
161 return MC_EXPRESSION_E_STACK_UNDERFLOW;
163 uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
164 state->stack[state->stack_size-2] = result;
169 case DW_OP_plus_uconst:
170 if(state->stack_size==0)
171 return MC_EXPRESSION_E_STACK_UNDERFLOW;
172 state->stack[state->stack_size-1] += op->number;
176 if(state->stack_size==0)
177 return MC_EXPRESSION_E_STACK_UNDERFLOW;
178 state->stack[state->stack_size-1] = ~state->stack[state->stack_size-1];
182 if(state->stack_size==0)
183 return MC_EXPRESSION_E_STACK_UNDERFLOW;
185 intptr_t value = state->stack[state->stack_size-1];
186 if(value<0) value = -value;
187 state->stack[state->stack_size-1] = value;
192 if(state->stack_size<2)
193 return MC_EXPRESSION_E_STACK_UNDERFLOW;
195 uintptr_t result = state->stack[state->stack_size-2] - state->stack[state->stack_size-1];
196 state->stack[state->stack_size-2] = result;
202 if(state->stack_size<2)
203 return MC_EXPRESSION_E_STACK_UNDERFLOW;
205 uintptr_t result = state->stack[state->stack_size-2] & state->stack[state->stack_size-1];
206 state->stack[state->stack_size-2] = result;
212 if(state->stack_size<2)
213 return MC_EXPRESSION_E_STACK_UNDERFLOW;
215 uintptr_t result = state->stack[state->stack_size-2] | state->stack[state->stack_size-1];
216 state->stack[state->stack_size-2] = result;
222 if(state->stack_size<2)
223 return MC_EXPRESSION_E_STACK_UNDERFLOW;
225 uintptr_t result = state->stack[state->stack_size-2] ^ state->stack[state->stack_size-1];
226 state->stack[state->stack_size-2] = result;
235 case DW_OP_deref_size:
236 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
239 if(state->stack_size==0)
240 return MC_EXPRESSION_E_STACK_UNDERFLOW;
243 uintptr_t address = (uintptr_t) state->stack[state->stack_size-1];
244 uintptr_t* p = (uintptr_t*)mc_translate_address(address, state->snapshot);
245 state->stack[state->stack_size-1] = *p;
251 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
254 if(error) return error;
261 /** \brief Resolve a location expression
262 * \deprecated Use mc_dwarf_resolve_expression
264 uintptr_t mc_dwarf_resolve_location(mc_expression_t expression, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
265 s_mc_expression_state_t state;
266 memset(&state, 0, sizeof(s_mc_expression_state_t));
267 state.frame_base = frame_pointer_address;
269 state.snapshot = snapshot;
270 state.object_info = object_info;
272 if(mc_dwarf_execute_expression(expression->size, expression->ops, &state))
273 xbt_die("Error evaluating DWARF expression");
274 if(state.stack_size==0)
275 xbt_die("No value on the stack");
277 return state.stack[state.stack_size-1];
280 uintptr_t mc_dwarf_resolve_locations(mc_location_list_t locations, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
284 if(unw_get_reg(c, UNW_REG_IP, &ip))
285 xbt_die("Could not resolve IP");
288 for(size_t i=0; i!=locations->size; ++i) {
289 mc_expression_t expression = locations->locations + i;
290 if( (expression->lowpc==NULL && expression->highpc==NULL)
291 || (c && ip >= (unw_word_t) expression->lowpc && ip < (unw_word_t) expression->highpc)) {
292 return mc_dwarf_resolve_location(expression, object_info, c, frame_pointer_address, snapshot);
295 xbt_die("Could not resolve location");
298 /** \brief Find the frame base of a given frame
303 void* mc_find_frame_base(dw_frame_t frame, mc_object_info_t object_info, unw_cursor_t* unw_cursor) {
304 return (void*) mc_dwarf_resolve_locations(&frame->frame_base, object_info, unw_cursor, NULL, NULL);
307 void mc_dwarf_expression_clear(mc_expression_t expression) {
308 free(expression->ops);
309 expression->ops = NULL;
310 expression->size = 0;
311 expression->lowpc = NULL;
312 expression->highpc = NULL;
315 void mc_dwarf_location_list_clear(mc_location_list_t list) {
316 for(size_t i=0; i!=list->size; ++i) {
317 mc_dwarf_expression_clear(list->locations + i);
319 free(list->locations);
320 list->locations = NULL;
324 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops) {
325 if(expression->ops) {
326 free(expression->ops);
328 expression->lowpc = NULL;
329 expression->highpc = NULL;
330 expression->size = len;
331 expression->ops = xbt_malloc(len*sizeof(Dwarf_Op));
332 memcpy(expression->ops, ops, len*sizeof(Dwarf_Op));
335 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops) {
336 if(target->locations) {
337 mc_dwarf_location_list_clear(target);
340 target->locations = (mc_expression_t) xbt_malloc(sizeof(s_mc_expression_t));
341 mc_dwarf_expression_init(target->locations, len, ops);
344 void mc_dwarf_location_list_init(mc_location_list_t list, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
345 if(list->locations) {
346 mc_dwarf_location_list_clear(list);
350 ptrdiff_t offset = 0;
351 Dwarf_Addr base, start, end;
357 offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
361 xbt_die("Error while loading location list");
365 list->locations = (mc_expression_t) realloc(list->locations, list->size*sizeof(s_mc_expression_t));
366 mc_expression_t expression = list->locations + i;
368 void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
369 mc_dwarf_expression_init(expression, len, ops);
371 // If start == 0, this is not a location list:
372 expression->lowpc = start == 0 ? NULL : (char*) base + start;
373 expression->highpc = start == 0 ? NULL : (char*) base + end;