Navigation Menu

Skip to content

Commit

Permalink
Add relocation size
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Jun 28, 2017
1 parent f7617f0 commit f1766f2
Show file tree
Hide file tree
Showing 18 changed files with 996 additions and 174 deletions.
4 changes: 4 additions & 0 deletions api/python/ELF/objects/pyRelocation.cpp
Expand Up @@ -66,6 +66,10 @@ void init_ELF_Relocation_class(py::module& m) {
static_cast<getter_t<bool>>(&Relocation::is_rel),
"``True`` if the relocation doesn't use the :attr:`~lief.ELF.Relocation.addend` proprety")

.def_property_readonly("size",
static_cast<getter_t<uint32_t>>(&Relocation::size),
"Size in **bits** of the value patched by the relocation")


.def("__eq__", &Relocation::operator==)
.def("__ne__", &Relocation::operator!=)
Expand Down
133 changes: 133 additions & 0 deletions api/python/ELF/pyELFStructures.cpp
Expand Up @@ -700,6 +700,12 @@ void init_ELF_Structures_enum(py::module& m) {
.value(PY_ENUM(RELOC_ARM::R_ARM_THM_TLS_DESCSEQ16))
.value(PY_ENUM(RELOC_ARM::R_ARM_THM_TLS_DESCSEQ32))
.value(PY_ENUM(RELOC_ARM::R_ARM_IRELATIVE))
.value(PY_ENUM(RELOC_ARM::R_ARM_RXPC25))
.value(PY_ENUM(RELOC_ARM::R_ARM_RSBREL32))
.value(PY_ENUM(RELOC_ARM::R_ARM_THM_RPC22))
.value(PY_ENUM(RELOC_ARM::R_ARM_RREL32))
.value(PY_ENUM(RELOC_ARM::R_ARM_RPC24))
.value(PY_ENUM(RELOC_ARM::R_ARM_RBASE))
.export_values();


Expand Down Expand Up @@ -748,6 +754,133 @@ void init_ELF_Structures_enum(py::module& m) {
.export_values();


py::enum_<RELOC_AARCH64>(m, "RELOCATION_AARCH64")
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_NONE))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_ABS64))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_ABS32))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_ABS16))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_PREL64))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_PREL32))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_PREL16))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_UABS_G0))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_UABS_G0_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_UABS_G1))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_UABS_G1_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_UABS_G2))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_UABS_G2_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_UABS_G3))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_SABS_G0))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_SABS_G1))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_SABS_G2))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_LD_PREL_LO19))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_ADR_PREL_LO21))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_ADR_PREL_PG_HI21))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_ADR_PREL_PG_HI21_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_ADD_ABS_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_LDST8_ABS_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TSTBR14))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_CONDBR19))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_JUMP26))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_CALL26))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_LDST16_ABS_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_LDST32_ABS_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_LDST64_ABS_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_PREL_G0))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_PREL_G0_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_PREL_G1))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_PREL_G1_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_PREL_G2))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_PREL_G2_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_PREL_G3))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_LDST128_ABS_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_GOTOFF_G0))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_GOTOFF_G0_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_GOTOFF_G1))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_GOTOFF_G1_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_GOTOFF_G2))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_GOTOFF_G2_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_MOVW_GOTOFF_G3))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_GOTREL64))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_GOTREL32))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_GOT_LD_PREL19))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_LD64_GOTOFF_LO15))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_ADR_GOT_PAGE))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_LD64_GOT_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_LD64_GOTPAGE_LO15))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSGD_ADR_PREL21))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSGD_ADR_PAGE21))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSGD_ADD_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSGD_MOVW_G1))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSGD_MOVW_G0_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_ADR_PREL21))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_ADR_PAGE21))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_ADD_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_MOVW_G1))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_MOVW_G0_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_LD_PREL19))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_MOVW_DTPREL_G2))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_MOVW_DTPREL_G1))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_MOVW_DTPREL_G0))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_ADD_DTPREL_HI12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_ADD_DTPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_LDST8_DTPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_LDST16_DTPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_LDST32_DTPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_LDST64_DTPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_MOVW_TPREL_G2))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_MOVW_TPREL_G1))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_MOVW_TPREL_G0))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_ADD_TPREL_HI12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_ADD_TPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_LDST8_TPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_LDST16_TPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_LDST32_TPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_LDST64_TPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSDESC_LD_PREL19))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSDESC_ADR_PREL21))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSDESC_ADR_PAGE21))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSDESC_LD64_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSDESC_ADD_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSDESC_OFF_G1))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSDESC_OFF_G0_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSDESC_LDR))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSDESC_ADD))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSDESC_CALL))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_LDST128_TPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_LDST128_DTPREL_LO12))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSLD_LDST128_DTPREL_LO12_NC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_COPY))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_GLOB_DAT))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_JUMP_SLOT))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_RELATIVE))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLS_DTPREL64))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLS_DTPMOD64))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLS_TPREL64))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_TLSDESC))
.value(PY_ENUM(RELOC_AARCH64::R_AARCH64_IRELATIVE))
.export_values();


py::enum_<DYNSYM_COUNT_METHODS>(m, "DYNSYM_COUNT_METHODS")
.value(PY_ENUM(DYNSYM_COUNT_METHODS::COUNT_AUTO))
.value(PY_ENUM(DYNSYM_COUNT_METHODS::COUNT_SECTION))
Expand Down
52 changes: 35 additions & 17 deletions examples/python/elf_reader.py
Expand Up @@ -176,10 +176,10 @@ def print_relocations(binary):
## Dynamic relocations ##
if len(dynamicrelocations) > 0:
print("== Dynamic Relocations ==\n")
f_title = "|{:<10} | {:<10}| {:<30} |"
f_value = "|0x{:<8x} | {:<10}| {:<30} |"
f_title = "|{:<10} | {:<10}| {:<8}| {:<30} |"
f_value = "|0x{:<8x} | {:<10}| {:<8d}| {:<30} |"

print(f_title.format("Address", "Type", "Symbol"))
print(f_title.format("Address", "Type", "Size", "Symbol"))

for relocation in dynamicrelocations:
type = str(relocation.type)
Expand All @@ -189,19 +189,24 @@ def print_relocations(binary):
type = str(ELF.RELOCATION_i386(relocation.type))
elif binary.header.machine_type == ELF.ARCH.ARM:
type = str(ELF.RELOCATION_ARM(relocation.type))
try:
s = relocation.symbol
print(f_value.format(relocation.address, type.split(".")[-1], str(relocation.symbol.name)))
except lief.not_found:
pass
elif binary.header.machine_type == ELF.ARCH.AARCH64:
type = str(ELF.RELOCATION_AARCH64(relocation.type))

symbol_name = str(relocation.symbol.name) if relocation.has_symbol else ""

print(f_value.format(
relocation.address,
type.split(".")[-1],
relocation.size,
symbol_name))


if len(pltgot_relocations) > 0:
print("== PLT/GOT Relocations ==\n")
f_title = "|{:<10} | {:<10} |{:<30}|"
f_value = "|0x{:<8x} | {:<10} | {:<30}|"
f_title = "|{:<10} | {:<10}| {:<8}| {:<30} |"
f_value = "|0x{:<8x} | {:<10}| {:<8d}| {:<30} |"

print(f_title.format("Address", "Type", "Symbol"))
print(f_title.format("Address", "Type", "Size", "Symbol"))

for relocation in pltgot_relocations:
type = str(relocation.type)
Expand All @@ -211,9 +216,15 @@ def print_relocations(binary):
type = str(ELF.RELOCATION_i386(relocation.type))
elif binary.header.machine_type == ELF.ARCH.ARM:
type = str(ELF.RELOCATION_ARM(relocation.type))
elif binary.header.machine_type == ELF.ARCH.AARCH64:
type = str(ELF.RELOCATION_AARCH64(relocation.type))

symbol_name = str(relocation.symbol.name) if relocation.has_symbol else ""
print(f_value.format(relocation.address, type.split(".")[-1], symbol_name))
print(f_value.format(
relocation.address,
type.split(".")[-1],
relocation.size,
symbol_name))

def print_exported_symbols(binary):
symbols = binary.exported_symbols
Expand Down Expand Up @@ -311,15 +322,22 @@ def print_notes(binary):
print("Note #{:d}".format(idx))

print(format_str.format("Name:", note.name))
print(format_str.format("Type:", ELF.NOTE_TYPES(note.type)))
print(format_str.format("Type:", str(ELF.NOTE_TYPES(note.type)).split(".")[-1]))
print(format_str.format("Description:", description_str))

if ELF.NOTE_TYPES(note.type) == ELF.NOTE_TYPES.ABI_TAG:
version = note.version
version_str = "{:d}.{:d}.{:d}".format(version[0], version[1], version[2])
try:
version = note.version
version_str = "{:d}.{:d}.{:d}".format(version[0], version[1], version[2])

print(format_str.format("ABI:", note.abi))
print(format_str.format("Version:", version_str))
except lief.corrupted:
pass

if ELF.NOTE_TYPES(note.type) == ELF.NOTE_TYPES.GOLD_VERSION:
print(format_str.format("Version:", "".join(map(chr, note.description))))

print(format_str.format("ABI:", note.abi))
print(format_str.format("Version:", version_str))

print("\n")

Expand Down
Empty file modified examples/python/macho_reader.py 100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions include/LIEF/ELF/EnumToString.hpp
Expand Up @@ -33,6 +33,7 @@ DLL_PUBLIC const char* to_string(SYMBOL_TYPES e);
DLL_PUBLIC const char* to_string(RELOC_x86_64 e);
DLL_PUBLIC const char* to_string(RELOC_ARM e);
DLL_PUBLIC const char* to_string(RELOC_i386 e);
DLL_PUBLIC const char* to_string(RELOC_AARCH64 e);
DLL_PUBLIC const char* to_string(ELF_CLASS e);
DLL_PUBLIC const char* to_string(ELF_DATA e);
DLL_PUBLIC const char* to_string(OS_ABI e);
Expand Down
4 changes: 4 additions & 0 deletions include/LIEF/ELF/Relocation.hpp
Expand Up @@ -17,6 +17,7 @@
#define LIEF_ELF_RELOCATION_H_

#include <string>
#include <map>
#include <iostream>

#include "LIEF/Visitable.hpp"
Expand Down Expand Up @@ -53,6 +54,9 @@ class DLL_PUBLIC Relocation : public Visitable {
bool is_rel(void) const;
ARCH architecture(void) const;

//! @brief Return the **bit** size of the value to patch
uint32_t size(void) const;

bool has_symbol(void) const;
Symbol& symbol(void);
const Symbol& symbol(void) const;
Expand Down

0 comments on commit f1766f2

Please sign in to comment.