Coverage Report

Created: 2025-08-29 06:11

/src/elfutils/libelf/elf32_xlatetom.c
Line
Count
Source (jump to first uncovered line)
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
252k
{
48
252k
  if (src == NULL || dest == NULL)
49
0
    return NULL;
50
51
252k
  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
252k
  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
252k
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
68
252k
      && src->d_size % recsize != 0)
69
31
    {
70
31
      __libelf_seterrno (ELF_E_INVALID_DATA);
71
31
      return NULL;
72
31
    }
73
74
  /* Next see whether the converted data fits in the output buffer.  */
75
252k
  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
252k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
83
415
    {
84
415
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
85
415
      return NULL;
86
415
    }
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
251k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
100
251k
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
101
79.1k
    {
102
      /* We simply have to copy since the byte order is the same.  */
103
79.1k
      if (src->d_buf != dest->d_buf)
104
37.4k
  memmove (dest->d_buf, src->d_buf, src->d_size);
105
79.1k
    }
106
172k
  else
107
172k
    {
108
172k
      xfct_t fctp;
109
172k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
110
111
      /* Do the real work.  */
112
172k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
113
172k
    }
114
115
  /* Now set the real destination type and length since the operation was
116
     successful.  */
117
251k
  dest->d_type = src->d_type;
118
251k
  dest->d_size = src->d_size;
119
120
251k
  return dest;
121
252k
}
elf32_xlatetom
Line
Count
Source
47
225k
{
48
225k
  if (src == NULL || dest == NULL)
49
0
    return NULL;
50
51
225k
  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
225k
  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
225k
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
68
225k
      && src->d_size % recsize != 0)
69
14
    {
70
14
      __libelf_seterrno (ELF_E_INVALID_DATA);
71
14
      return NULL;
72
14
    }
73
74
  /* Next see whether the converted data fits in the output buffer.  */
75
225k
  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
225k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
83
210
    {
84
210
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
85
210
      return NULL;
86
210
    }
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
225k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
100
225k
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
101
58.9k
    {
102
      /* We simply have to copy since the byte order is the same.  */
103
58.9k
      if (src->d_buf != dest->d_buf)
104
25.3k
  memmove (dest->d_buf, src->d_buf, src->d_size);
105
58.9k
    }
106
166k
  else
107
166k
    {
108
166k
      xfct_t fctp;
109
166k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
110
111
      /* Do the real work.  */
112
166k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
113
166k
    }
114
115
  /* Now set the real destination type and length since the operation was
116
     successful.  */
117
225k
  dest->d_type = src->d_type;
118
225k
  dest->d_size = src->d_size;
119
120
225k
  return dest;
121
225k
}
elf64_xlatetom
Line
Count
Source
47
26.1k
{
48
26.1k
  if (src == NULL || dest == NULL)
49
0
    return NULL;
50
51
26.1k
  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
26.1k
  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
26.1k
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
68
26.1k
      && src->d_size % recsize != 0)
69
17
    {
70
17
      __libelf_seterrno (ELF_E_INVALID_DATA);
71
17
      return NULL;
72
17
    }
73
74
  /* Next see whether the converted data fits in the output buffer.  */
75
26.1k
  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
26.1k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
83
205
    {
84
205
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
85
205
      return NULL;
86
205
    }
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
25.9k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
100
25.9k
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
101
20.1k
    {
102
      /* We simply have to copy since the byte order is the same.  */
103
20.1k
      if (src->d_buf != dest->d_buf)
104
12.0k
  memmove (dest->d_buf, src->d_buf, src->d_size);
105
20.1k
    }
106
5.79k
  else
107
5.79k
    {
108
5.79k
      xfct_t fctp;
109
5.79k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
110
111
      /* Do the real work.  */
112
5.79k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
113
5.79k
    }
114
115
  /* Now set the real destination type and length since the operation was
116
     successful.  */
117
25.9k
  dest->d_type = src->d_type;
118
25.9k
  dest->d_size = src->d_size;
119
120
25.9k
  return dest;
121
26.1k
}
122
INTDEF(elfw2(LIBELFBITS, xlatetom))