Coverage Report

Created: 2026-01-10 06:23

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
10.9k
{
49
10.9k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
10.9k
  Elf *elf = scn->elf;
53
10.9k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
10.9k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
10.9k
  if (result != NULL)
58
0
    goto out;
59
60
10.9k
  size_t shnum;
61
10.9k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
10.9k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
10.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
10.9k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
10.9k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
10.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
10.9k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
10.9k
  if (elf->map_address != NULL)
78
0
    {
79
      /* First see whether the information in the ELF header is
80
   valid and it does not ask for too much.  */
81
0
      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
82
0
    || 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
0
      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
0
      void *file_shdr = ((char *) elf->map_address
96
0
       + elf->start_offset + ehdr->e_shoff);
97
98
0
      assert ((elf->flags & ELF_F_MALLOCED)
99
0
        || ehdr->e_ident[EI_DATA] != MY_ELFDATA
100
0
        || elf->cmd == ELF_C_READ_MMAP
101
0
        || (! ALLOW_UNALIGNED
102
0
      && ((uintptr_t) file_shdr
103
0
          & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
104
105
      /* Now copy the data and at the same time convert the byte order.  */
106
0
      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
107
0
  {
108
0
    assert ((elf->flags & ELF_F_MALLOCED)
109
0
      || elf->cmd == ELF_C_READ_MMAP
110
0
      || ! ALLOW_UNALIGNED);
111
0
    memcpy (shdr, file_shdr, size);
112
0
  }
113
0
      else
114
0
  {
115
0
    bool copy = ! (ALLOW_UNALIGNED
116
0
       || ((uintptr_t) file_shdr
117
0
           & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1))
118
0
           == 0);
119
0
    if (! copy)
120
0
      notcvt = (ElfW2(LIBELFBITS,Shdr) *)
121
0
        ((char *) elf->map_address
122
0
         + 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 free_and_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
0
    for (size_t cnt = 0; cnt < shnum; ++cnt)
137
0
      {
138
0
        CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
139
0
        CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
140
0
        CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
141
0
        CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
142
0
        CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
143
0
        CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
144
0
        CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
145
0
        CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
146
0
        CONVERT_TO (shdr[cnt].sh_addralign,
147
0
        notcvt[cnt].sh_addralign);
148
0
        CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
149
0
      }
150
151
0
    if (copy)
152
0
      free (notcvt);
153
0
  }
154
0
    }
155
10.9k
  else if (likely (elf->fildes != -1))
156
10.9k
    {
157
      /* Read the header.  */
158
10.9k
      ssize_t n = pread_retry (elf->fildes,
159
10.9k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
10.9k
             elf->start_offset + ehdr->e_shoff);
161
10.9k
      if (unlikely ((size_t) n != size))
162
0
  {
163
    /* Severe problems.  We cannot read the data.  */
164
0
    __libelf_seterrno (ELF_E_READ_ERROR);
165
0
    goto free_and_out;
166
0
  }
167
168
      /* If the byte order of the file is not the same as the one
169
   of the host convert the data now.  */
170
10.9k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
1.66M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
1.65M
    {
173
1.65M
      CONVERT (shdr[cnt].sh_name);
174
1.65M
      CONVERT (shdr[cnt].sh_type);
175
1.65M
      CONVERT (shdr[cnt].sh_flags);
176
1.65M
      CONVERT (shdr[cnt].sh_addr);
177
1.65M
      CONVERT (shdr[cnt].sh_offset);
178
1.65M
      CONVERT (shdr[cnt].sh_size);
179
1.65M
      CONVERT (shdr[cnt].sh_link);
180
1.65M
      CONVERT (shdr[cnt].sh_info);
181
1.65M
      CONVERT (shdr[cnt].sh_addralign);
182
1.65M
      CONVERT (shdr[cnt].sh_entsize);
183
1.65M
    }
184
10.9k
    }
185
0
  else
186
0
    {
187
      /* The file descriptor was already enabled and not all data was
188
   read.  Undo the allocation.  */
189
0
      __libelf_seterrno (ELF_E_FD_DISABLED);
190
191
0
    free_and_out:
192
0
      free (shdr);
193
0
      elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
194
0
      elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
195
196
0
      goto out;
197
0
    }
198
199
  /* Set the pointers in the `scn's.  */
200
2.96M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
2.95M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
2.95M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
10.9k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
10.9k
  assert (result != NULL);
206
207
10.9k
out:
208
10.9k
  return result;
209
10.9k
}
elf32_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
8.86k
{
49
8.86k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
8.86k
  Elf *elf = scn->elf;
53
8.86k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
8.86k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
8.86k
  if (result != NULL)
58
0
    goto out;
59
60
8.86k
  size_t shnum;
61
8.86k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
8.86k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
8.86k
  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
8.86k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
8.86k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
8.86k
  if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
71
0
    {
72
0
      __libelf_seterrno (ELF_E_NOMEM);
73
0
      goto out;
74
0
    }
75
8.86k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
8.86k
  if (elf->map_address != NULL)
78
0
    {
79
      /* First see whether the information in the ELF header is
80
   valid and it does not ask for too much.  */
81
0
      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
82
0
    || 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
0
      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
0
      void *file_shdr = ((char *) elf->map_address
96
0
       + elf->start_offset + ehdr->e_shoff);
97
98
0
      assert ((elf->flags & ELF_F_MALLOCED)
99
0
        || ehdr->e_ident[EI_DATA] != MY_ELFDATA
100
0
        || elf->cmd == ELF_C_READ_MMAP
101
0
        || (! ALLOW_UNALIGNED
102
0
      && ((uintptr_t) file_shdr
103
0
          & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
104
105
      /* Now copy the data and at the same time convert the byte order.  */
106
0
      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
107
0
  {
108
0
    assert ((elf->flags & ELF_F_MALLOCED)
109
0
      || elf->cmd == ELF_C_READ_MMAP
110
0
      || ! ALLOW_UNALIGNED);
111
0
    memcpy (shdr, file_shdr, size);
112
0
  }
113
0
      else
114
0
  {
115
0
    bool copy = ! (ALLOW_UNALIGNED
116
0
       || ((uintptr_t) file_shdr
117
0
           & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1))
118
0
           == 0);
119
0
    if (! copy)
120
0
      notcvt = (ElfW2(LIBELFBITS,Shdr) *)
121
0
        ((char *) elf->map_address
122
0
         + 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 free_and_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
0
    for (size_t cnt = 0; cnt < shnum; ++cnt)
137
0
      {
138
0
        CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
139
0
        CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
140
0
        CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
141
0
        CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
142
0
        CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
143
0
        CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
144
0
        CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
145
0
        CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
146
0
        CONVERT_TO (shdr[cnt].sh_addralign,
147
0
        notcvt[cnt].sh_addralign);
148
0
        CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
149
0
      }
150
151
0
    if (copy)
152
0
      free (notcvt);
153
0
  }
154
0
    }
155
8.86k
  else if (likely (elf->fildes != -1))
156
8.86k
    {
157
      /* Read the header.  */
158
8.86k
      ssize_t n = pread_retry (elf->fildes,
159
8.86k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
8.86k
             elf->start_offset + ehdr->e_shoff);
161
8.86k
      if (unlikely ((size_t) n != size))
162
0
  {
163
    /* Severe problems.  We cannot read the data.  */
164
0
    __libelf_seterrno (ELF_E_READ_ERROR);
165
0
    goto free_and_out;
166
0
  }
167
168
      /* If the byte order of the file is not the same as the one
169
   of the host convert the data now.  */
170
8.86k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
1.39M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
1.39M
    {
173
1.39M
      CONVERT (shdr[cnt].sh_name);
174
1.39M
      CONVERT (shdr[cnt].sh_type);
175
1.39M
      CONVERT (shdr[cnt].sh_flags);
176
1.39M
      CONVERT (shdr[cnt].sh_addr);
177
1.39M
      CONVERT (shdr[cnt].sh_offset);
178
1.39M
      CONVERT (shdr[cnt].sh_size);
179
1.39M
      CONVERT (shdr[cnt].sh_link);
180
1.39M
      CONVERT (shdr[cnt].sh_info);
181
1.39M
      CONVERT (shdr[cnt].sh_addralign);
182
1.39M
      CONVERT (shdr[cnt].sh_entsize);
183
1.39M
    }
184
8.86k
    }
185
0
  else
186
0
    {
187
      /* The file descriptor was already enabled and not all data was
188
   read.  Undo the allocation.  */
189
0
      __libelf_seterrno (ELF_E_FD_DISABLED);
190
191
0
    free_and_out:
192
0
      free (shdr);
193
0
      elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
194
0
      elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
195
196
0
      goto out;
197
0
    }
198
199
  /* Set the pointers in the `scn's.  */
200
2.51M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
2.50M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
2.50M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
8.86k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
8.86k
  assert (result != NULL);
206
207
8.86k
out:
208
8.86k
  return result;
209
8.86k
}
elf64_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
2.06k
{
49
2.06k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
2.06k
  Elf *elf = scn->elf;
53
2.06k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
2.06k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
2.06k
  if (result != NULL)
58
0
    goto out;
59
60
2.06k
  size_t shnum;
61
2.06k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
2.06k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
2.06k
  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
2.06k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
2.06k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
2.06k
  if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
71
0
    {
72
0
      __libelf_seterrno (ELF_E_NOMEM);
73
0
      goto out;
74
0
    }
75
2.06k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
2.06k
  if (elf->map_address != NULL)
78
0
    {
79
      /* First see whether the information in the ELF header is
80
   valid and it does not ask for too much.  */
81
0
      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
82
0
    || 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
0
      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
0
      void *file_shdr = ((char *) elf->map_address
96
0
       + elf->start_offset + ehdr->e_shoff);
97
98
0
      assert ((elf->flags & ELF_F_MALLOCED)
99
0
        || ehdr->e_ident[EI_DATA] != MY_ELFDATA
100
0
        || elf->cmd == ELF_C_READ_MMAP
101
0
        || (! ALLOW_UNALIGNED
102
0
      && ((uintptr_t) file_shdr
103
0
          & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
104
105
      /* Now copy the data and at the same time convert the byte order.  */
106
0
      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
107
0
  {
108
0
    assert ((elf->flags & ELF_F_MALLOCED)
109
0
      || elf->cmd == ELF_C_READ_MMAP
110
0
      || ! ALLOW_UNALIGNED);
111
0
    memcpy (shdr, file_shdr, size);
112
0
  }
113
0
      else
114
0
  {
115
0
    bool copy = ! (ALLOW_UNALIGNED
116
0
       || ((uintptr_t) file_shdr
117
0
           & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1))
118
0
           == 0);
119
0
    if (! copy)
120
0
      notcvt = (ElfW2(LIBELFBITS,Shdr) *)
121
0
        ((char *) elf->map_address
122
0
         + 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 free_and_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
0
    for (size_t cnt = 0; cnt < shnum; ++cnt)
137
0
      {
138
0
        CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
139
0
        CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
140
0
        CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
141
0
        CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
142
0
        CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
143
0
        CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
144
0
        CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
145
0
        CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
146
0
        CONVERT_TO (shdr[cnt].sh_addralign,
147
0
        notcvt[cnt].sh_addralign);
148
0
        CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
149
0
      }
150
151
0
    if (copy)
152
0
      free (notcvt);
153
0
  }
154
0
    }
155
2.06k
  else if (likely (elf->fildes != -1))
156
2.06k
    {
157
      /* Read the header.  */
158
2.06k
      ssize_t n = pread_retry (elf->fildes,
159
2.06k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
2.06k
             elf->start_offset + ehdr->e_shoff);
161
2.06k
      if (unlikely ((size_t) n != size))
162
0
  {
163
    /* Severe problems.  We cannot read the data.  */
164
0
    __libelf_seterrno (ELF_E_READ_ERROR);
165
0
    goto free_and_out;
166
0
  }
167
168
      /* If the byte order of the file is not the same as the one
169
   of the host convert the data now.  */
170
2.06k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
264k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
263k
    {
173
263k
      CONVERT (shdr[cnt].sh_name);
174
263k
      CONVERT (shdr[cnt].sh_type);
175
263k
      CONVERT (shdr[cnt].sh_flags);
176
263k
      CONVERT (shdr[cnt].sh_addr);
177
263k
      CONVERT (shdr[cnt].sh_offset);
178
263k
      CONVERT (shdr[cnt].sh_size);
179
263k
      CONVERT (shdr[cnt].sh_link);
180
263k
      CONVERT (shdr[cnt].sh_info);
181
263k
      CONVERT (shdr[cnt].sh_addralign);
182
263k
      CONVERT (shdr[cnt].sh_entsize);
183
263k
    }
184
2.06k
    }
185
0
  else
186
0
    {
187
      /* The file descriptor was already enabled and not all data was
188
   read.  Undo the allocation.  */
189
0
      __libelf_seterrno (ELF_E_FD_DISABLED);
190
191
0
    free_and_out:
192
0
      free (shdr);
193
0
      elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
194
0
      elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
195
196
0
      goto out;
197
0
    }
198
199
  /* Set the pointers in the `scn's.  */
200
454k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
452k
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
452k
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
2.06k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
2.06k
  assert (result != NULL);
206
207
2.06k
out:
208
2.06k
  return result;
209
2.06k
}
210
211
static bool
212
scn_valid (Elf_Scn *scn)
213
3.74M
{
214
3.74M
  if (scn == NULL)
215
0
    return false;
216
217
3.74M
  if (unlikely (scn->elf->state.elf.ehdr == NULL))
218
0
    {
219
0
      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
220
0
      return false;
221
0
    }
222
223
3.74M
  if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
224
0
    {
225
0
      __libelf_seterrno (ELF_E_INVALID_CLASS);
226
0
      return false;
227
0
    }
228
229
3.74M
  return true;
230
3.74M
}
elf32_getshdr.c:scn_valid
Line
Count
Source
213
3.11M
{
214
3.11M
  if (scn == NULL)
215
0
    return false;
216
217
3.11M
  if (unlikely (scn->elf->state.elf.ehdr == NULL))
218
0
    {
219
0
      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
220
0
      return false;
221
0
    }
222
223
3.11M
  if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
224
0
    {
225
0
      __libelf_seterrno (ELF_E_INVALID_CLASS);
226
0
      return false;
227
0
    }
228
229
3.11M
  return true;
230
3.11M
}
elf64_getshdr.c:scn_valid
Line
Count
Source
213
633k
{
214
633k
  if (scn == NULL)
215
0
    return false;
216
217
633k
  if (unlikely (scn->elf->state.elf.ehdr == NULL))
218
0
    {
219
0
      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
220
0
      return false;
221
0
    }
222
223
633k
  if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
224
0
    {
225
0
      __libelf_seterrno (ELF_E_INVALID_CLASS);
226
0
      return false;
227
0
    }
228
229
633k
  return true;
230
633k
}
231
232
ElfW2(LIBELFBITS,Shdr) *
233
internal_function
234
__elfw2(LIBELFBITS,getshdr_rdlock) (Elf_Scn *scn)
235
1.87M
{
236
1.87M
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
1.87M
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
1.87M
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
1.87M
  if (result == NULL)
243
10.9k
    {
244
10.9k
      rwlock_unlock (scn->elf->lock);
245
10.9k
      rwlock_wrlock (scn->elf->lock);
246
10.9k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
10.9k
      if (result == NULL)
248
10.9k
  result = load_shdr_wrlock (scn);
249
10.9k
    }
250
251
1.87M
  return result;
252
1.87M
}
__elf32_getshdr_rdlock
Line
Count
Source
235
1.56M
{
236
1.56M
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
1.56M
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
1.56M
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
1.56M
  if (result == NULL)
243
8.86k
    {
244
8.86k
      rwlock_unlock (scn->elf->lock);
245
8.86k
      rwlock_wrlock (scn->elf->lock);
246
8.86k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
8.86k
      if (result == NULL)
248
8.86k
  result = load_shdr_wrlock (scn);
249
8.86k
    }
250
251
1.56M
  return result;
252
1.56M
}
__elf64_getshdr_rdlock
Line
Count
Source
235
317k
{
236
317k
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
317k
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
317k
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
317k
  if (result == NULL)
243
2.06k
    {
244
2.06k
      rwlock_unlock (scn->elf->lock);
245
2.06k
      rwlock_wrlock (scn->elf->lock);
246
2.06k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
2.06k
      if (result == NULL)
248
2.06k
  result = load_shdr_wrlock (scn);
249
2.06k
    }
250
251
317k
  return result;
252
317k
}
253
254
ElfW2(LIBELFBITS,Shdr) *
255
internal_function
256
__elfw2(LIBELFBITS,getshdr_wrlock) (Elf_Scn *scn)
257
0
{
258
0
  ElfW2(LIBELFBITS,Shdr) *result;
259
260
0
  if (!scn_valid (scn))
261
0
    return NULL;
262
263
0
  result = scn->shdr.ELFW(e,LIBELFBITS);
264
0
  if (result == NULL)
265
0
    result = load_shdr_wrlock (scn);
266
267
0
  return result;
268
0
}
Unexecuted instantiation: __elf32_getshdr_wrlock
Unexecuted instantiation: __elf64_getshdr_wrlock
269
270
ElfW2(LIBELFBITS,Shdr) *
271
elfw2(LIBELFBITS,getshdr) (Elf_Scn *scn)
272
1.86M
{
273
1.86M
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
1.86M
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
1.86M
  rwlock_rdlock (scn->elf->lock);
279
1.86M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
1.86M
  rwlock_unlock (scn->elf->lock);
281
282
1.86M
  return result;
283
1.86M
}
elf32_getshdr
Line
Count
Source
272
1.55M
{
273
1.55M
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
1.55M
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
1.55M
  rwlock_rdlock (scn->elf->lock);
279
1.55M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
1.55M
  rwlock_unlock (scn->elf->lock);
281
282
1.55M
  return result;
283
1.55M
}
elf64_getshdr
Line
Count
Source
272
315k
{
273
315k
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
315k
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
315k
  rwlock_rdlock (scn->elf->lock);
279
315k
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
315k
  rwlock_unlock (scn->elf->lock);
281
282
315k
  return result;
283
315k
}