Debian Codegraph @

src/lib/libdwarf/dwarf_tied.c

raw
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 #ifdef DEBUG_PRIMARY_DBG /*debug dumpsignature */
53 void
54 _dwarf_dumpsig(const char *msg, Dwarf_Sig8 *sig,int lineno)
55 {
56 const char *sigv = 0;
57 unsigned u = 0;
58
59 printf("%s 0x",msg);
60 sigv = &sig->signature[0];
61 for (u = 0; u < 8; u++) {
62 printf("%02x",0xff&sigv[u]);
63 }
64 printf(" line %d\n",lineno);
65 }
66 #endif
67
68 void *
69 _dwarf_tied_make_entry(Dwarf_Sig8 *key, Dwarf_CU_Context val)
70 {
71 struct Dwarf_Tied_Entry_s *e = 0;
72 e = calloc(1,sizeof(struct Dwarf_Tied_Entry_s));
73 if (e) {
74 e->dt_key = *key;
75 e->dt_context = val;
76 }
77 return e;
78 }
79
80 /* Tied data Key is Dwarf_Sig8.
81 A hash needed because we are using a hash search
82 here. Would not be needed for the other tree searchs
83 like balanced trees.. */
84 DW_TSHASHTYPE
85 _dwarf_tied_data_hashfunc(const void *keyp)
86 {
87 const struct Dwarf_Tied_Entry_s * enp = keyp;
88 DW_TSHASHTYPE hashv = 0;
89 /* Just take some of the 8 bytes of the signature. */
90 memcpy(&hashv,enp->dt_key.signature,sizeof(hashv));
91 return hashv;
92 }
93
94 int
95 _dwarf_tied_compare_function(const void *l, const void *r)
96 {
97 const struct Dwarf_Tied_Entry_s * lp = l;
98 const struct Dwarf_Tied_Entry_s * rp = r;
99 const char *lcp = (const char *)&lp->dt_key.signature;
100 const char *rcp = (const char *)&rp->dt_key.signature;
101 const char *lcpend = lcp + sizeof(Dwarf_Sig8);
102
103 for ( ;lcp < lcpend; ++lcp,++rcp) {
104 if (*lcp < *rcp) {
105 return -1;
106 }
107 if (*lcp > *rcp) {
108 return 1;
109 }
110 }
111 /* match. */
112 return 0;
113 }
114
115 void
116 _dwarf_tied_destroy_free_node(void*nodep)
117 {
118 struct Dwarf_Tied_Entry_s * enp = nodep;
119 free(enp);
120 return;
121 }
122
123 /* This presumes only we are reading the debug_info
124 CUs from tieddbg. That is a reasonable
125 requirement, one hopes.
126 Currently it reads all the tied CUs at once up to
127 the point of finding a match unless there is an error..
128 This the only way we call _dwarf_next_cu_header*( )
129 on the tied file, so safe.
130 */
131 static int
132 _dwarf_loop_reading_debug_info_for_cu(
133 Dwarf_Debug tieddbg,
134 struct Dwarf_Tied_Entry_s *targsig,
135 Dwarf_Error *error)
136 {
137 /* We will not find tied signatures
138 for .debug_addr (or line tables) in .debug_types.
139 it seems. Those signatures point from
140 'normal' to 'dwo/dwp' (DWARF4) */
141 int is_info = TRUE;
142 Dwarf_Unsigned next_cu_offset = 0;
143
144 for (;;) {
145 int sres = DW_DLV_OK;
146 Dwarf_Half cu_type = 0;
147 Dwarf_CU_Context latestcontext = 0;
148 Dwarf_Unsigned cu_header_length = 0;
149 Dwarf_Unsigned abbrev_offset = 0;
150 Dwarf_Half version_stamp = 0;
151 Dwarf_Half address_size = 0;
152 Dwarf_Half extension_size = 0;
153 Dwarf_Half length_size = 0;
154 Dwarf_Sig8 signature;
155 Dwarf_Bool has_signature = FALSE;
156 Dwarf_Unsigned typeoffset = 0;
157
158 memset(&signature,0,sizeof(signature));
159 sres = _dwarf_next_cu_header_internal(tieddbg,
160 is_info,
161 /* no CU die wanted*/ NULL,
162 &cu_header_length, &version_stamp,
163 &abbrev_offset, &address_size,
164 &length_size,&extension_size,
165 &signature, &has_signature,
166 &typeoffset,
167 &next_cu_offset,
168 &cu_type, error);
169 if (sres == DW_DLV_ERROR) {
170 return sres;
171 }
172 if (sres == DW_DLV_NO_ENTRY) {
173 break;
174 }
175
176 latestcontext = tieddbg->de_info_reading.de_cu_context;
177
178 if (has_signature) {
179 void *retval = 0;
180 Dwarf_Sig8 consign;
181 void *entry = 0;
182
183 if (!latestcontext) {
184 /* FAILED might be out of memory.*/
185 return DW_DLV_NO_ENTRY;
186 }
187 consign = latestcontext->cc_signature;
188 entry = _dwarf_tied_make_entry(&consign,latestcontext);
189 if (!entry) {
190 return DW_DLV_NO_ENTRY;
191 }
192 /* Insert this signature and context. */
193 retval = dwarf_tsearch(entry,
194 &tieddbg->de_tied_data.td_tied_search,
195 _dwarf_tied_compare_function);
196 if (!retval) {
197 free(entry);
198 /* FAILED might be out of memory.*/
199 return DW_DLV_NO_ENTRY;
200 } else {
201 struct Dwarf_Tied_Data_s * retent =
202 *(struct Dwarf_Tied_Data_s**) retval;
203 if (retent == entry) {
204 /* we added a record. */
205 int res = _dwarf_tied_compare_function(
206 targsig,entry);
207 if (!res) {
208 /* Found match, stop looping */
209 return DW_DLV_OK;
210 }
211 continue;
212 } else {
213 /* found existing, no add */
214 free(entry);
215 continue;
216 }
217 }
218 }
219 }
220 /* Apparently we never found the sig we are looking for.
221 Pretend ok. Caller will check for success. */
222 return DW_DLV_OK;
223 }
224
225 /* If out of memory just return DW_DLV_NO_ENTRY.
226 This ensures all the tied CU contexts have been
227 created though the caller has most likely
228 never tried to read CUs in the tied-file.
229 */
230 int
231 _dwarf_search_for_signature(Dwarf_Debug tieddbg,
232 Dwarf_Sig8 sig,
233 Dwarf_CU_Context *context_out,
234 Dwarf_Error *error)
235 {
236
237 void *entry2 = 0;
238 struct Dwarf_Tied_Entry_s entry;
239 struct Dwarf_Tied_Data_s * tied = &tieddbg->de_tied_data;
240 int res = 0;
241
242 if (!tied->td_tied_search) {
243 dwarf_initialize_search_hash(&tied->td_tied_search,
244 _dwarf_tied_data_hashfunc,0);
245 if (!tied->td_tied_search) {
246 return DW_DLV_NO_ENTRY;
247 }
248 }
249 entry.dt_key = sig;
250 entry.dt_context = 0;
251 entry2 = dwarf_tfind(&entry,
252 &tied->td_tied_search,
253 _dwarf_tied_compare_function);
254 if (entry2) {
255 struct Dwarf_Tied_Entry_s *e2 =
256 *(struct Dwarf_Tied_Entry_s **)entry2;
257 *context_out = e2->dt_context;
258 return DW_DLV_OK;
259 }
260 /* We now ensure all tieddbg CUs signatures
261 are in the td_tied_search,
262 The caller is NOT doing
263 info section read operations
264 on the tieddbg in this (tied)dbg, so it
265 cannot goof up their _dwarf_next_cu_header*(). */
266 res = _dwarf_loop_reading_debug_info_for_cu(tieddbg,&entry,
267 error);
268 if (res == DW_DLV_ERROR) {
269 return res;
270 }
271 entry2 = dwarf_tfind(&entry,
272 &tied->td_tied_search,
273 _dwarf_tied_compare_function);
274 if (entry2) {
275 struct Dwarf_Tied_Entry_s *e2 =
276 *(struct Dwarf_Tied_Entry_s **)entry2;
277 *context_out = e2->dt_context;
278 return DW_DLV_OK;
279 }
280 return DW_DLV_NO_ENTRY;
281 }