blob: fbe719d8eceb1ae93a48da410a42acba4abd01e8 [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 Williamsc0f7c042017-02-23 20:41:17 -060046 with open(self.name, "rb") as f:
47 # Read 4k which should cover most of the headers we're after
48 self.data = f.read(4096)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050049
50 if len(self.data) < ELFFile.EI_NIDENT + 4:
51 raise NotELFFileError("%s is not an ELF" % self.name)
52
Patrick Williamsc0f7c042017-02-23 20:41:17 -060053 self.my_assert(self.data[0], 0x7f)
54 self.my_assert(self.data[1], ord('E'))
55 self.my_assert(self.data[2], ord('L'))
56 self.my_assert(self.data[3], ord('F'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 if self.bits == 0:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060058 if self.data[ELFFile.EI_CLASS] == ELFFile.ELFCLASS32:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 self.bits = 32
Patrick Williamsc0f7c042017-02-23 20:41:17 -060060 elif self.data[ELFFile.EI_CLASS] == ELFFile.ELFCLASS64:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 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:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060066 self.my_assert(self.data[ELFFile.EI_CLASS], ELFFile.ELFCLASS32)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067 elif self.bits == 64:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060068 self.my_assert(self.data[ELFFile.EI_CLASS], ELFFile.ELFCLASS64)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050070 raise NotELFFileError("Must specify unknown, 32 or 64 bit size.")
Patrick Williamsc0f7c042017-02-23 20:41:17 -060071 self.my_assert(self.data[ELFFile.EI_VERSION], ELFFile.EV_CURRENT)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072
73 self.sex = self.data[ELFFile.EI_DATA]
Patrick Williamsc0f7c042017-02-23 20:41:17 -060074 if self.sex == ELFFile.ELFDATANONE:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050075 raise NotELFFileError("self.sex == ELFDATANONE")
Patrick Williamsc0f7c042017-02-23 20:41:17 -060076 elif self.sex == ELFFile.ELFDATA2LSB:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 self.sex = "<"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060078 elif self.sex == ELFFile.ELFDATA2MSB:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079 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):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060084 return self.data[ELFFile.EI_OSABI]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
86 def abiVersion(self):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060087 return self.data[ELFFile.EI_ABIVERSION]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088
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
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600147def elf_machine_to_string(machine):
148 """
149 Return the name of a given ELF e_machine field or the hex value as a string
150 if it isn't recognised.
151 """
152 try:
153 return {
154 0x02: "SPARC",
155 0x03: "x86",
156 0x08: "MIPS",
157 0x14: "PowerPC",
158 0x28: "ARM",
159 0x2A: "SuperH",
160 0x32: "IA-64",
161 0x3E: "x86-64",
162 0xB7: "AArch64"
163 }[machine]
164 except:
165 return "Unknown (%s)" % repr(machine)
166
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500167if __name__ == "__main__":
168 import sys
169 elf = ELFFile(sys.argv[1])
170 elf.open()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600171 print(elf.isDynamic())