1 /* Transformation functions for ELF data types. 2 Copyright (C) 1998,1999,2000,2002,2004,2005,2006,2007 Red Hat, Inc. 3 This file is part of Red Hat elfutils. 4 Written by Ulrich Drepper <drepper (at) redhat.com>, 1998. 5 6 Red Hat elfutils is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by the 8 Free Software Foundation; version 2 of the License. 9 10 Red Hat elfutils is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License along 16 with Red Hat elfutils; if not, write to the Free Software Foundation, 17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 18 19 In addition, as a special exception, Red Hat, Inc. gives You the 20 additional right to link the code of Red Hat elfutils with code licensed 21 under any Open Source Initiative certified open source license 22 (http://www.opensource.org/licenses/index.php) which requires the 23 distribution of source code with any binary distribution and to 24 distribute linked combinations of the two. Non-GPL Code permitted under 25 this exception must only link to the code of Red Hat elfutils through 26 those well defined interfaces identified in the file named EXCEPTION 27 found in the source code files (the "Approved Interfaces"). The files 28 of Non-GPL Code may instantiate templates or use macros or inline 29 functions from the Approved Interfaces without causing the resulting 30 work to be covered by the GNU General Public License. Only Red Hat, 31 Inc. may make changes or additions to the list of Approved Interfaces. 32 Red Hat's grant of this exception is conditioned upon your not adding 33 any new exceptions. If you wish to add a new Approved Interface or 34 exception, please contact Red Hat. You must obey the GNU General Public 35 License in all respects for all of the Red Hat elfutils code and other 36 code used in conjunction with Red Hat elfutils except the Non-GPL Code 37 covered by this exception. If you modify this file, you may extend this 38 exception to your version of the file, but you are not obligated to do 39 so. If you do not wish to provide this exception without modification, 40 you must delete this exception statement from your version and license 41 this file solely under the GPL without exception. 42 43 Red Hat elfutils is an included package of the Open Invention Network. 44 An included package of the Open Invention Network is a package for which 45 Open Invention Network licensees cross-license their patents. No patent 46 license is granted, either expressly or impliedly, by designation as an 47 included package. Should you wish to participate in the Open Invention 48 Network licensing program, please visit www.openinventionnetwork.com 49 <http://www.openinventionnetwork.com>. */ 50 51 #ifdef HAVE_CONFIG_H 52 # include <config.h> 53 #endif 54 55 #include <byteswap.h> 56 #include <stdint.h> 57 #include <string.h> 58 #include <stdlib.h> 59 60 #include "libelfP.h" 61 62 #ifndef LIBELFBITS 63 # define LIBELFBITS 32 64 #endif 65 66 67 /* Well, what shall I say. Nothing to do here. */ 68 #define elf_cvt_Byte(dest, src, n) \ 69 (__builtin_constant_p (n) && (n) == 1 \ 70 ? (void) (*((char *) (dest)) = *((char *) (src))) \ 71 : Elf32_cvt_Byte (dest, src, n)) 72 static void 73 (elf_cvt_Byte) (void *dest, const void *src, size_t n, 74 int encode __attribute__ ((unused))) 75 { 76 memmove (dest, src, n); 77 } 78 79 80 /* We'll optimize the definition of the conversion functions here a 81 bit. We need only functions for 16, 32, and 64 bits. The 82 functions referenced in the table will be aliases for one of these 83 functions. Which one is decided by the ELFxx_FSZ_type. */ 84 85 #if ALLOW_UNALIGNED 86 87 #define FETCH(Bits, ptr) (*(const uint##Bits##_t *) ptr) 88 #define STORE(Bits, ptr, val) (*(uint##Bits##_t *) ptr = val) 89 90 #else 91 92 union unaligned 93 { 94 uint16_t u16; 95 uint32_t u32; 96 uint64_t u64; 97 } __attribute__ ((packed)); 98 99 #define FETCH(Bits, ptr) (((const union unaligned *) ptr)->u##Bits) 100 #define STORE(Bits, ptr, val) (((union unaligned *) ptr)->u##Bits = val) 101 102 #endif 103 104 /* Now define the conversion functions for the basic types. We use here 105 the fact that file and memory types are the same and that we have the 106 ELFxx_FSZ_* macros. 107 108 At the same time we define inline functions which we will use to 109 convert the complex types. */ 110 #define FUNDAMENTAL(NAME, Name, Bits) \ 111 INLINE2 (ELFW2(Bits,FSZ_##NAME), ElfW2(Bits,cvt_##Name), ElfW2(Bits,Name)) 112 #define INLINE2(Bytes, FName, TName) \ 113 INLINE3 (Bytes, FName, TName) 114 #define INLINE3(Bytes, FName, TName) \ 115 static inline void FName##1 (void *dest, const void *ptr) \ 116 { \ 117 switch (Bytes) \ 118 { \ 119 case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break; \ 120 case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break; \ 121 case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break; \ 122 default: \ 123 abort (); \ 124 } \ 125 } \ 126 \ 127 static void FName (void *dest, const void *ptr, size_t len, \ 128 int encode __attribute__ ((unused))) \ 129 { \ 130 size_t n = len / sizeof (TName); \ 131 if (dest < ptr) \ 132 while (n-- > 0) \ 133 { \ 134 FName##1 (dest, ptr); \ 135 dest += Bytes; \ 136 ptr += Bytes; \ 137 } \ 138 else \ 139 { \ 140 dest += len; \ 141 ptr += len; \ 142 while (n-- > 0) \ 143 { \ 144 ptr -= Bytes; \ 145 dest -= Bytes; \ 146 FName##1 (dest, ptr); \ 147 } \ 148 } \ 149 } 150 151 152 /* Now the tricky part: define the transformation functions for the 153 complex types. We will use the definitions of the types in 154 abstract.h. */ 155 #define START(Bits, Name, EName) \ 156 static void \ 157 ElfW2 (Bits, cvt_##Name) (void *dest, const void *src, size_t len, \ 158 int encode __attribute__ ((unused))) \ 159 { ElfW2(Bits, Name) *tdest = (ElfW2(Bits, Name) *) dest; \ 160 ElfW2(Bits, Name) *tsrc = (ElfW2(Bits, Name) *) src; \ 161 size_t n; \ 162 for (n = len / sizeof (ElfW2(Bits, Name)); n > 0; ++tdest, ++tsrc, --n) { 163 #define END(Bits, Name) } } 164 #define TYPE_EXTRA(Code) 165 #define TYPE_XLATE(Code) Code 166 #define TYPE_NAME(Type, Name) TYPE_NAME2 (Type, Name) 167 #define TYPE_NAME2(Type, Name) Type##1 (&tdest->Name, &tsrc->Name); 168 #define TYPE(Name, Bits) TYPE2 (Name, Bits) 169 #define TYPE2(Name, Bits) TYPE3 (Name##Bits) 170 #define TYPE3(Name) Name (cvt_) 171 172 /* Signal that we are generating conversion functions. */ 173 #define GENERATE_CONVERSION 174 175 /* First generate the 32-bit conversion functions. */ 176 #define LIBELFBITS 32 177 #include "gelf_xlate.h" 178 179 /* Now generate the 64-bit conversion functions. */ 180 #define LIBELFBITS 64 181 #include "gelf_xlate.h" 182 183 184 /* We have a few functions which we must create by hand since the sections 185 do not contain records of only one type. */ 186 #include "version_xlate.h" 187 #include "gnuhash_xlate.h" 188 #include "note_xlate.h" 189 190 191 /* Now the externally visible table with the function pointers. */ 192 const xfct_t __elf_xfctstom[EV_NUM - 1][EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] = 193 { 194 [EV_CURRENT - 1] = { 195 [EV_CURRENT - 1] = { 196 [ELFCLASS32 - 1] = { 197 #define define_xfcts(Bits) \ 198 [ELF_T_BYTE] = elf_cvt_Byte, \ 199 [ELF_T_ADDR] = ElfW2(Bits, cvt_Addr), \ 200 [ELF_T_DYN] = ElfW2(Bits, cvt_Dyn), \ 201 [ELF_T_EHDR] = ElfW2(Bits, cvt_Ehdr), \ 202 [ELF_T_HALF] = ElfW2(Bits, cvt_Half), \ 203 [ELF_T_OFF] = ElfW2(Bits, cvt_Off), \ 204 [ELF_T_PHDR] = ElfW2(Bits, cvt_Phdr), \ 205 [ELF_T_RELA] = ElfW2(Bits, cvt_Rela), \ 206 [ELF_T_REL] = ElfW2(Bits, cvt_Rel), \ 207 [ELF_T_SHDR] = ElfW2(Bits, cvt_Shdr), \ 208 [ELF_T_SWORD] = ElfW2(Bits, cvt_Sword), \ 209 [ELF_T_SYM] = ElfW2(Bits, cvt_Sym), \ 210 [ELF_T_WORD] = ElfW2(Bits, cvt_Word), \ 211 [ELF_T_XWORD] = ElfW2(Bits, cvt_Xword), \ 212 [ELF_T_SXWORD] = ElfW2(Bits, cvt_Sxword), \ 213 [ELF_T_VDEF] = elf_cvt_Verdef, \ 214 [ELF_T_VDAUX] = elf_cvt_Verdef, \ 215 [ELF_T_VNEED] = elf_cvt_Verneed, \ 216 [ELF_T_VNAUX] = elf_cvt_Verneed, \ 217 [ELF_T_NHDR] = elf_cvt_note, \ 218 [ELF_T_SYMINFO] = ElfW2(Bits, cvt_Syminfo), \ 219 [ELF_T_MOVE] = ElfW2(Bits, cvt_Move), \ 220 [ELF_T_LIB] = ElfW2(Bits, cvt_Lib), \ 221 [ELF_T_AUXV] = ElfW2(Bits, cvt_auxv_t) 222 define_xfcts (32), 223 [ELF_T_GNUHASH] = Elf32_cvt_Word 224 }, 225 [ELFCLASS64 - 1] = { 226 define_xfcts (64), 227 [ELF_T_GNUHASH] = elf_cvt_gnuhash 228 } 229 } 230 } 231 }; 232 /* For now we only handle the case where the memory representation is the 233 same as the file representation. Should this change we have to define 234 separate functions. For now reuse them. */ 235 #ifndef __APPLE__ 236 strong_alias (__elf_xfctstom, __elf_xfctstof) 237 #endif 238