LCOV - code coverage report
Current view: top level - libelf - elf_end.c (source / functions) Hit Total Coverage
Test: elfutils-0.189 Lines: 76 76 100.0 %
Date: 2023-06-27 14:24:55 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 64 69 92.8 %

           Branch data     Line data    Source code
       1                 :            : /* Free resources associated with Elf descriptor.
       2                 :            :    Copyright (C) 1998,1999,2000,2001,2002,2004,2005,2007,2015,2016 Red Hat, Inc.
       3                 :            :    This file is part of elfutils.
       4                 :            :    Written by Ulrich Drepper <drepper@redhat.com>, 1998.
       5                 :            : 
       6                 :            :    This file is free software; you can redistribute it and/or modify
       7                 :            :    it under the terms of either
       8                 :            : 
       9                 :            :      * the GNU Lesser General Public License as published by the Free
      10                 :            :        Software Foundation; either version 3 of the License, or (at
      11                 :            :        your option) any later version
      12                 :            : 
      13                 :            :    or
      14                 :            : 
      15                 :            :      * the GNU General Public License as published by the Free
      16                 :            :        Software Foundation; either version 2 of the License, or (at
      17                 :            :        your option) any later version
      18                 :            : 
      19                 :            :    or both in parallel, as here.
      20                 :            : 
      21                 :            :    elfutils is distributed in the hope that it will be useful, but
      22                 :            :    WITHOUT ANY WARRANTY; without even the implied warranty of
      23                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      24                 :            :    General Public License for more details.
      25                 :            : 
      26                 :            :    You should have received copies of the GNU General Public License and
      27                 :            :    the GNU Lesser General Public License along with this program.  If
      28                 :            :    not, see <http://www.gnu.org/licenses/>.  */
      29                 :            : 
      30                 :            : #ifdef HAVE_CONFIG_H
      31                 :            : # include <config.h>
      32                 :            : #endif
      33                 :            : 
      34                 :            : #include <assert.h>
      35                 :            : #include <stddef.h>
      36                 :            : #include <stdlib.h>
      37                 :            : 
      38                 :            : #include "libelfP.h"
      39                 :            : 
      40                 :            : 
      41                 :            : int
      42                 :      17257 : elf_end (Elf *elf)
      43                 :            : {
      44                 :      17260 :   Elf *parent;
      45                 :            : 
      46         [ +  + ]:      17260 :   if (elf == NULL)
      47                 :            :     /* This is allowed and is a no-op.  */
      48                 :            :     return 0;
      49                 :            : 
      50                 :            :   /* Make sure we are alone.  */
      51                 :      16908 :   rwlock_wrlock (elf->lock);
      52                 :            : 
      53   [ +  +  +  - ]:      16908 :   if (elf->ref_count != 0 && --elf->ref_count != 0)
      54                 :            :     {
      55                 :            :       /* Not yet the last activation.  */
      56                 :            :       int result = elf->ref_count;
      57                 :            :       rwlock_unlock (elf->lock);
      58                 :            :       return result;
      59                 :            :     }
      60                 :            : 
      61         [ +  + ]:      16908 :   if (elf->kind == ELF_K_AR)
      62                 :            :     {
      63                 :            :       /* We cannot remove the descriptor now since we still have some
      64                 :            :          descriptors which depend on it.  But we can free the archive
      65                 :            :          symbol table since this is only available via the archive ELF
      66                 :            :          descriptor.  The long name table cannot be freed yet since
      67                 :            :          the archive headers for the ELF files in the archive point
      68                 :            :          into this array.  */
      69         [ +  - ]:        160 :       if (elf->state.ar.ar_sym != (Elf_Arsym *) -1l)
      70                 :        160 :         free (elf->state.ar.ar_sym);
      71                 :        160 :       elf->state.ar.ar_sym = NULL;
      72                 :            : 
      73         [ +  + ]:        160 :       if (elf->state.ar.children != NULL)
      74                 :            :         return 0;
      75                 :            :     }
      76                 :            : 
      77                 :            :   /* Remove this structure from the children list.  */
      78                 :      16905 :   parent = elf->parent;
      79         [ +  + ]:      16905 :   if (parent != NULL)
      80                 :            :     {
      81                 :            :       /* This is tricky.  Lock must be acquire from the father to
      82                 :            :          the child but here we already have the child lock.  We
      83                 :            :          solve this problem by giving free the child lock.  The
      84                 :            :          state of REF_COUNT==0 is handled all over the library, so
      85                 :            :          this should be ok.  */
      86                 :       7264 :       rwlock_unlock (elf->lock);
      87                 :       7264 :       rwlock_rdlock (parent->lock);
      88                 :       7264 :       rwlock_wrlock (elf->lock);
      89                 :            : 
      90         [ +  + ]:       7264 :       if (parent->state.ar.children == elf)
      91                 :       7262 :         parent->state.ar.children = elf->next;
      92                 :            :       else
      93                 :            :         {
      94                 :            :           struct Elf *child = parent->state.ar.children;
      95                 :            : 
      96         [ +  + ]:          3 :           while (child->next != elf)
      97                 :            :             child = child->next;
      98                 :            : 
      99                 :          2 :           child->next = elf->next;
     100                 :            :         }
     101                 :            : 
     102                 :      16905 :       rwlock_unlock (parent->lock);
     103                 :            :     }
     104                 :            : 
     105                 :            :   /* This was the last activation.  Free all resources.  */
     106      [ +  +  + ]:      16905 :   switch (elf->kind)
     107                 :            :     {
     108                 :        157 :     case ELF_K_AR:
     109         [ +  + ]:        157 :       if (elf->state.ar.long_names != NULL)
     110                 :        102 :         free (elf->state.ar.long_names);
     111                 :            :       break;
     112                 :            : 
     113                 :      16163 :     case ELF_K_ELF:
     114                 :            :       {
     115                 :      16163 :         Elf_Data_Chunk *rawchunks
     116                 :            :           = (elf->class == ELFCLASS32
     117                 :            :              || (offsetof (struct Elf, state.elf32.rawchunks)
     118                 :            :                  == offsetof (struct Elf, state.elf64.rawchunks))
     119                 :            :              ? elf->state.elf32.rawchunks
     120                 :            :              : elf->state.elf64.rawchunks);
     121         [ +  + ]:      17182 :         while (rawchunks != NULL)
     122                 :            :           {
     123                 :       1019 :             Elf_Data_Chunk *next = rawchunks->next;
     124         [ +  + ]:       1019 :             if (rawchunks->dummy_scn.flags & ELF_F_MALLOCED)
     125                 :        572 :               free (rawchunks->data.d.d_buf);
     126                 :       1019 :             free (rawchunks);
     127                 :       1019 :             rawchunks = next;
     128                 :            :           }
     129                 :            : 
     130                 :      16163 :         Elf_ScnList *list = (elf->class == ELFCLASS32
     131                 :            :                              || (offsetof (struct Elf, state.elf32.scns)
     132                 :            :                                  == offsetof (struct Elf, state.elf64.scns))
     133                 :            :                              ? &elf->state.elf32.scns
     134                 :            :                              : &elf->state.elf64.scns);
     135                 :            : 
     136                 :      17148 :         do
     137                 :            :           {
     138                 :            :             /* Free all separately allocated section headers.  */
     139                 :      17148 :             size_t cnt = list->max;
     140                 :            : 
     141         [ +  + ]:    6573564 :             while (cnt-- > 0)
     142                 :            :               {
     143                 :            :                 /* These pointers can be NULL; it's safe to use
     144                 :            :                    'free' since it will check for this.  */
     145                 :    6556416 :                 Elf_Scn *scn = &list->data[cnt];
     146                 :    6556416 :                 Elf_Data_List *runp;
     147                 :            : 
     148         [ +  + ]:    6556416 :                 if ((scn->shdr_flags & ELF_F_MALLOCED) != 0)
     149                 :            :                   /* It doesn't matter which pointer.  */
     150                 :    1652917 :                   free (scn->shdr.e32);
     151                 :            : 
     152                 :            :                 /* Free zdata if uncompressed, but not yet used as
     153                 :            :                    rawdata_base.  If it is already used it will be
     154                 :            :                    freed below.  */
     155         [ +  + ]:    6556416 :                 if (scn->zdata_base != scn->rawdata_base)
     156                 :    1857112 :                   free (scn->zdata_base);
     157                 :            : 
     158                 :            :                 /* If the file has the same byte order and the
     159                 :            :                    architecture doesn't require overly stringent
     160                 :            :                    alignment the raw data buffer is the same as the
     161                 :            :                    one used for presenting to the caller.  */
     162         [ +  + ]:    6556416 :                 if (scn->data_base != scn->rawdata_base)
     163                 :     269468 :                   free (scn->data_base);
     164                 :            : 
     165                 :            :                 /* The section data is allocated if we couldn't mmap
     166                 :            :                    the file.  Or if we had to decompress.  */
     167         [ +  + ]:    6556416 :                 if (elf->map_address == NULL
     168         [ +  + ]:    4015677 :                     || scn->rawdata_base == scn->zdata_base
     169         [ -  + ]:    1125269 :                     || (scn->flags & ELF_F_MALLOCED) != 0)
     170                 :    5431147 :                   free (scn->rawdata_base);
     171                 :            : 
     172                 :            :                 /* Free the list of data buffers for the section.
     173                 :            :                    We don't free the buffers themselves since this
     174                 :            :                    is the users job.  */
     175                 :    6556416 :                 runp = scn->data_list.next;
     176         [ +  + ]:    6556510 :                 while (runp != NULL)
     177                 :            :                   {
     178                 :         94 :                     Elf_Data_List *oldp = runp;
     179                 :         94 :                     runp = runp->next;
     180         [ +  - ]:         94 :                     if ((oldp->flags & ELF_F_MALLOCED) != 0)
     181                 :         94 :                       free (oldp);
     182                 :            :                   }
     183                 :            :               }
     184                 :            : 
     185                 :            :             /* Free the memory for the array.  */
     186                 :      17148 :             Elf_ScnList *oldp = list;
     187                 :      17148 :             list = list->next;
     188   [ +  +  -  + ]:      17148 :             assert (list == NULL || oldp->cnt == oldp->max);
     189         [ +  + ]:      17148 :             if (oldp != (elf->class == ELFCLASS32
     190                 :            :                          || (offsetof (struct Elf, state.elf32.scns)
     191                 :            :                              == offsetof (struct Elf, state.elf64.scns))
     192                 :            :                          ? &elf->state.elf32.scns
     193                 :            :                          : &elf->state.elf64.scns))
     194                 :        985 :               free (oldp);
     195                 :            :           }
     196         [ +  + ]:      17148 :         while (list != NULL);
     197                 :            :       }
     198                 :            : 
     199                 :            :       /* Free the section header.  */
     200         [ +  + ]:      16163 :       if (elf->state.elf.shdr_malloced  != 0)
     201                 :       2351 :         free (elf->class == ELFCLASS32
     202                 :            :               || (offsetof (struct Elf, state.elf32.shdr)
     203                 :            :                   == offsetof (struct Elf, state.elf64.shdr))
     204                 :       2351 :               ? (void *) elf->state.elf32.shdr
     205                 :            :               : (void *) elf->state.elf64.shdr);
     206                 :            : 
     207                 :            :       /* Free the program header.  */
     208         [ +  + ]:      16163 :       if ((elf->state.elf.phdr_flags & ELF_F_MALLOCED) != 0)
     209                 :       1471 :         free (elf->class == ELFCLASS32
     210                 :            :               || (offsetof (struct Elf, state.elf32.phdr)
     211                 :            :                   == offsetof (struct Elf, state.elf64.phdr))
     212                 :       1471 :               ? (void *) elf->state.elf32.phdr
     213                 :            :               : (void *) elf->state.elf64.phdr);
     214                 :            :       break;
     215                 :            : 
     216                 :            :     default:
     217                 :            :       break;
     218                 :            :     }
     219                 :            : 
     220   [ +  +  +  + ]:      16905 :   if (elf->map_address != NULL && parent == NULL)
     221                 :            :     {
     222                 :            :       /* The file was read or mapped for this descriptor.  */
     223         [ +  + ]:       7858 :       if ((elf->flags & ELF_F_MALLOCED) != 0)
     224                 :         64 :         free (elf->map_address);
     225         [ +  + ]:       7794 :       else if ((elf->flags & ELF_F_MMAPPED) != 0)
     226                 :       7758 :         munmap (elf->map_address, elf->maximum_size);
     227                 :            :     }
     228                 :            : 
     229                 :      16905 :   rwlock_unlock (elf->lock);
     230                 :      16905 :   rwlock_fini (elf->lock);
     231                 :            : 
     232                 :            :   /* Finally the descriptor itself.  */
     233                 :      16905 :   free (elf);
     234                 :            : 
     235         [ +  + ]:       7264 :   return (parent != NULL && parent->ref_count == 0
     236         [ +  + ]:      16905 :           ? INTUSE(elf_end) (parent) : 0);
     237                 :            : }
     238                 :            : INTDEF(elf_end)

Generated by: LCOV version 1.16