1*f8fb3368SJohn Marino /*-
2*f8fb3368SJohn Marino * Copyright (c) 2006,2008 Joseph Koshy
3*f8fb3368SJohn Marino * All rights reserved.
4*f8fb3368SJohn Marino *
5*f8fb3368SJohn Marino * Redistribution and use in source and binary forms, with or without
6*f8fb3368SJohn Marino * modification, are permitted provided that the following conditions
7*f8fb3368SJohn Marino * are met:
8*f8fb3368SJohn Marino * 1. Redistributions of source code must retain the above copyright
9*f8fb3368SJohn Marino * notice, this list of conditions and the following disclaimer.
10*f8fb3368SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
11*f8fb3368SJohn Marino * notice, this list of conditions and the following disclaimer in the
12*f8fb3368SJohn Marino * documentation and/or other materials provided with the distribution.
13*f8fb3368SJohn Marino *
14*f8fb3368SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*f8fb3368SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*f8fb3368SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*f8fb3368SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*f8fb3368SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*f8fb3368SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*f8fb3368SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*f8fb3368SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*f8fb3368SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*f8fb3368SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*f8fb3368SJohn Marino * SUCH DAMAGE.
25*f8fb3368SJohn Marino */
26*f8fb3368SJohn Marino
27*f8fb3368SJohn Marino #include <assert.h>
28*f8fb3368SJohn Marino #include <libelf.h>
29*f8fb3368SJohn Marino
30*f8fb3368SJohn Marino #include "_libelf.h"
31*f8fb3368SJohn Marino
32*f8fb3368SJohn Marino ELFTC_VCSID("$Id: libelf_xlate.c 3174 2015-03-27 17:13:41Z emaste $");
33*f8fb3368SJohn Marino
34*f8fb3368SJohn Marino /*
35*f8fb3368SJohn Marino * Translate to/from the file representation of ELF objects.
36*f8fb3368SJohn Marino *
37*f8fb3368SJohn Marino * Translation could potentially involve the following
38*f8fb3368SJohn Marino * transformations:
39*f8fb3368SJohn Marino *
40*f8fb3368SJohn Marino * - an endianness conversion,
41*f8fb3368SJohn Marino * - a change of layout, as the file representation of ELF objects
42*f8fb3368SJohn Marino * can differ from their in-memory representation.
43*f8fb3368SJohn Marino * - a change in representation due to a layout version change.
44*f8fb3368SJohn Marino */
45*f8fb3368SJohn Marino
46*f8fb3368SJohn Marino Elf_Data *
_libelf_xlate(Elf_Data * dst,const Elf_Data * src,unsigned int encoding,int elfclass,int direction)47*f8fb3368SJohn Marino _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding,
48*f8fb3368SJohn Marino int elfclass, int direction)
49*f8fb3368SJohn Marino {
50*f8fb3368SJohn Marino int byteswap;
51*f8fb3368SJohn Marino size_t cnt, dsz, fsz, msz;
52*f8fb3368SJohn Marino uintptr_t sb, se, db, de;
53*f8fb3368SJohn Marino
54*f8fb3368SJohn Marino if (encoding == ELFDATANONE)
55*f8fb3368SJohn Marino encoding = LIBELF_PRIVATE(byteorder);
56*f8fb3368SJohn Marino
57*f8fb3368SJohn Marino if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) ||
58*f8fb3368SJohn Marino dst == NULL || src == NULL || dst == src) {
59*f8fb3368SJohn Marino LIBELF_SET_ERROR(ARGUMENT, 0);
60*f8fb3368SJohn Marino return (NULL);
61*f8fb3368SJohn Marino }
62*f8fb3368SJohn Marino
63*f8fb3368SJohn Marino assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
64*f8fb3368SJohn Marino assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
65*f8fb3368SJohn Marino
66*f8fb3368SJohn Marino if (dst->d_version != src->d_version) {
67*f8fb3368SJohn Marino LIBELF_SET_ERROR(UNIMPL, 0);
68*f8fb3368SJohn Marino return (NULL);
69*f8fb3368SJohn Marino }
70*f8fb3368SJohn Marino
71*f8fb3368SJohn Marino if (src->d_buf == NULL || dst->d_buf == NULL) {
72*f8fb3368SJohn Marino LIBELF_SET_ERROR(DATA, 0);
73*f8fb3368SJohn Marino return (NULL);
74*f8fb3368SJohn Marino }
75*f8fb3368SJohn Marino
76*f8fb3368SJohn Marino if ((int) src->d_type < 0 || src->d_type >= ELF_T_NUM) {
77*f8fb3368SJohn Marino LIBELF_SET_ERROR(DATA, 0);
78*f8fb3368SJohn Marino return (NULL);
79*f8fb3368SJohn Marino }
80*f8fb3368SJohn Marino
81*f8fb3368SJohn Marino if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
82*f8fb3368SJohn Marino (src->d_type, (size_t) 1, src->d_version)) == 0)
83*f8fb3368SJohn Marino return (NULL);
84*f8fb3368SJohn Marino
85*f8fb3368SJohn Marino msz = _libelf_msize(src->d_type, elfclass, src->d_version);
86*f8fb3368SJohn Marino
87*f8fb3368SJohn Marino assert(msz > 0);
88*f8fb3368SJohn Marino
89*f8fb3368SJohn Marino if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) {
90*f8fb3368SJohn Marino LIBELF_SET_ERROR(DATA, 0);
91*f8fb3368SJohn Marino return (NULL);
92*f8fb3368SJohn Marino }
93*f8fb3368SJohn Marino
94*f8fb3368SJohn Marino /*
95*f8fb3368SJohn Marino * Determine the number of objects that need to be converted, and
96*f8fb3368SJohn Marino * the space required for the converted objects in the destination
97*f8fb3368SJohn Marino * buffer.
98*f8fb3368SJohn Marino */
99*f8fb3368SJohn Marino if (direction == ELF_TOMEMORY) {
100*f8fb3368SJohn Marino cnt = (size_t) src->d_size / fsz;
101*f8fb3368SJohn Marino dsz = cnt * msz;
102*f8fb3368SJohn Marino } else {
103*f8fb3368SJohn Marino cnt = (size_t) src->d_size / msz;
104*f8fb3368SJohn Marino dsz = cnt * fsz;
105*f8fb3368SJohn Marino }
106*f8fb3368SJohn Marino
107*f8fb3368SJohn Marino if (dst->d_size < dsz) {
108*f8fb3368SJohn Marino LIBELF_SET_ERROR(DATA, 0);
109*f8fb3368SJohn Marino return (NULL);
110*f8fb3368SJohn Marino }
111*f8fb3368SJohn Marino
112*f8fb3368SJohn Marino sb = (uintptr_t) src->d_buf;
113*f8fb3368SJohn Marino se = sb + (size_t) src->d_size;
114*f8fb3368SJohn Marino db = (uintptr_t) dst->d_buf;
115*f8fb3368SJohn Marino de = db + (size_t) dst->d_size;
116*f8fb3368SJohn Marino
117*f8fb3368SJohn Marino /*
118*f8fb3368SJohn Marino * Check for overlapping buffers. Note that db == sb is
119*f8fb3368SJohn Marino * allowed.
120*f8fb3368SJohn Marino */
121*f8fb3368SJohn Marino if (db != sb && de > sb && se > db) {
122*f8fb3368SJohn Marino LIBELF_SET_ERROR(DATA, 0);
123*f8fb3368SJohn Marino return (NULL);
124*f8fb3368SJohn Marino }
125*f8fb3368SJohn Marino
126*f8fb3368SJohn Marino if ((direction == ELF_TOMEMORY ? db : sb) %
127*f8fb3368SJohn Marino _libelf_malign(src->d_type, elfclass)) {
128*f8fb3368SJohn Marino LIBELF_SET_ERROR(DATA, 0);
129*f8fb3368SJohn Marino return (NULL);
130*f8fb3368SJohn Marino }
131*f8fb3368SJohn Marino
132*f8fb3368SJohn Marino dst->d_type = src->d_type;
133*f8fb3368SJohn Marino dst->d_size = dsz;
134*f8fb3368SJohn Marino
135*f8fb3368SJohn Marino byteswap = encoding != LIBELF_PRIVATE(byteorder);
136*f8fb3368SJohn Marino
137*f8fb3368SJohn Marino if (src->d_size == 0 ||
138*f8fb3368SJohn Marino (db == sb && !byteswap && fsz == msz))
139*f8fb3368SJohn Marino return (dst); /* nothing more to do */
140*f8fb3368SJohn Marino
141*f8fb3368SJohn Marino if (!(_libelf_get_translator(src->d_type, direction, elfclass))
142*f8fb3368SJohn Marino (dst->d_buf, dsz, src->d_buf, cnt, byteswap)) {
143*f8fb3368SJohn Marino LIBELF_SET_ERROR(DATA, 0);
144*f8fb3368SJohn Marino return (NULL);
145*f8fb3368SJohn Marino }
146*f8fb3368SJohn Marino
147*f8fb3368SJohn Marino return (dst);
148*f8fb3368SJohn Marino }
149