Branch data Line data Source code
1 : : /* Convert from memory to file representation.
2 : : Copyright (C) 1998, 1999, 2000, 2002 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 <endian.h>
36 : : #include <string.h>
37 : :
38 : : #include "libelfP.h"
39 : :
40 : : #ifndef LIBELFBITS
41 : : # define LIBELFBITS 32
42 : : #endif
43 : :
44 : :
45 : : Elf_Data *
46 : 46557 : elfw2(LIBELFBITS, xlatetof) (dest, src, encode)
47 : : Elf_Data *dest;
48 : : const Elf_Data *src;
49 : : unsigned int encode;
50 : : {
51 : : /* First test whether the input data is really suitable for this
52 : : type. This means, whether there is an integer number of records.
53 : : Note that for this implementation the memory and file size of the
54 : : data types are identical. */
55 : : #if EV_NUM != 2
56 : : size_t recsize = __libelf_type_sizes[src->d_version - 1][ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
57 : : #else
58 : 46557 : size_t recsize = __libelf_type_sizes[0][ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
59 : : #endif
60 : :
61 [ - + ]: 46557 : if (src->d_size % recsize != 0)
62 : : {
63 : 0 : __libelf_seterrno (ELF_E_INVALID_DATA);
64 : 0 : return NULL;
65 : : }
66 : :
67 : : /* Next see whether the converted data fits in the output buffer. */
68 [ - + ]: 46557 : if (src->d_size > dest->d_size)
69 : : {
70 : 0 : __libelf_seterrno (ELF_E_DEST_SIZE);
71 : 0 : return NULL;
72 : : }
73 : :
74 : : /* Test the encode parameter. */
75 [ - + ]: 46557 : if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
76 : : {
77 : 0 : __libelf_seterrno (ELF_E_INVALID_ENCODING);
78 : 0 : return NULL;
79 : : }
80 : :
81 : : /* Determine the translation function to use.
82 : :
83 : : At this point we make an assumption which is valid for all
84 : : existing implementations so far: the memory and file sizes are
85 : : the same. This has very important consequences:
86 : : a) The requirement that the source and destination buffer can
87 : : overlap can easily be fulfilled.
88 : : b) We need only one function to convert from and memory to file
89 : : and vice versa since the function only has to copy and/or
90 : : change the byte order.
91 : : */
92 [ + + ]: 46557 : if ((__BYTE_ORDER == __LITTLE_ENDIAN && encode == ELFDATA2LSB)
93 : : || (__BYTE_ORDER == __BIG_ENDIAN && encode == ELFDATA2MSB))
94 : : {
95 : : /* We simply have to copy since the byte order is the same. */
96 [ + - ]: 25552 : if (src->d_buf != dest->d_buf)
97 : 25552 : memmove (dest->d_buf, src->d_buf, src->d_size);
98 : : }
99 : : else
100 : : {
101 : : xfct_t fctp;
102 : :
103 : : /* Get a pointer to the transformation functions. The `#ifdef' is
104 : : a small optimization since we don't anticipate another ELF
105 : : version and so would waste "precious" code. */
106 : : #if EV_NUM != 2
107 : : fctp = __elf_xfctstom[dest->d_version - 1][src->d_version - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
108 : : #else
109 : 21005 : fctp = __elf_xfctstom[0][0][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
110 : : #endif
111 : :
112 : : /* Do the real work. */
113 : 21005 : (*fctp) (dest->d_buf, src->d_buf, src->d_size, 1);
114 : : }
115 : :
116 : : /* Now set the real destination type and length since the operation was
117 : : successful. */
118 : 46557 : dest->d_type = src->d_type;
119 : 46557 : dest->d_size = src->d_size;
120 : :
121 : 46557 : return dest;
122 : : }
123 : : INTDEF(elfw2(LIBELFBITS, xlatetof))
|