Coverage Report

Created: 2026-01-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libunwind/src/elfxx.h
Line
Count
Source
1
/* libunwind - a platform-independent unwind library
2
   Copyright (C) 2003, 2005 Hewlett-Packard Co
3
   Copyright (C) 2007 David Mosberger-Tang
4
        Contributed by David Mosberger-Tang <dmosberger@gmail.com>
5
6
This file is part of libunwind.
7
8
Permission is hereby granted, free of charge, to any person obtaining
9
a copy of this software and associated documentation files (the
10
"Software"), to deal in the Software without restriction, including
11
without limitation the rights to use, copy, modify, merge, publish,
12
distribute, sublicense, and/or sell copies of the Software, and to
13
permit persons to whom the Software is furnished to do so, subject to
14
the following conditions:
15
16
The above copyright notice and this permission notice shall be
17
included in all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
26
27
#include <fcntl.h>
28
#include <unistd.h>
29
30
#include <sys/mman.h>
31
#include <sys/stat.h>
32
33
#include "libunwind_i.h"
34
35
#if UNW_ELF_CLASS == UNW_ELFCLASS32
36
# define ELF_W(x)       ELF32_##x
37
# define Elf_W(x)       Elf32_##x
38
# define elf_w(x)       _Uelf32_##x
39
#else
40
199M
# define ELF_W(x)       ELF64_##x
41
907k
# define Elf_W(x)       Elf64_##x
42
722k
# define elf_w(x)       _Uelf64_##x
43
#endif
44
45
extern int elf_w (get_proc_name) (unw_addr_space_t as,
46
                                  pid_t pid, unw_word_t ip,
47
                                  char *buf, size_t len,
48
                                  unw_word_t *offp);
49
50
extern int elf_w (get_proc_name_in_image) (unw_addr_space_t as,
51
                                           struct elf_image *ei,
52
                                           unsigned long segbase,
53
                                           unw_word_t ip,
54
                                           char *buf, size_t buf_len, unw_word_t *offp);
55
56
extern int elf_w (get_proc_ip_range) (unw_addr_space_t as,
57
                                      pid_t pid, unw_word_t ip,
58
                                      unw_word_t *start, unw_word_t *end);
59
60
extern int elf_w (get_proc_ip_range_in_image) (unw_addr_space_t as, struct elf_image *ei,
61
                                               unsigned long segbase, unw_word_t ip,
62
                                               unw_word_t *start, unw_word_t *end);
63
64
extern int elf_w (get_elf_filename) (unw_addr_space_t as, pid_t pid, unw_word_t ip,
65
                                     char *buf, size_t buf_len, unw_word_t *offp);
66
67
extern Elf_W (Shdr)* elf_w (find_section) (const struct elf_image *ei, const char* secname);
68
extern int elf_w (load_debuginfo) (const char* file, struct elf_image *ei, int is_local);
69
70
static inline int
71
elf_w (valid_object) (const struct elf_image *ei)
72
131k
{
73
131k
  if (ei->size <= EI_VERSION)
74
0
    return 0;
75
76
131k
  return (memcmp (ei->image, ELFMAG, SELFMAG) == 0
77
131k
          && ((uint8_t *) ei->image)[EI_CLASS] == UNW_ELF_CLASS
78
131k
          && ((uint8_t *) ei->image)[EI_VERSION] != EV_NONE
79
131k
          && ((uint8_t *) ei->image)[EI_VERSION] <= EV_CURRENT);
80
131k
}
Unexecuted instantiation: Lget_proc_name.c:_Uelf64_valid_object
Unexecuted instantiation: Lget_reg.c:_Uelf64_valid_object
Unexecuted instantiation: Lget_save_loc.c:_Uelf64_valid_object
Unexecuted instantiation: Linit_local.c:_Uelf64_valid_object
Unexecuted instantiation: Los-linux.c:_Uelf64_valid_object
Unexecuted instantiation: Lput_dynamic_unwind_info.c:_Uelf64_valid_object
Unexecuted instantiation: Lregs.c:_Uelf64_valid_object
Unexecuted instantiation: Lstep.c:_Uelf64_valid_object
Unexecuted instantiation: Lfind_dynamic_proc_info.c:_Uelf64_valid_object
Unexecuted instantiation: Lfind_proc_info-lsb.c:_Uelf64_valid_object
Unexecuted instantiation: Lget_accessors.c:_Uelf64_valid_object
Unexecuted instantiation: Lglobal.c:_Uelf64_valid_object
Unexecuted instantiation: Linit.c:_Uelf64_valid_object
Unexecuted instantiation: Lparser.c:_Uelf64_valid_object
Unexecuted instantiation: Lpe.c:_Uelf64_valid_object
Unexecuted instantiation: Lresume.c:_Uelf64_valid_object
Unexecuted instantiation: Lstash_frame.c:_Uelf64_valid_object
Unexecuted instantiation: dyn-info-list.c:_Uelf64_valid_object
elf64.c:_Uelf64_valid_object
Line
Count
Source
72
98.5k
{
73
98.5k
  if (ei->size <= EI_VERSION)
74
0
    return 0;
75
76
98.5k
  return (memcmp (ei->image, ELFMAG, SELFMAG) == 0
77
98.5k
          && ((uint8_t *) ei->image)[EI_CLASS] == UNW_ELF_CLASS
78
98.5k
          && ((uint8_t *) ei->image)[EI_VERSION] != EV_NONE
79
98.5k
          && ((uint8_t *) ei->image)[EI_VERSION] <= EV_CURRENT);
80
98.5k
}
Unexecuted instantiation: flush_cache.c:_Uelf64_valid_object
Unexecuted instantiation: global.c:_Uelf64_valid_object
Unexecuted instantiation: init.c:_Uelf64_valid_object
Unexecuted instantiation: is_fpreg.c:_Uelf64_valid_object
Unexecuted instantiation: mempool.c:_Uelf64_valid_object
os-linux.c:_Uelf64_valid_object
Line
Count
Source
72
32.8k
{
73
32.8k
  if (ei->size <= EI_VERSION)
74
0
    return 0;
75
76
32.8k
  return (memcmp (ei->image, ELFMAG, SELFMAG) == 0
77
32.8k
          && ((uint8_t *) ei->image)[EI_CLASS] == UNW_ELF_CLASS
78
32.8k
          && ((uint8_t *) ei->image)[EI_VERSION] != EV_NONE
79
32.8k
          && ((uint8_t *) ei->image)[EI_VERSION] <= EV_CURRENT);
80
32.8k
}
Unexecuted instantiation: Laddress_validator.c:_Uelf64_valid_object
Unexecuted instantiation: Ldyn-extract.c:_Uelf64_valid_object
Unexecuted instantiation: Lexpr.c:_Uelf64_valid_object
Unexecuted instantiation: Lfde.c:_Uelf64_valid_object
81
82
static inline int
83
elf_map_image (struct elf_image *ei, const char *path)
84
49.2k
{
85
49.2k
  struct stat stat;
86
49.2k
  int fd;
87
88
49.2k
  fd = open (path, O_RDONLY);
89
49.2k
  if (fd < 0)
90
16.4k
    return -1;
91
92
32.8k
  if (fstat (fd, &stat) < 0)
93
0
    {
94
0
      close (fd);
95
0
      return -1;
96
0
    }
97
98
32.8k
  ei->size = stat.st_size;
99
32.8k
  ei->image = mi_mmap (NULL, ei->size, PROT_READ, MAP_PRIVATE, fd, 0);
100
32.8k
  close (fd);
101
32.8k
  if (ei->image == MAP_FAILED)
102
0
    return -1;
103
104
32.8k
  if (!elf_w (valid_object) (ei))
105
0
  {
106
0
    mi_munmap(ei->image, ei->size);
107
0
    ei->image = NULL;
108
0
    return -1;
109
0
  }
110
111
32.8k
  return 0;
112
32.8k
}
Unexecuted instantiation: Lget_proc_name.c:elf_map_image
Unexecuted instantiation: Lget_reg.c:elf_map_image
Unexecuted instantiation: Lget_save_loc.c:elf_map_image
Unexecuted instantiation: Linit_local.c:elf_map_image
Unexecuted instantiation: Los-linux.c:elf_map_image
Unexecuted instantiation: Lput_dynamic_unwind_info.c:elf_map_image
Unexecuted instantiation: Lregs.c:elf_map_image
Unexecuted instantiation: Lstep.c:elf_map_image
Unexecuted instantiation: Lfind_dynamic_proc_info.c:elf_map_image
Unexecuted instantiation: Lfind_proc_info-lsb.c:elf_map_image
Unexecuted instantiation: Lget_accessors.c:elf_map_image
Unexecuted instantiation: Lglobal.c:elf_map_image
Unexecuted instantiation: Linit.c:elf_map_image
Unexecuted instantiation: Lparser.c:elf_map_image
Unexecuted instantiation: Lpe.c:elf_map_image
Unexecuted instantiation: Lresume.c:elf_map_image
Unexecuted instantiation: Lstash_frame.c:elf_map_image
Unexecuted instantiation: dyn-info-list.c:elf_map_image
elf64.c:elf_map_image
Line
Count
Source
84
16.4k
{
85
16.4k
  struct stat stat;
86
16.4k
  int fd;
87
88
16.4k
  fd = open (path, O_RDONLY);
89
16.4k
  if (fd < 0)
90
16.4k
    return -1;
91
92
0
  if (fstat (fd, &stat) < 0)
93
0
    {
94
0
      close (fd);
95
0
      return -1;
96
0
    }
97
98
0
  ei->size = stat.st_size;
99
0
  ei->image = mi_mmap (NULL, ei->size, PROT_READ, MAP_PRIVATE, fd, 0);
100
0
  close (fd);
101
0
  if (ei->image == MAP_FAILED)
102
0
    return -1;
103
104
0
  if (!elf_w (valid_object) (ei))
105
0
  {
106
0
    mi_munmap(ei->image, ei->size);
107
0
    ei->image = NULL;
108
0
    return -1;
109
0
  }
110
111
0
  return 0;
112
0
}
Unexecuted instantiation: flush_cache.c:elf_map_image
Unexecuted instantiation: global.c:elf_map_image
Unexecuted instantiation: init.c:elf_map_image
Unexecuted instantiation: is_fpreg.c:elf_map_image
Unexecuted instantiation: mempool.c:elf_map_image
os-linux.c:elf_map_image
Line
Count
Source
84
32.8k
{
85
32.8k
  struct stat stat;
86
32.8k
  int fd;
87
88
32.8k
  fd = open (path, O_RDONLY);
89
32.8k
  if (fd < 0)
90
0
    return -1;
91
92
32.8k
  if (fstat (fd, &stat) < 0)
93
0
    {
94
0
      close (fd);
95
0
      return -1;
96
0
    }
97
98
32.8k
  ei->size = stat.st_size;
99
32.8k
  ei->image = mi_mmap (NULL, ei->size, PROT_READ, MAP_PRIVATE, fd, 0);
100
32.8k
  close (fd);
101
32.8k
  if (ei->image == MAP_FAILED)
102
0
    return -1;
103
104
32.8k
  if (!elf_w (valid_object) (ei))
105
0
  {
106
0
    mi_munmap(ei->image, ei->size);
107
0
    ei->image = NULL;
108
0
    return -1;
109
0
  }
110
111
32.8k
  return 0;
112
32.8k
}
Unexecuted instantiation: Laddress_validator.c:elf_map_image
Unexecuted instantiation: Ldyn-extract.c:elf_map_image
Unexecuted instantiation: Lexpr.c:elf_map_image
Unexecuted instantiation: Lfde.c:elf_map_image
113
114
static inline const uint8_t* elf_w (get_program_segment) (const struct elf_image *ei,
115
  const Elf_W (Phdr) *phdr, const uint8_t** end)
116
36.9k
{
117
36.9k
  const uint8_t* result = NULL;
118
119
36.9k
  if (end)
120
36.9k
    *end = NULL;
121
122
36.9k
  if (!ei || !ei->image || !phdr || phdr->p_filesz == 0 || phdr->p_offset > ei->size)
123
0
    return NULL;
124
125
36.9k
  result = ((const uint8_t*)ei->image) + phdr->p_offset;
126
36.9k
  if (end)
127
36.9k
    *end = result + phdr->p_filesz;
128
129
36.9k
  return result;
130
36.9k
}
Unexecuted instantiation: Lget_proc_name.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lget_reg.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lget_save_loc.c:_Uelf64_get_program_segment
Unexecuted instantiation: Linit_local.c:_Uelf64_get_program_segment
Unexecuted instantiation: Los-linux.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lput_dynamic_unwind_info.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lregs.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lstep.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lfind_dynamic_proc_info.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lfind_proc_info-lsb.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lget_accessors.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lglobal.c:_Uelf64_get_program_segment
Unexecuted instantiation: Linit.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lparser.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lpe.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lresume.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lstash_frame.c:_Uelf64_get_program_segment
Unexecuted instantiation: dyn-info-list.c:_Uelf64_get_program_segment
elf64.c:_Uelf64_get_program_segment
Line
Count
Source
116
36.9k
{
117
36.9k
  const uint8_t* result = NULL;
118
119
36.9k
  if (end)
120
36.9k
    *end = NULL;
121
122
36.9k
  if (!ei || !ei->image || !phdr || phdr->p_filesz == 0 || phdr->p_offset > ei->size)
123
0
    return NULL;
124
125
36.9k
  result = ((const uint8_t*)ei->image) + phdr->p_offset;
126
36.9k
  if (end)
127
36.9k
    *end = result + phdr->p_filesz;
128
129
36.9k
  return result;
130
36.9k
}
Unexecuted instantiation: flush_cache.c:_Uelf64_get_program_segment
Unexecuted instantiation: global.c:_Uelf64_get_program_segment
Unexecuted instantiation: init.c:_Uelf64_get_program_segment
Unexecuted instantiation: is_fpreg.c:_Uelf64_get_program_segment
Unexecuted instantiation: mempool.c:_Uelf64_get_program_segment
Unexecuted instantiation: os-linux.c:_Uelf64_get_program_segment
Unexecuted instantiation: Laddress_validator.c:_Uelf64_get_program_segment
Unexecuted instantiation: Ldyn-extract.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lexpr.c:_Uelf64_get_program_segment
Unexecuted instantiation: Lfde.c:_Uelf64_get_program_segment