HIP: Heterogenous-computing Interface for Portability
elfio_symbols.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_SYMBOLS_HPP
24 #define ELFIO_SYMBOLS_HPP
25 
26 namespace ELFIO {
27 
28 //------------------------------------------------------------------------------
30  public:
31  //------------------------------------------------------------------------------
32  symbol_section_accessor(const elfio& elf_file_, section* symbol_section_)
33  : elf_file(elf_file_), symbol_section(symbol_section_) {
34  find_hash_section();
35  }
36 
37  //------------------------------------------------------------------------------
38  Elf_Xword get_symbols_num() const {
39  Elf_Xword nRet = 0;
40  if (0 != symbol_section->get_entry_size()) {
41  nRet = symbol_section->get_size() / symbol_section->get_entry_size();
42  }
43 
44  return nRet;
45  }
46 
47  //------------------------------------------------------------------------------
48  bool get_symbol(Elf_Xword index, std::string& name, Elf64_Addr& value, Elf_Xword& size,
49  unsigned char& bind, unsigned char& type, Elf_Half& section_index,
50  unsigned char& other) const {
51  bool ret = false;
52 
53  if (elf_file.get_class() == ELFCLASS32) {
54  ret = generic_get_symbol<Elf32_Sym>(index, name, value, size, bind, type, section_index,
55  other);
56  } else {
57  ret = generic_get_symbol<Elf64_Sym>(index, name, value, size, bind, type, section_index,
58  other);
59  }
60 
61  return ret;
62  }
63 
64  //------------------------------------------------------------------------------
65  bool get_symbol(const std::string& name, Elf64_Addr& value, Elf_Xword& size,
66  unsigned char& bind, unsigned char& type, Elf_Half& section_index,
67  unsigned char& other) const {
68  bool ret = false;
69 
70  if (0 != get_hash_table_index()) {
71  Elf_Word nbucket = *(Elf_Word*)hash_section->get_data();
72  Elf_Word nchain = *(Elf_Word*)(hash_section->get_data() + sizeof(Elf_Word));
73  Elf_Word val = elf_hash((const unsigned char*)name.c_str());
74 
75  Elf_Word y =
76  *(Elf_Word*)(hash_section->get_data() + (2 + val % nbucket) * sizeof(Elf_Word));
77  std::string str;
78  get_symbol(y, str, value, size, bind, type, section_index, other);
79  while (str != name && STN_UNDEF != y && y < nchain) {
80  y = *(Elf_Word*)(hash_section->get_data() + (2 + nbucket + y) * sizeof(Elf_Word));
81  get_symbol(y, str, value, size, bind, type, section_index, other);
82  }
83  if (str == name) {
84  ret = true;
85  }
86  }
87 
88  return ret;
89  }
90 
91  //------------------------------------------------------------------------------
92  Elf_Word add_symbol(Elf_Word name, Elf64_Addr value, Elf_Xword size, unsigned char info,
93  unsigned char other, Elf_Half shndx) {
94  Elf_Word nRet;
95 
96  if (symbol_section->get_size() == 0) {
97  if (elf_file.get_class() == ELFCLASS32) {
98  nRet = generic_add_symbol<Elf32_Sym>(0, 0, 0, 0, 0, 0);
99  } else {
100  nRet = generic_add_symbol<Elf64_Sym>(0, 0, 0, 0, 0, 0);
101  }
102  }
103 
104  if (elf_file.get_class() == ELFCLASS32) {
105  nRet = generic_add_symbol<Elf32_Sym>(name, value, size, info, other, shndx);
106  } else {
107  nRet = generic_add_symbol<Elf64_Sym>(name, value, size, info, other, shndx);
108  }
109 
110  return nRet;
111  }
112 
113  //------------------------------------------------------------------------------
114  Elf_Word add_symbol(Elf_Word name, Elf64_Addr value, Elf_Xword size, unsigned char bind,
115  unsigned char type, unsigned char other, Elf_Half shndx) {
116  return add_symbol(name, value, size, ELF_ST_INFO(bind, type), other, shndx);
117  }
118 
119  //------------------------------------------------------------------------------
120  Elf_Word add_symbol(string_section_accessor& pStrWriter, const char* str, Elf64_Addr value,
121  Elf_Xword size, unsigned char info, unsigned char other, Elf_Half shndx) {
122  Elf_Word index = pStrWriter.add_string(str);
123  return add_symbol(index, value, size, info, other, shndx);
124  }
125 
126  //------------------------------------------------------------------------------
127  Elf_Word add_symbol(string_section_accessor& pStrWriter, const char* str, Elf64_Addr value,
128  Elf_Xword size, unsigned char bind, unsigned char type, unsigned char other,
129  Elf_Half shndx) {
130  return add_symbol(pStrWriter, str, value, size, ELF_ST_INFO(bind, type), other, shndx);
131  }
132 
133  //------------------------------------------------------------------------------
134  private:
135  //------------------------------------------------------------------------------
136  void find_hash_section() {
137  hash_section = 0;
138  hash_section_index = 0;
139  Elf_Half nSecNo = elf_file.sections.size();
140  for (Elf_Half i = 0; i < nSecNo && 0 == hash_section_index; ++i) {
141  const section* sec = elf_file.sections[i];
142  if (sec->get_link() == symbol_section->get_index()) {
143  hash_section = sec;
144  hash_section_index = i;
145  }
146  }
147  }
148 
149  //------------------------------------------------------------------------------
150  Elf_Half get_string_table_index() const { return (Elf_Half)symbol_section->get_link(); }
151 
152  //------------------------------------------------------------------------------
153  Elf_Half get_hash_table_index() const { return hash_section_index; }
154 
155  //------------------------------------------------------------------------------
156  template <class T>
157  bool generic_get_symbol(Elf_Xword index, std::string& name, Elf64_Addr& value, Elf_Xword& size,
158  unsigned char& bind, unsigned char& type, Elf_Half& section_index,
159  unsigned char& other) const {
160  bool ret = false;
161 
162  if (index < get_symbols_num()) {
163  const T* pSym = reinterpret_cast<const T*>(symbol_section->get_data() +
164  index * symbol_section->get_entry_size());
165 
166  const endianess_convertor& convertor = elf_file.get_convertor();
167 
168  section* string_section = elf_file.sections[get_string_table_index()];
169  string_section_accessor str_reader(string_section);
170  const char* pStr = str_reader.get_string(convertor(pSym->st_name));
171  if (0 != pStr) {
172  name = pStr;
173  }
174  value = convertor(pSym->st_value);
175  size = convertor(pSym->st_size);
176  bind = ELF_ST_BIND(pSym->st_info);
177  type = ELF_ST_TYPE(pSym->st_info);
178  section_index = convertor(pSym->st_shndx);
179  other = pSym->st_other;
180 
181  ret = true;
182  }
183 
184  return ret;
185  }
186 
187  //------------------------------------------------------------------------------
188  template <class T>
189  Elf_Word generic_add_symbol(Elf_Word name, Elf64_Addr value, Elf_Xword size, unsigned char info,
190  unsigned char other, Elf_Half shndx) {
191  const endianess_convertor& convertor = elf_file.get_convertor();
192 
193  T entry;
194  entry.st_name = convertor(name);
195  entry.st_value = value;
196  entry.st_value = convertor(entry.st_value);
197  entry.st_size = size;
198  entry.st_size = convertor(entry.st_size);
199  entry.st_info = convertor(info);
200  entry.st_other = convertor(other);
201  entry.st_shndx = convertor(shndx);
202 
203  symbol_section->append_data(reinterpret_cast<char*>(&entry), sizeof(entry));
204 
205  Elf_Word nRet = symbol_section->get_size() / sizeof(entry) - 1;
206 
207  return nRet;
208  }
209 
210  //------------------------------------------------------------------------------
211  private:
212  const elfio& elf_file;
213  section* symbol_section;
214  Elf_Half hash_section_index;
215  const section* hash_section;
216 };
217 
218 } // namespace ELFIO
219 
220 #endif // ELFIO_SYMBOLS_HPP
ELFIO::string_section_accessor
Definition: elfio_strings.hpp:33
ELFIO::endianess_convertor
Definition: elfio_utils.hpp:51
ELFIO::elfio
Definition: elfio.hpp:59
ELFIO::symbol_section_accessor
Definition: elfio_symbols.hpp:29
ELFIO::section
Definition: elfio_section.hpp:31