Compare commits

...

2 Commits

Author SHA1 Message Date
Nicolas Kruse e4813006a5 bits attibute for relocations added, some docstring fixes and table output extended 2025-03-04 21:54:47 +01:00
Nicolas Kruse ea9660dd8e Bit number for relocation types fixed 2025-03-04 21:51:12 +01:00
2 changed files with 42 additions and 34 deletions

View File

@ -213,13 +213,17 @@ class elf_relocation():
Attributes:
file: Points to the parent ELF file object.
index: Absolut index of the relocation the associated relocation section
index: Absolut index of the relocation in the associated relocation section
symbol: Symbol to relocate
type: Type of the relocation
calculation: Description of the relocation calculation
target_section: Pointing to the section that is relocation applies to
bits: number ob bits to patch by the relocation
target_section: Pointing to the section where this relocation applies to
fields: All relocation header fields as dict
"""
@ -231,11 +235,13 @@ class elf_relocation():
self.symbol = file.symbols[symbol_index]
reloc_types = fdat.relocation_table_types.get(file.architecture)
if reloc_types and relocation_type in reloc_types:
self.calculation = reloc_types[relocation_type][2]
self.type = reloc_types[relocation_type][0]
self.bits = reloc_types[relocation_type][1]
self.calculation = reloc_types[relocation_type][2]
else:
self.calculation = ''
self.type = str(relocation_type)
self.bits = 0
self.calculation = ''
self.target_section: elf_section = file.sections[sh_info]
def __getitem__(self, key: str | int) -> int:
@ -248,7 +254,9 @@ class elf_relocation():
def __repr__(self) -> str:
return f'index {self.symbol.index}\n' +\
f'symbol {self.symbol.name}\n' +\
f'relocation type {self.type} ({self.calculation})\n' +\
f'relocation type {self.type}\n' +\
f'calculation {self.calculation}\n' +\
f'bits {self.bits}\n' +\
'\n'.join(f'{k:18} {v:4}' for k, v in self.fields.items()) + '\n'
@ -348,10 +356,10 @@ class relocation_list(elf_list[elf_relocation]):
"""A class for representing a list of ELF relocations
"""
def _compact_table(self) -> tuple[list[str], list[list[int | str]], list[str]]:
columns = ['index', 'symbol name', 'type', 'calculation']
columns = ['index', 'symbol name', 'type', 'calculation', 'bits']
data: list[list[str | int]] = [[item.index, item.symbol.name, item.type,
item.calculation] for item in self]
return columns, data, ['index']
item.calculation, item.bits] for item in self]
return columns, data, ['index', 'bits']
class elf_file:

View File

@ -525,9 +525,9 @@ relocation_table_types = {
0: ("R_RISCV_NONE", 0, ""),
1: ("R_RISCV_32", 32, "S + A"),
2: ("R_RISCV_64", 64, "S + A"),
3: ("R_RISCV_RELATIVE", 255, "B + A"),
3: ("R_RISCV_RELATIVE", 64, "B + A"),
4: ("R_RISCV_COPY", 0, ""),
5: ("R_RISCV_JUMP_SLOT", 255, "S"),
5: ("R_RISCV_JUMP_SLOT", 0, "S"),
6: ("R_RISCV_TLS_DTPMOD32", 32, "TLSMODULE"),
7: ("R_RISCV_TLS_DTPMOD64", 64, "TLSMODULE"),
8: ("R_RISCV_TLS_DTPREL32", 32, "S + A - TLS_DTV_OFFSET"),
@ -535,22 +535,22 @@ relocation_table_types = {
10: ("R_RISCV_TLS_TPREL32", 32, "S + A + TLSOFFSET"),
11: ("R_RISCV_TLS_TPREL64", 64, "S + A + TLSOFFSET"),
12: ("R_RISCV_TLSDESC", 0, "TLSDESC(S+A)"),
16: ("R_RISCV_BRANCH", 254, "S + A - P"),
17: ("R_RISCV_JAL", 248, "S + A - P"),
18: ("R_RISCV_CALL", 246, "S + A - P"),
19: ("R_RISCV_CALL_PLT", 246, "S + A - P"),
20: ("R_RISCV_GOT_HI20", 249, "G + GOT + A - P"),
21: ("R_RISCV_TLS_GOT_HI20", 249, ""),
22: ("R_RISCV_TLS_GD_HI20", 249, ""),
23: ("R_RISCV_PCREL_HI20", 249, "S + A - P"),
24: ("R_RISCV_PCREL_LO12_I", 251, "S - P"),
25: ("R_RISCV_PCREL_LO12_S", 250, "S - P"),
26: ("R_RISCV_HI20", 249, "S + A"),
27: ("R_RISCV_LO12_I", 251, "S + A"),
28: ("R_RISCV_LO12_S", 250, "S + A"),
29: ("R_RISCV_TPREL_HI20", 249, ""),
30: ("R_RISCV_TPREL_LO12_I", 251, ""),
31: ("R_RISCV_TPREL_LO12_S", 250, ""),
16: ("R_RISCV_BRANCH", 0, "S + A - P"),
17: ("R_RISCV_JAL", 0, "S + A - P"),
18: ("R_RISCV_CALL", 0, "S + A - P"),
19: ("R_RISCV_CALL_PLT", 0, "S + A - P"),
20: ("R_RISCV_GOT_HI20", 0, "G + GOT + A - P"),
21: ("R_RISCV_TLS_GOT_HI20", 0, ""),
22: ("R_RISCV_TLS_GD_HI20", 0, ""),
23: ("R_RISCV_PCREL_HI20", 0, "S + A - P"),
24: ("R_RISCV_PCREL_LO12_I", 0, "S - P"),
25: ("R_RISCV_PCREL_LO12_S", 0, "S - P"),
26: ("R_RISCV_HI20", 0, "S + A"),
27: ("R_RISCV_LO12_I", 0, "S + A"),
28: ("R_RISCV_LO12_S", 0, "S + A"),
29: ("R_RISCV_TPREL_HI20", 0, ""),
30: ("R_RISCV_TPREL_LO12_I", 0, ""),
31: ("R_RISCV_TPREL_LO12_S", 0, ""),
32: ("R_RISCV_TPREL_ADD", 0, ""),
33: ("R_RISCV_ADD8", 8, "V + S + A"),
34: ("R_RISCV_ADD16", 16, "V + S + A"),
@ -563,8 +563,8 @@ relocation_table_types = {
41: ("R_RISCV_GOT32_PCREL", 32, "G + GOT + A - P"),
42: ("R_RISCV_Reserved", 0, ""),
43: ("R_RISCV_ALIGN", 0, ""),
44: ("R_RISCV_RVC_BRANCH", 253, "S + A - P"),
45: ("R_RISCV_RVC_JUMP", 252, "S + A - P"),
44: ("R_RISCV_RVC_BRANCH", 0, "S + A - P"),
45: ("R_RISCV_RVC_JUMP", 0, "S + A - P"),
46: ("R_RISCV_Reserved", 0, ""),
51: ("R_RISCV_RELAX", 0, ""),
52: ("R_RISCV_SUB6", 6, "V - S - A"),
@ -573,13 +573,13 @@ relocation_table_types = {
55: ("R_RISCV_SET16", 16, "S + A"),
56: ("R_RISCV_SET32", 32, "S + A"),
57: ("R_RISCV_32_PCREL", 32, "S + A - P"),
58: ("R_RISCV_IRELATIVE", 255, "ifunc_resolver(B + A)"),
58: ("R_RISCV_IRELATIVE", 0, "ifunc_resolver(B + A)"),
59: ("R_RISCV_PLT32", 32, "S + A - P"),
60: ("R_RISCV_SET_ULEB128", 247, "S + A"),
61: ("R_RISCV_SUB_ULEB128", 247, "V - S - A"),
62: ("R_RISCV_TLSDESC_HI20", 249, "S + A - P"),
63: ("R_RISCV_TLSDESC_LOAD_LO12", 251, "S - P"),
64: ("R_RISCV_TLSDESC_ADD_LO12", 251, "S - P"),
60: ("R_RISCV_SET_ULEB128", 0, "S + A"),
61: ("R_RISCV_SUB_ULEB128", 0, "V - S - A"),
62: ("R_RISCV_TLSDESC_HI20", 0, "S + A - P"),
63: ("R_RISCV_TLSDESC_LOAD_LO12", 0, "S - P"),
64: ("R_RISCV_TLSDESC_ADD_LO12", 0, "S - P"),
65: ("R_RISCV_TLSDESC_CALL", 0, ""),
191: ("R_RISCV_VENDOR", 0, "")
}