blob: 3cfeee737b409c778dae0c2962d5536cd524b83f [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001import os, struct
2
3class NotELFFileError(Exception):
4 pass
5
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006class ELFFile:
7 EI_NIDENT = 16
8
9 EI_CLASS = 4
10 EI_DATA = 5
11 EI_VERSION = 6
12 EI_OSABI = 7
13 EI_ABIVERSION = 8
14
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050015 E_MACHINE = 0x12
16
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017 # possible values for EI_CLASS
18 ELFCLASSNONE = 0
19 ELFCLASS32 = 1
20 ELFCLASS64 = 2
21
22 # possible value for EI_VERSION
23 EV_CURRENT = 1
24
25 # possible values for EI_DATA
26 ELFDATANONE = 0
27 ELFDATA2LSB = 1
28 ELFDATA2MSB = 2
29
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050030 PT_INTERP = 3
31
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032 def my_assert(self, expectation, result):
33 if not expectation == result:
34 #print "'%x','%x' %s" % (ord(expectation), ord(result), self.name)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050035 raise NotELFFileError("%s is not an ELF" % self.name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036
37 def __init__(self, name, bits = 0):
38 self.name = name
39 self.bits = bits
40 self.objdump_output = {}
41
42 def open(self):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050043 if not os.path.isfile(self.name):
44 raise NotELFFileError("%s is not a normal file" % self.name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050046 self.file = file(self.name, "r")
47 # Read 4k which should cover most of the headers we're after
48 self.data = self.file.read(4096)
49
50 if len(self.data) < ELFFile.EI_NIDENT + 4:
51 raise NotELFFileError("%s is not an ELF" % self.name)
52
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053 self.my_assert(self.data[0], chr(0x7f) )
54 self.my_assert(self.data[1], 'E')
55 self.my_assert(self.data[2], 'L')
56 self.my_assert(self.data[3], 'F')
57 if self.bits == 0:
58 if self.data[ELFFile.EI_CLASS] == chr(ELFFile.ELFCLASS32):
59 self.bits = 32
60 elif self.data[ELFFile.EI_CLASS] == chr(ELFFile.ELFCLASS64):
61 self.bits = 64
62 else:
63 # Not 32-bit or 64.. lets assert
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050064 raise NotELFFileError("ELF but not 32 or 64 bit.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065 elif self.bits == 32:
66 self.my_assert(self.data[ELFFile.EI_CLASS], chr(ELFFile.ELFCLASS32))
67 elif self.bits == 64:
68 self.my_assert(self.data[ELFFile.EI_CLASS], chr(ELFFile.ELFCLASS64))
69 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050070 raise NotELFFileError("Must specify unknown, 32 or 64 bit size.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 self.my_assert(self.data[ELFFile.EI_VERSION], chr(ELFFile.EV_CURRENT) )
72
73 self.sex = self.data[ELFFile.EI_DATA]
74 if self.sex == chr(ELFFile.ELFDATANONE):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050075 raise NotELFFileError("self.sex == ELFDATANONE")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 elif self.sex == chr(ELFFile.ELFDATA2LSB):
77 self.sex = "<"
78 elif self.sex == chr(ELFFile.ELFDATA2MSB):
79 self.sex = ">"
80 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050081 raise NotELFFileError("Unknown self.sex")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082
83 def osAbi(self):
84 return ord(self.data[ELFFile.EI_OSABI])
85
86 def abiVersion(self):
87 return ord(self.data[ELFFile.EI_ABIVERSION])
88
89 def abiSize(self):
90 return self.bits
91
92 def isLittleEndian(self):
93 return self.sex == "<"
94
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050095 def isBigEndian(self):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096 return self.sex == ">"
97
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050098 def getShort(self, offset):
99 return struct.unpack_from(self.sex+"H", self.data, offset)[0]
100
101 def getWord(self, offset):
102 return struct.unpack_from(self.sex+"i", self.data, offset)[0]
103
104 def isDynamic(self):
105 """
106 Return True if there is a .interp segment (therefore dynamically
107 linked), otherwise False (statically linked).
108 """
109 offset = self.getWord(self.bits == 32 and 0x1C or 0x20)
110 size = self.getShort(self.bits == 32 and 0x2A or 0x36)
111 count = self.getShort(self.bits == 32 and 0x2C or 0x38)
112
113 for i in range(0, count):
114 p_type = self.getWord(offset + i * size)
115 if p_type == ELFFile.PT_INTERP:
116 return True
117 return False
118
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119 def machine(self):
120 """
121 We know the sex stored in self.sex and we
122 know the position
123 """
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500124 return self.getShort(ELFFile.E_MACHINE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125
126 def run_objdump(self, cmd, d):
127 import bb.process
128 import sys
129
130 if cmd in self.objdump_output:
131 return self.objdump_output[cmd]
132
133 objdump = d.getVar('OBJDUMP', True)
134
135 env = os.environ.copy()
136 env["LC_ALL"] = "C"
137 env["PATH"] = d.getVar('PATH', True)
138
139 try:
140 bb.note("%s %s %s" % (objdump, cmd, self.name))
141 self.objdump_output[cmd] = bb.process.run([objdump, cmd, self.name], env=env, shell=False)[0]
142 return self.objdump_output[cmd]
143 except Exception as e:
144 bb.note("%s %s %s failed: %s" % (objdump, cmd, self.name, e))
145 return ""
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500146
147if __name__ == "__main__":
148 import sys
149 elf = ELFFile(sys.argv[1])
150 elf.open()
151 print elf.isDynamic()