Coverage Report

Created: 2025-07-18 06:45

/src/libbpf/elfutils/libelf/common.h
Line
Count
Source (jump to first uncovered line)
1
/* Common definitions for handling files in memory or only on disk.
2
   Copyright (C) 1998, 1999, 2000, 2002, 2005, 2008 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
#ifndef _COMMON_H
31
#define _COMMON_H       1
32
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "libelfP.h"
37
38
static inline Elf_Kind
39
__attribute__ ((unused))
40
determine_kind (void *buf, size_t len)
41
11.5k
{
42
  /* First test for an archive.  */
43
11.5k
  if (len >= SARMAG && memcmp (buf, ARMAG, SARMAG) == 0)
44
2
    return ELF_K_AR;
45
46
  /* Next try ELF files.  */
47
11.5k
  if (len >= EI_NIDENT && memcmp (buf, ELFMAG, SELFMAG) == 0)
48
11.4k
    {
49
      /* Could be an ELF file.  */
50
11.4k
      int eclass = (int) ((unsigned char *) buf)[EI_CLASS];
51
11.4k
      int data = (int) ((unsigned char *) buf)[EI_DATA];
52
11.4k
      int version = (int) ((unsigned char *) buf)[EI_VERSION];
53
54
11.4k
      if (eclass > ELFCLASSNONE && eclass < ELFCLASSNUM
55
11.4k
    && data > ELFDATANONE && data < ELFDATANUM
56
11.4k
    && version == EV_CURRENT)
57
11.4k
  return ELF_K_ELF;
58
11.4k
    }
59
60
  /* We do not know this file type.  */
61
118
  return ELF_K_NONE;
62
11.5k
}
elf_begin.c:determine_kind
Line
Count
Source
41
11.5k
{
42
  /* First test for an archive.  */
43
11.5k
  if (len >= SARMAG && memcmp (buf, ARMAG, SARMAG) == 0)
44
2
    return ELF_K_AR;
45
46
  /* Next try ELF files.  */
47
11.5k
  if (len >= EI_NIDENT && memcmp (buf, ELFMAG, SELFMAG) == 0)
48
11.4k
    {
49
      /* Could be an ELF file.  */
50
11.4k
      int eclass = (int) ((unsigned char *) buf)[EI_CLASS];
51
11.4k
      int data = (int) ((unsigned char *) buf)[EI_DATA];
52
11.4k
      int version = (int) ((unsigned char *) buf)[EI_VERSION];
53
54
11.4k
      if (eclass > ELFCLASSNONE && eclass < ELFCLASSNUM
55
11.4k
    && data > ELFDATANONE && data < ELFDATANUM
56
11.4k
    && version == EV_CURRENT)
57
11.4k
  return ELF_K_ELF;
58
11.4k
    }
59
60
  /* We do not know this file type.  */
61
118
  return ELF_K_NONE;
62
11.5k
}
Unexecuted instantiation: elf64_getshdr.c:determine_kind
Unexecuted instantiation: elf_getdata.c:determine_kind
Unexecuted instantiation: elf_getshdrstrndx.c:determine_kind
Unexecuted instantiation: elf_compress.c:determine_kind
Unexecuted instantiation: gelf_xlate.c:determine_kind
Unexecuted instantiation: elf32_getphdr.c:determine_kind
Unexecuted instantiation: elf64_getphdr.c:determine_kind
Unexecuted instantiation: elf32_getshdr.c:determine_kind
Unexecuted instantiation: elf32_getchdr.c:determine_kind
Unexecuted instantiation: elf64_getchdr.c:determine_kind
63
64
65
/* Allocate an Elf descriptor and fill in the generic information.  */
66
static inline Elf *
67
__attribute__ ((unused))
68
allocate_elf (int fildes, void *map_address, int64_t offset, size_t maxsize,
69
              Elf_Cmd cmd, Elf *parent, Elf_Kind kind, size_t extra)
70
11.4k
{
71
11.4k
  Elf *result = calloc (1, sizeof (Elf) + extra);
72
11.4k
  if (result == NULL)
73
0
    __libelf_seterrno (ELF_E_NOMEM);
74
11.4k
  else
75
11.4k
    {
76
11.4k
      result->kind = kind;
77
11.4k
      result->ref_count = 1;
78
11.4k
      result->cmd = cmd;
79
11.4k
      result->fildes = fildes;
80
11.4k
      result->start_offset = offset;
81
11.4k
      result->maximum_size = maxsize;
82
11.4k
      result->map_address = map_address;
83
11.4k
      result->parent = parent;
84
85
11.4k
      rwlock_init (result->lock);
86
11.4k
    }
87
88
11.4k
  return result;
89
11.4k
}
elf_begin.c:allocate_elf
Line
Count
Source
70
11.4k
{
71
11.4k
  Elf *result = calloc (1, sizeof (Elf) + extra);
72
11.4k
  if (result == NULL)
73
0
    __libelf_seterrno (ELF_E_NOMEM);
74
11.4k
  else
75
11.4k
    {
76
11.4k
      result->kind = kind;
77
11.4k
      result->ref_count = 1;
78
11.4k
      result->cmd = cmd;
79
11.4k
      result->fildes = fildes;
80
11.4k
      result->start_offset = offset;
81
11.4k
      result->maximum_size = maxsize;
82
11.4k
      result->map_address = map_address;
83
11.4k
      result->parent = parent;
84
85
11.4k
      rwlock_init (result->lock);
86
11.4k
    }
87
88
11.4k
  return result;
89
11.4k
}
Unexecuted instantiation: elf64_getshdr.c:allocate_elf
Unexecuted instantiation: elf_getdata.c:allocate_elf
Unexecuted instantiation: elf_getshdrstrndx.c:allocate_elf
Unexecuted instantiation: elf_compress.c:allocate_elf
Unexecuted instantiation: gelf_xlate.c:allocate_elf
Unexecuted instantiation: elf32_getphdr.c:allocate_elf
Unexecuted instantiation: elf64_getphdr.c:allocate_elf
Unexecuted instantiation: elf32_getshdr.c:allocate_elf
Unexecuted instantiation: elf32_getchdr.c:allocate_elf
Unexecuted instantiation: elf64_getchdr.c:allocate_elf
90
91
92
/* Acquire lock for the descriptor and all children.  */
93
static void
94
__attribute__ ((unused))
95
libelf_acquire_all (Elf *elf)
96
0
{
97
0
  rwlock_wrlock (elf->lock);
98
0
99
0
  if (elf->kind == ELF_K_AR)
100
0
    {
101
0
      Elf *child = elf->state.ar.children;
102
0
103
0
      while (child != NULL)
104
0
  {
105
0
    if (child->ref_count != 0)
106
0
      libelf_acquire_all (child);
107
0
    child = child->next;
108
0
  }
109
0
    }
110
0
}
Unexecuted instantiation: elf_begin.c:libelf_acquire_all
Unexecuted instantiation: elf64_getshdr.c:libelf_acquire_all
Unexecuted instantiation: elf_getdata.c:libelf_acquire_all
Unexecuted instantiation: elf_getshdrstrndx.c:libelf_acquire_all
Unexecuted instantiation: elf_compress.c:libelf_acquire_all
Unexecuted instantiation: gelf_xlate.c:libelf_acquire_all
Unexecuted instantiation: elf32_getphdr.c:libelf_acquire_all
Unexecuted instantiation: elf64_getphdr.c:libelf_acquire_all
Unexecuted instantiation: elf32_getshdr.c:libelf_acquire_all
Unexecuted instantiation: elf32_getchdr.c:libelf_acquire_all
Unexecuted instantiation: elf64_getchdr.c:libelf_acquire_all
111
112
/* Release own lock and those of the children.  */
113
static void
114
__attribute__ ((unused))
115
libelf_release_all (Elf *elf)
116
0
{
117
0
  if (elf->kind == ELF_K_AR)
118
0
    {
119
0
      Elf *child = elf->state.ar.children;
120
0
121
0
      while (child != NULL)
122
0
  {
123
0
    if (child->ref_count != 0)
124
0
      libelf_release_all (child);
125
0
    child = child->next;
126
0
  }
127
0
    }
128
0
129
0
  rwlock_unlock (elf->lock);
130
0
}
Unexecuted instantiation: elf_begin.c:libelf_release_all
Unexecuted instantiation: elf64_getshdr.c:libelf_release_all
Unexecuted instantiation: elf_getdata.c:libelf_release_all
Unexecuted instantiation: elf_getshdrstrndx.c:libelf_release_all
Unexecuted instantiation: elf_compress.c:libelf_release_all
Unexecuted instantiation: gelf_xlate.c:libelf_release_all
Unexecuted instantiation: elf32_getphdr.c:libelf_release_all
Unexecuted instantiation: elf64_getphdr.c:libelf_release_all
Unexecuted instantiation: elf32_getshdr.c:libelf_release_all
Unexecuted instantiation: elf32_getchdr.c:libelf_release_all
Unexecuted instantiation: elf64_getchdr.c:libelf_release_all
131
132
133
/* Macro to convert endianness in place.  It determines the function it
134
   has to use itself.  */
135
#define CONVERT(Var) \
136
33.2k
  (Var) = (sizeof (Var) == 1                  \
137
33.2k
     ? (unsigned char) (Var)                \
138
33.2k
     : (sizeof (Var) == 2                  \
139
33.2k
        ? bswap_16 (Var)                  \
140
33.2k
        : (sizeof (Var) == 4                \
141
13.4k
     ? bswap_32 (Var)               \
142
13.4k
     : bswap_64 (Var))))
143
144
#define CONVERT_TO(Dst, Var) \
145
620k
  (Dst) = (sizeof (Var) == 1                  \
146
620k
     ? (unsigned char) (Var)                \
147
620k
     : (sizeof (Var) == 2                  \
148
620k
        ? bswap_16 (Var)                  \
149
620k
        : (sizeof (Var) == 4                \
150
620k
     ? bswap_32 (Var)               \
151
620k
     : bswap_64 (Var))))
152
153
154
#if BYTE_ORDER == LITTLE_ENDIAN
155
51.8k
# define MY_ELFDATA ELFDATA2LSB
156
#else
157
# define MY_ELFDATA ELFDATA2MSB
158
#endif
159
160
#endif  /* common.h */