LCOV - code coverage report
Current view: top level - libdw - dwarf_getsrclines.c (source / functions) Hit Total Coverage
Test: elfutils-0.187 Lines: 429 600 71.5 %
Date: 2022-04-26 02:07:47 Functions: 7 7 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 241 425 56.7 %

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

Generated by: LCOV version 1.14