Coverage Report

Created: 2026-09-01 06:28

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.4k
{
49
16.4k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
16.4k
  Elf *elf = scn->elf;
53
16.4k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
16.4k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
16.4k
  if (result != NULL)
58
0
    goto out;
59
60
16.4k
  size_t shnum;
61
16.4k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
16.4k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
16.4k
  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.4k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
16.4k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
16.4k
  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.4k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
16.4k
  if (elf->map_address != NULL)
78
4.42k
    {
79
      /* First see whether the information in the ELF header is
80
   valid and it does not ask for too much.  */
81
4.42k
      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
82
4.42k
    || 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
4.42k
      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
4.42k
      void *file_shdr = ((char *) elf->map_address
96
4.42k
       + elf->start_offset + ehdr->e_shoff);
97
98
4.42k
      assert ((elf->flags & ELF_F_MALLOCED)
99
4.42k
        || ehdr->e_ident[EI_DATA] != MY_ELFDATA
100
4.42k
        || elf->cmd == ELF_C_READ_MMAP
101
4.42k
        || (! ALLOW_UNALIGNED
102
4.42k
      && ((uintptr_t) file_shdr
103
4.42k
          & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
104
105
      /* Now copy the data and at the same time convert the byte order.  */
106
4.42k
      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
107
525
  {
108
525
    assert ((elf->flags & ELF_F_MALLOCED)
109
525
      || elf->cmd == ELF_C_READ_MMAP
110
525
      || ! ALLOW_UNALIGNED);
111
525
    memcpy (shdr, file_shdr, size);
112
525
  }
113
3.89k
      else
114
3.89k
  {
115
3.89k
    bool copy = ! (ALLOW_UNALIGNED
116
0
       || ((uintptr_t) file_shdr
117
0
           & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1))
118
0
           == 0);
119
3.89k
    if (! copy)
120
3.89k
      notcvt = (ElfW2(LIBELFBITS,Shdr) *)
121
3.89k
        ((char *) elf->map_address
122
3.89k
         + 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.99M
    for (size_t cnt = 0; cnt < shnum; ++cnt)
137
1.98M
      {
138
1.98M
        CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
139
1.98M
        CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
140
1.98M
        CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
141
1.98M
        CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
142
1.98M
        CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
143
1.98M
        CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
144
1.98M
        CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
145
1.98M
        CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
146
1.98M
        CONVERT_TO (shdr[cnt].sh_addralign,
147
1.98M
        notcvt[cnt].sh_addralign);
148
1.98M
        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.98M
        if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
154
67
      && shdr[cnt].sh_link < shnum)
155
47
    elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
156
47
      = cnt;
157
158
        /* Set the own shndx_index field in case it has not yet
159
     been set.  */
160
1.98M
        if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
161
1.98M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
162
1.98M
      = -1;
163
1.98M
      }
164
165
3.89k
    if (copy)
166
0
      free (notcvt);
167
3.89k
  }
168
4.42k
    }
169
12.0k
  else if (likely (elf->fildes != -1))
170
12.0k
    {
171
      /* Read the header.  */
172
12.0k
      ssize_t n = pread_retry (elf->fildes,
173
12.0k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
174
12.0k
             elf->start_offset + ehdr->e_shoff);
175
12.0k
      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
12.0k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
185
1.47M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
186
1.46M
    {
187
1.46M
      CONVERT (shdr[cnt].sh_name);
188
1.46M
      CONVERT (shdr[cnt].sh_type);
189
1.46M
      CONVERT (shdr[cnt].sh_flags);
190
1.46M
      CONVERT (shdr[cnt].sh_addr);
191
1.46M
      CONVERT (shdr[cnt].sh_offset);
192
1.46M
      CONVERT (shdr[cnt].sh_size);
193
1.46M
      CONVERT (shdr[cnt].sh_link);
194
1.46M
      CONVERT (shdr[cnt].sh_info);
195
1.46M
      CONVERT (shdr[cnt].sh_addralign);
196
1.46M
      CONVERT (shdr[cnt].sh_entsize);
197
1.46M
    }
198
12.0k
    }
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
4.71M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
215
4.69M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
216
4.69M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
217
218
16.4k
  result = scn->shdr.ELFW(e,LIBELFBITS);
219
16.4k
  assert (result != NULL);
220
221
16.4k
out:
222
16.4k
  return result;
223
16.4k
}
elf32_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
12.2k
{
49
12.2k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
12.2k
  Elf *elf = scn->elf;
53
12.2k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
12.2k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
12.2k
  if (result != NULL)
58
0
    goto out;
59
60
12.2k
  size_t shnum;
61
12.2k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
12.2k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
12.2k
  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
12.2k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
12.2k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
12.2k
  if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
71
0
    {
72
0
      __libelf_seterrno (ELF_E_NOMEM);
73
0
      goto out;
74
0
    }
75
12.2k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
12.2k
  if (elf->map_address != NULL)
78
2.71k
    {
79
      /* First see whether the information in the ELF header is
80
   valid and it does not ask for too much.  */
81
2.71k
      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
82
2.71k
    || 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
2.71k
      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
2.71k
      void *file_shdr = ((char *) elf->map_address
96
2.71k
       + elf->start_offset + ehdr->e_shoff);
97
98
2.71k
      assert ((elf->flags & ELF_F_MALLOCED)
99
2.71k
        || ehdr->e_ident[EI_DATA] != MY_ELFDATA
100
2.71k
        || elf->cmd == ELF_C_READ_MMAP
101
2.71k
        || (! ALLOW_UNALIGNED
102
2.71k
      && ((uintptr_t) file_shdr
103
2.71k
          & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
104
105
      /* Now copy the data and at the same time convert the byte order.  */
106
2.71k
      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
107
143
  {
108
143
    assert ((elf->flags & ELF_F_MALLOCED)
109
143
      || elf->cmd == ELF_C_READ_MMAP
110
143
      || ! ALLOW_UNALIGNED);
111
143
    memcpy (shdr, file_shdr, size);
112
143
  }
113
2.57k
      else
114
2.57k
  {
115
2.57k
    bool copy = ! (ALLOW_UNALIGNED
116
0
       || ((uintptr_t) file_shdr
117
0
           & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1))
118
0
           == 0);
119
2.57k
    if (! copy)
120
2.57k
      notcvt = (ElfW2(LIBELFBITS,Shdr) *)
121
2.57k
        ((char *) elf->map_address
122
2.57k
         + 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
570k
    for (size_t cnt = 0; cnt < shnum; ++cnt)
137
568k
      {
138
568k
        CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
139
568k
        CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
140
568k
        CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
141
568k
        CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
142
568k
        CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
143
568k
        CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
144
568k
        CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
145
568k
        CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
146
568k
        CONVERT_TO (shdr[cnt].sh_addralign,
147
568k
        notcvt[cnt].sh_addralign);
148
568k
        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
568k
        if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
154
23
      && shdr[cnt].sh_link < shnum)
155
13
    elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
156
13
      = cnt;
157
158
        /* Set the own shndx_index field in case it has not yet
159
     been set.  */
160
568k
        if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
161
568k
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
162
568k
      = -1;
163
568k
      }
164
165
2.57k
    if (copy)
166
0
      free (notcvt);
167
2.57k
  }
168
2.71k
    }
169
9.58k
  else if (likely (elf->fildes != -1))
170
9.58k
    {
171
      /* Read the header.  */
172
9.58k
      ssize_t n = pread_retry (elf->fildes,
173
9.58k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
174
9.58k
             elf->start_offset + ehdr->e_shoff);
175
9.58k
      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
9.58k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
185
1.22M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
186
1.22M
    {
187
1.22M
      CONVERT (shdr[cnt].sh_name);
188
1.22M
      CONVERT (shdr[cnt].sh_type);
189
1.22M
      CONVERT (shdr[cnt].sh_flags);
190
1.22M
      CONVERT (shdr[cnt].sh_addr);
191
1.22M
      CONVERT (shdr[cnt].sh_offset);
192
1.22M
      CONVERT (shdr[cnt].sh_size);
193
1.22M
      CONVERT (shdr[cnt].sh_link);
194
1.22M
      CONVERT (shdr[cnt].sh_info);
195
1.22M
      CONVERT (shdr[cnt].sh_addralign);
196
1.22M
      CONVERT (shdr[cnt].sh_entsize);
197
1.22M
    }
198
9.58k
    }
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.87M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
215
2.86M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
216
2.86M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
217
218
12.2k
  result = scn->shdr.ELFW(e,LIBELFBITS);
219
12.2k
  assert (result != NULL);
220
221
12.2k
out:
222
12.2k
  return result;
223
12.2k
}
elf64_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
4.19k
{
49
4.19k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
4.19k
  Elf *elf = scn->elf;
53
4.19k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
4.19k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
4.19k
  if (result != NULL)
58
0
    goto out;
59
60
4.19k
  size_t shnum;
61
4.19k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
4.19k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
4.19k
  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.19k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
4.19k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
4.19k
  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.19k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
4.19k
  if (elf->map_address != NULL)
78
1.71k
    {
79
      /* First see whether the information in the ELF header is
80
   valid and it does not ask for too much.  */
81
1.71k
      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
82
1.71k
    || 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.71k
      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.71k
      void *file_shdr = ((char *) elf->map_address
96
1.71k
       + elf->start_offset + ehdr->e_shoff);
97
98
1.71k
      assert ((elf->flags & ELF_F_MALLOCED)
99
1.71k
        || ehdr->e_ident[EI_DATA] != MY_ELFDATA
100
1.71k
        || elf->cmd == ELF_C_READ_MMAP
101
1.71k
        || (! ALLOW_UNALIGNED
102
1.71k
      && ((uintptr_t) file_shdr
103
1.71k
          & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
104
105
      /* Now copy the data and at the same time convert the byte order.  */
106
1.71k
      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
107
382
  {
108
382
    assert ((elf->flags & ELF_F_MALLOCED)
109
382
      || elf->cmd == ELF_C_READ_MMAP
110
382
      || ! ALLOW_UNALIGNED);
111
382
    memcpy (shdr, file_shdr, size);
112
382
  }
113
1.32k
      else
114
1.32k
  {
115
1.32k
    bool copy = ! (ALLOW_UNALIGNED
116
0
       || ((uintptr_t) file_shdr
117
0
           & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1))
118
0
           == 0);
119
1.32k
    if (! copy)
120
1.32k
      notcvt = (ElfW2(LIBELFBITS,Shdr) *)
121
1.32k
        ((char *) elf->map_address
122
1.32k
         + 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.42M
    for (size_t cnt = 0; cnt < shnum; ++cnt)
137
1.42M
      {
138
1.42M
        CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
139
1.42M
        CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
140
1.42M
        CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
141
1.42M
        CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
142
1.42M
        CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
143
1.42M
        CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
144
1.42M
        CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
145
1.42M
        CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
146
1.42M
        CONVERT_TO (shdr[cnt].sh_addralign,
147
1.42M
        notcvt[cnt].sh_addralign);
148
1.42M
        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.42M
        if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
154
44
      && shdr[cnt].sh_link < shnum)
155
34
    elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
156
34
      = cnt;
157
158
        /* Set the own shndx_index field in case it has not yet
159
     been set.  */
160
1.42M
        if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
161
1.42M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
162
1.42M
      = -1;
163
1.42M
      }
164
165
1.32k
    if (copy)
166
0
      free (notcvt);
167
1.32k
  }
168
1.71k
    }
169
2.48k
  else if (likely (elf->fildes != -1))
170
2.48k
    {
171
      /* Read the header.  */
172
2.48k
      ssize_t n = pread_retry (elf->fildes,
173
2.48k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
174
2.48k
             elf->start_offset + ehdr->e_shoff);
175
2.48k
      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.48k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
185
244k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
186
243k
    {
187
243k
      CONVERT (shdr[cnt].sh_name);
188
243k
      CONVERT (shdr[cnt].sh_type);
189
243k
      CONVERT (shdr[cnt].sh_flags);
190
243k
      CONVERT (shdr[cnt].sh_addr);
191
243k
      CONVERT (shdr[cnt].sh_offset);
192
243k
      CONVERT (shdr[cnt].sh_size);
193
243k
      CONVERT (shdr[cnt].sh_link);
194
243k
      CONVERT (shdr[cnt].sh_info);
195
243k
      CONVERT (shdr[cnt].sh_addralign);
196
243k
      CONVERT (shdr[cnt].sh_entsize);
197
243k
    }
198
2.48k
    }
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
1.83M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
215
1.83M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
216
1.83M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
217
218
4.19k
  result = scn->shdr.ELFW(e,LIBELFBITS);
219
4.19k
  assert (result != NULL);
220
221
4.19k
out:
222
4.19k
  return result;
223
4.19k
}
224
225
static bool
226
scn_valid (Elf_Scn *scn)
227
4.16M
{
228
4.16M
  if (scn == NULL)
229
0
    return false;
230
231
4.16M
  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.16M
  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.16M
  return true;
244
4.16M
}
elf32_getshdr.c:scn_valid
Line
Count
Source
227
3.55M
{
228
3.55M
  if (scn == NULL)
229
0
    return false;
230
231
3.55M
  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
3.55M
  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
3.55M
  return true;
244
3.55M
}
elf64_getshdr.c:scn_valid
Line
Count
Source
227
614k
{
228
614k
  if (scn == NULL)
229
0
    return false;
230
231
614k
  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
614k
  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
614k
  return true;
244
614k
}
245
246
ElfW2(LIBELFBITS,Shdr) *
247
internal_function
248
__elfw2(LIBELFBITS,getshdr_rdlock) (Elf_Scn *scn)
249
2.09M
{
250
2.09M
  ElfW2(LIBELFBITS,Shdr) *result;
251
252
2.09M
  if (!scn_valid (scn))
253
0
    return NULL;
254
255
2.09M
  result = scn->shdr.ELFW(e,LIBELFBITS);
256
2.09M
  if (result == NULL)
257
16.4k
    {
258
16.4k
      rwlock_unlock (scn->elf->lock);
259
16.4k
      rwlock_wrlock (scn->elf->lock);
260
16.4k
      result = scn->shdr.ELFW(e,LIBELFBITS);
261
16.4k
      if (result == NULL)
262
16.4k
  result = load_shdr_wrlock (scn);
263
16.4k
    }
264
265
2.09M
  return result;
266
2.09M
}
__elf32_getshdr_rdlock
Line
Count
Source
249
1.78M
{
250
1.78M
  ElfW2(LIBELFBITS,Shdr) *result;
251
252
1.78M
  if (!scn_valid (scn))
253
0
    return NULL;
254
255
1.78M
  result = scn->shdr.ELFW(e,LIBELFBITS);
256
1.78M
  if (result == NULL)
257
12.2k
    {
258
12.2k
      rwlock_unlock (scn->elf->lock);
259
12.2k
      rwlock_wrlock (scn->elf->lock);
260
12.2k
      result = scn->shdr.ELFW(e,LIBELFBITS);
261
12.2k
      if (result == NULL)
262
12.2k
  result = load_shdr_wrlock (scn);
263
12.2k
    }
264
265
1.78M
  return result;
266
1.78M
}
__elf64_getshdr_rdlock
Line
Count
Source
249
309k
{
250
309k
  ElfW2(LIBELFBITS,Shdr) *result;
251
252
309k
  if (!scn_valid (scn))
253
0
    return NULL;
254
255
309k
  result = scn->shdr.ELFW(e,LIBELFBITS);
256
309k
  if (result == NULL)
257
4.19k
    {
258
4.19k
      rwlock_unlock (scn->elf->lock);
259
4.19k
      rwlock_wrlock (scn->elf->lock);
260
4.19k
      result = scn->shdr.ELFW(e,LIBELFBITS);
261
4.19k
      if (result == NULL)
262
4.19k
  result = load_shdr_wrlock (scn);
263
4.19k
    }
264
265
309k
  return result;
266
309k
}
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.07M
{
287
2.07M
  ElfW2(LIBELFBITS,Shdr) *result;
288
289
2.07M
  if (!scn_valid (scn))
290
0
    return NULL;
291
292
2.07M
  rwlock_rdlock (scn->elf->lock);
293
2.07M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
294
2.07M
  rwlock_unlock (scn->elf->lock);
295
296
2.07M
  return result;
297
2.07M
}
elf32_getshdr
Line
Count
Source
286
1.76M
{
287
1.76M
  ElfW2(LIBELFBITS,Shdr) *result;
288
289
1.76M
  if (!scn_valid (scn))
290
0
    return NULL;
291
292
1.76M
  rwlock_rdlock (scn->elf->lock);
293
1.76M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
294
1.76M
  rwlock_unlock (scn->elf->lock);
295
296
1.76M
  return result;
297
1.76M
}
elf64_getshdr
Line
Count
Source
286
305k
{
287
305k
  ElfW2(LIBELFBITS,Shdr) *result;
288
289
305k
  if (!scn_valid (scn))
290
0
    return NULL;
291
292
305k
  rwlock_rdlock (scn->elf->lock);
293
305k
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
294
305k
  rwlock_unlock (scn->elf->lock);
295
296
305k
  return result;
297
305k
}