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