Coverage Report

Created: 2026-07-24 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/libelf/elf32_xlatetom.c
Line
Count
Source
1
/* Convert from file to memory representation.
2
   Copyright (C) 1998, 1999, 2000, 2002, 2012, 2015 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
#ifdef HAVE_CONFIG_H
31
# include <config.h>
32
#endif
33
34
#include <assert.h>
35
#include <string.h>
36
37
#include "libelfP.h"
38
39
#ifndef LIBELFBITS
40
# define LIBELFBITS 32
41
#endif
42
43
44
Elf_Data *
45
elfw2(LIBELFBITS, xlatetom) (Elf_Data *dest, const Elf_Data *src,
46
           unsigned int encode)
47
134k
{
48
134k
  if (src == NULL || dest == NULL)
49
0
    return NULL;
50
51
134k
  if (src->d_type >= ELF_T_NUM)
52
0
    {
53
0
      __libelf_seterrno (ELF_E_UNKNOWN_TYPE);
54
0
      return NULL;
55
0
    }
56
57
  /* First test whether the input data is really suitable for this
58
     type.  This means, whether there is an integer number of records.
59
     Note that for this implementation the memory and file size of the
60
     data types are identical.  */
61
134k
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
62
63
64
  /* We shouldn't require integer number of records when processing
65
     notes.  Payload bytes follow the header immediately, it's not an
66
     array of records as is the case otherwise.  */
67
134k
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
68
115k
      && src->d_size % recsize != 0)
69
24
    {
70
24
      __libelf_seterrno (ELF_E_INVALID_DATA);
71
24
      return NULL;
72
24
    }
73
74
  /* Next see whether the converted data fits in the output buffer.  */
75
134k
  if (src->d_size > dest->d_size)
76
0
    {
77
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
78
0
      return NULL;
79
0
    }
80
81
  /* Test the encode parameter.  */
82
134k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
83
430
    {
84
430
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
85
430
      return NULL;
86
430
    }
87
88
  /* Determine the translation function to use.
89
90
     At this point we make an assumption which is valid for all
91
     existing implementations so far: the memory and file sizes are
92
     the same.  This has very important consequences:
93
     a) The requirement that the source and destination buffer can
94
  overlap can easily be fulfilled.
95
     b) We need only one function to convert from and memory to file
96
  and vice versa since the function only has to copy and/or
97
  change the byte order.
98
  */
99
134k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
100
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
101
82.0k
    {
102
      /* We simply have to copy since the byte order is the same.  */
103
82.0k
      if (src->d_buf != dest->d_buf)
104
49.2k
  memmove (dest->d_buf, src->d_buf, src->d_size);
105
82.0k
    }
106
52.0k
  else
107
52.0k
    {
108
52.0k
      xfct_t fctp;
109
52.0k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
110
111
      /* Do the real work.  */
112
52.0k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
113
52.0k
    }
114
115
  /* Now set the real destination type and length since the operation was
116
     successful.  */
117
134k
  dest->d_type = src->d_type;
118
134k
  dest->d_size = src->d_size;
119
120
134k
  return dest;
121
134k
}
elf32_xlatetom
Line
Count
Source
47
102k
{
48
102k
  if (src == NULL || dest == NULL)
49
0
    return NULL;
50
51
102k
  if (src->d_type >= ELF_T_NUM)
52
0
    {
53
0
      __libelf_seterrno (ELF_E_UNKNOWN_TYPE);
54
0
      return NULL;
55
0
    }
56
57
  /* First test whether the input data is really suitable for this
58
     type.  This means, whether there is an integer number of records.
59
     Note that for this implementation the memory and file size of the
60
     data types are identical.  */
61
102k
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
62
63
64
  /* We shouldn't require integer number of records when processing
65
     notes.  Payload bytes follow the header immediately, it's not an
66
     array of records as is the case otherwise.  */
67
102k
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
68
83.3k
      && src->d_size % recsize != 0)
69
8
    {
70
8
      __libelf_seterrno (ELF_E_INVALID_DATA);
71
8
      return NULL;
72
8
    }
73
74
  /* Next see whether the converted data fits in the output buffer.  */
75
102k
  if (src->d_size > dest->d_size)
76
0
    {
77
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
78
0
      return NULL;
79
0
    }
80
81
  /* Test the encode parameter.  */
82
102k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
83
221
    {
84
221
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
85
221
      return NULL;
86
221
    }
87
88
  /* Determine the translation function to use.
89
90
     At this point we make an assumption which is valid for all
91
     existing implementations so far: the memory and file sizes are
92
     the same.  This has very important consequences:
93
     a) The requirement that the source and destination buffer can
94
  overlap can easily be fulfilled.
95
     b) We need only one function to convert from and memory to file
96
  and vice versa since the function only has to copy and/or
97
  change the byte order.
98
  */
99
102k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
100
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
101
54.7k
    {
102
      /* We simply have to copy since the byte order is the same.  */
103
54.7k
      if (src->d_buf != dest->d_buf)
104
30.4k
  memmove (dest->d_buf, src->d_buf, src->d_size);
105
54.7k
    }
106
47.7k
  else
107
47.7k
    {
108
47.7k
      xfct_t fctp;
109
47.7k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
110
111
      /* Do the real work.  */
112
47.7k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
113
47.7k
    }
114
115
  /* Now set the real destination type and length since the operation was
116
     successful.  */
117
102k
  dest->d_type = src->d_type;
118
102k
  dest->d_size = src->d_size;
119
120
102k
  return dest;
121
102k
}
elf64_xlatetom
Line
Count
Source
47
31.8k
{
48
31.8k
  if (src == NULL || dest == NULL)
49
0
    return NULL;
50
51
31.8k
  if (src->d_type >= ELF_T_NUM)
52
0
    {
53
0
      __libelf_seterrno (ELF_E_UNKNOWN_TYPE);
54
0
      return NULL;
55
0
    }
56
57
  /* First test whether the input data is really suitable for this
58
     type.  This means, whether there is an integer number of records.
59
     Note that for this implementation the memory and file size of the
60
     data types are identical.  */
61
31.8k
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
62
63
64
  /* We shouldn't require integer number of records when processing
65
     notes.  Payload bytes follow the header immediately, it's not an
66
     array of records as is the case otherwise.  */
67
31.8k
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
68
31.8k
      && src->d_size % recsize != 0)
69
16
    {
70
16
      __libelf_seterrno (ELF_E_INVALID_DATA);
71
16
      return NULL;
72
16
    }
73
74
  /* Next see whether the converted data fits in the output buffer.  */
75
31.7k
  if (src->d_size > dest->d_size)
76
0
    {
77
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
78
0
      return NULL;
79
0
    }
80
81
  /* Test the encode parameter.  */
82
31.7k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
83
209
    {
84
209
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
85
209
      return NULL;
86
209
    }
87
88
  /* Determine the translation function to use.
89
90
     At this point we make an assumption which is valid for all
91
     existing implementations so far: the memory and file sizes are
92
     the same.  This has very important consequences:
93
     a) The requirement that the source and destination buffer can
94
  overlap can easily be fulfilled.
95
     b) We need only one function to convert from and memory to file
96
  and vice versa since the function only has to copy and/or
97
  change the byte order.
98
  */
99
31.5k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
100
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
101
27.2k
    {
102
      /* We simply have to copy since the byte order is the same.  */
103
27.2k
      if (src->d_buf != dest->d_buf)
104
18.8k
  memmove (dest->d_buf, src->d_buf, src->d_size);
105
27.2k
    }
106
4.29k
  else
107
4.29k
    {
108
4.29k
      xfct_t fctp;
109
4.29k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
110
111
      /* Do the real work.  */
112
4.29k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
113
4.29k
    }
114
115
  /* Now set the real destination type and length since the operation was
116
     successful.  */
117
31.5k
  dest->d_type = src->d_type;
118
31.5k
  dest->d_size = src->d_size;
119
120
31.5k
  return dest;
121
31.7k
}
122
INTDEF(elfw2(LIBELFBITS, xlatetom))