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;
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)
102 state->addr += minimum_instr_len * ((state->op_index + op_advance)
103 / max_ops_per_instr);
104 state->op_index = (state->op_index + op_advance) % max_ops_per_instr;
108 add_new_line (struct line_state *state, struct linelist *new_line)
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 new_line->next = state->linelist;
115 new_line->sequence = state->nlinelist;
116 state->linelist = new_line;
117 ++(state->nlinelist);
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. */
125 new_line->line.field = state->field; \
126 if (unlikely (new_line->line.field != state->field)) \
139 SET (epilogue_begin);
149 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)
156 struct filelist *filelist = NULL;
157 size_t nfilelist = 0;
160 /* If there are a large number of lines, files or dirs don't blow up
161 the stack. Stack allocate some entries, only dynamically malloc
162 when more than MAX. */
163 #define MAX_STACK_ALLOC 4096
164 #define MAX_STACK_LINES MAX_STACK_ALLOC
165 #define MAX_STACK_FILES (MAX_STACK_ALLOC / 4)
166 #define MAX_STACK_DIRS (MAX_STACK_ALLOC / 16)
168 /* Initial statement program state (except for stmt_list, see below). */
169 struct line_state state =
176 /* We only store int but want to check for overflow (see SET above). */
179 .basic_block = false,
180 .prologue_end = false,
181 .epilogue_begin = false,
186 /* The dirs normally go on the stack, but if there are too many
187 we alloc them all. Set up stack storage early, so we can check on
188 error if we need to free them or not. */
194 struct dirlist dirstack[MAX_STACK_DIRS];
195 struct dirlist *dirarray = dirstack;
197 if (unlikely (linep + 4 > lineendp))
200 __libdw_seterrno (DWARF_E_INVALID_DEBUG_LINE);
204 Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
205 unsigned int length = 4;
206 if (unlikely (unit_length == DWARF3_LENGTH_64_BIT))
208 if (unlikely (linep + 8 > lineendp))
210 unit_length = read_8ubyte_unaligned_inc (dbg, linep);
214 /* Check whether we have enough room in the section. */
215 if (unlikely (unit_length > (size_t) (lineendp - linep)))
217 lineendp = linep + unit_length;
219 /* The next element of the header is the version identifier. */
220 if ((size_t) (lineendp - linep) < 2)
222 uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
223 if (unlikely (version < 2) || unlikely (version > 5))
225 __libdw_seterrno (DWARF_E_VERSION);
229 /* DWARF5 explicitly lists address and segment_selector sizes. */
232 if ((size_t) (lineendp - linep) < 2)
234 size_t line_address_size = *linep++;
235 size_t segment_selector_size = *linep++;
236 if (line_address_size != address_size || segment_selector_size != 0)
240 /* Next comes the header length. */
241 Dwarf_Word header_length;
244 if ((size_t) (lineendp - linep) < 4)
246 header_length = read_4ubyte_unaligned_inc (dbg, linep);
250 if ((size_t) (lineendp - linep) < 8)
252 header_length = read_8ubyte_unaligned_inc (dbg, linep);
254 const unsigned char *header_start = linep;
256 /* Next the minimum instruction length. */
257 uint_fast8_t minimum_instr_len = *linep++;
259 /* Next the maximum operations per instruction, in version 4 format. */
260 uint_fast8_t max_ops_per_instr = 1;
263 if (unlikely ((size_t) (lineendp - linep) < 1))
265 max_ops_per_instr = *linep++;
266 if (unlikely (max_ops_per_instr == 0))
270 /* 4 more bytes, is_stmt, line_base, line_range and opcode_base. */
271 if ((size_t) (lineendp - linep) < 4)
274 /* Then the flag determining the default value of the is_stmt
276 uint_fast8_t default_is_stmt = *linep++;
278 /* Now the line base. */
279 int_fast8_t line_base = (int8_t) *linep++;
281 /* And the line range. */
282 uint_fast8_t line_range = *linep++;
284 /* The opcode base. */
285 uint_fast8_t opcode_base = *linep++;
287 /* Remember array with the standard opcode length (-1 to account for
288 the opcode with value zero not being mentioned). */
289 const uint8_t *standard_opcode_lengths = linep - 1;
290 if (unlikely (lineendp - linep < opcode_base - 1))
292 linep += opcode_base - 1;
294 /* To read DWARF5 dir and file lists we need to know the forms. For
295 now we skip everything, except the DW_LNCT_path and
296 DW_LNCT_directory_index. */
298 unsigned char nforms = 0;
299 unsigned char form_path = -1; /* Which forms is DW_LNCT_path. */
300 unsigned char form_idx = -1; /* And which is DW_LNCT_directory_index. */
302 /* To read/skip form data. */
305 .sec_idx = IDX_debug_line,
307 .offset_size = length,
308 .address_size = address_size,
309 .startp = (void *) linep,
310 .endp = (void *) lineendp,
313 /* First count the entries. */
317 const unsigned char *dirp = linep;
318 while (dirp < lineendp && *dirp != 0)
320 uint8_t *endp = memchr (dirp, '\0', lineendp - dirp);
326 if (dirp >= lineendp || *dirp != '\0')
328 ndirs = ndirs + 1; /* There is always the "unknown" dir. */
332 if ((size_t) (lineendp - linep) < 1)
335 for (int i = 0; i < nforms; i++)
338 if ((size_t) (lineendp - linep) < 1)
340 get_uleb128 (desc, linep, lineendp);
341 if ((size_t) (lineendp - linep) < 1)
343 get_uleb128 (form, linep, lineendp);
345 if (! libdw_valid_user_form (form))
349 if (desc == DW_LNCT_path)
353 if (nforms > 0 && form_path == (unsigned char) -1)
356 if ((size_t) (lineendp - linep) < 1)
358 get_uleb128 (ndirs, linep, lineendp);
360 if (nforms == 0 && ndirs != 0)
363 /* Assume there is at least 1 byte needed per form to describe
364 the directory. Filters out insanely large ndirs. */
365 if (nforms != 0 && ndirs > (size_t) (lineendp - linep) / nforms)
369 /* Arrange the list in array form. */
371 if (ndirlist >= MAX_STACK_DIRS)
373 if (ndirlist > SIZE_MAX / sizeof (*dirarray))
375 dirarray = (struct dirlist *) malloc (ndirlist * sizeof (*dirarray));
376 if (unlikely (dirarray == NULL))
379 __libdw_seterrno (DWARF_E_NOMEM);
384 /* Entry zero is implicit for older versions, but explicit for 5+. */
385 struct dirlist comp_dir_elem;
388 /* First comes the list of directories. Add the compilation
389 directory first since the index zero is used for it. */
390 comp_dir_elem.dir = comp_dir;
391 comp_dir_elem.len = comp_dir ? strlen (comp_dir) : 0,
392 dirarray[0] = comp_dir_elem;
393 for (unsigned int n = 1; n < ndirlist; n++)
395 dirarray[n].dir = (char *) linep;
396 uint8_t *endp = memchr (linep, '\0', lineendp - linep);
397 assert (endp != NULL); // Checked above when calculating ndirlist.
398 dirarray[n].len = endp - linep;
401 /* Skip the final NUL byte. */
402 assert (*linep == '\0'); // Checked above when calculating ndirlist.
407 Dwarf_Attribute attr;
408 attr.code = DW_AT_name;
410 for (unsigned int n = 0; n < ndirlist; n++)
412 const char *dir = NULL;
413 for (unsigned char m = 0; m < nforms; m++)
417 attr.form = forms[m];
418 attr.valp = (void *) linep;
419 dir = dwarf_formstring (&attr);
422 size_t len = __libdw_form_val_len (&fake_cu, forms[m], linep);
423 if ((size_t) (lineendp - linep) < len)
432 dirarray[n].dir = dir;
433 dirarray[n].len = strlen (dir);
437 /* File index zero doesn't exist for DWARF < 5. Files are indexed
438 starting from 1. But for DWARF5 they are indexed starting from
439 zero, but the default index is still 1. In both cases the
440 "first" file is special and refers to the main compile unit file,
441 equal to the DW_AT_name of the DW_TAG_compile_unit. */
442 struct filelist null_file =
452 filelist = &null_file;
455 /* Allocate memory for a new file. For the first MAX_STACK_FILES
456 entries just return a slot in the preallocated stack array.
457 This is slightly complicated because in DWARF < 5 new files could
458 be defined with DW_LNE_define_file after the normal file list was
460 struct filelist flstack[MAX_STACK_FILES];
461 #define NEW_FILE() ({ \
462 struct filelist *fl = (nfilelist < MAX_STACK_FILES \
463 ? &flstack[nfilelist] \
464 : malloc (sizeof (struct filelist))); \
465 if (unlikely (fl == NULL)) \
468 fl->next = filelist; \
472 /* Now read the files. */
475 if (unlikely (linep >= lineendp))
477 while (linep < lineendp && *linep != '\0')
479 struct filelist *new_file = NEW_FILE ();
481 /* First comes the file name. */
482 char *fname = (char *) linep;
483 uint8_t *endp = memchr (fname, '\0', lineendp - linep);
486 size_t fnamelen = endp - (uint8_t *) fname;
489 /* Then the index. */
491 if (unlikely (linep >= lineendp))
493 get_uleb128 (diridx, linep, lineendp);
494 if (unlikely (diridx >= ndirlist))
496 __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
501 /* It's an absolute path. */
502 new_file->info.name = fname;
505 new_file->info.name = libdw_alloc (dbg, char, 1,
506 dirarray[diridx].len + 1
508 char *cp = new_file->info.name;
510 if (dirarray[diridx].dir != NULL)
512 /* This value could be NULL in case the DW_AT_comp_dir
513 was not present. We cannot do much in this case.
514 Just keep the file relative. */
515 cp = stpcpy (cp, dirarray[diridx].dir);
519 assert (strlen (new_file->info.name)
520 < dirarray[diridx].len + 1 + fnamelen + 1);
523 /* Next comes the modification time. */
524 if (unlikely (linep >= lineendp))
526 get_uleb128 (new_file->info.mtime, linep, lineendp);
528 /* Finally the length of the file. */
529 if (unlikely (linep >= lineendp))
531 get_uleb128 (new_file->info.length, linep, lineendp);
533 if (linep >= lineendp || *linep != '\0')
535 /* Skip the final NUL byte. */
540 if ((size_t) (lineendp - linep) < 1)
543 form_path = form_idx = -1;
544 for (int i = 0; i < nforms; i++)
547 if ((size_t) (lineendp - linep) < 1)
549 get_uleb128 (desc, linep, lineendp);
550 if ((size_t) (lineendp - linep) < 1)
552 get_uleb128 (form, linep, lineendp);
554 if (! libdw_valid_user_form (form))
558 if (desc == DW_LNCT_path)
560 else if (desc == DW_LNCT_directory_index)
564 if (nforms > 0 && (form_path == (unsigned char) -1
565 || form_idx == (unsigned char) -1))
569 get_uleb128 (nfiles, linep, lineendp);
571 if (nforms == 0 && nfiles != 0)
574 /* Assume there is at least 1 byte needed per form to describe
575 the file. Filters out insanely large nfiles. */
576 if (nforms != 0 && nfiles > (size_t) (lineendp - linep) / nforms)
579 Dwarf_Attribute attr;
581 for (unsigned int n = 0; n < nfiles; n++)
583 const char *fname = NULL;
584 Dwarf_Word diridx = (Dwarf_Word) -1;
585 for (unsigned char m = 0; m < nforms; m++)
589 attr.code = DW_AT_name;
590 attr.form = forms[m];
591 attr.valp = (void *) linep;
592 fname = dwarf_formstring (&attr);
594 else if (m == form_idx)
596 attr.code = DW_AT_decl_file; /* Close enough. */
597 attr.form = forms[m];
598 attr.valp = (void *) linep;
599 if (dwarf_formudata (&attr, &diridx) != 0)
600 diridx = (Dwarf_Word) -1;
603 size_t len = __libdw_form_val_len (&fake_cu, forms[m], linep);
604 if ((size_t) (lineendp - linep) < len)
610 if (fname == NULL || diridx == (Dwarf_Word) -1)
613 size_t fnamelen = strlen (fname);
615 if (unlikely (diridx >= ndirlist))
617 __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
621 /* Yes, weird. Looks like an off-by-one in the spec. */
622 struct filelist *new_file = n == 0 ? &null_file : NEW_FILE ();
624 /* We follow the same rules as above for DWARF < 5, even
625 though the standard doesn't explicitly mention absolute
626 paths and ignoring the dir index. */
628 /* It's an absolute path. */
629 new_file->info.name = (char *) fname;
632 new_file->info.name = libdw_alloc (dbg, char, 1,
633 dirarray[diridx].len + 1
635 char *cp = new_file->info.name;
637 /* In the DWARF >= 5 case, dir can never be NULL. */
638 cp = stpcpy (cp, dirarray[diridx].dir);
641 assert (strlen (new_file->info.name)
642 < dirarray[diridx].len + 1 + fnamelen + 1);
645 /* For now we just ignore the modification time and file length. */
646 new_file->info.mtime = 0;
647 new_file->info.length = 0;
651 /* Consistency check. */
652 if (unlikely (linep != header_start + header_length))
654 __libdw_seterrno (DWARF_E_INVALID_DWARF);
658 /* We are about to process the statement program. Most state machine
659 registers have already been initialize above. Just add the is_stmt
660 default. See 6.2.2 in the v2.1 specification. */
661 state.is_stmt = default_is_stmt;
663 /* Apply the "operation advance" from a special opcode or
664 DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
665 #define advance_pc(op_advance) \
666 run_advance_pc (&state, op_advance, minimum_instr_len, max_ops_per_instr)
668 /* Process the instructions. */
670 /* Adds a new line to the matrix. For the first MAX_STACK_LINES
671 entries just return a slot in the preallocated stack array. */
672 struct linelist llstack[MAX_STACK_LINES];
673 #define NEW_LINE(end_seq) \
675 struct linelist *ll = (state.nlinelist < MAX_STACK_LINES \
676 ? &llstack[state.nlinelist] \
677 : malloc (sizeof (struct linelist))); \
678 if (unlikely (ll == NULL)) \
680 state.end_sequence = end_seq; \
681 if (unlikely (add_new_line (&state, ll))) \
685 while (linep < lineendp)
691 /* Read the opcode. */
694 /* Is this a special opcode? */
695 if (likely (opcode >= opcode_base))
697 if (unlikely (line_range == 0))
700 /* Yes. Handling this is quite easy since the opcode value
703 opcode = (desired line increment - line_base)
704 + (line_range * address advance) + opcode_base
706 int line_increment = (line_base
707 + (opcode - opcode_base) % line_range);
709 /* Perform the increments. */
710 state.line += line_increment;
711 advance_pc ((opcode - opcode_base) / line_range);
713 /* Add a new line with the current state machine values. */
716 /* Reset the flags. */
717 state.basic_block = false;
718 state.prologue_end = false;
719 state.epilogue_begin = false;
720 state.discriminator = 0;
722 else if (opcode == 0)
724 /* This an extended opcode. */
725 if (unlikely (lineendp - linep < 2))
729 uint_fast8_t len = *linep++;
731 if (unlikely ((size_t) (lineendp - linep) < len))
734 /* The sub-opcode. */
739 case DW_LNE_end_sequence:
740 /* Add a new line with the current state machine values.
741 The is the end of the sequence. */
744 /* Reset the registers. */
750 state.is_stmt = default_is_stmt;
751 state.basic_block = false;
752 state.prologue_end = false;
753 state.epilogue_begin = false;
755 state.discriminator = 0;
758 case DW_LNE_set_address:
759 /* The value is an address. The size is defined as
760 apporiate for the target machine. We use the
761 address size field from the CU header. */
763 if (unlikely (lineendp - linep < (uint8_t) address_size))
765 if (__libdw_read_address_inc (dbg, IDX_debug_line, &linep,
766 address_size, &state.addr))
770 case DW_LNE_define_file:
772 char *fname = (char *) linep;
773 uint8_t *endp = memchr (linep, '\0', lineendp - linep);
776 size_t fnamelen = endp - linep;
780 if (unlikely (linep >= lineendp))
782 get_uleb128 (diridx, linep, lineendp);
783 if (unlikely (diridx >= ndirlist))
785 __libdw_seterrno (DWARF_E_INVALID_DIR_IDX);
789 if (unlikely (linep >= lineendp))
791 get_uleb128 (mtime, linep, lineendp);
792 Dwarf_Word filelength;
793 if (unlikely (linep >= lineendp))
795 get_uleb128 (filelength, linep, lineendp);
797 struct filelist *new_file = NEW_FILE ();
799 new_file->info.name = fname;
802 new_file->info.name =
803 libdw_alloc (dbg, char, 1, (dirarray[diridx].len + 1
805 char *cp = new_file->info.name;
807 if (dirarray[diridx].dir != NULL)
808 /* This value could be NULL in case the
809 DW_AT_comp_dir was not present. We
810 cannot do much in this case. Just
811 keep the file relative. */
813 cp = stpcpy (cp, dirarray[diridx].dir);
819 new_file->info.mtime = mtime;
820 new_file->info.length = filelength;
824 case DW_LNE_set_discriminator:
825 /* Takes one ULEB128 parameter, the discriminator. */
826 if (unlikely (standard_opcode_lengths[opcode] != 1))
829 if (unlikely (linep >= lineendp))
831 get_uleb128 (state.discriminator, linep, lineendp);
835 /* Unknown, ignore it. */
836 if (unlikely ((size_t) (lineendp - (linep - 1)) < len))
842 else if (opcode <= DW_LNS_set_isa)
844 /* This is a known standard opcode. */
848 /* Takes no argument. */
849 if (unlikely (standard_opcode_lengths[opcode] != 0))
852 /* Add a new line with the current state machine values. */
855 /* Reset the flags. */
856 state.basic_block = false;
857 state.prologue_end = false;
858 state.epilogue_begin = false;
859 state.discriminator = 0;
862 case DW_LNS_advance_pc:
863 /* Takes one uleb128 parameter which is added to the
865 if (unlikely (standard_opcode_lengths[opcode] != 1))
868 if (unlikely (linep >= lineendp))
870 get_uleb128 (u128, linep, lineendp);
874 case DW_LNS_advance_line:
875 /* Takes one sleb128 parameter which is added to the
877 if (unlikely (standard_opcode_lengths[opcode] != 1))
880 if (unlikely (linep >= lineendp))
882 get_sleb128 (s128, linep, lineendp);
886 case DW_LNS_set_file:
887 /* Takes one uleb128 parameter which is stored in file. */
888 if (unlikely (standard_opcode_lengths[opcode] != 1))
891 if (unlikely (linep >= lineendp))
893 get_uleb128 (u128, linep, lineendp);
897 case DW_LNS_set_column:
898 /* Takes one uleb128 parameter which is stored in column. */
899 if (unlikely (standard_opcode_lengths[opcode] != 1))
902 if (unlikely (linep >= lineendp))
904 get_uleb128 (u128, linep, lineendp);
908 case DW_LNS_negate_stmt:
909 /* Takes no argument. */
910 if (unlikely (standard_opcode_lengths[opcode] != 0))
913 state.is_stmt = 1 - state.is_stmt;
916 case DW_LNS_set_basic_block:
917 /* Takes no argument. */
918 if (unlikely (standard_opcode_lengths[opcode] != 0))
921 state.basic_block = true;
924 case DW_LNS_const_add_pc:
925 /* Takes no argument. */
926 if (unlikely (standard_opcode_lengths[opcode] != 0))
929 if (unlikely (line_range == 0))
932 advance_pc ((255 - opcode_base) / line_range);
935 case DW_LNS_fixed_advance_pc:
936 /* Takes one 16 bit parameter which is added to the
938 if (unlikely (standard_opcode_lengths[opcode] != 1)
939 || unlikely (lineendp - linep < 2))
942 state.addr += read_2ubyte_unaligned_inc (dbg, linep);
946 case DW_LNS_set_prologue_end:
947 /* Takes no argument. */
948 if (unlikely (standard_opcode_lengths[opcode] != 0))
951 state.prologue_end = true;
954 case DW_LNS_set_epilogue_begin:
955 /* Takes no argument. */
956 if (unlikely (standard_opcode_lengths[opcode] != 0))
959 state.epilogue_begin = true;
963 /* Takes one uleb128 parameter which is stored in isa. */
964 if (unlikely (standard_opcode_lengths[opcode] != 1))
967 if (unlikely (linep >= lineendp))
969 get_uleb128 (state.isa, linep, lineendp);
975 /* This is a new opcode the generator but not we know about.
976 Read the parameters associated with it but then discard
977 everything. Read all the parameters for this opcode. */
978 for (int n = standard_opcode_lengths[opcode]; n > 0; --n)
980 if (unlikely (linep >= lineendp))
982 get_uleb128 (u128, linep, lineendp);
985 /* Next round, ignore this opcode. */
990 /* Put all the files in an array. */
991 Dwarf_Files *files = libdw_alloc (dbg, Dwarf_Files,
993 + nfilelist * sizeof (Dwarf_Fileinfo)
994 + (ndirlist + 1) * sizeof (char *),
996 const char **dirs = (void *) &files->info[nfilelist];
998 struct filelist *fileslist = filelist;
999 files->nfiles = nfilelist;
1000 for (size_t n = nfilelist; n > 0; n--)
1002 files->info[n - 1] = fileslist->info;
1003 fileslist = fileslist->next;
1005 assert (fileslist == NULL);
1007 /* Put all the directory strings in an array. */
1008 files->ndirs = ndirlist;
1009 for (unsigned int i = 0; i < ndirlist; ++i)
1010 dirs[i] = dirarray[i].dir;
1011 dirs[ndirlist] = NULL;
1013 /* Pass the file data structure to the caller. */
1017 size_t buf_size = (sizeof (Dwarf_Lines)
1018 + (sizeof (Dwarf_Line) * state.nlinelist));
1019 void *buf = libdw_alloc (dbg, Dwarf_Lines, buf_size, 1);
1021 /* First use the buffer for the pointers, and sort the entries.
1022 We'll write the pointers in the end of the buffer, and then
1023 copy into the buffer from the beginning so the overlap works. */
1024 assert (sizeof (Dwarf_Line) >= sizeof (struct linelist *));
1025 struct linelist **sortlines = (buf + buf_size
1026 - sizeof (struct linelist **) * state.nlinelist);
1028 /* The list is in LIFO order and usually they come in clumps with
1029 ascending addresses. So fill from the back to probably start with
1030 runs already in order before we sort. */
1031 struct linelist *lineslist = state.linelist;
1032 for (size_t i = state.nlinelist; i-- > 0; )
1034 sortlines[i] = lineslist;
1035 lineslist = lineslist->next;
1037 assert (lineslist == NULL);
1039 /* Sort by ascending address. */
1040 qsort (sortlines, state.nlinelist, sizeof sortlines[0], &compare_lines);
1042 /* Now that they are sorted, put them in the final array.
1043 The buffers overlap, so we've clobbered the early elements
1044 of SORTLINES by the time we're reading the later ones. */
1045 Dwarf_Lines *lines = buf;
1046 lines->nlines = state.nlinelist;
1047 for (size_t i = 0; i < state.nlinelist; ++i)
1049 lines->info[i] = sortlines[i]->line;
1050 lines->info[i].files = files;
1053 /* Make sure the highest address for the CU is marked as end_sequence.
1054 This is required by the DWARF spec, but some compilers forget and
1055 dwfl_module_getsrc depends on it. */
1056 if (state.nlinelist > 0)
1057 lines->info[state.nlinelist - 1].end_sequence = 1;
1059 /* Pass the line structure back to the caller. */
1067 /* Free malloced line records, if any. */
1068 for (size_t i = MAX_STACK_LINES; i < state.nlinelist; i++)
1070 struct linelist *ll = state.linelist->next;
1071 free (state.linelist);
1072 state.linelist = ll;
1074 if (dirarray != dirstack)
1076 for (size_t i = MAX_STACK_FILES; i < nfilelist; i++)
1078 struct filelist *fl = filelist->next;
1087 files_lines_compare (const void *p1, const void *p2)
1089 const struct files_lines_s *t1 = p1;
1090 const struct files_lines_s *t2 = p2;
1092 if (t1->debug_line_offset < t2->debug_line_offset)
1094 if (t1->debug_line_offset > t2->debug_line_offset)
1102 __libdw_getsrclines (Dwarf *dbg, Dwarf_Off debug_line_offset,
1103 const char *comp_dir, unsigned address_size,
1104 Dwarf_Lines **linesp, Dwarf_Files **filesp)
1106 struct files_lines_s fake = { .debug_line_offset = debug_line_offset };
1107 struct files_lines_s **found = tfind (&fake, &dbg->files_lines,
1108 files_lines_compare);
1111 Elf_Data *data = __libdw_checked_get_data (dbg, IDX_debug_line);
1113 || __libdw_offset_in_section (dbg, IDX_debug_line,
1114 debug_line_offset, 1) != 0)
1117 const unsigned char *linep = data->d_buf + debug_line_offset;
1118 const unsigned char *lineendp = data->d_buf + data->d_size;
1120 struct files_lines_s *node = libdw_alloc (dbg, struct files_lines_s,
1123 if (read_srclines (dbg, linep, lineendp, comp_dir, address_size,
1124 &node->lines, &node->files) != 0)
1127 node->debug_line_offset = debug_line_offset;
1129 found = tsearch (node, &dbg->files_lines, files_lines_compare);
1132 __libdw_seterrno (DWARF_E_NOMEM);
1138 *linesp = (*found)->lines;
1141 *filesp = (*found)->files;
1146 /* Get the compilation directory, if any is set. */
1148 __libdw_getcompdir (Dwarf_Die *cudie)
1150 Dwarf_Attribute compdir_attr_mem;
1151 Dwarf_Attribute *compdir_attr = INTUSE(dwarf_attr) (cudie,
1154 return INTUSE(dwarf_formstring) (compdir_attr);
1158 dwarf_getsrclines (Dwarf_Die *cudie, Dwarf_Lines **lines, size_t *nlines)
1162 if (! is_cudie (cudie))
1164 __libdw_seterrno (DWARF_E_NOT_CUDIE);
1168 /* Get the information if it is not already known. */
1169 struct Dwarf_CU *const cu = cudie->cu;
1170 if (cu->lines == NULL)
1172 /* For split units always pick the lines from the skeleton. */
1173 if (cu->unit_type == DW_UT_split_compile
1174 || cu->unit_type == DW_UT_split_type)
1176 /* We tries, assume we fail... */
1177 cu->lines = (void *) -1l;
1179 Dwarf_CU *skel = __libdw_find_split_unit (cu);
1182 Dwarf_Die skeldie = CUDIE (skel);
1183 int res = INTUSE(dwarf_getsrclines) (&skeldie, lines, nlines);
1186 cu->lines = skel->lines;
1188 *nlines = cu->lines->nlines;
1193 __libdw_seterrno (DWARF_E_NO_DEBUG_LINE);
1197 /* Failsafe mode: no data found. */
1198 cu->lines = (void *) -1l;
1199 cu->files = (void *) -1l;
1201 /* The die must have a statement list associated. */
1202 Dwarf_Attribute stmt_list_mem;
1203 Dwarf_Attribute *stmt_list = INTUSE(dwarf_attr) (cudie, DW_AT_stmt_list,
1206 /* Get the offset into the .debug_line section. NB: this call
1207 also checks whether the previous dwarf_attr call failed. */
1208 Dwarf_Off debug_line_offset;
1209 if (__libdw_formptr (stmt_list, IDX_debug_line, DWARF_E_NO_DEBUG_LINE,
1210 NULL, &debug_line_offset) == NULL)
1213 if (__libdw_getsrclines (cu->dbg, debug_line_offset,
1214 __libdw_getcompdir (cudie),
1215 cu->address_size, &cu->lines, &cu->files) < 0)
1218 else if (cu->lines == (void *) -1l)
1222 *nlines = cu->lines->nlines;
1224 // XXX Eventually: unlocking here.
1228 INTDEF(dwarf_getsrclines)