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 : #include <search.h>
37 :
38 : #include "dwarf.h"
39 : #include "libdwP.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 :
56 : /* Compare by Dwarf_Line.addr, given pointers into an array of pointers. */
57 : static int
58 2086723 : compare_lines (const void *a, const void *b)
59 : {
60 2086723 : struct linelist *const *p1 = a;
61 2086723 : struct linelist *const *p2 = b;
62 2086723 : struct linelist *list1 = *p1;
63 2086723 : struct linelist *list2 = *p2;
64 2086723 : Dwarf_Line *line1 = &list1->line;
65 2086723 : Dwarf_Line *line2 = &list2->line;
66 :
67 2086723 : if (line1->addr != line2->addr)
68 2080206 : return (line1->addr < line2->addr) ? -1 : 1;
69 :
70 : /* An end_sequence marker precedes a normal record at the same address. */
71 6517 : if (line1->end_sequence != line2->end_sequence)
72 13 : return line2->end_sequence - line1->end_sequence;
73 :
74 : /* Otherwise, the linelist sequence maintains a stable sort. */
75 13008 : return (list1->sequence < list2->sequence) ? -1
76 6504 : : (list1->sequence > list2->sequence) ? 1
77 0 : : 0;
78 : }
79 :
80 : struct line_state
81 : {
82 : Dwarf_Word addr;
83 : unsigned int op_index;
84 : unsigned int file;
85 : int64_t line;
86 : unsigned int column;
87 : uint_fast8_t is_stmt;
88 : bool basic_block;
89 : bool prologue_end;
90 : bool epilogue_begin;
91 : unsigned int isa;
92 : unsigned int discriminator;
93 : struct linelist *linelist;
94 : size_t nlinelist;
95 : unsigned int end_sequence;
96 : };
97 :
98 : static inline void
99 : run_advance_pc (struct line_state *state, unsigned int op_advance,
100 : uint_fast8_t minimum_instr_len, uint_fast8_t max_ops_per_instr)
101 : {
102 1263006 : state->addr += minimum_instr_len * ((state->op_index + op_advance)
103 631503 : / max_ops_per_instr);
104 631503 : state->op_index = (state->op_index + op_advance) % max_ops_per_instr;
105 : }
106 :
107 : static inline bool
108 521336 : add_new_line (struct line_state *state, struct linelist *new_line)
109 : {
110 : /* Set the line information. For some fields we use bitfields,
111 : so we would lose information if the encoded values are too large.
112 : Check just for paranoia, and call the data "invalid" if it
113 : violates our assumptions on reasonable limits for the values. */
114 521336 : new_line->next = state->linelist;
115 521336 : new_line->sequence = state->nlinelist;
116 521336 : state->linelist = new_line;
117 521336 : ++(state->nlinelist);
118 :
119 : /* Set the line information. For some fields we use bitfields,
120 : so we would lose information if the encoded values are too large.
121 : Check just for paranoia, and call the data "invalid" if it
122 : violates our assumptions on reasonable limits for the values. */
123 : #define SET(field) \
124 : do { \
125 : new_line->line.field = state->field; \
126 : if (unlikely (new_line->line.field != state->field)) \
127 : return true; \
128 : } while (0)
129 :
130 521336 : SET (addr);
131 521336 : SET (op_index);
132 521336 : SET (file);
133 521336 : SET (line);
134 521336 : SET (column);
135 521336 : SET (is_stmt);
136 521336 : SET (basic_block);
137 521336 : SET (end_sequence);
138 521336 : SET (prologue_end);
139 521336 : SET (epilogue_begin);
140 521336 : SET (isa);
141 521336 : SET (discriminator);
142 :
143 : #undef SET
144 :
145 521336 : return false;
146 : }
147 :
148 : static int
149 5372 : read_srclines (Dwarf *dbg,
150 : const unsigned char *linep, const unsigned char *lineendp,
151 : const char *comp_dir, unsigned address_size,
152 : Dwarf_Lines **linesp, Dwarf_Files **filesp)
153 : {
154 5372 : int res = -1;
155 :
156 5372 : struct filelist *filelist = NULL;
157 5372 : size_t nfilelist = 0;
158 5372 : size_t ndirlist = 0;
159 :
160 : /* If there are a large number of lines, files or dirs don't blow up
161 : the stack. Stack allocate some entries, only dynamically malloc
162 : when more than MAX. */
163 : #define MAX_STACK_ALLOC 4096
164 : #define MAX_STACK_LINES MAX_STACK_ALLOC
165 : #define MAX_STACK_FILES (MAX_STACK_ALLOC / 4)
166 : #define MAX_STACK_DIRS (MAX_STACK_ALLOC / 16)
167 :
168 : /* Initial statement program state (except for stmt_list, see below). */
169 5372 : struct line_state state =
170 : {
171 : .linelist = NULL,
172 : .nlinelist = 0,
173 : .addr = 0,
174 : .op_index = 0,
175 : .file = 1,
176 : /* We only store int but want to check for overflow (see SET above). */
177 : .line = 1,
178 : .column = 0,
179 : .basic_block = false,
180 : .prologue_end = false,
181 : .epilogue_begin = false,
182 : .isa = 0,
183 : .discriminator = 0
184 : };
185 :
186 : /* The dirs normally go on the stack, but if there are too many
187 : we alloc them all. Set up stack storage early, so we can check on
188 : error if we need to free them or not. */
189 : struct dirlist
190 : {
191 : const char *dir;
192 : size_t len;
193 : };
194 : struct dirlist dirstack[MAX_STACK_DIRS];
195 5372 : struct dirlist *dirarray = dirstack;
196 :
197 5372 : if (unlikely (linep + 4 > lineendp))
198 : {
199 : invalid_data:
200 0 : __libdw_seterrno (DWARF_E_INVALID_DEBUG_LINE);
201 0 : goto out;
202 : }
203 :
204 10697 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
205 5372 : unsigned int length = 4;
206 5372 : if (unlikely (unit_length == DWARF3_LENGTH_64_BIT))
207 : {
208 0 : if (unlikely (linep + 8 > lineendp))
209 : goto invalid_data;
210 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
211 0 : length = 8;
212 : }
213 :
214 : /* Check whether we have enough room in the section. */
215 5372 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
216 : goto invalid_data;
217 5372 : lineendp = linep + unit_length;
218 :
219 : /* The next element of the header is the version identifier. */
220 5372 : if ((size_t) (lineendp - linep) < 2)
221 : goto invalid_data;
222 5372 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
223 5372 : if (unlikely (version < 2) || unlikely (version > 5))
224 : {
225 0 : __libdw_seterrno (DWARF_E_VERSION);
226 0 : goto out;
227 : }
228 :
229 : /* DWARF5 explicitly lists address and segment_selector sizes. */
230 5372 : if (version >= 5)
231 : {
232 11 : if ((size_t) (lineendp - linep) < 2)
233 : goto invalid_data;
234 11 : size_t line_address_size = *linep++;
235 11 : size_t segment_selector_size = *linep++;
236 11 : if (line_address_size != address_size || segment_selector_size != 0)
237 : goto invalid_data;
238 : }
239 :
240 : /* Next comes the header length. */
241 : Dwarf_Word header_length;
242 5372 : if (length == 4)
243 : {
244 5372 : if ((size_t) (lineendp - linep) < 4)
245 : goto invalid_data;
246 10697 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
247 : }
248 : else
249 : {
250 0 : if ((size_t) (lineendp - linep) < 8)
251 : goto invalid_data;
252 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
253 : }
254 5372 : const unsigned char *header_start = linep;
255 :
256 : /* Next the minimum instruction length. */
257 5372 : uint_fast8_t minimum_instr_len = *linep++;
258 :
259 : /* Next the maximum operations per instruction, in version 4 format. */
260 5372 : uint_fast8_t max_ops_per_instr = 1;
261 5372 : if (version >= 4)
262 : {
263 28 : if (unlikely ((size_t) (lineendp - linep) < 1))
264 : goto invalid_data;
265 28 : max_ops_per_instr = *linep++;
266 28 : if (unlikely (max_ops_per_instr == 0))
267 : goto invalid_data;
268 : }
269 :
270 : /* 4 more bytes, is_stmt, line_base, line_range and opcode_base. */
271 5372 : if ((size_t) (lineendp - linep) < 4)
272 : goto invalid_data;
273 :
274 : /* Then the flag determining the default value of the is_stmt
275 : register. */
276 5372 : uint_fast8_t default_is_stmt = *linep++;
277 :
278 : /* Now the line base. */
279 5372 : int_fast8_t line_base = (int8_t) *linep++;
280 :
281 : /* And the line range. */
282 5372 : uint_fast8_t line_range = *linep++;
283 :
284 : /* The opcode base. */
285 5372 : uint_fast8_t opcode_base = *linep++;
286 :
287 : /* Remember array with the standard opcode length (-1 to account for
288 : the opcode with value zero not being mentioned). */
289 5372 : const uint8_t *standard_opcode_lengths = linep - 1;
290 5372 : if (unlikely (lineendp - linep < opcode_base - 1))
291 : goto invalid_data;
292 5372 : linep += opcode_base - 1;
293 :
294 : /* To read DWARF5 dir and file lists we need to know the forms. For
295 : now we skip everything, except the DW_LNCT_path and
296 : DW_LNCT_directory_index. */
297 : uint16_t forms[256];
298 5372 : unsigned char nforms = 0;
299 5372 : unsigned char form_path = -1; /* Which forms is DW_LNCT_path. */
300 5372 : unsigned char form_idx = -1; /* And which is DW_LNCT_directory_index. */
301 :
302 : /* To read/skip form data. */
303 5372 : Dwarf_CU fake_cu = {
304 : .dbg = dbg,
305 : .sec_idx = IDX_debug_line,
306 : .version = 5,
307 : .offset_size = length,
308 : .address_size = address_size,
309 : .startp = (void *) linep,
310 : .endp = (void *) lineendp,
311 : };
312 :
313 : /* First count the entries. */
314 5372 : size_t ndirs = 0;
315 5372 : if (version < 5)
316 : {
317 : const unsigned char *dirp = linep;
318 35812 : while (*dirp != 0)
319 : {
320 30451 : uint8_t *endp = memchr (dirp, '\0', lineendp - dirp);
321 30451 : if (endp == NULL)
322 : goto invalid_data;
323 30451 : ++ndirs;
324 30451 : dirp = endp + 1;
325 : }
326 5361 : ndirs = ndirs + 1; /* There is always the "unknown" dir. */
327 : }
328 : else
329 : {
330 11 : if ((size_t) (lineendp - linep) < 1)
331 : goto invalid_data;
332 11 : nforms = *linep++;
333 22 : for (int i = 0; i < nforms; i++)
334 : {
335 : uint16_t desc, form;
336 11 : if ((size_t) (lineendp - linep) < 1)
337 : goto invalid_data;
338 11 : get_uleb128 (desc, linep, lineendp);
339 11 : if ((size_t) (lineendp - linep) < 1)
340 : goto invalid_data;
341 11 : get_uleb128 (form, linep, lineendp);
342 :
343 11 : if (! libdw_valid_user_form (form))
344 : goto invalid_data;
345 :
346 11 : forms[i] = form;
347 11 : if (desc == DW_LNCT_path)
348 11 : form_path = i;
349 : }
350 :
351 11 : if (nforms > 0 && form_path == (unsigned char) -1)
352 : goto invalid_data;
353 :
354 11 : if ((size_t) (lineendp - linep) < 1)
355 : goto invalid_data;
356 11 : get_uleb128 (ndirs, linep, lineendp);
357 :
358 11 : if (nforms == 0 && ndirs != 0)
359 : goto invalid_data;
360 :
361 : /* Assume there is at least 1 byte needed per form to describe
362 : the directory. Filters out insanely large ndirs. */
363 11 : if (nforms != 0 && ndirs > (size_t) (lineendp - linep) / nforms)
364 : goto invalid_data;
365 : }
366 :
367 : /* Arrange the list in array form. */
368 5372 : ndirlist = ndirs;
369 5372 : if (ndirlist >= MAX_STACK_DIRS)
370 : {
371 0 : if (ndirlist > SIZE_MAX / sizeof (*dirarray))
372 : goto no_mem;
373 0 : dirarray = (struct dirlist *) malloc (ndirlist * sizeof (*dirarray));
374 0 : if (unlikely (dirarray == NULL))
375 : {
376 : no_mem:
377 0 : __libdw_seterrno (DWARF_E_NOMEM);
378 0 : goto out;
379 : }
380 : }
381 :
382 : /* Entry zero is implicit for older versions, but explicit for 5+. */
383 : struct dirlist comp_dir_elem;
384 5372 : if (version < 5)
385 : {
386 : /* First comes the list of directories. Add the compilation
387 : directory first since the index zero is used for it. */
388 5361 : comp_dir_elem.dir = comp_dir;
389 5361 : comp_dir_elem.len = comp_dir ? strlen (comp_dir) : 0,
390 5361 : dirarray[0] = comp_dir_elem;
391 35812 : for (unsigned int n = 1; n < ndirlist; n++)
392 : {
393 30451 : dirarray[n].dir = (char *) linep;
394 30451 : uint8_t *endp = memchr (linep, '\0', lineendp - linep);
395 30451 : assert (endp != NULL);
396 30451 : dirarray[n].len = endp - linep;
397 30451 : linep = endp + 1;
398 : }
399 : /* Skip the final NUL byte. */
400 5361 : ++linep;
401 : }
402 : else
403 : {
404 : Dwarf_Attribute attr;
405 11 : attr.code = DW_AT_name;
406 11 : attr.cu = &fake_cu;
407 33 : for (unsigned int n = 0; n < ndirlist; n++)
408 : {
409 : const char *dir = NULL;
410 22 : for (unsigned char m = 0; m < nforms; m++)
411 : {
412 22 : if (m == form_path)
413 : {
414 22 : attr.form = forms[m];
415 22 : attr.valp = (void *) linep;
416 22 : dir = dwarf_formstring (&attr);
417 : }
418 :
419 22 : size_t len = __libdw_form_val_len (&fake_cu, forms[m], linep);
420 22 : if ((size_t) (lineendp - linep) < len)
421 : goto invalid_data;
422 :
423 22 : linep += len;
424 : }
425 :
426 22 : if (dir == NULL)
427 : goto invalid_data;
428 :
429 22 : dirarray[n].dir = dir;
430 22 : dirarray[n].len = strlen (dir);
431 : }
432 : }
433 :
434 : /* File index zero doesn't exist for DWARF < 5. Files are indexed
435 : starting from 1. But for DWARF5 they are indexed starting from
436 : zero, but the default index is still 1. In both cases the
437 : "first" file is special and refers to the main compile unit file,
438 : equal to the DW_AT_name of the DW_TAG_compile_unit. */
439 5372 : struct filelist null_file =
440 : {
441 : .info =
442 : {
443 : .name = "???",
444 : .mtime = 0,
445 : .length = 0
446 : },
447 : .next = NULL
448 : };
449 5372 : filelist = &null_file;
450 5372 : nfilelist = 1;
451 :
452 : /* Allocate memory for a new file. For the first MAX_STACK_FILES
453 : entries just return a slot in the preallocated stack array.
454 : This is slightly complicated because in DWARF < 5 new files could
455 : be defined with DW_LNE_define_file after the normal file list was
456 : read. */
457 : struct filelist flstack[MAX_STACK_FILES];
458 : #define NEW_FILE() ({ \
459 : struct filelist *fl = (nfilelist < MAX_STACK_FILES \
460 : ? &flstack[nfilelist] \
461 : : malloc (sizeof (struct filelist))); \
462 : if (unlikely (fl == NULL)) \
463 : goto no_mem; \
464 : ++nfilelist; \
465 : fl->next = filelist; \
466 : filelist = fl; \
467 : fl; })
468 :
469 : /* Now read the files. */
470 5372 : if (version < 5)
471 : {
472 5361 : if (unlikely (linep >= lineendp))
473 : goto invalid_data;
474 84443 : while (*linep != 0)
475 : {
476 79082 : struct filelist *new_file = NEW_FILE ();
477 :
478 : /* First comes the file name. */
479 79082 : char *fname = (char *) linep;
480 79082 : uint8_t *endp = memchr (fname, '\0', lineendp - linep);
481 79082 : if (endp == NULL)
482 : goto invalid_data;
483 79082 : size_t fnamelen = endp - (uint8_t *) fname;
484 79082 : linep = endp + 1;
485 :
486 : /* Then the index. */
487 : Dwarf_Word diridx;
488 79082 : if (unlikely (linep >= lineendp))
489 : goto invalid_data;
490 79082 : get_uleb128 (diridx, linep, lineendp);
491 79082 : if (unlikely (diridx >= ndirlist))
492 : {
493 0 : __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
494 0 : goto out;
495 : }
496 :
497 79082 : if (*fname == '/')
498 : /* It's an absolute path. */
499 354 : new_file->info.name = fname;
500 : else
501 : {
502 78728 : new_file->info.name = libdw_alloc (dbg, char, 1,
503 : dirarray[diridx].len + 1
504 : + fnamelen + 1);
505 78728 : char *cp = new_file->info.name;
506 :
507 78728 : if (dirarray[diridx].dir != NULL)
508 : {
509 : /* This value could be NULL in case the DW_AT_comp_dir
510 : was not present. We cannot do much in this case.
511 : Just keep the file relative. */
512 157444 : cp = stpcpy (cp, dirarray[diridx].dir);
513 78722 : *cp++ = '/';
514 : }
515 : strcpy (cp, fname);
516 78728 : assert (strlen (new_file->info.name)
517 : < dirarray[diridx].len + 1 + fnamelen + 1);
518 : }
519 :
520 : /* Next comes the modification time. */
521 79082 : if (unlikely (linep >= lineendp))
522 : goto invalid_data;
523 79082 : get_uleb128 (new_file->info.mtime, linep, lineendp);
524 :
525 : /* Finally the length of the file. */
526 79082 : if (unlikely (linep >= lineendp))
527 : goto invalid_data;
528 79082 : get_uleb128 (new_file->info.length, linep, lineendp);
529 : }
530 : /* Skip the final NUL byte. */
531 5361 : ++linep;
532 : }
533 : else
534 : {
535 11 : if ((size_t) (lineendp - linep) < 1)
536 : goto invalid_data;
537 11 : nforms = *linep++;
538 11 : form_path = form_idx = -1;
539 33 : for (int i = 0; i < nforms; i++)
540 : {
541 : uint16_t desc, form;
542 22 : if ((size_t) (lineendp - linep) < 1)
543 : goto invalid_data;
544 22 : get_uleb128 (desc, linep, lineendp);
545 22 : if ((size_t) (lineendp - linep) < 1)
546 : goto invalid_data;
547 22 : get_uleb128 (form, linep, lineendp);
548 :
549 22 : if (! libdw_valid_user_form (form))
550 : goto invalid_data;
551 :
552 22 : forms[i] = form;
553 22 : if (desc == DW_LNCT_path)
554 11 : form_path = i;
555 11 : else if (desc == DW_LNCT_directory_index)
556 11 : form_idx = i;
557 : }
558 :
559 22 : if (nforms > 0 && (form_path == (unsigned char) -1
560 11 : || form_idx == (unsigned char) -1))
561 : goto invalid_data;
562 :
563 : size_t nfiles;
564 11 : get_uleb128 (nfiles, linep, lineendp);
565 :
566 11 : if (nforms == 0 && nfiles != 0)
567 : goto invalid_data;
568 :
569 : /* Assume there is at least 1 byte needed per form to describe
570 : the file. Filters out insanely large nfiles. */
571 11 : if (nforms != 0 && nfiles > (size_t) (lineendp - linep) / nforms)
572 : goto invalid_data;
573 :
574 : Dwarf_Attribute attr;
575 11 : attr.cu = &fake_cu;
576 55 : for (unsigned int n = 0; n < nfiles; n++)
577 : {
578 44 : const char *fname = NULL;
579 44 : Dwarf_Word diridx = (Dwarf_Word) -1;
580 132 : for (unsigned char m = 0; m < nforms; m++)
581 : {
582 88 : if (m == form_path)
583 : {
584 44 : attr.code = DW_AT_name;
585 44 : attr.form = forms[m];
586 44 : attr.valp = (void *) linep;
587 44 : fname = dwarf_formstring (&attr);
588 : }
589 44 : else if (m == form_idx)
590 : {
591 44 : attr.code = DW_AT_decl_file; /* Close enough. */
592 44 : attr.form = forms[m];
593 44 : attr.valp = (void *) linep;
594 44 : if (dwarf_formudata (&attr, &diridx) != 0)
595 0 : diridx = (Dwarf_Word) -1;
596 : }
597 :
598 88 : size_t len = __libdw_form_val_len (&fake_cu, forms[m], linep);
599 88 : if ((size_t) (lineendp - linep) < len)
600 : goto invalid_data;
601 :
602 88 : linep += len;
603 : }
604 :
605 44 : if (fname == NULL || diridx == (Dwarf_Word) -1)
606 : goto invalid_data;
607 :
608 44 : size_t fnamelen = strlen (fname);
609 :
610 44 : if (unlikely (diridx >= ndirlist))
611 : {
612 0 : __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
613 0 : goto out;
614 : }
615 :
616 : /* Yes, weird. Looks like an off-by-one in the spec. */
617 44 : struct filelist *new_file = n == 0 ? &null_file : NEW_FILE ();
618 :
619 : /* We follow the same rules as above for DWARF < 5, even
620 : though the standard doesn't explicitly mention absolute
621 : paths and ignoring the dir index. */
622 44 : if (*fname == '/')
623 : /* It's an absolute path. */
624 0 : new_file->info.name = (char *) fname;
625 : else
626 : {
627 44 : new_file->info.name = libdw_alloc (dbg, char, 1,
628 : dirarray[diridx].len + 1
629 : + fnamelen + 1);
630 44 : char *cp = new_file->info.name;
631 :
632 : /* In the DWARF >= 5 case, dir can never be NULL. */
633 88 : cp = stpcpy (cp, dirarray[diridx].dir);
634 44 : *cp++ = '/';
635 : strcpy (cp, fname);
636 44 : assert (strlen (new_file->info.name)
637 : < dirarray[diridx].len + 1 + fnamelen + 1);
638 : }
639 :
640 : /* For now we just ignore the modification time and file length. */
641 44 : new_file->info.mtime = 0;
642 44 : new_file->info.length = 0;
643 : }
644 : }
645 :
646 : /* Consistency check. */
647 5372 : if (unlikely (linep != header_start + header_length))
648 : {
649 0 : __libdw_seterrno (DWARF_E_INVALID_DWARF);
650 0 : goto out;
651 : }
652 :
653 : /* We are about to process the statement program. Most state machine
654 : registers have already been initialize above. Just add the is_stmt
655 : default. See 6.2.2 in the v2.1 specification. */
656 5372 : state.is_stmt = default_is_stmt;
657 :
658 : /* Apply the "operation advance" from a special opcode or
659 : DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
660 : #define advance_pc(op_advance) \
661 : run_advance_pc (&state, op_advance, minimum_instr_len, max_ops_per_instr)
662 :
663 : /* Process the instructions. */
664 :
665 : /* Adds a new line to the matrix. For the first MAX_STACK_LINES
666 : entries just return a slot in the preallocated stack array. */
667 : struct linelist llstack[MAX_STACK_LINES];
668 : #define NEW_LINE(end_seq) \
669 : do { \
670 : struct linelist *ll = (state.nlinelist < MAX_STACK_LINES \
671 : ? &llstack[state.nlinelist] \
672 : : malloc (sizeof (struct linelist))); \
673 : if (unlikely (ll == NULL)) \
674 : goto no_mem; \
675 : state.end_sequence = end_seq; \
676 : if (unlikely (add_new_line (&state, ll))) \
677 : goto invalid_data; \
678 : } while (0)
679 :
680 1050971 : while (linep < lineendp)
681 : {
682 : unsigned int opcode;
683 : unsigned int u128;
684 : int s128;
685 :
686 : /* Read the opcode. */
687 1040227 : opcode = *linep++;
688 :
689 : /* Is this a special opcode? */
690 1040227 : if (likely (opcode >= opcode_base))
691 : {
692 477846 : if (unlikely (line_range == 0))
693 : goto invalid_data;
694 :
695 : /* Yes. Handling this is quite easy since the opcode value
696 : is computed with
697 :
698 : opcode = (desired line increment - line_base)
699 : + (line_range * address advance) + opcode_base
700 : */
701 477846 : int line_increment = (line_base
702 477846 : + (opcode - opcode_base) % line_range);
703 :
704 : /* Perform the increments. */
705 477846 : state.line += line_increment;
706 477846 : advance_pc ((opcode - opcode_base) / line_range);
707 :
708 : /* Add a new line with the current state machine values. */
709 477846 : NEW_LINE (0);
710 :
711 : /* Reset the flags. */
712 477846 : state.basic_block = false;
713 477846 : state.prologue_end = false;
714 477846 : state.epilogue_begin = false;
715 477846 : state.discriminator = 0;
716 : }
717 562381 : else if (opcode == 0)
718 : {
719 : /* This an extended opcode. */
720 67608 : if (unlikely (lineendp - linep < 2))
721 : goto invalid_data;
722 :
723 : /* The length. */
724 67608 : uint_fast8_t len = *linep++;
725 :
726 67608 : if (unlikely ((size_t) (lineendp - linep) < len))
727 : goto invalid_data;
728 :
729 : /* The sub-opcode. */
730 67608 : opcode = *linep++;
731 :
732 67608 : switch (opcode)
733 : {
734 : case DW_LNE_end_sequence:
735 : /* Add a new line with the current state machine values.
736 : The is the end of the sequence. */
737 8570 : NEW_LINE (1);
738 :
739 : /* Reset the registers. */
740 8570 : state.addr = 0;
741 8570 : state.op_index = 0;
742 8570 : state.file = 1;
743 8570 : state.line = 1;
744 8570 : state.column = 0;
745 8570 : state.is_stmt = default_is_stmt;
746 8570 : state.basic_block = false;
747 8570 : state.prologue_end = false;
748 8570 : state.epilogue_begin = false;
749 8570 : state.isa = 0;
750 8570 : state.discriminator = 0;
751 8570 : break;
752 :
753 : case DW_LNE_set_address:
754 : /* The value is an address. The size is defined as
755 : apporiate for the target machine. We use the
756 : address size field from the CU header. */
757 9159 : state.op_index = 0;
758 9159 : if (unlikely (lineendp - linep < (uint8_t) address_size))
759 : goto invalid_data;
760 9159 : if (__libdw_read_address_inc (dbg, IDX_debug_line, &linep,
761 : address_size, &state.addr))
762 : goto out;
763 : break;
764 :
765 : case DW_LNE_define_file:
766 : {
767 0 : char *fname = (char *) linep;
768 0 : uint8_t *endp = memchr (linep, '\0', lineendp - linep);
769 0 : if (endp == NULL)
770 : goto invalid_data;
771 0 : size_t fnamelen = endp - linep;
772 0 : linep = endp + 1;
773 :
774 : unsigned int diridx;
775 0 : if (unlikely (linep >= lineendp))
776 : goto invalid_data;
777 0 : get_uleb128 (diridx, linep, lineendp);
778 0 : if (unlikely (diridx >= ndirlist))
779 : {
780 0 : __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
781 0 : goto invalid_data;
782 : }
783 : Dwarf_Word mtime;
784 0 : if (unlikely (linep >= lineendp))
785 : goto invalid_data;
786 0 : get_uleb128 (mtime, linep, lineendp);
787 : Dwarf_Word filelength;
788 0 : if (unlikely (linep >= lineendp))
789 : goto invalid_data;
790 0 : get_uleb128 (filelength, linep, lineendp);
791 :
792 0 : struct filelist *new_file = NEW_FILE ();
793 0 : if (fname[0] == '/')
794 0 : new_file->info.name = fname;
795 : else
796 : {
797 0 : new_file->info.name =
798 0 : libdw_alloc (dbg, char, 1, (dirarray[diridx].len + 1
799 : + fnamelen + 1));
800 0 : char *cp = new_file->info.name;
801 :
802 0 : if (dirarray[diridx].dir != NULL)
803 : /* This value could be NULL in case the
804 : DW_AT_comp_dir was not present. We
805 : cannot do much in this case. Just
806 : keep the file relative. */
807 : {
808 0 : cp = stpcpy (cp, dirarray[diridx].dir);
809 0 : *cp++ = '/';
810 : }
811 : strcpy (cp, fname);
812 : }
813 :
814 0 : new_file->info.mtime = mtime;
815 0 : new_file->info.length = filelength;
816 : }
817 0 : break;
818 :
819 : case DW_LNE_set_discriminator:
820 : /* Takes one ULEB128 parameter, the discriminator. */
821 49875 : if (unlikely (standard_opcode_lengths[opcode] != 1))
822 : goto invalid_data;
823 :
824 49875 : if (unlikely (linep >= lineendp))
825 : goto invalid_data;
826 49875 : get_uleb128 (state.discriminator, linep, lineendp);
827 49875 : break;
828 :
829 : default:
830 : /* Unknown, ignore it. */
831 4 : if (unlikely ((size_t) (lineendp - (linep - 1)) < len))
832 : goto invalid_data;
833 4 : linep += len - 1;
834 4 : break;
835 : }
836 : }
837 494773 : else if (opcode <= DW_LNS_set_isa)
838 : {
839 : /* This is a known standard opcode. */
840 494773 : switch (opcode)
841 : {
842 : case DW_LNS_copy:
843 : /* Takes no argument. */
844 34920 : if (unlikely (standard_opcode_lengths[opcode] != 0))
845 : goto invalid_data;
846 :
847 : /* Add a new line with the current state machine values. */
848 34920 : NEW_LINE (0);
849 :
850 : /* Reset the flags. */
851 34920 : state.basic_block = false;
852 34920 : state.prologue_end = false;
853 34920 : state.epilogue_begin = false;
854 34920 : state.discriminator = 0;
855 34920 : break;
856 :
857 : case DW_LNS_advance_pc:
858 : /* Takes one uleb128 parameter which is added to the
859 : address. */
860 44899 : if (unlikely (standard_opcode_lengths[opcode] != 1))
861 : goto invalid_data;
862 :
863 44899 : if (unlikely (linep >= lineendp))
864 : goto invalid_data;
865 44899 : get_uleb128 (u128, linep, lineendp);
866 44899 : advance_pc (u128);
867 : break;
868 :
869 : case DW_LNS_advance_line:
870 : /* Takes one sleb128 parameter which is added to the
871 : line. */
872 205141 : if (unlikely (standard_opcode_lengths[opcode] != 1))
873 : goto invalid_data;
874 :
875 205141 : if (unlikely (linep >= lineendp))
876 : goto invalid_data;
877 205141 : get_sleb128 (s128, linep, lineendp);
878 205141 : state.line += s128;
879 205141 : break;
880 :
881 : case DW_LNS_set_file:
882 : /* Takes one uleb128 parameter which is stored in file. */
883 79867 : if (unlikely (standard_opcode_lengths[opcode] != 1))
884 : goto invalid_data;
885 :
886 79867 : if (unlikely (linep >= lineendp))
887 : goto invalid_data;
888 79867 : get_uleb128 (u128, linep, lineendp);
889 79867 : state.file = u128;
890 79867 : break;
891 :
892 : case DW_LNS_set_column:
893 : /* Takes one uleb128 parameter which is stored in column. */
894 338 : if (unlikely (standard_opcode_lengths[opcode] != 1))
895 : goto invalid_data;
896 :
897 338 : if (unlikely (linep >= lineendp))
898 : goto invalid_data;
899 338 : get_uleb128 (u128, linep, lineendp);
900 338 : state.column = u128;
901 338 : break;
902 :
903 : case DW_LNS_negate_stmt:
904 : /* Takes no argument. */
905 20847 : if (unlikely (standard_opcode_lengths[opcode] != 0))
906 : goto invalid_data;
907 :
908 20847 : state.is_stmt = 1 - state.is_stmt;
909 20847 : break;
910 :
911 : case DW_LNS_set_basic_block:
912 : /* Takes no argument. */
913 0 : if (unlikely (standard_opcode_lengths[opcode] != 0))
914 : goto invalid_data;
915 :
916 0 : state.basic_block = true;
917 0 : break;
918 :
919 : case DW_LNS_const_add_pc:
920 : /* Takes no argument. */
921 108758 : if (unlikely (standard_opcode_lengths[opcode] != 0))
922 : goto invalid_data;
923 :
924 108758 : if (unlikely (line_range == 0))
925 : goto invalid_data;
926 :
927 108758 : advance_pc ((255 - opcode_base) / line_range);
928 : break;
929 :
930 : case DW_LNS_fixed_advance_pc:
931 : /* Takes one 16 bit parameter which is added to the
932 : address. */
933 0 : if (unlikely (standard_opcode_lengths[opcode] != 1)
934 0 : || unlikely (lineendp - linep < 2))
935 : goto invalid_data;
936 :
937 0 : state.addr += read_2ubyte_unaligned_inc (dbg, linep);
938 0 : state.op_index = 0;
939 0 : break;
940 :
941 : case DW_LNS_set_prologue_end:
942 : /* Takes no argument. */
943 3 : if (unlikely (standard_opcode_lengths[opcode] != 0))
944 : goto invalid_data;
945 :
946 3 : state.prologue_end = true;
947 3 : break;
948 :
949 : case DW_LNS_set_epilogue_begin:
950 : /* Takes no argument. */
951 0 : if (unlikely (standard_opcode_lengths[opcode] != 0))
952 : goto invalid_data;
953 :
954 0 : state.epilogue_begin = true;
955 0 : break;
956 :
957 : case DW_LNS_set_isa:
958 : /* Takes one uleb128 parameter which is stored in isa. */
959 0 : if (unlikely (standard_opcode_lengths[opcode] != 1))
960 : goto invalid_data;
961 :
962 0 : if (unlikely (linep >= lineendp))
963 : goto invalid_data;
964 0 : get_uleb128 (state.isa, linep, lineendp);
965 0 : break;
966 : }
967 : }
968 : else
969 : {
970 : /* This is a new opcode the generator but not we know about.
971 : Read the parameters associated with it but then discard
972 : everything. Read all the parameters for this opcode. */
973 0 : for (int n = standard_opcode_lengths[opcode]; n > 0; --n)
974 : {
975 0 : if (unlikely (linep >= lineendp))
976 : goto invalid_data;
977 0 : get_uleb128 (u128, linep, lineendp);
978 : }
979 :
980 : /* Next round, ignore this opcode. */
981 0 : continue;
982 : }
983 : }
984 :
985 : /* Put all the files in an array. */
986 5372 : Dwarf_Files *files = libdw_alloc (dbg, Dwarf_Files,
987 : sizeof (Dwarf_Files)
988 : + nfilelist * sizeof (Dwarf_Fileinfo)
989 : + (ndirlist + 1) * sizeof (char *),
990 : 1);
991 5372 : const char **dirs = (void *) &files->info[nfilelist];
992 :
993 5372 : struct filelist *fileslist = filelist;
994 5372 : files->nfiles = nfilelist;
995 89859 : for (size_t n = nfilelist; n > 0; n--)
996 : {
997 84487 : files->info[n - 1] = fileslist->info;
998 84487 : fileslist = fileslist->next;
999 : }
1000 5372 : assert (fileslist == NULL);
1001 :
1002 : /* Put all the directory strings in an array. */
1003 5372 : files->ndirs = ndirlist;
1004 41206 : for (unsigned int i = 0; i < ndirlist; ++i)
1005 35834 : dirs[i] = dirarray[i].dir;
1006 5372 : dirs[ndirlist] = NULL;
1007 :
1008 : /* Pass the file data structure to the caller. */
1009 5372 : if (filesp != NULL)
1010 5372 : *filesp = files;
1011 :
1012 5372 : size_t buf_size = (sizeof (Dwarf_Lines)
1013 5372 : + (sizeof (Dwarf_Line) * state.nlinelist));
1014 5372 : void *buf = libdw_alloc (dbg, Dwarf_Lines, buf_size, 1);
1015 :
1016 : /* First use the buffer for the pointers, and sort the entries.
1017 : We'll write the pointers in the end of the buffer, and then
1018 : copy into the buffer from the beginning so the overlap works. */
1019 : assert (sizeof (Dwarf_Line) >= sizeof (struct linelist *));
1020 5372 : struct linelist **sortlines = (buf + buf_size
1021 5372 : - sizeof (struct linelist **) * state.nlinelist);
1022 :
1023 : /* The list is in LIFO order and usually they come in clumps with
1024 : ascending addresses. So fill from the back to probably start with
1025 : runs already in order before we sort. */
1026 5372 : struct linelist *lineslist = state.linelist;
1027 532080 : for (size_t i = state.nlinelist; i-- > 0; )
1028 : {
1029 521336 : sortlines[i] = lineslist;
1030 521336 : lineslist = lineslist->next;
1031 : }
1032 5372 : assert (lineslist == NULL);
1033 :
1034 : /* Sort by ascending address. */
1035 5372 : qsort (sortlines, state.nlinelist, sizeof sortlines[0], &compare_lines);
1036 :
1037 : /* Now that they are sorted, put them in the final array.
1038 : The buffers overlap, so we've clobbered the early elements
1039 : of SORTLINES by the time we're reading the later ones. */
1040 5372 : Dwarf_Lines *lines = buf;
1041 5372 : lines->nlines = state.nlinelist;
1042 526708 : for (size_t i = 0; i < state.nlinelist; ++i)
1043 : {
1044 521336 : lines->info[i] = sortlines[i]->line;
1045 521336 : lines->info[i].files = files;
1046 : }
1047 :
1048 : /* Make sure the highest address for the CU is marked as end_sequence.
1049 : This is required by the DWARF spec, but some compilers forget and
1050 : dwfl_module_getsrc depends on it. */
1051 5372 : if (state.nlinelist > 0)
1052 5346 : lines->info[state.nlinelist - 1].end_sequence = 1;
1053 :
1054 : /* Pass the line structure back to the caller. */
1055 5372 : if (linesp != NULL)
1056 5372 : *linesp = lines;
1057 :
1058 : /* Success. */
1059 : res = 0;
1060 :
1061 : out:
1062 : /* Free malloced line records, if any. */
1063 17140 : for (size_t i = MAX_STACK_LINES; i < state.nlinelist; i++)
1064 : {
1065 11768 : struct linelist *ll = state.linelist->next;
1066 11768 : free (state.linelist);
1067 11768 : state.linelist = ll;
1068 : }
1069 5372 : if (dirarray != dirstack)
1070 0 : free (dirarray);
1071 0 : for (size_t i = MAX_STACK_FILES; i < nfilelist; i++)
1072 : {
1073 0 : struct filelist *fl = filelist->next;
1074 0 : free (filelist);
1075 0 : filelist = fl;
1076 : }
1077 :
1078 5372 : return res;
1079 : }
1080 :
1081 : static int
1082 86466 : files_lines_compare (const void *p1, const void *p2)
1083 : {
1084 86466 : const struct files_lines_s *t1 = p1;
1085 86466 : const struct files_lines_s *t2 = p2;
1086 :
1087 86466 : if (t1->debug_line_offset < t2->debug_line_offset)
1088 : return -1;
1089 86454 : if (t1->debug_line_offset > t2->debug_line_offset)
1090 : return 1;
1091 :
1092 2 : return 0;
1093 : }
1094 :
1095 : int
1096 : internal_function
1097 5374 : __libdw_getsrclines (Dwarf *dbg, Dwarf_Off debug_line_offset,
1098 : const char *comp_dir, unsigned address_size,
1099 : Dwarf_Lines **linesp, Dwarf_Files **filesp)
1100 : {
1101 5374 : struct files_lines_s fake = { .debug_line_offset = debug_line_offset };
1102 5374 : struct files_lines_s **found = tfind (&fake, &dbg->files_lines,
1103 : files_lines_compare);
1104 5374 : if (found == NULL)
1105 : {
1106 5372 : Elf_Data *data = __libdw_checked_get_data (dbg, IDX_debug_line);
1107 5372 : if (data == NULL
1108 5372 : || __libdw_offset_in_section (dbg, IDX_debug_line,
1109 : debug_line_offset, 1) != 0)
1110 : return -1;
1111 :
1112 5372 : const unsigned char *linep = data->d_buf + debug_line_offset;
1113 5372 : const unsigned char *lineendp = data->d_buf + data->d_size;
1114 :
1115 5372 : struct files_lines_s *node = libdw_alloc (dbg, struct files_lines_s,
1116 : sizeof *node, 1);
1117 :
1118 5372 : if (read_srclines (dbg, linep, lineendp, comp_dir, address_size,
1119 : &node->lines, &node->files) != 0)
1120 : return -1;
1121 :
1122 5372 : node->debug_line_offset = debug_line_offset;
1123 :
1124 5372 : found = tsearch (node, &dbg->files_lines, files_lines_compare);
1125 5372 : if (found == NULL)
1126 : {
1127 0 : __libdw_seterrno (DWARF_E_NOMEM);
1128 0 : return -1;
1129 : }
1130 : }
1131 :
1132 5374 : if (linesp != NULL)
1133 5347 : *linesp = (*found)->lines;
1134 :
1135 5374 : if (filesp != NULL)
1136 5355 : *filesp = (*found)->files;
1137 :
1138 : return 0;
1139 : }
1140 :
1141 : /* Get the compilation directory, if any is set. */
1142 : const char *
1143 5372 : __libdw_getcompdir (Dwarf_Die *cudie)
1144 : {
1145 : Dwarf_Attribute compdir_attr_mem;
1146 5372 : Dwarf_Attribute *compdir_attr = INTUSE(dwarf_attr) (cudie,
1147 : DW_AT_comp_dir,
1148 : &compdir_attr_mem);
1149 5372 : return INTUSE(dwarf_formstring) (compdir_attr);
1150 : }
1151 :
1152 : int
1153 16080 : dwarf_getsrclines (Dwarf_Die *cudie, Dwarf_Lines **lines, size_t *nlines)
1154 : {
1155 5360 : if (cudie == NULL)
1156 : return -1;
1157 10720 : if (! is_cudie (cudie))
1158 : {
1159 0 : __libdw_seterrno (DWARF_E_NOT_CUDIE);
1160 0 : return -1;
1161 : }
1162 :
1163 : /* Get the information if it is not already known. */
1164 5360 : struct Dwarf_CU *const cu = cudie->cu;
1165 5360 : if (cu->lines == NULL)
1166 : {
1167 : /* For split units always pick the lines from the skeleton. */
1168 10656 : if (cu->unit_type == DW_UT_split_compile
1169 5328 : || cu->unit_type == DW_UT_split_type)
1170 : {
1171 : /* We tries, assume we fail... */
1172 0 : cu->lines = (void *) -1l;
1173 :
1174 0 : Dwarf_CU *skel = __libdw_find_split_unit (cu);
1175 0 : if (skel != NULL)
1176 : {
1177 0 : Dwarf_Die skeldie = CUDIE (skel);
1178 0 : int res = INTUSE(dwarf_getsrclines) (&skeldie, lines, nlines);
1179 0 : if (res == 0)
1180 : {
1181 0 : cu->lines = skel->lines;
1182 0 : *lines = cu->lines;
1183 0 : *nlines = cu->lines->nlines;
1184 : }
1185 : return res;
1186 : }
1187 :
1188 0 : __libdw_seterrno (DWARF_E_NO_DEBUG_LINE);
1189 0 : return -1;
1190 : }
1191 :
1192 : /* Failsafe mode: no data found. */
1193 5328 : cu->lines = (void *) -1l;
1194 5328 : cu->files = (void *) -1l;
1195 :
1196 : /* The die must have a statement list associated. */
1197 : Dwarf_Attribute stmt_list_mem;
1198 5328 : Dwarf_Attribute *stmt_list = INTUSE(dwarf_attr) (cudie, DW_AT_stmt_list,
1199 : &stmt_list_mem);
1200 :
1201 : /* Get the offset into the .debug_line section. NB: this call
1202 : also checks whether the previous dwarf_attr call failed. */
1203 : Dwarf_Off debug_line_offset;
1204 5328 : if (__libdw_formptr (stmt_list, IDX_debug_line, DWARF_E_NO_DEBUG_LINE,
1205 : NULL, &debug_line_offset) == NULL)
1206 : return -1;
1207 :
1208 10656 : if (__libdw_getsrclines (cu->dbg, debug_line_offset,
1209 : __libdw_getcompdir (cudie),
1210 5328 : cu->address_size, &cu->lines, &cu->files) < 0)
1211 : return -1;
1212 : }
1213 32 : else if (cu->lines == (void *) -1l)
1214 : return -1;
1215 :
1216 5360 : *lines = cu->lines;
1217 5360 : *nlines = cu->lines->nlines;
1218 :
1219 : // XXX Eventually: unlocking here.
1220 :
1221 5360 : return 0;
1222 : }
1223 : INTDEF(dwarf_getsrclines)
|