23 #ifndef ELFIO_SYMBOLS_HPP
24 #define ELFIO_SYMBOLS_HPP
33 : elf_file(elf_file_), symbol_section(symbol_section_) {
38 Elf_Xword get_symbols_num()
const {
40 if (0 != symbol_section->get_entry_size()) {
41 nRet = symbol_section->get_size() / symbol_section->get_entry_size();
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 {
53 if (elf_file.get_class() == ELFCLASS32) {
54 ret = generic_get_symbol<Elf32_Sym>(index, name, value, size, bind, type, section_index,
57 ret = generic_get_symbol<Elf64_Sym>(index, name, value, size, bind, type, section_index,
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 {
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());
76 *(Elf_Word*)(hash_section->get_data() + (2 + val % nbucket) *
sizeof(Elf_Word));
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);
92 Elf_Word add_symbol(Elf_Word name, Elf64_Addr value, Elf_Xword size,
unsigned char info,
93 unsigned char other, Elf_Half shndx) {
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);
100 nRet = generic_add_symbol<Elf64_Sym>(0, 0, 0, 0, 0, 0);
104 if (elf_file.get_class() == ELFCLASS32) {
105 nRet = generic_add_symbol<Elf32_Sym>(name, value, size, info, other, shndx);
107 nRet = generic_add_symbol<Elf64_Sym>(name, value, size, info, other, shndx);
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);
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);
128 Elf_Xword size,
unsigned char bind,
unsigned char type,
unsigned char other,
130 return add_symbol(pStrWriter, str, value, size, ELF_ST_INFO(bind, type), other, shndx);
136 void find_hash_section() {
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()) {
144 hash_section_index = i;
150 Elf_Half get_string_table_index()
const {
return (Elf_Half)symbol_section->get_link(); }
153 Elf_Half get_hash_table_index()
const {
return hash_section_index; }
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 {
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());
168 section* string_section = elf_file.sections[get_string_table_index()];
170 const char* pStr = str_reader.get_string(convertor(pSym->st_name));
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;
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) {
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);
203 symbol_section->append_data(
reinterpret_cast<char*
>(&entry),
sizeof(entry));
205 Elf_Word nRet = symbol_section->get_size() /
sizeof(entry) - 1;
212 const elfio& elf_file;
214 Elf_Half hash_section_index;
220 #endif // ELFIO_SYMBOLS_HPP