Coverage Report

Created: 2025-04-11 06:15

/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.8k
{
49
10.8k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
10.8k
  Elf *elf = scn->elf;
53
10.8k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
10.8k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
10.8k
  if (result != NULL)
58
0
    goto out;
59
60
10.8k
  size_t shnum;
61
10.8k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
10.8k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
10.8k
  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.8k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
10.8k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
10.8k
  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.8k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
10.8k
  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.8k
  else if (likely (elf->fildes != -1))
156
10.8k
    {
157
      /* Read the header.  */
158
10.8k
      ssize_t n = pread_retry (elf->fildes,
159
10.8k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
10.8k
             elf->start_offset + ehdr->e_shoff);
161
10.8k
      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.8k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
1.93M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
1.93M
    {
173
1.93M
      CONVERT (shdr[cnt].sh_name);
174
1.93M
      CONVERT (shdr[cnt].sh_type);
175
1.93M
      CONVERT (shdr[cnt].sh_flags);
176
1.93M
      CONVERT (shdr[cnt].sh_addr);
177
1.93M
      CONVERT (shdr[cnt].sh_offset);
178
1.93M
      CONVERT (shdr[cnt].sh_size);
179
1.93M
      CONVERT (shdr[cnt].sh_link);
180
1.93M
      CONVERT (shdr[cnt].sh_info);
181
1.93M
      CONVERT (shdr[cnt].sh_addralign);
182
1.93M
      CONVERT (shdr[cnt].sh_entsize);
183
1.93M
    }
184
10.8k
    }
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
3.15M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
3.13M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
3.13M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
10.8k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
10.8k
  assert (result != NULL);
206
207
10.8k
out:
208
10.8k
  return result;
209
10.8k
}
elf32_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
8.82k
{
49
8.82k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
8.82k
  Elf *elf = scn->elf;
53
8.82k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
8.82k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
8.82k
  if (result != NULL)
58
0
    goto out;
59
60
8.82k
  size_t shnum;
61
8.82k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
8.82k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
8.82k
  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.82k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
8.82k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
8.82k
  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.82k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
8.82k
  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.82k
  else if (likely (elf->fildes != -1))
156
8.82k
    {
157
      /* Read the header.  */
158
8.82k
      ssize_t n = pread_retry (elf->fildes,
159
8.82k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
8.82k
             elf->start_offset + ehdr->e_shoff);
161
8.82k
      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.82k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
1.65M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
1.64M
    {
173
1.64M
      CONVERT (shdr[cnt].sh_name);
174
1.64M
      CONVERT (shdr[cnt].sh_type);
175
1.64M
      CONVERT (shdr[cnt].sh_flags);
176
1.64M
      CONVERT (shdr[cnt].sh_addr);
177
1.64M
      CONVERT (shdr[cnt].sh_offset);
178
1.64M
      CONVERT (shdr[cnt].sh_size);
179
1.64M
      CONVERT (shdr[cnt].sh_link);
180
1.64M
      CONVERT (shdr[cnt].sh_info);
181
1.64M
      CONVERT (shdr[cnt].sh_addralign);
182
1.64M
      CONVERT (shdr[cnt].sh_entsize);
183
1.64M
    }
184
8.82k
    }
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.77M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
2.76M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
2.76M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
8.82k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
8.82k
  assert (result != NULL);
206
207
8.82k
out:
208
8.82k
  return result;
209
8.82k
}
elf64_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
2.00k
{
49
2.00k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
2.00k
  Elf *elf = scn->elf;
53
2.00k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
2.00k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
2.00k
  if (result != NULL)
58
0
    goto out;
59
60
2.00k
  size_t shnum;
61
2.00k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
2.00k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
2.00k
  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.00k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
2.00k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
2.00k
  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.00k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
2.00k
  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.00k
  else if (likely (elf->fildes != -1))
156
2.00k
    {
157
      /* Read the header.  */
158
2.00k
      ssize_t n = pread_retry (elf->fildes,
159
2.00k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
2.00k
             elf->start_offset + ehdr->e_shoff);
161
2.00k
      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.00k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
283k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
282k
    {
173
282k
      CONVERT (shdr[cnt].sh_name);
174
282k
      CONVERT (shdr[cnt].sh_type);
175
282k
      CONVERT (shdr[cnt].sh_flags);
176
282k
      CONVERT (shdr[cnt].sh_addr);
177
282k
      CONVERT (shdr[cnt].sh_offset);
178
282k
      CONVERT (shdr[cnt].sh_size);
179
282k
      CONVERT (shdr[cnt].sh_link);
180
282k
      CONVERT (shdr[cnt].sh_info);
181
282k
      CONVERT (shdr[cnt].sh_addralign);
182
282k
      CONVERT (shdr[cnt].sh_entsize);
183
282k
    }
184
2.00k
    }
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
378k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
376k
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
376k
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
2.00k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
2.00k
  assert (result != NULL);
206
207
2.00k
out:
208
2.00k
  return result;
209
2.00k
}
210
211
static bool
212
scn_valid (Elf_Scn *scn)
213
3.90M
{
214
3.90M
  if (scn == NULL)
215
0
    return false;
216
217
3.90M
  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.90M
  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.90M
  return true;
230
3.90M
}
elf32_getshdr.c:scn_valid
Line
Count
Source
213
3.56M
{
214
3.56M
  if (scn == NULL)
215
0
    return false;
216
217
3.56M
  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.56M
  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.56M
  return true;
230
3.56M
}
elf64_getshdr.c:scn_valid
Line
Count
Source
213
339k
{
214
339k
  if (scn == NULL)
215
0
    return false;
216
217
339k
  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
339k
  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
339k
  return true;
230
339k
}
231
232
ElfW2(LIBELFBITS,Shdr) *
233
internal_function
234
__elfw2(LIBELFBITS,getshdr_rdlock) (Elf_Scn *scn)
235
1.95M
{
236
1.95M
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
1.95M
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
1.95M
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
1.95M
  if (result == NULL)
243
10.8k
    {
244
10.8k
      rwlock_unlock (scn->elf->lock);
245
10.8k
      rwlock_wrlock (scn->elf->lock);
246
10.8k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
10.8k
      if (result == NULL)
248
10.8k
  result = load_shdr_wrlock (scn);
249
10.8k
    }
250
251
1.95M
  return result;
252
1.95M
}
__elf32_getshdr_rdlock
Line
Count
Source
235
1.78M
{
236
1.78M
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
1.78M
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
1.78M
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
1.78M
  if (result == NULL)
243
8.82k
    {
244
8.82k
      rwlock_unlock (scn->elf->lock);
245
8.82k
      rwlock_wrlock (scn->elf->lock);
246
8.82k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
8.82k
      if (result == NULL)
248
8.82k
  result = load_shdr_wrlock (scn);
249
8.82k
    }
250
251
1.78M
  return result;
252
1.78M
}
__elf64_getshdr_rdlock
Line
Count
Source
235
170k
{
236
170k
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
170k
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
170k
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
170k
  if (result == NULL)
243
2.00k
    {
244
2.00k
      rwlock_unlock (scn->elf->lock);
245
2.00k
      rwlock_wrlock (scn->elf->lock);
246
2.00k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
2.00k
      if (result == NULL)
248
2.00k
  result = load_shdr_wrlock (scn);
249
2.00k
    }
250
251
170k
  return result;
252
170k
}
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.94M
{
273
1.94M
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
1.94M
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
1.94M
  rwlock_rdlock (scn->elf->lock);
279
1.94M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
1.94M
  rwlock_unlock (scn->elf->lock);
281
282
1.94M
  return result;
283
1.94M
}
elf32_getshdr
Line
Count
Source
272
1.77M
{
273
1.77M
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
1.77M
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
1.77M
  rwlock_rdlock (scn->elf->lock);
279
1.77M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
1.77M
  rwlock_unlock (scn->elf->lock);
281
282
1.77M
  return result;
283
1.77M
}
elf64_getshdr
Line
Count
Source
272
168k
{
273
168k
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
168k
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
168k
  rwlock_rdlock (scn->elf->lock);
279
168k
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
168k
  rwlock_unlock (scn->elf->lock);
281
282
168k
  return result;
283
168k
}