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.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
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
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
18 or both in parallel, as here.
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.
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/>. */
45 struct filelist *next;
51 struct linelist *next;
56 /* Compare by Dwarf_Line.addr, given pointers into an array of pointers. */
58 compare_lines (const void *a, const void *b)
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;
67 if (line1->addr != line2->addr)
68 return (line1->addr < line2->addr) ? -1 : 1;
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;
74 /* Otherwise, the linelist sequence maintains a stable sort. */
75 return (list1->sequence < list2->sequence) ? -1
76 : (list1->sequence > list2->sequence) ? 1
83 unsigned int op_index;
92 unsigned int discriminator;
93 struct linelist *linelist;
95 unsigned int end_sequence;
97 unsigned int function_name;
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)
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;
110 add_new_line (struct line_state *state, struct linelist *new_line)
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);
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. */
127 new_line->line.field = state->field; \
128 if (unlikely (new_line->line.field != state->field)) \
141 SET (epilogue_begin);
153 read_srclines (Dwarf *dbg,
154 const unsigned char *linep, const unsigned char *lineendp,
155 const char *comp_dir, unsigned address_size,
156 Dwarf_Lines **linesp, Dwarf_Files **filesp)
160 struct filelist *filelist = NULL;
161 size_t nfilelist = 0;
164 /* If there are a large number of lines, files or dirs don't blow up
165 the stack. Stack allocate some entries, only dynamically malloc
166 when more than MAX. */
167 #define MAX_STACK_ALLOC 4096
168 #define MAX_STACK_LINES (MAX_STACK_ALLOC / 2)
169 #define MAX_STACK_FILES (MAX_STACK_ALLOC / 4)
170 #define MAX_STACK_DIRS (MAX_STACK_ALLOC / 16)
172 /* Initial statement program state (except for stmt_list, see below). */
173 struct line_state state =
180 /* We only store int but want to check for overflow (see SET above). */
183 .basic_block = false,
184 .prologue_end = false,
185 .epilogue_begin = false,
192 /* The dirs normally go on the stack, but if there are too many
193 we alloc them all. Set up stack storage early, so we can check on
194 error if we need to free them or not. */
200 struct dirlist dirstack[MAX_STACK_DIRS];
201 struct dirlist *dirarray = dirstack;
203 if (unlikely (linep + 4 > lineendp))
206 __libdw_seterrno (DWARF_E_INVALID_DEBUG_LINE);
210 Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
211 unsigned int length = 4;
212 if (unlikely (unit_length == DWARF3_LENGTH_64_BIT))
214 if (unlikely (linep + 8 > lineendp))
216 unit_length = read_8ubyte_unaligned_inc (dbg, linep);
220 /* Check whether we have enough room in the section. */
221 if (unlikely (unit_length > (size_t) (lineendp - linep)))
223 lineendp = linep + unit_length;
225 /* The next element of the header is the version identifier. */
226 if ((size_t) (lineendp - linep) < 2)
228 uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
229 if (unlikely (version < 2) || unlikely (version > 5))
231 __libdw_seterrno (DWARF_E_VERSION);
235 /* DWARF5 explicitly lists address and segment_selector sizes. */
238 if ((size_t) (lineendp - linep) < 2)
240 size_t line_address_size = *linep++;
241 size_t segment_selector_size = *linep++;
242 if (line_address_size != address_size || segment_selector_size != 0)
246 /* Next comes the header length. */
247 Dwarf_Word header_length;
250 if ((size_t) (lineendp - linep) < 4)
252 header_length = read_4ubyte_unaligned_inc (dbg, linep);
256 if ((size_t) (lineendp - linep) < 8)
258 header_length = read_8ubyte_unaligned_inc (dbg, linep);
260 const unsigned char *header_start = linep;
262 /* Next the minimum instruction length. */
263 uint_fast8_t minimum_instr_len = *linep++;
265 /* Next the maximum operations per instruction, in version 4 format. */
266 uint_fast8_t max_ops_per_instr = 1;
269 if (unlikely ((size_t) (lineendp - linep) < 1))
271 max_ops_per_instr = *linep++;
272 if (unlikely (max_ops_per_instr == 0))
276 /* 4 more bytes, is_stmt, line_base, line_range and opcode_base. */
277 if ((size_t) (lineendp - linep) < 4)
280 /* Then the flag determining the default value of the is_stmt
282 uint_fast8_t default_is_stmt = *linep++;
284 /* Now the line base. */
285 int_fast8_t line_base = (int8_t) *linep++;
287 /* And the line range. */
288 uint_fast8_t line_range = *linep++;
290 /* The opcode base. */
291 uint_fast8_t opcode_base = *linep++;
293 /* Remember array with the standard opcode length (-1 to account for
294 the opcode with value zero not being mentioned). */
295 const uint8_t *standard_opcode_lengths = linep - 1;
296 if (unlikely (lineendp - linep < opcode_base - 1))
298 linep += opcode_base - 1;
300 /* To read DWARF5 dir and file lists we need to know the forms. For
301 now we skip everything, except the DW_LNCT_path and
302 DW_LNCT_directory_index. */
304 unsigned char nforms = 0;
305 unsigned char form_path = -1; /* Which forms is DW_LNCT_path. */
306 unsigned char form_idx = -1; /* And which is DW_LNCT_directory_index. */
308 /* To read/skip form data. */
311 .sec_idx = IDX_debug_line,
313 .offset_size = length,
314 .address_size = address_size,
315 .startp = (void *) linep,
316 .endp = (void *) lineendp,
319 /* First count the entries. */
323 const unsigned char *dirp = linep;
324 while (dirp < lineendp && *dirp != 0)
326 uint8_t *endp = memchr (dirp, '\0', lineendp - dirp);
332 if (dirp >= lineendp || *dirp != '\0')
334 ndirs = ndirs + 1; /* There is always the "unknown" dir. */
338 if ((size_t) (lineendp - linep) < 1)
341 for (int i = 0; i < nforms; i++)
344 if ((size_t) (lineendp - linep) < 1)
346 get_uleb128 (desc, linep, lineendp);
347 if ((size_t) (lineendp - linep) < 1)
349 get_uleb128 (form, linep, lineendp);
351 if (! libdw_valid_user_form (form))
355 if (desc == DW_LNCT_path)
359 if (nforms > 0 && form_path == (unsigned char) -1)
362 if ((size_t) (lineendp - linep) < 1)
364 get_uleb128 (ndirs, linep, lineendp);
366 if (nforms == 0 && ndirs != 0)
369 /* Assume there is at least 1 byte needed per form to describe
370 the directory. Filters out insanely large ndirs. */
371 if (nforms != 0 && ndirs > (size_t) (lineendp - linep) / nforms)
375 /* Arrange the list in array form. */
377 if (ndirlist >= MAX_STACK_DIRS)
379 if (ndirlist > SIZE_MAX / sizeof (*dirarray))
381 dirarray = malloc (ndirlist * sizeof (*dirarray));
382 if (unlikely (dirarray == NULL))
385 __libdw_seterrno (DWARF_E_NOMEM);
390 /* Entry zero is implicit for older versions, but explicit for 5+. */
391 struct dirlist comp_dir_elem;
394 /* First comes the list of directories. Add the compilation
395 directory first since the index zero is used for it. */
396 comp_dir_elem.dir = comp_dir;
397 comp_dir_elem.len = comp_dir ? strlen (comp_dir) : 0,
398 dirarray[0] = comp_dir_elem;
399 for (unsigned int n = 1; n < ndirlist; n++)
401 dirarray[n].dir = (char *) linep;
402 uint8_t *endp = memchr (linep, '\0', lineendp - linep);
403 assert (endp != NULL); // Checked above when calculating ndirlist.
404 dirarray[n].len = endp - linep;
407 /* Skip the final NUL byte. */
408 assert (*linep == '\0'); // Checked above when calculating ndirlist.
413 Dwarf_Attribute attr;
414 attr.code = DW_AT_name;
416 for (unsigned int n = 0; n < ndirlist; n++)
418 const char *dir = NULL;
419 for (unsigned char m = 0; m < nforms; m++)
423 attr.form = forms[m];
424 attr.valp = (void *) linep;
425 dir = dwarf_formstring (&attr);
428 size_t len = __libdw_form_val_len (&fake_cu, forms[m], linep);
429 if ((size_t) (lineendp - linep) < len)
438 dirarray[n].dir = dir;
439 dirarray[n].len = strlen (dir);
443 /* File index zero doesn't exist for DWARF < 5. Files are indexed
444 starting from 1. But for DWARF5 they are indexed starting from
445 zero, but the default index is still 1. In both cases the
446 "first" file is special and refers to the main compile unit file,
447 equal to the DW_AT_name of the DW_TAG_compile_unit. */
448 struct filelist null_file =
458 filelist = &null_file;
461 /* Allocate memory for a new file. For the first MAX_STACK_FILES
462 entries just return a slot in the preallocated stack array.
463 This is slightly complicated because in DWARF < 5 new files could
464 be defined with DW_LNE_define_file after the normal file list was
466 struct filelist flstack[MAX_STACK_FILES];
467 #define NEW_FILE() ({ \
468 struct filelist *fl = (nfilelist < MAX_STACK_FILES \
469 ? &flstack[nfilelist] \
470 : malloc (sizeof (struct filelist))); \
471 if (unlikely (fl == NULL)) \
474 fl->next = filelist; \
478 /* Now read the files. */
481 if (unlikely (linep >= lineendp))
483 while (linep < lineendp && *linep != '\0')
485 struct filelist *new_file = NEW_FILE ();
487 /* First comes the file name. */
488 char *fname = (char *) linep;
489 uint8_t *endp = memchr (fname, '\0', lineendp - linep);
492 size_t fnamelen = endp - (uint8_t *) fname;
495 /* Then the index. */
497 if (unlikely (linep >= lineendp))
499 get_uleb128 (diridx, linep, lineendp);
500 if (unlikely (diridx >= ndirlist))
502 __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
507 /* It's an absolute path. */
508 new_file->info.name = fname;
511 new_file->info.name = libdw_alloc (dbg, char, 1,
512 dirarray[diridx].len + 1
514 char *cp = new_file->info.name;
516 if (dirarray[diridx].dir != NULL)
518 /* This value could be NULL in case the DW_AT_comp_dir
519 was not present. We cannot do much in this case.
520 Just keep the file relative. */
521 cp = stpcpy (cp, dirarray[diridx].dir);
525 assert (strlen (new_file->info.name)
526 < dirarray[diridx].len + 1 + fnamelen + 1);
529 /* Next comes the modification time. */
530 if (unlikely (linep >= lineendp))
532 get_uleb128 (new_file->info.mtime, linep, lineendp);
534 /* Finally the length of the file. */
535 if (unlikely (linep >= lineendp))
537 get_uleb128 (new_file->info.length, linep, lineendp);
539 if (linep >= lineendp || *linep != '\0')
541 /* Skip the final NUL byte. */
546 if ((size_t) (lineendp - linep) < 1)
549 form_path = form_idx = -1;
550 for (int i = 0; i < nforms; i++)
553 if ((size_t) (lineendp - linep) < 1)
555 get_uleb128 (desc, linep, lineendp);
556 if ((size_t) (lineendp - linep) < 1)
558 get_uleb128 (form, linep, lineendp);
560 if (! libdw_valid_user_form (form))
564 if (desc == DW_LNCT_path)
566 else if (desc == DW_LNCT_directory_index)
570 if (nforms > 0 && (form_path == (unsigned char) -1
571 || form_idx == (unsigned char) -1))
575 get_uleb128 (nfiles, linep, lineendp);
577 if (nforms == 0 && nfiles != 0)
580 /* Assume there is at least 1 byte needed per form to describe
581 the file. Filters out insanely large nfiles. */
582 if (nforms != 0 && nfiles > (size_t) (lineendp - linep) / nforms)
585 Dwarf_Attribute attr;
587 for (unsigned int n = 0; n < nfiles; n++)
589 const char *fname = NULL;
590 Dwarf_Word diridx = (Dwarf_Word) -1;
591 for (unsigned char m = 0; m < nforms; m++)
595 attr.code = DW_AT_name;
596 attr.form = forms[m];
597 attr.valp = (void *) linep;
598 fname = dwarf_formstring (&attr);
600 else if (m == form_idx)
602 attr.code = DW_AT_decl_file; /* Close enough. */
603 attr.form = forms[m];
604 attr.valp = (void *) linep;
605 if (dwarf_formudata (&attr, &diridx) != 0)
606 diridx = (Dwarf_Word) -1;
609 size_t len = __libdw_form_val_len (&fake_cu, forms[m], linep);
610 if ((size_t) (lineendp - linep) < len)
616 if (fname == NULL || diridx == (Dwarf_Word) -1)
619 size_t fnamelen = strlen (fname);
621 if (unlikely (diridx >= ndirlist))
623 __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
627 /* Yes, weird. Looks like an off-by-one in the spec. */
628 struct filelist *new_file = n == 0 ? &null_file : NEW_FILE ();
630 /* We follow the same rules as above for DWARF < 5, even
631 though the standard doesn't explicitly mention absolute
632 paths and ignoring the dir index. */
634 /* It's an absolute path. */
635 new_file->info.name = (char *) fname;
638 new_file->info.name = libdw_alloc (dbg, char, 1,
639 dirarray[diridx].len + 1
641 char *cp = new_file->info.name;
643 /* In the DWARF >= 5 case, dir can never be NULL. */
644 cp = stpcpy (cp, dirarray[diridx].dir);
647 assert (strlen (new_file->info.name)
648 < dirarray[diridx].len + 1 + fnamelen + 1);
651 /* For now we just ignore the modification time and file length. */
652 new_file->info.mtime = 0;
653 new_file->info.length = 0;
657 unsigned int debug_str_offset = 0;
658 if (unlikely (linep == header_start + header_length - 4))
660 /* CUBINs contain an unsigned 4-byte offset */
661 debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
664 /* Consistency check. */
665 if (unlikely (linep != header_start + header_length))
667 __libdw_seterrno (DWARF_E_INVALID_DWARF);
671 /* We are about to process the statement program. Most state machine
672 registers have already been initialize above. Just add the is_stmt
673 default. See 6.2.2 in the v2.1 specification. */
674 state.is_stmt = default_is_stmt;
676 /* Apply the "operation advance" from a special opcode or
677 DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
678 #define advance_pc(op_advance) \
679 run_advance_pc (&state, op_advance, minimum_instr_len, max_ops_per_instr)
681 /* Process the instructions. */
683 /* Adds a new line to the matrix. For the first MAX_STACK_LINES
684 entries just return a slot in the preallocated stack array. */
685 struct linelist llstack[MAX_STACK_LINES];
686 #define NEW_LINE(end_seq) \
688 struct linelist *ll = (state.nlinelist < MAX_STACK_LINES \
689 ? &llstack[state.nlinelist] \
690 : malloc (sizeof (struct linelist))); \
691 if (unlikely (ll == NULL)) \
693 state.end_sequence = end_seq; \
694 if (unlikely (add_new_line (&state, ll))) \
698 while (linep < lineendp)
704 /* Read the opcode. */
707 /* Is this a special opcode? */
708 if (likely (opcode >= opcode_base))
710 if (unlikely (line_range == 0))
713 /* Yes. Handling this is quite easy since the opcode value
716 opcode = (desired line increment - line_base)
717 + (line_range * address advance) + opcode_base
719 int line_increment = (line_base
720 + (opcode - opcode_base) % line_range);
722 /* Perform the increments. */
723 state.line += line_increment;
724 advance_pc ((opcode - opcode_base) / line_range);
726 /* Add a new line with the current state machine values. */
729 /* Reset the flags. */
730 state.basic_block = false;
731 state.prologue_end = false;
732 state.epilogue_begin = false;
733 state.discriminator = 0;
735 else if (opcode == 0)
737 /* This an extended opcode. */
738 if (unlikely (lineendp - linep < 2))
742 uint_fast8_t len = *linep++;
744 if (unlikely ((size_t) (lineendp - linep) < len))
747 /* The sub-opcode. */
752 case DW_LNE_end_sequence:
753 /* Add a new line with the current state machine values.
754 The is the end of the sequence. */
757 /* Reset the registers. */
763 state.is_stmt = default_is_stmt;
764 state.basic_block = false;
765 state.prologue_end = false;
766 state.epilogue_begin = false;
768 state.discriminator = 0;
770 state.function_name = 0;
773 case DW_LNE_set_address:
774 /* The value is an address. The size is defined as
775 appropriate for the target machine. We use the
776 address size field from the CU header. */
778 if (unlikely (lineendp - linep < (uint8_t) address_size))
780 if (__libdw_read_address_inc (dbg, IDX_debug_line, &linep,
781 address_size, &state.addr))
785 case DW_LNE_define_file:
787 char *fname = (char *) linep;
788 uint8_t *endp = memchr (linep, '\0', lineendp - linep);
791 size_t fnamelen = endp - linep;
795 if (unlikely (linep >= lineendp))
797 get_uleb128 (diridx, linep, lineendp);
798 if (unlikely (diridx >= ndirlist))
800 __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
804 if (unlikely (linep >= lineendp))
806 get_uleb128 (mtime, linep, lineendp);
807 Dwarf_Word filelength;
808 if (unlikely (linep >= lineendp))
810 get_uleb128 (filelength, linep, lineendp);
812 struct filelist *new_file = NEW_FILE ();
814 new_file->info.name = fname;
817 new_file->info.name =
818 libdw_alloc (dbg, char, 1, (dirarray[diridx].len + 1
820 char *cp = new_file->info.name;
822 if (dirarray[diridx].dir != NULL)
823 /* This value could be NULL in case the
824 DW_AT_comp_dir was not present. We
825 cannot do much in this case. Just
826 keep the file relative. */
828 cp = stpcpy (cp, dirarray[diridx].dir);
834 new_file->info.mtime = mtime;
835 new_file->info.length = filelength;
839 case DW_LNE_set_discriminator:
840 /* Takes one ULEB128 parameter, the discriminator. */
841 if (unlikely (standard_opcode_lengths[opcode] != 1))
844 if (unlikely (linep >= lineendp))
846 get_uleb128 (state.discriminator, linep, lineendp);
849 case DW_LNE_NVIDIA_inlined_call:
850 if (unlikely (linep >= lineendp))
852 get_uleb128 (state.context, linep, lineendp);
853 if (unlikely (linep >= lineendp))
855 get_uleb128 (state.function_name, linep, lineendp);
856 state.function_name += debug_str_offset;
859 case DW_LNE_NVIDIA_set_function_name:
860 if (unlikely (linep >= lineendp))
862 get_uleb128 (state.function_name, linep, lineendp);
863 state.function_name += debug_str_offset;
867 /* Unknown, ignore it. */
868 if (unlikely ((size_t) (lineendp - (linep - 1)) < len))
874 else if (opcode <= DW_LNS_set_isa)
876 /* This is a known standard opcode. */
880 /* Takes no argument. */
881 if (unlikely (standard_opcode_lengths[opcode] != 0))
884 /* Add a new line with the current state machine values. */
887 /* Reset the flags. */
888 state.basic_block = false;
889 state.prologue_end = false;
890 state.epilogue_begin = false;
891 state.discriminator = 0;
894 case DW_LNS_advance_pc:
895 /* Takes one uleb128 parameter which is added to the
897 if (unlikely (standard_opcode_lengths[opcode] != 1))
900 if (unlikely (linep >= lineendp))
902 get_uleb128 (u128, linep, lineendp);
906 case DW_LNS_advance_line:
907 /* Takes one sleb128 parameter which is added to the
909 if (unlikely (standard_opcode_lengths[opcode] != 1))
912 if (unlikely (linep >= lineendp))
914 get_sleb128 (s128, linep, lineendp);
918 case DW_LNS_set_file:
919 /* Takes one uleb128 parameter which is stored in file. */
920 if (unlikely (standard_opcode_lengths[opcode] != 1))
923 if (unlikely (linep >= lineendp))
925 get_uleb128 (u128, linep, lineendp);
929 case DW_LNS_set_column:
930 /* Takes one uleb128 parameter which is stored in column. */
931 if (unlikely (standard_opcode_lengths[opcode] != 1))
934 if (unlikely (linep >= lineendp))
936 get_uleb128 (u128, linep, lineendp);
940 case DW_LNS_negate_stmt:
941 /* Takes no argument. */
942 if (unlikely (standard_opcode_lengths[opcode] != 0))
945 state.is_stmt = 1 - state.is_stmt;
948 case DW_LNS_set_basic_block:
949 /* Takes no argument. */
950 if (unlikely (standard_opcode_lengths[opcode] != 0))
953 state.basic_block = true;
956 case DW_LNS_const_add_pc:
957 /* Takes no argument. */
958 if (unlikely (standard_opcode_lengths[opcode] != 0))
961 if (unlikely (line_range == 0))
964 advance_pc ((255 - opcode_base) / line_range);
967 case DW_LNS_fixed_advance_pc:
968 /* Takes one 16 bit parameter which is added to the
970 if (unlikely (standard_opcode_lengths[opcode] != 1)
971 || unlikely (lineendp - linep < 2))
974 state.addr += read_2ubyte_unaligned_inc (dbg, linep);
978 case DW_LNS_set_prologue_end:
979 /* Takes no argument. */
980 if (unlikely (standard_opcode_lengths[opcode] != 0))
983 state.prologue_end = true;
986 case DW_LNS_set_epilogue_begin:
987 /* Takes no argument. */
988 if (unlikely (standard_opcode_lengths[opcode] != 0))
991 state.epilogue_begin = true;
995 /* Takes one uleb128 parameter which is stored in isa. */
996 if (unlikely (standard_opcode_lengths[opcode] != 1))
999 if (unlikely (linep >= lineendp))
1001 get_uleb128 (state.isa, linep, lineendp);
1007 /* This is a new opcode the generator but not we know about.
1008 Read the parameters associated with it but then discard
1009 everything. Read all the parameters for this opcode. */
1010 for (int n = standard_opcode_lengths[opcode]; n > 0; --n)
1012 if (unlikely (linep >= lineendp))
1014 get_uleb128 (u128, linep, lineendp);
1017 /* Next round, ignore this opcode. */
1022 /* Put all the files in an array. */
1023 Dwarf_Files *files = libdw_alloc (dbg, Dwarf_Files,
1024 sizeof (Dwarf_Files)
1025 + nfilelist * sizeof (Dwarf_Fileinfo)
1026 + (ndirlist + 1) * sizeof (char *),
1028 const char **dirs = (void *) &files->info[nfilelist];
1030 struct filelist *fileslist = filelist;
1031 files->nfiles = nfilelist;
1032 for (size_t n = nfilelist; n > 0; n--)
1034 files->info[n - 1] = fileslist->info;
1035 fileslist = fileslist->next;
1037 assert (fileslist == NULL);
1039 /* Put all the directory strings in an array. */
1040 files->ndirs = ndirlist;
1041 for (unsigned int i = 0; i < ndirlist; ++i)
1042 dirs[i] = dirarray[i].dir;
1043 dirs[ndirlist] = NULL;
1045 /* Pass the file data structure to the caller. */
1049 size_t buf_size = (sizeof (Dwarf_Lines)
1050 + (sizeof (Dwarf_Line) * state.nlinelist));
1051 void *buf = libdw_alloc (dbg, Dwarf_Lines, buf_size, 1);
1053 /* First use the buffer for the pointers, and sort the entries.
1054 We'll write the pointers in the end of the buffer, and then
1055 copy into the buffer from the beginning so the overlap works. */
1056 assert (sizeof (Dwarf_Line) >= sizeof (struct linelist *));
1057 struct linelist **sortlines = (buf + buf_size
1058 - sizeof (struct linelist **) * state.nlinelist);
1060 /* The list is in LIFO order and usually they come in clumps with
1061 ascending addresses. So fill from the back to probably start with
1062 runs already in order before we sort. */
1063 struct linelist *lineslist = state.linelist;
1064 for (size_t i = state.nlinelist; i-- > 0; )
1066 sortlines[i] = lineslist;
1067 lineslist = lineslist->next;
1069 assert (lineslist == NULL);
1071 /* Sort by ascending address. */
1072 qsort (sortlines, state.nlinelist, sizeof sortlines[0], &compare_lines);
1074 /* Now that they are sorted, put them in the final array.
1075 The buffers overlap, so we've clobbered the early elements
1076 of SORTLINES by the time we're reading the later ones. */
1077 Dwarf_Lines *lines = buf;
1078 lines->nlines = state.nlinelist;
1079 for (size_t i = 0; i < state.nlinelist; ++i)
1081 lines->info[i] = sortlines[i]->line;
1082 lines->info[i].files = files;
1085 /* Make sure the highest address for the CU is marked as end_sequence.
1086 This is required by the DWARF spec, but some compilers forget and
1087 dwfl_module_getsrc depends on it. */
1088 if (state.nlinelist > 0)
1089 lines->info[state.nlinelist - 1].end_sequence = 1;
1091 /* Pass the line structure back to the caller. */
1099 /* Free malloced line records, if any. */
1100 for (size_t i = MAX_STACK_LINES; i < state.nlinelist; i++)
1102 struct linelist *ll = state.linelist->next;
1103 free (state.linelist);
1104 state.linelist = ll;
1106 if (dirarray != dirstack)
1108 for (size_t i = MAX_STACK_FILES; i < nfilelist; i++)
1110 struct filelist *fl = filelist->next;
1119 files_lines_compare (const void *p1, const void *p2)
1121 const struct files_lines_s *t1 = p1;
1122 const struct files_lines_s *t2 = p2;
1124 if (t1->debug_line_offset < t2->debug_line_offset)
1126 if (t1->debug_line_offset > t2->debug_line_offset)
1134 __libdw_getsrclines (Dwarf *dbg, Dwarf_Off debug_line_offset,
1135 const char *comp_dir, unsigned address_size,
1136 Dwarf_Lines **linesp, Dwarf_Files **filesp)
1138 struct files_lines_s fake = { .debug_line_offset = debug_line_offset };
1139 struct files_lines_s **found = tfind (&fake, &dbg->files_lines,
1140 files_lines_compare);
1143 Elf_Data *data = __libdw_checked_get_data (dbg, IDX_debug_line);
1145 || __libdw_offset_in_section (dbg, IDX_debug_line,
1146 debug_line_offset, 1) != 0)
1149 const unsigned char *linep = data->d_buf + debug_line_offset;
1150 const unsigned char *lineendp = data->d_buf + data->d_size;
1152 struct files_lines_s *node = libdw_alloc (dbg, struct files_lines_s,
1155 if (read_srclines (dbg, linep, lineendp, comp_dir, address_size,
1156 &node->lines, &node->files) != 0)
1159 node->debug_line_offset = debug_line_offset;
1161 found = tsearch (node, &dbg->files_lines, files_lines_compare);
1164 __libdw_seterrno (DWARF_E_NOMEM);
1170 *linesp = (*found)->lines;
1173 *filesp = (*found)->files;
1178 /* Get the compilation directory, if any is set. */
1180 __libdw_getcompdir (Dwarf_Die *cudie)
1182 Dwarf_Attribute compdir_attr_mem;
1183 Dwarf_Attribute *compdir_attr = INTUSE(dwarf_attr) (cudie,
1186 return INTUSE(dwarf_formstring) (compdir_attr);
1190 dwarf_getsrclines (Dwarf_Die *cudie, Dwarf_Lines **lines, size_t *nlines)
1194 if (! is_cudie (cudie))
1196 __libdw_seterrno (DWARF_E_NOT_CUDIE);
1200 /* Get the information if it is not already known. */
1201 struct Dwarf_CU *const cu = cudie->cu;
1202 if (cu->lines == NULL)
1204 /* For split units always pick the lines from the skeleton. */
1205 if (cu->unit_type == DW_UT_split_compile
1206 || cu->unit_type == DW_UT_split_type)
1208 /* We tries, assume we fail... */
1209 cu->lines = (void *) -1l;
1211 Dwarf_CU *skel = __libdw_find_split_unit (cu);
1214 Dwarf_Die skeldie = CUDIE (skel);
1215 int res = INTUSE(dwarf_getsrclines) (&skeldie, lines, nlines);
1218 cu->lines = skel->lines;
1220 *nlines = cu->lines->nlines;
1225 __libdw_seterrno (DWARF_E_NO_DEBUG_LINE);
1229 /* Failsafe mode: no data found. */
1230 cu->lines = (void *) -1l;
1231 cu->files = (void *) -1l;
1233 /* The die must have a statement list associated. */
1234 Dwarf_Attribute stmt_list_mem;
1235 Dwarf_Attribute *stmt_list = INTUSE(dwarf_attr) (cudie, DW_AT_stmt_list,
1238 /* Get the offset into the .debug_line section. NB: this call
1239 also checks whether the previous dwarf_attr call failed. */
1240 Dwarf_Off debug_line_offset;
1241 if (__libdw_formptr (stmt_list, IDX_debug_line, DWARF_E_NO_DEBUG_LINE,
1242 NULL, &debug_line_offset) == NULL)
1245 if (__libdw_getsrclines (cu->dbg, debug_line_offset,
1246 __libdw_getcompdir (cudie),
1247 cu->address_size, &cu->lines, &cu->files) < 0)
1250 else if (cu->lines == (void *) -1l)
1254 *nlines = cu->lines->nlines;
1256 // XXX Eventually: unlocking here.
1260 INTDEF(dwarf_getsrclines)