Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5085faef90e2f30d2a000cebb20d4df4e258ee48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************************************************
 * Copyright (c) 2008, 2016 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 * You may elect to redistribute this code under either of these licenses.
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/

/*
 * This module implements DWARF expressions evaluation.
 */

#include <tcf/config.h>

#if ENABLE_ELF && ENABLE_DebugContext

#include <assert.h>
#include <tcf/framework/myalloc.h>
#include <tcf/framework/exceptions.h>
#include <tcf/framework/errors.h>
#include <tcf/services/dwarf.h>
#include <tcf/services/dwarfexpr.h>
#include <tcf/services/dwarfecomp.h>
#include <tcf/services/stacktrace.h>
#include <tcf/services/elf-symbols.h>
#include <tcf/services/vm.h>

void dwarf_get_expression_list(PropertyValue * Value, DWARFExpressionInfo ** List) {
    CompUnit * Unit = Value->mObject->mCompUnit;

    if (Value->mAddr == NULL || Value->mSize == 0) str_exception(ERR_INV_DWARF, "Invalid format of location expression");

    if (Value->mForm == FORM_DATA4 || Value->mForm == FORM_DATA8 || Value->mForm == FORM_SEC_OFFSET) {
        U8_T Base = 0;
        U8_T Offset = 0;
        U8_T AddrMax = ~(U8_T)0;
        DWARFCache * Cache = (DWARFCache *)Unit->mFile->dwarf_dt_cache;
        DWARFExpressionInfo * Last = NULL;

        assert(Cache->magic == DWARF_CACHE_MAGIC);
        if (Cache->mDebugLoc == NULL) str_exception(ERR_INV_DWARF, "Missing .debug_loc section");
        dio_EnterSection(&Unit->mDesc, Unit->mDesc.mSection, Value->mAddr - (U1_T *)Unit->mDesc.mSection->data);
        Offset = dio_ReadAddressX(NULL, Value->mSize);
        dio_ExitSection();
        Base = Unit->mObject->u.mCode.mLowPC;
        if (Unit->mDesc.mAddressSize < 8) AddrMax = ((U8_T)1 << Unit->mDesc.mAddressSize * 8) - 1;
        dio_EnterSection(&Unit->mDesc, Cache->mDebugLoc, Offset);
        for (;;) {
            ELF_Section * S0 = NULL;
            ELF_Section * S1 = NULL;
            U8_T Addr0 = dio_ReadAddress(&S0);
            U8_T Addr1 = dio_ReadAddress(&S1);
            if (Addr0 == AddrMax) {
                Base = Addr1;
            }
            else if (Addr0 == 0 && Addr1 == 0) {
                break;
            }
            else if (Addr0 > Addr1) {
                str_exception(ERR_INV_DWARF, "Invalid .debug_loc section");
            }
            else {
                U2_T Size = dio_ReadU2();
                U8_T RT_Addr0 = 0;
                if (S0 == NULL) S0 = Unit->mTextSection;
                RT_Addr0 = elf_map_to_run_time_address(Value->mContext, Unit->mFile, S0, Base + Addr0);
                if (!errno) {
                    DWARFExpressionInfo * Info = (DWARFExpressionInfo *)tmp_alloc_zero(sizeof(DWARFExpressionInfo));
                    Info->object = Value->mObject;
                    Info->code_addr = RT_Addr0;
                    Info->code_size = Addr1 - Addr0;
                    Info->section = Cache->mDebugLoc;
                    Info->expr_addr = dio_GetDataPtr();
                    Info->expr_size = Size;
                    Info->attr = Value->mAttr;
                    Info->form = Value->mForm;
                    if (Last == NULL) *List = Info;
                    else Last->next = Info;
                    Last = Info;
                }
                dio_Skip(Size);
            }
        }
        dio_ExitSection();
        if (Last == NULL) str_exception(ERR_OTHER, "Object is not available at this location in the code");
    }
    else {
        DWARFExpressionInfo * Info = (DWARFExpressionInfo *)tmp_alloc_zero(sizeof(DWARFExpressionInfo));
        Info->object = Value->mObject;
        Info->section = Unit->mDesc.mSection;
        Info->expr_addr = Value->mAddr;
        Info->expr_size = Value->mSize;
        Info->attr = Value->mAttr;
        Info->form = Value->mForm;
        *List = Info;
    }
}

typedef struct ExpressionArgs {
    PropertyValue * value;
    uint64_t * args;
    unsigned args_cnt;
} ExpressionArgs;

static void evaluate_expression(void * x) {
    ExpressionArgs * expr_args = (ExpressionArgs *)x;
    PropertyValue * Value = expr_args->value;
    uint64_t * args = expr_args->args;
    unsigned args_cnt = expr_args->args_cnt;
    CompUnit * Unit = Value->mObject->mCompUnit;
    LocationExpressionState * State = NULL;
    DWARFExpressionInfo * Info = NULL;

    State = (LocationExpressionState *)tmp_alloc_zero(sizeof(LocationExpressionState));
    State->stk = (uint64_t *)tmp_alloc(sizeof(uint64_t) * (State->stk_max = 8));
    State->type_stk = (uint8_t *)tmp_alloc_zero(State->stk_max);
    State->ctx = Value->mContext;
    if (Value->mFrame != STACK_NO_FRAME &&
            get_frame_info(Value->mContext, Value->mFrame, &State->stack_frame) < 0)
        exception(errno);
    State->addr_size = Unit->mDesc.mAddressSize;
    State->reg_id_scope = Unit->mRegIdScope;
    State->args = args;
    State->args_cnt = args_cnt;

    if (Value->mAttr == AT_data_member_location) {
        if (args_cnt < 1) exception(ERR_INV_CONT_OBJ);
        State->stk[State->stk_pos++] = args[0];
    }
    else if (Value->mAttr == AT_use_location) {
        if (args_cnt < 2) exception(ERR_INV_CONT_OBJ);
        State->stk[State->stk_pos++] = args[1];
        State->stk[State->stk_pos++] = args[0];
    }
    if (Value->mPieces != NULL || Value->mAddr == NULL || Value->mSize == 0) {
        str_exception(ERR_INV_DWARF, "Invalid DWARF expression reference");
    }
    dwarf_get_expression_list(Value, &Info);
    dwarf_transform_expression(Value->mContext, Value->mFrame, Info);
    State->code = Info->expr_addr;
    State->code_len = Info->expr_size;
    if (evaluate_vm_expression(State) < 0) exception(errno);
    assert(State->code_pos == State->code_len);

    Value->mForm = FORM_EXPR_VALUE;
    Value->mAddr = NULL;
    Value->mValue = 0;
    Value->mSize = 0;
    Value->mBigEndian = State->reg_id_scope.big_endian;
    Value->mPieces = NULL;
    Value->mPieceCnt = 0;

    if (State->pieces_cnt) {
        Value->mPieces = State->pieces;
        Value->mPieceCnt = State->pieces_cnt;
    }
    else {
        Value->mValue = State->stk[--State->stk_pos];
    }

    if (State->stk_pos != 0) {
        str_exception(ERR_INV_DWARF, "Invalid DWARF expression stack");
    }
}

void dwarf_evaluate_expression(PropertyValue * value, uint64_t * args, unsigned args_cnt) {
    /* Need to save symbols state, because expression evaluation calls get_stack_tracing_info() */
    ExpressionArgs expr_args;
    expr_args.value = value;
    expr_args.args = args;
    expr_args.args_cnt = args_cnt;
    if (elf_save_symbols_state(evaluate_expression, &expr_args) < 0) exception(errno);
}


#endif /* ENABLE_ELF && ENABLE_DebugContext */

Back to the top