Coverage Report

Created: 2026-09-06 06:52

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
31.8k
{
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
31.8k
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
53
54
55
  /* We shouldn't require integer number of records when processing
56
     notes.  Payload bytes follow the header immediately, it's not an
57
     array of records as is the case otherwise.  */
58
31.8k
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
59
28.9k
      && src->d_size % recsize != 0)
60
21
    {
61
21
      __libelf_seterrno (ELF_E_INVALID_DATA);
62
21
      return NULL;
63
21
    }
64
65
  /* Next see whether the converted data fits in the output buffer.  */
66
31.8k
  if (src->d_size > dest->d_size)
67
0
    {
68
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
69
0
      return NULL;
70
0
    }
71
72
  /* Test the encode parameter.  */
73
31.8k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
74
434
    {
75
434
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
76
434
      return NULL;
77
434
    }
78
79
  /* Determine the translation function to use.
80
81
     At this point we make an assumption which is valid for all
82
     existing implementations so far: the memory and file sizes are
83
     the same.  This has very important consequences:
84
     a) The requirement that the source and destination buffer can
85
  overlap can easily be fulfilled.
86
     b) We need only one function to convert from and memory to file
87
  and vice versa since the function only has to copy and/or
88
  change the byte order.
89
  */
90
31.4k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
91
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
92
17.6k
    {
93
      /* We simply have to copy since the byte order is the same.  */
94
17.6k
      if (src->d_buf != dest->d_buf)
95
11.4k
  memmove (dest->d_buf, src->d_buf, src->d_size);
96
17.6k
    }
97
13.8k
  else
98
13.8k
    {
99
13.8k
      xfct_t fctp;
100
13.8k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
101
102
      /* Do the real work.  */
103
13.8k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
104
13.8k
    }
105
106
  /* Now set the real destination type and length since the operation was
107
     successful.  */
108
31.4k
  dest->d_type = src->d_type;
109
31.4k
  dest->d_size = src->d_size;
110
111
31.4k
  return dest;
112
31.8k
}
elf64_xlatetom
Line
Count
Source
47
10.5k
{
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.5k
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
53
54
55
  /* We shouldn't require integer number of records when processing
56
     notes.  Payload bytes follow the header immediately, it's not an
57
     array of records as is the case otherwise.  */
58
10.5k
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
59
10.5k
      && src->d_size % recsize != 0)
60
16
    {
61
16
      __libelf_seterrno (ELF_E_INVALID_DATA);
62
16
      return NULL;
63
16
    }
64
65
  /* Next see whether the converted data fits in the output buffer.  */
66
10.5k
  if (src->d_size > dest->d_size)
67
0
    {
68
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
69
0
      return NULL;
70
0
    }
71
72
  /* Test the encode parameter.  */
73
10.5k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
74
206
    {
75
206
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
76
206
      return NULL;
77
206
    }
78
79
  /* Determine the translation function to use.
80
81
     At this point we make an assumption which is valid for all
82
     existing implementations so far: the memory and file sizes are
83
     the same.  This has very important consequences:
84
     a) The requirement that the source and destination buffer can
85
  overlap can easily be fulfilled.
86
     b) We need only one function to convert from and memory to file
87
  and vice versa since the function only has to copy and/or
88
  change the byte order.
89
  */
90
10.3k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
91
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
92
8.78k
    {
93
      /* We simply have to copy since the byte order is the same.  */
94
8.78k
      if (src->d_buf != dest->d_buf)
95
5.94k
  memmove (dest->d_buf, src->d_buf, src->d_size);
96
8.78k
    }
97
1.56k
  else
98
1.56k
    {
99
1.56k
      xfct_t fctp;
100
1.56k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
101
102
      /* Do the real work.  */
103
1.56k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
104
1.56k
    }
105
106
  /* Now set the real destination type and length since the operation was
107
     successful.  */
108
10.3k
  dest->d_type = src->d_type;
109
10.3k
  dest->d_size = src->d_size;
110
111
10.3k
  return dest;
112
10.5k
}
elf32_xlatetom
Line
Count
Source
47
21.2k
{
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
21.2k
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
53
54
55
  /* We shouldn't require integer number of records when processing
56
     notes.  Payload bytes follow the header immediately, it's not an
57
     array of records as is the case otherwise.  */
58
21.2k
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
59
18.3k
      && src->d_size % recsize != 0)
60
5
    {
61
5
      __libelf_seterrno (ELF_E_INVALID_DATA);
62
5
      return NULL;
63
5
    }
64
65
  /* Next see whether the converted data fits in the output buffer.  */
66
21.2k
  if (src->d_size > dest->d_size)
67
0
    {
68
0
      __libelf_seterrno (ELF_E_DEST_SIZE);
69
0
      return NULL;
70
0
    }
71
72
  /* Test the encode parameter.  */
73
21.2k
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
74
228
    {
75
228
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
76
228
      return NULL;
77
228
    }
78
79
  /* Determine the translation function to use.
80
81
     At this point we make an assumption which is valid for all
82
     existing implementations so far: the memory and file sizes are
83
     the same.  This has very important consequences:
84
     a) The requirement that the source and destination buffer can
85
  overlap can easily be fulfilled.
86
     b) We need only one function to convert from and memory to file
87
  and vice versa since the function only has to copy and/or
88
  change the byte order.
89
  */
90
21.0k
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
91
0
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
92
8.81k
    {
93
      /* We simply have to copy since the byte order is the same.  */
94
8.81k
      if (src->d_buf != dest->d_buf)
95
5.45k
  memmove (dest->d_buf, src->d_buf, src->d_size);
96
8.81k
    }
97
12.2k
  else
98
12.2k
    {
99
12.2k
      xfct_t fctp;
100
12.2k
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
101
102
      /* Do the real work.  */
103
12.2k
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
104
12.2k
    }
105
106
  /* Now set the real destination type and length since the operation was
107
     successful.  */
108
21.0k
  dest->d_type = src->d_type;
109
21.0k
  dest->d_size = src->d_size;
110
111
21.0k
  return dest;
112
21.2k
}
113
INTDEF(elfw2(LIBELFBITS, xlatetom))