Coverage Report

Created: 2026-10-17 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/libelf/elf32_getshdr.c
Line
Count
Source
1
/* Return section header.
2
   Copyright (C) 1998-2002, 2005, 2007, 2009, 2012, 2014, 2015 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 <errno.h>
36
#include <stdbool.h>
37
38
#include "libelfP.h"
39
#include "common.h"
40
41
#ifndef LIBELFBITS
42
# define LIBELFBITS 32
43
#endif
44
45
46
static ElfW2(LIBELFBITS,Shdr) *
47
load_shdr_wrlock (Elf_Scn *scn)
48
16.1k
{
49
16.1k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
16.1k
  Elf *elf = scn->elf;
53
16.1k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
16.1k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
16.1k
  if (result != NULL)
58
0
    goto out;
59
60
16.1k
  size_t shnum;
61
16.1k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
16.1k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
16.1k
  size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
65
66
  /* Allocate memory for the section headers.  We know the number
67
     of entries from the ELF header.  */
68
16.1k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
16.1k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
16.1k
  if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
71
0
    {
72
0
      __libelf_seterrno (ELF_E_NOMEM);
73
0
      goto out;
74
0
    }
75
16.1k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
16.1k
  if (elf->map_address != NULL)
78
5.06k
    {
79
      /* First see whether the information in the ELF header is
80
   valid and it does not ask for too much.  */
81
5.06k
      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
82
5.06k
    || unlikely (elf->maximum_size - ehdr->e_shoff < size))
83
0
  {
84
    /* Something is wrong.  */
85
0
    __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
86
0
    goto free_and_out;
87
0
  }
88
89
5.06k
      ElfW2(LIBELFBITS,Shdr) *notcvt;
90
91
      /* All the data is already mapped.  If we could use it
92
   directly this would already have happened.  Unless
93
   we allocated the memory ourselves and the ELF_F_MALLOCED
94
   flag is set.  */
95
5.06k
      void *file_shdr = ((char *) elf->map_address
96
5.06k
       + elf->start_offset + ehdr->e_shoff);
97
98
5.06k
      assert ((elf->flags & ELF_F_MALLOCED)
99
5.06k
        || ehdr->e_ident[EI_DATA] != MY_ELFDATA
100
5.06k
        || elf->cmd == ELF_C_READ_MMAP
101
5.06k
        || (! ALLOW_UNALIGNED
102
5.06k
      && ((uintptr_t) file_shdr
103
5.06k
          & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
104
105
      /* Now copy the data and at the same time convert the byte order.  */
106
5.06k
      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
107
610
  {
108
610
    assert ((elf->flags & ELF_F_MALLOCED)
109
610
      || elf->cmd == ELF_C_READ_MMAP
110
610
      || ! ALLOW_UNALIGNED);
111
610
    memcpy (shdr, file_shdr, size);
112
610
  }
113
4.45k
      else
114
4.45k
  {
115
4.45k
    bool copy = ! (ALLOW_UNALIGNED
116
0
       || ((uintptr_t) file_shdr
117
0
           & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1))
118
0
           == 0);
119
4.45k
    if (! copy)
120
4.45k
      notcvt = (ElfW2(LIBELFBITS,Shdr) *)
121
4.45k
        ((char *) elf->map_address
122
4.45k
         + elf->start_offset + ehdr->e_shoff);
123
0
    else
124
0
      {
125
0
        notcvt = (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
126
0
        if (unlikely (notcvt == NULL))
127
0
    {
128
0
      __libelf_seterrno (ELF_E_NOMEM);
129
0
      goto out;
130
0
    }
131
0
        memcpy (notcvt, ((char *) elf->map_address
132
0
             + elf->start_offset + ehdr->e_shoff),
133
0
          size);
134
0
      }
135
136
3.14M
    for (size_t cnt = 0; cnt < shnum; ++cnt)
137
3.14M
      {
138
3.14M
        CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
139
3.14M
        CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
140
3.14M
        CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
141
3.14M
        CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
142
3.14M
        CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
143
3.14M
        CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
144
3.14M
        CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
145
3.14M
        CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
146
3.14M
        CONVERT_TO (shdr[cnt].sh_addralign,
147
3.14M
        notcvt[cnt].sh_addralign);
148
3.14M
        CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
149
150
        /* If this is a section with an extended index add a
151
     reference in the section which uses the extended
152
     index.  */
153
3.14M
        if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
154
2.57k
      && shdr[cnt].sh_link < shnum)
155
1.07k
    elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
156
1.07k
      = cnt;
157
158
        /* Set the own shndx_index field in case it has not yet
159
     been set.  */
160
3.14M
        if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
161
3.14M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
162
3.14M
      = -1;
163
3.14M
      }
164
165
4.45k
    if (copy)
166
0
      free (notcvt);
167
4.45k
  }
168
5.06k
    }
169
11.1k
  else if (likely (elf->fildes != -1))
170
11.1k
    {
171
      /* Read the header.  */
172
11.1k
      ssize_t n = pread_retry (elf->fildes,
173
11.1k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
174
11.1k
             elf->start_offset + ehdr->e_shoff);
175
11.1k
      if (unlikely ((size_t) n != size))
176
0
  {
177
    /* Severe problems.  We cannot read the data.  */
178
0
    __libelf_seterrno (ELF_E_READ_ERROR);
179
0
    goto free_and_out;
180
0
  }
181
182
      /* If the byte order of the file is not the same as the one
183
   of the host convert the data now.  */
184
11.1k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
185
1.33M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
186
1.32M
    {
187
1.32M
      CONVERT (shdr[cnt].sh_name);
188
1.32M
      CONVERT (shdr[cnt].sh_type);
189
1.32M
      CONVERT (shdr[cnt].sh_flags);
190
1.32M
      CONVERT (shdr[cnt].sh_addr);
191
1.32M
      CONVERT (shdr[cnt].sh_offset);
192
1.32M
      CONVERT (shdr[cnt].sh_size);
193
1.32M
      CONVERT (shdr[cnt].sh_link);
194
1.32M
      CONVERT (shdr[cnt].sh_info);
195
1.32M
      CONVERT (shdr[cnt].sh_addralign);
196
1.32M
      CONVERT (shdr[cnt].sh_entsize);
197
1.32M
    }
198
11.1k
    }
199
0
  else
200
0
    {
201
      /* The file descriptor was already enabled and not all data was
202
   read.  Undo the allocation.  */
203
0
      __libelf_seterrno (ELF_E_FD_DISABLED);
204
205
0
    free_and_out:
206
0
      free (shdr);
207
0
      elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
208
0
      elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
209
210
0
      goto out;
211
0
    }
212
213
  /* Set the pointers in the `scn's.  */
214
5.72M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
215
5.70M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
216
5.70M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
217
218
16.1k
  result = scn->shdr.ELFW(e,LIBELFBITS);
219
16.1k
  assert (result != NULL);
220
221
16.1k
out:
222
16.1k
  return result;
223
16.1k
}
elf32_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
11.9k
{
49
11.9k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
11.9k
  Elf *elf = scn->elf;
53
11.9k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
11.9k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
11.9k
  if (result != NULL)
58
0
    goto out;
59
60
11.9k
  size_t shnum;
61
11.9k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
11.9k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
11.9k
  size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
65
66
  /* Allocate memory for the section headers.  We know the number
67
     of entries from the ELF header.  */
68
11.9k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
11.9k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
11.9k
  if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
71
0
    {
72
0
      __libelf_seterrno (ELF_E_NOMEM);
73
0
      goto out;
74
0
    }
75
11.9k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
11.9k
  if (elf->map_address != NULL)
78
3.09k
    {
79
      /* First see whether the information in the ELF header is
80
   valid and it does not ask for too much.  */
81
3.09k
      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
82
3.09k
    || unlikely (elf->maximum_size - ehdr->e_shoff < size))
83
0
  {
84
    /* Something is wrong.  */
85
0
    __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
86
0
    goto free_and_out;
87
0
  }
88
89
3.09k
      ElfW2(LIBELFBITS,Shdr) *notcvt;
90
91
      /* All the data is already mapped.  If we could use it
92
   directly this would already have happened.  Unless
93
   we allocated the memory ourselves and the ELF_F_MALLOCED
94
   flag is set.  */
95
3.09k
      void *file_shdr = ((char *) elf->map_address
96
3.09k
       + elf->start_offset + ehdr->e_shoff);
97
98
3.09k
      assert ((elf->flags & ELF_F_MALLOCED)
99
3.09k
        || ehdr->e_ident[EI_DATA] != MY_ELFDATA
100
3.09k
        || elf->cmd == ELF_C_READ_MMAP
101
3.09k
        || (! ALLOW_UNALIGNED
102
3.09k
      && ((uintptr_t) file_shdr
103
3.09k
          & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
104
105
      /* Now copy the data and at the same time convert the byte order.  */
106
3.09k
      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
107
144
  {
108
144
    assert ((elf->flags & ELF_F_MALLOCED)
109
144
      || elf->cmd == ELF_C_READ_MMAP
110
144
      || ! ALLOW_UNALIGNED);
111
144
    memcpy (shdr, file_shdr, size);
112
144
  }
113
2.95k
      else
114
2.95k
  {
115
2.95k
    bool copy = ! (ALLOW_UNALIGNED
116
0
       || ((uintptr_t) file_shdr
117
0
           & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1))
118
0
           == 0);
119
2.95k
    if (! copy)
120
2.95k
      notcvt = (ElfW2(LIBELFBITS,Shdr) *)
121
2.95k
        ((char *) elf->map_address
122
2.95k
         + elf->start_offset + ehdr->e_shoff);
123
0
    else
124
0
      {
125
0
        notcvt = (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
126
0
        if (unlikely (notcvt == NULL))
127
0
    {
128
0
      __libelf_seterrno (ELF_E_NOMEM);
129
0
      goto out;
130
0
    }
131
0
        memcpy (notcvt, ((char *) elf->map_address
132
0
             + elf->start_offset + ehdr->e_shoff),
133
0
          size);
134
0
      }
135
136
1.49M
    for (size_t cnt = 0; cnt < shnum; ++cnt)
137
1.48M
      {
138
1.48M
        CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
139
1.48M
        CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
140
1.48M
        CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
141
1.48M
        CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
142
1.48M
        CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
143
1.48M
        CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
144
1.48M
        CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
145
1.48M
        CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
146
1.48M
        CONVERT_TO (shdr[cnt].sh_addralign,
147
1.48M
        notcvt[cnt].sh_addralign);
148
1.48M
        CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
149
150
        /* If this is a section with an extended index add a
151
     reference in the section which uses the extended
152
     index.  */
153
1.48M
        if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
154
1.08k
      && shdr[cnt].sh_link < shnum)
155
518
    elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
156
518
      = cnt;
157
158
        /* Set the own shndx_index field in case it has not yet
159
     been set.  */
160
1.48M
        if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
161
1.48M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
162
1.48M
      = -1;
163
1.48M
      }
164
165
2.95k
    if (copy)
166
0
      free (notcvt);
167
2.95k
  }
168
3.09k
    }
169
8.85k
  else if (likely (elf->fildes != -1))
170
8.85k
    {
171
      /* Read the header.  */
172
8.85k
      ssize_t n = pread_retry (elf->fildes,
173
8.85k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
174
8.85k
             elf->start_offset + ehdr->e_shoff);
175
8.85k
      if (unlikely ((size_t) n != size))
176
0
  {
177
    /* Severe problems.  We cannot read the data.  */
178
0
    __libelf_seterrno (ELF_E_READ_ERROR);
179
0
    goto free_and_out;
180
0
  }
181
182
      /* If the byte order of the file is not the same as the one
183
   of the host convert the data now.  */
184
8.85k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
185
1.10M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
186
1.09M
    {
187
1.09M
      CONVERT (shdr[cnt].sh_name);
188
1.09M
      CONVERT (shdr[cnt].sh_type);
189
1.09M
      CONVERT (shdr[cnt].sh_flags);
190
1.09M
      CONVERT (shdr[cnt].sh_addr);
191
1.09M
      CONVERT (shdr[cnt].sh_offset);
192
1.09M
      CONVERT (shdr[cnt].sh_size);
193
1.09M
      CONVERT (shdr[cnt].sh_link);
194
1.09M
      CONVERT (shdr[cnt].sh_info);
195
1.09M
      CONVERT (shdr[cnt].sh_addralign);
196
1.09M
      CONVERT (shdr[cnt].sh_entsize);
197
1.09M
    }
198
8.85k
    }
199
0
  else
200
0
    {
201
      /* The file descriptor was already enabled and not all data was
202
   read.  Undo the allocation.  */
203
0
      __libelf_seterrno (ELF_E_FD_DISABLED);
204
205
0
    free_and_out:
206
0
      free (shdr);
207
0
      elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
208
0
      elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
209
210
0
      goto out;
211
0
    }
212
213
  /* Set the pointers in the `scn's.  */
214
3.60M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
215
3.59M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
216
3.59M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
217
218
11.9k
  result = scn->shdr.ELFW(e,LIBELFBITS);
219
11.9k
  assert (result != NULL);
220
221
11.9k
out:
222
11.9k
  return result;
223
11.9k
}
elf64_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
4.24k
{
49
4.24k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
4.24k
  Elf *elf = scn->elf;
53
4.24k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
4.24k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
4.24k
  if (result != NULL)
58
0
    goto out;
59
60
4.24k
  size_t shnum;
61
4.24k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
4.24k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
4.24k
  size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
65
66
  /* Allocate memory for the section headers.  We know the number
67
     of entries from the ELF header.  */
68
4.24k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
4.24k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
4.24k
  if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
71
0
    {
72
0
      __libelf_seterrno (ELF_E_NOMEM);
73
0
      goto out;
74
0
    }
75
4.24k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
4.24k
  if (elf->map_address != NULL)
78
1.97k
    {
79
      /* First see whether the information in the ELF header is
80
   valid and it does not ask for too much.  */
81
1.97k
      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
82
1.97k
    || unlikely (elf->maximum_size - ehdr->e_shoff < size))
83
0
  {
84
    /* Something is wrong.  */
85
0
    __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
86
0
    goto free_and_out;
87
0
  }
88
89
1.97k
      ElfW2(LIBELFBITS,Shdr) *notcvt;
90
91
      /* All the data is already mapped.  If we could use it
92
   directly this would already have happened.  Unless
93
   we allocated the memory ourselves and the ELF_F_MALLOCED
94
   flag is set.  */
95
1.97k
      void *file_shdr = ((char *) elf->map_address
96
1.97k
       + elf->start_offset + ehdr->e_shoff);
97
98
1.97k
      assert ((elf->flags & ELF_F_MALLOCED)
99
1.97k
        || ehdr->e_ident[EI_DATA] != MY_ELFDATA
100
1.97k
        || elf->cmd == ELF_C_READ_MMAP
101
1.97k
        || (! ALLOW_UNALIGNED
102
1.97k
      && ((uintptr_t) file_shdr
103
1.97k
          & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
104
105
      /* Now copy the data and at the same time convert the byte order.  */
106
1.97k
      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
107
466
  {
108
466
    assert ((elf->flags & ELF_F_MALLOCED)
109
466
      || elf->cmd == ELF_C_READ_MMAP
110
466
      || ! ALLOW_UNALIGNED);
111
466
    memcpy (shdr, file_shdr, size);
112
466
  }
113
1.50k
      else
114
1.50k
  {
115
1.50k
    bool copy = ! (ALLOW_UNALIGNED
116
0
       || ((uintptr_t) file_shdr
117
0
           & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1))
118
0
           == 0);
119
1.50k
    if (! copy)
120
1.50k
      notcvt = (ElfW2(LIBELFBITS,Shdr) *)
121
1.50k
        ((char *) elf->map_address
122
1.50k
         + elf->start_offset + ehdr->e_shoff);
123
0
    else
124
0
      {
125
0
        notcvt = (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
126
0
        if (unlikely (notcvt == NULL))
127
0
    {
128
0
      __libelf_seterrno (ELF_E_NOMEM);
129
0
      goto out;
130
0
    }
131
0
        memcpy (notcvt, ((char *) elf->map_address
132
0
             + elf->start_offset + ehdr->e_shoff),
133
0
          size);
134
0
      }
135
136
1.65M
    for (size_t cnt = 0; cnt < shnum; ++cnt)
137
1.65M
      {
138
1.65M
        CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
139
1.65M
        CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
140
1.65M
        CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
141
1.65M
        CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
142
1.65M
        CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
143
1.65M
        CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
144
1.65M
        CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
145
1.65M
        CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
146
1.65M
        CONVERT_TO (shdr[cnt].sh_addralign,
147
1.65M
        notcvt[cnt].sh_addralign);
148
1.65M
        CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
149
150
        /* If this is a section with an extended index add a
151
     reference in the section which uses the extended
152
     index.  */
153
1.65M
        if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
154
1.48k
      && shdr[cnt].sh_link < shnum)
155
554
    elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
156
554
      = cnt;
157
158
        /* Set the own shndx_index field in case it has not yet
159
     been set.  */
160
1.65M
        if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
161
1.65M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
162
1.65M
      = -1;
163
1.65M
      }
164
165
1.50k
    if (copy)
166
0
      free (notcvt);
167
1.50k
  }
168
1.97k
    }
169
2.27k
  else if (likely (elf->fildes != -1))
170
2.27k
    {
171
      /* Read the header.  */
172
2.27k
      ssize_t n = pread_retry (elf->fildes,
173
2.27k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
174
2.27k
             elf->start_offset + ehdr->e_shoff);
175
2.27k
      if (unlikely ((size_t) n != size))
176
0
  {
177
    /* Severe problems.  We cannot read the data.  */
178
0
    __libelf_seterrno (ELF_E_READ_ERROR);
179
0
    goto free_and_out;
180
0
  }
181
182
      /* If the byte order of the file is not the same as the one
183
   of the host convert the data now.  */
184
2.27k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
185
231k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
186
230k
    {
187
230k
      CONVERT (shdr[cnt].sh_name);
188
230k
      CONVERT (shdr[cnt].sh_type);
189
230k
      CONVERT (shdr[cnt].sh_flags);
190
230k
      CONVERT (shdr[cnt].sh_addr);
191
230k
      CONVERT (shdr[cnt].sh_offset);
192
230k
      CONVERT (shdr[cnt].sh_size);
193
230k
      CONVERT (shdr[cnt].sh_link);
194
230k
      CONVERT (shdr[cnt].sh_info);
195
230k
      CONVERT (shdr[cnt].sh_addralign);
196
230k
      CONVERT (shdr[cnt].sh_entsize);
197
230k
    }
198
2.27k
    }
199
0
  else
200
0
    {
201
      /* The file descriptor was already enabled and not all data was
202
   read.  Undo the allocation.  */
203
0
      __libelf_seterrno (ELF_E_FD_DISABLED);
204
205
0
    free_and_out:
206
0
      free (shdr);
207
0
      elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
208
0
      elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
209
210
0
      goto out;
211
0
    }
212
213
  /* Set the pointers in the `scn's.  */
214
2.11M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
215
2.11M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
216
2.11M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
217
218
4.24k
  result = scn->shdr.ELFW(e,LIBELFBITS);
219
4.24k
  assert (result != NULL);
220
221
4.24k
out:
222
4.24k
  return result;
223
4.24k
}
224
225
static bool
226
scn_valid (Elf_Scn *scn)
227
5.08M
{
228
5.08M
  if (scn == NULL)
229
0
    return false;
230
231
5.08M
  if (unlikely (scn->elf->state.elf.ehdr == NULL))
232
0
    {
233
0
      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
234
0
      return false;
235
0
    }
236
237
5.08M
  if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
238
0
    {
239
0
      __libelf_seterrno (ELF_E_INVALID_CLASS);
240
0
      return false;
241
0
    }
242
243
5.08M
  return true;
244
5.08M
}
elf32_getshdr.c:scn_valid
Line
Count
Source
227
4.30M
{
228
4.30M
  if (scn == NULL)
229
0
    return false;
230
231
4.30M
  if (unlikely (scn->elf->state.elf.ehdr == NULL))
232
0
    {
233
0
      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
234
0
      return false;
235
0
    }
236
237
4.30M
  if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
238
0
    {
239
0
      __libelf_seterrno (ELF_E_INVALID_CLASS);
240
0
      return false;
241
0
    }
242
243
4.30M
  return true;
244
4.30M
}
elf64_getshdr.c:scn_valid
Line
Count
Source
227
779k
{
228
779k
  if (scn == NULL)
229
0
    return false;
230
231
779k
  if (unlikely (scn->elf->state.elf.ehdr == NULL))
232
0
    {
233
0
      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
234
0
      return false;
235
0
    }
236
237
779k
  if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
238
0
    {
239
0
      __libelf_seterrno (ELF_E_INVALID_CLASS);
240
0
      return false;
241
0
    }
242
243
779k
  return true;
244
779k
}
245
246
ElfW2(LIBELFBITS,Shdr) *
247
internal_function
248
__elfw2(LIBELFBITS,getshdr_rdlock) (Elf_Scn *scn)
249
2.55M
{
250
2.55M
  ElfW2(LIBELFBITS,Shdr) *result;
251
252
2.55M
  if (!scn_valid (scn))
253
0
    return NULL;
254
255
2.55M
  result = scn->shdr.ELFW(e,LIBELFBITS);
256
2.55M
  if (result == NULL)
257
16.1k
    {
258
16.1k
      rwlock_unlock (scn->elf->lock);
259
16.1k
      rwlock_wrlock (scn->elf->lock);
260
16.1k
      result = scn->shdr.ELFW(e,LIBELFBITS);
261
16.1k
      if (result == NULL)
262
16.1k
  result = load_shdr_wrlock (scn);
263
16.1k
    }
264
265
2.55M
  return result;
266
2.55M
}
__elf32_getshdr_rdlock
Line
Count
Source
249
2.16M
{
250
2.16M
  ElfW2(LIBELFBITS,Shdr) *result;
251
252
2.16M
  if (!scn_valid (scn))
253
0
    return NULL;
254
255
2.16M
  result = scn->shdr.ELFW(e,LIBELFBITS);
256
2.16M
  if (result == NULL)
257
11.9k
    {
258
11.9k
      rwlock_unlock (scn->elf->lock);
259
11.9k
      rwlock_wrlock (scn->elf->lock);
260
11.9k
      result = scn->shdr.ELFW(e,LIBELFBITS);
261
11.9k
      if (result == NULL)
262
11.9k
  result = load_shdr_wrlock (scn);
263
11.9k
    }
264
265
2.16M
  return result;
266
2.16M
}
__elf64_getshdr_rdlock
Line
Count
Source
249
391k
{
250
391k
  ElfW2(LIBELFBITS,Shdr) *result;
251
252
391k
  if (!scn_valid (scn))
253
0
    return NULL;
254
255
391k
  result = scn->shdr.ELFW(e,LIBELFBITS);
256
391k
  if (result == NULL)
257
4.24k
    {
258
4.24k
      rwlock_unlock (scn->elf->lock);
259
4.24k
      rwlock_wrlock (scn->elf->lock);
260
4.24k
      result = scn->shdr.ELFW(e,LIBELFBITS);
261
4.24k
      if (result == NULL)
262
4.24k
  result = load_shdr_wrlock (scn);
263
4.24k
    }
264
265
391k
  return result;
266
391k
}
267
268
ElfW2(LIBELFBITS,Shdr) *
269
internal_function
270
__elfw2(LIBELFBITS,getshdr_wrlock) (Elf_Scn *scn)
271
0
{
272
0
  ElfW2(LIBELFBITS,Shdr) *result;
273
274
0
  if (!scn_valid (scn))
275
0
    return NULL;
276
277
0
  result = scn->shdr.ELFW(e,LIBELFBITS);
278
0
  if (result == NULL)
279
0
    result = load_shdr_wrlock (scn);
280
281
0
  return result;
282
0
}
Unexecuted instantiation: __elf32_getshdr_wrlock
Unexecuted instantiation: __elf64_getshdr_wrlock
283
284
ElfW2(LIBELFBITS,Shdr) *
285
elfw2(LIBELFBITS,getshdr) (Elf_Scn *scn)
286
2.53M
{
287
2.53M
  ElfW2(LIBELFBITS,Shdr) *result;
288
289
2.53M
  if (!scn_valid (scn))
290
0
    return NULL;
291
292
2.53M
  rwlock_rdlock (scn->elf->lock);
293
2.53M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
294
2.53M
  rwlock_unlock (scn->elf->lock);
295
296
2.53M
  return result;
297
2.53M
}
elf32_getshdr
Line
Count
Source
286
2.14M
{
287
2.14M
  ElfW2(LIBELFBITS,Shdr) *result;
288
289
2.14M
  if (!scn_valid (scn))
290
0
    return NULL;
291
292
2.14M
  rwlock_rdlock (scn->elf->lock);
293
2.14M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
294
2.14M
  rwlock_unlock (scn->elf->lock);
295
296
2.14M
  return result;
297
2.14M
}
elf64_getshdr
Line
Count
Source
286
387k
{
287
387k
  ElfW2(LIBELFBITS,Shdr) *result;
288
289
387k
  if (!scn_valid (scn))
290
0
    return NULL;
291
292
387k
  rwlock_rdlock (scn->elf->lock);
293
387k
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
294
387k
  rwlock_unlock (scn->elf->lock);
295
296
387k
  return result;
297
387k
}