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