Coverage Report

Created: 2026-01-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/libelf/elf32_xlatetof.c
Line
Count
Source
1
/* Convert from memory to file representation.
2
   Copyright (C) 1998, 1999, 2000, 2002, 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, xlatetof) (Elf_Data *dest, const Elf_Data *src,
46
           unsigned int encode)
47
12.7k
{
48
12.7k
  if (src == NULL || dest == NULL)
49
0
    return NULL;
50
51
12.7k
  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
12.7k
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
62
63
  /* We shouldn't require integer number of records when processing
64
     notes.  Payload bytes follow the header immediately, it's not an
65
     array of records as is the case otherwise.  */
66
12.7k
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
67
12.7k
      && src->d_size % recsize != 0)
68
5.54k
    {
69
5.54k
      __libelf_seterrno (ELF_E_INVALID_DATA);
70
5.54k
      return NULL;
71
5.54k
    }
72
73
  /* Next see whether the converted data fits in the output buffer.  */
74
7.22k
  if (src->d_size > dest->d_size)
75
0
    {
76
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
77
0
      return NULL;
78
0
    }
79
80
  /* Test the encode parameter.  */
81
7.22k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
82
0
    {
83
0
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
84
0
      return NULL;
85
0
    }
86
87
  /* Determine the translation function to use.
88
89
     At this point we make an assumption which is valid for all
90
     existing implementations so far: the memory and file sizes are
91
     the same.  This has very important consequences:
92
     a) The requirement that the source and destination buffer can
93
  overlap can easily be fulfilled.
94
     b) We need only one function to convert from and memory to file
95
  and vice versa since the function only has to copy and/or
96
  change the byte order.
97
  */
98
7.22k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
99
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
100
0
    {
101
      /* We simply have to copy since the byte order is the same.  */
102
0
      if (src->d_buf != dest->d_buf)
103
0
  memmove (dest->d_buf, src->d_buf, src->d_size);
104
0
    }
105
7.22k
  else
106
7.22k
    {
107
7.22k
      xfct_t fctp;
108
7.22k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
109
110
      /* Do the real work.  */
111
7.22k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 1);
112
7.22k
    }
113
114
  /* Now set the real destination type and length since the operation was
115
     successful.  */
116
7.22k
  dest->d_type = src->d_type;
117
7.22k
  dest->d_size = src->d_size;
118
119
7.22k
  return dest;
120
7.22k
}
elf32_xlatetof
Line
Count
Source
47
11.9k
{
48
11.9k
  if (src == NULL || dest == NULL)
49
0
    return NULL;
50
51
11.9k
  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
11.9k
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
62
63
  /* We shouldn't require integer number of records when processing
64
     notes.  Payload bytes follow the header immediately, it's not an
65
     array of records as is the case otherwise.  */
66
11.9k
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
67
11.9k
      && src->d_size % recsize != 0)
68
4.90k
    {
69
4.90k
      __libelf_seterrno (ELF_E_INVALID_DATA);
70
4.90k
      return NULL;
71
4.90k
    }
72
73
  /* Next see whether the converted data fits in the output buffer.  */
74
7.01k
  if (src->d_size > dest->d_size)
75
0
    {
76
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
77
0
      return NULL;
78
0
    }
79
80
  /* Test the encode parameter.  */
81
7.01k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
82
0
    {
83
0
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
84
0
      return NULL;
85
0
    }
86
87
  /* Determine the translation function to use.
88
89
     At this point we make an assumption which is valid for all
90
     existing implementations so far: the memory and file sizes are
91
     the same.  This has very important consequences:
92
     a) The requirement that the source and destination buffer can
93
  overlap can easily be fulfilled.
94
     b) We need only one function to convert from and memory to file
95
  and vice versa since the function only has to copy and/or
96
  change the byte order.
97
  */
98
7.01k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
99
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
100
0
    {
101
      /* We simply have to copy since the byte order is the same.  */
102
0
      if (src->d_buf != dest->d_buf)
103
0
  memmove (dest->d_buf, src->d_buf, src->d_size);
104
0
    }
105
7.01k
  else
106
7.01k
    {
107
7.01k
      xfct_t fctp;
108
7.01k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
109
110
      /* Do the real work.  */
111
7.01k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 1);
112
7.01k
    }
113
114
  /* Now set the real destination type and length since the operation was
115
     successful.  */
116
7.01k
  dest->d_type = src->d_type;
117
7.01k
  dest->d_size = src->d_size;
118
119
7.01k
  return dest;
120
7.01k
}
elf64_xlatetof
Line
Count
Source
47
853
{
48
853
  if (src == NULL || dest == NULL)
49
0
    return NULL;
50
51
853
  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
853
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
62
63
  /* We shouldn't require integer number of records when processing
64
     notes.  Payload bytes follow the header immediately, it's not an
65
     array of records as is the case otherwise.  */
66
853
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
67
853
      && src->d_size % recsize != 0)
68
640
    {
69
640
      __libelf_seterrno (ELF_E_INVALID_DATA);
70
640
      return NULL;
71
640
    }
72
73
  /* Next see whether the converted data fits in the output buffer.  */
74
213
  if (src->d_size > dest->d_size)
75
0
    {
76
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
77
0
      return NULL;
78
0
    }
79
80
  /* Test the encode parameter.  */
81
213
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
82
0
    {
83
0
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
84
0
      return NULL;
85
0
    }
86
87
  /* Determine the translation function to use.
88
89
     At this point we make an assumption which is valid for all
90
     existing implementations so far: the memory and file sizes are
91
     the same.  This has very important consequences:
92
     a) The requirement that the source and destination buffer can
93
  overlap can easily be fulfilled.
94
     b) We need only one function to convert from and memory to file
95
  and vice versa since the function only has to copy and/or
96
  change the byte order.
97
  */
98
213
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
99
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
100
0
    {
101
      /* We simply have to copy since the byte order is the same.  */
102
0
      if (src->d_buf != dest->d_buf)
103
0
  memmove (dest->d_buf, src->d_buf, src->d_size);
104
0
    }
105
213
  else
106
213
    {
107
213
      xfct_t fctp;
108
213
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
109
110
      /* Do the real work.  */
111
213
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 1);
112
213
    }
113
114
  /* Now set the real destination type and length since the operation was
115
     successful.  */
116
213
  dest->d_type = src->d_type;
117
213
  dest->d_size = src->d_size;
118
119
213
  return dest;
120
213
}
121
INTDEF(elfw2(LIBELFBITS, xlatetof))