LCOV - code coverage report
Current view: top level - libelf - elf_end.c (source / functions) Hit Total Coverage
Test: elfutils-0.189 Lines: 77 77 100.0 %
Date: 2023-07-20 11:26:31 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 62 67 92.5 %

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

Generated by: LCOV version 1.16