23 #ifndef ELFIO_RELOCATION_HPP
24 #define ELFIO_RELOCATION_HPP
32 static int get_r_sym(Elf_Xword info) {
return ELF32_R_SYM((Elf_Word)info); }
33 static int get_r_type(Elf_Xword info) {
return ELF32_R_TYPE((Elf_Word)info); }
37 static int get_r_sym(Elf_Xword info) {
return ELF32_R_SYM((Elf_Word)info); }
38 static int get_r_type(Elf_Xword info) {
return ELF32_R_TYPE((Elf_Word)info); }
42 static int get_r_sym(Elf_Xword info) {
return ELF64_R_SYM(info); }
43 static int get_r_type(Elf_Xword info) {
return ELF64_R_TYPE(info); }
47 static int get_r_sym(Elf_Xword info) {
return ELF64_R_SYM(info); }
48 static int get_r_type(Elf_Xword info) {
return ELF64_R_TYPE(info); }
57 : elf_file(elf_file_), relocation_section(section_) {}
60 Elf_Xword get_entries_num()
const {
63 if (0 != relocation_section->get_entry_size()) {
64 nRet = relocation_section->get_size() / relocation_section->get_entry_size();
71 bool get_entry(Elf_Xword index, Elf64_Addr& offset, Elf_Word& symbol, Elf_Word& type,
72 Elf_Sxword& addend)
const {
73 if (index >= get_entries_num()) {
77 if (elf_file.get_class() == ELFCLASS32) {
78 if (SHT_REL == relocation_section->get_type()) {
79 generic_get_entry_rel<Elf32_Rel>(index, offset, symbol, type, addend);
80 }
else if (SHT_RELA == relocation_section->get_type()) {
81 generic_get_entry_rela<Elf32_Rela>(index, offset, symbol, type, addend);
84 if (SHT_REL == relocation_section->get_type()) {
85 generic_get_entry_rel<Elf64_Rel>(index, offset, symbol, type, addend);
86 }
else if (SHT_RELA == relocation_section->get_type()) {
87 generic_get_entry_rela<Elf64_Rela>(index, offset, symbol, type, addend);
95 bool get_entry(Elf_Xword index, Elf64_Addr& offset, Elf64_Addr& symbolValue,
96 std::string& symbolName, Elf_Word& type, Elf_Sxword& addend,
97 Elf_Sxword& calcValue)
const {
100 bool ret = get_entry(index, offset, symbol, type, addend);
105 unsigned char symbolType;
110 ret = ret && symbols.get_symbol(symbol, symbolName, symbolValue, size, bind, symbolType,
119 calcValue = symbolValue + addend;
122 calcValue = symbolValue + addend - offset;
135 calcValue = symbolValue;
156 void add_entry(Elf64_Addr offset, Elf_Xword info) {
157 if (elf_file.get_class() == ELFCLASS32) {
158 generic_add_entry<Elf32_Rel>(offset, info);
160 generic_add_entry<Elf64_Rel>(offset, info);
165 void add_entry(Elf64_Addr offset, Elf_Word symbol,
unsigned char type) {
167 if (elf_file.get_class() == ELFCLASS32) {
168 info = ELF32_R_INFO((Elf_Xword)symbol, type);
170 info = ELF64_R_INFO((Elf_Xword)symbol, type);
173 add_entry(offset, info);
177 void add_entry(Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend) {
178 if (elf_file.get_class() == ELFCLASS32) {
179 generic_add_entry<Elf32_Rela>(offset, info, addend);
181 generic_add_entry<Elf64_Rela>(offset, info, addend);
186 void add_entry(Elf64_Addr offset, Elf_Word symbol,
unsigned char type, Elf_Sxword addend) {
188 if (elf_file.get_class() == ELFCLASS32) {
189 info = ELF32_R_INFO((Elf_Xword)symbol, type);
191 info = ELF64_R_INFO((Elf_Xword)symbol, type);
194 add_entry(offset, info, addend);
200 unsigned char sym_info,
unsigned char other, Elf_Half shndx, Elf64_Addr offset,
201 unsigned char type) {
202 Elf_Word str_index = str_writer.add_string(str);
203 Elf_Word sym_index = sym_writer.add_symbol(str_index, value, size, sym_info, other, shndx);
204 add_entry(offset, sym_index, type);
210 Elf_Half get_symbol_table_index()
const {
return (Elf_Half)relocation_section->get_link(); }
214 void generic_get_entry_rel(Elf_Xword index, Elf64_Addr& offset, Elf_Word& symbol,
215 Elf_Word& type, Elf_Sxword& addend)
const {
218 const T* pEntry =
reinterpret_cast<const T*
>(relocation_section->get_data() +
219 index * relocation_section->get_entry_size());
220 offset = convertor(pEntry->r_offset);
221 Elf_Xword tmp = convertor(pEntry->r_info);
229 void generic_get_entry_rela(Elf_Xword index, Elf64_Addr& offset, Elf_Word& symbol,
230 Elf_Word& type, Elf_Sxword& addend)
const {
233 const T* pEntry =
reinterpret_cast<const T*
>(relocation_section->get_data() +
234 index * relocation_section->get_entry_size());
235 offset = convertor(pEntry->r_offset);
236 Elf_Xword tmp = convertor(pEntry->r_info);
239 addend = convertor(pEntry->r_addend);
244 void generic_add_entry(Elf64_Addr offset, Elf_Xword info) {
248 entry.r_offset = offset;
250 entry.r_offset = convertor(entry.r_offset);
251 entry.r_info = convertor(entry.r_info);
253 relocation_section->append_data(
reinterpret_cast<char*
>(&entry),
sizeof(entry));
258 void generic_add_entry(Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend) {
262 entry.r_offset = offset;
264 entry.r_addend = addend;
265 entry.r_offset = convertor(entry.r_offset);
266 entry.r_info = convertor(entry.r_info);
267 entry.r_addend = convertor(entry.r_addend);
269 relocation_section->append_data(
reinterpret_cast<char*
>(&entry),
sizeof(entry));
274 const elfio& elf_file;
280 #endif // ELFIO_RELOCATION_HPP