HIP: Heterogenous-computing Interface for Portability
elfio_utils.hpp
1 /*
2 Copyright (C) 2001-2015 by Serge Lamikhov-Center
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 #ifndef ELFIO_UTILS_HPP
24 #define ELFIO_UTILS_HPP
25 
26 #define ELFIO_GET_ACCESS(TYPE, NAME, FIELD) \
27  TYPE get_##NAME() const { return (*convertor)(FIELD); }
28 #define ELFIO_SET_ACCESS(TYPE, NAME, FIELD) \
29  void set_##NAME(TYPE value) { \
30  FIELD = value; \
31  FIELD = (*convertor)(FIELD); \
32  }
33 #define ELFIO_GET_SET_ACCESS(TYPE, NAME, FIELD) \
34  TYPE get_##NAME() const { return (*convertor)(FIELD); } \
35  void set_##NAME(TYPE value) { \
36  FIELD = value; \
37  FIELD = (*convertor)(FIELD); \
38  }
39 
40 #define ELFIO_GET_ACCESS_DECL(TYPE, NAME) virtual TYPE get_##NAME() const = 0
41 
42 #define ELFIO_SET_ACCESS_DECL(TYPE, NAME) virtual void set_##NAME(TYPE value) = 0
43 
44 #define ELFIO_GET_SET_ACCESS_DECL(TYPE, NAME) \
45  virtual TYPE get_##NAME() const = 0; \
46  virtual void set_##NAME(TYPE value) = 0
47 
48 namespace ELFIO {
49 
50 //------------------------------------------------------------------------------
52  public:
53  //------------------------------------------------------------------------------
54  endianess_convertor() { need_conversion = false; }
55 
56  //------------------------------------------------------------------------------
57  void setup(unsigned char elf_file_encoding) {
58  need_conversion = (elf_file_encoding != get_host_encoding());
59  }
60 
61  //------------------------------------------------------------------------------
62  uint64_t operator()(uint64_t value) const {
63  if (!need_conversion) {
64  return value;
65  }
66  value = ((value & 0x00000000000000FFull) << 56) | ((value & 0x000000000000FF00ull) << 40) |
67  ((value & 0x0000000000FF0000ull) << 24) | ((value & 0x00000000FF000000ull) << 8) |
68  ((value & 0x000000FF00000000ull) >> 8) | ((value & 0x0000FF0000000000ull) >> 24) |
69  ((value & 0x00FF000000000000ull) >> 40) | ((value & 0xFF00000000000000ull) >> 56);
70 
71  return value;
72  }
73 
74  //------------------------------------------------------------------------------
75  int64_t operator()(int64_t value) const {
76  if (!need_conversion) {
77  return value;
78  }
79  return (int64_t)(*this)((uint64_t)value);
80  }
81 
82  //------------------------------------------------------------------------------
83  uint32_t operator()(uint32_t value) const {
84  if (!need_conversion) {
85  return value;
86  }
87  value = ((value & 0x000000FF) << 24) | ((value & 0x0000FF00) << 8) |
88  ((value & 0x00FF0000) >> 8) | ((value & 0xFF000000) >> 24);
89 
90  return value;
91  }
92 
93  //------------------------------------------------------------------------------
94  int32_t operator()(int32_t value) const {
95  if (!need_conversion) {
96  return value;
97  }
98  return (int32_t)(*this)((uint32_t)value);
99  }
100 
101  //------------------------------------------------------------------------------
102  uint16_t operator()(uint16_t value) const {
103  if (!need_conversion) {
104  return value;
105  }
106  value = ((value & 0x00FF) << 8) | ((value & 0xFF00) >> 8);
107 
108  return value;
109  }
110 
111  //------------------------------------------------------------------------------
112  int16_t operator()(int16_t value) const {
113  if (!need_conversion) {
114  return value;
115  }
116  return (int16_t)(*this)((uint16_t)value);
117  }
118 
119  //------------------------------------------------------------------------------
120  int8_t operator()(int8_t value) const { return value; }
121 
122  //------------------------------------------------------------------------------
123  uint8_t operator()(uint8_t value) const { return value; }
124 
125  //------------------------------------------------------------------------------
126  private:
127  //------------------------------------------------------------------------------
128  unsigned char get_host_encoding() const {
129  static const int tmp = 1;
130  if (1 == *(char*)&tmp) {
131  return ELFDATA2LSB;
132  } else {
133  return ELFDATA2MSB;
134  }
135  }
136 
137  //------------------------------------------------------------------------------
138  private:
139  bool need_conversion;
140 };
141 
142 
143 //------------------------------------------------------------------------------
144 inline uint32_t elf_hash(const unsigned char* name) {
145  uint32_t h = 0, g;
146  while (*name) {
147  h = (h << 4) + *name++;
148  g = h & 0xf0000000;
149  if (g != 0) h ^= g >> 24;
150  h &= ~g;
151  }
152  return h;
153 }
154 
155 } // namespace ELFIO
156 
157 #endif // ELFIO_UTILS_HPP
ELFIO::endianess_convertor
Definition: elfio_utils.hpp:51