Add `set print repeats' tests for C/C++ arrays
[binutils-gdb.git] / gdb / dwarf2 / line-header.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "dwarf2/comp-unit-head.h"
22 #include "dwarf2/leb.h"
23 #include "dwarf2/line-header.h"
24 #include "dwarf2/read.h"
25 #include "complaints.h"
26 #include "filenames.h"
27
28 void
29 line_header::add_include_dir (const char *include_dir)
30 {
31 if (dwarf_line_debug >= 2)
32 {
33 size_t new_size;
34 if (version >= 5)
35 new_size = m_include_dirs.size ();
36 else
37 new_size = m_include_dirs.size () + 1;
38 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
39 new_size, include_dir);
40 }
41 m_include_dirs.push_back (include_dir);
42 }
43
44 void
45 line_header::add_file_name (const char *name,
46 dir_index d_index,
47 unsigned int mod_time,
48 unsigned int length)
49 {
50 if (dwarf_line_debug >= 2)
51 {
52 size_t new_size;
53 if (version >= 5)
54 new_size = file_names_size ();
55 else
56 new_size = file_names_size () + 1;
57 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
58 new_size, name);
59 }
60 m_file_names.emplace_back (name, d_index, mod_time, length);
61 }
62
63 gdb::unique_xmalloc_ptr<char>
64 line_header::file_file_name (int file) const
65 {
66 /* Is the file number a valid index into the line header's file name
67 table? Remember that file numbers start with one, not zero. */
68 if (is_valid_file_index (file))
69 {
70 const file_entry *fe = file_name_at (file);
71
72 if (!IS_ABSOLUTE_PATH (fe->name))
73 {
74 const char *dir = fe->include_dir (this);
75 if (dir != NULL)
76 return gdb::unique_xmalloc_ptr<char> (concat (dir, SLASH_STRING,
77 fe->name,
78 (char *) NULL));
79 }
80 return make_unique_xstrdup (fe->name);
81 }
82 else
83 {
84 /* The compiler produced a bogus file number. We can at least
85 record the macro definitions made in the file, even if we
86 won't be able to find the file by name. */
87 char fake_name[80];
88
89 xsnprintf (fake_name, sizeof (fake_name),
90 "<bad macro file number %d>", file);
91
92 complaint (_("bad file number in macro information (%d)"),
93 file);
94
95 return make_unique_xstrdup (fake_name);
96 }
97 }
98
99 static void
100 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
101 {
102 complaint (_("statement list doesn't fit in .debug_line section"));
103 }
104
105 /* Cover function for read_initial_length.
106 Returns the length of the object at BUF, and stores the size of the
107 initial length in *BYTES_READ and stores the size that offsets will be in
108 *OFFSET_SIZE.
109 If the initial length size is not equivalent to that specified in
110 CU_HEADER then issue a complaint.
111 This is useful when reading non-comp-unit headers. */
112
113 static LONGEST
114 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
115 const struct comp_unit_head *cu_header,
116 unsigned int *bytes_read,
117 unsigned int *offset_size)
118 {
119 LONGEST length = read_initial_length (abfd, buf, bytes_read);
120
121 gdb_assert (cu_header->initial_length_size == 4
122 || cu_header->initial_length_size == 8
123 || cu_header->initial_length_size == 12);
124
125 if (cu_header->initial_length_size != *bytes_read)
126 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
127
128 *offset_size = (*bytes_read == 4) ? 4 : 8;
129 return length;
130 }
131
132 /* Read directory or file name entry format, starting with byte of
133 format count entries, ULEB128 pairs of entry formats, ULEB128 of
134 entries count and the entries themselves in the described entry
135 format. */
136
137 static void
138 read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
139 const gdb_byte **bufp, struct line_header *lh,
140 unsigned int offset_size,
141 void (*callback) (struct line_header *lh,
142 const char *name,
143 dir_index d_index,
144 unsigned int mod_time,
145 unsigned int length))
146 {
147 gdb_byte format_count, formati;
148 ULONGEST data_count, datai;
149 const gdb_byte *buf = *bufp;
150 const gdb_byte *format_header_data;
151 unsigned int bytes_read;
152
153 format_count = read_1_byte (abfd, buf);
154 buf += 1;
155 format_header_data = buf;
156 for (formati = 0; formati < format_count; formati++)
157 {
158 read_unsigned_leb128 (abfd, buf, &bytes_read);
159 buf += bytes_read;
160 read_unsigned_leb128 (abfd, buf, &bytes_read);
161 buf += bytes_read;
162 }
163
164 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
165 buf += bytes_read;
166 for (datai = 0; datai < data_count; datai++)
167 {
168 const gdb_byte *format = format_header_data;
169 struct file_entry fe;
170
171 for (formati = 0; formati < format_count; formati++)
172 {
173 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
174 format += bytes_read;
175
176 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
177 format += bytes_read;
178
179 gdb::optional<const char *> string;
180 gdb::optional<unsigned int> uint;
181
182 switch (form)
183 {
184 case DW_FORM_string:
185 string.emplace (read_direct_string (abfd, buf, &bytes_read));
186 buf += bytes_read;
187 break;
188
189 case DW_FORM_line_strp:
190 {
191 const char *str
192 = per_objfile->read_line_string (buf, offset_size);
193 string.emplace (str);
194 buf += offset_size;
195 }
196 break;
197
198 case DW_FORM_data1:
199 uint.emplace (read_1_byte (abfd, buf));
200 buf += 1;
201 break;
202
203 case DW_FORM_data2:
204 uint.emplace (read_2_bytes (abfd, buf));
205 buf += 2;
206 break;
207
208 case DW_FORM_data4:
209 uint.emplace (read_4_bytes (abfd, buf));
210 buf += 4;
211 break;
212
213 case DW_FORM_data8:
214 uint.emplace (read_8_bytes (abfd, buf));
215 buf += 8;
216 break;
217
218 case DW_FORM_data16:
219 /* This is used for MD5, but file_entry does not record MD5s. */
220 buf += 16;
221 break;
222
223 case DW_FORM_udata:
224 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
225 buf += bytes_read;
226 break;
227
228 case DW_FORM_block:
229 /* It is valid only for DW_LNCT_timestamp which is ignored by
230 current GDB. */
231 break;
232 }
233
234 switch (content_type)
235 {
236 case DW_LNCT_path:
237 if (string.has_value ())
238 fe.name = *string;
239 break;
240 case DW_LNCT_directory_index:
241 if (uint.has_value ())
242 fe.d_index = (dir_index) *uint;
243 break;
244 case DW_LNCT_timestamp:
245 if (uint.has_value ())
246 fe.mod_time = *uint;
247 break;
248 case DW_LNCT_size:
249 if (uint.has_value ())
250 fe.length = *uint;
251 break;
252 case DW_LNCT_MD5:
253 break;
254 default:
255 complaint (_("Unknown format content type %s"),
256 pulongest (content_type));
257 }
258 }
259
260 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
261 }
262
263 *bufp = buf;
264 }
265
266 /* See line-header.h. */
267
268 line_header_up
269 dwarf_decode_line_header (sect_offset sect_off, bool is_dwz,
270 dwarf2_per_objfile *per_objfile,
271 struct dwarf2_section_info *section,
272 const struct comp_unit_head *cu_header)
273 {
274 const gdb_byte *line_ptr;
275 unsigned int bytes_read, offset_size;
276 int i;
277 const char *cur_dir, *cur_file;
278
279 bfd *abfd = section->get_bfd_owner ();
280
281 /* Make sure that at least there's room for the total_length field.
282 That could be 12 bytes long, but we're just going to fudge that. */
283 if (to_underlying (sect_off) + 4 >= section->size)
284 {
285 dwarf2_statement_list_fits_in_line_number_section_complaint ();
286 return 0;
287 }
288
289 line_header_up lh (new line_header ());
290
291 lh->sect_off = sect_off;
292 lh->offset_in_dwz = is_dwz;
293
294 line_ptr = section->buffer + to_underlying (sect_off);
295
296 /* Read in the header. */
297 lh->total_length =
298 read_checked_initial_length_and_offset (abfd, line_ptr, cu_header,
299 &bytes_read, &offset_size);
300 line_ptr += bytes_read;
301
302 const gdb_byte *start_here = line_ptr;
303
304 if (line_ptr + lh->total_length > (section->buffer + section->size))
305 {
306 dwarf2_statement_list_fits_in_line_number_section_complaint ();
307 return 0;
308 }
309 lh->statement_program_end = start_here + lh->total_length;
310 lh->version = read_2_bytes (abfd, line_ptr);
311 line_ptr += 2;
312 if (lh->version > 5)
313 {
314 /* This is a version we don't understand. The format could have
315 changed in ways we don't handle properly so just punt. */
316 complaint (_("unsupported version in .debug_line section"));
317 return NULL;
318 }
319 if (lh->version >= 5)
320 {
321 gdb_byte segment_selector_size;
322
323 /* Skip address size. */
324 read_1_byte (abfd, line_ptr);
325 line_ptr += 1;
326
327 segment_selector_size = read_1_byte (abfd, line_ptr);
328 line_ptr += 1;
329 if (segment_selector_size != 0)
330 {
331 complaint (_("unsupported segment selector size %u "
332 "in .debug_line section"),
333 segment_selector_size);
334 return NULL;
335 }
336 }
337 lh->header_length = read_offset (abfd, line_ptr, offset_size);
338 line_ptr += offset_size;
339 lh->statement_program_start = line_ptr + lh->header_length;
340 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
341 line_ptr += 1;
342 if (lh->version >= 4)
343 {
344 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
345 line_ptr += 1;
346 }
347 else
348 lh->maximum_ops_per_instruction = 1;
349
350 if (lh->maximum_ops_per_instruction == 0)
351 {
352 lh->maximum_ops_per_instruction = 1;
353 complaint (_("invalid maximum_ops_per_instruction "
354 "in `.debug_line' section"));
355 }
356
357 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
358 line_ptr += 1;
359 lh->line_base = read_1_signed_byte (abfd, line_ptr);
360 line_ptr += 1;
361 lh->line_range = read_1_byte (abfd, line_ptr);
362 line_ptr += 1;
363 lh->opcode_base = read_1_byte (abfd, line_ptr);
364 line_ptr += 1;
365 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
366
367 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
368 for (i = 1; i < lh->opcode_base; ++i)
369 {
370 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
371 line_ptr += 1;
372 }
373
374 if (lh->version >= 5)
375 {
376 /* Read directory table. */
377 read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
378 offset_size,
379 [] (struct line_header *header, const char *name,
380 dir_index d_index, unsigned int mod_time,
381 unsigned int length)
382 {
383 header->add_include_dir (name);
384 });
385
386 /* Read file name table. */
387 read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
388 offset_size,
389 [] (struct line_header *header, const char *name,
390 dir_index d_index, unsigned int mod_time,
391 unsigned int length)
392 {
393 header->add_file_name (name, d_index, mod_time, length);
394 });
395 }
396 else
397 {
398 /* Read directory table. */
399 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
400 {
401 line_ptr += bytes_read;
402 lh->add_include_dir (cur_dir);
403 }
404 line_ptr += bytes_read;
405
406 /* Read file name table. */
407 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
408 {
409 unsigned int mod_time, length;
410 dir_index d_index;
411
412 line_ptr += bytes_read;
413 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
414 line_ptr += bytes_read;
415 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
416 line_ptr += bytes_read;
417 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
418 line_ptr += bytes_read;
419
420 lh->add_file_name (cur_file, d_index, mod_time, length);
421 }
422 line_ptr += bytes_read;
423 }
424
425 if (line_ptr > (section->buffer + section->size))
426 complaint (_("line number info header doesn't "
427 "fit in `.debug_line' section"));
428
429 return lh;
430 }