Coverage Report

Created: 2026-11-22 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/lib/system.h
Line
Count
Source
1
/* Declarations for common convenience functions.
2
   Copyright (C) 2006-2011 Red Hat, Inc.
3
   Copyright (C) 2022 Mark J. Wielaard <mark@klomp.org>
4
   This file is part of elfutils.
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 LIB_SYSTEM_H
31
#define LIB_SYSTEM_H  1
32
33
#include <config.h>
34
35
#include <errno.h>
36
#include <stddef.h>
37
#include <stdint.h>
38
#include <string.h>
39
#include <stdarg.h>
40
#include <stdlib.h>
41
42
/* System dependent headers */
43
#include <byteswap.h>
44
#include <endian.h>
45
#include <sys/mman.h>
46
#include <sys/param.h>
47
#include <unistd.h>
48
49
#if defined(HAVE_ERROR_H)
50
#include <error.h>
51
#elif defined(HAVE_ERR_H)
52
extern int error_message_count;
53
void error(int status, int errnum, const char *format, ...);
54
#else
55
#error "err.h or error.h must be available"
56
#endif
57
58
/* error (EXIT_FAILURE, ...) should be noreturn but on some systems it
59
   isn't.  This may cause warnings about code that should not be reachable.
60
   So have an explicit error_exit wrapper that is noreturn (because it
61
   calls exit explicitly).  */
62
#define error_exit(errnum,...) do { \
63
    error (EXIT_FAILURE,errnum,__VA_ARGS__); \
64
    exit (EXIT_FAILURE); \
65
  } while (0)
66
67
#if BYTE_ORDER == LITTLE_ENDIAN
68
# define LE32(n)  (n)
69
# define LE64(n)  (n)
70
# define BE32(n)  bswap_32 (n)
71
# define BE64(n)  bswap_64 (n)
72
#elif BYTE_ORDER == BIG_ENDIAN
73
# define BE32(n)  (n)
74
# define BE64(n)  (n)
75
# define LE32(n)  bswap_32 (n)
76
# define LE64(n)  bswap_64 (n)
77
#else
78
# error "Unknown byte order"
79
#endif
80
81
#ifndef MAX
82
#define MAX(m, n) ((m) < (n) ? (n) : (m))
83
#endif
84
85
#ifndef MIN
86
#define MIN(m, n) ((m) < (n) ? (m) : (n))
87
#endif
88
89
#if !HAVE_DECL_POWEROF2
90
#define powerof2(x) (((x) & ((x) - 1)) == 0)
91
#endif
92
93
#if !HAVE_DECL_MEMPCPY
94
#define mempcpy(dest, src, n) \
95
    ((void *) ((char *) memcpy (dest, src, n) + (size_t) n))
96
#endif
97
98
#if !HAVE_DECL_REALLOCARRAY
99
static inline void *
100
reallocarray (void *ptr, size_t nmemb, size_t size)
101
{
102
  if (size > 0 && nmemb > SIZE_MAX / size)
103
    {
104
      errno = ENOMEM;
105
      return NULL;
106
    }
107
  return realloc (ptr, nmemb * size);
108
}
109
#endif
110
111
/* Return TRUE if the start of STR matches PREFIX, FALSE otherwise.  */
112
113
static inline int
114
startswith (const char *str, const char *prefix)
115
0
{
116
0
  return strncmp (str, prefix, strlen (prefix)) == 0;
117
0
}
Unexecuted instantiation: fuzz-libelf.c:startswith
Unexecuted instantiation: elf_version.c:startswith
Unexecuted instantiation: elf_error.c:startswith
Unexecuted instantiation: elf_begin.c:startswith
Unexecuted instantiation: elf_end.c:startswith
Unexecuted instantiation: elf_rawfile.c:startswith
Unexecuted instantiation: elf_readall.c:startswith
Unexecuted instantiation: elf_nextscn.c:startswith
Unexecuted instantiation: gelf_getshdr.c:startswith
Unexecuted instantiation: elf_strptr.c:startswith
Unexecuted instantiation: elf_getdata.c:startswith
Unexecuted instantiation: elf_getshdrstrndx.c:startswith
Unexecuted instantiation: elf_compress.c:startswith
Unexecuted instantiation: elf_compress_gnu.c:startswith
Unexecuted instantiation: gelf_fsize.c:startswith
Unexecuted instantiation: gelf_xlate.c:startswith
Unexecuted instantiation: gelf_getehdr.c:startswith
Unexecuted instantiation: elf32_getshdr.c:startswith
Unexecuted instantiation: elf64_getshdr.c:startswith
Unexecuted instantiation: gelf_xlatetof.c:startswith
Unexecuted instantiation: elf_getshdrnum.c:startswith
Unexecuted instantiation: gelf_getchdr.c:startswith
Unexecuted instantiation: elf32_xlatetof.c:startswith
Unexecuted instantiation: elf64_xlatetof.c:startswith
Unexecuted instantiation: elf32_getchdr.c:startswith
Unexecuted instantiation: elf64_getchdr.c:startswith
118
119
/* A special gettext function we use if the strings are too short.  */
120
#define sgettext(Str) \
121
  ({ const char *__res = strrchr (_(Str), '|');           \
122
     __res ? __res + 1 : Str; })
123
124
#define gettext_noop(Str) Str
125
126
#ifndef TEMP_FAILURE_RETRY
127
#define TEMP_FAILURE_RETRY(expression) \
128
  ({ ssize_t __res; \
129
     do \
130
       __res = expression; \
131
     while (__res == -1 && errno == EINTR); \
132
     __res; })
133
#endif
134
135
#ifndef ACCESSPERMS
136
#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
137
#endif
138
139
#ifndef ALLPERMS
140
#define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
141
#endif
142
143
#ifndef DEFFILEMODE
144
#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)/* 0666 */
145
#endif
146
147
static inline ssize_t __attribute__ ((unused))
148
pwrite_retry (int fd, const void *buf, size_t len, off_t off)
149
0
{
150
0
  ssize_t recvd = 0;
151
0
152
0
  do
153
0
    {
154
0
      ssize_t ret = TEMP_FAILURE_RETRY (pwrite (fd, ((char *)buf) + recvd, len - recvd,
155
0
            off + recvd));
156
0
      if (ret <= 0)
157
0
  return ret < 0 ? ret : recvd;
158
0
159
0
      recvd += ret;
160
0
    }
161
0
  while ((size_t) recvd < len);
162
0
163
0
  return recvd;
164
0
}
Unexecuted instantiation: fuzz-libelf.c:pwrite_retry
Unexecuted instantiation: elf_version.c:pwrite_retry
Unexecuted instantiation: elf_error.c:pwrite_retry
Unexecuted instantiation: elf_begin.c:pwrite_retry
Unexecuted instantiation: elf_end.c:pwrite_retry
Unexecuted instantiation: elf_rawfile.c:pwrite_retry
Unexecuted instantiation: elf_readall.c:pwrite_retry
Unexecuted instantiation: elf_nextscn.c:pwrite_retry
Unexecuted instantiation: gelf_getshdr.c:pwrite_retry
Unexecuted instantiation: elf_strptr.c:pwrite_retry
Unexecuted instantiation: elf_getdata.c:pwrite_retry
Unexecuted instantiation: elf_getshdrstrndx.c:pwrite_retry
Unexecuted instantiation: elf_compress.c:pwrite_retry
Unexecuted instantiation: elf_compress_gnu.c:pwrite_retry
Unexecuted instantiation: gelf_fsize.c:pwrite_retry
Unexecuted instantiation: gelf_xlate.c:pwrite_retry
Unexecuted instantiation: gelf_getehdr.c:pwrite_retry
Unexecuted instantiation: elf32_getshdr.c:pwrite_retry
Unexecuted instantiation: elf64_getshdr.c:pwrite_retry
Unexecuted instantiation: gelf_xlatetof.c:pwrite_retry
Unexecuted instantiation: elf_getshdrnum.c:pwrite_retry
Unexecuted instantiation: gelf_getchdr.c:pwrite_retry
Unexecuted instantiation: elf32_xlatetof.c:pwrite_retry
Unexecuted instantiation: elf64_xlatetof.c:pwrite_retry
Unexecuted instantiation: elf32_getchdr.c:pwrite_retry
Unexecuted instantiation: elf64_getchdr.c:pwrite_retry
165
166
static inline ssize_t __attribute__ ((unused))
167
write_retry (int fd, const void *buf, size_t len)
168
6.60k
{
169
6.60k
  ssize_t recvd = 0;
170
171
6.60k
  do
172
6.60k
    {
173
6.60k
      ssize_t ret = TEMP_FAILURE_RETRY (write (fd, ((char *)buf) + recvd, len - recvd));
174
6.60k
      if (ret <= 0)
175
0
  return ret < 0 ? ret : recvd;
176
177
6.60k
      recvd += ret;
178
6.60k
    }
179
6.60k
  while ((size_t) recvd < len);
180
181
6.60k
  return recvd;
182
6.60k
}
fuzz-libelf.c:write_retry
Line
Count
Source
168
6.60k
{
169
6.60k
  ssize_t recvd = 0;
170
171
6.60k
  do
172
6.60k
    {
173
6.60k
      ssize_t ret = TEMP_FAILURE_RETRY (write (fd, ((char *)buf) + recvd, len - recvd));
174
6.60k
      if (ret <= 0)
175
0
  return ret < 0 ? ret : recvd;
176
177
6.60k
      recvd += ret;
178
6.60k
    }
179
6.60k
  while ((size_t) recvd < len);
180
181
6.60k
  return recvd;
182
6.60k
}
Unexecuted instantiation: elf_version.c:write_retry
Unexecuted instantiation: elf_error.c:write_retry
Unexecuted instantiation: elf_begin.c:write_retry
Unexecuted instantiation: elf_end.c:write_retry
Unexecuted instantiation: elf_rawfile.c:write_retry
Unexecuted instantiation: elf_readall.c:write_retry
Unexecuted instantiation: elf_nextscn.c:write_retry
Unexecuted instantiation: gelf_getshdr.c:write_retry
Unexecuted instantiation: elf_strptr.c:write_retry
Unexecuted instantiation: elf_getdata.c:write_retry
Unexecuted instantiation: elf_getshdrstrndx.c:write_retry
Unexecuted instantiation: elf_compress.c:write_retry
Unexecuted instantiation: elf_compress_gnu.c:write_retry
Unexecuted instantiation: gelf_fsize.c:write_retry
Unexecuted instantiation: gelf_xlate.c:write_retry
Unexecuted instantiation: gelf_getehdr.c:write_retry
Unexecuted instantiation: elf32_getshdr.c:write_retry
Unexecuted instantiation: elf64_getshdr.c:write_retry
Unexecuted instantiation: gelf_xlatetof.c:write_retry
Unexecuted instantiation: elf_getshdrnum.c:write_retry
Unexecuted instantiation: gelf_getchdr.c:write_retry
Unexecuted instantiation: elf32_xlatetof.c:write_retry
Unexecuted instantiation: elf64_xlatetof.c:write_retry
Unexecuted instantiation: elf32_getchdr.c:write_retry
Unexecuted instantiation: elf64_getchdr.c:write_retry
183
184
static inline ssize_t __attribute__ ((unused))
185
pread_retry (int fd, void *buf, size_t len, off_t off)
186
143k
{
187
143k
  ssize_t recvd = 0;
188
189
143k
  do
190
143k
    {
191
143k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
192
143k
                 off + recvd));
193
143k
      if (ret <= 0)
194
13.2k
  return ret < 0 ? ret : recvd;
195
196
129k
      recvd += ret;
197
129k
    }
198
143k
  while ((size_t) recvd < len);
199
200
129k
  return recvd;
201
143k
}
Unexecuted instantiation: fuzz-libelf.c:pread_retry
Unexecuted instantiation: elf_version.c:pread_retry
Unexecuted instantiation: elf_error.c:pread_retry
elf_begin.c:pread_retry
Line
Count
Source
186
33.8k
{
187
33.8k
  ssize_t recvd = 0;
188
189
33.8k
  do
190
33.8k
    {
191
33.8k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
192
33.8k
                 off + recvd));
193
33.8k
      if (ret <= 0)
194
13.2k
  return ret < 0 ? ret : recvd;
195
196
20.6k
      recvd += ret;
197
20.6k
    }
198
33.8k
  while ((size_t) recvd < len);
199
200
20.6k
  return recvd;
201
33.8k
}
Unexecuted instantiation: elf_end.c:pread_retry
Unexecuted instantiation: elf_rawfile.c:pread_retry
elf_readall.c:pread_retry
Line
Count
Source
186
6.53k
{
187
6.53k
  ssize_t recvd = 0;
188
189
6.53k
  do
190
6.53k
    {
191
6.53k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
192
6.53k
                 off + recvd));
193
6.53k
      if (ret <= 0)
194
0
  return ret < 0 ? ret : recvd;
195
196
6.53k
      recvd += ret;
197
6.53k
    }
198
6.53k
  while ((size_t) recvd < len);
199
200
6.53k
  return recvd;
201
6.53k
}
Unexecuted instantiation: elf_nextscn.c:pread_retry
Unexecuted instantiation: gelf_getshdr.c:pread_retry
Unexecuted instantiation: elf_strptr.c:pread_retry
elf_getdata.c:pread_retry
Line
Count
Source
186
90.8k
{
187
90.8k
  ssize_t recvd = 0;
188
189
90.8k
  do
190
90.8k
    {
191
90.8k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
192
90.8k
                 off + recvd));
193
90.8k
      if (ret <= 0)
194
0
  return ret < 0 ? ret : recvd;
195
196
90.8k
      recvd += ret;
197
90.8k
    }
198
90.8k
  while ((size_t) recvd < len);
199
200
90.8k
  return recvd;
201
90.8k
}
elf_getshdrstrndx.c:pread_retry
Line
Count
Source
186
176
{
187
176
  ssize_t recvd = 0;
188
189
176
  do
190
176
    {
191
176
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
192
176
                 off + recvd));
193
176
      if (ret <= 0)
194
0
  return ret < 0 ? ret : recvd;
195
196
176
      recvd += ret;
197
176
    }
198
176
  while ((size_t) recvd < len);
199
200
176
  return recvd;
201
176
}
Unexecuted instantiation: elf_compress.c:pread_retry
Unexecuted instantiation: elf_compress_gnu.c:pread_retry
Unexecuted instantiation: gelf_fsize.c:pread_retry
Unexecuted instantiation: gelf_xlate.c:pread_retry
Unexecuted instantiation: gelf_getehdr.c:pread_retry
elf32_getshdr.c:pread_retry
Line
Count
Source
186
9.28k
{
187
9.28k
  ssize_t recvd = 0;
188
189
9.28k
  do
190
9.28k
    {
191
9.28k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
192
9.28k
                 off + recvd));
193
9.28k
      if (ret <= 0)
194
0
  return ret < 0 ? ret : recvd;
195
196
9.28k
      recvd += ret;
197
9.28k
    }
198
9.28k
  while ((size_t) recvd < len);
199
200
9.28k
  return recvd;
201
9.28k
}
elf64_getshdr.c:pread_retry
Line
Count
Source
186
2.34k
{
187
2.34k
  ssize_t recvd = 0;
188
189
2.34k
  do
190
2.34k
    {
191
2.34k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
192
2.34k
                 off + recvd));
193
2.34k
      if (ret <= 0)
194
0
  return ret < 0 ? ret : recvd;
195
196
2.34k
      recvd += ret;
197
2.34k
    }
198
2.34k
  while ((size_t) recvd < len);
199
200
2.34k
  return recvd;
201
2.34k
}
Unexecuted instantiation: gelf_xlatetof.c:pread_retry
Unexecuted instantiation: elf_getshdrnum.c:pread_retry
Unexecuted instantiation: gelf_getchdr.c:pread_retry
Unexecuted instantiation: elf32_xlatetof.c:pread_retry
Unexecuted instantiation: elf64_xlatetof.c:pread_retry
Unexecuted instantiation: elf32_getchdr.c:pread_retry
Unexecuted instantiation: elf64_getchdr.c:pread_retry
202
203
/* The demangler from libstdc++.  */
204
extern char *__cxa_demangle (const char *mangled_name, char *output_buffer,
205
           size_t *length, int *status);
206
207
/* A static assertion.  This will cause a compile-time error if EXPR,
208
   which must be a compile-time constant, is false.  */
209
210
#define eu_static_assert(expr)            \
211
  extern int never_defined_just_used_for_checking[(expr) ? 1 : -1]  \
212
    __attribute__ ((unused))
213
214
#endif /* system.h */