xref: /aosp_15_r20/external/elfutils/libelf/elf32_xlatetom.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1*7304104dSAndroid Build Coastguard Worker /* Convert from file to memory representation.
2*7304104dSAndroid Build Coastguard Worker    Copyright (C) 1998, 1999, 2000, 2002, 2012, 2015 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker    This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker    Written by Ulrich Drepper <[email protected]>, 1998.
5*7304104dSAndroid Build Coastguard Worker 
6*7304104dSAndroid Build Coastguard Worker    This file is free software; you can redistribute it and/or modify
7*7304104dSAndroid Build Coastguard Worker    it under the terms of either
8*7304104dSAndroid Build Coastguard Worker 
9*7304104dSAndroid Build Coastguard Worker      * the GNU Lesser General Public License as published by the Free
10*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 3 of the License, or (at
11*7304104dSAndroid Build Coastguard Worker        your option) any later version
12*7304104dSAndroid Build Coastguard Worker 
13*7304104dSAndroid Build Coastguard Worker    or
14*7304104dSAndroid Build Coastguard Worker 
15*7304104dSAndroid Build Coastguard Worker      * the GNU General Public License as published by the Free
16*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 2 of the License, or (at
17*7304104dSAndroid Build Coastguard Worker        your option) any later version
18*7304104dSAndroid Build Coastguard Worker 
19*7304104dSAndroid Build Coastguard Worker    or both in parallel, as here.
20*7304104dSAndroid Build Coastguard Worker 
21*7304104dSAndroid Build Coastguard Worker    elfutils is distributed in the hope that it will be useful, but
22*7304104dSAndroid Build Coastguard Worker    WITHOUT ANY WARRANTY; without even the implied warranty of
23*7304104dSAndroid Build Coastguard Worker    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24*7304104dSAndroid Build Coastguard Worker    General Public License for more details.
25*7304104dSAndroid Build Coastguard Worker 
26*7304104dSAndroid Build Coastguard Worker    You should have received copies of the GNU General Public License and
27*7304104dSAndroid Build Coastguard Worker    the GNU Lesser General Public License along with this program.  If
28*7304104dSAndroid Build Coastguard Worker    not, see <http://www.gnu.org/licenses/>.  */
29*7304104dSAndroid Build Coastguard Worker 
30*7304104dSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
31*7304104dSAndroid Build Coastguard Worker # include <config.h>
32*7304104dSAndroid Build Coastguard Worker #endif
33*7304104dSAndroid Build Coastguard Worker 
34*7304104dSAndroid Build Coastguard Worker #include <assert.h>
35*7304104dSAndroid Build Coastguard Worker #include <string.h>
36*7304104dSAndroid Build Coastguard Worker 
37*7304104dSAndroid Build Coastguard Worker #include "libelfP.h"
38*7304104dSAndroid Build Coastguard Worker 
39*7304104dSAndroid Build Coastguard Worker #ifndef LIBELFBITS
40*7304104dSAndroid Build Coastguard Worker # define LIBELFBITS	32
41*7304104dSAndroid Build Coastguard Worker #endif
42*7304104dSAndroid Build Coastguard Worker 
43*7304104dSAndroid Build Coastguard Worker 
44*7304104dSAndroid Build Coastguard Worker Elf_Data *
elfw2(LIBELFBITS,xlatetom)45*7304104dSAndroid Build Coastguard Worker elfw2(LIBELFBITS, xlatetom) (Elf_Data *dest, const Elf_Data *src,
46*7304104dSAndroid Build Coastguard Worker 			     unsigned int encode)
47*7304104dSAndroid Build Coastguard Worker {
48*7304104dSAndroid Build Coastguard Worker   /* First test whether the input data is really suitable for this
49*7304104dSAndroid Build Coastguard Worker      type.  This means, whether there is an integer number of records.
50*7304104dSAndroid Build Coastguard Worker      Note that for this implementation the memory and file size of the
51*7304104dSAndroid Build Coastguard Worker      data types are identical.  */
52*7304104dSAndroid Build Coastguard Worker   size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
53*7304104dSAndroid Build Coastguard Worker 
54*7304104dSAndroid Build Coastguard Worker 
55*7304104dSAndroid Build Coastguard Worker   /* We shouldn't require integer number of records when processing
56*7304104dSAndroid Build Coastguard Worker      notes.  Payload bytes follow the header immediately, it's not an
57*7304104dSAndroid Build Coastguard Worker      array of records as is the case otherwise.  */
58*7304104dSAndroid Build Coastguard Worker   if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
59*7304104dSAndroid Build Coastguard Worker       && src->d_size % recsize != 0)
60*7304104dSAndroid Build Coastguard Worker     {
61*7304104dSAndroid Build Coastguard Worker       __libelf_seterrno (ELF_E_INVALID_DATA);
62*7304104dSAndroid Build Coastguard Worker       return NULL;
63*7304104dSAndroid Build Coastguard Worker     }
64*7304104dSAndroid Build Coastguard Worker 
65*7304104dSAndroid Build Coastguard Worker   /* Next see whether the converted data fits in the output buffer.  */
66*7304104dSAndroid Build Coastguard Worker   if (src->d_size > dest->d_size)
67*7304104dSAndroid Build Coastguard Worker     {
68*7304104dSAndroid Build Coastguard Worker       __libelf_seterrno (ELF_E_DEST_SIZE);
69*7304104dSAndroid Build Coastguard Worker       return NULL;
70*7304104dSAndroid Build Coastguard Worker     }
71*7304104dSAndroid Build Coastguard Worker 
72*7304104dSAndroid Build Coastguard Worker   /* Test the encode parameter.  */
73*7304104dSAndroid Build Coastguard Worker   if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
74*7304104dSAndroid Build Coastguard Worker     {
75*7304104dSAndroid Build Coastguard Worker       __libelf_seterrno (ELF_E_INVALID_ENCODING);
76*7304104dSAndroid Build Coastguard Worker       return NULL;
77*7304104dSAndroid Build Coastguard Worker     }
78*7304104dSAndroid Build Coastguard Worker 
79*7304104dSAndroid Build Coastguard Worker   /* Determine the translation function to use.
80*7304104dSAndroid Build Coastguard Worker 
81*7304104dSAndroid Build Coastguard Worker      At this point we make an assumption which is valid for all
82*7304104dSAndroid Build Coastguard Worker      existing implementations so far: the memory and file sizes are
83*7304104dSAndroid Build Coastguard Worker      the same.  This has very important consequences:
84*7304104dSAndroid Build Coastguard Worker      a) The requirement that the source and destination buffer can
85*7304104dSAndroid Build Coastguard Worker 	overlap can easily be fulfilled.
86*7304104dSAndroid Build Coastguard Worker      b) We need only one function to convert from and memory to file
87*7304104dSAndroid Build Coastguard Worker 	and vice versa since the function only has to copy and/or
88*7304104dSAndroid Build Coastguard Worker 	change the byte order.
89*7304104dSAndroid Build Coastguard Worker   */
90*7304104dSAndroid Build Coastguard Worker   if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
91*7304104dSAndroid Build Coastguard Worker       || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
92*7304104dSAndroid Build Coastguard Worker     {
93*7304104dSAndroid Build Coastguard Worker       /* We simply have to copy since the byte order is the same.  */
94*7304104dSAndroid Build Coastguard Worker       if (src->d_buf != dest->d_buf)
95*7304104dSAndroid Build Coastguard Worker 	memmove (dest->d_buf, src->d_buf, src->d_size);
96*7304104dSAndroid Build Coastguard Worker     }
97*7304104dSAndroid Build Coastguard Worker   else
98*7304104dSAndroid Build Coastguard Worker     {
99*7304104dSAndroid Build Coastguard Worker       xfct_t fctp;
100*7304104dSAndroid Build Coastguard Worker       fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
101*7304104dSAndroid Build Coastguard Worker 
102*7304104dSAndroid Build Coastguard Worker       /* Do the real work.  */
103*7304104dSAndroid Build Coastguard Worker       (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
104*7304104dSAndroid Build Coastguard Worker     }
105*7304104dSAndroid Build Coastguard Worker 
106*7304104dSAndroid Build Coastguard Worker   /* Now set the real destination type and length since the operation was
107*7304104dSAndroid Build Coastguard Worker      successful.  */
108*7304104dSAndroid Build Coastguard Worker   dest->d_type = src->d_type;
109*7304104dSAndroid Build Coastguard Worker   dest->d_size = src->d_size;
110*7304104dSAndroid Build Coastguard Worker 
111*7304104dSAndroid Build Coastguard Worker   return dest;
112*7304104dSAndroid Build Coastguard Worker }
113*7304104dSAndroid Build Coastguard Worker INTDEF(elfw2(LIBELFBITS, xlatetom))
114