libelf_xlate.c revision 210338
1251875Speter/*-
2251875Speter * Copyright (c) 2006 Joseph Koshy
3251875Speter * All rights reserved.
4251875Speter *
5251875Speter * Redistribution and use in source and binary forms, with or without
6251875Speter * modification, are permitted provided that the following conditions
7251875Speter * are met:
8251875Speter * 1. Redistributions of source code must retain the above copyright
9251875Speter *    notice, this list of conditions and the following disclaimer.
10251875Speter * 2. Redistributions in binary form must reproduce the above copyright
11251875Speter *    notice, this list of conditions and the following disclaimer in the
12251875Speter *    documentation and/or other materials provided with the distribution.
13251875Speter *
14251875Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15251875Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16251875Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17251875Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18251875Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19251875Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20251875Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21251875Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22251875Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23251875Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24251875Speter * SUCH DAMAGE.
25251875Speter */
26251875Speter
27251875Speter#include <sys/cdefs.h>
28251875Speter__FBSDID("$FreeBSD: head/lib/libelf/libelf_xlate.c 210338 2010-07-21 10:25:02Z kaiw $");
29251875Speter
30251875Speter#include <assert.h>
31251875Speter#include <libelf.h>
32251875Speter
33251875Speter#include "_libelf.h"
34251875Speter
35251875Speter/*
36251875Speter * Translate to/from the file representation of ELF objects.
37251875Speter *
38251875Speter * Translation could potentially involve the following
39251875Speter * transformations:
40251875Speter *
41251875Speter * - an endianness conversion,
42251875Speter * - a change of layout, as the file representation of ELF objects
43251875Speter *   can differ from their in-memory representation.
44251875Speter * - a change in representation due to a layout version change.
45251875Speter */
46251875Speter
47251875SpeterElf_Data *
48251875Speter_libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding,
49251875Speter    int elfclass, int direction)
50251875Speter{
51251875Speter	int byteswap;
52251875Speter	size_t cnt, dsz, fsz, msz;
53251875Speter	uintptr_t sb, se, db, de;
54251875Speter
55251875Speter	if (encoding == ELFDATANONE)
56251875Speter		encoding = LIBELF_PRIVATE(byteorder);
57251875Speter
58251875Speter	if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) ||
59251875Speter	    dst == NULL || src == NULL || dst == src)	{
60251875Speter		LIBELF_SET_ERROR(ARGUMENT, 0);
61251875Speter		return (NULL);
62251875Speter	}
63251875Speter
64251875Speter	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
65251875Speter	assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
66251875Speter
67251875Speter	if (dst->d_version != src->d_version) {
68251875Speter		LIBELF_SET_ERROR(UNIMPL, 0);
69251875Speter		return (NULL);
70251875Speter	}
71251875Speter
72251875Speter	if  (src->d_buf == NULL || dst->d_buf == NULL) {
73251875Speter		LIBELF_SET_ERROR(DATA, 0);
74251875Speter		return (NULL);
75251875Speter	}
76251875Speter
77251875Speter	if ((int) src->d_type < 0 || src->d_type >= ELF_T_NUM) {
78251875Speter		LIBELF_SET_ERROR(DATA, 0);
79251875Speter		return (NULL);
80251875Speter	}
81251875Speter
82251875Speter	if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
83251875Speter	    (src->d_type, (size_t) 1, src->d_version)) == 0)
84251875Speter		return (NULL);
85251875Speter
86251875Speter	msz = _libelf_msize(src->d_type, elfclass, src->d_version);
87251875Speter
88251875Speter	assert(msz > 0);
89251875Speter
90251875Speter	if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) {
91251875Speter		LIBELF_SET_ERROR(DATA, 0);
92251875Speter		return (NULL);
93251875Speter	}
94251875Speter
95251875Speter	/*
96251875Speter	 * Determine the number of objects that need to be converted, and
97251875Speter	 * the space required for the converted objects in the destination
98251875Speter	 * buffer.
99251875Speter	 */
100251875Speter	if (direction == ELF_TOMEMORY) {
101251875Speter		cnt = src->d_size / fsz;
102251875Speter		dsz = cnt * msz;
103251875Speter	} else {
104251875Speter		cnt = src->d_size / msz;
105251875Speter		dsz = cnt * fsz;
106251875Speter	}
107251875Speter
108251875Speter	if (dst->d_size  <  dsz) {
109251875Speter		LIBELF_SET_ERROR(DATA, 0);
110251875Speter		return (NULL);
111251875Speter	}
112251875Speter
113251875Speter	sb = (uintptr_t) src->d_buf;
114251875Speter	se = sb + src->d_size;
115251875Speter	db = (uintptr_t) dst->d_buf;
116251875Speter	de = db + dst->d_size;
117251875Speter
118251875Speter	/*
119251875Speter	 * Check for overlapping buffers.  Note that db == sb is
120251875Speter	 * allowed.
121251875Speter	 */
122251875Speter	if (db != sb && de > sb && se > db) {
123251875Speter		LIBELF_SET_ERROR(DATA, 0);
124251875Speter		return (NULL);
125251875Speter	}
126251875Speter
127251875Speter	if ((direction == ELF_TOMEMORY ? db : sb) %
128251875Speter	    _libelf_malign(src->d_type, elfclass)) {
129251875Speter		LIBELF_SET_ERROR(DATA, 0);
130251875Speter		return (NULL);
131251875Speter	}
132251875Speter
133251875Speter	dst->d_type = src->d_type;
134251875Speter	dst->d_size = dsz;
135251875Speter
136251875Speter	byteswap = encoding != LIBELF_PRIVATE(byteorder);
137251875Speter
138251875Speter	if (src->d_size == 0 ||
139251875Speter	    (db == sb && !byteswap && fsz == msz))
140251875Speter		return (dst);	/* nothing more to do */
141251875Speter
142251875Speter	if (!(_libelf_get_translator(src->d_type, direction, elfclass))
143251875Speter	    (dst->d_buf, dsz, src->d_buf, cnt, byteswap)) {
144251875Speter		LIBELF_SET_ERROR(DATA, 0);
145251875Speter		return (NULL);
146251875Speter	}
147251875Speter
148251875Speter	return (dst);
149251875Speter}
150251875Speter