Coverage Report

Created: 2025-04-11 06:16

/src/elfutils/libelf/gelf_xlate.c
Line
Count
Source (jump to first uncovered line)
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
47.3M
#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
47.3M
  {                       \
97
47.3M
    switch (Bytes)                    \
98
47.3M
      {                       \
99
1.38M
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
45.8M
      case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;       \
101
38.6k
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
47.3M
      }                       \
105
47.3M
  }                        \
gelf_xlate.c:Elf32_cvt_Addr1
Line
Count
Source
96
1.62M
  {                       \
97
1.62M
    switch (Bytes)                    \
98
1.62M
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
1.62M
      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
1.62M
      }                       \
105
1.62M
  }                        \
gelf_xlate.c:Elf32_cvt_Sword1
Line
Count
Source
96
458k
  {                       \
97
458k
    switch (Bytes)                    \
98
458k
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
458k
      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
458k
      }                       \
105
458k
  }                        \
gelf_xlate.c:Elf32_cvt_Half1
Line
Count
Source
96
2.35k
  {                       \
97
2.35k
    switch (Bytes)                    \
98
2.35k
      {                       \
99
2.35k
      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
2.35k
      }                       \
105
2.35k
  }                        \
gelf_xlate.c:Elf32_cvt_Word1
Line
Count
Source
96
43.4M
  {                       \
97
43.4M
    switch (Bytes)                    \
98
43.4M
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
43.4M
      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
43.4M
      }                       \
105
43.4M
  }                        \
Unexecuted instantiation: gelf_xlate.c:Elf32_cvt_Off1
gelf_xlate.c:Elf32_cvt_Xword1
Line
Count
Source
96
1.04k
  {                       \
97
1.04k
    switch (Bytes)                    \
98
1.04k
      {                       \
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
1.04k
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
1.04k
      }                       \
105
1.04k
  }                        \
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
21.8k
  {                       \
97
21.8k
    switch (Bytes)                    \
98
21.8k
      {                       \
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
21.8k
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
21.8k
      }                       \
105
21.8k
  }                        \
gelf_xlate.c:Elf64_cvt_Xword1
Line
Count
Source
96
14.0k
  {                       \
97
14.0k
    switch (Bytes)                    \
98
14.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
14.0k
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
14.0k
      }                       \
105
14.0k
  }                        \
gelf_xlate.c:Elf64_cvt_Half1
Line
Count
Source
96
1.38M
  {                       \
97
1.38M
    switch (Bytes)                    \
98
1.38M
      {                       \
99
1.38M
      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
1.38M
      }                       \
105
1.38M
  }                        \
gelf_xlate.c:Elf64_cvt_Word1
Line
Count
Source
96
311k
  {                       \
97
311k
    switch (Bytes)                    \
98
311k
      {                       \
99
0
      case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;       \
100
311k
      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
311k
      }                       \
105
311k
  }                        \
Unexecuted instantiation: gelf_xlate.c:Elf64_cvt_Off1
gelf_xlate.c:Elf64_cvt_Sxword1
Line
Count
Source
96
1.71k
  {                       \
97
1.71k
    switch (Bytes)                    \
98
1.71k
      {                       \
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
1.71k
      case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;       \
102
0
      default:                      \
103
0
  abort ();                   \
104
1.71k
      }                       \
105
1.71k
  }                        \
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
170
  {                       \
110
170
    size_t n = len / sizeof (TName);                \
111
170
    if (dest < ptr)                   \
112
667k
      while (n-- > 0)                   \
113
667k
  {                     \
114
667k
    FName##1 (dest, ptr);                 \
115
667k
    dest += Bytes;                  \
116
667k
    ptr += Bytes;                   \
117
667k
  }                      \
118
170
    else                      \
119
170
      {                       \
120
95
  dest += len;                    \
121
95
  ptr += len;                   \
122
1.39M
  while (n-- > 0)                   \
123
1.39M
    {                     \
124
1.39M
      ptr -= Bytes;                 \
125
1.39M
      dest -= Bytes;                  \
126
1.39M
      FName##1 (dest, ptr);               \
127
1.39M
    }                      \
128
95
      }                        \
129
170
  }
gelf_xlate.c:Elf32_cvt_Addr
Line
Count
Source
109
26
  {                       \
110
26
    size_t n = len / sizeof (TName);                \
111
26
    if (dest < ptr)                   \
112
457k
      while (n-- > 0)                   \
113
457k
  {                     \
114
457k
    FName##1 (dest, ptr);                 \
115
457k
    dest += Bytes;                  \
116
457k
    ptr += Bytes;                   \
117
457k
  }                      \
118
26
    else                      \
119
26
      {                       \
120
11
  dest += len;                    \
121
11
  ptr += len;                   \
122
388k
  while (n-- > 0)                   \
123
388k
    {                     \
124
388k
      ptr -= Bytes;                 \
125
388k
      dest -= Bytes;                  \
126
388k
      FName##1 (dest, ptr);               \
127
388k
    }                      \
128
11
      }                        \
129
26
  }
gelf_xlate.c:Elf32_cvt_Half
Line
Count
Source
109
14
  {                       \
110
14
    size_t n = len / sizeof (TName);                \
111
14
    if (dest < ptr)                   \
112
42
      while (n-- > 0)                   \
113
39
  {                     \
114
39
    FName##1 (dest, ptr);                 \
115
39
    dest += Bytes;                  \
116
39
    ptr += Bytes;                   \
117
39
  }                      \
118
14
    else                      \
119
14
      {                       \
120
11
  dest += len;                    \
121
11
  ptr += len;                   \
122
408
  while (n-- > 0)                   \
123
397
    {                     \
124
397
      ptr -= Bytes;                 \
125
397
      dest -= Bytes;                  \
126
397
      FName##1 (dest, ptr);               \
127
397
    }                      \
128
11
      }                        \
129
14
  }
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
44
  {                       \
110
44
    size_t n = len / sizeof (TName);                \
111
44
    if (dest < ptr)                   \
112
2.87k
      while (n-- > 0)                   \
113
2.87k
  {                     \
114
2.87k
    FName##1 (dest, ptr);                 \
115
2.87k
    dest += Bytes;                  \
116
2.87k
    ptr += Bytes;                   \
117
2.87k
  }                      \
118
44
    else                      \
119
44
      {                       \
120
36
  dest += len;                    \
121
36
  ptr += len;                   \
122
155
  while (n-- > 0)                   \
123
119
    {                     \
124
119
      ptr -= Bytes;                 \
125
119
      dest -= Bytes;                  \
126
119
      FName##1 (dest, ptr);               \
127
119
    }                      \
128
36
      }                        \
129
44
  }
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
20
  {                       \
110
20
    size_t n = len / sizeof (TName);                \
111
20
    if (dest < ptr)                   \
112
18.5k
      while (n-- > 0)                   \
113
18.4k
  {                     \
114
18.4k
    FName##1 (dest, ptr);                 \
115
18.4k
    dest += Bytes;                  \
116
18.4k
    ptr += Bytes;                   \
117
18.4k
  }                      \
118
20
    else                      \
119
20
      {                       \
120
5
  dest += len;                    \
121
5
  ptr += len;                   \
122
89
  while (n-- > 0)                   \
123
84
    {                     \
124
84
      ptr -= Bytes;                 \
125
84
      dest -= Bytes;                  \
126
84
      FName##1 (dest, ptr);               \
127
84
    }                      \
128
5
      }                        \
129
20
  }
gelf_xlate.c:Elf64_cvt_Half
Line
Count
Source
109
31
  {                       \
110
31
    size_t n = len / sizeof (TName);                \
111
31
    if (dest < ptr)                   \
112
184k
      while (n-- > 0)                   \
113
184k
  {                     \
114
184k
    FName##1 (dest, ptr);                 \
115
184k
    dest += Bytes;                  \
116
184k
    ptr += Bytes;                   \
117
184k
  }                      \
118
31
    else                      \
119
31
      {                       \
120
13
  dest += len;                    \
121
13
  ptr += len;                   \
122
729k
  while (n-- > 0)                   \
123
729k
    {                     \
124
729k
      ptr -= Bytes;                 \
125
729k
      dest -= Bytes;                  \
126
729k
      FName##1 (dest, ptr);               \
127
729k
    }                      \
128
13
      }                        \
129
31
  }
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
23
  {                       \
110
23
    size_t n = len / sizeof (TName);                \
111
23
    if (dest < ptr)                   \
112
3.39k
      while (n-- > 0)                   \
113
3.38k
  {                     \
114
3.38k
    FName##1 (dest, ptr);                 \
115
3.38k
    dest += Bytes;                  \
116
3.38k
    ptr += Bytes;                   \
117
3.38k
  }                      \
118
23
    else                      \
119
23
      {                       \
120
12
  dest += len;                    \
121
12
  ptr += len;                   \
122
278k
  while (n-- > 0)                   \
123
278k
    {                     \
124
278k
      ptr -= Bytes;                 \
125
278k
      dest -= Bytes;                  \
126
278k
      FName##1 (dest, ptr);               \
127
278k
    }                      \
128
12
      }                        \
129
23
  }
gelf_xlate.c:Elf64_cvt_Xword
Line
Count
Source
109
12
  {                       \
110
12
    size_t n = len / sizeof (TName);                \
111
12
    if (dest < ptr)                   \
112
139
      while (n-- > 0)                   \
113
134
  {                     \
114
134
    FName##1 (dest, ptr);                 \
115
134
    dest += Bytes;                  \
116
134
    ptr += Bytes;                   \
117
134
  }                      \
118
12
    else                      \
119
12
      {                       \
120
7
  dest += len;                    \
121
7
  ptr += len;                   \
122
113
  while (n-- > 0)                   \
123
106
    {                     \
124
106
      ptr -= Bytes;                 \
125
106
      dest -= Bytes;                  \
126
106
      FName##1 (dest, ptr);               \
127
106
    }                      \
128
7
      }                        \
129
12
  }
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
14.2M
  { ElfW2(Bits, Name) *tdest = (ElfW2(Bits, Name) *) dest;         \
140
14.2M
    ElfW2(Bits, Name) *tsrc = (ElfW2(Bits, Name) *) src;         \
141
14.2M
    size_t sz = sizeof (ElfW2(Bits, Name));             \
142
14.2M
    size_t n;                     \
143
29.5M
    for (n = len / sz; n > 0; ++tdest, ++tsrc, --n) {
144
#define END(Bits, Name)                   \
145
15.3M
    }                        \
146
14.2M
    if (len % sz > 0) /* Cannot convert partial structures, just copy. */     \
147
14.2M
      memmove (dest, src, len % sz);               \
148
14.2M
  }
149
#define TYPE_EXTRA(Code)
150
167k
#define TYPE_XLATE(Code) Code
151
45.0M
#define TYPE_NAME(Type, Name) TYPE_NAME2 (Type, Name)
152
45.0M
#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
};