Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 48dee5787c6e136ac4622470db1de77ec757a032 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*******************************************************************************
 * Copyright (c) 2008, 2010 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 <config.h>

#if ENABLE_ELF && ENABLE_DebugContext

#include <assert.h>
#include <stdio.h>
#include <framework/events.h>
#include <framework/myalloc.h>
#include <framework/exceptions.h>
#include <framework/errors.h>
#include <framework/trace.h>
#include <services/dwarf.h>
#include <services/dwarfio.h>
#include <services/dwarfexpr.h>
#include <services/stacktrace.h>
#include <services/vm.h>

typedef struct ValuePieces {
    U4_T mCnt;
    U4_T mMax;
    PropertyValuePiece * mArray;
    struct ValuePieces * mNext;
} ValuePieces;

static VMState sState;
static ELF_Section * sSection = NULL;
static U8_T sSectionOffs = 0;
static PropertyValue * sValue = NULL;
static ValuePieces * sValuePieces = NULL;

static ValuePieces * sFreeValuePieces = NULL;
static ValuePieces * sBusyValuePieces = NULL;
static unsigned sValuePiecesCnt = 0;
static int sValuePiecesPosted = 0;

static void free_value_pieces(void * args) {
    while (sBusyValuePieces != NULL) {
        ValuePieces * Pieces = sBusyValuePieces;
        sBusyValuePieces = Pieces->mNext;
        Pieces->mNext = sFreeValuePieces;
        sFreeValuePieces = Pieces;
    }
    while (sFreeValuePieces != NULL && sValuePiecesCnt > 16) {
        ValuePieces * Pieces = sFreeValuePieces;
        sFreeValuePieces = sFreeValuePieces->mNext;
        sValuePiecesCnt--;
        loc_free(Pieces->mArray);
        loc_free(Pieces);
    }
}

static ValuePieces * alloc_value_pieces(void) {
    ValuePieces * Pieces = sFreeValuePieces;
    if (Pieces == NULL) {
        Pieces = (ValuePieces *)loc_alloc_zero(sizeof(ValuePieces));
        sValuePiecesCnt++;
    }
    else {
        sFreeValuePieces = sFreeValuePieces->mNext;
    }
    Pieces->mNext = sBusyValuePieces;
    sBusyValuePieces = Pieces;
    if (!sValuePiecesPosted && sValuePiecesCnt > 8) {
        post_event(free_value_pieces, NULL);
        sValuePiecesPosted = 1;
    }
    Pieces->mCnt = 0;
    return Pieces;
}

static StackFrame * get_stack_frame(PropertyValue * sValue) {
    StackFrame * Info = NULL;
    if (sValue->mFrame == STACK_NO_FRAME) return NULL;
    if (get_frame_info(sValue->mContext, sValue->mFrame, &Info) < 0) exception(errno);
    return Info;
}

static ObjectInfo * get_parent_function(ObjectInfo * Info) {
    while (Info != NULL) {
        switch (Info->mTag) {
        case TAG_global_subroutine:
        case TAG_subroutine:
        case TAG_subprogram:
        case TAG_entry_point:
            return Info;
        }
        Info = Info->mParent;
    }
    return NULL;
}

static U8_T read_address(void) {
    U8_T addr = 0;
    ELF_Section * section = NULL;
    CompUnit * Unit = sValue->mObject->mCompUnit;

    addr = dio_ReadAddress(&section);
    addr = elf_map_to_run_time_address(sState.ctx, Unit->mFile, section, (ContextAddress)addr);
    if (addr == 0) str_exception(ERR_INV_ADDRESS, "Object has no RT address");
    return addr;
}

static U8_T get_fbreg(void) {
    PropertyValue FP;
    CompUnit * Unit = sValue->mObject->mCompUnit;
    ObjectInfo * Parent = get_parent_function(sValue->mObject);
    U8_T addr = 0;

    if (Parent == NULL) str_exception(ERR_INV_DWARF, "OP_fbreg: no parent function");
    memset(&FP, 0, sizeof(FP));

    {
        PropertyValue * OrgValue = sValue;
        ValuePieces * OrgValuePieces = sValuePieces;
        ELF_Section * OrgSection = sSection;
        U8_T OrgSectionOffs = sSectionOffs;
        VMState OrgState = sState;

        read_and_evaluate_dwarf_object_property(sState.ctx, sState.stack_frame, 0, Parent, AT_frame_base, &FP);

        assert(sState.ctx == OrgState.ctx);
        assert(sState.addr_size == OrgState.addr_size);
        assert(sState.big_endian == OrgState.big_endian);

        sState.code = OrgState.code;
        sState.code_pos = OrgState.code_pos;
        sState.code_len = OrgState.code_len;
        sState.object_address = OrgState.object_address;
        sSectionOffs = OrgSectionOffs;
        sSection = OrgSection;
        sValuePieces = OrgValuePieces;
        sValue = OrgValue;
    }

    if (FP.mRegister != NULL) {
        if (read_reg_value(get_stack_frame(&FP), FP.mRegister, &addr) < 0) exception(errno);
    }
    else {
        addr = get_numeric_property_value(&FP);
    }
    dio_EnterSection(&Unit->mDesc, sSection, sSectionOffs + sState.code_pos);
    return addr + dio_ReadS8LEB128();
}

static void client_op(uint8_t op) {
    dio_SetPos(sSectionOffs + sState.code_pos);
    switch (op) {
    case OP_addr:
        sState.stk[sState.stk_pos++] = read_address();
        break;
    case OP_fbreg:
        if (sState.stack_frame == STACK_NO_FRAME) str_exception(ERR_INV_CONTEXT, "Invalid stack frame");
        sState.stk[sState.stk_pos++] = get_fbreg();
        break;
    default:
        trace(LOG_ALWAYS, "Unsupported DWARF expression op 0x%02x", op);
        str_exception(ERR_UNSUPPORTED, "Unsupported DWARF expression op");
    }
    sState.code_pos = (size_t)(dio_GetPos() - sSectionOffs);
}

static void evaluate_expression(ELF_Section * Section, U1_T * Buf, size_t Size) {
    int error = 0;
    CompUnit * Unit = sValue->mObject->mCompUnit;

    sState.code = Buf;
    sState.code_len = Size;
    sState.code_pos = 0;
    sSection = Section;
    sSectionOffs = Buf - (U1_T *)Section->data;
    dio_EnterSection(&Unit->mDesc, sSection, sSectionOffs);
    if (evaluate_vm_expression(&sState) < 0) error = errno;
    if (!error && sState.piece_bits) {
        sValuePieces = alloc_value_pieces();
        while (!error) {
            PropertyValuePiece * Piece;
            if (sValuePieces->mCnt >= sValuePieces->mMax) {
                sValuePieces->mMax += 8;
                sValuePieces->mArray = (PropertyValuePiece *)loc_realloc(sValuePieces->mArray,
                    sizeof(PropertyValuePiece) * sValuePieces->mMax);
            }
            Piece = sValuePieces->mArray + sValuePieces->mCnt++;
            memset(Piece, 0, sizeof(PropertyValuePiece));
            if (sState.reg) {
                Piece->mRegister = sState.reg;
                Piece->mBigEndian = sState.reg->big_endian;
            }
            else {
                Piece->mAddress = sState.stk[--sState.stk_pos];
                Piece->mBigEndian = sState.big_endian;
            }
            Piece->mBitOffset = sState.piece_offs;
            Piece->mBitSize = sState.piece_bits;
            if (sState.code_pos >= sState.code_len) break;
            if (evaluate_vm_expression(&sState) < 0) error = errno;
        }
    }
    dio_ExitSection();
    assert(error || sState.code_pos == sState.code_len);
    if (error) exception(error);
}

static void evaluate_location(void) {
    U8_T IP = 0;
    U8_T Offset = 0;
    U8_T Base = 0;
    CompUnit * Unit = sValue->mObject->mCompUnit;
    DWARFCache * Cache = (DWARFCache *)Unit->mFile->dwarf_dt_cache;
    U8_T AddrMax = ~(U8_T)0;

    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, sValue->mAddr - (U1_T *)Unit->mDesc.mSection->data);
    Offset = dio_ReadUX(sValue->mSize);
    dio_ExitSection();
    Base = Unit->mLowPC;
    if (Unit->mDesc.mAddressSize < 8) AddrMax = ((U8_T)1 << Unit->mDesc.mAddressSize * 8) - 1;
    if (read_reg_value(get_stack_frame(sValue), get_PC_definition(sValue->mContext), &IP) < 0) exception(errno);
    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 (S0 == NULL) S0 = Unit->mTextSection;
        if (S1 == NULL) S1 = Unit->mTextSection;
        if (Addr0 == AddrMax) {
            Base = Addr1;
        }
        else if (Addr0 == 0 && Addr1 == 0) {
            break;
        }
        else if (S0 != S1 || Addr0 > Addr1) {
            str_exception(ERR_INV_DWARF, "Invalid .debug_loc section");
        }
        else {
            U2_T Size = dio_ReadU2();
            U8_T RTAddr0 = elf_map_to_run_time_address(sValue->mContext, Unit->mFile, S0, (ContextAddress)(Base + Addr0));
            U8_T RTAddr1 = Addr1 - Addr0 + RTAddr0;
            if (RTAddr0 != 0 && IP >= RTAddr0 && IP < RTAddr1) {
                U1_T * Buf = dio_GetDataPtr();
                dio_ExitSection();
                evaluate_expression(Cache->mDebugLoc, Buf, Size);
                return;
            }
            dio_Skip(Size);
        }
    }
    dio_ExitSection();
    str_exception(ERR_OTHER, "Object is not available at this location in the code");
}

void dwarf_evaluate_expression(U8_T BaseAddress, PropertyValue * v) {
    unsigned stk_pos = 0;
    CompUnit * Unit = v->mObject->mCompUnit;

    sValue = v;
    sValuePieces = NULL;
    sState.ctx = sValue->mContext;
    sState.addr_size = Unit->mDesc.mAddressSize;
    sState.big_endian = Unit->mFile->big_endian;
    sState.stack_frame = sValue->mFrame;
    sState.reg_id_scope = Unit->mRegIdScope;
    sState.object_address = BaseAddress;
    sState.client_op = client_op;;

    if (sValue->mAttr != AT_frame_base) sState.stk_pos = 0;
    stk_pos = sState.stk_pos;

    if (sValue->mAttr == AT_data_member_location) {
        if (sState.stk_pos >= sState.stk_max) {
            sState.stk_max += 8;
            sState.stk = (U8_T *)loc_alloc(sizeof(U8_T) * sState.stk_max);
        }
        sState.stk[sState.stk_pos++] = BaseAddress;
    }
    if (sValue->mRegister != NULL || sValue->mAddr == NULL || sValue->mSize == 0) {
        str_exception(ERR_INV_DWARF, "invalid DWARF expression reference");
    }
    if (sValue->mForm == FORM_DATA4 || sValue->mForm == FORM_DATA8) {
        if (sValue->mFrame == STACK_NO_FRAME) str_exception(ERR_INV_CONTEXT, "need stack frame");
        evaluate_location();
    }
    else {
        evaluate_expression(Unit->mDesc.mSection, sValue->mAddr, sValue->mSize);
    }

    sValue->mAddr = NULL;
    sValue->mValue = 0;
    sValue->mSize = 0;
    sValue->mBigEndian = sState.big_endian;
    sValue->mRegister = NULL;
    sValue->mPieces = NULL;
    sValue->mPieceCnt = 0;

    if (sValuePieces) {
        sValue->mPieces = sValuePieces->mArray;
        sValue->mPieceCnt = sValuePieces->mCnt;
    }
    else if (sState.reg) {
        sValue->mSize = sState.reg->size;
        sValue->mBigEndian = sState.reg->big_endian;
        sValue->mRegister = sState.reg;
    }
    else {
        sValue->mValue = sState.stk[--sState.stk_pos];
    }

    if (sState.stk_pos != stk_pos) {
        str_exception(ERR_INV_DWARF, "Invalid DWARF expression stack");
    }
}

#endif /* ENABLE_ELF && ENABLE_DebugContext */

Back to the top