Coverage Report

Created: 2026-11-22 07:03

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
10.7k
{
48
  /* First test whether the input data is really suitable for this
49
     type.  This means, whether there is an integer number of records.
50
     Note that for this implementation the memory and file size of the
51
     data types are identical.  */
52
10.7k
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
53
54
10.7k
  if (src->d_size % recsize != 0)
55
4.00k
    {
56
4.00k
      __libelf_seterrno (ELF_E_INVALID_DATA);
57
4.00k
      return NULL;
58
4.00k
    }
59
60
  /* Next see whether the converted data fits in the output buffer.  */
61
6.70k
  if (src->d_size > dest->d_size)
62
0
    {
63
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
64
0
      return NULL;
65
0
    }
66
67
  /* Test the encode parameter.  */
68
6.70k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
69
0
    {
70
0
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
71
0
      return NULL;
72
0
    }
73
74
  /* Determine the translation function to use.
75
76
     At this point we make an assumption which is valid for all
77
     existing implementations so far: the memory and file sizes are
78
     the same.  This has very important consequences:
79
     a) The requirement that the source and destination buffer can
80
  overlap can easily be fulfilled.
81
     b) We need only one function to convert from and memory to file
82
  and vice versa since the function only has to copy and/or
83
  change the byte order.
84
  */
85
6.70k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
86
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
87
0
    {
88
      /* We simply have to copy since the byte order is the same.  */
89
0
      if (src->d_buf != dest->d_buf)
90
0
  memmove (dest->d_buf, src->d_buf, src->d_size);
91
0
    }
92
6.70k
  else
93
6.70k
    {
94
6.70k
      xfct_t fctp;
95
6.70k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
96
97
      /* Do the real work.  */
98
6.70k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 1);
99
6.70k
    }
100
101
  /* Now set the real destination type and length since the operation was
102
     successful.  */
103
6.70k
  dest->d_type = src->d_type;
104
6.70k
  dest->d_size = src->d_size;
105
106
6.70k
  return dest;
107
6.70k
}
elf32_xlatetof
Line
Count
Source
47
9.96k
{
48
  /* First test whether the input data is really suitable for this
49
     type.  This means, whether there is an integer number of records.
50
     Note that for this implementation the memory and file size of the
51
     data types are identical.  */
52
9.96k
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
53
54
9.96k
  if (src->d_size % recsize != 0)
55
3.43k
    {
56
3.43k
      __libelf_seterrno (ELF_E_INVALID_DATA);
57
3.43k
      return NULL;
58
3.43k
    }
59
60
  /* Next see whether the converted data fits in the output buffer.  */
61
6.53k
  if (src->d_size > dest->d_size)
62
0
    {
63
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
64
0
      return NULL;
65
0
    }
66
67
  /* Test the encode parameter.  */
68
6.53k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
69
0
    {
70
0
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
71
0
      return NULL;
72
0
    }
73
74
  /* Determine the translation function to use.
75
76
     At this point we make an assumption which is valid for all
77
     existing implementations so far: the memory and file sizes are
78
     the same.  This has very important consequences:
79
     a) The requirement that the source and destination buffer can
80
  overlap can easily be fulfilled.
81
     b) We need only one function to convert from and memory to file
82
  and vice versa since the function only has to copy and/or
83
  change the byte order.
84
  */
85
6.53k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
86
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
87
0
    {
88
      /* We simply have to copy since the byte order is the same.  */
89
0
      if (src->d_buf != dest->d_buf)
90
0
  memmove (dest->d_buf, src->d_buf, src->d_size);
91
0
    }
92
6.53k
  else
93
6.53k
    {
94
6.53k
      xfct_t fctp;
95
6.53k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
96
97
      /* Do the real work.  */
98
6.53k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 1);
99
6.53k
    }
100
101
  /* Now set the real destination type and length since the operation was
102
     successful.  */
103
6.53k
  dest->d_type = src->d_type;
104
6.53k
  dest->d_size = src->d_size;
105
106
6.53k
  return dest;
107
6.53k
}
elf64_xlatetof
Line
Count
Source
47
751
{
48
  /* First test whether the input data is really suitable for this
49
     type.  This means, whether there is an integer number of records.
50
     Note that for this implementation the memory and file size of the
51
     data types are identical.  */
52
751
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
53
54
751
  if (src->d_size % recsize != 0)
55
576
    {
56
576
      __libelf_seterrno (ELF_E_INVALID_DATA);
57
576
      return NULL;
58
576
    }
59
60
  /* Next see whether the converted data fits in the output buffer.  */
61
175
  if (src->d_size > dest->d_size)
62
0
    {
63
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
64
0
      return NULL;
65
0
    }
66
67
  /* Test the encode parameter.  */
68
175
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
69
0
    {
70
0
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
71
0
      return NULL;
72
0
    }
73
74
  /* Determine the translation function to use.
75
76
     At this point we make an assumption which is valid for all
77
     existing implementations so far: the memory and file sizes are
78
     the same.  This has very important consequences:
79
     a) The requirement that the source and destination buffer can
80
  overlap can easily be fulfilled.
81
     b) We need only one function to convert from and memory to file
82
  and vice versa since the function only has to copy and/or
83
  change the byte order.
84
  */
85
175
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
86
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
87
0
    {
88
      /* We simply have to copy since the byte order is the same.  */
89
0
      if (src->d_buf != dest->d_buf)
90
0
  memmove (dest->d_buf, src->d_buf, src->d_size);
91
0
    }
92
175
  else
93
175
    {
94
175
      xfct_t fctp;
95
175
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
96
97
      /* Do the real work.  */
98
175
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 1);
99
175
    }
100
101
  /* Now set the real destination type and length since the operation was
102
     successful.  */
103
175
  dest->d_type = src->d_type;
104
175
  dest->d_size = src->d_size;
105
106
175
  return dest;
107
175
}
108
INTDEF(elfw2(LIBELFBITS, xlatetof))