Coverage Report

Created: 2026-02-14 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/libelf/common.h
Line
Count
Source
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
236k
{
42
  /* First test for an archive.  */
43
236k
  if (len >= SARMAG && memcmp (buf, ARMAG, SARMAG) == 0)
44
157k
    return ELF_K_AR;
45
46
  /* Next try ELF files.  */
47
78.7k
  if (len >= EI_NIDENT && memcmp (buf, ELFMAG, SELFMAG) == 0)
48
71.0k
    {
49
      /* Could be an ELF file.  */
50
71.0k
      int eclass = (int) ((unsigned char *) buf)[EI_CLASS];
51
71.0k
      int data = (int) ((unsigned char *) buf)[EI_DATA];
52
71.0k
      int version = (int) ((unsigned char *) buf)[EI_VERSION];
53
54
71.0k
      if (eclass > ELFCLASSNONE && eclass < ELFCLASSNUM
55
70.5k
    && data > ELFDATANONE && data < ELFDATANUM
56
70.0k
    && version == EV_CURRENT)
57
68.5k
  return ELF_K_ELF;
58
71.0k
    }
59
60
  /* We do not know this file type.  */
61
10.2k
  return ELF_K_NONE;
62
78.7k
}
Unexecuted instantiation: bpf_disasm.c:determine_kind
Unexecuted instantiation: fde.c:determine_kind
Unexecuted instantiation: cfi.c:determine_kind
Unexecuted instantiation: dwarf_getcfi_elf.c:determine_kind
Unexecuted instantiation: dwarf_next_cfi.c:determine_kind
Unexecuted instantiation: cie.c:determine_kind
elf_begin.c:determine_kind
Line
Count
Source
41
236k
{
42
  /* First test for an archive.  */
43
236k
  if (len >= SARMAG && memcmp (buf, ARMAG, SARMAG) == 0)
44
157k
    return ELF_K_AR;
45
46
  /* Next try ELF files.  */
47
78.7k
  if (len >= EI_NIDENT && memcmp (buf, ELFMAG, SELFMAG) == 0)
48
71.0k
    {
49
      /* Could be an ELF file.  */
50
71.0k
      int eclass = (int) ((unsigned char *) buf)[EI_CLASS];
51
71.0k
      int data = (int) ((unsigned char *) buf)[EI_DATA];
52
71.0k
      int version = (int) ((unsigned char *) buf)[EI_VERSION];
53
54
71.0k
      if (eclass > ELFCLASSNONE && eclass < ELFCLASSNUM
55
70.5k
    && data > ELFDATANONE && data < ELFDATANUM
56
70.0k
    && version == EV_CURRENT)
57
68.5k
  return ELF_K_ELF;
58
71.0k
    }
59
60
  /* We do not know this file type.  */
61
10.2k
  return ELF_K_NONE;
62
78.7k
}
Unexecuted instantiation: gelf_xlate.c:determine_kind
Unexecuted instantiation: elf_readall.c:determine_kind
Unexecuted instantiation: elf_getdata.c:determine_kind
Unexecuted instantiation: elf_getdata_rawchunk.c:determine_kind
Unexecuted instantiation: elf_getshdrstrndx.c:determine_kind
Unexecuted instantiation: elf_compress.c:determine_kind
Unexecuted instantiation: elf_compress_gnu.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: elf64_getshdr.c:determine_kind
Unexecuted instantiation: elf32_getchdr.c:determine_kind
Unexecuted instantiation: elf64_getchdr.c:determine_kind
Unexecuted instantiation: dwfl_segment_report_module.c:determine_kind
Unexecuted instantiation: elf_clone.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
235k
{
71
235k
  Elf *result = calloc (1, sizeof (Elf) + extra);
72
235k
  if (result == NULL)
73
0
    __libelf_seterrno (ELF_E_NOMEM);
74
235k
  else
75
235k
    {
76
235k
      result->kind = kind;
77
235k
      result->ref_count = 1;
78
235k
      result->cmd = cmd;
79
235k
      result->fildes = fildes;
80
235k
      result->start_offset = offset;
81
235k
      result->maximum_size = maxsize;
82
235k
      result->map_address = map_address;
83
235k
      result->parent = parent;
84
85
235k
      rwlock_init (result->lock);
86
235k
    }
87
88
235k
  return result;
89
235k
}
Unexecuted instantiation: bpf_disasm.c:allocate_elf
Unexecuted instantiation: fde.c:allocate_elf
Unexecuted instantiation: cfi.c:allocate_elf
Unexecuted instantiation: dwarf_getcfi_elf.c:allocate_elf
Unexecuted instantiation: dwarf_next_cfi.c:allocate_elf
Unexecuted instantiation: cie.c:allocate_elf
elf_begin.c:allocate_elf
Line
Count
Source
70
235k
{
71
235k
  Elf *result = calloc (1, sizeof (Elf) + extra);
72
235k
  if (result == NULL)
73
0
    __libelf_seterrno (ELF_E_NOMEM);
74
235k
  else
75
235k
    {
76
235k
      result->kind = kind;
77
235k
      result->ref_count = 1;
78
235k
      result->cmd = cmd;
79
235k
      result->fildes = fildes;
80
235k
      result->start_offset = offset;
81
235k
      result->maximum_size = maxsize;
82
235k
      result->map_address = map_address;
83
235k
      result->parent = parent;
84
85
235k
      rwlock_init (result->lock);
86
235k
    }
87
88
235k
  return result;
89
235k
}
Unexecuted instantiation: gelf_xlate.c:allocate_elf
Unexecuted instantiation: elf_readall.c:allocate_elf
Unexecuted instantiation: elf_getdata.c:allocate_elf
Unexecuted instantiation: elf_getdata_rawchunk.c:allocate_elf
Unexecuted instantiation: elf_getshdrstrndx.c:allocate_elf
Unexecuted instantiation: elf_compress.c:allocate_elf
Unexecuted instantiation: elf_compress_gnu.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: elf64_getshdr.c:allocate_elf
Unexecuted instantiation: elf32_getchdr.c:allocate_elf
Unexecuted instantiation: elf64_getchdr.c:allocate_elf
Unexecuted instantiation: dwfl_segment_report_module.c:allocate_elf
Unexecuted instantiation: elf_clone.c:allocate_elf
90
91
92
/* Caller must hold a lock for ELF. If there are children then a lock
93
   will be acquired for each of them (recursively).  */
94
static void
95
__attribute__ ((unused))
96
libelf_acquire_all_children (Elf *elf)
97
6.07k
{
98
6.07k
  if (elf->kind == ELF_K_AR)
99
1
    {
100
1
      Elf *child = elf->state.ar.children;
101
102
1
      while (child != NULL)
103
0
  {
104
0
    rwlock_wrlock (child->lock);
105
106
0
    if (child->ref_count != 0)
107
0
      libelf_acquire_all_children (child);
108
109
0
    child = child->next;
110
0
  }
111
1
    }
112
6.07k
}
Unexecuted instantiation: bpf_disasm.c:libelf_acquire_all_children
Unexecuted instantiation: fde.c:libelf_acquire_all_children
Unexecuted instantiation: cfi.c:libelf_acquire_all_children
Unexecuted instantiation: dwarf_getcfi_elf.c:libelf_acquire_all_children
Unexecuted instantiation: dwarf_next_cfi.c:libelf_acquire_all_children
Unexecuted instantiation: cie.c:libelf_acquire_all_children
Unexecuted instantiation: elf_begin.c:libelf_acquire_all_children
Unexecuted instantiation: gelf_xlate.c:libelf_acquire_all_children
elf_readall.c:libelf_acquire_all_children
Line
Count
Source
97
6.07k
{
98
6.07k
  if (elf->kind == ELF_K_AR)
99
1
    {
100
1
      Elf *child = elf->state.ar.children;
101
102
1
      while (child != NULL)
103
0
  {
104
0
    rwlock_wrlock (child->lock);
105
106
0
    if (child->ref_count != 0)
107
0
      libelf_acquire_all_children (child);
108
109
0
    child = child->next;
110
0
  }
111
1
    }
112
6.07k
}
Unexecuted instantiation: elf_getdata.c:libelf_acquire_all_children
Unexecuted instantiation: elf_getdata_rawchunk.c:libelf_acquire_all_children
Unexecuted instantiation: elf_getshdrstrndx.c:libelf_acquire_all_children
Unexecuted instantiation: elf_compress.c:libelf_acquire_all_children
Unexecuted instantiation: elf_compress_gnu.c:libelf_acquire_all_children
Unexecuted instantiation: elf32_getphdr.c:libelf_acquire_all_children
Unexecuted instantiation: elf64_getphdr.c:libelf_acquire_all_children
Unexecuted instantiation: elf32_getshdr.c:libelf_acquire_all_children
Unexecuted instantiation: elf64_getshdr.c:libelf_acquire_all_children
Unexecuted instantiation: elf32_getchdr.c:libelf_acquire_all_children
Unexecuted instantiation: elf64_getchdr.c:libelf_acquire_all_children
Unexecuted instantiation: dwfl_segment_report_module.c:libelf_acquire_all_children
Unexecuted instantiation: elf_clone.c:libelf_acquire_all_children
113
114
115
/* Caller must hold a lock for ELF. If there are children then a lock
116
   will be released for each of them (recursively).  */
117
static void
118
__attribute__ ((unused))
119
libelf_release_all_children (Elf *elf)
120
6.07k
{
121
6.07k
  if (elf->kind == ELF_K_AR)
122
1
    {
123
1
      Elf *child = elf->state.ar.children;
124
125
1
      while (child != NULL)
126
0
  {
127
0
    if (child->ref_count != 0)
128
0
      libelf_release_all_children (child);
129
130
0
    rwlock_unlock (child->lock);
131
0
    child = child->next;
132
0
  }
133
1
    }
134
6.07k
}
Unexecuted instantiation: bpf_disasm.c:libelf_release_all_children
Unexecuted instantiation: fde.c:libelf_release_all_children
Unexecuted instantiation: cfi.c:libelf_release_all_children
Unexecuted instantiation: dwarf_getcfi_elf.c:libelf_release_all_children
Unexecuted instantiation: dwarf_next_cfi.c:libelf_release_all_children
Unexecuted instantiation: cie.c:libelf_release_all_children
Unexecuted instantiation: elf_begin.c:libelf_release_all_children
Unexecuted instantiation: gelf_xlate.c:libelf_release_all_children
elf_readall.c:libelf_release_all_children
Line
Count
Source
120
6.07k
{
121
6.07k
  if (elf->kind == ELF_K_AR)
122
1
    {
123
1
      Elf *child = elf->state.ar.children;
124
125
1
      while (child != NULL)
126
0
  {
127
0
    if (child->ref_count != 0)
128
0
      libelf_release_all_children (child);
129
130
0
    rwlock_unlock (child->lock);
131
0
    child = child->next;
132
0
  }
133
1
    }
134
6.07k
}
Unexecuted instantiation: elf_getdata.c:libelf_release_all_children
Unexecuted instantiation: elf_getdata_rawchunk.c:libelf_release_all_children
Unexecuted instantiation: elf_getshdrstrndx.c:libelf_release_all_children
Unexecuted instantiation: elf_compress.c:libelf_release_all_children
Unexecuted instantiation: elf_compress_gnu.c:libelf_release_all_children
Unexecuted instantiation: elf32_getphdr.c:libelf_release_all_children
Unexecuted instantiation: elf64_getphdr.c:libelf_release_all_children
Unexecuted instantiation: elf32_getshdr.c:libelf_release_all_children
Unexecuted instantiation: elf64_getshdr.c:libelf_release_all_children
Unexecuted instantiation: elf32_getchdr.c:libelf_release_all_children
Unexecuted instantiation: elf64_getchdr.c:libelf_release_all_children
Unexecuted instantiation: dwfl_segment_report_module.c:libelf_release_all_children
Unexecuted instantiation: elf_clone.c:libelf_release_all_children
135
136
137
/* Macro to convert endianness in place.  It determines the function it
138
   has to use itself.  */
139
#define CONVERT(Var) \
140
17.8M
  (Var) = (sizeof (Var) == 1                  \
141
17.8M
     ? (unsigned char) (Var)                \
142
17.8M
     : (sizeof (Var) == 2                  \
143
17.8M
        ? bswap_16 (Var)                  \
144
17.8M
        : (sizeof (Var) == 4                \
145
17.5M
     ? bswap_32 (Var)               \
146
17.5M
     : bswap_64 (Var))))
147
148
#define CONVERT_TO(Dst, Var) \
149
36.8M
  (Dst) = (sizeof (Var) == 1                  \
150
36.8M
     ? (unsigned char) (Var)                \
151
36.8M
     : (sizeof (Var) == 2                  \
152
36.8M
        ? bswap_16 (Var)                  \
153
36.8M
        : (sizeof (Var) == 4                \
154
36.8M
     ? bswap_32 (Var)               \
155
36.8M
     : bswap_64 (Var))))
156
157
158
#if BYTE_ORDER == LITTLE_ENDIAN
159
464k
# define MY_ELFDATA ELFDATA2LSB
160
#else
161
# define MY_ELFDATA ELFDATA2MSB
162
#endif
163
164
#endif  /* common.h */