Automatic date update in version.in
[binutils-gdb.git] / gdb / dwarf2 / macro.c
blob99c3653a2c3a27cf264346cdb35deeb6c4d9dbc9
1 /* Read DWARF macro information
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
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.
12 This file is part of GDB.
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.
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.
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/>. */
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"
40 static void
41 dwarf2_macro_malformed_definition_complaint (const char *arg1)
43 complaint (_("macro debug info contains a "
44 "malformed macro definition:\n`%s'"),
45 arg1);
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)
54 /* File name relative to the compilation directory of this source file. */
55 std::string file_name = lh->file_file_name (file);
57 if (! current_file)
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 ();
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);
68 else
69 current_file = macro_include (current_file, line, file_name.c_str ());
71 return current_file;
74 static const char *
75 consume_improper_spaces (const char *p, const char *body)
77 if (*p == ' ')
79 complaint (_("macro definition contains spaces "
80 "in formal argument list:\n`%s'"),
81 body);
83 while (*p == ' ')
84 p++;
87 return p;
91 static void
92 parse_macro_definition (struct macro_source_file *file, int line,
93 const char *body)
95 const char *p;
97 /* The body string takes one of two forms. For object-like macro
98 definitions, it should be:
100 <macro name> " " <definition>
102 For function-like macro definitions, it should be:
104 <macro name> "() " <definition>
106 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
108 Spaces may appear only where explicitly indicated, and in the
109 <definition>.
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.
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. */
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;
128 if (*p == ' ' || *p == '\0')
130 /* It's an object-like macro. */
131 int name_len = p - body;
132 std::string name (body, name_len);
133 const char *replacement;
135 if (*p == ' ')
136 replacement = body + name_len + 1;
137 else
139 dwarf2_macro_malformed_definition_complaint (body);
140 replacement = body + name_len;
143 macro_define_object (file, line, name.c_str (), replacement);
145 else if (*p == '(')
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);
153 p++;
155 p = consume_improper_spaces (p, body);
157 /* Parse the formal argument list. */
158 while (*p && *p != ')')
160 /* Find the extent of the current argument name. */
161 const char *arg_start = p;
163 while (*p && *p != ',' && *p != ')' && *p != ' ')
164 p++;
166 if (! *p || p == arg_start)
167 dwarf2_macro_malformed_definition_complaint (body);
168 else
170 /* Make sure argv has room for the new argument. */
171 if (argc >= argv_size)
173 argv_size *= 2;
174 argv = XRESIZEVEC (char *, argv, argv_size);
177 argv[argc++] = savestring (arg_start, p - arg_start);
180 p = consume_improper_spaces (p, body);
182 /* Consume the comma, if present. */
183 if (*p == ',')
185 p++;
187 p = consume_improper_spaces (p, body);
191 if (*p == ')')
193 p++;
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')
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,
208 else
209 /* Just complain. */
210 dwarf2_macro_malformed_definition_complaint (body);
212 else
213 /* Just complain. */
214 dwarf2_macro_malformed_definition_complaint (body);
217 int i;
219 for (i = 0; i < argc; i++)
220 xfree (argv[i]);
222 xfree (argv);
224 else
225 dwarf2_macro_malformed_definition_complaint (body);
228 /* Skip some bytes from BYTES according to the form given in FORM.
229 Returns the new pointer. */
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)
237 unsigned int bytes_read;
239 switch (form)
241 case DW_FORM_data1:
242 case DW_FORM_flag:
243 ++bytes;
244 break;
246 case DW_FORM_data2:
247 bytes += 2;
248 break;
250 case DW_FORM_data4:
251 bytes += 4;
252 break;
254 case DW_FORM_data8:
255 bytes += 8;
256 break;
258 case DW_FORM_data16:
259 bytes += 16;
260 break;
262 case DW_FORM_string:
263 read_direct_string (abfd, bytes, &bytes_read);
264 bytes += bytes_read;
265 break;
267 case DW_FORM_sec_offset:
268 case DW_FORM_strp:
269 case DW_FORM_GNU_strp_alt:
270 bytes += offset_size;
271 break;
273 case DW_FORM_block:
274 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
275 bytes += bytes_read;
276 break;
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;
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)
297 section->overflow_complaint ();
298 return NULL;
300 break;
302 case DW_FORM_implicit_const:
303 break;
305 default:
307 complaint (_("invalid form 0x%x in `%s'"),
308 form, section->get_name ());
309 return NULL;
313 return bytes;
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. */
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)
328 unsigned int bytes_read, i;
329 unsigned long arg;
330 const gdb_byte *defn;
332 if (opcode_definitions[opcode] == NULL)
334 complaint (_("unrecognized DW_MACINFO or DW_MACRO opcode 0x%x"),
335 opcode);
336 return NULL;
339 defn = opcode_definitions[opcode];
340 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
341 defn += bytes_read;
343 for (i = 0; i < arg; ++i)
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)
350 /* skip_form_bytes already issued the complaint. */
351 return NULL;
355 return mac_ptr;
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. */
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)
370 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
372 if (section_is_gnu)
374 unsigned int version, flags;
376 version = read_2_bytes (abfd, mac_ptr);
377 if (version != 4 && version != 5)
379 complaint (_("unrecognized version `%d' in .debug_macro section"),
380 version);
381 return NULL;
383 mac_ptr += 2;
385 flags = read_1_byte (abfd, mac_ptr);
386 ++mac_ptr;
387 *offset_size = (flags & 1) ? 8 : 4;
389 if ((flags & 2) != 0)
390 /* We don't need the line table offset. */
391 mac_ptr += *offset_size;
393 /* Vendor opcode descriptions. */
394 if ((flags & 4) != 0)
396 unsigned int i, count;
398 count = read_1_byte (abfd, mac_ptr);
399 ++mac_ptr;
400 for (i = 0; i < count; ++i)
402 unsigned int opcode, bytes_read;
403 unsigned long arg;
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;
415 return mac_ptr;
418 /* A helper for dwarf_decode_macros that handles the GNU extensions,
419 including DW_MACRO_import. */
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)
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];
441 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
442 &offset_size, section_is_gnu);
443 if (mac_ptr == NULL)
445 /* We already issued a complaint. */
446 return;
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. */
456 at_commandline = 1;
460 /* Do we at least have room for a macinfo type byte? */
461 if (mac_ptr >= mac_end)
463 section->overflow_complaint ();
464 break;
467 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
468 mac_ptr++;
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)
476 /* A zero macinfo type indicates the end of the macro
477 information. */
478 case 0:
479 break;
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:
488 unsigned int bytes_read;
489 int line;
490 const char *body;
491 int is_define;
493 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
494 mac_ptr += bytes_read;
496 if (macinfo_type == DW_MACRO_define
497 || macinfo_type == DW_MACRO_undef)
499 body = read_direct_string (abfd, mac_ptr, &bytes_read);
500 mac_ptr += bytes_read;
502 else
504 LONGEST str_offset;
506 str_offset = read_offset (abfd, mac_ptr, offset_size);
507 mac_ptr += offset_size;
509 if (macinfo_type == DW_MACRO_define_sup
510 || macinfo_type == DW_MACRO_undef_sup
511 || section_is_dwz)
513 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd,
514 true);
516 body = dwz->read_string (objfile, str_offset);
518 else
519 body = per_objfile->per_bfd->str.read_string (objfile,
520 str_offset,
521 "DW_FORM_strp");
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)
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;
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);
543 if (body == NULL)
545 /* Fedora's rpm-build's "debugedit" binary
546 corrupted .debug_macro sections.
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);
557 else if (is_define)
558 parse_macro_definition (current_file, line, body);
559 else
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);
567 break;
569 case DW_MACRO_define_strx:
570 case DW_MACRO_undef_strx:
572 unsigned int bytes_read;
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;
579 /* Use of the strx operators requires a DW_AT_str_offsets_base. */
580 if (!str_offsets_base.has_value ())
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;
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);
596 const char *macinfo_str = (macinfo_type == DW_MACRO_define_strx ?
597 "DW_MACRO_define_strx" : "DW_MACRO_undef_strx");
599 if (*str_offsets_base + offset_index * offset_size
600 >= str_offsets_section->size)
602 complaint (_("%s pointing outside of .debug_str_offsets section "
603 "[in module %s]"), macinfo_str, objfile_name (objfile));
604 break;
607 ULONGEST str_offset = read_offset (abfd, info_ptr, offset_size);
609 const char *body = str_section->read_string (objfile, str_offset,
610 macinfo_str);
611 if (current_file == nullptr)
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;
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);
626 break;
628 case DW_MACRO_start_file:
630 unsigned int bytes_read;
631 int line, file;
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;
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);
645 if (at_commandline)
647 /* This DW_MACRO_start_file was executed in the
648 pass one. */
649 at_commandline = 0;
651 else
652 current_file = macro_start_file (builder, file, line,
653 current_file, lh);
655 break;
657 case DW_MACRO_end_file:
658 if (! current_file)
659 complaint (_("macro debug info has an unmatched "
660 "`close_file' directive"));
661 else
663 current_file = current_file->included_by;
664 if (! current_file)
666 enum dwarf_macro_record_type next_type;
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. */
673 /* Do we at least have room for a macinfo type byte? */
674 if (mac_ptr >= mac_end)
676 section->overflow_complaint ();
677 return;
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"));
689 return;
692 break;
694 case DW_MACRO_import:
695 case DW_MACRO_import_sup:
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;
705 offset = read_offset (abfd, mac_ptr, offset_size);
706 mac_ptr += offset_size;
708 if (macinfo_type == DW_MACRO_import_sup)
710 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd,
711 true);
713 dwz->macro.read (objfile);
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;
721 new_mac_ptr = include_section->buffer + offset;
722 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
724 if (*slot != NULL)
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"));
731 else
733 *slot = (void *) new_mac_ptr;
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);
742 htab_remove_elt (include_hash, (void *) new_mac_ptr);
745 break;
747 case DW_MACINFO_vendor_ext:
748 if (!section_is_gnu)
750 unsigned int bytes_read;
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;
759 /* We don't recognize any vendor extensions. */
760 break;
762 /* FALLTHROUGH */
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;
772 DIAGNOSTIC_POP
773 } while (macinfo_type != 0);
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)
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;
793 abfd = section->get_bfd_owner ();
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.
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. */
806 mac_ptr = section->buffer + offset;
807 mac_end = section->buffer + section->size;
809 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
810 &offset_size, section_is_gnu);
811 if (mac_ptr == NULL)
813 /* We already issued a complaint. */
814 return;
819 /* Do we at least have room for a macinfo type byte? */
820 if (mac_ptr >= mac_end)
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;
828 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
829 mac_ptr++;
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)
837 /* A zero macinfo type indicates the end of the macro
838 information. */
839 case 0:
840 break;
842 case DW_MACRO_define:
843 case DW_MACRO_undef:
844 /* Only skip the data by MAC_PTR. */
846 unsigned int bytes_read;
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;
853 break;
855 case DW_MACRO_start_file:
857 unsigned int bytes_read;
858 int line, file;
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;
865 current_file = macro_start_file (builder, file, line,
866 current_file, lh);
868 break;
870 case DW_MACRO_end_file:
871 /* No data to skip by MAC_PTR. */
872 break;
874 case DW_MACRO_define_strp:
875 case DW_MACRO_undef_strp:
876 case DW_MACRO_define_sup:
877 case DW_MACRO_undef_sup:
879 unsigned int bytes_read;
881 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
882 mac_ptr += bytes_read;
883 mac_ptr += offset_size;
885 break;
886 case DW_MACRO_define_strx:
887 case DW_MACRO_undef_strx:
889 unsigned int bytes_read;
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;
896 break;
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;
906 case DW_MACINFO_vendor_ext:
907 /* Only skip the data by MAC_PTR. */
908 if (!section_is_gnu)
910 unsigned int bytes_read;
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;
917 /* FALLTHROUGH */
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;
927 DIAGNOSTIC_POP
928 } while (macinfo_type != 0 && current_file == NULL);
930 /* Second pass: Process all entries.
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. */
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 ());