Branch data Line data Source code
1 : : /* Convert from memory to file representation. 2 : : Copyright (C) 1998, 1999, 2000, 2002, 2015 Red Hat, Inc. 3 : : This file is part of elfutils. 4 : : Written by Ulrich Drepper <drepper@redhat.com>, 1998. 5 : : 6 : : This file is free software; you can redistribute it and/or modify 7 : : it under the terms of either 8 : : 9 : : * the GNU Lesser General Public License as published by the Free 10 : : Software Foundation; either version 3 of the License, or (at 11 : : your option) any later version 12 : : 13 : : or 14 : : 15 : : * the GNU General Public License as published by the Free 16 : : Software Foundation; either version 2 of the License, or (at 17 : : your option) any later version 18 : : 19 : : or both in parallel, as here. 20 : : 21 : : elfutils is distributed in the hope that it will be useful, but 22 : : WITHOUT ANY WARRANTY; without even the implied warranty of 23 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 : : General Public License for more details. 25 : : 26 : : You should have received copies of the GNU General Public License and 27 : : the GNU Lesser General Public License along with this program. If 28 : : not, see <http://www.gnu.org/licenses/>. */ 29 : : 30 : : #ifdef HAVE_CONFIG_H 31 : : # include <config.h> 32 : : #endif 33 : : 34 : : #include <assert.h> 35 : : #include <string.h> 36 : : 37 : : #include "libelfP.h" 38 : : 39 : : #ifndef LIBELFBITS 40 : : # define LIBELFBITS 32 41 : : #endif 42 : : 43 : : 44 : : Elf_Data * 45 : 343414 : elfw2(LIBELFBITS, xlatetof) (Elf_Data *dest, const Elf_Data *src, 46 : : unsigned int encode) 47 : : { 48 : : /* First test whether the input data is really suitable for this 49 : : type. This means, whether there is an integer number of records. 50 : : Note that for this implementation the memory and file size of the 51 : : data types are identical. */ 52 : 343414 : size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type]; 53 : : 54 : : /* We shouldn't require integer number of records when processing 55 : : notes. Payload bytes follow the header immediately, it's not an 56 : : array of records as is the case otherwise. */ 57 [ + - ]: 343414 : if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8 58 [ - + ]: 343414 : && src->d_size % recsize != 0) 59 : : { 60 : 0 : __libelf_seterrno (ELF_E_INVALID_DATA); 61 : 0 : return NULL; 62 : : } 63 : : 64 : : /* Next see whether the converted data fits in the output buffer. */ 65 [ - + ]: 343414 : if (src->d_size > dest->d_size) 66 : : { 67 : 0 : __libelf_seterrno (ELF_E_DEST_SIZE); 68 : 0 : return NULL; 69 : : } 70 : : 71 : : /* Test the encode parameter. */ 72 [ - + ]: 343414 : if (encode != ELFDATA2LSB && encode != ELFDATA2MSB) 73 : : { 74 : 0 : __libelf_seterrno (ELF_E_INVALID_ENCODING); 75 : 0 : return NULL; 76 : : } 77 : : 78 : : /* Determine the translation function to use. 79 : : 80 : : At this point we make an assumption which is valid for all 81 : : existing implementations so far: the memory and file sizes are 82 : : the same. This has very important consequences: 83 : : a) The requirement that the source and destination buffer can 84 : : overlap can easily be fulfilled. 85 : : b) We need only one function to convert from and memory to file 86 : : and vice versa since the function only has to copy and/or 87 : : change the byte order. 88 : : */ 89 [ + + ]: 343414 : if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB) 90 : : || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB)) 91 : : { 92 : : /* We simply have to copy since the byte order is the same. */ 93 [ + - ]: 259540 : if (src->d_buf != dest->d_buf) 94 : 259540 : memmove (dest->d_buf, src->d_buf, src->d_size); 95 : : } 96 : : else 97 : : { 98 : 83874 : xfct_t fctp; 99 : 83874 : fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type]; 100 : : 101 : : /* Do the real work. */ 102 : 83874 : (*fctp) (dest->d_buf, src->d_buf, src->d_size, 1); 103 : : } 104 : : 105 : : /* Now set the real destination type and length since the operation was 106 : : successful. */ 107 : 343414 : dest->d_type = src->d_type; 108 : 343414 : dest->d_size = src->d_size; 109 : : 110 : 343414 : return dest; 111 : : } 112 : : INTDEF(elfw2(LIBELFBITS, xlatetof))