| 142 | |
| 143 | @exceptions_handler(Exception) |
| 144 | def print_sections(binary): |
| 145 | |
| 146 | f_title = "|{:<20}|{:<16}|{:<16}|{:<16}|{:16}|{:22}|{:19}|{:25}|{:25}|" |
| 147 | f_value = "|{:<20}|0x{:<13x} |0x{:<13x} |0x{:<13x} |0x{:<13x} |0x{:<19x} |0x{:<16x} |{:<25}|{:<25}" |
| 148 | |
| 149 | print("== Sections ==") |
| 150 | |
| 151 | print(f_title.format( |
| 152 | "Name", "Virtual Address", "Offset", "Size", |
| 153 | "Alignement", "Number of Relocations", "Relocation offset", |
| 154 | "Type", "Flags")) |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | for section in binary.sections: |
| 160 | flags_str = " - ".join([str(s).split(".")[-1] for s in section.flags_list]) |
| 161 | print(f_value.format( |
| 162 | section.name, |
| 163 | section.virtual_address, |
| 164 | section.offset, |
| 165 | section.size, |
| 166 | section.alignment, |
| 167 | section.numberof_relocations, |
| 168 | section.relocation_offset, |
| 169 | str(section.type).split(".")[-1], |
| 170 | flags_str)) |
| 171 | if len(section.relocations) > 0: |
| 172 | for idx, reloc in enumerate(section.relocations): |
| 173 | name = reloc.symbol.name if reloc.has_symbol else "" |
| 174 | secname = " - " + reloc.section.name if reloc.has_section else "" |
| 175 | type = str(reloc.type) |
| 176 | if reloc.architecture == MachO.Header.CPU_TYPE.X86: |
| 177 | type = str(MachO.X86_RELOCATION(reloc.type)) |
| 178 | |
| 179 | if reloc.architecture == MachO.Header.CPU_TYPE.x86_64: |
| 180 | type = str(MachO.X86_64_RELOCATION(reloc.type)) |
| 181 | |
| 182 | if reloc.architecture == MachO.Header.CPU_TYPE.ARM: |
| 183 | type = str(MachO.ARM_RELOCATION(reloc.type)) |
| 184 | |
| 185 | if reloc.architecture == MachO.Header.CPU_TYPE.ARM64: |
| 186 | type = str(MachO.ARM64_RELOCATION(reloc.type)) |
| 187 | |
| 188 | if reloc.architecture == MachO.Header.CPU_TYPE.POWERPC: |
| 189 | type = str(MachO.PPC_RELOCATION(reloc.type)) |
| 190 | |
| 191 | |
| 192 | print(" [Reloc #{:d} {section}] {name:<10} 0x{address:<6x} {type:<20} {size:d} {pcrel} {scat}".format( |
| 193 | idx, |
| 194 | section=secname, |
| 195 | name=name, |
| 196 | address=reloc.address, |
| 197 | type=type.split(".")[-1], |
| 198 | size=reloc.size, |
| 199 | pcrel=str(reloc.pc_relative), |
| 200 | scat=str(reloc.is_scattered))) |
| 201 | print("") |