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

Generated by: LCOV version 1.14