Skip to main content
summaryrefslogtreecommitdiffstats
blob: 53beba42b49fc7201c370a664debcea510635489 (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
334
335
336
337
338
339
340
341
342
343
344
345
346
/*******************************************************************************
 * Copyright (c) 2011 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
 *******************************************************************************/

/*
 * Transformation of DWARF expressions to a portable form.
 *
 * Functions in this module use exceptions to report errors, see exceptions.h
 */

#include <tcf/config.h>

#if ENABLE_ELF && ENABLE_DebugContext

#include <assert.h>
#include <tcf/framework/myalloc.h>
#include <tcf/framework/exceptions.h>
#include <tcf/services/dwarf.h>
#include <tcf/services/dwarfecomp.h>

static U1_T * buf = NULL;
static size_t buf_pos = 0;
static size_t buf_max = 0;
static DWARFExpressionInfo * expr = NULL;
static size_t expr_pos = 0;
static Context * expr_ctx = NULL;
static U8_T expr_ip = 0;

static void add(unsigned n) {
    if (buf_pos >= buf_max) {
        buf_max *= 2;
        buf = (U1_T *)tmp_realloc(buf, buf_max);
    }
    buf[buf_pos++] = (U1_T)n;
}

static void copy(size_t n) {
    while (n > 0) {
        if (expr_pos >= expr->expr_size) exception(ERR_INV_DWARF);
        add(expr->expr_addr[expr_pos++]);
        n--;
    }
}

static void copy_leb128(void) {
    for (;;) {
        U1_T n = expr->expr_addr[expr_pos++];
        add(n);
        if ((n & 0x80) == 0) break;
    }
}

static void add_uleb128(U8_T x) {
    for (;;) {
        U1_T n = (U1_T)(x & 0x7Fu);
        x = x >> 7;
        if (x == 0) {
            add(n);
            break;
        }
        add(n | 0x80u);
    }
}

#if 0
/* Not used yet */
static void add_sleb128(I8_T x) {
    for (;;) {
        U1_T n = (U1_T)(x & 0x7Fu);
        x = x >> 7;
        if (x == 0 || (x == 0xFFu && (n & 0x40))) {
            add(n);
            break;
        }
        add(n | 0x80u);
    }
}
#endif

static void add_expression(DWARFExpressionInfo * info);

static void op_addr(void) {
    ContextAddress addr = 0;
    ELF_Section * section = NULL;
    U8_T pos = 0;

    expr_pos++;
    pos = expr->expr_addr + expr_pos - (U1_T *)expr->section->data;
    dio_EnterSection(&expr->unit->mDesc, expr->section, pos);
    addr = (ContextAddress)dio_ReadAddress(&section);
    expr_pos += (size_t)(dio_GetPos() - pos);
    dio_ExitSection();
    addr = elf_map_to_run_time_address(expr_ctx, expr->unit->mFile, section, addr);
    if (errno) str_exception(errno, "Cannot get object run-time address");
    add(OP_constu);
    add_uleb128(addr);
}

static void op_fbreg(void) {
    PropertyValue fp;
    DWARFExpressionInfo info;
    ObjectInfo * parent = get_parent_function(expr->object);

    expr_pos++;
    memset(&fp, 0, sizeof(fp));
    if (parent == NULL) str_exception(ERR_INV_DWARF, "OP_fbreg: no parent function");
    read_dwarf_object_property(expr_ctx, STACK_NO_FRAME, parent, AT_frame_base, &fp);
    dwarf_find_expression(&fp, expr_ip, &info);
    switch (*info.expr_addr) {
    case OP_reg:
        add(OP_basereg);
        {
            unsigned i = 1;
            while (i < info.unit->mDesc.mAddressSize + 1u) {
                add(info.expr_addr[i++]);
            }
        }
        copy_leb128();
        return;
    case OP_regx:
        add(OP_bregx);
        {
            unsigned i = 1;
            for (;;) {
                U1_T n = info.expr_addr[i++];
                add(n);
                if ((n & 0x80) == 0) break;
            }
        }
        copy_leb128();
        return;
    case OP_reg0:
    case OP_reg1:
    case OP_reg2:
    case OP_reg3:
    case OP_reg4:
    case OP_reg5:
    case OP_reg6:
    case OP_reg7:
    case OP_reg8:
    case OP_reg9:
    case OP_reg10:
    case OP_reg11:
    case OP_reg12:
    case OP_reg13:
    case OP_reg14:
    case OP_reg15:
    case OP_reg16:
    case OP_reg17:
    case OP_reg18:
    case OP_reg19:
    case OP_reg20:
    case OP_reg21:
    case OP_reg22:
    case OP_reg23:
    case OP_reg24:
    case OP_reg25:
    case OP_reg26:
    case OP_reg27:
    case OP_reg28:
    case OP_reg29:
    case OP_reg30:
    case OP_reg31:
        add(OP_breg0 + (*info.expr_addr - OP_reg0));
        copy_leb128();
        return;
    }
    add_expression(&info);
    add(OP_consts);
    copy_leb128();
    add(OP_add);
}

static void add_expression(DWARFExpressionInfo * info) {
    DWARFExpressionInfo * org_expr = expr;
    size_t org_expr_pos = expr_pos;

    if (expr != NULL && info->code_size) {
        if (expr->code_size) {
            if (info->code_addr > expr->code_addr) {
                U8_T d = info->code_addr - expr->code_addr;
                assert(expr->code_size > d);
                expr->code_addr += d;
                expr->code_size -= d;
            }
            if (info->code_addr + info->code_size < expr->code_addr + expr->code_size) {
                U8_T d = (expr->code_addr + expr->code_size) - (info->code_addr + info->code_size);
                assert(expr->code_size > d);
                expr->code_size -= d;
            }
        }
        else {
            expr->code_addr = info->code_addr;
            expr->code_size = info->code_size;
        }
    }

    expr = info;
    expr_pos = 0;
    while (expr_pos < info->expr_size) {
        U1_T op = info->expr_addr[expr_pos];
        switch (op) {
        case OP_const1u:
        case OP_const1s:
        case OP_pick:
        case OP_deref_size:
        case OP_xderef_size:
            copy(2);
            break;
        case OP_const:
        case OP_reg:
        case OP_basereg:
            copy(1 + info->unit->mDesc.mAddressSize);
            break;
        case OP_const2u:
        case OP_const2s:
            copy(3);
            break;
        case OP_const4u:
        case OP_const4s:
            copy(5);
            break;
        case OP_const8u:
        case OP_const8s:
            copy(9);
            break;
        case OP_constu:
        case OP_consts:
        case OP_plus_uconst:
        case OP_regx:
        case OP_breg0:
        case OP_breg1:
        case OP_breg2:
        case OP_breg3:
        case OP_breg4:
        case OP_breg5:
        case OP_breg6:
        case OP_breg7:
        case OP_breg8:
        case OP_breg9:
        case OP_breg10:
        case OP_breg11:
        case OP_breg12:
        case OP_breg13:
        case OP_breg14:
        case OP_breg15:
        case OP_breg16:
        case OP_breg17:
        case OP_breg18:
        case OP_breg19:
        case OP_breg20:
        case OP_breg21:
        case OP_breg22:
        case OP_breg23:
        case OP_breg24:
        case OP_breg25:
        case OP_breg26:
        case OP_breg27:
        case OP_breg28:
        case OP_breg29:
        case OP_breg30:
        case OP_breg31:
        case OP_piece:
            add(op);
            expr_pos++;
            copy_leb128();
            break;
        case OP_bra:
        case OP_skip:
            str_exception(ERR_UNSUPPORTED, "OP_bra/OP_skip not supported yet");
            break;
        case OP_bregx:
        case OP_bit_piece:
            add(op);
            expr_pos++;
            copy_leb128();
            copy_leb128();
            break;
        case OP_implicit_value:
            {
                unsigned i = 0;
                size_t j = expr_pos + 1u;
                size_t size = 0;
                for (;; i += 7) {
                    U1_T n = info->expr_addr[j++];
                    size |= (n & 0x7Fu) << i;
                    if ((n & 0x80) == 0) break;
                }
                copy(j + size - expr_pos);
            }
            break;
        case OP_fbreg:
            op_fbreg();
            break;
        case OP_addr:
            op_addr();
            break;
        case OP_form_tls_address:
        case OP_GNU_push_tls_address:
        case OP_GNU_implicit_pointer:
        case OP_call2:
        case OP_call4:
        case OP_call_ref:
            str_fmt_exception(ERR_OTHER, "Unsupported DWARF expression op 0x%02x", op);
            break;
        default:
            add(op);
            expr_pos++;
            break;
        }
    }
    expr = org_expr;
    expr_pos = org_expr_pos;
}

void dwarf_transform_expression(Context * ctx, ContextAddress ip, DWARFExpressionInfo * info) {
    buf_pos = 0;
    buf_max = info->expr_size * 2;
    buf = (U1_T *)tmp_alloc(buf_max);
    expr_ctx = ctx;
    expr_ip = ip;
    expr = NULL;
    add_expression(info);
    info->expr_addr = buf;
    info->expr_size = buf_pos;
    buf_pos = 0;
    buf_max = 0;
    buf = NULL;
    expr = NULL;
    expr_pos = 0;
    expr_ctx = NULL;
    expr_ip = 0;
}

#endif

Back to the top