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