Coverage Report

Created: 2025-08-29 06:11

/src/elfutils/libelf/elf32_getshdr.c
Line
Count
Source (jump to first uncovered line)
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.4k
{
49
10.4k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
10.4k
  Elf *elf = scn->elf;
53
10.4k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
10.4k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
10.4k
  if (result != NULL)
58
0
    goto out;
59
60
10.4k
  size_t shnum;
61
10.4k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
10.4k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
10.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
10.4k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
10.4k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
10.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
10.4k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
10.4k
  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.4k
  else if (likely (elf->fildes != -1))
156
10.4k
    {
157
      /* Read the header.  */
158
10.4k
      ssize_t n = pread_retry (elf->fildes,
159
10.4k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
10.4k
             elf->start_offset + ehdr->e_shoff);
161
10.4k
      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.4k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
1.70M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
1.70M
    {
173
1.70M
      CONVERT (shdr[cnt].sh_name);
174
1.70M
      CONVERT (shdr[cnt].sh_type);
175
1.70M
      CONVERT (shdr[cnt].sh_flags);
176
1.70M
      CONVERT (shdr[cnt].sh_addr);
177
1.70M
      CONVERT (shdr[cnt].sh_offset);
178
1.70M
      CONVERT (shdr[cnt].sh_size);
179
1.70M
      CONVERT (shdr[cnt].sh_link);
180
1.70M
      CONVERT (shdr[cnt].sh_info);
181
1.70M
      CONVERT (shdr[cnt].sh_addralign);
182
1.70M
      CONVERT (shdr[cnt].sh_entsize);
183
1.70M
    }
184
10.4k
    }
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.73M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
2.72M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
2.72M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
10.4k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
10.4k
  assert (result != NULL);
206
207
10.4k
out:
208
10.4k
  return result;
209
10.4k
}
elf32_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
8.36k
{
49
8.36k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
8.36k
  Elf *elf = scn->elf;
53
8.36k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
8.36k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
8.36k
  if (result != NULL)
58
0
    goto out;
59
60
8.36k
  size_t shnum;
61
8.36k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
8.36k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
8.36k
  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.36k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
8.36k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
8.36k
  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.36k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
8.36k
  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.36k
  else if (likely (elf->fildes != -1))
156
8.36k
    {
157
      /* Read the header.  */
158
8.36k
      ssize_t n = pread_retry (elf->fildes,
159
8.36k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
8.36k
             elf->start_offset + ehdr->e_shoff);
161
8.36k
      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.36k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
1.51M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
1.50M
    {
173
1.50M
      CONVERT (shdr[cnt].sh_name);
174
1.50M
      CONVERT (shdr[cnt].sh_type);
175
1.50M
      CONVERT (shdr[cnt].sh_flags);
176
1.50M
      CONVERT (shdr[cnt].sh_addr);
177
1.50M
      CONVERT (shdr[cnt].sh_offset);
178
1.50M
      CONVERT (shdr[cnt].sh_size);
179
1.50M
      CONVERT (shdr[cnt].sh_link);
180
1.50M
      CONVERT (shdr[cnt].sh_info);
181
1.50M
      CONVERT (shdr[cnt].sh_addralign);
182
1.50M
      CONVERT (shdr[cnt].sh_entsize);
183
1.50M
    }
184
8.36k
    }
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.44M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
2.43M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
2.43M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
8.36k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
8.36k
  assert (result != NULL);
206
207
8.36k
out:
208
8.36k
  return result;
209
8.36k
}
elf64_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
2.05k
{
49
2.05k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
2.05k
  Elf *elf = scn->elf;
53
2.05k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
2.05k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
2.05k
  if (result != NULL)
58
0
    goto out;
59
60
2.05k
  size_t shnum;
61
2.05k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
2.05k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
2.05k
  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.05k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
2.05k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
2.05k
  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.05k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
2.05k
  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.05k
  else if (likely (elf->fildes != -1))
156
2.05k
    {
157
      /* Read the header.  */
158
2.05k
      ssize_t n = pread_retry (elf->fildes,
159
2.05k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
2.05k
             elf->start_offset + ehdr->e_shoff);
161
2.05k
      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.05k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
196k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
195k
    {
173
195k
      CONVERT (shdr[cnt].sh_name);
174
195k
      CONVERT (shdr[cnt].sh_type);
175
195k
      CONVERT (shdr[cnt].sh_flags);
176
195k
      CONVERT (shdr[cnt].sh_addr);
177
195k
      CONVERT (shdr[cnt].sh_offset);
178
195k
      CONVERT (shdr[cnt].sh_size);
179
195k
      CONVERT (shdr[cnt].sh_link);
180
195k
      CONVERT (shdr[cnt].sh_info);
181
195k
      CONVERT (shdr[cnt].sh_addralign);
182
195k
      CONVERT (shdr[cnt].sh_entsize);
183
195k
    }
184
2.05k
    }
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
291k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
289k
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
289k
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
2.05k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
2.05k
  assert (result != NULL);
206
207
2.05k
out:
208
2.05k
  return result;
209
2.05k
}
210
211
static bool
212
scn_valid (Elf_Scn *scn)
213
3.57M
{
214
3.57M
  if (scn == NULL)
215
0
    return false;
216
217
3.57M
  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.57M
  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.57M
  return true;
230
3.57M
}
elf32_getshdr.c:scn_valid
Line
Count
Source
213
3.28M
{
214
3.28M
  if (scn == NULL)
215
0
    return false;
216
217
3.28M
  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.28M
  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.28M
  return true;
230
3.28M
}
elf64_getshdr.c:scn_valid
Line
Count
Source
213
294k
{
214
294k
  if (scn == NULL)
215
0
    return false;
216
217
294k
  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
294k
  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
294k
  return true;
230
294k
}
231
232
ElfW2(LIBELFBITS,Shdr) *
233
internal_function
234
__elfw2(LIBELFBITS,getshdr_rdlock) (Elf_Scn *scn)
235
1.79M
{
236
1.79M
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
1.79M
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
1.79M
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
1.79M
  if (result == NULL)
243
10.4k
    {
244
10.4k
      rwlock_unlock (scn->elf->lock);
245
10.4k
      rwlock_wrlock (scn->elf->lock);
246
10.4k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
10.4k
      if (result == NULL)
248
10.4k
  result = load_shdr_wrlock (scn);
249
10.4k
    }
250
251
1.79M
  return result;
252
1.79M
}
__elf32_getshdr_rdlock
Line
Count
Source
235
1.64M
{
236
1.64M
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
1.64M
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
1.64M
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
1.64M
  if (result == NULL)
243
8.36k
    {
244
8.36k
      rwlock_unlock (scn->elf->lock);
245
8.36k
      rwlock_wrlock (scn->elf->lock);
246
8.36k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
8.36k
      if (result == NULL)
248
8.36k
  result = load_shdr_wrlock (scn);
249
8.36k
    }
250
251
1.64M
  return result;
252
1.64M
}
__elf64_getshdr_rdlock
Line
Count
Source
235
148k
{
236
148k
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
148k
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
148k
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
148k
  if (result == NULL)
243
2.05k
    {
244
2.05k
      rwlock_unlock (scn->elf->lock);
245
2.05k
      rwlock_wrlock (scn->elf->lock);
246
2.05k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
2.05k
      if (result == NULL)
248
2.05k
  result = load_shdr_wrlock (scn);
249
2.05k
    }
250
251
148k
  return result;
252
148k
}
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.78M
{
273
1.78M
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
1.78M
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
1.78M
  rwlock_rdlock (scn->elf->lock);
279
1.78M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
1.78M
  rwlock_unlock (scn->elf->lock);
281
282
1.78M
  return result;
283
1.78M
}
elf32_getshdr
Line
Count
Source
272
1.63M
{
273
1.63M
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
1.63M
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
1.63M
  rwlock_rdlock (scn->elf->lock);
279
1.63M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
1.63M
  rwlock_unlock (scn->elf->lock);
281
282
1.63M
  return result;
283
1.63M
}
elf64_getshdr
Line
Count
Source
272
145k
{
273
145k
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
145k
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
145k
  rwlock_rdlock (scn->elf->lock);
279
145k
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
145k
  rwlock_unlock (scn->elf->lock);
281
282
145k
  return result;
283
145k
}