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