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