Branch data Line data Source code
1 : : /* Return line number information of CU.
2 : : Copyright (C) 2004-2010, 2013, 2014, 2015, 2016, 2018 Red Hat, Inc.
3 : : This file is part of elfutils.
4 : :
5 : : This file is free software; you can redistribute it and/or modify
6 : : it under the terms of either
7 : :
8 : : * the GNU Lesser General Public License as published by the Free
9 : : Software Foundation; either version 3 of the License, or (at
10 : : your option) any later version
11 : :
12 : : or
13 : :
14 : : * the GNU General Public License as published by the Free
15 : : Software Foundation; either version 2 of the License, or (at
16 : : your option) any later version
17 : :
18 : : or both in parallel, as here.
19 : :
20 : : elfutils is distributed in the hope that it will be useful, but
21 : : WITHOUT ANY WARRANTY; without even the implied warranty of
22 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 : : General Public License for more details.
24 : :
25 : : You should have received copies of the GNU General Public License and
26 : : the GNU Lesser General Public License along with this program. If
27 : : not, see <http://www.gnu.org/licenses/>. */
28 : :
29 : : #ifdef HAVE_CONFIG_H
30 : : # include <config.h>
31 : : #endif
32 : :
33 : : #include <assert.h>
34 : : #include <stdlib.h>
35 : : #include <string.h>
36 : :
37 : : #include "dwarf.h"
38 : : #include "libdwP.h"
39 : : #include "eu-search.h"
40 : :
41 : :
42 : : struct filelist
43 : : {
44 : : Dwarf_Fileinfo info;
45 : : struct filelist *next;
46 : : };
47 : :
48 : : struct linelist
49 : : {
50 : : Dwarf_Line line;
51 : : struct linelist *next;
52 : : size_t sequence;
53 : : };
54 : :
55 : : struct dirlist
56 : : {
57 : : const char *dir;
58 : : size_t len;
59 : : };
60 : :
61 : : /* Compare by Dwarf_Line.addr, given pointers into an array of pointers. */
62 : : static int
63 : 3951020 : compare_lines (const void *a, const void *b)
64 : : {
65 : 3951020 : struct linelist *const *p1 = a;
66 : 3951020 : struct linelist *const *p2 = b;
67 : 3951020 : struct linelist *list1 = *p1;
68 : 3951020 : struct linelist *list2 = *p2;
69 : 3951020 : Dwarf_Line *line1 = &list1->line;
70 : 3951020 : Dwarf_Line *line2 = &list2->line;
71 : :
72 [ + + ]: 3951020 : if (line1->addr != line2->addr)
73 [ + + ]: 3624994 : return (line1->addr < line2->addr) ? -1 : 1;
74 : :
75 : : /* An end_sequence marker precedes a normal record at the same address. */
76 [ + + ]: 326026 : if (line1->end_sequence != line2->end_sequence)
77 : 222 : return line2->end_sequence - line1->end_sequence;
78 : :
79 : : /* Otherwise, the linelist sequence maintains a stable sort. */
80 : 325804 : return (list1->sequence < list2->sequence) ? -1
81 [ - + ]: 325804 : : (list1->sequence > list2->sequence) ? 1
82 : 0 : : 0;
83 : : }
84 : :
85 : : /* Decoded .debug_line program header. */
86 : : struct line_header
87 : : {
88 : : /* Header entries */
89 : : Dwarf_Word unit_length;
90 : : unsigned int length;
91 : : uint_fast16_t version;
92 : : size_t line_address_size;
93 : : size_t segment_selector_size;
94 : : Dwarf_Word header_length;
95 : : const unsigned char *header_start;
96 : : uint_fast8_t minimum_instr_len;
97 : : uint_fast8_t max_ops_per_instr;
98 : : uint_fast8_t default_is_stmt;
99 : : int_fast8_t line_base;
100 : : uint_fast8_t line_range;
101 : : uint_fast8_t opcode_base;
102 : : const uint8_t *standard_opcode_lengths;
103 : : unsigned int debug_str_offset; /* CUBIN only */
104 : : size_t files_start;
105 : : };
106 : :
107 : : struct line_state
108 : : {
109 : : Dwarf_Word addr;
110 : : unsigned int op_index;
111 : : unsigned int file;
112 : : int64_t line;
113 : : unsigned int column;
114 : : uint_fast8_t is_stmt;
115 : : bool basic_block;
116 : : bool prologue_end;
117 : : bool epilogue_begin;
118 : : unsigned int isa;
119 : : unsigned int discriminator;
120 : : struct linelist *linelist;
121 : : size_t nlinelist;
122 : : unsigned int end_sequence;
123 : : unsigned int context;
124 : : unsigned int function_name;
125 : : };
126 : :
127 : : static inline void
128 : 762038 : run_advance_pc (struct line_state *state, unsigned int op_advance,
129 : : uint_fast8_t minimum_instr_len, uint_fast8_t max_ops_per_instr)
130 : : {
131 : 762038 : state->addr += minimum_instr_len * ((state->op_index + op_advance)
132 : 762038 : / max_ops_per_instr);
133 : 762038 : state->op_index = (state->op_index + op_advance) % max_ops_per_instr;
134 : 762038 : }
135 : :
136 : : static inline bool
137 : 867124 : add_new_line (struct line_state *state, struct linelist *new_line)
138 : : {
139 : : /* Set the line information. For some fields we use bitfields,
140 : : so we would lose information if the encoded values are too large.
141 : : Check just for paranoia, and call the data "invalid" if it
142 : : violates our assumptions on reasonable limits for the values. */
143 : 867124 : new_line->next = state->linelist;
144 : 867124 : new_line->sequence = state->nlinelist;
145 : 867124 : state->linelist = new_line;
146 : 867124 : ++(state->nlinelist);
147 : :
148 : : /* Set the line information. For some fields we use bitfields,
149 : : so we would lose information if the encoded values are too large.
150 : : Check just for paranoia, and call the data "invalid" if it
151 : : violates our assumptions on reasonable limits for the values. */
152 : : #define SET(field) \
153 : : do { \
154 : : new_line->line.field = state->field; \
155 : : if (unlikely (new_line->line.field != state->field)) \
156 : : return true; \
157 : : } while (0)
158 : :
159 : : /* Same as above, but don't flag as "invalid" just use truncated
160 : : value. Used for discriminator for which llvm might use a value
161 : : that won't fit 24 bits. */
162 : : #define SETX(field) \
163 : : new_line->line.field = state->field; \
164 : :
165 : 867124 : SET (addr);
166 [ + - ]: 867124 : SET (op_index);
167 : 867124 : SET (file);
168 [ + - ]: 867124 : SET (line);
169 [ + - ]: 867124 : SET (column);
170 [ + - ]: 867124 : SET (is_stmt);
171 [ + - ]: 867124 : SET (basic_block);
172 [ + - ]: 867124 : SET (end_sequence);
173 [ + - ]: 867124 : SET (prologue_end);
174 [ + - ]: 867124 : SET (epilogue_begin);
175 [ + - ]: 867124 : SET (isa);
176 : 867124 : SETX (discriminator);
177 : 867124 : SET (context);
178 : 867124 : SET (function_name);
179 : :
180 : : #undef SET
181 : :
182 : : return false;
183 : : }
184 : :
185 : : /* Read the .debug_line program header. Return 0 if sucessful, otherwise set
186 : : libdw errno and return -1. */
187 : :
188 : : static int
189 : 38968 : read_line_header (Dwarf *dbg, unsigned address_size,
190 : : const unsigned char *linep, const unsigned char *lineendp,
191 : : struct line_header *lh)
192 : : {
193 : 38968 : const unsigned char *line_start = linep;
194 : :
195 [ - + ]: 38968 : if (unlikely (linep + 4 > lineendp))
196 : 0 : goto invalid_data;
197 : :
198 [ + + ]: 38968 : lh->unit_length = read_4ubyte_unaligned_inc (dbg, linep);
199 : 38968 : lh->length = 4;
200 [ - + ]: 38968 : if (unlikely (lh->unit_length == DWARF3_LENGTH_64_BIT))
201 : : {
202 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
203 : 0 : goto invalid_data;
204 [ # # ]: 0 : lh->unit_length = read_8ubyte_unaligned_inc (dbg, linep);
205 : 0 : lh->length = 8;
206 : : }
207 : :
208 : : /* Check whether we have enough room in the section. */
209 [ - + ]: 38968 : if (unlikely (lh->unit_length > (size_t) (lineendp - linep)))
210 : 0 : goto invalid_data;
211 : 38968 : lineendp = linep + lh->unit_length;
212 : :
213 : : /* The next element of the header is the version identifier. */
214 [ - + ]: 38968 : if ((size_t) (lineendp - linep) < 2)
215 : 0 : goto invalid_data;
216 [ + + ]: 38968 : lh->version = read_2ubyte_unaligned_inc (dbg, linep);
217 [ + - - + ]: 38968 : if (unlikely (lh->version < 2) || unlikely (lh->version > 5))
218 : : {
219 : 0 : __libdw_seterrno (DWARF_E_VERSION);
220 : 0 : return -1;
221 : : }
222 : :
223 : : /* DWARF5 explicitly lists address and segment_selector sizes. */
224 [ + + ]: 38968 : if (lh->version >= 5)
225 : : {
226 [ - + ]: 38176 : if ((size_t) (lineendp - linep) < 2)
227 : 0 : goto invalid_data;
228 : 38176 : lh->line_address_size = *linep++;
229 : 38176 : lh->segment_selector_size = *linep++;
230 [ + - - + ]: 38176 : if (lh->line_address_size != address_size || lh->segment_selector_size != 0)
231 : 0 : goto invalid_data;
232 : : }
233 : :
234 : : /* Next comes the header length. */
235 [ + - ]: 38968 : if (lh->length == 4)
236 : : {
237 [ - + ]: 38968 : if ((size_t) (lineendp - linep) < 4)
238 : 0 : goto invalid_data;
239 [ + + ]: 38968 : lh->header_length = read_4ubyte_unaligned_inc (dbg, linep);
240 : : }
241 : : else
242 : : {
243 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
244 : 0 : goto invalid_data;
245 [ # # ]: 0 : lh->header_length = read_8ubyte_unaligned_inc (dbg, linep);
246 : : }
247 : 38968 : lh->header_start = linep;
248 : :
249 : : /* The header length is the number of bytes from here to the start of
250 : : the line number program, so it must stay within the unit (and thus
251 : : the section). Without this check a bogus header_length makes
252 : : read_srcfiles/read_srclines compute an out-of-bounds end pointer
253 : : (header_start + header_length) and read past the section while
254 : : parsing the directory and file tables. */
255 [ - + ]: 38968 : if (unlikely (lh->header_length > (size_t) (lineendp - linep)))
256 : 0 : goto invalid_data;
257 : :
258 : : /* Next the minimum instruction length. */
259 [ - + ]: 38968 : if (unlikely ((size_t) (lineendp - linep) < 1))
260 : 0 : goto invalid_data;
261 : 38968 : lh->minimum_instr_len = *linep++;
262 : :
263 : : /* Next the maximum operations per instruction, in version 4 format. */
264 : 38968 : lh->max_ops_per_instr = 1;
265 [ + + ]: 38968 : if (lh->version >= 4)
266 : : {
267 [ - + ]: 38290 : if (unlikely ((size_t) (lineendp - linep) < 1))
268 : 0 : goto invalid_data;
269 : 38290 : lh->max_ops_per_instr = *linep++;
270 [ - + ]: 38290 : if (unlikely (lh->max_ops_per_instr == 0))
271 : 0 : goto invalid_data;
272 : : }
273 : :
274 : : /* 4 more bytes, is_stmt, line_base, line_range and opcode_base. */
275 [ - + ]: 38968 : if ((size_t) (lineendp - linep) < 4)
276 : 0 : goto invalid_data;
277 : :
278 : : /* Then the flag determining the default value of the is_stmt
279 : : register. */
280 : 38968 : lh->default_is_stmt = *linep++;
281 : :
282 : : /* Now the line base. */
283 : 38968 : lh->line_base = (int8_t) *linep++;
284 : :
285 : : /* And the line range. */
286 : 38968 : lh->line_range = *linep++;
287 : :
288 : : /* The opcode base. */
289 : 38968 : lh->opcode_base = *linep++;
290 : :
291 : : /* If the line header uses the NVIDIA CUBIN extension, debug_str_offset's
292 : : actual value will be read from the last 4 bytes of the header. */
293 : 38968 : lh->debug_str_offset = 0;
294 : :
295 : : /* Remember array with the standard opcode length (-1 to account for
296 : : the opcode with value zero not being mentioned). */
297 : 38968 : lh->standard_opcode_lengths = linep - 1;
298 [ - + ]: 38968 : if (unlikely (lineendp - linep < lh->opcode_base - 1))
299 : 0 : goto invalid_data;
300 : 38968 : linep += lh->opcode_base - 1;
301 : :
302 : : /* Record beginning of the file information. */
303 : 38968 : lh->files_start = (size_t) (linep - line_start);
304 : :
305 : 38968 : return 0;
306 : :
307 : 0 : invalid_data:
308 : 0 : __libdw_seterrno (DWARF_E_INVALID_DEBUG_LINE);
309 : 0 : return -1;
310 : : }
311 : :
312 : : /* If there are a large number of lines, files or dirs don't blow up
313 : : the stack. Stack allocate some entries, only dynamically malloc
314 : : when more than MAX. */
315 : : #define MAX_STACK_ALLOC 4096
316 : : #define MAX_STACK_LINES (MAX_STACK_ALLOC / 2)
317 : : #define MAX_STACK_FILES (MAX_STACK_ALLOC / 4)
318 : : #define MAX_STACK_DIRS (MAX_STACK_ALLOC / 16)
319 : :
320 : : static int
321 : 38612 : read_srcfiles (Dwarf *dbg,
322 : : const unsigned char *linep, const unsigned char *lineendp,
323 : : const char *comp_dir, unsigned address_size,
324 : : struct line_header *lh, Dwarf_Files **filesp)
325 : : {
326 [ + - ]: 38612 : if (filesp == NULL)
327 : : return -1;
328 : :
329 : 38612 : struct line_header lh_local;
330 : :
331 [ + + ]: 38612 : if (lh == NULL)
332 : : {
333 [ + - ]: 35324 : if (read_line_header (dbg, address_size, linep, lineendp, &lh_local) != 0)
334 : : return -1;
335 : : lh = &lh_local;
336 : : }
337 : :
338 : 38612 : int res = -1;
339 : :
340 : 38612 : struct filelist *filelist = NULL;
341 : 38612 : size_t nfilelist = 0;
342 : 38612 : size_t ndirlist = 0;
343 : :
344 : : /* The dirs normally go on the stack, but if there are too many
345 : : we alloc them all. Set up stack storage early, so we can check on
346 : : error if we need to free them or not. */
347 : 38612 : struct dirlist dirstack[MAX_STACK_DIRS];
348 : 38612 : struct dirlist *dirarray = dirstack;
349 : :
350 : : /* To read DWARF5 dir and file lists we need to know the forms. For
351 : : now we skip everything, except the DW_LNCT_path and
352 : : DW_LNCT_directory_index. */
353 : 38612 : uint16_t forms[256];
354 : 38612 : unsigned char nforms = 0;
355 : 38612 : unsigned char form_path = -1; /* Which forms is DW_LNCT_path. */
356 : 38612 : unsigned char form_idx = -1; /* And which is DW_LNCT_directory_index. */
357 : :
358 : : /* Set lineendp to the end of the file information. */
359 : 38612 : lineendp = lh->header_start + lh->header_length;
360 : :
361 : : /* Advance linep to the beginning of the header's srcfile information. */
362 : 38612 : linep += lh->files_start;
363 : :
364 : : /* To read/skip form data. */
365 : 38612 : Dwarf_CU fake_cu = {
366 : : .dbg = dbg,
367 : : .sec_idx = IDX_debug_line,
368 : : .version = 5,
369 : 38612 : .offset_size = lh->length,
370 : : .address_size = address_size,
371 : : .startp = (void *) linep,
372 : : .endp = (void *) lineendp,
373 : : };
374 : :
375 : : /* First count the entries. */
376 : 38612 : size_t ndirs = 0;
377 [ + + ]: 38612 : if (lh->version < 5)
378 : : {
379 : : const unsigned char *dirp = linep;
380 [ + - + + ]: 1650 : while (dirp < lineendp && *dirp != 0)
381 : : {
382 : 884 : const uint8_t *endp = memchr (dirp, '\0', lineendp - dirp);
383 [ - + ]: 884 : if (endp == NULL)
384 : 0 : goto invalid_data;
385 : 884 : ++ndirs;
386 : 884 : dirp = endp + 1;
387 : : }
388 [ + - - + ]: 766 : if (dirp >= lineendp || *dirp != '\0')
389 : 0 : goto invalid_data;
390 : 766 : ndirs = ndirs + 1; /* There is always the "unknown" dir. */
391 : : }
392 : : else
393 : : {
394 [ - + ]: 37846 : if ((size_t) (lineendp - linep) < 1)
395 : 0 : goto invalid_data;
396 : 37846 : nforms = *linep++;
397 [ + + ]: 75692 : for (int i = 0; i < nforms; i++)
398 : : {
399 : 37846 : uint16_t desc, form;
400 [ - + ]: 37846 : if ((size_t) (lineendp - linep) < 1)
401 : 0 : goto invalid_data;
402 : 37846 : get_uleb128 (desc, linep, lineendp);
403 [ - + ]: 37846 : if ((size_t) (lineendp - linep) < 1)
404 : 0 : goto invalid_data;
405 : 37846 : get_uleb128 (form, linep, lineendp);
406 : :
407 [ - + ]: 37846 : if (! libdw_valid_user_form (form))
408 : 0 : goto invalid_data;
409 : :
410 : 37846 : forms[i] = form;
411 [ + - ]: 37846 : if (desc == DW_LNCT_path)
412 : 37846 : form_path = i;
413 : : }
414 : :
415 [ - + ]: 37846 : if (nforms > 0 && form_path == (unsigned char) -1)
416 : 0 : goto invalid_data;
417 : :
418 [ - + ]: 37846 : if ((size_t) (lineendp - linep) < 1)
419 : 0 : goto invalid_data;
420 : 37846 : get_uleb128 (ndirs, linep, lineendp);
421 : :
422 [ - + ]: 37846 : if (nforms == 0 && ndirs != 0)
423 : 0 : goto invalid_data;
424 : :
425 : : /* Assume there is at least 1 byte needed per form to describe
426 : : the directory. Filters out insanely large ndirs. */
427 [ + - - + ]: 37846 : if (nforms != 0 && ndirs > (size_t) (lineendp - linep) / nforms)
428 : 0 : goto invalid_data;
429 : : }
430 : :
431 : : /* Arrange the list in array form. */
432 : 38612 : ndirlist = ndirs;
433 [ - + ]: 38612 : if (ndirlist >= MAX_STACK_DIRS)
434 : : {
435 [ # # ]: 0 : if (ndirlist > SIZE_MAX / sizeof (*dirarray))
436 : 0 : goto no_mem;
437 : 0 : dirarray = malloc (ndirlist * sizeof (*dirarray));
438 [ # # ]: 0 : if (unlikely (dirarray == NULL))
439 : : {
440 : 0 : no_mem:
441 : 0 : __libdw_seterrno (DWARF_E_NOMEM);
442 : 0 : goto out;
443 : : }
444 : : }
445 : :
446 : : /* Entry zero is implicit for older versions, but explicit for 5+. */
447 : 38612 : struct dirlist comp_dir_elem;
448 [ + + ]: 38612 : if (lh->version < 5)
449 : : {
450 : : /* First comes the list of directories. Add the compilation
451 : : directory first since the index zero is used for it. */
452 : 766 : comp_dir_elem.dir = comp_dir;
453 [ + + ]: 766 : comp_dir_elem.len = comp_dir ? strlen (comp_dir) : 0,
454 : 766 : dirarray[0] = comp_dir_elem;
455 [ + + ]: 1650 : for (unsigned int n = 1; n < ndirlist; n++)
456 : : {
457 : 884 : dirarray[n].dir = (char *) linep;
458 : 884 : const uint8_t *endp = memchr (linep, '\0', lineendp - linep);
459 [ - + ]: 884 : assert (endp != NULL); // Checked above when calculating ndirlist.
460 : 884 : dirarray[n].len = endp - linep;
461 : 884 : linep = endp + 1;
462 : : }
463 : : /* Skip the final NUL byte. */
464 [ - + ]: 766 : assert (*linep == '\0'); // Checked above when calculating ndirlist.
465 : 766 : ++linep;
466 : : }
467 : : else
468 : : {
469 : 37846 : Dwarf_Attribute attr;
470 : 37846 : attr.code = DW_AT_name;
471 : 37846 : attr.cu = &fake_cu;
472 [ + + ]: 565032 : for (unsigned int n = 0; n < ndirlist; n++)
473 : : {
474 : : const char *dir = NULL;
475 [ + + ]: 1054372 : for (unsigned char m = 0; m < nforms; m++)
476 : : {
477 [ + - ]: 527186 : if (m == form_path)
478 : : {
479 : 527186 : attr.form = forms[m];
480 : 527186 : attr.valp = (void *) linep;
481 : 527186 : dir = dwarf_formstring (&attr);
482 : : }
483 : :
484 : 527186 : size_t len = __libdw_form_val_len (&fake_cu, forms[m], linep);
485 [ - + ]: 527186 : if ((size_t) (lineendp - linep) < len)
486 : 0 : goto invalid_data;
487 : :
488 : 527186 : linep += len;
489 : : }
490 : :
491 [ - + ]: 527186 : if (dir == NULL)
492 : 0 : goto invalid_data;
493 : :
494 : 527186 : dirarray[n].dir = dir;
495 : 527186 : dirarray[n].len = strlen (dir);
496 : : }
497 : : }
498 : :
499 : : /* File index zero doesn't exist for DWARF < 5. Files are indexed
500 : : starting from 1. But for DWARF5 they are indexed starting from
501 : : zero, but the default index is still 1. In both cases the
502 : : "first" file is special and refers to the main compile unit file,
503 : : equal to the DW_AT_name of the DW_TAG_compile_unit. */
504 : 38612 : struct filelist null_file =
505 : : {
506 : : .info =
507 : : {
508 : : .name = "???",
509 : : .mtime = 0,
510 : : .length = 0
511 : : },
512 : : .next = NULL
513 : : };
514 : 38612 : filelist = &null_file;
515 : 38612 : nfilelist = 1;
516 : :
517 : : /* Allocate memory for a new file. For the first MAX_STACK_FILES
518 : : entries just return a slot in the preallocated stack array.
519 : : This is slightly complicated because in DWARF < 5 new files could
520 : : be defined with DW_LNE_define_file after the normal file list was
521 : : read. */
522 : 38612 : struct filelist flstack[MAX_STACK_FILES];
523 : : #define NEW_FILE() ({ \
524 : : struct filelist *fl = (nfilelist < MAX_STACK_FILES \
525 : : ? &flstack[nfilelist] \
526 : : : malloc (sizeof (struct filelist))); \
527 : : if (unlikely (fl == NULL)) \
528 : : goto no_mem; \
529 : : ++nfilelist; \
530 : : fl->next = filelist; \
531 : : filelist = fl; \
532 : : fl; })
533 : :
534 : : /* Now read the files. */
535 [ + + ]: 38612 : if (lh->version < 5)
536 : : {
537 [ - + ]: 766 : if (unlikely (linep >= lineendp))
538 : 0 : goto invalid_data;
539 [ + - + + ]: 8852 : while (linep < lineendp && *linep != '\0')
540 : : {
541 [ - + - - ]: 8086 : struct filelist *new_file = NEW_FILE ();
542 : :
543 : : /* First comes the file name. */
544 : 8086 : char *fname = (char *) linep;
545 : 8086 : uint8_t *endp = memchr (fname, '\0', lineendp - linep);
546 [ - + ]: 8086 : if (endp == NULL)
547 : 0 : goto invalid_data;
548 : 8086 : size_t fnamelen = endp - (uint8_t *) fname;
549 : 8086 : linep = endp + 1;
550 : :
551 : : /* Then the index. */
552 : 8086 : Dwarf_Word diridx;
553 [ - + ]: 8086 : if (unlikely (linep >= lineendp))
554 : 0 : goto invalid_data;
555 : 8086 : get_uleb128 (diridx, linep, lineendp);
556 [ - + ]: 8086 : if (unlikely (diridx >= ndirlist))
557 : : {
558 : 0 : __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
559 : 0 : goto out;
560 : : }
561 : :
562 [ + + ]: 8086 : if (*fname == '/')
563 : : /* It's an absolute path. */
564 : 728 : new_file->info.name = fname;
565 : : else
566 : : {
567 [ + + ]: 7358 : new_file->info.name = libdw_alloc (dbg, char, 1,
568 : : dirarray[diridx].len + 1
569 : : + fnamelen + 1);
570 : 7358 : char *cp = new_file->info.name;
571 : :
572 [ + + ]: 7358 : if (dirarray[diridx].dir != NULL)
573 : : {
574 : : /* This value could be NULL in case the DW_AT_comp_dir
575 : : was not present. We cannot do much in this case.
576 : : Just keep the file relative. */
577 : 7346 : cp = stpcpy (cp, dirarray[diridx].dir);
578 : 7346 : *cp++ = '/';
579 : : }
580 [ - + ]: 7358 : strcpy (cp, fname);
581 [ - + ]: 7358 : assert (strlen (new_file->info.name)
582 : : < dirarray[diridx].len + 1 + fnamelen + 1);
583 : : }
584 : :
585 : : /* Next comes the modification time. */
586 [ - + ]: 8086 : if (unlikely (linep >= lineendp))
587 : 0 : goto invalid_data;
588 : 8086 : get_uleb128 (new_file->info.mtime, linep, lineendp);
589 : :
590 : : /* Finally the length of the file. */
591 [ - + ]: 8086 : if (unlikely (linep >= lineendp))
592 : 0 : goto invalid_data;
593 : 8086 : get_uleb128 (new_file->info.length, linep, lineendp);
594 : : }
595 [ + - - + ]: 766 : if (linep >= lineendp || *linep != '\0')
596 : 0 : goto invalid_data;
597 : : /* Skip the final NUL byte. */
598 : 766 : ++linep;
599 : : }
600 : : else
601 : : {
602 [ - + ]: 37846 : if ((size_t) (lineendp - linep) < 1)
603 : 0 : goto invalid_data;
604 : 37846 : nforms = *linep++;
605 : 37846 : form_path = form_idx = -1;
606 [ + + ]: 113552 : for (int i = 0; i < nforms; i++)
607 : : {
608 : 75706 : uint16_t desc, form;
609 [ - + ]: 75706 : if ((size_t) (lineendp - linep) < 1)
610 : 0 : goto invalid_data;
611 : 75706 : get_uleb128 (desc, linep, lineendp);
612 [ - + ]: 75706 : if ((size_t) (lineendp - linep) < 1)
613 : 0 : goto invalid_data;
614 : 75706 : get_uleb128 (form, linep, lineendp);
615 : :
616 [ - + ]: 75706 : if (! libdw_valid_user_form (form))
617 : 0 : goto invalid_data;
618 : :
619 : 75706 : forms[i] = form;
620 [ + + ]: 75706 : if (desc == DW_LNCT_path)
621 : 37846 : form_path = i;
622 [ + + ]: 37860 : else if (desc == DW_LNCT_directory_index)
623 : 37846 : form_idx = i;
624 : : }
625 : :
626 [ + - ]: 37846 : if (nforms > 0 && (form_path == (unsigned char) -1
627 [ - + ]: 37846 : || form_idx == (unsigned char) -1))
628 : 0 : goto invalid_data;
629 : :
630 : 37846 : size_t nfiles;
631 [ - + ]: 37846 : if ((size_t) (lineendp - linep) < 1)
632 : 0 : goto invalid_data;
633 : 37846 : get_uleb128 (nfiles, linep, lineendp);
634 : :
635 [ - + ]: 37846 : if (nforms == 0 && nfiles != 0)
636 : 0 : goto invalid_data;
637 : :
638 : : /* Assume there is at least 1 byte needed per form to describe
639 : : the file. Filters out insanely large nfiles. */
640 [ + - - + ]: 37846 : if (nforms != 0 && nfiles > (size_t) (lineendp - linep) / nforms)
641 : 0 : goto invalid_data;
642 : :
643 : 37846 : Dwarf_Attribute attr;
644 : 37846 : attr.cu = &fake_cu;
645 [ + + ]: 5281114 : for (unsigned int n = 0; n < nfiles; n++)
646 : : {
647 : 5243268 : const char *fname = NULL;
648 : 5243268 : Dwarf_Word diridx = (Dwarf_Word) -1;
649 [ + + ]: 15729838 : for (unsigned char m = 0; m < nforms; m++)
650 : : {
651 [ + + ]: 10486570 : if (m == form_path)
652 : : {
653 : 5243268 : attr.code = DW_AT_name;
654 : 5243268 : attr.form = forms[m];
655 : 5243268 : attr.valp = (void *) linep;
656 : 5243268 : fname = dwarf_formstring (&attr);
657 : : }
658 [ + + ]: 5243302 : else if (m == form_idx)
659 : : {
660 : 5243268 : attr.code = DW_AT_decl_file; /* Close enough. */
661 : 5243268 : attr.form = forms[m];
662 : 5243268 : attr.valp = (void *) linep;
663 [ - + ]: 5243268 : if (dwarf_formudata (&attr, &diridx) != 0)
664 : 0 : diridx = (Dwarf_Word) -1;
665 : : }
666 : :
667 : 10486570 : size_t len = __libdw_form_val_len (&fake_cu, forms[m], linep);
668 [ - + ]: 10486570 : if ((size_t) (lineendp - linep) < len)
669 : 0 : goto invalid_data;
670 : :
671 : 10486570 : linep += len;
672 : : }
673 : :
674 [ + - - + ]: 5243268 : if (fname == NULL || diridx == (Dwarf_Word) -1)
675 : 0 : goto invalid_data;
676 : :
677 : 5243268 : size_t fnamelen = strlen (fname);
678 : :
679 [ - + ]: 5243268 : if (unlikely (diridx >= ndirlist))
680 : : {
681 : 0 : __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
682 : 0 : goto out;
683 : : }
684 : :
685 : : /* Yes, weird. Looks like an off-by-one in the spec. */
686 [ + + - + : 5243268 : struct filelist *new_file = n == 0 ? &null_file : NEW_FILE ();
- - ]
687 : :
688 : : /* We follow the same rules as above for DWARF < 5, even
689 : : though the standard doesn't explicitly mention absolute
690 : : paths and ignoring the dir index. */
691 [ + + ]: 5243268 : if (*fname == '/')
692 : : /* It's an absolute path. */
693 : 8 : new_file->info.name = (char *) fname;
694 : : else
695 : : {
696 [ + + ]: 5243260 : new_file->info.name = libdw_alloc (dbg, char, 1,
697 : : dirarray[diridx].len + 1
698 : : + fnamelen + 1);
699 : 5243260 : char *cp = new_file->info.name;
700 : :
701 : : /* In the DWARF >= 5 case, dir can never be NULL. */
702 [ - + ]: 5243260 : cp = stpcpy (cp, dirarray[diridx].dir);
703 : 5243260 : *cp++ = '/';
704 [ - + ]: 5243260 : strcpy (cp, fname);
705 [ - + ]: 5243260 : assert (strlen (new_file->info.name)
706 : : < dirarray[diridx].len + 1 + fnamelen + 1);
707 : : }
708 : :
709 : : /* For now we just ignore the modification time and file length. */
710 : 5243268 : new_file->info.mtime = 0;
711 : 5243268 : new_file->info.length = 0;
712 : : }
713 : : }
714 : :
715 [ + + ]: 38612 : if (unlikely (linep == lh->header_start + lh->header_length - 4))
716 : : {
717 : : /* CUBINs contain an unsigned 4-byte offset */
718 [ - + ]: 2 : lh->debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
719 : : }
720 : :
721 : : /* Consistency check. */
722 [ - + ]: 38612 : if (unlikely (linep != lh->header_start + lh->header_length))
723 : 0 : goto invalid_data;
724 : :
725 : : /* Put all the files in an array. */
726 [ + + ]: 38612 : Dwarf_Files *files = libdw_alloc (dbg, Dwarf_Files,
727 : : sizeof (Dwarf_Files)
728 : : + nfilelist * sizeof (Dwarf_Fileinfo)
729 : : + (ndirlist + 1) * sizeof (char *),
730 : : 1);
731 : :
732 [ - + ]: 38612 : if (unlikely (files == NULL))
733 : 0 : goto no_mem;
734 : :
735 : 38612 : const char **dirs = (void *) &files->info[nfilelist];
736 : :
737 : 38612 : struct filelist *fileslist = filelist;
738 : 38612 : files->nfiles = nfilelist;
739 [ + + ]: 5290732 : for (size_t n = nfilelist; n > 0; n--)
740 : : {
741 : 5252120 : files->info[n - 1] = fileslist->info;
742 : 5252120 : fileslist = fileslist->next;
743 : : }
744 [ - + ]: 38612 : assert (fileslist == NULL);
745 : :
746 : : /* Put all the directory strings in an array. */
747 : 38612 : files->ndirs = ndirlist;
748 [ + + ]: 567448 : for (unsigned int i = 0; i < ndirlist; ++i)
749 : 528836 : dirs[i] = dirarray[i].dir;
750 : 38612 : dirs[ndirlist] = NULL;
751 : :
752 : : /* Pass the file data structure to the caller. */
753 : 38612 : *filesp = files;
754 : :
755 : 38612 : res = 0;
756 : 38612 : goto out;
757 : :
758 : 0 : invalid_data:
759 : 0 : __libdw_seterrno (DWARF_E_INVALID_DEBUG_LINE);
760 : :
761 : 38612 : out:
762 [ - + ]: 38612 : if (dirarray != dirstack)
763 : 0 : free (dirarray);
764 [ - + ]: 38612 : for (size_t i = MAX_STACK_FILES; i < nfilelist; i++)
765 : : {
766 : 0 : struct filelist *fl = filelist->next;
767 : 0 : free (filelist);
768 : 0 : filelist = fl;
769 : : }
770 : :
771 : : return res;
772 : : }
773 : :
774 : : static int
775 : 3644 : read_srclines (Dwarf *dbg,
776 : : const unsigned char *linep, const unsigned char *lineendp,
777 : : const char *comp_dir, unsigned address_size,
778 : : Dwarf_Lines **linesp, Dwarf_Files **filesp,
779 : : bool use_cached_files)
780 : : {
781 : 3644 : int res = -1;
782 : 3644 : struct line_header lh;
783 : :
784 [ + - ]: 3644 : if (read_line_header (dbg, address_size, linep, lineendp, &lh) != 0)
785 : : return res;
786 : :
787 : : /* Use the filesp srcfiles if they've already been read. */
788 [ + + ]: 3644 : if (!use_cached_files
789 [ + - ]: 3288 : && read_srcfiles (dbg, linep, lineendp, comp_dir,
790 : : address_size, &lh, filesp) != 0)
791 : : return res;
792 : :
793 : : /* Initial statement program state (except for stmt_list, see below). */
794 : 3644 : struct line_state state =
795 : : {
796 : : .linelist = NULL,
797 : : .nlinelist = 0,
798 : : .addr = 0,
799 : : .op_index = 0,
800 : : .file = 1,
801 : : /* We only store int but want to check for overflow (see SET above). */
802 : : .line = 1,
803 : : .column = 0,
804 : : .basic_block = false,
805 : : .prologue_end = false,
806 : : .epilogue_begin = false,
807 : : .isa = 0,
808 : : .discriminator = 0,
809 : : .context = 0,
810 : : .function_name = 0
811 : : };
812 : :
813 : : /* We are about to process the statement program. Most state machine
814 : : registers have already been initialize above. Just add the is_stmt
815 : : default. See 6.2.2 in the v2.1 specification. */
816 : 3644 : state.is_stmt = lh.default_is_stmt;
817 : :
818 : : /* Apply the "operation advance" from a special opcode or
819 : : DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
820 : : #define advance_pc(op_advance) \
821 : : run_advance_pc (&state, op_advance, lh.minimum_instr_len, \
822 : : lh.max_ops_per_instr)
823 : :
824 : : /* Process the instructions. */
825 : :
826 : : /* Adds a new line to the matrix. For the first MAX_STACK_LINES
827 : : entries just return a slot in the preallocated stack array. */
828 : 3644 : struct linelist llstack[MAX_STACK_LINES];
829 : : #define NEW_LINE(end_seq) \
830 : : do { \
831 : : struct linelist *ll = (state.nlinelist < MAX_STACK_LINES \
832 : : ? &llstack[state.nlinelist] \
833 : : : malloc (sizeof (struct linelist))); \
834 : : if (unlikely (ll == NULL)) \
835 : : { \
836 : : __libdw_seterrno (DWARF_E_NOMEM); \
837 : : goto out; \
838 : : } \
839 : : state.end_sequence = end_seq; \
840 : : if (unlikely (add_new_line (&state, ll))) \
841 : : goto invalid_data; \
842 : : } while (0)
843 : :
844 : : /* If DW_LNE_define_file is present, then additional files will be
845 : : added to filesp. */
846 : 3644 : size_t nfilelist = 0;
847 : 3644 : struct filelist *filelist = NULL;
848 : :
849 : : /* Set lineendp to the end of the line program. */
850 : 3644 : lineendp = linep + lh.length + lh.unit_length;
851 : :
852 : : /* Set linep to the beginning of the line program. */
853 : 3644 : linep = lh.header_start + lh.header_length;
854 : :
855 [ + + ]: 2239884 : while (linep < lineendp)
856 : : {
857 : 2236240 : unsigned int opcode;
858 : 2236240 : unsigned int u128;
859 : 2236240 : int s128;
860 : :
861 : : /* Read the opcode. */
862 : 2236240 : opcode = *linep++;
863 : :
864 : : /* Is this a special opcode? */
865 [ + + ]: 2236240 : if (likely (opcode >= lh.opcode_base))
866 : : {
867 [ - + ]: 645466 : if (unlikely (lh.line_range == 0))
868 : 0 : goto invalid_data;
869 : :
870 : : /* Yes. Handling this is quite easy since the opcode value
871 : : is computed with
872 : :
873 : : opcode = (desired line increment - line_base)
874 : : + (line_range * address advance) + opcode_base
875 : : */
876 : 645466 : int line_increment = (lh.line_base
877 : 645466 : + (opcode - lh.opcode_base) % lh.line_range);
878 : :
879 : : /* Perform the increments. */
880 : 645466 : state.line += line_increment;
881 : 645466 : advance_pc ((opcode - lh.opcode_base) / lh.line_range);
882 : :
883 : : /* Add a new line with the current state machine values. */
884 [ + + - + : 645466 : NEW_LINE (0);
- + ]
885 : :
886 : : /* Reset the flags. */
887 : 645466 : state.basic_block = false;
888 : 645466 : state.prologue_end = false;
889 : 645466 : state.epilogue_begin = false;
890 : 645466 : state.discriminator = 0;
891 : : }
892 [ + + ]: 1590774 : else if (opcode == 0)
893 : : {
894 : : /* This an extended opcode. */
895 [ - + ]: 168686 : if (unlikely (lineendp - linep < 2))
896 : 0 : goto invalid_data;
897 : :
898 : : /* The length. */
899 : 168686 : uint_fast8_t len = *linep++;
900 : :
901 [ - + ]: 168686 : if (unlikely ((size_t) (lineendp - linep) < len))
902 : 0 : goto invalid_data;
903 : :
904 : : /* The sub-opcode. */
905 : 168686 : opcode = *linep++;
906 : :
907 [ + + + + : 168686 : switch (opcode)
+ - - ]
908 : : {
909 : 3856 : case DW_LNE_end_sequence:
910 : : /* Add a new line with the current state machine values.
911 : : The is the end of the sequence. */
912 [ + + - + : 3856 : NEW_LINE (1);
- + ]
913 : :
914 : : /* Reset the registers. */
915 : 3856 : state.addr = 0;
916 : 3856 : state.op_index = 0;
917 : 3856 : state.file = 1;
918 : 3856 : state.line = 1;
919 : 3856 : state.column = 0;
920 : 3856 : state.is_stmt = lh.default_is_stmt;
921 : 3856 : state.basic_block = false;
922 : 3856 : state.prologue_end = false;
923 : 3856 : state.epilogue_begin = false;
924 : 3856 : state.isa = 0;
925 : 3856 : state.discriminator = 0;
926 : 3856 : state.context = 0;
927 : 3856 : state.function_name = 0;
928 : 3856 : break;
929 : :
930 : 4738 : case DW_LNE_set_address:
931 : : /* The value is an address. The size is defined as
932 : : appropriate for the target machine. We use the
933 : : address size field from the CU header. */
934 : 4738 : state.op_index = 0;
935 [ - + ]: 4738 : if (unlikely (lineendp - linep < (uint8_t) address_size))
936 : 0 : goto invalid_data;
937 [ - + ]: 4738 : if (__libdw_read_address_inc (dbg, IDX_debug_line, &linep,
938 : : address_size, &state.addr))
939 : 0 : goto out;
940 : : break;
941 : :
942 : 4 : case DW_LNE_define_file:
943 : : {
944 : 4 : char *fname = (char *) linep;
945 : 4 : const uint8_t *endp = memchr (linep, '\0', lineendp - linep);
946 [ - + ]: 4 : if (endp == NULL)
947 : 0 : goto invalid_data;
948 : 4 : size_t fnamelen = endp - linep;
949 : 4 : linep = endp + 1;
950 : :
951 : 4 : unsigned int diridx;
952 [ - + ]: 4 : if (unlikely (linep >= lineendp))
953 : 0 : goto invalid_data;
954 : 4 : get_uleb128 (diridx, linep, lineendp);
955 : :
956 : 4 : size_t ndirs = (*filesp)->ndirs;
957 [ - + ]: 4 : if (unlikely (diridx >= ndirs))
958 : : {
959 : 0 : __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
960 : 0 : goto invalid_data;
961 : : }
962 : 4 : Dwarf_Word mtime;
963 [ - + ]: 4 : if (unlikely (linep >= lineendp))
964 : 0 : goto invalid_data;
965 : 4 : get_uleb128 (mtime, linep, lineendp);
966 : 4 : Dwarf_Word filelength;
967 [ - + ]: 4 : if (unlikely (linep >= lineendp))
968 : 0 : goto invalid_data;
969 : 4 : get_uleb128 (filelength, linep, lineendp);
970 : :
971 : : /* Add new_file to filelist that will be merged with filesp. */
972 : 4 : struct filelist *new_file = malloc (sizeof (struct filelist));
973 [ - + ]: 4 : if (unlikely (new_file == NULL))
974 : : {
975 : 0 : __libdw_seterrno (DWARF_E_NOMEM);
976 : 0 : goto out;
977 : : }
978 : 4 : nfilelist++;
979 : 4 : new_file->next = filelist;
980 : 4 : filelist = new_file;
981 : :
982 [ - + ]: 4 : if (fname[0] == '/')
983 : 0 : new_file->info.name = fname;
984 : : else
985 : : {
986 : : /* Directory names are stored in a char *[ndirs] located
987 : : after the last Dwarf_Fileinfo_s. */
988 : 4 : size_t nfiles = (*filesp)->nfiles;
989 : 4 : const char **dirarray
990 : : = (const char **) &((*filesp)->info[nfiles]);
991 : :
992 : : /* Note in case of bad DWARF4 with missing
993 : : DW_AT_comp_dir there might not be a (zero)
994 : : directory name. We cannot do much in this
995 : : case. Just keep the file relative. */
996 : 4 : const char *dname = dirarray[diridx];
997 [ + - ]: 4 : size_t dnamelen = dname != NULL ? strlen (dname) : 0;
998 : :
999 : 8 : new_file->info.name =
1000 [ - + ]: 4 : libdw_alloc (dbg, char, 1, (dnamelen + fnamelen + 2));
1001 : 4 : char *cp = new_file->info.name;
1002 : :
1003 [ + - ]: 4 : if (dname != NULL)
1004 : : {
1005 : 4 : cp = stpcpy (cp, dname);
1006 : 4 : *cp++ = '/';
1007 : : }
1008 : 4 : strcpy (cp, fname);
1009 : : }
1010 : :
1011 : 4 : new_file->info.mtime = mtime;
1012 : 4 : new_file->info.length = filelength;
1013 : : }
1014 : 4 : break;
1015 : :
1016 : 160074 : case DW_LNE_set_discriminator:
1017 : : /* Takes one ULEB128 parameter, the discriminator. */
1018 [ - + ]: 160074 : if (unlikely (lh.standard_opcode_lengths[opcode] != 1))
1019 : 0 : goto invalid_data;
1020 : :
1021 [ - + ]: 160074 : if (unlikely (linep >= lineendp))
1022 : 0 : goto invalid_data;
1023 : 160074 : get_uleb128 (state.discriminator, linep, lineendp);
1024 : 160074 : break;
1025 : :
1026 : 14 : case DW_LNE_NVIDIA_inlined_call:
1027 [ - + ]: 14 : if (unlikely (linep >= lineendp))
1028 : 0 : goto invalid_data;
1029 : 14 : get_uleb128 (state.context, linep, lineendp);
1030 [ - + ]: 14 : if (unlikely (linep >= lineendp))
1031 : 0 : goto invalid_data;
1032 : 14 : get_uleb128 (state.function_name, linep, lineendp);
1033 : 14 : state.function_name += lh.debug_str_offset;
1034 : 14 : break;
1035 : :
1036 : 0 : case DW_LNE_NVIDIA_set_function_name:
1037 [ # # ]: 0 : if (unlikely (linep >= lineendp))
1038 : 0 : goto invalid_data;
1039 : 0 : get_uleb128 (state.function_name, linep, lineendp);
1040 : 0 : state.function_name += lh.debug_str_offset;
1041 : 0 : break;
1042 : :
1043 : 0 : default:
1044 : : /* Unknown, ignore it. */
1045 [ # # ]: 0 : if (unlikely ((size_t) (lineendp - (linep - 1)) < len))
1046 : 0 : goto invalid_data;
1047 : 0 : linep += len - 1;
1048 : 0 : break;
1049 : : }
1050 : : }
1051 [ + - ]: 1422088 : else if (opcode <= DW_LNS_set_isa)
1052 : : {
1053 : : /* This is a known standard opcode. */
1054 [ + + + + : 1422088 : switch (opcode)
+ + - + -
+ - - - ]
1055 : : {
1056 : 217802 : case DW_LNS_copy:
1057 : : /* Takes no argument. */
1058 [ - + ]: 217802 : if (unlikely (lh.standard_opcode_lengths[opcode] != 0))
1059 : 0 : goto invalid_data;
1060 : :
1061 : : /* Add a new line with the current state machine values. */
1062 [ + + - + : 217802 : NEW_LINE (0);
- + ]
1063 : :
1064 : : /* Reset the flags. */
1065 : 217802 : state.basic_block = false;
1066 : 217802 : state.prologue_end = false;
1067 : 217802 : state.epilogue_begin = false;
1068 : 217802 : state.discriminator = 0;
1069 : 217802 : break;
1070 : :
1071 : 17758 : case DW_LNS_advance_pc:
1072 : : /* Takes one uleb128 parameter which is added to the
1073 : : address. */
1074 [ - + ]: 17758 : if (unlikely (lh.standard_opcode_lengths[opcode] != 1))
1075 : 0 : goto invalid_data;
1076 : :
1077 [ - + ]: 17758 : if (unlikely (linep >= lineendp))
1078 : 0 : goto invalid_data;
1079 : 17758 : get_uleb128 (u128, linep, lineendp);
1080 : 17758 : advance_pc (u128);
1081 : 17758 : break;
1082 : :
1083 : 96954 : case DW_LNS_advance_line:
1084 : : /* Takes one sleb128 parameter which is added to the
1085 : : line. */
1086 [ - + ]: 96954 : if (unlikely (lh.standard_opcode_lengths[opcode] != 1))
1087 : 0 : goto invalid_data;
1088 : :
1089 [ - + ]: 96954 : if (unlikely (linep >= lineendp))
1090 : 0 : goto invalid_data;
1091 : 96954 : get_sleb128 (s128, linep, lineendp);
1092 : 96954 : state.line += s128;
1093 : 96954 : break;
1094 : :
1095 : 21726 : case DW_LNS_set_file:
1096 : : /* Takes one uleb128 parameter which is stored in file. */
1097 [ - + ]: 21726 : if (unlikely (lh.standard_opcode_lengths[opcode] != 1))
1098 : 0 : goto invalid_data;
1099 : :
1100 [ - + ]: 21726 : if (unlikely (linep >= lineendp))
1101 : 0 : goto invalid_data;
1102 : 21726 : get_uleb128 (u128, linep, lineendp);
1103 : 21726 : state.file = u128;
1104 : 21726 : break;
1105 : :
1106 : 538956 : case DW_LNS_set_column:
1107 : : /* Takes one uleb128 parameter which is stored in column. */
1108 [ - + ]: 538956 : if (unlikely (lh.standard_opcode_lengths[opcode] != 1))
1109 : 0 : goto invalid_data;
1110 : :
1111 [ - + ]: 538956 : if (unlikely (linep >= lineendp))
1112 : 0 : goto invalid_data;
1113 : 538956 : get_uleb128 (u128, linep, lineendp);
1114 : 538956 : state.column = u128;
1115 : 538956 : break;
1116 : :
1117 : 430076 : case DW_LNS_negate_stmt:
1118 : : /* Takes no argument. */
1119 [ - + ]: 430076 : if (unlikely (lh.standard_opcode_lengths[opcode] != 0))
1120 : 0 : goto invalid_data;
1121 : :
1122 : 430076 : state.is_stmt = 1 - state.is_stmt;
1123 : 430076 : break;
1124 : :
1125 : 0 : case DW_LNS_set_basic_block:
1126 : : /* Takes no argument. */
1127 [ # # ]: 0 : if (unlikely (lh.standard_opcode_lengths[opcode] != 0))
1128 : 0 : goto invalid_data;
1129 : :
1130 : 0 : state.basic_block = true;
1131 : 0 : break;
1132 : :
1133 : 98814 : case DW_LNS_const_add_pc:
1134 : : /* Takes no argument. */
1135 [ - + ]: 98814 : if (unlikely (lh.standard_opcode_lengths[opcode] != 0))
1136 : 0 : goto invalid_data;
1137 : :
1138 [ - + ]: 98814 : if (unlikely (lh.line_range == 0))
1139 : 0 : goto invalid_data;
1140 : :
1141 : 98814 : advance_pc ((255 - lh.opcode_base) / lh.line_range);
1142 : 98814 : break;
1143 : :
1144 : 0 : case DW_LNS_fixed_advance_pc:
1145 : : /* Takes one 16 bit parameter which is added to the
1146 : : address. */
1147 [ # # ]: 0 : if (unlikely (lh.standard_opcode_lengths[opcode] != 1)
1148 [ # # ]: 0 : || unlikely (lineendp - linep < 2))
1149 : 0 : goto invalid_data;
1150 : :
1151 [ # # ]: 0 : state.addr += read_2ubyte_unaligned_inc (dbg, linep);
1152 : 0 : state.op_index = 0;
1153 : 0 : break;
1154 : :
1155 : 2 : case DW_LNS_set_prologue_end:
1156 : : /* Takes no argument. */
1157 [ - + ]: 2 : if (unlikely (lh.standard_opcode_lengths[opcode] != 0))
1158 : 0 : goto invalid_data;
1159 : :
1160 : 2 : state.prologue_end = true;
1161 : 2 : break;
1162 : :
1163 : 0 : case DW_LNS_set_epilogue_begin:
1164 : : /* Takes no argument. */
1165 [ # # ]: 0 : if (unlikely (lh.standard_opcode_lengths[opcode] != 0))
1166 : 0 : goto invalid_data;
1167 : :
1168 : 0 : state.epilogue_begin = true;
1169 : 0 : break;
1170 : :
1171 : 0 : case DW_LNS_set_isa:
1172 : : /* Takes one uleb128 parameter which is stored in isa. */
1173 [ # # ]: 0 : if (unlikely (lh.standard_opcode_lengths[opcode] != 1))
1174 : 0 : goto invalid_data;
1175 : :
1176 [ # # ]: 0 : if (unlikely (linep >= lineendp))
1177 : 0 : goto invalid_data;
1178 : 0 : get_uleb128 (state.isa, linep, lineendp);
1179 : 0 : break;
1180 : : }
1181 : : }
1182 : : else
1183 : : {
1184 : : /* This is a new opcode the generator but not we know about.
1185 : : Read the parameters associated with it but then discard
1186 : : everything. Read all the parameters for this opcode. */
1187 [ # # ]: 0 : for (int n = lh.standard_opcode_lengths[opcode]; n > 0; --n)
1188 : : {
1189 [ # # ]: 0 : if (unlikely (linep >= lineendp))
1190 : 0 : goto invalid_data;
1191 : 0 : get_uleb128 (u128, linep, lineendp);
1192 : : }
1193 : :
1194 : : /* Next round, ignore this opcode. */
1195 : 0 : continue;
1196 : : }
1197 : : }
1198 : :
1199 : : /* Merge filesp with the files from DW_LNE_define_file, if any. */
1200 [ + + ]: 3644 : if (unlikely (filelist != NULL))
1201 : : {
1202 : 2 : Dwarf_Files *prevfiles = *filesp;
1203 : 2 : size_t ndirs = prevfiles->ndirs;
1204 : 2 : size_t nprevfiles = prevfiles->nfiles;
1205 : 2 : size_t nnewfiles = nprevfiles + nfilelist;
1206 : :
1207 : 4 : Dwarf_Files *newfiles
1208 [ - + ]: 2 : = libdw_alloc (dbg, Dwarf_Files,
1209 : : sizeof (Dwarf_Files)
1210 : : + nnewfiles * sizeof (Dwarf_Fileinfo)
1211 : : + (ndirs + 1) * sizeof (char *),
1212 : : 1);
1213 : :
1214 : :
1215 : : /* Copy prevfiles to newfiles. */
1216 [ + + ]: 90 : for (size_t n = 0; n < nprevfiles; n++)
1217 : 88 : newfiles->info[n] = prevfiles->info[n];
1218 : :
1219 : : /* Add files from DW_LNE_define_file to newfiles. */
1220 : : struct filelist *fileslist = filelist;
1221 [ + + ]: 6 : for (size_t n = nfilelist; n > 0; n--)
1222 : : {
1223 : 4 : newfiles->info[nprevfiles + n - 1] = fileslist->info;
1224 : 4 : fileslist = fileslist->next;
1225 : : }
1226 : :
1227 [ - + ]: 2 : if (fileslist != NULL)
1228 : 0 : goto invalid_data;
1229 : :
1230 : 2 : const char **newdirs = (void *) &newfiles->info[nnewfiles];
1231 : 2 : const char **prevdirs = (void *) &prevfiles->info[nprevfiles];
1232 : :
1233 : : /* Copy prevdirs to newdirs. */
1234 [ + + ]: 12 : for (size_t n = 0; n < ndirs; n++)
1235 : 10 : newdirs[n] = prevdirs[n];
1236 : :
1237 : : /* Update filesp. */
1238 : 2 : newfiles->nfiles = nnewfiles;
1239 : 2 : newfiles->ndirs = prevfiles->ndirs;
1240 : 2 : *filesp = newfiles;
1241 : : }
1242 : :
1243 : 3644 : size_t buf_size = (sizeof (Dwarf_Lines)
1244 : 3644 : + (sizeof (Dwarf_Line) * state.nlinelist));
1245 [ + + ]: 3644 : void *buf = libdw_alloc (dbg, Dwarf_Lines, buf_size, 1);
1246 : :
1247 : : /* First use the buffer for the pointers, and sort the entries.
1248 : : We'll write the pointers in the end of the buffer, and then
1249 : : copy into the buffer from the beginning so the overlap works. */
1250 : 3644 : assert (sizeof (Dwarf_Line) >= sizeof (struct linelist *));
1251 : 3644 : struct linelist **sortlines = (buf + buf_size
1252 : 3644 : - sizeof (struct linelist **) * state.nlinelist);
1253 : :
1254 : : /* The list is in LIFO order and usually they come in clumps with
1255 : : ascending addresses. So fill from the back to probably start with
1256 : : runs already in order before we sort. */
1257 : 3644 : struct linelist *lineslist = state.linelist;
1258 [ + + ]: 870768 : for (size_t i = state.nlinelist; i-- > 0; )
1259 : : {
1260 : 867124 : sortlines[i] = lineslist;
1261 : 867124 : lineslist = lineslist->next;
1262 : : }
1263 [ - + ]: 3644 : assert (lineslist == NULL);
1264 : :
1265 : : /* Sort by ascending address. */
1266 : 3644 : qsort (sortlines, state.nlinelist, sizeof sortlines[0], &compare_lines);
1267 : :
1268 : : /* Now that they are sorted, put them in the final array.
1269 : : The buffers overlap, so we've clobbered the early elements
1270 : : of SORTLINES by the time we're reading the later ones. */
1271 : 3644 : Dwarf_Lines *lines = buf;
1272 : 3644 : lines->nlines = state.nlinelist;
1273 [ + + ]: 870768 : for (size_t i = 0; i < state.nlinelist; ++i)
1274 : : {
1275 : 867124 : lines->info[i] = sortlines[i]->line;
1276 : 867124 : lines->info[i].files = *filesp;
1277 : : }
1278 : :
1279 : : /* Make sure the highest address for the CU is marked as end_sequence.
1280 : : This is required by the DWARF spec, but some compilers forget and
1281 : : dwfl_module_getsrc depends on it. */
1282 [ + + ]: 3644 : if (state.nlinelist > 0)
1283 : 3626 : lines->info[state.nlinelist - 1].end_sequence = 1;
1284 : :
1285 : : /* Pass the line structure back to the caller. */
1286 [ + - ]: 3644 : if (linesp != NULL)
1287 : 3644 : *linesp = lines;
1288 : :
1289 : : /* Success. */
1290 : 3644 : res = 0;
1291 : 3644 : goto out;
1292 : :
1293 : 0 : invalid_data:
1294 : 0 : __libdw_seterrno (DWARF_E_INVALID_DEBUG_LINE);
1295 : :
1296 : 3644 : out:
1297 : : /* Free malloced line records, if any. */
1298 [ + + ]: 72318 : for (size_t i = MAX_STACK_LINES; i < state.nlinelist; i++)
1299 : : {
1300 : 68674 : struct linelist *ll = state.linelist->next;
1301 : 68674 : free (state.linelist);
1302 : 68674 : state.linelist = ll;
1303 : : }
1304 : :
1305 : : /* Free file records from DW_LNE_define_file, if any. */
1306 [ + + ]: 3648 : for (size_t i = 0; i < nfilelist; i++)
1307 : : {
1308 : 4 : struct filelist *fl = filelist->next;
1309 : 4 : free (filelist);
1310 : 4 : filelist = fl;
1311 : : }
1312 : :
1313 : : return res;
1314 : : }
1315 : :
1316 : : static int
1317 : 750210 : files_lines_compare (const void *p1, const void *p2)
1318 : : {
1319 : 750210 : const struct files_lines_s *t1 = p1;
1320 : 750210 : const struct files_lines_s *t2 = p2;
1321 : :
1322 [ + + ]: 750210 : if (t1->debug_line_offset < t2->debug_line_offset)
1323 : : return -1;
1324 [ + + ]: 747998 : if (t1->debug_line_offset > t2->debug_line_offset)
1325 : 746280 : return 1;
1326 : :
1327 : : return 0;
1328 : : }
1329 : :
1330 : : static int
1331 : 40330 : get_lines_or_files (Dwarf *dbg, Dwarf_Off debug_line_offset,
1332 : : const char *comp_dir, unsigned address_size,
1333 : : Dwarf_Lines **linesp, Dwarf_Files **filesp)
1334 : : {
1335 : 40330 : int ret = -1;
1336 : :
1337 : 40330 : mutex_lock (dbg->lines_files_lock);
1338 : :
1339 : 40330 : struct files_lines_s fake = { .debug_line_offset = debug_line_offset };
1340 : 40330 : struct files_lines_s **found = eu_tfind_nolock (&fake, &dbg->files_lines_tree,
1341 : : files_lines_compare);
1342 [ + + ]: 40330 : if (found == NULL)
1343 : : {
1344 : : /* This .debug_line is being read for the first time. */
1345 : 38612 : Elf_Data *data = __libdw_checked_get_data (dbg, IDX_debug_line);
1346 [ + - ]: 38612 : if (data == NULL
1347 [ - + ]: 38612 : || __libdw_offset_in_section (dbg, IDX_debug_line,
1348 : : debug_line_offset, 1) != 0)
1349 : 0 : goto out;
1350 : :
1351 : 38612 : const unsigned char *linep = data->d_buf + debug_line_offset;
1352 : 38612 : const unsigned char *lineendp = data->d_buf + data->d_size;
1353 : :
1354 [ + + ]: 38612 : struct files_lines_s *node = libdw_alloc (dbg, struct files_lines_s,
1355 : : sizeof *node, 1);
1356 : :
1357 : : /* Srcfiles will be read but srclines might not. Set lines here
1358 : : to avoid possible uninitialized value errors. */
1359 : 38612 : node->lines = NULL;
1360 : :
1361 : : /* If linesp is NULL then read srcfiles without reading srclines. */
1362 [ + + ]: 38612 : if (linesp == NULL)
1363 : : {
1364 [ - + ]: 35324 : if (read_srcfiles (dbg, linep, lineendp, comp_dir, address_size,
1365 : : NULL, &node->files) != 0)
1366 : 0 : goto out;
1367 : : }
1368 [ - + ]: 3288 : else if (read_srclines (dbg, linep, lineendp, comp_dir, address_size,
1369 : : &node->lines, &node->files, false) != 0)
1370 : 0 : goto out;
1371 : :
1372 : 38612 : node->debug_line_offset = debug_line_offset;
1373 : :
1374 : 38612 : found = eu_tsearch_nolock (node, &dbg->files_lines_tree,
1375 : : files_lines_compare);
1376 [ - + ]: 38612 : if (found == NULL)
1377 : : {
1378 : 0 : __libdw_seterrno (DWARF_E_NOMEM);
1379 : 0 : goto out;
1380 : : }
1381 : : }
1382 [ + - ]: 1718 : else if (*found != NULL
1383 [ + - ]: 1718 : && (*found)->files != NULL
1384 [ + + ]: 1718 : && (*found)->lines == NULL)
1385 : : {
1386 : : /* Srcfiles were already read from this .debug_line. Now read
1387 : : srclines. */
1388 : 356 : Elf_Data *data = __libdw_checked_get_data (dbg, IDX_debug_line);
1389 [ + - ]: 356 : if (data == NULL
1390 [ - + ]: 356 : || __libdw_offset_in_section (dbg, IDX_debug_line,
1391 : : debug_line_offset, 1) != 0)
1392 : 0 : goto out;
1393 : :
1394 : 356 : const unsigned char *linep = data->d_buf + debug_line_offset;
1395 : 356 : const unsigned char *lineendp = data->d_buf + data->d_size;
1396 : :
1397 : 356 : struct files_lines_s *node = *found;
1398 : :
1399 [ - + ]: 356 : if (read_srclines (dbg, linep, lineendp, comp_dir, address_size,
1400 : : &node->lines, &node->files, true) != 0)
1401 : 0 : goto out;
1402 : : }
1403 [ + - ]: 1362 : else if (*found != NULL
1404 [ - + ]: 1362 : && (*found)->files == NULL
1405 [ # # ]: 0 : && (*found)->lines != NULL)
1406 : : {
1407 : : /* If srclines were read then srcfiles should have also been read. */
1408 : 0 : __libdw_seterrno (DWARF_E_INVALID_DEBUG_LINE);
1409 : 0 : goto out;
1410 : : }
1411 : :
1412 [ + + ]: 40330 : if (linesp != NULL)
1413 : 3318 : *linesp = (*found)->lines;
1414 : :
1415 [ + + ]: 40330 : if (filesp != NULL)
1416 : 40200 : *filesp = (*found)->files;
1417 : :
1418 : : ret = 0;
1419 : :
1420 : 40330 : out:
1421 : 40330 : mutex_unlock (dbg->lines_files_lock);
1422 : 40330 : return ret;
1423 : : }
1424 : :
1425 : : int
1426 : : internal_function
1427 : 3376 : __libdw_getsrclines (Dwarf *dbg, Dwarf_Off debug_line_offset,
1428 : : const char *comp_dir, unsigned address_size,
1429 : : Dwarf_Lines **linesp, Dwarf_Files **filesp)
1430 : : {
1431 : 3376 : return get_lines_or_files (dbg, debug_line_offset, comp_dir,
1432 : : address_size, linesp, filesp);
1433 : : }
1434 : :
1435 : : int
1436 : : internal_function
1437 : 36954 : __libdw_getsrcfiles (Dwarf *dbg, Dwarf_Off debug_line_offset,
1438 : : const char *comp_dir, unsigned address_size,
1439 : : Dwarf_Files **filesp)
1440 : : {
1441 : 36954 : return get_lines_or_files (dbg, debug_line_offset, comp_dir,
1442 : : address_size, NULL, filesp);
1443 : : }
1444 : :
1445 : : /* Get the compilation directory, if any is set. */
1446 : : const char *
1447 : 40266 : __libdw_getcompdir (Dwarf_Die *cudie)
1448 : : {
1449 : 40266 : Dwarf_Attribute compdir_attr_mem;
1450 : 40266 : Dwarf_Attribute *compdir_attr = INTUSE(dwarf_attr) (cudie,
1451 : : DW_AT_comp_dir,
1452 : : &compdir_attr_mem);
1453 : 40266 : return INTUSE(dwarf_formstring) (compdir_attr);
1454 : : }
1455 : :
1456 : : int
1457 : 3276 : dwarf_getsrclines (Dwarf_Die *cudie, Dwarf_Lines **lines, size_t *nlines)
1458 : : {
1459 [ + - ]: 3276 : if (cudie == NULL)
1460 : : return -1;
1461 [ - + ]: 3276 : if (! is_cudie (cudie))
1462 : : {
1463 : 0 : __libdw_seterrno (DWARF_E_NOT_CUDIE);
1464 : 0 : return -1;
1465 : : }
1466 : :
1467 : 3276 : struct Dwarf_CU *const cu = cudie->cu;
1468 : 3276 : mutex_lock (cu->src_lock);
1469 : :
1470 : : /* Get the information if it is not already known. */
1471 [ + + ]: 3276 : if (cu->lines == NULL)
1472 : : {
1473 : : /* For split units always pick the lines from the skeleton. */
1474 : 3188 : if (cu->unit_type == DW_UT_split_compile
1475 [ - + ]: 3188 : || cu->unit_type == DW_UT_split_type)
1476 : : {
1477 : : /* We tries, assume we fail... */
1478 : 0 : cu->lines = (void *) -1l;
1479 : :
1480 : 0 : Dwarf_CU *skel = __libdw_find_split_unit (cu);
1481 [ # # ]: 0 : if (skel != NULL)
1482 : : {
1483 : 0 : Dwarf_Die skeldie = CUDIE (skel);
1484 : 0 : int res = INTUSE(dwarf_getsrclines) (&skeldie, lines, nlines);
1485 [ # # ]: 0 : if (res == 0)
1486 : : {
1487 : 0 : cu->lines = skel->lines;
1488 : 0 : *lines = cu->lines;
1489 : 0 : *nlines = cu->lines->nlines;
1490 : : }
1491 : :
1492 : 0 : mutex_unlock (cu->src_lock);
1493 : 0 : return res;
1494 : : }
1495 : :
1496 : 0 : __libdw_seterrno (DWARF_E_NO_DEBUG_LINE);
1497 : 0 : mutex_unlock (cu->src_lock);
1498 : 0 : return -1;
1499 : : }
1500 : :
1501 : : /* Failsafe mode: no data found. */
1502 : 3188 : cu->lines = (void *) -1l;
1503 : 3188 : cu->files = (void *) -1l;
1504 : :
1505 : : /* The die must have a statement list associated. */
1506 : 3188 : Dwarf_Attribute stmt_list_mem;
1507 : 3188 : Dwarf_Attribute *stmt_list = INTUSE(dwarf_attr) (cudie, DW_AT_stmt_list,
1508 : : &stmt_list_mem);
1509 : :
1510 : : /* Get the offset into the .debug_line section. NB: this call
1511 : : also checks whether the previous dwarf_attr call failed. */
1512 : 3188 : Dwarf_Off debug_line_offset;
1513 [ + - ]: 3188 : if (__libdw_formptr (stmt_list, IDX_debug_line, DWARF_E_NO_DEBUG_LINE,
1514 : : NULL, &debug_line_offset) == NULL)
1515 : : {
1516 : : mutex_unlock (cu->src_lock);
1517 : : return -1;
1518 : : }
1519 : :
1520 [ + - ]: 3188 : if (__libdw_getsrclines (cu->dbg, debug_line_offset,
1521 : : __libdw_getcompdir (cudie),
1522 : 3188 : cu->address_size, &cu->lines, &cu->files) < 0)
1523 : : {
1524 : : mutex_unlock (cu->src_lock);
1525 : : return -1;
1526 : : }
1527 : : }
1528 [ + - ]: 88 : else if (cu->lines == (void *) -1l)
1529 : : {
1530 : : mutex_unlock (cu->src_lock);
1531 : : return -1;
1532 : : }
1533 : :
1534 : 3276 : *lines = cu->lines;
1535 : 3276 : *nlines = cu->lines->nlines;
1536 : :
1537 : 3276 : mutex_unlock (cu->src_lock);
1538 : 3276 : return 0;
1539 : : }
1540 : : INTDEF(dwarf_getsrclines)
|