• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Convert from memory to file 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, xlatetof) (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   if (src->d_size % recsize != 0)
50     {
51       __libelf_seterrno (ELF_E_INVALID_DATA);
52       return NULL;
53     }
54 
55   /* Next see whether the converted data fits in the output buffer.  */
56   if (src->d_size > dest->d_size)
57     {
58       __libelf_seterrno (ELF_E_DEST_SIZE);
59       return NULL;
60     }
61 
62   /* Test the encode parameter.  */
63   if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
64     {
65       __libelf_seterrno (ELF_E_INVALID_ENCODING);
66       return NULL;
67     }
68 
69   /* Determine the translation function to use.
70 
71      At this point we make an assumption which is valid for all
72      existing implementations so far: the memory and file sizes are
73      the same.  This has very important consequences:
74      a) The requirement that the source and destination buffer can
75 	overlap can easily be fulfilled.
76      b) We need only one function to convert from and memory to file
77 	and vice versa since the function only has to copy and/or
78 	change the byte order.
79   */
80   if ((__BYTE_ORDER == __LITTLE_ENDIAN && encode == ELFDATA2LSB)
81       || (__BYTE_ORDER == __BIG_ENDIAN && encode == ELFDATA2MSB))
82     {
83       /* We simply have to copy since the byte order is the same.  */
84       if (src->d_buf != dest->d_buf)
85 	memmove (dest->d_buf, src->d_buf, src->d_size);
86     }
87   else
88     {
89       xfct_t fctp;
90 
91       /* Get a pointer to the transformation functions.  The `#ifdef' is
92 	 a small optimization since we don't anticipate another ELF
93 	 version and so would waste "precious" code.  */
94 #if EV_NUM != 2
95       fctp = __elf_xfctstom[dest->d_version - 1][src->d_version - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
96 #else
97       fctp = __elf_xfctstom[0][0][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
98 #endif
99 
100       /* Do the real work.  */
101       (*fctp) (dest->d_buf, src->d_buf, src->d_size, 1);
102     }
103 
104   /* Now set the real destination type and length since the operation was
105      successful.  */
106   dest->d_type = src->d_type;
107   dest->d_size = src->d_size;
108 
109   return dest;
110 }
111 INTDEF(elfw2(LIBELFBITS, xlatetof))
112