Coverage Report

Created: 2025-11-11 06:45

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.77M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
1.76M
    {
173
1.76M
      CONVERT (shdr[cnt].sh_name);
174
1.76M
      CONVERT (shdr[cnt].sh_type);
175
1.76M
      CONVERT (shdr[cnt].sh_flags);
176
1.76M
      CONVERT (shdr[cnt].sh_addr);
177
1.76M
      CONVERT (shdr[cnt].sh_offset);
178
1.76M
      CONVERT (shdr[cnt].sh_size);
179
1.76M
      CONVERT (shdr[cnt].sh_link);
180
1.76M
      CONVERT (shdr[cnt].sh_info);
181
1.76M
      CONVERT (shdr[cnt].sh_addralign);
182
1.76M
      CONVERT (shdr[cnt].sh_entsize);
183
1.76M
    }
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.95M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
2.94M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
2.94M
      = &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.92k
{
49
8.92k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
8.92k
  Elf *elf = scn->elf;
53
8.92k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
8.92k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
8.92k
  if (result != NULL)
58
0
    goto out;
59
60
8.92k
  size_t shnum;
61
8.92k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
8.92k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
8.92k
  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.92k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
8.92k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
8.92k
  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.92k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
8.92k
  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.92k
  else if (likely (elf->fildes != -1))
156
8.92k
    {
157
      /* Read the header.  */
158
8.92k
      ssize_t n = pread_retry (elf->fildes,
159
8.92k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
8.92k
             elf->start_offset + ehdr->e_shoff);
161
8.92k
      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.92k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
1.52M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
1.52M
    {
173
1.52M
      CONVERT (shdr[cnt].sh_name);
174
1.52M
      CONVERT (shdr[cnt].sh_type);
175
1.52M
      CONVERT (shdr[cnt].sh_flags);
176
1.52M
      CONVERT (shdr[cnt].sh_addr);
177
1.52M
      CONVERT (shdr[cnt].sh_offset);
178
1.52M
      CONVERT (shdr[cnt].sh_size);
179
1.52M
      CONVERT (shdr[cnt].sh_link);
180
1.52M
      CONVERT (shdr[cnt].sh_info);
181
1.52M
      CONVERT (shdr[cnt].sh_addralign);
182
1.52M
      CONVERT (shdr[cnt].sh_entsize);
183
1.52M
    }
184
8.92k
    }
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.54M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
2.53M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
2.53M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
8.92k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
8.92k
  assert (result != NULL);
206
207
8.92k
out:
208
8.92k
  return result;
209
8.92k
}
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
248k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
247k
    {
173
247k
      CONVERT (shdr[cnt].sh_name);
174
247k
      CONVERT (shdr[cnt].sh_type);
175
247k
      CONVERT (shdr[cnt].sh_flags);
176
247k
      CONVERT (shdr[cnt].sh_addr);
177
247k
      CONVERT (shdr[cnt].sh_offset);
178
247k
      CONVERT (shdr[cnt].sh_size);
179
247k
      CONVERT (shdr[cnt].sh_link);
180
247k
      CONVERT (shdr[cnt].sh_info);
181
247k
      CONVERT (shdr[cnt].sh_addralign);
182
247k
      CONVERT (shdr[cnt].sh_entsize);
183
247k
    }
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
408k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
406k
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
406k
      = &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.68M
{
214
3.68M
  if (scn == NULL)
215
0
    return false;
216
217
3.68M
  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.68M
  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.68M
  return true;
230
3.68M
}
elf32_getshdr.c:scn_valid
Line
Count
Source
213
3.22M
{
214
3.22M
  if (scn == NULL)
215
0
    return false;
216
217
3.22M
  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.22M
  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.22M
  return true;
230
3.22M
}
elf64_getshdr.c:scn_valid
Line
Count
Source
213
461k
{
214
461k
  if (scn == NULL)
215
0
    return false;
216
217
461k
  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
461k
  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
461k
  return true;
230
461k
}
231
232
ElfW2(LIBELFBITS,Shdr) *
233
internal_function
234
__elfw2(LIBELFBITS,getshdr_rdlock) (Elf_Scn *scn)
235
1.84M
{
236
1.84M
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
1.84M
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
1.84M
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
1.84M
  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.84M
  return result;
252
1.84M
}
__elf32_getshdr_rdlock
Line
Count
Source
235
1.61M
{
236
1.61M
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
1.61M
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
1.61M
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
1.61M
  if (result == NULL)
243
8.92k
    {
244
8.92k
      rwlock_unlock (scn->elf->lock);
245
8.92k
      rwlock_wrlock (scn->elf->lock);
246
8.92k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
8.92k
      if (result == NULL)
248
8.92k
  result = load_shdr_wrlock (scn);
249
8.92k
    }
250
251
1.61M
  return result;
252
1.61M
}
__elf64_getshdr_rdlock
Line
Count
Source
235
231k
{
236
231k
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
231k
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
231k
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
231k
  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
231k
  return result;
252
231k
}
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.83M
{
273
1.83M
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
1.83M
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
1.83M
  rwlock_rdlock (scn->elf->lock);
279
1.83M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
1.83M
  rwlock_unlock (scn->elf->lock);
281
282
1.83M
  return result;
283
1.83M
}
elf32_getshdr
Line
Count
Source
272
1.60M
{
273
1.60M
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
1.60M
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
1.60M
  rwlock_rdlock (scn->elf->lock);
279
1.60M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
1.60M
  rwlock_unlock (scn->elf->lock);
281
282
1.60M
  return result;
283
1.60M
}
elf64_getshdr
Line
Count
Source
272
229k
{
273
229k
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
229k
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
229k
  rwlock_rdlock (scn->elf->lock);
279
229k
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
229k
  rwlock_unlock (scn->elf->lock);
281
282
229k
  return result;
283
229k
}