blob: c417051a3a839c4343fd972bd77b1c9148bf6789 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Transformation functions for ELF data types.
Roland McGrathcb6d8652007-08-23 08:10:54 +00002 Copyright (C) 1998,1999,2000,2002,2004,2005,2006,2007 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 * 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
Ulrich Drepper361df7d2006-04-04 21:38:57 +000022 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000025
Mark Wielaardde2ed972012-06-05 17:15:16 +020026 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000029
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
34#include <byteswap.h>
35#include <stdint.h>
36#include <string.h>
Roland McGrath7747de92005-12-08 01:36:18 +000037#include <stdlib.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000038
39#include "libelfP.h"
40
41#ifndef LIBELFBITS
42# define LIBELFBITS 32
43#endif
44
45
46/* Well, what shall I say. Nothing to do here. */
47#define elf_cvt_Byte(dest, src, n) \
48 (__builtin_constant_p (n) && (n) == 1 \
49 ? (void) (*((char *) (dest)) = *((char *) (src))) \
50 : Elf32_cvt_Byte (dest, src, n))
51static void
52(elf_cvt_Byte) (void *dest, const void *src, size_t n,
53 int encode __attribute__ ((unused)))
54{
55 memmove (dest, src, n);
56}
57
58
59/* We'll optimize the definition of the conversion functions here a
60 bit. We need only functions for 16, 32, and 64 bits. The
61 functions referenced in the table will be aliases for one of these
62 functions. Which one is decided by the ELFxx_FSZ_type. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000063
Roland McGrath7747de92005-12-08 01:36:18 +000064#if ALLOW_UNALIGNED
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000065
Roland McGrath7747de92005-12-08 01:36:18 +000066#define FETCH(Bits, ptr) (*(const uint##Bits##_t *) ptr)
67#define STORE(Bits, ptr, val) (*(uint##Bits##_t *) ptr = val)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000068
Roland McGrath7747de92005-12-08 01:36:18 +000069#else
70
71union unaligned
72 {
73 uint16_t u16;
74 uint32_t u32;
75 uint64_t u64;
76 } __attribute__ ((packed));
77
78#define FETCH(Bits, ptr) (((const union unaligned *) ptr)->u##Bits)
79#define STORE(Bits, ptr, val) (((union unaligned *) ptr)->u##Bits = val)
80
81#endif
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000082
83/* Now define the conversion functions for the basic types. We use here
84 the fact that file and memory types are the same and that we have the
85 ELFxx_FSZ_* macros.
86
87 At the same time we define inline functions which we will use to
88 convert the complex types. */
89#define FUNDAMENTAL(NAME, Name, Bits) \
90 INLINE2 (ELFW2(Bits,FSZ_##NAME), ElfW2(Bits,cvt_##Name), ElfW2(Bits,Name))
91#define INLINE2(Bytes, FName, TName) \
92 INLINE3 (Bytes, FName, TName)
Roland McGrath7747de92005-12-08 01:36:18 +000093#define INLINE3(Bytes, FName, TName) \
94 static inline void FName##1 (void *dest, const void *ptr) \
95 { \
96 switch (Bytes) \
97 { \
98 case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break; \
99 case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break; \
100 case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break; \
101 default: \
102 abort (); \
103 } \
104 } \
105 \
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000106 static void FName (void *dest, const void *ptr, size_t len, \
107 int encode __attribute__ ((unused))) \
108 { \
109 size_t n = len / sizeof (TName); \
110 if (dest < ptr) \
Roland McGrath7747de92005-12-08 01:36:18 +0000111 while (n-- > 0) \
112 { \
113 FName##1 (dest, ptr); \
114 dest += Bytes; \
115 ptr += Bytes; \
116 } \
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000117 else \
118 { \
Roland McGrath7747de92005-12-08 01:36:18 +0000119 dest += len; \
120 ptr += len; \
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000121 while (n-- > 0) \
122 { \
Roland McGrath7747de92005-12-08 01:36:18 +0000123 ptr -= Bytes; \
124 dest -= Bytes; \
125 FName##1 (dest, ptr); \
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000126 } \
127 } \
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000128 }
129
130
131/* Now the tricky part: define the transformation functions for the
132 complex types. We will use the definitions of the types in
133 abstract.h. */
134#define START(Bits, Name, EName) \
135 static void \
136 ElfW2 (Bits, cvt_##Name) (void *dest, const void *src, size_t len, \
137 int encode __attribute__ ((unused))) \
138 { ElfW2(Bits, Name) *tdest = (ElfW2(Bits, Name) *) dest; \
139 ElfW2(Bits, Name) *tsrc = (ElfW2(Bits, Name) *) src; \
140 size_t n; \
141 for (n = len / sizeof (ElfW2(Bits, Name)); n > 0; ++tdest, ++tsrc, --n) {
142#define END(Bits, Name) } }
143#define TYPE_EXTRA(Code)
144#define TYPE_XLATE(Code) Code
145#define TYPE_NAME(Type, Name) TYPE_NAME2 (Type, Name)
146#define TYPE_NAME2(Type, Name) Type##1 (&tdest->Name, &tsrc->Name);
147#define TYPE(Name, Bits) TYPE2 (Name, Bits)
148#define TYPE2(Name, Bits) TYPE3 (Name##Bits)
149#define TYPE3(Name) Name (cvt_)
150
151/* Signal that we are generating conversion functions. */
152#define GENERATE_CONVERSION
153
154/* First generate the 32-bit conversion functions. */
155#define LIBELFBITS 32
156#include "gelf_xlate.h"
157
158/* Now generate the 64-bit conversion functions. */
159#define LIBELFBITS 64
160#include "gelf_xlate.h"
161
162
163/* We have a few functions which we must create by hand since the sections
164 do not contain records of only one type. */
165#include "version_xlate.h"
Ulrich Drepper8ae58142006-07-12 05:22:32 +0000166#include "gnuhash_xlate.h"
Roland McGrathc76f0b02007-09-27 07:31:33 +0000167#include "note_xlate.h"
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000168
169
170/* Now the externally visible table with the function pointers. */
171const xfct_t __elf_xfctstom[EV_NUM - 1][EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] =
172{
173 [EV_CURRENT - 1] = {
174 [EV_CURRENT - 1] = {
175 [ELFCLASS32 - 1] = {
176#define define_xfcts(Bits) \
177 [ELF_T_BYTE] = elf_cvt_Byte, \
178 [ELF_T_ADDR] = ElfW2(Bits, cvt_Addr), \
179 [ELF_T_DYN] = ElfW2(Bits, cvt_Dyn), \
180 [ELF_T_EHDR] = ElfW2(Bits, cvt_Ehdr), \
181 [ELF_T_HALF] = ElfW2(Bits, cvt_Half), \
182 [ELF_T_OFF] = ElfW2(Bits, cvt_Off), \
183 [ELF_T_PHDR] = ElfW2(Bits, cvt_Phdr), \
184 [ELF_T_RELA] = ElfW2(Bits, cvt_Rela), \
185 [ELF_T_REL] = ElfW2(Bits, cvt_Rel), \
186 [ELF_T_SHDR] = ElfW2(Bits, cvt_Shdr), \
187 [ELF_T_SWORD] = ElfW2(Bits, cvt_Sword), \
188 [ELF_T_SYM] = ElfW2(Bits, cvt_Sym), \
189 [ELF_T_WORD] = ElfW2(Bits, cvt_Word), \
190 [ELF_T_XWORD] = ElfW2(Bits, cvt_Xword), \
191 [ELF_T_SXWORD] = ElfW2(Bits, cvt_Sxword), \
192 [ELF_T_VDEF] = elf_cvt_Verdef, \
193 [ELF_T_VDAUX] = elf_cvt_Verdef, \
194 [ELF_T_VNEED] = elf_cvt_Verneed, \
195 [ELF_T_VNAUX] = elf_cvt_Verneed, \
Roland McGrathc76f0b02007-09-27 07:31:33 +0000196 [ELF_T_NHDR] = elf_cvt_note, \
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000197 [ELF_T_SYMINFO] = ElfW2(Bits, cvt_Syminfo), \
198 [ELF_T_MOVE] = ElfW2(Bits, cvt_Move), \
Roland McGrathcb6d8652007-08-23 08:10:54 +0000199 [ELF_T_LIB] = ElfW2(Bits, cvt_Lib), \
200 [ELF_T_AUXV] = ElfW2(Bits, cvt_auxv_t)
Ulrich Drepper8ae58142006-07-12 05:22:32 +0000201 define_xfcts (32),
202 [ELF_T_GNUHASH] = Elf32_cvt_Word
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000203 },
204 [ELFCLASS64 - 1] = {
Ulrich Drepper8ae58142006-07-12 05:22:32 +0000205 define_xfcts (64),
206 [ELF_T_GNUHASH] = elf_cvt_gnuhash
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000207 }
208 }
209 }
210};
211/* For now we only handle the case where the memory representation is the
212 same as the file representation. Should this change we have to define
213 separate functions. For now reuse them. */
214strong_alias (__elf_xfctstom, __elf_xfctstof)