Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | class ELFFile: |
| 2 | EI_NIDENT = 16 |
| 3 | |
| 4 | EI_CLASS = 4 |
| 5 | EI_DATA = 5 |
| 6 | EI_VERSION = 6 |
| 7 | EI_OSABI = 7 |
| 8 | EI_ABIVERSION = 8 |
| 9 | |
| 10 | # possible values for EI_CLASS |
| 11 | ELFCLASSNONE = 0 |
| 12 | ELFCLASS32 = 1 |
| 13 | ELFCLASS64 = 2 |
| 14 | |
| 15 | # possible value for EI_VERSION |
| 16 | EV_CURRENT = 1 |
| 17 | |
| 18 | # possible values for EI_DATA |
| 19 | ELFDATANONE = 0 |
| 20 | ELFDATA2LSB = 1 |
| 21 | ELFDATA2MSB = 2 |
| 22 | |
| 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") |
| 27 | |
| 28 | def __init__(self, name, bits = 0): |
| 29 | self.name = name |
| 30 | self.bits = bits |
| 31 | self.objdump_output = {} |
| 32 | |
| 33 | def open(self): |
| 34 | self.file = file(self.name, "r") |
| 35 | self.data = self.file.read(ELFFile.EI_NIDENT+4) |
| 36 | |
| 37 | self.my_assert(len(self.data), ELFFile.EI_NIDENT+4) |
| 38 | self.my_assert(self.data[0], chr(0x7f) ) |
| 39 | self.my_assert(self.data[1], 'E') |
| 40 | self.my_assert(self.data[2], 'L') |
| 41 | self.my_assert(self.data[3], 'F') |
| 42 | if self.bits == 0: |
| 43 | if self.data[ELFFile.EI_CLASS] == chr(ELFFile.ELFCLASS32): |
| 44 | self.bits = 32 |
| 45 | elif self.data[ELFFile.EI_CLASS] == chr(ELFFile.ELFCLASS64): |
| 46 | self.bits = 64 |
| 47 | else: |
| 48 | # Not 32-bit or 64.. lets assert |
| 49 | raise Exception("ELF but not 32 or 64 bit.") |
| 50 | elif self.bits == 32: |
| 51 | self.my_assert(self.data[ELFFile.EI_CLASS], chr(ELFFile.ELFCLASS32)) |
| 52 | elif self.bits == 64: |
| 53 | self.my_assert(self.data[ELFFile.EI_CLASS], chr(ELFFile.ELFCLASS64)) |
| 54 | else: |
| 55 | raise Exception("Must specify unknown, 32 or 64 bit size.") |
| 56 | self.my_assert(self.data[ELFFile.EI_VERSION], chr(ELFFile.EV_CURRENT) ) |
| 57 | |
| 58 | self.sex = self.data[ELFFile.EI_DATA] |
| 59 | if self.sex == chr(ELFFile.ELFDATANONE): |
| 60 | raise Exception("self.sex == ELFDATANONE") |
| 61 | elif self.sex == chr(ELFFile.ELFDATA2LSB): |
| 62 | self.sex = "<" |
| 63 | elif self.sex == chr(ELFFile.ELFDATA2MSB): |
| 64 | self.sex = ">" |
| 65 | else: |
| 66 | raise Exception("Unknown self.sex") |
| 67 | |
| 68 | def osAbi(self): |
| 69 | return ord(self.data[ELFFile.EI_OSABI]) |
| 70 | |
| 71 | def abiVersion(self): |
| 72 | return ord(self.data[ELFFile.EI_ABIVERSION]) |
| 73 | |
| 74 | def abiSize(self): |
| 75 | return self.bits |
| 76 | |
| 77 | def isLittleEndian(self): |
| 78 | return self.sex == "<" |
| 79 | |
| 80 | def isBigEngian(self): |
| 81 | return self.sex == ">" |
| 82 | |
| 83 | def machine(self): |
| 84 | """ |
| 85 | We know the sex stored in self.sex and we |
| 86 | know the position |
| 87 | """ |
| 88 | import struct |
| 89 | (a,) = struct.unpack(self.sex+"H", self.data[18:20]) |
| 90 | return a |
| 91 | |
| 92 | def run_objdump(self, cmd, d): |
| 93 | import bb.process |
| 94 | import sys |
| 95 | |
| 96 | if cmd in self.objdump_output: |
| 97 | return self.objdump_output[cmd] |
| 98 | |
| 99 | objdump = d.getVar('OBJDUMP', True) |
| 100 | |
| 101 | env = os.environ.copy() |
| 102 | env["LC_ALL"] = "C" |
| 103 | env["PATH"] = d.getVar('PATH', True) |
| 104 | |
| 105 | try: |
| 106 | bb.note("%s %s %s" % (objdump, cmd, self.name)) |
| 107 | self.objdump_output[cmd] = bb.process.run([objdump, cmd, self.name], env=env, shell=False)[0] |
| 108 | return self.objdump_output[cmd] |
| 109 | except Exception as e: |
| 110 | bb.note("%s %s %s failed: %s" % (objdump, cmd, self.name, e)) |
| 111 | return "" |