summaryrefslogtreecommitdiff
path: root/libdwarf/dwarf_frame3.c
blob: 6f6e25d59b7ba8d202accf7659b898d20216a02f (plain)
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
/*

  Copyright (C) 2000-2006 Silicon Graphics, Inc.  All Rights Reserved.
  Portions Copyright (C) 2009-2011 David Anderson. All Rights Reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2.1 of the GNU Lesser General Public License 
  as published by the Free Software Foundation.

  This program is distributed in the hope that it would be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

  Further, this software is distributed without any warranty that it is
  free of the rightful claim of any third person regarding infringement 
  or the like.  Any license provided herein, whether implied or 
  otherwise, applies only to this software file.  Patent licenses, if
  any, provided herein do not apply to combinations of this program with 
  other software, or any other product whatsoever.  

  You should have received a copy of the GNU Lesser General Public 
  License along with this program; if not, write the Free Software 
  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
  USA.

  Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
  Mountain View, CA 94043, or:

  http://www.sgi.com

  For further information regarding this notice, see:

  http://oss.sgi.com/projects/GenInfo/NoticeExplan

*/



#include "config.h"
#include "dwarf_incl.h"
#include <stdio.h>
#include <stdlib.h>
#include "dwarf_frame.h"
#include "dwarf_arange.h" /* using Arange as a way to build a list */

/*  Used by rqs (an IRIX application).  
    Not needed except for that one application.
    Should be moved to its own source file since
    it is so rarely needed.
    Returns DW_DLV_OK if returns the arrays.
    Returns DW_DLV_NO_ENTRY if no section. ?? (How do I tell?)
    Returns DW_DLV_ERROR if there is an error.

    Uses DW_FRAME_CFA_COL because IRIX is only DWARF2
    and that is what IRIX compilers and compatible
    compilers support on IRIX.
*/
int
_dwarf_frame_address_offsets(Dwarf_Debug dbg, Dwarf_Addr ** addrlist,
    Dwarf_Off ** offsetlist,
    Dwarf_Signed * returncount,
    Dwarf_Error * err)
{
    int retval = DW_DLV_OK;
    int res = DW_DLV_ERROR;
    Dwarf_Cie *cie_data;
    Dwarf_Signed cie_count;
    Dwarf_Fde *fde_data;
    Dwarf_Signed fde_count;
    Dwarf_Signed i;
    Dwarf_Frame_Op *frame_inst;
    Dwarf_Fde fdep;
    Dwarf_Cie ciep;
    Dwarf_Chain curr_chain = 0;
    Dwarf_Chain head_chain = 0;
    Dwarf_Chain prev_chain = 0;
    Dwarf_Arange arange;
    Dwarf_Unsigned arange_count = 0;
    Dwarf_Addr *arange_addrs = 0;
    Dwarf_Off *arange_offsets = 0;

    res = dwarf_get_fde_list(dbg, &cie_data, &cie_count,
        &fde_data, &fde_count, err);
    if (res != DW_DLV_OK) {
        return res;
    }

    res = _dwarf_load_section(dbg, &dbg->de_debug_frame, err);
    if (res != DW_DLV_OK) {
        return res;
    }
    if (!dbg->de_debug_frame.dss_size) {
        return (DW_DLV_NO_ENTRY);
    }

    for (i = 0; i < cie_count; i++) {
        Dwarf_Off instoff = 0;
        Dwarf_Signed initial_instructions_length = 0;
        Dwarf_Small *instr_end = 0;
        Dwarf_Sword icount = 0;
        int j = 0;
        int dw_err;

        ciep = cie_data[i];
        instoff = ciep->ci_cie_instr_start - dbg->de_debug_frame.dss_data;
        initial_instructions_length = ciep->ci_length +
            ciep->ci_length_size + ciep->ci_extension_size -
            (ciep->ci_cie_instr_start - ciep->ci_cie_start);
        instr_end = ciep->ci_cie_instr_start +
            initial_instructions_length;
        res = _dwarf_exec_frame_instr( /* make_instr */ true,
            &frame_inst,
            /* search_pc= */ false,
            /* search_pc_val= */ 0,
            /* location */ 0,
            ciep->ci_cie_instr_start,
            instr_end,
            /* Dwarf_frame= */ 0,
            /* cie= */ 0,
            dbg,
            DW_FRAME_CFA_COL,
            &icount, &dw_err);
        if (res == DW_DLV_ERROR) {
            _dwarf_error(dbg, err, dw_err);
            return (res);
        } else if (res == DW_DLV_NO_ENTRY) {
            continue;
        }

        for (j = 0; j < icount; ++j) {
            Dwarf_Frame_Op *finst = frame_inst + j;

            if (finst->fp_base_op == 0 && finst->fp_extended_op == 1) {
                /* is DW_CFA_set_loc */
                Dwarf_Addr add = (Dwarf_Addr) finst->fp_offset;
                Dwarf_Off off = finst->fp_instr_offset + instoff;

                arange = (Dwarf_Arange)
                    _dwarf_get_alloc(dbg, DW_DLA_ARANGE, 1);
                if (arange == NULL) {
                    _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
                    return (DW_DLV_ERROR);
                }
                arange->ar_address = add;
                arange->ar_info_offset = off;
                arange_count++;
                curr_chain = (Dwarf_Chain)
                    _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
                if (curr_chain == NULL) {
                    _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
                    return (DW_DLV_ERROR);
                }
                curr_chain->ch_item = arange;
                if (head_chain == NULL)
                    head_chain = prev_chain = curr_chain;
                else {
                    prev_chain->ch_next = curr_chain;
                    prev_chain = curr_chain;
                }
            }
        }
        dwarf_dealloc(dbg, frame_inst, DW_DLA_FRAME_BLOCK);

    }
    for (i = 0; i < fde_count; i++) {
        Dwarf_Small *instr_end = 0;
        Dwarf_Sword icount = 0;
        Dwarf_Signed instructions_length = 0;
        Dwarf_Off instoff = 0;
        Dwarf_Off off = 0;
        Dwarf_Addr addr = 0;
        int j = 0;
        int dw_err;

        fdep = fde_data[i];
        off = fdep->fd_initial_loc_pos - dbg->de_debug_frame.dss_data;
        addr = fdep->fd_initial_location;
        arange = (Dwarf_Arange)
            _dwarf_get_alloc(dbg, DW_DLA_ARANGE, 1);
        if (arange == NULL) {
            _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
            return (DW_DLV_ERROR);
        }
        arange->ar_address = addr;
        arange->ar_info_offset = off;
        arange_count++;
        curr_chain = (Dwarf_Chain)
            _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
        if (curr_chain == NULL) {
            _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
            return (DW_DLV_ERROR);
        }
        curr_chain->ch_item = arange;
        if (head_chain == NULL)
            head_chain = prev_chain = curr_chain;
        else {
            prev_chain->ch_next = curr_chain;
            prev_chain = curr_chain;
        }


        instoff = fdep->fd_fde_instr_start - dbg->de_debug_frame.dss_data;
        instructions_length = fdep->fd_length +
            fdep->fd_length_size + fdep->fd_extension_size -
            (fdep->fd_fde_instr_start - fdep->fd_fde_start);
        instr_end = fdep->fd_fde_instr_start + instructions_length;
        res = _dwarf_exec_frame_instr( /* make_instr */ true,
            &frame_inst,
            /* search_pc= */ false,
            /* search_pc_val= */ 0,
            /* location */ 0,
            fdep->fd_fde_instr_start,
            instr_end,
            /* Dwarf_frame= */ 0,
            /* cie= */ 0,
            dbg,
            DW_FRAME_CFA_COL,
            &icount, &dw_err);
        if (res == DW_DLV_ERROR) {
            _dwarf_error(dbg, err, dw_err);
            return (res);
        } else if (res == DW_DLV_NO_ENTRY) {
            continue;
        }

        for (j = 0; j < icount; ++j) {
            Dwarf_Frame_Op *finst2 = frame_inst + j;

            if (finst2->fp_base_op == 0 && finst2->fp_extended_op == 1) {
                /* is DW_CFA_set_loc */
                Dwarf_Addr add = (Dwarf_Addr) finst2->fp_offset;
                Dwarf_Off off = finst2->fp_instr_offset + instoff;

                arange = (Dwarf_Arange)
                    _dwarf_get_alloc(dbg, DW_DLA_ARANGE, 1);
                if (arange == NULL) {
                    _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
                    return (DW_DLV_ERROR);
                }
                arange->ar_address = add;
                arange->ar_info_offset = off;
                arange_count++;
                curr_chain = (Dwarf_Chain)
                    _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
                if (curr_chain == NULL) {
                    _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
                    return (DW_DLV_ERROR);
                }
                curr_chain->ch_item = arange;
                if (head_chain == NULL)
                    head_chain = prev_chain = curr_chain;
                else {
                    prev_chain->ch_next = curr_chain;
                    prev_chain = curr_chain;
                }

            }
        }
        dwarf_dealloc(dbg, frame_inst, DW_DLA_FRAME_BLOCK);

    }
    dwarf_dealloc(dbg, fde_data, DW_DLA_LIST);
    dwarf_dealloc(dbg, cie_data, DW_DLA_LIST);
    arange_addrs = (Dwarf_Addr *)
        _dwarf_get_alloc(dbg, DW_DLA_ADDR, arange_count);
    if (arange_addrs == NULL) {
        _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
        return (DW_DLV_ERROR);
    }
    arange_offsets = (Dwarf_Off *)
        _dwarf_get_alloc(dbg, DW_DLA_ADDR, arange_count);
    if (arange_offsets == NULL) {
        _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
        return (DW_DLV_ERROR);
    }

    curr_chain = head_chain;
    for (i = 0; i < arange_count; i++) {
        Dwarf_Arange ar = curr_chain->ch_item;

        arange_addrs[i] = ar->ar_address;
        arange_offsets[i] = ar->ar_info_offset;
        prev_chain = curr_chain;
        curr_chain = curr_chain->ch_next;
        dwarf_dealloc(dbg, ar, DW_DLA_ARANGE);
        dwarf_dealloc(dbg, prev_chain, DW_DLA_CHAIN);
    }
    *returncount = arange_count;
    *offsetlist = arange_offsets;
    *addrlist = arange_addrs;
    return retval;
}