LCOV - code coverage report
Current view: top level - libdw - dwarf_begin_elf.c (source / functions) Hit Total Coverage
Test: elfutils-0.187 Lines: 161 247 65.2 %
Date: 2022-04-26 02:07:47 Functions: 6 7 85.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 117 178 65.7 %

           Branch data     Line data    Source code
       1                 :            : /* Create descriptor from ELF descriptor for processing file.
       2                 :            :    Copyright (C) 2002-2011, 2014, 2015, 2017, 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 <system.h>
      34                 :            : 
      35                 :            : #include <assert.h>
      36                 :            : #include <stdbool.h>
      37                 :            : #include <stddef.h>
      38                 :            : #include <stdlib.h>
      39                 :            : #include <stdio.h>
      40                 :            : #include <string.h>
      41                 :            : #include <unistd.h>
      42                 :            : #include <sys/types.h>
      43                 :            : #include <sys/stat.h>
      44                 :            : #include <fcntl.h>
      45                 :            : #include <endian.h>
      46                 :            : 
      47                 :            : #include "libelfP.h"
      48                 :            : #include "libdwP.h"
      49                 :            : 
      50                 :            : 
      51                 :            : /* Section names.  (Note .debug_str_offsets is the largest 19 chars.)  */
      52                 :            : static const char dwarf_scnnames[IDX_last][19] =
      53                 :            : {
      54                 :            :   [IDX_debug_info] = ".debug_info",
      55                 :            :   [IDX_debug_types] = ".debug_types",
      56                 :            :   [IDX_debug_abbrev] = ".debug_abbrev",
      57                 :            :   [IDX_debug_addr] = ".debug_addr",
      58                 :            :   [IDX_debug_aranges] = ".debug_aranges",
      59                 :            :   [IDX_debug_line] = ".debug_line",
      60                 :            :   [IDX_debug_line_str] = ".debug_line_str",
      61                 :            :   [IDX_debug_frame] = ".debug_frame",
      62                 :            :   [IDX_debug_loc] = ".debug_loc",
      63                 :            :   [IDX_debug_loclists] = ".debug_loclists",
      64                 :            :   [IDX_debug_pubnames] = ".debug_pubnames",
      65                 :            :   [IDX_debug_str] = ".debug_str",
      66                 :            :   [IDX_debug_str_offsets] = ".debug_str_offsets",
      67                 :            :   [IDX_debug_macinfo] = ".debug_macinfo",
      68                 :            :   [IDX_debug_macro] = ".debug_macro",
      69                 :            :   [IDX_debug_ranges] = ".debug_ranges",
      70                 :            :   [IDX_debug_rnglists] = ".debug_rnglists",
      71                 :            :   [IDX_gnu_debugaltlink] = ".gnu_debugaltlink"
      72                 :            : };
      73                 :            : #define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0]))
      74                 :            : 
      75                 :            : static enum dwarf_type
      76                 :     164859 : scn_dwarf_type (Dwarf *result, size_t shstrndx, Elf_Scn *scn)
      77                 :            : {
      78                 :     164859 :   GElf_Shdr shdr_mem;
      79                 :     164859 :   GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
      80         [ +  - ]:     164859 :   if (shdr == NULL)
      81                 :            :     return TYPE_UNKNOWN;
      82                 :            : 
      83                 :     329718 :   const char *scnname = elf_strptr (result->elf, shstrndx,
      84                 :     164859 :                                     shdr->sh_name);
      85         [ +  + ]:     164859 :   if (scnname != NULL)
      86                 :            :     {
      87         [ +  + ]:     164818 :       if (startswith (scnname, ".gnu.debuglto_.debug"))
      88                 :            :         return TYPE_GNU_LTO;
      89   [ +  +  +  + ]:     164813 :       else if (startswith (scnname, ".debug_") || startswith (scnname, ".zdebug_"))
      90                 :            :         {
      91                 :       6087 :           size_t len = strlen (scnname);
      92         [ +  + ]:       6087 :           if (strcmp (scnname + len - 4, ".dwo") == 0)
      93                 :            :             return TYPE_DWO;
      94                 :            :           else
      95                 :       5669 :             return TYPE_PLAIN;
      96                 :            :         }
      97                 :            :     }
      98                 :            :   return TYPE_UNKNOWN;
      99                 :            : }
     100                 :            : static Dwarf *
     101                 :     281663 : check_section (Dwarf *result, size_t shstrndx, Elf_Scn *scn, bool inscngrp)
     102                 :            : {
     103                 :     281663 :   GElf_Shdr shdr_mem;
     104                 :     281663 :   GElf_Shdr *shdr;
     105                 :            : 
     106                 :            :   /* Get the section header data.  */
     107                 :     281663 :   shdr = gelf_getshdr (scn, &shdr_mem);
     108         [ -  + ]:     281663 :   if (shdr == NULL)
     109                 :            :     /* We may read /proc/PID/mem with only program headers mapped and section
     110                 :            :        headers out of the mapped pages.  */
     111                 :          0 :     goto err;
     112                 :            : 
     113                 :            :   /* Ignore any SHT_NOBITS sections.  Debugging sections should not
     114                 :            :      have been stripped, but in case of a corrupt file we won't try
     115                 :            :      to look at the missing data.  */
     116         [ +  + ]:     281663 :   if (unlikely (shdr->sh_type == SHT_NOBITS))
     117                 :            :     return result;
     118                 :            : 
     119                 :            :   /* Make sure the section is part of a section group only iff we
     120                 :            :      really need it.  If we are looking for the global (= non-section
     121                 :            :      group debug info) we have to ignore all the info in section
     122                 :            :      groups.  If we are looking into a section group we cannot look at
     123                 :            :      a section which isn't part of the section group.  */
     124   [ +  -  +  + ]:     266297 :   if (! inscngrp && (shdr->sh_flags & SHF_GROUP) != 0)
     125                 :            :     /* Ignore the section.  */
     126                 :            :     return result;
     127                 :            : 
     128                 :            : 
     129                 :            :   /* We recognize the DWARF section by their names.  This is not very
     130                 :            :      safe and stable but the best we can do.  */
     131                 :     532592 :   const char *scnname = elf_strptr (result->elf, shstrndx,
     132                 :     266296 :                                     shdr->sh_name);
     133         [ +  + ]:     266296 :   if (scnname == NULL)
     134                 :            :     {
     135                 :            :       /* The section name must be valid.  Otherwise is the ELF file
     136                 :            :          invalid.  */
     137                 :          1 :     err:
     138                 :          1 :       Dwarf_Sig8_Hash_free (&result->sig8_hash);
     139                 :          1 :       __libdw_seterrno (DWARF_E_INVALID_ELF);
     140                 :          1 :       free (result);
     141                 :          1 :       return NULL;
     142                 :            :     }
     143                 :            : 
     144                 :            :   /* Recognize the various sections.  Most names start with .debug_.
     145                 :            :      They might be compressed (and start with .z).  Or end with .dwo
     146                 :            :      for split dwarf sections.  Or start with .gnu.debuglto_ for
     147                 :            :      LTO debug sections.  We should only use one consistent set at
     148                 :            :      a time.  We prefer PLAIN over DWO over LTO.  */
     149                 :            :   size_t cnt;
     150                 :    4593440 :   bool gnu_compressed = false;
     151         [ +  + ]:    4593440 :   for (cnt = 0; cnt < ndwarf_scnnames; ++cnt)
     152                 :            :     {
     153                 :    4366731 :       size_t dbglen = strlen (dwarf_scnnames[cnt]);
     154                 :    4366731 :       size_t scnlen = strlen (scnname);
     155         [ +  + ]:    4366731 :       if (strncmp (scnname, dwarf_scnnames[cnt], dbglen) == 0
     156         [ +  + ]:      39635 :           && (dbglen == scnlen
     157         [ +  + ]:        549 :               || (scnlen == dbglen + 4
     158         [ +  + ]:        431 :                   && strstr (scnname, ".dwo") == scnname + dbglen)))
     159                 :            :         {
     160         [ +  + ]:      39504 :           if (dbglen == scnlen)
     161                 :            :             {
     162         [ -  + ]:      39086 :               if (result->type == TYPE_PLAIN)
     163                 :            :                 break;
     164                 :            :             }
     165         [ -  + ]:        418 :           else if (result->type == TYPE_DWO)
     166                 :            :             break;
     167                 :            :         }
     168   [ +  +  +  + ]:    4327227 :       else if (scnname[0] == '.' && scnname[1] == 'z'
     169         [ +  + ]:        594 :                && (strncmp (&scnname[2], &dwarf_scnnames[cnt][1],
     170                 :            :                             dbglen - 1) == 0
     171         [ -  + ]:         82 :                    && (scnlen == dbglen + 1
     172         [ #  # ]:          0 :                        || (scnlen == dbglen + 5
     173                 :          0 :                            && strstr (scnname,
     174         [ #  # ]:          0 :                                       ".dwo") == scnname + dbglen + 1))))
     175                 :            :         {
     176         [ +  - ]:         82 :           if (scnlen == dbglen + 1)
     177                 :            :             {
     178         [ -  + ]:         82 :               if (result->type == TYPE_PLAIN)
     179                 :            :                 {
     180                 :            :                   gnu_compressed = true;
     181                 :            :                   break;
     182                 :            :                 }
     183                 :            :             }
     184         [ #  # ]:          0 :           else if (result->type <= TYPE_DWO)
     185                 :            :             {
     186                 :            :               gnu_compressed = true;
     187                 :            :               break;
     188                 :            :             }
     189                 :            :         }
     190         [ +  + ]:    4327145 :       else if (scnlen > 14 /* .gnu.debuglto_ prefix. */
     191         [ +  + ]:     128723 :                && startswith (scnname, ".gnu.debuglto_")
     192         [ +  + ]:         90 :                && strcmp (&scnname[14], dwarf_scnnames[cnt]) == 0)
     193                 :            :         {
     194         [ +  - ]:          5 :           if (result->type == TYPE_GNU_LTO)
     195                 :            :             break;
     196                 :            :         }
     197                 :            :     }
     198                 :            : 
     199         [ +  + ]:     266295 :   if (cnt >= ndwarf_scnnames)
     200                 :            :     /* Not a debug section; ignore it. */
     201                 :            :     return result;
     202                 :            : 
     203         [ +  - ]:      39586 :   if (unlikely (result->sectiondata[cnt] != NULL))
     204                 :            :     /* A section appears twice.  That's bad.  We ignore the section.  */
     205                 :            :     return result;
     206                 :            : 
     207                 :            :   /* We cannot know whether or not a GNU compressed section has already
     208                 :            :      been uncompressed or not, so ignore any errors.  */
     209         [ +  + ]:      39586 :   if (gnu_compressed)
     210                 :         82 :     elf_compress_gnu (scn, 0, 0);
     211                 :            : 
     212         [ +  + ]:      39586 :   if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
     213                 :            :     {
     214         [ +  - ]:        347 :       if (elf_compress (scn, 0, 0) < 0)
     215                 :            :         {
     216                 :            :           /* It would be nice if we could fail with a specific error.
     217                 :            :              But we don't know if this was an essential section or not.
     218                 :            :              So just continue for now. See also valid_p().  */
     219                 :            :           return result;
     220                 :            :         }
     221                 :            :     }
     222                 :            : 
     223                 :            :   /* Get the section data.  */
     224                 :      39586 :   Elf_Data *data = elf_getdata (scn, NULL);
     225         [ -  + ]:      39586 :   if (data == NULL)
     226                 :          0 :     goto err;
     227                 :            : 
     228   [ +  +  +  - ]:      39586 :   if (data->d_buf == NULL || data->d_size == 0)
     229                 :            :     /* No data actually available, ignore it. */
     230                 :            :     return result;
     231                 :            : 
     232                 :            :   /* We can now read the section data into results. */
     233                 :      39580 :   result->sectiondata[cnt] = data;
     234                 :            : 
     235                 :      39580 :   return result;
     236                 :            : }
     237                 :            : 
     238                 :            : 
     239                 :            : /* Helper function to set debugdir field.  We want to cache the dir
     240                 :            :    where we found this Dwarf ELF file to locate alt and dwo files.  */
     241                 :            : char *
     242                 :       6357 : __libdw_debugdir (int fd)
     243                 :            : {
     244                 :            :   /* strlen ("/proc/self/fd/") = 14 + strlen (<MAXINT>) = 10 + 1 = 25.  */
     245                 :       6357 :   char devfdpath[25];
     246         [ -  + ]:       6357 :   sprintf (devfdpath, "/proc/self/fd/%u", fd);
     247         [ -  + ]:       6357 :   char *fdpath = realpath (devfdpath, NULL);
     248                 :       6357 :   char *fddir;
     249   [ +  +  +  - ]:       6357 :   if (fdpath != NULL && fdpath[0] == '/'
     250         [ +  - ]:       5963 :       && (fddir = strrchr (fdpath, '/')) != NULL)
     251                 :            :     {
     252                 :       5963 :       *++fddir = '\0';
     253                 :       5963 :       return fdpath;
     254                 :            :     }
     255                 :            :   return NULL;
     256                 :            : }
     257                 :            : 
     258                 :            : 
     259                 :            : /* Check whether all the necessary DWARF information is available.  */
     260                 :            : static Dwarf *
     261                 :       5903 : valid_p (Dwarf *result)
     262                 :            : {
     263                 :            :   /* We looked at all the sections.  Now determine whether all the
     264                 :            :      sections with debugging information we need are there.
     265                 :            : 
     266                 :            :      Require at least one section that can be read "standalone".  */
     267         [ +  + ]:       5903 :   if (likely (result != NULL)
     268   [ +  +  +  +  :       5902 :       && unlikely (result->sectiondata[IDX_debug_info] == NULL
                   +  + ]
     269                 :            :                    && result->sectiondata[IDX_debug_line] == NULL
     270                 :            :                    && result->sectiondata[IDX_debug_frame] == NULL))
     271                 :            :     {
     272                 :        163 :       Dwarf_Sig8_Hash_free (&result->sig8_hash);
     273                 :        163 :       __libdw_seterrno (DWARF_E_NO_DWARF);
     274                 :        163 :       free (result);
     275                 :        163 :       result = NULL;
     276                 :            :     }
     277                 :            : 
     278                 :            :   /* We are setting up some "fake" CUs, which need an address size.
     279                 :            :      Check the ELF class to come up with something reasonable.  */
     280                 :       5903 :   int elf_addr_size = 8;
     281         [ +  + ]:       5903 :   if (result != NULL)
     282                 :            :     {
     283                 :       5739 :       GElf_Ehdr ehdr;
     284         [ -  + ]:       5739 :       if (gelf_getehdr (result->elf, &ehdr) == NULL)
     285                 :            :         {
     286                 :          0 :           Dwarf_Sig8_Hash_free (&result->sig8_hash);
     287                 :          0 :           __libdw_seterrno (DWARF_E_INVALID_ELF);
     288                 :          0 :           free (result);
     289                 :          0 :           result = NULL;
     290                 :            :         }
     291         [ +  + ]:       5739 :       else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
     292                 :        111 :         elf_addr_size = 4;
     293                 :            :     }
     294                 :            : 
     295                 :            :   /* For dwarf_location_attr () we need a "fake" CU to indicate
     296                 :            :      where the "fake" attribute data comes from.  This is a block
     297                 :            :      inside the .debug_loc or .debug_loclists section.  */
     298   [ +  +  +  + ]:       5903 :   if (result != NULL && result->sectiondata[IDX_debug_loc] != NULL)
     299                 :            :     {
     300                 :       5278 :       result->fake_loc_cu = malloc (sizeof (Dwarf_CU));
     301         [ -  + ]:       5278 :       if (unlikely (result->fake_loc_cu == NULL))
     302                 :            :         {
     303                 :          0 :           Dwarf_Sig8_Hash_free (&result->sig8_hash);
     304                 :          0 :           __libdw_seterrno (DWARF_E_NOMEM);
     305                 :          0 :           free (result);
     306                 :          0 :           result = NULL;
     307                 :            :         }
     308                 :            :       else
     309                 :            :         {
     310                 :       5278 :           result->fake_loc_cu->sec_idx = IDX_debug_loc;
     311                 :       5278 :           result->fake_loc_cu->dbg = result;
     312                 :       5278 :           result->fake_loc_cu->startp
     313                 :       5278 :             = result->sectiondata[IDX_debug_loc]->d_buf;
     314                 :       5278 :           result->fake_loc_cu->endp
     315                 :       5278 :             = (result->sectiondata[IDX_debug_loc]->d_buf
     316                 :       5278 :                + result->sectiondata[IDX_debug_loc]->d_size);
     317                 :       5278 :           result->fake_loc_cu->locs = NULL;
     318                 :       5278 :           result->fake_loc_cu->address_size = elf_addr_size;
     319                 :       5278 :           result->fake_loc_cu->offset_size = 4;
     320                 :       5278 :           result->fake_loc_cu->version = 4;
     321                 :       5278 :           result->fake_loc_cu->split = NULL;
     322                 :            :         }
     323                 :            :     }
     324                 :            : 
     325   [ +  +  +  + ]:       5903 :   if (result != NULL && result->sectiondata[IDX_debug_loclists] != NULL)
     326                 :            :     {
     327                 :         46 :       result->fake_loclists_cu = malloc (sizeof (Dwarf_CU));
     328         [ -  + ]:         46 :       if (unlikely (result->fake_loclists_cu == NULL))
     329                 :            :         {
     330                 :          0 :           Dwarf_Sig8_Hash_free (&result->sig8_hash);
     331                 :          0 :           __libdw_seterrno (DWARF_E_NOMEM);
     332                 :          0 :           free (result->fake_loc_cu);
     333                 :          0 :           free (result);
     334                 :          0 :           result = NULL;
     335                 :            :         }
     336                 :            :       else
     337                 :            :         {
     338                 :         46 :           result->fake_loclists_cu->sec_idx = IDX_debug_loclists;
     339                 :         46 :           result->fake_loclists_cu->dbg = result;
     340                 :         46 :           result->fake_loclists_cu->startp
     341                 :         46 :             = result->sectiondata[IDX_debug_loclists]->d_buf;
     342                 :         46 :           result->fake_loclists_cu->endp
     343                 :         46 :             = (result->sectiondata[IDX_debug_loclists]->d_buf
     344                 :         46 :                + result->sectiondata[IDX_debug_loclists]->d_size);
     345                 :         46 :           result->fake_loclists_cu->locs = NULL;
     346                 :         46 :           result->fake_loclists_cu->address_size = elf_addr_size;
     347                 :         46 :           result->fake_loclists_cu->offset_size = 4;
     348                 :         46 :           result->fake_loclists_cu->version = 5;
     349                 :         46 :           result->fake_loclists_cu->split = NULL;
     350                 :            :         }
     351                 :            :     }
     352                 :            : 
     353                 :            :   /* For DW_OP_constx/GNU_const_index and DW_OP_addrx/GNU_addr_index
     354                 :            :      the dwarf_location_attr () will need a "fake" address CU to
     355                 :            :      indicate where the attribute data comes from.  This is a just
     356                 :            :      inside the .debug_addr section, if it exists.  */
     357   [ +  +  +  + ]:       5903 :   if (result != NULL && result->sectiondata[IDX_debug_addr] != NULL)
     358                 :            :     {
     359                 :         41 :       result->fake_addr_cu = malloc (sizeof (Dwarf_CU));
     360         [ -  + ]:         41 :       if (unlikely (result->fake_addr_cu == NULL))
     361                 :            :         {
     362                 :          0 :           Dwarf_Sig8_Hash_free (&result->sig8_hash);
     363                 :          0 :           __libdw_seterrno (DWARF_E_NOMEM);
     364                 :          0 :           free (result->fake_loc_cu);
     365                 :          0 :           free (result->fake_loclists_cu);
     366                 :          0 :           free (result);
     367                 :          0 :           result = NULL;
     368                 :            :         }
     369                 :            :       else
     370                 :            :         {
     371                 :         41 :           result->fake_addr_cu->sec_idx = IDX_debug_addr;
     372                 :         41 :           result->fake_addr_cu->dbg = result;
     373                 :         41 :           result->fake_addr_cu->startp
     374                 :         41 :             = result->sectiondata[IDX_debug_addr]->d_buf;
     375                 :         41 :           result->fake_addr_cu->endp
     376                 :         41 :             = (result->sectiondata[IDX_debug_addr]->d_buf
     377                 :         41 :                + result->sectiondata[IDX_debug_addr]->d_size);
     378                 :         41 :           result->fake_addr_cu->locs = NULL;
     379                 :         41 :           result->fake_addr_cu->address_size = elf_addr_size;
     380                 :         41 :           result->fake_addr_cu->offset_size = 4;
     381                 :         41 :           result->fake_addr_cu->version = 5;
     382                 :         41 :           result->fake_addr_cu->split = NULL;
     383                 :            :         }
     384                 :            :     }
     385                 :            : 
     386         [ +  + ]:       5903 :   if (result != NULL)
     387                 :       5739 :     result->debugdir = __libdw_debugdir (result->elf->fildes);
     388                 :            : 
     389                 :       5903 :   return result;
     390                 :            : }
     391                 :            : 
     392                 :            : 
     393                 :            : static Dwarf *
     394                 :       5903 : global_read (Dwarf *result, Elf *elf, size_t shstrndx)
     395                 :            : {
     396                 :       5903 :   Elf_Scn *scn = NULL;
     397                 :            : 
     398                 :            :   /* First check the type (PLAIN, DWO, LTO) we are looking for.  We
     399                 :            :      prefer PLAIN if available over DWO, over LTO.  */
     400   [ +  +  +  + ]:     170762 :   while ((scn = elf_nextscn (elf, scn)) != NULL && result->type != TYPE_PLAIN)
     401                 :            :     {
     402                 :     164859 :       enum dwarf_type type = scn_dwarf_type (result, shstrndx, scn);
     403         [ +  + ]:     164859 :       if (type > result->type)
     404                 :       5740 :         result->type = type;
     405                 :            :     }
     406                 :            : 
     407                 :            :   scn = NULL;
     408   [ +  +  +  + ]:     287566 :   while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL)
     409                 :     281663 :     result = check_section (result, shstrndx, scn, false);
     410                 :            : 
     411                 :       5903 :   return valid_p (result);
     412                 :            : }
     413                 :            : 
     414                 :            : 
     415                 :            : static Dwarf *
     416                 :          0 : scngrp_read (Dwarf *result, Elf *elf, size_t shstrndx, Elf_Scn *scngrp)
     417                 :            : {
     418                 :          0 :   GElf_Shdr shdr_mem;
     419                 :          0 :   GElf_Shdr *shdr = gelf_getshdr (scngrp, &shdr_mem);
     420         [ #  # ]:          0 :   if (shdr == NULL)
     421                 :            :     {
     422                 :          0 :       Dwarf_Sig8_Hash_free (&result->sig8_hash);
     423                 :          0 :       __libdw_seterrno (DWARF_E_INVALID_ELF);
     424                 :          0 :       free (result);
     425                 :          0 :       return NULL;
     426                 :            :     }
     427                 :            : 
     428         [ #  # ]:          0 :   if ((shdr->sh_flags & SHF_COMPRESSED) != 0
     429         [ #  # ]:          0 :       && elf_compress (scngrp, 0, 0) < 0)
     430                 :            :     {
     431                 :          0 :       Dwarf_Sig8_Hash_free (&result->sig8_hash);
     432                 :          0 :       __libdw_seterrno (DWARF_E_COMPRESSED_ERROR);
     433                 :          0 :       free (result);
     434                 :          0 :       return NULL;
     435                 :            :     }
     436                 :            : 
     437                 :            :   /* SCNGRP is the section descriptor for a section group which might
     438                 :            :      contain debug sections.  */
     439                 :          0 :   Elf_Data *data = elf_getdata (scngrp, NULL);
     440         [ #  # ]:          0 :   if (data == NULL)
     441                 :            :     {
     442                 :            :       /* We cannot read the section content.  Fail!  */
     443                 :          0 :       Dwarf_Sig8_Hash_free (&result->sig8_hash);
     444                 :          0 :       free (result);
     445                 :          0 :       return NULL;
     446                 :            :     }
     447                 :            : 
     448                 :            :   /* The content of the section is a number of 32-bit words which
     449                 :            :      represent section indices.  The first word is a flag word.  */
     450                 :          0 :   Elf32_Word *scnidx = (Elf32_Word *) data->d_buf;
     451                 :          0 :   size_t cnt;
     452                 :            : 
     453                 :            :   /* First check the type (PLAIN, DWO, LTO) we are looking for.  We
     454                 :            :      prefer PLAIN if available over DWO, over LTO.  */
     455         [ #  # ]:          0 :   for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt)
     456                 :            :     {
     457                 :          0 :       Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
     458         [ #  # ]:          0 :       if (scn == NULL)
     459                 :            :         {
     460                 :            :           /* A section group refers to a non-existing section.  Should
     461                 :            :              never happen.  */
     462                 :          0 :           Dwarf_Sig8_Hash_free (&result->sig8_hash);
     463                 :          0 :           __libdw_seterrno (DWARF_E_INVALID_ELF);
     464                 :          0 :           free (result);
     465                 :          0 :           return NULL;
     466                 :            :         }
     467                 :            : 
     468                 :          0 :       enum dwarf_type type = scn_dwarf_type (result, shstrndx, scn);
     469         [ #  # ]:          0 :       if (type > result->type)
     470                 :          0 :         result->type = type;
     471                 :            :     }
     472                 :            : 
     473   [ #  #  #  # ]:          0 :   for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size && result != NULL; ++cnt)
     474                 :            :     {
     475                 :          0 :       Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
     476         [ #  # ]:          0 :       assert (scn != NULL); // checked above
     477                 :          0 :       result = check_section (result, shstrndx, scn, true);
     478         [ #  # ]:          0 :       if (result == NULL)
     479                 :            :         break;
     480                 :            :     }
     481                 :            : 
     482                 :          0 :   return valid_p (result);
     483                 :            : }
     484                 :            : 
     485                 :            : 
     486                 :            : Dwarf *
     487                 :       5903 : dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp)
     488                 :            : {
     489                 :       5903 :   GElf_Ehdr *ehdr;
     490                 :       5903 :   GElf_Ehdr ehdr_mem;
     491                 :            : 
     492                 :            :   /* Get the ELF header of the file.  We need various pieces of
     493                 :            :      information from it.  */
     494                 :       5903 :   ehdr = gelf_getehdr (elf, &ehdr_mem);
     495         [ -  + ]:       5903 :   if (ehdr == NULL)
     496                 :            :     {
     497         [ #  # ]:          0 :       if (elf_kind (elf) != ELF_K_ELF)
     498                 :          0 :         __libdw_seterrno (DWARF_E_NOELF);
     499                 :            :       else
     500                 :          0 :         __libdw_seterrno (DWARF_E_GETEHDR_ERROR);
     501                 :            : 
     502                 :          0 :       return NULL;
     503                 :            :     }
     504                 :            : 
     505                 :            : 
     506                 :            :   /* Default memory allocation size.  */
     507                 :       5903 :   size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *);
     508         [ -  + ]:       5903 :   assert (sizeof (struct Dwarf) < mem_default_size);
     509                 :            : 
     510                 :            :   /* Allocate the data structure.  */
     511                 :       5903 :   Dwarf *result = calloc (1, sizeof (Dwarf));
     512         [ +  - ]:       5903 :   if (unlikely (result == NULL)
     513         [ -  + ]:       5903 :       || unlikely (Dwarf_Sig8_Hash_init (&result->sig8_hash, 11) < 0))
     514                 :            :     {
     515                 :          0 :       free (result);
     516                 :          0 :       __libdw_seterrno (DWARF_E_NOMEM);
     517                 :          0 :       return NULL;
     518                 :            :     }
     519                 :            : 
     520                 :            :   /* Fill in some values.  */
     521         [ +  + ]:       5903 :   if ((BYTE_ORDER == LITTLE_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
     522                 :            :       || (BYTE_ORDER == BIG_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2LSB))
     523                 :         78 :     result->other_byte_order = true;
     524                 :            : 
     525                 :       5903 :   result->elf = elf;
     526                 :       5903 :   result->alt_fd = -1;
     527                 :            : 
     528                 :            :   /* Initialize the memory handling.  Initial blocks are allocated on first
     529                 :            :      actual allocation.  */
     530                 :       5903 :   result->mem_default_size = mem_default_size;
     531                 :       5903 :   result->oom_handler = __libdw_oom;
     532         [ -  + ]:       5903 :   if (pthread_rwlock_init(&result->mem_rwl, NULL) != 0)
     533                 :            :     {
     534                 :          0 :       free (result);
     535                 :          0 :       __libdw_seterrno (DWARF_E_NOMEM); /* no memory.  */
     536                 :          0 :       return NULL;
     537                 :            :     }
     538                 :       5903 :   result->mem_stacks = 0;
     539                 :       5903 :   result->mem_tails = NULL;
     540                 :            : 
     541         [ +  - ]:       5903 :   if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR)
     542                 :            :     {
     543                 :            :       /* All sections are recognized by name, so pass the section header
     544                 :            :          string index along to easily get the section names.  */
     545                 :       5903 :       size_t shstrndx;
     546         [ -  + ]:       5903 :       if (elf_getshdrstrndx (elf, &shstrndx) != 0)
     547                 :            :         {
     548                 :          0 :           Dwarf_Sig8_Hash_free (&result->sig8_hash);
     549                 :          0 :           __libdw_seterrno (DWARF_E_INVALID_ELF);
     550                 :          0 :           free (result);
     551                 :          0 :           return NULL;
     552                 :            :         }
     553                 :            : 
     554                 :            :       /* If the caller provides a section group we get the DWARF
     555                 :            :          sections only from this section group.  Otherwise we search
     556                 :            :          for the first section with the required name.  Further
     557                 :            :          sections with the name are ignored.  The DWARF specification
     558                 :            :          does not really say this is allowed.  */
     559         [ +  - ]:       5903 :       if (scngrp == NULL)
     560                 :       5903 :         return global_read (result, elf, shstrndx);
     561                 :            :       else
     562                 :          0 :         return scngrp_read (result, elf, shstrndx, scngrp);
     563                 :            :     }
     564         [ #  # ]:          0 :   else if (cmd == DWARF_C_WRITE)
     565                 :            :     {
     566                 :          0 :       Dwarf_Sig8_Hash_free (&result->sig8_hash);
     567                 :          0 :       __libdw_seterrno (DWARF_E_UNIMPL);
     568                 :          0 :       free (result);
     569                 :          0 :       return NULL;
     570                 :            :     }
     571                 :            : 
     572                 :          0 :   Dwarf_Sig8_Hash_free (&result->sig8_hash);
     573                 :          0 :   __libdw_seterrno (DWARF_E_INVALID_CMD);
     574                 :          0 :   free (result);
     575                 :          0 :   return NULL;
     576                 :            : }
     577                 :            : INTDEF(dwarf_begin_elf)

Generated by: LCOV version 1.14