Coverage Report

Created: 2023-12-08 06:43

/src/libdwarf/src/lib/libdwarf/dwarf_tied.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
3
  Copyright (C) 2015-2015 David Anderson. All Rights Reserved.
4
5
  This program is free software; you can redistribute it
6
  and/or modify it under the terms of version 2.1 of the
7
  GNU Lesser General Public License as published by the Free
8
  Software Foundation.
9
10
  This program is distributed in the hope that it would be
11
  useful, but WITHOUT ANY WARRANTY; without even the implied
12
  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13
  PURPOSE.
14
15
  Further, this software is distributed without any warranty
16
  that it is free of the rightful claim of any third person
17
  regarding infringement or the like.  Any license provided
18
  herein, whether implied or otherwise, applies only to this
19
  software file.  Patent licenses, if any, provided herein
20
  do not apply to combinations of this program with other
21
  software, or any other product whatsoever.
22
23
  You should have received a copy of the GNU Lesser General
24
  Public License along with this program; if not, write the
25
  Free Software Foundation, Inc., 51 Franklin Street - Fifth
26
  Floor, Boston MA 02110-1301, USA.
27
28
*/
29
30
#include <config.h>
31
32
#include <stdio.h>  /* printf() */
33
#include <stdlib.h> /* calloc() free() */
34
#include <string.h> /* memcpy() memset() */
35
36
#if defined(_WIN32) && defined(HAVE_STDAFX_H)
37
#include "stdafx.h"
38
#endif /* HAVE_STDAFX_H */
39
40
#ifdef HAVE_STDINT_H
41
#include <stdint.h> /* uintptr_t */
42
#endif /* HAVE_STDINT_H */
43
44
#include "dwarf.h"
45
#include "libdwarf.h"
46
#include "libdwarf_private.h"
47
#include "dwarf_base_types.h"
48
#include "dwarf_opaque.h"
49
#include "dwarf_tsearch.h"
50
#include "dwarf_tied_decls.h"
51
52
void
53
_dwarf_dumpsig(const char *msg, Dwarf_Sig8 *sig,int lineno)
54
0
{
55
0
    const char *sigv = 0;
56
0
    unsigned u = 0;
57
58
0
    printf("%s 0x",msg);
59
0
    sigv = &sig->signature[0];
60
0
    for (u = 0; u < 8; u++) {
61
0
        printf("%02x",0xff&sigv[u]);
62
0
    }
63
0
    printf(" line %d\n",lineno);
64
0
}
65
66
void *
67
_dwarf_tied_make_entry(Dwarf_Sig8 *key, Dwarf_CU_Context val)
68
0
{
69
0
    struct Dwarf_Tied_Entry_s *e = 0;
70
0
    e = calloc(1,sizeof(struct Dwarf_Tied_Entry_s));
71
0
    if (e) {
72
0
        e->dt_key =    *key;
73
0
        e->dt_context = val;
74
0
    }
75
0
    return e;
76
0
}
77
78
/*  Tied data Key is Dwarf_Sig8.
79
    A hash needed because we are using a hash search
80
    here. Would not be needed for the other tree searchs
81
    like balanced trees..  */
82
DW_TSHASHTYPE
83
_dwarf_tied_data_hashfunc(const void *keyp)
84
0
{
85
0
    const struct Dwarf_Tied_Entry_s * enp = keyp;
86
0
    DW_TSHASHTYPE hashv = 0;
87
    /* Just take some of the 8 bytes of the signature. */
88
0
    memcpy(&hashv,enp->dt_key.signature,sizeof(hashv));
89
0
    return hashv;
90
0
}
91
92
int
93
_dwarf_tied_compare_function(const void *l, const void *r)
94
0
{
95
0
    const struct Dwarf_Tied_Entry_s * lp = l;
96
0
    const struct Dwarf_Tied_Entry_s * rp = r;
97
0
    const char *lcp = (const char *)&lp->dt_key.signature;
98
0
    const char *rcp = (const char *)&rp->dt_key.signature;
99
0
    const char *lcpend = lcp + sizeof(Dwarf_Sig8);
100
101
0
    for ( ;lcp < lcpend; ++lcp,++rcp) {
102
0
        if (*lcp < *rcp) {
103
0
            return -1;
104
0
        }
105
0
        if (*lcp > *rcp) {
106
0
            return 1;
107
0
        }
108
0
    }
109
    /* match. */
110
0
    return 0;
111
0
}
112
113
void
114
_dwarf_tied_destroy_free_node(void*nodep)
115
0
{
116
0
    struct Dwarf_Tied_Entry_s * enp = nodep;
117
0
    free(enp);
118
0
    return;
119
0
}
120
121
/*  This presumes only we are reading the debug_info
122
    CUs from tieddbg. That is a reasonable
123
    requirement, one hopes.
124
    Currently it reads all the tied CUs at once up to
125
    the point of finding a match unless there is an error..
126
    This the only way we call _dwarf_next_cu_header*( )
127
    on the tied file, so safe.
128
    */
129
static int
130
_dwarf_loop_reading_debug_info_for_cu(
131
    Dwarf_Debug tieddbg,
132
    Dwarf_Error *error)
133
0
{
134
    /*  We will not find tied signatures
135
        for .debug_addr (or line tables) in .debug_types.
136
        it seems. Those signatures point from
137
        'normal' to 'dwo/dwp'  (DWARF4) */
138
0
    int is_info = TRUE;
139
0
    Dwarf_CU_Context startingcontext = 0;
140
0
    Dwarf_Unsigned next_cu_offset = 0;
141
142
0
    startingcontext = tieddbg->de_info_reading.de_cu_context;
143
144
0
    if (startingcontext) {
145
0
        next_cu_offset =
146
0
            startingcontext->cc_debug_offset +
147
0
            startingcontext->cc_length +
148
0
            startingcontext->cc_length_size +
149
0
            startingcontext->cc_extension_size;
150
0
    }
151
152
0
    for (;;) {
153
0
        int sres = DW_DLV_OK;
154
0
        Dwarf_Half cu_type = 0;
155
0
        Dwarf_CU_Context latestcontext = 0;
156
0
        Dwarf_Unsigned cu_header_length = 0;
157
0
        Dwarf_Unsigned abbrev_offset = 0;
158
0
        Dwarf_Half version_stamp = 0;
159
0
        Dwarf_Half address_size = 0;
160
0
        Dwarf_Half extension_size = 0;
161
0
        Dwarf_Half length_size = 0;
162
0
        Dwarf_Sig8 signature;
163
0
        Dwarf_Bool has_signature = FALSE;
164
0
        Dwarf_Unsigned typeoffset = 0;
165
166
0
        memset(&signature,0,sizeof(signature));
167
0
        sres = _dwarf_next_cu_header_internal(tieddbg,
168
0
            is_info,
169
            /* no CU die wanted*/ NULL,
170
0
            &cu_header_length, &version_stamp,
171
0
            &abbrev_offset, &address_size,
172
0
            &length_size,&extension_size,
173
0
            &signature, &has_signature,
174
0
            &typeoffset,
175
0
            &next_cu_offset,
176
0
            &cu_type, error);
177
0
        if (sres == DW_DLV_NO_ENTRY) {
178
0
            break;
179
0
        }
180
181
0
        latestcontext = tieddbg->de_info_reading.de_cu_context;
182
183
0
        if (has_signature) {
184
0
            void *retval = 0;
185
0
            Dwarf_Sig8 consign =
186
0
                latestcontext->cc_signature;
187
0
            void *entry =
188
0
                _dwarf_tied_make_entry(&consign,latestcontext);
189
0
            if (!entry) {
190
0
                return DW_DLV_NO_ENTRY;
191
0
            }
192
            /* Insert this signature and context. */
193
0
            retval = dwarf_tsearch(entry,
194
0
                &tieddbg->de_tied_data.td_tied_search,
195
0
                _dwarf_tied_compare_function);
196
0
            if (!retval) {
197
0
                free(entry);
198
                /* FAILED might be out of memory.*/
199
0
                return DW_DLV_NO_ENTRY;
200
0
            } else {
201
0
                struct Dwarf_Tied_Data_s * retent =
202
0
                    *(struct Dwarf_Tied_Data_s**) retval;
203
0
                if (retent == entry) {
204
                    /*  we added a record. */
205
0
                    return DW_DLV_OK;
206
0
                } else {
207
                    /*  found existing, no add */
208
0
                    free(entry);
209
0
                    return DW_DLV_OK;
210
0
                }
211
0
            }
212
0
        }
213
0
    }
214
    /*  Apparently we never found the sig we are looking for.
215
        Pretend ok.  Caller will check for success. */
216
0
    return DW_DLV_OK;
217
0
}
218
219
/* If out of memory just return DW_DLV_NO_ENTRY.
220
*/
221
int
222
_dwarf_search_for_signature(Dwarf_Debug tieddbg,
223
    Dwarf_Sig8 sig,
224
    Dwarf_CU_Context *context_out,
225
    Dwarf_Error *error)
226
0
{
227
228
0
    void *entry2 = 0;
229
0
    struct Dwarf_Tied_Entry_s entry;
230
0
    struct Dwarf_Tied_Data_s * tied = &tieddbg->de_tied_data;
231
0
    int res = 0;
232
233
0
    if (!tied->td_tied_search) {
234
0
        dwarf_initialize_search_hash(&tied->td_tied_search,
235
0
            _dwarf_tied_data_hashfunc,0);
236
0
        if (!tied->td_tied_search) {
237
0
            return DW_DLV_NO_ENTRY;
238
0
        }
239
0
    }
240
0
    entry.dt_key = sig;
241
0
    entry.dt_context = 0;
242
0
    entry2 = dwarf_tfind(&entry,
243
0
        &tied->td_tied_search,
244
0
        _dwarf_tied_compare_function);
245
0
    if (entry2) {
246
0
        struct Dwarf_Tied_Entry_s *e2 =
247
0
            *(struct Dwarf_Tied_Entry_s **)entry2;
248
0
        *context_out = e2->dt_context;
249
0
        return DW_DLV_OK;
250
0
    }
251
252
    /*  We now ensure all tieddbg CUs signatures
253
        are in the td_tied_search,
254
        The caller is NOT doing
255
        info section read operations
256
        on the tieddbg in this (tied)dbg, so it
257
        cannot goof up their _dwarf_next_cu_header*().  */
258
0
    res  = _dwarf_loop_reading_debug_info_for_cu(tieddbg,error);
259
0
    if (res == DW_DLV_ERROR) {
260
0
        return res;
261
0
    }
262
0
    entry2 = dwarf_tfind(&entry,
263
0
        &tied->td_tied_search,
264
0
        _dwarf_tied_compare_function);
265
0
    if (entry2) {
266
0
        struct Dwarf_Tied_Entry_s *e2 =
267
0
            *(struct Dwarf_Tied_Entry_s **)entry2;
268
0
        *context_out = e2->dt_context;
269
0
        return DW_DLV_OK;
270
0
    }
271
0
    return DW_DLV_NO_ENTRY;
272
0
}