Fix a new warning on Cygwin
[binutils-gdb.git] / gdb / dwarf2 / macro.c
1 /* Read DWARF macro information
2
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 #include "defs.h"
28 #include "dwarf2/read.h"
29 #include "dwarf2/leb.h"
30 #include "dwarf2/expr.h"
31 #include "dwarf2/line-header.h"
32 #include "dwarf2/section.h"
33 #include "dwarf2/macro.h"
34 #include "dwarf2/dwz.h"
35 #include "buildsym.h"
36 #include "macrotab.h"
37 #include "complaints.h"
38 #include "objfiles.h"
39
40 static void
41 dwarf2_macro_malformed_definition_complaint (const char *arg1)
42 {
43 complaint (_("macro debug info contains a "
44 "malformed macro definition:\n`%s'"),
45 arg1);
46 }
47
48 static struct macro_source_file *
49 macro_start_file (buildsym_compunit *builder,
50 int file, int line,
51 struct macro_source_file *current_file,
52 const struct line_header *lh)
53 {
54 /* File name relative to the compilation directory of this source file. */
55 std::string file_name = lh->file_file_name (file);
56
57 if (! current_file)
58 {
59 /* Note: We don't create a macro table for this compilation unit
60 at all until we actually get a filename. */
61 struct macro_table *macro_table = builder->get_macro_table ();
62
63 /* If we have no current file, then this must be the start_file
64 directive for the compilation unit's main source file. */
65 current_file = macro_set_main (macro_table, file_name.c_str ());
66 macro_define_special (macro_table);
67 }
68 else
69 current_file = macro_include (current_file, line, file_name.c_str ());
70
71 return current_file;
72 }
73
74 static const char *
75 consume_improper_spaces (const char *p, const char *body)
76 {
77 if (*p == ' ')
78 {
79 complaint (_("macro definition contains spaces "
80 "in formal argument list:\n`%s'"),
81 body);
82
83 while (*p == ' ')
84 p++;
85 }
86
87 return p;
88 }
89
90
91 static void
92 parse_macro_definition (struct macro_source_file *file, int line,
93 const char *body)
94 {
95 const char *p;
96
97 /* The body string takes one of two forms. For object-like macro
98 definitions, it should be:
99
100 <macro name> " " <definition>
101
102 For function-like macro definitions, it should be:
103
104 <macro name> "() " <definition>
105 or
106 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
107
108 Spaces may appear only where explicitly indicated, and in the
109 <definition>.
110
111 The Dwarf 2 spec says that an object-like macro's name is always
112 followed by a space, but versions of GCC around March 2002 omit
113 the space when the macro's definition is the empty string.
114
115 The Dwarf 2 spec says that there should be no spaces between the
116 formal arguments in a function-like macro's formal argument list,
117 but versions of GCC around March 2002 include spaces after the
118 commas. */
119
120
121 /* Find the extent of the macro name. The macro name is terminated
122 by either a space or null character (for an object-like macro) or
123 an opening paren (for a function-like macro). */
124 for (p = body; *p; p++)
125 if (*p == ' ' || *p == '(')
126 break;
127
128 if (*p == ' ' || *p == '\0')
129 {
130 /* It's an object-like macro. */
131 int name_len = p - body;
132 std::string name (body, name_len);
133 const char *replacement;
134
135 if (*p == ' ')
136 replacement = body + name_len + 1;
137 else
138 {
139 dwarf2_macro_malformed_definition_complaint (body);
140 replacement = body + name_len;
141 }
142
143 macro_define_object (file, line, name.c_str (), replacement);
144 }
145 else if (*p == '(')
146 {
147 /* It's a function-like macro. */
148 std::string name (body, p - body);
149 int argc = 0;
150 int argv_size = 1;
151 char **argv = XNEWVEC (char *, argv_size);
152
153 p++;
154
155 p = consume_improper_spaces (p, body);
156
157 /* Parse the formal argument list. */
158 while (*p && *p != ')')
159 {
160 /* Find the extent of the current argument name. */
161 const char *arg_start = p;
162
163 while (*p && *p != ',' && *p != ')' && *p != ' ')
164 p++;
165
166 if (! *p || p == arg_start)
167 dwarf2_macro_malformed_definition_complaint (body);
168 else
169 {
170 /* Make sure argv has room for the new argument. */
171 if (argc >= argv_size)
172 {
173 argv_size *= 2;
174 argv = XRESIZEVEC (char *, argv, argv_size);
175 }
176
177 argv[argc++] = savestring (arg_start, p - arg_start);
178 }
179
180 p = consume_improper_spaces (p, body);
181
182 /* Consume the comma, if present. */
183 if (*p == ',')
184 {
185 p++;
186
187 p = consume_improper_spaces (p, body);
188 }
189 }
190
191 if (*p == ')')
192 {
193 p++;
194
195 if (*p == ' ')
196 /* Perfectly formed definition, no complaints. */
197 macro_define_function (file, line, name.c_str (),
198 argc, (const char **) argv,
199 p + 1);
200 else if (*p == '\0')
201 {
202 /* Complain, but do define it. */
203 dwarf2_macro_malformed_definition_complaint (body);
204 macro_define_function (file, line, name.c_str (),
205 argc, (const char **) argv,
206 p);
207 }
208 else
209 /* Just complain. */
210 dwarf2_macro_malformed_definition_complaint (body);
211 }
212 else
213 /* Just complain. */
214 dwarf2_macro_malformed_definition_complaint (body);
215
216 {
217 int i;
218
219 for (i = 0; i < argc; i++)
220 xfree (argv[i]);
221 }
222 xfree (argv);
223 }
224 else
225 dwarf2_macro_malformed_definition_complaint (body);
226 }
227
228 /* Skip some bytes from BYTES according to the form given in FORM.
229 Returns the new pointer. */
230
231 static const gdb_byte *
232 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
233 enum dwarf_form form,
234 unsigned int offset_size,
235 const struct dwarf2_section_info *section)
236 {
237 unsigned int bytes_read;
238
239 switch (form)
240 {
241 case DW_FORM_data1:
242 case DW_FORM_flag:
243 ++bytes;
244 break;
245
246 case DW_FORM_data2:
247 bytes += 2;
248 break;
249
250 case DW_FORM_data4:
251 bytes += 4;
252 break;
253
254 case DW_FORM_data8:
255 bytes += 8;
256 break;
257
258 case DW_FORM_data16:
259 bytes += 16;
260 break;
261
262 case DW_FORM_string:
263 read_direct_string (abfd, bytes, &bytes_read);
264 bytes += bytes_read;
265 break;
266
267 case DW_FORM_sec_offset:
268 case DW_FORM_strp:
269 case DW_FORM_GNU_strp_alt:
270 bytes += offset_size;
271 break;
272
273 case DW_FORM_block:
274 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
275 bytes += bytes_read;
276 break;
277
278 case DW_FORM_block1:
279 bytes += 1 + read_1_byte (abfd, bytes);
280 break;
281 case DW_FORM_block2:
282 bytes += 2 + read_2_bytes (abfd, bytes);
283 break;
284 case DW_FORM_block4:
285 bytes += 4 + read_4_bytes (abfd, bytes);
286 break;
287
288 case DW_FORM_addrx:
289 case DW_FORM_sdata:
290 case DW_FORM_strx:
291 case DW_FORM_udata:
292 case DW_FORM_GNU_addr_index:
293 case DW_FORM_GNU_str_index:
294 bytes = gdb_skip_leb128 (bytes, buffer_end);
295 if (bytes == NULL)
296 {
297 section->overflow_complaint ();
298 return NULL;
299 }
300 break;
301
302 case DW_FORM_implicit_const:
303 break;
304
305 default:
306 {
307 complaint (_("invalid form 0x%x in `%s'"),
308 form, section->get_name ());
309 return NULL;
310 }
311 }
312
313 return bytes;
314 }
315
316 /* A helper for dwarf_decode_macros that handles skipping an unknown
317 opcode. Returns an updated pointer to the macro data buffer; or,
318 on error, issues a complaint and returns NULL. */
319
320 static const gdb_byte *
321 skip_unknown_opcode (unsigned int opcode,
322 const gdb_byte **opcode_definitions,
323 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
324 bfd *abfd,
325 unsigned int offset_size,
326 const struct dwarf2_section_info *section)
327 {
328 unsigned int bytes_read, i;
329 unsigned long arg;
330 const gdb_byte *defn;
331
332 if (opcode_definitions[opcode] == NULL)
333 {
334 complaint (_("unrecognized DW_MACINFO or DW_MACRO opcode 0x%x"),
335 opcode);
336 return NULL;
337 }
338
339 defn = opcode_definitions[opcode];
340 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
341 defn += bytes_read;
342
343 for (i = 0; i < arg; ++i)
344 {
345 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
346 (enum dwarf_form) defn[i], offset_size,
347 section);
348 if (mac_ptr == NULL)
349 {
350 /* skip_form_bytes already issued the complaint. */
351 return NULL;
352 }
353 }
354
355 return mac_ptr;
356 }
357
358 /* A helper function which parses the header of a macro section.
359 If the macro section is the extended (for now called "GNU") type,
360 then this updates *OFFSET_SIZE. Returns a pointer to just after
361 the header, or issues a complaint and returns NULL on error. */
362
363 static const gdb_byte *
364 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
365 bfd *abfd,
366 const gdb_byte *mac_ptr,
367 unsigned int *offset_size,
368 int section_is_gnu)
369 {
370 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
371
372 if (section_is_gnu)
373 {
374 unsigned int version, flags;
375
376 version = read_2_bytes (abfd, mac_ptr);
377 if (version != 4 && version != 5)
378 {
379 complaint (_("unrecognized version `%d' in .debug_macro section"),
380 version);
381 return NULL;
382 }
383 mac_ptr += 2;
384
385 flags = read_1_byte (abfd, mac_ptr);
386 ++mac_ptr;
387 *offset_size = (flags & 1) ? 8 : 4;
388
389 if ((flags & 2) != 0)
390 /* We don't need the line table offset. */
391 mac_ptr += *offset_size;
392
393 /* Vendor opcode descriptions. */
394 if ((flags & 4) != 0)
395 {
396 unsigned int i, count;
397
398 count = read_1_byte (abfd, mac_ptr);
399 ++mac_ptr;
400 for (i = 0; i < count; ++i)
401 {
402 unsigned int opcode, bytes_read;
403 unsigned long arg;
404
405 opcode = read_1_byte (abfd, mac_ptr);
406 ++mac_ptr;
407 opcode_definitions[opcode] = mac_ptr;
408 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
409 mac_ptr += bytes_read;
410 mac_ptr += arg;
411 }
412 }
413 }
414
415 return mac_ptr;
416 }
417
418 /* A helper for dwarf_decode_macros that handles the GNU extensions,
419 including DW_MACRO_import. */
420
421 static void
422 dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
423 buildsym_compunit *builder,
424 bfd *abfd,
425 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
426 struct macro_source_file *current_file,
427 const struct line_header *lh,
428 const struct dwarf2_section_info *section,
429 int section_is_gnu, int section_is_dwz,
430 unsigned int offset_size,
431 struct dwarf2_section_info *str_section,
432 struct dwarf2_section_info *str_offsets_section,
433 gdb::optional<ULONGEST> str_offsets_base,
434 htab_t include_hash)
435 {
436 struct objfile *objfile = per_objfile->objfile;
437 enum dwarf_macro_record_type macinfo_type;
438 int at_commandline;
439 const gdb_byte *opcode_definitions[256];
440
441 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
442 &offset_size, section_is_gnu);
443 if (mac_ptr == NULL)
444 {
445 /* We already issued a complaint. */
446 return;
447 }
448
449 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
450 GDB is still reading the definitions from command line. First
451 DW_MACINFO_start_file will need to be ignored as it was already executed
452 to create CURRENT_FILE for the main source holding also the command line
453 definitions. On first met DW_MACINFO_start_file this flag is reset to
454 normally execute all the remaining DW_MACINFO_start_file macinfos. */
455
456 at_commandline = 1;
457
458 do
459 {
460 /* Do we at least have room for a macinfo type byte? */
461 if (mac_ptr >= mac_end)
462 {
463 section->overflow_complaint ();
464 break;
465 }
466
467 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
468 mac_ptr++;
469
470 /* Note that we rely on the fact that the corresponding GNU and
471 DWARF constants are the same. */
472 DIAGNOSTIC_PUSH
473 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
474 switch (macinfo_type)
475 {
476 /* A zero macinfo type indicates the end of the macro
477 information. */
478 case 0:
479 break;
480
481 case DW_MACRO_define:
482 case DW_MACRO_undef:
483 case DW_MACRO_define_strp:
484 case DW_MACRO_undef_strp:
485 case DW_MACRO_define_sup:
486 case DW_MACRO_undef_sup:
487 {
488 unsigned int bytes_read;
489 int line;
490 const char *body;
491 int is_define;
492
493 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
494 mac_ptr += bytes_read;
495
496 if (macinfo_type == DW_MACRO_define
497 || macinfo_type == DW_MACRO_undef)
498 {
499 body = read_direct_string (abfd, mac_ptr, &bytes_read);
500 mac_ptr += bytes_read;
501 }
502 else
503 {
504 LONGEST str_offset;
505
506 str_offset = read_offset (abfd, mac_ptr, offset_size);
507 mac_ptr += offset_size;
508
509 if (macinfo_type == DW_MACRO_define_sup
510 || macinfo_type == DW_MACRO_undef_sup
511 || section_is_dwz)
512 {
513 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd,
514 true);
515
516 body = dwz->read_string (objfile, str_offset);
517 }
518 else
519 body = per_objfile->per_bfd->str.read_string (objfile,
520 str_offset,
521 "DW_FORM_strp");
522 }
523
524 is_define = (macinfo_type == DW_MACRO_define
525 || macinfo_type == DW_MACRO_define_strp
526 || macinfo_type == DW_MACRO_define_sup);
527 if (! current_file)
528 {
529 /* DWARF violation as no main source is present. */
530 complaint (_("debug info with no main source gives macro %s "
531 "on line %d: %s"),
532 is_define ? _("definition") : _("undefinition"),
533 line, body);
534 break;
535 }
536 if ((line == 0 && !at_commandline)
537 || (line != 0 && at_commandline))
538 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
539 at_commandline ? _("command-line") : _("in-file"),
540 is_define ? _("definition") : _("undefinition"),
541 line == 0 ? _("zero") : _("non-zero"), line, body);
542
543 if (body == NULL)
544 {
545 /* Fedora's rpm-build's "debugedit" binary
546 corrupted .debug_macro sections.
547
548 For more info, see
549 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
550 complaint (_("debug info gives %s invalid macro %s "
551 "without body (corrupted?) at line %d "
552 "on file %s"),
553 at_commandline ? _("command-line") : _("in-file"),
554 is_define ? _("definition") : _("undefinition"),
555 line, current_file->filename);
556 }
557 else if (is_define)
558 parse_macro_definition (current_file, line, body);
559 else
560 {
561 gdb_assert (macinfo_type == DW_MACRO_undef
562 || macinfo_type == DW_MACRO_undef_strp
563 || macinfo_type == DW_MACRO_undef_sup);
564 macro_undef (current_file, line, body);
565 }
566 }
567 break;
568
569 case DW_MACRO_define_strx:
570 case DW_MACRO_undef_strx:
571 {
572 unsigned int bytes_read;
573
574 int line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
575 mac_ptr += bytes_read;
576 int offset_index = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
577 mac_ptr += bytes_read;
578
579 /* Use of the strx operators requires a DW_AT_str_offsets_base. */
580 if (!str_offsets_base.has_value ())
581 {
582 complaint (_("use of %s with unknown string offsets base "
583 "[in module %s]"),
584 (macinfo_type == DW_MACRO_define_strx
585 ? "DW_MACRO_define_strx"
586 : "DW_MACRO_undef_strx"),
587 objfile_name (objfile));
588 break;
589 }
590
591 str_offsets_section->read (objfile);
592 const gdb_byte *info_ptr = (str_offsets_section->buffer
593 + *str_offsets_base
594 + offset_index * offset_size);
595
596 const char *macinfo_str = (macinfo_type == DW_MACRO_define_strx ?
597 "DW_MACRO_define_strx" : "DW_MACRO_undef_strx");
598
599 if (*str_offsets_base + offset_index * offset_size
600 >= str_offsets_section->size)
601 {
602 complaint (_("%s pointing outside of .debug_str_offsets section "
603 "[in module %s]"), macinfo_str, objfile_name (objfile));
604 break;
605 }
606
607 ULONGEST str_offset = read_offset (abfd, info_ptr, offset_size);
608
609 const char *body = str_section->read_string (objfile, str_offset,
610 macinfo_str);
611 if (current_file == nullptr)
612 {
613 /* DWARF violation as no main source is present. */
614 complaint (_("debug info with no main source gives macro %s "
615 "on line %d: %s"),
616 macinfo_type == DW_MACRO_define_strx ? _("definition")
617 : _("undefinition"), line, body);
618 break;
619 }
620
621 if (macinfo_type == DW_MACRO_define_strx)
622 parse_macro_definition (current_file, line, body);
623 else
624 macro_undef (current_file, line, body);
625 }
626 break;
627
628 case DW_MACRO_start_file:
629 {
630 unsigned int bytes_read;
631 int line, file;
632
633 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
634 mac_ptr += bytes_read;
635 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
636 mac_ptr += bytes_read;
637
638 if ((line == 0 && !at_commandline)
639 || (line != 0 && at_commandline))
640 complaint (_("debug info gives source %d included "
641 "from %s at %s line %d"),
642 file, at_commandline ? _("command-line") : _("file"),
643 line == 0 ? _("zero") : _("non-zero"), line);
644
645 if (at_commandline)
646 {
647 /* This DW_MACRO_start_file was executed in the
648 pass one. */
649 at_commandline = 0;
650 }
651 else
652 current_file = macro_start_file (builder, file, line,
653 current_file, lh);
654 }
655 break;
656
657 case DW_MACRO_end_file:
658 if (! current_file)
659 complaint (_("macro debug info has an unmatched "
660 "`close_file' directive"));
661 else
662 {
663 current_file = current_file->included_by;
664 if (! current_file)
665 {
666 enum dwarf_macro_record_type next_type;
667
668 /* GCC circa March 2002 doesn't produce the zero
669 type byte marking the end of the compilation
670 unit. Complain if it's not there, but exit no
671 matter what. */
672
673 /* Do we at least have room for a macinfo type byte? */
674 if (mac_ptr >= mac_end)
675 {
676 section->overflow_complaint ();
677 return;
678 }
679
680 /* We don't increment mac_ptr here, so this is just
681 a look-ahead. */
682 next_type
683 = (enum dwarf_macro_record_type) read_1_byte (abfd,
684 mac_ptr);
685 if (next_type != 0)
686 complaint (_("no terminating 0-type entry for "
687 "macros in `.debug_macinfo' section"));
688
689 return;
690 }
691 }
692 break;
693
694 case DW_MACRO_import:
695 case DW_MACRO_import_sup:
696 {
697 LONGEST offset;
698 void **slot;
699 bfd *include_bfd = abfd;
700 const struct dwarf2_section_info *include_section = section;
701 const gdb_byte *include_mac_end = mac_end;
702 int is_dwz = section_is_dwz;
703 const gdb_byte *new_mac_ptr;
704
705 offset = read_offset (abfd, mac_ptr, offset_size);
706 mac_ptr += offset_size;
707
708 if (macinfo_type == DW_MACRO_import_sup)
709 {
710 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd,
711 true);
712
713 dwz->macro.read (objfile);
714
715 include_section = &dwz->macro;
716 include_bfd = include_section->get_bfd_owner ();
717 include_mac_end = dwz->macro.buffer + dwz->macro.size;
718 is_dwz = 1;
719 }
720
721 new_mac_ptr = include_section->buffer + offset;
722 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
723
724 if (*slot != NULL)
725 {
726 /* This has actually happened; see
727 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
728 complaint (_("recursive DW_MACRO_import in "
729 ".debug_macro section"));
730 }
731 else
732 {
733 *slot = (void *) new_mac_ptr;
734
735 dwarf_decode_macro_bytes (per_objfile, builder, include_bfd,
736 new_mac_ptr, include_mac_end,
737 current_file, lh, section,
738 section_is_gnu, is_dwz, offset_size,
739 str_section, str_offsets_section,
740 str_offsets_base, include_hash);
741
742 htab_remove_elt (include_hash, (void *) new_mac_ptr);
743 }
744 }
745 break;
746
747 case DW_MACINFO_vendor_ext:
748 if (!section_is_gnu)
749 {
750 unsigned int bytes_read;
751
752 /* This reads the constant, but since we don't recognize
753 any vendor extensions, we ignore it. */
754 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
755 mac_ptr += bytes_read;
756 read_direct_string (abfd, mac_ptr, &bytes_read);
757 mac_ptr += bytes_read;
758
759 /* We don't recognize any vendor extensions. */
760 break;
761 }
762 /* FALLTHROUGH */
763
764 default:
765 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
766 mac_ptr, mac_end, abfd, offset_size,
767 section);
768 if (mac_ptr == NULL)
769 return;
770 break;
771 }
772 DIAGNOSTIC_POP
773 } while (macinfo_type != 0);
774 }
775
776 void
777 dwarf_decode_macros (dwarf2_per_objfile *per_objfile,
778 buildsym_compunit *builder,
779 const dwarf2_section_info *section,
780 const struct line_header *lh, unsigned int offset_size,
781 unsigned int offset, struct dwarf2_section_info *str_section,
782 struct dwarf2_section_info *str_offsets_section,
783 gdb::optional<ULONGEST> str_offsets_base,
784 int section_is_gnu)
785 {
786 bfd *abfd;
787 const gdb_byte *mac_ptr, *mac_end;
788 struct macro_source_file *current_file = 0;
789 enum dwarf_macro_record_type macinfo_type;
790 const gdb_byte *opcode_definitions[256];
791 void **slot;
792
793 abfd = section->get_bfd_owner ();
794
795 /* First pass: Find the name of the base filename.
796 This filename is needed in order to process all macros whose definition
797 (or undefinition) comes from the command line. These macros are defined
798 before the first DW_MACINFO_start_file entry, and yet still need to be
799 associated to the base file.
800
801 To determine the base file name, we scan the macro definitions until we
802 reach the first DW_MACINFO_start_file entry. We then initialize
803 CURRENT_FILE accordingly so that any macro definition found before the
804 first DW_MACINFO_start_file can still be associated to the base file. */
805
806 mac_ptr = section->buffer + offset;
807 mac_end = section->buffer + section->size;
808
809 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
810 &offset_size, section_is_gnu);
811 if (mac_ptr == NULL)
812 {
813 /* We already issued a complaint. */
814 return;
815 }
816
817 do
818 {
819 /* Do we at least have room for a macinfo type byte? */
820 if (mac_ptr >= mac_end)
821 {
822 /* Complaint is printed during the second pass as GDB will probably
823 stop the first pass earlier upon finding
824 DW_MACINFO_start_file. */
825 break;
826 }
827
828 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
829 mac_ptr++;
830
831 /* Note that we rely on the fact that the corresponding GNU and
832 DWARF constants are the same. */
833 DIAGNOSTIC_PUSH
834 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
835 switch (macinfo_type)
836 {
837 /* A zero macinfo type indicates the end of the macro
838 information. */
839 case 0:
840 break;
841
842 case DW_MACRO_define:
843 case DW_MACRO_undef:
844 /* Only skip the data by MAC_PTR. */
845 {
846 unsigned int bytes_read;
847
848 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
849 mac_ptr += bytes_read;
850 read_direct_string (abfd, mac_ptr, &bytes_read);
851 mac_ptr += bytes_read;
852 }
853 break;
854
855 case DW_MACRO_start_file:
856 {
857 unsigned int bytes_read;
858 int line, file;
859
860 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
861 mac_ptr += bytes_read;
862 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
863 mac_ptr += bytes_read;
864
865 current_file = macro_start_file (builder, file, line,
866 current_file, lh);
867 }
868 break;
869
870 case DW_MACRO_end_file:
871 /* No data to skip by MAC_PTR. */
872 break;
873
874 case DW_MACRO_define_strp:
875 case DW_MACRO_undef_strp:
876 case DW_MACRO_define_sup:
877 case DW_MACRO_undef_sup:
878 {
879 unsigned int bytes_read;
880
881 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
882 mac_ptr += bytes_read;
883 mac_ptr += offset_size;
884 }
885 break;
886 case DW_MACRO_define_strx:
887 case DW_MACRO_undef_strx:
888 {
889 unsigned int bytes_read;
890
891 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
892 mac_ptr += bytes_read;
893 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
894 mac_ptr += bytes_read;
895 }
896 break;
897
898 case DW_MACRO_import:
899 case DW_MACRO_import_sup:
900 /* Note that, according to the spec, a transparent include
901 chain cannot call DW_MACRO_start_file. So, we can just
902 skip this opcode. */
903 mac_ptr += offset_size;
904 break;
905
906 case DW_MACINFO_vendor_ext:
907 /* Only skip the data by MAC_PTR. */
908 if (!section_is_gnu)
909 {
910 unsigned int bytes_read;
911
912 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
913 mac_ptr += bytes_read;
914 read_direct_string (abfd, mac_ptr, &bytes_read);
915 mac_ptr += bytes_read;
916 }
917 /* FALLTHROUGH */
918
919 default:
920 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
921 mac_ptr, mac_end, abfd, offset_size,
922 section);
923 if (mac_ptr == NULL)
924 return;
925 break;
926 }
927 DIAGNOSTIC_POP
928 } while (macinfo_type != 0 && current_file == NULL);
929
930 /* Second pass: Process all entries.
931
932 Use the AT_COMMAND_LINE flag to determine whether we are still processing
933 command-line macro definitions/undefinitions. This flag is unset when we
934 reach the first DW_MACINFO_start_file entry. */
935
936 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
937 htab_eq_pointer,
938 NULL, xcalloc, xfree));
939 mac_ptr = section->buffer + offset;
940 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
941 *slot = (void *) mac_ptr;
942 dwarf_decode_macro_bytes (per_objfile, builder, abfd, mac_ptr, mac_end,
943 current_file, lh, section, section_is_gnu, 0,
944 offset_size, str_section, str_offsets_section,
945 str_offsets_base, include_hash.get ());
946 }