Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
| 4 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 5 | import os, struct, mmap |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 6 | |
| 7 | class NotELFFileError(Exception): |
| 8 | pass |
| 9 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | class ELFFile: |
| 11 | EI_NIDENT = 16 |
| 12 | |
| 13 | EI_CLASS = 4 |
| 14 | EI_DATA = 5 |
| 15 | EI_VERSION = 6 |
| 16 | EI_OSABI = 7 |
| 17 | EI_ABIVERSION = 8 |
| 18 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 19 | E_MACHINE = 0x12 |
| 20 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | # possible values for EI_CLASS |
| 22 | ELFCLASSNONE = 0 |
| 23 | ELFCLASS32 = 1 |
| 24 | ELFCLASS64 = 2 |
| 25 | |
| 26 | # possible value for EI_VERSION |
| 27 | EV_CURRENT = 1 |
| 28 | |
| 29 | # possible values for EI_DATA |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 30 | EI_DATA_NONE = 0 |
| 31 | EI_DATA_LSB = 1 |
| 32 | EI_DATA_MSB = 2 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 34 | PT_INTERP = 3 |
| 35 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | def my_assert(self, expectation, result): |
| 37 | if not expectation == result: |
| 38 | #print "'%x','%x' %s" % (ord(expectation), ord(result), self.name) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 39 | raise NotELFFileError("%s is not an ELF" % self.name) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 41 | def __init__(self, name): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | self.name = name |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | self.objdump_output = {} |
| 44 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 45 | # Context Manager functions to close the mmap explicitly |
| 46 | def __enter__(self): |
| 47 | return self |
| 48 | |
| 49 | def __exit__(self, exc_type, exc_value, traceback): |
| 50 | self.data.close() |
| 51 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | def open(self): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 53 | with open(self.name, "rb") as f: |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 54 | try: |
| 55 | self.data = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) |
| 56 | except ValueError: |
| 57 | # This means the file is empty |
| 58 | raise NotELFFileError("%s is empty" % self.name) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 59 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 60 | # Check the file has the minimum number of ELF table entries |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 61 | if len(self.data) < ELFFile.EI_NIDENT + 4: |
| 62 | raise NotELFFileError("%s is not an ELF" % self.name) |
| 63 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 64 | # ELF header |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 65 | self.my_assert(self.data[0], 0x7f) |
| 66 | self.my_assert(self.data[1], ord('E')) |
| 67 | self.my_assert(self.data[2], ord('L')) |
| 68 | self.my_assert(self.data[3], ord('F')) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 69 | if self.data[ELFFile.EI_CLASS] == ELFFile.ELFCLASS32: |
| 70 | self.bits = 32 |
| 71 | elif self.data[ELFFile.EI_CLASS] == ELFFile.ELFCLASS64: |
| 72 | self.bits = 64 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | else: |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 74 | # Not 32-bit or 64.. lets assert |
| 75 | raise NotELFFileError("ELF but not 32 or 64 bit.") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 76 | self.my_assert(self.data[ELFFile.EI_VERSION], ELFFile.EV_CURRENT) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 78 | self.endian = self.data[ELFFile.EI_DATA] |
| 79 | if self.endian not in (ELFFile.EI_DATA_LSB, ELFFile.EI_DATA_MSB): |
| 80 | raise NotELFFileError("Unexpected EI_DATA %x" % self.endian) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | |
| 82 | def osAbi(self): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 83 | return self.data[ELFFile.EI_OSABI] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 84 | |
| 85 | def abiVersion(self): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 86 | return self.data[ELFFile.EI_ABIVERSION] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | |
| 88 | def abiSize(self): |
| 89 | return self.bits |
| 90 | |
| 91 | def isLittleEndian(self): |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 92 | return self.endian == ELFFile.EI_DATA_LSB |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 94 | def isBigEndian(self): |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 95 | return self.endian == ELFFile.EI_DATA_MSB |
| 96 | |
| 97 | def getStructEndian(self): |
| 98 | return {ELFFile.EI_DATA_LSB: "<", |
| 99 | ELFFile.EI_DATA_MSB: ">"}[self.endian] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 101 | def getShort(self, offset): |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 102 | return struct.unpack_from(self.getStructEndian() + "H", self.data, offset)[0] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 103 | |
| 104 | def getWord(self, offset): |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 105 | return struct.unpack_from(self.getStructEndian() + "i", self.data, offset)[0] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 106 | |
| 107 | def isDynamic(self): |
| 108 | """ |
| 109 | Return True if there is a .interp segment (therefore dynamically |
| 110 | linked), otherwise False (statically linked). |
| 111 | """ |
| 112 | offset = self.getWord(self.bits == 32 and 0x1C or 0x20) |
| 113 | size = self.getShort(self.bits == 32 and 0x2A or 0x36) |
| 114 | count = self.getShort(self.bits == 32 and 0x2C or 0x38) |
| 115 | |
| 116 | for i in range(0, count): |
| 117 | p_type = self.getWord(offset + i * size) |
| 118 | if p_type == ELFFile.PT_INTERP: |
| 119 | return True |
| 120 | return False |
| 121 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 122 | def machine(self): |
| 123 | """ |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 124 | We know the endian stored in self.endian and we |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | know the position |
| 126 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 127 | return self.getShort(ELFFile.E_MACHINE) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | |
| 129 | def run_objdump(self, cmd, d): |
| 130 | import bb.process |
| 131 | import sys |
| 132 | |
| 133 | if cmd in self.objdump_output: |
| 134 | return self.objdump_output[cmd] |
| 135 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 136 | objdump = d.getVar('OBJDUMP') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 137 | |
| 138 | env = os.environ.copy() |
| 139 | env["LC_ALL"] = "C" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 140 | env["PATH"] = d.getVar('PATH') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | |
| 142 | try: |
| 143 | bb.note("%s %s %s" % (objdump, cmd, self.name)) |
| 144 | self.objdump_output[cmd] = bb.process.run([objdump, cmd, self.name], env=env, shell=False)[0] |
| 145 | return self.objdump_output[cmd] |
| 146 | except Exception as e: |
| 147 | bb.note("%s %s %s failed: %s" % (objdump, cmd, self.name, e)) |
| 148 | return "" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 149 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 150 | def elf_machine_to_string(machine): |
| 151 | """ |
| 152 | Return the name of a given ELF e_machine field or the hex value as a string |
| 153 | if it isn't recognised. |
| 154 | """ |
| 155 | try: |
| 156 | return { |
| 157 | 0x02: "SPARC", |
| 158 | 0x03: "x86", |
| 159 | 0x08: "MIPS", |
| 160 | 0x14: "PowerPC", |
| 161 | 0x28: "ARM", |
| 162 | 0x2A: "SuperH", |
| 163 | 0x32: "IA-64", |
| 164 | 0x3E: "x86-64", |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 165 | 0xB7: "AArch64", |
| 166 | 0xF7: "BPF" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 167 | }[machine] |
| 168 | except: |
| 169 | return "Unknown (%s)" % repr(machine) |
| 170 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 171 | if __name__ == "__main__": |
| 172 | import sys |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 173 | |
| 174 | with ELFFile(sys.argv[1]) as elf: |
| 175 | elf.open() |
| 176 | print(elf.isDynamic()) |