Coverage Report

Created: 2026-11-22 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/libelf/elf_end.c
Line
Count
Source
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
free_chunk (void *n)
45
2.67k
{
46
2.67k
  Elf_Data_Chunk *rawchunk = (Elf_Data_Chunk *)n;
47
2.67k
  if (rawchunk->dummy_scn.flags & ELF_F_MALLOCED)
48
2.21k
    free (rawchunk->data.d.d_buf);
49
2.67k
  free (rawchunk);
50
2.67k
}
51
52
int
53
elf_end (Elf *elf)
54
259k
{
55
259k
  Elf *parent;
56
57
259k
  if (elf == NULL)
58
    /* This is allowed and is a no-op.  */
59
112
    return 0;
60
61
  /* Make sure we are alone.  */
62
259k
  rwlock_wrlock (elf->lock);
63
64
259k
  if (elf->ref_count != 0 && --elf->ref_count != 0)
65
0
    {
66
      /* Not yet the last activation.  */
67
0
      int result = elf->ref_count;
68
0
      rwlock_unlock (elf->lock);
69
0
      return result;
70
0
    }
71
72
259k
  if (elf->kind == ELF_K_AR)
73
224k
    {
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
224k
      if (elf->state.ar.ar_sym != (Elf_Arsym *) -1l)
81
224k
  free (elf->state.ar.ar_sym);
82
224k
      elf->state.ar.ar_sym = NULL;
83
84
224k
      if (elf->state.ar.children != NULL)
85
34.9k
  {
86
34.9k
    rwlock_unlock(elf->lock);
87
34.9k
    return 0;
88
34.9k
  }
89
224k
    }
90
91
  /* Remove this structure from the children list.  */
92
224k
  parent = elf->parent;
93
224k
  if (parent != NULL)
94
208k
    {
95
      /* This is tricky.  Lock must be acquire from the father to
96
   the child but here we already have the child lock.  We
97
   solve this problem by giving free the child lock.  The
98
   state of REF_COUNT==0 is handled all over the library, so
99
   this should be ok.  */
100
208k
      rwlock_unlock (elf->lock);
101
208k
      rwlock_rdlock (parent->lock);
102
208k
      rwlock_wrlock (elf->lock);
103
104
208k
      if (parent->state.ar.children == elf)
105
190k
  parent->state.ar.children = elf->next;
106
17.8k
      else
107
17.8k
  {
108
17.8k
    struct Elf *child = parent->state.ar.children;
109
110
700k
    while (child->next != elf)
111
682k
      child = child->next;
112
113
17.8k
    child->next = elf->next;
114
17.8k
  }
115
116
208k
      rwlock_unlock (parent->lock);
117
208k
    }
118
119
  /* This was the last activation.  Free all resources.  */
120
224k
  switch (elf->kind)
121
224k
    {
122
190k
    case ELF_K_AR:
123
190k
      if (elf->state.ar.long_names != NULL)
124
266
  free (elf->state.ar.long_names);
125
190k
      break;
126
127
26.9k
    case ELF_K_ELF:
128
26.9k
      {
129
26.9k
  void *rawchunks
130
26.9k
    = (elf->class == ELFCLASS32
131
10.0k
       || (offsetof (struct Elf, state.elf32.rawchunks)
132
10.0k
     == offsetof (struct Elf, state.elf64.rawchunks))
133
26.9k
       ? elf->state.elf32.rawchunks
134
26.9k
       : elf->state.elf64.rawchunks);
135
26.9k
  tdestroy (rawchunks, free_chunk);
136
137
26.9k
  Elf_ScnList *list = (elf->class == ELFCLASS32
138
10.0k
           || (offsetof (struct Elf, state.elf32.scns)
139
10.0k
         == offsetof (struct Elf, state.elf64.scns))
140
26.9k
           ? &elf->state.elf32.scns
141
26.9k
           : &elf->state.elf64.scns);
142
143
26.9k
  do
144
26.9k
    {
145
      /* Free all separately allocated section headers.  */
146
26.9k
      size_t cnt = list->max;
147
148
17.8M
      while (cnt-- > 0)
149
17.8M
        {
150
    /* These pointers can be NULL; it's safe to use
151
       'free' since it will check for this.  */
152
17.8M
    Elf_Scn *scn = &list->data[cnt];
153
17.8M
    Elf_Data_List *runp;
154
155
17.8M
    if ((scn->shdr_flags & ELF_F_MALLOCED) != 0)
156
      /* It doesn't matter which pointer.  */
157
0
      free (scn->shdr.e32);
158
159
    /* Free zdata if uncompressed, but not yet used as
160
       rawdata_base.  If it is already used it will be
161
       freed below.  */
162
17.8M
    if (scn->zdata_base != scn->rawdata_base)
163
5.23M
      {
164
5.23M
        free (scn->zdata_base);
165
5.23M
        scn->zdata_base = NULL;
166
5.23M
      }
167
168
    /* If the file has the same byte order and the
169
       architecture doesn't require overly stringent
170
       alignment the raw data buffer is the same as the
171
       one used for presenting to the caller.  */
172
17.8M
    if (scn->data_base != scn->rawdata_base)
173
6.16k
      free (scn->data_base);
174
175
    /* The section data is allocated if we couldn't mmap
176
       the file.  Or if we had to decompress.  */
177
17.8M
    if (elf->map_address == NULL
178
17.8M
        || scn->rawdata_base == scn->zdata_base
179
5.23M
        || (scn->flags & ELF_F_MALLOCED) != 0)
180
12.6M
      free (scn->rawdata_base);
181
182
    /* Free the list of data buffers for the section.
183
       We don't free the buffers themselves since this
184
       is the users job.  */
185
17.8M
    runp = scn->data_list.next;
186
17.8M
    while (runp != NULL)
187
0
      {
188
0
        Elf_Data_List *oldp = runp;
189
0
        runp = runp->next;
190
0
        if ((oldp->flags & ELF_F_MALLOCED) != 0)
191
0
          free (oldp);
192
0
      }
193
17.8M
        }
194
195
      /* Free the memory for the array.  */
196
26.9k
      Elf_ScnList *oldp = list;
197
26.9k
      list = list->next;
198
26.9k
      assert (list == NULL || oldp->cnt == oldp->max);
199
26.9k
      if (oldp != (elf->class == ELFCLASS32
200
10.0k
       || (offsetof (struct Elf, state.elf32.scns)
201
10.0k
           == offsetof (struct Elf, state.elf64.scns))
202
26.9k
       ? &elf->state.elf32.scns
203
26.9k
       : &elf->state.elf64.scns))
204
0
        free (oldp);
205
26.9k
    }
206
26.9k
  while (list != NULL);
207
26.9k
      }
208
209
      /* Free the section header.  */
210
26.9k
      if (elf->state.elf.shdr_malloced  != 0)
211
2.82k
  free (elf->class == ELFCLASS32
212
1.49k
        || (offsetof (struct Elf, state.elf32.shdr)
213
1.49k
      == offsetof (struct Elf, state.elf64.shdr))
214
2.82k
        ? (void *) elf->state.elf32.shdr
215
2.82k
        : (void *) elf->state.elf64.shdr);
216
217
      /* Free the program header.  */
218
26.9k
      if ((elf->state.elf.phdr_flags & ELF_F_MALLOCED) != 0)
219
1.20k
  free (elf->class == ELFCLASS32
220
683
        || (offsetof (struct Elf, state.elf32.phdr)
221
683
      == offsetof (struct Elf, state.elf64.phdr))
222
1.20k
        ? (void *) elf->state.elf32.phdr
223
1.20k
        : (void *) elf->state.elf64.phdr);
224
26.9k
      break;
225
226
7.12k
    default:
227
7.12k
      break;
228
224k
    }
229
230
224k
  if (elf->map_address != NULL && parent == NULL)
231
15.3k
    {
232
      /* The file was read or mapped for this descriptor.  */
233
15.3k
      if ((elf->flags & ELF_F_MALLOCED) != 0)
234
3.09k
  free (elf->map_address);
235
12.3k
      else if ((elf->flags & ELF_F_MMAPPED) != 0)
236
12.0k
  munmap (elf->map_address, elf->maximum_size);
237
15.3k
    }
238
239
224k
  rwlock_unlock (elf->lock);
240
224k
  rwlock_fini (elf->lock);
241
242
  /* Finally the descriptor itself.  */
243
224k
  free (elf);
244
245
224k
  return (parent != NULL && parent->ref_count == 0
246
224k
    ? INTUSE(elf_end) (parent) : 0);
247
224k
}
248
INTDEF(elf_end)