Coverage Report

Created: 2026-01-17 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/libelf/elf_getdata_rawchunk.c
Line
Count
Source
1
/* Return converted data from raw chunk of ELF file.
2
   Copyright (C) 2007, 2014, 2015 Red Hat, Inc.
3
   Copyright (C) 2022, 2023 Mark J. Wielaard <mark@klomp.org>
4
   This file is part of elfutils.
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 <errno.h>
36
#include <string.h>
37
38
#include "libelfP.h"
39
#include "common.h"
40
#include "eu-search.h"
41
42
static int
43
chunk_compare (const void *a, const void *b)
44
180k
{
45
180k
  Elf_Data_Chunk *da = (Elf_Data_Chunk *)a;
46
180k
  Elf_Data_Chunk *db = (Elf_Data_Chunk *)b;
47
48
180k
  if (da->offset != db->offset)
49
97.0k
    return da->offset - db->offset;
50
51
83.4k
  if (da->data.d.d_size != db->data.d.d_size)
52
47.8k
    return da->data.d.d_size - db->data.d.d_size;
53
54
35.6k
  return da->data.d.d_type - db->data.d.d_type;
55
83.4k
}
56
57
Elf_Data *
58
elf_getdata_rawchunk (Elf *elf, int64_t offset, size_t size, Elf_Type type)
59
64.5k
{
60
64.5k
  if (unlikely (elf == NULL))
61
0
    return NULL;
62
63
64.5k
  if (unlikely (elf->kind != ELF_K_ELF))
64
0
    {
65
      /* No valid descriptor.  */
66
0
      __libelf_seterrno (ELF_E_INVALID_HANDLE);
67
0
      return NULL;
68
0
    }
69
70
64.5k
  if (unlikely (offset < 0 || (uint64_t) offset > elf->maximum_size
71
64.5k
    || elf->maximum_size - (uint64_t) offset < size))
72
73
25.4k
    {
74
      /* Invalid request.  */
75
25.4k
      __libelf_seterrno (ELF_E_INVALID_OP);
76
25.4k
      return NULL;
77
25.4k
    }
78
79
39.0k
  if (type >= ELF_T_NUM)
80
0
    {
81
0
      __libelf_seterrno (ELF_E_UNKNOWN_TYPE);
82
0
      return NULL;
83
0
    }
84
85
  /* Get the raw bytes from the file.  */
86
39.0k
  void *rawchunk;
87
39.0k
  int flags = 0;
88
39.0k
  Elf_Data *result = NULL;
89
90
39.0k
  rwlock_wrlock (elf->lock);
91
92
  /* Maybe we already got this chunk?  */
93
39.0k
  Elf_Data_Chunk key;
94
39.0k
  key.offset = offset;
95
39.0k
  key.data.d.d_size = size;
96
39.0k
  key.data.d.d_type = type;
97
39.0k
  Elf_Data_Chunk **found
98
39.0k
    = eu_tsearch_nolock (&key, &elf->state.elf.rawchunk_tree, &chunk_compare);
99
100
39.0k
  if (found == NULL)
101
0
    goto nomem;
102
103
  /* Existing entry.  */
104
39.0k
  if (*found != &key && *found != NULL)
105
35.3k
    {
106
35.3k
      result = &(*found)->data.d;
107
35.3k
      goto out;
108
35.3k
    }
109
110
  /* New entry.  Note that *found will point to the newly inserted
111
     (dummy) key.  We'll replace it with a real rawchunk when that is
112
     setup.  Make sure to tdelete the dummy key if anything goes
113
     wrong.  */
114
115
3.72k
  size_t align = __libelf_type_align (elf->class, type);
116
3.72k
  if (elf->map_address != NULL)
117
3.72k
    {
118
    /* If the file is mmap'ed we can use it directly, if aligned for type.  */
119
3.72k
      char *rawdata = elf->map_address + elf->start_offset + offset;
120
3.72k
      if (((uintptr_t) rawdata & (align - 1)) == 0)
121
2.68k
  rawchunk = rawdata;
122
1.03k
      else
123
1.03k
  {
124
    /* We allocate the memory and memcpy it to get aligned data. */
125
1.03k
    rawchunk = malloc (size);
126
1.03k
    if (rawchunk == NULL)
127
0
      goto nomem;
128
1.03k
    memcpy (rawchunk, rawdata, size);
129
1.03k
    flags = ELF_F_MALLOCED;
130
1.03k
  }
131
3.72k
    }
132
0
  else
133
0
    {
134
      /* We allocate the memory and read the data from the file.  */
135
0
      rawchunk = malloc (size);
136
0
      if (rawchunk == NULL)
137
0
  {
138
0
  nomem:
139
0
    eu_tdelete_nolock (&key, &elf->state.elf.rawchunk_tree,
140
0
           &chunk_compare);
141
0
    __libelf_seterrno (ELF_E_NOMEM);
142
0
    goto out;
143
0
  }
144
145
      /* Read the file content.  */
146
0
      if (unlikely ((size_t) pread_retry (elf->fildes, rawchunk, size,
147
0
            elf->start_offset + offset)
148
0
        != size))
149
0
  {
150
    /* Something went wrong.  */
151
0
    eu_tdelete_nolock (&key, &elf->state.elf.rawchunk_tree,
152
0
           &chunk_compare);
153
0
    free (rawchunk);
154
0
    __libelf_seterrno (ELF_E_READ_ERROR);
155
0
    goto out;
156
0
  }
157
158
0
      flags = ELF_F_MALLOCED;
159
0
    }
160
161
  /* Copy and/or convert the data as needed for aligned native-order access.  */
162
3.72k
  void *buffer;
163
3.72k
  if (elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA)
164
1.35k
    {
165
1.35k
      if (((uintptr_t) rawchunk & (align - 1)) == 0)
166
  /* No need to copy, we can use the raw data.  */
167
1.35k
  buffer = rawchunk;
168
0
      else
169
0
  {
170
    /* A malloc'd block is always sufficiently aligned.  */
171
0
    assert (flags == 0);
172
173
0
    buffer = malloc (size);
174
0
    if (unlikely (buffer == NULL))
175
0
      goto nomem;
176
0
    flags = ELF_F_MALLOCED;
177
178
    /* The copy will be appropriately aligned for direct access.  */
179
0
    memcpy (buffer, rawchunk, size);
180
181
0
    free (rawchunk);
182
0
  }
183
1.35k
    }
184
2.36k
  else
185
2.36k
    {
186
2.36k
      if (flags)
187
652
  buffer = rawchunk;
188
1.70k
      else
189
1.70k
  {
190
1.70k
    buffer = malloc (size);
191
1.70k
    if (unlikely (buffer == NULL))
192
0
      goto nomem;
193
1.70k
    flags = ELF_F_MALLOCED;
194
1.70k
  }
195
196
      /* Call the conversion function.  */
197
2.36k
      (*__elf_xfctstom[elf->class - 1][type])(buffer, rawchunk, size, 0);
198
199
2.36k
      if (!flags)
200
0
  free (rawchunk);
201
2.36k
    }
202
203
  /* Allocate the dummy container to point at this buffer.  */
204
3.72k
  Elf_Data_Chunk *chunk = calloc (1, sizeof *chunk);
205
3.72k
  if (chunk == NULL)
206
0
    {
207
0
      if (flags)
208
0
  free (buffer);
209
0
      goto nomem;
210
0
    }
211
212
3.72k
  chunk->dummy_scn.elf = elf;
213
3.72k
  chunk->dummy_scn.flags = flags;
214
3.72k
  chunk->data.s = &chunk->dummy_scn;
215
3.72k
  chunk->data.d.d_buf = buffer;
216
3.72k
  chunk->data.d.d_size = size;
217
3.72k
  chunk->data.d.d_type = type;
218
3.72k
  chunk->data.d.d_align = align;
219
3.72k
  chunk->data.d.d_version = EV_CURRENT;
220
3.72k
  chunk->offset = offset;
221
222
3.72k
  *found = chunk;
223
3.72k
  result = &chunk->data.d;
224
225
39.0k
 out:
226
39.0k
  rwlock_unlock (elf->lock);
227
39.0k
  return result;
228
3.72k
}