def print_relocations(binary): print("== Relocations ==") f_value = "|0x{address:<10x} | {size:<4d} | {type:<15} | {pcrel:<11} | {secseg:<23} | {symbol}" f_title = "|{address:<12} | {size:<4} | {type:<15} | {pcrel:<11} | {secseg:<23} | {symbol}" print(f_title.format( address="Address", size="Size", type="Type", pcrel="PC Relative", secseg="Section/Section", symbol="Symbol")) for reloc in binary.relocations: type_str = "" if reloc.origin == lief.MachO.RELOCATION_ORIGINS.DYLDINFO: type_str = str(lief.MachO.REBASE_TYPES(reloc.type)).split(".")[-1] if reloc.origin == lief.MachO.RELOCATION_ORIGINS.RELOC_TABLE: if reloc.architecture == MachO.CPU_TYPES.x86: type_str = str(MachO.X86_RELOCATION(reloc.type)) if reloc.architecture == MachO.CPU_TYPES.x86_64: type_str = str(MachO.X86_64_RELOCATION(reloc.type)) if reloc.architecture == MachO.CPU_TYPES.ARM: type_str = str(MachO.ARM_RELOCATION(reloc.type)) if reloc.architecture == MachO.CPU_TYPES.ARM64: type_str = str(MachO.ARM64_RELOCATION(reloc.type)) if reloc.architecture == MachO.CPU_TYPES.POWERPC: type_str = str(MachO.PPC_RELOCATION(reloc.type)) type_str = type_str.split(".")[-1] symbol_name = "" if reloc.has_symbol: symbol_name = reloc.symbol.name secseg_name = "" if reloc.has_segment and reloc.has_section: secseg_name = "{}.{}".format(reloc.segment.name, reloc.section.name) else: if reloc.has_segment: secseg_name = reloc.segment.name if reloc.has_section: secseg_name = reloc.section.name print(f_value.format( address=reloc.address, size=reloc.size, type=type_str, pcrel=str(reloc.pc_relative), secseg=secseg_name, symbol=symbol_name)) print("")
def print_sections(binary): f_title = "|{:<20}|{:<16}|{:<16}|{:<16}|{:16}|{:22}|{:19}|{:25}|{:25}|" f_value = "|{:<20}|0x{:<13x} |0x{:<13x} |0x{:<13x} |0x{:<13x} |0x{:<19x} |0x{:<16x} |{:<25}|{:<25}" print("== Sections ==") print(f_title.format( "Name", "Virtual Address", "Offset", "Size", "Alignement", "Number of Relocations", "Relocation offset", "Type", "Flags")) for section in binary.sections: flags_str = " - ".join([str(s).split(".")[-1] for s in section.flags_list]) print(f_value.format( section.name, section.virtual_address, section.offset, section.size, section.alignment, section.numberof_relocations, section.relocation_offset, str(section.type).split(".")[-1], flags_str)) if len(section.relocations) > 0: for idx, reloc in enumerate(section.relocations): name = reloc.symbol.name if reloc.has_symbol else "" secname = " - " + reloc.section.name if reloc.has_section else "" type = str(reloc.type) if reloc.architecture == MachO.CPU_TYPES.x86: type = str(MachO.X86_RELOCATION(reloc.type)) if reloc.architecture == MachO.CPU_TYPES.x86_64: type = str(MachO.X86_64_RELOCATION(reloc.type)) if reloc.architecture == MachO.CPU_TYPES.ARM: type = str(MachO.ARM_RELOCATION(reloc.type)) if reloc.architecture == MachO.CPU_TYPES.ARM64: type = str(MachO.ARM64_RELOCATION(reloc.type)) if reloc.architecture == MachO.CPU_TYPES.POWERPC: type = str(MachO.PPC_RELOCATION(reloc.type)) print(" [Reloc #{:d} {section}] {name:<10} 0x{address:<6x} {type:<20} {size:d} {pcrel} {scat}".format( idx, section=secname, name=name, address=reloc.address, type=type.split(".")[-1], size=reloc.size, pcrel=str(reloc.pc_relative), scat=str(reloc.is_scattered))) print("") print("")