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

Generated by: LCOV version 1.14