1 | /* |
---|
2 | This file is part of Libelfpp. |
---|
3 | |
---|
4 | Libelfpp is free software: you can redistribute it and/or modify |
---|
5 | it under the terms of the GNU Lesser General Public License as |
---|
6 | published by the Free Software Foundation, either version 3 of the |
---|
7 | License, or (at your option) any later version. |
---|
8 | |
---|
9 | Libelfpp is distributed in the hope that it will be useful, but |
---|
10 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 | General Public License for more details. |
---|
13 | |
---|
14 | You should have received a copy of the GNU Lesser General Public |
---|
15 | License along with Libelfpp. If not, see |
---|
16 | <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | Copyright (c) Alexandre Becoulet <alexandre.becoulet@free.fr> |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef ELFPP_ACCESS_HXX_ |
---|
22 | #define ELFPP_ACCESS_HXX_ |
---|
23 | |
---|
24 | #ifdef __linux__ |
---|
25 | |
---|
26 | # include <endian.h> |
---|
27 | |
---|
28 | #elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) |
---|
29 | |
---|
30 | # include <machine/endian.h> |
---|
31 | # define __BYTE_ORDER BYTE_ORDER |
---|
32 | # define __LITTLE_ENDIAN LITTLE_ENDIAN |
---|
33 | # define __BIG_ENDIAN BIG_ENDIAN |
---|
34 | |
---|
35 | #else |
---|
36 | # error Unable to guess endianness |
---|
37 | #endif |
---|
38 | |
---|
39 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
---|
40 | # define SWAPVALUE ELFDATA2MSB |
---|
41 | #elif __BYTE_ORDER == __BIG_ENDIAN |
---|
42 | # define SWAPVALUE ELFDATA2LSB |
---|
43 | #endif |
---|
44 | |
---|
45 | namespace elfpp |
---|
46 | { |
---|
47 | |
---|
48 | template <typename bits_t, int width> |
---|
49 | unsigned int elfn_access<bits_t, width>::rel_sym(uint64_t info) |
---|
50 | { |
---|
51 | if (width == 32) |
---|
52 | return info >> 8; |
---|
53 | else |
---|
54 | return info >> 32; |
---|
55 | } |
---|
56 | |
---|
57 | template <typename bits_t, int width> |
---|
58 | enum reloc_e elfn_access<bits_t, width>::rel_type(uint64_t info) |
---|
59 | { |
---|
60 | if (width == 32) |
---|
61 | return (enum reloc_e)(info & 0xff); |
---|
62 | else |
---|
63 | return (enum reloc_e)(info & 0xffffffff); |
---|
64 | } |
---|
65 | |
---|
66 | template <typename bits_t, int width> |
---|
67 | uint64_t elfn_access<bits_t, width>::rel_info(unsigned int sym, enum reloc_e type) |
---|
68 | { |
---|
69 | if (width == 32) |
---|
70 | return (sym << 8) | (type & 0xff); |
---|
71 | else |
---|
72 | return ((uint64_t)sym << 32) | type; |
---|
73 | } |
---|
74 | |
---|
75 | template <typename bits_t, int width> |
---|
76 | uint16_t elfn_access<bits_t, width>::swap(uint16_t x) |
---|
77 | { |
---|
78 | if (byteorder_ == SWAPVALUE) |
---|
79 | return (x << 8) | (x >> 8); |
---|
80 | |
---|
81 | return x; |
---|
82 | } |
---|
83 | |
---|
84 | template <typename bits_t, int width> |
---|
85 | int16_t elfn_access<bits_t, width>::swap(int16_t x) |
---|
86 | { |
---|
87 | return swap((uint16_t)x); |
---|
88 | } |
---|
89 | |
---|
90 | template <typename bits_t, int width> |
---|
91 | uint32_t elfn_access<bits_t, width>::swap(uint32_t x) |
---|
92 | { |
---|
93 | if (byteorder_ == SWAPVALUE) |
---|
94 | return (((x >> 24) & 0x000000ff) | |
---|
95 | ((x >> 8 ) & 0x0000ff00) | |
---|
96 | ((x << 8 ) & 0x00ff0000) | |
---|
97 | ((x << 24) & 0xff000000)); |
---|
98 | |
---|
99 | return x; |
---|
100 | } |
---|
101 | |
---|
102 | template <typename bits_t, int width> |
---|
103 | int32_t elfn_access<bits_t, width>::swap(int32_t x) |
---|
104 | { |
---|
105 | return swap((uint32_t)x); |
---|
106 | } |
---|
107 | |
---|
108 | template <typename bits_t, int width> |
---|
109 | uint64_t elfn_access<bits_t, width>::swap(uint64_t x) |
---|
110 | { |
---|
111 | if (byteorder_ == SWAPVALUE) |
---|
112 | return (((x >> 56) & 0x00000000000000ffLL) | |
---|
113 | ((x >> 40) & 0x000000000000ff00LL) | |
---|
114 | ((x >> 24) & 0x0000000000ff0000LL) | |
---|
115 | ((x >> 8) & 0x00000000ff000000LL) | |
---|
116 | ((x << 8) & 0x000000ff00000000LL) | |
---|
117 | ((x << 24) & 0x0000ff0000000000LL) | |
---|
118 | ((x << 40) & 0x00ff000000000000LL) | |
---|
119 | ((x << 56) & 0xff00000000000000LL)); |
---|
120 | |
---|
121 | return x; |
---|
122 | } |
---|
123 | |
---|
124 | template <typename bits_t, int width> |
---|
125 | int64_t elfn_access<bits_t, width>::swap(int64_t x) |
---|
126 | { |
---|
127 | return swap((uint64_t)x); |
---|
128 | } |
---|
129 | |
---|
130 | template <typename bits_t, int width> |
---|
131 | template <typename X, typename Y> |
---|
132 | void elfn_access<bits_t, width>::swap(X *a, Y x) |
---|
133 | { |
---|
134 | *a = swap((X)x); |
---|
135 | } |
---|
136 | |
---|
137 | } |
---|
138 | |
---|
139 | #endif |
---|
140 | |
---|