1 /* Read DWARF macro information
3 Copyright (C) 1994-2020 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
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/>. */
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"
37 #include "complaints.h"
40 dwarf2_macro_malformed_definition_complaint (const char *arg1
)
42 complaint (_("macro debug info contains a "
43 "malformed macro definition:\n`%s'"),
47 static struct macro_source_file
*
48 macro_start_file (buildsym_compunit
*builder
,
50 struct macro_source_file
*current_file
,
51 const struct line_header
*lh
)
53 /* File name relative to the compilation directory of this source file. */
54 gdb::unique_xmalloc_ptr
<char> file_name
= lh
->file_file_name (file
);
58 /* Note: We don't create a macro table for this compilation unit
59 at all until we actually get a filename. */
60 struct macro_table
*macro_table
= builder
->get_macro_table ();
62 /* If we have no current file, then this must be the start_file
63 directive for the compilation unit's main source file. */
64 current_file
= macro_set_main (macro_table
, file_name
.get ());
65 macro_define_special (macro_table
);
68 current_file
= macro_include (current_file
, line
, file_name
.get ());
74 consume_improper_spaces (const char *p
, const char *body
)
78 complaint (_("macro definition contains spaces "
79 "in formal argument list:\n`%s'"),
91 parse_macro_definition (struct macro_source_file
*file
, int line
,
96 /* The body string takes one of two forms. For object-like macro
97 definitions, it should be:
99 <macro name> " " <definition>
101 For function-like macro definitions, it should be:
103 <macro name> "() " <definition>
105 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
107 Spaces may appear only where explicitly indicated, and in the
110 The Dwarf 2 spec says that an object-like macro's name is always
111 followed by a space, but versions of GCC around March 2002 omit
112 the space when the macro's definition is the empty string.
114 The Dwarf 2 spec says that there should be no spaces between the
115 formal arguments in a function-like macro's formal argument list,
116 but versions of GCC around March 2002 include spaces after the
120 /* Find the extent of the macro name. The macro name is terminated
121 by either a space or null character (for an object-like macro) or
122 an opening paren (for a function-like macro). */
123 for (p
= body
; *p
; p
++)
124 if (*p
== ' ' || *p
== '(')
127 if (*p
== ' ' || *p
== '\0')
129 /* It's an object-like macro. */
130 int name_len
= p
- body
;
131 std::string
name (body
, name_len
);
132 const char *replacement
;
135 replacement
= body
+ name_len
+ 1;
138 dwarf2_macro_malformed_definition_complaint (body
);
139 replacement
= body
+ name_len
;
142 macro_define_object (file
, line
, name
.c_str (), replacement
);
146 /* It's a function-like macro. */
147 std::string
name (body
, p
- body
);
150 char **argv
= XNEWVEC (char *, argv_size
);
154 p
= consume_improper_spaces (p
, body
);
156 /* Parse the formal argument list. */
157 while (*p
&& *p
!= ')')
159 /* Find the extent of the current argument name. */
160 const char *arg_start
= p
;
162 while (*p
&& *p
!= ',' && *p
!= ')' && *p
!= ' ')
165 if (! *p
|| p
== arg_start
)
166 dwarf2_macro_malformed_definition_complaint (body
);
169 /* Make sure argv has room for the new argument. */
170 if (argc
>= argv_size
)
173 argv
= XRESIZEVEC (char *, argv
, argv_size
);
176 argv
[argc
++] = savestring (arg_start
, p
- arg_start
);
179 p
= consume_improper_spaces (p
, body
);
181 /* Consume the comma, if present. */
186 p
= consume_improper_spaces (p
, body
);
195 /* Perfectly formed definition, no complaints. */
196 macro_define_function (file
, line
, name
.c_str (),
197 argc
, (const char **) argv
,
201 /* Complain, but do define it. */
202 dwarf2_macro_malformed_definition_complaint (body
);
203 macro_define_function (file
, line
, name
.c_str (),
204 argc
, (const char **) argv
,
209 dwarf2_macro_malformed_definition_complaint (body
);
213 dwarf2_macro_malformed_definition_complaint (body
);
218 for (i
= 0; i
< argc
; i
++)
224 dwarf2_macro_malformed_definition_complaint (body
);
227 /* Skip some bytes from BYTES according to the form given in FORM.
228 Returns the new pointer. */
230 static const gdb_byte
*
231 skip_form_bytes (bfd
*abfd
, const gdb_byte
*bytes
, const gdb_byte
*buffer_end
,
232 enum dwarf_form form
,
233 unsigned int offset_size
,
234 const struct dwarf2_section_info
*section
)
236 unsigned int bytes_read
;
262 read_direct_string (abfd
, bytes
, &bytes_read
);
266 case DW_FORM_sec_offset
:
268 case DW_FORM_GNU_strp_alt
:
269 bytes
+= offset_size
;
273 bytes
+= read_unsigned_leb128 (abfd
, bytes
, &bytes_read
);
278 bytes
+= 1 + read_1_byte (abfd
, bytes
);
281 bytes
+= 2 + read_2_bytes (abfd
, bytes
);
284 bytes
+= 4 + read_4_bytes (abfd
, bytes
);
291 case DW_FORM_GNU_addr_index
:
292 case DW_FORM_GNU_str_index
:
293 bytes
= gdb_skip_leb128 (bytes
, buffer_end
);
296 section
->overflow_complaint ();
301 case DW_FORM_implicit_const
:
306 complaint (_("invalid form 0x%x in `%s'"),
307 form
, section
->get_name ());
315 /* A helper for dwarf_decode_macros that handles skipping an unknown
316 opcode. Returns an updated pointer to the macro data buffer; or,
317 on error, issues a complaint and returns NULL. */
319 static const gdb_byte
*
320 skip_unknown_opcode (unsigned int opcode
,
321 const gdb_byte
**opcode_definitions
,
322 const gdb_byte
*mac_ptr
, const gdb_byte
*mac_end
,
324 unsigned int offset_size
,
325 const struct dwarf2_section_info
*section
)
327 unsigned int bytes_read
, i
;
329 const gdb_byte
*defn
;
331 if (opcode_definitions
[opcode
] == NULL
)
333 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
338 defn
= opcode_definitions
[opcode
];
339 arg
= read_unsigned_leb128 (abfd
, defn
, &bytes_read
);
342 for (i
= 0; i
< arg
; ++i
)
344 mac_ptr
= skip_form_bytes (abfd
, mac_ptr
, mac_end
,
345 (enum dwarf_form
) defn
[i
], offset_size
,
349 /* skip_form_bytes already issued the complaint. */
357 /* A helper function which parses the header of a macro section.
358 If the macro section is the extended (for now called "GNU") type,
359 then this updates *OFFSET_SIZE. Returns a pointer to just after
360 the header, or issues a complaint and returns NULL on error. */
362 static const gdb_byte
*
363 dwarf_parse_macro_header (const gdb_byte
**opcode_definitions
,
365 const gdb_byte
*mac_ptr
,
366 unsigned int *offset_size
,
369 memset (opcode_definitions
, 0, 256 * sizeof (gdb_byte
*));
373 unsigned int version
, flags
;
375 version
= read_2_bytes (abfd
, mac_ptr
);
376 if (version
!= 4 && version
!= 5)
378 complaint (_("unrecognized version `%d' in .debug_macro section"),
384 flags
= read_1_byte (abfd
, mac_ptr
);
386 *offset_size
= (flags
& 1) ? 8 : 4;
388 if ((flags
& 2) != 0)
389 /* We don't need the line table offset. */
390 mac_ptr
+= *offset_size
;
392 /* Vendor opcode descriptions. */
393 if ((flags
& 4) != 0)
395 unsigned int i
, count
;
397 count
= read_1_byte (abfd
, mac_ptr
);
399 for (i
= 0; i
< count
; ++i
)
401 unsigned int opcode
, bytes_read
;
404 opcode
= read_1_byte (abfd
, mac_ptr
);
406 opcode_definitions
[opcode
] = mac_ptr
;
407 arg
= read_unsigned_leb128 (abfd
, mac_ptr
, &bytes_read
);
408 mac_ptr
+= bytes_read
;
417 /* A helper for dwarf_decode_macros that handles the GNU extensions,
418 including DW_MACRO_import. */
421 dwarf_decode_macro_bytes (dwarf2_per_objfile
*per_objfile
,
422 buildsym_compunit
*builder
,
424 const gdb_byte
*mac_ptr
, const gdb_byte
*mac_end
,
425 struct macro_source_file
*current_file
,
426 const struct line_header
*lh
,
427 const struct dwarf2_section_info
*section
,
428 int section_is_gnu
, int section_is_dwz
,
429 unsigned int offset_size
,
432 struct objfile
*objfile
= per_objfile
->objfile
;
433 enum dwarf_macro_record_type macinfo_type
;
435 const gdb_byte
*opcode_definitions
[256];
437 mac_ptr
= dwarf_parse_macro_header (opcode_definitions
, abfd
, mac_ptr
,
438 &offset_size
, section_is_gnu
);
441 /* We already issued a complaint. */
445 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
446 GDB is still reading the definitions from command line. First
447 DW_MACINFO_start_file will need to be ignored as it was already executed
448 to create CURRENT_FILE for the main source holding also the command line
449 definitions. On first met DW_MACINFO_start_file this flag is reset to
450 normally execute all the remaining DW_MACINFO_start_file macinfos. */
456 /* Do we at least have room for a macinfo type byte? */
457 if (mac_ptr
>= mac_end
)
459 section
->overflow_complaint ();
463 macinfo_type
= (enum dwarf_macro_record_type
) read_1_byte (abfd
, mac_ptr
);
466 /* Note that we rely on the fact that the corresponding GNU and
467 DWARF constants are the same. */
469 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
470 switch (macinfo_type
)
472 /* A zero macinfo type indicates the end of the macro
477 case DW_MACRO_define
:
479 case DW_MACRO_define_strp
:
480 case DW_MACRO_undef_strp
:
481 case DW_MACRO_define_sup
:
482 case DW_MACRO_undef_sup
:
484 unsigned int bytes_read
;
489 line
= read_unsigned_leb128 (abfd
, mac_ptr
, &bytes_read
);
490 mac_ptr
+= bytes_read
;
492 if (macinfo_type
== DW_MACRO_define
493 || macinfo_type
== DW_MACRO_undef
)
495 body
= read_direct_string (abfd
, mac_ptr
, &bytes_read
);
496 mac_ptr
+= bytes_read
;
502 str_offset
= read_offset (abfd
, mac_ptr
, offset_size
);
503 mac_ptr
+= offset_size
;
505 if (macinfo_type
== DW_MACRO_define_sup
506 || macinfo_type
== DW_MACRO_undef_sup
509 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
);
511 body
= dwz
->read_string (objfile
, str_offset
);
514 body
= per_objfile
->per_bfd
->str
.read_string (objfile
,
519 is_define
= (macinfo_type
== DW_MACRO_define
520 || macinfo_type
== DW_MACRO_define_strp
521 || macinfo_type
== DW_MACRO_define_sup
);
524 /* DWARF violation as no main source is present. */
525 complaint (_("debug info with no main source gives macro %s "
527 is_define
? _("definition") : _("undefinition"),
531 if ((line
== 0 && !at_commandline
)
532 || (line
!= 0 && at_commandline
))
533 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
534 at_commandline
? _("command-line") : _("in-file"),
535 is_define
? _("definition") : _("undefinition"),
536 line
== 0 ? _("zero") : _("non-zero"), line
, body
);
540 /* Fedora's rpm-build's "debugedit" binary
541 corrupted .debug_macro sections.
544 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
545 complaint (_("debug info gives %s invalid macro %s "
546 "without body (corrupted?) at line %d "
548 at_commandline
? _("command-line") : _("in-file"),
549 is_define
? _("definition") : _("undefinition"),
550 line
, current_file
->filename
);
553 parse_macro_definition (current_file
, line
, body
);
556 gdb_assert (macinfo_type
== DW_MACRO_undef
557 || macinfo_type
== DW_MACRO_undef_strp
558 || macinfo_type
== DW_MACRO_undef_sup
);
559 macro_undef (current_file
, line
, body
);
564 case DW_MACRO_start_file
:
566 unsigned int bytes_read
;
569 line
= read_unsigned_leb128 (abfd
, mac_ptr
, &bytes_read
);
570 mac_ptr
+= bytes_read
;
571 file
= read_unsigned_leb128 (abfd
, mac_ptr
, &bytes_read
);
572 mac_ptr
+= bytes_read
;
574 if ((line
== 0 && !at_commandline
)
575 || (line
!= 0 && at_commandline
))
576 complaint (_("debug info gives source %d included "
577 "from %s at %s line %d"),
578 file
, at_commandline
? _("command-line") : _("file"),
579 line
== 0 ? _("zero") : _("non-zero"), line
);
583 /* This DW_MACRO_start_file was executed in the
588 current_file
= macro_start_file (builder
, file
, line
,
593 case DW_MACRO_end_file
:
595 complaint (_("macro debug info has an unmatched "
596 "`close_file' directive"));
599 current_file
= current_file
->included_by
;
602 enum dwarf_macro_record_type next_type
;
604 /* GCC circa March 2002 doesn't produce the zero
605 type byte marking the end of the compilation
606 unit. Complain if it's not there, but exit no
609 /* Do we at least have room for a macinfo type byte? */
610 if (mac_ptr
>= mac_end
)
612 section
->overflow_complaint ();
616 /* We don't increment mac_ptr here, so this is just
619 = (enum dwarf_macro_record_type
) read_1_byte (abfd
,
622 complaint (_("no terminating 0-type entry for "
623 "macros in `.debug_macinfo' section"));
630 case DW_MACRO_import
:
631 case DW_MACRO_import_sup
:
635 bfd
*include_bfd
= abfd
;
636 const struct dwarf2_section_info
*include_section
= section
;
637 const gdb_byte
*include_mac_end
= mac_end
;
638 int is_dwz
= section_is_dwz
;
639 const gdb_byte
*new_mac_ptr
;
641 offset
= read_offset (abfd
, mac_ptr
, offset_size
);
642 mac_ptr
+= offset_size
;
644 if (macinfo_type
== DW_MACRO_import_sup
)
646 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
);
648 dwz
->macro
.read (objfile
);
650 include_section
= &dwz
->macro
;
651 include_bfd
= include_section
->get_bfd_owner ();
652 include_mac_end
= dwz
->macro
.buffer
+ dwz
->macro
.size
;
656 new_mac_ptr
= include_section
->buffer
+ offset
;
657 slot
= htab_find_slot (include_hash
, new_mac_ptr
, INSERT
);
661 /* This has actually happened; see
662 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
663 complaint (_("recursive DW_MACRO_import in "
664 ".debug_macro section"));
668 *slot
= (void *) new_mac_ptr
;
670 dwarf_decode_macro_bytes (per_objfile
, builder
, include_bfd
,
671 new_mac_ptr
, include_mac_end
,
672 current_file
, lh
, section
,
673 section_is_gnu
, is_dwz
, offset_size
,
676 htab_remove_elt (include_hash
, (void *) new_mac_ptr
);
681 case DW_MACINFO_vendor_ext
:
684 unsigned int bytes_read
;
686 /* This reads the constant, but since we don't recognize
687 any vendor extensions, we ignore it. */
688 read_unsigned_leb128 (abfd
, mac_ptr
, &bytes_read
);
689 mac_ptr
+= bytes_read
;
690 read_direct_string (abfd
, mac_ptr
, &bytes_read
);
691 mac_ptr
+= bytes_read
;
693 /* We don't recognize any vendor extensions. */
699 mac_ptr
= skip_unknown_opcode (macinfo_type
, opcode_definitions
,
700 mac_ptr
, mac_end
, abfd
, offset_size
,
707 } while (macinfo_type
!= 0);
711 dwarf_decode_macros (dwarf2_per_objfile
*per_objfile
,
712 buildsym_compunit
*builder
,
713 const dwarf2_section_info
*section
,
714 const struct line_header
*lh
, unsigned int offset_size
,
715 unsigned int offset
, int section_is_gnu
)
718 const gdb_byte
*mac_ptr
, *mac_end
;
719 struct macro_source_file
*current_file
= 0;
720 enum dwarf_macro_record_type macinfo_type
;
721 const gdb_byte
*opcode_definitions
[256];
724 abfd
= section
->get_bfd_owner ();
726 /* First pass: Find the name of the base filename.
727 This filename is needed in order to process all macros whose definition
728 (or undefinition) comes from the command line. These macros are defined
729 before the first DW_MACINFO_start_file entry, and yet still need to be
730 associated to the base file.
732 To determine the base file name, we scan the macro definitions until we
733 reach the first DW_MACINFO_start_file entry. We then initialize
734 CURRENT_FILE accordingly so that any macro definition found before the
735 first DW_MACINFO_start_file can still be associated to the base file. */
737 mac_ptr
= section
->buffer
+ offset
;
738 mac_end
= section
->buffer
+ section
->size
;
740 mac_ptr
= dwarf_parse_macro_header (opcode_definitions
, abfd
, mac_ptr
,
741 &offset_size
, section_is_gnu
);
744 /* We already issued a complaint. */
750 /* Do we at least have room for a macinfo type byte? */
751 if (mac_ptr
>= mac_end
)
753 /* Complaint is printed during the second pass as GDB will probably
754 stop the first pass earlier upon finding
755 DW_MACINFO_start_file. */
759 macinfo_type
= (enum dwarf_macro_record_type
) read_1_byte (abfd
, mac_ptr
);
762 /* Note that we rely on the fact that the corresponding GNU and
763 DWARF constants are the same. */
765 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
766 switch (macinfo_type
)
768 /* A zero macinfo type indicates the end of the macro
773 case DW_MACRO_define
:
775 /* Only skip the data by MAC_PTR. */
777 unsigned int bytes_read
;
779 read_unsigned_leb128 (abfd
, mac_ptr
, &bytes_read
);
780 mac_ptr
+= bytes_read
;
781 read_direct_string (abfd
, mac_ptr
, &bytes_read
);
782 mac_ptr
+= bytes_read
;
786 case DW_MACRO_start_file
:
788 unsigned int bytes_read
;
791 line
= read_unsigned_leb128 (abfd
, mac_ptr
, &bytes_read
);
792 mac_ptr
+= bytes_read
;
793 file
= read_unsigned_leb128 (abfd
, mac_ptr
, &bytes_read
);
794 mac_ptr
+= bytes_read
;
796 current_file
= macro_start_file (builder
, file
, line
,
801 case DW_MACRO_end_file
:
802 /* No data to skip by MAC_PTR. */
805 case DW_MACRO_define_strp
:
806 case DW_MACRO_undef_strp
:
807 case DW_MACRO_define_sup
:
808 case DW_MACRO_undef_sup
:
810 unsigned int bytes_read
;
812 read_unsigned_leb128 (abfd
, mac_ptr
, &bytes_read
);
813 mac_ptr
+= bytes_read
;
814 mac_ptr
+= offset_size
;
818 case DW_MACRO_import
:
819 case DW_MACRO_import_sup
:
820 /* Note that, according to the spec, a transparent include
821 chain cannot call DW_MACRO_start_file. So, we can just
823 mac_ptr
+= offset_size
;
826 case DW_MACINFO_vendor_ext
:
827 /* Only skip the data by MAC_PTR. */
830 unsigned int bytes_read
;
832 read_unsigned_leb128 (abfd
, mac_ptr
, &bytes_read
);
833 mac_ptr
+= bytes_read
;
834 read_direct_string (abfd
, mac_ptr
, &bytes_read
);
835 mac_ptr
+= bytes_read
;
840 mac_ptr
= skip_unknown_opcode (macinfo_type
, opcode_definitions
,
841 mac_ptr
, mac_end
, abfd
, offset_size
,
848 } while (macinfo_type
!= 0 && current_file
== NULL
);
850 /* Second pass: Process all entries.
852 Use the AT_COMMAND_LINE flag to determine whether we are still processing
853 command-line macro definitions/undefinitions. This flag is unset when we
854 reach the first DW_MACINFO_start_file entry. */
856 htab_up
include_hash (htab_create_alloc (1, htab_hash_pointer
,
858 NULL
, xcalloc
, xfree
));
859 mac_ptr
= section
->buffer
+ offset
;
860 slot
= htab_find_slot (include_hash
.get (), mac_ptr
, INSERT
);
861 *slot
= (void *) mac_ptr
;
862 dwarf_decode_macro_bytes (per_objfile
, builder
, abfd
, mac_ptr
, mac_end
,
863 current_file
, lh
, section
, section_is_gnu
, 0,
864 offset_size
, include_hash
.get ());