Debian Codegraph @

src/lib/libdwarf/dwarf_fission_to_cu.c

raw
1 /*
2 Copyright (C) 2018-2020 David Anderson. All Rights Reserved.
3
4 This program is free software; you can redistribute it
5 and/or modify it under the terms of version 2.1 of the
6 GNU Lesser General Public License as published by the
7 Free Software Foundation.
8
9 This program is distributed in the hope that it would
10 be useful, but WITHOUT ANY WARRANTY; without even the
11 implied warranty of MERCHANTABILITY or FITNESS FOR A
12 PARTICULAR PURPOSE.
13
14 Further, this software is distributed without any warranty
15 that it is free of the rightful claim of any third person
16 regarding infringement or the like. Any license provided
17 herein, whether implied or otherwise, applies only to
18 this software file. Patent licenses, if any, provided
19 herein do not apply to combinations of this program with
20 other software, or any other product whatsoever.
21
22 You should have received a copy of the GNU Lesser General
23 Public License along with this program; if not, write
24 the Free Software Foundation, Inc., 51 Franklin Street -
25 Fifth Floor, Boston MA 02110-1301, USA.
26 */
27
28 #include <config.h>
29
30 #include <string.h> /* memset() */
31 #include <stdlib.h> /* free() */
32 #include <stdio.h> /* debugging printf */
33
34 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
35 #include "stdafx.h"
36 #endif /* HAVE_STDAFX_H */
37
38 #include "dwarf.h"
39 #include "libdwarf.h"
40 #include "libdwarf_private.h"
41 #include "dwarf_base_types.h"
42 #include "dwarf_opaque.h"
43 #include "dwarf_alloc.h"
44 #include "dwarf_error.h"
45 #include "dwarf_util.h"
46 #include "dwarf_string.h"
47 #include "dwarf_str_offsets.h"
48 #include "dwarf_loc.h"
49 #include "dwarf_loclists.h"
50 #include "dwarf_rnglists.h"
51
52 /* RETURNS DW_DLV_OK and sets values
53 through the return-value pointers.
54 Or returns DW_DLV_NO_ENTRY */
55 int
56 _dwarf_has_SECT_fission(Dwarf_CU_Context ctx,
57 unsigned int SECT_number,
58 Dwarf_Bool *hasfissionoffset,
59 Dwarf_Unsigned *loclistsbase)
60 {
61 struct Dwarf_Debug_Fission_Per_CU_s *fis = 0;
62 Dwarf_Unsigned fisindex = SECT_number;
63
64 fis = &ctx->cc_dwp_offsets;
65 if (fis->pcu_type && fis->pcu_size[fisindex]) {
66 *loclistsbase = fis->pcu_offset[fisindex];
67 *hasfissionoffset = TRUE;
68 return DW_DLV_OK;
69 }
70 return DW_DLV_NO_ENTRY;
71 }
72
73 /* ASSERT: dbg,cu_context, and fsd are non-NULL
74 as the caller ensured that.
75 With no DW_AT_loclists_base this computes one. */
76 const struct Dwarf_Loclists_Context_s localcontxt_zero;
77 static int
78 load_xu_loclists_into_cucontext(Dwarf_Debug dbg,
79 Dwarf_CU_Context cu_context,
80 struct Dwarf_Debug_Fission_Per_CU_s*fsd,
81 int fsd_index,
82 Dwarf_Error *error)
83 {
84 Dwarf_Unsigned size = 0;
85 Dwarf_Unsigned soff_hdroffset = 0;
86 Dwarf_Unsigned soff_size = 0;
87 struct Dwarf_Loclists_Context_s localcontxt;
88 Dwarf_Loclists_Context buildhere = &localcontxt;
89 Dwarf_Unsigned nextset = 0;
90 int res = 0;
91
92 if (!fsd) {
93 _dwarf_error_string(dbg, error, DW_DLE_XU_TYPE_ARG_ERROR,
94 "DW_DLE_XU_TYPE_ARG_ERROR: a required argument to"
95 "load_xu_loclists_into_cucontext() is NULL");
96 return DW_DLV_ERROR;
97 }
98 localcontxt = localcontxt_zero;
99 size = fsd->pcu_size[fsd_index];
100 soff_hdroffset = fsd->pcu_offset[fsd_index];
101 soff_size = dbg->de_debug_loclists.dss_size;
102 if (!soff_size) {
103 return DW_DLV_NO_ENTRY;
104 }
105 if (soff_hdroffset >= soff_size) {
106 /* Something is badly wrong. Ignore it here. */
107 return DW_DLV_NO_ENTRY;
108 }
109 res = _dwarf_internal_read_loclists_header(dbg, FALSE,
110 0,soff_size,
111 dbg->de_debug_loclists.dss_data,
112 dbg->de_debug_loclists.dss_data +soff_size,
113 soff_hdroffset,
114 buildhere,
115 &nextset,error);
116 if (res != DW_DLV_OK) {
117 free(buildhere->lc_offset_value_array);
118 buildhere->lc_offset_value_array = 0;
119 return res;
120 }
121 cu_context->cc_loclists_base_present = TRUE;
122 cu_context->cc_loclists_base_contr_size = size;
123 cu_context->cc_loclists_base =
124 buildhere->lc_offsets_off_in_sect;
125 free(buildhere->lc_offset_value_array);
126 buildhere->lc_offset_value_array = 0;
127 return DW_DLV_OK;
128 }
129
130 /*
131
132 ASSERT: dbg,cu_context, and fsd are non-NULL
133 as the caller ensured that.
134 If .debug_cu_index or
135 .debug_tu_index is present it might help us find
136 the offset for this CU's .debug_str_offsets.
137 */
138 static int
139 load_xu_str_offsets_into_cucontext(Dwarf_Debug dbg,
140 Dwarf_CU_Context cu_context,
141 struct Dwarf_Debug_Fission_Per_CU_s*fsd,
142 int fsd_index,
143 Dwarf_Error *error )
144 {
145 Dwarf_Small *soff_secptr = 0;
146 Dwarf_Unsigned soff_hdroffset = 0;
147 Dwarf_Unsigned soff_size = 0;
148 Dwarf_Small *soff_eptr = 0;
149 int res = 0;
150
151 res = _dwarf_load_section(dbg, &dbg->de_debug_str_offsets,
152 error);
153 if (res != DW_DLV_OK) {
154 return res;
155 }
156 soff_hdroffset = fsd->pcu_offset[fsd_index];
157 soff_secptr = dbg->de_debug_str_offsets.dss_data;
158 soff_size = dbg->de_debug_str_offsets.dss_size;
159 if (soff_hdroffset >= soff_size) {
160 /* Something is badly wrong. Ignore it here. */
161 return DW_DLV_NO_ENTRY;
162 }
163
164 {
165 Dwarf_Unsigned length = 0;
166 Dwarf_Half offset_size = 0;
167 Dwarf_Half extension_size = 0;
168 Dwarf_Half version = 0;
169 Dwarf_Half padding = 0;
170 Dwarf_Unsigned local_offset_to_array=0;
171 Dwarf_Unsigned total_table_length =0;
172 struct Dwarf_Str_Offsets_Table_s sotstr;
173
174 memset(&sotstr,0,sizeof(sotstr));
175 sotstr.so_dbg = dbg;
176 sotstr.so_section_start_ptr = soff_secptr;
177 sotstr.so_section_end_ptr = soff_eptr;
178 sotstr.so_section_size = soff_size;
179 sotstr.so_next_table_offset = soff_hdroffset;
180 res = _dwarf_read_str_offsets_header(&sotstr,
181 cu_context,
182 &length,&offset_size,
183 &extension_size,&version,&padding,
184 &local_offset_to_array,
185 &total_table_length,
186 error);
187 if (res != DW_DLV_OK) {
188 if (res == DW_DLV_ERROR && error) {
189 dwarf_dealloc_error(dbg,*error);
190 *error = 0;
191 }
192 return DW_DLV_NO_ENTRY;
193 }
194 /* See dwarf_opaque.h for comments. */
195 cu_context->cc_str_offsets_tab_present = TRUE;
196 cu_context->cc_str_offsets_header_offset = soff_hdroffset;
197 cu_context->cc_str_offsets_tab_to_array =
198 local_offset_to_array;
199 cu_context->cc_str_offsets_table_size = total_table_length;
200 cu_context->cc_str_offsets_version = version;
201 cu_context->cc_str_offsets_offset_size = offset_size;
202 }
203 return DW_DLV_OK;
204 }
205
206 /* ASSERT: dbg,cu_context, and fsd are non-NULL
207 as the caller ensured that. */
208 static int
209 load_xu_debug_macro_into_cucontext(Dwarf_Debug dbg,
210 Dwarf_CU_Context cu_context,
211 struct Dwarf_Debug_Fission_Per_CU_s*fsd,
212 int fsd_index,
213 Dwarf_Error *error )
214 {
215 Dwarf_Unsigned size = 0;
216 Dwarf_Unsigned soff_hdroffset = 0;
217 Dwarf_Unsigned soff_size = 0;
218 int res = 0;
219
220 res = _dwarf_load_section(dbg, &dbg->de_debug_macro,
221 error);
222 if (res != DW_DLV_OK) {
223 return res;
224 }
225 size = fsd->pcu_size[fsd_index];
226 soff_hdroffset = fsd->pcu_offset[fsd_index];
227 soff_size = dbg->de_debug_macro.dss_size;
228 if (!soff_size) {
229 return DW_DLV_NO_ENTRY;
230 }
231 if (soff_hdroffset >= soff_size) {
232 /* Something is badly wrong. Ignore it here. */
233 return DW_DLV_NO_ENTRY;
234 }
235 /* Presently assuming that DW_AT_macros
236 and the fission entry both
237 indicate the beginning
238 of a .debug_macro sectiom macro header.
239 (not true for str_offsets or for loclists!)
240 */
241 cu_context->cc_macro_base_present = TRUE;
242 cu_context->cc_macro_base_contr_size = size;
243 cu_context->cc_macro_base = soff_hdroffset;
244 /* FIXME cc_macro_header_length_present? */
245 return DW_DLV_OK;
246 }
247
248 /* ASSERT: dbg,cu_context, and fsd are non-NULL
249 as the caller ensured that.
250 With no DW_AT_rnglists_base present this
251 computes the value. */
252 const struct Dwarf_Rnglists_Context_s builddata_zero;
253 static int
254 load_xu_rnglists_into_cucontext(Dwarf_Debug dbg,
255 Dwarf_CU_Context cu_context,
256 struct Dwarf_Debug_Fission_Per_CU_s*fsd,
257 int fsd_index,
258 Dwarf_Error *error )
259 {
260 Dwarf_Unsigned size = 0;
261 Dwarf_Unsigned soff_hdroffset = 0;
262 Dwarf_Unsigned soff_size = 0;
263 struct Dwarf_Rnglists_Context_s builddata;
264 Dwarf_Rnglists_Context buildhere = &builddata;
265 Dwarf_Unsigned nextoffset = 0;
266 int res = 0;
267
268 builddata = builddata_zero;
269 res = _dwarf_load_section(dbg, &dbg->de_debug_rnglists,
270 error);
271 if (res != DW_DLV_OK) {
272 return res;
273 }
274 size = fsd->pcu_size[fsd_index];
275 soff_hdroffset = fsd->pcu_offset[fsd_index];
276 soff_size = dbg->de_debug_rnglists.dss_size;
277 if (!soff_size) {
278 return DW_DLV_NO_ENTRY;
279 }
280 if (soff_hdroffset >= soff_size) {
281 /* Something is badly wrong. Ignore it here. */
282 return DW_DLV_NO_ENTRY;
283 }
284 memset(buildhere,0,sizeof(builddata));
285 res = _dwarf_internal_read_rnglists_header(dbg, TRUE,
286 0,soff_size,
287 dbg->de_debug_rnglists.dss_data,
288 dbg->de_debug_rnglists.dss_data+soff_size,
289 soff_hdroffset,buildhere,
290 &nextoffset,error);
291 if (res != DW_DLV_OK) {
292 return res;
293 }
294
295 cu_context->cc_rnglists_base =
296 buildhere->rc_offsets_off_in_sect;
297 printf("debug SET rnglists base from rc_offsetts_off_in_sectt: "
298 "0x%lx lie %d\n",
299 (unsigned long)cu_context->cc_rnglists_base,
300 __LINE__);
301 cu_context->cc_rnglists_base_present = TRUE;
302 cu_context->cc_rnglists_base_contr_size = size;
303 /* FIXME cc_rnglists_header_length_present? */
304 return DW_DLV_OK;
305 }
306
307 static const char *keylist[2] = {
308 "cu",
309 "tu"
310 };
311 /* ASSERT: The context has a signature.
312
313 ASSERT: dbg and cu_context are non-NULL
314 as the caller tested them.
315
316 _dwarf_make_CU_Context() calls
317 finish_up_cu_context_from_cudie() which calls
318 us here.
319 Then, _dwarf_make_CU_Context() calls
320 _dwarf_merge_all_base_attrs_of_cu_die() if there
321 is a tied (executable) object known.
322 (not all base attrs are merged from tied. Certainly not
323 .debug_rnglists or .debug_loclists, but here we
324 load correct table information
325 for the CU being read as a replacement
326 if a CU has no base attr for rnglists and/or loclists)
327
328 Called by dwarf_die_deliv.c
329 */
330 const struct Dwarf_Debug_Fission_Per_CU_s fission_data_zero;
331 int
332 _dwarf_find_all_offsets_via_fission(Dwarf_Debug dbg,
333 Dwarf_CU_Context cu_context,
334 Dwarf_Error *error)
335 {
336 struct Dwarf_Debug_Fission_Per_CU_s fission_data;
337 struct Dwarf_Debug_Fission_Per_CU_s*fsd = 0;
338 int si = 0;
339 int smax = 2;
340 int fdres = 0;
341 int res = 0;
342
343 fission_data = fission_data_zero;
344 fsd = &fission_data;
345 for (si = 0; si < smax ; ++si) {
346 int sec_index = 0;
347
348 memset(&fission_data,0,sizeof(fission_data));
349 fdres = dwarf_get_debugfission_for_key(dbg,
350 &cu_context->cc_signature,
351 keylist[si],
352 fsd,error);
353 if (fdres == DW_DLV_NO_ENTRY) {
354 continue;
355 }
356 if (fdres == DW_DLV_ERROR) {
357 if (error) {
358 dwarf_dealloc_error(dbg,*error);
359 *error = 0;
360 }
361 continue;
362 }
363 for (sec_index = 1; sec_index < DW_FISSION_SECT_COUNT;
364 ++sec_index) {
365 if (!fsd->pcu_size[sec_index]) {
366 continue;
367 }
368 res = DW_DLV_OK;
369 switch(sec_index) {
370 /* these handled elsewhere, such
371 as by _dwarf_get_dwp_extra_offset()
372 _dwarf_get_fission_addition_die()
373 case DW_SECT_INFO:
374 case DW_SECT_ABBREV:
375 case DW_SECT_LINE:
376 */
377
378 case DW_SECT_LOCLISTS:
379 res = load_xu_loclists_into_cucontext(dbg,
380 cu_context,
381 fsd,sec_index,error);
382 break;
383 case DW_SECT_RNGLISTS:
384 res = load_xu_rnglists_into_cucontext(dbg,
385 cu_context,
386 fsd,sec_index,error);
387 break;
388 case DW_SECT_STR_OFFSETS:
389 res = load_xu_str_offsets_into_cucontext(dbg,
390 cu_context,
391 fsd,sec_index,error);
392 break;
393 case DW_SECT_MACRO:
394 res = load_xu_debug_macro_into_cucontext(dbg,
395 cu_context,
396 fsd,sec_index,error);
397 break;
398 default:
399 res = DW_DLV_OK;
400 break;
401 }
402 if (res == DW_DLV_ERROR) {
403 return res;
404 }
405 }
406 }
407 return DW_DLV_OK;
408 }