1164190Sjkoshy/*-
2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy
3164190Sjkoshy * All rights reserved.
4164190Sjkoshy *
5164190Sjkoshy * Redistribution and use in source and binary forms, with or without
6164190Sjkoshy * modification, are permitted provided that the following conditions
7164190Sjkoshy * are met:
8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
9164190Sjkoshy *    notice, this list of conditions and the following disclaimer.
10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12164190Sjkoshy *    documentation and/or other materials provided with the distribution.
13164190Sjkoshy *
14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17164190Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24164190Sjkoshy * SUCH DAMAGE.
25164190Sjkoshy */
26164190Sjkoshy
27164190Sjkoshy#include <sys/cdefs.h>
28164190Sjkoshy__FBSDID("$FreeBSD$");
29164190Sjkoshy
30164190Sjkoshy#include <assert.h>
31164190Sjkoshy#include <libelf.h>
32164190Sjkoshy
33164190Sjkoshy#include "_libelf.h"
34164190Sjkoshy
35164190Sjkoshy/*
36164190Sjkoshy * Translate to/from the file representation of ELF objects.
37164190Sjkoshy *
38164190Sjkoshy * Translation could potentially involve the following
39164190Sjkoshy * transformations:
40164190Sjkoshy *
41164190Sjkoshy * - an endianness conversion,
42164190Sjkoshy * - a change of layout, as the file representation of ELF objects
43164190Sjkoshy *   can differ from their in-memory representation.
44164190Sjkoshy * - a change in representation due to a layout version change.
45164190Sjkoshy */
46164190Sjkoshy
47164190SjkoshyElf_Data *
48164190Sjkoshy_libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding,
49164190Sjkoshy    int elfclass, int direction)
50164190Sjkoshy{
51210338Skaiw	int byteswap;
52164190Sjkoshy	size_t cnt, dsz, fsz, msz;
53164190Sjkoshy	uintptr_t sb, se, db, de;
54164190Sjkoshy
55164190Sjkoshy	if (encoding == ELFDATANONE)
56164190Sjkoshy		encoding = LIBELF_PRIVATE(byteorder);
57164190Sjkoshy
58164190Sjkoshy	if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) ||
59164190Sjkoshy	    dst == NULL || src == NULL || dst == src)	{
60164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
61164190Sjkoshy		return (NULL);
62164190Sjkoshy	}
63164190Sjkoshy
64164190Sjkoshy	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
65164190Sjkoshy	assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
66164190Sjkoshy
67164190Sjkoshy	if (dst->d_version != src->d_version) {
68164190Sjkoshy		LIBELF_SET_ERROR(UNIMPL, 0);
69164190Sjkoshy		return (NULL);
70164190Sjkoshy	}
71164190Sjkoshy
72173924Sjkoshy	if  (src->d_buf == NULL || dst->d_buf == NULL) {
73164190Sjkoshy		LIBELF_SET_ERROR(DATA, 0);
74164190Sjkoshy		return (NULL);
75164190Sjkoshy	}
76164190Sjkoshy
77164190Sjkoshy	if ((int) src->d_type < 0 || src->d_type >= ELF_T_NUM) {
78164190Sjkoshy		LIBELF_SET_ERROR(DATA, 0);
79164190Sjkoshy		return (NULL);
80164190Sjkoshy	}
81164190Sjkoshy
82173924Sjkoshy	if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
83173924Sjkoshy	    (src->d_type, (size_t) 1, src->d_version)) == 0)
84164190Sjkoshy		return (NULL);
85164190Sjkoshy
86164190Sjkoshy	msz = _libelf_msize(src->d_type, elfclass, src->d_version);
87164190Sjkoshy
88164190Sjkoshy	assert(msz > 0);
89164190Sjkoshy
90164190Sjkoshy	if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) {
91164190Sjkoshy		LIBELF_SET_ERROR(DATA, 0);
92164190Sjkoshy		return (NULL);
93164190Sjkoshy	}
94164190Sjkoshy
95165523Sjkoshy	/*
96165523Sjkoshy	 * Determine the number of objects that need to be converted, and
97165523Sjkoshy	 * the space required for the converted objects in the destination
98165523Sjkoshy	 * buffer.
99165523Sjkoshy	 */
100165523Sjkoshy	if (direction == ELF_TOMEMORY) {
101165523Sjkoshy		cnt = src->d_size / fsz;
102165523Sjkoshy		dsz = cnt * msz;
103165523Sjkoshy	} else {
104165523Sjkoshy		cnt = src->d_size / msz;
105165523Sjkoshy		dsz = cnt * fsz;
106165523Sjkoshy	}
107164190Sjkoshy
108164190Sjkoshy	if (dst->d_size  <  dsz) {
109164190Sjkoshy		LIBELF_SET_ERROR(DATA, 0);
110164190Sjkoshy		return (NULL);
111164190Sjkoshy	}
112164190Sjkoshy
113164190Sjkoshy	sb = (uintptr_t) src->d_buf;
114164190Sjkoshy	se = sb + src->d_size;
115164190Sjkoshy	db = (uintptr_t) dst->d_buf;
116164190Sjkoshy	de = db + dst->d_size;
117164190Sjkoshy
118164190Sjkoshy	/*
119164190Sjkoshy	 * Check for overlapping buffers.  Note that db == sb is
120165523Sjkoshy	 * allowed.
121164190Sjkoshy	 */
122165523Sjkoshy	if (db != sb && de > sb && se > db) {
123164190Sjkoshy		LIBELF_SET_ERROR(DATA, 0);
124164190Sjkoshy		return (NULL);
125164190Sjkoshy	}
126164190Sjkoshy
127164190Sjkoshy	if ((direction == ELF_TOMEMORY ? db : sb) %
128164190Sjkoshy	    _libelf_malign(src->d_type, elfclass)) {
129164190Sjkoshy		LIBELF_SET_ERROR(DATA, 0);
130164190Sjkoshy		return (NULL);
131164190Sjkoshy	}
132164190Sjkoshy
133164190Sjkoshy	dst->d_type = src->d_type;
134165523Sjkoshy	dst->d_size = dsz;
135164190Sjkoshy
136210338Skaiw	byteswap = encoding != LIBELF_PRIVATE(byteorder);
137210338Skaiw
138173924Sjkoshy	if (src->d_size == 0 ||
139210338Skaiw	    (db == sb && !byteswap && fsz == msz))
140164190Sjkoshy		return (dst);	/* nothing more to do */
141164190Sjkoshy
142210338Skaiw	if (!(_libelf_get_translator(src->d_type, direction, elfclass))
143210338Skaiw	    (dst->d_buf, dsz, src->d_buf, cnt, byteswap)) {
144210338Skaiw		LIBELF_SET_ERROR(DATA, 0);
145210338Skaiw		return (NULL);
146210338Skaiw	}
147164190Sjkoshy
148164190Sjkoshy	return (dst);
149164190Sjkoshy}
150