Coverage Report

Created: 2023-06-07 06:23

/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
19
{
45
19
  ElfW2(LIBELFBITS,Shdr) *shdr = elfw2(LIBELFBITS,getshdr) (scn);
46
19
  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
19
  if ((shdr->sh_flags & SHF_ALLOC) != 0)
52
0
    {
53
0
      __libelf_seterrno (ELF_E_INVALID_SECTION_FLAGS);
54
0
      return NULL;
55
0
    }
56
57
19
  if (shdr->sh_type == SHT_NULL
58
19
      || shdr->sh_type == SHT_NOBITS)
59
0
    {
60
0
      __libelf_seterrno (ELF_E_INVALID_SECTION_TYPE);
61
0
      return NULL;
62
0
    }
63
64
19
  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
19
  Elf_Data *d  = elf_getdata (scn, NULL);
73
19
  if (d == NULL)
74
13
    return NULL;
75
76
6
  if (d->d_size < sizeof (ElfW2(LIBELFBITS,Chdr)) || d->d_buf == NULL)
77
0
    {
78
0
      __libelf_seterrno (ELF_E_INVALID_DATA);
79
0
      return NULL;
80
0
    }
81
82
6
  return (ElfW2(LIBELFBITS,Chdr) *) d->d_buf;
83
6
}
Unexecuted instantiation: elf32_getchdr
elf64_getchdr
Line
Count
Source
44
19
{
45
19
  ElfW2(LIBELFBITS,Shdr) *shdr = elfw2(LIBELFBITS,getshdr) (scn);
46
19
  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
19
  if ((shdr->sh_flags & SHF_ALLOC) != 0)
52
0
    {
53
0
      __libelf_seterrno (ELF_E_INVALID_SECTION_FLAGS);
54
0
      return NULL;
55
0
    }
56
57
19
  if (shdr->sh_type == SHT_NULL
58
19
      || shdr->sh_type == SHT_NOBITS)
59
0
    {
60
0
      __libelf_seterrno (ELF_E_INVALID_SECTION_TYPE);
61
0
      return NULL;
62
0
    }
63
64
19
  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
19
  Elf_Data *d  = elf_getdata (scn, NULL);
73
19
  if (d == NULL)
74
13
    return NULL;
75
76
6
  if (d->d_size < sizeof (ElfW2(LIBELFBITS,Chdr)) || d->d_buf == NULL)
77
0
    {
78
0
      __libelf_seterrno (ELF_E_INVALID_DATA);
79
0
      return NULL;
80
0
    }
81
82
6
  return (ElfW2(LIBELFBITS,Chdr) *) d->d_buf;
83
6
}