• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Convert from file to memory representation.
2    Copyright (C) 1998, 1999, 2000, 2002 Red Hat, Inc.
3    Written by Ulrich Drepper <drepper@redhat.com>, 1998.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, version 2.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17 
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include <assert.h>
23 //#include <endian.h>
24 #include <string.h>
25 
26 #include "libelfP.h"
27 
28 #ifndef LIBELFBITS
29 # define LIBELFBITS	32
30 #endif
31 
32 
33 Elf_Data *
34 elfw2(LIBELFBITS, xlatetom) (dest, src, encode)
35      Elf_Data *dest;
36      const Elf_Data *src;
37      unsigned int encode;
38 {
39   /* First test whether the input data is really suitable for this
40      type.  This means, whether there is an integer number of records.
41      Note that for this implementation the memory and file size of the
42      data types are identical.  */
43 #if EV_NUM != 2
44   size_t recsize = __libelf_type_sizes[src->d_version - 1][ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
45 #else
46   size_t recsize = __libelf_type_sizes[0][ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
47 #endif
48 
49 
50   if (src->d_size % recsize != 0)
51     {
52       __libelf_seterrno (ELF_E_INVALID_DATA);
53       return NULL;
54     }
55 
56   /* Next see whether the converted data fits in the output buffer.  */
57   if (src->d_size > dest->d_size)
58     {
59       __libelf_seterrno (ELF_E_DEST_SIZE);
60       return NULL;
61     }
62 
63   /* Test the encode parameter.  */
64   if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
65     {
66       __libelf_seterrno (ELF_E_INVALID_ENCODING);
67       return NULL;
68     }
69 
70   /* Determine the translation function to use.
71 
72      At this point we make an assumption which is valid for all
73      existing implementations so far: the memory and file sizes are
74      the same.  This has very important consequences:
75      a) The requirement that the source and destination buffer can
76 	overlap can easily be fulfilled.
77      b) We need only one function to convert from and memory to file
78 	and vice versa since the function only has to copy and/or
79 	change the byte order.
80   */
81   if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
82       || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
83     {
84       /* We simply have to copy since the byte order is the same.  */
85       if (src->d_buf != dest->d_buf)
86 	memmove (dest->d_buf, src->d_buf, src->d_size);
87     }
88   else
89     {
90       xfct_t fctp;
91 
92       /* Get a pointer to the transformation functions.  The `#ifdef' is
93 	 a small optimization since we don't anticipate another ELF
94 	 version and so would waste "precious" code.  */
95 #if EV_NUM != 2
96       fctp = __elf_xfctstom[src->d_version - 1][dest->d_version - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
97 #else
98       fctp = __elf_xfctstom[0][0][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
99 #endif
100 
101       /* Do the real work.  */
102       (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
103     }
104 
105   /* Now set the real destination type and length since the operation was
106      successful.  */
107   dest->d_type = src->d_type;
108   dest->d_size = src->d_size;
109 
110   return dest;
111 }
112 INTDEF(elfw2(LIBELFBITS, xlatetom))
113