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