xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/dwarf2/macro.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* Read DWARF macro information
27d62b00eSchristos 
3*6881a400Schristos    Copyright (C) 1994-2023 Free Software Foundation, Inc.
47d62b00eSchristos 
57d62b00eSchristos    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
67d62b00eSchristos    Inc.  with support from Florida State University (under contract
77d62b00eSchristos    with the Ada Joint Program Office), and Silicon Graphics, Inc.
87d62b00eSchristos    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
97d62b00eSchristos    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
107d62b00eSchristos    support.
117d62b00eSchristos 
127d62b00eSchristos    This file is part of GDB.
137d62b00eSchristos 
147d62b00eSchristos    This program is free software; you can redistribute it and/or modify
157d62b00eSchristos    it under the terms of the GNU General Public License as published by
167d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
177d62b00eSchristos    (at your option) any later version.
187d62b00eSchristos 
197d62b00eSchristos    This program is distributed in the hope that it will be useful,
207d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
217d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
227d62b00eSchristos    GNU General Public License for more details.
237d62b00eSchristos 
247d62b00eSchristos    You should have received a copy of the GNU General Public License
257d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
267d62b00eSchristos 
277d62b00eSchristos #include "defs.h"
287d62b00eSchristos #include "dwarf2/read.h"
297d62b00eSchristos #include "dwarf2/leb.h"
307d62b00eSchristos #include "dwarf2/expr.h"
317d62b00eSchristos #include "dwarf2/line-header.h"
327d62b00eSchristos #include "dwarf2/section.h"
337d62b00eSchristos #include "dwarf2/macro.h"
347d62b00eSchristos #include "dwarf2/dwz.h"
357d62b00eSchristos #include "buildsym.h"
367d62b00eSchristos #include "macrotab.h"
377d62b00eSchristos #include "complaints.h"
38*6881a400Schristos #include "objfiles.h"
397d62b00eSchristos 
407d62b00eSchristos static void
417d62b00eSchristos dwarf2_macro_malformed_definition_complaint (const char *arg1)
427d62b00eSchristos {
437d62b00eSchristos   complaint (_("macro debug info contains a "
447d62b00eSchristos 	       "malformed macro definition:\n`%s'"),
457d62b00eSchristos 	     arg1);
467d62b00eSchristos }
477d62b00eSchristos 
487d62b00eSchristos static struct macro_source_file *
497d62b00eSchristos macro_start_file (buildsym_compunit *builder,
507d62b00eSchristos 		  int file, int line,
517d62b00eSchristos 		  struct macro_source_file *current_file,
527d62b00eSchristos 		  const struct line_header *lh)
537d62b00eSchristos {
547d62b00eSchristos   /* File name relative to the compilation directory of this source file.  */
55*6881a400Schristos   const file_entry *fe = lh->file_name_at (file);
56*6881a400Schristos   std::string file_name;
57*6881a400Schristos 
58*6881a400Schristos   if (fe != nullptr)
59*6881a400Schristos     file_name = lh->file_file_name (*fe);
60*6881a400Schristos   else
61*6881a400Schristos     {
62*6881a400Schristos       /* The compiler produced a bogus file number.  We can at least
63*6881a400Schristos 	 record the macro definitions made in the file, even if we
64*6881a400Schristos 	 won't be able to find the file by name.  */
65*6881a400Schristos       complaint (_("bad file number in macro information (%d)"),
66*6881a400Schristos 		 file);
67*6881a400Schristos 
68*6881a400Schristos       file_name = string_printf ("<bad macro file number %d>", file);
69*6881a400Schristos     }
707d62b00eSchristos 
717d62b00eSchristos   if (! current_file)
727d62b00eSchristos     {
737d62b00eSchristos       /* Note: We don't create a macro table for this compilation unit
747d62b00eSchristos 	 at all until we actually get a filename.  */
757d62b00eSchristos       struct macro_table *macro_table = builder->get_macro_table ();
767d62b00eSchristos 
777d62b00eSchristos       /* If we have no current file, then this must be the start_file
787d62b00eSchristos 	 directive for the compilation unit's main source file.  */
79*6881a400Schristos       current_file = macro_set_main (macro_table, file_name.c_str ());
807d62b00eSchristos       macro_define_special (macro_table);
817d62b00eSchristos     }
827d62b00eSchristos   else
83*6881a400Schristos     current_file = macro_include (current_file, line, file_name.c_str ());
847d62b00eSchristos 
857d62b00eSchristos   return current_file;
867d62b00eSchristos }
877d62b00eSchristos 
887d62b00eSchristos static const char *
897d62b00eSchristos consume_improper_spaces (const char *p, const char *body)
907d62b00eSchristos {
917d62b00eSchristos   if (*p == ' ')
927d62b00eSchristos     {
937d62b00eSchristos       complaint (_("macro definition contains spaces "
947d62b00eSchristos 		   "in formal argument list:\n`%s'"),
957d62b00eSchristos 		 body);
967d62b00eSchristos 
977d62b00eSchristos       while (*p == ' ')
987d62b00eSchristos 	p++;
997d62b00eSchristos     }
1007d62b00eSchristos 
1017d62b00eSchristos   return p;
1027d62b00eSchristos }
1037d62b00eSchristos 
1047d62b00eSchristos 
1057d62b00eSchristos static void
1067d62b00eSchristos parse_macro_definition (struct macro_source_file *file, int line,
1077d62b00eSchristos 			const char *body)
1087d62b00eSchristos {
1097d62b00eSchristos   const char *p;
1107d62b00eSchristos 
1117d62b00eSchristos   /* The body string takes one of two forms.  For object-like macro
1127d62b00eSchristos      definitions, it should be:
1137d62b00eSchristos 
1147d62b00eSchristos 	<macro name> " " <definition>
1157d62b00eSchristos 
1167d62b00eSchristos      For function-like macro definitions, it should be:
1177d62b00eSchristos 
1187d62b00eSchristos 	<macro name> "() " <definition>
1197d62b00eSchristos      or
1207d62b00eSchristos 	<macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
1217d62b00eSchristos 
1227d62b00eSchristos      Spaces may appear only where explicitly indicated, and in the
1237d62b00eSchristos      <definition>.
1247d62b00eSchristos 
1257d62b00eSchristos      The Dwarf 2 spec says that an object-like macro's name is always
1267d62b00eSchristos      followed by a space, but versions of GCC around March 2002 omit
1277d62b00eSchristos      the space when the macro's definition is the empty string.
1287d62b00eSchristos 
1297d62b00eSchristos      The Dwarf 2 spec says that there should be no spaces between the
1307d62b00eSchristos      formal arguments in a function-like macro's formal argument list,
1317d62b00eSchristos      but versions of GCC around March 2002 include spaces after the
1327d62b00eSchristos      commas.  */
1337d62b00eSchristos 
1347d62b00eSchristos 
1357d62b00eSchristos   /* Find the extent of the macro name.  The macro name is terminated
1367d62b00eSchristos      by either a space or null character (for an object-like macro) or
1377d62b00eSchristos      an opening paren (for a function-like macro).  */
1387d62b00eSchristos   for (p = body; *p; p++)
1397d62b00eSchristos     if (*p == ' ' || *p == '(')
1407d62b00eSchristos       break;
1417d62b00eSchristos 
1427d62b00eSchristos   if (*p == ' ' || *p == '\0')
1437d62b00eSchristos     {
1447d62b00eSchristos       /* It's an object-like macro.  */
1457d62b00eSchristos       int name_len = p - body;
1467d62b00eSchristos       std::string name (body, name_len);
1477d62b00eSchristos       const char *replacement;
1487d62b00eSchristos 
1497d62b00eSchristos       if (*p == ' ')
1507d62b00eSchristos 	replacement = body + name_len + 1;
1517d62b00eSchristos       else
1527d62b00eSchristos 	{
1537d62b00eSchristos 	  dwarf2_macro_malformed_definition_complaint (body);
1547d62b00eSchristos 	  replacement = body + name_len;
1557d62b00eSchristos 	}
1567d62b00eSchristos 
1577d62b00eSchristos       macro_define_object (file, line, name.c_str (), replacement);
1587d62b00eSchristos     }
1597d62b00eSchristos   else if (*p == '(')
1607d62b00eSchristos     {
1617d62b00eSchristos       /* It's a function-like macro.  */
1627d62b00eSchristos       std::string name (body, p - body);
1637d62b00eSchristos       int argc = 0;
1647d62b00eSchristos       int argv_size = 1;
1657d62b00eSchristos       char **argv = XNEWVEC (char *, argv_size);
1667d62b00eSchristos 
1677d62b00eSchristos       p++;
1687d62b00eSchristos 
1697d62b00eSchristos       p = consume_improper_spaces (p, body);
1707d62b00eSchristos 
1717d62b00eSchristos       /* Parse the formal argument list.  */
1727d62b00eSchristos       while (*p && *p != ')')
1737d62b00eSchristos 	{
1747d62b00eSchristos 	  /* Find the extent of the current argument name.  */
1757d62b00eSchristos 	  const char *arg_start = p;
1767d62b00eSchristos 
1777d62b00eSchristos 	  while (*p && *p != ',' && *p != ')' && *p != ' ')
1787d62b00eSchristos 	    p++;
1797d62b00eSchristos 
1807d62b00eSchristos 	  if (! *p || p == arg_start)
1817d62b00eSchristos 	    dwarf2_macro_malformed_definition_complaint (body);
1827d62b00eSchristos 	  else
1837d62b00eSchristos 	    {
1847d62b00eSchristos 	      /* Make sure argv has room for the new argument.  */
1857d62b00eSchristos 	      if (argc >= argv_size)
1867d62b00eSchristos 		{
1877d62b00eSchristos 		  argv_size *= 2;
1887d62b00eSchristos 		  argv = XRESIZEVEC (char *, argv, argv_size);
1897d62b00eSchristos 		}
1907d62b00eSchristos 
1917d62b00eSchristos 	      argv[argc++] = savestring (arg_start, p - arg_start);
1927d62b00eSchristos 	    }
1937d62b00eSchristos 
1947d62b00eSchristos 	  p = consume_improper_spaces (p, body);
1957d62b00eSchristos 
1967d62b00eSchristos 	  /* Consume the comma, if present.  */
1977d62b00eSchristos 	  if (*p == ',')
1987d62b00eSchristos 	    {
1997d62b00eSchristos 	      p++;
2007d62b00eSchristos 
2017d62b00eSchristos 	      p = consume_improper_spaces (p, body);
2027d62b00eSchristos 	    }
2037d62b00eSchristos 	}
2047d62b00eSchristos 
2057d62b00eSchristos       if (*p == ')')
2067d62b00eSchristos 	{
2077d62b00eSchristos 	  p++;
2087d62b00eSchristos 
2097d62b00eSchristos 	  if (*p == ' ')
2107d62b00eSchristos 	    /* Perfectly formed definition, no complaints.  */
2117d62b00eSchristos 	    macro_define_function (file, line, name.c_str (),
2127d62b00eSchristos 				   argc, (const char **) argv,
2137d62b00eSchristos 				   p + 1);
2147d62b00eSchristos 	  else if (*p == '\0')
2157d62b00eSchristos 	    {
2167d62b00eSchristos 	      /* Complain, but do define it.  */
2177d62b00eSchristos 	      dwarf2_macro_malformed_definition_complaint (body);
2187d62b00eSchristos 	      macro_define_function (file, line, name.c_str (),
2197d62b00eSchristos 				     argc, (const char **) argv,
2207d62b00eSchristos 				     p);
2217d62b00eSchristos 	    }
2227d62b00eSchristos 	  else
2237d62b00eSchristos 	    /* Just complain.  */
2247d62b00eSchristos 	    dwarf2_macro_malformed_definition_complaint (body);
2257d62b00eSchristos 	}
2267d62b00eSchristos       else
2277d62b00eSchristos 	/* Just complain.  */
2287d62b00eSchristos 	dwarf2_macro_malformed_definition_complaint (body);
2297d62b00eSchristos 
2307d62b00eSchristos       {
2317d62b00eSchristos 	int i;
2327d62b00eSchristos 
2337d62b00eSchristos 	for (i = 0; i < argc; i++)
2347d62b00eSchristos 	  xfree (argv[i]);
2357d62b00eSchristos       }
2367d62b00eSchristos       xfree (argv);
2377d62b00eSchristos     }
2387d62b00eSchristos   else
2397d62b00eSchristos     dwarf2_macro_malformed_definition_complaint (body);
2407d62b00eSchristos }
2417d62b00eSchristos 
2427d62b00eSchristos /* Skip some bytes from BYTES according to the form given in FORM.
2437d62b00eSchristos    Returns the new pointer.  */
2447d62b00eSchristos 
2457d62b00eSchristos static const gdb_byte *
2467d62b00eSchristos skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
2477d62b00eSchristos 		 enum dwarf_form form,
2487d62b00eSchristos 		 unsigned int offset_size,
2497d62b00eSchristos 		 const struct dwarf2_section_info *section)
2507d62b00eSchristos {
2517d62b00eSchristos   unsigned int bytes_read;
2527d62b00eSchristos 
2537d62b00eSchristos   switch (form)
2547d62b00eSchristos     {
2557d62b00eSchristos     case DW_FORM_data1:
2567d62b00eSchristos     case DW_FORM_flag:
2577d62b00eSchristos       ++bytes;
2587d62b00eSchristos       break;
2597d62b00eSchristos 
2607d62b00eSchristos     case DW_FORM_data2:
2617d62b00eSchristos       bytes += 2;
2627d62b00eSchristos       break;
2637d62b00eSchristos 
2647d62b00eSchristos     case DW_FORM_data4:
2657d62b00eSchristos       bytes += 4;
2667d62b00eSchristos       break;
2677d62b00eSchristos 
2687d62b00eSchristos     case DW_FORM_data8:
2697d62b00eSchristos       bytes += 8;
2707d62b00eSchristos       break;
2717d62b00eSchristos 
2727d62b00eSchristos     case DW_FORM_data16:
2737d62b00eSchristos       bytes += 16;
2747d62b00eSchristos       break;
2757d62b00eSchristos 
2767d62b00eSchristos     case DW_FORM_string:
2777d62b00eSchristos       read_direct_string (abfd, bytes, &bytes_read);
2787d62b00eSchristos       bytes += bytes_read;
2797d62b00eSchristos       break;
2807d62b00eSchristos 
2817d62b00eSchristos     case DW_FORM_sec_offset:
2827d62b00eSchristos     case DW_FORM_strp:
2837d62b00eSchristos     case DW_FORM_GNU_strp_alt:
2847d62b00eSchristos       bytes += offset_size;
2857d62b00eSchristos       break;
2867d62b00eSchristos 
2877d62b00eSchristos     case DW_FORM_block:
2887d62b00eSchristos       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
2897d62b00eSchristos       bytes += bytes_read;
2907d62b00eSchristos       break;
2917d62b00eSchristos 
2927d62b00eSchristos     case DW_FORM_block1:
2937d62b00eSchristos       bytes += 1 + read_1_byte (abfd, bytes);
2947d62b00eSchristos       break;
2957d62b00eSchristos     case DW_FORM_block2:
2967d62b00eSchristos       bytes += 2 + read_2_bytes (abfd, bytes);
2977d62b00eSchristos       break;
2987d62b00eSchristos     case DW_FORM_block4:
2997d62b00eSchristos       bytes += 4 + read_4_bytes (abfd, bytes);
3007d62b00eSchristos       break;
3017d62b00eSchristos 
3027d62b00eSchristos     case DW_FORM_addrx:
3037d62b00eSchristos     case DW_FORM_sdata:
3047d62b00eSchristos     case DW_FORM_strx:
3057d62b00eSchristos     case DW_FORM_udata:
3067d62b00eSchristos     case DW_FORM_GNU_addr_index:
3077d62b00eSchristos     case DW_FORM_GNU_str_index:
3087d62b00eSchristos       bytes = gdb_skip_leb128 (bytes, buffer_end);
3097d62b00eSchristos       if (bytes == NULL)
3107d62b00eSchristos 	{
3117d62b00eSchristos 	  section->overflow_complaint ();
3127d62b00eSchristos 	  return NULL;
3137d62b00eSchristos 	}
3147d62b00eSchristos       break;
3157d62b00eSchristos 
3167d62b00eSchristos     case DW_FORM_implicit_const:
3177d62b00eSchristos       break;
3187d62b00eSchristos 
3197d62b00eSchristos     default:
3207d62b00eSchristos       {
3217d62b00eSchristos 	complaint (_("invalid form 0x%x in `%s'"),
3227d62b00eSchristos 		   form, section->get_name ());
3237d62b00eSchristos 	return NULL;
3247d62b00eSchristos       }
3257d62b00eSchristos     }
3267d62b00eSchristos 
3277d62b00eSchristos   return bytes;
3287d62b00eSchristos }
3297d62b00eSchristos 
3307d62b00eSchristos /* A helper for dwarf_decode_macros that handles skipping an unknown
3317d62b00eSchristos    opcode.  Returns an updated pointer to the macro data buffer; or,
3327d62b00eSchristos    on error, issues a complaint and returns NULL.  */
3337d62b00eSchristos 
3347d62b00eSchristos static const gdb_byte *
3357d62b00eSchristos skip_unknown_opcode (unsigned int opcode,
3367d62b00eSchristos 		     const gdb_byte **opcode_definitions,
3377d62b00eSchristos 		     const gdb_byte *mac_ptr, const gdb_byte *mac_end,
3387d62b00eSchristos 		     bfd *abfd,
3397d62b00eSchristos 		     unsigned int offset_size,
3407d62b00eSchristos 		     const struct dwarf2_section_info *section)
3417d62b00eSchristos {
3427d62b00eSchristos   unsigned int bytes_read, i;
3437d62b00eSchristos   unsigned long arg;
3447d62b00eSchristos   const gdb_byte *defn;
3457d62b00eSchristos 
3467d62b00eSchristos   if (opcode_definitions[opcode] == NULL)
3477d62b00eSchristos     {
348*6881a400Schristos       complaint (_("unrecognized DW_MACINFO or DW_MACRO opcode 0x%x"),
3497d62b00eSchristos 		 opcode);
3507d62b00eSchristos       return NULL;
3517d62b00eSchristos     }
3527d62b00eSchristos 
3537d62b00eSchristos   defn = opcode_definitions[opcode];
3547d62b00eSchristos   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
3557d62b00eSchristos   defn += bytes_read;
3567d62b00eSchristos 
3577d62b00eSchristos   for (i = 0; i < arg; ++i)
3587d62b00eSchristos     {
3597d62b00eSchristos       mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
3607d62b00eSchristos 				 (enum dwarf_form) defn[i], offset_size,
3617d62b00eSchristos 				 section);
3627d62b00eSchristos       if (mac_ptr == NULL)
3637d62b00eSchristos 	{
3647d62b00eSchristos 	  /* skip_form_bytes already issued the complaint.  */
3657d62b00eSchristos 	  return NULL;
3667d62b00eSchristos 	}
3677d62b00eSchristos     }
3687d62b00eSchristos 
3697d62b00eSchristos   return mac_ptr;
3707d62b00eSchristos }
3717d62b00eSchristos 
3727d62b00eSchristos /* A helper function which parses the header of a macro section.
3737d62b00eSchristos    If the macro section is the extended (for now called "GNU") type,
3747d62b00eSchristos    then this updates *OFFSET_SIZE.  Returns a pointer to just after
3757d62b00eSchristos    the header, or issues a complaint and returns NULL on error.  */
3767d62b00eSchristos 
3777d62b00eSchristos static const gdb_byte *
3787d62b00eSchristos dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
3797d62b00eSchristos 			  bfd *abfd,
3807d62b00eSchristos 			  const gdb_byte *mac_ptr,
3817d62b00eSchristos 			  unsigned int *offset_size,
3827d62b00eSchristos 			  int section_is_gnu)
3837d62b00eSchristos {
3847d62b00eSchristos   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
3857d62b00eSchristos 
3867d62b00eSchristos   if (section_is_gnu)
3877d62b00eSchristos     {
3887d62b00eSchristos       unsigned int version, flags;
3897d62b00eSchristos 
3907d62b00eSchristos       version = read_2_bytes (abfd, mac_ptr);
3917d62b00eSchristos       if (version != 4 && version != 5)
3927d62b00eSchristos 	{
3937d62b00eSchristos 	  complaint (_("unrecognized version `%d' in .debug_macro section"),
3947d62b00eSchristos 		     version);
3957d62b00eSchristos 	  return NULL;
3967d62b00eSchristos 	}
3977d62b00eSchristos       mac_ptr += 2;
3987d62b00eSchristos 
3997d62b00eSchristos       flags = read_1_byte (abfd, mac_ptr);
4007d62b00eSchristos       ++mac_ptr;
4017d62b00eSchristos       *offset_size = (flags & 1) ? 8 : 4;
4027d62b00eSchristos 
4037d62b00eSchristos       if ((flags & 2) != 0)
4047d62b00eSchristos 	/* We don't need the line table offset.  */
4057d62b00eSchristos 	mac_ptr += *offset_size;
4067d62b00eSchristos 
4077d62b00eSchristos       /* Vendor opcode descriptions.  */
4087d62b00eSchristos       if ((flags & 4) != 0)
4097d62b00eSchristos 	{
4107d62b00eSchristos 	  unsigned int i, count;
4117d62b00eSchristos 
4127d62b00eSchristos 	  count = read_1_byte (abfd, mac_ptr);
4137d62b00eSchristos 	  ++mac_ptr;
4147d62b00eSchristos 	  for (i = 0; i < count; ++i)
4157d62b00eSchristos 	    {
4167d62b00eSchristos 	      unsigned int opcode, bytes_read;
4177d62b00eSchristos 	      unsigned long arg;
4187d62b00eSchristos 
4197d62b00eSchristos 	      opcode = read_1_byte (abfd, mac_ptr);
4207d62b00eSchristos 	      ++mac_ptr;
4217d62b00eSchristos 	      opcode_definitions[opcode] = mac_ptr;
4227d62b00eSchristos 	      arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
4237d62b00eSchristos 	      mac_ptr += bytes_read;
4247d62b00eSchristos 	      mac_ptr += arg;
4257d62b00eSchristos 	    }
4267d62b00eSchristos 	}
4277d62b00eSchristos     }
4287d62b00eSchristos 
4297d62b00eSchristos   return mac_ptr;
4307d62b00eSchristos }
4317d62b00eSchristos 
4327d62b00eSchristos /* A helper for dwarf_decode_macros that handles the GNU extensions,
4337d62b00eSchristos    including DW_MACRO_import.  */
4347d62b00eSchristos 
4357d62b00eSchristos static void
4367d62b00eSchristos dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
4377d62b00eSchristos 			  buildsym_compunit *builder,
4387d62b00eSchristos 			  bfd *abfd,
4397d62b00eSchristos 			  const gdb_byte *mac_ptr, const gdb_byte *mac_end,
4407d62b00eSchristos 			  struct macro_source_file *current_file,
4417d62b00eSchristos 			  const struct line_header *lh,
4427d62b00eSchristos 			  const struct dwarf2_section_info *section,
4437d62b00eSchristos 			  int section_is_gnu, int section_is_dwz,
4447d62b00eSchristos 			  unsigned int offset_size,
445*6881a400Schristos 			  struct dwarf2_section_info *str_section,
446*6881a400Schristos 			  struct dwarf2_section_info *str_offsets_section,
447*6881a400Schristos 			  gdb::optional<ULONGEST> str_offsets_base,
448*6881a400Schristos 			  htab_t include_hash, struct dwarf2_cu *cu)
4497d62b00eSchristos {
4507d62b00eSchristos   struct objfile *objfile = per_objfile->objfile;
4517d62b00eSchristos   enum dwarf_macro_record_type macinfo_type;
4527d62b00eSchristos   int at_commandline;
4537d62b00eSchristos   const gdb_byte *opcode_definitions[256];
4547d62b00eSchristos 
4557d62b00eSchristos   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
4567d62b00eSchristos 				      &offset_size, section_is_gnu);
4577d62b00eSchristos   if (mac_ptr == NULL)
4587d62b00eSchristos     {
4597d62b00eSchristos       /* We already issued a complaint.  */
4607d62b00eSchristos       return;
4617d62b00eSchristos     }
4627d62b00eSchristos 
4637d62b00eSchristos   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
4647d62b00eSchristos      GDB is still reading the definitions from command line.  First
4657d62b00eSchristos      DW_MACINFO_start_file will need to be ignored as it was already executed
4667d62b00eSchristos      to create CURRENT_FILE for the main source holding also the command line
4677d62b00eSchristos      definitions.  On first met DW_MACINFO_start_file this flag is reset to
4687d62b00eSchristos      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
4697d62b00eSchristos 
4707d62b00eSchristos   at_commandline = 1;
4717d62b00eSchristos 
4727d62b00eSchristos   do
4737d62b00eSchristos     {
4747d62b00eSchristos       /* Do we at least have room for a macinfo type byte?  */
4757d62b00eSchristos       if (mac_ptr >= mac_end)
4767d62b00eSchristos 	{
4777d62b00eSchristos 	  section->overflow_complaint ();
4787d62b00eSchristos 	  break;
4797d62b00eSchristos 	}
4807d62b00eSchristos 
4817d62b00eSchristos       macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
4827d62b00eSchristos       mac_ptr++;
4837d62b00eSchristos 
4847d62b00eSchristos       /* Note that we rely on the fact that the corresponding GNU and
4857d62b00eSchristos 	 DWARF constants are the same.  */
4867d62b00eSchristos       DIAGNOSTIC_PUSH
4877d62b00eSchristos       DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
4887d62b00eSchristos       switch (macinfo_type)
4897d62b00eSchristos 	{
4907d62b00eSchristos 	  /* A zero macinfo type indicates the end of the macro
4917d62b00eSchristos 	     information.  */
4927d62b00eSchristos 	case 0:
4937d62b00eSchristos 	  break;
4947d62b00eSchristos 
4957d62b00eSchristos 	case DW_MACRO_define:
4967d62b00eSchristos 	case DW_MACRO_undef:
4977d62b00eSchristos 	case DW_MACRO_define_strp:
4987d62b00eSchristos 	case DW_MACRO_undef_strp:
4997d62b00eSchristos 	case DW_MACRO_define_sup:
5007d62b00eSchristos 	case DW_MACRO_undef_sup:
5017d62b00eSchristos 	  {
5027d62b00eSchristos 	    unsigned int bytes_read;
5037d62b00eSchristos 	    int line;
5047d62b00eSchristos 	    const char *body;
5057d62b00eSchristos 	    int is_define;
5067d62b00eSchristos 
5077d62b00eSchristos 	    line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
5087d62b00eSchristos 	    mac_ptr += bytes_read;
5097d62b00eSchristos 
5107d62b00eSchristos 	    if (macinfo_type == DW_MACRO_define
5117d62b00eSchristos 		|| macinfo_type == DW_MACRO_undef)
5127d62b00eSchristos 	      {
5137d62b00eSchristos 		body = read_direct_string (abfd, mac_ptr, &bytes_read);
5147d62b00eSchristos 		mac_ptr += bytes_read;
5157d62b00eSchristos 	      }
5167d62b00eSchristos 	    else
5177d62b00eSchristos 	      {
5187d62b00eSchristos 		LONGEST str_offset;
5197d62b00eSchristos 
5207d62b00eSchristos 		str_offset = read_offset (abfd, mac_ptr, offset_size);
5217d62b00eSchristos 		mac_ptr += offset_size;
5227d62b00eSchristos 
5237d62b00eSchristos 		if (macinfo_type == DW_MACRO_define_sup
5247d62b00eSchristos 		    || macinfo_type == DW_MACRO_undef_sup
5257d62b00eSchristos 		    || section_is_dwz)
5267d62b00eSchristos 		  {
527*6881a400Schristos 		    dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd,
528*6881a400Schristos 							 true);
5297d62b00eSchristos 
5307d62b00eSchristos 		    body = dwz->read_string (objfile, str_offset);
5317d62b00eSchristos 		  }
5327d62b00eSchristos 		else
5337d62b00eSchristos 		  body = per_objfile->per_bfd->str.read_string (objfile,
5347d62b00eSchristos 								str_offset,
5357d62b00eSchristos 								"DW_FORM_strp");
5367d62b00eSchristos 	      }
5377d62b00eSchristos 
5387d62b00eSchristos 	    is_define = (macinfo_type == DW_MACRO_define
5397d62b00eSchristos 			 || macinfo_type == DW_MACRO_define_strp
5407d62b00eSchristos 			 || macinfo_type == DW_MACRO_define_sup);
5417d62b00eSchristos 	    if (! current_file)
5427d62b00eSchristos 	      {
5437d62b00eSchristos 		/* DWARF violation as no main source is present.  */
5447d62b00eSchristos 		complaint (_("debug info with no main source gives macro %s "
5457d62b00eSchristos 			     "on line %d: %s"),
5467d62b00eSchristos 			   is_define ? _("definition") : _("undefinition"),
5477d62b00eSchristos 			   line, body);
5487d62b00eSchristos 		break;
5497d62b00eSchristos 	      }
5507d62b00eSchristos 	    if ((line == 0 && !at_commandline)
5517d62b00eSchristos 		|| (line != 0 && at_commandline))
5527d62b00eSchristos 	      complaint (_("debug info gives %s macro %s with %s line %d: %s"),
5537d62b00eSchristos 			 at_commandline ? _("command-line") : _("in-file"),
5547d62b00eSchristos 			 is_define ? _("definition") : _("undefinition"),
5557d62b00eSchristos 			 line == 0 ? _("zero") : _("non-zero"), line, body);
5567d62b00eSchristos 
5577d62b00eSchristos 	    if (body == NULL)
5587d62b00eSchristos 	      {
5597d62b00eSchristos 		/* Fedora's rpm-build's "debugedit" binary
5607d62b00eSchristos 		   corrupted .debug_macro sections.
5617d62b00eSchristos 
5627d62b00eSchristos 		   For more info, see
5637d62b00eSchristos 		   https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
5647d62b00eSchristos 		complaint (_("debug info gives %s invalid macro %s "
5657d62b00eSchristos 			     "without body (corrupted?) at line %d "
5667d62b00eSchristos 			     "on file %s"),
5677d62b00eSchristos 			   at_commandline ? _("command-line") : _("in-file"),
5687d62b00eSchristos 			   is_define ? _("definition") : _("undefinition"),
5697d62b00eSchristos 			   line, current_file->filename);
5707d62b00eSchristos 	      }
5717d62b00eSchristos 	    else if (is_define)
5727d62b00eSchristos 	      parse_macro_definition (current_file, line, body);
5737d62b00eSchristos 	    else
5747d62b00eSchristos 	      {
5757d62b00eSchristos 		gdb_assert (macinfo_type == DW_MACRO_undef
5767d62b00eSchristos 			    || macinfo_type == DW_MACRO_undef_strp
5777d62b00eSchristos 			    || macinfo_type == DW_MACRO_undef_sup);
5787d62b00eSchristos 		macro_undef (current_file, line, body);
5797d62b00eSchristos 	      }
5807d62b00eSchristos 	  }
5817d62b00eSchristos 	  break;
5827d62b00eSchristos 
583*6881a400Schristos 	case DW_MACRO_define_strx:
584*6881a400Schristos 	case DW_MACRO_undef_strx:
585*6881a400Schristos 	  {
586*6881a400Schristos 	    unsigned int bytes_read;
587*6881a400Schristos 
588*6881a400Schristos 	    int line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
589*6881a400Schristos 	    mac_ptr += bytes_read;
590*6881a400Schristos 	    int offset_index = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
591*6881a400Schristos 	    mac_ptr += bytes_read;
592*6881a400Schristos 
593*6881a400Schristos 	    /* Use of the strx operators requires a DW_AT_str_offsets_base.  */
594*6881a400Schristos 	    if (!str_offsets_base.has_value ())
595*6881a400Schristos 	      {
596*6881a400Schristos 		complaint (_("use of %s with unknown string offsets base "
597*6881a400Schristos 			     "[in module %s]"),
598*6881a400Schristos 			   (macinfo_type == DW_MACRO_define_strx
599*6881a400Schristos 			    ? "DW_MACRO_define_strx"
600*6881a400Schristos 			    : "DW_MACRO_undef_strx"),
601*6881a400Schristos 			   objfile_name (objfile));
602*6881a400Schristos 		break;
603*6881a400Schristos 	      }
604*6881a400Schristos 
605*6881a400Schristos 	    str_offsets_section->read (objfile);
606*6881a400Schristos 	    const gdb_byte *info_ptr = (str_offsets_section->buffer
607*6881a400Schristos 					+ *str_offsets_base
608*6881a400Schristos 					+ offset_index * offset_size);
609*6881a400Schristos 
610*6881a400Schristos 	    const char *macinfo_str = (macinfo_type == DW_MACRO_define_strx ?
611*6881a400Schristos 				       "DW_MACRO_define_strx" : "DW_MACRO_undef_strx");
612*6881a400Schristos 
613*6881a400Schristos 	    if (*str_offsets_base + offset_index * offset_size
614*6881a400Schristos 		>= str_offsets_section->size)
615*6881a400Schristos 	      {
616*6881a400Schristos 		complaint (_("%s pointing outside of .debug_str_offsets section "
617*6881a400Schristos 			     "[in module %s]"), macinfo_str, objfile_name (objfile));
618*6881a400Schristos 		break;
619*6881a400Schristos 	      }
620*6881a400Schristos 
621*6881a400Schristos 	    ULONGEST str_offset = read_offset (abfd, info_ptr, offset_size);
622*6881a400Schristos 
623*6881a400Schristos 	    const char *body = str_section->read_string (objfile, str_offset,
624*6881a400Schristos 							 macinfo_str);
625*6881a400Schristos 	    if (current_file == nullptr)
626*6881a400Schristos 	      {
627*6881a400Schristos 		/* DWARF violation as no main source is present.  */
628*6881a400Schristos 		complaint (_("debug info with no main source gives macro %s "
629*6881a400Schristos 			     "on line %d: %s"),
630*6881a400Schristos 			     macinfo_type == DW_MACRO_define_strx ? _("definition")
631*6881a400Schristos 			     : _("undefinition"), line, body);
632*6881a400Schristos 		break;
633*6881a400Schristos 	      }
634*6881a400Schristos 
635*6881a400Schristos 	    if (macinfo_type == DW_MACRO_define_strx)
636*6881a400Schristos 	      parse_macro_definition (current_file, line, body);
637*6881a400Schristos 	    else
638*6881a400Schristos 	      macro_undef (current_file, line, body);
639*6881a400Schristos 	   }
640*6881a400Schristos 	   break;
641*6881a400Schristos 
6427d62b00eSchristos 	case DW_MACRO_start_file:
6437d62b00eSchristos 	  {
6447d62b00eSchristos 	    unsigned int bytes_read;
6457d62b00eSchristos 	    int line, file;
6467d62b00eSchristos 
6477d62b00eSchristos 	    line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
6487d62b00eSchristos 	    mac_ptr += bytes_read;
6497d62b00eSchristos 	    file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
6507d62b00eSchristos 	    mac_ptr += bytes_read;
6517d62b00eSchristos 
6527d62b00eSchristos 	    if ((line == 0 && !at_commandline)
6537d62b00eSchristos 		|| (line != 0 && at_commandline))
6547d62b00eSchristos 	      complaint (_("debug info gives source %d included "
6557d62b00eSchristos 			   "from %s at %s line %d"),
6567d62b00eSchristos 			 file, at_commandline ? _("command-line") : _("file"),
6577d62b00eSchristos 			 line == 0 ? _("zero") : _("non-zero"), line);
6587d62b00eSchristos 
6597d62b00eSchristos 	    if (at_commandline)
6607d62b00eSchristos 	      {
6617d62b00eSchristos 		/* This DW_MACRO_start_file was executed in the
6627d62b00eSchristos 		   pass one.  */
6637d62b00eSchristos 		at_commandline = 0;
6647d62b00eSchristos 	      }
6657d62b00eSchristos 	    else
6667d62b00eSchristos 	      current_file = macro_start_file (builder, file, line,
6677d62b00eSchristos 					       current_file, lh);
6687d62b00eSchristos 	  }
6697d62b00eSchristos 	  break;
6707d62b00eSchristos 
6717d62b00eSchristos 	case DW_MACRO_end_file:
6727d62b00eSchristos 	  if (! current_file)
6737d62b00eSchristos 	    complaint (_("macro debug info has an unmatched "
6747d62b00eSchristos 			 "`close_file' directive"));
675*6881a400Schristos 	  else if (current_file->included_by == nullptr
676*6881a400Schristos 		   && producer_is_clang (cu))
677*6881a400Schristos 	    {
678*6881a400Schristos 	      /* Clang, until the current version, misplaces some macro
679*6881a400Schristos 		 definitions - such as ones defined in the command line,
680*6881a400Schristos 		 putting them after the last DW_MACRO_end_file instead of
681*6881a400Schristos 		 before the first DW_MACRO_start_file.  Since at the time
682*6881a400Schristos 		 of writing there is no clang version with this bug fixed,
683*6881a400Schristos 		 we check for any clang producer.  This should be changed
684*6881a400Schristos 		 to producer_is_clang_lt_XX when possible. */
685*6881a400Schristos 	    }
6867d62b00eSchristos 	  else
6877d62b00eSchristos 	    {
6887d62b00eSchristos 	      current_file = current_file->included_by;
6897d62b00eSchristos 	      if (! current_file)
6907d62b00eSchristos 		{
6917d62b00eSchristos 		  enum dwarf_macro_record_type next_type;
6927d62b00eSchristos 
6937d62b00eSchristos 		  /* GCC circa March 2002 doesn't produce the zero
6947d62b00eSchristos 		     type byte marking the end of the compilation
6957d62b00eSchristos 		     unit.  Complain if it's not there, but exit no
6967d62b00eSchristos 		     matter what.  */
6977d62b00eSchristos 
6987d62b00eSchristos 		  /* Do we at least have room for a macinfo type byte?  */
6997d62b00eSchristos 		  if (mac_ptr >= mac_end)
7007d62b00eSchristos 		    {
7017d62b00eSchristos 		      section->overflow_complaint ();
7027d62b00eSchristos 		      return;
7037d62b00eSchristos 		    }
7047d62b00eSchristos 
7057d62b00eSchristos 		  /* We don't increment mac_ptr here, so this is just
7067d62b00eSchristos 		     a look-ahead.  */
7077d62b00eSchristos 		  next_type
7087d62b00eSchristos 		    = (enum dwarf_macro_record_type) read_1_byte (abfd,
7097d62b00eSchristos 								  mac_ptr);
7107d62b00eSchristos 		  if (next_type != 0)
7117d62b00eSchristos 		    complaint (_("no terminating 0-type entry for "
7127d62b00eSchristos 				 "macros in `.debug_macinfo' section"));
7137d62b00eSchristos 
7147d62b00eSchristos 		  return;
7157d62b00eSchristos 		}
7167d62b00eSchristos 	    }
7177d62b00eSchristos 	  break;
7187d62b00eSchristos 
7197d62b00eSchristos 	case DW_MACRO_import:
7207d62b00eSchristos 	case DW_MACRO_import_sup:
7217d62b00eSchristos 	  {
7227d62b00eSchristos 	    LONGEST offset;
7237d62b00eSchristos 	    void **slot;
7247d62b00eSchristos 	    bfd *include_bfd = abfd;
7257d62b00eSchristos 	    const struct dwarf2_section_info *include_section = section;
7267d62b00eSchristos 	    const gdb_byte *include_mac_end = mac_end;
7277d62b00eSchristos 	    int is_dwz = section_is_dwz;
7287d62b00eSchristos 	    const gdb_byte *new_mac_ptr;
7297d62b00eSchristos 
7307d62b00eSchristos 	    offset = read_offset (abfd, mac_ptr, offset_size);
7317d62b00eSchristos 	    mac_ptr += offset_size;
7327d62b00eSchristos 
7337d62b00eSchristos 	    if (macinfo_type == DW_MACRO_import_sup)
7347d62b00eSchristos 	      {
735*6881a400Schristos 		dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd,
736*6881a400Schristos 						     true);
7377d62b00eSchristos 
7387d62b00eSchristos 		dwz->macro.read (objfile);
7397d62b00eSchristos 
7407d62b00eSchristos 		include_section = &dwz->macro;
7417d62b00eSchristos 		include_bfd = include_section->get_bfd_owner ();
7427d62b00eSchristos 		include_mac_end = dwz->macro.buffer + dwz->macro.size;
7437d62b00eSchristos 		is_dwz = 1;
7447d62b00eSchristos 	      }
7457d62b00eSchristos 
7467d62b00eSchristos 	    new_mac_ptr = include_section->buffer + offset;
7477d62b00eSchristos 	    slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
7487d62b00eSchristos 
7497d62b00eSchristos 	    if (*slot != NULL)
7507d62b00eSchristos 	      {
7517d62b00eSchristos 		/* This has actually happened; see
7527d62b00eSchristos 		   http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
7537d62b00eSchristos 		complaint (_("recursive DW_MACRO_import in "
7547d62b00eSchristos 			     ".debug_macro section"));
7557d62b00eSchristos 	      }
7567d62b00eSchristos 	    else
7577d62b00eSchristos 	      {
7587d62b00eSchristos 		*slot = (void *) new_mac_ptr;
7597d62b00eSchristos 
7607d62b00eSchristos 		dwarf_decode_macro_bytes (per_objfile, builder, include_bfd,
7617d62b00eSchristos 					  new_mac_ptr, include_mac_end,
7627d62b00eSchristos 					  current_file, lh, section,
7637d62b00eSchristos 					  section_is_gnu, is_dwz, offset_size,
764*6881a400Schristos 					  str_section, str_offsets_section,
765*6881a400Schristos 					  str_offsets_base, include_hash, cu);
7667d62b00eSchristos 
7677d62b00eSchristos 		htab_remove_elt (include_hash, (void *) new_mac_ptr);
7687d62b00eSchristos 	      }
7697d62b00eSchristos 	  }
7707d62b00eSchristos 	  break;
7717d62b00eSchristos 
7727d62b00eSchristos 	case DW_MACINFO_vendor_ext:
7737d62b00eSchristos 	  if (!section_is_gnu)
7747d62b00eSchristos 	    {
7757d62b00eSchristos 	      unsigned int bytes_read;
7767d62b00eSchristos 
7777d62b00eSchristos 	      /* This reads the constant, but since we don't recognize
7787d62b00eSchristos 		 any vendor extensions, we ignore it.  */
7797d62b00eSchristos 	      read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
7807d62b00eSchristos 	      mac_ptr += bytes_read;
7817d62b00eSchristos 	      read_direct_string (abfd, mac_ptr, &bytes_read);
7827d62b00eSchristos 	      mac_ptr += bytes_read;
7837d62b00eSchristos 
7847d62b00eSchristos 	      /* We don't recognize any vendor extensions.  */
7857d62b00eSchristos 	      break;
7867d62b00eSchristos 	    }
7877d62b00eSchristos 	  /* FALLTHROUGH */
7887d62b00eSchristos 
7897d62b00eSchristos 	default:
7907d62b00eSchristos 	  mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
7917d62b00eSchristos 					 mac_ptr, mac_end, abfd, offset_size,
7927d62b00eSchristos 					 section);
7937d62b00eSchristos 	  if (mac_ptr == NULL)
7947d62b00eSchristos 	    return;
7957d62b00eSchristos 	  break;
7967d62b00eSchristos 	}
7977d62b00eSchristos       DIAGNOSTIC_POP
7987d62b00eSchristos     } while (macinfo_type != 0);
7997d62b00eSchristos }
8007d62b00eSchristos 
8017d62b00eSchristos void
8027d62b00eSchristos dwarf_decode_macros (dwarf2_per_objfile *per_objfile,
8037d62b00eSchristos 		     buildsym_compunit *builder,
8047d62b00eSchristos 		     const dwarf2_section_info *section,
8057d62b00eSchristos 		     const struct line_header *lh, unsigned int offset_size,
806*6881a400Schristos 		     unsigned int offset, struct dwarf2_section_info *str_section,
807*6881a400Schristos 		     struct dwarf2_section_info *str_offsets_section,
808*6881a400Schristos 		     gdb::optional<ULONGEST> str_offsets_base,
809*6881a400Schristos 		     int section_is_gnu, struct dwarf2_cu *cu)
8107d62b00eSchristos {
8117d62b00eSchristos   bfd *abfd;
8127d62b00eSchristos   const gdb_byte *mac_ptr, *mac_end;
8137d62b00eSchristos   struct macro_source_file *current_file = 0;
8147d62b00eSchristos   enum dwarf_macro_record_type macinfo_type;
8157d62b00eSchristos   const gdb_byte *opcode_definitions[256];
8167d62b00eSchristos   void **slot;
8177d62b00eSchristos 
8187d62b00eSchristos   abfd = section->get_bfd_owner ();
8197d62b00eSchristos 
8207d62b00eSchristos   /* First pass: Find the name of the base filename.
8217d62b00eSchristos      This filename is needed in order to process all macros whose definition
8227d62b00eSchristos      (or undefinition) comes from the command line.  These macros are defined
8237d62b00eSchristos      before the first DW_MACINFO_start_file entry, and yet still need to be
8247d62b00eSchristos      associated to the base file.
8257d62b00eSchristos 
8267d62b00eSchristos      To determine the base file name, we scan the macro definitions until we
8277d62b00eSchristos      reach the first DW_MACINFO_start_file entry.  We then initialize
8287d62b00eSchristos      CURRENT_FILE accordingly so that any macro definition found before the
8297d62b00eSchristos      first DW_MACINFO_start_file can still be associated to the base file.  */
8307d62b00eSchristos 
8317d62b00eSchristos   mac_ptr = section->buffer + offset;
8327d62b00eSchristos   mac_end = section->buffer + section->size;
8337d62b00eSchristos 
8347d62b00eSchristos   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
8357d62b00eSchristos 				      &offset_size, section_is_gnu);
8367d62b00eSchristos   if (mac_ptr == NULL)
8377d62b00eSchristos     {
8387d62b00eSchristos       /* We already issued a complaint.  */
8397d62b00eSchristos       return;
8407d62b00eSchristos     }
8417d62b00eSchristos 
8427d62b00eSchristos   do
8437d62b00eSchristos     {
8447d62b00eSchristos       /* Do we at least have room for a macinfo type byte?  */
8457d62b00eSchristos       if (mac_ptr >= mac_end)
8467d62b00eSchristos 	{
8477d62b00eSchristos 	  /* Complaint is printed during the second pass as GDB will probably
8487d62b00eSchristos 	     stop the first pass earlier upon finding
8497d62b00eSchristos 	     DW_MACINFO_start_file.  */
8507d62b00eSchristos 	  break;
8517d62b00eSchristos 	}
8527d62b00eSchristos 
8537d62b00eSchristos       macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
8547d62b00eSchristos       mac_ptr++;
8557d62b00eSchristos 
8567d62b00eSchristos       /* Note that we rely on the fact that the corresponding GNU and
8577d62b00eSchristos 	 DWARF constants are the same.  */
8587d62b00eSchristos       DIAGNOSTIC_PUSH
8597d62b00eSchristos       DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
8607d62b00eSchristos       switch (macinfo_type)
8617d62b00eSchristos 	{
8627d62b00eSchristos 	  /* A zero macinfo type indicates the end of the macro
8637d62b00eSchristos 	     information.  */
8647d62b00eSchristos 	case 0:
8657d62b00eSchristos 	  break;
8667d62b00eSchristos 
8677d62b00eSchristos 	case DW_MACRO_define:
8687d62b00eSchristos 	case DW_MACRO_undef:
8697d62b00eSchristos 	  /* Only skip the data by MAC_PTR.  */
8707d62b00eSchristos 	  {
8717d62b00eSchristos 	    unsigned int bytes_read;
8727d62b00eSchristos 
8737d62b00eSchristos 	    read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8747d62b00eSchristos 	    mac_ptr += bytes_read;
8757d62b00eSchristos 	    read_direct_string (abfd, mac_ptr, &bytes_read);
8767d62b00eSchristos 	    mac_ptr += bytes_read;
8777d62b00eSchristos 	  }
8787d62b00eSchristos 	  break;
8797d62b00eSchristos 
8807d62b00eSchristos 	case DW_MACRO_start_file:
8817d62b00eSchristos 	  {
8827d62b00eSchristos 	    unsigned int bytes_read;
8837d62b00eSchristos 	    int line, file;
8847d62b00eSchristos 
8857d62b00eSchristos 	    line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8867d62b00eSchristos 	    mac_ptr += bytes_read;
8877d62b00eSchristos 	    file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8887d62b00eSchristos 	    mac_ptr += bytes_read;
8897d62b00eSchristos 
8907d62b00eSchristos 	    current_file = macro_start_file (builder, file, line,
8917d62b00eSchristos 					     current_file, lh);
8927d62b00eSchristos 	  }
8937d62b00eSchristos 	  break;
8947d62b00eSchristos 
8957d62b00eSchristos 	case DW_MACRO_end_file:
8967d62b00eSchristos 	  /* No data to skip by MAC_PTR.  */
8977d62b00eSchristos 	  break;
8987d62b00eSchristos 
8997d62b00eSchristos 	case DW_MACRO_define_strp:
9007d62b00eSchristos 	case DW_MACRO_undef_strp:
9017d62b00eSchristos 	case DW_MACRO_define_sup:
9027d62b00eSchristos 	case DW_MACRO_undef_sup:
9037d62b00eSchristos 	  {
9047d62b00eSchristos 	    unsigned int bytes_read;
9057d62b00eSchristos 
9067d62b00eSchristos 	    read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9077d62b00eSchristos 	    mac_ptr += bytes_read;
9087d62b00eSchristos 	    mac_ptr += offset_size;
9097d62b00eSchristos 	  }
9107d62b00eSchristos 	  break;
911*6881a400Schristos 	case DW_MACRO_define_strx:
912*6881a400Schristos 	case DW_MACRO_undef_strx:
913*6881a400Schristos 	  {
914*6881a400Schristos 	    unsigned int bytes_read;
915*6881a400Schristos 
916*6881a400Schristos 	    read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
917*6881a400Schristos 	    mac_ptr += bytes_read;
918*6881a400Schristos 	    read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
919*6881a400Schristos 	    mac_ptr += bytes_read;
920*6881a400Schristos 	  }
921*6881a400Schristos 	  break;
9227d62b00eSchristos 
9237d62b00eSchristos 	case DW_MACRO_import:
9247d62b00eSchristos 	case DW_MACRO_import_sup:
9257d62b00eSchristos 	  /* Note that, according to the spec, a transparent include
9267d62b00eSchristos 	     chain cannot call DW_MACRO_start_file.  So, we can just
9277d62b00eSchristos 	     skip this opcode.  */
9287d62b00eSchristos 	  mac_ptr += offset_size;
9297d62b00eSchristos 	  break;
9307d62b00eSchristos 
9317d62b00eSchristos 	case DW_MACINFO_vendor_ext:
9327d62b00eSchristos 	  /* Only skip the data by MAC_PTR.  */
9337d62b00eSchristos 	  if (!section_is_gnu)
9347d62b00eSchristos 	    {
9357d62b00eSchristos 	      unsigned int bytes_read;
9367d62b00eSchristos 
9377d62b00eSchristos 	      read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9387d62b00eSchristos 	      mac_ptr += bytes_read;
9397d62b00eSchristos 	      read_direct_string (abfd, mac_ptr, &bytes_read);
9407d62b00eSchristos 	      mac_ptr += bytes_read;
9417d62b00eSchristos 	    }
9427d62b00eSchristos 	  /* FALLTHROUGH */
9437d62b00eSchristos 
9447d62b00eSchristos 	default:
9457d62b00eSchristos 	  mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
9467d62b00eSchristos 					 mac_ptr, mac_end, abfd, offset_size,
9477d62b00eSchristos 					 section);
9487d62b00eSchristos 	  if (mac_ptr == NULL)
9497d62b00eSchristos 	    return;
9507d62b00eSchristos 	  break;
9517d62b00eSchristos 	}
9527d62b00eSchristos       DIAGNOSTIC_POP
9537d62b00eSchristos     } while (macinfo_type != 0 && current_file == NULL);
9547d62b00eSchristos 
9557d62b00eSchristos   /* Second pass: Process all entries.
9567d62b00eSchristos 
9577d62b00eSchristos      Use the AT_COMMAND_LINE flag to determine whether we are still processing
9587d62b00eSchristos      command-line macro definitions/undefinitions.  This flag is unset when we
9597d62b00eSchristos      reach the first DW_MACINFO_start_file entry.  */
9607d62b00eSchristos 
9617d62b00eSchristos   htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
9627d62b00eSchristos 					   htab_eq_pointer,
9637d62b00eSchristos 					   NULL, xcalloc, xfree));
9647d62b00eSchristos   mac_ptr = section->buffer + offset;
9657d62b00eSchristos   slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
9667d62b00eSchristos   *slot = (void *) mac_ptr;
9677d62b00eSchristos   dwarf_decode_macro_bytes (per_objfile, builder, abfd, mac_ptr, mac_end,
9687d62b00eSchristos 			    current_file, lh, section, section_is_gnu, 0,
969*6881a400Schristos 			    offset_size, str_section, str_offsets_section,
970*6881a400Schristos 			    str_offsets_base, include_hash.get (), cu);
9717d62b00eSchristos }
972