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 | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 46 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 49 | |
| 50 | if len(self.data) < ELFFile.EI_NIDENT + 4: |
| 51 | raise NotELFFileError("%s is not an ELF" % self.name) |
| 52 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 53 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | if self.bits == 0: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 58 | if self.data[ELFFile.EI_CLASS] == ELFFile.ELFCLASS32: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | self.bits = 32 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 60 | elif self.data[ELFFile.EI_CLASS] == ELFFile.ELFCLASS64: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 66 | self.my_assert(self.data[ELFFile.EI_CLASS], ELFFile.ELFCLASS32) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | elif self.bits == 64: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 68 | self.my_assert(self.data[ELFFile.EI_CLASS], ELFFile.ELFCLASS64) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 71 | self.my_assert(self.data[ELFFile.EI_VERSION], ELFFile.EV_CURRENT) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | |
| 73 | self.sex = self.data[ELFFile.EI_DATA] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 74 | if self.sex == ELFFile.ELFDATANONE: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 75 | raise NotELFFileError("self.sex == ELFDATANONE") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 76 | elif self.sex == ELFFile.ELFDATA2LSB: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | self.sex = "<" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 78 | elif self.sex == ELFFile.ELFDATA2MSB: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 84 | return self.data[ELFFile.EI_OSABI] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | |
| 86 | def abiVersion(self): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 87 | return self.data[ELFFile.EI_ABIVERSION] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 147 | def 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 167 | if __name__ == "__main__": |
| 168 | import sys |
| 169 | elf = ELFFile(sys.argv[1]) |
| 170 | elf.open() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 171 | print(elf.isDynamic()) |