Coverage Report

Created: 2026-10-23 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/libelf/gelf_xlate.c
Line
Count
Source
1
/* Transformation functions for ELF data types.
2
   Copyright (C) 1998,1999,2000,2002,2004,2005,2006,2007,2015 Red Hat, Inc.
3
   Copyright (C) 2022 Mark J. Wielaard <mark@klomp.org>
4
   This file is part of elfutils.
5
   Written by Ulrich Drepper <drepper@redhat.com>, 1998.
6
7
   This file is free software; you can redistribute it and/or modify
8
   it under the terms of either
9
10
     * the GNU Lesser General Public License as published by the Free
11
       Software Foundation; either version 3 of the License, or (at
12
       your option) any later version
13
14
   or
15
16
     * the GNU General Public License as published by the Free
17
       Software Foundation; either version 2 of the License, or (at
18
       your option) any later version
19
20
   or both in parallel, as here.
21
22
   elfutils is distributed in the hope that it will be useful, but
23
   WITHOUT ANY WARRANTY; without even the implied warranty of
24
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
   General Public License for more details.
26
27
   You should have received copies of the GNU General Public License and
28
   the GNU Lesser General Public License along with this program.  If
29
   not, see <http://www.gnu.org/licenses/>.  */
30
31
#ifdef HAVE_CONFIG_H
32
# include <config.h>
33
#endif
34
35
#include <stdint.h>
36
#include <string.h>
37
#include <stdlib.h>
38
39
#include "libelfP.h"
40
41
#ifndef LIBELFBITS
42
# define LIBELFBITS 32
43
#endif
44
45
46
/* Well, what shall I say.  Nothing to do here.  */
47
#define elf_cvt_Byte(dest, src, n) \
48
  (__builtin_constant_p (n) && (n) == 1               \
49
   ? (void) (*((char *) (dest)) = *((char *) (src)))            \
50
   : Elf32_cvt_Byte (dest, src, n))
51
static void
52
(elf_cvt_Byte) (void *dest, const void *src, size_t n,
53
    int encode __attribute__ ((unused)))
54
0
{
55
0
  if (n != 0)
56
0
    memmove (dest, src, n);
57
0
}
58
59
60
/* We'll optimize the definition of the conversion functions here a
61
   bit.  We need only functions for 16, 32, and 64 bits.  The
62
   functions referenced in the table will be aliases for one of these
63
   functions.  Which one is decided by the ELFxx_FSZ_type.  */
64
65
#if ALLOW_UNALIGNED
66
67
#define FETCH(Bits, ptr)  (*(const uint##Bits##_t *) ptr)
68
30.4M
#define STORE(Bits, ptr, val) (*(uint##Bits##_t *) ptr = val)
69
70
#else
71
72
union unaligned
73
  {
74
    uint16_t u16;
75
    uint32_t u32;
76
    uint64_t u64;
77
  } attribute_packed;
78
79
#define FETCH(Bits, ptr)  (((const union unaligned *) ptr)->u##Bits)
80
#define STORE(Bits, ptr, val) (((union unaligned *) ptr)->u##Bits = val)
81
82
#endif
83
84
/* Now define the conversion functions for the basic types.  We use here
85
   the fact that file and memory types are the same and that we have the
86
   ELFxx_FSZ_* macros.
87
88
   At the same time we define inline functions which we will use to
89
   convert the complex types.  */
90
#define FUNDAMENTAL(NAME, Name, Bits) \
91
  INLINE2 (ELFW2(Bits,FSZ_##NAME), ElfW2(Bits,cvt_##Name), ElfW2(Bits,Name))
92
#define INLINE2(Bytes, FName, TName) \
93
  INLINE3 (Bytes, FName, TName)
94
#define INLINE3(Bytes, FName, TName)                \
95
  static inline void FName##1 (void *dest, const void *ptr)         \
96
30.4M
  {                       \
97
30.4M
    switch (Bytes)                    \
98
30.4M
      {                       \
99
814k
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
29.3M
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
261k
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
30.4M
      }                       \
105
30.4M
  }                        \
gelf_xlate.c:Elf32_cvt_Addr1
Line
Count
Source
96
28.5M
  {                       \
97
28.5M
    switch (Bytes)                    \
98
28.5M
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
28.5M
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
0
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
28.5M
      }                       \
105
28.5M
  }                        \
gelf_xlate.c:Elf32_cvt_Sword1
Line
Count
Source
96
86.2k
  {                       \
97
86.2k
    switch (Bytes)                    \
98
86.2k
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
86.2k
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
0
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
86.2k
      }                       \
105
86.2k
  }                        \
gelf_xlate.c:Elf32_cvt_Half1
Line
Count
Source
96
578k
  {                       \
97
578k
    switch (Bytes)                    \
98
578k
      {                       \
99
578k
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
0
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
0
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
578k
      }                       \
105
578k
  }                        \
gelf_xlate.c:Elf32_cvt_Word1
Line
Count
Source
96
558k
  {                       \
97
558k
    switch (Bytes)                    \
98
558k
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
558k
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
0
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
558k
      }                       \
105
558k
  }                        \
Unexecuted instantiation: gelf_xlate.c:Elf32_cvt_Off1
gelf_xlate.c:Elf32_cvt_Xword1
Line
Count
Source
96
53.6k
  {                       \
97
53.6k
    switch (Bytes)                    \
98
53.6k
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
0
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
53.6k
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
53.6k
      }                       \
105
53.6k
  }                        \
Unexecuted instantiation: gelf_xlate.c:Elf32_cvt_Sxword1
Unexecuted instantiation: gelf_xlate.c:Elf32_cvt_Relr1
gelf_xlate.c:Elf64_cvt_Addr1
Line
Count
Source
96
69.5k
  {                       \
97
69.5k
    switch (Bytes)                    \
98
69.5k
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
0
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
69.5k
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
69.5k
      }                       \
105
69.5k
  }                        \
gelf_xlate.c:Elf64_cvt_Xword1
Line
Count
Source
96
124k
  {                       \
97
124k
    switch (Bytes)                    \
98
124k
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
0
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
124k
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
124k
      }                       \
105
124k
  }                        \
gelf_xlate.c:Elf64_cvt_Half1
Line
Count
Source
96
235k
  {                       \
97
235k
    switch (Bytes)                    \
98
235k
      {                       \
99
235k
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
0
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
0
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
235k
      }                       \
105
235k
  }                        \
gelf_xlate.c:Elf64_cvt_Word1
Line
Count
Source
96
131k
  {                       \
97
131k
    switch (Bytes)                    \
98
131k
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
131k
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
0
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
131k
      }                       \
105
131k
  }                        \
Unexecuted instantiation: gelf_xlate.c:Elf64_cvt_Off1
gelf_xlate.c:Elf64_cvt_Sxword1
Line
Count
Source
96
13.0k
  {                       \
97
13.0k
    switch (Bytes)                    \
98
13.0k
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
0
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
13.0k
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
13.0k
      }                       \
105
13.0k
  }                        \
Unexecuted instantiation: gelf_xlate.c:Elf64_cvt_Sword1
Unexecuted instantiation: gelf_xlate.c:Elf64_cvt_Relr1
106
                        \
107
  static void FName (void *dest, const void *ptr, size_t len,         \
108
         int encode __attribute__ ((unused)))         \
109
7.55k
  {                       \
110
7.55k
    size_t n = len / sizeof (TName);                \
111
7.55k
    if (dest < ptr)                   \
112
380k
      while (n-- > 0)                   \
113
378k
  {                     \
114
378k
    FName##1 (dest, ptr);                  \
115
378k
    dest += Bytes;                  \
116
378k
    ptr += Bytes;                   \
117
378k
  }                      \
118
7.55k
    else                      \
119
7.55k
      {                       \
120
5.18k
  dest += len;                    \
121
5.18k
  ptr += len;                   \
122
29.0M
  while (n-- > 0)                   \
123
29.0M
    {                     \
124
29.0M
      ptr -= Bytes;                 \
125
29.0M
      dest -= Bytes;                  \
126
29.0M
      FName##1 (dest, ptr);                \
127
29.0M
    }                      \
128
5.18k
      }                        \
129
7.55k
  }
gelf_xlate.c:Elf32_cvt_Addr
Line
Count
Source
109
1.42k
  {                       \
110
1.42k
    size_t n = len / sizeof (TName);                \
111
1.42k
    if (dest < ptr)                   \
112
224k
      while (n-- > 0)                   \
113
223k
  {                     \
114
223k
    FName##1 (dest, ptr);                  \
115
223k
    dest += Bytes;                  \
116
223k
    ptr += Bytes;                   \
117
223k
  }                      \
118
1.42k
    else                      \
119
1.42k
      {                       \
120
1.08k
  dest += len;                    \
121
1.08k
  ptr += len;                   \
122
28.2M
  while (n-- > 0)                   \
123
28.2M
    {                     \
124
28.2M
      ptr -= Bytes;                 \
125
28.2M
      dest -= Bytes;                  \
126
28.2M
      FName##1 (dest, ptr);                \
127
28.2M
    }                      \
128
1.08k
      }                        \
129
1.42k
  }
gelf_xlate.c:Elf32_cvt_Half
Line
Count
Source
109
1.30k
  {                       \
110
1.30k
    size_t n = len / sizeof (TName);                \
111
1.30k
    if (dest < ptr)                   \
112
43.5k
      while (n-- > 0)                   \
113
43.2k
  {                     \
114
43.2k
    FName##1 (dest, ptr);                  \
115
43.2k
    dest += Bytes;                  \
116
43.2k
    ptr += Bytes;                   \
117
43.2k
  }                      \
118
1.30k
    else                      \
119
1.30k
      {                       \
120
971
  dest += len;                    \
121
971
  ptr += len;                   \
122
303k
  while (n-- > 0)                   \
123
302k
    {                     \
124
302k
      ptr -= Bytes;                 \
125
302k
      dest -= Bytes;                  \
126
302k
      FName##1 (dest, ptr);                \
127
302k
    }                      \
128
971
      }                        \
129
1.30k
  }
Unexecuted instantiation: gelf_xlate.c:Elf32_cvt_Off
Unexecuted instantiation: gelf_xlate.c:Elf32_cvt_Sword
gelf_xlate.c:Elf32_cvt_Word
Line
Count
Source
109
2.13k
  {                       \
110
2.13k
    size_t n = len / sizeof (TName);                \
111
2.13k
    if (dest < ptr)                   \
112
40.8k
      while (n-- > 0)                   \
113
40.1k
  {                     \
114
40.1k
    FName##1 (dest, ptr);                  \
115
40.1k
    dest += Bytes;                  \
116
40.1k
    ptr += Bytes;                   \
117
40.1k
  }                      \
118
2.13k
    else                      \
119
2.13k
      {                       \
120
1.45k
  dest += len;                    \
121
1.45k
  ptr += len;                   \
122
223k
  while (n-- > 0)                   \
123
222k
    {                     \
124
222k
      ptr -= Bytes;                 \
125
222k
      dest -= Bytes;                  \
126
222k
      FName##1 (dest, ptr);                \
127
222k
    }                      \
128
1.45k
      }                        \
129
2.13k
  }
Unexecuted instantiation: gelf_xlate.c:Elf32_cvt_Xword
Unexecuted instantiation: gelf_xlate.c:Elf32_cvt_Sxword
Unexecuted instantiation: gelf_xlate.c:Elf32_cvt_Relr
gelf_xlate.c:Elf64_cvt_Addr
Line
Count
Source
109
808
  {                       \
110
808
    size_t n = len / sizeof (TName);                \
111
808
    if (dest < ptr)                   \
112
8.18k
      while (n-- > 0)                   \
113
7.90k
  {                     \
114
7.90k
    FName##1 (dest, ptr);                  \
115
7.90k
    dest += Bytes;                  \
116
7.90k
    ptr += Bytes;                   \
117
7.90k
  }                      \
118
808
    else                      \
119
808
      {                       \
120
521
  dest += len;                    \
121
521
  ptr += len;                   \
122
38.3k
  while (n-- > 0)                   \
123
37.8k
    {                     \
124
37.8k
      ptr -= Bytes;                 \
125
37.8k
      dest -= Bytes;                  \
126
37.8k
      FName##1 (dest, ptr);                \
127
37.8k
    }                      \
128
521
      }                        \
129
808
  }
gelf_xlate.c:Elf64_cvt_Half
Line
Count
Source
109
587
  {                       \
110
587
    size_t n = len / sizeof (TName);                \
111
587
    if (dest < ptr)                   \
112
45.8k
      while (n-- > 0)                   \
113
45.5k
  {                     \
114
45.5k
    FName##1 (dest, ptr);                  \
115
45.5k
    dest += Bytes;                  \
116
45.5k
    ptr += Bytes;                   \
117
45.5k
  }                      \
118
587
    else                      \
119
587
      {                       \
120
350
  dest += len;                    \
121
350
  ptr += len;                   \
122
81.0k
  while (n-- > 0)                   \
123
80.6k
    {                     \
124
80.6k
      ptr -= Bytes;                 \
125
80.6k
      dest -= Bytes;                  \
126
80.6k
      FName##1 (dest, ptr);                \
127
80.6k
    }                      \
128
350
      }                        \
129
587
  }
Unexecuted instantiation: gelf_xlate.c:Elf64_cvt_Off
Unexecuted instantiation: gelf_xlate.c:Elf64_cvt_Sword
gelf_xlate.c:Elf64_cvt_Word
Line
Count
Source
109
698
  {                       \
110
698
    size_t n = len / sizeof (TName);                \
111
698
    if (dest < ptr)                   \
112
12.9k
      while (n-- > 0)                   \
113
12.6k
  {                     \
114
12.6k
    FName##1 (dest, ptr);                  \
115
12.6k
    dest += Bytes;                  \
116
12.6k
    ptr += Bytes;                   \
117
12.6k
  }                      \
118
698
    else                      \
119
698
      {                       \
120
434
  dest += len;                    \
121
434
  ptr += len;                   \
122
105k
  while (n-- > 0)                   \
123
105k
    {                     \
124
105k
      ptr -= Bytes;                 \
125
105k
      dest -= Bytes;                  \
126
105k
      FName##1 (dest, ptr);                \
127
105k
    }                      \
128
434
      }                        \
129
698
  }
gelf_xlate.c:Elf64_cvt_Xword
Line
Count
Source
109
603
  {                       \
110
603
    size_t n = len / sizeof (TName);                \
111
603
    if (dest < ptr)                   \
112
4.74k
      while (n-- > 0)                   \
113
4.52k
  {                     \
114
4.52k
    FName##1 (dest, ptr);                  \
115
4.52k
    dest += Bytes;                  \
116
4.52k
    ptr += Bytes;                   \
117
4.52k
  }                      \
118
603
    else                      \
119
603
      {                       \
120
379
  dest += len;                    \
121
379
  ptr += len;                   \
122
91.4k
  while (n-- > 0)                   \
123
91.0k
    {                     \
124
91.0k
      ptr -= Bytes;                 \
125
91.0k
      dest -= Bytes;                  \
126
91.0k
      FName##1 (dest, ptr);                \
127
91.0k
    }                      \
128
379
      }                        \
129
603
  }
Unexecuted instantiation: gelf_xlate.c:Elf64_cvt_Sxword
Unexecuted instantiation: gelf_xlate.c:Elf64_cvt_Relr
130
131
132
/* Now the tricky part: define the transformation functions for the
133
   complex types.  We will use the definitions of the types in
134
   abstract.h.  */
135
#define START(Bits, Name, EName) \
136
  static void                     \
137
  ElfW2 (Bits, cvt_##Name) (void *dest, const void *src, size_t len,        \
138
          int encode __attribute__ ((unused)))        \
139
46.7k
  { ElfW2(Bits, Name) *tdest = (ElfW2(Bits, Name) *) dest;         \
140
46.7k
    ElfW2(Bits, Name) *tsrc = (ElfW2(Bits, Name) *) src;         \
141
46.7k
    size_t sz = sizeof (ElfW2(Bits, Name));             \
142
46.7k
    size_t n;                     \
143
410k
    for (n = len / sz; n > 0; ++tdest, ++tsrc, --n) {
144
#define END(Bits, Name)                   \
145
363k
    }                        \
146
46.7k
    if (len % sz > 0) /* Cannot convert partial structures, just copy. */     \
147
46.7k
      memmove (dest, src, len % sz);               \
148
46.7k
  }
149
#define TYPE_EXTRA(Code)
150
72.9k
#define TYPE_XLATE(Code) Code
151
947k
#define TYPE_NAME(Type, Name) TYPE_NAME2 (Type, Name)
152
947k
#define TYPE_NAME2(Type, Name) Type##1 (&tdest->Name, &tsrc->Name);
153
#define TYPE(Name, Bits) TYPE2 (Name, Bits)
154
#define TYPE2(Name, Bits) TYPE3 (Name##Bits)
155
#define TYPE3(Name) Name (cvt_)
156
157
/* Signal that we are generating conversion functions.  */
158
#define GENERATE_CONVERSION
159
160
/* First generate the 32-bit conversion functions.  */
161
#define LIBELFBITS 32
162
#include "gelf_xlate.h"
163
164
/* Now generate the 64-bit conversion functions.  */
165
#define LIBELFBITS 64
166
#include "gelf_xlate.h"
167
168
169
/* We have a few functions which we must create by hand since the sections
170
   do not contain records of only one type.  */
171
#include "version_xlate.h"
172
#include "gnuhash_xlate.h"
173
#include "note_xlate.h"
174
#include "chdr_xlate.h"
175
176
177
/* Now the externally visible table with the function pointers.  */
178
const xfct_t __elf_xfctstom[ELFCLASSNUM - 1][ELF_T_NUM] =
179
{
180
      [ELFCLASS32 - 1] = {
181
#define define_xfcts(Bits) \
182
  [ELF_T_BYTE]  = elf_cvt_Byte,               \
183
  [ELF_T_ADDR]  = ElfW2(Bits, cvt_Addr),            \
184
  [ELF_T_DYN] = ElfW2(Bits, cvt_Dyn),             \
185
  [ELF_T_EHDR]  = ElfW2(Bits, cvt_Ehdr),            \
186
  [ELF_T_HALF]  = ElfW2(Bits, cvt_Half),            \
187
  [ELF_T_OFF] = ElfW2(Bits, cvt_Off),             \
188
  [ELF_T_PHDR]  = ElfW2(Bits, cvt_Phdr),            \
189
  [ELF_T_RELA]  = ElfW2(Bits, cvt_Rela),            \
190
  [ELF_T_REL] = ElfW2(Bits, cvt_Rel),             \
191
  [ELF_T_SHDR]  = ElfW2(Bits, cvt_Shdr),            \
192
  [ELF_T_SWORD] = ElfW2(Bits, cvt_Sword),           \
193
  [ELF_T_SYM] = ElfW2(Bits, cvt_Sym),             \
194
  [ELF_T_WORD]  = ElfW2(Bits, cvt_Word),            \
195
  [ELF_T_XWORD] = ElfW2(Bits, cvt_Xword),           \
196
  [ELF_T_SXWORD]  = ElfW2(Bits, cvt_Sxword),            \
197
  [ELF_T_VDEF]  = elf_cvt_Verdef,             \
198
  [ELF_T_VDAUX] = elf_cvt_Verdef,             \
199
  [ELF_T_VNEED] = elf_cvt_Verneed,              \
200
  [ELF_T_VNAUX] = elf_cvt_Verneed,              \
201
  [ELF_T_NHDR]  = elf_cvt_note4,              \
202
  [ELF_T_NHDR8] = elf_cvt_note8,              \
203
  [ELF_T_SYMINFO] = ElfW2(Bits, cvt_Syminfo),           \
204
  [ELF_T_MOVE]  = ElfW2(Bits, cvt_Move),            \
205
  [ELF_T_LIB] = ElfW2(Bits, cvt_Lib),             \
206
  [ELF_T_AUXV]  = ElfW2(Bits, cvt_auxv_t),            \
207
  [ELF_T_CHDR]  = ElfW2(Bits, cvt_chdr),            \
208
  [ELF_T_RELR]  = ElfW2(Bits, cvt_Relr)
209
        define_xfcts (32),
210
  [ELF_T_GNUHASH] = Elf32_cvt_Word
211
      },
212
      [ELFCLASS64 - 1] = {
213
  define_xfcts (64),
214
  [ELF_T_GNUHASH] = elf_cvt_gnuhash
215
      }
216
};