LCOV - code coverage report
Current view: top level - libdw - dwarf_getsrclines.c (source / functions) Hit Total Coverage
Test: elfutils-0.172 Lines: 380 476 79.8 %
Date: 2018-06-11 22:52:14 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13