Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 1 | import os, struct |
| 2 | |
| 3 | class NotELFFileError(Exception): |
| 4 | pass |
| 5 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | class 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 15 | E_MACHINE = 0x12 |
| 16 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | # 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 30 | PT_INTERP = 3 |
| 31 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | def my_assert(self, expectation, result): |
| 33 | if not expectation == result: |
| 34 | #print "'%x','%x' %s" % (ord(expectation), ord(result), self.name) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 35 | raise NotELFFileError("%s is not an ELF" % self.name) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | |
| 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 43 | if not os.path.isfile(self.name): |
| 44 | raise NotELFFileError("%s is not a normal file" % self.name) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 46 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 64 | raise NotELFFileError("ELF but not 32 or 64 bit.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 70 | raise NotELFFileError("Must specify unknown, 32 or 64 bit size.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 75 | raise NotELFFileError("self.sex == ELFDATANONE") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | elif self.sex == chr(ELFFile.ELFDATA2LSB): |
| 77 | self.sex = "<" |
| 78 | elif self.sex == chr(ELFFile.ELFDATA2MSB): |
| 79 | self.sex = ">" |
| 80 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 81 | raise NotELFFileError("Unknown self.sex") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | |
| 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 95 | def isBigEndian(self): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | return self.sex == ">" |
| 97 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 98 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 119 | def machine(self): |
| 120 | """ |
| 121 | We know the sex stored in self.sex and we |
| 122 | know the position |
| 123 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 124 | return self.getShort(ELFFile.E_MACHINE) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | |
| 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 146 | |
| 147 | if __name__ == "__main__": |
| 148 | import sys |
| 149 | elf = ELFFile(sys.argv[1]) |
| 150 | elf.open() |
| 151 | print elf.isDynamic() |