Coverage Report

Created: 2026-01-17 06:53

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.5k
{
49
10.5k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
10.5k
  Elf *elf = scn->elf;
53
10.5k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
10.5k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
10.5k
  if (result != NULL)
58
0
    goto out;
59
60
10.5k
  size_t shnum;
61
10.5k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
10.5k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
10.5k
  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.5k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
10.5k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
10.5k
  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.5k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
10.5k
  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.5k
  else if (likely (elf->fildes != -1))
156
10.5k
    {
157
      /* Read the header.  */
158
10.5k
      ssize_t n = pread_retry (elf->fildes,
159
10.5k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
10.5k
             elf->start_offset + ehdr->e_shoff);
161
10.5k
      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.5k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
1.58M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
1.58M
    {
173
1.58M
      CONVERT (shdr[cnt].sh_name);
174
1.58M
      CONVERT (shdr[cnt].sh_type);
175
1.58M
      CONVERT (shdr[cnt].sh_flags);
176
1.58M
      CONVERT (shdr[cnt].sh_addr);
177
1.58M
      CONVERT (shdr[cnt].sh_offset);
178
1.58M
      CONVERT (shdr[cnt].sh_size);
179
1.58M
      CONVERT (shdr[cnt].sh_link);
180
1.58M
      CONVERT (shdr[cnt].sh_info);
181
1.58M
      CONVERT (shdr[cnt].sh_addralign);
182
1.58M
      CONVERT (shdr[cnt].sh_entsize);
183
1.58M
    }
184
10.5k
    }
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.88M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
2.87M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
2.87M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
10.5k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
10.5k
  assert (result != NULL);
206
207
10.5k
out:
208
10.5k
  return result;
209
10.5k
}
elf32_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
8.59k
{
49
8.59k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
8.59k
  Elf *elf = scn->elf;
53
8.59k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
8.59k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
8.59k
  if (result != NULL)
58
0
    goto out;
59
60
8.59k
  size_t shnum;
61
8.59k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
8.59k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
8.59k
  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.59k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
8.59k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
8.59k
  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.59k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
8.59k
  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.59k
  else if (likely (elf->fildes != -1))
156
8.59k
    {
157
      /* Read the header.  */
158
8.59k
      ssize_t n = pread_retry (elf->fildes,
159
8.59k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
8.59k
             elf->start_offset + ehdr->e_shoff);
161
8.59k
      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.59k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
1.33M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
1.32M
    {
173
1.32M
      CONVERT (shdr[cnt].sh_name);
174
1.32M
      CONVERT (shdr[cnt].sh_type);
175
1.32M
      CONVERT (shdr[cnt].sh_flags);
176
1.32M
      CONVERT (shdr[cnt].sh_addr);
177
1.32M
      CONVERT (shdr[cnt].sh_offset);
178
1.32M
      CONVERT (shdr[cnt].sh_size);
179
1.32M
      CONVERT (shdr[cnt].sh_link);
180
1.32M
      CONVERT (shdr[cnt].sh_info);
181
1.32M
      CONVERT (shdr[cnt].sh_addralign);
182
1.32M
      CONVERT (shdr[cnt].sh_entsize);
183
1.32M
    }
184
8.59k
    }
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.42M
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
2.41M
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
2.41M
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
8.59k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
8.59k
  assert (result != NULL);
206
207
8.59k
out:
208
8.59k
  return result;
209
8.59k
}
elf64_getshdr.c:load_shdr_wrlock
Line
Count
Source
48
1.94k
{
49
1.94k
  ElfW2(LIBELFBITS,Shdr) *result;
50
51
  /* Read the section header table.  */
52
1.94k
  Elf *elf = scn->elf;
53
1.94k
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
54
55
  /* Try again, maybe the data is there now.  */
56
1.94k
  result = scn->shdr.ELFW(e,LIBELFBITS);
57
1.94k
  if (result != NULL)
58
0
    goto out;
59
60
1.94k
  size_t shnum;
61
1.94k
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
62
1.94k
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
63
0
    goto out;
64
1.94k
  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
1.94k
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
69
1.94k
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
70
1.94k
  if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
71
0
    {
72
0
      __libelf_seterrno (ELF_E_NOMEM);
73
0
      goto out;
74
0
    }
75
1.94k
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
76
77
1.94k
  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
1.94k
  else if (likely (elf->fildes != -1))
156
1.94k
    {
157
      /* Read the header.  */
158
1.94k
      ssize_t n = pread_retry (elf->fildes,
159
1.94k
             elf->state.ELFW(elf,LIBELFBITS).shdr, size,
160
1.94k
             elf->start_offset + ehdr->e_shoff);
161
1.94k
      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
1.94k
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
171
253k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
172
252k
    {
173
252k
      CONVERT (shdr[cnt].sh_name);
174
252k
      CONVERT (shdr[cnt].sh_type);
175
252k
      CONVERT (shdr[cnt].sh_flags);
176
252k
      CONVERT (shdr[cnt].sh_addr);
177
252k
      CONVERT (shdr[cnt].sh_offset);
178
252k
      CONVERT (shdr[cnt].sh_size);
179
252k
      CONVERT (shdr[cnt].sh_link);
180
252k
      CONVERT (shdr[cnt].sh_info);
181
252k
      CONVERT (shdr[cnt].sh_addralign);
182
252k
      CONVERT (shdr[cnt].sh_entsize);
183
252k
    }
184
1.94k
    }
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
453k
  for (size_t cnt = 0; cnt < shnum; ++cnt)
201
451k
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
202
451k
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
203
204
1.94k
  result = scn->shdr.ELFW(e,LIBELFBITS);
205
1.94k
  assert (result != NULL);
206
207
1.94k
out:
208
1.94k
  return result;
209
1.94k
}
210
211
static bool
212
scn_valid (Elf_Scn *scn)
213
3.60M
{
214
3.60M
  if (scn == NULL)
215
0
    return false;
216
217
3.60M
  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.60M
  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.60M
  return true;
230
3.60M
}
elf32_getshdr.c:scn_valid
Line
Count
Source
213
2.98M
{
214
2.98M
  if (scn == NULL)
215
0
    return false;
216
217
2.98M
  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
2.98M
  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
2.98M
  return true;
230
2.98M
}
elf64_getshdr.c:scn_valid
Line
Count
Source
213
615k
{
214
615k
  if (scn == NULL)
215
0
    return false;
216
217
615k
  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
615k
  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
615k
  return true;
230
615k
}
231
232
ElfW2(LIBELFBITS,Shdr) *
233
internal_function
234
__elfw2(LIBELFBITS,getshdr_rdlock) (Elf_Scn *scn)
235
1.80M
{
236
1.80M
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
1.80M
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
1.80M
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
1.80M
  if (result == NULL)
243
10.5k
    {
244
10.5k
      rwlock_unlock (scn->elf->lock);
245
10.5k
      rwlock_wrlock (scn->elf->lock);
246
10.5k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
10.5k
      if (result == NULL)
248
10.5k
  result = load_shdr_wrlock (scn);
249
10.5k
    }
250
251
1.80M
  return result;
252
1.80M
}
__elf32_getshdr_rdlock
Line
Count
Source
235
1.49M
{
236
1.49M
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
1.49M
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
1.49M
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
1.49M
  if (result == NULL)
243
8.59k
    {
244
8.59k
      rwlock_unlock (scn->elf->lock);
245
8.59k
      rwlock_wrlock (scn->elf->lock);
246
8.59k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
8.59k
      if (result == NULL)
248
8.59k
  result = load_shdr_wrlock (scn);
249
8.59k
    }
250
251
1.49M
  return result;
252
1.49M
}
__elf64_getshdr_rdlock
Line
Count
Source
235
308k
{
236
308k
  ElfW2(LIBELFBITS,Shdr) *result;
237
238
308k
  if (!scn_valid (scn))
239
0
    return NULL;
240
241
308k
  result = scn->shdr.ELFW(e,LIBELFBITS);
242
308k
  if (result == NULL)
243
1.94k
    {
244
1.94k
      rwlock_unlock (scn->elf->lock);
245
1.94k
      rwlock_wrlock (scn->elf->lock);
246
1.94k
      result = scn->shdr.ELFW(e,LIBELFBITS);
247
1.94k
      if (result == NULL)
248
1.94k
  result = load_shdr_wrlock (scn);
249
1.94k
    }
250
251
308k
  return result;
252
308k
}
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.79M
{
273
1.79M
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
1.79M
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
1.79M
  rwlock_rdlock (scn->elf->lock);
279
1.79M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
1.79M
  rwlock_unlock (scn->elf->lock);
281
282
1.79M
  return result;
283
1.79M
}
elf32_getshdr
Line
Count
Source
272
1.48M
{
273
1.48M
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
1.48M
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
1.48M
  rwlock_rdlock (scn->elf->lock);
279
1.48M
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
1.48M
  rwlock_unlock (scn->elf->lock);
281
282
1.48M
  return result;
283
1.48M
}
elf64_getshdr
Line
Count
Source
272
307k
{
273
307k
  ElfW2(LIBELFBITS,Shdr) *result;
274
275
307k
  if (!scn_valid (scn))
276
0
    return NULL;
277
278
307k
  rwlock_rdlock (scn->elf->lock);
279
307k
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
280
307k
  rwlock_unlock (scn->elf->lock);
281
282
307k
  return result;
283
307k
}