Coverage Report

Created: 2025-08-26 06:08

/src/libbpf/elfutils/libelf/elf32_getchdr.c
Line
Count
Source (jump to first uncovered line)
1
/* Return section compression header.
2
   Copyright (C) 2015 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 <libelf.h>
34
#include "libelfP.h"
35
#include "common.h"
36
37
#ifndef LIBELFBITS
38
# define LIBELFBITS 32
39
#endif
40
41
42
ElfW2(LIBELFBITS,Chdr) *
43
elfw2(LIBELFBITS,getchdr) (Elf_Scn *scn)
44
2.56k
{
45
2.56k
  ElfW2(LIBELFBITS,Shdr) *shdr = elfw2(LIBELFBITS,getshdr) (scn);
46
2.56k
  if (shdr == NULL)
47
0
    return NULL;
48
49
  /* Must have SHF_COMPRESSED flag set.  Allocated or no bits sections
50
     can never be compressed.  */
51
2.56k
  if ((shdr->sh_flags & SHF_ALLOC) != 0)
52
205
    {
53
205
      __libelf_seterrno (ELF_E_INVALID_SECTION_FLAGS);
54
205
      return NULL;
55
205
    }
56
57
2.35k
  if (shdr->sh_type == SHT_NULL
58
2.35k
      || shdr->sh_type == SHT_NOBITS)
59
0
    {
60
0
      __libelf_seterrno (ELF_E_INVALID_SECTION_TYPE);
61
0
      return NULL;
62
0
    }
63
64
2.35k
  if ((shdr->sh_flags & SHF_COMPRESSED) == 0)
65
0
    {
66
0
      __libelf_seterrno (ELF_E_NOT_COMPRESSED);
67
0
      return NULL;
68
0
    }
69
70
  /* This makes sure the data is in the correct format, so we don't
71
     need to swap fields. */
72
2.35k
  Elf_Data *d  = elf_getdata (scn, NULL);
73
2.35k
  if (d == NULL)
74
382
    return NULL;
75
76
1.97k
  if (d->d_size < sizeof (ElfW2(LIBELFBITS,Chdr)) || d->d_buf == NULL)
77
458
    {
78
458
      __libelf_seterrno (ELF_E_INVALID_DATA);
79
458
      return NULL;
80
458
    }
81
82
1.51k
  return (ElfW2(LIBELFBITS,Chdr) *) d->d_buf;
83
1.97k
}
Unexecuted instantiation: elf32_getchdr
elf64_getchdr
Line
Count
Source
44
2.56k
{
45
2.56k
  ElfW2(LIBELFBITS,Shdr) *shdr = elfw2(LIBELFBITS,getshdr) (scn);
46
2.56k
  if (shdr == NULL)
47
0
    return NULL;
48
49
  /* Must have SHF_COMPRESSED flag set.  Allocated or no bits sections
50
     can never be compressed.  */
51
2.56k
  if ((shdr->sh_flags & SHF_ALLOC) != 0)
52
205
    {
53
205
      __libelf_seterrno (ELF_E_INVALID_SECTION_FLAGS);
54
205
      return NULL;
55
205
    }
56
57
2.35k
  if (shdr->sh_type == SHT_NULL
58
2.35k
      || shdr->sh_type == SHT_NOBITS)
59
0
    {
60
0
      __libelf_seterrno (ELF_E_INVALID_SECTION_TYPE);
61
0
      return NULL;
62
0
    }
63
64
2.35k
  if ((shdr->sh_flags & SHF_COMPRESSED) == 0)
65
0
    {
66
0
      __libelf_seterrno (ELF_E_NOT_COMPRESSED);
67
0
      return NULL;
68
0
    }
69
70
  /* This makes sure the data is in the correct format, so we don't
71
     need to swap fields. */
72
2.35k
  Elf_Data *d  = elf_getdata (scn, NULL);
73
2.35k
  if (d == NULL)
74
382
    return NULL;
75
76
1.97k
  if (d->d_size < sizeof (ElfW2(LIBELFBITS,Chdr)) || d->d_buf == NULL)
77
458
    {
78
458
      __libelf_seterrno (ELF_E_INVALID_DATA);
79
458
      return NULL;
80
458
    }
81
82
1.51k
  return (ElfW2(LIBELFBITS,Chdr) *) d->d_buf;
83
1.97k
}