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