importloggingfrom.elfrelocimportELFRelocfrom.genericimport(GenericAbsoluteAddendReloc,GenericCopyReloc,GenericJumpslotReloc,GenericRelativeReloc,GenericTLSDoffsetReloc,GenericTLSModIdReloc,GenericTLSOffsetReloc,)log=logging.getLogger(name=__name__)arch="PPC32"# Reference: System V Application Binary Interface, PowerPC Processor Supplement# http://refspecs.linux-foundation.org/elf/elfspec_ppc.pdf# PPC constants/masks to be used in relocationsPPC_WORD32=0xFFFFFFFFPPC_WORD30=0xFFFFFFFCPPC_LOW24=0x03FFFFFCPPC_LOW14=0x0020FFFCPPC_HALF16=0xFFFFPPC_BL_INST=0x48000001
[docs]classR_PPC_ADDR14(ELFReloc):""" Relocation Type: 0x7 Calculation: (S + A) >> 2 Field: low14* """@propertydefvalue(self):A=self.addendS=self.resolvedby.rebased_addrresult=(S+A)>>2returnresult
[docs]classR_PPC_ADDR14_BRTAKEN(ELFReloc):""" Relocation Type: 0x8 Calculation: (S + A) >> 2 Field: low14* """@propertydefvalue(self):A=self.addendS=self.resolvedby.rebased_addrresult=(S+A)>>2returnresult
[docs]classR_PPC_ADDR14_BRNTAKEN(ELFReloc):""" Relocation Type: 0x9 Calculation: (S + A) >> 2 Field: low14* """@propertydefvalue(self):A=self.addendS=self.resolvedby.rebased_addrresult=(S+A)>>2returnresult
[docs]classR_PPC_REL24(ELFReloc):# pylint: disable=undefined-variable""" Relocation Type: 0xa Calculation: (S + A - P) >> 2 Field: low24* R_PPC_REL24 is a special type of relocation. The instruction must be modified for this type. This relocation type resolves branch-and-link instructions. Prior to relocation, all instances of the branch-and-link instruction will consist of the following bytecode: 48 00 00 01. The problem with this is that all instances will result in calls to the current address - thus an infinite loop. After calculating the relocation result in R_PPC_REL24, you will have an address offset to the call. The result must be resolved to the correct instruction encoding. """@propertydefvalue(self):A=self.addendS=self.resolvedby.rebased_addrP=self.rebased_addrresult=(S+A-P)>>2result=(result<<2)&PPC_LOW24result=(A&~PPC_LOW24)|resultresult=result|PPC_BL_INSTreturnresult
[docs]classR_PPC_REL14(ELFReloc):""" Relocation Type: 0xb Calculation: (S + A - P) >> 2 Field: low14* """@propertydefvalue(self):A=self.addendS=self.resolvedby.rebased_addrP=self.rebased_addrresult=(S+A-P)>>2result=(result<<2)&PPC_LOW14result=(A&~PPC_LOW14)|resultreturnresult
[docs]classR_PPC_REL14_BRTAKEN(ELFReloc):""" Relocation Type: 0xc Calculation: (S + A - P) >> 2 Field: low14* """@propertydefvalue(self):A=self.addendS=self.resolvedby.rebased_addrP=self.rebased_addrresult=(S+A-P)>>2result=(result<<2)&PPC_LOW14result=(A&~PPC_LOW14)|resultreturnresult
[docs]classR_PPC_REL14_BRNTAKEN(ELFReloc):""" Relocation Type: 0xd Calculation: (S + A - P) >> 2 Field: low14* """@propertydefvalue(self):A=self.addendS=self.resolvedby.rebased_addrP=self.rebased_addrresult=(S+A-P)>>2result=(result<<2)&PPC_LOW14result=(A&~PPC_LOW14)|resultreturnresult
[docs]defrelocate(self):if"DT_PPC_GOT"notinself.owner._dynamicand"DT_LOPROC"notinself.owner._dynamic:log.error("This binary is relocated incorrectly. See https://github.com/angr/cle/issues/142 for details.")super().relocate()
[docs]classR_PPC_UADDR32(ELFReloc):""" Relocation Type: 0x18 Calculation: S + A Field: word32 """@propertydefvalue(self):A=self.addendS=self.resolvedby.rebased_addrresult=S+Areturnresult
[docs]classR_PPC_UADDR16(ELFReloc):""" Relocation Type: 0x19 Calculation: S + A Field: half16* """@propertydefvalue(self):A=self.addendS=self.resolvedby.rebased_addrresult=S+Areturnresult
[docs]classR_PPC_REL32(ELFReloc):# pylint: disable=undefined-variable""" Relocation Type: 0x1a Calculation: S + A - P Field: word32 """@propertydefvalue(self):P=self.rebased_addrA=self.addendS=self.resolvedby.rebased_addrresult=(S+A-P)&PPC_WORD32returnresult
[docs]classR_PPC_SECTOFF(ELFReloc):""" Relocation Type: 0x21 Calculation: R + A Field: half16* """@propertydefvalue(self):R=self.relative_addrA=self.addendresult=R+Areturnresult