Debian Codegraph @

src/lib/libdwarf/dwarf_macro5.c

raw
1 /*
2 Copyright (C) 2015-2022 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 Free
7 Software Foundation.
8
9 This program is distributed in the hope that it would be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 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 this
18 software file. Patent licenses, if any, provided herein
19 do not apply to combinations of this program with other
20 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 the
24 Free Software Foundation, Inc., 51 Franklin Street - Fifth
25 Floor, Boston MA 02110-1301, USA.
26
27 */
28
29 #include <config.h>
30
31 #include <stdlib.h> /* calloc() free() malloc() */
32 #include <string.h> /* memset() strcat() strlen() */
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_safe_strcpy.h"
43 #include "dwarf_opaque.h"
44 #include "dwarf_alloc.h"
45 #include "dwarf_error.h"
46 #include "dwarf_util.h"
47 #include "dwarf_macro5.h"
48 #include "dwarf_string.h"
49 #include "dwarf_str_offsets.h"
50
51 #define MC_SENTINEL 0xada
52
53 #define CHECKNULLCONTEXT(m,d,e) \
54 if (!(m) || (m)->mc_sentinel != MC_SENTINEL) { \
55 if (m) { (d) = (m)->mc_dbg; } \
56 _dwarf_error_string((d), (e), \
57 DW_DLE_BAD_MACRO_HEADER_POINTER, \
58 "DW_DLE_BAD_MACRO_HEADER_POINTER " \
59 " NULL header or corrupt header"); \
60 return DW_DLV_ERROR; \
61 }
62
63 /* Section 6.3: Macro Information:
64 Each macro unit ends with an entry
65 containing an opcode of 0. */
66
67 static const Dwarf_Small dwarf_udata_string_form[] =
68 {DW_FORM_udata,DW_FORM_string};
69 static const Dwarf_Small dwarf_udata_udata_form[] =
70 {DW_FORM_udata,DW_FORM_udata};
71 static const Dwarf_Small dwarf_udata_strp_form[] =
72 {DW_FORM_udata,DW_FORM_strp};
73 static const Dwarf_Small dwarf_udata_strp_sup_form[] =
74 {DW_FORM_udata,DW_FORM_strp_sup};
75 static const Dwarf_Small dwarf_secoffset_form[] =
76 {DW_FORM_sec_offset};
77 static const Dwarf_Small dwarf_udata_strx_form[] =
78 {DW_FORM_udata,DW_FORM_strx};
79
80 struct Dwarf_Macro_Forms_s dw5formsarray[] = {
81 {0,0,0},
82 {DW_MACRO_define,2,dwarf_udata_string_form},
83 {DW_MACRO_undef,2,dwarf_udata_string_form},
84 {DW_MACRO_start_file,2,dwarf_udata_udata_form},
85 {DW_MACRO_end_file,0,0},
86
87 {DW_MACRO_define_strp,2,dwarf_udata_strp_form},
88 {DW_MACRO_undef_strp,2,dwarf_udata_strp_form},
89 {DW_MACRO_import,1,dwarf_secoffset_form},
90
91 {DW_MACRO_define_sup,2,dwarf_udata_strp_sup_form},
92 {DW_MACRO_undef_sup,2,dwarf_udata_strp_sup_form},
93 {DW_MACRO_import_sup,1,dwarf_secoffset_form},
94
95 {DW_MACRO_define_strx,2,dwarf_udata_strx_form},
96 {DW_MACRO_undef_strx,2,dwarf_udata_strx_form},
97 };
98
99 /* Represents DWARF 5 macro info */
100 /* .debug_macro predefined, in order by value */
101 static const struct Dwarf_Macro_OperationsList_s
102 dwarf_default_macro_opslist = {
103 13, dw5formsarray
104 };
105
106 static int _dwarf_internal_macro_context_by_offset(Dwarf_Debug dbg,
107 Dwarf_Unsigned offset,
108 Dwarf_Unsigned * version_out,
109 Dwarf_Macro_Context * macro_context_out,
110 Dwarf_Unsigned *macro_ops_count_out,
111 Dwarf_Unsigned *macro_ops_data_length,
112 char **srcfiles,
113 Dwarf_Signed srcfilescount,
114 const char *comp_dir,
115 const char *comp_name,
116 Dwarf_CU_Context cu_context,
117 Dwarf_Error * error);
118
119 static int _dwarf_internal_macro_context(Dwarf_Die die,
120 Dwarf_Bool offset_specified,
121 Dwarf_Unsigned offset,
122 Dwarf_Unsigned * version_out,
123 Dwarf_Macro_Context * macro_context_out,
124 Dwarf_Unsigned *macro_unit_offset_out,
125 Dwarf_Unsigned *macro_ops_count_out,
126 Dwarf_Unsigned *macro_ops_data_length,
127 Dwarf_Error * error);
128
129 static int
130 is_std_moperator(Dwarf_Small op)
131 {
132 if (op >= 1 && op <= DW_MACRO_undef_strx) {
133 return TRUE;
134 }
135 return FALSE;
136 }
137
138 static int
139 _dwarf_skim_forms(Dwarf_Debug dbg,
140 Dwarf_Macro_Context mcontext,
141 Dwarf_Small *mdata_start,
142 unsigned formcount,
143 const Dwarf_Small *forms,
144 Dwarf_Small *section_end,
145 Dwarf_Unsigned *forms_length,
146 Dwarf_Error *error)
147 {
148 unsigned i = 0;
149 Dwarf_Small curform = 0 ;
150 Dwarf_Unsigned totallen = 0;
151 Dwarf_Unsigned v = 0;
152 Dwarf_Unsigned ret_value = 0;
153 Dwarf_Unsigned length;
154 Dwarf_Small *mdata = mdata_start;
155 Dwarf_Unsigned leb128_length = 0;
156
157 for ( ; i < formcount; ++i) {
158 curform = forms[i];
159 if (mdata >= section_end) {
160 _dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
161 return DW_DLV_ERROR;
162 }
163 switch(curform) {
164 default:
165 _dwarf_error(dbg,error,
166 DW_DLE_DEBUG_FORM_HANDLING_INCOMPLETE);
167 return DW_DLV_ERROR;
168 case DW_FORM_block1:
169 v = *(Dwarf_Small *) mdata;
170 totallen += v+1;
171 mdata += v+1;
172 break;
173 case DW_FORM_block2:
174 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
175 mdata, DWARF_HALF_SIZE,
176 error,section_end);
177 v = ret_value + DWARF_HALF_SIZE;
178 totallen += v;
179 mdata += v;
180 break;
181 case DW_FORM_block4:
182 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
183 mdata, DWARF_32BIT_SIZE,
184 error,section_end);
185 v = ret_value + DWARF_32BIT_SIZE;
186 totallen += v;
187 mdata += v;
188 break;
189 case DW_FORM_data1:
190 v = 1;
191 totallen += v;
192 mdata += v;
193 break;
194 case DW_FORM_data2:
195 v = 2;
196 totallen += v;
197 mdata += v;
198 break;
199 case DW_FORM_data4:
200 v = 4;
201 totallen += v;
202 mdata += v;
203 break;
204 case DW_FORM_data8:
205 v = 8;
206 totallen += v;
207 mdata += v;
208 break;
209 case DW_FORM_data16:
210 v = 8;
211 totallen += v;
212 mdata += v;
213 break;
214 case DW_FORM_string: {
215 int res = _dwarf_check_string_valid(dbg,
216 mdata,mdata, section_end,
217 DW_DLE_MACRO_STRING_BAD,error);
218 if (res != DW_DLV_OK) {
219 return res;
220 }
221 v = strlen((char *) mdata) + 1;
222 totallen += v;
223 mdata += v;
224 }
225 break;
226 case DW_FORM_block:
227 DECODE_LEB128_UWORD_LEN_CK(mdata,length,leb128_length,
228 dbg, error,section_end);
229 v = length + leb128_length;
230 totallen += v;
231 break;
232 case DW_FORM_flag:
233 v = 1;
234 totallen += v;
235 mdata += v;
236 break;
237 case DW_FORM_sec_offset:
238 /* If 32bit dwarf, is 4. Else is 64bit dwarf and is 8. */
239 v = mcontext->mc_offset_size;
240 totallen += v;
241 mdata += v;
242 break;
243 case DW_FORM_sdata:
244 /* Discard the decoded value, we just want the length
245 of the value. */
246 DECODE_LEB128_UWORD_LEN_CK(mdata,length,leb128_length,
247 dbg, error,section_end);
248 totallen += v;
249 break;
250 case DW_FORM_strx:
251 DECODE_LEB128_UWORD_LEN_CK(mdata,length,leb128_length,
252 dbg, error,section_end);
253 totallen += leb128_length;;
254 break;
255 case DW_FORM_strp:
256 v = mcontext->mc_offset_size;
257 mdata += v;
258 totallen += v;
259 break;
260 case DW_FORM_udata:
261 /* Discard the decoded value, we just want the length
262 of the value. */
263 DECODE_LEB128_UWORD_LEN_CK(mdata,length,leb128_length,
264 dbg, error,section_end);
265 totallen += leb128_length;
266 break;
267 }
268 }
269 if (mdata > section_end) {
270 _dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
271 return DW_DLV_ERROR;
272 }
273 *forms_length = totallen;
274 return DW_DLV_OK;
275 }
276
277 #if 0 /* FOR DEBUGGING */
278 static void
279 dump_bytes_x(Dwarf_Small * start, long len)
280 {
281 Dwarf_Small *end = start + len;
282 Dwarf_Small *cur = start;
283 unsigned pos = 0;
284
285 printf("dump %ld bytes, start at 0x%lx\n",
286 len,(unsigned long)start);
287 printf("0x");
288 for (; cur < end;pos++, cur++) {
289 if (!(pos %4)) {
290 printf(" ");
291 }
292 printf("%02x",*cur);
293 }
294 printf("\n");
295 }
296 Dwarf_Bool
297 is_defundef(unsigned op)
298 {
299 switch(op){
300 case DW_MACRO_define:
301 case DW_MACRO_undef:
302 case DW_MACRO_define_strp:
303 case DW_MACRO_undef_strp:
304 case DW_MACRO_define_strx:
305 case DW_MACRO_undef_strx:
306 case DW_MACRO_define_sup:
307 case DW_MACRO_undef_sup:
308 return TRUE;
309 default: break;
310 }
311 return FALSE;
312 }
313 #endif /*0*/
314
315 /* On first call (for this macro_context),
316 build_ops_array is FALSE. On second,
317 it is TRUE and we know the count so we allocate and fill in
318 the ops array. */
319 static int
320 _dwarf_get_macro_ops_count_internal(Dwarf_Macro_Context macro_context,
321 Dwarf_Bool build_ops_array,
322 Dwarf_Error *error)
323 {
324 Dwarf_Debug dbg = 0;
325 Dwarf_Small *mdata = 0;
326 Dwarf_Small *section_end = 0;
327 Dwarf_Small *section_base = 0;
328 Dwarf_Unsigned opcount = 0;
329 Dwarf_Unsigned known_ops_count = 0;
330 struct Dwarf_Macro_Operator_s *opsarray = 0;
331 struct Dwarf_Macro_Operator_s *curopsentry = 0;
332 int res = 0;
333
334 dbg = macro_context->mc_dbg;
335 if (build_ops_array) {
336 known_ops_count = macro_context->mc_macro_ops_count;
337 opsarray = (struct Dwarf_Macro_Operator_s *)
338 calloc(known_ops_count,
339 sizeof(struct Dwarf_Macro_Operator_s));
340 if (!opsarray) {
341 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
342 return DW_DLV_ERROR;
343 }
344 curopsentry = opsarray;
345 macro_context->mc_ops = opsarray;
346 }
347 section_base = dbg->de_debug_macro.dss_data;
348 section_end = section_base + dbg->de_debug_macro.dss_size;
349 mdata = macro_context->mc_macro_ops;
350
351 while (mdata < section_end) {
352 Dwarf_Small op = 0;
353
354 op = *mdata;
355 ++opcount;
356 ++mdata;
357 if (!op) {
358 Dwarf_Unsigned opslen = 0;
359 /* End of ops, this is terminator, count the ending 0
360 as an operator so dwarfdump can print it.
361 Normally we don't see this, the end operator
362 signals end. */
363 opslen = mdata - macro_context->mc_macro_ops;
364 macro_context->mc_macro_ops_count = opcount;
365 macro_context->mc_ops_data_length = opslen;
366 macro_context->mc_total_length = opslen +
367 macro_context->mc_macro_header_length;
368 if (build_ops_array) {
369 curopsentry->mo_opcode = op;
370 curopsentry->mo_form = 0;
371 curopsentry->mo_data = 0;
372 }
373 return DW_DLV_OK;
374 }
375 if (is_std_moperator(op)) {
376 struct Dwarf_Macro_Forms_s * ourform =
377 dw5formsarray + op;
378 /* ASSERT: op == ourform->mf_code */
379 unsigned formcount = ourform->mf_formcount;
380 const Dwarf_Small *forms = ourform->mf_formbytes;
381 Dwarf_Unsigned forms_length = 0;
382
383 res = _dwarf_skim_forms(dbg,macro_context,mdata,
384 formcount,forms,
385 section_end,
386 &forms_length,error);
387 if ( res != DW_DLV_OK) {
388 return res;
389 }
390 if (build_ops_array) {
391 curopsentry->mo_opcode = op;
392 curopsentry->mo_form = ourform;
393 curopsentry->mo_data = mdata;
394 }
395 mdata += forms_length;
396 } else {
397 /* FIXME Add support for user defined ops. */
398 _dwarf_error(dbg, error, DW_DLE_MACRO_OP_UNHANDLED);
399 return DW_DLV_ERROR;
400 }
401 if (mdata > section_end) {
402 _dwarf_error(dbg, error, DW_DLE_MACRO_PAST_END);
403 return DW_DLV_ERROR;
404 }
405 if (build_ops_array) {
406 curopsentry++;
407 }
408 }
409 _dwarf_error(dbg, error, DW_DLE_MACRO_PAST_END);
410 return DW_DLV_ERROR;
411 }
412
413 int
414 dwarf_get_macro_op(Dwarf_Macro_Context macro_context,
415 Dwarf_Unsigned op_number,
416 Dwarf_Unsigned * op_start_section_offset,
417 Dwarf_Half * macro_operator,
418 Dwarf_Half * forms_count,
419 const Dwarf_Small ** formcode_array,
420 Dwarf_Error *error)
421 {
422 struct Dwarf_Macro_Operator_s *curop = 0;
423 Dwarf_Debug dbg = 0;
424 Dwarf_Unsigned op_offset = 0;
425 Dwarf_Half operator = 0;
426
427 CHECKNULLCONTEXT(macro_context,dbg,error);
428 dbg = macro_context->mc_dbg;
429 if (op_number >= macro_context->mc_macro_ops_count) {
430 _dwarf_error(dbg, error,DW_DLE_BAD_MACRO_INDEX);
431 return DW_DLV_ERROR;
432 }
433 curop = macro_context->mc_ops + op_number;
434 operator = curop->mo_opcode;
435 if (!operator) {
436 /* For the null byte at the end
437 of an operator list. */
438 *op_start_section_offset = macro_context->mc_total_length+
439 macro_context->mc_section_offset -1;
440 *macro_operator = operator;
441 *forms_count = 0;
442 *formcode_array = 0;
443 return DW_DLV_OK;
444 }
445 op_offset =
446 ((curop->mo_data -1) - macro_context->mc_macro_header) +
447 macro_context->mc_section_offset;
448 if (op_offset >= macro_context->mc_section_size) {
449 dwarfstring m;
450 char buf[50];
451
452 dwarfstring_constructor_static(&m,buf,sizeof(buf));
453 dwarfstring_append_printf_u(&m,
454 "DW_DLE_MACRO_OFFSET_BAD: offset 0x%lx",
455 op_offset);
456 dwarfstring_append_printf_u(&m,
457 " >= section size of 0x%lx",
458 macro_context->mc_section_size);
459 _dwarf_error_string(dbg,error,DW_DLE_MACRO_OFFSET_BAD,
460 dwarfstring_string(&m));
461 dwarfstring_destructor(&m);
462 return DW_DLV_ERROR;
463 }
464 *op_start_section_offset = op_offset;
465 *macro_operator = operator;
466 if (curop->mo_form) {
467 *forms_count = curop->mo_form->mf_formcount;
468 *formcode_array = curop->mo_form->mf_formbytes;
469 } else {
470 /* ASSERT: macro_operator == 0 */
471 *forms_count = 0;
472 *formcode_array = 0;
473 }
474 return DW_DLV_OK;
475 }
476
477 /* Here a DW_DLV_NO_ENTRY return means the macro operator
478 is not a def/undef operator. */
479 int
480 dwarf_get_macro_defundef(Dwarf_Macro_Context macro_context,
481 Dwarf_Unsigned op_number,
482 Dwarf_Unsigned * line_number,
483 Dwarf_Unsigned * index,
484 Dwarf_Unsigned * offset,
485 Dwarf_Half * forms_count,
486 const char ** macro_string,
487 Dwarf_Error *error)
488 {
489 Dwarf_Debug dbg = 0;
490 Dwarf_Small *mdata = 0;
491 int res = 0;
492 Dwarf_Small *startptr = 0;
493 Dwarf_Small *endptr = 0;
494 Dwarf_Half lformscount = 0;
495 struct Dwarf_Macro_Operator_s *curop = 0;
496 unsigned macop = 0;
497
498 CHECKNULLCONTEXT(macro_context,dbg,error);
499 dbg = macro_context->mc_dbg;
500 if (op_number >= macro_context->mc_macro_ops_count) {
501 _dwarf_error(dbg, error,DW_DLE_BAD_MACRO_INDEX);
502 return DW_DLV_ERROR;
503 }
504 curop = macro_context->mc_ops + op_number;
505 macop = curop->mo_opcode;
506 startptr = macro_context->mc_macro_header;
507 endptr = startptr + macro_context->mc_total_length;
508 mdata = curop->mo_data;
509 lformscount = curop->mo_form->mf_formcount;
510 if (lformscount != 2) {
511 /*_dwarf_error(dbg, error,DW_DLE_MACRO_OPCODE_FORM_BAD);*/
512 return DW_DLV_NO_ENTRY;
513 }
514 switch(macop){
515 case DW_MACRO_define:
516 case DW_MACRO_undef: {
517 Dwarf_Unsigned linenum = 0;
518 const char * content = 0;
519
520 DECODE_LEB128_UWORD_CK(mdata,linenum,
521 dbg, error,endptr);
522 content = (const char *)mdata;
523 res = _dwarf_check_string_valid(dbg,
524 startptr,mdata, endptr,
525 DW_DLE_MACRO_STRING_BAD,error);
526 if (res != DW_DLV_OK) {
527 return res;
528 }
529 *line_number = linenum;
530 *index = 0;
531 *offset = 0;
532 *forms_count = lformscount;
533 *macro_string = content;
534 }
535 return DW_DLV_OK;
536 case DW_MACRO_define_strp:
537 case DW_MACRO_undef_strp: {
538 Dwarf_Unsigned linenum = 0;
539 Dwarf_Unsigned stringoffset = 0;
540 Dwarf_Small form1 = curop->mo_form->mf_formbytes[1];
541 char * localstr = 0;
542
543 DECODE_LEB128_UWORD_CK(mdata,linenum,
544 dbg, error,endptr);
545 READ_UNALIGNED_CK(dbg,stringoffset,Dwarf_Unsigned,
546 mdata,macro_context->mc_offset_size,
547 error,endptr);
548 res = _dwarf_extract_local_debug_str_string_given_offset(dbg,
549 form1,
550 stringoffset,
551 &localstr,
552 error);
553 *index = 0;
554 *line_number = linenum;
555 *offset = stringoffset;
556 *forms_count = lformscount;
557 if (res == DW_DLV_ERROR) {
558 *macro_string = "<Error: getting local .debug_str>";
559 return res;
560 } else if (res == DW_DLV_NO_ENTRY) {
561 *macro_string = "<Error: NO_ENTRY on "
562 ".debug_string (strp)>";
563 } else {
564 *macro_string = (const char *)localstr;
565 }
566 }
567 return DW_DLV_OK;
568 case DW_MACRO_define_strx:
569 case DW_MACRO_undef_strx: {
570 Dwarf_Unsigned linenum = 0;
571 Dwarf_Unsigned stringindex = 0;
572 Dwarf_Unsigned offsettostr= 0;
573 int ress = 0;
574 Dwarf_Byte_Ptr mdata_copy = 0;
575 Dwarf_Small form1 = curop->mo_form->mf_formbytes[1];
576
577 DECODE_LEB128_UWORD_CK(mdata,linenum, dbg, error,endptr);
578 *line_number = linenum;
579 mdata_copy = mdata;
580 DECODE_LEB128_UWORD_CK(mdata_copy,stringindex,
581 dbg, error,endptr);
582 /* mdata_copy is for call below */
583
584 *index = stringindex;
585 *forms_count = lformscount;
586
587 /* Redoes the index-getting. Gets offset. */
588 ress = _dwarf_extract_string_offset_via_str_offsets(dbg,
589 mdata_copy,
590 endptr,
591 form1,
592 macro_context->mc_cu_context,
593 &offsettostr,
594 error);
595 if (ress == DW_DLV_ERROR) {
596 return ress;
597 }
598 if (ress == DW_DLV_OK) {
599 char *localstr = 0;
600
601 *index = stringindex;
602 *offset = offsettostr;
603 ress =
604 _dwarf_extract_local_debug_str_string_given_offset(
605 dbg,
606 form1,
607 offsettostr,
608 &localstr,
609 error);
610 if (ress == DW_DLV_ERROR) {
611 return ress;
612 } else if (ress == DW_DLV_NO_ENTRY){
613 *macro_string = "<:No string available>";
614 } else {
615 *macro_string = (const char *)localstr;
616 /* All is ok. */
617 }
618 } else {
619 *index = stringindex;
620 *offset = 0;
621 *macro_string = "<.debug_str_offsets not available>";
622 }
623 }
624 return DW_DLV_OK;
625 case DW_MACRO_define_sup:
626 case DW_MACRO_undef_sup: {
627 Dwarf_Unsigned linenum = 0;
628 Dwarf_Unsigned supoffset = 0;
629 char *localstring = 0;
630 int resup = 0;
631 Dwarf_Error lerr = 0;
632
633 DECODE_LEB128_UWORD_CK(mdata,linenum,
634 dbg, error,endptr);
635 READ_UNALIGNED_CK(dbg,supoffset,Dwarf_Unsigned,
636 mdata,macro_context->mc_offset_size,
637 error,endptr);
638 *line_number = linenum;
639 *index = 0;
640 *offset = supoffset;
641 *forms_count = lformscount;
642 resup = _dwarf_get_string_from_tied(dbg, supoffset,
643 &localstring, &lerr);
644 if (resup != DW_DLV_OK) {
645 if (resup == DW_DLV_ERROR) {
646 Dwarf_Unsigned myerrno =
647 (unsigned int)dwarf_errno(lerr);
648 if (myerrno == DW_DLE_NO_TIED_FILE_AVAILABLE) {
649 *macro_string =
650 (char *)"<DW_FORM_str_sup-no-tied_file>";
651 } else {
652 _dwarf_error(dbg,error,myerrno);
653 *macro_string =
654 (char *)"<Error: DW_FORM_str_sup-got-error>";
655 }
656 dwarf_dealloc_error(dbg,lerr);
657 } else {
658 *macro_string = "<DW_FORM_str_sup-no-entry>";
659 }
660 return resup;
661 }
662 *macro_string = (const char *)localstring;
663 /* If NO ENTRY available, return DW_DLV_NO_ENTRY.
664 We suspect this is better than DW_DLV_OK. */
665 return resup;
666 }
667 default:
668 _dwarf_error(dbg,error,DW_DLE_MACRO_OP_UNHANDLED);
669 return DW_DLV_ERROR;
670 }
671 return DW_DLV_NO_ENTRY;
672 }
673
674 /* ASSERT: we elsewhere guarantee room to copy into.
675 If trimtarg ==1, trim trailing slash in targ.
676 Caller should not pass in 'src'
677 with leading / */
678 static void
679 specialcat(char *targ,char *src,int trimtarg)
680 {
681 char *last = 0;
682
683 while( *targ) {
684 last = targ;
685 targ++;
686 }
687 /* TARG now points at terminating NUL */
688 /* LAST points at final character in targ. */
689 if (trimtarg ) {
690 if (last && *last == '/') {
691 /* Truncate. */
692 *last = 0;
693 targ = last;
694 /* TARG again points at terminating NUL */
695 }
696 }
697 while (*src) {
698 *targ = *src;
699 targ++;
700 src++;
701 }
702 *targ = 0;
703 }
704
705 /* If returns NULL caller must handle it. */
706 static const char *
707 construct_from_dir_and_name(const char *dir,
708 const char *name)
709 {
710 size_t truelen = 0;
711 char *final = 0;
712
713 /* Allow for NUL char and added / */
714 truelen = strlen(dir) + strlen(name) + 1 +1;
715 final = (char *)malloc(truelen);
716 if (!final) {
717 return NULL;
718 }
719 final[0] = 0;
720 specialcat(final,(char *)dir,1);
721 strcat(final,"/");
722 specialcat(final,(char *)name,0);
723 return final;
724 }
725
726 /* If returns NULL caller must handle it. */
727 static const char *
728 construct_at_path_from_parts(Dwarf_Macro_Context mc)
729 {
730 if (mc->mc_file_path) {
731 return mc->mc_file_path;
732 }
733 if (!mc->mc_at_comp_dir || !mc->mc_at_comp_dir[0]) {
734 return mc->mc_at_name;
735 }
736 if (!mc->mc_at_name || !mc->mc_at_name[0]) {
737 return NULL;
738 }
739 if (_dwarf_file_name_is_full_path(
740 (Dwarf_Small *)mc->mc_at_name)) {
741 return mc->mc_at_name;
742 }
743 /* Dwarf_Macro_Context destructor will free this. */
744 mc->mc_file_path = construct_from_dir_and_name(
745 mc->mc_at_comp_dir,mc->mc_at_name);
746 return mc->mc_file_path;
747 }
748
749 int
750 dwarf_get_macro_startend_file(Dwarf_Macro_Context macro_context,
751 Dwarf_Unsigned op_number,
752 Dwarf_Unsigned * line_number,
753 Dwarf_Unsigned * name_index_to_line_tab,
754 const char ** src_file_name,
755 Dwarf_Error *error)
756 {
757 Dwarf_Debug dbg = 0;
758 Dwarf_Small *mdata = 0;
759 unsigned macop = 0;
760 struct Dwarf_Macro_Operator_s *curop = 0;
761 Dwarf_Byte_Ptr startptr = 0;
762 Dwarf_Byte_Ptr endptr = 0;
763
764 CHECKNULLCONTEXT(macro_context,dbg,error);
765 dbg = macro_context->mc_dbg;
766 if (op_number >= macro_context->mc_macro_ops_count) {
767 _dwarf_error(dbg, error,DW_DLE_BAD_MACRO_INDEX);
768 return DW_DLV_ERROR;
769 }
770 startptr = macro_context->mc_macro_header;
771 endptr = startptr + macro_context->mc_total_length;
772
773 curop = macro_context->mc_ops + op_number;
774 macop = curop->mo_opcode;
775 mdata = curop->mo_data;
776 if (macop != DW_MACRO_start_file && macop != DW_MACRO_end_file) {
777 return DW_DLV_NO_ENTRY;
778 }
779 if (macop == DW_MACRO_start_file) {
780 Dwarf_Unsigned linenum = 0;
781 Dwarf_Unsigned srcindex = 0;
782 Dwarf_Signed trueindex = 0;
783
784 DECODE_LEB128_UWORD_CK(mdata,linenum, dbg, error,endptr);
785 DECODE_LEB128_UWORD_CK(mdata,srcindex, dbg, error,endptr);
786 *line_number = linenum;
787 *name_index_to_line_tab = srcindex;
788 /* We deal with DWARF4 GNU extension
789 with .debug_macro version number 4
790 and DWARF5 .debug_macro version number 5.
791 */
792 if (macro_context->mc_version_number == DW_MACRO_VERSION5) {
793 trueindex = srcindex;
794 if (trueindex < 0) {
795 *src_file_name =
796 "<source-file-index-low-no-name-available>";
797 return DW_DLV_OK;
798 }
799 if (trueindex < macro_context->mc_srcfiles_count) {
800 *src_file_name =
801 macro_context->mc_srcfiles[trueindex];
802 return DW_DLV_OK;
803 } else {
804 *src_file_name =
805 "<src-index-high-no-source-file-name-available>";
806 return DW_DLV_OK;
807 }
808 } else {
809 /* All except DWARF5 */
810 /* Unsigned to signed here. */
811 trueindex = srcindex;
812 /* Protects against crazy big srcindex,
813 overflow territory. */
814 if (trueindex < 0 ) {
815 /* Something insane here. */
816 *src_file_name =
817 "<source-file-index-low-no-name-available>";
818 return DW_DLV_OK;
819 }
820 /* Protects against crazy big srcindex,
821 overflow territory. */
822 if (trueindex > (macro_context->mc_srcfiles_count+1)) {
823 /* Something insane here. */
824 *src_file_name =
825 "<source-file-index-high-no-name-available>";
826 return DW_DLV_OK;
827 }
828 --trueindex; /* might now be -1 */
829 if (trueindex > macro_context->mc_srcfiles_count) {
830 *src_file_name =
831 "<adjusted-source-file-index-high-"
832 "no-name-available>";
833 }
834 if (srcindex > 0 &&
835 trueindex < macro_context->mc_srcfiles_count) {
836 *src_file_name =
837 macro_context->mc_srcfiles[trueindex];
838 } else {
839 const char *mcatcomp =
840 construct_at_path_from_parts(macro_context);
841 if (mcatcomp) {
842 *src_file_name = mcatcomp;
843 } else {
844 *src_file_name =
845 "<no-source-file-name-available>";
846 }
847 }
848 }
849 } else {
850 /* DW_MACRO_end_file. No operands. */
851 }
852 return DW_DLV_OK;
853 }
854
855 /* Target_offset is the offset in a .debug_macro section,
856 of a macro unit header.
857 Returns DW_DLV_NO_ENTRY if the macro operator is not
858 one of the import operators. */
859 int
860 dwarf_get_macro_import(Dwarf_Macro_Context macro_context,
861 Dwarf_Unsigned op_number,
862 Dwarf_Unsigned * target_offset,
863 Dwarf_Error *error)
864 {
865 Dwarf_Unsigned supoffset = 0;
866 Dwarf_Debug dbg = 0;
867 unsigned macop = 0;
868 struct Dwarf_Macro_Operator_s *curop = 0;
869 Dwarf_Small *mdata = 0;
870 Dwarf_Byte_Ptr startptr = 0;
871 Dwarf_Byte_Ptr endptr = 0;
872
873 CHECKNULLCONTEXT(macro_context,dbg,error);
874 startptr = macro_context->mc_macro_header;
875 endptr = startptr + macro_context->mc_total_length;
876 dbg = macro_context->mc_dbg;
877 if (op_number >= macro_context->mc_macro_ops_count) {
878 _dwarf_error(dbg, error,DW_DLE_BAD_MACRO_INDEX);
879 return DW_DLV_ERROR;
880 }
881 curop = macro_context->mc_ops + op_number;
882 macop = curop->mo_opcode;
883 mdata = curop->mo_data;
884 if (macop != DW_MACRO_import && macop != DW_MACRO_import_sup) {
885 return DW_DLV_NO_ENTRY;
886 }
887 READ_UNALIGNED_CK(dbg,supoffset,Dwarf_Unsigned,
888 mdata,macro_context->mc_offset_size,
889 error,endptr);
890 *target_offset = supoffset;
891 return DW_DLV_OK;
892 }
893
894 /* */
895 static int
896 valid_macro_form(Dwarf_Half form)
897 {
898 switch(form) {
899 case DW_FORM_block:
900 case DW_FORM_block1:
901 case DW_FORM_block2:
902 case DW_FORM_block4:
903 case DW_FORM_data1:
904 case DW_FORM_data2:
905 case DW_FORM_data4:
906 case DW_FORM_data8:
907 case DW_FORM_data16:
908 case DW_FORM_sdata:
909 case DW_FORM_udata:
910 case DW_FORM_flag:
911 case DW_FORM_sec_offset:
912 case DW_FORM_string:
913 case DW_FORM_strp:
914 case DW_FORM_strx:
915 return TRUE;
916 default: break;
917 }
918 return FALSE;
919 }
920
921 static int
922 validate_opcode(Dwarf_Debug dbg,
923 struct Dwarf_Macro_Forms_s *curform,
924 Dwarf_Error * error)
925 {
926 unsigned i = 0;
927 struct Dwarf_Macro_Forms_s *stdfptr = 0;
928 if (curform->mf_code >= DW_MACRO_lo_user) {
929 /* Nothing to check. user level. */
930 return DW_DLV_OK;
931 }
932 if (curform->mf_code > DW_MACRO_undef_strx) {
933 _dwarf_error(dbg, error, DW_DLE_MACRO_OPCODE_BAD);
934 return DW_DLV_ERROR;
935 }
936 if (!curform->mf_code){
937 _dwarf_error(dbg, error, DW_DLE_MACRO_OPCODE_BAD);
938 return DW_DLV_ERROR;
939 }
940 stdfptr = &dwarf_default_macro_opslist.mol_data[curform->mf_code];
941
942 if (curform->mf_formcount != stdfptr->mf_formcount) {
943 _dwarf_error(dbg, error, DW_DLE_MACRO_OPCODE_FORM_BAD);
944 return DW_DLV_ERROR;
945 }
946 for (i = 0; i < curform->mf_formcount; ++i) {
947 if (curform->mf_formbytes[i] != stdfptr->mf_formbytes[1]) {
948 _dwarf_error(dbg, error, DW_DLE_MACRO_OPCODE_FORM_BAD);
949 return DW_DLV_ERROR;
950 }
951 }
952 return DW_DLV_OK;
953 }
954
955 static int
956 read_operands_table(Dwarf_Macro_Context macro_context,
957 Dwarf_Small * macro_header,
958 Dwarf_Small * macro_data,
959 Dwarf_Small * section_base,
960 Dwarf_Unsigned section_size,
961 Dwarf_Unsigned *table_size_out,
962 Dwarf_Error *error)
963 {
964 Dwarf_Small* table_data_start = macro_data;
965 Dwarf_Unsigned local_size = 0;
966 Dwarf_Unsigned cur_offset = 0;
967 Dwarf_Small operand_table_count = 0;
968 unsigned i = 0;
969 struct Dwarf_Macro_Forms_s *curformentry = 0;
970 Dwarf_Debug dbg = 0;
971 Dwarf_Byte_Ptr startptr = 0;
972 Dwarf_Byte_Ptr endptr = 0;
973
974 CHECKNULLCONTEXT(macro_context,dbg,error);
975 dbg = macro_context->mc_dbg;
976 cur_offset = (1+ macro_data) - macro_header;
977 if (cur_offset >= section_size) {
978 _dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
979 return DW_DLV_ERROR;
980 }
981
982 startptr = macro_context->mc_macro_header;
983 endptr = startptr + macro_context->mc_total_length;
984 READ_UNALIGNED_CK(dbg,operand_table_count,Dwarf_Small,
985 macro_data,sizeof(Dwarf_Small),error,endptr);
986 macro_data += sizeof(Dwarf_Small);
987 /* Estimating minimum size */
988 local_size = operand_table_count * 4;
989
990 cur_offset = (local_size+ macro_data) - section_base;
991 if (cur_offset >= section_size) {
992 _dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
993 return DW_DLV_ERROR;
994 }
995 /* first, get size of table. */
996 table_data_start = macro_data;
997 for (i = 0; i < operand_table_count; ++i) {
998 /* Compiler warning about unused opcode_number
999 variable should be ignored. */
1000 Dwarf_Unsigned formcount = 0;
1001 #if 0 /* No need to actually read, just update pointer*/
1002 Dwarf_Small opcode_number = 0;
1003 READ_UNALIGNED_CK(dbg,opcode_number,Dwarf_Small,
1004 macro_data,sizeof(Dwarf_Small),error,endptr);
1005 #endif /*0*/
1006 macro_data += sizeof(Dwarf_Small);
1007
1008 DECODE_LEB128_UWORD_CK(macro_data,formcount,
1009 dbg, error, endptr);
1010 cur_offset = (formcount+ macro_data) - section_base;
1011 if (cur_offset >= section_size) {
1012 _dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
1013 return DW_DLV_ERROR;
1014 }
1015 /* The 1 ubyte forms follow. Step past them. */
1016 macro_data += formcount;
1017 }
1018 /* reset for reread. */
1019 macro_data = table_data_start;
1020 /* allocate table */
1021 macro_context->mc_opcode_forms = (struct Dwarf_Macro_Forms_s *)
1022 calloc(operand_table_count,
1023 sizeof(struct Dwarf_Macro_Forms_s));
1024 macro_context->mc_opcode_count = operand_table_count;
1025 if (!macro_context->mc_opcode_forms) {
1026 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
1027 return DW_DLV_ERROR;
1028 }
1029
1030 curformentry = macro_context->mc_opcode_forms;
1031 for (i = 0; i < operand_table_count; ++i,++curformentry) {
1032 Dwarf_Small opcode_number = 0;
1033 Dwarf_Unsigned formcount = 0;
1034 int res = 0;
1035
1036 cur_offset = (2 + macro_data) - section_base;
1037 if (cur_offset >= section_size) {
1038 _dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
1039 return DW_DLV_ERROR;
1040 }
1041 READ_UNALIGNED_CK(dbg,opcode_number,Dwarf_Small,
1042 macro_data,sizeof(Dwarf_Small),
1043 error,endptr);
1044 macro_data += sizeof(Dwarf_Small);
1045 DECODE_LEB128_UWORD_CK(macro_data,formcount,
1046 dbg, error, endptr);
1047
1048 curformentry->mf_code = opcode_number;
1049 curformentry->mf_formcount = (Dwarf_Small)formcount;
1050
1051 cur_offset = (formcount+ macro_data) - section_base;
1052 if (cur_offset >= section_size) {
1053 _dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
1054 return DW_DLV_ERROR;
1055 }
1056 curformentry->mf_formbytes = macro_data;
1057 macro_data += formcount;
1058 if (opcode_number > DW_MACRO_undef_strx ) {
1059 Dwarf_Half k = 0;
1060 for (k = 0; k < formcount; ++k) {
1061 if (!valid_macro_form(
1062 curformentry->mf_formbytes[k])) {
1063 _dwarf_error(dbg, error,
1064 DW_DLE_MACRO_OP_UNHANDLED);
1065 return DW_DLV_ERROR;
1066 }
1067 }
1068 }
1069 res = validate_opcode(macro_context->mc_dbg,
1070 curformentry, error);
1071 if (res != DW_DLV_OK) {
1072 return res;
1073 }
1074 }
1075 *table_size_out = macro_data - table_data_start;
1076 return DW_DLV_OK;
1077 }
1078
1079 /* This is not the normal srcfiles from dwarf_srcfiles.
1080 See translate translate_srcfiles_to_srcfiles2().
1081 It is a list, but the contents were directly malloc,
1082 not _dwarf_get_alloc.
1083 */
1084 static void
1085 dealloc_macro_srcfiles(char ** srcfiles,
1086 Dwarf_Signed srcfiles_count)
1087 {
1088 Dwarf_Signed i = 0;
1089 if (!srcfiles || !srcfiles_count) {
1090 return;
1091 }
1092 for (i = 0; i < srcfiles_count; ++i) {
1093 if (srcfiles[i]) {
1094 free(srcfiles[i]);
1095 srcfiles[i] = 0;
1096 }
1097 }
1098 free(srcfiles);
1099 }
1100
1101 /* This makes the macro context safe from
1102 duplicate frees in case of error. */
1103 static int
1104 translate_srcfiles_to_srcfiles2(char **srcfiles,
1105 Dwarf_Signed srcfiles_count,
1106 char **srcfiles2)
1107 {
1108 Dwarf_Signed i = 0;
1109
1110 for (i = 0; i < srcfiles_count; ++i) {
1111 char * ostr = 0;
1112 char * newstr = 0;
1113 size_t slen = 0;
1114
1115 ostr = srcfiles[i];
1116 slen = strlen(ostr);
1117 newstr = calloc(1,slen+1);
1118 if (!newstr) {
1119 return DW_DLV_ERROR;
1120 }
1121 _dwarf_safe_strcpy(newstr,slen+1,ostr,slen);
1122 srcfiles2[i] = newstr;
1123 }
1124 return DW_DLV_OK;
1125 }
1126
1127 static void
1128 drop_srcfiles(Dwarf_Debug dbg,char ** srcfiles,
1129 Dwarf_Signed srcfiles_count)
1130 {
1131 Dwarf_Signed i = 0;
1132 for (i = 0; i < srcfiles_count; ++i) {
1133 if (srcfiles[i]) {
1134 dwarf_dealloc(dbg, srcfiles[i], DW_DLA_STRING);
1135 }
1136 }
1137 dwarf_dealloc(dbg, srcfiles, DW_DLA_LIST);
1138 }
1139
1140 static int
1141 _dwarf_internal_macro_context(Dwarf_Die die,
1142 Dwarf_Bool offset_specified,
1143 Dwarf_Unsigned offset_in,
1144 Dwarf_Unsigned * version_out,
1145 Dwarf_Macro_Context * macro_context_out,
1146 Dwarf_Unsigned * macro_unit_offset_out,
1147 Dwarf_Unsigned * macro_ops_count_out,
1148 Dwarf_Unsigned * macro_ops_data_length,
1149 Dwarf_Error * error)
1150 {
1151 Dwarf_CU_Context cu_context = 0;
1152
1153 /* The Dwarf_Debug this die belongs to. */
1154 Dwarf_Debug dbg = 0;
1155 int resattr = DW_DLV_ERROR;
1156 int lres = DW_DLV_ERROR;
1157 int res = DW_DLV_ERROR;
1158 Dwarf_Unsigned macro_offset = 0;
1159 Dwarf_Attribute macro_attr = 0;
1160 Dwarf_Signed srcfiles_count = 0;
1161 Dwarf_Signed srcfiles2_count = 0;
1162 char ** srcfiles = 0;
1163
1164 /* srcfiles uses dwarf_get_alloc for strings
1165 so dealloc_macro_srcfiles() here will result in double-dealloc
1166 when dwarf_finish() happens to see the string deallocs
1167 before the macro context dealloc (the context dealloc
1168 will call dealloc_macro_srcfiles() !).
1169
1170 Also see the comment at _dwarf_macro_destructor() here.
1171 */
1172 char ** srcfiles2 = 0;
1173
1174 const char *comp_dir = 0;
1175 const char *comp_name = 0;
1176
1177 /* ***** BEGIN CODE ***** */
1178 if (error != NULL) {
1179 *error = NULL;
1180 }
1181
1182 CHECK_DIE(die, DW_DLV_ERROR);
1183 cu_context = die->di_cu_context;
1184 dbg = cu_context->cc_dbg;
1185
1186 /* Doing the load here results in duplication of the
1187 section-load call (in the by_offset
1188 interface below) but detects the missing section
1189 quickly. */
1190 res = _dwarf_load_section(dbg, &dbg->de_debug_macro,error);
1191 if (res != DW_DLV_OK) {
1192 return res;
1193 }
1194 if (!dbg->de_debug_macro.dss_size) {
1195 return DW_DLV_NO_ENTRY;
1196 }
1197 resattr = dwarf_attr(die, DW_AT_macros, &macro_attr, error);
1198 if (resattr == DW_DLV_NO_ENTRY) {
1199 resattr = dwarf_attr(die, DW_AT_GNU_macros,
1200 &macro_attr, error);
1201 }
1202 if (resattr != DW_DLV_OK) {
1203 return resattr;
1204 }
1205 if (!offset_specified) {
1206 lres = dwarf_global_formref(macro_attr,
1207 &macro_offset, error);
1208 if (lres != DW_DLV_OK) {
1209 dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
1210 return lres;
1211 }
1212 } else {
1213 macro_offset = offset_in;
1214 }
1215 /* If DWP cc_macro_base may be non-zero */
1216 macro_offset += cu_context->cc_macro_base;
1217
1218 lres = dwarf_srcfiles(die,&srcfiles,&srcfiles_count, error);
1219 if (lres == DW_DLV_ERROR) {
1220 dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
1221 return lres;
1222 }
1223 lres = _dwarf_internal_get_die_comp_dir(die, &comp_dir,
1224 &comp_name,error);
1225 if (lres == DW_DLV_ERROR) {
1226 drop_srcfiles(dbg,srcfiles,srcfiles_count);
1227 srcfiles = 0;
1228 srcfiles_count = 0;
1229 dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
1230 srcfiles = 0;
1231 return lres;
1232 }
1233 *macro_unit_offset_out = macro_offset;
1234 /* We cannot use space allocated by
1235 _dwarf_get_alloc() in the macro_context
1236 we will allocate shortly.
1237 So copy from what we have to a similar data set
1238 but malloc space directly. */
1239
1240 if (srcfiles_count > 0) {
1241 srcfiles2 = (char **) calloc(srcfiles_count, sizeof(char *));
1242 if (!srcfiles2) {
1243 dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
1244 drop_srcfiles(dbg,srcfiles,srcfiles_count);
1245 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
1246 return DW_DLV_ERROR;
1247 }
1248 lres = translate_srcfiles_to_srcfiles2(srcfiles,
1249 srcfiles_count,srcfiles2);
1250 drop_srcfiles(dbg,srcfiles,srcfiles_count);
1251 srcfiles2_count = srcfiles_count;
1252 srcfiles = 0;
1253 srcfiles_count = 0;
1254 if (lres != DW_DLV_OK) {
1255 dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
1256 dealloc_macro_srcfiles(srcfiles2, srcfiles2_count);
1257 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
1258 return lres;
1259 }
1260 } else {
1261 drop_srcfiles(dbg,srcfiles,srcfiles_count);
1262 srcfiles = 0;
1263 srcfiles_count = 0;
1264 }
1265
1266 dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
1267 /* NO ENTRY or OK we accept, though NO ENTRY means there
1268 are no source files available. */
1269 lres = _dwarf_internal_macro_context_by_offset(dbg,
1270 macro_offset,version_out,macro_context_out,
1271 macro_ops_count_out,
1272 macro_ops_data_length,
1273 srcfiles2,srcfiles2_count,
1274 comp_dir,
1275 comp_name,
1276 cu_context,
1277 error);
1278 /* In case of ERROR or NO_ENTRY srcfiles2 is already freed. */
1279 return lres;
1280 }
1281
1282 static int
1283 _dwarf_internal_macro_context_by_offset(Dwarf_Debug dbg,
1284 Dwarf_Unsigned offset,
1285 Dwarf_Unsigned * version_out,
1286 Dwarf_Macro_Context * macro_context_out,
1287 Dwarf_Unsigned * macro_ops_count_out,
1288 Dwarf_Unsigned * macro_ops_data_length,
1289 char **srcfiles,
1290 Dwarf_Signed srcfilescount,
1291 const char *comp_dir,
1292 const char *comp_name,
1293 Dwarf_CU_Context cu_context,
1294 Dwarf_Error * error)
1295 {
1296 Dwarf_Unsigned line_table_offset = 0;
1297 Dwarf_Small * macro_header = 0;
1298 Dwarf_Small * macro_data = 0;
1299 Dwarf_Unsigned version = 0;
1300 Dwarf_Unsigned flags = 0;
1301 Dwarf_Small offset_size = 4;
1302 Dwarf_Unsigned cur_offset = 0;
1303 Dwarf_Unsigned section_size = 0;
1304 Dwarf_Small *section_base = 0;
1305 Dwarf_Small *section_end = 0;
1306 Dwarf_Unsigned optablesize = 0;
1307 Dwarf_Unsigned macro_offset = offset;
1308 int res = 0;
1309 Dwarf_Macro_Context macro_context = 0;
1310 Dwarf_Bool build_ops_array = FALSE;
1311
1312 res = _dwarf_load_section(dbg, &dbg->de_debug_macro,error);
1313 if (res != DW_DLV_OK) {
1314 dealloc_macro_srcfiles(srcfiles,srcfilescount);
1315 return res;
1316 }
1317 if (!dbg->de_debug_macro.dss_size) {
1318 dealloc_macro_srcfiles(srcfiles,srcfilescount);
1319 return DW_DLV_NO_ENTRY;
1320 }
1321
1322 section_base = dbg->de_debug_macro.dss_data;
1323 section_size = dbg->de_debug_macro.dss_size;
1324 /* The '3' ensures the header initial bytes present too. */
1325 if ((3+macro_offset) >= section_size) {
1326 dealloc_macro_srcfiles(srcfiles,srcfilescount);
1327 _dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
1328 return DW_DLV_ERROR;
1329 }
1330 macro_header = macro_offset + section_base;
1331 macro_data = macro_header;
1332 section_end = section_base +section_size;
1333
1334 macro_context = (Dwarf_Macro_Context)
1335 _dwarf_get_alloc(dbg,DW_DLA_MACRO_CONTEXT,1);
1336 if (!macro_context) {
1337 dealloc_macro_srcfiles(srcfiles,srcfilescount);
1338 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
1339 return DW_DLV_ERROR;
1340 }
1341
1342 if ((section_base + DWARF_HALF_SIZE + sizeof(Dwarf_Small)) >
1343 section_end ) {
1344 dealloc_macro_srcfiles(srcfiles,srcfilescount);
1345 dwarf_dealloc_macro_context(macro_context);
1346 _dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
1347 return DW_DLV_ERROR;
1348 }
1349 /* Note here so if error return we get these freed eventually. */
1350 macro_context->mc_srcfiles = srcfiles;
1351 macro_context->mc_srcfiles_count = srcfilescount;
1352 macro_context->mc_cu_context = cu_context;
1353
1354 res = _dwarf_read_unaligned_ck_wrapper(dbg,
1355 &version,macro_data,DWARF_HALF_SIZE,section_end,
1356 error);
1357 if (res != DW_DLV_OK) {
1358 dwarf_dealloc_macro_context(macro_context);
1359 return res;
1360 }
1361 if (version != DW_MACRO_VERSION4 &&
1362 version != DW_MACRO_VERSION5) {
1363 dwarfstring ms;
1364
1365 dwarfstring_constructor(&ms);
1366 dwarfstring_append_printf_u(&ms,
1367 "DW_DLE_MACRO_VERSION_ERROR: "
1368 "version 0x%x ",version);
1369 dwarfstring_append_printf_u(&ms,
1370 "at section offset "
1371 "0x%" DW_PR_XZEROS DW_PR_DUx " "
1372 "is incorrect, only 5 "
1373 "or the GNU extension value of 4 are valid. "
1374 "Corrupt dwarf.",
1375 macro_offset);
1376 _dwarf_error_string(dbg,error,
1377 DW_DLE_MACRO_VERSION_ERROR,
1378 dwarfstring_string(&ms));
1379 dwarfstring_destructor(&ms);
1380 dwarf_dealloc_macro_context(macro_context);
1381 return DW_DLV_ERROR;
1382 }
1383 macro_data += DWARF_HALF_SIZE;
1384 res = _dwarf_read_unaligned_ck_wrapper(dbg,
1385 &flags,macro_data,sizeof(Dwarf_Small),section_end,
1386 error);
1387 if (res != DW_DLV_OK) {
1388 dwarf_dealloc_macro_context(macro_context);
1389 return res;
1390 }
1391 macro_data += sizeof(Dwarf_Small);
1392
1393 macro_context->mc_at_comp_dir = comp_dir;
1394 macro_context->mc_at_name = comp_name;
1395 macro_context->mc_macro_header = macro_header;
1396 macro_context->mc_section_offset = macro_offset;
1397 macro_context->mc_section_size = section_size;
1398 macro_context->mc_version_number = (Dwarf_Half)version;
1399 macro_context->mc_flags = (Dwarf_Small)flags;
1400 macro_context->mc_dbg = dbg;
1401 macro_context->mc_offset_size_flag =
1402 flags& MACRO_OFFSET_SIZE_FLAG?TRUE:FALSE;
1403 macro_context->mc_debug_line_offset_flag =
1404 flags& MACRO_LINE_OFFSET_FLAG?TRUE:FALSE;
1405 macro_context->mc_operands_table_flag =
1406 flags& MACRO_OP_TABLE_FLAG?TRUE:FALSE;
1407 offset_size = macro_context->mc_offset_size_flag?8:4;
1408 macro_context->mc_offset_size = offset_size;
1409 if (macro_context->mc_debug_line_offset_flag) {
1410 cur_offset = (offset_size+ macro_data) - section_base;
1411 if (cur_offset >= section_size) {
1412 dwarf_dealloc_macro_context(macro_context);
1413 _dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
1414 return DW_DLV_ERROR;
1415 }
1416 res = _dwarf_read_unaligned_ck_wrapper(dbg,
1417 &line_table_offset,macro_data,
1418 offset_size,section_end,
1419 error);
1420 if (res != DW_DLV_OK) {
1421 dwarf_dealloc_macro_context(macro_context);
1422 return res;
1423 }
1424 macro_data += offset_size;
1425 macro_context->mc_debug_line_offset = line_table_offset;
1426 }
1427 if (macro_context->mc_operands_table_flag) {
1428 res = read_operands_table(macro_context,
1429 macro_header,
1430 macro_data,
1431 section_base,
1432 section_size,
1433 &optablesize,
1434 error);
1435 if (res != DW_DLV_OK) {
1436 dwarf_dealloc_macro_context(macro_context);
1437 return res;
1438 }
1439 }
1440
1441 macro_data += optablesize;
1442 macro_context->mc_macro_ops = macro_data;
1443 macro_context->mc_macro_header_length =
1444 (Dwarf_Half)(macro_data - macro_header);
1445 build_ops_array = FALSE;
1446 res = _dwarf_get_macro_ops_count_internal(macro_context,
1447 build_ops_array,
1448 error);
1449 if (res != DW_DLV_OK) {
1450 dwarf_dealloc_macro_context(macro_context);
1451 return res;
1452 }
1453 build_ops_array = TRUE;
1454 res = _dwarf_get_macro_ops_count_internal(macro_context,
1455 build_ops_array,
1456 error);
1457 if (res != DW_DLV_OK) {
1458 dwarf_dealloc_macro_context(macro_context);
1459 return res;
1460 }
1461 *macro_ops_count_out = macro_context->mc_macro_ops_count;
1462 *macro_ops_data_length = macro_context->mc_ops_data_length;
1463 *version_out = version;
1464 *macro_context_out = macro_context;
1465 return DW_DLV_OK;
1466 }
1467
1468 int
1469 dwarf_macro_context_total_length(Dwarf_Macro_Context head,
1470 Dwarf_Unsigned * mac_total_len,
1471 Dwarf_Error *error)
1472 {
1473 Dwarf_Debug dbg = 0;
1474
1475 if (head) {
1476 dbg = head->mc_dbg;
1477 }
1478 CHECKNULLCONTEXT(head,dbg,error);
1479 *mac_total_len = head->mc_total_length;
1480 return DW_DLV_OK;
1481 }
1482
1483 int
1484 dwarf_macro_context_head(Dwarf_Macro_Context head,
1485 Dwarf_Half * version,
1486 Dwarf_Unsigned * mac_offset,
1487 Dwarf_Unsigned * mac_len,
1488 Dwarf_Unsigned * mac_header_len,
1489 unsigned * flags,
1490 Dwarf_Bool * has_line_offset,
1491 Dwarf_Unsigned * line_offset,
1492 Dwarf_Bool * has_offset_size_64,
1493 Dwarf_Bool * has_operands_table,
1494 Dwarf_Half * opcode_count,
1495 Dwarf_Error *error)
1496 {
1497 Dwarf_Debug dbg = 0;
1498
1499 CHECKNULLCONTEXT(head,dbg,error);
1500 *version = head->mc_version_number;
1501 *mac_offset = head->mc_section_offset;
1502 *mac_len = head->mc_total_length;
1503 *mac_header_len = head->mc_macro_header_length;
1504 *flags = head->mc_flags;
1505 *line_offset = head->mc_debug_line_offset;
1506 *has_line_offset = head->mc_debug_line_offset_flag;
1507 *has_offset_size_64 = head->mc_offset_size_flag;
1508 *has_operands_table = head->mc_operands_table_flag;
1509 *opcode_count = head->mc_opcode_count;
1510 return DW_DLV_OK;
1511 }
1512 int
1513 dwarf_macro_operands_table(Dwarf_Macro_Context head,
1514 Dwarf_Half index, /* 0 to opcode_count -1 */
1515 Dwarf_Half *opcode_number,
1516 Dwarf_Half *operand_count,
1517 const Dwarf_Small **operand_array,
1518 Dwarf_Error *error)
1519 {
1520 struct Dwarf_Macro_Forms_s * ops = 0;
1521 Dwarf_Debug dbg = 0;
1522
1523 CHECKNULLCONTEXT(head,dbg,error);
1524 dbg = head->mc_dbg;
1525 if (index >= head->mc_opcode_count) {
1526 _dwarf_error(dbg, error, DW_DLE_BAD_MACRO_INDEX);
1527 return DW_DLV_ERROR;
1528 }
1529 ops = head->mc_opcode_forms + index;
1530 *opcode_number = ops->mf_code;
1531 *operand_count = ops->mf_formcount;
1532 *operand_array = ops->mf_formbytes;
1533 return DW_DLV_OK;
1534 }
1535
1536 /* The base interface to the .debug_macro section data
1537 for a specific CU.
1538
1539 The version number passed back by *version_out
1540 may be 4 (a gnu extension of DWARF) or 5. */
1541 int
1542 dwarf_get_macro_context(Dwarf_Die cu_die,
1543 Dwarf_Unsigned * version_out,
1544 Dwarf_Macro_Context * macro_context,
1545 Dwarf_Unsigned * macro_unit_offset_out,
1546 Dwarf_Unsigned * macro_ops_count_out,
1547 Dwarf_Unsigned * macro_ops_data_length,
1548 Dwarf_Error * error)
1549 {
1550 int res = 0;
1551 Dwarf_Bool offset_specified = FALSE;
1552 Dwarf_Unsigned offset = 0;
1553
1554 res = _dwarf_internal_macro_context(cu_die,
1555 offset_specified,
1556 offset,
1557 version_out,
1558 macro_context,
1559 macro_unit_offset_out,
1560 macro_ops_count_out,
1561 macro_ops_data_length,
1562 error);
1563 return res;
1564 }
1565
1566 /* Like dwarf_get_macro_context but
1567 here we use a specified offset instead of
1568 the offset in the cu_die. */
1569 int
1570 dwarf_get_macro_context_by_offset(Dwarf_Die cu_die,
1571 Dwarf_Unsigned offset,
1572 Dwarf_Unsigned * version_out,
1573 Dwarf_Macro_Context * macro_context,
1574 Dwarf_Unsigned * macro_ops_count_out,
1575 Dwarf_Unsigned * macro_ops_data_length,
1576 Dwarf_Error * error)
1577 {
1578 int res = 0;
1579 Dwarf_Bool offset_specified = TRUE;
1580 Dwarf_Unsigned macro_unit_offset_out = 0;
1581
1582 res = _dwarf_internal_macro_context(cu_die,
1583 offset_specified,
1584 offset,
1585 version_out,
1586 macro_context,
1587 &macro_unit_offset_out,
1588 macro_ops_count_out,
1589 macro_ops_data_length,
1590 error);
1591 return res;
1592 }
1593
1594 int dwarf_get_macro_section_name(Dwarf_Debug dbg,
1595 const char **sec_name_out,
1596 Dwarf_Error *error)
1597 {
1598 struct Dwarf_Section_s *sec = 0;
1599
1600 CHECK_DBG(dbg,error,"dwarf_get_macro_section_name()");
1601 sec = &dbg->de_debug_macro;
1602 if (sec->dss_size == 0) {
1603 /* We don't have such a section at all. */
1604 return DW_DLV_NO_ENTRY;
1605 }
1606 *sec_name_out = sec->dss_name;
1607 return DW_DLV_OK;
1608 }
1609
1610 void
1611 dwarf_dealloc_macro_context(Dwarf_Macro_Context mc)
1612 {
1613 Dwarf_Debug dbg = 0;
1614
1615 if (!mc) {
1616 return;
1617 }
1618 dbg = mc->mc_dbg;
1619 /* See _dwarf_macro_destructor() here */
1620 dwarf_dealloc(dbg,mc,DW_DLA_MACRO_CONTEXT);
1621 }
1622
1623 int
1624 _dwarf_macro_constructor(Dwarf_Debug dbg, void *m)
1625 {
1626 /* Nothing to do, the space is zeroed out */
1627 Dwarf_Macro_Context mc= (Dwarf_Macro_Context)m;
1628 /* Arbitrary sentinel. For debugging. */
1629 mc->mc_sentinel = MC_SENTINEL;
1630 mc->mc_dbg = dbg;
1631 return DW_DLV_OK;
1632 }
1633
1634 /* Here we free various fields of Dwarf_Macro_Context.
1635 The fields do not get dealloc'd.
1636 If we had a separate destructor for hand-calling
1637 (meaning when an error is detected during creation
1638 of a Dwarf_Macro_Context)
1639 and one for calling by dwarf_dealloc() then
1640 we could have the hand-calling dwarf_dealloc the fields
1641 and the one called on the dealloc of a Dwarf_Macro_Context
1642 could leave the _dwarf_get_alloc() fields for for
1643 normal dwarf_finish() cleanup.
1644
1645 But for now we share this destructor for both purposes
1646 so no fields are _dwarf_get_alloc() and all are free-d
1647 here..
1648 */
1649 void
1650 _dwarf_macro_destructor(void *m)
1651 {
1652 Dwarf_Macro_Context mc= (Dwarf_Macro_Context)m;
1653
1654 dealloc_macro_srcfiles(mc->mc_srcfiles, mc->mc_srcfiles_count);
1655 mc->mc_srcfiles = 0;
1656 mc->mc_srcfiles_count = 0;
1657 free((void *)mc->mc_file_path);
1658 mc->mc_file_path = 0;
1659 free(mc->mc_ops);
1660 mc->mc_ops = 0;
1661 free(mc->mc_opcode_forms);
1662 mc->mc_opcode_forms = 0;
1663 memset(mc,0,sizeof(*mc));
1664 /* Just a recognizable sentinel.
1665 For debugging. No real meaning . */
1666 mc->mc_sentinel = 0xdeadbeef;
1667 }