Coverage Report

Created: 2026-10-23 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/libelf/elf_getphdrnum.c
Line
Count
Source
1
/* Return number of program headers in the ELF file.
2
   Copyright (C) 2010, 2014, 2015, 2016 Red Hat, Inc.
3
   This file is part of elfutils.
4
5
   This file is free software; you can redistribute it and/or modify
6
   it under the terms of either
7
8
     * the GNU Lesser General Public License as published by the Free
9
       Software Foundation; either version 3 of the License, or (at
10
       your option) any later version
11
12
   or
13
14
     * the GNU General Public License as published by the Free
15
       Software Foundation; either version 2 of the License, or (at
16
       your option) any later version
17
18
   or both in parallel, as here.
19
20
   elfutils is distributed in the hope that it will be useful, but
21
   WITHOUT ANY WARRANTY; without even the implied warranty of
22
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
   General Public License for more details.
24
25
   You should have received copies of the GNU General Public License and
26
   the GNU Lesser General Public License along with this program.  If
27
   not, see <http://www.gnu.org/licenses/>.  */
28
29
#ifdef HAVE_CONFIG_H
30
# include <config.h>
31
#endif
32
33
#include <assert.h>
34
#include <gelf.h>
35
#include <stddef.h>
36
37
#include "libelfP.h"
38
39
40
int
41
internal_function
42
__elf_getphdrnum_rdlock (Elf *elf, size_t *dst)
43
189M
{
44
189M
 if (unlikely (elf->state.elf64.ehdr == NULL))
45
0
   {
46
     /* Maybe no ELF header was created yet.  */
47
0
     *dst = 0;
48
0
     __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
49
0
     return -1;
50
0
   }
51
52
189M
 *dst = (elf->class == ELFCLASS32
53
189M
   ? elf->state.elf32.ehdr->e_phnum
54
189M
   : elf->state.elf64.ehdr->e_phnum);
55
56
189M
 if (*dst == PN_XNUM)
57
13.4M
   {
58
13.4M
     const Elf_ScnList *const scns = (elf->class == ELFCLASS32
59
13.4M
              ? &elf->state.elf32.scns
60
13.4M
              : &elf->state.elf64.scns);
61
62
     /* If there are no section headers, perhaps this is really just 65536
63
  written without PN_XNUM support.  Either that or it's bad data.  */
64
65
13.4M
     if (elf->class == ELFCLASS32)
66
13.0M
       {
67
13.0M
   if (likely (scns->cnt > 0))
68
8.03M
     {
69
8.03M
       Elf_Scn *scn = &elf->state.elf32.scns.data[0];
70
8.03M
       Elf32_Shdr *shdr = scn->shdr.e32 ?: __elf32_getshdr_rdlock (scn);
71
8.03M
       if (shdr)
72
8.03M
         *dst = shdr->sh_info;
73
8.03M
     }
74
13.0M
       }
75
395k
     else
76
395k
       {
77
395k
   if (likely (scns->cnt > 0))
78
1.23k
     {
79
1.23k
       Elf_Scn *scn = &elf->state.elf64.scns.data[0];
80
1.23k
       Elf64_Shdr *shdr = scn->shdr.e64 ?: __elf64_getshdr_rdlock (scn);
81
1.23k
       if (shdr)
82
1.23k
         *dst = shdr->sh_info;
83
1.23k
     }
84
395k
       }
85
13.4M
   }
86
87
189M
 return 0;
88
189M
}
89
90
int
91
internal_function
92
__elf_getphdrnum_chk_rdlock (Elf *elf, size_t *dst)
93
189M
{
94
189M
  int result = __elf_getphdrnum_rdlock (elf, dst);
95
96
  /* If the phdrs haven't been created or read in yet then do some
97
     sanity checking to make sure phnum and phoff are consistent.  */
98
189M
  if (elf->state.elf.phdr == NULL)
99
40.5k
    {
100
40.5k
      Elf64_Off off = (elf->class == ELFCLASS32
101
40.5k
           ? elf->state.elf32.ehdr->e_phoff
102
40.5k
           : elf->state.elf64.ehdr->e_phoff);
103
40.5k
      if (unlikely (off == 0))
104
13.8k
  {
105
13.8k
    *dst = 0;
106
13.8k
    return result;
107
13.8k
  }
108
109
26.6k
      if (unlikely (off >= elf->maximum_size))
110
1.31k
  {
111
1.31k
    __libelf_seterrno (ELF_E_INVALID_DATA);
112
1.31k
    return -1;
113
1.31k
  }
114
115
      /* Check for too many sections.  */
116
25.3k
      size_t phdr_size = (elf->class == ELFCLASS32
117
25.3k
        ? sizeof (Elf32_Phdr) : sizeof (Elf64_Phdr));
118
25.3k
      if (unlikely (*dst > SIZE_MAX / phdr_size))
119
0
  {
120
0
    __libelf_seterrno (ELF_E_INVALID_DATA);
121
0
    return -1;
122
0
  }
123
124
      /* Truncated file?  Don't return more than can be indexed.  */
125
25.3k
      if (unlikely (elf->maximum_size - off < *dst * phdr_size))
126
1.47k
  *dst = (elf->maximum_size - off) / phdr_size;
127
25.3k
    }
128
129
189M
  return result;
130
189M
}
131
132
int
133
elf_getphdrnum (Elf *elf, size_t *dst)
134
51.9k
{
135
51.9k
  int result;
136
137
51.9k
  if (elf == NULL)
138
0
    return -1;
139
140
51.9k
  if (unlikely (elf->kind != ELF_K_ELF))
141
131
    {
142
131
      __libelf_seterrno (ELF_E_INVALID_HANDLE);
143
131
      return -1;
144
131
    }
145
146
51.8k
  rwlock_rdlock (elf->lock);
147
51.8k
  result = __elf_getphdrnum_chk_rdlock (elf, dst);
148
51.8k
  rwlock_unlock (elf->lock);
149
150
51.8k
  return result;
151
51.9k
}