12de3b87aSKai Wang /*-
22de3b87aSKai Wang * Copyright (c) 2006,2008 Joseph Koshy
32de3b87aSKai Wang * All rights reserved.
42de3b87aSKai Wang *
52de3b87aSKai Wang * Redistribution and use in source and binary forms, with or without
62de3b87aSKai Wang * modification, are permitted provided that the following conditions
72de3b87aSKai Wang * are met:
82de3b87aSKai Wang * 1. Redistributions of source code must retain the above copyright
92de3b87aSKai Wang * notice, this list of conditions and the following disclaimer.
102de3b87aSKai Wang * 2. Redistributions in binary form must reproduce the above copyright
112de3b87aSKai Wang * notice, this list of conditions and the following disclaimer in the
122de3b87aSKai Wang * documentation and/or other materials provided with the distribution.
132de3b87aSKai Wang *
142de3b87aSKai Wang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152de3b87aSKai Wang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162de3b87aSKai Wang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172de3b87aSKai Wang * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182de3b87aSKai Wang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192de3b87aSKai Wang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202de3b87aSKai Wang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212de3b87aSKai Wang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222de3b87aSKai Wang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232de3b87aSKai Wang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242de3b87aSKai Wang * SUCH DAMAGE.
252de3b87aSKai Wang */
262de3b87aSKai Wang
272de3b87aSKai Wang #include <assert.h>
282de3b87aSKai Wang #include <libelf.h>
292de3b87aSKai Wang
302de3b87aSKai Wang #include "_libelf.h"
312de3b87aSKai Wang
32*d003e0d7SEd Maste ELFTC_VCSID("$Id: libelf_xlate.c 3732 2019-04-22 11:08:38Z jkoshy $");
332de3b87aSKai Wang
342de3b87aSKai Wang /*
352de3b87aSKai Wang * Translate to/from the file representation of ELF objects.
362de3b87aSKai Wang *
372de3b87aSKai Wang * Translation could potentially involve the following
382de3b87aSKai Wang * transformations:
392de3b87aSKai Wang *
402de3b87aSKai Wang * - an endianness conversion,
412de3b87aSKai Wang * - a change of layout, as the file representation of ELF objects
422de3b87aSKai Wang * can differ from their in-memory representation.
432de3b87aSKai Wang * - a change in representation due to a layout version change.
442de3b87aSKai Wang */
452de3b87aSKai Wang
462de3b87aSKai Wang Elf_Data *
_libelf_xlate(Elf_Data * dst,const Elf_Data * src,unsigned int encoding,int elfclass,int elfmachine,int direction)472de3b87aSKai Wang _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding,
48ae500c1fSEd Maste int elfclass, int elfmachine, int direction)
492de3b87aSKai Wang {
502de3b87aSKai Wang int byteswap;
512de3b87aSKai Wang size_t cnt, dsz, fsz, msz;
522de3b87aSKai Wang uintptr_t sb, se, db, de;
53ae500c1fSEd Maste _libelf_translator_function *xlator;
542de3b87aSKai Wang
552de3b87aSKai Wang if (encoding == ELFDATANONE)
562de3b87aSKai Wang encoding = LIBELF_PRIVATE(byteorder);
572de3b87aSKai Wang
582de3b87aSKai Wang if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) ||
592de3b87aSKai Wang dst == NULL || src == NULL || dst == src) {
602de3b87aSKai Wang LIBELF_SET_ERROR(ARGUMENT, 0);
612de3b87aSKai Wang return (NULL);
622de3b87aSKai Wang }
632de3b87aSKai Wang
642de3b87aSKai Wang assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
652de3b87aSKai Wang assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
662de3b87aSKai Wang
672de3b87aSKai Wang if (dst->d_version != src->d_version) {
682de3b87aSKai Wang LIBELF_SET_ERROR(UNIMPL, 0);
692de3b87aSKai Wang return (NULL);
702de3b87aSKai Wang }
712de3b87aSKai Wang
722de3b87aSKai Wang if (src->d_buf == NULL || dst->d_buf == NULL) {
732de3b87aSKai Wang LIBELF_SET_ERROR(DATA, 0);
742de3b87aSKai Wang return (NULL);
752de3b87aSKai Wang }
762de3b87aSKai Wang
772de3b87aSKai Wang if ((int) src->d_type < 0 || src->d_type >= ELF_T_NUM) {
782de3b87aSKai Wang LIBELF_SET_ERROR(DATA, 0);
792de3b87aSKai Wang return (NULL);
802de3b87aSKai Wang }
812de3b87aSKai Wang
822de3b87aSKai Wang if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
832de3b87aSKai Wang (src->d_type, (size_t) 1, src->d_version)) == 0)
842de3b87aSKai Wang return (NULL);
852de3b87aSKai Wang
86*d003e0d7SEd Maste if ((msz = _libelf_msize(src->d_type, elfclass, src->d_version)) == 0)
87*d003e0d7SEd Maste return (NULL);
882de3b87aSKai Wang
892de3b87aSKai Wang if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) {
902de3b87aSKai Wang LIBELF_SET_ERROR(DATA, 0);
912de3b87aSKai Wang return (NULL);
922de3b87aSKai Wang }
932de3b87aSKai Wang
942de3b87aSKai Wang /*
952de3b87aSKai Wang * Determine the number of objects that need to be converted, and
962de3b87aSKai Wang * the space required for the converted objects in the destination
972de3b87aSKai Wang * buffer.
982de3b87aSKai Wang */
992de3b87aSKai Wang if (direction == ELF_TOMEMORY) {
100cf781b2eSEd Maste cnt = (size_t) src->d_size / fsz;
1012de3b87aSKai Wang dsz = cnt * msz;
1022de3b87aSKai Wang } else {
103cf781b2eSEd Maste cnt = (size_t) src->d_size / msz;
1042de3b87aSKai Wang dsz = cnt * fsz;
1052de3b87aSKai Wang }
1062de3b87aSKai Wang
1072de3b87aSKai Wang if (dst->d_size < dsz) {
1082de3b87aSKai Wang LIBELF_SET_ERROR(DATA, 0);
1092de3b87aSKai Wang return (NULL);
1102de3b87aSKai Wang }
1112de3b87aSKai Wang
1122de3b87aSKai Wang sb = (uintptr_t) src->d_buf;
113cf781b2eSEd Maste se = sb + (size_t) src->d_size;
1142de3b87aSKai Wang db = (uintptr_t) dst->d_buf;
115cf781b2eSEd Maste de = db + (size_t) dst->d_size;
1162de3b87aSKai Wang
1172de3b87aSKai Wang /*
1182de3b87aSKai Wang * Check for overlapping buffers. Note that db == sb is
1192de3b87aSKai Wang * allowed.
1202de3b87aSKai Wang */
1212de3b87aSKai Wang if (db != sb && de > sb && se > db) {
1222de3b87aSKai Wang LIBELF_SET_ERROR(DATA, 0);
1232de3b87aSKai Wang return (NULL);
1242de3b87aSKai Wang }
1252de3b87aSKai Wang
1262de3b87aSKai Wang if ((direction == ELF_TOMEMORY ? db : sb) %
1272de3b87aSKai Wang _libelf_malign(src->d_type, elfclass)) {
1282de3b87aSKai Wang LIBELF_SET_ERROR(DATA, 0);
1292de3b87aSKai Wang return (NULL);
1302de3b87aSKai Wang }
1312de3b87aSKai Wang
1322de3b87aSKai Wang dst->d_type = src->d_type;
1332de3b87aSKai Wang dst->d_size = dsz;
1342de3b87aSKai Wang
1352de3b87aSKai Wang byteswap = encoding != LIBELF_PRIVATE(byteorder);
1362de3b87aSKai Wang
1372de3b87aSKai Wang if (src->d_size == 0 ||
1382de3b87aSKai Wang (db == sb && !byteswap && fsz == msz))
1392de3b87aSKai Wang return (dst); /* nothing more to do */
1402de3b87aSKai Wang
141ae500c1fSEd Maste xlator = _libelf_get_translator(src->d_type, direction, elfclass,
142ae500c1fSEd Maste elfmachine);
143ae500c1fSEd Maste if (!xlator(dst->d_buf, dsz, src->d_buf, cnt, byteswap)) {
1442de3b87aSKai Wang LIBELF_SET_ERROR(DATA, 0);
1452de3b87aSKai Wang return (NULL);
1462de3b87aSKai Wang }
1472de3b87aSKai Wang
1482de3b87aSKai Wang return (dst);
1492de3b87aSKai Wang }
150