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