10 # possible values for EI_CLASS
15 # possible value for EI_VERSION
18 # possible values for EI_DATA
23 def my_assert(self
, expectation
, result
):
24 if not expectation
== result
:
25 #print "'%x','%x' %s" % (ord(expectation), ord(result), self.name)
26 raise Exception("This does not work as expected")
28 def __init__(self
, name
, bits32
):
33 self
.file = file(self
.name
, "r")
34 self
.data
= self
.file.read(ELFFile
.EI_NIDENT
+4)
36 self
.my_assert(len(self
.data
), ELFFile
.EI_NIDENT
+4)
37 self
.my_assert(self
.data
[0], chr(0x7f) )
38 self
.my_assert(self
.data
[1], 'E')
39 self
.my_assert(self
.data
[2], 'L')
40 self
.my_assert(self
.data
[3], 'F')
42 self
.my_assert(self
.data
[ELFFile
.EI_CLASS
], chr(ELFFile
.ELFCLASS32
))
44 self
.my_assert(self
.data
[ELFFile
.EI_CLASS
], chr(ELFFile
.ELFCLASS64
))
45 self
.my_assert(self
.data
[ELFFile
.EI_VERSION
], chr(ELFFile
.EV_CURRENT
) )
47 self
.sex
= self
.data
[ELFFile
.EI_DATA
]
48 if self
.sex
== chr(ELFFile
.ELFDATANONE
):
49 raise Exception("self.sex == ELFDATANONE")
50 elif self
.sex
== chr(ELFFile
.ELFDATA2LSB
):
52 elif self
.sex
== chr(ELFFile
.ELFDATA2MSB
):
55 raise Exception("Unknown self.sex")
58 return ord(self
.data
[ELFFile
.EI_OSABI
])
61 return ord(self
.data
[ELFFile
.EI_ABIVERSION
])
63 def isLittleEndian(self
):
64 return self
.sex
== "<"
66 def isBigEngian(self
):
67 return self
.sex
== ">"
71 We know the sex stored in self.sex and we
75 (a
,) = struct
.unpack(self
.sex
+"H", self
.data
[18:20])